sch_dsmark.c 11 KB

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