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