sch_dsmark.c 12 KB

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