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
12 #include "libiproute/utils.h"
13 #include "libiproute/ip_common.h"
14 #include "libiproute/rt_names.h"
15 #include <linux/pkt_sched.h> /* for the TC_H_* macros */
17 #define parse_rtattr_nested(tb, max, rta) \
18 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
20 /* nullifies tb on error */
21 #define __parse_rtattr_nested_compat(tb, max, rta, len) \
22 ({if ((RTA_PAYLOAD(rta) >= len) && \
23 (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr))) { \
24 rta = RTA_DATA(rta) + RTA_ALIGN(len); \
25 parse_rtattr_nested(tb, max, rta); \
27 memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
30 #define parse_rtattr_nested_compat(tb, max, rta, data, len) \
31 ({data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
32 __parse_rtattr_nested_compat(tb, max, rta, len); })
34 #define show_details (0) /* not implemented. Does anyone need it? */
35 #define use_iec (0) /* not currently documented in the upstream manpage */
45 #define G (*(struct globals*)&bb_common_bufsiz1)
46 #define filter_ifindex (G.filter_ifindex)
47 #define filter_qdisc (G.filter_qdisc)
48 #define filter_parent (G.filter_parent)
49 #define filter_prio (G.filter_prio)
50 #define filter_proto (G.filter_proto)
52 void BUG_tc_globals_too_big(void);
53 #define INIT_G() do { \
54 if (sizeof(G) > COMMON_BUFSIZE) \
55 BUG_tc_globals_too_big(); \
58 /* Allocates a buffer containing the name of a class id.
59 * The caller must free the returned memory. */
60 static char* print_tc_classid(uint32_t cid)
62 #if 0 /* IMPOSSIBLE */
64 return xasprintf("root");
67 if (cid == TC_H_UNSPEC)
68 return xasprintf("none");
69 else if (TC_H_MAJ(cid) == 0)
70 return xasprintf(":%x", TC_H_MIN(cid));
71 else if (TC_H_MIN(cid) == 0)
72 return xasprintf("%x:", TC_H_MAJ(cid)>>16);
74 return xasprintf("%x:%x", TC_H_MAJ(cid)>>16, TC_H_MIN(cid));
77 /* Get a qdisc handle. Return 0 on success, !0 otherwise. */
78 static int get_qdisc_handle(__u32 *h, const char *str) {
83 if (!strcmp(str, "none"))
85 maj = strtoul(str, &p, 16);
89 if (*p != ':' && *p != '\0')
96 /* Get class ID. Return 0 on success, !0 otherwise. */
97 static int get_tc_classid(__u32 *h, const char *str) {
102 if (!strcmp(str, "root"))
105 if (!strcmp(str, "none"))
107 maj = strtoul(str, &p, 16);
118 min = strtoul(str, &p, 16);
119 //FIXME: check for "" too?
120 if (*p != '\0' || min >= (1<<16))
130 static void print_rate(char *buf, int len, uint32_t rate)
132 double tmp = (double)rate*8;
135 if (tmp >= 1000.0*1024.0*1024.0)
136 snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0);
137 else if (tmp >= 1000.0*1024)
138 snprintf(buf, len, "%.0fKibit", tmp/1024);
140 snprintf(buf, len, "%.0fbit", tmp);
142 if (tmp >= 1000.0*1000000.0)
143 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
144 else if (tmp >= 1000.0 * 1000.0)
145 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
147 snprintf(buf, len, "%.0fbit", tmp);
151 /* This is "pfifo_fast". */
152 static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
156 static int prio_print_opt(struct rtattr *opt)
159 struct tc_prio_qopt *qopt;
160 struct rtattr *tb[TCA_PRIO_MAX+1];
164 parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt));
167 printf("bands %u priomap ", qopt->bands);
168 for (i=0; i<=TC_PRIO_MAX; i++)
169 printf(" %d", qopt->priomap[i]);
172 printf(" multiqueue: o%s ",
173 *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
178 /* Class Based Queue */
179 static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
183 static int cbq_print_opt(struct rtattr *opt)
185 struct rtattr *tb[TCA_CBQ_MAX+1];
186 struct tc_ratespec *r = NULL;
187 struct tc_cbq_lssopt *lss = NULL;
188 struct tc_cbq_wrropt *wrr = NULL;
189 struct tc_cbq_fopt *fopt = NULL;
190 struct tc_cbq_ovl *ovl = NULL;
191 const char *const error = "CBQ: too short %s opt";
196 parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
198 if (tb[TCA_CBQ_RATE]) {
199 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
200 bb_error_msg(error, "rate");
202 r = RTA_DATA(tb[TCA_CBQ_RATE]);
204 if (tb[TCA_CBQ_LSSOPT]) {
205 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
206 bb_error_msg(error, "lss");
208 lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
210 if (tb[TCA_CBQ_WRROPT]) {
211 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
212 bb_error_msg(error, "wrr");
214 wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
216 if (tb[TCA_CBQ_FOPT]) {
217 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
218 bb_error_msg(error, "fopt");
220 fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
222 if (tb[TCA_CBQ_OVL_STRATEGY]) {
223 if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl))
224 bb_error_msg("CBQ: too short overlimit strategy %u/%u",
225 (unsigned) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]),
226 (unsigned) sizeof(*ovl));
228 ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
232 print_rate(buf, sizeof(buf), r->rate);
233 printf("rate %s ", buf);
235 printf("cell %ub ", 1<<r->cell_log);
237 printf("mpu %ub ", r->mpu);
239 printf("overhead %ub ", r->overhead);
242 if (lss && lss->flags) {
245 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
249 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
257 if (wrr->priority != TC_CBQ_MAXPRIO)
258 printf("prio %u", wrr->priority);
260 printf("prio no-transmit");
262 printf("/%u ", wrr->cpriority);
263 if (wrr->weight != 1) {
264 print_rate(buf, sizeof(buf), wrr->weight);
265 printf("weight %s ", buf);
268 printf("allot %ub ", wrr->allot);
275 static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
276 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
278 struct tcmsg *msg = NLMSG_DATA(hdr);
279 int len = hdr->nlmsg_len;
280 struct rtattr * tb[TCA_MAX+1];
283 if (hdr->nlmsg_type != RTM_NEWQDISC && hdr->nlmsg_type != RTM_DELQDISC) {
284 /* bb_error_msg("not a qdisc"); */
285 return 0; /* ??? mimic upstream; should perhaps return -1 */
287 len -= NLMSG_LENGTH(sizeof(*msg));
289 /* bb_error_msg("wrong len %d", len); */
292 /* not the desired interface? */
293 if (filter_ifindex && filter_ifindex != msg->tcm_ifindex)
295 memset (tb, 0, sizeof(tb));
296 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
297 if (tb[TCA_KIND] == NULL) {
298 /* bb_error_msg("%s: NULL kind", "qdisc"); */
301 if (hdr->nlmsg_type == RTM_DELQDISC)
303 name = (char*)RTA_DATA(tb[TCA_KIND]);
304 printf("qdisc %s %x: ", name, msg->tcm_handle>>16);
305 if (filter_ifindex == 0)
306 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
307 if (msg->tcm_parent == TC_H_ROOT)
309 else if (msg->tcm_parent) {
310 char *classid = print_tc_classid(msg->tcm_parent);
311 printf("parent %s ", classid);
312 if (ENABLE_FEATURE_CLEAN_UP)
315 if (msg->tcm_info != 1)
316 printf("refcnt %d ", msg->tcm_info);
317 if (tb[TCA_OPTIONS]) {
318 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
319 int qqq = index_in_strings(_q_, name);
320 if (qqq == 0) { /* pfifo_fast aka prio */
321 prio_print_opt(tb[TCA_OPTIONS]);
322 } else if (qqq == 1) { /* class based queuing */
323 cbq_print_opt(tb[TCA_OPTIONS]);
325 bb_error_msg("unknown %s", name);
331 static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
332 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
334 struct tcmsg *msg = NLMSG_DATA(hdr);
335 int len = hdr->nlmsg_len;
336 struct rtattr * tb[TCA_MAX+1];
337 char *name, *classid;
339 /*XXX Eventually factor out common code */
341 if (hdr->nlmsg_type != RTM_NEWTCLASS && hdr->nlmsg_type != RTM_DELTCLASS) {
342 /* bb_error_msg("not a class"); */
343 return 0; /* ??? mimic upstream; should perhaps return -1 */
345 len -= NLMSG_LENGTH(sizeof(*msg));
347 /* bb_error_msg("wrong len %d", len); */
350 /* not the desired interface? */
351 if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc))
353 memset (tb, 0, sizeof(tb));
354 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
355 if (tb[TCA_KIND] == NULL) {
356 /* bb_error_msg("%s: NULL kind", "class"); */
359 if (hdr->nlmsg_type == RTM_DELTCLASS)
362 name = (char*)RTA_DATA(tb[TCA_KIND]);
363 classid = !msg->tcm_handle ? NULL : print_tc_classid(
364 filter_qdisc ? TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
365 printf ("class %s %s", name, classid);
366 if (ENABLE_FEATURE_CLEAN_UP)
369 if (filter_ifindex == 0)
370 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
371 if (msg->tcm_parent == TC_H_ROOT)
373 else if (msg->tcm_parent) {
374 classid = print_tc_classid(filter_qdisc ?
375 TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
376 printf("parent %s ", classid);
377 if (ENABLE_FEATURE_CLEAN_UP)
381 printf("leaf %x ", msg->tcm_info >> 16);
382 /* Do that get_qdisc_kind(RTA_DATA(tb[TCA_KIND])). */
383 if (tb[TCA_OPTIONS]) {
384 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
385 int qqq = index_in_strings(_q_, name);
386 if (qqq == 0) { /* pfifo_fast aka prio */
387 /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/
388 } else if (qqq == 1) { /* class based queuing */
389 /* cbq_print_copt() is identical to cbq_print_opt(). */
390 cbq_print_opt(tb[TCA_OPTIONS]);
392 bb_error_msg("unknown %s", name);
399 static int print_filter(const struct sockaddr_nl *who UNUSED_PARAM,
400 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
402 struct tcmsg *msg = NLMSG_DATA(hdr);
403 int len = hdr->nlmsg_len;
404 struct rtattr * tb[TCA_MAX+1];
408 int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
409 int tc_main(int argc UNUSED_PARAM, char **argv)
411 static const char objects[] ALIGN1 =
412 "qdisc\0""class\0""filter\0"
414 enum { OBJ_qdisc = 0, OBJ_class, OBJ_filter };
415 static const char commands[] ALIGN1 =
416 "add\0""delete\0""change\0"
417 "link\0" /* only qdisc */
421 static const char args[] ALIGN1 =
422 "dev\0" /* qdisc, class, filter */
423 "root\0" /* class, filter */
424 "parent\0" /* class, filter */
425 "qdisc\0" /* class */
426 "handle\0" /* change: qdisc, class(classid) list: filter */
427 "classid\0" /* change: for class use "handle" */
428 "preference\0""priority\0""protocol\0" /* filter */
430 enum { CMD_add = 0, CMD_del, CMD_change, CMD_link, CMD_replace, CMD_show };
431 enum { ARG_dev = 0, ARG_root, ARG_parent, ARG_qdisc,
432 ARG_handle, ARG_classid, ARG_pref, ARG_prio, ARG_proto};
433 struct rtnl_handle rth;
435 int ret, obj, cmd, arg;
445 obj = index_in_substrings(objects, *argv++);
450 cmd = CMD_show; /* list is the default */
452 cmd = index_in_substrings(commands, *argv);
454 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
457 memset(&msg, 0, sizeof(msg));
458 msg.tcm_family = AF_UNSPEC;
461 arg = index_in_substrings(args, *argv);
462 if (arg == ARG_dev) {
465 duparg2("dev", *argv);
467 msg.tcm_ifindex = xll_name_to_index(dev);
469 filter_ifindex = msg.tcm_ifindex;
471 if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
472 || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
475 /* We don't care about duparg2("qdisc handle",*argv) for now */
476 if (get_qdisc_handle(&filter_qdisc, *argv))
477 invarg(*argv, "qdisc");
482 || (obj == OBJ_filter && arg >= ARG_pref)
487 invarg(*argv, "command");
490 if (arg == ARG_root) {
492 duparg("parent", *argv);
493 msg.tcm_parent = TC_H_ROOT;
494 if (obj == OBJ_filter)
495 filter_parent = TC_H_ROOT;
496 } else if (arg == ARG_parent) {
499 duparg(*argv, "parent");
500 if (get_tc_classid(&handle, *argv))
501 invarg(*argv, "parent");
502 msg.tcm_parent = handle;
503 if (obj == OBJ_filter)
504 filter_parent = handle;
505 } else if (arg == ARG_handle) { /* filter::list */
507 duparg(*argv, "handle");
508 /* reject LONG_MIN || LONG_MAX */
510 if ((slash = strchr(handle, '/')) != NULL)
513 msg.tcm_handle = get_u32(*argv, "handle");
514 /* if (slash) {if (get_u32(__u32 &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
515 } else if (arg == ARG_classid && obj == OBJ_class && cmd == CMD_change){
516 } else if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
518 duparg(*argv, "priority");
519 filter_prio = get_u32(*argv, "priority");
520 } else if (arg == ARG_proto) { /* filter::list */
523 duparg(*argv, "protocol");
524 if (ll_proto_a2n(&tmp, *argv))
525 invarg(*argv, "protocol");
529 if (cmd >= CMD_show) { /* show or list */
530 if (obj == OBJ_filter)
531 msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
532 if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
533 obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
534 &msg, sizeof(msg)) < 0)
535 bb_simple_perror_msg_and_die("can't send dump request");
537 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
538 obj == OBJ_class ? print_class : print_filter,
541 if (ENABLE_FEATURE_CLEAN_UP) {