sch_dsmark.c 12 KB

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