]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
1f23971ae4b9a1ed86a48a1498664c3ed102de0a
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"
48 static char *conf_file
;
50 struct rtnl_handle rth
;
52 static void *BODY
; /* cached handle dlopen(NULL) */
53 static struct qdisc_util
*qdisc_list
;
54 static struct filter_util
*filter_list
;
56 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
59 if (opt
&& RTA_PAYLOAD(opt
))
60 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
61 (unsigned int) RTA_PAYLOAD(opt
));
65 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
,
66 struct nlmsghdr
*n
, const char *dev
)
70 "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n",
77 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
79 if (opt
&& RTA_PAYLOAD(opt
))
80 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
81 fhandle
, (unsigned int) RTA_PAYLOAD(opt
));
83 fprintf(f
, "fh %08x ", fhandle
);
87 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
,
88 int argc
, char **argv
, struct nlmsghdr
*n
)
94 "Unknown filter \"%s\", hence option \"%s\" is unparsable\n",
99 struct tcmsg
*t
= NLMSG_DATA(n
);
101 if (get_u32(&handle
, fhandle
, 16)) {
102 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
105 t
->tcm_handle
= handle
;
110 struct qdisc_util
*get_qdisc_kind(const char *str
)
114 struct qdisc_util
*q
;
116 for (q
= qdisc_list
; q
; q
= q
->next
)
117 if (strcmp(q
->id
, str
) == 0)
120 snprintf(buf
, sizeof(buf
), "%s/q_%s.so", get_tc_lib(), str
);
121 dlh
= dlopen(buf
, RTLD_LAZY
);
123 /* look in current binary, only open once */
126 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
132 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
138 q
->next
= qdisc_list
;
143 q
= calloc(1, sizeof(*q
));
146 q
->parse_qopt
= parse_noqopt
;
147 q
->print_qopt
= print_noqopt
;
154 struct filter_util
*get_filter_kind(const char *str
)
158 struct filter_util
*q
;
160 for (q
= filter_list
; q
; q
= q
->next
)
161 if (strcmp(q
->id
, str
) == 0)
164 snprintf(buf
, sizeof(buf
), "%s/f_%s.so", get_tc_lib(), str
);
165 dlh
= dlopen(buf
, RTLD_LAZY
);
169 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
175 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
181 q
->next
= filter_list
;
185 q
= calloc(1, sizeof(*q
));
187 strncpy(q
->id
, str
, 15);
188 q
->parse_fopt
= parse_nofopt
;
189 q
->print_fopt
= print_nofopt
;
195 static void usage(void)
198 "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
199 " tc [-force] -batch filename\n"
200 "where OBJECT := { qdisc | class | filter | chain |\n"
201 " action | monitor | exec }\n"
202 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n"
203 " -o[neline] | -j[son] | -p[retty] | -c[olor]\n"
204 " -b[atch] [filename] | -n[etns] name | -N[umeric] |\n"
205 " -nm | -nam[es] | { -cf | -conf } path }\n");
208 static int do_cmd(int argc
, char **argv
, void *buf
, size_t buflen
)
210 if (matches(*argv
, "qdisc") == 0)
211 return do_qdisc(argc
-1, argv
+1);
212 if (matches(*argv
, "class") == 0)
213 return do_class(argc
-1, argv
+1);
214 if (matches(*argv
, "filter") == 0)
215 return do_filter(argc
-1, argv
+1, buf
, buflen
);
216 if (matches(*argv
, "chain") == 0)
217 return do_chain(argc
-1, argv
+1, buf
, buflen
);
218 if (matches(*argv
, "actions") == 0)
219 return do_action(argc
-1, argv
+1, buf
, buflen
);
220 if (matches(*argv
, "monitor") == 0)
221 return do_tcmonitor(argc
-1, argv
+1);
222 if (matches(*argv
, "exec") == 0)
223 return do_exec(argc
-1, argv
+1);
224 if (matches(*argv
, "help") == 0) {
229 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
234 #define TC_MAX_SUBC 10
235 static bool batchsize_enabled(int argc
, char *argv
[])
239 char *subc
[TC_MAX_SUBC
];
241 { "filter", { "add", "delete", "change", "replace", NULL
} },
242 { "actions", { "add", "change", "replace", NULL
} },
251 for (iter
= table
; iter
->c
; iter
++) {
252 if (matches(argv
[0], iter
->c
))
254 for (i
= 0; i
< TC_MAX_SUBC
; i
++) {
256 if (s
&& matches(argv
[1], s
) == 0)
265 struct batch_buf
*next
;
266 char buf
[16420]; /* sizeof (struct nlmsghdr) +
267 max(sizeof (struct tcmsg) +
268 sizeof (struct tcamsg)) +
272 static struct batch_buf
*get_batch_buf(struct batch_buf
**pool
,
273 struct batch_buf
**head
,
274 struct batch_buf
**tail
)
276 struct batch_buf
*buf
;
279 buf
= calloc(1, sizeof(struct batch_buf
));
282 *pool
= (*pool
)->next
;
283 memset(buf
, 0, sizeof(struct batch_buf
));
296 static void put_batch_bufs(struct batch_buf
**pool
,
297 struct batch_buf
**head
,
298 struct batch_buf
**tail
)
300 if (*head
== NULL
|| *tail
== NULL
)
306 (*tail
)->next
= *pool
;
313 static void free_batch_bufs(struct batch_buf
**pool
)
315 struct batch_buf
*buf
;
317 for (buf
= *pool
; buf
!= NULL
; buf
= *pool
) {
324 static int batch(const char *name
)
326 struct batch_buf
*head
= NULL
, *tail
= NULL
, *buf_pool
= NULL
;
327 char *largv
[100], *largv_next
[100];
328 char *line
, *line_next
= NULL
;
329 bool bs_enabled_next
= false;
330 bool bs_enabled
= false;
331 bool lastline
= false;
332 int largc
, largc_next
;
333 bool bs_enabled_saved
;
341 if (name
&& strcmp(name
, "-") != 0) {
342 if (freopen(name
, "r", stdin
) == NULL
) {
344 "Cannot open file \"%s\" for reading: %s\n",
345 name
, strerror(errno
));
352 if (rtnl_open(&rth
, 0) < 0) {
353 fprintf(stderr
, "Cannot open rtnetlink\n");
358 if (getcmdline(&line
, &len
, stdin
) == -1)
360 largc
= makeargs(line
, largv
, 100);
361 bs_enabled
= batchsize_enabled(largc
, largv
);
362 bs_enabled_saved
= bs_enabled
;
364 if (getcmdline(&line_next
, &len
, stdin
) == -1)
367 largc_next
= makeargs(line_next
, largv_next
, 100);
368 bs_enabled_next
= batchsize_enabled(largc_next
, largv_next
);
370 struct batch_buf
*buf
;
372 buf
= get_batch_buf(&buf_pool
, &head
, &tail
);
375 "failed to allocate batch_buf\n");
382 * In batch mode, if we haven't accumulated enough commands
383 * and this is not the last command and this command & next
384 * command both support the batchsize feature, don't send the
385 * message immediately.
387 if (!lastline
&& bs_enabled
&& bs_enabled_next
388 && batchsize
!= MSG_IOV_MAX
)
396 bs_enabled_saved
= bs_enabled
;
397 bs_enabled
= bs_enabled_next
;
398 bs_enabled_next
= false;
402 memcpy(largv
, largv_next
, largc
* sizeof(char *));
403 continue; /* blank line */
406 err
= do_cmd(largc
, largv
, tail
== NULL
? NULL
: tail
->buf
,
407 tail
== NULL
? 0 : sizeof(tail
->buf
));
410 fprintf(stderr
, "Command failed %s:%d\n", name
,
417 memcpy(largv
, largv_next
, largc
* sizeof(char *));
419 if (send
&& bs_enabled_saved
) {
420 struct iovec
*iov
, *iovs
;
421 struct batch_buf
*buf
;
424 iov
= iovs
= malloc(batchsize
* sizeof(struct iovec
));
425 for (buf
= head
; buf
!= NULL
; buf
= buf
->next
, ++iov
) {
426 n
= (struct nlmsghdr
*)&buf
->buf
;
428 iov
->iov_len
= n
->nlmsg_len
;
431 err
= rtnl_talk_iov(&rth
, iovs
, batchsize
, NULL
);
432 put_batch_bufs(&buf_pool
, &head
, &tail
);
435 fprintf(stderr
, "Command failed %s:%d\n", name
,
436 cmdlineno
- (batchsize
+ err
) - 1);
445 free_batch_bufs(&buf_pool
);
454 int main(int argc
, char **argv
)
457 char *batch_file
= NULL
;
460 if (argv
[1][0] != '-')
462 if (matches(argv
[1], "-stats") == 0 ||
463 matches(argv
[1], "-statistics") == 0) {
465 } else if (matches(argv
[1], "-details") == 0) {
467 } else if (matches(argv
[1], "-raw") == 0) {
469 } else if (matches(argv
[1], "-pretty") == 0) {
471 } else if (matches(argv
[1], "-graph") == 0) {
473 } else if (matches(argv
[1], "-Version") == 0) {
474 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
476 } else if (matches(argv
[1], "-iec") == 0) {
478 } else if (matches(argv
[1], "-help") == 0) {
481 } else if (matches(argv
[1], "-force") == 0) {
483 } else if (matches(argv
[1], "-batch") == 0) {
487 batch_file
= argv
[1];
488 } else if (matches(argv
[1], "-netns") == 0) {
490 if (netns_switch(argv
[1]))
492 } else if (matches(argv
[1], "-Numeric") == 0) {
494 } else if (matches(argv
[1], "-names") == 0 ||
495 matches(argv
[1], "-nm") == 0) {
497 } else if (matches(argv
[1], "-cf") == 0 ||
498 matches(argv
[1], "-conf") == 0) {
501 } else if (matches_color(argv
[1], &color
)) {
502 } else if (matches(argv
[1], "-timestamp") == 0) {
504 } else if (matches(argv
[1], "-tshort") == 0) {
507 } else if (matches(argv
[1], "-json") == 0) {
509 } else if (matches(argv
[1], "-oneline") == 0) {
513 "Option \"%s\" is unknown, try \"tc -help\".\n",
520 _SL_
= oneline
? "\\" : "\n";
522 check_enable_color(color
, json
);
525 return batch(batch_file
);
533 if (rtnl_open(&rth
, 0) < 0) {
534 fprintf(stderr
, "Cannot open rtnetlink\n");
538 if (use_names
&& cls_names_init(conf_file
)) {
543 ret
= do_cmd(argc
-1, argv
+1, NULL
, 0);