1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7 * Bernhard Reutner-Fischer adjusted for busybox
10 //usage:#define tc_trivial_usage
11 /* //usage: "[OPTIONS] OBJECT CMD [dev STRING]" */
12 //usage: "OBJECT CMD [dev STRING]"
13 //usage:#define tc_full_usage "\n\n"
14 //usage: "OBJECT: {qdisc|class|filter}\n"
15 //usage: "CMD: {add|del|change|replace|show}\n"
17 //usage: "qdisc [ handle QHANDLE ] [ root |"IF_FEATURE_TC_INGRESS(" ingress |")" parent CLASSID ]\n"
18 /* //usage: "[ estimator INTERVAL TIME_CONSTANT ]\n" */
19 //usage: " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n"
20 //usage: " QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n"
21 //usage: "qdisc show [ dev STRING ]"IF_FEATURE_TC_INGRESS(" [ingress]")"\n"
22 //usage: "class [ classid CLASSID ] [ root | parent CLASSID ]\n"
23 //usage: " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n"
24 //usage: "class show [ dev STRING ] [ root | parent CLASSID ]\n"
25 //usage: "filter [ pref PRIO ] [ protocol PROTO ]\n"
26 /* //usage: "\t[ estimator INTERVAL TIME_CONSTANT ]\n" */
27 //usage: " [ root | classid CLASSID ] [ handle FILTERID ]\n"
28 //usage: " [ [ FILTER_TYPE ] [ help | OPTIONS ] ]\n"
29 //usage: "filter show [ dev STRING ] [ root | parent CLASSID ]"
33 #include "libiproute/utils.h"
34 #include "libiproute/ip_common.h"
35 #include "libiproute/rt_names.h"
36 #include <linux/pkt_sched.h> /* for the TC_H_* macros */
38 #define parse_rtattr_nested(tb, max, rta) \
39 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
41 /* nullifies tb on error */
42 #define __parse_rtattr_nested_compat(tb, max, rta, len) \
43 ({if ((RTA_PAYLOAD(rta) >= len) && \
44 (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr))) { \
45 rta = RTA_DATA(rta) + RTA_ALIGN(len); \
46 parse_rtattr_nested(tb, max, rta); \
48 memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
51 #define parse_rtattr_nested_compat(tb, max, rta, data, len) \
52 ({data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
53 __parse_rtattr_nested_compat(tb, max, rta, len); })
55 #define show_details (0) /* not implemented. Does anyone need it? */
56 #define use_iec (0) /* not currently documented in the upstream manpage */
61 uint32_t filter_qdisc
;
62 uint32_t filter_parent
;
64 uint32_t filter_proto
;
66 #define G (*(struct globals*)&bb_common_bufsiz1)
67 struct BUG_G_too_big
{
68 char BUG_G_too_big
[sizeof(G
) <= COMMON_BUFSIZE
? 1 : -1];
70 #define filter_ifindex (G.filter_ifindex)
71 #define filter_qdisc (G.filter_qdisc)
72 #define filter_parent (G.filter_parent)
73 #define filter_prio (G.filter_prio)
74 #define filter_proto (G.filter_proto)
75 #define INIT_G() do { } while (0)
77 /* Allocates a buffer containing the name of a class id.
78 * The caller must free the returned memory. */
79 static char* print_tc_classid(uint32_t cid
)
81 #if 0 /* IMPOSSIBLE */
83 return xasprintf("root");
86 if (cid
== TC_H_UNSPEC
)
87 return xasprintf("none");
88 else if (TC_H_MAJ(cid
) == 0)
89 return xasprintf(":%x", TC_H_MIN(cid
));
90 else if (TC_H_MIN(cid
) == 0)
91 return xasprintf("%x:", TC_H_MAJ(cid
)>>16);
93 return xasprintf("%x:%x", TC_H_MAJ(cid
)>>16, TC_H_MIN(cid
));
96 /* Get a qdisc handle. Return 0 on success, !0 otherwise. */
97 static int get_qdisc_handle(uint32_t *h
, const char *str
) {
102 if (!strcmp(str
, "none"))
104 maj
= strtoul(str
, &p
, 16);
108 if (*p
!= ':' && *p
!= '\0')
115 /* Get class ID. Return 0 on success, !0 otherwise. */
116 static int get_tc_classid(uint32_t *h
, const char *str
) {
121 if (!strcmp(str
, "root"))
124 if (!strcmp(str
, "none"))
126 maj
= strtoul(str
, &p
, 16);
137 min
= strtoul(str
, &p
, 16);
138 //FIXME: check for "" too?
139 if (*p
!= '\0' || min
>= (1<<16))
149 static void print_rate(char *buf
, int len
, uint32_t rate
)
151 double tmp
= (double)rate
*8;
154 if (tmp
>= 1000.0*1024.0*1024.0)
155 snprintf(buf
, len
, "%.0fMibit", tmp
/1024.0*1024.0);
156 else if (tmp
>= 1000.0*1024)
157 snprintf(buf
, len
, "%.0fKibit", tmp
/1024);
159 snprintf(buf
, len
, "%.0fbit", tmp
);
161 if (tmp
>= 1000.0*1000000.0)
162 snprintf(buf
, len
, "%.0fMbit", tmp
/1000000.0);
163 else if (tmp
>= 1000.0 * 1000.0)
164 snprintf(buf
, len
, "%.0fKbit", tmp
/1000.0);
166 snprintf(buf
, len
, "%.0fbit", tmp
);
170 /* This is "pfifo_fast". */
171 static int prio_parse_opt(int argc
, char **argv
, struct nlmsghdr
*n
)
175 static int prio_print_opt(struct rtattr
*opt
)
178 struct tc_prio_qopt
*qopt
;
179 struct rtattr
*tb
[TCA_PRIO_MAX
+1];
183 parse_rtattr_nested_compat(tb
, TCA_PRIO_MAX
, opt
, qopt
, sizeof(*qopt
));
186 printf("bands %u priomap ", qopt
->bands
);
187 for (i
=0; i
<=TC_PRIO_MAX
; i
++)
188 printf(" %d", qopt
->priomap
[i
]);
191 printf(" multiqueue: o%s ",
192 *(unsigned char *)RTA_DATA(tb
[TCA_PRIO_MQ
]) ? "n" : "ff");
197 /* Class Based Queue */
198 static int cbq_parse_opt(int argc
, char **argv
, struct nlmsghdr
*n
)
202 static int cbq_print_opt(struct rtattr
*opt
)
204 struct rtattr
*tb
[TCA_CBQ_MAX
+1];
205 struct tc_ratespec
*r
= NULL
;
206 struct tc_cbq_lssopt
*lss
= NULL
;
207 struct tc_cbq_wrropt
*wrr
= NULL
;
208 struct tc_cbq_fopt
*fopt
= NULL
;
209 struct tc_cbq_ovl
*ovl
= NULL
;
210 const char *const error
= "CBQ: too short %s opt";
215 parse_rtattr_nested(tb
, TCA_CBQ_MAX
, opt
);
217 if (tb
[TCA_CBQ_RATE
]) {
218 if (RTA_PAYLOAD(tb
[TCA_CBQ_RATE
]) < sizeof(*r
))
219 bb_error_msg(error
, "rate");
221 r
= RTA_DATA(tb
[TCA_CBQ_RATE
]);
223 if (tb
[TCA_CBQ_LSSOPT
]) {
224 if (RTA_PAYLOAD(tb
[TCA_CBQ_LSSOPT
]) < sizeof(*lss
))
225 bb_error_msg(error
, "lss");
227 lss
= RTA_DATA(tb
[TCA_CBQ_LSSOPT
]);
229 if (tb
[TCA_CBQ_WRROPT
]) {
230 if (RTA_PAYLOAD(tb
[TCA_CBQ_WRROPT
]) < sizeof(*wrr
))
231 bb_error_msg(error
, "wrr");
233 wrr
= RTA_DATA(tb
[TCA_CBQ_WRROPT
]);
235 if (tb
[TCA_CBQ_FOPT
]) {
236 if (RTA_PAYLOAD(tb
[TCA_CBQ_FOPT
]) < sizeof(*fopt
))
237 bb_error_msg(error
, "fopt");
239 fopt
= RTA_DATA(tb
[TCA_CBQ_FOPT
]);
241 if (tb
[TCA_CBQ_OVL_STRATEGY
]) {
242 if (RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]) < sizeof(*ovl
))
243 bb_error_msg("CBQ: too short overlimit strategy %u/%u",
244 (unsigned) RTA_PAYLOAD(tb
[TCA_CBQ_OVL_STRATEGY
]),
245 (unsigned) sizeof(*ovl
));
247 ovl
= RTA_DATA(tb
[TCA_CBQ_OVL_STRATEGY
]);
251 print_rate(buf
, sizeof(buf
), r
->rate
);
252 printf("rate %s ", buf
);
254 printf("cell %ub ", 1<<r
->cell_log
);
256 printf("mpu %ub ", r
->mpu
);
258 printf("overhead %ub ", r
->overhead
);
261 if (lss
&& lss
->flags
) {
264 if (lss
->flags
&TCF_CBQ_LSS_BOUNDED
) {
268 if (lss
->flags
&TCF_CBQ_LSS_ISOLATED
) {
276 if (wrr
->priority
!= TC_CBQ_MAXPRIO
)
277 printf("prio %u", wrr
->priority
);
279 printf("prio no-transmit");
281 printf("/%u ", wrr
->cpriority
);
282 if (wrr
->weight
!= 1) {
283 print_rate(buf
, sizeof(buf
), wrr
->weight
);
284 printf("weight %s ", buf
);
287 printf("allot %ub ", wrr
->allot
);
294 static int print_qdisc(const struct sockaddr_nl
*who UNUSED_PARAM
,
295 struct nlmsghdr
*hdr
, void *arg UNUSED_PARAM
)
297 struct tcmsg
*msg
= NLMSG_DATA(hdr
);
298 int len
= hdr
->nlmsg_len
;
299 struct rtattr
* tb
[TCA_MAX
+1];
302 if (hdr
->nlmsg_type
!= RTM_NEWQDISC
&& hdr
->nlmsg_type
!= RTM_DELQDISC
) {
303 /* bb_error_msg("not a qdisc"); */
304 return 0; /* ??? mimic upstream; should perhaps return -1 */
306 len
-= NLMSG_LENGTH(sizeof(*msg
));
308 /* bb_error_msg("wrong len %d", len); */
311 /* not the desired interface? */
312 if (filter_ifindex
&& filter_ifindex
!= msg
->tcm_ifindex
)
314 memset (tb
, 0, sizeof(tb
));
315 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(msg
), len
);
316 if (tb
[TCA_KIND
] == NULL
) {
317 /* bb_error_msg("%s: NULL kind", "qdisc"); */
320 if (hdr
->nlmsg_type
== RTM_DELQDISC
)
322 name
= (char*)RTA_DATA(tb
[TCA_KIND
]);
323 printf("qdisc %s %x: ", name
, msg
->tcm_handle
>>16);
324 if (filter_ifindex
== 0)
325 printf("dev %s ", ll_index_to_name(msg
->tcm_ifindex
));
326 if (msg
->tcm_parent
== TC_H_ROOT
)
328 else if (msg
->tcm_parent
) {
329 char *classid
= print_tc_classid(msg
->tcm_parent
);
330 printf("parent %s ", classid
);
331 if (ENABLE_FEATURE_CLEAN_UP
)
334 if (msg
->tcm_info
!= 1)
335 printf("refcnt %d ", msg
->tcm_info
);
336 if (tb
[TCA_OPTIONS
]) {
337 static const char _q_
[] ALIGN1
= "pfifo_fast\0""cbq\0";
338 int qqq
= index_in_strings(_q_
, name
);
339 if (qqq
== 0) { /* pfifo_fast aka prio */
340 prio_print_opt(tb
[TCA_OPTIONS
]);
341 } else if (qqq
== 1) { /* class based queuing */
342 cbq_print_opt(tb
[TCA_OPTIONS
]);
344 bb_error_msg("unknown %s", name
);
350 static int print_class(const struct sockaddr_nl
*who UNUSED_PARAM
,
351 struct nlmsghdr
*hdr
, void *arg UNUSED_PARAM
)
353 struct tcmsg
*msg
= NLMSG_DATA(hdr
);
354 int len
= hdr
->nlmsg_len
;
355 struct rtattr
* tb
[TCA_MAX
+1];
356 char *name
, *classid
;
358 /*XXX Eventually factor out common code */
360 if (hdr
->nlmsg_type
!= RTM_NEWTCLASS
&& hdr
->nlmsg_type
!= RTM_DELTCLASS
) {
361 /* bb_error_msg("not a class"); */
362 return 0; /* ??? mimic upstream; should perhaps return -1 */
364 len
-= NLMSG_LENGTH(sizeof(*msg
));
366 /* bb_error_msg("wrong len %d", len); */
369 /* not the desired interface? */
370 if (filter_qdisc
&& TC_H_MAJ(msg
->tcm_handle
^filter_qdisc
))
372 memset (tb
, 0, sizeof(tb
));
373 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(msg
), len
);
374 if (tb
[TCA_KIND
] == NULL
) {
375 /* bb_error_msg("%s: NULL kind", "class"); */
378 if (hdr
->nlmsg_type
== RTM_DELTCLASS
)
381 name
= (char*)RTA_DATA(tb
[TCA_KIND
]);
382 classid
= !msg
->tcm_handle
? NULL
: print_tc_classid(
383 filter_qdisc
? TC_H_MIN(msg
->tcm_parent
) : msg
->tcm_parent
);
384 printf ("class %s %s", name
, classid
);
385 if (ENABLE_FEATURE_CLEAN_UP
)
388 if (filter_ifindex
== 0)
389 printf("dev %s ", ll_index_to_name(msg
->tcm_ifindex
));
390 if (msg
->tcm_parent
== TC_H_ROOT
)
392 else if (msg
->tcm_parent
) {
393 classid
= print_tc_classid(filter_qdisc
?
394 TC_H_MIN(msg
->tcm_parent
) : msg
->tcm_parent
);
395 printf("parent %s ", classid
);
396 if (ENABLE_FEATURE_CLEAN_UP
)
400 printf("leaf %x ", msg
->tcm_info
>> 16);
401 /* Do that get_qdisc_kind(RTA_DATA(tb[TCA_KIND])). */
402 if (tb
[TCA_OPTIONS
]) {
403 static const char _q_
[] ALIGN1
= "pfifo_fast\0""cbq\0";
404 int qqq
= index_in_strings(_q_
, name
);
405 if (qqq
== 0) { /* pfifo_fast aka prio */
406 /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/
407 } else if (qqq
== 1) { /* class based queuing */
408 /* cbq_print_copt() is identical to cbq_print_opt(). */
409 cbq_print_opt(tb
[TCA_OPTIONS
]);
411 bb_error_msg("unknown %s", name
);
418 static int print_filter(const struct sockaddr_nl
*who UNUSED_PARAM
,
419 struct nlmsghdr
*hdr
, void *arg UNUSED_PARAM
)
421 struct tcmsg
*msg
= NLMSG_DATA(hdr
);
422 int len
= hdr
->nlmsg_len
;
423 struct rtattr
* tb
[TCA_MAX
+1];
427 int tc_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
428 int tc_main(int argc UNUSED_PARAM
, char **argv
)
430 static const char objects
[] ALIGN1
=
431 "qdisc\0""class\0""filter\0"
433 enum { OBJ_qdisc
= 0, OBJ_class
, OBJ_filter
};
434 static const char commands
[] ALIGN1
=
435 "add\0""delete\0""change\0"
436 "link\0" /* only qdisc */
440 static const char args
[] ALIGN1
=
441 "dev\0" /* qdisc, class, filter */
442 "root\0" /* class, filter */
443 "parent\0" /* class, filter */
444 "qdisc\0" /* class */
445 "handle\0" /* change: qdisc, class(classid) list: filter */
446 "classid\0" /* change: for class use "handle" */
447 "preference\0""priority\0""protocol\0" /* filter */
449 enum { CMD_add
= 0, CMD_del
, CMD_change
, CMD_link
, CMD_replace
, CMD_show
};
450 enum { ARG_dev
= 0, ARG_root
, ARG_parent
, ARG_qdisc
,
451 ARG_handle
, ARG_classid
, ARG_pref
, ARG_prio
, ARG_proto
};
452 struct rtnl_handle rth
;
454 int ret
, obj
, cmd
, arg
;
464 obj
= index_in_substrings(objects
, *argv
++);
469 cmd
= CMD_show
; /* list is the default */
471 cmd
= index_in_substrings(commands
, *argv
);
473 bb_error_msg_and_die(bb_msg_invalid_arg
, *argv
, applet_name
);
476 memset(&msg
, 0, sizeof(msg
));
477 msg
.tcm_family
= AF_UNSPEC
;
480 arg
= index_in_substrings(args
, *argv
);
481 if (arg
== ARG_dev
) {
484 duparg2("dev", *argv
);
486 msg
.tcm_ifindex
= xll_name_to_index(dev
);
488 filter_ifindex
= msg
.tcm_ifindex
;
490 if ((arg
== ARG_qdisc
&& obj
== OBJ_class
&& cmd
>= CMD_show
)
491 || (arg
== ARG_handle
&& obj
== OBJ_qdisc
&& cmd
== CMD_change
)
494 /* We don't care about duparg2("qdisc handle",*argv) for now */
495 if (get_qdisc_handle(&filter_qdisc
, *argv
))
496 invarg(*argv
, "qdisc");
501 || (obj
== OBJ_filter
&& arg
>= ARG_pref
)
506 invarg(*argv
, "command");
509 if (arg
== ARG_root
) {
511 duparg("parent", *argv
);
512 msg
.tcm_parent
= TC_H_ROOT
;
513 if (obj
== OBJ_filter
)
514 filter_parent
= TC_H_ROOT
;
515 } else if (arg
== ARG_parent
) {
518 duparg(*argv
, "parent");
519 if (get_tc_classid(&handle
, *argv
))
520 invarg(*argv
, "parent");
521 msg
.tcm_parent
= handle
;
522 if (obj
== OBJ_filter
)
523 filter_parent
= handle
;
524 } else if (arg
== ARG_handle
) { /* filter::list */
526 duparg(*argv
, "handle");
527 /* reject LONG_MIN || LONG_MAX */
529 slash = strchr(handle, '/');
533 msg
.tcm_handle
= get_u32(*argv
, "handle");
534 /* if (slash) {if (get_u32(uint32_t &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
535 } else if (arg
== ARG_classid
&& obj
== OBJ_class
&& cmd
== CMD_change
){
536 } else if (arg
== ARG_pref
|| arg
== ARG_prio
) { /* filter::list */
538 duparg(*argv
, "priority");
539 filter_prio
= get_u32(*argv
, "priority");
540 } else if (arg
== ARG_proto
) { /* filter::list */
543 duparg(*argv
, "protocol");
544 if (ll_proto_a2n(&tmp
, *argv
))
545 invarg(*argv
, "protocol");
549 if (cmd
>= CMD_show
) { /* show or list */
550 if (obj
== OBJ_filter
)
551 msg
.tcm_info
= TC_H_MAKE(filter_prio
<<16, filter_proto
);
552 if (rtnl_dump_request(&rth
, obj
== OBJ_qdisc
? RTM_GETQDISC
:
553 obj
== OBJ_class
? RTM_GETTCLASS
: RTM_GETTFILTER
,
554 &msg
, sizeof(msg
)) < 0)
555 bb_simple_perror_msg_and_die("can't send dump request");
557 xrtnl_dump_filter(&rth
, obj
== OBJ_qdisc
? print_qdisc
:
558 obj
== OBJ_class
? print_class
: print_filter
,
561 if (ENABLE_FEATURE_CLEAN_UP
) {