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 | list } [ dev STRING ] [ QDISC_ID ] [ 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"
43 "QDISC_ID := { root | ingress | handle QHANDLE | parent CLASSID }\n");
47 static int tc_qdisc_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
49 struct qdisc_util
*q
= NULL
;
50 struct tc_estimator est
= {};
52 struct tc_sizespec szopts
;
55 char d
[IFNAMSIZ
] = {};
56 char k
[FILTER_NAMESZ
] = {};
60 char buf
[TCA_BUF_MAX
];
62 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
63 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
65 .t
.tcm_family
= AF_UNSPEC
,
67 __u32 ingress_block
= 0;
68 __u32 egress_block
= 0;
71 if (strcmp(*argv
, "dev") == 0) {
75 strncpy(d
, *argv
, sizeof(d
)-1);
76 } else if (strcmp(*argv
, "handle") == 0) {
80 duparg("handle", *argv
);
82 if (get_qdisc_handle(&handle
, *argv
))
83 invarg("invalid qdisc ID", *argv
);
84 req
.t
.tcm_handle
= handle
;
85 } else if (strcmp(*argv
, "root") == 0) {
86 if (req
.t
.tcm_parent
) {
87 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
90 req
.t
.tcm_parent
= TC_H_ROOT
;
91 } else if (strcmp(*argv
, "clsact") == 0) {
92 if (req
.t
.tcm_parent
) {
93 fprintf(stderr
, "Error: \"clsact\" is a duplicate parent ID\n");
96 req
.t
.tcm_parent
= TC_H_CLSACT
;
97 strncpy(k
, "clsact", sizeof(k
) - 1);
98 q
= get_qdisc_kind(k
);
99 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_CLSACT
, 0);
102 } else if (strcmp(*argv
, "ingress") == 0) {
103 if (req
.t
.tcm_parent
) {
104 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
107 req
.t
.tcm_parent
= TC_H_INGRESS
;
108 strncpy(k
, "ingress", sizeof(k
) - 1);
109 q
= get_qdisc_kind(k
);
110 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_INGRESS
, 0);
113 } else if (strcmp(*argv
, "parent") == 0) {
117 if (req
.t
.tcm_parent
)
118 duparg("parent", *argv
);
119 if (get_tc_classid(&handle
, *argv
))
120 invarg("invalid parent ID", *argv
);
121 req
.t
.tcm_parent
= handle
;
122 } else if (matches(*argv
, "estimator") == 0) {
123 if (parse_estimator(&argc
, &argv
, &est
))
125 } else if (matches(*argv
, "stab") == 0) {
126 if (parse_size_table(&argc
, &argv
, &stab
.szopts
) < 0)
129 } else if (matches(*argv
, "ingress_block") == 0) {
131 if (get_u32(&ingress_block
, *argv
, 0) || !ingress_block
)
132 invarg("invalid ingress block index value", *argv
);
133 } else if (matches(*argv
, "egress_block") == 0) {
135 if (get_u32(&egress_block
, *argv
, 0) || !egress_block
)
136 invarg("invalid egress block index value", *argv
);
137 } else if (matches(*argv
, "help") == 0) {
140 strncpy(k
, *argv
, sizeof(k
)-1);
142 q
= get_qdisc_kind(k
);
150 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
152 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
155 addattr32(&req
.n
, sizeof(req
),
156 TCA_INGRESS_BLOCK
, ingress_block
);
158 addattr32(&req
.n
, sizeof(req
),
159 TCA_EGRESS_BLOCK
, egress_block
);
163 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
, d
))
166 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
171 if (matches(*argv
, "help") == 0)
174 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
179 if (check_size_table_opts(&stab
.szopts
)) {
182 if (tc_calc_size_table(&stab
.szopts
, &stab
.data
) < 0) {
183 fprintf(stderr
, "failed to calculate size table.\n");
187 tail
= addattr_nest(&req
.n
, sizeof(req
), TCA_STAB
);
188 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_BASE
, &stab
.szopts
,
189 sizeof(stab
.szopts
));
191 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_DATA
, stab
.data
,
192 stab
.szopts
.tsize
* sizeof(__u16
));
193 addattr_nest_end(&req
.n
, tail
);
203 idx
= ll_name_to_index(d
);
206 req
.t
.tcm_ifindex
= idx
;
209 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
215 static int filter_ifindex
;
216 static __u32 filter_parent
;
217 static __u32 filter_handle
;
219 int print_qdisc(struct nlmsghdr
*n
, void *arg
)
221 FILE *fp
= (FILE *)arg
;
222 struct tcmsg
*t
= NLMSG_DATA(n
);
223 int len
= n
->nlmsg_len
;
224 struct rtattr
*tb
[TCA_MAX
+1];
225 struct qdisc_util
*q
;
228 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
229 fprintf(stderr
, "Not a qdisc\n");
232 len
-= NLMSG_LENGTH(sizeof(*t
));
234 fprintf(stderr
, "Wrong len %d\n", len
);
238 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
241 if (filter_handle
&& filter_handle
!= t
->tcm_handle
)
244 if (filter_parent
&& filter_parent
!= t
->tcm_parent
)
247 parse_rtattr_flags(tb
, TCA_MAX
, TCA_RTA(t
), len
, NLA_F_NESTED
);
249 if (tb
[TCA_KIND
] == NULL
) {
250 fprintf(stderr
, "print_qdisc: NULL kind\n");
254 open_json_object(NULL
);
256 if (n
->nlmsg_type
== RTM_DELQDISC
)
257 print_bool(PRINT_ANY
, "deleted", "deleted ", true);
259 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
260 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
261 (n
->nlmsg_flags
& NLM_F_REPLACE
))
262 print_bool(PRINT_ANY
, "replaced", "replaced ", true);
264 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
265 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
266 (n
->nlmsg_flags
& NLM_F_EXCL
))
267 print_bool(PRINT_ANY
, "added", "added ", true);
269 print_string(PRINT_ANY
, "kind", "qdisc %s",
270 rta_getattr_str(tb
[TCA_KIND
]));
271 sprintf(abuf
, "%x:", t
->tcm_handle
>> 16);
272 print_string(PRINT_ANY
, "handle", " %s", abuf
);
274 sprintf(abuf
, "[%08x]", t
->tcm_handle
);
275 print_string(PRINT_FP
, NULL
, "%s", abuf
);
277 print_string(PRINT_FP
, NULL
, " ", NULL
);
279 if (filter_ifindex
== 0)
280 print_devname(PRINT_ANY
, t
->tcm_ifindex
);
282 if (t
->tcm_parent
== TC_H_ROOT
)
283 print_bool(PRINT_ANY
, "root", "root ", true);
284 else if (t
->tcm_parent
) {
285 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
286 print_string(PRINT_ANY
, "parent", "parent %s ", abuf
);
289 if (t
->tcm_info
!= 1)
290 print_uint(PRINT_ANY
, "refcnt", "refcnt %u ", t
->tcm_info
);
292 if (tb
[TCA_HW_OFFLOAD
] &&
293 (rta_getattr_u8(tb
[TCA_HW_OFFLOAD
])))
294 print_bool(PRINT_ANY
, "offloaded", "offloaded ", true);
296 if (tb
[TCA_INGRESS_BLOCK
] &&
297 RTA_PAYLOAD(tb
[TCA_INGRESS_BLOCK
]) >= sizeof(__u32
)) {
298 __u32 block
= rta_getattr_u32(tb
[TCA_INGRESS_BLOCK
]);
301 print_uint(PRINT_ANY
, "ingress_block",
302 "ingress_block %u ", block
);
305 if (tb
[TCA_EGRESS_BLOCK
] &&
306 RTA_PAYLOAD(tb
[TCA_EGRESS_BLOCK
]) >= sizeof(__u32
)) {
307 __u32 block
= rta_getattr_u32(tb
[TCA_EGRESS_BLOCK
]);
310 print_uint(PRINT_ANY
, "egress_block",
311 "egress_block %u ", block
);
314 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
315 if (strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])) == 0)
316 q
= get_qdisc_kind("prio");
318 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
320 open_json_object("options");
321 if (tb
[TCA_OPTIONS
]) {
323 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
325 fprintf(stderr
, "Cannot parse qdisc parameters\n");
331 if (show_details
&& tb
[TCA_STAB
]) {
332 print_size_table(fp
, " ", tb
[TCA_STAB
]);
337 struct rtattr
*xstats
= NULL
;
339 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
340 print_tcstats_attr(fp
, tb
, " ", &xstats
);
344 if (q
&& xstats
&& q
->print_xstats
) {
345 q
->print_xstats(q
, fp
, xstats
);
354 static int tc_qdisc_list(int argc
, char **argv
)
361 .n
.nlmsg_type
= RTM_GETQDISC
,
362 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
363 .t
.tcm_family
= AF_UNSPEC
,
366 char d
[IFNAMSIZ
] = {};
367 bool dump_invisible
= false;
371 if (strcmp(*argv
, "dev") == 0) {
373 strncpy(d
, *argv
, sizeof(d
)-1);
374 } else if (strcmp(*argv
, "root") == 0) {
376 invarg("parent is already specified", *argv
);
377 else if (filter_handle
)
378 invarg("handle is already specified", *argv
);
379 filter_parent
= TC_H_ROOT
;
380 } else if (strcmp(*argv
, "ingress") == 0 ||
381 strcmp(*argv
, "clsact") == 0) {
383 invarg("parent is already specified", *argv
);
384 else if (filter_handle
)
385 invarg("handle is already specified", *argv
);
386 filter_parent
= TC_H_INGRESS
;
387 } else if (matches(*argv
, "parent") == 0) {
389 invarg("parent is already specified", *argv
);
390 else if (filter_handle
)
391 invarg("handle is already specified", *argv
);
393 if (get_tc_classid(&handle
, *argv
))
394 invarg("invalid parent ID", *argv
);
395 filter_parent
= handle
;
396 } else if (matches(*argv
, "handle") == 0) {
398 invarg("parent is already specified", *argv
);
399 else if (filter_handle
)
400 invarg("handle is already specified", *argv
);
402 if (get_qdisc_handle(&handle
, *argv
))
403 invarg("invalid handle ID", *argv
);
404 filter_handle
= handle
;
405 } else if (matches(*argv
, "help") == 0) {
407 } else if (strcmp(*argv
, "invisible") == 0) {
408 dump_invisible
= true;
410 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
420 req
.t
.tcm_ifindex
= ll_name_to_index(d
);
421 if (!req
.t
.tcm_ifindex
)
423 filter_ifindex
= req
.t
.tcm_ifindex
;
426 if (dump_invisible
) {
427 addattr(&req
.n
, 256, TCA_DUMP_INVISIBLE
);
430 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
431 perror("Cannot send request");
436 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
) < 0) {
437 fprintf(stderr
, "Dump terminated\n");
445 int do_qdisc(int argc
, char **argv
)
448 return tc_qdisc_list(0, NULL
);
449 if (matches(*argv
, "add") == 0)
450 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
451 if (matches(*argv
, "change") == 0)
452 return tc_qdisc_modify(RTM_NEWQDISC
, 0, argc
-1, argv
+1);
453 if (matches(*argv
, "replace") == 0)
454 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
455 if (matches(*argv
, "link") == 0)
456 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_REPLACE
, argc
-1, argv
+1);
457 if (matches(*argv
, "delete") == 0)
458 return tc_qdisc_modify(RTM_DELQDISC
, 0, argc
-1, argv
+1);
459 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
460 || matches(*argv
, "lst") == 0)
461 return tc_qdisc_list(argc
-1, argv
+1);
462 if (matches(*argv
, "help") == 0) {
466 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);
470 struct tc_qdisc_block_exists_ctx
{
475 static int tc_qdisc_block_exists_cb(struct nlmsghdr
*n
, void *arg
)
477 struct tc_qdisc_block_exists_ctx
*ctx
= arg
;
478 struct tcmsg
*t
= NLMSG_DATA(n
);
479 struct rtattr
*tb
[TCA_MAX
+1];
480 int len
= n
->nlmsg_len
;
481 struct qdisc_util
*q
;
485 if (n
->nlmsg_type
!= RTM_NEWQDISC
)
488 len
-= NLMSG_LENGTH(sizeof(*t
));
492 parse_rtattr_flags(tb
, TCA_MAX
, TCA_RTA(t
), len
, NLA_F_NESTED
);
494 if (tb
[TCA_KIND
] == NULL
)
497 if (tb
[TCA_INGRESS_BLOCK
] &&
498 RTA_PAYLOAD(tb
[TCA_INGRESS_BLOCK
]) >= sizeof(__u32
)) {
499 __u32 block
= rta_getattr_u32(tb
[TCA_INGRESS_BLOCK
]);
501 if (block
== ctx
->block_index
)
505 if (tb
[TCA_EGRESS_BLOCK
] &&
506 RTA_PAYLOAD(tb
[TCA_EGRESS_BLOCK
]) >= sizeof(__u32
)) {
507 __u32 block
= rta_getattr_u32(tb
[TCA_EGRESS_BLOCK
]);
509 if (block
== ctx
->block_index
)
513 kind
= rta_getattr_str(tb
[TCA_KIND
]);
514 q
= get_qdisc_kind(kind
);
520 err
= q
->has_block(q
, tb
[TCA_OPTIONS
], ctx
->block_index
, &found
);
530 bool tc_qdisc_block_exists(__u32 block_index
)
532 struct tc_qdisc_block_exists_ctx ctx
= { .block_index
= block_index
};
533 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
535 if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
536 perror("Cannot send dump request");
540 if (rtnl_dump_filter(&rth
, tc_qdisc_block_exists_cb
, &ctx
) < 0) {
541 perror("Dump terminated\n");