blob: 3115a525457fe995a43467b857a6db1547ae0607 [file] [log] [blame]
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00001/* vi: set sw=4 ts=4: */
2/*
3 * tc.c "tc" utility frontend.
4 *
5 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6 *
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00009 * Bernhard Reutner-Fischer adjusted for busybox
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000010 */
11
12#include "libbb.h"
13
14#include "libiproute/utils.h"
15#include "libiproute/ip_common.h"
16#include "libiproute/rt_names.h"
17#include <linux/pkt_sched.h> /* for the TC_H_* macros */
18
19#define parse_rtattr_nested(tb, max, rta) \
20 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
21
22/* nullifies tb on error */
23#define __parse_rtattr_nested_compat(tb, max, rta, len) \
24 ({if ((RTA_PAYLOAD(rta) >= len) && \
25 (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr))) { \
26 rta = RTA_DATA(rta) + RTA_ALIGN(len); \
27 parse_rtattr_nested(tb, max, rta); \
28 } else \
29 memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
30 })
31
32#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
33 ({data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
34 __parse_rtattr_nested_compat(tb, max, rta, len); })
35
36#define show_details (0) /* not implemented. Does anyone need it? */
37#define use_iec (0) /* not currently documented in the upstream manpage */
38
39
40struct globals {
41 int filter_ifindex;
42 __u32 filter_qdisc;
43 __u32 filter_parent;
44 __u32 filter_prio;
45 __u32 filter_proto;
46};
47
48#define G (*(struct globals*)&bb_common_bufsiz1)
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
55void BUG_tc_globals_too_big(void);
56#define INIT_G() do { \
57 if (sizeof(G) > COMMON_BUFSIZE) \
58 BUG_tc_globals_too_big(); \
59} while (0)
60
61/* Allocates a buffer containing the name of a class id.
62 * The caller must free the returned memory. */
63static char* print_tc_classid(uint32_t cid)
64{
65#if 0 /* IMPOSSIBLE */
66 if (cid == TC_H_ROOT)
67 return xasprintf("root");
68 else
69#endif
70 if (cid == TC_H_UNSPEC)
71 return xasprintf("none");
72 else if (TC_H_MAJ(cid) == 0)
73 return xasprintf(":%x", TC_H_MIN(cid));
74 else if (TC_H_MIN(cid) == 0)
75 return xasprintf("%x:", TC_H_MAJ(cid)>>16);
76 else
77 return xasprintf("%x:%x", TC_H_MAJ(cid)>>16, TC_H_MIN(cid));
78}
79
80/* Get a qdisc handle. Return 0 on success, !0 otherwise. */
81static int get_qdisc_handle(__u32 *h, const char *str) {
82 __u32 maj;
83 char *p;
84
85 maj = TC_H_UNSPEC;
86 if (!strcmp(str, "none"))
87 goto ok;
88 maj = strtoul(str, &p, 16);
89 if (p == str)
90 return 1;
91 maj <<= 16;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020092 if (*p != ':' && *p != '\0')
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000093 return 1;
94 ok:
95 *h = maj;
96 return 0;
97}
98
99/* Get class ID. Return 0 on success, !0 otherwise. */
100static int get_tc_classid(__u32 *h, const char *str) {
101 __u32 maj, min;
102 char *p;
103
104 maj = TC_H_ROOT;
105 if (!strcmp(str, "root"))
106 goto ok;
107 maj = TC_H_UNSPEC;
108 if (!strcmp(str, "none"))
109 goto ok;
110 maj = strtoul(str, &p, 16);
111 if (p == str) {
112 if (*p != ':')
113 return 1;
114 maj = 0;
115 }
116 if (*p == ':') {
117 if (maj >= (1<<16))
118 return 1;
119 maj <<= 16;
120 str = p + 1;
121 min = strtoul(str, &p, 16);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200122//FIXME: check for "" too?
123 if (*p != '\0' || min >= (1<<16))
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000124 return 1;
125 maj |= min;
126 } else if (*p != 0)
127 return 1;
128 ok:
129 *h = maj;
130 return 0;
131}
132
133static void print_rate(char *buf, int len, uint32_t rate)
134{
135 double tmp = (double)rate*8;
136
137 if (use_iec) {
138 if (tmp >= 1000.0*1024.0*1024.0)
139 snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0);
140 else if (tmp >= 1000.0*1024)
141 snprintf(buf, len, "%.0fKibit", tmp/1024);
142 else
143 snprintf(buf, len, "%.0fbit", tmp);
144 } else {
145 if (tmp >= 1000.0*1000000.0)
146 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
147 else if (tmp >= 1000.0 * 1000.0)
148 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
149 else
150 snprintf(buf, len, "%.0fbit", tmp);
151 }
152}
153
154/* This is "pfifo_fast". */
155static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
156{
157 return 0;
158}
159static int prio_print_opt(struct rtattr *opt)
160{
161 int i;
162 struct tc_prio_qopt *qopt;
163 struct rtattr *tb[TCA_PRIO_MAX+1];
164
165 if (opt == NULL)
166 return 0;
167 parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt));
168 if (tb == NULL)
169 return 0;
170 printf("bands %u priomap ", qopt->bands);
171 for (i=0; i<=TC_PRIO_MAX; i++)
172 printf(" %d", qopt->priomap[i]);
173
174 if (tb[TCA_PRIO_MQ])
175 printf(" multiqueue: o%s ",
176 *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
177
178 return 0;
179}
180
181/* Class Based Queue */
182static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
183{
184 return 0;
185}
186static int cbq_print_opt(struct rtattr *opt)
187{
188 struct rtattr *tb[TCA_CBQ_MAX+1];
189 struct tc_ratespec *r = NULL;
190 struct tc_cbq_lssopt *lss = NULL;
191 struct tc_cbq_wrropt *wrr = NULL;
192 struct tc_cbq_fopt *fopt = NULL;
193 struct tc_cbq_ovl *ovl = NULL;
194 const char * const error = "CBQ: too short %s opt";
195 RESERVE_CONFIG_BUFFER(buf, 64);
196
197 if (opt == NULL)
198 goto done;
199 parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
200
201 if (tb[TCA_CBQ_RATE]) {
202 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
203 bb_error_msg(error, "rate");
204 else
205 r = RTA_DATA(tb[TCA_CBQ_RATE]);
206 }
207 if (tb[TCA_CBQ_LSSOPT]) {
208 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
209 bb_error_msg(error, "lss");
210 else
211 lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
212 }
213 if (tb[TCA_CBQ_WRROPT]) {
214 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
215 bb_error_msg(error, "wrr");
216 else
217 wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
218 }
219 if (tb[TCA_CBQ_FOPT]) {
220 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
221 bb_error_msg(error, "fopt");
222 else
223 fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
224 }
225 if (tb[TCA_CBQ_OVL_STRATEGY]) {
226 if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl))
227 bb_error_msg("CBQ: too short overlimit strategy %u/%u",
228 (unsigned) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]),
229 (unsigned) sizeof(*ovl));
230 else
231 ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
232 }
233
234 if (r) {
235 print_rate(buf, sizeof(buf), r->rate);
236 printf("rate %s ", buf);
237 if (show_details) {
238 printf("cell %ub ", 1<<r->cell_log);
239 if (r->mpu)
240 printf("mpu %ub ", r->mpu);
241 if (r->overhead)
242 printf("overhead %ub ", r->overhead);
243 }
244 }
245 if (lss && lss->flags) {
246 bool comma = false;
247 bb_putchar('(');
248 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
249 printf("bounded");
250 comma = true;
251 }
252 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
253 if (comma)
254 bb_putchar(',');
255 printf("isolated");
256 }
257 printf(") ");
258 }
259 if (wrr) {
260 if (wrr->priority != TC_CBQ_MAXPRIO)
261 printf("prio %u", wrr->priority);
262 else
263 printf("prio no-transmit");
264 if (show_details) {
265 printf("/%u ", wrr->cpriority);
266 if (wrr->weight != 1) {
267 print_rate(buf, sizeof(buf), wrr->weight);
268 printf("weight %s ", buf);
269 }
270 if (wrr->allot)
271 printf("allot %ub ", wrr->allot);
272 }
273 }
274 done:
275 RELEASE_CONFIG_BUFFER(buf);
276 return 0;
277}
278
279static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
280 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
281{
282 struct tcmsg *msg = NLMSG_DATA(hdr);
283 int len = hdr->nlmsg_len;
284 struct rtattr * tb[TCA_MAX+1];
285 char *name;
286
287 if (hdr->nlmsg_type != RTM_NEWQDISC && hdr->nlmsg_type != RTM_DELQDISC) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000288 /* bb_error_msg("not a qdisc"); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000289 return 0; /* ??? mimic upstream; should perhaps return -1 */
290 }
291 len -= NLMSG_LENGTH(sizeof(*msg));
292 if (len < 0) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000293 /* bb_error_msg("wrong len %d", len); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000294 return -1;
295 }
296 /* not the desired interface? */
297 if (filter_ifindex && filter_ifindex != msg->tcm_ifindex)
298 return 0;
299 memset (tb, 0, sizeof(tb));
300 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
301 if (tb[TCA_KIND] == NULL) {
302 /* bb_error_msg("%s: NULL kind", "qdisc"); */
303 return -1;
304 }
305 if (hdr->nlmsg_type == RTM_DELQDISC)
306 printf("deleted ");
307 name = (char*)RTA_DATA(tb[TCA_KIND]);
308 printf("qdisc %s %x: ", name, msg->tcm_handle>>16);
309 if (filter_ifindex == 0)
310 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
311 if (msg->tcm_parent == TC_H_ROOT)
312 printf("root ");
313 else if (msg->tcm_parent) {
314 char *classid = print_tc_classid(msg->tcm_parent);
315 printf("parent %s ", classid);
316 if (ENABLE_FEATURE_CLEAN_UP)
317 free(classid);
318 }
319 if (msg->tcm_info != 1)
320 printf("refcnt %d ", msg->tcm_info);
321 if (tb[TCA_OPTIONS]) {
322 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
323 int qqq = index_in_strings(_q_, name);
324 if (qqq == 0) { /* pfifo_fast aka prio */
325 prio_print_opt(tb[TCA_OPTIONS]);
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200326 } else if (qqq == 1) { /* class based queuing */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000327 cbq_print_opt(tb[TCA_OPTIONS]);
328 } else
329 bb_error_msg("unknown %s", name);
330 }
331 bb_putchar('\n');
332 return 0;
333}
334
335static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
336 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
337{
338 struct tcmsg *msg = NLMSG_DATA(hdr);
339 int len = hdr->nlmsg_len;
340 struct rtattr * tb[TCA_MAX+1];
341 char *name, *classid;
342
343 /*XXX Eventually factor out common code */
344
345 if (hdr->nlmsg_type != RTM_NEWTCLASS && hdr->nlmsg_type != RTM_DELTCLASS) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000346 /* bb_error_msg("not a class"); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000347 return 0; /* ??? mimic upstream; should perhaps return -1 */
348 }
349 len -= NLMSG_LENGTH(sizeof(*msg));
350 if (len < 0) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000351 /* bb_error_msg("wrong len %d", len); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000352 return -1;
353 }
354 /* not the desired interface? */
355 if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc))
356 return 0;
357 memset (tb, 0, sizeof(tb));
358 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
359 if (tb[TCA_KIND] == NULL) {
360 /* bb_error_msg("%s: NULL kind", "class"); */
361 return -1;
362 }
363 if (hdr->nlmsg_type == RTM_DELTCLASS)
364 printf("deleted ");
365
366 name = (char*)RTA_DATA(tb[TCA_KIND]);
367 classid = !msg->tcm_handle ? NULL : print_tc_classid(
368 filter_qdisc ? TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
369 printf ("class %s %s", name, classid);
370 if (ENABLE_FEATURE_CLEAN_UP)
371 free(classid);
372
373 if (filter_ifindex == 0)
374 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
375 if (msg->tcm_parent == TC_H_ROOT)
376 printf("root ");
377 else if (msg->tcm_parent) {
378 classid = print_tc_classid(filter_qdisc ?
379 TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
380 printf("parent %s ", classid);
381 if (ENABLE_FEATURE_CLEAN_UP)
382 free(classid);
383 }
384 if (msg->tcm_info)
385 printf("leaf %x ", msg->tcm_info >> 16);
386 /* Do that get_qdisc_kind(RTA_DATA(tb[TCA_KIND])). */
387 if (tb[TCA_OPTIONS]) {
388 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
389 int qqq = index_in_strings(_q_, name);
390 if (qqq == 0) { /* pfifo_fast aka prio */
391 /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200392 } else if (qqq == 1) { /* class based queuing */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000393 /* cbq_print_copt() is identical to cbq_print_opt(). */
394 cbq_print_opt(tb[TCA_OPTIONS]);
395 } else
396 bb_error_msg("unknown %s", name);
397 }
398 bb_putchar('\n');
399
400 return 0;
401}
402
403static int print_filter(const struct sockaddr_nl *who UNUSED_PARAM,
404 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
405{
406 struct tcmsg *msg = NLMSG_DATA(hdr);
407 int len = hdr->nlmsg_len;
408 struct rtattr * tb[TCA_MAX+1];
409 return 0;
410}
411
412int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
413int tc_main(int argc UNUSED_PARAM, char **argv)
414{
415 static const char objects[] ALIGN1 =
416 "qdisc\0""class\0""filter\0"
417 ;
418 enum { OBJ_qdisc = 0, OBJ_class, OBJ_filter };
419 static const char commands[] ALIGN1 =
420 "add\0""delete\0""change\0"
421 "link\0" /* only qdisc */
422 "replace\0"
423 "show\0""list\0"
424 ;
425 static const char args[] ALIGN1 =
426 "dev\0" /* qdisc, class, filter */
427 "root\0" /* class, filter */
428 "parent\0" /* class, filter */
429 "qdisc\0" /* class */
430 "handle\0" /* change: qdisc, class(classid) list: filter */
431 "classid\0" /* change: for class use "handle" */
432 "preference\0""priority\0""protocol\0" /* filter */
433 ;
434 enum { CMD_add = 0, CMD_del, CMD_change, CMD_link, CMD_replace, CMD_show };
435 enum { ARG_dev = 0, ARG_root, ARG_parent, ARG_qdisc,
436 ARG_handle, ARG_classid, ARG_pref, ARG_prio, ARG_proto};
437 struct rtnl_handle rth;
438 struct tcmsg msg;
439 int ret, obj, cmd, arg;
440 char *dev = NULL;
441
442 INIT_G();
443
444 if (!*++argv)
445 bb_show_usage();
446 xrtnl_open(&rth);
447 ret = EXIT_SUCCESS;
448
449 obj = index_in_substrings(objects, *argv++);
450
451 if (obj < OBJ_qdisc)
452 bb_show_usage();
453 if (!*argv)
454 cmd = CMD_show; /* list is the default */
455 else {
456 cmd = index_in_substrings(commands, *argv);
457 if (cmd < 0)
458 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
459 argv++;
460 }
461 memset(&msg, 0, sizeof(msg));
462 msg.tcm_family = AF_UNSPEC;
463 ll_init_map(&rth);
464 while (*argv) {
465 arg = index_in_substrings(args, *argv);
466 if (arg == ARG_dev) {
467 NEXT_ARG();
468 if (dev)
469 duparg2("dev", *argv);
470 dev = *argv++;
471 msg.tcm_ifindex = xll_name_to_index(dev);
472 if (cmd >= CMD_show)
473 filter_ifindex = msg.tcm_ifindex;
474 } else if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
475 || (arg == ARG_handle && obj == OBJ_qdisc
476 && cmd == CMD_change)) {
477 NEXT_ARG();
478 /* We don't care about duparg2("qdisc handle",*argv) for now */
479 if (get_qdisc_handle(&filter_qdisc, *argv))
480 invarg(*argv, "qdisc");
481 } else if (obj != OBJ_qdisc &&
482 (arg == ARG_root
483 || arg == ARG_parent
484 || (obj == OBJ_filter && arg >= ARG_pref))) {
485 } else {
486 invarg(*argv, "command");
487 }
488 NEXT_ARG();
489 if (arg == ARG_root) {
490 if (msg.tcm_parent)
491 duparg("parent", *argv);
492 msg.tcm_parent = TC_H_ROOT;
493 if (obj == OBJ_filter)
494 filter_parent = TC_H_ROOT;
495 } else if (arg == ARG_parent) {
496 __u32 handle;
497 if (msg.tcm_parent)
498 duparg(*argv, "parent");
499 if (get_tc_classid(&handle, *argv))
500 invarg(*argv, "parent");
501 msg.tcm_parent = handle;
502 if (obj == OBJ_filter)
503 filter_parent = handle;
504 } else if (arg == ARG_handle) { /* filter::list */
505 if (msg.tcm_handle)
506 duparg(*argv, "handle");
507 /* reject LONG_MIN || LONG_MAX */
508 /* TODO: for fw
509 if ((slash = strchr(handle, '/')) != NULL)
510 *slash = '\0';
511 */
Denis Vlasenko76140a72009-03-05 09:21:57 +0000512 msg.tcm_handle = get_u32(*argv, "handle");
513 /* if (slash) {if (get_u32(__u32 &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000514 } else if (arg == ARG_classid && obj == OBJ_class && cmd == CMD_change){
515 } else if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
516 if (filter_prio)
517 duparg(*argv, "priority");
Denis Vlasenko76140a72009-03-05 09:21:57 +0000518 filter_prio = get_u32(*argv, "priority");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000519 } else if (arg == ARG_proto) { /* filter::list */
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200520 uint16_t tmp;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000521 if (filter_proto)
522 duparg(*argv, "protocol");
523 if (ll_proto_a2n(&tmp, *argv))
524 invarg(*argv, "protocol");
525 filter_proto = tmp;
526 }
527 }
528 if (cmd >= CMD_show) { /* show or list */
529 if (obj == OBJ_filter)
530 msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
531 if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
532 obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
533 &msg, sizeof(msg)) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100534 bb_simple_perror_msg_and_die("can't send dump request");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000535
536 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
537 obj == OBJ_class ? print_class : print_filter,
538 NULL);
539 }
540 if (ENABLE_FEATURE_CLEAN_UP) {
541 rtnl_close(&rth);
542 }
543 return ret;
544}