]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
2 * tc.c "tc" utility frontend.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
30 #include "tc_common.h"
31 #include "namespace.h"
46 static char *conf_file
;
48 struct rtnl_handle rth
;
50 static void *BODY
; /* cached handle dlopen(NULL) */
51 static struct qdisc_util
*qdisc_list
;
52 static struct filter_util
*filter_list
;
54 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
57 if (opt
&& RTA_PAYLOAD(opt
))
58 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
59 (unsigned int) RTA_PAYLOAD(opt
));
63 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
, const char *dev
)
66 fprintf(stderr
, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
72 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
74 if (opt
&& RTA_PAYLOAD(opt
))
75 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
76 fhandle
, (unsigned int) RTA_PAYLOAD(opt
));
78 fprintf(f
, "fh %08x ", fhandle
);
82 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
, int argc
, char **argv
, struct nlmsghdr
*n
)
87 fprintf(stderr
, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
91 struct tcmsg
*t
= NLMSG_DATA(n
);
93 if (get_u32(&handle
, fhandle
, 16)) {
94 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
97 t
->tcm_handle
= handle
;
102 struct qdisc_util
*get_qdisc_kind(const char *str
)
106 struct qdisc_util
*q
;
108 for (q
= qdisc_list
; q
; q
= q
->next
)
109 if (strcmp(q
->id
, str
) == 0)
112 snprintf(buf
, sizeof(buf
), "%s/q_%s.so", get_tc_lib(), str
);
113 dlh
= dlopen(buf
, RTLD_LAZY
);
115 /* look in current binary, only open once */
118 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
124 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
130 q
->next
= qdisc_list
;
135 q
= calloc(1, sizeof(*q
));
138 q
->parse_qopt
= parse_noqopt
;
139 q
->print_qopt
= print_noqopt
;
146 struct filter_util
*get_filter_kind(const char *str
)
150 struct filter_util
*q
;
152 for (q
= filter_list
; q
; q
= q
->next
)
153 if (strcmp(q
->id
, str
) == 0)
156 snprintf(buf
, sizeof(buf
), "%s/f_%s.so", get_tc_lib(), str
);
157 dlh
= dlopen(buf
, RTLD_LAZY
);
161 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
167 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
173 q
->next
= filter_list
;
177 q
= calloc(1, sizeof(*q
));
179 strncpy(q
->id
, str
, 15);
180 q
->parse_fopt
= parse_nofopt
;
181 q
->print_fopt
= print_nofopt
;
187 static void usage(void)
189 fprintf(stderr
, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
190 " tc [-force] -batch filename\n"
191 "where OBJECT := { qdisc | class | filter | action | monitor | exec }\n"
192 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] | -n[etns] name |\n"
193 " -nm | -nam[es] | { -cf | -conf } path } | -j[son]\n");
196 static int do_cmd(int argc
, char **argv
, void *buf
, size_t buflen
)
198 if (matches(*argv
, "qdisc") == 0)
199 return do_qdisc(argc
-1, argv
+1);
200 if (matches(*argv
, "class") == 0)
201 return do_class(argc
-1, argv
+1);
202 if (matches(*argv
, "filter") == 0)
203 return do_filter(argc
-1, argv
+1, buf
, buflen
);
204 if (matches(*argv
, "actions") == 0)
205 return do_action(argc
-1, argv
+1, buf
, buflen
);
206 if (matches(*argv
, "monitor") == 0)
207 return do_tcmonitor(argc
-1, argv
+1);
208 if (matches(*argv
, "exec") == 0)
209 return do_exec(argc
-1, argv
+1);
210 if (matches(*argv
, "help") == 0) {
215 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
220 #define TC_MAX_SUBC 10
221 static bool batchsize_enabled(int argc
, char *argv
[])
225 char *subc
[TC_MAX_SUBC
];
227 {"filter", {"add", "delete", "change", "replace", NULL
}},
228 {"actions", {"add", "change", "replace", NULL
}},
237 for (iter
= table
; iter
->c
; iter
++) {
238 if (matches(argv
[0], iter
->c
))
240 for (i
= 0; i
< TC_MAX_SUBC
; i
++) {
242 if (s
&& matches(argv
[1], s
) == 0)
251 struct batch_buf
*next
;
252 char buf
[16420]; /* sizeof (struct nlmsghdr) +
253 max(sizeof (struct tcmsg) +
254 sizeof (struct tcamsg)) +
258 static struct batch_buf
*get_batch_buf(struct batch_buf
**pool
,
259 struct batch_buf
**head
,
260 struct batch_buf
**tail
)
262 struct batch_buf
*buf
;
265 buf
= calloc(1, sizeof (struct batch_buf
));
268 *pool
= (*pool
)->next
;
269 memset(buf
, 0, sizeof (struct batch_buf
));
282 static void put_batch_bufs(struct batch_buf
**pool
,
283 struct batch_buf
**head
,
284 struct batch_buf
**tail
)
286 if (*head
== NULL
|| *tail
== NULL
)
292 (*tail
)->next
= *pool
;
299 static void free_batch_bufs(struct batch_buf
**pool
)
301 struct batch_buf
*buf
;
303 for (buf
= *pool
; buf
!= NULL
; buf
= *pool
) {
310 static int batch(const char *name
)
312 struct batch_buf
*head
= NULL
, *tail
= NULL
, *buf_pool
= NULL
;
313 char *largv
[100], *largv_next
[100];
314 char *line
, *line_next
= NULL
;
315 bool bs_enabled_next
= false;
316 bool bs_enabled
= false;
317 bool lastline
= false;
318 int largc
, largc_next
;
319 bool bs_enabled_saved
;
326 if (name
&& strcmp(name
, "-") != 0) {
327 if (freopen(name
, "r", stdin
) == NULL
) {
328 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s\n",
329 name
, strerror(errno
));
336 if (rtnl_open(&rth
, 0) < 0) {
337 fprintf(stderr
, "Cannot open rtnetlink\n");
342 if (getcmdline(&line
, &len
, stdin
) == -1)
344 largc
= makeargs(line
, largv
, 100);
345 bs_enabled
= batchsize_enabled(largc
, largv
);
346 bs_enabled_saved
= bs_enabled
;
348 if (getcmdline(&line_next
, &len
, stdin
) == -1)
351 largc_next
= makeargs(line_next
, largv_next
, 100);
352 bs_enabled_next
= batchsize_enabled(largc_next
, largv_next
);
354 struct batch_buf
*buf
;
356 buf
= get_batch_buf(&buf_pool
, &head
, &tail
);
359 "failed to allocate batch_buf\n");
366 * In batch mode, if we haven't accumulated enough commands
367 * and this is not the last command and this command & next
368 * command both support the batchsize feature, don't send the
369 * message immediately.
371 if (!lastline
&& bs_enabled
&& bs_enabled_next
372 && batchsize
!= MSG_IOV_MAX
)
380 bs_enabled_saved
= bs_enabled
;
381 bs_enabled
= bs_enabled_next
;
382 bs_enabled_next
= false;
386 memcpy(largv
, largv_next
, largc
* sizeof(char *));
387 continue; /* blank line */
390 ret
= do_cmd(largc
, largv
, tail
== NULL
? NULL
: tail
->buf
,
391 tail
== NULL
? 0 : sizeof (tail
->buf
));
393 fprintf(stderr
, "Command failed %s:%d\n", name
,
400 memcpy(largv
, largv_next
, largc
* sizeof(char *));
402 if (send
&& bs_enabled_saved
) {
403 struct iovec
*iov
, *iovs
;
404 struct batch_buf
*buf
;
407 iov
= iovs
= malloc(batchsize
* sizeof (struct iovec
));
408 for (buf
= head
; buf
!= NULL
; buf
= buf
->next
, ++iov
) {
409 n
= (struct nlmsghdr
*)&buf
->buf
;
411 iov
->iov_len
= n
->nlmsg_len
;
414 ret
= rtnl_talk_iov(&rth
, iovs
, batchsize
, NULL
);
416 fprintf(stderr
, "Command failed %s:%d\n", name
,
417 cmdlineno
- (batchsize
+ ret
) - 1);
420 put_batch_bufs(&buf_pool
, &head
, &tail
);
426 free_batch_bufs(&buf_pool
);
435 int main(int argc
, char **argv
)
438 char *batch_file
= NULL
;
441 if (argv
[1][0] != '-')
443 if (matches(argv
[1], "-stats") == 0 ||
444 matches(argv
[1], "-statistics") == 0) {
446 } else if (matches(argv
[1], "-details") == 0) {
448 } else if (matches(argv
[1], "-raw") == 0) {
450 } else if (matches(argv
[1], "-pretty") == 0) {
452 } else if (matches(argv
[1], "-graph") == 0) {
454 } else if (matches(argv
[1], "-Version") == 0) {
455 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
457 } else if (matches(argv
[1], "-iec") == 0) {
459 } else if (matches(argv
[1], "-help") == 0) {
462 } else if (matches(argv
[1], "-force") == 0) {
464 } else if (matches(argv
[1], "-batch") == 0) {
468 batch_file
= argv
[1];
469 } else if (matches(argv
[1], "-netns") == 0) {
471 if (netns_switch(argv
[1]))
473 } else if (matches(argv
[1], "-names") == 0 ||
474 matches(argv
[1], "-nm") == 0) {
476 } else if (matches(argv
[1], "-cf") == 0 ||
477 matches(argv
[1], "-conf") == 0) {
480 } else if (matches(argv
[1], "-timestamp") == 0) {
482 } else if (matches(argv
[1], "-tshort") == 0) {
485 } else if (matches(argv
[1], "-json") == 0) {
488 fprintf(stderr
, "Option \"%s\" is unknown, try \"tc -help\".\n", argv
[1]);
495 return batch(batch_file
);
503 if (rtnl_open(&rth
, 0) < 0) {
504 fprintf(stderr
, "Cannot open rtnetlink\n");
508 if (use_names
&& cls_names_init(conf_file
)) {
513 ret
= do_cmd(argc
-1, argv
+1, NULL
, 0);