sch_dsmark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* net/sched/sch_dsmark.c - Differentiated Services field marker */
  2. /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
  3. #include <linux/config.h>
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/types.h>
  7. #include <linux/string.h>
  8. #include <linux/errno.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/netdevice.h> /* for pkt_sched */
  11. #include <linux/rtnetlink.h>
  12. #include <net/pkt_sched.h>
  13. #include <net/dsfield.h>
  14. #include <net/inet_ecn.h>
  15. #include <asm/byteorder.h>
  16. #if 1 /* control */
  17. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  18. #else
  19. #define DPRINTK(format,args...)
  20. #endif
  21. #if 0 /* data */
  22. #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
  23. #else
  24. #define D2PRINTK(format,args...)
  25. #endif
  26. #define PRIV(sch) qdisc_priv(sch)
  27. /*
  28. * classid class marking
  29. * ------- ----- -------
  30. * n/a 0 n/a
  31. * x:0 1 use entry [0]
  32. * ... ... ...
  33. * x:y y>0 y+1 use entry [y]
  34. * ... ... ...
  35. * x:indices-1 indices use entry [indices-1]
  36. * ... ... ...
  37. * x:y y+1 use entry [y & (indices-1)]
  38. * ... ... ...
  39. * 0xffff 0x10000 use entry [indices-1]
  40. */
  41. #define NO_DEFAULT_INDEX (1 << 16)
  42. struct dsmark_qdisc_data {
  43. struct Qdisc *q;
  44. struct tcf_proto *filter_list;
  45. __u8 *mask; /* "owns" the array */
  46. __u8 *value;
  47. __u16 indices;
  48. __u32 default_index; /* index range is 0...0xffff */
  49. int set_tc_index;
  50. };
  51. /* ------------------------- Class/flow operations ------------------------- */
  52. static int dsmark_graft(struct Qdisc *sch,unsigned long arg,
  53. struct Qdisc *new,struct Qdisc **old)
  54. {
  55. struct dsmark_qdisc_data *p = PRIV(sch);
  56. DPRINTK("dsmark_graft(sch %p,[qdisc %p],new %p,old %p)\n",sch,p,new,
  57. old);
  58. if (!new)
  59. new = &noop_qdisc;
  60. sch_tree_lock(sch);
  61. *old = xchg(&p->q,new);
  62. if (*old)
  63. qdisc_reset(*old);
  64. sch->q.qlen = 0;
  65. sch_tree_unlock(sch); /* @@@ move up ? */
  66. return 0;
  67. }
  68. static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
  69. {
  70. struct dsmark_qdisc_data *p = PRIV(sch);
  71. return p->q;
  72. }
  73. static unsigned long dsmark_get(struct Qdisc *sch,u32 classid)
  74. {
  75. struct dsmark_qdisc_data *p __attribute__((unused)) = PRIV(sch);
  76. DPRINTK("dsmark_get(sch %p,[qdisc %p],classid %x)\n",sch,p,classid);
  77. return TC_H_MIN(classid)+1;
  78. }
  79. static unsigned long dsmark_bind_filter(struct Qdisc *sch,
  80. unsigned long parent, u32 classid)
  81. {
  82. return dsmark_get(sch,classid);
  83. }
  84. static void dsmark_put(struct Qdisc *sch, unsigned long cl)
  85. {
  86. }
  87. static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
  88. struct rtattr **tca, unsigned long *arg)
  89. {
  90. struct dsmark_qdisc_data *p = PRIV(sch);
  91. struct rtattr *opt = tca[TCA_OPTIONS-1];
  92. struct rtattr *tb[TCA_DSMARK_MAX];
  93. DPRINTK("dsmark_change(sch %p,[qdisc %p],classid %x,parent %x),"
  94. "arg 0x%lx\n",sch,p,classid,parent,*arg);
  95. if (*arg > p->indices)
  96. return -ENOENT;
  97. if (!opt || rtattr_parse_nested(tb, TCA_DSMARK_MAX, opt))
  98. return -EINVAL;
  99. if (tb[TCA_DSMARK_MASK-1]) {
  100. if (!RTA_PAYLOAD(tb[TCA_DSMARK_MASK-1]))
  101. return -EINVAL;
  102. p->mask[*arg-1] = *(__u8 *) RTA_DATA(tb[TCA_DSMARK_MASK-1]);
  103. }
  104. if (tb[TCA_DSMARK_VALUE-1]) {
  105. if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE-1]))
  106. return -EINVAL;
  107. p->value[*arg-1] = *(__u8 *) RTA_DATA(tb[TCA_DSMARK_VALUE-1]);
  108. }
  109. return 0;
  110. }
  111. static int dsmark_delete(struct Qdisc *sch,unsigned long arg)
  112. {
  113. struct dsmark_qdisc_data *p = PRIV(sch);
  114. if (!arg || arg > p->indices)
  115. return -EINVAL;
  116. p->mask[arg-1] = 0xff;
  117. p->value[arg-1] = 0;
  118. return 0;
  119. }
  120. static void dsmark_walk(struct Qdisc *sch,struct qdisc_walker *walker)
  121. {
  122. struct dsmark_qdisc_data *p = PRIV(sch);
  123. int i;
  124. DPRINTK("dsmark_walk(sch %p,[qdisc %p],walker %p)\n",sch,p,walker);
  125. if (walker->stop)
  126. return;
  127. for (i = 0; i < p->indices; i++) {
  128. if (p->mask[i] == 0xff && !p->value[i])
  129. goto ignore;
  130. if (walker->count >= walker->skip) {
  131. if (walker->fn(sch, i+1, walker) < 0) {
  132. walker->stop = 1;
  133. break;
  134. }
  135. }
  136. ignore:
  137. walker->count++;
  138. }
  139. }
  140. static struct tcf_proto **dsmark_find_tcf(struct Qdisc *sch,unsigned long cl)
  141. {
  142. struct dsmark_qdisc_data *p = PRIV(sch);
  143. return &p->filter_list;
  144. }
  145. /* --------------------------- Qdisc operations ---------------------------- */
  146. static int dsmark_enqueue(struct sk_buff *skb,struct Qdisc *sch)
  147. {
  148. struct dsmark_qdisc_data *p = PRIV(sch);
  149. struct tcf_result res;
  150. int result;
  151. int ret = NET_XMIT_POLICED;
  152. D2PRINTK("dsmark_enqueue(skb %p,sch %p,[qdisc %p])\n",skb,sch,p);
  153. if (p->set_tc_index) {
  154. /* FIXME: Safe with non-linear skbs? --RR */
  155. switch (skb->protocol) {
  156. case __constant_htons(ETH_P_IP):
  157. skb->tc_index = ipv4_get_dsfield(skb->nh.iph)
  158. & ~INET_ECN_MASK;
  159. break;
  160. case __constant_htons(ETH_P_IPV6):
  161. skb->tc_index = ipv6_get_dsfield(skb->nh.ipv6h)
  162. & ~INET_ECN_MASK;
  163. break;
  164. default:
  165. skb->tc_index = 0;
  166. break;
  167. };
  168. }
  169. result = TC_POLICE_OK; /* be nice to gcc */
  170. if (TC_H_MAJ(skb->priority) == sch->handle) {
  171. skb->tc_index = TC_H_MIN(skb->priority);
  172. } else {
  173. result = tc_classify(skb,p->filter_list,&res);
  174. D2PRINTK("result %d class 0x%04x\n",result,res.classid);
  175. switch (result) {
  176. #ifdef CONFIG_NET_CLS_POLICE
  177. case TC_POLICE_SHOT:
  178. kfree_skb(skb);
  179. break;
  180. #if 0
  181. case TC_POLICE_RECLASSIFY:
  182. /* FIXME: what to do here ??? */
  183. #endif
  184. #endif
  185. case TC_POLICE_OK:
  186. skb->tc_index = TC_H_MIN(res.classid);
  187. break;
  188. case TC_POLICE_UNSPEC:
  189. /* fall through */
  190. default:
  191. if (p->default_index != NO_DEFAULT_INDEX)
  192. skb->tc_index = p->default_index;
  193. break;
  194. };
  195. }
  196. if (
  197. #ifdef CONFIG_NET_CLS_POLICE
  198. result == TC_POLICE_SHOT ||
  199. #endif
  200. ((ret = p->q->enqueue(skb,p->q)) != 0)) {
  201. sch->qstats.drops++;
  202. return ret;
  203. }
  204. sch->bstats.bytes += skb->len;
  205. sch->bstats.packets++;
  206. sch->q.qlen++;
  207. return ret;
  208. }
  209. static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
  210. {
  211. struct dsmark_qdisc_data *p = PRIV(sch);
  212. struct sk_buff *skb;
  213. int index;
  214. D2PRINTK("dsmark_dequeue(sch %p,[qdisc %p])\n",sch,p);
  215. skb = p->q->ops->dequeue(p->q);
  216. if (!skb)
  217. return NULL;
  218. sch->q.qlen--;
  219. index = skb->tc_index & (p->indices-1);
  220. D2PRINTK("index %d->%d\n",skb->tc_index,index);
  221. switch (skb->protocol) {
  222. case __constant_htons(ETH_P_IP):
  223. ipv4_change_dsfield(skb->nh.iph,
  224. p->mask[index],p->value[index]);
  225. break;
  226. case __constant_htons(ETH_P_IPV6):
  227. ipv6_change_dsfield(skb->nh.ipv6h,
  228. p->mask[index],p->value[index]);
  229. break;
  230. default:
  231. /*
  232. * Only complain if a change was actually attempted.
  233. * This way, we can send non-IP traffic through dsmark
  234. * and don't need yet another qdisc as a bypass.
  235. */
  236. if (p->mask[index] != 0xff || p->value[index])
  237. printk(KERN_WARNING "dsmark_dequeue: "
  238. "unsupported protocol %d\n",
  239. htons(skb->protocol));
  240. break;
  241. };
  242. return skb;
  243. }
  244. static int dsmark_requeue(struct sk_buff *skb,struct Qdisc *sch)
  245. {
  246. int ret;
  247. struct dsmark_qdisc_data *p = PRIV(sch);
  248. D2PRINTK("dsmark_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,p);
  249. if ((ret = p->q->ops->requeue(skb, p->q)) == 0) {
  250. sch->q.qlen++;
  251. sch->qstats.requeues++;
  252. return 0;
  253. }
  254. sch->qstats.drops++;
  255. return ret;
  256. }
  257. static unsigned int dsmark_drop(struct Qdisc *sch)
  258. {
  259. struct dsmark_qdisc_data *p = PRIV(sch);
  260. unsigned int len;
  261. DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n",sch,p);
  262. if (!p->q->ops->drop)
  263. return 0;
  264. if (!(len = p->q->ops->drop(p->q)))
  265. return 0;
  266. sch->q.qlen--;
  267. return len;
  268. }
  269. static int dsmark_init(struct Qdisc *sch,struct rtattr *opt)
  270. {
  271. struct dsmark_qdisc_data *p = PRIV(sch);
  272. struct rtattr *tb[TCA_DSMARK_MAX];
  273. __u16 tmp;
  274. DPRINTK("dsmark_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
  275. if (!opt ||
  276. rtattr_parse(tb,TCA_DSMARK_MAX,RTA_DATA(opt),RTA_PAYLOAD(opt)) < 0 ||
  277. !tb[TCA_DSMARK_INDICES-1] ||
  278. RTA_PAYLOAD(tb[TCA_DSMARK_INDICES-1]) < sizeof(__u16))
  279. return -EINVAL;
  280. p->indices = *(__u16 *) RTA_DATA(tb[TCA_DSMARK_INDICES-1]);
  281. if (!p->indices)
  282. return -EINVAL;
  283. for (tmp = p->indices; tmp != 1; tmp >>= 1) {
  284. if (tmp & 1)
  285. return -EINVAL;
  286. }
  287. p->default_index = NO_DEFAULT_INDEX;
  288. if (tb[TCA_DSMARK_DEFAULT_INDEX-1]) {
  289. if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX-1]) < sizeof(__u16))
  290. return -EINVAL;
  291. p->default_index =
  292. *(__u16 *) RTA_DATA(tb[TCA_DSMARK_DEFAULT_INDEX-1]);
  293. }
  294. p->set_tc_index = !!tb[TCA_DSMARK_SET_TC_INDEX-1];
  295. p->mask = kmalloc(p->indices*2,GFP_KERNEL);
  296. if (!p->mask)
  297. return -ENOMEM;
  298. p->value = p->mask+p->indices;
  299. memset(p->mask,0xff,p->indices);
  300. memset(p->value,0,p->indices);
  301. if (!(p->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops)))
  302. p->q = &noop_qdisc;
  303. DPRINTK("dsmark_init: qdisc %p\n",&p->q);
  304. return 0;
  305. }
  306. static void dsmark_reset(struct Qdisc *sch)
  307. {
  308. struct dsmark_qdisc_data *p = PRIV(sch);
  309. DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n",sch,p);
  310. qdisc_reset(p->q);
  311. sch->q.qlen = 0;
  312. }
  313. static void dsmark_destroy(struct Qdisc *sch)
  314. {
  315. struct dsmark_qdisc_data *p = PRIV(sch);
  316. struct tcf_proto *tp;
  317. DPRINTK("dsmark_destroy(sch %p,[qdisc %p])\n",sch,p);
  318. while (p->filter_list) {
  319. tp = p->filter_list;
  320. p->filter_list = tp->next;
  321. tcf_destroy(tp);
  322. }
  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 = PRIV(sch);
  330. unsigned char *b = skb->tail;
  331. struct rtattr *rta;
  332. DPRINTK("dsmark_dump_class(sch %p,[qdisc %p],class %ld\n",sch,p,cl);
  333. if (!cl || cl > p->indices)
  334. return -EINVAL;
  335. tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle),cl-1);
  336. rta = (struct rtattr *) b;
  337. RTA_PUT(skb,TCA_OPTIONS,0,NULL);
  338. RTA_PUT(skb,TCA_DSMARK_MASK,1,&p->mask[cl-1]);
  339. RTA_PUT(skb,TCA_DSMARK_VALUE,1,&p->value[cl-1]);
  340. rta->rta_len = skb->tail-b;
  341. return skb->len;
  342. rtattr_failure:
  343. skb_trim(skb,b-skb->data);
  344. return -1;
  345. }
  346. static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
  347. {
  348. struct dsmark_qdisc_data *p = PRIV(sch);
  349. unsigned char *b = skb->tail;
  350. struct rtattr *rta;
  351. rta = (struct rtattr *) b;
  352. RTA_PUT(skb,TCA_OPTIONS,0,NULL);
  353. RTA_PUT(skb,TCA_DSMARK_INDICES,sizeof(__u16),&p->indices);
  354. if (p->default_index != NO_DEFAULT_INDEX) {
  355. __u16 tmp = p->default_index;
  356. RTA_PUT(skb,TCA_DSMARK_DEFAULT_INDEX, sizeof(__u16), &tmp);
  357. }
  358. if (p->set_tc_index)
  359. RTA_PUT(skb, TCA_DSMARK_SET_TC_INDEX, 0, NULL);
  360. rta->rta_len = skb->tail-b;
  361. return skb->len;
  362. rtattr_failure:
  363. skb_trim(skb,b-skb->data);
  364. return -1;
  365. }
  366. static 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 = {
  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. .requeue = dsmark_requeue,
  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");