class.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * lib/route/class.c Queueing 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-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup tc
  13. * @defgroup class Queueing Classes
  14. * @{
  15. */
  16. #include <netlink-local.h>
  17. #include <netlink-tc.h>
  18. #include <netlink/netlink.h>
  19. #include <netlink/route/tc.h>
  20. #include <netlink/route/class.h>
  21. #include <netlink/route/class-modules.h>
  22. #include <netlink/route/qdisc.h>
  23. #include <netlink/route/classifier.h>
  24. #include <netlink/utils.h>
  25. static struct nl_cache_ops rtnl_class_ops;
  26. static int class_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  27. struct nlmsghdr *n, struct nl_parser_param *pp)
  28. {
  29. int err;
  30. struct rtnl_class *class;
  31. struct rtnl_class_ops *cops;
  32. class = rtnl_class_alloc();
  33. if (!class) {
  34. err = nl_errno(ENOMEM);
  35. goto errout;
  36. }
  37. class->ce_msgtype = n->nlmsg_type;
  38. err = tca_msg_parser(n, (struct rtnl_tca *) class);
  39. if (err < 0)
  40. goto errout_free;
  41. cops = rtnl_class_lookup_ops(class);
  42. if (cops && cops->co_msg_parser) {
  43. err = cops->co_msg_parser(class);
  44. if (err < 0)
  45. goto errout_free;
  46. }
  47. err = pp->pp_cb((struct nl_object *) class, pp);
  48. if (err < 0)
  49. goto errout_free;
  50. err = P_ACCEPT;
  51. errout_free:
  52. rtnl_class_put(class);
  53. errout:
  54. return err;
  55. }
  56. static int class_request_update(struct nl_cache *cache,
  57. struct nl_handle *handle)
  58. {
  59. struct tcmsg tchdr = {
  60. .tcm_family = AF_UNSPEC,
  61. .tcm_ifindex = cache->c_iarg1,
  62. };
  63. return nl_send_simple(handle, RTM_GETTCLASS, NLM_F_DUMP, &tchdr,
  64. sizeof(tchdr));
  65. }
  66. /**
  67. * @name Addition/Modification
  68. * @{
  69. */
  70. static struct nl_msg *class_build(struct rtnl_class *class, int type, int flags)
  71. {
  72. struct rtnl_class_ops *cops;
  73. struct nl_msg *msg;
  74. int err;
  75. msg = tca_build_msg((struct rtnl_tca *) class, type, flags);
  76. if (!msg)
  77. goto errout;
  78. cops = rtnl_class_lookup_ops(class);
  79. if (cops && cops->co_get_opts) {
  80. struct nl_msg *opts;
  81. opts = cops->co_get_opts(class);
  82. if (opts) {
  83. err = nla_put_nested(msg, TCA_OPTIONS, opts);
  84. nlmsg_free(opts);
  85. if (err < 0)
  86. goto errout;
  87. }
  88. }
  89. return msg;
  90. errout:
  91. nlmsg_free(msg);
  92. return NULL;
  93. }
  94. /**
  95. * Build a netlink message to add a new class
  96. * @arg class class to add
  97. * @arg flags additional netlink message flags
  98. *
  99. * Builds a new netlink message requesting an addition of a class.
  100. * The netlink message header isn't fully equipped with all relevant
  101. * fields and must be sent out via nl_send_auto_complete() or
  102. * supplemented as needed.
  103. *
  104. * Common message flags
  105. * - NLM_F_REPLACE - replace possibly existing classes
  106. *
  107. * @return New netlink message
  108. */
  109. struct nl_msg *rtnl_class_build_add_request(struct rtnl_class *class, int flags)
  110. {
  111. return class_build(class, RTM_NEWTCLASS, NLM_F_CREATE | flags);
  112. }
  113. /**
  114. * Add a new class
  115. * @arg handle netlink handle
  116. * @arg class class to delete
  117. * @arg flags additional netlink message flags
  118. *
  119. * Builds a netlink message by calling rtnl_qdisc_build_add_request(),
  120. * sends the request to the kernel and waits for the next ACK to be
  121. * received and thus blocks until the request has been processed.
  122. *
  123. * Common message flags
  124. * - NLM_F_REPLACE - replace possibly existing classes
  125. *
  126. * @return 0 on success or a negative error code
  127. */
  128. int rtnl_class_add(struct nl_handle *handle, struct rtnl_class *class,
  129. int flags)
  130. {
  131. struct nl_msg *msg;
  132. int err;
  133. msg = rtnl_class_build_add_request(class, flags);
  134. if (!msg)
  135. return nl_errno(ENOMEM);
  136. err = nl_send_auto_complete(handle, msg);
  137. nlmsg_free(msg);
  138. if (err < 0)
  139. return err;
  140. return nl_wait_for_ack(handle);
  141. }
  142. /** @} */
  143. /**
  144. * @name Cache Management
  145. * @{
  146. */
  147. /**
  148. * Build a class cache including all classes attached to the specified interface
  149. * @arg handle netlink handle
  150. * @arg ifindex interface index of the link the classes are
  151. * attached to.
  152. *
  153. * Allocates a new cache, initializes it properly and updates it to
  154. * include all classes attached to the specified interface.
  155. *
  156. * @return The cache or NULL if an error has occured.
  157. */
  158. struct nl_cache * rtnl_class_alloc_cache(struct nl_handle *handle, int ifindex)
  159. {
  160. struct nl_cache * cache;
  161. cache = nl_cache_alloc(&rtnl_class_ops);
  162. if (!cache)
  163. return NULL;
  164. cache->c_iarg1 = ifindex;
  165. if (handle && nl_cache_refill(handle, cache) < 0) {
  166. nl_cache_free(cache);
  167. return NULL;
  168. }
  169. return cache;
  170. }
  171. /** @} */
  172. static struct nl_cache_ops rtnl_class_ops = {
  173. .co_name = "route/class",
  174. .co_hdrsize = sizeof(struct tcmsg),
  175. .co_msgtypes = {
  176. { RTM_NEWTCLASS, NL_ACT_NEW, "new" },
  177. { RTM_DELTCLASS, NL_ACT_DEL, "del" },
  178. { RTM_GETTCLASS, NL_ACT_GET, "get" },
  179. END_OF_MSGTYPES_LIST,
  180. },
  181. .co_protocol = NETLINK_ROUTE,
  182. .co_request_update = &class_request_update,
  183. .co_msg_parser = &class_msg_parser,
  184. .co_obj_ops = &class_obj_ops,
  185. };
  186. static void __init class_init(void)
  187. {
  188. nl_cache_mngt_register(&rtnl_class_ops);
  189. }
  190. static void __exit class_exit(void)
  191. {
  192. nl_cache_mngt_unregister(&rtnl_class_ops);
  193. }
  194. /** @} */