]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
e08f322a528a5fe6a27056804a2d71cca4ddb13f
[mirror_iproute2.git] / tc / tc.c
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>
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"
31 #include "namespace.h"
32
33 int show_stats;
34 int show_details;
35 int show_raw;
36 int show_graph;
37 int timestamp;
38
39 int batch_mode;
40 int use_iec;
41 int force;
42 bool use_names;
43 int json;
44 int color;
45 int oneline;
46
47 static char *conf_file;
48
49 struct rtnl_handle rth;
50
51 static void *BODY; /* cached handle dlopen(NULL) */
52 static struct qdisc_util *qdisc_list;
53 static struct filter_util *filter_list;
54
55 static int print_noqopt(struct qdisc_util *qu, FILE *f,
56 struct rtattr *opt)
57 {
58 if (opt && RTA_PAYLOAD(opt))
59 fprintf(f, "[Unknown qdisc, optlen=%u] ",
60 (unsigned int) RTA_PAYLOAD(opt));
61 return 0;
62 }
63
64 static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv,
65 struct nlmsghdr *n, const char *dev)
66 {
67 if (argc) {
68 fprintf(stderr,
69 "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n",
70 qu->id, *argv);
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))
79 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
80 fhandle, (unsigned int) RTA_PAYLOAD(opt));
81 else if (fhandle)
82 fprintf(f, "fh %08x ", fhandle);
83 return 0;
84 }
85
86 static int parse_nofopt(struct filter_util *qu, char *fhandle,
87 int argc, char **argv, struct nlmsghdr *n)
88 {
89 __u32 handle;
90
91 if (argc) {
92 fprintf(stderr,
93 "Unknown filter \"%s\", hence option \"%s\" is unparsable\n",
94 qu->id, *argv);
95 return -1;
96 }
97 if (fhandle) {
98 struct tcmsg *t = NLMSG_DATA(n);
99
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
109 struct qdisc_util *get_qdisc_kind(const char *str)
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
119 snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
120 dlh = dlopen(buf, RTLD_LAZY);
121 if (!dlh) {
122 /* look in current binary, only open once */
123 dlh = BODY;
124 if (dlh == NULL) {
125 dlh = BODY = dlopen(NULL, RTLD_LAZY);
126 if (dlh == NULL)
127 goto noexist;
128 }
129 }
130
131 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
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:
142 q = calloc(1, sizeof(*q));
143 if (q) {
144 q->id = strdup(str);
145 q->parse_qopt = parse_noqopt;
146 q->print_qopt = print_noqopt;
147 goto reg;
148 }
149 return q;
150 }
151
152
153 struct filter_util *get_filter_kind(const char *str)
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
163 snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
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
174 snprintf(buf, sizeof(buf), "%s_filter_util", str);
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;
183 noexist:
184 q = calloc(1, sizeof(*q));
185 if (q) {
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
194 static void usage(void)
195 {
196 fprintf(stderr,
197 "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
198 " tc [-force] -batch filename\n"
199 "where OBJECT := { qdisc | class | filter | chain |\n"
200 " 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");
205 }
206
207 static int do_cmd(int argc, char **argv, void *buf, size_t buflen)
208 {
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, "chain") == 0)
216 return do_chain(argc-1, argv+1, buf, buflen);
217 if (matches(*argv, "actions") == 0)
218 return do_action(argc-1, argv+1, buf, buflen);
219 if (matches(*argv, "monitor") == 0)
220 return do_tcmonitor(argc-1, argv+1);
221 if (matches(*argv, "exec") == 0)
222 return do_exec(argc-1, argv+1);
223 if (matches(*argv, "help") == 0) {
224 usage();
225 return 0;
226 }
227
228 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n",
229 *argv);
230 return -1;
231 }
232
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[] = {
240 { "filter", { "add", "delete", "change", "replace", NULL} },
241 { "actions", { "add", "change", "replace", NULL} },
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)
278 buf = calloc(1, sizeof(struct batch_buf));
279 else {
280 buf = *pool;
281 *pool = (*pool)->next;
282 memset(buf, 0, sizeof(struct batch_buf));
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
323 static int batch(const char *name)
324 {
325 struct batch_buf *head = NULL, *tail = NULL, *buf_pool = NULL;
326 char *largv[100], *largv_next[100];
327 char *line, *line_next = NULL;
328 bool bs_enabled = false;
329 bool lastline = false;
330 int largc, largc_next;
331 bool bs_enabled_saved;
332 bool bs_enabled_next;
333 int batchsize = 0;
334 size_t len = 0;
335 int ret = 0;
336 int err;
337 bool send;
338
339 batch_mode = 1;
340 if (name && strcmp(name, "-") != 0) {
341 if (freopen(name, "r", stdin) == NULL) {
342 fprintf(stderr,
343 "Cannot open file \"%s\" for reading: %s\n",
344 name, strerror(errno));
345 return -1;
346 }
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
356 cmdlineno = 0;
357 if (getcmdline(&line, &len, stdin) == -1)
358 goto Exit;
359 largc = makeargs(line, largv, 100);
360 bs_enabled = batchsize_enabled(largc, largv);
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 }
378
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;
396
397 if (largc == 0) {
398 largc = largc_next;
399 memcpy(largv, largv_next, largc * sizeof(char *));
400 continue; /* blank line */
401 }
402
403 err = do_cmd(largc, largv, tail == NULL ? NULL : tail->buf,
404 tail == NULL ? 0 : sizeof(tail->buf));
405 fflush(stdout);
406 if (err != 0) {
407 fprintf(stderr, "Command failed %s:%d\n", name,
408 cmdlineno - 1);
409 ret = 1;
410 if (!force)
411 break;
412 }
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
421 iov = iovs = malloc(batchsize * sizeof(struct iovec));
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
428 err = rtnl_talk_iov(&rth, iovs, batchsize, NULL);
429 put_batch_bufs(&buf_pool, &head, &tail);
430 free(iovs);
431 if (err < 0) {
432 fprintf(stderr, "Command failed %s:%d\n", name,
433 cmdlineno - (batchsize + err) - 1);
434 ret = 1;
435 if (!force)
436 break;
437 }
438 batchsize = 0;
439 }
440 } while (!lastline);
441
442 free_batch_bufs(&buf_pool);
443 Exit:
444 free(line);
445 rtnl_close(&rth);
446
447 return ret;
448 }
449
450
451 int main(int argc, char **argv)
452 {
453 int ret;
454 char *batch_file = NULL;
455
456 while (argc > 1) {
457 if (argv[1][0] != '-')
458 break;
459 if (matches(argv[1], "-stats") == 0 ||
460 matches(argv[1], "-statistics") == 0) {
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;
466 } else if (matches(argv[1], "-pretty") == 0) {
467 ++pretty;
468 } else if (matches(argv[1], "-graph") == 0) {
469 show_graph = 1;
470 } else if (matches(argv[1], "-Version") == 0) {
471 printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
472 return 0;
473 } else if (matches(argv[1], "-iec") == 0) {
474 ++use_iec;
475 } else if (matches(argv[1], "-help") == 0) {
476 usage();
477 return 0;
478 } else if (matches(argv[1], "-force") == 0) {
479 ++force;
480 } else if (matches(argv[1], "-batch") == 0) {
481 argc--; argv++;
482 if (argc <= 1)
483 usage();
484 batch_file = argv[1];
485 } else if (matches(argv[1], "-netns") == 0) {
486 NEXT_ARG();
487 if (netns_switch(argv[1]))
488 return -1;
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];
496 } else if (matches_color(argv[1], &color)) {
497 } else if (matches(argv[1], "-timestamp") == 0) {
498 timestamp++;
499 } else if (matches(argv[1], "-tshort") == 0) {
500 ++timestamp;
501 ++timestamp_short;
502 } else if (matches(argv[1], "-json") == 0) {
503 ++json;
504 } else if (matches(argv[1], "-oneline") == 0) {
505 ++oneline;
506 } else {
507 fprintf(stderr,
508 "Option \"%s\" is unknown, try \"tc -help\".\n",
509 argv[1]);
510 return -1;
511 }
512 argc--; argv++;
513 }
514
515 _SL_ = oneline ? "\\" : "\n";
516
517 check_enable_color(color, json);
518
519 if (batch_file)
520 return batch(batch_file);
521
522 if (argc <= 1) {
523 usage();
524 return 0;
525 }
526
527 tc_core_init();
528 if (rtnl_open(&rth, 0) < 0) {
529 fprintf(stderr, "Cannot open rtnetlink\n");
530 exit(1);
531 }
532
533 if (use_names && cls_names_init(conf_file)) {
534 ret = -1;
535 goto Exit;
536 }
537
538 ret = do_cmd(argc-1, argv+1, NULL, 0);
539 Exit:
540 rtnl_close(&rth);
541
542 if (use_names)
543 cls_names_uninit();
544
545 return ret;
546 }