sch_dsmark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 <net/pkt_sched.h>
  11. #include <net/dsfield.h>
  12. #include <net/inet_ecn.h>
  13. #include <asm/byteorder.h>
  14. #if 0 /* control */
  15. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  16. #else
  17. #define DPRINTK(format,args...)
  18. #endif
  19. #if 0 /* data */
  20. #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
  21. #else
  22. #define D2PRINTK(format,args...)
  23. #endif
  24. #define PRIV(sch) ((struct dsmark_qdisc_data *) qdisc_priv(sch))
  25. /*
  26. * classid class marking
  27. * ------- ----- -------
  28. * n/a 0 n/a
  29. * x:0 1 use entry [0]
  30. * ... ... ...
  31. * x:y y>0 y+1 use entry [y]
  32. * ... ... ...
  33. * x:indices-1 indices use entry [indices-1]
  34. * ... ... ...
  35. * x:y y+1 use entry [y & (indices-1)]
  36. * ... ... ...
  37. * 0xffff 0x10000 use entry [indices-1]
  38. */
  39. #define NO_DEFAULT_INDEX (1 << 16)
  40. struct dsmark_qdisc_data {
  41. struct Qdisc *q;
  42. struct tcf_proto *filter_list;
  43. u8 *mask; /* "owns" the array */
  44. u8 *value;
  45. u16 indices;
  46. u32 default_index; /* index range is 0...0xffff */
  47. int set_tc_index;
  48. };
  49. static inline int dsmark_valid_indices(u16 indices)
  50. {
  51. while (indices != 1) {
  52. if (indices & 1)
  53. return 0;
  54. indices >>= 1;
  55. }
  56. return 1;
  57. }
  58. static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index)
  59. {
  60. return (index <= p->indices && index > 0);
  61. }
  62. /* ------------------------- Class/flow operations ------------------------- */
  63. static int dsmark_graft(struct Qdisc *sch, unsigned long arg,
  64. struct Qdisc *new, struct Qdisc **old)
  65. {
  66. struct dsmark_qdisc_data *p = PRIV(sch);
  67. DPRINTK("dsmark_graft(sch %p,[qdisc %p],new %p,old %p)\n",
  68. sch, p, new, old);
  69. if (new == NULL) {
  70. new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
  71. sch->handle);
  72. if (new == NULL)
  73. new = &noop_qdisc;
  74. }
  75. sch_tree_lock(sch);
  76. *old = xchg(&p->q, new);
  77. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  78. qdisc_reset(*old);
  79. sch_tree_unlock(sch);
  80. return 0;
  81. }
  82. static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
  83. {
  84. return PRIV(sch)->q;
  85. }
  86. static unsigned long dsmark_get(struct Qdisc *sch, u32 classid)
  87. {
  88. DPRINTK("dsmark_get(sch %p,[qdisc %p],classid %x)\n",
  89. sch, PRIV(sch), classid);
  90. return TC_H_MIN(classid) + 1;
  91. }
  92. static unsigned long dsmark_bind_filter(struct Qdisc *sch,
  93. unsigned long parent, u32 classid)
  94. {
  95. return dsmark_get(sch, classid);
  96. }
  97. static void dsmark_put(struct Qdisc *sch, unsigned long cl)
  98. {
  99. }
  100. static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
  101. struct rtattr **tca, unsigned long *arg)
  102. {
  103. struct dsmark_qdisc_data *p = PRIV(sch);
  104. struct rtattr *opt = tca[TCA_OPTIONS-1];
  105. struct rtattr *tb[TCA_DSMARK_MAX];
  106. int err = -EINVAL;
  107. u8 mask = 0;
  108. DPRINTK("dsmark_change(sch %p,[qdisc %p],classid %x,parent %x),"
  109. "arg 0x%lx\n", sch, p, classid, parent, *arg);
  110. if (!dsmark_valid_index(p, *arg)) {
  111. err = -ENOENT;
  112. goto rtattr_failure;
  113. }
  114. if (!opt || rtattr_parse_nested(tb, TCA_DSMARK_MAX, opt))
  115. goto rtattr_failure;
  116. if (tb[TCA_DSMARK_MASK-1])
  117. mask = RTA_GET_U8(tb[TCA_DSMARK_MASK-1]);
  118. if (tb[TCA_DSMARK_VALUE-1])
  119. p->value[*arg-1] = RTA_GET_U8(tb[TCA_DSMARK_VALUE-1]);
  120. if (tb[TCA_DSMARK_MASK-1])
  121. p->mask[*arg-1] = mask;
  122. err = 0;
  123. rtattr_failure:
  124. return err;
  125. }
  126. static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
  127. {
  128. struct dsmark_qdisc_data *p = PRIV(sch);
  129. if (!dsmark_valid_index(p, arg))
  130. return -EINVAL;
  131. p->mask[arg-1] = 0xff;
  132. p->value[arg-1] = 0;
  133. return 0;
  134. }
  135. static void dsmark_walk(struct Qdisc *sch,struct qdisc_walker *walker)
  136. {
  137. struct dsmark_qdisc_data *p = PRIV(sch);
  138. int i;
  139. DPRINTK("dsmark_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
  140. if (walker->stop)
  141. return;
  142. for (i = 0; i < p->indices; i++) {
  143. if (p->mask[i] == 0xff && !p->value[i])
  144. goto ignore;
  145. if (walker->count >= walker->skip) {
  146. if (walker->fn(sch, i+1, walker) < 0) {
  147. walker->stop = 1;
  148. break;
  149. }
  150. }
  151. ignore:
  152. walker->count++;
  153. }
  154. }
  155. static struct tcf_proto **dsmark_find_tcf(struct Qdisc *sch,unsigned long cl)
  156. {
  157. return &PRIV(sch)->filter_list;
  158. }
  159. /* --------------------------- Qdisc operations ---------------------------- */
  160. static int dsmark_enqueue(struct sk_buff *skb,struct Qdisc *sch)
  161. {
  162. struct dsmark_qdisc_data *p = PRIV(sch);
  163. int err;
  164. D2PRINTK("dsmark_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
  165. if (p->set_tc_index) {
  166. /* FIXME: Safe with non-linear skbs? --RR */
  167. switch (skb->protocol) {
  168. case __constant_htons(ETH_P_IP):
  169. skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
  170. & ~INET_ECN_MASK;
  171. break;
  172. case __constant_htons(ETH_P_IPV6):
  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. D2PRINTK("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;
  193. case TC_ACT_SHOT:
  194. kfree_skb(skb);
  195. sch->qstats.drops++;
  196. return NET_XMIT_BYPASS;
  197. #endif
  198. case TC_ACT_OK:
  199. skb->tc_index = TC_H_MIN(res.classid);
  200. break;
  201. default:
  202. if (p->default_index != NO_DEFAULT_INDEX)
  203. skb->tc_index = p->default_index;
  204. break;
  205. }
  206. }
  207. err = p->q->enqueue(skb,p->q);
  208. if (err != NET_XMIT_SUCCESS) {
  209. sch->qstats.drops++;
  210. return err;
  211. }
  212. sch->bstats.bytes += skb->len;
  213. sch->bstats.packets++;
  214. sch->q.qlen++;
  215. return NET_XMIT_SUCCESS;
  216. }
  217. static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
  218. {
  219. struct dsmark_qdisc_data *p = PRIV(sch);
  220. struct sk_buff *skb;
  221. u32 index;
  222. D2PRINTK("dsmark_dequeue(sch %p,[qdisc %p])\n", sch, p);
  223. skb = p->q->ops->dequeue(p->q);
  224. if (skb == NULL)
  225. return NULL;
  226. sch->q.qlen--;
  227. index = skb->tc_index & (p->indices - 1);
  228. D2PRINTK("index %d->%d\n", skb->tc_index, index);
  229. switch (skb->protocol) {
  230. case __constant_htons(ETH_P_IP):
  231. ipv4_change_dsfield(ip_hdr(skb), p->mask[index],
  232. p->value[index]);
  233. break;
  234. case __constant_htons(ETH_P_IPV6):
  235. ipv6_change_dsfield(ipv6_hdr(skb), p->mask[index],
  236. p->value[index]);
  237. break;
  238. default:
  239. /*
  240. * Only complain if a change was actually attempted.
  241. * This way, we can send non-IP traffic through dsmark
  242. * and don't need yet another qdisc as a bypass.
  243. */
  244. if (p->mask[index] != 0xff || p->value[index])
  245. printk(KERN_WARNING "dsmark_dequeue: "
  246. "unsupported protocol %d\n",
  247. ntohs(skb->protocol));
  248. break;
  249. }
  250. return skb;
  251. }
  252. static int dsmark_requeue(struct sk_buff *skb,struct Qdisc *sch)
  253. {
  254. struct dsmark_qdisc_data *p = PRIV(sch);
  255. int err;
  256. D2PRINTK("dsmark_requeue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
  257. err = p->q->ops->requeue(skb, p->q);
  258. if (err != NET_XMIT_SUCCESS) {
  259. sch->qstats.drops++;
  260. return err;
  261. }
  262. sch->q.qlen++;
  263. sch->qstats.requeues++;
  264. return NET_XMIT_SUCCESS;
  265. }
  266. static unsigned int dsmark_drop(struct Qdisc *sch)
  267. {
  268. struct dsmark_qdisc_data *p = PRIV(sch);
  269. unsigned int len;
  270. DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n", sch, p);
  271. if (p->q->ops->drop == NULL)
  272. return 0;
  273. len = p->q->ops->drop(p->q);
  274. if (len)
  275. sch->q.qlen--;
  276. return len;
  277. }
  278. static int dsmark_init(struct Qdisc *sch, struct rtattr *opt)
  279. {
  280. struct dsmark_qdisc_data *p = PRIV(sch);
  281. struct rtattr *tb[TCA_DSMARK_MAX];
  282. int err = -EINVAL;
  283. u32 default_index = NO_DEFAULT_INDEX;
  284. u16 indices;
  285. u8 *mask;
  286. DPRINTK("dsmark_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
  287. if (!opt || rtattr_parse_nested(tb, TCA_DSMARK_MAX, opt) < 0)
  288. goto errout;
  289. indices = RTA_GET_U16(tb[TCA_DSMARK_INDICES-1]);
  290. if (!indices || !dsmark_valid_indices(indices))
  291. goto errout;
  292. if (tb[TCA_DSMARK_DEFAULT_INDEX-1])
  293. default_index = RTA_GET_U16(tb[TCA_DSMARK_DEFAULT_INDEX-1]);
  294. mask = kmalloc(indices * 2, GFP_KERNEL);
  295. if (mask == NULL) {
  296. err = -ENOMEM;
  297. goto errout;
  298. }
  299. p->mask = mask;
  300. memset(p->mask, 0xff, indices);
  301. p->value = p->mask + indices;
  302. memset(p->value, 0, indices);
  303. p->indices = indices;
  304. p->default_index = default_index;
  305. p->set_tc_index = RTA_GET_FLAG(tb[TCA_DSMARK_SET_TC_INDEX-1]);
  306. p->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, sch->handle);
  307. if (p->q == NULL)
  308. p->q = &noop_qdisc;
  309. DPRINTK("dsmark_init: qdisc %p\n", p->q);
  310. err = 0;
  311. errout:
  312. rtattr_failure:
  313. return err;
  314. }
  315. static void dsmark_reset(struct Qdisc *sch)
  316. {
  317. struct dsmark_qdisc_data *p = PRIV(sch);
  318. DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n", sch, p);
  319. qdisc_reset(p->q);
  320. sch->q.qlen = 0;
  321. }
  322. static void dsmark_destroy(struct Qdisc *sch)
  323. {
  324. struct dsmark_qdisc_data *p = PRIV(sch);
  325. DPRINTK("dsmark_destroy(sch %p,[qdisc %p])\n", sch, p);
  326. tcf_destroy_chain(p->filter_list);
  327. qdisc_destroy(p->q);
  328. kfree(p->mask);
  329. }
  330. static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
  331. struct sk_buff *skb, struct tcmsg *tcm)
  332. {
  333. struct dsmark_qdisc_data *p = PRIV(sch);
  334. struct rtattr *opts = NULL;
  335. DPRINTK("dsmark_dump_class(sch %p,[qdisc %p],class %ld\n", sch, p, cl);
  336. if (!dsmark_valid_index(p, cl))
  337. return -EINVAL;
  338. tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl-1);
  339. tcm->tcm_info = p->q->handle;
  340. opts = RTA_NEST(skb, TCA_OPTIONS);
  341. RTA_PUT_U8(skb,TCA_DSMARK_MASK, p->mask[cl-1]);
  342. RTA_PUT_U8(skb,TCA_DSMARK_VALUE, p->value[cl-1]);
  343. return RTA_NEST_END(skb, opts);
  344. rtattr_failure:
  345. return RTA_NEST_CANCEL(skb, opts);
  346. }
  347. static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
  348. {
  349. struct dsmark_qdisc_data *p = PRIV(sch);
  350. struct rtattr *opts = NULL;
  351. opts = RTA_NEST(skb, TCA_OPTIONS);
  352. RTA_PUT_U16(skb, TCA_DSMARK_INDICES, p->indices);
  353. if (p->default_index != NO_DEFAULT_INDEX)
  354. RTA_PUT_U16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index);
  355. if (p->set_tc_index)
  356. RTA_PUT_FLAG(skb, TCA_DSMARK_SET_TC_INDEX);
  357. return RTA_NEST_END(skb, opts);
  358. rtattr_failure:
  359. return RTA_NEST_CANCEL(skb, opts);
  360. }
  361. static struct Qdisc_class_ops dsmark_class_ops = {
  362. .graft = dsmark_graft,
  363. .leaf = dsmark_leaf,
  364. .get = dsmark_get,
  365. .put = dsmark_put,
  366. .change = dsmark_change,
  367. .delete = dsmark_delete,
  368. .walk = dsmark_walk,
  369. .tcf_chain = dsmark_find_tcf,
  370. .bind_tcf = dsmark_bind_filter,
  371. .unbind_tcf = dsmark_put,
  372. .dump = dsmark_dump_class,
  373. };
  374. static struct Qdisc_ops dsmark_qdisc_ops = {
  375. .next = NULL,
  376. .cl_ops = &dsmark_class_ops,
  377. .id = "dsmark",
  378. .priv_size = sizeof(struct dsmark_qdisc_data),
  379. .enqueue = dsmark_enqueue,
  380. .dequeue = dsmark_dequeue,
  381. .requeue = dsmark_requeue,
  382. .drop = dsmark_drop,
  383. .init = dsmark_init,
  384. .reset = dsmark_reset,
  385. .destroy = dsmark_destroy,
  386. .change = NULL,
  387. .dump = dsmark_dump,
  388. .owner = THIS_MODULE,
  389. };
  390. static int __init dsmark_module_init(void)
  391. {
  392. return register_qdisc(&dsmark_qdisc_ops);
  393. }
  394. static void __exit dsmark_module_exit(void)
  395. {
  396. unregister_qdisc(&dsmark_qdisc_ops);
  397. }
  398. module_init(dsmark_module_init)
  399. module_exit(dsmark_module_exit)
  400. MODULE_LICENSE("GPL");