class.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * lib/route/class.c Traffic Classes
  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-2013 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup tc
  13. * @defgroup class Traffic Classes
  14. * @{
  15. */
  16. #include <netlink-private/netlink.h>
  17. #include <netlink-private/tc.h>
  18. #include <netlink/netlink.h>
  19. #include <netlink-private/route/tc-api.h>
  20. #include <netlink/route/class.h>
  21. #include <netlink/route/qdisc.h>
  22. #include <netlink/route/classifier.h>
  23. #include <netlink/utils.h>
  24. static struct nl_cache_ops rtnl_class_ops;
  25. static struct nl_object_ops class_obj_ops;
  26. static void class_dump_details(struct rtnl_tc *tc, struct nl_dump_params *p)
  27. {
  28. struct rtnl_class *class = (struct rtnl_class *) tc;
  29. char buf[32];
  30. if (class->c_info)
  31. nl_dump(p, "child-qdisc %s ",
  32. rtnl_tc_handle2str(class->c_info, buf, sizeof(buf)));
  33. }
  34. static int class_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  35. struct nlmsghdr *nlh, struct nl_parser_param *pp)
  36. {
  37. struct rtnl_class *class;
  38. int err;
  39. if (!(class = rtnl_class_alloc()))
  40. return -NLE_NOMEM;
  41. if ((err = rtnl_tc_msg_parse(nlh, TC_CAST(class))) < 0)
  42. goto errout;
  43. err = pp->pp_cb(OBJ_CAST(class), pp);
  44. errout:
  45. rtnl_class_put(class);
  46. return err;
  47. }
  48. static int class_request_update(struct nl_cache *cache, struct nl_sock *sk)
  49. {
  50. struct tcmsg tchdr = {
  51. .tcm_family = AF_UNSPEC,
  52. .tcm_ifindex = cache->c_iarg1,
  53. };
  54. return nl_send_simple(sk, RTM_GETTCLASS, NLM_F_DUMP, &tchdr,
  55. sizeof(tchdr));
  56. }
  57. /**
  58. * @name Allocation/Freeing
  59. * @{
  60. */
  61. struct rtnl_class *rtnl_class_alloc(void)
  62. {
  63. struct rtnl_tc *tc;
  64. tc = TC_CAST(nl_object_alloc(&class_obj_ops));
  65. if (tc)
  66. tc->tc_type = RTNL_TC_TYPE_CLASS;
  67. return (struct rtnl_class *) tc;
  68. }
  69. void rtnl_class_put(struct rtnl_class *class)
  70. {
  71. nl_object_put((struct nl_object *) class);
  72. }
  73. /** @} */
  74. /**
  75. * @name Addition/Modification/Deletion
  76. * @{
  77. */
  78. static int class_build(struct rtnl_class *class, int type, int flags,
  79. struct nl_msg **result)
  80. {
  81. uint32_t needed = TCA_ATTR_PARENT | TCA_ATTR_HANDLE;
  82. if ((class->ce_mask & needed) == needed &&
  83. TC_H_MAJ(class->c_parent) && TC_H_MAJ(class->c_handle) &&
  84. TC_H_MAJ(class->c_parent) != TC_H_MAJ(class->c_handle)) {
  85. APPBUG("TC_H_MAJ(parent) must match TC_H_MAJ(handle)");
  86. return -NLE_INVAL;
  87. }
  88. return rtnl_tc_msg_build(TC_CAST(class), type, flags, result);
  89. }
  90. /**
  91. * Build a netlink message requesting the addition of a traffic class
  92. * @arg class Traffic class to add
  93. * @arg flags Additional netlink message flags
  94. * @arg result Pointer to store resulting netlink message
  95. *
  96. * The behaviour of this function is identical to rtnl_class_add() with
  97. * the exception that it will not send the message but return it int the
  98. * provided return pointer instead.
  99. *
  100. * @see rtnl_class_add()
  101. *
  102. * @return 0 on success or a negative error code.
  103. */
  104. int rtnl_class_build_add_request(struct rtnl_class *class, int flags,
  105. struct nl_msg **result)
  106. {
  107. return class_build(class, RTM_NEWTCLASS, flags, result);
  108. }
  109. /**
  110. * Add/Update traffic class
  111. * @arg sk Netlink socket
  112. * @arg class Traffic class to add
  113. * @arg flags Additional netlink message flags
  114. *
  115. * Builds a \c RTM_NEWTCLASS netlink message requesting the addition
  116. * of a new traffic class and sends the message to the kernel. The
  117. * configuration of the traffic class is derived from the attributes
  118. * of the specified traffic class.
  119. *
  120. * The following flags may be specified:
  121. * - \c NLM_F_CREATE: Create traffic class if it does not exist,
  122. * otherwise -NLE_OBJ_NOTFOUND is returned.
  123. * - \c NLM_F_EXCL: Return -NLE_EXISTS if a traffic class with
  124. * matching handle exists already.
  125. *
  126. * Existing traffic classes with matching handles will be updated,
  127. * unless the flag \c NLM_F_EXCL is specified. If no matching traffic
  128. * class exists, it will be created if the flag \c NLM_F_CREATE is set,
  129. * otherwise the error -NLE_OBJ_NOTFOUND is returned.
  130. *
  131. * If the parent qdisc does not support classes, the error
  132. * \c NLE_OPNOTSUPP is returned.
  133. *
  134. * After sending, the function will wait for the ACK or an eventual
  135. * error message to be received and will therefore block until the
  136. * operation has been completed.
  137. *
  138. * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
  139. * this function to return immediately after sending. In this case,
  140. * it is the responsibility of the caller to handle any error
  141. * messages returned.
  142. *
  143. * @return 0 on success or a negative error code.
  144. */
  145. int rtnl_class_add(struct nl_sock *sk, struct rtnl_class *class, int flags)
  146. {
  147. struct nl_msg *msg;
  148. int err;
  149. if ((err = rtnl_class_build_add_request(class, flags, &msg)) < 0)
  150. return err;
  151. return nl_send_sync(sk, msg);
  152. }
  153. /**
  154. * Build netlink message requesting the deletion of a traffic class
  155. * @arg class Traffic class to delete
  156. * @arg result Pointer to store resulting netlink message
  157. *
  158. * The behaviour of this function is identical to rtnl_class_delete() with
  159. * the exception that it will not send the message but return it in the
  160. * provided return pointer instead.
  161. *
  162. * @see rtnl_class_delete()
  163. *
  164. * @return 0 on success or a negative error code.
  165. */
  166. int rtnl_class_build_delete_request(struct rtnl_class *class, struct nl_msg **result)
  167. {
  168. struct nl_msg *msg;
  169. struct tcmsg tchdr;
  170. uint32_t required = TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE;
  171. if ((class->ce_mask & required) != required) {
  172. APPBUG("ifindex and handle must be specified");
  173. return -NLE_MISSING_ATTR;
  174. }
  175. if (!(msg = nlmsg_alloc_simple(RTM_DELTCLASS, 0)))
  176. return -NLE_NOMEM;
  177. memset(&tchdr, 0, sizeof(tchdr));
  178. tchdr.tcm_family = AF_UNSPEC;
  179. tchdr.tcm_ifindex = class->c_ifindex;
  180. tchdr.tcm_handle = class->c_handle;
  181. if (class->ce_mask & TCA_ATTR_PARENT)
  182. tchdr.tcm_parent = class->c_parent;
  183. if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0) {
  184. nlmsg_free(msg);
  185. return -NLE_MSGSIZE;
  186. }
  187. *result = msg;
  188. return 0;
  189. }
  190. /**
  191. * Delete traffic class
  192. * @arg sk Netlink socket
  193. * @arg class Traffic class to delete
  194. *
  195. * Builds a \c RTM_DELTCLASS netlink message requesting the deletion
  196. * of a traffic class and sends the message to the kernel.
  197. *
  198. * The message is constructed out of the following attributes:
  199. * - \c ifindex and \c handle (required)
  200. * - \c parent (optional, must match if provided)
  201. *
  202. * All other class attributes including all class type specific
  203. * attributes are ignored.
  204. *
  205. * After sending, the function will wait for the ACK or an eventual
  206. * error message to be received and will therefore block until the
  207. * operation has been completed.
  208. *
  209. * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
  210. * this function to return immediately after sending. In this case,
  211. * it is the responsibility of the caller to handle any error
  212. * messages returned.
  213. *
  214. * @return 0 on success or a negative error code.
  215. */
  216. int rtnl_class_delete(struct nl_sock *sk, struct rtnl_class *class)
  217. {
  218. struct nl_msg *msg;
  219. int err;
  220. if ((err = rtnl_class_build_delete_request(class, &msg)) < 0)
  221. return err;
  222. return nl_send_sync(sk, msg);
  223. }
  224. /** @} */
  225. /**
  226. * @name Leaf Qdisc
  227. * @{
  228. */
  229. /**
  230. * Lookup the leaf qdisc of a traffic class
  231. * @arg class the parent traffic class
  232. * @arg cache a qdisc cache allocated using rtnl_qdisc_alloc_cache()
  233. *
  234. * @return Matching Qdisc or NULL if the traffic class has no leaf qdisc
  235. */
  236. struct rtnl_qdisc *rtnl_class_leaf_qdisc(struct rtnl_class *class,
  237. struct nl_cache *cache)
  238. {
  239. struct rtnl_qdisc *leaf;
  240. if (!class->c_info)
  241. return NULL;
  242. leaf = rtnl_qdisc_get_by_parent(cache, class->c_ifindex,
  243. class->c_handle);
  244. if (!leaf || leaf->q_handle != class->c_info)
  245. return NULL;
  246. return leaf;
  247. }
  248. /** @} */
  249. /**
  250. * @name Cache Related Functions
  251. * @{
  252. */
  253. /**
  254. * Allocate a cache and fill it with all configured traffic classes
  255. * @arg sk Netlink socket
  256. * @arg ifindex Interface index of the network device
  257. * @arg result Pointer to store the created cache
  258. *
  259. * Allocates a new traffic class cache and fills it with a list of all
  260. * configured traffic classes on a specific network device. Release the
  261. * cache with nl_cache_free().
  262. *
  263. * @return 0 on success or a negative error code.
  264. */
  265. int rtnl_class_alloc_cache(struct nl_sock *sk, int ifindex,
  266. struct nl_cache **result)
  267. {
  268. struct nl_cache * cache;
  269. int err;
  270. if (!ifindex) {
  271. APPBUG("ifindex must be specified");
  272. return -NLE_INVAL;
  273. }
  274. if (!(cache = nl_cache_alloc(&rtnl_class_ops)))
  275. return -NLE_NOMEM;
  276. cache->c_iarg1 = ifindex;
  277. if (sk && (err = nl_cache_refill(sk, cache)) < 0) {
  278. nl_cache_free(cache);
  279. return err;
  280. }
  281. *result = cache;
  282. return 0;
  283. }
  284. /**
  285. * Search traffic class by interface index and handle
  286. * @arg cache Traffic class cache
  287. * @arg ifindex Interface index
  288. * @arg handle ID of traffic class
  289. *
  290. * Searches a traffic class cache previously allocated with
  291. * rtnl_class_alloc_cache() and searches for a traffi class matching
  292. * the interface index and handle.
  293. *
  294. * The reference counter is incremented before returning the traffic
  295. * class, therefore the reference must be given back with rtnl_class_put()
  296. * after usage.
  297. *
  298. * @return Traffic class or NULL if no match was found.
  299. */
  300. struct rtnl_class *rtnl_class_get(struct nl_cache *cache, int ifindex,
  301. uint32_t handle)
  302. {
  303. struct rtnl_class *class;
  304. if (cache->c_ops != &rtnl_class_ops)
  305. return NULL;
  306. nl_list_for_each_entry(class, &cache->c_items, ce_list) {
  307. if (class->c_handle == handle && class->c_ifindex == ifindex) {
  308. nl_object_get((struct nl_object *) class);
  309. return class;
  310. }
  311. }
  312. return NULL;
  313. }
  314. /** @} */
  315. /**
  316. * @name Deprecated Functions
  317. * @{
  318. */
  319. /**
  320. * Call a callback for each child of a class
  321. *
  322. * @deprecated Use of this function is deprecated, it does not allow
  323. * to handle the out of memory situation that can occur.
  324. */
  325. void rtnl_class_foreach_child(struct rtnl_class *class, struct nl_cache *cache,
  326. void (*cb)(struct nl_object *, void *), void *arg)
  327. {
  328. struct rtnl_class *filter;
  329. filter = rtnl_class_alloc();
  330. if (!filter)
  331. return;
  332. rtnl_tc_set_parent(TC_CAST(filter), class->c_handle);
  333. rtnl_tc_set_ifindex(TC_CAST(filter), class->c_ifindex);
  334. rtnl_tc_set_kind(TC_CAST(filter), class->c_kind);
  335. nl_cache_foreach_filter(cache, OBJ_CAST(filter), cb, arg);
  336. rtnl_class_put(filter);
  337. }
  338. /**
  339. * Call a callback for each classifier attached to the class
  340. *
  341. * @deprecated Use of this function is deprecated, it does not allow
  342. * to handle the out of memory situation that can occur.
  343. */
  344. void rtnl_class_foreach_cls(struct rtnl_class *class, struct nl_cache *cache,
  345. void (*cb)(struct nl_object *, void *), void *arg)
  346. {
  347. struct rtnl_cls *filter;
  348. filter = rtnl_cls_alloc();
  349. if (!filter)
  350. return;
  351. rtnl_tc_set_ifindex((struct rtnl_tc *) filter, class->c_ifindex);
  352. rtnl_tc_set_parent((struct rtnl_tc *) filter, class->c_parent);
  353. nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
  354. rtnl_cls_put(filter);
  355. }
  356. /** @} */
  357. static struct rtnl_tc_type_ops class_ops = {
  358. .tt_type = RTNL_TC_TYPE_CLASS,
  359. .tt_dump_prefix = "class",
  360. .tt_dump = {
  361. [NL_DUMP_DETAILS] = class_dump_details,
  362. },
  363. };
  364. static struct nl_object_ops class_obj_ops = {
  365. .oo_name = "route/class",
  366. .oo_size = sizeof(struct rtnl_class),
  367. .oo_free_data = rtnl_tc_free_data,
  368. .oo_clone = rtnl_tc_clone,
  369. .oo_dump = {
  370. [NL_DUMP_LINE] = rtnl_tc_dump_line,
  371. [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
  372. [NL_DUMP_STATS] = rtnl_tc_dump_stats,
  373. },
  374. .oo_compare = rtnl_tc_compare,
  375. .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
  376. };
  377. static struct nl_cache_ops rtnl_class_ops = {
  378. .co_name = "route/class",
  379. .co_hdrsize = sizeof(struct tcmsg),
  380. .co_msgtypes = {
  381. { RTM_NEWTCLASS, NL_ACT_NEW, "new" },
  382. { RTM_DELTCLASS, NL_ACT_DEL, "del" },
  383. { RTM_GETTCLASS, NL_ACT_GET, "get" },
  384. END_OF_MSGTYPES_LIST,
  385. },
  386. .co_protocol = NETLINK_ROUTE,
  387. .co_groups = tc_groups,
  388. .co_request_update = &class_request_update,
  389. .co_msg_parser = &class_msg_parser,
  390. .co_obj_ops = &class_obj_ops,
  391. };
  392. static void __init class_init(void)
  393. {
  394. rtnl_tc_type_register(&class_ops);
  395. nl_cache_mngt_register(&rtnl_class_ops);
  396. }
  397. static void __exit class_exit(void)
  398. {
  399. nl_cache_mngt_unregister(&rtnl_class_ops);
  400. rtnl_tc_type_unregister(&class_ops);
  401. }
  402. /** @} */