1 Index: linux-2.4.35.4/Documentation/Configure.help
2 ===================================================================
3 --- linux-2.4.35.4.orig/Documentation/Configure.help
4 +++ linux-2.4.35.4/Documentation/Configure.help
5 @@ -11153,6 +11153,24 @@ CONFIG_NET_SCH_HFSC
6 whenever you want). If you want to compile it as a module, say M
7 here and read <file:Documentation/modules.txt>.
11 + Say Y here if you want to use the Stochastic Fairness Queueing (SFQ)
12 + packet scheduling algorithm for some of your network devices or as a
13 + leaf discipline for the CBQ scheduling algorithm (see the top of
14 + <file:net/sched/sch_esfq.c> for details and references about the SFQ
17 + This is an enchanced SFQ version which allows you to control the
18 + hardcoded values in the SFQ scheduler: queue depth, hash table size,
19 + queues limit. Also adds control to the hash function used to identify
20 + packet flows. Hash by src or dst ip and original sfq hash.
22 + This code is also available as a module called sch_esfq.o ( = code
23 + which can be inserted in and removed from the running kernel
24 + whenever you want). If you want to compile it as a module, say M
25 + here and read <file:Documentation/modules.txt>.
29 Say Y here if you want to use the Clark-Shenker-Zhang (CSZ) packet
30 Index: linux-2.4.35.4/include/linux/pkt_sched.h
31 ===================================================================
32 --- linux-2.4.35.4.orig/include/linux/pkt_sched.h
33 +++ linux-2.4.35.4/include/linux/pkt_sched.h
34 @@ -173,8 +173,36 @@ struct tc_sfq_qopt
36 * The only reason for this is efficiency, it is possible
37 * to change these parameters in compile time.
39 + * If you need to play with these values use esfq instead.
47 + TCA_SFQ_HASH_CLASSIC,
51 + TCA_SFQ_HASH_CTORIGDST,
52 + TCA_SFQ_HASH_CTORIGSRC,
53 + TCA_SFQ_HASH_CTREPLDST,
54 + TCA_SFQ_HASH_CTREPLSRC,
55 + TCA_SFQ_HASH_CTNATCHG,
60 + unsigned quantum; /* Bytes per round allocated to flow */
61 + int perturb_period; /* Period of hash perturbation */
62 + __u32 limit; /* Maximal packets in queue */
63 + unsigned divisor; /* Hash divisor */
64 + unsigned flows; /* Maximal number of flows */
65 + unsigned hash_kind; /* Hash function to use for flow identification */
71 Index: linux-2.4.35.4/net/sched/Config.in
72 ===================================================================
73 --- linux-2.4.35.4.orig/net/sched/Config.in
74 +++ linux-2.4.35.4/net/sched/Config.in
76 tristate ' The simplest PRIO pseudoscheduler' CONFIG_NET_SCH_PRIO
77 tristate ' RED queue' CONFIG_NET_SCH_RED
78 tristate ' SFQ queue' CONFIG_NET_SCH_SFQ
79 +tristate ' ESFQ queue' CONFIG_NET_SCH_ESFQ
80 tristate ' TEQL queue' CONFIG_NET_SCH_TEQL
81 tristate ' TBF queue' CONFIG_NET_SCH_TBF
82 tristate ' GRED queue' CONFIG_NET_SCH_GRED
83 Index: linux-2.4.35.4/net/sched/Makefile
84 ===================================================================
85 --- linux-2.4.35.4.orig/net/sched/Makefile
86 +++ linux-2.4.35.4/net/sched/Makefile
87 @@ -19,6 +19,7 @@ obj-$(CONFIG_NET_SCH_HPFQ) += sch_hpfq.o
88 obj-$(CONFIG_NET_SCH_HFSC) += sch_hfsc.o
89 obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o
90 obj-$(CONFIG_NET_SCH_SFQ) += sch_sfq.o
91 +obj-$(CONFIG_NET_SCH_ESFQ) += sch_esfq.o
92 obj-$(CONFIG_NET_SCH_RED) += sch_red.o
93 obj-$(CONFIG_NET_SCH_TBF) += sch_tbf.o
94 obj-$(CONFIG_NET_SCH_PRIO) += sch_prio.o
95 Index: linux-2.4.35.4/net/sched/sch_esfq.c
96 ===================================================================
98 +++ linux-2.4.35.4/net/sched/sch_esfq.c
101 + * net/sched/sch_esfq.c Extended Stochastic Fairness Queueing discipline.
103 + * This program is free software; you can redistribute it and/or
104 + * modify it under the terms of the GNU General Public License
105 + * as published by the Free Software Foundation; either version
106 + * 2 of the License, or (at your option) any later version.
108 + * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
110 + * Changes: Alexander Atanasov, <alex@ssi.bg>
111 + * Added dynamic depth,limit,divisor,hash_kind options.
112 + * Added dst and src hashes.
114 + * Alexander Clouter, <alex@digriz.org.uk>
115 + * Ported ESFQ to Linux 2.6.
117 + * Corey Hickey, <bugfood-c@fatooh.org>
118 + * Maintenance of the Linux 2.6 port.
119 + * Added fwmark hash (thanks to Robert Kurjata).
120 + * Added usage of jhash.
124 +#include <linux/config.h>
125 +#include <linux/module.h>
126 +#include <asm/uaccess.h>
127 +#include <asm/system.h>
128 +#include <linux/bitops.h>
129 +#include <linux/types.h>
130 +#include <linux/kernel.h>
131 +#include <linux/sched.h>
132 +#include <linux/string.h>
133 +#include <linux/mm.h>
134 +#include <linux/socket.h>
135 +#include <linux/sockios.h>
136 +#include <linux/in.h>
137 +#include <linux/errno.h>
138 +#include <linux/interrupt.h>
139 +#include <linux/if_ether.h>
140 +#include <linux/inet.h>
141 +#include <linux/netdevice.h>
142 +#include <linux/etherdevice.h>
143 +#include <linux/notifier.h>
144 +#include <linux/init.h>
146 +#include <net/route.h>
147 +#include <linux/skbuff.h>
148 +#include <net/sock.h>
149 +#include <net/pkt_sched.h>
150 +#include <linux/jhash.h>
152 +#define IPPROTO_DCCP 33
153 +#define qdisc_priv(q) ((void *)(q->data))
155 +#ifdef CONFIG_IP_NF_CONNTRACK
156 +/* #include <net/netfilter/nf_conntrack.h> */
157 +#include <linux/netfilter_ipv4/ip_conntrack.h>
160 +/* Stochastic Fairness Queuing algorithm.
161 + For more comments look at sch_sfq.c.
162 + The difference is that you can change limit, depth,
163 + hash table size and choose alternate hash types.
165 + classic: same as in sch_sfq.c
166 + dst: destination IP address
167 + src: source IP address
168 + ctorigdst: original destination IP address
169 + ctorigsrc: original source IP address
170 + ctrepldst: reply destination IP address
171 + ctreplsrc: reply source IP
172 + ctnatchg: use the address which changed via nat
177 +/* This type should contain at least SFQ_DEPTH*2 values */
178 +typedef unsigned int esfq_index;
186 +struct esfq_sched_data
189 + int perturb_period;
190 + unsigned quantum; /* Allotment per round: MUST BE >= MTU */
193 + unsigned hash_divisor;
194 + unsigned hash_kind;
196 + struct timer_list perturb_timer;
198 + esfq_index tail; /* Index of current slot in round */
199 + esfq_index max_depth; /* Maximal depth */
201 + esfq_index *ht; /* Hash table */
202 + esfq_index *next; /* Active slots link */
203 + short *allot; /* Current allotment per slot */
204 + unsigned short *hash; /* Hash value indexed by slots */
205 + struct sk_buff_head *qs; /* Slot queue */
206 + struct esfq_head *dep; /* Linked list of slots, indexed by depth */
207 + unsigned dyn_min; /* For dynamic divisor adjustment; minimum value seen */
208 + unsigned dyn_max; /* maximum value seen */
209 + unsigned dyn_range; /* saved range */
212 +/* This contains the info we will hash. */
213 +struct esfq_packet_info
215 + u32 proto; /* protocol or port */
216 + u32 src; /* source from packet header */
217 + u32 dst; /* destination from packet header */
218 + u32 ctorigsrc; /* original source from conntrack */
219 + u32 ctorigdst; /* original destination from conntrack */
220 + u32 ctreplsrc; /* reply source from conntrack */
221 + u32 ctrepldst; /* reply destination from conntrack */
224 +static __inline__ unsigned esfq_jhash_1word(struct esfq_sched_data *q,u32 a)
226 + return jhash_1word(a, q->perturbation) & (q->hash_divisor-1);
229 +static __inline__ unsigned esfq_jhash_2words(struct esfq_sched_data *q, u32 a, u32 b)
231 + return jhash_2words(a, b, q->perturbation) & (q->hash_divisor-1);
234 +static __inline__ unsigned esfq_jhash_3words(struct esfq_sched_data *q, u32 a, u32 b, u32 c)
236 + return jhash_3words(a, b, c, q->perturbation) & (q->hash_divisor-1);
240 +static unsigned esfq_hash(struct esfq_sched_data *q, struct sk_buff *skb)
242 + struct esfq_packet_info info;
243 +#ifdef CONFIG_IP_NF_CONNTRACK
244 + enum ip_conntrack_info ctinfo;
245 + struct ip_conntrack *ct = ip_conntrack_get(skb, &ctinfo);
248 + switch (skb->protocol) {
249 + case __constant_htons(ETH_P_IP):
251 + struct iphdr *iph = skb->nh.iph;
252 + info.dst = iph->daddr;
253 + info.src = iph->saddr;
254 + if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
255 + (iph->protocol == IPPROTO_TCP ||
256 + iph->protocol == IPPROTO_UDP ||
257 + iph->protocol == IPPROTO_SCTP ||
258 + iph->protocol == IPPROTO_DCCP ||
259 + iph->protocol == IPPROTO_ESP))
260 + info.proto = *(((u32*)iph) + iph->ihl);
262 + info.proto = iph->protocol;
266 + info.dst = (u32)(unsigned long)skb->dst;
267 + info.src = (u32)(unsigned long)skb->sk;
268 + info.proto = skb->protocol;
271 +#ifdef CONFIG_IP_NF_CONNTRACK
272 + /* defaults if there is no conntrack info */
273 + info.ctorigsrc = info.src;
274 + info.ctorigdst = info.dst;
275 + info.ctreplsrc = info.dst;
276 + info.ctrepldst = info.src;
277 + /* collect conntrack info */
280 + if (skb->protocol == __constant_htons(ETH_P_IP)) {
281 + info.ctorigsrc = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
282 + info.ctorigdst = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
283 + info.ctreplsrc = ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
284 + info.ctrepldst = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
289 + switch(q->hash_kind)
291 + case TCA_SFQ_HASH_CLASSIC:
292 + return esfq_jhash_3words(q, info.dst, info.src, info.proto);
293 + case TCA_SFQ_HASH_DST:
294 + return esfq_jhash_1word(q, info.dst);
295 + case TCA_SFQ_HASH_SRC:
296 + return esfq_jhash_1word(q, info.src);
297 +#ifdef CONFIG_IP_NF_CONNTRACK
298 + case TCA_SFQ_HASH_CTORIGDST:
299 + return esfq_jhash_1word(q, info.ctorigdst);
300 + case TCA_SFQ_HASH_CTORIGSRC:
301 + return esfq_jhash_1word(q, info.ctorigsrc);
302 + case TCA_SFQ_HASH_CTREPLDST:
303 + return esfq_jhash_1word(q, info.ctrepldst);
304 + case TCA_SFQ_HASH_CTREPLSRC:
305 + return esfq_jhash_1word(q, info.ctreplsrc);
306 + case TCA_SFQ_HASH_CTNATCHG:
308 + if (info.ctorigdst == info.ctreplsrc)
309 + return esfq_jhash_1word(q, info.ctorigsrc);
311 + return esfq_jhash_1word(q, info.ctreplsrc);
315 + if (net_ratelimit())
316 + printk(KERN_WARNING "ESFQ: Unknown hash method. Falling back to classic.\n");
318 + return esfq_jhash_3words(q, info.dst, info.src, info.proto);
321 +static inline void esfq_link(struct esfq_sched_data *q, esfq_index x)
324 + int d = q->qs[x].qlen + q->depth;
327 + n = q->dep[d].next;
328 + q->dep[x].next = n;
329 + q->dep[x].prev = p;
330 + q->dep[p].next = q->dep[n].prev = x;
333 +static inline void esfq_dec(struct esfq_sched_data *q, esfq_index x)
337 + n = q->dep[x].next;
338 + p = q->dep[x].prev;
339 + q->dep[p].next = n;
340 + q->dep[n].prev = p;
342 + if (n == p && q->max_depth == q->qs[x].qlen + 1)
348 +static inline void esfq_inc(struct esfq_sched_data *q, esfq_index x)
353 + n = q->dep[x].next;
354 + p = q->dep[x].prev;
355 + q->dep[p].next = n;
356 + q->dep[n].prev = p;
358 + if (q->max_depth < d)
364 +static unsigned int esfq_drop(struct Qdisc *sch)
366 + struct esfq_sched_data *q = qdisc_priv(sch);
367 + esfq_index d = q->max_depth;
368 + struct sk_buff *skb;
371 + /* Queue is full! Find the longest slot and
372 + drop a packet from it */
375 + esfq_index x = q->dep[d+q->depth].next;
376 + skb = q->qs[x].prev;
378 + __skb_unlink(skb, &q->qs[x]);
382 + sch->stats.drops++;
383 + sch->stats.backlog -= len;
388 + /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
389 + d = q->next[q->tail];
390 + q->next[q->tail] = q->next[d];
391 + q->allot[q->next[d]] += q->quantum;
392 + skb = q->qs[d].prev;
394 + __skb_unlink(skb, &q->qs[d]);
398 + q->ht[q->hash[d]] = q->depth;
399 + sch->stats.drops++;
400 + sch->stats.backlog -= len;
408 +esfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
410 + struct esfq_sched_data *q = qdisc_priv(sch);
411 + unsigned hash = esfq_hash(q, skb);
412 + unsigned depth = q->depth;
417 + q->ht[hash] = x = q->dep[depth].next;
420 + sch->stats.backlog += skb->len;
421 + __skb_queue_tail(&q->qs[x], skb);
423 + if (q->qs[x].qlen == 1) { /* The flow is new */
424 + if (q->tail == depth) { /* It is the first flow */
427 + q->allot[x] = q->quantum;
429 + q->next[x] = q->next[q->tail];
430 + q->next[q->tail] = x;
434 + if (++sch->q.qlen < q->limit-1) {
435 + sch->stats.bytes += skb->len;
436 + sch->stats.packets++;
441 + return NET_XMIT_CN;
445 +esfq_requeue(struct sk_buff *skb, struct Qdisc* sch)
447 + struct esfq_sched_data *q = qdisc_priv(sch);
448 + unsigned hash = esfq_hash(q, skb);
449 + unsigned depth = q->depth;
454 + q->ht[hash] = x = q->dep[depth].next;
457 + sch->stats.backlog += skb->len;
458 + __skb_queue_head(&q->qs[x], skb);
460 + if (q->qs[x].qlen == 1) { /* The flow is new */
461 + if (q->tail == depth) { /* It is the first flow */
464 + q->allot[x] = q->quantum;
466 + q->next[x] = q->next[q->tail];
467 + q->next[q->tail] = x;
471 + if (++sch->q.qlen < q->limit - 1) {
475 + sch->stats.drops++;
477 + return NET_XMIT_CN;
483 +static struct sk_buff *
484 +esfq_dequeue(struct Qdisc* sch)
486 + struct esfq_sched_data *q = qdisc_priv(sch);
487 + struct sk_buff *skb;
488 + unsigned depth = q->depth;
489 + esfq_index a, old_a;
491 + /* No active slots */
492 + if (q->tail == depth)
495 + a = old_a = q->next[q->tail];
498 + skb = __skb_dequeue(&q->qs[a]);
501 + sch->stats.backlog -= skb->len;
503 + /* Is the slot empty? */
504 + if (q->qs[a].qlen == 0) {
505 + q->ht[q->hash[a]] = depth;
511 + q->next[q->tail] = a;
512 + q->allot[a] += q->quantum;
513 + } else if ((q->allot[a] -= skb->len) <= 0) {
516 + q->allot[a] += q->quantum;
523 +esfq_reset(struct Qdisc* sch)
525 + struct sk_buff *skb;
527 + while ((skb = esfq_dequeue(sch)) != NULL)
531 +static void esfq_perturbation(unsigned long arg)
533 + struct Qdisc *sch = (struct Qdisc*)arg;
534 + struct esfq_sched_data *q = qdisc_priv(sch);
536 + q->perturbation = net_random()&0x1F;
538 + if (q->perturb_period) {
539 + q->perturb_timer.expires = jiffies + q->perturb_period;
540 + add_timer(&q->perturb_timer);
544 +static int esfq_change(struct Qdisc *sch, struct rtattr *opt)
546 + struct esfq_sched_data *q = qdisc_priv(sch);
547 + struct tc_esfq_qopt *ctl = RTA_DATA(opt);
548 + int old_perturb = q->perturb_period;
550 + if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
553 + sch_tree_lock(sch);
554 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
555 + q->perturb_period = ctl->perturb_period*HZ;
556 +// q->hash_divisor = ctl->divisor;
557 +// q->tail = q->limit = q->depth = ctl->flows;
560 + q->limit = min_t(u32, ctl->limit, q->depth);
562 + if (ctl->hash_kind) {
563 + q->hash_kind = ctl->hash_kind;
564 + if (q->hash_kind != TCA_SFQ_HASH_CLASSIC)
565 + q->perturb_period = 0;
568 + // is sch_tree_lock enough to do this ?
569 + while (sch->q.qlen >= q->limit-1)
573 + del_timer(&q->perturb_timer);
574 + if (q->perturb_period) {
575 + q->perturb_timer.expires = jiffies + q->perturb_period;
576 + add_timer(&q->perturb_timer);
578 + q->perturbation = 0;
580 + sch_tree_unlock(sch);
584 +static int esfq_init(struct Qdisc *sch, struct rtattr *opt)
586 + struct esfq_sched_data *q = qdisc_priv(sch);
587 + struct tc_esfq_qopt *ctl;
588 + esfq_index p = ~0U/2;
591 + if (opt && opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
594 + init_timer(&q->perturb_timer);
595 + q->perturb_timer.data = (unsigned long)sch;
596 + q->perturb_timer.function = esfq_perturbation;
597 + q->perturbation = 0;
598 + q->hash_kind = TCA_SFQ_HASH_CLASSIC;
600 + q->dyn_min = ~0U; /* maximum value for this type */
601 + q->dyn_max = 0; /* dyn_min/dyn_max will be set properly upon first packet */
603 + q->quantum = psched_mtu(sch->dev);
604 + q->perturb_period = 0;
605 + q->hash_divisor = 1024;
606 + q->tail = q->limit = q->depth = 128;
609 + ctl = RTA_DATA(opt);
610 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
611 + q->perturb_period = ctl->perturb_period*HZ;
612 + q->hash_divisor = ctl->divisor ? : 1024;
613 + q->tail = q->limit = q->depth = ctl->flows ? : 128;
615 + if ( q->depth > p - 1 )
619 + q->limit = min_t(u32, ctl->limit, q->depth);
621 + if (ctl->hash_kind) {
622 + q->hash_kind = ctl->hash_kind;
625 + if (q->perturb_period) {
626 + q->perturb_timer.expires = jiffies + q->perturb_period;
627 + add_timer(&q->perturb_timer);
631 + q->ht = kmalloc(q->hash_divisor*sizeof(esfq_index), GFP_KERNEL);
635 + q->dep = kmalloc((1+q->depth*2)*sizeof(struct esfq_head), GFP_KERNEL);
638 + q->next = kmalloc(q->depth*sizeof(esfq_index), GFP_KERNEL);
642 + q->allot = kmalloc(q->depth*sizeof(short), GFP_KERNEL);
645 + q->hash = kmalloc(q->depth*sizeof(unsigned short), GFP_KERNEL);
648 + q->qs = kmalloc(q->depth*sizeof(struct sk_buff_head), GFP_KERNEL);
652 + for (i=0; i< q->hash_divisor; i++)
653 + q->ht[i] = q->depth;
654 + for (i=0; i<q->depth; i++) {
655 + skb_queue_head_init(&q->qs[i]);
656 + q->dep[i+q->depth].next = i+q->depth;
657 + q->dep[i+q->depth].prev = i+q->depth;
660 + for (i=0; i<q->depth; i++)
664 + del_timer(&q->perturb_timer);
680 +static void esfq_destroy(struct Qdisc *sch)
682 + struct esfq_sched_data *q = qdisc_priv(sch);
683 + del_timer(&q->perturb_timer);
698 +static int esfq_dump(struct Qdisc *sch, struct sk_buff *skb)
700 + struct esfq_sched_data *q = qdisc_priv(sch);
701 + unsigned char *b = skb->tail;
702 + struct tc_esfq_qopt opt;
704 + opt.quantum = q->quantum;
705 + opt.perturb_period = q->perturb_period/HZ;
707 + opt.limit = q->limit;
708 + opt.divisor = q->hash_divisor;
709 + opt.flows = q->depth;
710 + opt.hash_kind = q->hash_kind;
712 + RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
717 + skb_trim(skb, b - skb->data);
721 +static struct Qdisc_ops esfq_qdisc_ops =
726 + .priv_size = sizeof(struct esfq_sched_data),
727 + .enqueue = esfq_enqueue,
728 + .dequeue = esfq_dequeue,
729 + .requeue = esfq_requeue,
732 + .reset = esfq_reset,
733 + .destroy = esfq_destroy,
734 + .change = NULL, /* esfq_change - needs more work */
738 +static int __init esfq_module_init(void)
740 + return register_qdisc(&esfq_qdisc_ops);
742 +static void __exit esfq_module_exit(void)
744 + unregister_qdisc(&esfq_qdisc_ops);
746 +module_init(esfq_module_init)
747 +module_exit(esfq_module_exit)
748 +MODULE_LICENSE("GPL");