ntpd: promote log level 3 to production
[platform/upstream/busybox.git] / networking / tc.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4  *
5  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  *
7  * Bernhard Reutner-Fischer adjusted for busybox
8  */
9
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 ]"
30
31 #include "libbb.h"
32
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 */
37
38 #define parse_rtattr_nested(tb, max, rta) \
39         (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
40
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)); \
49         })
50
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); })
54
55 #define show_details (0) /* not implemented. Does anyone need it? */
56 #define use_iec (0) /* not currently documented in the upstream manpage */
57
58
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];
69 };
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)
76
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)
80 {
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));
94 }
95
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;
100
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;
110  ok:
111         *h = maj;
112         return 0;
113 }
114
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;
119
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;
131         }
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;
144  ok:
145         *h = maj;
146         return 0;
147 }
148
149 static void print_rate(char *buf, int len, uint32_t rate)
150 {
151         double tmp = (double)rate*8;
152
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);
167         }
168 }
169
170 /* This is "pfifo_fast".  */
171 static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
172 {
173         return 0;
174 }
175 static int prio_print_opt(struct rtattr *opt)
176 {
177         int i;
178         struct tc_prio_qopt *qopt;
179         struct rtattr *tb[TCA_PRIO_MAX+1];
180
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]);
189
190         if (tb[TCA_PRIO_MQ])
191                 printf(" multiqueue: o%s ",
192                     *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
193
194         return 0;
195 }
196
197 /* Class Based Queue */
198 static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
199 {
200         return 0;
201 }
202 static int cbq_print_opt(struct rtattr *opt)
203 {
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];
212
213         if (opt == NULL)
214                 goto done;
215         parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
216
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]);
222         }
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]);
228         }
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]);
234         }
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]);
240         }
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]);
248         }
249
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);
259                 }
260         }
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;
267                 }
268                 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
269                         if (comma)
270                                 bb_putchar(',');
271                         printf("isolated");
272                 }
273                 printf(") ");
274         }
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);
285                         }
286                         if (wrr->allot)
287                                 printf("allot %ub ", wrr->allot);
288                 }
289         }
290  done:
291         return 0;
292 }
293
294 static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
295                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
296 {
297         struct tcmsg *msg = NLMSG_DATA(hdr);
298         int len = hdr->nlmsg_len;
299         struct rtattr * tb[TCA_MAX+1];
300         char *name;
301
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 */
305         }
306         len -= NLMSG_LENGTH(sizeof(*msg));
307         if (len < 0) {
308                 /* bb_error_msg("wrong len %d", len); */
309                 return -1;
310         }
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;
319         }
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);
333         }
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);
345         }
346         bb_putchar('\n');
347         return 0;
348 }
349
350 static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
351                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
352 {
353         struct tcmsg *msg = NLMSG_DATA(hdr);
354         int len = hdr->nlmsg_len;
355         struct rtattr * tb[TCA_MAX+1];
356         char *name, *classid;
357
358         /*XXX Eventually factor out common code */
359
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 */
363         }
364         len -= NLMSG_LENGTH(sizeof(*msg));
365         if (len < 0) {
366                 /* bb_error_msg("wrong len %d", len); */
367                 return -1;
368         }
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;
377         }
378         if (hdr->nlmsg_type == RTM_DELTCLASS)
379                 printf("deleted ");
380
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);
387
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);
398         }
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);
412         }
413         bb_putchar('\n');
414
415         return 0;
416 }
417
418 static int print_filter(const struct sockaddr_nl *who UNUSED_PARAM,
419                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
420 {
421         return 0;
422 }
423
424 int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
425 int tc_main(int argc UNUSED_PARAM, char **argv)
426 {
427         static const char objects[] ALIGN1 =
428                 "qdisc\0""class\0""filter\0"
429                 ;
430         enum { OBJ_qdisc = 0, OBJ_class, OBJ_filter };
431         static const char commands[] ALIGN1 =
432                 "add\0""delete\0""change\0"
433                 "link\0" /* only qdisc */
434                 "replace\0"
435                 "show\0""list\0"
436                 ;
437         static const char args[] ALIGN1 =
438                 "dev\0" /* qdisc, class, filter */
439                 "root\0" /* class, filter */
440                 "parent\0" /* class, filter */
441                 "qdisc\0" /* class */
442                 "handle\0" /* change: qdisc, class(classid) list: filter */
443                 "classid\0" /* change: for class use "handle" */
444                 "preference\0""priority\0""protocol\0" /* filter */
445                 ;
446         enum { CMD_add = 0, CMD_del, CMD_change, CMD_link, CMD_replace, CMD_show };
447         enum { ARG_dev = 0, ARG_root, ARG_parent, ARG_qdisc,
448                         ARG_handle, ARG_classid, ARG_pref, ARG_prio, ARG_proto};
449         struct rtnl_handle rth;
450         struct tcmsg msg;
451         int ret, obj, cmd, arg;
452         char *dev = NULL;
453
454         INIT_G();
455
456         if (!*++argv)
457                 bb_show_usage();
458         xrtnl_open(&rth);
459         ret = EXIT_SUCCESS;
460
461         obj = index_in_substrings(objects, *argv++);
462
463         if (obj < OBJ_qdisc)
464                 bb_show_usage();
465         if (!*argv)
466                 cmd = CMD_show; /* list is the default */
467         else {
468                 cmd = index_in_substrings(commands, *argv);
469                 if (cmd < 0)
470                         bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
471                 argv++;
472         }
473         memset(&msg, 0, sizeof(msg));
474         msg.tcm_family = AF_UNSPEC;
475         ll_init_map(&rth);
476         while (*argv) {
477                 arg = index_in_substrings(args, *argv);
478                 if (arg == ARG_dev) {
479                         NEXT_ARG();
480                         if (dev)
481                                 duparg2("dev", *argv);
482                         dev = *argv++;
483                         msg.tcm_ifindex = xll_name_to_index(dev);
484                         if (cmd >= CMD_show)
485                                 filter_ifindex = msg.tcm_ifindex;
486                 } else
487                 if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
488                  || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
489                 ) {
490                         NEXT_ARG();
491                         /* We don't care about duparg2("qdisc handle",*argv) for now */
492                         if (get_qdisc_handle(&filter_qdisc, *argv))
493                                 invarg(*argv, "qdisc");
494                 } else
495                 if (obj != OBJ_qdisc
496                  && (arg == ARG_root
497                     || arg == ARG_parent
498                     || (obj == OBJ_filter && arg >= ARG_pref)
499                     )
500                 ) {
501                         /* nothing */
502                 } else {
503                         invarg(*argv, "command");
504                 }
505                 NEXT_ARG();
506                 if (arg == ARG_root) {
507                         if (msg.tcm_parent)
508                                 duparg("parent", *argv);
509                         msg.tcm_parent = TC_H_ROOT;
510                         if (obj == OBJ_filter)
511                                 filter_parent = TC_H_ROOT;
512                 } else if (arg == ARG_parent) {
513                         uint32_t handle;
514                         if (msg.tcm_parent)
515                                 duparg(*argv, "parent");
516                         if (get_tc_classid(&handle, *argv))
517                                 invarg(*argv, "parent");
518                         msg.tcm_parent = handle;
519                         if (obj == OBJ_filter)
520                                 filter_parent = handle;
521                 } else if (arg == ARG_handle) { /* filter::list */
522                         if (msg.tcm_handle)
523                                 duparg(*argv, "handle");
524                         /* reject LONG_MIN || LONG_MAX */
525                         /* TODO: for fw
526                         slash = strchr(handle, '/');
527                         if (slash != NULL)
528                                    *slash = '\0';
529                          */
530                         msg.tcm_handle = get_u32(*argv, "handle");
531                         /* if (slash) {if (get_u32(uint32_t &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
532                 } else if (arg == ARG_classid && obj == OBJ_class && cmd == CMD_change){
533                 } else if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
534                         if (filter_prio)
535                                 duparg(*argv, "priority");
536                         filter_prio = get_u32(*argv, "priority");
537                 } else if (arg == ARG_proto) { /* filter::list */
538                         uint16_t tmp;
539                         if (filter_proto)
540                                 duparg(*argv, "protocol");
541                         if (ll_proto_a2n(&tmp, *argv))
542                                 invarg(*argv, "protocol");
543                         filter_proto = tmp;
544                 }
545         }
546         if (cmd >= CMD_show) { /* show or list */
547                 if (obj == OBJ_filter)
548                         msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
549                 if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
550                                                 obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
551                                                 &msg, sizeof(msg)) < 0)
552                         bb_simple_perror_msg_and_die("can't send dump request");
553
554                 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
555                                                 obj == OBJ_class ? print_class : print_filter,
556                                                 NULL);
557         }
558         if (ENABLE_FEATURE_CLEAN_UP) {
559                 rtnl_close(&rth);
560         }
561         return ret;
562 }