]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
0d223281ba25ec2fa978648bc05ced586a1ee76a
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 | action | monitor | exec }\n"
201 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n"
202 " -o[neline] | -j[son] | -p[retty] | -c[olor]\n"
203 " -b[atch] [filename] | -n[etns] name |\n"
204 " -nm | -nam[es] | { -cf | -conf } path }\n");
207 static int do_cmd(int argc
, char **argv
, void *buf
, size_t buflen
)
209 if (matches(*argv
, "qdisc") == 0)
210 return do_qdisc(argc
-1, argv
+1);
211 if (matches(*argv
, "class") == 0)
212 return do_class(argc
-1, argv
+1);
213 if (matches(*argv
, "filter") == 0)
214 return do_filter(argc
-1, argv
+1, buf
, buflen
);
215 if (matches(*argv
, "actions") == 0)
216 return do_action(argc
-1, argv
+1, buf
, buflen
);
217 if (matches(*argv
, "monitor") == 0)
218 return do_tcmonitor(argc
-1, argv
+1);
219 if (matches(*argv
, "exec") == 0)
220 return do_exec(argc
-1, argv
+1);
221 if (matches(*argv
, "help") == 0) {
226 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
231 #define TC_MAX_SUBC 10
232 static bool batchsize_enabled(int argc
, char *argv
[])
236 char *subc
[TC_MAX_SUBC
];
238 { "filter", { "add", "delete", "change", "replace", NULL
} },
239 { "actions", { "add", "change", "replace", NULL
} },
248 for (iter
= table
; iter
->c
; iter
++) {
249 if (matches(argv
[0], iter
->c
))
251 for (i
= 0; i
< TC_MAX_SUBC
; i
++) {
253 if (s
&& matches(argv
[1], s
) == 0)
262 struct batch_buf
*next
;
263 char buf
[16420]; /* sizeof (struct nlmsghdr) +
264 max(sizeof (struct tcmsg) +
265 sizeof (struct tcamsg)) +
269 static struct batch_buf
*get_batch_buf(struct batch_buf
**pool
,
270 struct batch_buf
**head
,
271 struct batch_buf
**tail
)
273 struct batch_buf
*buf
;
276 buf
= calloc(1, sizeof(struct batch_buf
));
279 *pool
= (*pool
)->next
;
280 memset(buf
, 0, sizeof(struct batch_buf
));
293 static void put_batch_bufs(struct batch_buf
**pool
,
294 struct batch_buf
**head
,
295 struct batch_buf
**tail
)
297 if (*head
== NULL
|| *tail
== NULL
)
303 (*tail
)->next
= *pool
;
310 static void free_batch_bufs(struct batch_buf
**pool
)
312 struct batch_buf
*buf
;
314 for (buf
= *pool
; buf
!= NULL
; buf
= *pool
) {
321 static int batch(const char *name
)
323 struct batch_buf
*head
= NULL
, *tail
= NULL
, *buf_pool
= NULL
;
324 char *largv
[100], *largv_next
[100];
325 char *line
, *line_next
= NULL
;
326 bool bs_enabled_next
= false;
327 bool bs_enabled
= false;
328 bool lastline
= false;
329 int largc
, largc_next
;
330 bool bs_enabled_saved
;
337 if (name
&& strcmp(name
, "-") != 0) {
338 if (freopen(name
, "r", stdin
) == NULL
) {
340 "Cannot open file \"%s\" for reading: %s\n",
341 name
, strerror(errno
));
348 if (rtnl_open(&rth
, 0) < 0) {
349 fprintf(stderr
, "Cannot open rtnetlink\n");
354 if (getcmdline(&line
, &len
, stdin
) == -1)
356 largc
= makeargs(line
, largv
, 100);
357 bs_enabled
= batchsize_enabled(largc
, largv
);
358 bs_enabled_saved
= bs_enabled
;
360 if (getcmdline(&line_next
, &len
, stdin
) == -1)
363 largc_next
= makeargs(line_next
, largv_next
, 100);
364 bs_enabled_next
= batchsize_enabled(largc_next
, largv_next
);
366 struct batch_buf
*buf
;
368 buf
= get_batch_buf(&buf_pool
, &head
, &tail
);
371 "failed to allocate batch_buf\n");
378 * In batch mode, if we haven't accumulated enough commands
379 * and this is not the last command and this command & next
380 * command both support the batchsize feature, don't send the
381 * message immediately.
383 if (!lastline
&& bs_enabled
&& bs_enabled_next
384 && batchsize
!= MSG_IOV_MAX
)
392 bs_enabled_saved
= bs_enabled
;
393 bs_enabled
= bs_enabled_next
;
394 bs_enabled_next
= false;
398 memcpy(largv
, largv_next
, largc
* sizeof(char *));
399 continue; /* blank line */
402 ret
= do_cmd(largc
, largv
, tail
== NULL
? NULL
: tail
->buf
,
403 tail
== NULL
? 0 : sizeof(tail
->buf
));
405 fprintf(stderr
, "Command failed %s:%d\n", name
,
412 memcpy(largv
, largv_next
, largc
* sizeof(char *));
414 if (send
&& bs_enabled_saved
) {
415 struct iovec
*iov
, *iovs
;
416 struct batch_buf
*buf
;
419 iov
= iovs
= malloc(batchsize
* sizeof(struct iovec
));
420 for (buf
= head
; buf
!= NULL
; buf
= buf
->next
, ++iov
) {
421 n
= (struct nlmsghdr
*)&buf
->buf
;
423 iov
->iov_len
= n
->nlmsg_len
;
426 ret
= rtnl_talk_iov(&rth
, iovs
, batchsize
, NULL
);
428 fprintf(stderr
, "Command failed %s:%d\n", name
,
429 cmdlineno
- (batchsize
+ ret
) - 1);
432 put_batch_bufs(&buf_pool
, &head
, &tail
);
438 free_batch_bufs(&buf_pool
);
447 int main(int argc
, char **argv
)
450 char *batch_file
= NULL
;
453 if (argv
[1][0] != '-')
455 if (matches(argv
[1], "-stats") == 0 ||
456 matches(argv
[1], "-statistics") == 0) {
458 } else if (matches(argv
[1], "-details") == 0) {
460 } else if (matches(argv
[1], "-raw") == 0) {
462 } else if (matches(argv
[1], "-pretty") == 0) {
464 } else if (matches(argv
[1], "-graph") == 0) {
466 } else if (matches(argv
[1], "-Version") == 0) {
467 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
469 } else if (matches(argv
[1], "-iec") == 0) {
471 } else if (matches(argv
[1], "-help") == 0) {
474 } else if (matches(argv
[1], "-force") == 0) {
476 } else if (matches(argv
[1], "-batch") == 0) {
480 batch_file
= argv
[1];
481 } else if (matches(argv
[1], "-netns") == 0) {
483 if (netns_switch(argv
[1]))
485 } else if (matches(argv
[1], "-names") == 0 ||
486 matches(argv
[1], "-nm") == 0) {
488 } else if (matches(argv
[1], "-cf") == 0 ||
489 matches(argv
[1], "-conf") == 0) {
492 } else if (matches(argv
[1], "-color") == 0) {
494 } else if (matches(argv
[1], "-timestamp") == 0) {
496 } else if (matches(argv
[1], "-tshort") == 0) {
499 } else if (matches(argv
[1], "-json") == 0) {
501 } else if (matches(argv
[1], "-oneline") == 0) {
505 "Option \"%s\" is unknown, try \"tc -help\".\n",
512 _SL_
= oneline
? "\\" : "\n";
518 return batch(batch_file
);
526 if (rtnl_open(&rth
, 0) < 0) {
527 fprintf(stderr
, "Cannot open rtnetlink\n");
531 if (use_names
&& cls_names_init(conf_file
)) {
536 ret
= do_cmd(argc
-1, argv
+1, NULL
, 0);