1 diff -urN linux-2.4.34/Documentation/Configure.help linux-2.4.34/Documentation/Configure.help
2 --- linux-2.4.34/Documentation/Configure.help 2007-05-10 19:37:42.000000000 -0400
3 +++ linux-2.4.34/Documentation/Configure.help 2007-05-10 19:49:49.000000000 -0400
4 @@ -11127,6 +11127,24 @@
5 whenever you want). If you want to compile it as a module, say M
6 here and read <file:Documentation/modules.txt>.
10 + Say Y here if you want to use the Stochastic Fairness Queueing (SFQ)
11 + packet scheduling algorithm for some of your network devices or as a
12 + leaf discipline for the CBQ scheduling algorithm (see the top of
13 + <file:net/sched/sch_esfq.c> for details and references about the SFQ
16 + This is an enchanced SFQ version which allows you to control the
17 + hardcoded values in the SFQ scheduler: queue depth, hash table size,
18 + queues limit. Also adds control to the hash function used to identify
19 + packet flows. Hash by src or dst ip and original sfq hash.
21 + This code is also available as a module called sch_esfq.o ( = code
22 + which can be inserted in and removed from the running kernel
23 + whenever you want). If you want to compile it as a module, say M
24 + here and read <file:Documentation/modules.txt>.
28 Say Y here if you want to use the Clark-Shenker-Zhang (CSZ) packet
29 diff -urN linux-2.4.34/include/linux/pkt_sched.h linux-2.4.34/include/linux/pkt_sched.h
30 --- linux-2.4.34/include/linux/pkt_sched.h 2007-05-10 19:38:19.000000000 -0400
31 +++ linux-2.4.34/include/linux/pkt_sched.h 2007-05-10 19:53:59.000000000 -0400
34 * The only reason for this is efficiency, it is possible
35 * to change these parameters in compile time.
37 + * If you need to play with these values use esfq instead.
45 + TCA_SFQ_HASH_CLASSIC,
49 + TCA_SFQ_HASH_CTORIGDST,
50 + TCA_SFQ_HASH_CTORIGSRC,
51 + TCA_SFQ_HASH_CTREPLDST,
52 + TCA_SFQ_HASH_CTREPLSRC,
53 + TCA_SFQ_HASH_CTNATCHG,
58 + unsigned quantum; /* Bytes per round allocated to flow */
59 + int perturb_period; /* Period of hash perturbation */
60 + __u32 limit; /* Maximal packets in queue */
61 + unsigned divisor; /* Hash divisor */
62 + unsigned flows; /* Maximal number of flows */
63 + unsigned hash_kind; /* Hash function to use for flow identification */
69 diff -urN linux-2.4.34/net/sched/Config.in linux-2.4.34/net/sched/Config.in
70 --- linux-2.4.34/net/sched/Config.in 2007-05-10 19:38:31.000000000 -0400
71 +++ linux-2.4.34/net/sched/Config.in 2007-05-10 19:54:45.000000000 -0400
73 tristate ' The simplest PRIO pseudoscheduler' CONFIG_NET_SCH_PRIO
74 tristate ' RED queue' CONFIG_NET_SCH_RED
75 tristate ' SFQ queue' CONFIG_NET_SCH_SFQ
76 +tristate ' ESFQ queue' CONFIG_NET_SCH_ESFQ
77 tristate ' TEQL queue' CONFIG_NET_SCH_TEQL
78 tristate ' TBF queue' CONFIG_NET_SCH_TBF
79 tristate ' GRED queue' CONFIG_NET_SCH_GRED
80 diff -urN linux-2.4.34/net/sched/Makefile linux-2.4.34/net/sched/Makefile
81 --- linux-2.4.34/net/sched/Makefile 2007-05-10 19:38:31.000000000 -0400
82 +++ linux-2.4.34/net/sched/Makefile 2007-05-10 19:55:13.000000000 -0400
84 obj-$(CONFIG_NET_SCH_HFSC) += sch_hfsc.o
85 obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o
86 obj-$(CONFIG_NET_SCH_SFQ) += sch_sfq.o
87 +obj-$(CONFIG_NET_SCH_ESFQ) += sch_esfq.o
88 obj-$(CONFIG_NET_SCH_RED) += sch_red.o
89 obj-$(CONFIG_NET_SCH_TBF) += sch_tbf.o
90 obj-$(CONFIG_NET_SCH_PRIO) += sch_prio.o
91 diff -urN linux-2.4.34/net/sched/sch_esfq.c linux-2.4.34/net/sched/sch_esfq.c
92 --- linux-2.4.34/net/sched/sch_esfq.c 1969-12-31 19:00:00.000000000 -0500
93 +++ linux-2.4.34/net/sched/sch_esfq.c 2007-05-10 19:57:15.000000000 -0400
96 + * net/sched/sch_esfq.c Extended Stochastic Fairness Queueing discipline.
98 + * This program is free software; you can redistribute it and/or
99 + * modify it under the terms of the GNU General Public License
100 + * as published by the Free Software Foundation; either version
101 + * 2 of the License, or (at your option) any later version.
103 + * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
105 + * Changes: Alexander Atanasov, <alex@ssi.bg>
106 + * Added dynamic depth,limit,divisor,hash_kind options.
107 + * Added dst and src hashes.
109 + * Alexander Clouter, <alex@digriz.org.uk>
110 + * Ported ESFQ to Linux 2.6.
112 + * Corey Hickey, <bugfood-c@fatooh.org>
113 + * Maintenance of the Linux 2.6 port.
114 + * Added fwmark hash (thanks to Robert Kurjata).
115 + * Added usage of jhash.
119 +#include <linux/config.h>
120 +#include <linux/module.h>
121 +#include <asm/uaccess.h>
122 +#include <asm/system.h>
123 +#include <linux/bitops.h>
124 +#include <linux/types.h>
125 +#include <linux/kernel.h>
126 +#include <linux/sched.h>
127 +#include <linux/string.h>
128 +#include <linux/mm.h>
129 +#include <linux/socket.h>
130 +#include <linux/sockios.h>
131 +#include <linux/in.h>
132 +#include <linux/errno.h>
133 +#include <linux/interrupt.h>
134 +#include <linux/if_ether.h>
135 +#include <linux/inet.h>
136 +#include <linux/netdevice.h>
137 +#include <linux/etherdevice.h>
138 +#include <linux/notifier.h>
139 +#include <linux/init.h>
141 +#include <net/route.h>
142 +#include <linux/skbuff.h>
143 +#include <net/sock.h>
144 +#include <net/pkt_sched.h>
145 +#include <linux/jhash.h>
147 +#define IPPROTO_DCCP 33
148 +#define qdisc_priv(q) ((void *)(q->data))
150 +#ifdef CONFIG_IP_NF_CONNTRACK
151 +/* #include <net/netfilter/nf_conntrack.h> */
152 +#include <linux/netfilter_ipv4/ip_conntrack.h>
155 +/* Stochastic Fairness Queuing algorithm.
156 + For more comments look at sch_sfq.c.
157 + The difference is that you can change limit, depth,
158 + hash table size and choose alternate hash types.
160 + classic: same as in sch_sfq.c
161 + dst: destination IP address
162 + src: source IP address
163 + ctorigdst: original destination IP address
164 + ctorigsrc: original source IP address
165 + ctrepldst: reply destination IP address
166 + ctreplsrc: reply source IP
167 + ctnatchg: use the address which changed via nat
172 +/* This type should contain at least SFQ_DEPTH*2 values */
173 +typedef unsigned int esfq_index;
181 +struct esfq_sched_data
184 + int perturb_period;
185 + unsigned quantum; /* Allotment per round: MUST BE >= MTU */
188 + unsigned hash_divisor;
189 + unsigned hash_kind;
191 + struct timer_list perturb_timer;
193 + esfq_index tail; /* Index of current slot in round */
194 + esfq_index max_depth; /* Maximal depth */
196 + esfq_index *ht; /* Hash table */
197 + esfq_index *next; /* Active slots link */
198 + short *allot; /* Current allotment per slot */
199 + unsigned short *hash; /* Hash value indexed by slots */
200 + struct sk_buff_head *qs; /* Slot queue */
201 + struct esfq_head *dep; /* Linked list of slots, indexed by depth */
202 + unsigned dyn_min; /* For dynamic divisor adjustment; minimum value seen */
203 + unsigned dyn_max; /* maximum value seen */
204 + unsigned dyn_range; /* saved range */
207 +/* This contains the info we will hash. */
208 +struct esfq_packet_info
210 + u32 proto; /* protocol or port */
211 + u32 src; /* source from packet header */
212 + u32 dst; /* destination from packet header */
213 + u32 ctorigsrc; /* original source from conntrack */
214 + u32 ctorigdst; /* original destination from conntrack */
215 + u32 ctreplsrc; /* reply source from conntrack */
216 + u32 ctrepldst; /* reply destination from conntrack */
219 +static __inline__ unsigned esfq_jhash_1word(struct esfq_sched_data *q,u32 a)
221 + return jhash_1word(a, q->perturbation) & (q->hash_divisor-1);
224 +static __inline__ unsigned esfq_jhash_2words(struct esfq_sched_data *q, u32 a, u32 b)
226 + return jhash_2words(a, b, q->perturbation) & (q->hash_divisor-1);
229 +static __inline__ unsigned esfq_jhash_3words(struct esfq_sched_data *q, u32 a, u32 b, u32 c)
231 + return jhash_3words(a, b, c, q->perturbation) & (q->hash_divisor-1);
235 +static unsigned esfq_hash(struct esfq_sched_data *q, struct sk_buff *skb)
237 + struct esfq_packet_info info;
238 +#ifdef CONFIG_IP_NF_CONNTRACK
239 + enum ip_conntrack_info ctinfo;
240 + struct ip_conntrack *ct = ip_conntrack_get(skb, &ctinfo);
243 + switch (skb->protocol) {
244 + case __constant_htons(ETH_P_IP):
246 + struct iphdr *iph = skb->nh.iph;
247 + info.dst = iph->daddr;
248 + info.src = iph->saddr;
249 + if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
250 + (iph->protocol == IPPROTO_TCP ||
251 + iph->protocol == IPPROTO_UDP ||
252 + iph->protocol == IPPROTO_SCTP ||
253 + iph->protocol == IPPROTO_DCCP ||
254 + iph->protocol == IPPROTO_ESP))
255 + info.proto = *(((u32*)iph) + iph->ihl);
257 + info.proto = iph->protocol;
261 + info.dst = (u32)(unsigned long)skb->dst;
262 + info.src = (u32)(unsigned long)skb->sk;
263 + info.proto = skb->protocol;
266 +#ifdef CONFIG_IP_NF_CONNTRACK
267 + /* defaults if there is no conntrack info */
268 + info.ctorigsrc = info.src;
269 + info.ctorigdst = info.dst;
270 + info.ctreplsrc = info.dst;
271 + info.ctrepldst = info.src;
272 + /* collect conntrack info */
275 + if (skb->protocol == __constant_htons(ETH_P_IP)) {
276 + info.ctorigsrc = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
277 + info.ctorigdst = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
278 + info.ctreplsrc = ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
279 + info.ctrepldst = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
284 + switch(q->hash_kind)
286 + case TCA_SFQ_HASH_CLASSIC:
287 + return esfq_jhash_3words(q, info.dst, info.src, info.proto);
288 + case TCA_SFQ_HASH_DST:
289 + return esfq_jhash_1word(q, info.dst);
290 + case TCA_SFQ_HASH_SRC:
291 + return esfq_jhash_1word(q, info.src);
292 +#ifdef CONFIG_IP_NF_CONNTRACK
293 + case TCA_SFQ_HASH_CTORIGDST:
294 + return esfq_jhash_1word(q, info.ctorigdst);
295 + case TCA_SFQ_HASH_CTORIGSRC:
296 + return esfq_jhash_1word(q, info.ctorigsrc);
297 + case TCA_SFQ_HASH_CTREPLDST:
298 + return esfq_jhash_1word(q, info.ctrepldst);
299 + case TCA_SFQ_HASH_CTREPLSRC:
300 + return esfq_jhash_1word(q, info.ctreplsrc);
301 + case TCA_SFQ_HASH_CTNATCHG:
303 + if (info.ctorigdst == info.ctreplsrc)
304 + return esfq_jhash_1word(q, info.ctorigsrc);
306 + return esfq_jhash_1word(q, info.ctreplsrc);
310 + if (net_ratelimit())
311 + printk(KERN_WARNING "ESFQ: Unknown hash method. Falling back to classic.\n");
313 + return esfq_jhash_3words(q, info.dst, info.src, info.proto);
316 +static inline void esfq_link(struct esfq_sched_data *q, esfq_index x)
319 + int d = q->qs[x].qlen + q->depth;
322 + n = q->dep[d].next;
323 + q->dep[x].next = n;
324 + q->dep[x].prev = p;
325 + q->dep[p].next = q->dep[n].prev = x;
328 +static inline void esfq_dec(struct esfq_sched_data *q, esfq_index x)
332 + n = q->dep[x].next;
333 + p = q->dep[x].prev;
334 + q->dep[p].next = n;
335 + q->dep[n].prev = p;
337 + if (n == p && q->max_depth == q->qs[x].qlen + 1)
343 +static inline void esfq_inc(struct esfq_sched_data *q, esfq_index x)
348 + n = q->dep[x].next;
349 + p = q->dep[x].prev;
350 + q->dep[p].next = n;
351 + q->dep[n].prev = p;
353 + if (q->max_depth < d)
359 +static unsigned int esfq_drop(struct Qdisc *sch)
361 + struct esfq_sched_data *q = qdisc_priv(sch);
362 + esfq_index d = q->max_depth;
363 + struct sk_buff *skb;
366 + /* Queue is full! Find the longest slot and
367 + drop a packet from it */
370 + esfq_index x = q->dep[d+q->depth].next;
371 + skb = q->qs[x].prev;
373 + __skb_unlink(skb, &q->qs[x]);
377 + sch->stats.drops++;
378 + sch->stats.backlog -= len;
383 + /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
384 + d = q->next[q->tail];
385 + q->next[q->tail] = q->next[d];
386 + q->allot[q->next[d]] += q->quantum;
387 + skb = q->qs[d].prev;
389 + __skb_unlink(skb, &q->qs[d]);
393 + q->ht[q->hash[d]] = q->depth;
394 + sch->stats.drops++;
395 + sch->stats.backlog -= len;
403 +esfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
405 + struct esfq_sched_data *q = qdisc_priv(sch);
406 + unsigned hash = esfq_hash(q, skb);
407 + unsigned depth = q->depth;
412 + q->ht[hash] = x = q->dep[depth].next;
415 + sch->stats.backlog += skb->len;
416 + __skb_queue_tail(&q->qs[x], skb);
418 + if (q->qs[x].qlen == 1) { /* The flow is new */
419 + if (q->tail == depth) { /* It is the first flow */
422 + q->allot[x] = q->quantum;
424 + q->next[x] = q->next[q->tail];
425 + q->next[q->tail] = x;
429 + if (++sch->q.qlen < q->limit-1) {
430 + sch->stats.bytes += skb->len;
431 + sch->stats.packets++;
436 + return NET_XMIT_CN;
440 +esfq_requeue(struct sk_buff *skb, struct Qdisc* sch)
442 + struct esfq_sched_data *q = qdisc_priv(sch);
443 + unsigned hash = esfq_hash(q, skb);
444 + unsigned depth = q->depth;
449 + q->ht[hash] = x = q->dep[depth].next;
452 + sch->stats.backlog += skb->len;
453 + __skb_queue_head(&q->qs[x], skb);
455 + if (q->qs[x].qlen == 1) { /* The flow is new */
456 + if (q->tail == depth) { /* It is the first flow */
459 + q->allot[x] = q->quantum;
461 + q->next[x] = q->next[q->tail];
462 + q->next[q->tail] = x;
466 + if (++sch->q.qlen < q->limit - 1) {
470 + sch->stats.drops++;
472 + return NET_XMIT_CN;
478 +static struct sk_buff *
479 +esfq_dequeue(struct Qdisc* sch)
481 + struct esfq_sched_data *q = qdisc_priv(sch);
482 + struct sk_buff *skb;
483 + unsigned depth = q->depth;
484 + esfq_index a, old_a;
486 + /* No active slots */
487 + if (q->tail == depth)
490 + a = old_a = q->next[q->tail];
493 + skb = __skb_dequeue(&q->qs[a]);
496 + sch->stats.backlog -= skb->len;
498 + /* Is the slot empty? */
499 + if (q->qs[a].qlen == 0) {
500 + q->ht[q->hash[a]] = depth;
506 + q->next[q->tail] = a;
507 + q->allot[a] += q->quantum;
508 + } else if ((q->allot[a] -= skb->len) <= 0) {
511 + q->allot[a] += q->quantum;
518 +esfq_reset(struct Qdisc* sch)
520 + struct sk_buff *skb;
522 + while ((skb = esfq_dequeue(sch)) != NULL)
526 +static void esfq_perturbation(unsigned long arg)
528 + struct Qdisc *sch = (struct Qdisc*)arg;
529 + struct esfq_sched_data *q = qdisc_priv(sch);
531 + q->perturbation = net_random()&0x1F;
533 + if (q->perturb_period) {
534 + q->perturb_timer.expires = jiffies + q->perturb_period;
535 + add_timer(&q->perturb_timer);
539 +static int esfq_change(struct Qdisc *sch, struct rtattr *opt)
541 + struct esfq_sched_data *q = qdisc_priv(sch);
542 + struct tc_esfq_qopt *ctl = RTA_DATA(opt);
543 + int old_perturb = q->perturb_period;
545 + if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
548 + sch_tree_lock(sch);
549 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
550 + q->perturb_period = ctl->perturb_period*HZ;
551 +// q->hash_divisor = ctl->divisor;
552 +// q->tail = q->limit = q->depth = ctl->flows;
555 + q->limit = min_t(u32, ctl->limit, q->depth);
557 + if (ctl->hash_kind) {
558 + q->hash_kind = ctl->hash_kind;
559 + if (q->hash_kind != TCA_SFQ_HASH_CLASSIC)
560 + q->perturb_period = 0;
563 + // is sch_tree_lock enough to do this ?
564 + while (sch->q.qlen >= q->limit-1)
568 + del_timer(&q->perturb_timer);
569 + if (q->perturb_period) {
570 + q->perturb_timer.expires = jiffies + q->perturb_period;
571 + add_timer(&q->perturb_timer);
573 + q->perturbation = 0;
575 + sch_tree_unlock(sch);
579 +static int esfq_init(struct Qdisc *sch, struct rtattr *opt)
581 + struct esfq_sched_data *q = qdisc_priv(sch);
582 + struct tc_esfq_qopt *ctl;
583 + esfq_index p = ~0U/2;
586 + if (opt && opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
589 + init_timer(&q->perturb_timer);
590 + q->perturb_timer.data = (unsigned long)sch;
591 + q->perturb_timer.function = esfq_perturbation;
592 + q->perturbation = 0;
593 + q->hash_kind = TCA_SFQ_HASH_CLASSIC;
595 + q->dyn_min = ~0U; /* maximum value for this type */
596 + q->dyn_max = 0; /* dyn_min/dyn_max will be set properly upon first packet */
598 + q->quantum = psched_mtu(sch->dev);
599 + q->perturb_period = 0;
600 + q->hash_divisor = 1024;
601 + q->tail = q->limit = q->depth = 128;
604 + ctl = RTA_DATA(opt);
605 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
606 + q->perturb_period = ctl->perturb_period*HZ;
607 + q->hash_divisor = ctl->divisor ? : 1024;
608 + q->tail = q->limit = q->depth = ctl->flows ? : 128;
610 + if ( q->depth > p - 1 )
614 + q->limit = min_t(u32, ctl->limit, q->depth);
616 + if (ctl->hash_kind) {
617 + q->hash_kind = ctl->hash_kind;
620 + if (q->perturb_period) {
621 + q->perturb_timer.expires = jiffies + q->perturb_period;
622 + add_timer(&q->perturb_timer);
626 + q->ht = kmalloc(q->hash_divisor*sizeof(esfq_index), GFP_KERNEL);
630 + q->dep = kmalloc((1+q->depth*2)*sizeof(struct esfq_head), GFP_KERNEL);
633 + q->next = kmalloc(q->depth*sizeof(esfq_index), GFP_KERNEL);
637 + q->allot = kmalloc(q->depth*sizeof(short), GFP_KERNEL);
640 + q->hash = kmalloc(q->depth*sizeof(unsigned short), GFP_KERNEL);
643 + q->qs = kmalloc(q->depth*sizeof(struct sk_buff_head), GFP_KERNEL);
647 + for (i=0; i< q->hash_divisor; i++)
648 + q->ht[i] = q->depth;
649 + for (i=0; i<q->depth; i++) {
650 + skb_queue_head_init(&q->qs[i]);
651 + q->dep[i+q->depth].next = i+q->depth;
652 + q->dep[i+q->depth].prev = i+q->depth;
655 + for (i=0; i<q->depth; i++)
659 + del_timer(&q->perturb_timer);
675 +static void esfq_destroy(struct Qdisc *sch)
677 + struct esfq_sched_data *q = qdisc_priv(sch);
678 + del_timer(&q->perturb_timer);
693 +static int esfq_dump(struct Qdisc *sch, struct sk_buff *skb)
695 + struct esfq_sched_data *q = qdisc_priv(sch);
696 + unsigned char *b = skb->tail;
697 + struct tc_esfq_qopt opt;
699 + opt.quantum = q->quantum;
700 + opt.perturb_period = q->perturb_period/HZ;
702 + opt.limit = q->limit;
703 + opt.divisor = q->hash_divisor;
704 + opt.flows = q->depth;
705 + opt.hash_kind = q->hash_kind;
707 + RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
712 + skb_trim(skb, b - skb->data);
716 +static struct Qdisc_ops esfq_qdisc_ops =
721 + .priv_size = sizeof(struct esfq_sched_data),
722 + .enqueue = esfq_enqueue,
723 + .dequeue = esfq_dequeue,
724 + .requeue = esfq_requeue,
727 + .reset = esfq_reset,
728 + .destroy = esfq_destroy,
729 + .change = NULL, /* esfq_change - needs more work */
733 +static int __init esfq_module_init(void)
735 + return register_qdisc(&esfq_qdisc_ops);
737 +static void __exit esfq_module_exit(void)
739 + unregister_qdisc(&esfq_qdisc_ops);
741 +module_init(esfq_module_init)
742 +module_exit(esfq_module_exit)
743 +MODULE_LICENSE("GPL");