]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * tc.c "tc" utility frontend. | |
3 | * | |
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. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
11 | * Fixes: | |
12 | * | |
13 | * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults | |
14 | */ | |
15 | ||
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <unistd.h> | |
aba5acdf SH |
19 | #include <fcntl.h> |
20 | #include <dlfcn.h> | |
21 | #include <sys/socket.h> | |
22 | #include <netinet/in.h> | |
23 | #include <arpa/inet.h> | |
24 | #include <string.h> | |
25 | #include <errno.h> | |
26 | ||
27 | #include "SNAPSHOT.h" | |
28 | #include "utils.h" | |
29 | #include "tc_util.h" | |
30 | #include "tc_common.h" | |
67e1d73b | 31 | #include "namespace.h" |
aba5acdf | 32 | |
32a121cb SH |
33 | int show_stats; |
34 | int show_details; | |
35 | int show_raw; | |
36 | int show_pretty; | |
37 | int show_graph; | |
32a6fbe5 | 38 | int timestamp; |
44dcfe82 | 39 | |
32a121cb | 40 | int batch_mode; |
32a121cb SH |
41 | int use_iec; |
42 | int force; | |
43 | bool use_names; | |
c91d262f | 44 | int json; |
a233caa0 | 45 | int pretty; |
4612d04d VK |
46 | |
47 | static char *conf_file; | |
48 | ||
7901660a | 49 | struct rtnl_handle rth; |
aba5acdf | 50 | |
32a121cb SH |
51 | static void *BODY; /* cached handle dlopen(NULL) */ |
52 | static struct qdisc_util *qdisc_list; | |
53 | static struct filter_util *filter_list; | |
aba5acdf | 54 | |
ae665a52 | 55 | static int print_noqopt(struct qdisc_util *qu, FILE *f, |
1798c9d5 | 56 | struct rtattr *opt) |
aba5acdf SH |
57 | { |
58 | if (opt && RTA_PAYLOAD(opt)) | |
ae665a52 | 59 | fprintf(f, "[Unknown qdisc, optlen=%u] ", |
32a121cb | 60 | (unsigned int) RTA_PAYLOAD(opt)); |
aba5acdf SH |
61 | return 0; |
62 | } | |
63 | ||
927e3cfb | 64 | static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev) |
aba5acdf SH |
65 | { |
66 | if (argc) { | |
67 | fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv); | |
68 | return -1; | |
69 | } | |
70 | return 0; | |
71 | } | |
72 | ||
73 | static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle) | |
74 | { | |
75 | if (opt && RTA_PAYLOAD(opt)) | |
ae665a52 | 76 | fprintf(f, "fh %08x [Unknown filter, optlen=%u] ", |
32a121cb | 77 | fhandle, (unsigned int) RTA_PAYLOAD(opt)); |
aba5acdf SH |
78 | else if (fhandle) |
79 | fprintf(f, "fh %08x ", fhandle); | |
80 | return 0; | |
81 | } | |
82 | ||
83 | static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n) | |
84 | { | |
85 | __u32 handle; | |
86 | ||
87 | if (argc) { | |
88 | fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv); | |
89 | return -1; | |
90 | } | |
91 | if (fhandle) { | |
92 | struct tcmsg *t = NLMSG_DATA(n); | |
32a121cb | 93 | |
aba5acdf SH |
94 | if (get_u32(&handle, fhandle, 16)) { |
95 | fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle); | |
96 | return -1; | |
97 | } | |
98 | t->tcm_handle = handle; | |
99 | } | |
100 | return 0; | |
101 | } | |
102 | ||
4094db72 | 103 | struct qdisc_util *get_qdisc_kind(const char *str) |
aba5acdf SH |
104 | { |
105 | void *dlh; | |
106 | char buf[256]; | |
107 | struct qdisc_util *q; | |
108 | ||
109 | for (q = qdisc_list; q; q = q->next) | |
110 | if (strcmp(q->id, str) == 0) | |
111 | return q; | |
112 | ||
aa27f88c | 113 | snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str); |
aba5acdf | 114 | dlh = dlopen(buf, RTLD_LAZY); |
b7a45150 SH |
115 | if (!dlh) { |
116 | /* look in current binary, only open once */ | |
aba5acdf SH |
117 | dlh = BODY; |
118 | if (dlh == NULL) { | |
119 | dlh = BODY = dlopen(NULL, RTLD_LAZY); | |
120 | if (dlh == NULL) | |
121 | goto noexist; | |
122 | } | |
123 | } | |
124 | ||
95812b56 | 125 | snprintf(buf, sizeof(buf), "%s_qdisc_util", str); |
aba5acdf SH |
126 | q = dlsym(dlh, buf); |
127 | if (q == NULL) | |
128 | goto noexist; | |
129 | ||
130 | reg: | |
131 | q->next = qdisc_list; | |
132 | qdisc_list = q; | |
133 | return q; | |
134 | ||
135 | noexist: | |
f89bb021 | 136 | q = calloc(1, sizeof(*q)); |
aba5acdf | 137 | if (q) { |
f89bb021 | 138 | q->id = strdup(str); |
aba5acdf SH |
139 | q->parse_qopt = parse_noqopt; |
140 | q->print_qopt = print_noqopt; | |
141 | goto reg; | |
142 | } | |
143 | return q; | |
144 | } | |
145 | ||
146 | ||
4094db72 | 147 | struct filter_util *get_filter_kind(const char *str) |
aba5acdf SH |
148 | { |
149 | void *dlh; | |
150 | char buf[256]; | |
151 | struct filter_util *q; | |
152 | ||
153 | for (q = filter_list; q; q = q->next) | |
154 | if (strcmp(q->id, str) == 0) | |
155 | return q; | |
156 | ||
aa27f88c | 157 | snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str); |
aba5acdf SH |
158 | dlh = dlopen(buf, RTLD_LAZY); |
159 | if (dlh == NULL) { | |
160 | dlh = BODY; | |
161 | if (dlh == NULL) { | |
162 | dlh = BODY = dlopen(NULL, RTLD_LAZY); | |
163 | if (dlh == NULL) | |
164 | goto noexist; | |
165 | } | |
166 | } | |
167 | ||
95812b56 | 168 | snprintf(buf, sizeof(buf), "%s_filter_util", str); |
aba5acdf SH |
169 | q = dlsym(dlh, buf); |
170 | if (q == NULL) | |
171 | goto noexist; | |
172 | ||
173 | reg: | |
174 | q->next = filter_list; | |
175 | filter_list = q; | |
176 | return q; | |
aba5acdf | 177 | noexist: |
f89bb021 | 178 | q = calloc(1, sizeof(*q)); |
aba5acdf | 179 | if (q) { |
aba5acdf SH |
180 | strncpy(q->id, str, 15); |
181 | q->parse_fopt = parse_nofopt; | |
182 | q->print_fopt = print_nofopt; | |
183 | goto reg; | |
184 | } | |
185 | return q; | |
186 | } | |
187 | ||
c2f3a0f9 | 188 | static void usage(void) |
aba5acdf SH |
189 | { |
190 | fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n" | |
10494d27 | 191 | " tc [-force] -batch filename\n" |
32a121cb SH |
192 | "where OBJECT := { qdisc | class | filter | action | monitor | exec }\n" |
193 | " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] | -n[etns] name |\n" | |
c91d262f | 194 | " -nm | -nam[es] | { -cf | -conf } path } | -j[son]\n"); |
c2f3a0f9 | 195 | } |
196 | ||
485d0c60 | 197 | static int do_cmd(int argc, char **argv, void *buf, size_t buflen) |
c2f3a0f9 | 198 | { |
199 | if (matches(*argv, "qdisc") == 0) | |
200 | return do_qdisc(argc-1, argv+1); | |
c2f3a0f9 | 201 | if (matches(*argv, "class") == 0) |
202 | return do_class(argc-1, argv+1); | |
c2f3a0f9 | 203 | if (matches(*argv, "filter") == 0) |
485d0c60 | 204 | return do_filter(argc-1, argv+1, buf, buflen); |
c2f3a0f9 | 205 | if (matches(*argv, "actions") == 0) |
485d0c60 | 206 | return do_action(argc-1, argv+1, buf, buflen); |
5bec3484 JHS |
207 | if (matches(*argv, "monitor") == 0) |
208 | return do_tcmonitor(argc-1, argv+1); | |
4bd62446 DB |
209 | if (matches(*argv, "exec") == 0) |
210 | return do_exec(argc-1, argv+1); | |
c2f3a0f9 | 211 | if (matches(*argv, "help") == 0) { |
212 | usage(); | |
213 | return 0; | |
214 | } | |
ae665a52 SH |
215 | |
216 | fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", | |
c2f3a0f9 | 217 | *argv); |
044ebf35 | 218 | return -1; |
aba5acdf SH |
219 | } |
220 | ||
485d0c60 CM |
221 | #define TC_MAX_SUBC 10 |
222 | static bool batchsize_enabled(int argc, char *argv[]) | |
223 | { | |
224 | struct { | |
225 | char *c; | |
226 | char *subc[TC_MAX_SUBC]; | |
227 | } table[] = { | |
228 | {"filter", {"add", "delete", "change", "replace", NULL}}, | |
229 | {"actions", {"add", "change", "replace", NULL}}, | |
230 | { NULL }, | |
231 | }, *iter; | |
232 | char *s; | |
233 | int i; | |
234 | ||
235 | if (argc < 2) | |
236 | return false; | |
237 | ||
238 | for (iter = table; iter->c; iter++) { | |
239 | if (matches(argv[0], iter->c)) | |
240 | continue; | |
241 | for (i = 0; i < TC_MAX_SUBC; i++) { | |
242 | s = iter->subc[i]; | |
243 | if (s && matches(argv[1], s) == 0) | |
244 | return true; | |
245 | } | |
246 | } | |
247 | ||
248 | return false; | |
249 | } | |
250 | ||
251 | struct batch_buf { | |
252 | struct batch_buf *next; | |
253 | char buf[16420]; /* sizeof (struct nlmsghdr) + | |
254 | max(sizeof (struct tcmsg) + | |
255 | sizeof (struct tcamsg)) + | |
256 | MAX_MSG */ | |
257 | }; | |
258 | ||
259 | static struct batch_buf *get_batch_buf(struct batch_buf **pool, | |
260 | struct batch_buf **head, | |
261 | struct batch_buf **tail) | |
262 | { | |
263 | struct batch_buf *buf; | |
264 | ||
265 | if (*pool == NULL) | |
266 | buf = calloc(1, sizeof (struct batch_buf)); | |
267 | else { | |
268 | buf = *pool; | |
269 | *pool = (*pool)->next; | |
270 | memset(buf, 0, sizeof (struct batch_buf)); | |
271 | } | |
272 | ||
273 | if (*head == NULL) | |
274 | *head = *tail = buf; | |
275 | else { | |
276 | (*tail)->next = buf; | |
277 | (*tail) = buf; | |
278 | } | |
279 | ||
280 | return buf; | |
281 | } | |
282 | ||
283 | static void put_batch_bufs(struct batch_buf **pool, | |
284 | struct batch_buf **head, | |
285 | struct batch_buf **tail) | |
286 | { | |
287 | if (*head == NULL || *tail == NULL) | |
288 | return; | |
289 | ||
290 | if (*pool == NULL) | |
291 | *pool = *head; | |
292 | else { | |
293 | (*tail)->next = *pool; | |
294 | *pool = *head; | |
295 | } | |
296 | *head = NULL; | |
297 | *tail = NULL; | |
298 | } | |
299 | ||
300 | static void free_batch_bufs(struct batch_buf **pool) | |
301 | { | |
302 | struct batch_buf *buf; | |
303 | ||
304 | for (buf = *pool; buf != NULL; buf = *pool) { | |
305 | *pool = buf->next; | |
306 | free(buf); | |
307 | } | |
308 | *pool = NULL; | |
309 | } | |
310 | ||
c2f3a0f9 | 311 | static int batch(const char *name) |
aba5acdf | 312 | { |
485d0c60 CM |
313 | struct batch_buf *head = NULL, *tail = NULL, *buf_pool = NULL; |
314 | char *largv[100], *largv_next[100]; | |
315 | char *line, *line_next = NULL; | |
316 | bool bs_enabled_next = false; | |
317 | bool bs_enabled = false; | |
318 | bool lastline = false; | |
319 | int largc, largc_next; | |
320 | bool bs_enabled_saved; | |
321 | int batchsize = 0; | |
c2f3a0f9 | 322 | size_t len = 0; |
08856f02 | 323 | int ret = 0; |
485d0c60 | 324 | bool send; |
c2f3a0f9 | 325 | |
a3aa47a5 | 326 | batch_mode = 1; |
dd3e9085 | 327 | if (name && strcmp(name, "-") != 0) { |
c2f3a0f9 | 328 | if (freopen(name, "r", stdin) == NULL) { |
84d66882 | 329 | fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n", |
c2f3a0f9 | 330 | name, strerror(errno)); |
044ebf35 | 331 | return -1; |
aba5acdf | 332 | } |
c2f3a0f9 | 333 | } |
334 | ||
335 | tc_core_init(); | |
336 | ||
337 | if (rtnl_open(&rth, 0) < 0) { | |
338 | fprintf(stderr, "Cannot open rtnetlink\n"); | |
339 | return -1; | |
340 | } | |
341 | ||
351efcde | 342 | cmdlineno = 0; |
485d0c60 CM |
343 | if (getcmdline(&line, &len, stdin) == -1) |
344 | goto Exit; | |
345 | largc = makeargs(line, largv, 100); | |
346 | bs_enabled = batchsize_enabled(largc, largv); | |
347 | bs_enabled_saved = bs_enabled; | |
348 | do { | |
349 | if (getcmdline(&line_next, &len, stdin) == -1) | |
350 | lastline = true; | |
351 | ||
352 | largc_next = makeargs(line_next, largv_next, 100); | |
353 | bs_enabled_next = batchsize_enabled(largc_next, largv_next); | |
354 | if (bs_enabled) { | |
355 | struct batch_buf *buf; | |
356 | ||
357 | buf = get_batch_buf(&buf_pool, &head, &tail); | |
358 | if (!buf) { | |
359 | fprintf(stderr, | |
360 | "failed to allocate batch_buf\n"); | |
361 | return -1; | |
362 | } | |
363 | ++batchsize; | |
364 | } | |
aba5acdf | 365 | |
485d0c60 CM |
366 | /* |
367 | * In batch mode, if we haven't accumulated enough commands | |
368 | * and this is not the last command and this command & next | |
369 | * command both support the batchsize feature, don't send the | |
370 | * message immediately. | |
371 | */ | |
372 | if (!lastline && bs_enabled && bs_enabled_next | |
373 | && batchsize != MSG_IOV_MAX) | |
374 | send = false; | |
375 | else | |
376 | send = true; | |
377 | ||
378 | line = line_next; | |
379 | line_next = NULL; | |
380 | len = 0; | |
381 | bs_enabled_saved = bs_enabled; | |
382 | bs_enabled = bs_enabled_next; | |
383 | bs_enabled_next = false; | |
384 | ||
385 | if (largc == 0) { | |
386 | largc = largc_next; | |
387 | memcpy(largv, largv_next, largc * sizeof(char *)); | |
08856f02 | 388 | continue; /* blank line */ |
485d0c60 | 389 | } |
aba5acdf | 390 | |
485d0c60 CM |
391 | ret = do_cmd(largc, largv, tail == NULL ? NULL : tail->buf, |
392 | tail == NULL ? 0 : sizeof (tail->buf)); | |
393 | if (ret != 0) { | |
394 | fprintf(stderr, "Command failed %s:%d\n", name, | |
395 | cmdlineno - 1); | |
08856f02 SH |
396 | ret = 1; |
397 | if (!force) | |
398 | break; | |
aba5acdf | 399 | } |
485d0c60 CM |
400 | largc = largc_next; |
401 | memcpy(largv, largv_next, largc * sizeof(char *)); | |
402 | ||
403 | if (send && bs_enabled_saved) { | |
404 | struct iovec *iov, *iovs; | |
405 | struct batch_buf *buf; | |
406 | struct nlmsghdr *n; | |
407 | ||
408 | iov = iovs = malloc(batchsize * sizeof (struct iovec)); | |
409 | for (buf = head; buf != NULL; buf = buf->next, ++iov) { | |
410 | n = (struct nlmsghdr *)&buf->buf; | |
411 | iov->iov_base = n; | |
412 | iov->iov_len = n->nlmsg_len; | |
413 | } | |
414 | ||
415 | ret = rtnl_talk_iov(&rth, iovs, batchsize, NULL); | |
416 | if (ret < 0) { | |
417 | fprintf(stderr, "Command failed %s:%d\n", name, | |
418 | cmdlineno - (batchsize + ret) - 1); | |
419 | return 2; | |
420 | } | |
421 | put_batch_bufs(&buf_pool, &head, &tail); | |
422 | batchsize = 0; | |
423 | free(iovs); | |
424 | } | |
425 | } while (!lastline); | |
7901660a | 426 | |
485d0c60 CM |
427 | free_batch_bufs(&buf_pool); |
428 | Exit: | |
429 | free(line); | |
c2f3a0f9 | 430 | rtnl_close(&rth); |
485d0c60 | 431 | |
c2f3a0f9 | 432 | return ret; |
433 | } | |
7901660a | 434 | |
c2f3a0f9 | 435 | |
436 | int main(int argc, char **argv) | |
437 | { | |
438 | int ret; | |
a3aa47a5 | 439 | char *batch_file = NULL; |
aba5acdf SH |
440 | |
441 | while (argc > 1) { | |
442 | if (argv[1][0] != '-') | |
443 | break; | |
444 | if (matches(argv[1], "-stats") == 0 || | |
3ea2bf45 | 445 | matches(argv[1], "-statistics") == 0) { |
aba5acdf SH |
446 | ++show_stats; |
447 | } else if (matches(argv[1], "-details") == 0) { | |
448 | ++show_details; | |
449 | } else if (matches(argv[1], "-raw") == 0) { | |
450 | ++show_raw; | |
44dcfe82 SH |
451 | } else if (matches(argv[1], "-pretty") == 0) { |
452 | ++show_pretty; | |
d954b34a VK |
453 | } else if (matches(argv[1], "-graph") == 0) { |
454 | show_graph = 1; | |
aba5acdf SH |
455 | } else if (matches(argv[1], "-Version") == 0) { |
456 | printf("tc utility, iproute2-ss%s\n", SNAPSHOT); | |
044ebf35 | 457 | return 0; |
3ea2bf45 SH |
458 | } else if (matches(argv[1], "-iec") == 0) { |
459 | ++use_iec; | |
aba5acdf SH |
460 | } else if (matches(argv[1], "-help") == 0) { |
461 | usage(); | |
c2f3a0f9 | 462 | return 0; |
08856f02 SH |
463 | } else if (matches(argv[1], "-force") == 0) { |
464 | ++force; | |
4612d04d | 465 | } else if (matches(argv[1], "-batch") == 0) { |
08856f02 | 466 | argc--; argv++; |
a3aa47a5 SH |
467 | if (argc <= 1) |
468 | usage(); | |
469 | batch_file = argv[1]; | |
67e1d73b VK |
470 | } else if (matches(argv[1], "-netns") == 0) { |
471 | NEXT_ARG(); | |
472 | if (netns_switch(argv[1])) | |
473 | return -1; | |
4612d04d VK |
474 | } else if (matches(argv[1], "-names") == 0 || |
475 | matches(argv[1], "-nm") == 0) { | |
476 | use_names = true; | |
477 | } else if (matches(argv[1], "-cf") == 0 || | |
478 | matches(argv[1], "-conf") == 0) { | |
479 | NEXT_ARG(); | |
480 | conf_file = argv[1]; | |
32a6fbe5 ED |
481 | } else if (matches(argv[1], "-timestamp") == 0) { |
482 | timestamp++; | |
483 | } else if (matches(argv[1], "-tshort") == 0) { | |
484 | ++timestamp; | |
485 | ++timestamp_short; | |
c91d262f JP |
486 | } else if (matches(argv[1], "-json") == 0) { |
487 | ++json; | |
a233caa0 SH |
488 | } else if (matches(argv[1], "-pretty") == 0) { |
489 | ++pretty; | |
aba5acdf SH |
490 | } else { |
491 | fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]); | |
044ebf35 | 492 | return -1; |
aba5acdf SH |
493 | } |
494 | argc--; argv++; | |
495 | } | |
496 | ||
a3aa47a5 SH |
497 | if (batch_file) |
498 | return batch(batch_file); | |
08856f02 | 499 | |
c2f3a0f9 | 500 | if (argc <= 1) { |
501 | usage(); | |
502 | return 0; | |
503 | } | |
504 | ||
aba5acdf | 505 | tc_core_init(); |
7901660a SH |
506 | if (rtnl_open(&rth, 0) < 0) { |
507 | fprintf(stderr, "Cannot open rtnetlink\n"); | |
508 | exit(1); | |
509 | } | |
aba5acdf | 510 | |
4612d04d VK |
511 | if (use_names && cls_names_init(conf_file)) { |
512 | ret = -1; | |
513 | goto Exit; | |
514 | } | |
515 | ||
485d0c60 | 516 | ret = do_cmd(argc-1, argv+1, NULL, 0); |
4612d04d | 517 | Exit: |
7901660a | 518 | rtnl_close(&rth); |
c2f3a0f9 | 519 | |
4612d04d VK |
520 | if (use_names) |
521 | cls_names_uninit(); | |
522 | ||
c2f3a0f9 | 523 | return ret; |
aba5acdf | 524 | } |