sch_dsmark.c 11 KB

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