dsmark.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * lib/route/sch/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-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup qdisc_api
  13. * @ingroup class_api
  14. * @defgroup dsmark Differentiated Services Marker (DSMARK)
  15. * @{
  16. */
  17. #include <netlink-local.h>
  18. #include <netlink-tc.h>
  19. #include <netlink/netlink.h>
  20. #include <netlink/utils.h>
  21. #include <netlink/route/qdisc.h>
  22. #include <netlink/route/qdisc-modules.h>
  23. #include <netlink/route/class.h>
  24. #include <netlink/route/class-modules.h>
  25. #include <netlink/route/sch/dsmark.h>
  26. /** @cond SKIP */
  27. #define SCH_DSMARK_ATTR_INDICES 0x1
  28. #define SCH_DSMARK_ATTR_DEFAULT_INDEX 0x2
  29. #define SCH_DSMARK_ATTR_SET_TC_INDEX 0x4
  30. #define SCH_DSMARK_ATTR_MASK 0x1
  31. #define SCH_DSMARK_ATTR_VALUE 0x2
  32. /** @endcond */
  33. static inline struct rtnl_dsmark_qdisc *dsmark_qdisc(struct rtnl_qdisc *qdisc)
  34. {
  35. return (struct rtnl_dsmark_qdisc *) qdisc->q_subdata;
  36. }
  37. static inline struct rtnl_dsmark_qdisc *
  38. dsmark_qdisc_alloc(struct rtnl_qdisc *qdisc)
  39. {
  40. if (!qdisc->q_subdata)
  41. qdisc->q_subdata = calloc(1, sizeof(struct rtnl_dsmark_qdisc));
  42. return dsmark_qdisc(qdisc);
  43. }
  44. static struct nla_policy dsmark_policy[TCA_DSMARK_MAX+1] = {
  45. [TCA_DSMARK_INDICES] = { .type = NLA_U16 },
  46. [TCA_DSMARK_DEFAULT_INDEX] = { .type = NLA_U16 },
  47. [TCA_DSMARK_SET_TC_INDEX] = { .type = NLA_FLAG },
  48. [TCA_DSMARK_VALUE] = { .type = NLA_U8 },
  49. [TCA_DSMARK_MASK] = { .type = NLA_U8 },
  50. };
  51. static int dsmark_qdisc_msg_parser(struct rtnl_qdisc *qdisc)
  52. {
  53. int err;
  54. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  55. struct rtnl_dsmark_qdisc *dsmark;
  56. err = tca_parse(tb, TCA_DSMARK_MAX, (struct rtnl_tca *) qdisc,
  57. dsmark_policy);
  58. if (err < 0)
  59. return err;
  60. dsmark = dsmark_qdisc_alloc(qdisc);
  61. if (!dsmark)
  62. return nl_errno(ENOMEM);
  63. if (tb[TCA_DSMARK_INDICES]) {
  64. dsmark->qdm_indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
  65. dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
  66. }
  67. if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
  68. dsmark->qdm_default_index =
  69. nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
  70. dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
  71. }
  72. if (tb[TCA_DSMARK_SET_TC_INDEX]) {
  73. dsmark->qdm_set_tc_index = 1;
  74. dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
  75. }
  76. return 0;
  77. }
  78. static inline struct rtnl_dsmark_class *dsmark_class(struct rtnl_class *class)
  79. {
  80. return (struct rtnl_dsmark_class *) class->c_subdata;
  81. }
  82. static inline struct rtnl_dsmark_class *
  83. dsmark_class_alloc(struct rtnl_class *class)
  84. {
  85. if (!class->c_subdata)
  86. class->c_subdata = calloc(1, sizeof(struct rtnl_dsmark_class));
  87. return dsmark_class(class);
  88. }
  89. static int dsmark_class_msg_parser(struct rtnl_class *class)
  90. {
  91. int err;
  92. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  93. struct rtnl_dsmark_class *dsmark;
  94. err = tca_parse(tb, TCA_DSMARK_MAX, (struct rtnl_tca *) class,
  95. dsmark_policy);
  96. if (err < 0)
  97. return err;
  98. dsmark = dsmark_class_alloc(class);
  99. if (!dsmark)
  100. return nl_errno(ENOMEM);
  101. if (tb[TCA_DSMARK_MASK]) {
  102. dsmark->cdm_bmask = nla_get_u8(tb[TCA_DSMARK_MASK]);
  103. dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
  104. }
  105. if (tb[TCA_DSMARK_VALUE]) {
  106. dsmark->cdm_value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
  107. dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
  108. }
  109. return 0;
  110. }
  111. static int dsmark_qdisc_dump_brief(struct rtnl_qdisc *qdisc,
  112. struct nl_dump_params *p, int line)
  113. {
  114. struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
  115. if (dsmark && (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES))
  116. dp_dump(p, " indices 0x%04x", dsmark->qdm_indices);
  117. return line;
  118. }
  119. static int dsmark_qdisc_dump_full(struct rtnl_qdisc *qdisc,
  120. struct nl_dump_params *p, int line)
  121. {
  122. struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
  123. if (!dsmark)
  124. goto ignore;
  125. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
  126. dp_dump(p, " default index 0x%04x", dsmark->qdm_default_index);
  127. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
  128. dp_dump(p, " set-tc-index");
  129. ignore:
  130. return line;
  131. }
  132. static int dsmark_class_dump_brief(struct rtnl_class *class,
  133. struct nl_dump_params *p, int line)
  134. {
  135. struct rtnl_dsmark_class *dsmark = dsmark_class(class);
  136. if (!dsmark)
  137. goto ignore;
  138. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
  139. dp_dump(p, " value 0x%02x", dsmark->cdm_value);
  140. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
  141. dp_dump(p, " mask 0x%02x", dsmark->cdm_bmask);
  142. ignore:
  143. return line;
  144. }
  145. static struct nl_msg *dsmark_qdisc_get_opts(struct rtnl_qdisc *qdisc)
  146. {
  147. struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
  148. struct nl_msg *msg;
  149. if (!dsmark)
  150. return NULL;
  151. msg = nlmsg_alloc();
  152. if (!msg)
  153. goto nla_put_failure;
  154. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
  155. NLA_PUT_U16(msg, TCA_DSMARK_INDICES, dsmark->qdm_indices);
  156. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
  157. NLA_PUT_U16(msg, TCA_DSMARK_DEFAULT_INDEX,
  158. dsmark->qdm_default_index);
  159. if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
  160. NLA_PUT_FLAG(msg, TCA_DSMARK_SET_TC_INDEX);
  161. return msg;
  162. nla_put_failure:
  163. nlmsg_free(msg);
  164. return NULL;
  165. }
  166. static struct nl_msg *dsmark_class_get_opts(struct rtnl_class *class)
  167. {
  168. struct rtnl_dsmark_class *dsmark = dsmark_class(class);
  169. struct nl_msg *msg;
  170. if (!dsmark)
  171. return NULL;
  172. msg = nlmsg_alloc();
  173. if (!msg)
  174. goto nla_put_failure;
  175. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
  176. NLA_PUT_U8(msg, TCA_DSMARK_MASK, dsmark->cdm_bmask);
  177. if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
  178. NLA_PUT_U8(msg, TCA_DSMARK_VALUE, dsmark->cdm_value);
  179. return msg;
  180. nla_put_failure:
  181. nlmsg_free(msg);
  182. return NULL;
  183. }
  184. /**
  185. * @name Class Attribute Access
  186. * @{
  187. */
  188. /**
  189. * Set bitmask of DSMARK class.
  190. * @arg class DSMARK class to be modified.
  191. * @arg mask New bitmask.
  192. * @return 0 on success or a negative error code.
  193. */
  194. int rtnl_class_dsmark_set_bitmask(struct rtnl_class *class, uint8_t mask)
  195. {
  196. struct rtnl_dsmark_class *dsmark;
  197. dsmark = dsmark_class(class);
  198. if (!dsmark)
  199. return nl_errno(ENOMEM);
  200. dsmark->cdm_bmask = mask;
  201. dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
  202. return 0;
  203. }
  204. /**
  205. * Get bitmask of DSMARK class.
  206. * @arg class DSMARK class.
  207. * @return Bitmask or a negative error code.
  208. */
  209. int rtnl_class_dsmark_get_bitmask(struct rtnl_class *class)
  210. {
  211. struct rtnl_dsmark_class *dsmark;
  212. dsmark = dsmark_class(class);
  213. if (dsmark && dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
  214. return dsmark->cdm_bmask;
  215. else
  216. return nl_errno(ENOENT);
  217. }
  218. /**
  219. * Set value of DSMARK class.
  220. * @arg class DSMARK class to be modified.
  221. * @arg value New value.
  222. * @return 0 on success or a negative errror code.
  223. */
  224. int rtnl_class_dsmark_set_value(struct rtnl_class *class, uint8_t value)
  225. {
  226. struct rtnl_dsmark_class *dsmark;
  227. dsmark = dsmark_class(class);
  228. if (!dsmark)
  229. return nl_errno(ENOMEM);
  230. dsmark->cdm_value = value;
  231. dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
  232. return 0;
  233. }
  234. /**
  235. * Get value of DSMARK class.
  236. * @arg class DSMARK class.
  237. * @return Value or a negative error code.
  238. */
  239. int rtnl_class_dsmark_get_value(struct rtnl_class *class)
  240. {
  241. struct rtnl_dsmark_class *dsmark;
  242. dsmark = dsmark_class(class);
  243. if (dsmark && dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
  244. return dsmark->cdm_value;
  245. else
  246. return nl_errno(ENOENT);
  247. }
  248. /** @} */
  249. /**
  250. * @name Qdisc Attribute Access
  251. * @{
  252. */
  253. /**
  254. * Set indices of DSMARK qdisc.
  255. * @arg qdisc DSMARK qdisc to be modified.
  256. * @arg indices New indices.
  257. */
  258. int rtnl_qdisc_dsmark_set_indices(struct rtnl_qdisc *qdisc, uint16_t indices)
  259. {
  260. struct rtnl_dsmark_qdisc *dsmark;
  261. dsmark = dsmark_qdisc(qdisc);
  262. if (!dsmark)
  263. return nl_errno(ENOMEM);
  264. dsmark->qdm_indices = indices;
  265. dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
  266. return 0;
  267. }
  268. /**
  269. * Get indices of DSMARK qdisc.
  270. * @arg qdisc DSMARK qdisc.
  271. * @return Indices or a negative error code.
  272. */
  273. int rtnl_qdisc_dsmark_get_indices(struct rtnl_qdisc *qdisc)
  274. {
  275. struct rtnl_dsmark_qdisc *dsmark;
  276. dsmark = dsmark_qdisc(qdisc);
  277. if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
  278. return dsmark->qdm_indices;
  279. else
  280. return nl_errno(ENOENT);
  281. }
  282. /**
  283. * Set default index of DSMARK qdisc.
  284. * @arg qdisc DSMARK qdisc to be modified.
  285. * @arg default_index New default index.
  286. * @return 0 on success or a negative error code.
  287. */
  288. int rtnl_qdisc_dsmark_set_default_index(struct rtnl_qdisc *qdisc,
  289. uint16_t default_index)
  290. {
  291. struct rtnl_dsmark_qdisc *dsmark;
  292. dsmark = dsmark_qdisc(qdisc);
  293. if (!dsmark)
  294. return nl_errno(ENOMEM);
  295. dsmark->qdm_default_index = default_index;
  296. dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
  297. return 0;
  298. }
  299. /**
  300. * Get default index of DSMARK qdisc.
  301. * @arg qdisc DSMARK qdisc.
  302. * @return Default index or a negative error code.
  303. */
  304. int rtnl_qdisc_dsmark_get_default_index(struct rtnl_qdisc *qdisc)
  305. {
  306. struct rtnl_dsmark_qdisc *dsmark;
  307. dsmark = dsmark_qdisc(qdisc);
  308. if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
  309. return dsmark->qdm_default_index;
  310. else
  311. return nl_errno(ENOENT);
  312. }
  313. /**
  314. * Set set-tc-index flag of DSMARK qdisc.
  315. * @arg qdisc DSMARK qdisc to be modified.
  316. * @arg flag Flag indicating whether to enable or disable.
  317. * @return 0 on success or a negative error code.
  318. */
  319. int rtnl_qdisc_dsmark_set_set_tc_index(struct rtnl_qdisc *qdisc, int flag)
  320. {
  321. struct rtnl_dsmark_qdisc *dsmark;
  322. dsmark = dsmark_qdisc(qdisc);
  323. if (!dsmark)
  324. return nl_errno(ENOMEM);
  325. dsmark->qdm_set_tc_index = !!flag;
  326. dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
  327. return 0;
  328. }
  329. /**
  330. * Get set-tc-index flag of DSMARK qdisc.
  331. * @arg qdisc DSMARK qdisc to be modified.
  332. * @return 1 or 0 to indicate wehther the flag is enabled or a negative
  333. * error code.
  334. */
  335. int rtnl_qdisc_dsmark_get_set_tc_index(struct rtnl_qdisc *qdisc)
  336. {
  337. struct rtnl_dsmark_qdisc *dsmark;
  338. dsmark = dsmark_qdisc(qdisc);
  339. if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
  340. return dsmark->qdm_set_tc_index;
  341. else
  342. return nl_errno(ENOENT);
  343. }
  344. /** @} */
  345. static struct rtnl_qdisc_ops dsmark_qdisc_ops = {
  346. .qo_kind = "dsmark",
  347. .qo_msg_parser = dsmark_qdisc_msg_parser,
  348. .qo_dump[NL_DUMP_BRIEF] = dsmark_qdisc_dump_brief,
  349. .qo_dump[NL_DUMP_FULL] = dsmark_qdisc_dump_full,
  350. .qo_get_opts = dsmark_qdisc_get_opts,
  351. };
  352. static struct rtnl_class_ops dsmark_class_ops = {
  353. .co_kind = "dsmark",
  354. .co_msg_parser = dsmark_class_msg_parser,
  355. .co_dump[NL_DUMP_BRIEF] = dsmark_class_dump_brief,
  356. .co_get_opts = dsmark_class_get_opts,
  357. };
  358. static void __init dsmark_init(void)
  359. {
  360. rtnl_qdisc_register(&dsmark_qdisc_ops);
  361. rtnl_class_register(&dsmark_class_ops);
  362. }
  363. static void __exit dsmark_exit(void)
  364. {
  365. rtnl_qdisc_unregister(&dsmark_qdisc_ops);
  366. rtnl_class_unregister(&dsmark_class_ops);
  367. }
  368. /** @} */