prio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * lib/route/qdisc/prio.c PRIO Qdisc/Class
  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 qdisc
  13. * @defgroup qdisc_prio (Fast) Prio
  14. * @brief
  15. *
  16. * @par 1) Typical PRIO configuration
  17. * @code
  18. * // Specify the maximal number of bands to be used for this PRIO qdisc.
  19. * rtnl_qdisc_prio_set_bands(qdisc, QDISC_PRIO_DEFAULT_BANDS);
  20. *
  21. * // Provide a map assigning each priority to a band number.
  22. * uint8_t map[] = QDISC_PRIO_DEFAULT_PRIOMAP;
  23. * rtnl_qdisc_prio_set_priomap(qdisc, map, sizeof(map));
  24. * @endcode
  25. * @{
  26. */
  27. #include <netlink-private/netlink.h>
  28. #include <netlink-private/tc.h>
  29. #include <netlink/netlink.h>
  30. #include <netlink/utils.h>
  31. #include <netlink-private/route/tc-api.h>
  32. #include <netlink/route/qdisc.h>
  33. #include <netlink/route/qdisc/prio.h>
  34. /** @cond SKIP */
  35. #define SCH_PRIO_ATTR_BANDS 1
  36. #define SCH_PRIO_ATTR_PRIOMAP 2
  37. /** @endcond */
  38. static int prio_msg_parser(struct rtnl_tc *tc, void *data)
  39. {
  40. struct rtnl_prio *prio = data;
  41. struct tc_prio_qopt *opt;
  42. if (tc->tc_opts->d_size < sizeof(*opt))
  43. return -NLE_INVAL;
  44. opt = (struct tc_prio_qopt *) tc->tc_opts->d_data;
  45. prio->qp_bands = opt->bands;
  46. memcpy(prio->qp_priomap, opt->priomap, sizeof(prio->qp_priomap));
  47. prio->qp_mask = (SCH_PRIO_ATTR_BANDS | SCH_PRIO_ATTR_PRIOMAP);
  48. return 0;
  49. }
  50. static void prio_dump_line(struct rtnl_tc *tc, void *data,
  51. struct nl_dump_params *p)
  52. {
  53. struct rtnl_prio *prio = data;
  54. if (prio)
  55. nl_dump(p, " bands %u", prio->qp_bands);
  56. }
  57. static void prio_dump_details(struct rtnl_tc *tc, void *data,
  58. struct nl_dump_params *p)
  59. {
  60. struct rtnl_prio *prio = data;
  61. int i, hp;
  62. if (!prio)
  63. return;
  64. nl_dump(p, "priomap [");
  65. for (i = 0; i <= TC_PRIO_MAX; i++)
  66. nl_dump(p, "%u%s", prio->qp_priomap[i],
  67. i < TC_PRIO_MAX ? " " : "");
  68. nl_dump(p, "]\n");
  69. nl_new_line(p);
  70. hp = (((TC_PRIO_MAX/2) + 1) & ~1);
  71. for (i = 0; i < hp; i++) {
  72. char a[32];
  73. nl_dump(p, " %18s => %u",
  74. rtnl_prio2str(i, a, sizeof(a)),
  75. prio->qp_priomap[i]);
  76. if (hp+i <= TC_PRIO_MAX) {
  77. nl_dump(p, " %18s => %u",
  78. rtnl_prio2str(hp+i, a, sizeof(a)),
  79. prio->qp_priomap[hp+i]);
  80. if (i < (hp - 1)) {
  81. nl_dump(p, "\n");
  82. nl_new_line(p);
  83. }
  84. }
  85. }
  86. }
  87. static int prio_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
  88. {
  89. struct rtnl_prio *prio = data;
  90. struct tc_prio_qopt opts;
  91. if (!prio || !(prio->qp_mask & SCH_PRIO_ATTR_PRIOMAP))
  92. BUG();
  93. opts.bands = prio->qp_bands;
  94. memcpy(opts.priomap, prio->qp_priomap, sizeof(opts.priomap));
  95. return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
  96. }
  97. /**
  98. * @name Attribute Modification
  99. * @{
  100. */
  101. /**
  102. * Set number of bands of PRIO qdisc.
  103. * @arg qdisc PRIO qdisc to be modified.
  104. * @arg bands New number of bands.
  105. * @return 0 on success or a negative error code.
  106. */
  107. void rtnl_qdisc_prio_set_bands(struct rtnl_qdisc *qdisc, int bands)
  108. {
  109. struct rtnl_prio *prio;
  110. if (!(prio = rtnl_tc_data(TC_CAST(qdisc))))
  111. BUG();
  112. prio->qp_bands = bands;
  113. prio->qp_mask |= SCH_PRIO_ATTR_BANDS;
  114. }
  115. /**
  116. * Get number of bands of PRIO qdisc.
  117. * @arg qdisc PRIO qdisc.
  118. * @return Number of bands or a negative error code.
  119. */
  120. int rtnl_qdisc_prio_get_bands(struct rtnl_qdisc *qdisc)
  121. {
  122. struct rtnl_prio *prio;
  123. if (!(prio = rtnl_tc_data(TC_CAST(qdisc))))
  124. BUG();
  125. if (prio->qp_mask & SCH_PRIO_ATTR_BANDS)
  126. return prio->qp_bands;
  127. else
  128. return -NLE_NOMEM;
  129. }
  130. /**
  131. * Set priomap of the PRIO qdisc.
  132. * @arg qdisc PRIO qdisc to be modified.
  133. * @arg priomap New priority mapping.
  134. * @arg len Length of priomap (# of elements).
  135. * @return 0 on success or a negative error code.
  136. */
  137. int rtnl_qdisc_prio_set_priomap(struct rtnl_qdisc *qdisc, uint8_t priomap[],
  138. int len)
  139. {
  140. struct rtnl_prio *prio;
  141. int i;
  142. if (!(prio = rtnl_tc_data(TC_CAST(qdisc))))
  143. BUG();
  144. if (!(prio->qp_mask & SCH_PRIO_ATTR_BANDS))
  145. return -NLE_MISSING_ATTR;
  146. if ((len / sizeof(uint8_t)) > (TC_PRIO_MAX+1))
  147. return -NLE_RANGE;
  148. for (i = 0; i <= TC_PRIO_MAX; i++) {
  149. if (priomap[i] > prio->qp_bands)
  150. return -NLE_RANGE;
  151. }
  152. memcpy(prio->qp_priomap, priomap, len);
  153. prio->qp_mask |= SCH_PRIO_ATTR_PRIOMAP;
  154. return 0;
  155. }
  156. /**
  157. * Get priomap of a PRIO qdisc.
  158. * @arg qdisc PRIO qdisc.
  159. * @return Priority mapping as array of size TC_PRIO_MAX+1
  160. * or NULL if an error occured.
  161. */
  162. uint8_t *rtnl_qdisc_prio_get_priomap(struct rtnl_qdisc *qdisc)
  163. {
  164. struct rtnl_prio *prio;
  165. if (!(prio = rtnl_tc_data(TC_CAST(qdisc))))
  166. BUG();
  167. if (prio->qp_mask & SCH_PRIO_ATTR_PRIOMAP)
  168. return prio->qp_priomap;
  169. else
  170. return NULL;
  171. }
  172. /** @} */
  173. /**
  174. * @name Priority Band Translations
  175. * @{
  176. */
  177. static const struct trans_tbl prios[] = {
  178. __ADD(TC_PRIO_BESTEFFORT,besteffort)
  179. __ADD(TC_PRIO_FILLER,filler)
  180. __ADD(TC_PRIO_BULK,bulk)
  181. __ADD(TC_PRIO_INTERACTIVE_BULK,interactive_bulk)
  182. __ADD(TC_PRIO_INTERACTIVE,interactive)
  183. __ADD(TC_PRIO_CONTROL,control)
  184. };
  185. /**
  186. * Convert priority to character string.
  187. * @arg prio Priority.
  188. * @arg buf Destination buffer
  189. * @arg size Size of destination buffer.
  190. *
  191. * Converts a priority to a character string and stores the result in
  192. * the specified destination buffer.
  193. *
  194. * @return Name of priority as character string.
  195. */
  196. char * rtnl_prio2str(int prio, char *buf, size_t size)
  197. {
  198. return __type2str(prio, buf, size, prios, ARRAY_SIZE(prios));
  199. }
  200. /**
  201. * Convert character string to priority.
  202. * @arg name Name of priority.
  203. *
  204. * Converts the provided character string specifying a priority
  205. * to the corresponding numeric value.
  206. *
  207. * @return Numeric priority or a negative value if no match was found.
  208. */
  209. int rtnl_str2prio(const char *name)
  210. {
  211. return __str2type(name, prios, ARRAY_SIZE(prios));
  212. }
  213. /** @} */
  214. static struct rtnl_tc_ops prio_ops = {
  215. .to_kind = "prio",
  216. .to_type = RTNL_TC_TYPE_QDISC,
  217. .to_size = sizeof(struct rtnl_prio),
  218. .to_msg_parser = prio_msg_parser,
  219. .to_dump = {
  220. [NL_DUMP_LINE] = prio_dump_line,
  221. [NL_DUMP_DETAILS] = prio_dump_details,
  222. },
  223. .to_msg_fill = prio_msg_fill,
  224. };
  225. static struct rtnl_tc_ops pfifo_fast_ops = {
  226. .to_kind = "pfifo_fast",
  227. .to_type = RTNL_TC_TYPE_QDISC,
  228. .to_size = sizeof(struct rtnl_prio),
  229. .to_msg_parser = prio_msg_parser,
  230. .to_dump = {
  231. [NL_DUMP_LINE] = prio_dump_line,
  232. [NL_DUMP_DETAILS] = prio_dump_details,
  233. },
  234. .to_msg_fill = prio_msg_fill,
  235. };
  236. static void __init prio_init(void)
  237. {
  238. rtnl_tc_register(&prio_ops);
  239. rtnl_tc_register(&pfifo_fast_ops);
  240. }
  241. static void __exit prio_exit(void)
  242. {
  243. rtnl_tc_unregister(&prio_ops);
  244. rtnl_tc_unregister(&pfifo_fast_ops);
  245. }
  246. /** @} */