2 * tc_qdisc.c "tc qdisc".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * J Hadi Salim: Extension to ingress
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
26 #include "tc_common.h"
28 static int usage(void)
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"
38 " tc qdisc show [ dev STRING ] [ ingress | clsact ] [ invisible ]\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");
46 static int tc_qdisc_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
48 struct qdisc_util
*q
= NULL
;
49 struct tc_estimator est
= {};
51 struct tc_sizespec szopts
;
54 char d
[IFNAMSIZ
] = {};
55 char k
[FILTER_NAMESZ
] = {};
59 char buf
[TCA_BUF_MAX
];
61 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
62 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
64 .t
.tcm_family
= AF_UNSPEC
,
66 __u32 ingress_block
= 0;
67 __u32 egress_block
= 0;
70 if (strcmp(*argv
, "dev") == 0) {
74 strncpy(d
, *argv
, sizeof(d
)-1);
75 } else if (strcmp(*argv
, "handle") == 0) {
79 duparg("handle", *argv
);
81 if (get_qdisc_handle(&handle
, *argv
))
82 invarg("invalid qdisc ID", *argv
);
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");
89 req
.t
.tcm_parent
= TC_H_ROOT
;
90 } else if (strcmp(*argv
, "clsact") == 0) {
91 if (req
.t
.tcm_parent
) {
92 fprintf(stderr
, "Error: \"clsact\" is a duplicate parent ID\n");
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);
101 } else if (strcmp(*argv
, "ingress") == 0) {
102 if (req
.t
.tcm_parent
) {
103 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
106 req
.t
.tcm_parent
= TC_H_INGRESS
;
107 strncpy(k
, "ingress", sizeof(k
) - 1);
108 q
= get_qdisc_kind(k
);
109 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_INGRESS
, 0);
112 } else if (strcmp(*argv
, "parent") == 0) {
116 if (req
.t
.tcm_parent
)
117 duparg("parent", *argv
);
118 if (get_tc_classid(&handle
, *argv
))
119 invarg("invalid parent ID", *argv
);
120 req
.t
.tcm_parent
= handle
;
121 } else if (matches(*argv
, "estimator") == 0) {
122 if (parse_estimator(&argc
, &argv
, &est
))
124 } else if (matches(*argv
, "stab") == 0) {
125 if (parse_size_table(&argc
, &argv
, &stab
.szopts
) < 0)
128 } else if (matches(*argv
, "ingress_block") == 0) {
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) {
134 if (get_u32(&egress_block
, *argv
, 0) || !egress_block
)
135 invarg("invalid egress block index value", *argv
);
136 } else if (matches(*argv
, "help") == 0) {
139 strncpy(k
, *argv
, sizeof(k
)-1);
141 q
= get_qdisc_kind(k
);
149 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
151 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
154 addattr32(&req
.n
, sizeof(req
),
155 TCA_INGRESS_BLOCK
, ingress_block
);
157 addattr32(&req
.n
, sizeof(req
),
158 TCA_EGRESS_BLOCK
, egress_block
);
162 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
, d
))
165 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
170 if (matches(*argv
, "help") == 0)
173 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
178 if (check_size_table_opts(&stab
.szopts
)) {
181 if (tc_calc_size_table(&stab
.szopts
, &stab
.data
) < 0) {
182 fprintf(stderr
, "failed to calculate size table.\n");
186 tail
= addattr_nest(&req
.n
, sizeof(req
), TCA_STAB
);
187 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_BASE
, &stab
.szopts
,
188 sizeof(stab
.szopts
));
190 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_DATA
, stab
.data
,
191 stab
.szopts
.tsize
* sizeof(__u16
));
192 addattr_nest_end(&req
.n
, tail
);
202 idx
= ll_name_to_index(d
);
205 req
.t
.tcm_ifindex
= idx
;
208 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
214 static int filter_ifindex
;
216 int print_qdisc(struct nlmsghdr
*n
, void *arg
)
218 FILE *fp
= (FILE *)arg
;
219 struct tcmsg
*t
= NLMSG_DATA(n
);
220 int len
= n
->nlmsg_len
;
221 struct rtattr
*tb
[TCA_MAX
+1];
222 struct qdisc_util
*q
;
225 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
226 fprintf(stderr
, "Not a qdisc\n");
229 len
-= NLMSG_LENGTH(sizeof(*t
));
231 fprintf(stderr
, "Wrong len %d\n", len
);
235 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
238 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
240 if (tb
[TCA_KIND
] == NULL
) {
241 fprintf(stderr
, "print_qdisc: NULL kind\n");
245 open_json_object(NULL
);
247 if (n
->nlmsg_type
== RTM_DELQDISC
)
248 print_bool(PRINT_ANY
, "deleted", "deleted ", true);
250 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
251 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
252 (n
->nlmsg_flags
& NLM_F_REPLACE
))
253 print_bool(PRINT_ANY
, "replaced", "replaced ", true);
255 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
256 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
257 (n
->nlmsg_flags
& NLM_F_EXCL
))
258 print_bool(PRINT_ANY
, "added", "added ", true);
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
);
265 sprintf(abuf
, "[%08x]", t
->tcm_handle
);
266 print_string(PRINT_FP
, NULL
, "%s", abuf
);
268 print_string(PRINT_FP
, NULL
, " ", NULL
);
270 if (filter_ifindex
== 0)
271 print_devname(PRINT_ANY
, t
->tcm_ifindex
);
273 if (t
->tcm_parent
== TC_H_ROOT
)
274 print_bool(PRINT_ANY
, "root", "root ", true);
275 else if (t
->tcm_parent
) {
276 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
277 print_string(PRINT_ANY
, "parent", "parent %s ", abuf
);
280 if (t
->tcm_info
!= 1)
281 print_uint(PRINT_ANY
, "refcnt", "refcnt %u ", t
->tcm_info
);
283 if (tb
[TCA_HW_OFFLOAD
] &&
284 (rta_getattr_u8(tb
[TCA_HW_OFFLOAD
])))
285 print_bool(PRINT_ANY
, "offloaded", "offloaded ", true);
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
]);
292 print_uint(PRINT_ANY
, "ingress_block",
293 "ingress_block %u ", block
);
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
]);
301 print_uint(PRINT_ANY
, "egress_block",
302 "egress_block %u ", block
);
305 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
306 if (strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])) == 0)
307 q
= get_qdisc_kind("prio");
309 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
311 open_json_object("options");
312 if (tb
[TCA_OPTIONS
]) {
314 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
316 print_string(PRINT_FP
, NULL
,
317 "[cannot parse qdisc parameters]", NULL
);
321 print_string(PRINT_FP
, NULL
, "\n", NULL
);
323 if (show_details
&& tb
[TCA_STAB
]) {
324 print_size_table(fp
, " ", tb
[TCA_STAB
]);
325 print_string(PRINT_FP
, NULL
, "\n", NULL
);
329 struct rtattr
*xstats
= NULL
;
331 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
332 print_tcstats_attr(fp
, tb
, " ", &xstats
);
333 print_string(PRINT_FP
, NULL
, "\n", NULL
);
336 if (q
&& xstats
&& q
->print_xstats
) {
337 q
->print_xstats(q
, fp
, xstats
);
338 print_string(PRINT_FP
, NULL
, "\n", NULL
);
346 static int tc_qdisc_list(int argc
, char **argv
)
348 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
349 char d
[IFNAMSIZ
] = {};
350 bool dump_invisible
= false;
353 if (strcmp(*argv
, "dev") == 0) {
355 strncpy(d
, *argv
, sizeof(d
)-1);
356 } else if (strcmp(*argv
, "ingress") == 0 ||
357 strcmp(*argv
, "clsact") == 0) {
359 fprintf(stderr
, "Duplicate parent ID\n");
362 t
.tcm_parent
= TC_H_INGRESS
;
363 } else if (matches(*argv
, "help") == 0) {
365 } else if (strcmp(*argv
, "invisible") == 0) {
366 dump_invisible
= true;
368 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
378 t
.tcm_ifindex
= ll_name_to_index(d
);
381 filter_ifindex
= t
.tcm_ifindex
;
384 if (dump_invisible
) {
390 .n
.nlmsg_type
= RTM_GETQDISC
,
391 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
394 req
.t
.tcm_family
= AF_UNSPEC
;
396 addattr(&req
.n
, 256, TCA_DUMP_INVISIBLE
);
397 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
398 perror("Cannot send dump request");
402 } else if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
403 perror("Cannot send dump request");
408 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
) < 0) {
409 fprintf(stderr
, "Dump terminated\n");
417 int do_qdisc(int argc
, char **argv
)
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);
432 if (matches(*argv
, "get") == 0)
433 return tc_qdisc_get(RTM_GETQDISC
, 0, argc
-1, argv
+1);
435 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
436 || matches(*argv
, "lst") == 0)
437 return tc_qdisc_list(argc
-1, argv
+1);
438 if (matches(*argv
, "help") == 0) {
442 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);
446 struct tc_qdisc_block_exists_ctx
{
451 static int tc_qdisc_block_exists_cb(struct nlmsghdr
*n
, void *arg
)
453 struct tc_qdisc_block_exists_ctx
*ctx
= arg
;
454 struct tcmsg
*t
= NLMSG_DATA(n
);
455 struct rtattr
*tb
[TCA_MAX
+1];
456 int len
= n
->nlmsg_len
;
458 if (n
->nlmsg_type
!= RTM_NEWQDISC
)
461 len
-= NLMSG_LENGTH(sizeof(*t
));
465 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
467 if (tb
[TCA_KIND
] == NULL
)
470 if (tb
[TCA_INGRESS_BLOCK
] &&
471 RTA_PAYLOAD(tb
[TCA_INGRESS_BLOCK
]) >= sizeof(__u32
)) {
472 __u32 block
= rta_getattr_u32(tb
[TCA_INGRESS_BLOCK
]);
474 if (block
== ctx
->block_index
)
478 if (tb
[TCA_EGRESS_BLOCK
] &&
479 RTA_PAYLOAD(tb
[TCA_EGRESS_BLOCK
]) >= sizeof(__u32
)) {
480 __u32 block
= rta_getattr_u32(tb
[TCA_EGRESS_BLOCK
]);
482 if (block
== ctx
->block_index
)
488 bool tc_qdisc_block_exists(__u32 block_index
)
490 struct tc_qdisc_block_exists_ctx ctx
= { .block_index
= block_index
};
491 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
493 if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
494 perror("Cannot send dump request");
498 if (rtnl_dump_filter(&rth
, tc_qdisc_block_exists_cb
, &ctx
) < 0) {
499 perror("Dump terminated\n");