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