Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / networking / tc.c
blob1574353a5cf94031d61bade2b657252213718253
1 /* vi: set sw=4 ts=4: */
2 /*
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
8 */
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"
16 //usage: "\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 ]"
31 #include "libbb.h"
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); \
47 } else \
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 */
59 struct globals {
60 int filter_ifindex;
61 uint32_t filter_qdisc;
62 uint32_t filter_parent;
63 uint32_t filter_prio;
64 uint32_t filter_proto;
65 } FIX_ALIASING;
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 */
82 if (cid == TC_H_ROOT)
83 return xasprintf("root");
84 else
85 #endif
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);
92 else
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) {
98 uint32_t maj;
99 char *p;
101 maj = TC_H_UNSPEC;
102 if (!strcmp(str, "none"))
103 goto ok;
104 maj = strtoul(str, &p, 16);
105 if (p == str)
106 return 1;
107 maj <<= 16;
108 if (*p != ':' && *p != '\0')
109 return 1;
111 *h = maj;
112 return 0;
115 /* Get class ID. Return 0 on success, !0 otherwise. */
116 static int get_tc_classid(uint32_t *h, const char *str) {
117 uint32_t maj, min;
118 char *p;
120 maj = TC_H_ROOT;
121 if (!strcmp(str, "root"))
122 goto ok;
123 maj = TC_H_UNSPEC;
124 if (!strcmp(str, "none"))
125 goto ok;
126 maj = strtoul(str, &p, 16);
127 if (p == str) {
128 if (*p != ':')
129 return 1;
130 maj = 0;
132 if (*p == ':') {
133 if (maj >= (1<<16))
134 return 1;
135 maj <<= 16;
136 str = p + 1;
137 min = strtoul(str, &p, 16);
138 //FIXME: check for "" too?
139 if (*p != '\0' || min >= (1<<16))
140 return 1;
141 maj |= min;
142 } else if (*p != 0)
143 return 1;
145 *h = maj;
146 return 0;
149 static void print_rate(char *buf, int len, uint32_t rate)
151 double tmp = (double)rate*8;
153 if (use_iec) {
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);
158 else
159 snprintf(buf, len, "%.0fbit", tmp);
160 } else {
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);
165 else
166 snprintf(buf, len, "%.0fbit", tmp);
170 /* This is "pfifo_fast". */
171 static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
173 return 0;
175 static int prio_print_opt(struct rtattr *opt)
177 int i;
178 struct tc_prio_qopt *qopt;
179 struct rtattr *tb[TCA_PRIO_MAX+1];
181 if (opt == NULL)
182 return 0;
183 parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt));
184 if (tb == NULL)
185 return 0;
186 printf("bands %u priomap ", qopt->bands);
187 for (i=0; i<=TC_PRIO_MAX; i++)
188 printf(" %d", qopt->priomap[i]);
190 if (tb[TCA_PRIO_MQ])
191 printf(" multiqueue: o%s ",
192 *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
194 return 0;
197 /* Class Based Queue */
198 static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
200 return 0;
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";
211 char buf[64];
213 if (opt == NULL)
214 goto done;
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");
220 else
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");
226 else
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");
232 else
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");
238 else
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));
246 else
247 ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
250 if (r) {
251 print_rate(buf, sizeof(buf), r->rate);
252 printf("rate %s ", buf);
253 if (show_details) {
254 printf("cell %ub ", 1<<r->cell_log);
255 if (r->mpu)
256 printf("mpu %ub ", r->mpu);
257 if (r->overhead)
258 printf("overhead %ub ", r->overhead);
261 if (lss && lss->flags) {
262 bool comma = false;
263 bb_putchar('(');
264 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
265 printf("bounded");
266 comma = true;
268 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
269 if (comma)
270 bb_putchar(',');
271 printf("isolated");
273 printf(") ");
275 if (wrr) {
276 if (wrr->priority != TC_CBQ_MAXPRIO)
277 printf("prio %u", wrr->priority);
278 else
279 printf("prio no-transmit");
280 if (show_details) {
281 printf("/%u ", wrr->cpriority);
282 if (wrr->weight != 1) {
283 print_rate(buf, sizeof(buf), wrr->weight);
284 printf("weight %s ", buf);
286 if (wrr->allot)
287 printf("allot %ub ", wrr->allot);
290 done:
291 return 0;
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];
300 char *name;
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));
307 if (len < 0) {
308 /* bb_error_msg("wrong len %d", len); */
309 return -1;
311 /* not the desired interface? */
312 if (filter_ifindex && filter_ifindex != msg->tcm_ifindex)
313 return 0;
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"); */
318 return -1;
320 if (hdr->nlmsg_type == RTM_DELQDISC)
321 printf("deleted ");
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)
327 printf("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)
332 free(classid);
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]);
343 } else
344 bb_error_msg("unknown %s", name);
346 bb_putchar('\n');
347 return 0;
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));
365 if (len < 0) {
366 /* bb_error_msg("wrong len %d", len); */
367 return -1;
369 /* not the desired interface? */
370 if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc))
371 return 0;
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"); */
376 return -1;
378 if (hdr->nlmsg_type == RTM_DELTCLASS)
379 printf("deleted ");
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)
386 free(classid);
388 if (filter_ifindex == 0)
389 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
390 if (msg->tcm_parent == TC_H_ROOT)
391 printf("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)
397 free(classid);
399 if (msg->tcm_info)
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]);
410 } else
411 bb_error_msg("unknown %s", name);
413 bb_putchar('\n');
415 return 0;
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];
424 return 0;
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 */
437 "replace\0"
438 "show\0""list\0"
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;
453 struct tcmsg msg;
454 int ret, obj, cmd, arg;
455 char *dev = NULL;
457 INIT_G();
459 if (!*++argv)
460 bb_show_usage();
461 xrtnl_open(&rth);
462 ret = EXIT_SUCCESS;
464 obj = index_in_substrings(objects, *argv++);
466 if (obj < OBJ_qdisc)
467 bb_show_usage();
468 if (!*argv)
469 cmd = CMD_show; /* list is the default */
470 else {
471 cmd = index_in_substrings(commands, *argv);
472 if (cmd < 0)
473 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
474 argv++;
476 memset(&msg, 0, sizeof(msg));
477 msg.tcm_family = AF_UNSPEC;
478 ll_init_map(&rth);
479 while (*argv) {
480 arg = index_in_substrings(args, *argv);
481 if (arg == ARG_dev) {
482 NEXT_ARG();
483 if (dev)
484 duparg2("dev", *argv);
485 dev = *argv++;
486 msg.tcm_ifindex = xll_name_to_index(dev);
487 if (cmd >= CMD_show)
488 filter_ifindex = msg.tcm_ifindex;
489 } else
490 if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
491 || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
493 NEXT_ARG();
494 /* We don't care about duparg2("qdisc handle",*argv) for now */
495 if (get_qdisc_handle(&filter_qdisc, *argv))
496 invarg(*argv, "qdisc");
497 } else
498 if (obj != OBJ_qdisc
499 && (arg == ARG_root
500 || arg == ARG_parent
501 || (obj == OBJ_filter && arg >= ARG_pref)
504 /* nothing */
505 } else {
506 invarg(*argv, "command");
508 NEXT_ARG();
509 if (arg == ARG_root) {
510 if (msg.tcm_parent)
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) {
516 uint32_t handle;
517 if (msg.tcm_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 */
525 if (msg.tcm_handle)
526 duparg(*argv, "handle");
527 /* reject LONG_MIN || LONG_MAX */
528 /* TODO: for fw
529 if ((slash = strchr(handle, '/')) != NULL)
530 *slash = '\0';
532 msg.tcm_handle = get_u32(*argv, "handle");
533 /* if (slash) {if (get_u32(uint32_t &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
534 } else if (arg == ARG_classid && obj == OBJ_class && cmd == CMD_change){
535 } else if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
536 if (filter_prio)
537 duparg(*argv, "priority");
538 filter_prio = get_u32(*argv, "priority");
539 } else if (arg == ARG_proto) { /* filter::list */
540 uint16_t tmp;
541 if (filter_proto)
542 duparg(*argv, "protocol");
543 if (ll_proto_a2n(&tmp, *argv))
544 invarg(*argv, "protocol");
545 filter_proto = tmp;
548 if (cmd >= CMD_show) { /* show or list */
549 if (obj == OBJ_filter)
550 msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
551 if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
552 obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
553 &msg, sizeof(msg)) < 0)
554 bb_simple_perror_msg_and_die("can't send dump request");
556 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
557 obj == OBJ_class ? print_class : print_filter,
558 NULL);
560 if (ENABLE_FEATURE_CLEAN_UP) {
561 rtnl_close(&rth);
563 return ret;