2 * net/sched/sch_skbprio.c SKB Priority Queue.
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.
9 * Authors: Nishanth Devarajan, <ndev2021@gmail.com>
10 * Cody Doucette, <doucette@bu.edu>
11 * original idea by Michel Machado, Cody Doucette, and Qiaobin Fu
14 #include <linux/string.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/skbuff.h>
21 #include <net/pkt_sched.h>
22 #include <net/sch_generic.h>
23 #include <net/inet_ecn.h>
26 * =================================
28 * Skbprio (SKB Priority Queue) is a queueing discipline that prioritizes
29 * packets according to their skb->priority field. Under congestion,
30 * Skbprio drops already-enqueued lower priority packets to make space
31 * available for higher priority packets; it was conceived as a solution
32 * for denial-of-service defenses that need to route packets with different
33 * priorities as a mean to overcome DoS attacks.
36 struct skbprio_sched_data
{
38 struct sk_buff_head qdiscs
[SKBPRIO_MAX_PRIORITY
];
39 struct gnet_stats_queue qstats
[SKBPRIO_MAX_PRIORITY
];
44 static u16
calc_new_high_prio(const struct skbprio_sched_data
*q
)
48 for (prio
= q
->highest_prio
- 1; prio
>= q
->lowest_prio
; prio
--) {
49 if (!skb_queue_empty(&q
->qdiscs
[prio
]))
53 /* SKB queue is empty, return 0 (default highest priority setting). */
57 static u16
calc_new_low_prio(const struct skbprio_sched_data
*q
)
61 for (prio
= q
->lowest_prio
+ 1; prio
<= q
->highest_prio
; prio
++) {
62 if (!skb_queue_empty(&q
->qdiscs
[prio
]))
66 /* SKB queue is empty, return SKBPRIO_MAX_PRIORITY - 1
67 * (default lowest priority setting).
69 return SKBPRIO_MAX_PRIORITY
- 1;
72 static int skbprio_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
73 struct sk_buff
**to_free
)
75 const unsigned int max_priority
= SKBPRIO_MAX_PRIORITY
- 1;
76 struct skbprio_sched_data
*q
= qdisc_priv(sch
);
77 struct sk_buff_head
*qdisc
;
78 struct sk_buff_head
*lp_qdisc
;
79 struct sk_buff
*to_drop
;
82 /* Obtain the priority of @skb. */
83 prio
= min(skb
->priority
, max_priority
);
85 qdisc
= &q
->qdiscs
[prio
];
86 if (sch
->q
.qlen
< sch
->limit
) {
87 __skb_queue_tail(qdisc
, skb
);
88 qdisc_qstats_backlog_inc(sch
, skb
);
89 q
->qstats
[prio
].backlog
+= qdisc_pkt_len(skb
);
91 /* Check to update highest and lowest priorities. */
92 if (prio
> q
->highest_prio
)
93 q
->highest_prio
= prio
;
95 if (prio
< q
->lowest_prio
)
96 q
->lowest_prio
= prio
;
99 return NET_XMIT_SUCCESS
;
102 /* If this packet has the lowest priority, drop it. */
105 q
->qstats
[prio
].drops
++;
106 q
->qstats
[prio
].overlimits
++;
107 return qdisc_drop(skb
, sch
, to_free
);
110 __skb_queue_tail(qdisc
, skb
);
111 qdisc_qstats_backlog_inc(sch
, skb
);
112 q
->qstats
[prio
].backlog
+= qdisc_pkt_len(skb
);
114 /* Drop the packet at the tail of the lowest priority qdisc. */
115 lp_qdisc
= &q
->qdiscs
[lp
];
116 to_drop
= __skb_dequeue_tail(lp_qdisc
);
118 qdisc_qstats_backlog_dec(sch
, to_drop
);
119 qdisc_drop(to_drop
, sch
, to_free
);
121 q
->qstats
[lp
].backlog
-= qdisc_pkt_len(to_drop
);
122 q
->qstats
[lp
].drops
++;
123 q
->qstats
[lp
].overlimits
++;
125 /* Check to update highest and lowest priorities. */
126 if (skb_queue_empty(lp_qdisc
)) {
127 if (q
->lowest_prio
== q
->highest_prio
) {
128 /* The incoming packet is the only packet in queue. */
129 BUG_ON(sch
->q
.qlen
!= 1);
130 q
->lowest_prio
= prio
;
131 q
->highest_prio
= prio
;
133 q
->lowest_prio
= calc_new_low_prio(q
);
137 if (prio
> q
->highest_prio
)
138 q
->highest_prio
= prio
;
143 static struct sk_buff
*skbprio_dequeue(struct Qdisc
*sch
)
145 struct skbprio_sched_data
*q
= qdisc_priv(sch
);
146 struct sk_buff_head
*hpq
= &q
->qdiscs
[q
->highest_prio
];
147 struct sk_buff
*skb
= __skb_dequeue(hpq
);
153 qdisc_qstats_backlog_dec(sch
, skb
);
154 qdisc_bstats_update(sch
, skb
);
156 q
->qstats
[q
->highest_prio
].backlog
-= qdisc_pkt_len(skb
);
158 /* Update highest priority field. */
159 if (skb_queue_empty(hpq
)) {
160 if (q
->lowest_prio
== q
->highest_prio
) {
163 q
->lowest_prio
= SKBPRIO_MAX_PRIORITY
- 1;
165 q
->highest_prio
= calc_new_high_prio(q
);
171 static int skbprio_change(struct Qdisc
*sch
, struct nlattr
*opt
,
172 struct netlink_ext_ack
*extack
)
174 struct tc_skbprio_qopt
*ctl
= nla_data(opt
);
176 sch
->limit
= ctl
->limit
;
180 static int skbprio_init(struct Qdisc
*sch
, struct nlattr
*opt
,
181 struct netlink_ext_ack
*extack
)
183 struct skbprio_sched_data
*q
= qdisc_priv(sch
);
186 /* Initialise all queues, one for each possible priority. */
187 for (prio
= 0; prio
< SKBPRIO_MAX_PRIORITY
; prio
++)
188 __skb_queue_head_init(&q
->qdiscs
[prio
]);
190 memset(&q
->qstats
, 0, sizeof(q
->qstats
));
192 q
->lowest_prio
= SKBPRIO_MAX_PRIORITY
- 1;
197 return skbprio_change(sch
, opt
, extack
);
200 static int skbprio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
202 struct tc_skbprio_qopt opt
;
204 opt
.limit
= sch
->limit
;
206 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
212 static void skbprio_reset(struct Qdisc
*sch
)
214 struct skbprio_sched_data
*q
= qdisc_priv(sch
);
217 sch
->qstats
.backlog
= 0;
220 for (prio
= 0; prio
< SKBPRIO_MAX_PRIORITY
; prio
++)
221 __skb_queue_purge(&q
->qdiscs
[prio
]);
223 memset(&q
->qstats
, 0, sizeof(q
->qstats
));
225 q
->lowest_prio
= SKBPRIO_MAX_PRIORITY
- 1;
228 static void skbprio_destroy(struct Qdisc
*sch
)
230 struct skbprio_sched_data
*q
= qdisc_priv(sch
);
233 for (prio
= 0; prio
< SKBPRIO_MAX_PRIORITY
; prio
++)
234 __skb_queue_purge(&q
->qdiscs
[prio
]);
237 static struct Qdisc
*skbprio_leaf(struct Qdisc
*sch
, unsigned long arg
)
242 static unsigned long skbprio_find(struct Qdisc
*sch
, u32 classid
)
247 static int skbprio_dump_class(struct Qdisc
*sch
, unsigned long cl
,
248 struct sk_buff
*skb
, struct tcmsg
*tcm
)
250 tcm
->tcm_handle
|= TC_H_MIN(cl
);
254 static int skbprio_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
257 struct skbprio_sched_data
*q
= qdisc_priv(sch
);
258 if (gnet_stats_copy_queue(d
, NULL
, &q
->qstats
[cl
- 1],
259 q
->qstats
[cl
- 1].qlen
) < 0)
264 static void skbprio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
271 for (i
= 0; i
< SKBPRIO_MAX_PRIORITY
; i
++) {
272 if (arg
->count
< arg
->skip
) {
276 if (arg
->fn(sch
, i
+ 1, arg
) < 0) {
284 static const struct Qdisc_class_ops skbprio_class_ops
= {
285 .leaf
= skbprio_leaf
,
286 .find
= skbprio_find
,
287 .dump
= skbprio_dump_class
,
288 .dump_stats
= skbprio_dump_class_stats
,
289 .walk
= skbprio_walk
,
292 static struct Qdisc_ops skbprio_qdisc_ops __read_mostly
= {
293 .cl_ops
= &skbprio_class_ops
,
295 .priv_size
= sizeof(struct skbprio_sched_data
),
296 .enqueue
= skbprio_enqueue
,
297 .dequeue
= skbprio_dequeue
,
298 .peek
= qdisc_peek_dequeued
,
299 .init
= skbprio_init
,
300 .reset
= skbprio_reset
,
301 .change
= skbprio_change
,
302 .dump
= skbprio_dump
,
303 .destroy
= skbprio_destroy
,
304 .owner
= THIS_MODULE
,
307 static int __init
skbprio_module_init(void)
309 return register_qdisc(&skbprio_qdisc_ops
);
312 static void __exit
skbprio_module_exit(void)
314 unregister_qdisc(&skbprio_qdisc_ops
);
317 module_init(skbprio_module_init
)
318 module_exit(skbprio_module_exit
)
320 MODULE_LICENSE("GPL");