dsmark.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * lib/route/qdisc/dsmark.c DSMARK
  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. * @ingroup class
  14. * @defgroup qdisc_dsmark Differentiated Services Marker (DSMARK)
  15. * @{
  16. */
  17. #include <netlink-private/netlink.h>
  18. #include <netlink-private/tc.h>
  19. #include <netlink/netlink.h>
  20. #include <netlink/utils.h>
  21. #include <netlink/route/qdisc.h>
  22. #include <netlink-private/route/tc-api.h>
  23. #include <netlink/route/class.h>
  24. #include <netlink/route/qdisc/dsmark.h>
  25. /** @cond SKIP */
  26. #define SCH_DSMARK_ATTR_INDICES 0x1
  27. #define SCH_DSMARK_ATTR_DEFAULT_INDEX 0x2
  28. #define SCH_DSMARK_ATTR_SET_TC_INDEX 0x4
  29. #define SCH_DSMARK_ATTR_MASK 0x1
  30. #define SCH_DSMARK_ATTR_VALUE 0x2
  31. /** @endcond */
  32. static struct nla_policy dsmark_policy[TCA_DSMARK_MAX+1] = {
  33. [TCA_DSMARK_INDICES] = { .type = NLA_U16 },
  34. [TCA_DSMARK_DEFAULT_INDEX] = { .type = NLA_U16 },
  35. [TCA_DSMARK_SET_TC_INDEX] = { .type = NLA_FLAG },
  36. [TCA_DSMARK_VALUE] = { .type = NLA_U8 },
  37. [TCA_DSMARK_MASK] = { .type = NLA_U8 },
  38. };
  39. static int dsmark_qdisc_msg_parser(struct rtnl_tc *tc, void *data)
  40. {
  41. struct rtnl_dsmark_qdisc *dsmark = data;
  42. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  43. int err;
  44. err = tca_parse(tb, TCA_DSMARK_MAX, tc, dsmark_policy);
  45. if (err < 0)
  46. return err;
  47. if (tb[TCA_DSMARK_INDICES]) {
  48. dsmark->qdm_indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
  49. dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
  50. }
  51. if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
  52. dsmark->qdm_default_index =
  53. nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
  54. dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
  55. }
  56. if (tb[TCA_DSMARK_SET_TC_INDEX]) {
  57. dsmark->qdm_set_tc_index = 1;
  58. dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
  59. }
  60. return 0;
  61. }
  62. static int dsmark_class_msg_parser(struct rtnl_tc *tc, void *data)
  63. {
  64. struct rtnl_dsmark_class *dsmark = data;
  65. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  66. int err;
  67. err = tca_parse(tb, TCA_DSMARK_MAX, tc, dsmark_policy);
  68. if (err < 0)
  69. return err;
  70. if (tb[TCA_DSMARK_MASK]) {
  71. dsmark->cdm_bmask = nla_get_u8(tb[TCA_DSMARK_MASK]);
  72. dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
  73. }
  74. if (tb[TCA_DSMARK_VALUE]) {
  75. dsmark->cdm_value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
  76. dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
  77. }
  78. return 0;
  79. }
  80. static void dsmark_qdisc_dump_line(struct rtnl_tc *tc, void *data,
  81. struct nl_dump_params *p)
  82. {
  83. struct rtnl_dsmark_qdisc *dsmark = data;
  84. if (dsmark && (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES))
  85. nl_dump(p, " indices 0x%04x", dsmark->qdm_indices);
  86. }
  87. static void dsmark_qdisc_dump_details(struct rtnl_tc *tc, void *data,
  88. struct nl_dump_params *p)
  89. {
  90. struct rtnl_dsmark_qdisc *dsmark = data;
  91. if (!dsmark)
  92. return;
  93. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
  94. nl_dump(p, " default index 0x%04x", dsmark->qdm_default_index);
  95. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
  96. nl_dump(p, " set-tc-index");
  97. }
  98. static void dsmark_class_dump_line(struct rtnl_tc *tc, void *data,
  99. struct nl_dump_params *p)
  100. {
  101. struct rtnl_dsmark_class *dsmark = data;
  102. if (!dsmark)
  103. return;
  104. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
  105. nl_dump(p, " value 0x%02x", dsmark->cdm_value);
  106. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
  107. nl_dump(p, " mask 0x%02x", dsmark->cdm_bmask);
  108. }
  109. static int dsmark_qdisc_msg_fill(struct rtnl_tc *tc, void *data,
  110. struct nl_msg *msg)
  111. {
  112. struct rtnl_dsmark_qdisc *dsmark = data;
  113. if (!dsmark)
  114. return 0;
  115. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
  116. NLA_PUT_U16(msg, TCA_DSMARK_INDICES, dsmark->qdm_indices);
  117. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
  118. NLA_PUT_U16(msg, TCA_DSMARK_DEFAULT_INDEX,
  119. dsmark->qdm_default_index);
  120. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
  121. NLA_PUT_FLAG(msg, TCA_DSMARK_SET_TC_INDEX);
  122. return 0;
  123. nla_put_failure:
  124. return -NLE_MSGSIZE;
  125. }
  126. static int dsmark_class_msg_fill(struct rtnl_tc *tc, void *data,
  127. struct nl_msg *msg)
  128. {
  129. struct rtnl_dsmark_class *dsmark = data;
  130. if (!dsmark)
  131. return 0;
  132. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
  133. NLA_PUT_U8(msg, TCA_DSMARK_MASK, dsmark->cdm_bmask);
  134. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
  135. NLA_PUT_U8(msg, TCA_DSMARK_VALUE, dsmark->cdm_value);
  136. return 0;
  137. nla_put_failure:
  138. return -NLE_MSGSIZE;
  139. }
  140. /**
  141. * @name Class Attribute Access
  142. * @{
  143. */
  144. /**
  145. * Set bitmask of DSMARK class.
  146. * @arg class DSMARK class to be modified.
  147. * @arg mask New bitmask.
  148. * @return 0 on success or a negative error code.
  149. */
  150. int rtnl_class_dsmark_set_bitmask(struct rtnl_class *class, uint8_t mask)
  151. {
  152. struct rtnl_dsmark_class *dsmark;
  153. if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
  154. return -NLE_NOMEM;
  155. dsmark->cdm_bmask = mask;
  156. dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
  157. return 0;
  158. }
  159. /**
  160. * Get bitmask of DSMARK class.
  161. * @arg class DSMARK class.
  162. * @return Bitmask or a negative error code.
  163. */
  164. int rtnl_class_dsmark_get_bitmask(struct rtnl_class *class)
  165. {
  166. struct rtnl_dsmark_class *dsmark;
  167. if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
  168. return -NLE_NOMEM;
  169. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
  170. return dsmark->cdm_bmask;
  171. else
  172. return -NLE_NOATTR;
  173. }
  174. /**
  175. * Set value of DSMARK class.
  176. * @arg class DSMARK class to be modified.
  177. * @arg value New value.
  178. * @return 0 on success or a negative errror code.
  179. */
  180. int rtnl_class_dsmark_set_value(struct rtnl_class *class, uint8_t value)
  181. {
  182. struct rtnl_dsmark_class *dsmark;
  183. if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
  184. return -NLE_NOMEM;
  185. dsmark->cdm_value = value;
  186. dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
  187. return 0;
  188. }
  189. /**
  190. * Get value of DSMARK class.
  191. * @arg class DSMARK class.
  192. * @return Value or a negative error code.
  193. */
  194. int rtnl_class_dsmark_get_value(struct rtnl_class *class)
  195. {
  196. struct rtnl_dsmark_class *dsmark;
  197. if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
  198. return -NLE_NOMEM;
  199. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
  200. return dsmark->cdm_value;
  201. else
  202. return -NLE_NOATTR;
  203. }
  204. /** @} */
  205. /**
  206. * @name Qdisc Attribute Access
  207. * @{
  208. */
  209. /**
  210. * Set indices of DSMARK qdisc.
  211. * @arg qdisc DSMARK qdisc to be modified.
  212. * @arg indices New indices.
  213. */
  214. int rtnl_qdisc_dsmark_set_indices(struct rtnl_qdisc *qdisc, uint16_t indices)
  215. {
  216. struct rtnl_dsmark_qdisc *dsmark;
  217. if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
  218. return -NLE_NOMEM;
  219. dsmark->qdm_indices = indices;
  220. dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
  221. return 0;
  222. }
  223. /**
  224. * Get indices of DSMARK qdisc.
  225. * @arg qdisc DSMARK qdisc.
  226. * @return Indices or a negative error code.
  227. */
  228. int rtnl_qdisc_dsmark_get_indices(struct rtnl_qdisc *qdisc)
  229. {
  230. struct rtnl_dsmark_qdisc *dsmark;
  231. if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
  232. return -NLE_NOMEM;
  233. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
  234. return dsmark->qdm_indices;
  235. else
  236. return -NLE_NOATTR;
  237. }
  238. /**
  239. * Set default index of DSMARK qdisc.
  240. * @arg qdisc DSMARK qdisc to be modified.
  241. * @arg default_index New default index.
  242. * @return 0 on success or a negative error code.
  243. */
  244. int rtnl_qdisc_dsmark_set_default_index(struct rtnl_qdisc *qdisc,
  245. uint16_t default_index)
  246. {
  247. struct rtnl_dsmark_qdisc *dsmark;
  248. if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
  249. return -NLE_NOMEM;
  250. dsmark->qdm_default_index = default_index;
  251. dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
  252. return 0;
  253. }
  254. /**
  255. * Get default index of DSMARK qdisc.
  256. * @arg qdisc DSMARK qdisc.
  257. * @return Default index or a negative error code.
  258. */
  259. int rtnl_qdisc_dsmark_get_default_index(struct rtnl_qdisc *qdisc)
  260. {
  261. struct rtnl_dsmark_qdisc *dsmark;
  262. if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
  263. return -NLE_NOMEM;
  264. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
  265. return dsmark->qdm_default_index;
  266. else
  267. return -NLE_NOATTR;
  268. }
  269. /**
  270. * Set set-tc-index flag of DSMARK qdisc.
  271. * @arg qdisc DSMARK qdisc to be modified.
  272. * @arg flag Flag indicating whether to enable or disable.
  273. * @return 0 on success or a negative error code.
  274. */
  275. int rtnl_qdisc_dsmark_set_set_tc_index(struct rtnl_qdisc *qdisc, int flag)
  276. {
  277. struct rtnl_dsmark_qdisc *dsmark;
  278. if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
  279. return -NLE_NOMEM;
  280. dsmark->qdm_set_tc_index = !!flag;
  281. dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
  282. return 0;
  283. }
  284. /**
  285. * Get set-tc-index flag of DSMARK qdisc.
  286. * @arg qdisc DSMARK qdisc to be modified.
  287. * @return 1 or 0 to indicate wehther the flag is enabled or a negative
  288. * error code.
  289. */
  290. int rtnl_qdisc_dsmark_get_set_tc_index(struct rtnl_qdisc *qdisc)
  291. {
  292. struct rtnl_dsmark_qdisc *dsmark;
  293. if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
  294. return -NLE_NOMEM;
  295. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
  296. return dsmark->qdm_set_tc_index;
  297. else
  298. return -NLE_NOATTR;
  299. }
  300. /** @} */
  301. static struct rtnl_tc_ops dsmark_qdisc_ops = {
  302. .to_kind = "dsmark",
  303. .to_type = RTNL_TC_TYPE_QDISC,
  304. .to_size = sizeof(struct rtnl_dsmark_qdisc),
  305. .to_msg_parser = dsmark_qdisc_msg_parser,
  306. .to_dump = {
  307. [NL_DUMP_LINE] = dsmark_qdisc_dump_line,
  308. [NL_DUMP_DETAILS] = dsmark_qdisc_dump_details,
  309. },
  310. .to_msg_fill = dsmark_qdisc_msg_fill,
  311. };
  312. static struct rtnl_tc_ops dsmark_class_ops = {
  313. .to_kind = "dsmark",
  314. .to_type = RTNL_TC_TYPE_CLASS,
  315. .to_size = sizeof(struct rtnl_dsmark_class),
  316. .to_msg_parser = dsmark_class_msg_parser,
  317. .to_dump[NL_DUMP_LINE] = dsmark_class_dump_line,
  318. .to_msg_fill = dsmark_class_msg_fill,
  319. };
  320. static void __init dsmark_init(void)
  321. {
  322. rtnl_tc_register(&dsmark_qdisc_ops);
  323. rtnl_tc_register(&dsmark_class_ops);
  324. }
  325. static void __exit dsmark_exit(void)
  326. {
  327. rtnl_tc_unregister(&dsmark_qdisc_ops);
  328. rtnl_tc_unregister(&dsmark_class_ops);
  329. }
  330. /** @} */