]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_qdisc.c
treewide: Use addattr_nest()/addattr_nest_end() to handle nested attributes
[mirror_iproute2.git] / tc / tc_qdisc.c
1 /*
2 * tc_qdisc.c "tc qdisc".
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 * J Hadi Salim: Extension to ingress
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21 #include <math.h>
22 #include <malloc.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26 #include "tc_common.h"
27
28 static int usage(void)
29 {
30 fprintf(stderr, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
31 fprintf(stderr, " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n");
32 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
33 fprintf(stderr, " [ stab [ help | STAB_OPTIONS] ]\n");
34 fprintf(stderr, " [ ingress_block BLOCK_INDEX ] [ egress_block BLOCK_INDEX ]\n");
35 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr, "\n");
37 fprintf(stderr, " tc qdisc show [ dev STRING ] [ ingress | clsact ] [ invisible ]\n");
38 fprintf(stderr, "Where:\n");
39 fprintf(stderr, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n");
40 fprintf(stderr, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n");
41 fprintf(stderr, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
42 return -1;
43 }
44
45 static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
46 {
47 struct qdisc_util *q = NULL;
48 struct tc_estimator est = {};
49 struct {
50 struct tc_sizespec szopts;
51 __u16 *data;
52 } stab = {};
53 char d[IFNAMSIZ] = {};
54 char k[FILTER_NAMESZ] = {};
55 struct {
56 struct nlmsghdr n;
57 struct tcmsg t;
58 char buf[TCA_BUF_MAX];
59 } req = {
60 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
61 .n.nlmsg_flags = NLM_F_REQUEST | flags,
62 .n.nlmsg_type = cmd,
63 .t.tcm_family = AF_UNSPEC,
64 };
65 __u32 ingress_block = 0;
66 __u32 egress_block = 0;
67
68 while (argc > 0) {
69 if (strcmp(*argv, "dev") == 0) {
70 NEXT_ARG();
71 if (d[0])
72 duparg("dev", *argv);
73 strncpy(d, *argv, sizeof(d)-1);
74 } else if (strcmp(*argv, "handle") == 0) {
75 __u32 handle;
76
77 if (req.t.tcm_handle)
78 duparg("handle", *argv);
79 NEXT_ARG();
80 if (get_qdisc_handle(&handle, *argv))
81 invarg("invalid qdisc ID", *argv);
82 req.t.tcm_handle = handle;
83 } else if (strcmp(*argv, "root") == 0) {
84 if (req.t.tcm_parent) {
85 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
86 return -1;
87 }
88 req.t.tcm_parent = TC_H_ROOT;
89 } else if (strcmp(*argv, "clsact") == 0) {
90 if (req.t.tcm_parent) {
91 fprintf(stderr, "Error: \"clsact\" is a duplicate parent ID\n");
92 return -1;
93 }
94 req.t.tcm_parent = TC_H_CLSACT;
95 strncpy(k, "clsact", sizeof(k) - 1);
96 q = get_qdisc_kind(k);
97 req.t.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
98 NEXT_ARG_FWD();
99 break;
100 } else if (strcmp(*argv, "ingress") == 0) {
101 if (req.t.tcm_parent) {
102 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n");
103 return -1;
104 }
105 req.t.tcm_parent = TC_H_INGRESS;
106 strncpy(k, "ingress", sizeof(k) - 1);
107 q = get_qdisc_kind(k);
108 req.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
109 NEXT_ARG_FWD();
110 break;
111 } else if (strcmp(*argv, "parent") == 0) {
112 __u32 handle;
113
114 NEXT_ARG();
115 if (req.t.tcm_parent)
116 duparg("parent", *argv);
117 if (get_tc_classid(&handle, *argv))
118 invarg("invalid parent ID", *argv);
119 req.t.tcm_parent = handle;
120 } else if (matches(*argv, "estimator") == 0) {
121 if (parse_estimator(&argc, &argv, &est))
122 return -1;
123 } else if (matches(*argv, "stab") == 0) {
124 if (parse_size_table(&argc, &argv, &stab.szopts) < 0)
125 return -1;
126 continue;
127 } else if (matches(*argv, "ingress_block") == 0) {
128 NEXT_ARG();
129 if (get_u32(&ingress_block, *argv, 0) || !ingress_block)
130 invarg("invalid ingress block index value", *argv);
131 } else if (matches(*argv, "egress_block") == 0) {
132 NEXT_ARG();
133 if (get_u32(&egress_block, *argv, 0) || !egress_block)
134 invarg("invalid egress block index value", *argv);
135 } else if (matches(*argv, "help") == 0) {
136 usage();
137 } else {
138 strncpy(k, *argv, sizeof(k)-1);
139
140 q = get_qdisc_kind(k);
141 argc--; argv++;
142 break;
143 }
144 argc--; argv++;
145 }
146
147 if (k[0])
148 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
149 if (est.ewma_log)
150 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
151
152 if (ingress_block)
153 addattr32(&req.n, sizeof(req),
154 TCA_INGRESS_BLOCK, ingress_block);
155 if (egress_block)
156 addattr32(&req.n, sizeof(req),
157 TCA_EGRESS_BLOCK, egress_block);
158
159 if (q) {
160 if (q->parse_qopt) {
161 if (q->parse_qopt(q, argc, argv, &req.n, d))
162 return 1;
163 } else if (argc) {
164 fprintf(stderr, "qdisc '%s' does not support option parsing\n", k);
165 return -1;
166 }
167 } else {
168 if (argc) {
169 if (matches(*argv, "help") == 0)
170 usage();
171
172 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv);
173 return -1;
174 }
175 }
176
177 if (check_size_table_opts(&stab.szopts)) {
178 struct rtattr *tail;
179
180 if (tc_calc_size_table(&stab.szopts, &stab.data) < 0) {
181 fprintf(stderr, "failed to calculate size table.\n");
182 return -1;
183 }
184
185 tail = addattr_nest(&req.n, sizeof(req), TCA_STAB);
186 addattr_l(&req.n, sizeof(req), TCA_STAB_BASE, &stab.szopts,
187 sizeof(stab.szopts));
188 if (stab.data)
189 addattr_l(&req.n, sizeof(req), TCA_STAB_DATA, stab.data,
190 stab.szopts.tsize * sizeof(__u16));
191 addattr_nest_end(&req.n, tail);
192 if (stab.data)
193 free(stab.data);
194 }
195
196 if (d[0]) {
197 int idx;
198
199 ll_init_map(&rth);
200
201 idx = ll_name_to_index(d);
202 if (idx == 0) {
203 fprintf(stderr, "Cannot find device \"%s\"\n", d);
204 return 1;
205 }
206 req.t.tcm_ifindex = idx;
207 }
208
209 if (rtnl_talk(&rth, &req.n, NULL) < 0)
210 return 2;
211
212 return 0;
213 }
214
215 static int filter_ifindex;
216
217 int print_qdisc(const struct sockaddr_nl *who,
218 struct nlmsghdr *n, void *arg)
219 {
220 FILE *fp = (FILE *)arg;
221 struct tcmsg *t = NLMSG_DATA(n);
222 int len = n->nlmsg_len;
223 struct rtattr *tb[TCA_MAX+1];
224 struct qdisc_util *q;
225 char abuf[256];
226
227 if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) {
228 fprintf(stderr, "Not a qdisc\n");
229 return 0;
230 }
231 len -= NLMSG_LENGTH(sizeof(*t));
232 if (len < 0) {
233 fprintf(stderr, "Wrong len %d\n", len);
234 return -1;
235 }
236
237 if (filter_ifindex && filter_ifindex != t->tcm_ifindex)
238 return 0;
239
240 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
241
242 if (tb[TCA_KIND] == NULL) {
243 fprintf(stderr, "print_qdisc: NULL kind\n");
244 return -1;
245 }
246
247 open_json_object(NULL);
248
249 if (n->nlmsg_type == RTM_DELQDISC)
250 print_bool(PRINT_ANY, "deleted", "deleted ", true);
251
252 if (n->nlmsg_type == RTM_NEWQDISC &&
253 (n->nlmsg_flags & NLM_F_CREATE) &&
254 (n->nlmsg_flags & NLM_F_REPLACE))
255 print_bool(PRINT_ANY, "replaced", "replaced ", true);
256
257 if (n->nlmsg_type == RTM_NEWQDISC &&
258 (n->nlmsg_flags & NLM_F_CREATE) &&
259 (n->nlmsg_flags & NLM_F_EXCL))
260 print_bool(PRINT_ANY, "added", "added ", true);
261
262 print_string(PRINT_ANY, "kind", "qdisc %s",
263 rta_getattr_str(tb[TCA_KIND]));
264 sprintf(abuf, "%x:", t->tcm_handle >> 16);
265 print_string(PRINT_ANY, "handle", " %s", abuf);
266 if (show_raw) {
267 sprintf(abuf, "[%08x]", t->tcm_handle);
268 print_string(PRINT_FP, NULL, "%s", abuf);
269 }
270 print_string(PRINT_FP, NULL, " ", NULL);
271
272 if (filter_ifindex == 0)
273 print_string(PRINT_ANY, "dev", "dev %s ",
274 ll_index_to_name(t->tcm_ifindex));
275
276 if (t->tcm_parent == TC_H_ROOT)
277 print_bool(PRINT_ANY, "root", "root ", true);
278 else if (t->tcm_parent) {
279 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
280 print_string(PRINT_ANY, "parent", "parent %s ", abuf);
281 }
282
283 if (t->tcm_info != 1)
284 print_uint(PRINT_ANY, "refcnt", "refcnt %u ", t->tcm_info);
285
286 if (tb[TCA_HW_OFFLOAD] &&
287 (rta_getattr_u8(tb[TCA_HW_OFFLOAD])))
288 print_bool(PRINT_ANY, "offloaded", "offloaded ", true);
289
290 if (tb[TCA_INGRESS_BLOCK] &&
291 RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) {
292 __u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]);
293
294 if (block)
295 print_uint(PRINT_ANY, "ingress_block",
296 "ingress_block %u ", block);
297 }
298
299 if (tb[TCA_EGRESS_BLOCK] &&
300 RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) {
301 __u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]);
302
303 if (block)
304 print_uint(PRINT_ANY, "egress_block",
305 "egress_block %u ", block);
306 }
307
308 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
309 if (strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])) == 0)
310 q = get_qdisc_kind("prio");
311 else
312 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
313
314 open_json_object("options");
315 if (tb[TCA_OPTIONS]) {
316 if (q)
317 q->print_qopt(q, fp, tb[TCA_OPTIONS]);
318 else
319 print_string(PRINT_FP, NULL,
320 "[cannot parse qdisc parameters]", NULL);
321 }
322 close_json_object();
323
324 print_string(PRINT_FP, NULL, "\n", NULL);
325
326 if (show_details && tb[TCA_STAB]) {
327 print_size_table(fp, " ", tb[TCA_STAB]);
328 print_string(PRINT_FP, NULL, "\n", NULL);
329 }
330
331 if (show_stats) {
332 struct rtattr *xstats = NULL;
333
334 if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
335 print_tcstats_attr(fp, tb, " ", &xstats);
336 print_string(PRINT_FP, NULL, "\n", NULL);
337 }
338
339 if (q && xstats && q->print_xstats) {
340 q->print_xstats(q, fp, xstats);
341 print_string(PRINT_FP, NULL, "\n", NULL);
342 }
343 }
344 close_json_object();
345 fflush(fp);
346 return 0;
347 }
348
349 static int tc_qdisc_list(int argc, char **argv)
350 {
351 struct tcmsg t = { .tcm_family = AF_UNSPEC };
352 char d[IFNAMSIZ] = {};
353 bool dump_invisible = false;
354
355 while (argc > 0) {
356 if (strcmp(*argv, "dev") == 0) {
357 NEXT_ARG();
358 strncpy(d, *argv, sizeof(d)-1);
359 } else if (strcmp(*argv, "ingress") == 0 ||
360 strcmp(*argv, "clsact") == 0) {
361 if (t.tcm_parent) {
362 fprintf(stderr, "Duplicate parent ID\n");
363 usage();
364 }
365 t.tcm_parent = TC_H_INGRESS;
366 } else if (matches(*argv, "help") == 0) {
367 usage();
368 } else if (strcmp(*argv, "invisible") == 0) {
369 dump_invisible = true;
370 } else {
371 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
372 return -1;
373 }
374
375 argc--; argv++;
376 }
377
378 ll_init_map(&rth);
379
380 if (d[0]) {
381 t.tcm_ifindex = ll_name_to_index(d);
382 if (t.tcm_ifindex == 0) {
383 fprintf(stderr, "Cannot find device \"%s\"\n", d);
384 return 1;
385 }
386 filter_ifindex = t.tcm_ifindex;
387 }
388
389 if (dump_invisible) {
390 struct {
391 struct nlmsghdr n;
392 struct tcmsg t;
393 char buf[256];
394 } req = {
395 .n.nlmsg_type = RTM_GETQDISC,
396 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
397 };
398
399 req.t.tcm_family = AF_UNSPEC;
400
401 addattr(&req.n, 256, TCA_DUMP_INVISIBLE);
402 if (rtnl_dump_request_n(&rth, &req.n) < 0) {
403 perror("Cannot send dump request");
404 return 1;
405 }
406
407 } else if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
408 perror("Cannot send dump request");
409 return 1;
410 }
411
412 new_json_obj(json);
413 if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) {
414 fprintf(stderr, "Dump terminated\n");
415 return 1;
416 }
417 delete_json_obj();
418
419 return 0;
420 }
421
422 int do_qdisc(int argc, char **argv)
423 {
424 if (argc < 1)
425 return tc_qdisc_list(0, NULL);
426 if (matches(*argv, "add") == 0)
427 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
428 if (matches(*argv, "change") == 0)
429 return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1);
430 if (matches(*argv, "replace") == 0)
431 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
432 if (matches(*argv, "link") == 0)
433 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1);
434 if (matches(*argv, "delete") == 0)
435 return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1);
436 #if 0
437 if (matches(*argv, "get") == 0)
438 return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1);
439 #endif
440 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
441 || matches(*argv, "lst") == 0)
442 return tc_qdisc_list(argc-1, argv+1);
443 if (matches(*argv, "help") == 0) {
444 usage();
445 return 0;
446 }
447 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);
448 return -1;
449 }
450
451 struct tc_qdisc_block_exists_ctx {
452 __u32 block_index;
453 bool found;
454 };
455
456 static int tc_qdisc_block_exists_cb(const struct sockaddr_nl *who,
457 struct nlmsghdr *n, void *arg)
458 {
459 struct tc_qdisc_block_exists_ctx *ctx = arg;
460 struct tcmsg *t = NLMSG_DATA(n);
461 struct rtattr *tb[TCA_MAX+1];
462 int len = n->nlmsg_len;
463
464 if (n->nlmsg_type != RTM_NEWQDISC)
465 return 0;
466
467 len -= NLMSG_LENGTH(sizeof(*t));
468 if (len < 0)
469 return -1;
470
471 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
472
473 if (tb[TCA_KIND] == NULL)
474 return -1;
475
476 if (tb[TCA_INGRESS_BLOCK] &&
477 RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) {
478 __u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]);
479
480 if (block == ctx->block_index)
481 ctx->found = true;
482 }
483
484 if (tb[TCA_EGRESS_BLOCK] &&
485 RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) {
486 __u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]);
487
488 if (block == ctx->block_index)
489 ctx->found = true;
490 }
491 return 0;
492 }
493
494 bool tc_qdisc_block_exists(__u32 block_index)
495 {
496 struct tc_qdisc_block_exists_ctx ctx = { .block_index = block_index };
497 struct tcmsg t = { .tcm_family = AF_UNSPEC };
498
499 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
500 perror("Cannot send dump request");
501 return false;
502 }
503
504 if (rtnl_dump_filter(&rth, tc_qdisc_block_exists_cb, &ctx) < 0) {
505 perror("Dump terminated\n");
506 return false;
507 }
508
509 return ctx.found;
510 }