sch_dsmark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 *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("dsmark_graft(sch %p,[qdisc %p],new %p,old %p)\n",
  50. sch, p, new, old);
  51. if (new == NULL) {
  52. new = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  53. &pfifo_qdisc_ops,
  54. sch->handle);
  55. if (new == NULL)
  56. new = &noop_qdisc;
  57. }
  58. sch_tree_lock(sch);
  59. *old = p->q;
  60. p->q = new;
  61. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  62. qdisc_reset(*old);
  63. sch_tree_unlock(sch);
  64. return 0;
  65. }
  66. static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
  67. {
  68. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  69. return p->q;
  70. }
  71. static unsigned long dsmark_get(struct Qdisc *sch, u32 classid)
  72. {
  73. pr_debug("dsmark_get(sch %p,[qdisc %p],classid %x)\n",
  74. sch, qdisc_priv(sch), classid);
  75. return TC_H_MIN(classid) + 1;
  76. }
  77. static unsigned long dsmark_bind_filter(struct Qdisc *sch,
  78. unsigned long parent, u32 classid)
  79. {
  80. return dsmark_get(sch, classid);
  81. }
  82. static void dsmark_put(struct Qdisc *sch, unsigned long cl)
  83. {
  84. }
  85. static const struct nla_policy dsmark_policy[TCA_DSMARK_MAX + 1] = {
  86. [TCA_DSMARK_INDICES] = { .type = NLA_U16 },
  87. [TCA_DSMARK_DEFAULT_INDEX] = { .type = NLA_U16 },
  88. [TCA_DSMARK_SET_TC_INDEX] = { .type = NLA_FLAG },
  89. [TCA_DSMARK_MASK] = { .type = NLA_U8 },
  90. [TCA_DSMARK_VALUE] = { .type = NLA_U8 },
  91. };
  92. static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
  93. struct nlattr **tca, unsigned long *arg)
  94. {
  95. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  96. struct nlattr *opt = tca[TCA_OPTIONS];
  97. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  98. int err = -EINVAL;
  99. u8 mask = 0;
  100. pr_debug("dsmark_change(sch %p,[qdisc %p],classid %x,parent %x),"
  101. "arg 0x%lx\n", sch, p, classid, parent, *arg);
  102. if (!dsmark_valid_index(p, *arg)) {
  103. err = -ENOENT;
  104. goto errout;
  105. }
  106. if (!opt)
  107. goto errout;
  108. err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
  109. if (err < 0)
  110. goto errout;
  111. if (tb[TCA_DSMARK_MASK])
  112. mask = nla_get_u8(tb[TCA_DSMARK_MASK]);
  113. if (tb[TCA_DSMARK_VALUE])
  114. p->value[*arg-1] = nla_get_u8(tb[TCA_DSMARK_VALUE]);
  115. if (tb[TCA_DSMARK_MASK])
  116. p->mask[*arg-1] = mask;
  117. err = 0;
  118. errout:
  119. return err;
  120. }
  121. static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
  122. {
  123. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  124. if (!dsmark_valid_index(p, arg))
  125. return -EINVAL;
  126. p->mask[arg-1] = 0xff;
  127. p->value[arg-1] = 0;
  128. return 0;
  129. }
  130. static void dsmark_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  131. {
  132. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  133. int i;
  134. pr_debug("dsmark_walk(sch %p,[qdisc %p],walker %p)\n", 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 **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("dsmark_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
  162. if (p->set_tc_index) {
  163. switch (skb->protocol) {
  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. int result = tc_classify(skb, p->filter_list, &res);
  186. pr_debug("result %d class 0x%04x\n", result, res.classid);
  187. switch (result) {
  188. #ifdef CONFIG_NET_CLS_ACT
  189. case TC_ACT_QUEUED:
  190. case TC_ACT_STOLEN:
  191. kfree_skb(skb);
  192. return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  193. case TC_ACT_SHOT:
  194. goto drop;
  195. #endif
  196. case TC_ACT_OK:
  197. skb->tc_index = TC_H_MIN(res.classid);
  198. break;
  199. default:
  200. if (p->default_index != NO_DEFAULT_INDEX)
  201. skb->tc_index = p->default_index;
  202. break;
  203. }
  204. }
  205. err = qdisc_enqueue(skb, p->q);
  206. if (err != NET_XMIT_SUCCESS) {
  207. if (net_xmit_drop_count(err))
  208. sch->qstats.drops++;
  209. return err;
  210. }
  211. sch->bstats.bytes += qdisc_pkt_len(skb);
  212. sch->bstats.packets++;
  213. sch->q.qlen++;
  214. return NET_XMIT_SUCCESS;
  215. drop:
  216. kfree_skb(skb);
  217. sch->qstats.drops++;
  218. return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  219. }
  220. static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
  221. {
  222. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  223. struct sk_buff *skb;
  224. u32 index;
  225. pr_debug("dsmark_dequeue(sch %p,[qdisc %p])\n", sch, p);
  226. skb = p->q->ops->dequeue(p->q);
  227. if (skb == NULL)
  228. return NULL;
  229. sch->q.qlen--;
  230. index = skb->tc_index & (p->indices - 1);
  231. pr_debug("index %d->%d\n", skb->tc_index, index);
  232. switch (skb->protocol) {
  233. case htons(ETH_P_IP):
  234. ipv4_change_dsfield(ip_hdr(skb), p->mask[index],
  235. p->value[index]);
  236. break;
  237. case htons(ETH_P_IPV6):
  238. ipv6_change_dsfield(ipv6_hdr(skb), p->mask[index],
  239. p->value[index]);
  240. break;
  241. default:
  242. /*
  243. * Only complain if a change was actually attempted.
  244. * This way, we can send non-IP traffic through dsmark
  245. * and don't need yet another qdisc as a bypass.
  246. */
  247. if (p->mask[index] != 0xff || p->value[index])
  248. printk(KERN_WARNING
  249. "dsmark_dequeue: unsupported protocol %d\n",
  250. ntohs(skb->protocol));
  251. break;
  252. }
  253. return skb;
  254. }
  255. static struct sk_buff *dsmark_peek(struct Qdisc *sch)
  256. {
  257. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  258. pr_debug("dsmark_peek(sch %p,[qdisc %p])\n", sch, p);
  259. return p->q->ops->peek(p->q);
  260. }
  261. static unsigned int dsmark_drop(struct Qdisc *sch)
  262. {
  263. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  264. unsigned int len;
  265. pr_debug("dsmark_reset(sch %p,[qdisc %p])\n", sch, p);
  266. if (p->q->ops->drop == NULL)
  267. return 0;
  268. len = p->q->ops->drop(p->q);
  269. if (len)
  270. sch->q.qlen--;
  271. return len;
  272. }
  273. static int dsmark_init(struct Qdisc *sch, struct nlattr *opt)
  274. {
  275. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  276. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  277. int err = -EINVAL;
  278. u32 default_index = NO_DEFAULT_INDEX;
  279. u16 indices;
  280. u8 *mask;
  281. pr_debug("dsmark_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
  282. if (!opt)
  283. goto errout;
  284. err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
  285. if (err < 0)
  286. goto errout;
  287. err = -EINVAL;
  288. indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
  289. if (hweight32(indices) != 1)
  290. goto errout;
  291. if (tb[TCA_DSMARK_DEFAULT_INDEX])
  292. default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
  293. mask = kmalloc(indices * 2, GFP_KERNEL);
  294. if (mask == NULL) {
  295. err = -ENOMEM;
  296. goto errout;
  297. }
  298. p->mask = mask;
  299. memset(p->mask, 0xff, indices);
  300. p->value = p->mask + indices;
  301. memset(p->value, 0, indices);
  302. p->indices = indices;
  303. p->default_index = default_index;
  304. p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
  305. p->q = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  306. &pfifo_qdisc_ops, sch->handle);
  307. if (p->q == NULL)
  308. p->q = &noop_qdisc;
  309. pr_debug("dsmark_init: qdisc %p\n", p->q);
  310. err = 0;
  311. errout:
  312. return err;
  313. }
  314. static void dsmark_reset(struct Qdisc *sch)
  315. {
  316. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  317. pr_debug("dsmark_reset(sch %p,[qdisc %p])\n", sch, p);
  318. qdisc_reset(p->q);
  319. sch->q.qlen = 0;
  320. }
  321. static void dsmark_destroy(struct Qdisc *sch)
  322. {
  323. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  324. pr_debug("dsmark_destroy(sch %p,[qdisc %p])\n", sch, p);
  325. tcf_destroy_chain(&p->filter_list);
  326. qdisc_destroy(p->q);
  327. kfree(p->mask);
  328. }
  329. static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
  330. struct sk_buff *skb, struct tcmsg *tcm)
  331. {
  332. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  333. struct nlattr *opts = NULL;
  334. pr_debug("dsmark_dump_class(sch %p,[qdisc %p],class %ld\n", sch, p, cl);
  335. if (!dsmark_valid_index(p, cl))
  336. return -EINVAL;
  337. tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl-1);
  338. tcm->tcm_info = p->q->handle;
  339. opts = nla_nest_start(skb, TCA_OPTIONS);
  340. if (opts == NULL)
  341. goto nla_put_failure;
  342. NLA_PUT_U8(skb, TCA_DSMARK_MASK, p->mask[cl-1]);
  343. NLA_PUT_U8(skb, TCA_DSMARK_VALUE, p->value[cl-1]);
  344. return nla_nest_end(skb, opts);
  345. nla_put_failure:
  346. nla_nest_cancel(skb, opts);
  347. return -EMSGSIZE;
  348. }
  349. static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
  350. {
  351. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  352. struct nlattr *opts = NULL;
  353. opts = nla_nest_start(skb, TCA_OPTIONS);
  354. if (opts == NULL)
  355. goto nla_put_failure;
  356. NLA_PUT_U16(skb, TCA_DSMARK_INDICES, p->indices);
  357. if (p->default_index != NO_DEFAULT_INDEX)
  358. NLA_PUT_U16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index);
  359. if (p->set_tc_index)
  360. NLA_PUT_FLAG(skb, TCA_DSMARK_SET_TC_INDEX);
  361. return nla_nest_end(skb, opts);
  362. nla_put_failure:
  363. nla_nest_cancel(skb, opts);
  364. return -EMSGSIZE;
  365. }
  366. static const struct Qdisc_class_ops dsmark_class_ops = {
  367. .graft = dsmark_graft,
  368. .leaf = dsmark_leaf,
  369. .get = dsmark_get,
  370. .put = dsmark_put,
  371. .change = dsmark_change,
  372. .delete = dsmark_delete,
  373. .walk = dsmark_walk,
  374. .tcf_chain = dsmark_find_tcf,
  375. .bind_tcf = dsmark_bind_filter,
  376. .unbind_tcf = dsmark_put,
  377. .dump = dsmark_dump_class,
  378. };
  379. static struct Qdisc_ops dsmark_qdisc_ops __read_mostly = {
  380. .next = NULL,
  381. .cl_ops = &dsmark_class_ops,
  382. .id = "dsmark",
  383. .priv_size = sizeof(struct dsmark_qdisc_data),
  384. .enqueue = dsmark_enqueue,
  385. .dequeue = dsmark_dequeue,
  386. .peek = dsmark_peek,
  387. .drop = dsmark_drop,
  388. .init = dsmark_init,
  389. .reset = dsmark_reset,
  390. .destroy = dsmark_destroy,
  391. .change = NULL,
  392. .dump = dsmark_dump,
  393. .owner = THIS_MODULE,
  394. };
  395. static int __init dsmark_module_init(void)
  396. {
  397. return register_qdisc(&dsmark_qdisc_ops);
  398. }
  399. static void __exit dsmark_module_exit(void)
  400. {
  401. unregister_qdisc(&dsmark_qdisc_ops);
  402. }
  403. module_init(dsmark_module_init)
  404. module_exit(dsmark_module_exit)
  405. MODULE_LICENSE("GPL");