qdisc_obj.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * lib/route/qdisc_obj.c Queueing Discipline Object
  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 qdisc
  13. * @defgroup qdisc_obj Queueing Discipline Object
  14. * @{
  15. */
  16. #include <netlink-local.h>
  17. #include <netlink-tc.h>
  18. #include <netlink/netlink.h>
  19. #include <netlink/utils.h>
  20. #include <netlink/route/link.h>
  21. #include <netlink/route/tc.h>
  22. #include <netlink/route/qdisc.h>
  23. #include <netlink/route/class.h>
  24. #include <netlink/route/classifier.h>
  25. #include <netlink/route/qdisc-modules.h>
  26. static void qdisc_free_data(struct nl_object *obj)
  27. {
  28. struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
  29. struct rtnl_qdisc_ops *qops;
  30. tca_free_data((struct rtnl_tca *) qdisc);
  31. qops = rtnl_qdisc_lookup_ops(qdisc);
  32. if (qops && qops->qo_free_data)
  33. qops->qo_free_data(qdisc);
  34. }
  35. static int qdisc_clone(struct nl_object *_dst, struct nl_object *_src)
  36. {
  37. struct rtnl_qdisc *dst = (struct rtnl_qdisc *) _dst;
  38. struct rtnl_qdisc *src = (struct rtnl_qdisc *) _src;
  39. struct rtnl_qdisc_ops *qops;
  40. int err;
  41. err = tca_clone((struct rtnl_tca *) dst, (struct rtnl_tca *) src);
  42. if (err < 0)
  43. goto errout;
  44. qops = rtnl_qdisc_lookup_ops(src);
  45. if (qops && qops->qo_clone)
  46. err = qops->qo_clone(dst, src);
  47. errout:
  48. return err;
  49. }
  50. static int qdisc_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
  51. {
  52. struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
  53. struct rtnl_qdisc_ops *qops;
  54. int line = tca_dump_brief((struct rtnl_tca *) qdisc, "qdisc", p, 0);
  55. qops = rtnl_qdisc_lookup_ops(qdisc);
  56. if (qops && qops->qo_dump[NL_DUMP_BRIEF])
  57. line = qops->qo_dump[NL_DUMP_BRIEF](qdisc, p, line);
  58. dp_dump(p, "\n");
  59. return line;
  60. }
  61. static int qdisc_dump_full(struct nl_object *arg, struct nl_dump_params *p)
  62. {
  63. struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg;
  64. struct rtnl_qdisc_ops *qops;
  65. int line = qdisc_dump_brief(arg, p);
  66. line = tca_dump_full((struct rtnl_tca *) qdisc, p, line);
  67. dp_dump(p, "refcnt %u ", qdisc->q_info);
  68. qops = rtnl_qdisc_lookup_ops(qdisc);
  69. if (qops && qops->qo_dump[NL_DUMP_FULL])
  70. line = qops->qo_dump[NL_DUMP_FULL](qdisc, p, line);
  71. dp_dump(p, "\n");
  72. return line;
  73. }
  74. static int qdisc_dump_stats(struct nl_object *arg, struct nl_dump_params *p)
  75. {
  76. struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg;
  77. struct rtnl_qdisc_ops *qops;
  78. int line = qdisc_dump_full(arg, p);
  79. line = tca_dump_stats((struct rtnl_tca *) qdisc, p, line );
  80. dp_dump(p, "\n");
  81. qops = rtnl_qdisc_lookup_ops(qdisc);
  82. if (qops && qops->qo_dump[NL_DUMP_STATS])
  83. line = qops->qo_dump[NL_DUMP_STATS](qdisc, p, line);
  84. return line;
  85. }
  86. /**
  87. * @name Allocation/Freeing
  88. * @{
  89. */
  90. struct rtnl_qdisc *rtnl_qdisc_alloc(void)
  91. {
  92. return (struct rtnl_qdisc *) nl_object_alloc(&qdisc_obj_ops);
  93. }
  94. void rtnl_qdisc_put(struct rtnl_qdisc *qdisc)
  95. {
  96. nl_object_put((struct nl_object *) qdisc);
  97. }
  98. /** @} */
  99. /**
  100. * @name Iterators
  101. * @{
  102. */
  103. /**
  104. * Call a callback for each child class of a qdisc
  105. * @arg qdisc the parent qdisc
  106. * @arg cache a class cache including all classes of the interface
  107. * the specified qdisc is attached to
  108. * @arg cb callback function
  109. * @arg arg argument to be passed to callback function
  110. */
  111. void rtnl_qdisc_foreach_child(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
  112. void (*cb)(struct nl_object *, void *), void *arg)
  113. {
  114. struct rtnl_class *filter;
  115. filter = rtnl_class_alloc();
  116. if (!filter)
  117. return;
  118. rtnl_class_set_parent(filter, qdisc->q_handle);
  119. rtnl_class_set_ifindex(filter, qdisc->q_ifindex);
  120. rtnl_class_set_kind(filter, qdisc->q_kind);
  121. nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
  122. rtnl_class_put(filter);
  123. }
  124. /**
  125. * Call a callback for each filter attached to the qdisc
  126. * @arg qdisc the parent qdisc
  127. * @arg cache a filter cache including at least all the filters
  128. * attached to the specified qdisc
  129. * @arg cb callback function
  130. * @arg arg argument to be passed to callback function
  131. */
  132. void rtnl_qdisc_foreach_cls(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
  133. void (*cb)(struct nl_object *, void *), void *arg)
  134. {
  135. struct rtnl_cls *filter;
  136. filter = rtnl_cls_alloc();
  137. if (!filter)
  138. return;
  139. rtnl_cls_set_ifindex(filter, qdisc->q_ifindex);
  140. rtnl_cls_set_parent(filter, qdisc->q_parent);
  141. nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
  142. rtnl_cls_put(filter);
  143. }
  144. /** @} */
  145. /**
  146. * @name Attributes
  147. * @{
  148. */
  149. void rtnl_qdisc_set_ifindex(struct rtnl_qdisc *qdisc, int ifindex)
  150. {
  151. tca_set_ifindex((struct rtnl_tca *) qdisc, ifindex);
  152. }
  153. int rtnl_qdisc_get_ifindex(struct rtnl_qdisc *qdisc)
  154. {
  155. return tca_get_ifindex((struct rtnl_tca *) qdisc);
  156. }
  157. void rtnl_qdisc_set_handle(struct rtnl_qdisc *qdisc, uint32_t handle)
  158. {
  159. tca_set_handle((struct rtnl_tca *) qdisc, handle);
  160. }
  161. uint32_t rtnl_qdisc_get_handle(struct rtnl_qdisc *qdisc)
  162. {
  163. return tca_get_handle((struct rtnl_tca *) qdisc);
  164. }
  165. void rtnl_qdisc_set_parent(struct rtnl_qdisc *qdisc, uint32_t parent)
  166. {
  167. tca_set_parent((struct rtnl_tca *) qdisc, parent);
  168. }
  169. uint32_t rtnl_qdisc_get_parent(struct rtnl_qdisc *qdisc)
  170. {
  171. return tca_get_parent((struct rtnl_tca *) qdisc);
  172. }
  173. void rtnl_qdisc_set_kind(struct rtnl_qdisc *qdisc, const char *name)
  174. {
  175. tca_set_kind((struct rtnl_tca *) qdisc, name);
  176. qdisc->q_ops = __rtnl_qdisc_lookup_ops(name);
  177. }
  178. char *rtnl_qdisc_get_kind(struct rtnl_qdisc *qdisc)
  179. {
  180. return tca_get_kind((struct rtnl_tca *) qdisc);
  181. }
  182. uint64_t rtnl_qdisc_get_stat(struct rtnl_qdisc *qdisc,
  183. enum rtnl_tc_stats_id id)
  184. {
  185. return tca_get_stat((struct rtnl_tca *) qdisc, id);
  186. }
  187. /** @} */
  188. /**
  189. * @name Qdisc Specific Options
  190. * @{
  191. */
  192. /**
  193. * Return qdisc specific options for use in TCA_OPTIONS
  194. * @arg qdisc qdisc carrying the optiosn
  195. *
  196. * @return new headerless netlink message carrying the options as payload
  197. */
  198. struct nl_msg *rtnl_qdisc_get_opts(struct rtnl_qdisc *qdisc)
  199. {
  200. struct rtnl_qdisc_ops *ops;
  201. ops = rtnl_qdisc_lookup_ops(qdisc);
  202. if (ops && ops->qo_get_opts)
  203. return ops->qo_get_opts(qdisc);
  204. return NULL;
  205. }
  206. /** @} */
  207. struct nl_object_ops qdisc_obj_ops = {
  208. .oo_name = "route/qdisc",
  209. .oo_size = sizeof(struct rtnl_qdisc),
  210. .oo_free_data = qdisc_free_data,
  211. .oo_clone = qdisc_clone,
  212. .oo_dump[NL_DUMP_BRIEF] = qdisc_dump_brief,
  213. .oo_dump[NL_DUMP_FULL] = qdisc_dump_full,
  214. .oo_dump[NL_DUMP_STATS] = qdisc_dump_stats,
  215. .oo_compare = tca_compare,
  216. .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
  217. };
  218. /** @} */