tc.c 14 KB

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