]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
ip: add a new parameter -Numeric
[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 int numeric;
47
48 static char *conf_file;
49
50 struct rtnl_handle rth;
51
52 static void *BODY; /* cached handle dlopen(NULL) */
53 static struct qdisc_util *qdisc_list;
54 static struct filter_util *filter_list;
55
56 static int print_noqopt(struct qdisc_util *qu, FILE *f,
57 struct rtattr *opt)
58 {
59 if (opt && RTA_PAYLOAD(opt))
60 fprintf(f, "[Unknown qdisc, optlen=%u] ",
61 (unsigned int) RTA_PAYLOAD(opt));
62 return 0;
63 }
64
65 static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv,
66 struct nlmsghdr *n, const char *dev)
67 {
68 if (argc) {
69 fprintf(stderr,
70 "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n",
71 qu->id, *argv);
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))
80 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
81 fhandle, (unsigned int) RTA_PAYLOAD(opt));
82 else if (fhandle)
83 fprintf(f, "fh %08x ", fhandle);
84 return 0;
85 }
86
87 static int parse_nofopt(struct filter_util *qu, char *fhandle,
88 int argc, char **argv, struct nlmsghdr *n)
89 {
90 __u32 handle;
91
92 if (argc) {
93 fprintf(stderr,
94 "Unknown filter \"%s\", hence option \"%s\" is unparsable\n",
95 qu->id, *argv);
96 return -1;
97 }
98 if (fhandle) {
99 struct tcmsg *t = NLMSG_DATA(n);
100
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
110 struct qdisc_util *get_qdisc_kind(const char *str)
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
120 snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
121 dlh = dlopen(buf, RTLD_LAZY);
122 if (!dlh) {
123 /* look in current binary, only open once */
124 dlh = BODY;
125 if (dlh == NULL) {
126 dlh = BODY = dlopen(NULL, RTLD_LAZY);
127 if (dlh == NULL)
128 goto noexist;
129 }
130 }
131
132 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
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:
143 q = calloc(1, sizeof(*q));
144 if (q) {
145 q->id = strdup(str);
146 q->parse_qopt = parse_noqopt;
147 q->print_qopt = print_noqopt;
148 goto reg;
149 }
150 return q;
151 }
152
153
154 struct filter_util *get_filter_kind(const char *str)
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
164 snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
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
175 snprintf(buf, sizeof(buf), "%s_filter_util", str);
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;
184 noexist:
185 q = calloc(1, sizeof(*q));
186 if (q) {
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
195 static void usage(void)
196 {
197 fprintf(stderr,
198 "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
199 " tc [-force] -batch filename\n"
200 "where OBJECT := { qdisc | class | filter | chain |\n"
201 " action | monitor | exec }\n"
202 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n"
203 " -o[neline] | -j[son] | -p[retty] | -c[olor]\n"
204 " -b[atch] [filename] | -n[etns] name | -N[umeric] |\n"
205 " -nm | -nam[es] | { -cf | -conf } path }\n");
206 }
207
208 static int do_cmd(int argc, char **argv, void *buf, size_t buflen)
209 {
210 if (matches(*argv, "qdisc") == 0)
211 return do_qdisc(argc-1, argv+1);
212 if (matches(*argv, "class") == 0)
213 return do_class(argc-1, argv+1);
214 if (matches(*argv, "filter") == 0)
215 return do_filter(argc-1, argv+1, buf, buflen);
216 if (matches(*argv, "chain") == 0)
217 return do_chain(argc-1, argv+1, buf, buflen);
218 if (matches(*argv, "actions") == 0)
219 return do_action(argc-1, argv+1, buf, buflen);
220 if (matches(*argv, "monitor") == 0)
221 return do_tcmonitor(argc-1, argv+1);
222 if (matches(*argv, "exec") == 0)
223 return do_exec(argc-1, argv+1);
224 if (matches(*argv, "help") == 0) {
225 usage();
226 return 0;
227 }
228
229 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n",
230 *argv);
231 return -1;
232 }
233
234 #define TC_MAX_SUBC 10
235 static bool batchsize_enabled(int argc, char *argv[])
236 {
237 struct {
238 char *c;
239 char *subc[TC_MAX_SUBC];
240 } table[] = {
241 { "filter", { "add", "delete", "change", "replace", NULL} },
242 { "actions", { "add", "change", "replace", NULL} },
243 { NULL },
244 }, *iter;
245 char *s;
246 int i;
247
248 if (argc < 2)
249 return false;
250
251 for (iter = table; iter->c; iter++) {
252 if (matches(argv[0], iter->c))
253 continue;
254 for (i = 0; i < TC_MAX_SUBC; i++) {
255 s = iter->subc[i];
256 if (s && matches(argv[1], s) == 0)
257 return true;
258 }
259 }
260
261 return false;
262 }
263
264 struct batch_buf {
265 struct batch_buf *next;
266 char buf[16420]; /* sizeof (struct nlmsghdr) +
267 max(sizeof (struct tcmsg) +
268 sizeof (struct tcamsg)) +
269 MAX_MSG */
270 };
271
272 static struct batch_buf *get_batch_buf(struct batch_buf **pool,
273 struct batch_buf **head,
274 struct batch_buf **tail)
275 {
276 struct batch_buf *buf;
277
278 if (*pool == NULL)
279 buf = calloc(1, sizeof(struct batch_buf));
280 else {
281 buf = *pool;
282 *pool = (*pool)->next;
283 memset(buf, 0, sizeof(struct batch_buf));
284 }
285
286 if (*head == NULL)
287 *head = *tail = buf;
288 else {
289 (*tail)->next = buf;
290 (*tail) = buf;
291 }
292
293 return buf;
294 }
295
296 static void put_batch_bufs(struct batch_buf **pool,
297 struct batch_buf **head,
298 struct batch_buf **tail)
299 {
300 if (*head == NULL || *tail == NULL)
301 return;
302
303 if (*pool == NULL)
304 *pool = *head;
305 else {
306 (*tail)->next = *pool;
307 *pool = *head;
308 }
309 *head = NULL;
310 *tail = NULL;
311 }
312
313 static void free_batch_bufs(struct batch_buf **pool)
314 {
315 struct batch_buf *buf;
316
317 for (buf = *pool; buf != NULL; buf = *pool) {
318 *pool = buf->next;
319 free(buf);
320 }
321 *pool = NULL;
322 }
323
324 static int batch(const char *name)
325 {
326 struct batch_buf *head = NULL, *tail = NULL, *buf_pool = NULL;
327 char *largv[100], *largv_next[100];
328 char *line, *line_next = NULL;
329 bool bs_enabled = false;
330 bool lastline = false;
331 int largc, largc_next;
332 bool bs_enabled_saved;
333 bool bs_enabled_next;
334 int batchsize = 0;
335 size_t len = 0;
336 int ret = 0;
337 int err;
338 bool send;
339
340 batch_mode = 1;
341 if (name && strcmp(name, "-") != 0) {
342 if (freopen(name, "r", stdin) == NULL) {
343 fprintf(stderr,
344 "Cannot open file \"%s\" for reading: %s\n",
345 name, strerror(errno));
346 return -1;
347 }
348 }
349
350 tc_core_init();
351
352 if (rtnl_open(&rth, 0) < 0) {
353 fprintf(stderr, "Cannot open rtnetlink\n");
354 return -1;
355 }
356
357 cmdlineno = 0;
358 if (getcmdline(&line, &len, stdin) == -1)
359 goto Exit;
360 largc = makeargs(line, largv, 100);
361 bs_enabled = batchsize_enabled(largc, largv);
362 do {
363 if (getcmdline(&line_next, &len, stdin) == -1)
364 lastline = true;
365
366 largc_next = makeargs(line_next, largv_next, 100);
367 bs_enabled_next = batchsize_enabled(largc_next, largv_next);
368 if (bs_enabled) {
369 struct batch_buf *buf;
370
371 buf = get_batch_buf(&buf_pool, &head, &tail);
372 if (!buf) {
373 fprintf(stderr,
374 "failed to allocate batch_buf\n");
375 return -1;
376 }
377 ++batchsize;
378 }
379
380 /*
381 * In batch mode, if we haven't accumulated enough commands
382 * and this is not the last command and this command & next
383 * command both support the batchsize feature, don't send the
384 * message immediately.
385 */
386 if (!lastline && bs_enabled && bs_enabled_next
387 && batchsize != MSG_IOV_MAX)
388 send = false;
389 else
390 send = true;
391
392 line = line_next;
393 line_next = NULL;
394 len = 0;
395 bs_enabled_saved = bs_enabled;
396 bs_enabled = bs_enabled_next;
397
398 if (largc == 0) {
399 largc = largc_next;
400 memcpy(largv, largv_next, largc * sizeof(char *));
401 continue; /* blank line */
402 }
403
404 err = do_cmd(largc, largv, tail == NULL ? NULL : tail->buf,
405 tail == NULL ? 0 : sizeof(tail->buf));
406 fflush(stdout);
407 if (err != 0) {
408 fprintf(stderr, "Command failed %s:%d\n", name,
409 cmdlineno - 1);
410 ret = 1;
411 if (!force)
412 break;
413 }
414 largc = largc_next;
415 memcpy(largv, largv_next, largc * sizeof(char *));
416
417 if (send && bs_enabled_saved) {
418 struct iovec *iov, *iovs;
419 struct batch_buf *buf;
420 struct nlmsghdr *n;
421
422 iov = iovs = malloc(batchsize * sizeof(struct iovec));
423 for (buf = head; buf != NULL; buf = buf->next, ++iov) {
424 n = (struct nlmsghdr *)&buf->buf;
425 iov->iov_base = n;
426 iov->iov_len = n->nlmsg_len;
427 }
428
429 err = rtnl_talk_iov(&rth, iovs, batchsize, NULL);
430 put_batch_bufs(&buf_pool, &head, &tail);
431 free(iovs);
432 if (err < 0) {
433 fprintf(stderr, "Command failed %s:%d\n", name,
434 cmdlineno - (batchsize + err) - 1);
435 ret = 1;
436 if (!force)
437 break;
438 }
439 batchsize = 0;
440 }
441 } while (!lastline);
442
443 free_batch_bufs(&buf_pool);
444 Exit:
445 free(line);
446 rtnl_close(&rth);
447
448 return ret;
449 }
450
451
452 int main(int argc, char **argv)
453 {
454 int ret;
455 char *batch_file = NULL;
456
457 while (argc > 1) {
458 if (argv[1][0] != '-')
459 break;
460 if (matches(argv[1], "-stats") == 0 ||
461 matches(argv[1], "-statistics") == 0) {
462 ++show_stats;
463 } else if (matches(argv[1], "-details") == 0) {
464 ++show_details;
465 } else if (matches(argv[1], "-raw") == 0) {
466 ++show_raw;
467 } else if (matches(argv[1], "-pretty") == 0) {
468 ++pretty;
469 } else if (matches(argv[1], "-graph") == 0) {
470 show_graph = 1;
471 } else if (matches(argv[1], "-Version") == 0) {
472 printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
473 return 0;
474 } else if (matches(argv[1], "-iec") == 0) {
475 ++use_iec;
476 } else if (matches(argv[1], "-help") == 0) {
477 usage();
478 return 0;
479 } else if (matches(argv[1], "-force") == 0) {
480 ++force;
481 } else if (matches(argv[1], "-batch") == 0) {
482 argc--; argv++;
483 if (argc <= 1)
484 usage();
485 batch_file = argv[1];
486 } else if (matches(argv[1], "-netns") == 0) {
487 NEXT_ARG();
488 if (netns_switch(argv[1]))
489 return -1;
490 } else if (matches(argv[1], "-Numeric") == 0) {
491 ++numeric;
492 } else if (matches(argv[1], "-names") == 0 ||
493 matches(argv[1], "-nm") == 0) {
494 use_names = true;
495 } else if (matches(argv[1], "-cf") == 0 ||
496 matches(argv[1], "-conf") == 0) {
497 NEXT_ARG();
498 conf_file = argv[1];
499 } else if (matches_color(argv[1], &color)) {
500 } else if (matches(argv[1], "-timestamp") == 0) {
501 timestamp++;
502 } else if (matches(argv[1], "-tshort") == 0) {
503 ++timestamp;
504 ++timestamp_short;
505 } else if (matches(argv[1], "-json") == 0) {
506 ++json;
507 } else if (matches(argv[1], "-oneline") == 0) {
508 ++oneline;
509 } else {
510 fprintf(stderr,
511 "Option \"%s\" is unknown, try \"tc -help\".\n",
512 argv[1]);
513 return -1;
514 }
515 argc--; argv++;
516 }
517
518 _SL_ = oneline ? "\\" : "\n";
519
520 check_enable_color(color, json);
521
522 if (batch_file)
523 return batch(batch_file);
524
525 if (argc <= 1) {
526 usage();
527 return 0;
528 }
529
530 tc_core_init();
531 if (rtnl_open(&rth, 0) < 0) {
532 fprintf(stderr, "Cannot open rtnetlink\n");
533 exit(1);
534 }
535
536 if (use_names && cls_names_init(conf_file)) {
537 ret = -1;
538 goto Exit;
539 }
540
541 ret = do_cmd(argc-1, argv+1, NULL, 0);
542 Exit:
543 rtnl_close(&rth);
544
545 if (use_names)
546 cls_names_uninit();
547
548 return ret;
549 }