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 <net/netlink.h>
34 #include <linux/ipv6.h>
35 #include <net/route.h>
36 #include <linux/skbuff.h>
38 #include <net/pkt_sched.h>
41 /* Stochastic Fairness Queuing algorithm.
42 =======================================
45 Paul E. McKenney "Stochastic Fairness Queuing",
46 IEEE INFOCOMM'90 Proceedings, San Francisco, 1990.
48 Paul E. McKenney "Stochastic Fairness Queuing",
49 "Interworking: Research and Experience", v.2, 1991, p.113-131.
53 M. Shreedhar and George Varghese "Efficient Fair
54 Queuing using Deficit Round Robin", Proc. SIGCOMM 95.
57 This is not the thing that is usually called (W)FQ nowadays.
58 It does not use any timestamp mechanism, but instead
59 processes queues in round-robin order.
63 - It is very cheap. Both CPU and memory requirements are minimal.
67 - "Stochastic" -> It is not 100% fair.
68 When hash collisions occur, several flows are considered as one.
70 - "Round-robin" -> It introduces larger delays than virtual clock
71 based schemes, and should not be used for isolating interactive
72 traffic from non-interactive. It means, that this scheduler
73 should be used as leaf of CBQ or P3, which put interactive traffic
74 to higher priority band.
76 We still need true WFQ for top level CSZ, but using WFQ
77 for the best effort traffic is absolutely pointless:
78 SFQ is superior for this purpose.
81 This implementation limits maximal queue length to 128;
82 maximal mtu to 2^15-1; number of hash buckets to 1024.
83 The only goal of this restrictions was that all data
84 fit into one 4K page :-). Struct sfq_sched_data is
85 organized in anti-cache manner: all the data for a bucket
86 are scattered over different locations. This is not good,
87 but it allowed me to put it into 4K.
89 It is easy to increase these values, but not in flight. */
92 #define SFQ_HASH_DIVISOR 1024
94 /* This type should contain at least SFQ_DEPTH*2 values */
95 typedef unsigned char sfq_index
;
103 struct sfq_sched_data
107 unsigned quantum
; /* Allotment per round: MUST BE >= MTU */
111 struct timer_list perturb_timer
;
113 sfq_index tail
; /* Index of current slot in round */
114 sfq_index max_depth
; /* Maximal depth */
116 sfq_index ht
[SFQ_HASH_DIVISOR
]; /* Hash table */
117 sfq_index next
[SFQ_DEPTH
]; /* Active slots link */
118 short allot
[SFQ_DEPTH
]; /* Current allotment per slot */
119 unsigned short hash
[SFQ_DEPTH
]; /* Hash value indexed by slots */
120 struct sk_buff_head qs
[SFQ_DEPTH
]; /* Slot queue */
121 struct sfq_head dep
[SFQ_DEPTH
*2]; /* Linked list of slots, indexed by depth */
124 static __inline__
unsigned sfq_fold_hash(struct sfq_sched_data
*q
, u32 h
, u32 h1
)
126 int pert
= q
->perturbation
;
128 /* Have we any rotation primitives? If not, WHY? */
129 h
^= (h1
<<pert
) ^ (h1
>>(0x1F - pert
));
134 static unsigned sfq_hash(struct sfq_sched_data
*q
, struct sk_buff
*skb
)
138 switch (skb
->protocol
) {
139 case __constant_htons(ETH_P_IP
):
141 const struct iphdr
*iph
= ip_hdr(skb
);
143 h2
= iph
->saddr
^iph
->protocol
;
144 if (!(iph
->frag_off
&htons(IP_MF
|IP_OFFSET
)) &&
145 (iph
->protocol
== IPPROTO_TCP
||
146 iph
->protocol
== IPPROTO_UDP
||
147 iph
->protocol
== IPPROTO_UDPLITE
||
148 iph
->protocol
== IPPROTO_SCTP
||
149 iph
->protocol
== IPPROTO_DCCP
||
150 iph
->protocol
== IPPROTO_ESP
))
151 h2
^= *(((u32
*)iph
) + iph
->ihl
);
154 case __constant_htons(ETH_P_IPV6
):
156 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
157 h
= iph
->daddr
.s6_addr32
[3];
158 h2
= iph
->saddr
.s6_addr32
[3]^iph
->nexthdr
;
159 if (iph
->nexthdr
== IPPROTO_TCP
||
160 iph
->nexthdr
== IPPROTO_UDP
||
161 iph
->nexthdr
== IPPROTO_UDPLITE
||
162 iph
->nexthdr
== IPPROTO_SCTP
||
163 iph
->nexthdr
== IPPROTO_DCCP
||
164 iph
->nexthdr
== IPPROTO_ESP
)
165 h2
^= *(u32
*)&iph
[1];
169 h
= (u32
)(unsigned long)skb
->dst
^skb
->protocol
;
170 h2
= (u32
)(unsigned long)skb
->sk
;
172 return sfq_fold_hash(q
, h
, h2
);
175 static inline void sfq_link(struct sfq_sched_data
*q
, sfq_index x
)
178 int d
= q
->qs
[x
].qlen
+ SFQ_DEPTH
;
184 q
->dep
[p
].next
= q
->dep
[n
].prev
= x
;
187 static inline void sfq_dec(struct sfq_sched_data
*q
, sfq_index x
)
196 if (n
== p
&& q
->max_depth
== q
->qs
[x
].qlen
+ 1)
202 static inline void sfq_inc(struct sfq_sched_data
*q
, sfq_index x
)
212 if (q
->max_depth
< d
)
218 static unsigned int sfq_drop(struct Qdisc
*sch
)
220 struct sfq_sched_data
*q
= qdisc_priv(sch
);
221 sfq_index d
= q
->max_depth
;
225 /* Queue is full! Find the longest slot and
226 drop a packet from it */
229 sfq_index x
= q
->dep
[d
+SFQ_DEPTH
].next
;
232 __skb_unlink(skb
, &q
->qs
[x
]);
237 sch
->qstats
.backlog
-= len
;
242 /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
243 d
= q
->next
[q
->tail
];
244 q
->next
[q
->tail
] = q
->next
[d
];
245 q
->allot
[q
->next
[d
]] += q
->quantum
;
248 __skb_unlink(skb
, &q
->qs
[d
]);
252 q
->ht
[q
->hash
[d
]] = SFQ_DEPTH
;
254 sch
->qstats
.backlog
-= len
;
262 sfq_enqueue(struct sk_buff
*skb
, struct Qdisc
* sch
)
264 struct sfq_sched_data
*q
= qdisc_priv(sch
);
265 unsigned hash
= sfq_hash(q
, skb
);
269 if (x
== SFQ_DEPTH
) {
270 q
->ht
[hash
] = x
= q
->dep
[SFQ_DEPTH
].next
;
273 sch
->qstats
.backlog
+= skb
->len
;
274 __skb_queue_tail(&q
->qs
[x
], skb
);
276 if (q
->qs
[x
].qlen
== 1) { /* The flow is new */
277 if (q
->tail
== SFQ_DEPTH
) { /* It is the first flow */
280 q
->allot
[x
] = q
->quantum
;
282 q
->next
[x
] = q
->next
[q
->tail
];
283 q
->next
[q
->tail
] = x
;
287 if (++sch
->q
.qlen
< q
->limit
-1) {
288 sch
->bstats
.bytes
+= skb
->len
;
289 sch
->bstats
.packets
++;
298 sfq_requeue(struct sk_buff
*skb
, struct Qdisc
* sch
)
300 struct sfq_sched_data
*q
= qdisc_priv(sch
);
301 unsigned hash
= sfq_hash(q
, skb
);
305 if (x
== SFQ_DEPTH
) {
306 q
->ht
[hash
] = x
= q
->dep
[SFQ_DEPTH
].next
;
309 sch
->qstats
.backlog
+= skb
->len
;
310 __skb_queue_head(&q
->qs
[x
], skb
);
312 if (q
->qs
[x
].qlen
== 1) { /* The flow is new */
313 if (q
->tail
== SFQ_DEPTH
) { /* It is the first flow */
316 q
->allot
[x
] = q
->quantum
;
318 q
->next
[x
] = q
->next
[q
->tail
];
319 q
->next
[q
->tail
] = x
;
323 if (++sch
->q
.qlen
< q
->limit
- 1) {
324 sch
->qstats
.requeues
++;
336 static struct sk_buff
*
337 sfq_dequeue(struct Qdisc
* sch
)
339 struct sfq_sched_data
*q
= qdisc_priv(sch
);
343 /* No active slots */
344 if (q
->tail
== SFQ_DEPTH
)
347 a
= old_a
= q
->next
[q
->tail
];
350 skb
= __skb_dequeue(&q
->qs
[a
]);
353 sch
->qstats
.backlog
-= skb
->len
;
355 /* Is the slot empty? */
356 if (q
->qs
[a
].qlen
== 0) {
357 q
->ht
[q
->hash
[a
]] = SFQ_DEPTH
;
363 q
->next
[q
->tail
] = a
;
364 q
->allot
[a
] += q
->quantum
;
365 } else if ((q
->allot
[a
] -= skb
->len
) <= 0) {
368 q
->allot
[a
] += q
->quantum
;
374 sfq_reset(struct Qdisc
* sch
)
378 while ((skb
= sfq_dequeue(sch
)) != NULL
)
382 static void sfq_perturbation(unsigned long arg
)
384 struct Qdisc
*sch
= (struct Qdisc
*)arg
;
385 struct sfq_sched_data
*q
= qdisc_priv(sch
);
387 q
->perturbation
= net_random()&0x1F;
389 if (q
->perturb_period
) {
390 q
->perturb_timer
.expires
= jiffies
+ q
->perturb_period
;
391 add_timer(&q
->perturb_timer
);
395 static int sfq_change(struct Qdisc
*sch
, struct rtattr
*opt
)
397 struct sfq_sched_data
*q
= qdisc_priv(sch
);
398 struct tc_sfq_qopt
*ctl
= RTA_DATA(opt
);
401 if (opt
->rta_len
< RTA_LENGTH(sizeof(*ctl
)))
405 q
->quantum
= ctl
->quantum
? : psched_mtu(sch
->dev
);
406 q
->perturb_period
= ctl
->perturb_period
*HZ
;
408 q
->limit
= min_t(u32
, ctl
->limit
, SFQ_DEPTH
);
411 while (sch
->q
.qlen
>= q
->limit
-1)
413 qdisc_tree_decrease_qlen(sch
, qlen
- sch
->q
.qlen
);
415 del_timer(&q
->perturb_timer
);
416 if (q
->perturb_period
) {
417 q
->perturb_timer
.expires
= jiffies
+ q
->perturb_period
;
418 add_timer(&q
->perturb_timer
);
420 sch_tree_unlock(sch
);
424 static int sfq_init(struct Qdisc
*sch
, struct rtattr
*opt
)
426 struct sfq_sched_data
*q
= qdisc_priv(sch
);
429 init_timer(&q
->perturb_timer
);
430 q
->perturb_timer
.data
= (unsigned long)sch
;
431 q
->perturb_timer
.function
= sfq_perturbation
;
433 for (i
=0; i
<SFQ_HASH_DIVISOR
; i
++)
434 q
->ht
[i
] = SFQ_DEPTH
;
435 for (i
=0; i
<SFQ_DEPTH
; i
++) {
436 skb_queue_head_init(&q
->qs
[i
]);
437 q
->dep
[i
+SFQ_DEPTH
].next
= i
+SFQ_DEPTH
;
438 q
->dep
[i
+SFQ_DEPTH
].prev
= i
+SFQ_DEPTH
;
440 q
->limit
= SFQ_DEPTH
;
444 q
->quantum
= psched_mtu(sch
->dev
);
445 q
->perturb_period
= 0;
447 int err
= sfq_change(sch
, opt
);
451 for (i
=0; i
<SFQ_DEPTH
; i
++)
456 static void sfq_destroy(struct Qdisc
*sch
)
458 struct sfq_sched_data
*q
= qdisc_priv(sch
);
459 del_timer(&q
->perturb_timer
);
462 static int sfq_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
464 struct sfq_sched_data
*q
= qdisc_priv(sch
);
465 unsigned char *b
= skb_tail_pointer(skb
);
466 struct tc_sfq_qopt opt
;
468 opt
.quantum
= q
->quantum
;
469 opt
.perturb_period
= q
->perturb_period
/HZ
;
471 opt
.limit
= q
->limit
;
472 opt
.divisor
= SFQ_HASH_DIVISOR
;
473 opt
.flows
= q
->limit
;
475 RTA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
484 static struct Qdisc_ops sfq_qdisc_ops
= {
488 .priv_size
= sizeof(struct sfq_sched_data
),
489 .enqueue
= sfq_enqueue
,
490 .dequeue
= sfq_dequeue
,
491 .requeue
= sfq_requeue
,
495 .destroy
= sfq_destroy
,
498 .owner
= THIS_MODULE
,
501 static int __init
sfq_module_init(void)
503 return register_qdisc(&sfq_qdisc_ops
);
505 static void __exit
sfq_module_exit(void)
507 unregister_qdisc(&sfq_qdisc_ops
);
509 module_init(sfq_module_init
)
510 module_exit(sfq_module_exit
)
511 MODULE_LICENSE("GPL");