1 diff -Naur linux-2.6.13.3.old/include/linux/pkt_sched.h linux-2.6.13.3/include/linux/pkt_sched.h
2 --- linux-2.6.13.3.old/include/linux/pkt_sched.h 2005-10-23 17:39:16.000000000 -0700
3 +++ linux-2.6.13.3/include/linux/pkt_sched.h 2005-10-21 18:13:37.000000000 -0700
6 * The only reason for this is efficiency, it is possible
7 * to change these parameters in compile time.
9 + * If you need to play with these values use esfq instead.
17 + TCA_SFQ_HASH_CLASSIC,
20 + TCA_SFQ_HASH_FWMARK,
22 + TCA_SFQ_HASH_DSTDIR,
23 + TCA_SFQ_HASH_SRCDIR,
24 + TCA_SFQ_HASH_FWMARKDIR,
29 + unsigned quantum; /* Bytes per round allocated to flow */
30 + int perturb_period; /* Period of hash perturbation */
31 + __u32 limit; /* Maximal packets in queue */
32 + unsigned divisor; /* Hash divisor */
33 + unsigned flows; /* Maximal number of flows */
34 + unsigned hash_kind; /* Hash function to use for flow identification */
40 diff -Naur linux-2.6.13.3.old/net/sched/Kconfig linux-2.6.13.3/net/sched/Kconfig
41 --- linux-2.6.13.3.old/net/sched/Kconfig 2005-10-23 17:39:16.000000000 -0700
42 +++ linux-2.6.13.3/net/sched/Kconfig 2005-10-09 16:56:27.000000000 -0700
44 To compile this code as a module, choose M here: the
45 module will be called sch_sfq.
48 + tristate "ESFQ queue"
49 + depends on NET_SCHED
51 + Say Y here if you want to use the Enhanced Stochastic Fairness
52 + Queueing (ESFQ) packet scheduling algorithm for some of your network
53 + devices or as a leaf discipline for a classful qdisc such as HTB or
54 + CBQ (see the top of <file:net/sched/sch_esfq.c> for details and
55 + references to the SFQ algorithm).
57 + This is an enchanced SFQ version which allows you to control some
58 + hardcoded values in the SFQ scheduler: queue depth, hash table size,
61 + ESFQ also adds control to the hash function used to identify packet
62 + flows. The original SFQ hashes by individual flow (TCP session or UDP
63 + stream); ESFQ can hash by src or dst IP as well, which can be more
64 + fair to users in some networking situations.
66 + To compile this code as a module, choose M here: the
67 + module will be called sch_esfq.
72 diff -Naur linux-2.6.13.3.old/net/sched/Makefile linux-2.6.13.3/net/sched/Makefile
73 --- linux-2.6.13.3.old/net/sched/Makefile 2005-10-23 17:39:16.000000000 -0700
74 +++ linux-2.6.13.3/net/sched/Makefile 2005-10-09 16:56:27.000000000 -0700
76 obj-$(CONFIG_NET_SCH_INGRESS) += sch_ingress.o
77 obj-$(CONFIG_NET_SCH_DSMARK) += sch_dsmark.o
78 obj-$(CONFIG_NET_SCH_SFQ) += sch_sfq.o
79 +obj-$(CONFIG_NET_SCH_ESFQ) += sch_esfq.o
80 obj-$(CONFIG_NET_SCH_TBF) += sch_tbf.o
81 obj-$(CONFIG_NET_SCH_TEQL) += sch_teql.o
82 obj-$(CONFIG_NET_SCH_PRIO) += sch_prio.o
83 diff -Naur linux-2.6.13.3.old/net/sched/sch_esfq.c linux-2.6.13.3/net/sched/sch_esfq.c
84 --- linux-2.6.13.3.old/net/sched/sch_esfq.c 1969-12-31 16:00:00.000000000 -0800
85 +++ linux-2.6.13.3/net/sched/sch_esfq.c 2005-10-23 17:32:19.000000000 -0700
88 + * net/sched/sch_esfq.c Extended Stochastic Fairness Queueing discipline.
90 + * This program is free software; you can redistribute it and/or
91 + * modify it under the terms of the GNU General Public License
92 + * as published by the Free Software Foundation; either version
93 + * 2 of the License, or (at your option) any later version.
95 + * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
97 + * Changes: Alexander Atanasov, <alex@ssi.bg>
98 + * Added dynamic depth,limit,divisor,hash_kind options.
99 + * Added dst and src hashes.
101 + * Alexander Clouter, <alex@digriz.org.uk>
102 + * Ported ESFQ to Linux 2.6.
104 + * Corey Hickey, <bugfood-c@fatooh.org>
105 + * Maintenance of the Linux 2.6 port.
106 + * Added fwmark hash (thanks to Robert Kurjata)
107 + * Added direct hashing for src, dst, and fwmark.
111 +#include <linux/config.h>
112 +#include <linux/module.h>
113 +#include <asm/uaccess.h>
114 +#include <asm/system.h>
115 +#include <linux/bitops.h>
116 +#include <linux/types.h>
117 +#include <linux/kernel.h>
118 +#include <linux/jiffies.h>
119 +#include <linux/string.h>
120 +#include <linux/mm.h>
121 +#include <linux/socket.h>
122 +#include <linux/sockios.h>
123 +#include <linux/in.h>
124 +#include <linux/errno.h>
125 +#include <linux/interrupt.h>
126 +#include <linux/if_ether.h>
127 +#include <linux/inet.h>
128 +#include <linux/netdevice.h>
129 +#include <linux/etherdevice.h>
130 +#include <linux/notifier.h>
131 +#include <linux/init.h>
133 +#include <linux/ipv6.h>
134 +#include <net/route.h>
135 +#include <linux/skbuff.h>
136 +#include <net/sock.h>
137 +#include <net/pkt_sched.h>
140 +/* Stochastic Fairness Queuing algorithm.
141 + For more comments look at sch_sfq.c.
142 + The difference is that you can change limit, depth,
143 + hash table size and choose 7 hash types.
145 + classic: same as in sch_sfq.c
146 + dst: destination IP address
147 + src: source IP address
148 + fwmark: netfilter mark value
151 + fwmark_direct: direct hashing of the above sources
154 + make sfq_change work.
158 +/* This type should contain at least SFQ_DEPTH*2 values */
159 +typedef unsigned int esfq_index;
167 +struct esfq_sched_data
170 + int perturb_period;
171 + unsigned quantum; /* Allotment per round: MUST BE >= MTU */
174 + unsigned hash_divisor;
175 + unsigned hash_kind;
177 + struct timer_list perturb_timer;
179 + esfq_index tail; /* Index of current slot in round */
180 + esfq_index max_depth; /* Maximal depth */
182 + esfq_index *ht; /* Hash table */
183 + esfq_index *next; /* Active slots link */
184 + short *allot; /* Current allotment per slot */
185 + unsigned short *hash; /* Hash value indexed by slots */
186 + struct sk_buff_head *qs; /* Slot queue */
187 + struct esfq_head *dep; /* Linked list of slots, indexed by depth */
188 + unsigned dyn_min; /* For dynamic divisor adjustment; minimum value seen */
189 + unsigned dyn_max; /* maximum value seen */
190 + unsigned dyn_range; /* saved range */
193 +static __inline__ unsigned esfq_hash_u32(struct esfq_sched_data *q,u32 h)
195 + int pert = q->perturbation;
198 + h = (h<<pert) ^ (h>>(0x1F - pert));
200 + h = ntohl(h) * 2654435761UL;
201 + return h & (q->hash_divisor-1);
204 +/* Hash input values directly into the "nearest" slot, taking into account the
205 + * range of input values seen. This is most useful when the hash table is at
206 + * least as large as the range of possible values. */
207 +static __inline__ unsigned esfq_hash_direct(struct esfq_sched_data *q, u32 h)
209 + /* adjust minimum and maximum */
210 + if (h < q->dyn_min || h > q->dyn_max) {
211 + q->dyn_min = h < q->dyn_min ? h : q->dyn_min;
212 + q->dyn_max = h > q->dyn_max ? h : q->dyn_max;
214 + /* find new range */
215 + if ((q->dyn_range = q->dyn_max - q->dyn_min) >= q->hash_divisor)
216 + printk(KERN_WARNING "ESFQ: (direct hash) Input range %u is larger than hash "
217 + "table. See ESFQ README for details.\n", q->dyn_range);
220 + /* hash input values into slot numbers */
221 + if (q->dyn_min == q->dyn_max)
222 + return 0; /* only one value seen; avoid division by 0 */
224 + return (h - q->dyn_min) * (q->hash_divisor - 1) / q->dyn_range;
227 +static __inline__ unsigned esfq_fold_hash_classic(struct esfq_sched_data *q, u32 h, u32 h1)
229 + int pert = q->perturbation;
231 + /* Have we any rotation primitives? If not, WHY? */
232 + h ^= (h1<<pert) ^ (h1>>(0x1F - pert));
234 + return h & (q->hash_divisor-1);
237 +static unsigned esfq_hash(struct esfq_sched_data *q, struct sk_buff *skb)
243 + switch (skb->protocol) {
244 + case __constant_htons(ETH_P_IP):
246 + struct iphdr *iph = skb->nh.iph;
250 + h2 = hs^iph->protocol;
251 + if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
252 + (iph->protocol == IPPROTO_TCP ||
253 + iph->protocol == IPPROTO_UDP ||
254 + iph->protocol == IPPROTO_ESP))
255 + h2 ^= *(((u32*)iph) + iph->ihl);
258 + case __constant_htons(ETH_P_IPV6):
260 + struct ipv6hdr *iph = skb->nh.ipv6h;
261 + h = iph->daddr.s6_addr32[3];
262 + hs = iph->saddr.s6_addr32[3];
264 + h2 = hs^iph->nexthdr;
265 + if (iph->nexthdr == IPPROTO_TCP ||
266 + iph->nexthdr == IPPROTO_UDP ||
267 + iph->nexthdr == IPPROTO_ESP)
268 + h2 ^= *(u32*)&iph[1];
272 + h = (u32)(unsigned long)skb->dst;
273 + hs = (u32)(unsigned long)skb->sk;
275 + h2 = hs^skb->protocol;
277 + switch(q->hash_kind)
279 + case TCA_SFQ_HASH_CLASSIC:
280 + return esfq_fold_hash_classic(q, h, h2);
281 + case TCA_SFQ_HASH_DST:
282 + return esfq_hash_u32(q,h);
283 + case TCA_SFQ_HASH_DSTDIR:
284 + return esfq_hash_direct(q, ntohl(h));
285 + case TCA_SFQ_HASH_SRC:
286 + return esfq_hash_u32(q,hs);
287 + case TCA_SFQ_HASH_SRCDIR:
288 + return esfq_hash_direct(q, ntohl(hs));
289 +#ifdef CONFIG_NETFILTER
290 + case TCA_SFQ_HASH_FWMARK:
291 + return esfq_hash_u32(q,nfm);
292 + case TCA_SFQ_HASH_FWMARKDIR:
293 + return esfq_hash_direct(q,nfm);
296 + if (net_ratelimit())
297 + printk(KERN_WARNING "ESFQ: Unknown hash method. Falling back to classic.\n");
299 + return esfq_fold_hash_classic(q, h, h2);
302 +static inline void esfq_link(struct esfq_sched_data *q, esfq_index x)
305 + int d = q->qs[x].qlen + q->depth;
308 + n = q->dep[d].next;
309 + q->dep[x].next = n;
310 + q->dep[x].prev = p;
311 + q->dep[p].next = q->dep[n].prev = x;
314 +static inline void esfq_dec(struct esfq_sched_data *q, esfq_index x)
318 + n = q->dep[x].next;
319 + p = q->dep[x].prev;
320 + q->dep[p].next = n;
321 + q->dep[n].prev = p;
323 + if (n == p && q->max_depth == q->qs[x].qlen + 1)
329 +static inline void esfq_inc(struct esfq_sched_data *q, esfq_index x)
334 + n = q->dep[x].next;
335 + p = q->dep[x].prev;
336 + q->dep[p].next = n;
337 + q->dep[n].prev = p;
339 + if (q->max_depth < d)
345 +static unsigned int esfq_drop(struct Qdisc *sch)
347 + struct esfq_sched_data *q = qdisc_priv(sch);
348 + esfq_index d = q->max_depth;
349 + struct sk_buff *skb;
352 + /* Queue is full! Find the longest slot and
353 + drop a packet from it */
356 + esfq_index x = q->dep[d+q->depth].next;
357 + skb = q->qs[x].prev;
359 + __skb_unlink(skb, &q->qs[x]);
363 + sch->qstats.drops++;
368 + /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
369 + d = q->next[q->tail];
370 + q->next[q->tail] = q->next[d];
371 + q->allot[q->next[d]] += q->quantum;
372 + skb = q->qs[d].prev;
374 + __skb_unlink(skb, &q->qs[d]);
378 + q->ht[q->hash[d]] = q->depth;
379 + sch->qstats.drops++;
387 +esfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
389 + struct esfq_sched_data *q = qdisc_priv(sch);
390 + unsigned hash = esfq_hash(q, skb);
391 + unsigned depth = q->depth;
396 + q->ht[hash] = x = q->dep[depth].next;
399 + __skb_queue_tail(&q->qs[x], skb);
401 + if (q->qs[x].qlen == 1) { /* The flow is new */
402 + if (q->tail == depth) { /* It is the first flow */
405 + q->allot[x] = q->quantum;
407 + q->next[x] = q->next[q->tail];
408 + q->next[q->tail] = x;
412 + if (++sch->q.qlen < q->limit-1) {
413 + sch->bstats.bytes += skb->len;
414 + sch->bstats.packets++;
419 + return NET_XMIT_CN;
423 +esfq_requeue(struct sk_buff *skb, struct Qdisc* sch)
425 + struct esfq_sched_data *q = qdisc_priv(sch);
426 + unsigned hash = esfq_hash(q, skb);
427 + unsigned depth = q->depth;
432 + q->ht[hash] = x = q->dep[depth].next;
435 + __skb_queue_head(&q->qs[x], skb);
437 + if (q->qs[x].qlen == 1) { /* The flow is new */
438 + if (q->tail == depth) { /* It is the first flow */
441 + q->allot[x] = q->quantum;
443 + q->next[x] = q->next[q->tail];
444 + q->next[q->tail] = x;
448 + if (++sch->q.qlen < q->limit - 1) {
449 + sch->qstats.requeues++;
453 + sch->qstats.drops++;
455 + return NET_XMIT_CN;
461 +static struct sk_buff *
462 +esfq_dequeue(struct Qdisc* sch)
464 + struct esfq_sched_data *q = qdisc_priv(sch);
465 + struct sk_buff *skb;
466 + unsigned depth = q->depth;
467 + esfq_index a, old_a;
469 + /* No active slots */
470 + if (q->tail == depth)
473 + a = old_a = q->next[q->tail];
476 + skb = __skb_dequeue(&q->qs[a]);
480 + /* Is the slot empty? */
481 + if (q->qs[a].qlen == 0) {
482 + q->ht[q->hash[a]] = depth;
488 + q->next[q->tail] = a;
489 + q->allot[a] += q->quantum;
490 + } else if ((q->allot[a] -= skb->len) <= 0) {
493 + q->allot[a] += q->quantum;
500 +esfq_reset(struct Qdisc* sch)
502 + struct sk_buff *skb;
504 + while ((skb = esfq_dequeue(sch)) != NULL)
508 +static void esfq_perturbation(unsigned long arg)
510 + struct Qdisc *sch = (struct Qdisc*)arg;
511 + struct esfq_sched_data *q = qdisc_priv(sch);
513 + q->perturbation = net_random()&0x1F;
515 + if (q->perturb_period) {
516 + q->perturb_timer.expires = jiffies + q->perturb_period;
517 + add_timer(&q->perturb_timer);
521 +static int esfq_change(struct Qdisc *sch, struct rtattr *opt)
523 + struct esfq_sched_data *q = qdisc_priv(sch);
524 + struct tc_esfq_qopt *ctl = RTA_DATA(opt);
525 + int old_perturb = q->perturb_period;
527 + if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
530 + sch_tree_lock(sch);
531 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
532 + q->perturb_period = ctl->perturb_period*HZ;
533 +// q->hash_divisor = ctl->divisor;
534 +// q->tail = q->limit = q->depth = ctl->flows;
537 + q->limit = min_t(u32, ctl->limit, q->depth);
539 + if (ctl->hash_kind) {
540 + q->hash_kind = ctl->hash_kind;
541 + if (q->hash_kind != TCA_SFQ_HASH_CLASSIC)
542 + q->perturb_period = 0;
545 + // is sch_tree_lock enough to do this ?
546 + while (sch->q.qlen >= q->limit-1)
550 + del_timer(&q->perturb_timer);
551 + if (q->perturb_period) {
552 + q->perturb_timer.expires = jiffies + q->perturb_period;
553 + add_timer(&q->perturb_timer);
555 + q->perturbation = 0;
557 + sch_tree_unlock(sch);
561 +static int esfq_init(struct Qdisc *sch, struct rtattr *opt)
563 + struct esfq_sched_data *q = qdisc_priv(sch);
564 + struct tc_esfq_qopt *ctl;
565 + esfq_index p = ~0UL/2;
568 + if (opt && opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
571 + init_timer(&q->perturb_timer);
572 + q->perturb_timer.data = (unsigned long)sch;
573 + q->perturb_timer.function = esfq_perturbation;
574 + q->perturbation = 0;
575 + q->hash_kind = TCA_SFQ_HASH_CLASSIC;
577 + q->dyn_min = ~0U; /* maximum value for this type */
578 + q->dyn_max = 0; /* dyn_min/dyn_max will be set properly upon first packet */
580 + q->quantum = psched_mtu(sch->dev);
581 + q->perturb_period = 0;
582 + q->hash_divisor = 1024;
583 + q->tail = q->limit = q->depth = 128;
586 + ctl = RTA_DATA(opt);
587 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
588 + q->perturb_period = ctl->perturb_period*HZ;
589 + q->hash_divisor = ctl->divisor ? : 1024;
590 + q->tail = q->limit = q->depth = ctl->flows ? : 128;
592 + if ( q->depth > p - 1 )
596 + q->limit = min_t(u32, ctl->limit, q->depth);
598 + if (ctl->hash_kind) {
599 + q->hash_kind = ctl->hash_kind;
602 + if (q->perturb_period) {
603 + q->perturb_timer.expires = jiffies + q->perturb_period;
604 + add_timer(&q->perturb_timer);
608 + q->ht = kmalloc(q->hash_divisor*sizeof(esfq_index), GFP_KERNEL);
612 + q->dep = kmalloc((1+q->depth*2)*sizeof(struct esfq_head), GFP_KERNEL);
615 + q->next = kmalloc(q->depth*sizeof(esfq_index), GFP_KERNEL);
619 + q->allot = kmalloc(q->depth*sizeof(short), GFP_KERNEL);
622 + q->hash = kmalloc(q->depth*sizeof(unsigned short), GFP_KERNEL);
625 + q->qs = kmalloc(q->depth*sizeof(struct sk_buff_head), GFP_KERNEL);
629 + for (i=0; i< q->hash_divisor; i++)
630 + q->ht[i] = q->depth;
631 + for (i=0; i<q->depth; i++) {
632 + skb_queue_head_init(&q->qs[i]);
633 + q->dep[i+q->depth].next = i+q->depth;
634 + q->dep[i+q->depth].prev = i+q->depth;
637 + for (i=0; i<q->depth; i++)
656 +static void esfq_destroy(struct Qdisc *sch)
658 + struct esfq_sched_data *q = qdisc_priv(sch);
659 + del_timer(&q->perturb_timer);
674 +static int esfq_dump(struct Qdisc *sch, struct sk_buff *skb)
676 + struct esfq_sched_data *q = qdisc_priv(sch);
677 + unsigned char *b = skb->tail;
678 + struct tc_esfq_qopt opt;
680 + opt.quantum = q->quantum;
681 + opt.perturb_period = q->perturb_period/HZ;
683 + opt.limit = q->limit;
684 + opt.divisor = q->hash_divisor;
685 + opt.flows = q->depth;
686 + opt.hash_kind = q->hash_kind;
688 + RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
693 + skb_trim(skb, b - skb->data);
697 +static struct Qdisc_ops esfq_qdisc_ops =
702 + .priv_size = sizeof(struct esfq_sched_data),
703 + .enqueue = esfq_enqueue,
704 + .dequeue = esfq_dequeue,
705 + .requeue = esfq_requeue,
708 + .reset = esfq_reset,
709 + .destroy = esfq_destroy,
710 + .change = NULL, /* esfq_change - needs more work */
712 + .owner = THIS_MODULE,
715 +static int __init esfq_module_init(void)
717 + return register_qdisc(&esfq_qdisc_ops);
719 +static void __exit esfq_module_exit(void)
721 + unregister_qdisc(&esfq_qdisc_ops);
723 +module_init(esfq_module_init)
724 +module_exit(esfq_module_exit)
725 +MODULE_LICENSE("GPL");