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)
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
, " [ ingress_block BLOCK_INDEX ] [ egress_block BLOCK_INDEX ]\n");
35 fprintf(stderr
, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr
, "\n");
37 fprintf(stderr
, " tc qdisc show [ dev STRING ] [ ingress | clsact ] [ invisible ]\n");
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");
41 fprintf(stderr
, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
45 static int tc_qdisc_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
47 struct qdisc_util
*q
= NULL
;
48 struct tc_estimator est
= {};
50 struct tc_sizespec szopts
;
53 char d
[IFNAMSIZ
] = {};
54 char k
[FILTER_NAMESZ
] = {};
58 char buf
[TCA_BUF_MAX
];
60 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
61 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
63 .t
.tcm_family
= AF_UNSPEC
,
65 __u32 ingress_block
= 0;
66 __u32 egress_block
= 0;
69 if (strcmp(*argv
, "dev") == 0) {
73 strncpy(d
, *argv
, sizeof(d
)-1);
74 } else if (strcmp(*argv
, "handle") == 0) {
78 duparg("handle", *argv
);
80 if (get_qdisc_handle(&handle
, *argv
))
81 invarg("invalid qdisc ID", *argv
);
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");
88 req
.t
.tcm_parent
= TC_H_ROOT
;
89 } else if (strcmp(*argv
, "clsact") == 0) {
90 if (req
.t
.tcm_parent
) {
91 fprintf(stderr
, "Error: \"clsact\" is a duplicate parent ID\n");
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);
100 } else if (strcmp(*argv
, "ingress") == 0) {
101 if (req
.t
.tcm_parent
) {
102 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
105 req
.t
.tcm_parent
= TC_H_INGRESS
;
106 strncpy(k
, "ingress", sizeof(k
) - 1);
107 q
= get_qdisc_kind(k
);
108 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_INGRESS
, 0);
111 } else if (strcmp(*argv
, "parent") == 0) {
115 if (req
.t
.tcm_parent
)
116 duparg("parent", *argv
);
117 if (get_tc_classid(&handle
, *argv
))
118 invarg("invalid parent ID", *argv
);
119 req
.t
.tcm_parent
= handle
;
120 } else if (matches(*argv
, "estimator") == 0) {
121 if (parse_estimator(&argc
, &argv
, &est
))
123 } else if (matches(*argv
, "stab") == 0) {
124 if (parse_size_table(&argc
, &argv
, &stab
.szopts
) < 0)
127 } else if (matches(*argv
, "ingress_block") == 0) {
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) {
133 if (get_u32(&egress_block
, *argv
, 0) || !egress_block
)
134 invarg("invalid egress block index value", *argv
);
135 } else if (matches(*argv
, "help") == 0) {
138 strncpy(k
, *argv
, sizeof(k
)-1);
140 q
= get_qdisc_kind(k
);
148 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
150 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
153 addattr32(&req
.n
, sizeof(req
),
154 TCA_INGRESS_BLOCK
, ingress_block
);
156 addattr32(&req
.n
, sizeof(req
),
157 TCA_EGRESS_BLOCK
, egress_block
);
161 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
, d
))
164 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
169 if (matches(*argv
, "help") == 0)
172 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
177 if (check_size_table_opts(&stab
.szopts
)) {
180 if (tc_calc_size_table(&stab
.szopts
, &stab
.data
) < 0) {
181 fprintf(stderr
, "failed to calculate size table.\n");
185 tail
= addattr_nest(&req
.n
, sizeof(req
), TCA_STAB
);
186 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_BASE
, &stab
.szopts
,
187 sizeof(stab
.szopts
));
189 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_DATA
, stab
.data
,
190 stab
.szopts
.tsize
* sizeof(__u16
));
191 addattr_nest_end(&req
.n
, tail
);
201 idx
= ll_name_to_index(d
);
203 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
206 req
.t
.tcm_ifindex
= idx
;
209 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
215 static int filter_ifindex
;
217 int print_qdisc(const struct sockaddr_nl
*who
,
218 struct nlmsghdr
*n
, void *arg
)
220 FILE *fp
= (FILE *)arg
;
221 struct tcmsg
*t
= NLMSG_DATA(n
);
222 int len
= n
->nlmsg_len
;
223 struct rtattr
*tb
[TCA_MAX
+1];
224 struct qdisc_util
*q
;
227 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
228 fprintf(stderr
, "Not a qdisc\n");
231 len
-= NLMSG_LENGTH(sizeof(*t
));
233 fprintf(stderr
, "Wrong len %d\n", len
);
237 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
240 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
242 if (tb
[TCA_KIND
] == NULL
) {
243 fprintf(stderr
, "print_qdisc: NULL kind\n");
247 open_json_object(NULL
);
249 if (n
->nlmsg_type
== RTM_DELQDISC
)
250 print_bool(PRINT_ANY
, "deleted", "deleted ", true);
252 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
253 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
254 (n
->nlmsg_flags
& NLM_F_REPLACE
))
255 print_bool(PRINT_ANY
, "replaced", "replaced ", true);
257 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
258 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
259 (n
->nlmsg_flags
& NLM_F_EXCL
))
260 print_bool(PRINT_ANY
, "added", "added ", true);
262 print_string(PRINT_ANY
, "kind", "qdisc %s",
263 rta_getattr_str(tb
[TCA_KIND
]));
264 sprintf(abuf
, "%x:", t
->tcm_handle
>> 16);
265 print_string(PRINT_ANY
, "handle", " %s", abuf
);
267 sprintf(abuf
, "[%08x]", t
->tcm_handle
);
268 print_string(PRINT_FP
, NULL
, "%s", abuf
);
270 print_string(PRINT_FP
, NULL
, " ", NULL
);
272 if (filter_ifindex
== 0)
273 print_devname(PRINT_ANY
, t
->tcm_ifindex
);
275 if (t
->tcm_parent
== TC_H_ROOT
)
276 print_bool(PRINT_ANY
, "root", "root ", true);
277 else if (t
->tcm_parent
) {
278 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
279 print_string(PRINT_ANY
, "parent", "parent %s ", abuf
);
282 if (t
->tcm_info
!= 1)
283 print_uint(PRINT_ANY
, "refcnt", "refcnt %u ", t
->tcm_info
);
285 if (tb
[TCA_HW_OFFLOAD
] &&
286 (rta_getattr_u8(tb
[TCA_HW_OFFLOAD
])))
287 print_bool(PRINT_ANY
, "offloaded", "offloaded ", true);
289 if (tb
[TCA_INGRESS_BLOCK
] &&
290 RTA_PAYLOAD(tb
[TCA_INGRESS_BLOCK
]) >= sizeof(__u32
)) {
291 __u32 block
= rta_getattr_u32(tb
[TCA_INGRESS_BLOCK
]);
294 print_uint(PRINT_ANY
, "ingress_block",
295 "ingress_block %u ", block
);
298 if (tb
[TCA_EGRESS_BLOCK
] &&
299 RTA_PAYLOAD(tb
[TCA_EGRESS_BLOCK
]) >= sizeof(__u32
)) {
300 __u32 block
= rta_getattr_u32(tb
[TCA_EGRESS_BLOCK
]);
303 print_uint(PRINT_ANY
, "egress_block",
304 "egress_block %u ", block
);
307 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
308 if (strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])) == 0)
309 q
= get_qdisc_kind("prio");
311 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
313 open_json_object("options");
314 if (tb
[TCA_OPTIONS
]) {
316 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
318 print_string(PRINT_FP
, NULL
,
319 "[cannot parse qdisc parameters]", NULL
);
323 print_string(PRINT_FP
, NULL
, "\n", NULL
);
325 if (show_details
&& tb
[TCA_STAB
]) {
326 print_size_table(fp
, " ", tb
[TCA_STAB
]);
327 print_string(PRINT_FP
, NULL
, "\n", NULL
);
331 struct rtattr
*xstats
= NULL
;
333 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
334 print_tcstats_attr(fp
, tb
, " ", &xstats
);
335 print_string(PRINT_FP
, NULL
, "\n", NULL
);
338 if (q
&& xstats
&& q
->print_xstats
) {
339 q
->print_xstats(q
, fp
, xstats
);
340 print_string(PRINT_FP
, NULL
, "\n", NULL
);
348 static int tc_qdisc_list(int argc
, char **argv
)
350 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
351 char d
[IFNAMSIZ
] = {};
352 bool dump_invisible
= false;
355 if (strcmp(*argv
, "dev") == 0) {
357 strncpy(d
, *argv
, sizeof(d
)-1);
358 } else if (strcmp(*argv
, "ingress") == 0 ||
359 strcmp(*argv
, "clsact") == 0) {
361 fprintf(stderr
, "Duplicate parent ID\n");
364 t
.tcm_parent
= TC_H_INGRESS
;
365 } else if (matches(*argv
, "help") == 0) {
367 } else if (strcmp(*argv
, "invisible") == 0) {
368 dump_invisible
= true;
370 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
380 t
.tcm_ifindex
= ll_name_to_index(d
);
381 if (t
.tcm_ifindex
== 0) {
382 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
385 filter_ifindex
= t
.tcm_ifindex
;
388 if (dump_invisible
) {
394 .n
.nlmsg_type
= RTM_GETQDISC
,
395 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
398 req
.t
.tcm_family
= AF_UNSPEC
;
400 addattr(&req
.n
, 256, TCA_DUMP_INVISIBLE
);
401 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
402 perror("Cannot send dump request");
406 } else if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
407 perror("Cannot send dump request");
412 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
) < 0) {
413 fprintf(stderr
, "Dump terminated\n");
421 int do_qdisc(int argc
, char **argv
)
424 return tc_qdisc_list(0, NULL
);
425 if (matches(*argv
, "add") == 0)
426 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
427 if (matches(*argv
, "change") == 0)
428 return tc_qdisc_modify(RTM_NEWQDISC
, 0, argc
-1, argv
+1);
429 if (matches(*argv
, "replace") == 0)
430 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
431 if (matches(*argv
, "link") == 0)
432 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_REPLACE
, argc
-1, argv
+1);
433 if (matches(*argv
, "delete") == 0)
434 return tc_qdisc_modify(RTM_DELQDISC
, 0, argc
-1, argv
+1);
436 if (matches(*argv
, "get") == 0)
437 return tc_qdisc_get(RTM_GETQDISC
, 0, argc
-1, argv
+1);
439 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
440 || matches(*argv
, "lst") == 0)
441 return tc_qdisc_list(argc
-1, argv
+1);
442 if (matches(*argv
, "help") == 0) {
446 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);
450 struct tc_qdisc_block_exists_ctx
{
455 static int tc_qdisc_block_exists_cb(const struct sockaddr_nl
*who
,
456 struct nlmsghdr
*n
, void *arg
)
458 struct tc_qdisc_block_exists_ctx
*ctx
= arg
;
459 struct tcmsg
*t
= NLMSG_DATA(n
);
460 struct rtattr
*tb
[TCA_MAX
+1];
461 int len
= n
->nlmsg_len
;
463 if (n
->nlmsg_type
!= RTM_NEWQDISC
)
466 len
-= NLMSG_LENGTH(sizeof(*t
));
470 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
472 if (tb
[TCA_KIND
] == NULL
)
475 if (tb
[TCA_INGRESS_BLOCK
] &&
476 RTA_PAYLOAD(tb
[TCA_INGRESS_BLOCK
]) >= sizeof(__u32
)) {
477 __u32 block
= rta_getattr_u32(tb
[TCA_INGRESS_BLOCK
]);
479 if (block
== ctx
->block_index
)
483 if (tb
[TCA_EGRESS_BLOCK
] &&
484 RTA_PAYLOAD(tb
[TCA_EGRESS_BLOCK
]) >= sizeof(__u32
)) {
485 __u32 block
= rta_getattr_u32(tb
[TCA_EGRESS_BLOCK
]);
487 if (block
== ctx
->block_index
)
493 bool tc_qdisc_block_exists(__u32 block_index
)
495 struct tc_qdisc_block_exists_ctx ctx
= { .block_index
= block_index
};
496 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
498 if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
499 perror("Cannot send dump request");
503 if (rtnl_dump_filter(&rth
, tc_qdisc_block_exists_cb
, &ctx
) < 0) {
504 perror("Dump terminated\n");