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 struct BUG_G_too_big {
47 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
49 #define filter_ifindex (G.filter_ifindex)
50 #define filter_qdisc (G.filter_qdisc)
51 #define filter_parent (G.filter_parent)
52 #define filter_prio (G.filter_prio)
53 #define filter_proto (G.filter_proto)
54 #define INIT_G() do { } while (0)
56 /* Allocates a buffer containing the name of a class id.
57 * The caller must free the returned memory. */
58 static char* print_tc_classid(uint32_t cid)
60 #if 0 /* IMPOSSIBLE */
62 return xasprintf("root");
65 if (cid == TC_H_UNSPEC)
66 return xasprintf("none");
67 else if (TC_H_MAJ(cid) == 0)
68 return xasprintf(":%x", TC_H_MIN(cid));
69 else if (TC_H_MIN(cid) == 0)
70 return xasprintf("%x:", TC_H_MAJ(cid)>>16);
72 return xasprintf("%x:%x", TC_H_MAJ(cid)>>16, TC_H_MIN(cid));
75 /* Get a qdisc handle. Return 0 on success, !0 otherwise. */
76 static int get_qdisc_handle(__u32 *h, const char *str) {
81 if (!strcmp(str, "none"))
83 maj = strtoul(str, &p, 16);
87 if (*p != ':' && *p != '\0')
94 /* Get class ID. Return 0 on success, !0 otherwise. */
95 static int get_tc_classid(__u32 *h, const char *str) {
100 if (!strcmp(str, "root"))
103 if (!strcmp(str, "none"))
105 maj = strtoul(str, &p, 16);
116 min = strtoul(str, &p, 16);
117 //FIXME: check for "" too?
118 if (*p != '\0' || min >= (1<<16))
128 static void print_rate(char *buf, int len, uint32_t rate)
130 double tmp = (double)rate*8;
133 if (tmp >= 1000.0*1024.0*1024.0)
134 snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0);
135 else if (tmp >= 1000.0*1024)
136 snprintf(buf, len, "%.0fKibit", tmp/1024);
138 snprintf(buf, len, "%.0fbit", tmp);
140 if (tmp >= 1000.0*1000000.0)
141 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
142 else if (tmp >= 1000.0 * 1000.0)
143 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
145 snprintf(buf, len, "%.0fbit", tmp);
149 /* This is "pfifo_fast". */
150 static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
154 static int prio_print_opt(struct rtattr *opt)
157 struct tc_prio_qopt *qopt;
158 struct rtattr *tb[TCA_PRIO_MAX+1];
162 parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt));
165 printf("bands %u priomap ", qopt->bands);
166 for (i=0; i<=TC_PRIO_MAX; i++)
167 printf(" %d", qopt->priomap[i]);
170 printf(" multiqueue: o%s ",
171 *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
176 /* Class Based Queue */
177 static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
181 static int cbq_print_opt(struct rtattr *opt)
183 struct rtattr *tb[TCA_CBQ_MAX+1];
184 struct tc_ratespec *r = NULL;
185 struct tc_cbq_lssopt *lss = NULL;
186 struct tc_cbq_wrropt *wrr = NULL;
187 struct tc_cbq_fopt *fopt = NULL;
188 struct tc_cbq_ovl *ovl = NULL;
189 const char *const error = "CBQ: too short %s opt";
194 parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
196 if (tb[TCA_CBQ_RATE]) {
197 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
198 bb_error_msg(error, "rate");
200 r = RTA_DATA(tb[TCA_CBQ_RATE]);
202 if (tb[TCA_CBQ_LSSOPT]) {
203 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
204 bb_error_msg(error, "lss");
206 lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
208 if (tb[TCA_CBQ_WRROPT]) {
209 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
210 bb_error_msg(error, "wrr");
212 wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
214 if (tb[TCA_CBQ_FOPT]) {
215 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
216 bb_error_msg(error, "fopt");
218 fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
220 if (tb[TCA_CBQ_OVL_STRATEGY]) {
221 if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl))
222 bb_error_msg("CBQ: too short overlimit strategy %u/%u",
223 (unsigned) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]),
224 (unsigned) sizeof(*ovl));
226 ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
230 print_rate(buf, sizeof(buf), r->rate);
231 printf("rate %s ", buf);
233 printf("cell %ub ", 1<<r->cell_log);
235 printf("mpu %ub ", r->mpu);
237 printf("overhead %ub ", r->overhead);
240 if (lss && lss->flags) {
243 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
247 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
255 if (wrr->priority != TC_CBQ_MAXPRIO)
256 printf("prio %u", wrr->priority);
258 printf("prio no-transmit");
260 printf("/%u ", wrr->cpriority);
261 if (wrr->weight != 1) {
262 print_rate(buf, sizeof(buf), wrr->weight);
263 printf("weight %s ", buf);
266 printf("allot %ub ", wrr->allot);
273 static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
274 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
276 struct tcmsg *msg = NLMSG_DATA(hdr);
277 int len = hdr->nlmsg_len;
278 struct rtattr * tb[TCA_MAX+1];
281 if (hdr->nlmsg_type != RTM_NEWQDISC && hdr->nlmsg_type != RTM_DELQDISC) {
282 /* bb_error_msg("not a qdisc"); */
283 return 0; /* ??? mimic upstream; should perhaps return -1 */
285 len -= NLMSG_LENGTH(sizeof(*msg));
287 /* bb_error_msg("wrong len %d", len); */
290 /* not the desired interface? */
291 if (filter_ifindex && filter_ifindex != msg->tcm_ifindex)
293 memset (tb, 0, sizeof(tb));
294 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
295 if (tb[TCA_KIND] == NULL) {
296 /* bb_error_msg("%s: NULL kind", "qdisc"); */
299 if (hdr->nlmsg_type == RTM_DELQDISC)
301 name = (char*)RTA_DATA(tb[TCA_KIND]);
302 printf("qdisc %s %x: ", name, msg->tcm_handle>>16);
303 if (filter_ifindex == 0)
304 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
305 if (msg->tcm_parent == TC_H_ROOT)
307 else if (msg->tcm_parent) {
308 char *classid = print_tc_classid(msg->tcm_parent);
309 printf("parent %s ", classid);
310 if (ENABLE_FEATURE_CLEAN_UP)
313 if (msg->tcm_info != 1)
314 printf("refcnt %d ", msg->tcm_info);
315 if (tb[TCA_OPTIONS]) {
316 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
317 int qqq = index_in_strings(_q_, name);
318 if (qqq == 0) { /* pfifo_fast aka prio */
319 prio_print_opt(tb[TCA_OPTIONS]);
320 } else if (qqq == 1) { /* class based queuing */
321 cbq_print_opt(tb[TCA_OPTIONS]);
323 bb_error_msg("unknown %s", name);
329 static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
330 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
332 struct tcmsg *msg = NLMSG_DATA(hdr);
333 int len = hdr->nlmsg_len;
334 struct rtattr * tb[TCA_MAX+1];
335 char *name, *classid;
337 /*XXX Eventually factor out common code */
339 if (hdr->nlmsg_type != RTM_NEWTCLASS && hdr->nlmsg_type != RTM_DELTCLASS) {
340 /* bb_error_msg("not a class"); */
341 return 0; /* ??? mimic upstream; should perhaps return -1 */
343 len -= NLMSG_LENGTH(sizeof(*msg));
345 /* bb_error_msg("wrong len %d", len); */
348 /* not the desired interface? */
349 if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc))
351 memset (tb, 0, sizeof(tb));
352 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
353 if (tb[TCA_KIND] == NULL) {
354 /* bb_error_msg("%s: NULL kind", "class"); */
357 if (hdr->nlmsg_type == RTM_DELTCLASS)
360 name = (char*)RTA_DATA(tb[TCA_KIND]);
361 classid = !msg->tcm_handle ? NULL : print_tc_classid(
362 filter_qdisc ? TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
363 printf ("class %s %s", name, classid);
364 if (ENABLE_FEATURE_CLEAN_UP)
367 if (filter_ifindex == 0)
368 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
369 if (msg->tcm_parent == TC_H_ROOT)
371 else if (msg->tcm_parent) {
372 classid = print_tc_classid(filter_qdisc ?
373 TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
374 printf("parent %s ", classid);
375 if (ENABLE_FEATURE_CLEAN_UP)
379 printf("leaf %x ", msg->tcm_info >> 16);
380 /* Do that get_qdisc_kind(RTA_DATA(tb[TCA_KIND])). */
381 if (tb[TCA_OPTIONS]) {
382 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
383 int qqq = index_in_strings(_q_, name);
384 if (qqq == 0) { /* pfifo_fast aka prio */
385 /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/
386 } else if (qqq == 1) { /* class based queuing */
387 /* cbq_print_copt() is identical to cbq_print_opt(). */
388 cbq_print_opt(tb[TCA_OPTIONS]);
390 bb_error_msg("unknown %s", name);
397 static int print_filter(const struct sockaddr_nl *who UNUSED_PARAM,
398 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
400 struct tcmsg *msg = NLMSG_DATA(hdr);
401 int len = hdr->nlmsg_len;
402 struct rtattr * tb[TCA_MAX+1];
406 int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
407 int tc_main(int argc UNUSED_PARAM, char **argv)
409 static const char objects[] ALIGN1 =
410 "qdisc\0""class\0""filter\0"
412 enum { OBJ_qdisc = 0, OBJ_class, OBJ_filter };
413 static const char commands[] ALIGN1 =
414 "add\0""delete\0""change\0"
415 "link\0" /* only qdisc */
419 static const char args[] ALIGN1 =
420 "dev\0" /* qdisc, class, filter */
421 "root\0" /* class, filter */
422 "parent\0" /* class, filter */
423 "qdisc\0" /* class */
424 "handle\0" /* change: qdisc, class(classid) list: filter */
425 "classid\0" /* change: for class use "handle" */
426 "preference\0""priority\0""protocol\0" /* filter */
428 enum { CMD_add = 0, CMD_del, CMD_change, CMD_link, CMD_replace, CMD_show };
429 enum { ARG_dev = 0, ARG_root, ARG_parent, ARG_qdisc,
430 ARG_handle, ARG_classid, ARG_pref, ARG_prio, ARG_proto};
431 struct rtnl_handle rth;
433 int ret, obj, cmd, arg;
443 obj = index_in_substrings(objects, *argv++);
448 cmd = CMD_show; /* list is the default */
450 cmd = index_in_substrings(commands, *argv);
452 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
455 memset(&msg, 0, sizeof(msg));
456 msg.tcm_family = AF_UNSPEC;
459 arg = index_in_substrings(args, *argv);
460 if (arg == ARG_dev) {
463 duparg2("dev", *argv);
465 msg.tcm_ifindex = xll_name_to_index(dev);
467 filter_ifindex = msg.tcm_ifindex;
469 if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
470 || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
473 /* We don't care about duparg2("qdisc handle",*argv) for now */
474 if (get_qdisc_handle(&filter_qdisc, *argv))
475 invarg(*argv, "qdisc");
480 || (obj == OBJ_filter && arg >= ARG_pref)
485 invarg(*argv, "command");
488 if (arg == ARG_root) {
490 duparg("parent", *argv);
491 msg.tcm_parent = TC_H_ROOT;
492 if (obj == OBJ_filter)
493 filter_parent = TC_H_ROOT;
494 } else if (arg == ARG_parent) {
497 duparg(*argv, "parent");
498 if (get_tc_classid(&handle, *argv))
499 invarg(*argv, "parent");
500 msg.tcm_parent = handle;
501 if (obj == OBJ_filter)
502 filter_parent = handle;
503 } else if (arg == ARG_handle) { /* filter::list */
505 duparg(*argv, "handle");
506 /* reject LONG_MIN || LONG_MAX */
508 if ((slash = strchr(handle, '/')) != NULL)
511 msg.tcm_handle = get_u32(*argv, "handle");
512 /* if (slash) {if (get_u32(__u32 &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
513 } else if (arg == ARG_classid && obj == OBJ_class && cmd == CMD_change){
514 } else if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
516 duparg(*argv, "priority");
517 filter_prio = get_u32(*argv, "priority");
518 } else if (arg == ARG_proto) { /* filter::list */
521 duparg(*argv, "protocol");
522 if (ll_proto_a2n(&tmp, *argv))
523 invarg(*argv, "protocol");
527 if (cmd >= CMD_show) { /* show or list */
528 if (obj == OBJ_filter)
529 msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
530 if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
531 obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
532 &msg, sizeof(msg)) < 0)
533 bb_simple_perror_msg_and_die("can't send dump request");
535 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
536 obj == OBJ_class ? print_class : print_filter,
539 if (ENABLE_FEATURE_CLEAN_UP) {