2 * net/sched/sch_sfq.c Stochastic Fairness Queueing discipline.
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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/jiffies.h>
16 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/skbuff.h>
21 #include <linux/jhash.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <net/netlink.h>
25 #include <net/pkt_sched.h>
26 #include <net/flow_keys.h>
29 /* Stochastic Fairness Queuing algorithm.
30 =======================================
33 Paul E. McKenney "Stochastic Fairness Queuing",
34 IEEE INFOCOMM'90 Proceedings, San Francisco, 1990.
36 Paul E. McKenney "Stochastic Fairness Queuing",
37 "Interworking: Research and Experience", v.2, 1991, p.113-131.
41 M. Shreedhar and George Varghese "Efficient Fair
42 Queuing using Deficit Round Robin", Proc. SIGCOMM 95.
45 This is not the thing that is usually called (W)FQ nowadays.
46 It does not use any timestamp mechanism, but instead
47 processes queues in round-robin order.
51 - It is very cheap. Both CPU and memory requirements are minimal.
55 - "Stochastic" -> It is not 100% fair.
56 When hash collisions occur, several flows are considered as one.
58 - "Round-robin" -> It introduces larger delays than virtual clock
59 based schemes, and should not be used for isolating interactive
60 traffic from non-interactive. It means, that this scheduler
61 should be used as leaf of CBQ or P3, which put interactive traffic
62 to higher priority band.
64 We still need true WFQ for top level CSZ, but using WFQ
65 for the best effort traffic is absolutely pointless:
66 SFQ is superior for this purpose.
69 This implementation limits maximal queue length to 128;
70 max mtu to 2^18-1; max 128 flows, number of hash buckets to 1024.
71 The only goal of this restrictions was that all data
72 fit into one 4K page on 32bit arches.
74 It is easy to increase these values, but not in flight. */
76 #define SFQ_DEPTH 128 /* max number of packets per flow */
77 #define SFQ_SLOTS 128 /* max number of flows */
78 #define SFQ_EMPTY_SLOT 255
79 #define SFQ_DEFAULT_HASH_DIVISOR 1024
81 /* We use 16 bits to store allot, and want to handle packets up to 64K
82 * Scale allot by 8 (1<<3) so that no overflow occurs.
84 #define SFQ_ALLOT_SHIFT 3
85 #define SFQ_ALLOT_SIZE(X) DIV_ROUND_UP(X, 1 << SFQ_ALLOT_SHIFT)
87 /* This type should contain at least SFQ_DEPTH + SFQ_SLOTS values */
88 typedef unsigned char sfq_index
;
91 * We dont use pointers to save space.
92 * Small indexes [0 ... SFQ_SLOTS - 1] are 'pointers' to slots[] array
93 * while following values [SFQ_SLOTS ... SFQ_SLOTS + SFQ_DEPTH - 1]
94 * are 'pointers' to dep[] array
102 struct sk_buff
*skblist_next
;
103 struct sk_buff
*skblist_prev
;
104 sfq_index qlen
; /* number of skbs in skblist */
105 sfq_index next
; /* next slot in sfq chain */
106 struct sfq_head dep
; /* anchor in dep[] chains */
107 unsigned short hash
; /* hash value (index in ht[]) */
108 short allot
; /* credit for this slot */
111 struct sfq_sched_data
{
114 unsigned int quantum
; /* Allotment per round: MUST BE >= MTU */
116 unsigned int divisor
; /* number of slots in hash table */
118 struct tcf_proto
*filter_list
;
119 struct timer_list perturb_timer
;
121 sfq_index cur_depth
; /* depth of longest slot */
122 unsigned short scaled_quantum
; /* SFQ_ALLOT_SIZE(quantum) */
123 struct sfq_slot
*tail
; /* current slot in round */
124 sfq_index
*ht
; /* Hash table (divisor slots) */
125 struct sfq_slot slots
[SFQ_SLOTS
];
126 struct sfq_head dep
[SFQ_DEPTH
]; /* Linked list of slots, indexed by depth */
130 * sfq_head are either in a sfq_slot or in dep[] array
132 static inline struct sfq_head
*sfq_dep_head(struct sfq_sched_data
*q
, sfq_index val
)
135 return &q
->slots
[val
].dep
;
136 return &q
->dep
[val
- SFQ_SLOTS
];
140 * In order to be able to quickly rehash our queue when timer changes
141 * q->perturbation, we store flow_keys in skb->cb[]
144 struct flow_keys keys
;
147 static inline struct sfq_skb_cb
*sfq_skb_cb(const struct sk_buff
*skb
)
149 BUILD_BUG_ON(sizeof(skb
->cb
) <
150 sizeof(struct qdisc_skb_cb
) + sizeof(struct sfq_skb_cb
));
151 return (struct sfq_skb_cb
*)qdisc_skb_cb(skb
)->data
;
154 static unsigned int sfq_hash(const struct sfq_sched_data
*q
,
155 const struct sk_buff
*skb
)
157 const struct flow_keys
*keys
= &sfq_skb_cb(skb
)->keys
;
160 hash
= jhash_3words((__force u32
)keys
->dst
,
161 (__force u32
)keys
->src
^ keys
->ip_proto
,
162 (__force u32
)keys
->ports
, q
->perturbation
);
163 return hash
& (q
->divisor
- 1);
166 static unsigned int sfq_classify(struct sk_buff
*skb
, struct Qdisc
*sch
,
169 struct sfq_sched_data
*q
= qdisc_priv(sch
);
170 struct tcf_result res
;
173 if (TC_H_MAJ(skb
->priority
) == sch
->handle
&&
174 TC_H_MIN(skb
->priority
) > 0 &&
175 TC_H_MIN(skb
->priority
) <= q
->divisor
)
176 return TC_H_MIN(skb
->priority
);
178 if (!q
->filter_list
) {
179 skb_flow_dissect(skb
, &sfq_skb_cb(skb
)->keys
);
180 return sfq_hash(q
, skb
) + 1;
183 *qerr
= NET_XMIT_SUCCESS
| __NET_XMIT_BYPASS
;
184 result
= tc_classify(skb
, q
->filter_list
, &res
);
186 #ifdef CONFIG_NET_CLS_ACT
190 *qerr
= NET_XMIT_SUCCESS
| __NET_XMIT_STOLEN
;
195 if (TC_H_MIN(res
.classid
) <= q
->divisor
)
196 return TC_H_MIN(res
.classid
);
202 * x : slot number [0 .. SFQ_SLOTS - 1]
204 static inline void sfq_link(struct sfq_sched_data
*q
, sfq_index x
)
207 int qlen
= q
->slots
[x
].qlen
;
209 p
= qlen
+ SFQ_SLOTS
;
210 n
= q
->dep
[qlen
].next
;
212 q
->slots
[x
].dep
.next
= n
;
213 q
->slots
[x
].dep
.prev
= p
;
215 q
->dep
[qlen
].next
= x
; /* sfq_dep_head(q, p)->next = x */
216 sfq_dep_head(q
, n
)->prev
= x
;
219 #define sfq_unlink(q, x, n, p) \
220 n = q->slots[x].dep.next; \
221 p = q->slots[x].dep.prev; \
222 sfq_dep_head(q, p)->next = n; \
223 sfq_dep_head(q, n)->prev = p
226 static inline void sfq_dec(struct sfq_sched_data
*q
, sfq_index x
)
231 sfq_unlink(q
, x
, n
, p
);
233 d
= q
->slots
[x
].qlen
--;
234 if (n
== p
&& q
->cur_depth
== d
)
239 static inline void sfq_inc(struct sfq_sched_data
*q
, sfq_index x
)
244 sfq_unlink(q
, x
, n
, p
);
246 d
= ++q
->slots
[x
].qlen
;
247 if (q
->cur_depth
< d
)
252 /* helper functions : might be changed when/if skb use a standard list_head */
254 /* remove one skb from tail of slot queue */
255 static inline struct sk_buff
*slot_dequeue_tail(struct sfq_slot
*slot
)
257 struct sk_buff
*skb
= slot
->skblist_prev
;
259 slot
->skblist_prev
= skb
->prev
;
260 skb
->prev
->next
= (struct sk_buff
*)slot
;
261 skb
->next
= skb
->prev
= NULL
;
265 /* remove one skb from head of slot queue */
266 static inline struct sk_buff
*slot_dequeue_head(struct sfq_slot
*slot
)
268 struct sk_buff
*skb
= slot
->skblist_next
;
270 slot
->skblist_next
= skb
->next
;
271 skb
->next
->prev
= (struct sk_buff
*)slot
;
272 skb
->next
= skb
->prev
= NULL
;
276 static inline void slot_queue_init(struct sfq_slot
*slot
)
278 slot
->skblist_prev
= slot
->skblist_next
= (struct sk_buff
*)slot
;
281 /* add skb to slot queue (tail add) */
282 static inline void slot_queue_add(struct sfq_slot
*slot
, struct sk_buff
*skb
)
284 skb
->prev
= slot
->skblist_prev
;
285 skb
->next
= (struct sk_buff
*)slot
;
286 slot
->skblist_prev
->next
= skb
;
287 slot
->skblist_prev
= skb
;
290 #define slot_queue_walk(slot, skb) \
291 for (skb = slot->skblist_next; \
292 skb != (struct sk_buff *)slot; \
295 static unsigned int sfq_drop(struct Qdisc
*sch
)
297 struct sfq_sched_data
*q
= qdisc_priv(sch
);
298 sfq_index x
, d
= q
->cur_depth
;
301 struct sfq_slot
*slot
;
303 /* Queue is full! Find the longest slot and drop tail packet from it */
308 skb
= slot_dequeue_tail(slot
);
309 len
= qdisc_pkt_len(skb
);
314 sch
->qstats
.backlog
-= len
;
319 /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
322 q
->tail
->next
= slot
->next
;
323 q
->ht
[slot
->hash
] = SFQ_EMPTY_SLOT
;
331 sfq_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
)
333 struct sfq_sched_data
*q
= qdisc_priv(sch
);
336 struct sfq_slot
*slot
;
337 int uninitialized_var(ret
);
339 hash
= sfq_classify(skb
, sch
, &ret
);
341 if (ret
& __NET_XMIT_BYPASS
)
350 if (x
== SFQ_EMPTY_SLOT
) {
351 x
= q
->dep
[0].next
; /* get a free slot */
357 /* If selected queue has length q->limit, do simple tail drop,
358 * i.e. drop _this_ packet.
360 if (slot
->qlen
>= q
->limit
)
361 return qdisc_drop(skb
, sch
);
363 sch
->qstats
.backlog
+= qdisc_pkt_len(skb
);
364 slot_queue_add(slot
, skb
);
366 if (slot
->qlen
== 1) { /* The flow is new */
367 if (q
->tail
== NULL
) { /* It is the first flow */
371 slot
->next
= q
->tail
->next
;
374 slot
->allot
= q
->scaled_quantum
;
376 if (++sch
->q
.qlen
<= q
->limit
)
377 return NET_XMIT_SUCCESS
;
381 /* Return Congestion Notification only if we dropped a packet
384 if (qlen
!= slot
->qlen
)
387 /* As we dropped a packet, better let upper stack know this */
388 qdisc_tree_decrease_qlen(sch
, 1);
389 return NET_XMIT_SUCCESS
;
392 static struct sk_buff
*
393 sfq_dequeue(struct Qdisc
*sch
)
395 struct sfq_sched_data
*q
= qdisc_priv(sch
);
398 struct sfq_slot
*slot
;
400 /* No active slots */
407 if (slot
->allot
<= 0) {
409 slot
->allot
+= q
->scaled_quantum
;
412 skb
= slot_dequeue_head(slot
);
414 qdisc_bstats_update(sch
, skb
);
416 sch
->qstats
.backlog
-= qdisc_pkt_len(skb
);
418 /* Is the slot empty? */
419 if (slot
->qlen
== 0) {
420 q
->ht
[slot
->hash
] = SFQ_EMPTY_SLOT
;
423 q
->tail
= NULL
; /* no more active slots */
426 q
->tail
->next
= next_a
;
428 slot
->allot
-= SFQ_ALLOT_SIZE(qdisc_pkt_len(skb
));
434 sfq_reset(struct Qdisc
*sch
)
438 while ((skb
= sfq_dequeue(sch
)) != NULL
)
443 * When q->perturbation is changed, we rehash all queued skbs
444 * to avoid OOO (Out Of Order) effects.
445 * We dont use sfq_dequeue()/sfq_enqueue() because we dont want to change
448 static void sfq_rehash(struct sfq_sched_data
*q
)
452 struct sfq_slot
*slot
;
453 struct sk_buff_head list
;
455 __skb_queue_head_init(&list
);
457 for (i
= 0; i
< SFQ_SLOTS
; i
++) {
462 skb
= slot_dequeue_head(slot
);
464 __skb_queue_tail(&list
, skb
);
466 q
->ht
[slot
->hash
] = SFQ_EMPTY_SLOT
;
470 while ((skb
= __skb_dequeue(&list
)) != NULL
) {
471 unsigned int hash
= sfq_hash(q
, skb
);
472 sfq_index x
= q
->ht
[hash
];
475 if (x
== SFQ_EMPTY_SLOT
) {
476 x
= q
->dep
[0].next
; /* get a free slot */
481 slot_queue_add(slot
, skb
);
483 if (slot
->qlen
== 1) { /* The flow is new */
484 if (q
->tail
== NULL
) { /* It is the first flow */
487 slot
->next
= q
->tail
->next
;
491 slot
->allot
= q
->scaled_quantum
;
496 static void sfq_perturbation(unsigned long arg
)
498 struct Qdisc
*sch
= (struct Qdisc
*)arg
;
499 struct sfq_sched_data
*q
= qdisc_priv(sch
);
500 spinlock_t
*root_lock
= qdisc_lock(qdisc_root_sleeping(sch
));
502 spin_lock(root_lock
);
503 q
->perturbation
= net_random();
504 if (!q
->filter_list
&& q
->tail
)
506 spin_unlock(root_lock
);
508 if (q
->perturb_period
)
509 mod_timer(&q
->perturb_timer
, jiffies
+ q
->perturb_period
);
512 static int sfq_change(struct Qdisc
*sch
, struct nlattr
*opt
)
514 struct sfq_sched_data
*q
= qdisc_priv(sch
);
515 struct tc_sfq_qopt
*ctl
= nla_data(opt
);
518 if (opt
->nla_len
< nla_attr_size(sizeof(*ctl
)))
522 (!is_power_of_2(ctl
->divisor
) || ctl
->divisor
> 65536))
526 q
->quantum
= ctl
->quantum
? : psched_mtu(qdisc_dev(sch
));
527 q
->scaled_quantum
= SFQ_ALLOT_SIZE(q
->quantum
);
528 q
->perturb_period
= ctl
->perturb_period
* HZ
;
530 q
->limit
= min_t(u32
, ctl
->limit
, SFQ_DEPTH
- 1);
532 q
->divisor
= ctl
->divisor
;
534 while (sch
->q
.qlen
> q
->limit
)
536 qdisc_tree_decrease_qlen(sch
, qlen
- sch
->q
.qlen
);
538 del_timer(&q
->perturb_timer
);
539 if (q
->perturb_period
) {
540 mod_timer(&q
->perturb_timer
, jiffies
+ q
->perturb_period
);
541 q
->perturbation
= net_random();
543 sch_tree_unlock(sch
);
547 static void *sfq_alloc(size_t sz
)
549 void *ptr
= kmalloc(sz
, GFP_KERNEL
| __GFP_NOWARN
);
556 static void sfq_free(void *addr
)
559 if (is_vmalloc_addr(addr
))
566 static void sfq_destroy(struct Qdisc
*sch
)
568 struct sfq_sched_data
*q
= qdisc_priv(sch
);
570 tcf_destroy_chain(&q
->filter_list
);
571 q
->perturb_period
= 0;
572 del_timer_sync(&q
->perturb_timer
);
576 static int sfq_init(struct Qdisc
*sch
, struct nlattr
*opt
)
578 struct sfq_sched_data
*q
= qdisc_priv(sch
);
581 q
->perturb_timer
.function
= sfq_perturbation
;
582 q
->perturb_timer
.data
= (unsigned long)sch
;
583 init_timer_deferrable(&q
->perturb_timer
);
585 for (i
= 0; i
< SFQ_DEPTH
; i
++) {
586 q
->dep
[i
].next
= i
+ SFQ_SLOTS
;
587 q
->dep
[i
].prev
= i
+ SFQ_SLOTS
;
590 q
->limit
= SFQ_DEPTH
- 1;
593 q
->divisor
= SFQ_DEFAULT_HASH_DIVISOR
;
595 q
->quantum
= psched_mtu(qdisc_dev(sch
));
596 q
->scaled_quantum
= SFQ_ALLOT_SIZE(q
->quantum
);
597 q
->perturb_period
= 0;
598 q
->perturbation
= net_random();
600 int err
= sfq_change(sch
, opt
);
605 q
->ht
= sfq_alloc(sizeof(q
->ht
[0]) * q
->divisor
);
610 for (i
= 0; i
< q
->divisor
; i
++)
611 q
->ht
[i
] = SFQ_EMPTY_SLOT
;
613 for (i
= 0; i
< SFQ_SLOTS
; i
++) {
614 slot_queue_init(&q
->slots
[i
]);
618 sch
->flags
|= TCQ_F_CAN_BYPASS
;
620 sch
->flags
&= ~TCQ_F_CAN_BYPASS
;
624 static int sfq_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
626 struct sfq_sched_data
*q
= qdisc_priv(sch
);
627 unsigned char *b
= skb_tail_pointer(skb
);
628 struct tc_sfq_qopt opt
;
630 opt
.quantum
= q
->quantum
;
631 opt
.perturb_period
= q
->perturb_period
/ HZ
;
633 opt
.limit
= q
->limit
;
634 opt
.divisor
= q
->divisor
;
635 opt
.flows
= q
->limit
;
637 NLA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
646 static struct Qdisc
*sfq_leaf(struct Qdisc
*sch
, unsigned long arg
)
651 static unsigned long sfq_get(struct Qdisc
*sch
, u32 classid
)
656 static unsigned long sfq_bind(struct Qdisc
*sch
, unsigned long parent
,
659 /* we cannot bypass queue discipline anymore */
660 sch
->flags
&= ~TCQ_F_CAN_BYPASS
;
664 static void sfq_put(struct Qdisc
*q
, unsigned long cl
)
668 static struct tcf_proto
**sfq_find_tcf(struct Qdisc
*sch
, unsigned long cl
)
670 struct sfq_sched_data
*q
= qdisc_priv(sch
);
674 return &q
->filter_list
;
677 static int sfq_dump_class(struct Qdisc
*sch
, unsigned long cl
,
678 struct sk_buff
*skb
, struct tcmsg
*tcm
)
680 tcm
->tcm_handle
|= TC_H_MIN(cl
);
684 static int sfq_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
687 struct sfq_sched_data
*q
= qdisc_priv(sch
);
688 sfq_index idx
= q
->ht
[cl
- 1];
689 struct gnet_stats_queue qs
= { 0 };
690 struct tc_sfq_xstats xstats
= { 0 };
693 if (idx
!= SFQ_EMPTY_SLOT
) {
694 const struct sfq_slot
*slot
= &q
->slots
[idx
];
696 xstats
.allot
= slot
->allot
<< SFQ_ALLOT_SHIFT
;
697 qs
.qlen
= slot
->qlen
;
698 slot_queue_walk(slot
, skb
)
699 qs
.backlog
+= qdisc_pkt_len(skb
);
701 if (gnet_stats_copy_queue(d
, &qs
) < 0)
703 return gnet_stats_copy_app(d
, &xstats
, sizeof(xstats
));
706 static void sfq_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
708 struct sfq_sched_data
*q
= qdisc_priv(sch
);
714 for (i
= 0; i
< q
->divisor
; i
++) {
715 if (q
->ht
[i
] == SFQ_EMPTY_SLOT
||
716 arg
->count
< arg
->skip
) {
720 if (arg
->fn(sch
, i
+ 1, arg
) < 0) {
728 static const struct Qdisc_class_ops sfq_class_ops
= {
732 .tcf_chain
= sfq_find_tcf
,
733 .bind_tcf
= sfq_bind
,
734 .unbind_tcf
= sfq_put
,
735 .dump
= sfq_dump_class
,
736 .dump_stats
= sfq_dump_class_stats
,
740 static struct Qdisc_ops sfq_qdisc_ops __read_mostly
= {
741 .cl_ops
= &sfq_class_ops
,
743 .priv_size
= sizeof(struct sfq_sched_data
),
744 .enqueue
= sfq_enqueue
,
745 .dequeue
= sfq_dequeue
,
746 .peek
= qdisc_peek_dequeued
,
750 .destroy
= sfq_destroy
,
753 .owner
= THIS_MODULE
,
756 static int __init
sfq_module_init(void)
758 return register_qdisc(&sfq_qdisc_ops
);
760 static void __exit
sfq_module_exit(void)
762 unregister_qdisc(&sfq_qdisc_ops
);
764 module_init(sfq_module_init
)
765 module_exit(sfq_module_exit
)
766 MODULE_LICENSE("GPL");