]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_qdisc.c
uapi: update bpf.h
[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{
8589eb4e
MC
30 fprintf(stderr,
31 "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n"
32 " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n"
33 " [ estimator INTERVAL TIME_CONSTANT ]\n"
34 " [ stab [ help | STAB_OPTIONS] ]\n"
35 " [ ingress_block BLOCK_INDEX ] [ egress_block BLOCK_INDEX ]\n"
36 " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n"
37 "\n"
38 " tc qdisc show [ dev STRING ] [ ingress | clsact ] [ invisible ]\n"
39 "Where:\n"
40 "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n"
41 "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n"
42 "STAB_OPTIONS := ... try tc qdisc add stab help\n");
11656f1b 43 return -1;
aba5acdf
SH
44}
45
32a121cb 46static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
aba5acdf 47{
aba5acdf 48 struct qdisc_util *q = NULL;
9579afb2 49 struct tc_estimator est = {};
839c8456
JK
50 struct {
51 struct tc_sizespec szopts;
52 __u16 *data;
d17b136f 53 } stab = {};
b317557f
SH
54 char d[IFNAMSIZ] = {};
55 char k[FILTER_NAMESZ] = {};
f307c24e 56 struct {
32a121cb
SH
57 struct nlmsghdr n;
58 struct tcmsg t;
59 char buf[TCA_BUF_MAX];
d17b136f
PS
60 } req = {
61 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
62 .n.nlmsg_flags = NLM_F_REQUEST | flags,
63 .n.nlmsg_type = cmd,
64 .t.tcm_family = AF_UNSPEC,
65 };
063463ef
JP
66 __u32 ingress_block = 0;
67 __u32 egress_block = 0;
aba5acdf
SH
68
69 while (argc > 0) {
70 if (strcmp(*argv, "dev") == 0) {
71 NEXT_ARG();
72 if (d[0])
73 duparg("dev", *argv);
74 strncpy(d, *argv, sizeof(d)-1);
75 } else if (strcmp(*argv, "handle") == 0) {
76 __u32 handle;
32a121cb 77
aba5acdf
SH
78 if (req.t.tcm_handle)
79 duparg("handle", *argv);
80 NEXT_ARG();
81 if (get_qdisc_handle(&handle, *argv))
f1675d61 82 invarg("invalid qdisc ID", *argv);
aba5acdf
SH
83 req.t.tcm_handle = handle;
84 } else if (strcmp(*argv, "root") == 0) {
85 if (req.t.tcm_parent) {
86 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
11656f1b 87 return -1;
aba5acdf
SH
88 }
89 req.t.tcm_parent = TC_H_ROOT;
8f9afdd5
DB
90 } else if (strcmp(*argv, "clsact") == 0) {
91 if (req.t.tcm_parent) {
92 fprintf(stderr, "Error: \"clsact\" is a duplicate parent ID\n");
93 return -1;
94 }
95 req.t.tcm_parent = TC_H_CLSACT;
96 strncpy(k, "clsact", sizeof(k) - 1);
97 q = get_qdisc_kind(k);
98 req.t.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
99 NEXT_ARG_FWD();
100 break;
aba5acdf
SH
101 } else if (strcmp(*argv, "ingress") == 0) {
102 if (req.t.tcm_parent) {
103 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n");
11656f1b 104 return -1;
aba5acdf
SH
105 }
106 req.t.tcm_parent = TC_H_INGRESS;
0d45c4b4 107 strncpy(k, "ingress", sizeof(k) - 1);
aba5acdf 108 q = get_qdisc_kind(k);
0d45c4b4
DB
109 req.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
110 NEXT_ARG_FWD();
aba5acdf 111 break;
aba5acdf
SH
112 } else if (strcmp(*argv, "parent") == 0) {
113 __u32 handle;
32a121cb 114
aba5acdf
SH
115 NEXT_ARG();
116 if (req.t.tcm_parent)
117 duparg("parent", *argv);
118 if (get_tc_classid(&handle, *argv))
f1675d61 119 invarg("invalid parent ID", *argv);
aba5acdf
SH
120 req.t.tcm_parent = handle;
121 } else if (matches(*argv, "estimator") == 0) {
122 if (parse_estimator(&argc, &argv, &est))
123 return -1;
839c8456
JK
124 } else if (matches(*argv, "stab") == 0) {
125 if (parse_size_table(&argc, &argv, &stab.szopts) < 0)
126 return -1;
127 continue;
063463ef
JP
128 } else if (matches(*argv, "ingress_block") == 0) {
129 NEXT_ARG();
130 if (get_u32(&ingress_block, *argv, 0) || !ingress_block)
131 invarg("invalid ingress block index value", *argv);
132 } else if (matches(*argv, "egress_block") == 0) {
133 NEXT_ARG();
134 if (get_u32(&egress_block, *argv, 0) || !egress_block)
135 invarg("invalid egress block index value", *argv);
aba5acdf
SH
136 } else if (matches(*argv, "help") == 0) {
137 usage();
138 } else {
139 strncpy(k, *argv, sizeof(k)-1);
140
141 q = get_qdisc_kind(k);
142 argc--; argv++;
143 break;
144 }
145 argc--; argv++;
146 }
147
148 if (k[0])
149 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
150 if (est.ewma_log)
151 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
152
063463ef
JP
153 if (ingress_block)
154 addattr32(&req.n, sizeof(req),
155 TCA_INGRESS_BLOCK, ingress_block);
156 if (egress_block)
157 addattr32(&req.n, sizeof(req),
158 TCA_EGRESS_BLOCK, egress_block);
159
0a502b21
SH
160 if (q) {
161 if (q->parse_qopt) {
927e3cfb 162 if (q->parse_qopt(q, argc, argv, &req.n, d))
e9e78b0d 163 return 1;
0a502b21
SH
164 } else if (argc) {
165 fprintf(stderr, "qdisc '%s' does not support option parsing\n", k);
166 return -1;
167 }
168 } else {
169 if (argc) {
aba5acdf
SH
170 if (matches(*argv, "help") == 0)
171 usage();
172
173 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv);
11656f1b 174 return -1;
aba5acdf
SH
175 }
176 }
177
839c8456
JK
178 if (check_size_table_opts(&stab.szopts)) {
179 struct rtattr *tail;
180
181 if (tc_calc_size_table(&stab.szopts, &stab.data) < 0) {
182 fprintf(stderr, "failed to calculate size table.\n");
183 return -1;
184 }
185
c14f9d92 186 tail = addattr_nest(&req.n, sizeof(req), TCA_STAB);
839c8456
JK
187 addattr_l(&req.n, sizeof(req), TCA_STAB_BASE, &stab.szopts,
188 sizeof(stab.szopts));
189 if (stab.data)
190 addattr_l(&req.n, sizeof(req), TCA_STAB_DATA, stab.data,
191 stab.szopts.tsize * sizeof(__u16));
c14f9d92 192 addattr_nest_end(&req.n, tail);
839c8456
JK
193 if (stab.data)
194 free(stab.data);
195 }
196
aba5acdf
SH
197 if (d[0]) {
198 int idx;
199
3d0b7439 200 ll_init_map(&rth);
aba5acdf 201
b932e6f3 202 idx = ll_name_to_index(d);
fe99adbc
SP
203 if (!idx)
204 return -nodev(d);
aba5acdf
SH
205 req.t.tcm_ifindex = idx;
206 }
207
86bf43c7 208 if (rtnl_talk(&rth, &req.n, NULL) < 0)
11656f1b 209 return 2;
aba5acdf 210
aba5acdf
SH
211 return 0;
212}
213
aba5acdf
SH
214static int filter_ifindex;
215
cd554f2c 216int print_qdisc(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
eae5f4b5 238 parse_rtattr_flags(tb, TCA_MAX, TCA_RTA(t), len, NLA_F_NESTED);
aba5acdf
SH
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
d5ddb441 316 fprintf(stderr, "Cannot parse qdisc parameters\n");
aba5acdf 317 }
c91d262f
JP
318 close_json_object();
319
7b0d424a 320 print_nl();
b932e6f3 321
839c8456
JK
322 if (show_details && tb[TCA_STAB]) {
323 print_size_table(fp, " ", tb[TCA_STAB]);
7b0d424a 324 print_nl();
839c8456 325 }
b932e6f3 326
aba5acdf 327 if (show_stats) {
e5879dc6 328 struct rtattr *xstats = NULL;
329
330 if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
331 print_tcstats_attr(fp, tb, " ", &xstats);
7b0d424a 332 print_nl();
aba5acdf 333 }
649b0755 334
e5879dc6 335 if (q && xstats && q->print_xstats) {
336 q->print_xstats(q, fp, xstats);
7b0d424a 337 print_nl();
aba5acdf
SH
338 }
339 }
c91d262f 340 close_json_object();
aba5acdf
SH
341 fflush(fp);
342 return 0;
343}
344
d1f28cf1 345static int tc_qdisc_list(int argc, char **argv)
aba5acdf 346{
d17b136f 347 struct tcmsg t = { .tcm_family = AF_UNSPEC };
b317557f 348 char d[IFNAMSIZ] = {};
7c581a12 349 bool dump_invisible = false;
ae665a52 350
aba5acdf
SH
351 while (argc > 0) {
352 if (strcmp(*argv, "dev") == 0) {
353 NEXT_ARG();
354 strncpy(d, *argv, sizeof(d)-1);
32a121cb 355 } else if (strcmp(*argv, "ingress") == 0 ||
8f9afdd5 356 strcmp(*argv, "clsact") == 0) {
b932e6f3
SH
357 if (t.tcm_parent) {
358 fprintf(stderr, "Duplicate parent ID\n");
359 usage();
360 }
361 t.tcm_parent = TC_H_INGRESS;
aba5acdf
SH
362 } else if (matches(*argv, "help") == 0) {
363 usage();
7c581a12
JK
364 } else if (strcmp(*argv, "invisible") == 0) {
365 dump_invisible = true;
aba5acdf
SH
366 } else {
367 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
368 return -1;
369 }
370
371 argc--; argv++;
372 }
373
3d0b7439 374 ll_init_map(&rth);
aba5acdf
SH
375
376 if (d[0]) {
b932e6f3 377 t.tcm_ifindex = ll_name_to_index(d);
fe99adbc
SP
378 if (!t.tcm_ifindex)
379 return -nodev(d);
aba5acdf
SH
380 filter_ifindex = t.tcm_ifindex;
381 }
382
7c581a12
JK
383 if (dump_invisible) {
384 struct {
385 struct nlmsghdr n;
386 struct tcmsg t;
387 char buf[256];
388 } req = {
389 .n.nlmsg_type = RTM_GETQDISC,
390 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
391 };
392
393 req.t.tcm_family = AF_UNSPEC;
394
395 addattr(&req.n, 256, TCA_DUMP_INVISIBLE);
396 if (rtnl_dump_request_n(&rth, &req.n) < 0) {
397 perror("Cannot send dump request");
398 return 1;
399 }
400
401 } else if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
aba5acdf 402 perror("Cannot send dump request");
11656f1b 403 return 1;
aba5acdf
SH
404 }
405
c91d262f 406 new_json_obj(json);
3d0b7439 407 if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) {
aba5acdf 408 fprintf(stderr, "Dump terminated\n");
11656f1b 409 return 1;
aba5acdf 410 }
c91d262f 411 delete_json_obj();
aba5acdf 412
aba5acdf
SH
413 return 0;
414}
415
416int do_qdisc(int argc, char **argv)
417{
418 if (argc < 1)
419 return tc_qdisc_list(0, NULL);
420 if (matches(*argv, "add") == 0)
421 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
422 if (matches(*argv, "change") == 0)
423 return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1);
424 if (matches(*argv, "replace") == 0)
425 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
426 if (matches(*argv, "link") == 0)
427 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1);
428 if (matches(*argv, "delete") == 0)
429 return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1);
430#if 0
431 if (matches(*argv, "get") == 0)
432 return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1);
433#endif
434 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
435 || matches(*argv, "lst") == 0)
436 return tc_qdisc_list(argc-1, argv+1);
e5d179d8 437 if (matches(*argv, "help") == 0) {
aba5acdf 438 usage();
e5d179d8 439 return 0;
32a121cb 440 }
aba5acdf
SH
441 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);
442 return -1;
443}
d0bcedd5
JP
444
445struct tc_qdisc_block_exists_ctx {
446 __u32 block_index;
447 bool found;
448};
449
cd554f2c 450static int tc_qdisc_block_exists_cb(struct nlmsghdr *n, void *arg)
d0bcedd5
JP
451{
452 struct tc_qdisc_block_exists_ctx *ctx = arg;
453 struct tcmsg *t = NLMSG_DATA(n);
454 struct rtattr *tb[TCA_MAX+1];
455 int len = n->nlmsg_len;
456
457 if (n->nlmsg_type != RTM_NEWQDISC)
458 return 0;
459
460 len -= NLMSG_LENGTH(sizeof(*t));
461 if (len < 0)
462 return -1;
463
eae5f4b5 464 parse_rtattr_flags(tb, TCA_MAX, TCA_RTA(t), len, NLA_F_NESTED);
d0bcedd5
JP
465
466 if (tb[TCA_KIND] == NULL)
467 return -1;
468
469 if (tb[TCA_INGRESS_BLOCK] &&
470 RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) {
471 __u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]);
472
473 if (block == ctx->block_index)
474 ctx->found = true;
475 }
476
477 if (tb[TCA_EGRESS_BLOCK] &&
478 RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) {
479 __u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]);
480
481 if (block == ctx->block_index)
482 ctx->found = true;
483 }
484 return 0;
485}
486
487bool tc_qdisc_block_exists(__u32 block_index)
488{
489 struct tc_qdisc_block_exists_ctx ctx = { .block_index = block_index };
490 struct tcmsg t = { .tcm_family = AF_UNSPEC };
491
492 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
493 perror("Cannot send dump request");
494 return false;
495 }
496
497 if (rtnl_dump_filter(&rth, tc_qdisc_block_exists_cb, &ctx) < 0) {
498 perror("Dump terminated\n");
499 return false;
500 }
501
502 return ctx.found;
503}