sch_skbprio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * net/sched/sch_skbprio.c SKB Priority Queue.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Nishanth Devarajan, <ndev2021@gmail.com>
  10. * Cody Doucette, <doucette@bu.edu>
  11. * original idea by Michel Machado, Cody Doucette, and Qiaobin Fu
  12. */
  13. #include <linux/string.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/skbuff.h>
  20. #include <net/pkt_sched.h>
  21. #include <net/sch_generic.h>
  22. #include <net/inet_ecn.h>
  23. /* SKB Priority Queue
  24. * =================================
  25. *
  26. * Skbprio (SKB Priority Queue) is a queueing discipline that prioritizes
  27. * packets according to their skb->priority field. Under congestion,
  28. * Skbprio drops already-enqueued lower priority packets to make space
  29. * available for higher priority packets; it was conceived as a solution
  30. * for denial-of-service defenses that need to route packets with different
  31. * priorities as a mean to overcome DoS attacks.
  32. */
  33. struct skbprio_sched_data {
  34. /* Queue state. */
  35. struct sk_buff_head qdiscs[SKBPRIO_MAX_PRIORITY];
  36. struct gnet_stats_queue qstats[SKBPRIO_MAX_PRIORITY];
  37. u16 highest_prio;
  38. u16 lowest_prio;
  39. };
  40. static u16 calc_new_high_prio(const struct skbprio_sched_data *q)
  41. {
  42. int prio;
  43. for (prio = q->highest_prio - 1; prio >= q->lowest_prio; prio--) {
  44. if (!skb_queue_empty(&q->qdiscs[prio]))
  45. return prio;
  46. }
  47. /* SKB queue is empty, return 0 (default highest priority setting). */
  48. return 0;
  49. }
  50. static u16 calc_new_low_prio(const struct skbprio_sched_data *q)
  51. {
  52. int prio;
  53. for (prio = q->lowest_prio + 1; prio <= q->highest_prio; prio++) {
  54. if (!skb_queue_empty(&q->qdiscs[prio]))
  55. return prio;
  56. }
  57. /* SKB queue is empty, return SKBPRIO_MAX_PRIORITY - 1
  58. * (default lowest priority setting).
  59. */
  60. return SKBPRIO_MAX_PRIORITY - 1;
  61. }
  62. static int skbprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  63. struct sk_buff **to_free)
  64. {
  65. const unsigned int max_priority = SKBPRIO_MAX_PRIORITY - 1;
  66. struct skbprio_sched_data *q = qdisc_priv(sch);
  67. struct sk_buff_head *qdisc;
  68. struct sk_buff_head *lp_qdisc;
  69. struct sk_buff *to_drop;
  70. u16 prio, lp;
  71. /* Obtain the priority of @skb. */
  72. prio = min(skb->priority, max_priority);
  73. qdisc = &q->qdiscs[prio];
  74. if (sch->q.qlen < sch->limit) {
  75. __skb_queue_tail(qdisc, skb);
  76. qdisc_qstats_backlog_inc(sch, skb);
  77. q->qstats[prio].backlog += qdisc_pkt_len(skb);
  78. /* Check to update highest and lowest priorities. */
  79. if (prio > q->highest_prio)
  80. q->highest_prio = prio;
  81. if (prio < q->lowest_prio)
  82. q->lowest_prio = prio;
  83. sch->q.qlen++;
  84. return NET_XMIT_SUCCESS;
  85. }
  86. /* If this packet has the lowest priority, drop it. */
  87. lp = q->lowest_prio;
  88. if (prio <= lp) {
  89. q->qstats[prio].drops++;
  90. q->qstats[prio].overlimits++;
  91. return qdisc_drop(skb, sch, to_free);
  92. }
  93. __skb_queue_tail(qdisc, skb);
  94. qdisc_qstats_backlog_inc(sch, skb);
  95. q->qstats[prio].backlog += qdisc_pkt_len(skb);
  96. /* Drop the packet at the tail of the lowest priority qdisc. */
  97. lp_qdisc = &q->qdiscs[lp];
  98. to_drop = __skb_dequeue_tail(lp_qdisc);
  99. BUG_ON(!to_drop);
  100. qdisc_qstats_backlog_dec(sch, to_drop);
  101. qdisc_drop(to_drop, sch, to_free);
  102. q->qstats[lp].backlog -= qdisc_pkt_len(to_drop);
  103. q->qstats[lp].drops++;
  104. q->qstats[lp].overlimits++;
  105. /* Check to update highest and lowest priorities. */
  106. if (skb_queue_empty(lp_qdisc)) {
  107. if (q->lowest_prio == q->highest_prio) {
  108. /* The incoming packet is the only packet in queue. */
  109. BUG_ON(sch->q.qlen != 1);
  110. q->lowest_prio = prio;
  111. q->highest_prio = prio;
  112. } else {
  113. q->lowest_prio = calc_new_low_prio(q);
  114. }
  115. }
  116. if (prio > q->highest_prio)
  117. q->highest_prio = prio;
  118. return NET_XMIT_CN;
  119. }
  120. static struct sk_buff *skbprio_dequeue(struct Qdisc *sch)
  121. {
  122. struct skbprio_sched_data *q = qdisc_priv(sch);
  123. struct sk_buff_head *hpq = &q->qdiscs[q->highest_prio];
  124. struct sk_buff *skb = __skb_dequeue(hpq);
  125. if (unlikely(!skb))
  126. return NULL;
  127. sch->q.qlen--;
  128. qdisc_qstats_backlog_dec(sch, skb);
  129. qdisc_bstats_update(sch, skb);
  130. q->qstats[q->highest_prio].backlog -= qdisc_pkt_len(skb);
  131. /* Update highest priority field. */
  132. if (skb_queue_empty(hpq)) {
  133. if (q->lowest_prio == q->highest_prio) {
  134. BUG_ON(sch->q.qlen);
  135. q->highest_prio = 0;
  136. q->lowest_prio = SKBPRIO_MAX_PRIORITY - 1;
  137. } else {
  138. q->highest_prio = calc_new_high_prio(q);
  139. }
  140. }
  141. return skb;
  142. }
  143. static int skbprio_change(struct Qdisc *sch, struct nlattr *opt,
  144. struct netlink_ext_ack *extack)
  145. {
  146. struct tc_skbprio_qopt *ctl = nla_data(opt);
  147. sch->limit = ctl->limit;
  148. return 0;
  149. }
  150. static int skbprio_init(struct Qdisc *sch, struct nlattr *opt,
  151. struct netlink_ext_ack *extack)
  152. {
  153. struct skbprio_sched_data *q = qdisc_priv(sch);
  154. int prio;
  155. /* Initialise all queues, one for each possible priority. */
  156. for (prio = 0; prio < SKBPRIO_MAX_PRIORITY; prio++)
  157. __skb_queue_head_init(&q->qdiscs[prio]);
  158. memset(&q->qstats, 0, sizeof(q->qstats));
  159. q->highest_prio = 0;
  160. q->lowest_prio = SKBPRIO_MAX_PRIORITY - 1;
  161. sch->limit = 64;
  162. if (!opt)
  163. return 0;
  164. return skbprio_change(sch, opt, extack);
  165. }
  166. static int skbprio_dump(struct Qdisc *sch, struct sk_buff *skb)
  167. {
  168. struct tc_skbprio_qopt opt;
  169. opt.limit = sch->limit;
  170. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  171. return -1;
  172. return skb->len;
  173. }
  174. static void skbprio_reset(struct Qdisc *sch)
  175. {
  176. struct skbprio_sched_data *q = qdisc_priv(sch);
  177. int prio;
  178. sch->qstats.backlog = 0;
  179. sch->q.qlen = 0;
  180. for (prio = 0; prio < SKBPRIO_MAX_PRIORITY; prio++)
  181. __skb_queue_purge(&q->qdiscs[prio]);
  182. memset(&q->qstats, 0, sizeof(q->qstats));
  183. q->highest_prio = 0;
  184. q->lowest_prio = SKBPRIO_MAX_PRIORITY - 1;
  185. }
  186. static void skbprio_destroy(struct Qdisc *sch)
  187. {
  188. struct skbprio_sched_data *q = qdisc_priv(sch);
  189. int prio;
  190. for (prio = 0; prio < SKBPRIO_MAX_PRIORITY; prio++)
  191. __skb_queue_purge(&q->qdiscs[prio]);
  192. }
  193. static struct Qdisc *skbprio_leaf(struct Qdisc *sch, unsigned long arg)
  194. {
  195. return NULL;
  196. }
  197. static unsigned long skbprio_find(struct Qdisc *sch, u32 classid)
  198. {
  199. return 0;
  200. }
  201. static int skbprio_dump_class(struct Qdisc *sch, unsigned long cl,
  202. struct sk_buff *skb, struct tcmsg *tcm)
  203. {
  204. tcm->tcm_handle |= TC_H_MIN(cl);
  205. return 0;
  206. }
  207. static int skbprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  208. struct gnet_dump *d)
  209. {
  210. struct skbprio_sched_data *q = qdisc_priv(sch);
  211. if (gnet_stats_copy_queue(d, NULL, &q->qstats[cl - 1],
  212. q->qstats[cl - 1].qlen) < 0)
  213. return -1;
  214. return 0;
  215. }
  216. static void skbprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  217. {
  218. unsigned int i;
  219. if (arg->stop)
  220. return;
  221. for (i = 0; i < SKBPRIO_MAX_PRIORITY; i++) {
  222. if (arg->count < arg->skip) {
  223. arg->count++;
  224. continue;
  225. }
  226. if (arg->fn(sch, i + 1, arg) < 0) {
  227. arg->stop = 1;
  228. break;
  229. }
  230. arg->count++;
  231. }
  232. }
  233. static const struct Qdisc_class_ops skbprio_class_ops = {
  234. .leaf = skbprio_leaf,
  235. .find = skbprio_find,
  236. .dump = skbprio_dump_class,
  237. .dump_stats = skbprio_dump_class_stats,
  238. .walk = skbprio_walk,
  239. };
  240. static struct Qdisc_ops skbprio_qdisc_ops __read_mostly = {
  241. .cl_ops = &skbprio_class_ops,
  242. .id = "skbprio",
  243. .priv_size = sizeof(struct skbprio_sched_data),
  244. .enqueue = skbprio_enqueue,
  245. .dequeue = skbprio_dequeue,
  246. .peek = qdisc_peek_dequeued,
  247. .init = skbprio_init,
  248. .reset = skbprio_reset,
  249. .change = skbprio_change,
  250. .dump = skbprio_dump,
  251. .destroy = skbprio_destroy,
  252. .owner = THIS_MODULE,
  253. };
  254. static int __init skbprio_module_init(void)
  255. {
  256. return register_qdisc(&skbprio_qdisc_ops);
  257. }
  258. static void __exit skbprio_module_exit(void)
  259. {
  260. unregister_qdisc(&skbprio_qdisc_ops);
  261. }
  262. module_init(skbprio_module_init)
  263. module_exit(skbprio_module_exit)
  264. MODULE_LICENSE("GPL");