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