tc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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-2011 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup rtnl
  13. * @defgroup tc Traffic Control
  14. * @{
  15. */
  16. #include <netlink-private/netlink.h>
  17. #include <netlink-private/tc.h>
  18. #include <netlink/netlink.h>
  19. #include <netlink/utils.h>
  20. #include <netlink/route/rtnl.h>
  21. #include <netlink/route/link.h>
  22. #include <netlink/route/tc.h>
  23. #include <netlink-private/route/tc-api.h>
  24. /** @cond SKIP */
  25. static struct nl_list_head tc_ops_list[__RTNL_TC_TYPE_MAX];
  26. static struct rtnl_tc_type_ops *tc_type_ops[__RTNL_TC_TYPE_MAX];
  27. static struct nla_policy tc_policy[TCA_MAX+1] = {
  28. [TCA_KIND] = { .type = NLA_STRING,
  29. .maxlen = TCKINDSIZ },
  30. [TCA_STATS] = { .minlen = sizeof(struct tc_stats) },
  31. [TCA_STATS2] = { .type = NLA_NESTED },
  32. };
  33. int tca_parse(struct nlattr **tb, int maxattr, struct rtnl_tc *g,
  34. struct nla_policy *policy)
  35. {
  36. if (g->ce_mask & TCA_ATTR_OPTS)
  37. return nla_parse(tb, maxattr,
  38. (struct nlattr *) g->tc_opts->d_data,
  39. g->tc_opts->d_size, policy);
  40. else {
  41. /* Ugly but tb[] must be in a defined state even if no
  42. * attributes can be found. */
  43. memset(tb, 0, sizeof(struct nlattr *) * (maxattr + 1));
  44. return 0;
  45. }
  46. }
  47. static struct nla_policy tc_stats2_policy[TCA_STATS_MAX+1] = {
  48. [TCA_STATS_BASIC] = { .minlen = sizeof(struct gnet_stats_basic) },
  49. [TCA_STATS_RATE_EST] = { .minlen = sizeof(struct gnet_stats_rate_est) },
  50. [TCA_STATS_QUEUE] = { .minlen = sizeof(struct gnet_stats_queue) },
  51. };
  52. int rtnl_tc_msg_parse(struct nlmsghdr *n, struct rtnl_tc *tc)
  53. {
  54. struct nl_cache *link_cache;
  55. struct rtnl_tc_ops *ops;
  56. struct nlattr *tb[TCA_MAX + 1];
  57. char kind[TCKINDSIZ];
  58. struct tcmsg *tm;
  59. int err;
  60. tc->ce_msgtype = n->nlmsg_type;
  61. err = nlmsg_parse(n, sizeof(*tm), tb, TCA_MAX, tc_policy);
  62. if (err < 0)
  63. return err;
  64. if (tb[TCA_KIND] == NULL)
  65. return -NLE_MISSING_ATTR;
  66. nla_strlcpy(kind, tb[TCA_KIND], sizeof(kind));
  67. rtnl_tc_set_kind(tc, kind);
  68. tm = nlmsg_data(n);
  69. tc->tc_family = tm->tcm_family;
  70. tc->tc_ifindex = tm->tcm_ifindex;
  71. tc->tc_handle = tm->tcm_handle;
  72. tc->tc_parent = tm->tcm_parent;
  73. tc->tc_info = tm->tcm_info;
  74. tc->ce_mask |= (TCA_ATTR_FAMILY | TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE|
  75. TCA_ATTR_PARENT | TCA_ATTR_INFO);
  76. if (tb[TCA_OPTIONS]) {
  77. tc->tc_opts = nl_data_alloc_attr(tb[TCA_OPTIONS]);
  78. if (!tc->tc_opts)
  79. return -NLE_NOMEM;
  80. tc->ce_mask |= TCA_ATTR_OPTS;
  81. }
  82. if (tb[TCA_STATS2]) {
  83. struct nlattr *tbs[TCA_STATS_MAX + 1];
  84. err = nla_parse_nested(tbs, TCA_STATS_MAX, tb[TCA_STATS2],
  85. tc_stats2_policy);
  86. if (err < 0)
  87. return err;
  88. if (tbs[TCA_STATS_BASIC]) {
  89. struct gnet_stats_basic *bs;
  90. bs = nla_data(tbs[TCA_STATS_BASIC]);
  91. tc->tc_stats[RTNL_TC_BYTES] = bs->bytes;
  92. tc->tc_stats[RTNL_TC_PACKETS] = bs->packets;
  93. }
  94. if (tbs[TCA_STATS_RATE_EST]) {
  95. struct gnet_stats_rate_est *re;
  96. re = nla_data(tbs[TCA_STATS_RATE_EST]);
  97. tc->tc_stats[RTNL_TC_RATE_BPS] = re->bps;
  98. tc->tc_stats[RTNL_TC_RATE_PPS] = re->pps;
  99. }
  100. if (tbs[TCA_STATS_QUEUE]) {
  101. struct gnet_stats_queue *q;
  102. q = nla_data(tbs[TCA_STATS_QUEUE]);
  103. tc->tc_stats[RTNL_TC_QLEN] = q->qlen;
  104. tc->tc_stats[RTNL_TC_BACKLOG] = q->backlog;
  105. tc->tc_stats[RTNL_TC_DROPS] = q->drops;
  106. tc->tc_stats[RTNL_TC_REQUEUES] = q->requeues;
  107. tc->tc_stats[RTNL_TC_OVERLIMITS] = q->overlimits;
  108. }
  109. tc->ce_mask |= TCA_ATTR_STATS;
  110. if (tbs[TCA_STATS_APP]) {
  111. tc->tc_xstats = nl_data_alloc_attr(tbs[TCA_STATS_APP]);
  112. if (tc->tc_xstats == NULL)
  113. return -NLE_NOMEM;
  114. } else
  115. goto compat_xstats;
  116. } else {
  117. if (tb[TCA_STATS]) {
  118. struct tc_stats *st = nla_data(tb[TCA_STATS]);
  119. tc->tc_stats[RTNL_TC_BYTES] = st->bytes;
  120. tc->tc_stats[RTNL_TC_PACKETS] = st->packets;
  121. tc->tc_stats[RTNL_TC_RATE_BPS] = st->bps;
  122. tc->tc_stats[RTNL_TC_RATE_PPS] = st->pps;
  123. tc->tc_stats[RTNL_TC_QLEN] = st->qlen;
  124. tc->tc_stats[RTNL_TC_BACKLOG] = st->backlog;
  125. tc->tc_stats[RTNL_TC_DROPS] = st->drops;
  126. tc->tc_stats[RTNL_TC_OVERLIMITS]= st->overlimits;
  127. tc->ce_mask |= TCA_ATTR_STATS;
  128. }
  129. compat_xstats:
  130. if (tb[TCA_XSTATS]) {
  131. tc->tc_xstats = nl_data_alloc_attr(tb[TCA_XSTATS]);
  132. if (tc->tc_xstats == NULL)
  133. return -NLE_NOMEM;
  134. tc->ce_mask |= TCA_ATTR_XSTATS;
  135. }
  136. }
  137. ops = rtnl_tc_get_ops(tc);
  138. if (ops && ops->to_msg_parser) {
  139. void *data = rtnl_tc_data(tc);
  140. if (!data)
  141. return -NLE_NOMEM;
  142. err = ops->to_msg_parser(tc, data);
  143. if (err < 0)
  144. return err;
  145. }
  146. if ((link_cache = __nl_cache_mngt_require("route/link"))) {
  147. struct rtnl_link *link;
  148. if ((link = rtnl_link_get(link_cache, tc->tc_ifindex))) {
  149. rtnl_tc_set_link(tc, link);
  150. /* rtnl_tc_set_link incs refcnt */
  151. rtnl_link_put(link);
  152. }
  153. }
  154. return 0;
  155. }
  156. int rtnl_tc_msg_build(struct rtnl_tc *tc, int type, int flags,
  157. struct nl_msg **result)
  158. {
  159. struct nl_msg *msg;
  160. struct rtnl_tc_ops *ops;
  161. struct tcmsg tchdr = {
  162. .tcm_family = AF_UNSPEC,
  163. .tcm_ifindex = tc->tc_ifindex,
  164. .tcm_handle = tc->tc_handle,
  165. .tcm_parent = tc->tc_parent,
  166. };
  167. int err = -NLE_MSGSIZE;
  168. msg = nlmsg_alloc_simple(type, flags);
  169. if (!msg)
  170. return -NLE_NOMEM;
  171. if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0)
  172. goto nla_put_failure;
  173. if (tc->ce_mask & TCA_ATTR_KIND)
  174. NLA_PUT_STRING(msg, TCA_KIND, tc->tc_kind);
  175. ops = rtnl_tc_get_ops(tc);
  176. if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
  177. struct nlattr *opts;
  178. void *data = rtnl_tc_data(tc);
  179. if (ops->to_msg_fill) {
  180. if (!(opts = nla_nest_start(msg, TCA_OPTIONS)))
  181. goto nla_put_failure;
  182. if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
  183. goto nla_put_failure;
  184. nla_nest_end(msg, opts);
  185. } else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
  186. goto nla_put_failure;
  187. }
  188. *result = msg;
  189. return 0;
  190. nla_put_failure:
  191. nlmsg_free(msg);
  192. return err;
  193. }
  194. void tca_set_kind(struct rtnl_tc *t, const char *kind)
  195. {
  196. strncpy(t->tc_kind, kind, sizeof(t->tc_kind) - 1);
  197. t->ce_mask |= TCA_ATTR_KIND;
  198. }
  199. /** @endcond */
  200. /**
  201. * @name Attributes
  202. * @{
  203. */
  204. /**
  205. * Set interface index of traffic control object
  206. * @arg tc traffic control object
  207. * @arg ifindex interface index.
  208. *
  209. * Sets the interface index of a traffic control object. The interface
  210. * index defines the network device which this tc object is attached to.
  211. * This function will overwrite any network device assigned with previous
  212. * calls to rtnl_tc_set_ifindex() or rtnl_tc_set_link().
  213. */
  214. void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex)
  215. {
  216. /* Obsolete possible old link reference */
  217. rtnl_link_put(tc->tc_link);
  218. tc->tc_link = NULL;
  219. tc->ce_mask &= ~TCA_ATTR_LINK;
  220. tc->tc_ifindex = ifindex;
  221. tc->ce_mask |= TCA_ATTR_IFINDEX;
  222. }
  223. /**
  224. * Return interface index of traffic control object
  225. * @arg tc traffic control object
  226. */
  227. int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
  228. {
  229. return tc->tc_ifindex;
  230. }
  231. /**
  232. * Set link of traffic control object
  233. * @arg tc traffic control object
  234. * @arg link link object
  235. *
  236. * Sets the link of a traffic control object. This function serves
  237. * the same purpose as rtnl_tc_set_ifindex() but due to the continued
  238. * allowed access to the link object it gives it the possibility to
  239. * retrieve sane default values for the the MTU and the linktype.
  240. * Always prefer this function over rtnl_tc_set_ifindex() if you can
  241. * spare to have an additional link object around.
  242. */
  243. void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link)
  244. {
  245. rtnl_link_put(tc->tc_link);
  246. if (!link)
  247. return;
  248. if (!link->l_index)
  249. BUG();
  250. nl_object_get(OBJ_CAST(link));
  251. tc->tc_link = link;
  252. tc->tc_ifindex = link->l_index;
  253. tc->ce_mask |= TCA_ATTR_LINK | TCA_ATTR_IFINDEX;
  254. }
  255. /**
  256. * Get link of traffic control object
  257. * @arg tc traffic control object
  258. *
  259. * Returns the link of a traffic control object. The link is only
  260. * returned if it has been set before via rtnl_tc_set_link() or
  261. * if a link cache was available while parsing the tc object. This
  262. * function may still return NULL even if an ifindex is assigned to
  263. * the tc object. It will _not_ look up the link by itself.
  264. *
  265. * @note The returned link will have its reference counter incremented.
  266. * It is in the responsibility of the caller to return the
  267. * reference.
  268. *
  269. * @return link object or NULL if not set.
  270. */
  271. struct rtnl_link *rtnl_tc_get_link(struct rtnl_tc *tc)
  272. {
  273. if (tc->tc_link) {
  274. nl_object_get(OBJ_CAST(tc->tc_link));
  275. return tc->tc_link;
  276. }
  277. return NULL;
  278. }
  279. /**
  280. * Set the Maximum Transmission Unit (MTU) of traffic control object
  281. * @arg tc traffic control object
  282. * @arg mtu largest packet size expected
  283. *
  284. * Sets the MTU of a traffic control object. Not all traffic control
  285. * objects will make use of this but it helps while calculating rate
  286. * tables. This value is typically derived directly from the link
  287. * the tc object is attached to if the link has been assigned via
  288. * rtnl_tc_set_link(). It is usually not necessary to set the MTU
  289. * manually, this function is provided to allow overwriting the derived
  290. * value.
  291. */
  292. void rtnl_tc_set_mtu(struct rtnl_tc *tc, uint32_t mtu)
  293. {
  294. tc->tc_mtu = mtu;
  295. tc->ce_mask |= TCA_ATTR_MTU;
  296. }
  297. /**
  298. * Return the MTU of traffic control object
  299. * @arg tc traffic control object
  300. *
  301. * Returns the MTU of a traffic control object which has been set via:
  302. * -# User specified value set via rtnl_tc_set_mtu()
  303. * -# Dervied from link set via rtnl_tc_set_link()
  304. * -# Fall back to default: ethernet = 1500
  305. */
  306. uint32_t rtnl_tc_get_mtu(struct rtnl_tc *tc)
  307. {
  308. if (tc->ce_mask & TCA_ATTR_MTU)
  309. return tc->tc_mtu;
  310. else if (tc->ce_mask & TCA_ATTR_LINK)
  311. return tc->tc_link->l_mtu;
  312. else
  313. return 1500; /* default to ethernet */
  314. }
  315. /**
  316. * Set the Minimum Packet Unit (MPU) of a traffic control object
  317. * @arg tc traffic control object
  318. * @arg mpu minimum packet size expected
  319. *
  320. * Sets the MPU of a traffic contorl object. It specifies the minimum
  321. * packet size to ever hit this traffic control object. Not all traffic
  322. * control objects will make use of this but it helps while calculating
  323. * rate tables.
  324. */
  325. void rtnl_tc_set_mpu(struct rtnl_tc *tc, uint32_t mpu)
  326. {
  327. tc->tc_mpu = mpu;
  328. tc->ce_mask |= TCA_ATTR_MPU;
  329. }
  330. /**
  331. * Return the Minimum Packet Unit (MPU) of a traffic control object
  332. * @arg tc traffic control object
  333. *
  334. * @return The MPU previously set via rtnl_tc_set_mpu() or 0.
  335. */
  336. uint32_t rtnl_tc_get_mpu(struct rtnl_tc *tc)
  337. {
  338. return tc->tc_mpu;
  339. }
  340. /**
  341. * Set per packet overhead of a traffic control object
  342. * @arg tc traffic control object
  343. * @arg overhead overhead per packet in bytes
  344. *
  345. * Sets the per packet overhead in bytes occuring on the link not seen
  346. * by the kernel. This value can be used to correct size calculations
  347. * if the packet size on the wire does not match the packet sizes seen
  348. * in the network stack. Not all traffic control objects will make use
  349. * this but it helps while calculating accurate packet sizes in the
  350. * kernel.
  351. */
  352. void rtnl_tc_set_overhead(struct rtnl_tc *tc, uint32_t overhead)
  353. {
  354. tc->tc_overhead = overhead;
  355. tc->ce_mask |= TCA_ATTR_OVERHEAD;
  356. }
  357. /**
  358. * Return per packet overhead of a traffic control object
  359. * @arg tc traffic control object
  360. *
  361. * @return The overhead previously set by rtnl_tc_set_overhead() or 0.
  362. */
  363. uint32_t rtnl_tc_get_overhead(struct rtnl_tc *tc)
  364. {
  365. return tc->tc_overhead;
  366. }
  367. /**
  368. * Set the linktype of a traffic control object
  369. * @arg tc traffic control object
  370. * @arg type type of link (e.g. ARPHRD_ATM, ARPHRD_ETHER)
  371. *
  372. * Overwrites the type of link this traffic control object is attached to.
  373. * This value is typically derived from the link this tc object is attached
  374. * if the link has been assigned via rtnl_tc_set_link(). It is usually not
  375. * necessary to set the linktype manually. This function is provided to
  376. * allow overwriting the linktype.
  377. */
  378. void rtnl_tc_set_linktype(struct rtnl_tc *tc, uint32_t type)
  379. {
  380. tc->tc_linktype = type;
  381. tc->ce_mask |= TCA_ATTR_LINKTYPE;
  382. }
  383. /**
  384. * Return the linktype of a traffic control object
  385. * @arg tc traffic control object
  386. *
  387. * Returns the linktype of the link the traffic control object is attached to:
  388. * -# User specified value via rtnl_tc_set_linktype()
  389. * -# Value derived from link set via rtnl_tc_set_link()
  390. * -# Default fall-back: ARPHRD_ETHER
  391. */
  392. uint32_t rtnl_tc_get_linktype(struct rtnl_tc *tc)
  393. {
  394. if (tc->ce_mask & TCA_ATTR_LINKTYPE)
  395. return tc->tc_linktype;
  396. else if (tc->ce_mask & TCA_ATTR_LINK)
  397. return tc->tc_link->l_arptype;
  398. else
  399. return ARPHRD_ETHER; /* default to ethernet */
  400. }
  401. /**
  402. * Set identifier of traffic control object
  403. * @arg tc traffic control object
  404. * @arg id unique identifier
  405. */
  406. void rtnl_tc_set_handle(struct rtnl_tc *tc, uint32_t id)
  407. {
  408. tc->tc_handle = id;
  409. tc->ce_mask |= TCA_ATTR_HANDLE;
  410. }
  411. /**
  412. * Return identifier of a traffic control object
  413. * @arg tc traffic control object
  414. */
  415. uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc)
  416. {
  417. return tc->tc_handle;
  418. }
  419. /**
  420. * Set the parent identifier of a traffic control object
  421. * @arg tc traffic control object
  422. * @arg parent identifier of parent traffif control object
  423. *
  424. */
  425. void rtnl_tc_set_parent(struct rtnl_tc *tc, uint32_t parent)
  426. {
  427. tc->tc_parent = parent;
  428. tc->ce_mask |= TCA_ATTR_PARENT;
  429. }
  430. /**
  431. * Return parent identifier of a traffic control object
  432. * @arg tc traffic control object
  433. */
  434. uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
  435. {
  436. return tc->tc_parent;
  437. }
  438. /**
  439. * Define the type of traffic control object
  440. * @arg tc traffic control object
  441. * @arg kind name of the tc object type
  442. *
  443. * @return 0 on success or a negative error code
  444. */
  445. int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
  446. {
  447. if (tc->ce_mask & TCA_ATTR_KIND)
  448. return -NLE_EXIST;
  449. strncpy(tc->tc_kind, kind, sizeof(tc->tc_kind) - 1);
  450. tc->ce_mask |= TCA_ATTR_KIND;
  451. /* Force allocation of data */
  452. rtnl_tc_data(tc);
  453. return 0;
  454. }
  455. /**
  456. * Return kind of traffic control object
  457. * @arg tc traffic control object
  458. *
  459. * @return Kind of traffic control object or NULL if not set.
  460. */
  461. char *rtnl_tc_get_kind(struct rtnl_tc *tc)
  462. {
  463. if (tc->ce_mask & TCA_ATTR_KIND)
  464. return tc->tc_kind;
  465. else
  466. return NULL;
  467. }
  468. /**
  469. * Return value of a statistical counter of a traffic control object
  470. * @arg tc traffic control object
  471. * @arg id identifier of statistical counter
  472. *
  473. * @return Value of requested statistic counter or 0.
  474. */
  475. uint64_t rtnl_tc_get_stat(struct rtnl_tc *tc, enum rtnl_tc_stat id)
  476. {
  477. if (id < 0 || id > RTNL_TC_STATS_MAX)
  478. return 0;
  479. return tc->tc_stats[id];
  480. }
  481. /** @} */
  482. /**
  483. * @name Utilities
  484. * @{
  485. */
  486. /**
  487. * Calculate time required to transmit buffer at a specific rate
  488. * @arg bufsize Size of buffer to be transmited in bytes.
  489. * @arg rate Transmit rate in bytes per second.
  490. *
  491. * Calculates the number of micro seconds required to transmit a
  492. * specific buffer at a specific transmit rate.
  493. *
  494. * @f[
  495. * txtime=\frac{bufsize}{rate}10^6
  496. * @f]
  497. *
  498. * @return Required transmit time in micro seconds.
  499. */
  500. int rtnl_tc_calc_txtime(int bufsize, int rate)
  501. {
  502. double tx_time_secs;
  503. tx_time_secs = (double) bufsize / (double) rate;
  504. return tx_time_secs * 1000000.;
  505. }
  506. /**
  507. * Calculate buffer size able to transmit in a specific time and rate.
  508. * @arg txtime Available transmit time in micro seconds.
  509. * @arg rate Transmit rate in bytes per second.
  510. *
  511. * Calculates the size of the buffer that can be transmitted in a
  512. * specific time period at a specific transmit rate.
  513. *
  514. * @f[
  515. * bufsize=\frac{{txtime} \times {rate}}{10^6}
  516. * @f]
  517. *
  518. * @return Size of buffer in bytes.
  519. */
  520. int rtnl_tc_calc_bufsize(int txtime, int rate)
  521. {
  522. double bufsize;
  523. bufsize = (double) txtime * (double) rate;
  524. return bufsize / 1000000.;
  525. }
  526. /**
  527. * Calculate the binary logarithm for a specific cell size
  528. * @arg cell_size Size of cell, must be a power of two.
  529. * @return Binary logirhtm of cell size or a negative error code.
  530. */
  531. int rtnl_tc_calc_cell_log(int cell_size)
  532. {
  533. int i;
  534. for (i = 0; i < 32; i++)
  535. if ((1 << i) == cell_size)
  536. return i;
  537. return -NLE_INVAL;
  538. }
  539. /** @} */
  540. /**
  541. * @name Rate Tables
  542. * @{
  543. */
  544. /*
  545. * COPYRIGHT NOTE:
  546. * align_to_atm() and adjust_size() derived/coped from iproute2 source.
  547. */
  548. /*
  549. * The align to ATM cells is used for determining the (ATM) SAR
  550. * alignment overhead at the ATM layer. (SAR = Segmentation And
  551. * Reassembly). This is for example needed when scheduling packet on
  552. * an ADSL connection. Note that the extra ATM-AAL overhead is _not_
  553. * included in this calculation. This overhead is added in the kernel
  554. * before doing the rate table lookup, as this gives better precision
  555. * (as the table will always be aligned for 48 bytes).
  556. * --Hawk, d.7/11-2004. <hawk@diku.dk>
  557. */
  558. static unsigned int align_to_atm(unsigned int size)
  559. {
  560. int linksize, cells;
  561. cells = size / ATM_CELL_PAYLOAD;
  562. if ((size % ATM_CELL_PAYLOAD) > 0)
  563. cells++;
  564. linksize = cells * ATM_CELL_SIZE; /* Use full cell size to add ATM tax */
  565. return linksize;
  566. }
  567. static unsigned int adjust_size(unsigned int size, unsigned int mpu,
  568. uint32_t linktype)
  569. {
  570. if (size < mpu)
  571. size = mpu;
  572. switch (linktype) {
  573. case ARPHRD_ATM:
  574. return align_to_atm(size);
  575. case ARPHRD_ETHER:
  576. default:
  577. return size;
  578. }
  579. }
  580. /**
  581. * Compute a transmission time lookup table
  582. * @arg tc traffic control object
  583. * @arg spec Rate specification
  584. * @arg dst Destination buffer of RTNL_TC_RTABLE_SIZE uint32_t[].
  585. *
  586. * Computes a table of RTNL_TC_RTABLE_SIZE entries specyfing the
  587. * transmission times for various packet sizes, e.g. the transmission
  588. * time for a packet of size \c pktsize could be looked up:
  589. * @code
  590. * txtime = table[pktsize >> log2(mtu)];
  591. * @endcode
  592. */
  593. int rtnl_tc_build_rate_table(struct rtnl_tc *tc, struct rtnl_ratespec *spec,
  594. uint32_t *dst)
  595. {
  596. uint32_t mtu = rtnl_tc_get_mtu(tc);
  597. uint32_t linktype = rtnl_tc_get_linktype(tc);
  598. uint8_t cell_log = spec->rs_cell_log;
  599. unsigned int size, i;
  600. spec->rs_mpu = rtnl_tc_get_mpu(tc);
  601. spec->rs_overhead = rtnl_tc_get_overhead(tc);
  602. if (mtu == 0)
  603. mtu = 2047;
  604. if (cell_log == UINT8_MAX) {
  605. /*
  606. * cell_log not specified, calculate it. It has to specify the
  607. * minimum number of rshifts required to break the MTU to below
  608. * RTNL_TC_RTABLE_SIZE.
  609. */
  610. cell_log = 0;
  611. while ((mtu >> cell_log) >= RTNL_TC_RTABLE_SIZE)
  612. cell_log++;
  613. }
  614. for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
  615. size = adjust_size((i + 1) << cell_log, spec->rs_mpu, linktype);
  616. dst[i] = nl_us2ticks(rtnl_tc_calc_txtime(size, spec->rs_rate));
  617. }
  618. spec->rs_cell_align = -1;
  619. spec->rs_cell_log = cell_log;
  620. return 0;
  621. }
  622. /** @} */
  623. /**
  624. * @name TC implementation of cache functions
  625. */
  626. void rtnl_tc_free_data(struct nl_object *obj)
  627. {
  628. struct rtnl_tc *tc = TC_CAST(obj);
  629. struct rtnl_tc_ops *ops;
  630. rtnl_link_put(tc->tc_link);
  631. nl_data_free(tc->tc_opts);
  632. nl_data_free(tc->tc_xstats);
  633. if (tc->tc_subdata) {
  634. ops = rtnl_tc_get_ops(tc);
  635. if (ops && ops->to_free_data)
  636. ops->to_free_data(tc, nl_data_get(tc->tc_subdata));
  637. nl_data_free(tc->tc_subdata);
  638. }
  639. }
  640. int rtnl_tc_clone(struct nl_object *dstobj, struct nl_object *srcobj)
  641. {
  642. struct rtnl_tc *dst = TC_CAST(dstobj);
  643. struct rtnl_tc *src = TC_CAST(srcobj);
  644. struct rtnl_tc_ops *ops;
  645. if (src->tc_link) {
  646. nl_object_get(OBJ_CAST(src->tc_link));
  647. dst->tc_link = src->tc_link;
  648. }
  649. if (src->tc_opts) {
  650. dst->tc_opts = nl_data_clone(src->tc_opts);
  651. if (!dst->tc_opts)
  652. return -NLE_NOMEM;
  653. }
  654. if (src->tc_xstats) {
  655. dst->tc_xstats = nl_data_clone(src->tc_xstats);
  656. if (!dst->tc_xstats)
  657. return -NLE_NOMEM;
  658. }
  659. if (src->tc_subdata) {
  660. if (!(dst->tc_subdata = nl_data_clone(src->tc_subdata))) {
  661. return -NLE_NOMEM;
  662. }
  663. }
  664. ops = rtnl_tc_get_ops(src);
  665. if (ops && ops->to_clone) {
  666. void *a = rtnl_tc_data(dst), *b = rtnl_tc_data(src);
  667. if (!a)
  668. return 0;
  669. else if (!b)
  670. return -NLE_NOMEM;
  671. return ops->to_clone(a, b);
  672. }
  673. return 0;
  674. }
  675. static int tc_dump(struct rtnl_tc *tc, enum nl_dump_type type,
  676. struct nl_dump_params *p)
  677. {
  678. struct rtnl_tc_type_ops *type_ops;
  679. struct rtnl_tc_ops *ops;
  680. void *data = rtnl_tc_data(tc);
  681. type_ops = tc_type_ops[tc->tc_type];
  682. if (type_ops && type_ops->tt_dump[type])
  683. type_ops->tt_dump[type](tc, p);
  684. ops = rtnl_tc_get_ops(tc);
  685. if (ops && ops->to_dump[type]) {
  686. ops->to_dump[type](tc, data, p);
  687. return 1;
  688. }
  689. return 0;
  690. }
  691. void rtnl_tc_dump_line(struct nl_object *obj, struct nl_dump_params *p)
  692. {
  693. struct rtnl_tc_type_ops *type_ops;
  694. struct rtnl_tc *tc = TC_CAST(obj);
  695. struct nl_cache *link_cache;
  696. char buf[32];
  697. nl_new_line(p);
  698. type_ops = tc_type_ops[tc->tc_type];
  699. if (type_ops && type_ops->tt_dump_prefix)
  700. nl_dump(p, "%s ", type_ops->tt_dump_prefix);
  701. nl_dump(p, "%s ", tc->tc_kind);
  702. if ((link_cache = nl_cache_mngt_require_safe("route/link"))) {
  703. nl_dump(p, "dev %s ",
  704. rtnl_link_i2name(link_cache, tc->tc_ifindex,
  705. buf, sizeof(buf)));
  706. } else
  707. nl_dump(p, "dev %u ", tc->tc_ifindex);
  708. nl_dump(p, "id %s ",
  709. rtnl_tc_handle2str(tc->tc_handle, buf, sizeof(buf)));
  710. nl_dump(p, "parent %s",
  711. rtnl_tc_handle2str(tc->tc_parent, buf, sizeof(buf)));
  712. tc_dump(tc, NL_DUMP_LINE, p);
  713. nl_dump(p, "\n");
  714. if (link_cache)
  715. nl_cache_put(link_cache);
  716. }
  717. void rtnl_tc_dump_details(struct nl_object *obj, struct nl_dump_params *p)
  718. {
  719. struct rtnl_tc *tc = TC_CAST(obj);
  720. rtnl_tc_dump_line(OBJ_CAST(tc), p);
  721. nl_dump_line(p, " ");
  722. if (tc->ce_mask & TCA_ATTR_MTU)
  723. nl_dump(p, " mtu %u", tc->tc_mtu);
  724. if (tc->ce_mask & TCA_ATTR_MPU)
  725. nl_dump(p, " mpu %u", tc->tc_mpu);
  726. if (tc->ce_mask & TCA_ATTR_OVERHEAD)
  727. nl_dump(p, " overhead %u", tc->tc_overhead);
  728. if (!tc_dump(tc, NL_DUMP_DETAILS, p))
  729. nl_dump(p, "no options");
  730. nl_dump(p, "\n");
  731. }
  732. void rtnl_tc_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
  733. {
  734. struct rtnl_tc *tc = TC_CAST(obj);
  735. char *unit, fmt[64];
  736. float res;
  737. rtnl_tc_dump_details(OBJ_CAST(tc), p);
  738. strcpy(fmt, " %7.2f %s %10u %10u %10u %10u %10u\n");
  739. nl_dump_line(p,
  740. " Stats: bytes packets drops overlimits" \
  741. " qlen backlog\n");
  742. res = nl_cancel_down_bytes(tc->tc_stats[RTNL_TC_BYTES], &unit);
  743. if (*unit == 'B')
  744. fmt[11] = '9';
  745. nl_dump_line(p, fmt, res, unit,
  746. tc->tc_stats[RTNL_TC_PACKETS],
  747. tc->tc_stats[RTNL_TC_DROPS],
  748. tc->tc_stats[RTNL_TC_OVERLIMITS],
  749. tc->tc_stats[RTNL_TC_QLEN],
  750. tc->tc_stats[RTNL_TC_BACKLOG]);
  751. res = nl_cancel_down_bytes(tc->tc_stats[RTNL_TC_RATE_BPS], &unit);
  752. strcpy(fmt, " %7.2f %s/s%9u pps");
  753. if (*unit == 'B')
  754. fmt[11] = '9';
  755. nl_dump_line(p, fmt, res, unit, tc->tc_stats[RTNL_TC_RATE_PPS]);
  756. tc_dump(tc, NL_DUMP_LINE, p);
  757. nl_dump(p, "\n");
  758. }
  759. int rtnl_tc_compare(struct nl_object *aobj, struct nl_object *bobj,
  760. uint32_t attrs, int flags)
  761. {
  762. struct rtnl_tc *a = TC_CAST(aobj);
  763. struct rtnl_tc *b = TC_CAST(bobj);
  764. int diff = 0;
  765. #define TC_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, TCA_ATTR_##ATTR, a, b, EXPR)
  766. diff |= TC_DIFF(HANDLE, a->tc_handle != b->tc_handle);
  767. diff |= TC_DIFF(PARENT, a->tc_parent != b->tc_parent);
  768. diff |= TC_DIFF(IFINDEX, a->tc_ifindex != b->tc_ifindex);
  769. diff |= TC_DIFF(KIND, strcmp(a->tc_kind, b->tc_kind));
  770. #undef TC_DIFF
  771. return diff;
  772. }
  773. /** @} */
  774. /**
  775. * @name Modules API
  776. */
  777. struct rtnl_tc_ops *rtnl_tc_lookup_ops(enum rtnl_tc_type type, const char *kind)
  778. {
  779. struct rtnl_tc_ops *ops;
  780. nl_list_for_each_entry(ops, &tc_ops_list[type], to_list)
  781. if (!strcmp(kind, ops->to_kind))
  782. return ops;
  783. return NULL;
  784. }
  785. struct rtnl_tc_ops *rtnl_tc_get_ops(struct rtnl_tc *tc)
  786. {
  787. if (!tc->tc_ops)
  788. tc->tc_ops = rtnl_tc_lookup_ops(tc->tc_type, tc->tc_kind);
  789. return tc->tc_ops;
  790. }
  791. /**
  792. * Register a traffic control module
  793. * @arg ops traffic control module operations
  794. */
  795. int rtnl_tc_register(struct rtnl_tc_ops *ops)
  796. {
  797. static int init = 0;
  798. /*
  799. * Initialiation hack, make sure list is initialized when
  800. * the first tc module registers. Putting this in a
  801. * separate __init would required correct ordering of init
  802. * functions
  803. */
  804. if (!init) {
  805. int i;
  806. for (i = 0; i < __RTNL_TC_TYPE_MAX; i++)
  807. nl_init_list_head(&tc_ops_list[i]);
  808. init = 1;
  809. }
  810. if (!ops->to_kind || ops->to_type > RTNL_TC_TYPE_MAX)
  811. BUG();
  812. if (rtnl_tc_lookup_ops(ops->to_type, ops->to_kind))
  813. return -NLE_EXIST;
  814. nl_list_add_tail(&ops->to_list, &tc_ops_list[ops->to_type]);
  815. return 0;
  816. }
  817. /**
  818. * Unregister a traffic control module
  819. * @arg ops traffic control module operations
  820. */
  821. void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
  822. {
  823. nl_list_del(&ops->to_list);
  824. }
  825. /**
  826. * Return pointer to private data of traffic control object
  827. * @arg tc traffic control object
  828. *
  829. * Allocates the private traffic control object data section
  830. * as necessary and returns it.
  831. *
  832. * @return Pointer to private tc data or NULL if allocation failed.
  833. */
  834. void *rtnl_tc_data(struct rtnl_tc *tc)
  835. {
  836. if (!tc->tc_subdata) {
  837. size_t size;
  838. if (!tc->tc_ops) {
  839. if (!tc->tc_kind)
  840. BUG();
  841. if (!rtnl_tc_get_ops(tc))
  842. return NULL;
  843. }
  844. if (!(size = tc->tc_ops->to_size))
  845. BUG();
  846. if (!(tc->tc_subdata = nl_data_alloc(NULL, size)))
  847. return NULL;
  848. }
  849. return nl_data_get(tc->tc_subdata);
  850. }
  851. /**
  852. * Check traffic control object type and return private data section
  853. * @arg tc traffic control object
  854. * @arg ops expected traffic control object operations
  855. *
  856. * Checks whether the traffic control object matches the type
  857. * specified with the traffic control object operations. If the
  858. * type matches, the private tc object data is returned. If type
  859. * mismatches, APPBUG() will print a application bug warning.
  860. *
  861. * @see rtnl_tc_data()
  862. *
  863. * @return Pointer to private tc data or NULL if type mismatches.
  864. */
  865. void *rtnl_tc_data_check(struct rtnl_tc *tc, struct rtnl_tc_ops *ops)
  866. {
  867. if (tc->tc_ops != ops) {
  868. char buf[64];
  869. snprintf(buf, sizeof(buf),
  870. "tc object %p used in %s context but is of type %s",
  871. tc, ops->to_kind, tc->tc_ops->to_kind);
  872. APPBUG(buf);
  873. return NULL;
  874. }
  875. return rtnl_tc_data(tc);
  876. }
  877. struct nl_af_group tc_groups[] = {
  878. { AF_UNSPEC, RTNLGRP_TC },
  879. { END_OF_GROUP_LIST },
  880. };
  881. void rtnl_tc_type_register(struct rtnl_tc_type_ops *ops)
  882. {
  883. if (ops->tt_type > RTNL_TC_TYPE_MAX)
  884. BUG();
  885. tc_type_ops[ops->tt_type] = ops;
  886. }
  887. void rtnl_tc_type_unregister(struct rtnl_tc_type_ops *ops)
  888. {
  889. if (ops->tt_type > RTNL_TC_TYPE_MAX)
  890. BUG();
  891. tc_type_ops[ops->tt_type] = NULL;
  892. }
  893. /** @} */
  894. /** @} */