sch_dsmark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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/types.h>
  6. #include <linux/string.h>
  7. #include <linux/errno.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/rtnetlink.h>
  10. #include <linux/bitops.h>
  11. #include <net/pkt_sched.h>
  12. #include <net/dsfield.h>
  13. #include <net/inet_ecn.h>
  14. #include <asm/byteorder.h>
  15. /*
  16. * classid class marking
  17. * ------- ----- -------
  18. * n/a 0 n/a
  19. * x:0 1 use entry [0]
  20. * ... ... ...
  21. * x:y y>0 y+1 use entry [y]
  22. * ... ... ...
  23. * x:indices-1 indices use entry [indices-1]
  24. * ... ... ...
  25. * x:y y+1 use entry [y & (indices-1)]
  26. * ... ... ...
  27. * 0xffff 0x10000 use entry [indices-1]
  28. */
  29. #define NO_DEFAULT_INDEX (1 << 16)
  30. struct dsmark_qdisc_data {
  31. struct Qdisc *q;
  32. struct tcf_proto *filter_list;
  33. u8 *mask; /* "owns" the array */
  34. u8 *value;
  35. u16 indices;
  36. u32 default_index; /* index range is 0...0xffff */
  37. int set_tc_index;
  38. };
  39. static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index)
  40. {
  41. return (index <= p->indices && index > 0);
  42. }
  43. /* ------------------------- Class/flow operations ------------------------- */
  44. static int dsmark_graft(struct Qdisc *sch, unsigned long arg,
  45. struct Qdisc *new, struct Qdisc **old)
  46. {
  47. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  48. pr_debug("dsmark_graft(sch %p,[qdisc %p],new %p,old %p)\n",
  49. sch, p, new, old);
  50. if (new == NULL) {
  51. new = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  52. &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("dsmark_get(sch %p,[qdisc %p],classid %x)\n",
  73. 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("dsmark_change(sch %p,[qdisc %p],classid %x,parent %x),"
  100. "arg 0x%lx\n", 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("dsmark_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
  134. if (walker->stop)
  135. return;
  136. for (i = 0; i < p->indices; i++) {
  137. if (p->mask[i] == 0xff && !p->value[i])
  138. goto ignore;
  139. if (walker->count >= walker->skip) {
  140. if (walker->fn(sch, i+1, walker) < 0) {
  141. walker->stop = 1;
  142. break;
  143. }
  144. }
  145. ignore:
  146. walker->count++;
  147. }
  148. }
  149. static inline struct tcf_proto **dsmark_find_tcf(struct Qdisc *sch,
  150. unsigned long cl)
  151. {
  152. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  153. return &p->filter_list;
  154. }
  155. /* --------------------------- Qdisc operations ---------------------------- */
  156. static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  157. {
  158. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  159. int err;
  160. pr_debug("dsmark_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
  161. if (p->set_tc_index) {
  162. switch (skb->protocol) {
  163. case htons(ETH_P_IP):
  164. if (skb_cow_head(skb, sizeof(struct iphdr)))
  165. goto drop;
  166. skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
  167. & ~INET_ECN_MASK;
  168. break;
  169. case htons(ETH_P_IPV6):
  170. if (skb_cow_head(skb, sizeof(struct ipv6hdr)))
  171. goto drop;
  172. skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb))
  173. & ~INET_ECN_MASK;
  174. break;
  175. default:
  176. skb->tc_index = 0;
  177. break;
  178. }
  179. }
  180. if (TC_H_MAJ(skb->priority) == sch->handle)
  181. skb->tc_index = TC_H_MIN(skb->priority);
  182. else {
  183. struct tcf_result res;
  184. int result = tc_classify(skb, p->filter_list, &res);
  185. pr_debug("result %d class 0x%04x\n", result, res.classid);
  186. switch (result) {
  187. #ifdef CONFIG_NET_CLS_ACT
  188. case TC_ACT_QUEUED:
  189. case TC_ACT_STOLEN:
  190. kfree_skb(skb);
  191. return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  192. case TC_ACT_SHOT:
  193. goto drop;
  194. #endif
  195. case TC_ACT_OK:
  196. skb->tc_index = TC_H_MIN(res.classid);
  197. break;
  198. default:
  199. if (p->default_index != NO_DEFAULT_INDEX)
  200. skb->tc_index = p->default_index;
  201. break;
  202. }
  203. }
  204. err = qdisc_enqueue(skb, p->q);
  205. if (err != NET_XMIT_SUCCESS) {
  206. if (net_xmit_drop_count(err))
  207. sch->qstats.drops++;
  208. return err;
  209. }
  210. sch->bstats.bytes += qdisc_pkt_len(skb);
  211. sch->bstats.packets++;
  212. sch->q.qlen++;
  213. return NET_XMIT_SUCCESS;
  214. drop:
  215. kfree_skb(skb);
  216. sch->qstats.drops++;
  217. return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  218. }
  219. static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
  220. {
  221. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  222. struct sk_buff *skb;
  223. u32 index;
  224. pr_debug("dsmark_dequeue(sch %p,[qdisc %p])\n", sch, p);
  225. skb = p->q->ops->dequeue(p->q);
  226. if (skb == NULL)
  227. return NULL;
  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 (skb->protocol) {
  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. printk(KERN_WARNING
  248. "dsmark_dequeue: unsupported protocol %d\n",
  249. ntohs(skb->protocol));
  250. break;
  251. }
  252. return skb;
  253. }
  254. static struct sk_buff *dsmark_peek(struct Qdisc *sch)
  255. {
  256. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  257. pr_debug("dsmark_peek(sch %p,[qdisc %p])\n", sch, p);
  258. return p->q->ops->peek(p->q);
  259. }
  260. static unsigned int dsmark_drop(struct Qdisc *sch)
  261. {
  262. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  263. unsigned int len;
  264. pr_debug("dsmark_reset(sch %p,[qdisc %p])\n", sch, p);
  265. if (p->q->ops->drop == NULL)
  266. return 0;
  267. len = p->q->ops->drop(p->q);
  268. if (len)
  269. sch->q.qlen--;
  270. return len;
  271. }
  272. static int dsmark_init(struct Qdisc *sch, struct nlattr *opt)
  273. {
  274. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  275. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  276. int err = -EINVAL;
  277. u32 default_index = NO_DEFAULT_INDEX;
  278. u16 indices;
  279. u8 *mask;
  280. pr_debug("dsmark_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
  281. if (!opt)
  282. goto errout;
  283. err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
  284. if (err < 0)
  285. goto errout;
  286. err = -EINVAL;
  287. indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
  288. if (hweight32(indices) != 1)
  289. goto errout;
  290. if (tb[TCA_DSMARK_DEFAULT_INDEX])
  291. default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
  292. mask = kmalloc(indices * 2, GFP_KERNEL);
  293. if (mask == NULL) {
  294. err = -ENOMEM;
  295. goto errout;
  296. }
  297. p->mask = mask;
  298. memset(p->mask, 0xff, indices);
  299. p->value = p->mask + indices;
  300. memset(p->value, 0, indices);
  301. p->indices = indices;
  302. p->default_index = default_index;
  303. p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
  304. p->q = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  305. &pfifo_qdisc_ops, sch->handle);
  306. if (p->q == NULL)
  307. p->q = &noop_qdisc;
  308. pr_debug("dsmark_init: qdisc %p\n", p->q);
  309. err = 0;
  310. errout:
  311. return err;
  312. }
  313. static void dsmark_reset(struct Qdisc *sch)
  314. {
  315. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  316. pr_debug("dsmark_reset(sch %p,[qdisc %p])\n", sch, p);
  317. qdisc_reset(p->q);
  318. sch->q.qlen = 0;
  319. }
  320. static void dsmark_destroy(struct Qdisc *sch)
  321. {
  322. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  323. pr_debug("dsmark_destroy(sch %p,[qdisc %p])\n", sch, p);
  324. tcf_destroy_chain(&p->filter_list);
  325. qdisc_destroy(p->q);
  326. kfree(p->mask);
  327. }
  328. static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
  329. struct sk_buff *skb, struct tcmsg *tcm)
  330. {
  331. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  332. struct nlattr *opts = NULL;
  333. pr_debug("dsmark_dump_class(sch %p,[qdisc %p],class %ld\n", sch, p, cl);
  334. if (!dsmark_valid_index(p, cl))
  335. return -EINVAL;
  336. tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl-1);
  337. tcm->tcm_info = p->q->handle;
  338. opts = nla_nest_start(skb, TCA_OPTIONS);
  339. if (opts == NULL)
  340. goto nla_put_failure;
  341. NLA_PUT_U8(skb, TCA_DSMARK_MASK, p->mask[cl-1]);
  342. NLA_PUT_U8(skb, TCA_DSMARK_VALUE, p->value[cl-1]);
  343. return nla_nest_end(skb, opts);
  344. nla_put_failure:
  345. nla_nest_cancel(skb, opts);
  346. return -EMSGSIZE;
  347. }
  348. static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
  349. {
  350. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  351. struct nlattr *opts = NULL;
  352. opts = nla_nest_start(skb, TCA_OPTIONS);
  353. if (opts == NULL)
  354. goto nla_put_failure;
  355. NLA_PUT_U16(skb, TCA_DSMARK_INDICES, p->indices);
  356. if (p->default_index != NO_DEFAULT_INDEX)
  357. NLA_PUT_U16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index);
  358. if (p->set_tc_index)
  359. NLA_PUT_FLAG(skb, TCA_DSMARK_SET_TC_INDEX);
  360. return nla_nest_end(skb, opts);
  361. nla_put_failure:
  362. nla_nest_cancel(skb, opts);
  363. return -EMSGSIZE;
  364. }
  365. static const struct Qdisc_class_ops dsmark_class_ops = {
  366. .graft = dsmark_graft,
  367. .leaf = dsmark_leaf,
  368. .get = dsmark_get,
  369. .put = dsmark_put,
  370. .change = dsmark_change,
  371. .delete = dsmark_delete,
  372. .walk = dsmark_walk,
  373. .tcf_chain = dsmark_find_tcf,
  374. .bind_tcf = dsmark_bind_filter,
  375. .unbind_tcf = dsmark_put,
  376. .dump = dsmark_dump_class,
  377. };
  378. static struct Qdisc_ops dsmark_qdisc_ops __read_mostly = {
  379. .next = NULL,
  380. .cl_ops = &dsmark_class_ops,
  381. .id = "dsmark",
  382. .priv_size = sizeof(struct dsmark_qdisc_data),
  383. .enqueue = dsmark_enqueue,
  384. .dequeue = dsmark_dequeue,
  385. .peek = dsmark_peek,
  386. .drop = dsmark_drop,
  387. .init = dsmark_init,
  388. .reset = dsmark_reset,
  389. .destroy = dsmark_destroy,
  390. .change = NULL,
  391. .dump = dsmark_dump,
  392. .owner = THIS_MODULE,
  393. };
  394. static int __init dsmark_module_init(void)
  395. {
  396. return register_qdisc(&dsmark_qdisc_ops);
  397. }
  398. static void __exit dsmark_module_exit(void)
  399. {
  400. unregister_qdisc(&dsmark_qdisc_ops);
  401. }
  402. module_init(dsmark_module_init)
  403. module_exit(dsmark_module_exit)
  404. MODULE_LICENSE("GPL");