qdisc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * lib/route/qdisc.c Queueing Disciplines
  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 tc
  13. * @defgroup qdisc Queueing Disciplines
  14. *
  15. * @par Qdisc Handles
  16. * In general, qdiscs are identified by the major part of a traffic control
  17. * handle (the upper 16 bits). A few special values exist though:
  18. * - \c TC_H_ROOT: root qdisc (directly attached to the device)
  19. * - \c TC_H_INGRESS: ingress qdisc (directly attached to the device)
  20. * - \c TC_H_UNSPEC: unspecified qdisc (no reference)
  21. *
  22. * @par 1) Adding a Qdisc
  23. * @code
  24. * // Allocate a new empty qdisc to be filled out
  25. * struct rtnl_qdisc *qdisc = rtnl_qdisc_alloc();
  26. *
  27. * // ... specify the kind of the Qdisc
  28. * rtnl_qdisc_set_kind(qdisc, "pfifo");
  29. *
  30. * // Specify the device the qdisc should be attached to
  31. * rtnl_qdisc_set_ifindex(qdisc, ifindex);
  32. *
  33. * // ... specify the parent qdisc
  34. * rtnl_qdisc_set_parent(qdisc, TC_H_ROOT);
  35. *
  36. * // Specifying the handle is not required but makes reidentifying easier
  37. * // and may help to avoid adding a qdisc twice.
  38. * rtnl_qdisc_set_handle(qdisc, 0x000A0000);
  39. *
  40. * // Now on to specify the qdisc specific options, see the relevant qdisc
  41. * // modules for documentation, in this example we set the upper limit of
  42. * // the packet fifo qdisc to 64
  43. * rtnl_qdisc_fifo_set_limit(qdisc, 64);
  44. *
  45. * rtnl_qdisc_add(handle, qdisc, NLM_R_REPLACE);
  46. *
  47. * // Free up the memory
  48. * rtnl_qdisc_put(qdisc);
  49. * @endcode
  50. *
  51. * @par 2) Deleting a Qdisc
  52. * @code
  53. * // Allocate a new empty qdisc to be filled out with the parameters
  54. * // specifying the qdisc to be deleted. Alternatively a fully equiped
  55. * // Qdisc object from a cache can be used.
  56. * struct rtnl_qdisc *qdisc = rtnl_qdisc_alloc();
  57. *
  58. * // The interface index of the device the qdisc is on and the parent handle
  59. * // are the least required fields to be filled out.
  60. * // Note: Specify TC_H_ROOT or TC_H_INGRESS as parent handle to delete the
  61. * // root respectively root ingress qdisc.
  62. * rtnl_qdisc_set_ifindex(qdisc, ifindex);
  63. * rtnl_qdisc_set_parent(qdisc, parent_handle);
  64. *
  65. * // If required for identification, the handle can be specified as well.
  66. * rtnl_qdisc_set_handle(qdisc, qdisc_handle);
  67. *
  68. * // Not required but maybe helpful as sanity check, the kind of the qdisc
  69. * // can be specified to avoid mistakes.
  70. * rtnl_qdisc_set_kind(qdisc, "pfifo");
  71. *
  72. * // Finally delete the qdisc with rtnl_qdisc_delete(), alternatively
  73. * // rtnl_qdisc_build_delete_request() can be invoked to generate an
  74. * // appropritate netlink message to send out.
  75. * rtnl_qdisc_delete(handle, qdisc);
  76. *
  77. * // Free up the memory
  78. * rtnl_qdisc_put(qdisc);
  79. * @endcode
  80. *
  81. * @{
  82. */
  83. #include <netlink-local.h>
  84. #include <netlink-tc.h>
  85. #include <netlink/netlink.h>
  86. #include <netlink/utils.h>
  87. #include <netlink/route/link.h>
  88. #include <netlink/route/tc.h>
  89. #include <netlink/route/qdisc.h>
  90. #include <netlink/route/class.h>
  91. #include <netlink/route/classifier.h>
  92. #include <netlink/route/qdisc-modules.h>
  93. static struct nl_cache_ops rtnl_qdisc_ops;
  94. static int qdisc_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  95. struct nlmsghdr *n, struct nl_parser_param *pp)
  96. {
  97. int err = -ENOMEM;
  98. struct rtnl_qdisc *qdisc;
  99. struct rtnl_qdisc_ops *qops;
  100. qdisc = rtnl_qdisc_alloc();
  101. if (!qdisc) {
  102. err = nl_errno(ENOMEM);
  103. goto errout;
  104. }
  105. qdisc->ce_msgtype = n->nlmsg_type;
  106. err = tca_msg_parser(n, (struct rtnl_tca *) qdisc);
  107. if (err < 0)
  108. goto errout_free;
  109. qops = rtnl_qdisc_lookup_ops(qdisc);
  110. if (qops && qops->qo_msg_parser) {
  111. err = qops->qo_msg_parser(qdisc);
  112. if (err < 0)
  113. goto errout_free;
  114. }
  115. err = pp->pp_cb((struct nl_object *) qdisc, pp);
  116. if (err < 0)
  117. goto errout_free;
  118. err = P_ACCEPT;
  119. errout_free:
  120. rtnl_qdisc_put(qdisc);
  121. errout:
  122. return err;
  123. }
  124. static int qdisc_request_update(struct nl_cache *c, struct nl_handle *h)
  125. {
  126. struct tcmsg tchdr = {
  127. .tcm_family = AF_UNSPEC,
  128. .tcm_ifindex = c->c_iarg1,
  129. };
  130. return nl_send_simple(h, RTM_GETQDISC, NLM_F_DUMP, &tchdr,
  131. sizeof(tchdr));
  132. }
  133. /**
  134. * @name QDisc Addition
  135. * @{
  136. */
  137. static struct nl_msg *qdisc_build(struct rtnl_qdisc *qdisc, int type, int flags)
  138. {
  139. struct rtnl_qdisc_ops *qops;
  140. struct nl_msg *msg;
  141. int err;
  142. msg = tca_build_msg((struct rtnl_tca *) qdisc, type, flags);
  143. if (!msg)
  144. goto errout;
  145. qops = rtnl_qdisc_lookup_ops(qdisc);
  146. if (qops && qops->qo_get_opts) {
  147. struct nl_msg *opts;
  148. opts = qops->qo_get_opts(qdisc);
  149. if (opts) {
  150. err = nla_put_nested(msg, TCA_OPTIONS, opts);
  151. nlmsg_free(opts);
  152. if (err < 0)
  153. goto errout;
  154. }
  155. }
  156. return msg;
  157. errout:
  158. nlmsg_free(msg);
  159. return NULL;
  160. }
  161. /**
  162. * Build a netlink message to add a new qdisc
  163. * @arg qdisc qdisc to add
  164. * @arg flags additional netlink message flags
  165. *
  166. * Builds a new netlink message requesting an addition of a qdisc.
  167. * The netlink message header isn't fully equipped with all relevant
  168. * fields and must be sent out via nl_send_auto_complete() or
  169. * supplemented as needed.
  170. *
  171. * Common message flags used:
  172. * - NLM_F_REPLACE - replace a potential existing qdisc
  173. *
  174. * @return New netlink message
  175. */
  176. struct nl_msg *rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc,
  177. int flags)
  178. {
  179. struct nl_msg *msg;
  180. msg = qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_CREATE | flags);
  181. if (!msg)
  182. nl_errno(ENOMEM);
  183. return msg;
  184. }
  185. /**
  186. * Add a new qdisc
  187. * @arg handle netlink handle
  188. * @arg qdisc qdisc to delete
  189. * @arg flags additional netlink message flags
  190. *
  191. * Builds a netlink message by calling rtnl_qdisc_build_add_request(),
  192. * sends the request to the kernel and waits for the ACK to be
  193. * received and thus blocks until the request has been processed.
  194. *
  195. * Common message flags used:
  196. * - NLM_F_REPLACE - replace a potential existing qdisc
  197. *
  198. * @return 0 on success or a negative error code
  199. */
  200. int rtnl_qdisc_add(struct nl_handle *handle, struct rtnl_qdisc *qdisc,
  201. int flags)
  202. {
  203. struct nl_msg *msg;
  204. int err;
  205. msg = rtnl_qdisc_build_add_request(qdisc, flags);
  206. if (!msg)
  207. return nl_errno(ENOMEM);
  208. err = nl_send_auto_complete(handle, msg);
  209. nlmsg_free(msg);
  210. if (err < 0)
  211. return err;
  212. return nl_wait_for_ack(handle);
  213. }
  214. /** @} */
  215. /**
  216. * @name QDisc Modification
  217. * @{
  218. */
  219. /**
  220. * Build a netlink message to change attributes of a existing qdisc
  221. * @arg qdisc qdisc to change
  222. * @arg new new qdisc attributes
  223. *
  224. * Builds a new netlink message requesting an change of qdisc
  225. * attributes. The netlink message header isn't fully equipped
  226. * with all relevant fields and must be sent out via
  227. * nl_send_auto_complete() or supplemented as needed.
  228. *
  229. * @return New netlink message
  230. */
  231. struct nl_msg *rtnl_qdisc_build_change_request(struct rtnl_qdisc *qdisc,
  232. struct rtnl_qdisc *new)
  233. {
  234. return qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_REPLACE);
  235. }
  236. /**
  237. * Change attributes of a qdisc
  238. * @arg handle netlink handle
  239. * @arg qdisc qdisc to change
  240. * @arg new new qdisc attributes
  241. *
  242. * Builds a netlink message by calling rtnl_qdisc_build_change_request(),
  243. * sends the request to the kernel and waits for the ACK to be
  244. * received and thus blocks until the request has been processed.
  245. *
  246. * @return 0 on success or a negative error code
  247. */
  248. int rtnl_qdisc_change(struct nl_handle *handle, struct rtnl_qdisc *qdisc,
  249. struct rtnl_qdisc *new)
  250. {
  251. struct nl_msg *msg;
  252. int err;
  253. msg = rtnl_qdisc_build_change_request(qdisc, new);
  254. if (!msg)
  255. return nl_errno(ENOMEM);
  256. err = nl_send_auto_complete(handle, msg);
  257. nlmsg_free(msg);
  258. if (err < 0)
  259. return err;
  260. return nl_wait_for_ack(handle);
  261. }
  262. /** @} */
  263. /**
  264. * @name QDisc Deletion
  265. * @{
  266. */
  267. /**
  268. * Build a netlink request message to delete a qdisc
  269. * @arg qdisc qdisc to delete
  270. *
  271. * Builds a new netlink message requesting a deletion of a qdisc.
  272. * The netlink message header isn't fully equipped with all relevant
  273. * fields and must thus be sent out via nl_send_auto_complete()
  274. * or supplemented as needed.
  275. *
  276. * @return New netlink message
  277. */
  278. struct nl_msg *rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc)
  279. {
  280. struct nl_msg *msg;
  281. struct tcmsg tchdr;
  282. int required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
  283. if ((qdisc->ce_mask & required) != required)
  284. BUG();
  285. msg = nlmsg_alloc_simple(RTM_DELQDISC, 0);
  286. if (!msg)
  287. return NULL;
  288. tchdr.tcm_family = AF_UNSPEC,
  289. tchdr.tcm_handle = qdisc->q_handle,
  290. tchdr.tcm_parent = qdisc->q_parent,
  291. tchdr.tcm_ifindex = qdisc->q_ifindex,
  292. nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO);
  293. return msg;
  294. }
  295. /**
  296. * Delete a qdisc
  297. * @arg handle netlink handle
  298. * @arg qdisc qdisc to delete
  299. *
  300. * Builds a netlink message by calling rtnl_qdisc_build_delete_request(),
  301. * sends the request to the kernel and waits for the ACK to be
  302. * received and thus blocks until the request has been processed.
  303. *
  304. * @return 0 on success or a negative error code
  305. */
  306. int rtnl_qdisc_delete(struct nl_handle *handle, struct rtnl_qdisc *qdisc)
  307. {
  308. struct nl_msg *msg;
  309. int err;
  310. msg = rtnl_qdisc_build_delete_request(qdisc);
  311. if (!msg)
  312. return nl_errno(ENOMEM);
  313. err = nl_send_auto_complete(handle, msg);
  314. nlmsg_free(msg);
  315. if (err < 0)
  316. return err;
  317. return nl_wait_for_ack(handle);
  318. }
  319. /** @} */
  320. /**
  321. * @name Qdisc Cache Management
  322. * @{
  323. */
  324. /**
  325. * Build a qdisc cache including all qdiscs currently configured in
  326. * the kernel
  327. * @arg handle netlink handle
  328. *
  329. * Allocates a new cache, initializes it properly and updates it to
  330. * include all qdiscs currently configured in the kernel.
  331. *
  332. * @note The caller is responsible for destroying and freeing the
  333. * cache after using it.
  334. * @return The cache or NULL if an error has occured.
  335. */
  336. struct nl_cache * rtnl_qdisc_alloc_cache(struct nl_handle *handle)
  337. {
  338. struct nl_cache * cache;
  339. cache = nl_cache_alloc(&rtnl_qdisc_ops);
  340. if (cache == NULL)
  341. return NULL;
  342. if (handle && nl_cache_refill(handle, cache) < 0) {
  343. nl_cache_free(cache);
  344. return NULL;
  345. }
  346. return cache;
  347. }
  348. /**
  349. * Look up qdisc by its parent in the provided cache
  350. * @arg cache qdisc cache
  351. * @arg ifindex interface the qdisc is attached to
  352. * @arg parent parent handle
  353. * @return pointer to qdisc inside the cache or NULL if no match was found.
  354. */
  355. struct rtnl_qdisc * rtnl_qdisc_get_by_parent(struct nl_cache *cache,
  356. int ifindex, uint32_t parent)
  357. {
  358. struct rtnl_qdisc *q;
  359. if (cache->c_ops != &rtnl_qdisc_ops)
  360. return NULL;
  361. nl_list_for_each_entry(q, &cache->c_items, ce_list) {
  362. if (q->q_parent == parent && q->q_ifindex == ifindex) {
  363. nl_object_get((struct nl_object *) q);
  364. return q;
  365. }
  366. }
  367. return NULL;
  368. }
  369. /**
  370. * Look up qdisc by its handle in the provided cache
  371. * @arg cache qdisc cache
  372. * @arg ifindex interface the qdisc is attached to
  373. * @arg handle qdisc handle
  374. * @return pointer to qdisc inside the cache or NULL if no match was found.
  375. */
  376. struct rtnl_qdisc * rtnl_qdisc_get(struct nl_cache *cache,
  377. int ifindex, uint32_t handle)
  378. {
  379. struct rtnl_qdisc *q;
  380. if (cache->c_ops != &rtnl_qdisc_ops)
  381. return NULL;
  382. nl_list_for_each_entry(q, &cache->c_items, ce_list) {
  383. if (q->q_handle == handle && q->q_ifindex == ifindex) {
  384. nl_object_get((struct nl_object *) q);
  385. return q;
  386. }
  387. }
  388. return NULL;
  389. }
  390. /** @} */
  391. static struct nl_cache_ops rtnl_qdisc_ops = {
  392. .co_name = "route/qdisc",
  393. .co_hdrsize = sizeof(struct tcmsg),
  394. .co_msgtypes = {
  395. { RTM_NEWQDISC, NL_ACT_NEW, "new" },
  396. { RTM_DELQDISC, NL_ACT_DEL, "del" },
  397. { RTM_GETQDISC, NL_ACT_GET, "get" },
  398. END_OF_MSGTYPES_LIST,
  399. },
  400. .co_protocol = NETLINK_ROUTE,
  401. .co_request_update = qdisc_request_update,
  402. .co_msg_parser = qdisc_msg_parser,
  403. .co_obj_ops = &qdisc_obj_ops,
  404. };
  405. static void __init qdisc_init(void)
  406. {
  407. nl_cache_mngt_register(&rtnl_qdisc_ops);
  408. }
  409. static void __exit qdisc_exit(void)
  410. {
  411. nl_cache_mngt_unregister(&rtnl_qdisc_ops);
  412. }
  413. /** @} */