tc.c 14 KB

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