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