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