top: add keyboard commands to --help
[oweals/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 #include "libbb.h"
11
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 */
16
17 #define parse_rtattr_nested(tb, max, rta) \
18         (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
19
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); \
26           } else \
27                         memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
28         })
29
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); })
33
34 #define show_details (0) /* not implemented. Does anyone need it? */
35 #define use_iec (0) /* not currently documented in the upstream manpage */
36
37
38 struct globals {
39         int filter_ifindex;
40         __u32 filter_qdisc;
41         __u32 filter_parent;
42         __u32 filter_prio;
43         __u32 filter_proto;
44 } FIX_ALIASING;
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)
51
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(); \
56 } while (0)
57
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)
61 {
62 #if 0 /* IMPOSSIBLE */
63         if (cid == TC_H_ROOT)
64                 return xasprintf("root");
65         else
66 #endif
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);
73         else
74                 return xasprintf("%x:%x", TC_H_MAJ(cid)>>16, TC_H_MIN(cid));
75 }
76
77 /* Get a qdisc handle.  Return 0 on success, !0 otherwise.  */
78 static int get_qdisc_handle(__u32 *h, const char *str) {
79         __u32 maj;
80         char *p;
81
82         maj = TC_H_UNSPEC;
83         if (!strcmp(str, "none"))
84                 goto ok;
85         maj = strtoul(str, &p, 16);
86         if (p == str)
87                 return 1;
88         maj <<= 16;
89         if (*p != ':' && *p != '\0')
90                 return 1;
91  ok:
92         *h = maj;
93         return 0;
94 }
95
96 /* Get class ID.  Return 0 on success, !0 otherwise.  */
97 static int get_tc_classid(__u32 *h, const char *str) {
98         __u32 maj, min;
99         char *p;
100
101         maj = TC_H_ROOT;
102         if (!strcmp(str, "root"))
103                 goto ok;
104         maj = TC_H_UNSPEC;
105         if (!strcmp(str, "none"))
106                 goto ok;
107         maj = strtoul(str, &p, 16);
108         if (p == str) {
109                 if (*p != ':')
110                         return 1;
111                 maj = 0;
112         }
113         if (*p == ':') {
114                 if (maj >= (1<<16))
115                         return 1;
116                 maj <<= 16;
117                 str = p + 1;
118                 min = strtoul(str, &p, 16);
119 //FIXME: check for "" too?
120                 if (*p != '\0' || min >= (1<<16))
121                         return 1;
122                 maj |= min;
123         } else if (*p != 0)
124                 return 1;
125  ok:
126         *h = maj;
127         return 0;
128 }
129
130 static void print_rate(char *buf, int len, uint32_t rate)
131 {
132         double tmp = (double)rate*8;
133
134         if (use_iec) {
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);
139                 else
140                         snprintf(buf, len, "%.0fbit", tmp);
141         } else {
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);
146                 else
147                         snprintf(buf, len, "%.0fbit",  tmp);
148         }
149 }
150
151 /* This is "pfifo_fast".  */
152 static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
153 {
154         return 0;
155 }
156 static int prio_print_opt(struct rtattr *opt)
157 {
158         int i;
159         struct tc_prio_qopt *qopt;
160         struct rtattr *tb[TCA_PRIO_MAX+1];
161
162         if (opt == NULL)
163                 return 0;
164         parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt));
165         if (tb == NULL)
166                 return 0;
167         printf("bands %u priomap ", qopt->bands);
168         for (i=0; i<=TC_PRIO_MAX; i++)
169                 printf(" %d", qopt->priomap[i]);
170
171         if (tb[TCA_PRIO_MQ])
172                 printf(" multiqueue: o%s ",
173                     *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
174
175         return 0;
176 }
177
178 /* Class Based Queue */
179 static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
180 {
181         return 0;
182 }
183 static int cbq_print_opt(struct rtattr *opt)
184 {
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";
192         char buf[64];
193
194         if (opt == NULL)
195                 goto done;
196         parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
197
198         if (tb[TCA_CBQ_RATE]) {
199                 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
200                         bb_error_msg(error, "rate");
201                 else
202                         r = RTA_DATA(tb[TCA_CBQ_RATE]);
203         }
204         if (tb[TCA_CBQ_LSSOPT]) {
205                 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
206                         bb_error_msg(error, "lss");
207                 else
208                         lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
209         }
210         if (tb[TCA_CBQ_WRROPT]) {
211                 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
212                         bb_error_msg(error, "wrr");
213                 else
214                         wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
215         }
216         if (tb[TCA_CBQ_FOPT]) {
217                 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
218                         bb_error_msg(error, "fopt");
219                 else
220                         fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
221         }
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));
227                 else
228                         ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
229         }
230
231         if (r) {
232                 print_rate(buf, sizeof(buf), r->rate);
233                 printf("rate %s ", buf);
234                 if (show_details) {
235                         printf("cell %ub ", 1<<r->cell_log);
236                         if (r->mpu)
237                                 printf("mpu %ub ", r->mpu);
238                         if (r->overhead)
239                                 printf("overhead %ub ", r->overhead);
240                 }
241         }
242         if (lss && lss->flags) {
243                 bool comma = false;
244                 bb_putchar('(');
245                 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
246                         printf("bounded");
247                         comma = true;
248                 }
249                 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
250                         if (comma)
251                                 bb_putchar(',');
252                         printf("isolated");
253                 }
254                 printf(") ");
255         }
256         if (wrr) {
257                 if (wrr->priority != TC_CBQ_MAXPRIO)
258                         printf("prio %u", wrr->priority);
259                 else
260                         printf("prio no-transmit");
261                 if (show_details) {
262                         printf("/%u ", wrr->cpriority);
263                         if (wrr->weight != 1) {
264                                 print_rate(buf, sizeof(buf), wrr->weight);
265                                 printf("weight %s ", buf);
266                         }
267                         if (wrr->allot)
268                                 printf("allot %ub ", wrr->allot);
269                 }
270         }
271  done:
272         return 0;
273 }
274
275 static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
276                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
277 {
278         struct tcmsg *msg = NLMSG_DATA(hdr);
279         int len = hdr->nlmsg_len;
280         struct rtattr * tb[TCA_MAX+1];
281         char *name;
282
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 */
286         }
287         len -= NLMSG_LENGTH(sizeof(*msg));
288         if (len < 0) {
289                 /* bb_error_msg("wrong len %d", len); */
290                 return -1;
291         }
292         /* not the desired interface? */
293         if (filter_ifindex && filter_ifindex != msg->tcm_ifindex)
294                 return 0;
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"); */
299                 return -1;
300         }
301         if (hdr->nlmsg_type == RTM_DELQDISC)
302                 printf("deleted ");
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)
308                 printf("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)
313                         free(classid);
314         }
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]);
324                 } else
325                         bb_error_msg("unknown %s", name);
326         }
327         bb_putchar('\n');
328         return 0;
329 }
330
331 static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
332                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
333 {
334         struct tcmsg *msg = NLMSG_DATA(hdr);
335         int len = hdr->nlmsg_len;
336         struct rtattr * tb[TCA_MAX+1];
337         char *name, *classid;
338
339         /*XXX Eventually factor out common code */
340
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 */
344         }
345         len -= NLMSG_LENGTH(sizeof(*msg));
346         if (len < 0) {
347                 /* bb_error_msg("wrong len %d", len); */
348                 return -1;
349         }
350         /* not the desired interface? */
351         if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc))
352                 return 0;
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"); */
357                 return -1;
358         }
359         if (hdr->nlmsg_type == RTM_DELTCLASS)
360                 printf("deleted ");
361
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)
367                 free(classid);
368
369         if (filter_ifindex == 0)
370                 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
371         if (msg->tcm_parent == TC_H_ROOT)
372                 printf("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)
378                         free(classid);
379         }
380         if (msg->tcm_info)
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]);
391                 } else
392                         bb_error_msg("unknown %s", name);
393         }
394         bb_putchar('\n');
395
396         return 0;
397 }
398
399 static int print_filter(const struct sockaddr_nl *who UNUSED_PARAM,
400                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
401 {
402         struct tcmsg *msg = NLMSG_DATA(hdr);
403         int len = hdr->nlmsg_len;
404         struct rtattr * tb[TCA_MAX+1];
405         return 0;
406 }
407
408 int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
409 int tc_main(int argc UNUSED_PARAM, char **argv)
410 {
411         static const char objects[] ALIGN1 =
412                 "qdisc\0""class\0""filter\0"
413                 ;
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 */
418                 "replace\0"
419                 "show\0""list\0"
420                 ;
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 */
429                 ;
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;
434         struct tcmsg msg;
435         int ret, obj, cmd, arg;
436         char *dev = NULL;
437
438         INIT_G();
439
440         if (!*++argv)
441                 bb_show_usage();
442         xrtnl_open(&rth);
443         ret = EXIT_SUCCESS;
444
445         obj = index_in_substrings(objects, *argv++);
446
447         if (obj < OBJ_qdisc)
448                 bb_show_usage();
449         if (!*argv)
450                 cmd = CMD_show; /* list is the default */
451         else {
452                 cmd = index_in_substrings(commands, *argv);
453                 if (cmd < 0)
454                         bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
455                 argv++;
456         }
457         memset(&msg, 0, sizeof(msg));
458         msg.tcm_family = AF_UNSPEC;
459         ll_init_map(&rth);
460         while (*argv) {
461                 arg = index_in_substrings(args, *argv);
462                 if (arg == ARG_dev) {
463                         NEXT_ARG();
464                         if (dev)
465                                 duparg2("dev", *argv);
466                         dev = *argv++;
467                         msg.tcm_ifindex = xll_name_to_index(dev);
468                         if (cmd >= CMD_show)
469                                 filter_ifindex = msg.tcm_ifindex;
470                 } else
471                 if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
472                  || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
473                 ) {
474                         NEXT_ARG();
475                         /* We don't care about duparg2("qdisc handle",*argv) for now */
476                         if (get_qdisc_handle(&filter_qdisc, *argv))
477                                 invarg(*argv, "qdisc");
478                 } else
479                 if (obj != OBJ_qdisc
480                  && (arg == ARG_root
481                     || arg == ARG_parent
482                     || (obj == OBJ_filter && arg >= ARG_pref)
483                     )
484                 ) {
485                         /* nothing */
486                 } else {
487                         invarg(*argv, "command");
488                 }
489                 NEXT_ARG();
490                 if (arg == ARG_root) {
491                         if (msg.tcm_parent)
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) {
497                         __u32 handle;
498                         if (msg.tcm_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 */
506                         if (msg.tcm_handle)
507                                 duparg(*argv, "handle");
508                         /* reject LONG_MIN || LONG_MAX */
509                         /* TODO: for fw
510                            if ((slash = strchr(handle, '/')) != NULL)
511                                    *slash = '\0';
512                          */
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 */
517                         if (filter_prio)
518                                 duparg(*argv, "priority");
519                         filter_prio = get_u32(*argv, "priority");
520                 } else if (arg == ARG_proto) { /* filter::list */
521                         uint16_t tmp;
522                         if (filter_proto)
523                                 duparg(*argv, "protocol");
524                         if (ll_proto_a2n(&tmp, *argv))
525                                 invarg(*argv, "protocol");
526                         filter_proto = tmp;
527                 }
528         }
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");
536
537                 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
538                                                 obj == OBJ_class ? print_class : print_filter,
539                                                 NULL);
540         }
541         if (ENABLE_FEATURE_CLEAN_UP) {
542                 rtnl_close(&rth);
543         }
544         return ret;
545 }