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/ipv6.h>
21 #include <linux/skbuff.h>
23 #include <net/netlink.h>
24 #include <net/pkt_sched.h>
27 /* Stochastic Fairness Queuing algorithm.
28 =======================================
31 Paul E. McKenney "Stochastic Fairness Queuing",
32 IEEE INFOCOMM'90 Proceedings, San Francisco, 1990.
34 Paul E. McKenney "Stochastic Fairness Queuing",
35 "Interworking: Research and Experience", v.2, 1991, p.113-131.
39 M. Shreedhar and George Varghese "Efficient Fair
40 Queuing using Deficit Round Robin", Proc. SIGCOMM 95.
43 This is not the thing that is usually called (W)FQ nowadays.
44 It does not use any timestamp mechanism, but instead
45 processes queues in round-robin order.
49 - It is very cheap. Both CPU and memory requirements are minimal.
53 - "Stochastic" -> It is not 100% fair.
54 When hash collisions occur, several flows are considered as one.
56 - "Round-robin" -> It introduces larger delays than virtual clock
57 based schemes, and should not be used for isolating interactive
58 traffic from non-interactive. It means, that this scheduler
59 should be used as leaf of CBQ or P3, which put interactive traffic
60 to higher priority band.
62 We still need true WFQ for top level CSZ, but using WFQ
63 for the best effort traffic is absolutely pointless:
64 SFQ is superior for this purpose.
67 This implementation limits maximal queue length to 128;
68 maximal mtu to 2^15-1; number of hash buckets to 1024.
69 The only goal of this restrictions was that all data
70 fit into one 4K page :-). Struct sfq_sched_data is
71 organized in anti-cache manner: all the data for a bucket
72 are scattered over different locations. This is not good,
73 but it allowed me to put it into 4K.
75 It is easy to increase these values, but not in flight. */
78 #define SFQ_HASH_DIVISOR 1024
80 /* This type should contain at least SFQ_DEPTH*2 values */
81 typedef unsigned char sfq_index;
93 unsigned quantum; /* Allotment per round: MUST BE >= MTU */
97 struct timer_list perturb_timer;
99 sfq_index tail; /* Index of current slot in round */
100 sfq_index max_depth; /* Maximal depth */
102 sfq_index ht[SFQ_HASH_DIVISOR]; /* Hash table */
103 sfq_index next[SFQ_DEPTH]; /* Active slots link */
104 short allot[SFQ_DEPTH]; /* Current allotment per slot */
105 unsigned short hash[SFQ_DEPTH]; /* Hash value indexed by slots */
106 struct sk_buff_head qs[SFQ_DEPTH]; /* Slot queue */
107 struct sfq_head dep[SFQ_DEPTH*2]; /* Linked list of slots, indexed by depth */
110 static __inline__ unsigned sfq_fold_hash(struct sfq_sched_data *q, u32 h, u32 h1)
112 int pert = q->perturbation;
114 /* Have we any rotation primitives? If not, WHY? */
115 h ^= (h1<<pert) ^ (h1>>(0x1F - pert));
120 static unsigned sfq_hash(struct sfq_sched_data *q, struct sk_buff *skb)
124 switch (skb->protocol) {
125 case __constant_htons(ETH_P_IP):
127 const struct iphdr *iph = ip_hdr(skb);
129 h2 = iph->saddr^iph->protocol;
130 if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
131 (iph->protocol == IPPROTO_TCP ||
132 iph->protocol == IPPROTO_UDP ||
133 iph->protocol == IPPROTO_UDPLITE ||
134 iph->protocol == IPPROTO_SCTP ||
135 iph->protocol == IPPROTO_DCCP ||
136 iph->protocol == IPPROTO_ESP))
137 h2 ^= *(((u32*)iph) + iph->ihl);
140 case __constant_htons(ETH_P_IPV6):
142 struct ipv6hdr *iph = ipv6_hdr(skb);
143 h = iph->daddr.s6_addr32[3];
144 h2 = iph->saddr.s6_addr32[3]^iph->nexthdr;
145 if (iph->nexthdr == IPPROTO_TCP ||
146 iph->nexthdr == IPPROTO_UDP ||
147 iph->nexthdr == IPPROTO_UDPLITE ||
148 iph->nexthdr == IPPROTO_SCTP ||
149 iph->nexthdr == IPPROTO_DCCP ||
150 iph->nexthdr == IPPROTO_ESP)
151 h2 ^= *(u32*)&iph[1];
155 h = (u32)(unsigned long)skb->dst^skb->protocol;
156 h2 = (u32)(unsigned long)skb->sk;
158 return sfq_fold_hash(q, h, h2);
161 static inline void sfq_link(struct sfq_sched_data *q, sfq_index x)
164 int d = q->qs[x].qlen + SFQ_DEPTH;
170 q->dep[p].next = q->dep[n].prev = x;
173 static inline void sfq_dec(struct sfq_sched_data *q, sfq_index x)
182 if (n == p && q->max_depth == q->qs[x].qlen + 1)
188 static inline void sfq_inc(struct sfq_sched_data *q, sfq_index x)
198 if (q->max_depth < d)
204 static unsigned int sfq_drop(struct Qdisc *sch)
206 struct sfq_sched_data *q = qdisc_priv(sch);
207 sfq_index d = q->max_depth;
211 /* Queue is full! Find the longest slot and
212 drop a packet from it */
215 sfq_index x = q->dep[d+SFQ_DEPTH].next;
218 __skb_unlink(skb, &q->qs[x]);
223 sch->qstats.backlog -= len;
228 /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
229 d = q->next[q->tail];
230 q->next[q->tail] = q->next[d];
231 q->allot[q->next[d]] += q->quantum;
234 __skb_unlink(skb, &q->qs[d]);
238 q->ht[q->hash[d]] = SFQ_DEPTH;
240 sch->qstats.backlog -= len;
248 sfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
250 struct sfq_sched_data *q = qdisc_priv(sch);
251 unsigned hash = sfq_hash(q, skb);
255 if (x == SFQ_DEPTH) {
256 q->ht[hash] = x = q->dep[SFQ_DEPTH].next;
259 sch->qstats.backlog += skb->len;
260 __skb_queue_tail(&q->qs[x], skb);
262 if (q->qs[x].qlen == 1) { /* The flow is new */
263 if (q->tail == SFQ_DEPTH) { /* It is the first flow */
266 q->allot[x] = q->quantum;
268 q->next[x] = q->next[q->tail];
269 q->next[q->tail] = x;
273 if (++sch->q.qlen < q->limit-1) {
274 sch->bstats.bytes += skb->len;
275 sch->bstats.packets++;
284 sfq_requeue(struct sk_buff *skb, struct Qdisc* sch)
286 struct sfq_sched_data *q = qdisc_priv(sch);
287 unsigned hash = sfq_hash(q, skb);
291 if (x == SFQ_DEPTH) {
292 q->ht[hash] = x = q->dep[SFQ_DEPTH].next;
295 sch->qstats.backlog += skb->len;
296 __skb_queue_head(&q->qs[x], skb);
298 if (q->qs[x].qlen == 1) { /* The flow is new */
299 if (q->tail == SFQ_DEPTH) { /* It is the first flow */
302 q->allot[x] = q->quantum;
304 q->next[x] = q->next[q->tail];
305 q->next[q->tail] = x;
309 if (++sch->q.qlen < q->limit - 1) {
310 sch->qstats.requeues++;
322 static struct sk_buff *
323 sfq_dequeue(struct Qdisc* sch)
325 struct sfq_sched_data *q = qdisc_priv(sch);
329 /* No active slots */
330 if (q->tail == SFQ_DEPTH)
333 a = old_a = q->next[q->tail];
336 skb = __skb_dequeue(&q->qs[a]);
339 sch->qstats.backlog -= skb->len;
341 /* Is the slot empty? */
342 if (q->qs[a].qlen == 0) {
343 q->ht[q->hash[a]] = SFQ_DEPTH;
349 q->next[q->tail] = a;
350 q->allot[a] += q->quantum;
351 } else if ((q->allot[a] -= skb->len) <= 0) {
354 q->allot[a] += q->quantum;
360 sfq_reset(struct Qdisc* sch)
364 while ((skb = sfq_dequeue(sch)) != NULL)
368 static void sfq_perturbation(unsigned long arg)
370 struct Qdisc *sch = (struct Qdisc*)arg;
371 struct sfq_sched_data *q = qdisc_priv(sch);
373 q->perturbation = net_random()&0x1F;
375 if (q->perturb_period) {
376 q->perturb_timer.expires = jiffies + q->perturb_period;
377 add_timer(&q->perturb_timer);
381 static int sfq_change(struct Qdisc *sch, struct rtattr *opt)
383 struct sfq_sched_data *q = qdisc_priv(sch);
384 struct tc_sfq_qopt *ctl = RTA_DATA(opt);
387 if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
391 q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
392 q->perturb_period = ctl->perturb_period*HZ;
394 q->limit = min_t(u32, ctl->limit, SFQ_DEPTH);
397 while (sch->q.qlen >= q->limit-1)
399 qdisc_tree_decrease_qlen(sch, qlen - sch->q.qlen);
401 del_timer(&q->perturb_timer);
402 if (q->perturb_period) {
403 q->perturb_timer.expires = jiffies + q->perturb_period;
404 add_timer(&q->perturb_timer);
406 sch_tree_unlock(sch);
410 static int sfq_init(struct Qdisc *sch, struct rtattr *opt)
412 struct sfq_sched_data *q = qdisc_priv(sch);
415 init_timer(&q->perturb_timer);
416 q->perturb_timer.data = (unsigned long)sch;
417 q->perturb_timer.function = sfq_perturbation;
419 for (i=0; i<SFQ_HASH_DIVISOR; i++)
420 q->ht[i] = SFQ_DEPTH;
421 for (i=0; i<SFQ_DEPTH; i++) {
422 skb_queue_head_init(&q->qs[i]);
423 q->dep[i+SFQ_DEPTH].next = i+SFQ_DEPTH;
424 q->dep[i+SFQ_DEPTH].prev = i+SFQ_DEPTH;
426 q->limit = SFQ_DEPTH;
430 q->quantum = psched_mtu(sch->dev);
431 q->perturb_period = 0;
433 int err = sfq_change(sch, opt);
437 for (i=0; i<SFQ_DEPTH; i++)
442 static void sfq_destroy(struct Qdisc *sch)
444 struct sfq_sched_data *q = qdisc_priv(sch);
445 del_timer(&q->perturb_timer);
448 static int sfq_dump(struct Qdisc *sch, struct sk_buff *skb)
450 struct sfq_sched_data *q = qdisc_priv(sch);
451 unsigned char *b = skb_tail_pointer(skb);
452 struct tc_sfq_qopt opt;
454 opt.quantum = q->quantum;
455 opt.perturb_period = q->perturb_period/HZ;
457 opt.limit = q->limit;
458 opt.divisor = SFQ_HASH_DIVISOR;
459 opt.flows = q->limit;
461 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
470 static struct Qdisc_ops sfq_qdisc_ops = {
474 .priv_size = sizeof(struct sfq_sched_data),
475 .enqueue = sfq_enqueue,
476 .dequeue = sfq_dequeue,
477 .requeue = sfq_requeue,
481 .destroy = sfq_destroy,
484 .owner = THIS_MODULE,
487 static int __init sfq_module_init(void)
489 return register_qdisc(&sfq_qdisc_ops);
491 static void __exit sfq_module_exit(void)
493 unregister_qdisc(&sfq_qdisc_ops);
495 module_init(sfq_module_init)
496 module_exit(sfq_module_exit)
497 MODULE_LICENSE("GPL");