sch_dsmark.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/slab.h>
  6. #include <linux/types.h>
  7. #include <linux/string.h>
  8. #include <linux/errno.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/rtnetlink.h>
  11. #include <linux/bitops.h>
  12. #include <net/pkt_sched.h>
  13. #include <net/pkt_cls.h>
  14. #include <net/dsfield.h>
  15. #include <net/inet_ecn.h>
  16. #include <asm/byteorder.h>
  17. /*
  18. * classid class marking
  19. * ------- ----- -------
  20. * n/a 0 n/a
  21. * x:0 1 use entry [0]
  22. * ... ... ...
  23. * x:y y>0 y+1 use entry [y]
  24. * ... ... ...
  25. * x:indices-1 indices use entry [indices-1]
  26. * ... ... ...
  27. * x:y y+1 use entry [y & (indices-1)]
  28. * ... ... ...
  29. * 0xffff 0x10000 use entry [indices-1]
  30. */
  31. #define NO_DEFAULT_INDEX (1 << 16)
  32. struct mask_value {
  33. u8 mask;
  34. u8 value;
  35. };
  36. struct dsmark_qdisc_data {
  37. struct Qdisc *q;
  38. struct tcf_proto __rcu *filter_list;
  39. struct tcf_block *block;
  40. struct mask_value *mv;
  41. u16 indices;
  42. u8 set_tc_index;
  43. u32 default_index; /* index range is 0...0xffff */
  44. #define DSMARK_EMBEDDED_SZ 16
  45. struct mask_value embedded[DSMARK_EMBEDDED_SZ];
  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("%s(sch %p,[qdisc %p],new %p,old %p)\n",
  57. __func__, sch, p, new, old);
  58. if (new == NULL) {
  59. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  60. sch->handle);
  61. if (new == NULL)
  62. new = &noop_qdisc;
  63. }
  64. *old = qdisc_replace(sch, new, &p->q);
  65. return 0;
  66. }
  67. static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
  68. {
  69. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  70. return p->q;
  71. }
  72. static unsigned long dsmark_find(struct Qdisc *sch, u32 classid)
  73. {
  74. return TC_H_MIN(classid) + 1;
  75. }
  76. static unsigned long dsmark_bind_filter(struct Qdisc *sch,
  77. unsigned long parent, u32 classid)
  78. {
  79. pr_debug("%s(sch %p,[qdisc %p],classid %x)\n",
  80. __func__, sch, qdisc_priv(sch), classid);
  81. return dsmark_find(sch, classid);
  82. }
  83. static void dsmark_unbind_filter(struct Qdisc *sch, unsigned long cl)
  84. {
  85. }
  86. static const struct nla_policy dsmark_policy[TCA_DSMARK_MAX + 1] = {
  87. [TCA_DSMARK_INDICES] = { .type = NLA_U16 },
  88. [TCA_DSMARK_DEFAULT_INDEX] = { .type = NLA_U16 },
  89. [TCA_DSMARK_SET_TC_INDEX] = { .type = NLA_FLAG },
  90. [TCA_DSMARK_MASK] = { .type = NLA_U8 },
  91. [TCA_DSMARK_VALUE] = { .type = NLA_U8 },
  92. };
  93. static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
  94. struct nlattr **tca, unsigned long *arg)
  95. {
  96. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  97. struct nlattr *opt = tca[TCA_OPTIONS];
  98. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  99. int err = -EINVAL;
  100. pr_debug("%s(sch %p,[qdisc %p],classid %x,parent %x), arg 0x%lx\n",
  101. __func__, sch, p, classid, parent, *arg);
  102. if (!dsmark_valid_index(p, *arg)) {
  103. err = -ENOENT;
  104. goto errout;
  105. }
  106. if (!opt)
  107. goto errout;
  108. err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy, NULL);
  109. if (err < 0)
  110. goto errout;
  111. if (tb[TCA_DSMARK_VALUE])
  112. p->mv[*arg - 1].value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
  113. if (tb[TCA_DSMARK_MASK])
  114. p->mv[*arg - 1].mask = nla_get_u8(tb[TCA_DSMARK_MASK]);
  115. err = 0;
  116. errout:
  117. return err;
  118. }
  119. static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
  120. {
  121. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  122. if (!dsmark_valid_index(p, arg))
  123. return -EINVAL;
  124. p->mv[arg - 1].mask = 0xff;
  125. p->mv[arg - 1].value = 0;
  126. return 0;
  127. }
  128. static void dsmark_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  129. {
  130. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  131. int i;
  132. pr_debug("%s(sch %p,[qdisc %p],walker %p)\n",
  133. __func__, sch, p, walker);
  134. if (walker->stop)
  135. return;
  136. for (i = 0; i < p->indices; i++) {
  137. if (p->mv[i].mask == 0xff && !p->mv[i].value)
  138. goto ignore;
  139. if (walker->count >= walker->skip) {
  140. if (walker->fn(sch, i + 1, walker) < 0) {
  141. walker->stop = 1;
  142. break;
  143. }
  144. }
  145. ignore:
  146. walker->count++;
  147. }
  148. }
  149. static struct tcf_block *dsmark_tcf_block(struct Qdisc *sch, unsigned long cl)
  150. {
  151. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  152. return p->block;
  153. }
  154. /* --------------------------- Qdisc operations ---------------------------- */
  155. static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  156. struct sk_buff **to_free)
  157. {
  158. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  159. int err;
  160. pr_debug("%s(skb %p,sch %p,[qdisc %p])\n", __func__, skb, sch, p);
  161. if (p->set_tc_index) {
  162. int wlen = skb_network_offset(skb);
  163. switch (tc_skb_protocol(skb)) {
  164. case htons(ETH_P_IP):
  165. wlen += sizeof(struct iphdr);
  166. if (!pskb_may_pull(skb, wlen) ||
  167. skb_try_make_writable(skb, wlen))
  168. goto drop;
  169. skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
  170. & ~INET_ECN_MASK;
  171. break;
  172. case htons(ETH_P_IPV6):
  173. wlen += sizeof(struct ipv6hdr);
  174. if (!pskb_may_pull(skb, wlen) ||
  175. skb_try_make_writable(skb, wlen))
  176. goto drop;
  177. skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb))
  178. & ~INET_ECN_MASK;
  179. break;
  180. default:
  181. skb->tc_index = 0;
  182. break;
  183. }
  184. }
  185. if (TC_H_MAJ(skb->priority) == sch->handle)
  186. skb->tc_index = TC_H_MIN(skb->priority);
  187. else {
  188. struct tcf_result res;
  189. struct tcf_proto *fl = rcu_dereference_bh(p->filter_list);
  190. int result = tcf_classify(skb, fl, &res, false);
  191. pr_debug("result %d class 0x%04x\n", result, res.classid);
  192. switch (result) {
  193. #ifdef CONFIG_NET_CLS_ACT
  194. case TC_ACT_QUEUED:
  195. case TC_ACT_STOLEN:
  196. case TC_ACT_TRAP:
  197. __qdisc_drop(skb, to_free);
  198. return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  199. case TC_ACT_SHOT:
  200. goto drop;
  201. #endif
  202. case TC_ACT_OK:
  203. skb->tc_index = TC_H_MIN(res.classid);
  204. break;
  205. default:
  206. if (p->default_index != NO_DEFAULT_INDEX)
  207. skb->tc_index = p->default_index;
  208. break;
  209. }
  210. }
  211. err = qdisc_enqueue(skb, p->q, to_free);
  212. if (err != NET_XMIT_SUCCESS) {
  213. if (net_xmit_drop_count(err))
  214. qdisc_qstats_drop(sch);
  215. return err;
  216. }
  217. qdisc_qstats_backlog_inc(sch, skb);
  218. sch->q.qlen++;
  219. return NET_XMIT_SUCCESS;
  220. drop:
  221. qdisc_drop(skb, sch, to_free);
  222. return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  223. }
  224. static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
  225. {
  226. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  227. struct sk_buff *skb;
  228. u32 index;
  229. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  230. skb = qdisc_dequeue_peeked(p->q);
  231. if (skb == NULL)
  232. return NULL;
  233. qdisc_bstats_update(sch, skb);
  234. qdisc_qstats_backlog_dec(sch, skb);
  235. sch->q.qlen--;
  236. index = skb->tc_index & (p->indices - 1);
  237. pr_debug("index %d->%d\n", skb->tc_index, index);
  238. switch (tc_skb_protocol(skb)) {
  239. case htons(ETH_P_IP):
  240. ipv4_change_dsfield(ip_hdr(skb), p->mv[index].mask,
  241. p->mv[index].value);
  242. break;
  243. case htons(ETH_P_IPV6):
  244. ipv6_change_dsfield(ipv6_hdr(skb), p->mv[index].mask,
  245. p->mv[index].value);
  246. break;
  247. default:
  248. /*
  249. * Only complain if a change was actually attempted.
  250. * This way, we can send non-IP traffic through dsmark
  251. * and don't need yet another qdisc as a bypass.
  252. */
  253. if (p->mv[index].mask != 0xff || p->mv[index].value)
  254. pr_warn("%s: unsupported protocol %d\n",
  255. __func__, ntohs(tc_skb_protocol(skb)));
  256. break;
  257. }
  258. return skb;
  259. }
  260. static struct sk_buff *dsmark_peek(struct Qdisc *sch)
  261. {
  262. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  263. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  264. return p->q->ops->peek(p->q);
  265. }
  266. static int dsmark_init(struct Qdisc *sch, struct nlattr *opt)
  267. {
  268. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  269. struct nlattr *tb[TCA_DSMARK_MAX + 1];
  270. int err = -EINVAL;
  271. u32 default_index = NO_DEFAULT_INDEX;
  272. u16 indices;
  273. int i;
  274. pr_debug("%s(sch %p,[qdisc %p],opt %p)\n", __func__, sch, p, opt);
  275. if (!opt)
  276. goto errout;
  277. err = tcf_block_get(&p->block, &p->filter_list);
  278. if (err)
  279. return err;
  280. err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy, NULL);
  281. if (err < 0)
  282. goto errout;
  283. err = -EINVAL;
  284. if (!tb[TCA_DSMARK_INDICES])
  285. goto errout;
  286. indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
  287. if (hweight32(indices) != 1)
  288. goto errout;
  289. if (tb[TCA_DSMARK_DEFAULT_INDEX])
  290. default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
  291. if (indices <= DSMARK_EMBEDDED_SZ)
  292. p->mv = p->embedded;
  293. else
  294. p->mv = kmalloc_array(indices, sizeof(*p->mv), GFP_KERNEL);
  295. if (!p->mv) {
  296. err = -ENOMEM;
  297. goto errout;
  298. }
  299. for (i = 0; i < indices; i++) {
  300. p->mv[i].mask = 0xff;
  301. p->mv[i].value = 0;
  302. }
  303. p->indices = indices;
  304. p->default_index = default_index;
  305. p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
  306. p->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, sch->handle);
  307. if (p->q == NULL)
  308. p->q = &noop_qdisc;
  309. else
  310. qdisc_hash_add(p->q, true);
  311. pr_debug("%s: qdisc %p\n", __func__, p->q);
  312. err = 0;
  313. errout:
  314. return err;
  315. }
  316. static void dsmark_reset(struct Qdisc *sch)
  317. {
  318. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  319. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  320. if (p->q)
  321. qdisc_reset(p->q);
  322. sch->qstats.backlog = 0;
  323. sch->q.qlen = 0;
  324. }
  325. static void dsmark_destroy(struct Qdisc *sch)
  326. {
  327. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  328. pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
  329. tcf_block_put(p->block);
  330. qdisc_destroy(p->q);
  331. if (p->mv != p->embedded)
  332. kfree(p->mv);
  333. }
  334. static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
  335. struct sk_buff *skb, struct tcmsg *tcm)
  336. {
  337. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  338. struct nlattr *opts = NULL;
  339. pr_debug("%s(sch %p,[qdisc %p],class %ld\n", __func__, sch, p, cl);
  340. if (!dsmark_valid_index(p, cl))
  341. return -EINVAL;
  342. tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl - 1);
  343. tcm->tcm_info = p->q->handle;
  344. opts = nla_nest_start(skb, TCA_OPTIONS);
  345. if (opts == NULL)
  346. goto nla_put_failure;
  347. if (nla_put_u8(skb, TCA_DSMARK_MASK, p->mv[cl - 1].mask) ||
  348. nla_put_u8(skb, TCA_DSMARK_VALUE, p->mv[cl - 1].value))
  349. goto nla_put_failure;
  350. return nla_nest_end(skb, opts);
  351. nla_put_failure:
  352. nla_nest_cancel(skb, opts);
  353. return -EMSGSIZE;
  354. }
  355. static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
  356. {
  357. struct dsmark_qdisc_data *p = qdisc_priv(sch);
  358. struct nlattr *opts = NULL;
  359. opts = nla_nest_start(skb, TCA_OPTIONS);
  360. if (opts == NULL)
  361. goto nla_put_failure;
  362. if (nla_put_u16(skb, TCA_DSMARK_INDICES, p->indices))
  363. goto nla_put_failure;
  364. if (p->default_index != NO_DEFAULT_INDEX &&
  365. nla_put_u16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index))
  366. goto nla_put_failure;
  367. if (p->set_tc_index &&
  368. nla_put_flag(skb, TCA_DSMARK_SET_TC_INDEX))
  369. goto nla_put_failure;
  370. return nla_nest_end(skb, opts);
  371. nla_put_failure:
  372. nla_nest_cancel(skb, opts);
  373. return -EMSGSIZE;
  374. }
  375. static const struct Qdisc_class_ops dsmark_class_ops = {
  376. .graft = dsmark_graft,
  377. .leaf = dsmark_leaf,
  378. .find = dsmark_find,
  379. .change = dsmark_change,
  380. .delete = dsmark_delete,
  381. .walk = dsmark_walk,
  382. .tcf_block = dsmark_tcf_block,
  383. .bind_tcf = dsmark_bind_filter,
  384. .unbind_tcf = dsmark_unbind_filter,
  385. .dump = dsmark_dump_class,
  386. };
  387. static struct Qdisc_ops dsmark_qdisc_ops __read_mostly = {
  388. .next = NULL,
  389. .cl_ops = &dsmark_class_ops,
  390. .id = "dsmark",
  391. .priv_size = sizeof(struct dsmark_qdisc_data),
  392. .enqueue = dsmark_enqueue,
  393. .dequeue = dsmark_dequeue,
  394. .peek = dsmark_peek,
  395. .init = dsmark_init,
  396. .reset = dsmark_reset,
  397. .destroy = dsmark_destroy,
  398. .change = NULL,
  399. .dump = dsmark_dump,
  400. .owner = THIS_MODULE,
  401. };
  402. static int __init dsmark_module_init(void)
  403. {
  404. return register_qdisc(&dsmark_qdisc_ops);
  405. }
  406. static void __exit dsmark_module_exit(void)
  407. {
  408. unregister_qdisc(&dsmark_qdisc_ops);
  409. }
  410. module_init(dsmark_module_init)
  411. module_exit(dsmark_module_exit)
  412. MODULE_LICENSE("GPL");