sch_dsmark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* net/sched/sch_dsmark.c - Differentiated Services field marker */
  2. /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <linux/types.h>
  7. #include <linux/string.h>
  8. #include <linux/errno.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/rtnetlink.h>
  11. #include <linux/bitops.h>
  12. #include <net/pkt_sched.h>
  13. #include <net/dsfield.h>
  14. #include <net/inet_ecn.h>
  15. #include <asm/byteorder.h>
  16. /*
  17. * classid class marking
  18. * ------- ----- -------
  19. * n/a 0 n/a
  20. * x:0 1 use entry [0]
  21. * ... ... ...
  22. * x:y y>0 y+1 use entry [y]
  23. * ... ... ...
  24. * x:indices-1 indices use entry [indices-1]
  25. * ... ... ...
  26. * x:y y+1 use entry [y & (indices-1)]
  27. * ... ... ...
  28. * 0xffff 0x10000 use entry [indices-1]
  29. */
  30. #define NO_DEFAULT_INDEX (1 << 16)
  31. struct dsmark_qdisc_data {
  32. struct Qdisc *q;
  33. struct tcf_proto __rcu *filter_list;
  34. u8 *mask; /* "owns" the array */
  35. u8 *value;
  36. u16 indices;
  37. u32 default_index; /* index range is 0...0xffff */
  38. int set_tc_index;
  39. };
  40. static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index)
  41. {
  42. return index <= p->indices && index > 0;
  43. }
  44. /* ------------------------- Class/flow operations ------------------------- */
  45. static int dsmark_graft(struct Qdisc *sch, unsigned long arg,
  46. struct Qdisc *new, struct Qdisc **old)
  47. {
  48. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  49. pr_debug("%s(sch %p,[qdisc %p],new %p,old %p)\n",
  50. __func__, sch, p, new, old);
  51. if (new == NULL) {
  52. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  53. sch->handle);
  54. if (new == NULL)
  55. new = &noop_qdisc;
  56. }
  57. sch_tree_lock(sch);
  58. *old = p->q;
  59. p->q = new;
  60. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  61. qdisc_reset(*old);
  62. sch_tree_unlock(sch);
  63. return 0;
  64. }
  65. static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
  66. {
  67. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  68. return p->q;
  69. }
  70. static unsigned long dsmark_get(struct Qdisc *sch, u32 classid)
  71. {
  72. pr_debug("%s(sch %p,[qdisc %p],classid %x)\n",
  73. __func__, sch, qdisc_priv(sch), classid);
  74. return TC_H_MIN(classid) + 1;
  75. }
  76. static unsigned long dsmark_bind_filter(struct Qdisc *sch,
  77. unsigned long parent, u32 classid)
  78. {
  79. return dsmark_get(sch, classid);
  80. }
  81. static void dsmark_put(struct Qdisc *sch, unsigned long cl)
  82. {
  83. }
  84. static const struct nla_policy dsmark_policy[TCA_DSMARK_MAX + 1] = {
  85. [TCA_DSMARK_INDICES] = { .type = NLA_U16 },
  86. [TCA_DSMARK_DEFAULT_INDEX] = { .type = NLA_U16 },
  87. [TCA_DSMARK_SET_TC_INDEX] = { .type = NLA_FLAG },
  88. [TCA_DSMARK_MASK] = { .type = NLA_U8 },
  89. [TCA_DSMARK_VALUE] = { .type = NLA_U8 },
  90. };
  91. static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
  92. struct nlattr **tca, unsigned long *arg)
  93. {
  94. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  95. struct nlattr *opt = tca[TCA_OPTIONS];
  96. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  97. int err = -EINVAL;
  98. u8 mask = 0;
  99. pr_debug("%s(sch %p,[qdisc %p],classid %x,parent %x), arg 0x%lx\n",
  100. __func__, sch, p, classid, parent, *arg);
  101. if (!dsmark_valid_index(p, *arg)) {
  102. err = -ENOENT;
  103. goto errout;
  104. }
  105. if (!opt)
  106. goto errout;
  107. err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
  108. if (err < 0)
  109. goto errout;
  110. if (tb[TCA_DSMARK_MASK])
  111. mask = nla_get_u8(tb[TCA_DSMARK_MASK]);
  112. if (tb[TCA_DSMARK_VALUE])
  113. p->value[*arg - 1] = nla_get_u8(tb[TCA_DSMARK_VALUE]);
  114. if (tb[TCA_DSMARK_MASK])
  115. p->mask[*arg - 1] = mask;
  116. err = 0;
  117. errout:
  118. return err;
  119. }
  120. static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
  121. {
  122. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  123. if (!dsmark_valid_index(p, arg))
  124. return -EINVAL;
  125. p->mask[arg - 1] = 0xff;
  126. p->value[arg - 1] = 0;
  127. return 0;
  128. }
  129. static void dsmark_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  130. {
  131. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  132. int i;
  133. pr_debug("%s(sch %p,[qdisc %p],walker %p)\n",
  134. __func__, sch, p, walker);
  135. if (walker->stop)
  136. return;
  137. for (i = 0; i < p->indices; i++) {
  138. if (p->mask[i] == 0xff && !p->value[i])
  139. goto ignore;
  140. if (walker->count >= walker->skip) {
  141. if (walker->fn(sch, i + 1, walker) < 0) {
  142. walker->stop = 1;
  143. break;
  144. }
  145. }
  146. ignore:
  147. walker->count++;
  148. }
  149. }
  150. static inline struct tcf_proto __rcu **dsmark_find_tcf(struct Qdisc *sch,
  151. unsigned long cl)
  152. {
  153. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  154. return &p->filter_list;
  155. }
  156. /* --------------------------- Qdisc operations ---------------------------- */
  157. static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  158. {
  159. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  160. int err;
  161. pr_debug("%s(skb %p,sch %p,[qdisc %p])\n", __func__, skb, sch, p);
  162. if (p->set_tc_index) {
  163. switch (tc_skb_protocol(skb)) {
  164. case htons(ETH_P_IP):
  165. if (skb_cow_head(skb, sizeof(struct iphdr)))
  166. goto drop;
  167. skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
  168. & ~INET_ECN_MASK;
  169. break;
  170. case htons(ETH_P_IPV6):
  171. if (skb_cow_head(skb, sizeof(struct ipv6hdr)))
  172. goto drop;
  173. skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb))
  174. & ~INET_ECN_MASK;
  175. break;
  176. default:
  177. skb->tc_index = 0;
  178. break;
  179. }
  180. }
  181. if (TC_H_MAJ(skb->priority) == sch->handle)
  182. skb->tc_index = TC_H_MIN(skb->priority);
  183. else {
  184. struct tcf_result res;
  185. struct tcf_proto *fl = rcu_dereference_bh(p->filter_list);
  186. int result = tc_classify(skb, fl, &res);
  187. pr_debug("result %d class 0x%04x\n", result, res.classid);
  188. switch (result) {
  189. #ifdef CONFIG_NET_CLS_ACT
  190. case TC_ACT_QUEUED:
  191. case TC_ACT_STOLEN:
  192. kfree_skb(skb);
  193. return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  194. case TC_ACT_SHOT:
  195. goto drop;
  196. #endif
  197. case TC_ACT_OK:
  198. skb->tc_index = TC_H_MIN(res.classid);
  199. break;
  200. default:
  201. if (p->default_index != NO_DEFAULT_INDEX)
  202. skb->tc_index = p->default_index;
  203. break;
  204. }
  205. }
  206. err = qdisc_enqueue(skb, p->q);
  207. if (err != NET_XMIT_SUCCESS) {
  208. if (net_xmit_drop_count(err))
  209. qdisc_qstats_drop(sch);
  210. return err;
  211. }
  212. sch->q.qlen++;
  213. return NET_XMIT_SUCCESS;
  214. drop:
  215. qdisc_drop(skb, sch);
  216. return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  217. }
  218. static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
  219. {
  220. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  221. struct sk_buff *skb;
  222. u32 index;
  223. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  224. skb = p->q->ops->dequeue(p->q);
  225. if (skb == NULL)
  226. return NULL;
  227. qdisc_bstats_update(sch, skb);
  228. sch->q.qlen--;
  229. index = skb->tc_index & (p->indices - 1);
  230. pr_debug("index %d->%d\n", skb->tc_index, index);
  231. switch (tc_skb_protocol(skb)) {
  232. case htons(ETH_P_IP):
  233. ipv4_change_dsfield(ip_hdr(skb), p->mask[index],
  234. p->value[index]);
  235. break;
  236. case htons(ETH_P_IPV6):
  237. ipv6_change_dsfield(ipv6_hdr(skb), p->mask[index],
  238. p->value[index]);
  239. break;
  240. default:
  241. /*
  242. * Only complain if a change was actually attempted.
  243. * This way, we can send non-IP traffic through dsmark
  244. * and don't need yet another qdisc as a bypass.
  245. */
  246. if (p->mask[index] != 0xff || p->value[index])
  247. pr_warn("%s: unsupported protocol %d\n",
  248. __func__, ntohs(tc_skb_protocol(skb)));
  249. break;
  250. }
  251. return skb;
  252. }
  253. static struct sk_buff *dsmark_peek(struct Qdisc *sch)
  254. {
  255. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  256. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  257. return p->q->ops->peek(p->q);
  258. }
  259. static unsigned int dsmark_drop(struct Qdisc *sch)
  260. {
  261. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  262. unsigned int len;
  263. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  264. if (p->q->ops->drop == NULL)
  265. return 0;
  266. len = p->q->ops->drop(p->q);
  267. if (len)
  268. sch->q.qlen--;
  269. return len;
  270. }
  271. static int dsmark_init(struct Qdisc *sch, struct nlattr *opt)
  272. {
  273. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  274. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  275. int err = -EINVAL;
  276. u32 default_index = NO_DEFAULT_INDEX;
  277. u16 indices;
  278. u8 *mask;
  279. pr_debug("%s(sch %p,[qdisc %p],opt %p)\n", __func__, sch, p, opt);
  280. if (!opt)
  281. goto errout;
  282. err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
  283. if (err < 0)
  284. goto errout;
  285. err = -EINVAL;
  286. indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
  287. if (hweight32(indices) != 1)
  288. goto errout;
  289. if (tb[TCA_DSMARK_DEFAULT_INDEX])
  290. default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
  291. mask = kmalloc(indices * 2, GFP_KERNEL);
  292. if (mask == NULL) {
  293. err = -ENOMEM;
  294. goto errout;
  295. }
  296. p->mask = mask;
  297. memset(p->mask, 0xff, indices);
  298. p->value = p->mask + indices;
  299. memset(p->value, 0, indices);
  300. p->indices = indices;
  301. p->default_index = default_index;
  302. p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
  303. p->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, sch->handle);
  304. if (p->q == NULL)
  305. p->q = &noop_qdisc;
  306. pr_debug("%s: qdisc %p\n", __func__, p->q);
  307. err = 0;
  308. errout:
  309. return err;
  310. }
  311. static void dsmark_reset(struct Qdisc *sch)
  312. {
  313. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  314. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  315. qdisc_reset(p->q);
  316. sch->q.qlen = 0;
  317. }
  318. static void dsmark_destroy(struct Qdisc *sch)
  319. {
  320. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  321. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  322. tcf_destroy_chain(&p->filter_list);
  323. qdisc_destroy(p->q);
  324. kfree(p->mask);
  325. }
  326. static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
  327. struct sk_buff *skb, struct tcmsg *tcm)
  328. {
  329. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  330. struct nlattr *opts = NULL;
  331. pr_debug("%s(sch %p,[qdisc %p],class %ld\n", __func__, sch, p, cl);
  332. if (!dsmark_valid_index(p, cl))
  333. return -EINVAL;
  334. tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl - 1);
  335. tcm->tcm_info = p->q->handle;
  336. opts = nla_nest_start(skb, TCA_OPTIONS);
  337. if (opts == NULL)
  338. goto nla_put_failure;
  339. if (nla_put_u8(skb, TCA_DSMARK_MASK, p->mask[cl - 1]) ||
  340. nla_put_u8(skb, TCA_DSMARK_VALUE, p->value[cl - 1]))
  341. goto nla_put_failure;
  342. return nla_nest_end(skb, opts);
  343. nla_put_failure:
  344. nla_nest_cancel(skb, opts);
  345. return -EMSGSIZE;
  346. }
  347. static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
  348. {
  349. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  350. struct nlattr *opts = NULL;
  351. opts = nla_nest_start(skb, TCA_OPTIONS);
  352. if (opts == NULL)
  353. goto nla_put_failure;
  354. if (nla_put_u16(skb, TCA_DSMARK_INDICES, p->indices))
  355. goto nla_put_failure;
  356. if (p->default_index != NO_DEFAULT_INDEX &&
  357. nla_put_u16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index))
  358. goto nla_put_failure;
  359. if (p->set_tc_index &&
  360. nla_put_flag(skb, TCA_DSMARK_SET_TC_INDEX))
  361. goto nla_put_failure;
  362. return nla_nest_end(skb, opts);
  363. nla_put_failure:
  364. nla_nest_cancel(skb, opts);
  365. return -EMSGSIZE;
  366. }
  367. static const struct Qdisc_class_ops dsmark_class_ops = {
  368. .graft = dsmark_graft,
  369. .leaf = dsmark_leaf,
  370. .get = dsmark_get,
  371. .put = dsmark_put,
  372. .change = dsmark_change,
  373. .delete = dsmark_delete,
  374. .walk = dsmark_walk,
  375. .tcf_chain = dsmark_find_tcf,
  376. .bind_tcf = dsmark_bind_filter,
  377. .unbind_tcf = dsmark_put,
  378. .dump = dsmark_dump_class,
  379. };
  380. static struct Qdisc_ops dsmark_qdisc_ops __read_mostly = {
  381. .next = NULL,
  382. .cl_ops = &dsmark_class_ops,
  383. .id = "dsmark",
  384. .priv_size = sizeof(struct dsmark_qdisc_data),
  385. .enqueue = dsmark_enqueue,
  386. .dequeue = dsmark_dequeue,
  387. .peek = dsmark_peek,
  388. .drop = dsmark_drop,
  389. .init = dsmark_init,
  390. .reset = dsmark_reset,
  391. .destroy = dsmark_destroy,
  392. .change = NULL,
  393. .dump = dsmark_dump,
  394. .owner = THIS_MODULE,
  395. };
  396. static int __init dsmark_module_init(void)
  397. {
  398. return register_qdisc(&dsmark_qdisc_ops);
  399. }
  400. static void __exit dsmark_module_exit(void)
  401. {
  402. unregister_qdisc(&dsmark_qdisc_ops);
  403. }
  404. module_init(dsmark_module_init)
  405. module_exit(dsmark_module_exit)
  406. MODULE_LICENSE("GPL");