tc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * lib/route/tc.c Traffic Control
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup rtnl
  13. * @defgroup tc Traffic Control
  14. * @brief
  15. * @{
  16. */
  17. #include <netlink-local.h>
  18. #include <netlink-tc.h>
  19. #include <netlink/netlink.h>
  20. #include <netlink/utils.h>
  21. #include <netlink/route/rtnl.h>
  22. #include <netlink/route/link.h>
  23. #include <netlink/route/tc.h>
  24. /** @cond SKIP */
  25. static struct nla_policy tc_policy[TCA_MAX+1] = {
  26. [TCA_KIND] = { .type = NLA_STRING,
  27. .maxlen = TCKINDSIZ },
  28. [TCA_STATS] = { .minlen = sizeof(struct tc_stats) },
  29. [TCA_STATS2] = { .type = NLA_NESTED },
  30. };
  31. int tca_parse(struct nlattr **tb, int maxattr, struct rtnl_tca *g,
  32. struct nla_policy *policy)
  33. {
  34. if (g->ce_mask & TCA_ATTR_OPTS)
  35. return nla_parse(tb, maxattr,
  36. (struct nlattr *) g->tc_opts->d_data,
  37. g->tc_opts->d_size, policy);
  38. else {
  39. /* Ugly but tb[] must be in a defined state even if no
  40. * attributes can be found. */
  41. memset(tb, 0, sizeof(struct nlattr *) * (maxattr + 1));
  42. return 0;
  43. }
  44. }
  45. static struct nla_policy tc_stats2_policy[TCA_STATS_MAX+1] = {
  46. [TCA_STATS_BASIC] = { .minlen = sizeof(struct gnet_stats_basic) },
  47. [TCA_STATS_RATE_EST] = { .minlen = sizeof(struct gnet_stats_rate_est) },
  48. [TCA_STATS_QUEUE] = { .minlen = sizeof(struct gnet_stats_queue) },
  49. };
  50. int tca_msg_parser(struct nlmsghdr *n, struct rtnl_tca *g)
  51. {
  52. struct nlattr *tb[TCA_MAX + 1];
  53. struct tcmsg *tm;
  54. int err;
  55. err = nlmsg_parse(n, sizeof(*tm), tb, TCA_MAX, tc_policy);
  56. if (err < 0)
  57. return err;
  58. if (tb[TCA_KIND] == NULL)
  59. return nl_error(EINVAL, "Missing tca kind TLV");
  60. nla_strlcpy(g->tc_kind, tb[TCA_KIND], TCKINDSIZ);
  61. tm = nlmsg_data(n);
  62. g->tc_family = tm->tcm_family;
  63. g->tc_ifindex = tm->tcm_ifindex;
  64. g->tc_handle = tm->tcm_handle;
  65. g->tc_parent = tm->tcm_parent;
  66. g->tc_info = tm->tcm_info;
  67. g->ce_mask = (TCA_ATTR_FAMILY | TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE |
  68. TCA_ATTR_PARENT | TCA_ATTR_INFO | TCA_ATTR_KIND);
  69. if (tb[TCA_OPTIONS]) {
  70. g->tc_opts = nla_get_data(tb[TCA_OPTIONS]);
  71. if (!g->tc_opts)
  72. return nl_errno(ENOMEM);
  73. g->ce_mask |= TCA_ATTR_OPTS;
  74. }
  75. if (tb[TCA_STATS2]) {
  76. struct nlattr *tbs[TCA_STATS_MAX + 1];
  77. err = nla_parse_nested(tbs, TCA_STATS_MAX, tb[TCA_STATS2],
  78. tc_stats2_policy);
  79. if (err < 0)
  80. return err;
  81. if (tbs[TCA_STATS_BASIC]) {
  82. struct gnet_stats_basic *bs;
  83. bs = nla_data(tbs[TCA_STATS_BASIC]);
  84. g->tc_stats[RTNL_TC_BYTES] = bs->bytes;
  85. g->tc_stats[RTNL_TC_PACKETS] = bs->packets;
  86. }
  87. if (tbs[TCA_STATS_RATE_EST]) {
  88. struct gnet_stats_rate_est *re;
  89. re = nla_data(tbs[TCA_STATS_RATE_EST]);
  90. g->tc_stats[RTNL_TC_RATE_BPS] = re->bps;
  91. g->tc_stats[RTNL_TC_RATE_PPS] = re->pps;
  92. }
  93. if (tbs[TCA_STATS_QUEUE]) {
  94. struct gnet_stats_queue *q;
  95. q = nla_data(tbs[TCA_STATS_QUEUE]);
  96. g->tc_stats[RTNL_TC_QLEN] = q->qlen;
  97. g->tc_stats[RTNL_TC_BACKLOG] = q->backlog;
  98. g->tc_stats[RTNL_TC_DROPS] = q->drops;
  99. g->tc_stats[RTNL_TC_REQUEUES] = q->requeues;
  100. g->tc_stats[RTNL_TC_OVERLIMITS] = q->overlimits;
  101. }
  102. g->ce_mask |= TCA_ATTR_STATS;
  103. if (tbs[TCA_STATS_APP]) {
  104. g->tc_xstats = nla_get_data(tbs[TCA_STATS_APP]);
  105. if (g->tc_xstats == NULL)
  106. return -ENOMEM;
  107. } else
  108. goto compat_xstats;
  109. } else {
  110. if (tb[TCA_STATS]) {
  111. struct tc_stats *st = nla_data(tb[TCA_STATS]);
  112. g->tc_stats[RTNL_TC_BYTES] = st->bytes;
  113. g->tc_stats[RTNL_TC_PACKETS] = st->packets;
  114. g->tc_stats[RTNL_TC_RATE_BPS] = st->bps;
  115. g->tc_stats[RTNL_TC_RATE_PPS] = st->pps;
  116. g->tc_stats[RTNL_TC_QLEN] = st->qlen;
  117. g->tc_stats[RTNL_TC_BACKLOG] = st->backlog;
  118. g->tc_stats[RTNL_TC_DROPS] = st->drops;
  119. g->tc_stats[RTNL_TC_OVERLIMITS] = st->overlimits;
  120. g->ce_mask |= TCA_ATTR_STATS;
  121. }
  122. compat_xstats:
  123. if (tb[TCA_XSTATS]) {
  124. g->tc_xstats = nla_get_data(tb[TCA_XSTATS]);
  125. if (g->tc_xstats == NULL)
  126. return -ENOMEM;
  127. g->ce_mask |= TCA_ATTR_XSTATS;
  128. }
  129. }
  130. return 0;
  131. }
  132. void tca_free_data(struct rtnl_tca *tca)
  133. {
  134. nl_data_free(tca->tc_opts);
  135. nl_data_free(tca->tc_xstats);
  136. }
  137. int tca_clone(struct rtnl_tca *dst, struct rtnl_tca *src)
  138. {
  139. if (src->tc_opts) {
  140. dst->tc_opts = nl_data_clone(src->tc_opts);
  141. if (!dst->tc_opts)
  142. goto errout;
  143. }
  144. if (src->tc_xstats) {
  145. dst->tc_xstats = nl_data_clone(src->tc_xstats);
  146. if (!dst->tc_xstats)
  147. goto errout;
  148. }
  149. return 0;
  150. errout:
  151. return nl_get_errno();
  152. }
  153. int tca_dump_brief(struct rtnl_tca *g, const char *type,
  154. struct nl_dump_params *p, int line)
  155. {
  156. char handle[32], parent[32];
  157. struct nl_cache *link_cache;
  158. link_cache = nl_cache_mngt_require("route/link");
  159. dp_dump(p, "%s %s ", g->tc_kind, type);
  160. if (link_cache) {
  161. char buf[32];
  162. dp_dump(p, "dev %s ",
  163. rtnl_link_i2name(link_cache, g->tc_ifindex,
  164. buf, sizeof(buf)));
  165. } else
  166. dp_dump(p, "dev %u ", g->tc_ifindex);
  167. dp_dump(p, "handle %s parent %s",
  168. rtnl_tc_handle2str(g->tc_handle, handle, sizeof(handle)),
  169. rtnl_tc_handle2str(g->tc_parent, parent, sizeof(parent)));
  170. return 1;
  171. }
  172. int tca_dump_full(struct rtnl_tca *g, struct nl_dump_params *p, int line)
  173. {
  174. dp_dump_line(p, line++, " ");
  175. return line;
  176. }
  177. int tca_dump_stats(struct rtnl_tca *g, struct nl_dump_params *p, int line)
  178. {
  179. char *unit, fmt[64];
  180. float res;
  181. strcpy(fmt, " %7.2f %s %10u %10u %10u %10u %10u\n");
  182. dp_dump_line(p, line++,
  183. " Stats: bytes packets drops overlimits" \
  184. " qlen backlog\n");
  185. res = nl_cancel_down_bytes(g->tc_stats[RTNL_TC_BYTES], &unit);
  186. if (*unit == 'B')
  187. fmt[11] = '9';
  188. dp_dump_line(p, line++, fmt, res, unit,
  189. g->tc_stats[RTNL_TC_PACKETS],
  190. g->tc_stats[RTNL_TC_DROPS],
  191. g->tc_stats[RTNL_TC_OVERLIMITS],
  192. g->tc_stats[RTNL_TC_QLEN],
  193. g->tc_stats[RTNL_TC_BACKLOG]);
  194. res = nl_cancel_down_bytes(g->tc_stats[RTNL_TC_RATE_BPS], &unit);
  195. strcpy(fmt, " %7.2f %s/s%9u pps");
  196. if (*unit == 'B')
  197. fmt[11] = '9';
  198. dp_dump_line(p, line++, fmt, res, unit, g->tc_stats[RTNL_TC_RATE_PPS]);
  199. return line;
  200. }
  201. int tca_compare(struct nl_object *_a, struct nl_object *_b,
  202. uint32_t attrs, int flags)
  203. {
  204. struct rtnl_tca *a = (struct rtnl_tca *) _a;
  205. struct rtnl_tca *b = (struct rtnl_tca *) _b;
  206. int diff = 0;
  207. #define TC_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, TCA_ATTR_##ATTR, a, b, EXPR)
  208. diff |= TC_DIFF(HANDLE, a->tc_handle != b->tc_handle);
  209. diff |= TC_DIFF(PARENT, a->tc_parent != b->tc_parent);
  210. diff |= TC_DIFF(IFINDEX, a->tc_ifindex != b->tc_ifindex);
  211. diff |= TC_DIFF(KIND, strcmp(a->tc_kind, b->tc_kind));
  212. #undef TC_DIFF
  213. return diff;
  214. }
  215. void tca_set_ifindex(struct rtnl_tca *t, int ifindex)
  216. {
  217. t->tc_ifindex = ifindex;
  218. t->ce_mask |= TCA_ATTR_IFINDEX;
  219. }
  220. int tca_get_ifindex(struct rtnl_tca *t)
  221. {
  222. if (t->ce_mask & TCA_ATTR_IFINDEX)
  223. return t->tc_ifindex;
  224. else
  225. return RTNL_LINK_NOT_FOUND;
  226. }
  227. void tca_set_handle(struct rtnl_tca *t, uint32_t handle)
  228. {
  229. t->tc_handle = handle;
  230. t->ce_mask |= TCA_ATTR_HANDLE;
  231. }
  232. uint32_t tca_get_handle(struct rtnl_tca *t)
  233. {
  234. if (t->ce_mask & TCA_ATTR_HANDLE)
  235. return t->tc_handle;
  236. else
  237. return 0;
  238. }
  239. void tca_set_parent(struct rtnl_tca *t, uint32_t parent)
  240. {
  241. t->tc_parent = parent;
  242. t->ce_mask |= TCA_ATTR_PARENT;
  243. }
  244. uint32_t tca_get_parent(struct rtnl_tca *t)
  245. {
  246. if (t->ce_mask & TCA_ATTR_PARENT)
  247. return t->tc_parent;
  248. else
  249. return 0;
  250. }
  251. void tca_set_kind(struct rtnl_tca *t, const char *kind)
  252. {
  253. strncpy(t->tc_kind, kind, sizeof(t->tc_kind) - 1);
  254. t->ce_mask |= TCA_ATTR_KIND;
  255. }
  256. char *tca_get_kind(struct rtnl_tca *t)
  257. {
  258. if (t->ce_mask & TCA_ATTR_KIND)
  259. return t->tc_kind;
  260. else
  261. return NULL;
  262. }
  263. uint64_t tca_get_stat(struct rtnl_tca *t, int id)
  264. {
  265. if (id < 0 || id > RTNL_TC_STATS_MAX)
  266. return 0;
  267. return t->tc_stats[id];
  268. }
  269. struct nl_msg *tca_build_msg(struct rtnl_tca *tca, int type, int flags)
  270. {
  271. struct nl_msg *msg;
  272. struct tcmsg tchdr = {
  273. .tcm_family = AF_UNSPEC,
  274. .tcm_ifindex = tca->tc_ifindex,
  275. .tcm_handle = tca->tc_handle,
  276. .tcm_parent = tca->tc_parent,
  277. };
  278. msg = nlmsg_alloc_simple(type, flags);
  279. if (!msg)
  280. goto nla_put_failure;
  281. if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0)
  282. goto nla_put_failure;
  283. if (tca->ce_mask & TCA_ATTR_KIND)
  284. NLA_PUT_STRING(msg, TCA_KIND, tca->tc_kind);
  285. return msg;
  286. nla_put_failure:
  287. nlmsg_free(msg);
  288. return NULL;
  289. }
  290. /** @endcond */
  291. /**
  292. * @name Utilities
  293. * @{
  294. */
  295. /**
  296. * Calculate time required to transmit buffer at a specific rate
  297. * @arg bufsize Size of buffer to be transmited in bytes.
  298. * @arg rate Transmit rate in bytes per second.
  299. *
  300. * Calculates the number of micro seconds required to transmit a
  301. * specific buffer at a specific transmit rate.
  302. *
  303. * @f[
  304. * txtime=\frac{bufsize}{rate}10^6
  305. * @f]
  306. *
  307. * @return Required transmit time in micro seconds.
  308. */
  309. int rtnl_tc_calc_txtime(int bufsize, int rate)
  310. {
  311. double tx_time_secs;
  312. tx_time_secs = (double) bufsize / (double) rate;
  313. return tx_time_secs * 1000000.;
  314. }
  315. /**
  316. * Calculate buffer size able to transmit in a specific time and rate.
  317. * @arg txtime Available transmit time in micro seconds.
  318. * @arg rate Transmit rate in bytes per second.
  319. *
  320. * Calculates the size of the buffer that can be transmitted in a
  321. * specific time period at a specific transmit rate.
  322. *
  323. * @f[
  324. * bufsize=\frac{{txtime} \times {rate}}{10^6}
  325. * @f]
  326. *
  327. * @return Size of buffer in bytes.
  328. */
  329. int rtnl_tc_calc_bufsize(int txtime, int rate)
  330. {
  331. double bufsize;
  332. bufsize = (double) txtime * (double) rate;
  333. return bufsize / 1000000.;
  334. }
  335. /**
  336. * Calculate the binary logarithm for a specific cell size
  337. * @arg cell_size Size of cell, must be a power of two.
  338. * @return Binary logirhtm of cell size or a negative error code.
  339. */
  340. int rtnl_tc_calc_cell_log(int cell_size)
  341. {
  342. int i;
  343. for (i = 0; i < 32; i++)
  344. if ((1 << i) == cell_size)
  345. return i;
  346. return nl_errno(EINVAL);
  347. }
  348. /** @} */
  349. /**
  350. * @name Rate Tables
  351. * @{
  352. */
  353. /**
  354. * Compute a transmission time lookup table
  355. * @arg dst Destination buffer of RTNL_TC_RTABLE_SIZE uint32_t[].
  356. * @arg mpu Minimal size of a packet at all times.
  357. * @arg overhead Overhead to be added to each packet.
  358. * @arg cell Size of cell, i.e. size of step between entries in bytes.
  359. * @arg rate Rate in bytes per second.
  360. *
  361. * Computes a table of RTNL_TC_RTABLE_SIZE entries specyfing the
  362. * transmission times for various packet sizes, e.g. the transmission
  363. * time for a packet of size \c pktsize could be looked up:
  364. * @code
  365. * txtime = table[pktsize >> log2(cell)];
  366. * @endcode
  367. */
  368. int rtnl_tc_build_rate_table(uint32_t *dst, uint8_t mpu, uint8_t overhead,
  369. int cell, int rate)
  370. {
  371. int i, size, cell_log;
  372. cell_log = rtnl_tc_calc_cell_log(cell);
  373. if (cell_log < 0)
  374. return cell_log;
  375. for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
  376. size = (i << cell_log) + overhead;
  377. if (size < mpu)
  378. size = mpu;
  379. dst[i] = rtnl_tc_calc_txtime(size, rate);
  380. }
  381. return 0;
  382. }
  383. /** @} */
  384. /**
  385. * @name Traffic Control Handle Translations
  386. * @{
  387. */
  388. /**
  389. * Convert a traffic control handle to a character string (Reentrant).
  390. * @arg handle traffic control handle
  391. * @arg buf destination buffer
  392. * @arg len buffer length
  393. *
  394. * Converts a tarffic control handle to a character string in the
  395. * form of \c MAJ:MIN and stores it in the specified destination buffer.
  396. *
  397. * @return The destination buffer or the type encoded in hexidecimal
  398. * form if no match was found.
  399. */
  400. char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
  401. {
  402. if (TC_H_ROOT == handle)
  403. snprintf(buf, len, "root");
  404. else if (TC_H_UNSPEC == handle)
  405. snprintf(buf, len, "none");
  406. else if (0 == TC_H_MAJ(handle))
  407. snprintf(buf, len, ":%02x", TC_H_MIN(handle));
  408. else if (0 == TC_H_MIN(handle))
  409. snprintf(buf, len, "%02x:", TC_H_MAJ(handle) >> 16);
  410. else
  411. snprintf(buf, len, "%02x:%02x",
  412. TC_H_MAJ(handle) >> 16, TC_H_MIN(handle));
  413. return buf;
  414. }
  415. /**
  416. * Convert a charactering strint to a traffic control handle
  417. * @arg name traffic control handle as character string
  418. * @arg res destination buffer
  419. *
  420. * Converts the provided character string specifying a traffic
  421. * control handle to the corresponding numeric value.
  422. *
  423. * The handle must be provided in one of the following formats:
  424. * - root
  425. * - none
  426. * - XXXX:
  427. * - :YYYY
  428. * - XXXX:YYYY
  429. * - XXXXYYYY
  430. *
  431. * @return 0 on success or a negative error code
  432. */
  433. int rtnl_tc_str2handle(const char *name, uint32_t *res)
  434. {
  435. char *colon, *end;
  436. uint32_t h;
  437. if (!strcasecmp(name, "root")) {
  438. *res = TC_H_ROOT;
  439. return 0;
  440. }
  441. if (!strcasecmp(name, "none")) {
  442. *res = TC_H_UNSPEC;
  443. return 0;
  444. }
  445. h = strtoul(name, &colon, 16);
  446. if (colon == name) {
  447. /* :YYYY */
  448. h = 0;
  449. if (':' != *colon)
  450. return -EINVAL;
  451. }
  452. if (':' == *colon) {
  453. /* check if we would lose bits */
  454. if (TC_H_MAJ(h))
  455. return -ERANGE;
  456. h <<= 16;
  457. if ('\0' == colon[1]) {
  458. /* XXXX: */
  459. *res = h;
  460. } else {
  461. /* XXXX:YYYY */
  462. uint32_t l = strtoul(colon+1, &end, 16);
  463. /* check if we overlap with major part */
  464. if (TC_H_MAJ(l))
  465. return -ERANGE;
  466. if ('\0' != *end)
  467. return -EINVAL;
  468. *res = (h | l);
  469. }
  470. } else if ('\0' == *colon) {
  471. /* XXXXYYYY */
  472. *res = h;
  473. } else
  474. return -EINVAL;
  475. return 0;
  476. }
  477. /** @} */
  478. /** @} */