sch_dsmark.c 11 KB

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