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 <asm/uaccess.h>
14 #include <asm/system.h>
15 #include <linux/bitops.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/jiffies.h>
19 #include <linux/string.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
24 #include <linux/errno.h>
25 #include <linux/interrupt.h>
26 #include <linux/if_ether.h>
27 #include <linux/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/notifier.h>
31 #include <linux/init.h>
33 #include <linux/ipv6.h>
34 #include <net/route.h>
35 #include <linux/skbuff.h>
37 #include <net/pkt_sched.h>
40 /* Stochastic Fairness Queuing algorithm.
41 =======================================
44 Paul E. McKenney "Stochastic Fairness Queuing",
45 IEEE INFOCOMM'90 Proceedings, San Francisco, 1990.
47 Paul E. McKenney "Stochastic Fairness Queuing",
48 "Interworking: Research and Experience", v.2, 1991, p.113-131.
52 M. Shreedhar and George Varghese "Efficient Fair
53 Queuing using Deficit Round Robin", Proc. SIGCOMM 95.
56 This is not the thing that is usually called (W)FQ nowadays.
57 It does not use any timestamp mechanism, but instead
58 processes queues in round-robin order.
62 - It is very cheap. Both CPU and memory requirements are minimal.
66 - "Stochastic" -> It is not 100% fair.
67 When hash collisions occur, several flows are considered as one.
69 - "Round-robin" -> It introduces larger delays than virtual clock
70 based schemes, and should not be used for isolating interactive
71 traffic from non-interactive. It means, that this scheduler
72 should be used as leaf of CBQ or P3, which put interactive traffic
73 to higher priority band.
75 We still need true WFQ for top level CSZ, but using WFQ
76 for the best effort traffic is absolutely pointless:
77 SFQ is superior for this purpose.
80 This implementation limits maximal queue length to 128;
81 maximal mtu to 2^15-1; number of hash buckets to 1024.
82 The only goal of this restrictions was that all data
83 fit into one 4K page :-). Struct sfq_sched_data is
84 organized in anti-cache manner: all the data for a bucket
85 are scattered over different locations. This is not good,
86 but it allowed me to put it into 4K.
88 It is easy to increase these values, but not in flight. */
91 #define SFQ_HASH_DIVISOR 1024
93 /* This type should contain at least SFQ_DEPTH*2 values */
94 typedef unsigned char sfq_index
;
102 struct sfq_sched_data
106 unsigned quantum
; /* Allotment per round: MUST BE >= MTU */
110 struct timer_list perturb_timer
;
112 sfq_index tail
; /* Index of current slot in round */
113 sfq_index max_depth
; /* Maximal depth */
115 sfq_index ht
[SFQ_HASH_DIVISOR
]; /* Hash table */
116 sfq_index next
[SFQ_DEPTH
]; /* Active slots link */
117 short allot
[SFQ_DEPTH
]; /* Current allotment per slot */
118 unsigned short hash
[SFQ_DEPTH
]; /* Hash value indexed by slots */
119 struct sk_buff_head qs
[SFQ_DEPTH
]; /* Slot queue */
120 struct sfq_head dep
[SFQ_DEPTH
*2]; /* Linked list of slots, indexed by depth */
123 static __inline__
unsigned sfq_fold_hash(struct sfq_sched_data
*q
, u32 h
, u32 h1
)
125 int pert
= q
->perturbation
;
127 /* Have we any rotation primitives? If not, WHY? */
128 h
^= (h1
<<pert
) ^ (h1
>>(0x1F - pert
));
133 static unsigned sfq_hash(struct sfq_sched_data
*q
, struct sk_buff
*skb
)
137 switch (skb
->protocol
) {
138 case __constant_htons(ETH_P_IP
):
140 struct iphdr
*iph
= skb
->nh
.iph
;
142 h2
= iph
->saddr
^iph
->protocol
;
143 if (!(iph
->frag_off
&htons(IP_MF
|IP_OFFSET
)) &&
144 (iph
->protocol
== IPPROTO_TCP
||
145 iph
->protocol
== IPPROTO_UDP
||
146 iph
->protocol
== IPPROTO_UDPLITE
||
147 iph
->protocol
== IPPROTO_SCTP
||
148 iph
->protocol
== IPPROTO_DCCP
||
149 iph
->protocol
== IPPROTO_ESP
))
150 h2
^= *(((u32
*)iph
) + iph
->ihl
);
153 case __constant_htons(ETH_P_IPV6
):
155 struct ipv6hdr
*iph
= skb
->nh
.ipv6h
;
156 h
= iph
->daddr
.s6_addr32
[3];
157 h2
= iph
->saddr
.s6_addr32
[3]^iph
->nexthdr
;
158 if (iph
->nexthdr
== IPPROTO_TCP
||
159 iph
->nexthdr
== IPPROTO_UDP
||
160 iph
->nexthdr
== IPPROTO_UDPLITE
||
161 iph
->nexthdr
== IPPROTO_SCTP
||
162 iph
->nexthdr
== IPPROTO_DCCP
||
163 iph
->nexthdr
== IPPROTO_ESP
)
164 h2
^= *(u32
*)&iph
[1];
168 h
= (u32
)(unsigned long)skb
->dst
^skb
->protocol
;
169 h2
= (u32
)(unsigned long)skb
->sk
;
171 return sfq_fold_hash(q
, h
, h2
);
174 static inline void sfq_link(struct sfq_sched_data
*q
, sfq_index x
)
177 int d
= q
->qs
[x
].qlen
+ SFQ_DEPTH
;
183 q
->dep
[p
].next
= q
->dep
[n
].prev
= x
;
186 static inline void sfq_dec(struct sfq_sched_data
*q
, sfq_index x
)
195 if (n
== p
&& q
->max_depth
== q
->qs
[x
].qlen
+ 1)
201 static inline void sfq_inc(struct sfq_sched_data
*q
, sfq_index x
)
211 if (q
->max_depth
< d
)
217 static unsigned int sfq_drop(struct Qdisc
*sch
)
219 struct sfq_sched_data
*q
= qdisc_priv(sch
);
220 sfq_index d
= q
->max_depth
;
224 /* Queue is full! Find the longest slot and
225 drop a packet from it */
228 sfq_index x
= q
->dep
[d
+SFQ_DEPTH
].next
;
231 __skb_unlink(skb
, &q
->qs
[x
]);
236 sch
->qstats
.backlog
-= len
;
241 /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
242 d
= q
->next
[q
->tail
];
243 q
->next
[q
->tail
] = q
->next
[d
];
244 q
->allot
[q
->next
[d
]] += q
->quantum
;
247 __skb_unlink(skb
, &q
->qs
[d
]);
251 q
->ht
[q
->hash
[d
]] = SFQ_DEPTH
;
253 sch
->qstats
.backlog
-= len
;
261 sfq_enqueue(struct sk_buff
*skb
, struct Qdisc
* sch
)
263 struct sfq_sched_data
*q
= qdisc_priv(sch
);
264 unsigned hash
= sfq_hash(q
, skb
);
268 if (x
== SFQ_DEPTH
) {
269 q
->ht
[hash
] = x
= q
->dep
[SFQ_DEPTH
].next
;
272 sch
->qstats
.backlog
+= skb
->len
;
273 __skb_queue_tail(&q
->qs
[x
], skb
);
275 if (q
->qs
[x
].qlen
== 1) { /* The flow is new */
276 if (q
->tail
== SFQ_DEPTH
) { /* It is the first flow */
279 q
->allot
[x
] = q
->quantum
;
281 q
->next
[x
] = q
->next
[q
->tail
];
282 q
->next
[q
->tail
] = x
;
286 if (++sch
->q
.qlen
< q
->limit
-1) {
287 sch
->bstats
.bytes
+= skb
->len
;
288 sch
->bstats
.packets
++;
297 sfq_requeue(struct sk_buff
*skb
, struct Qdisc
* sch
)
299 struct sfq_sched_data
*q
= qdisc_priv(sch
);
300 unsigned hash
= sfq_hash(q
, skb
);
304 if (x
== SFQ_DEPTH
) {
305 q
->ht
[hash
] = x
= q
->dep
[SFQ_DEPTH
].next
;
308 sch
->qstats
.backlog
+= skb
->len
;
309 __skb_queue_head(&q
->qs
[x
], skb
);
311 if (q
->qs
[x
].qlen
== 1) { /* The flow is new */
312 if (q
->tail
== SFQ_DEPTH
) { /* It is the first flow */
315 q
->allot
[x
] = q
->quantum
;
317 q
->next
[x
] = q
->next
[q
->tail
];
318 q
->next
[q
->tail
] = x
;
322 if (++sch
->q
.qlen
< q
->limit
- 1) {
323 sch
->qstats
.requeues
++;
335 static struct sk_buff
*
336 sfq_dequeue(struct Qdisc
* sch
)
338 struct sfq_sched_data
*q
= qdisc_priv(sch
);
342 /* No active slots */
343 if (q
->tail
== SFQ_DEPTH
)
346 a
= old_a
= q
->next
[q
->tail
];
349 skb
= __skb_dequeue(&q
->qs
[a
]);
352 sch
->qstats
.backlog
-= skb
->len
;
354 /* Is the slot empty? */
355 if (q
->qs
[a
].qlen
== 0) {
356 q
->ht
[q
->hash
[a
]] = SFQ_DEPTH
;
362 q
->next
[q
->tail
] = a
;
363 q
->allot
[a
] += q
->quantum
;
364 } else if ((q
->allot
[a
] -= skb
->len
) <= 0) {
367 q
->allot
[a
] += q
->quantum
;
373 sfq_reset(struct Qdisc
* sch
)
377 while ((skb
= sfq_dequeue(sch
)) != NULL
)
381 static void sfq_perturbation(unsigned long arg
)
383 struct Qdisc
*sch
= (struct Qdisc
*)arg
;
384 struct sfq_sched_data
*q
= qdisc_priv(sch
);
386 q
->perturbation
= net_random()&0x1F;
388 if (q
->perturb_period
) {
389 q
->perturb_timer
.expires
= jiffies
+ q
->perturb_period
;
390 add_timer(&q
->perturb_timer
);
394 static int sfq_change(struct Qdisc
*sch
, struct rtattr
*opt
)
396 struct sfq_sched_data
*q
= qdisc_priv(sch
);
397 struct tc_sfq_qopt
*ctl
= RTA_DATA(opt
);
400 if (opt
->rta_len
< RTA_LENGTH(sizeof(*ctl
)))
404 q
->quantum
= ctl
->quantum
? : psched_mtu(sch
->dev
);
405 q
->perturb_period
= ctl
->perturb_period
*HZ
;
407 q
->limit
= min_t(u32
, ctl
->limit
, SFQ_DEPTH
);
410 while (sch
->q
.qlen
>= q
->limit
-1)
412 qdisc_tree_decrease_qlen(sch
, qlen
- sch
->q
.qlen
);
414 del_timer(&q
->perturb_timer
);
415 if (q
->perturb_period
) {
416 q
->perturb_timer
.expires
= jiffies
+ q
->perturb_period
;
417 add_timer(&q
->perturb_timer
);
419 sch_tree_unlock(sch
);
423 static int sfq_init(struct Qdisc
*sch
, struct rtattr
*opt
)
425 struct sfq_sched_data
*q
= qdisc_priv(sch
);
428 init_timer(&q
->perturb_timer
);
429 q
->perturb_timer
.data
= (unsigned long)sch
;
430 q
->perturb_timer
.function
= sfq_perturbation
;
432 for (i
=0; i
<SFQ_HASH_DIVISOR
; i
++)
433 q
->ht
[i
] = SFQ_DEPTH
;
434 for (i
=0; i
<SFQ_DEPTH
; i
++) {
435 skb_queue_head_init(&q
->qs
[i
]);
436 q
->dep
[i
+SFQ_DEPTH
].next
= i
+SFQ_DEPTH
;
437 q
->dep
[i
+SFQ_DEPTH
].prev
= i
+SFQ_DEPTH
;
439 q
->limit
= SFQ_DEPTH
;
443 q
->quantum
= psched_mtu(sch
->dev
);
444 q
->perturb_period
= 0;
446 int err
= sfq_change(sch
, opt
);
450 for (i
=0; i
<SFQ_DEPTH
; i
++)
455 static void sfq_destroy(struct Qdisc
*sch
)
457 struct sfq_sched_data
*q
= qdisc_priv(sch
);
458 del_timer(&q
->perturb_timer
);
461 static int sfq_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
463 struct sfq_sched_data
*q
= qdisc_priv(sch
);
464 unsigned char *b
= skb
->tail
;
465 struct tc_sfq_qopt opt
;
467 opt
.quantum
= q
->quantum
;
468 opt
.perturb_period
= q
->perturb_period
/HZ
;
470 opt
.limit
= q
->limit
;
471 opt
.divisor
= SFQ_HASH_DIVISOR
;
472 opt
.flows
= q
->limit
;
474 RTA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
479 skb_trim(skb
, b
- skb
->data
);
483 static struct Qdisc_ops sfq_qdisc_ops
= {
487 .priv_size
= sizeof(struct sfq_sched_data
),
488 .enqueue
= sfq_enqueue
,
489 .dequeue
= sfq_dequeue
,
490 .requeue
= sfq_requeue
,
494 .destroy
= sfq_destroy
,
497 .owner
= THIS_MODULE
,
500 static int __init
sfq_module_init(void)
502 return register_qdisc(&sfq_qdisc_ops
);
504 static void __exit
sfq_module_exit(void)
506 unregister_qdisc(&sfq_qdisc_ops
);
508 module_init(sfq_module_init
)
509 module_exit(sfq_module_exit
)
510 MODULE_LICENSE("GPL");