]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_qdisc.c
lib: introduce print_nl
[mirror_iproute2.git] / tc / tc_qdisc.c
CommitLineData
aba5acdf
SH
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>
aba5acdf
SH
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>
839c8456 22#include <malloc.h>
aba5acdf
SH
23
24#include "utils.h"
25#include "tc_util.h"
26#include "tc_common.h"
27
11656f1b 28static int usage(void)
aba5acdf 29{
e5d179d8 30 fprintf(stderr, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
8f9afdd5 31 fprintf(stderr, " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n");
aba5acdf 32 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
839c8456 33 fprintf(stderr, " [ stab [ help | STAB_OPTIONS] ]\n");
063463ef 34 fprintf(stderr, " [ ingress_block BLOCK_INDEX ] [ egress_block BLOCK_INDEX ]\n");
aba5acdf
SH
35 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr, "\n");
7c581a12 37 fprintf(stderr, " tc qdisc show [ dev STRING ] [ ingress | clsact ] [ invisible ]\n");
aba5acdf
SH
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");
839c8456 41 fprintf(stderr, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
11656f1b 42 return -1;
aba5acdf
SH
43}
44
32a121cb 45static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
aba5acdf 46{
aba5acdf 47 struct qdisc_util *q = NULL;
9579afb2 48 struct tc_estimator est = {};
839c8456
JK
49 struct {
50 struct tc_sizespec szopts;
51 __u16 *data;
d17b136f 52 } stab = {};
b317557f
SH
53 char d[IFNAMSIZ] = {};
54 char k[FILTER_NAMESZ] = {};
f307c24e 55 struct {
32a121cb
SH
56 struct nlmsghdr n;
57 struct tcmsg t;
58 char buf[TCA_BUF_MAX];
d17b136f
PS
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 };
063463ef
JP
65 __u32 ingress_block = 0;
66 __u32 egress_block = 0;
aba5acdf
SH
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;
32a121cb 76
aba5acdf
SH
77 if (req.t.tcm_handle)
78 duparg("handle", *argv);
79 NEXT_ARG();
80 if (get_qdisc_handle(&handle, *argv))
f1675d61 81 invarg("invalid qdisc ID", *argv);
aba5acdf
SH
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");
11656f1b 86 return -1;
aba5acdf
SH
87 }
88 req.t.tcm_parent = TC_H_ROOT;
8f9afdd5
DB
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;
aba5acdf
SH
100 } else if (strcmp(*argv, "ingress") == 0) {
101 if (req.t.tcm_parent) {
102 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n");
11656f1b 103 return -1;
aba5acdf
SH
104 }
105 req.t.tcm_parent = TC_H_INGRESS;
0d45c4b4 106 strncpy(k, "ingress", sizeof(k) - 1);
aba5acdf 107 q = get_qdisc_kind(k);
0d45c4b4
DB
108 req.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
109 NEXT_ARG_FWD();
aba5acdf 110 break;
aba5acdf
SH
111 } else if (strcmp(*argv, "parent") == 0) {
112 __u32 handle;
32a121cb 113
aba5acdf
SH
114 NEXT_ARG();
115 if (req.t.tcm_parent)
116 duparg("parent", *argv);
117 if (get_tc_classid(&handle, *argv))
f1675d61 118 invarg("invalid parent ID", *argv);
aba5acdf
SH
119 req.t.tcm_parent = handle;
120 } else if (matches(*argv, "estimator") == 0) {
121 if (parse_estimator(&argc, &argv, &est))
122 return -1;
839c8456
JK
123 } else if (matches(*argv, "stab") == 0) {
124 if (parse_size_table(&argc, &argv, &stab.szopts) < 0)
125 return -1;
126 continue;
063463ef
JP
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);
aba5acdf
SH
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
063463ef
JP
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
0a502b21
SH
159 if (q) {
160 if (q->parse_qopt) {
927e3cfb 161 if (q->parse_qopt(q, argc, argv, &req.n, d))
e9e78b0d 162 return 1;
0a502b21
SH
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) {
aba5acdf
SH
169 if (matches(*argv, "help") == 0)
170 usage();
171
172 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv);
11656f1b 173 return -1;
aba5acdf
SH
174 }
175 }
176
839c8456
JK
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
c14f9d92 185 tail = addattr_nest(&req.n, sizeof(req), TCA_STAB);
839c8456
JK
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));
c14f9d92 191 addattr_nest_end(&req.n, tail);
839c8456
JK
192 if (stab.data)
193 free(stab.data);
194 }
195
aba5acdf
SH
196 if (d[0]) {
197 int idx;
198
3d0b7439 199 ll_init_map(&rth);
aba5acdf 200
b932e6f3 201 idx = ll_name_to_index(d);
fe99adbc
SP
202 if (!idx)
203 return -nodev(d);
aba5acdf
SH
204 req.t.tcm_ifindex = idx;
205 }
206
86bf43c7 207 if (rtnl_talk(&rth, &req.n, NULL) < 0)
11656f1b 208 return 2;
aba5acdf 209
aba5acdf
SH
210 return 0;
211}
212
aba5acdf
SH
213static int filter_ifindex;
214
ae665a52 215int print_qdisc(const struct sockaddr_nl *who,
b932e6f3 216 struct nlmsghdr *n, void *arg)
aba5acdf 217{
32a121cb 218 FILE *fp = (FILE *)arg;
aba5acdf
SH
219 struct tcmsg *t = NLMSG_DATA(n);
220 int len = n->nlmsg_len;
32a121cb 221 struct rtattr *tb[TCA_MAX+1];
aba5acdf
SH
222 struct qdisc_util *q;
223 char abuf[256];
224
225 if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) {
226 fprintf(stderr, "Not a qdisc\n");
227 return 0;
228 }
229 len -= NLMSG_LENGTH(sizeof(*t));
230 if (len < 0) {
231 fprintf(stderr, "Wrong len %d\n", len);
232 return -1;
233 }
234
235 if (filter_ifindex && filter_ifindex != t->tcm_ifindex)
236 return 0;
237
aba5acdf
SH
238 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
239
240 if (tb[TCA_KIND] == NULL) {
2373fde9 241 fprintf(stderr, "print_qdisc: NULL kind\n");
aba5acdf
SH
242 return -1;
243 }
244
c91d262f
JP
245 open_json_object(NULL);
246
aba5acdf 247 if (n->nlmsg_type == RTM_DELQDISC)
c91d262f 248 print_bool(PRINT_ANY, "deleted", "deleted ", true);
aba5acdf 249
274b63ae
RM
250 if (n->nlmsg_type == RTM_NEWQDISC &&
251 (n->nlmsg_flags & NLM_F_CREATE) &&
252 (n->nlmsg_flags & NLM_F_REPLACE))
c91d262f 253 print_bool(PRINT_ANY, "replaced", "replaced ", true);
274b63ae
RM
254
255 if (n->nlmsg_type == RTM_NEWQDISC &&
256 (n->nlmsg_flags & NLM_F_CREATE) &&
257 (n->nlmsg_flags & NLM_F_EXCL))
c91d262f
JP
258 print_bool(PRINT_ANY, "added", "added ", true);
259
260 print_string(PRINT_ANY, "kind", "qdisc %s",
261 rta_getattr_str(tb[TCA_KIND]));
262 sprintf(abuf, "%x:", t->tcm_handle >> 16);
263 print_string(PRINT_ANY, "handle", " %s", abuf);
264 if (show_raw) {
265 sprintf(abuf, "[%08x]", t->tcm_handle);
266 print_string(PRINT_FP, NULL, "%s", abuf);
267 }
268 print_string(PRINT_FP, NULL, " ", NULL);
d42e1444 269
aba5acdf 270 if (filter_ifindex == 0)
2d165c08 271 print_devname(PRINT_ANY, t->tcm_ifindex);
b932e6f3 272
aba5acdf 273 if (t->tcm_parent == TC_H_ROOT)
c91d262f 274 print_bool(PRINT_ANY, "root", "root ", true);
aba5acdf
SH
275 else if (t->tcm_parent) {
276 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
c91d262f 277 print_string(PRINT_ANY, "parent", "parent %s ", abuf);
aba5acdf 278 }
b932e6f3
SH
279
280 if (t->tcm_info != 1)
c91d262f 281 print_uint(PRINT_ANY, "refcnt", "refcnt %u ", t->tcm_info);
ae665a52 282
b97c6fa7
YM
283 if (tb[TCA_HW_OFFLOAD] &&
284 (rta_getattr_u8(tb[TCA_HW_OFFLOAD])))
285 print_bool(PRINT_ANY, "offloaded", "offloaded ", true);
286
063463ef
JP
287 if (tb[TCA_INGRESS_BLOCK] &&
288 RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) {
289 __u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]);
290
291 if (block)
292 print_uint(PRINT_ANY, "ingress_block",
293 "ingress_block %u ", block);
294 }
295
296 if (tb[TCA_EGRESS_BLOCK] &&
297 RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) {
298 __u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]);
299
300 if (block)
301 print_uint(PRINT_ANY, "egress_block",
302 "egress_block %u ", block);
303 }
304
b932e6f3
SH
305 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
306 if (strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])) == 0)
2373fde9
SH
307 q = get_qdisc_kind("prio");
308 else
309 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
ae665a52 310
c91d262f 311 open_json_object("options");
aba5acdf
SH
312 if (tb[TCA_OPTIONS]) {
313 if (q)
314 q->print_qopt(q, fp, tb[TCA_OPTIONS]);
315 else
c91d262f
JP
316 print_string(PRINT_FP, NULL,
317 "[cannot parse qdisc parameters]", NULL);
aba5acdf 318 }
c91d262f
JP
319 close_json_object();
320
321 print_string(PRINT_FP, NULL, "\n", NULL);
b932e6f3 322
839c8456
JK
323 if (show_details && tb[TCA_STAB]) {
324 print_size_table(fp, " ", tb[TCA_STAB]);
c91d262f 325 print_string(PRINT_FP, NULL, "\n", NULL);
839c8456 326 }
b932e6f3 327
aba5acdf 328 if (show_stats) {
e5879dc6 329 struct rtattr *xstats = NULL;
330
331 if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
332 print_tcstats_attr(fp, tb, " ", &xstats);
c91d262f 333 print_string(PRINT_FP, NULL, "\n", NULL);
aba5acdf 334 }
649b0755 335
e5879dc6 336 if (q && xstats && q->print_xstats) {
337 q->print_xstats(q, fp, xstats);
c91d262f 338 print_string(PRINT_FP, NULL, "\n", NULL);
aba5acdf
SH
339 }
340 }
c91d262f 341 close_json_object();
aba5acdf
SH
342 fflush(fp);
343 return 0;
344}
345
d1f28cf1 346static int tc_qdisc_list(int argc, char **argv)
aba5acdf 347{
d17b136f 348 struct tcmsg t = { .tcm_family = AF_UNSPEC };
b317557f 349 char d[IFNAMSIZ] = {};
7c581a12 350 bool dump_invisible = false;
ae665a52 351
aba5acdf
SH
352 while (argc > 0) {
353 if (strcmp(*argv, "dev") == 0) {
354 NEXT_ARG();
355 strncpy(d, *argv, sizeof(d)-1);
32a121cb 356 } else if (strcmp(*argv, "ingress") == 0 ||
8f9afdd5 357 strcmp(*argv, "clsact") == 0) {
b932e6f3
SH
358 if (t.tcm_parent) {
359 fprintf(stderr, "Duplicate parent ID\n");
360 usage();
361 }
362 t.tcm_parent = TC_H_INGRESS;
aba5acdf
SH
363 } else if (matches(*argv, "help") == 0) {
364 usage();
7c581a12
JK
365 } else if (strcmp(*argv, "invisible") == 0) {
366 dump_invisible = true;
aba5acdf
SH
367 } else {
368 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
369 return -1;
370 }
371
372 argc--; argv++;
373 }
374
3d0b7439 375 ll_init_map(&rth);
aba5acdf
SH
376
377 if (d[0]) {
b932e6f3 378 t.tcm_ifindex = ll_name_to_index(d);
fe99adbc
SP
379 if (!t.tcm_ifindex)
380 return -nodev(d);
aba5acdf
SH
381 filter_ifindex = t.tcm_ifindex;
382 }
383
7c581a12
JK
384 if (dump_invisible) {
385 struct {
386 struct nlmsghdr n;
387 struct tcmsg t;
388 char buf[256];
389 } req = {
390 .n.nlmsg_type = RTM_GETQDISC,
391 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
392 };
393
394 req.t.tcm_family = AF_UNSPEC;
395
396 addattr(&req.n, 256, TCA_DUMP_INVISIBLE);
397 if (rtnl_dump_request_n(&rth, &req.n) < 0) {
398 perror("Cannot send dump request");
399 return 1;
400 }
401
402 } else if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
aba5acdf 403 perror("Cannot send dump request");
11656f1b 404 return 1;
aba5acdf
SH
405 }
406
c91d262f 407 new_json_obj(json);
3d0b7439 408 if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) {
aba5acdf 409 fprintf(stderr, "Dump terminated\n");
11656f1b 410 return 1;
aba5acdf 411 }
c91d262f 412 delete_json_obj();
aba5acdf 413
aba5acdf
SH
414 return 0;
415}
416
417int do_qdisc(int argc, char **argv)
418{
419 if (argc < 1)
420 return tc_qdisc_list(0, NULL);
421 if (matches(*argv, "add") == 0)
422 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
423 if (matches(*argv, "change") == 0)
424 return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1);
425 if (matches(*argv, "replace") == 0)
426 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
427 if (matches(*argv, "link") == 0)
428 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1);
429 if (matches(*argv, "delete") == 0)
430 return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1);
431#if 0
432 if (matches(*argv, "get") == 0)
433 return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1);
434#endif
435 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
436 || matches(*argv, "lst") == 0)
437 return tc_qdisc_list(argc-1, argv+1);
e5d179d8 438 if (matches(*argv, "help") == 0) {
aba5acdf 439 usage();
e5d179d8 440 return 0;
32a121cb 441 }
aba5acdf
SH
442 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);
443 return -1;
444}
d0bcedd5
JP
445
446struct tc_qdisc_block_exists_ctx {
447 __u32 block_index;
448 bool found;
449};
450
451static int tc_qdisc_block_exists_cb(const struct sockaddr_nl *who,
452 struct nlmsghdr *n, void *arg)
453{
454 struct tc_qdisc_block_exists_ctx *ctx = arg;
455 struct tcmsg *t = NLMSG_DATA(n);
456 struct rtattr *tb[TCA_MAX+1];
457 int len = n->nlmsg_len;
458
459 if (n->nlmsg_type != RTM_NEWQDISC)
460 return 0;
461
462 len -= NLMSG_LENGTH(sizeof(*t));
463 if (len < 0)
464 return -1;
465
466 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
467
468 if (tb[TCA_KIND] == NULL)
469 return -1;
470
471 if (tb[TCA_INGRESS_BLOCK] &&
472 RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) {
473 __u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]);
474
475 if (block == ctx->block_index)
476 ctx->found = true;
477 }
478
479 if (tb[TCA_EGRESS_BLOCK] &&
480 RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) {
481 __u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]);
482
483 if (block == ctx->block_index)
484 ctx->found = true;
485 }
486 return 0;
487}
488
489bool tc_qdisc_block_exists(__u32 block_index)
490{
491 struct tc_qdisc_block_exists_ctx ctx = { .block_index = block_index };
492 struct tcmsg t = { .tcm_family = AF_UNSPEC };
493
494 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
495 perror("Cannot send dump request");
496 return false;
497 }
498
499 if (rtnl_dump_filter(&rth, tc_qdisc_block_exists_cb, &ctx) < 0) {
500 perror("Dump terminated\n");
501 return false;
502 }
503
504 return ctx.found;
505}