1 diff -urN linux-2.6.19.old/include/linux/pkt_sched.h linux-2.6.19.dev/include/linux/pkt_sched.h
2 --- linux-2.6.19.old/include/linux/pkt_sched.h 2006-11-29 22:57:37.000000000 +0100
3 +++ linux-2.6.19.dev/include/linux/pkt_sched.h 2006-12-14 03:13:51.000000000 +0100
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 -urN linux-2.6.19.old/net/sched/Kconfig linux-2.6.19.dev/net/sched/Kconfig
41 --- linux-2.6.19.old/net/sched/Kconfig 2006-11-29 22:57:37.000000000 +0100
42 +++ linux-2.6.19.dev/net/sched/Kconfig 2006-12-14 03:13:51.000000000 +0100
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.
70 tristate "True Link Equalizer (TEQL)"
72 diff -urN linux-2.6.19.old/net/sched/Makefile linux-2.6.19.dev/net/sched/Makefile
73 --- linux-2.6.19.old/net/sched/Makefile 2006-11-29 22:57:37.000000000 +0100
74 +++ linux-2.6.19.dev/net/sched/Makefile 2006-12-14 03:13:51.000000000 +0100
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 -urN linux-2.6.19.old/net/sched/sch_esfq.c linux-2.6.19.dev/net/sched/sch_esfq.c
84 --- linux-2.6.19.old/net/sched/sch_esfq.c 1970-01-01 01:00:00.000000000 +0100
85 +++ linux-2.6.19.dev/net/sched/sch_esfq.c 2006-12-14 03:13:51.000000000 +0100
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/autoconf.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_SCTP ||
255 + iph->protocol == IPPROTO_DCCP ||
256 + iph->protocol == IPPROTO_ESP))
257 + h2 ^= *(((u32*)iph) + iph->ihl);
260 + case __constant_htons(ETH_P_IPV6):
262 + struct ipv6hdr *iph = skb->nh.ipv6h;
263 + h = iph->daddr.s6_addr32[3];
264 + hs = iph->saddr.s6_addr32[3];
266 + h2 = hs^iph->nexthdr;
267 + if (iph->nexthdr == IPPROTO_TCP ||
268 + iph->nexthdr == IPPROTO_UDP ||
269 + iph->nexthdr == IPPROTO_SCTP ||
270 + iph->nexthdr == IPPROTO_DCCP ||
271 + iph->nexthdr == IPPROTO_ESP)
272 + h2 ^= *(u32*)&iph[1];
276 + h = (u32)(unsigned long)skb->dst;
277 + hs = (u32)(unsigned long)skb->sk;
279 + h2 = hs^skb->protocol;
281 + switch(q->hash_kind)
283 + case TCA_SFQ_HASH_CLASSIC:
284 + return esfq_fold_hash_classic(q, h, h2);
285 + case TCA_SFQ_HASH_DST:
286 + return esfq_hash_u32(q,h);
287 + case TCA_SFQ_HASH_DSTDIR:
288 + return esfq_hash_direct(q, ntohl(h));
289 + case TCA_SFQ_HASH_SRC:
290 + return esfq_hash_u32(q,hs);
291 + case TCA_SFQ_HASH_SRCDIR:
292 + return esfq_hash_direct(q, ntohl(hs));
293 +#ifdef CONFIG_NETFILTER
294 + case TCA_SFQ_HASH_FWMARK:
295 + return esfq_hash_u32(q,nfm);
296 + case TCA_SFQ_HASH_FWMARKDIR:
297 + return esfq_hash_direct(q,nfm);
300 + if (net_ratelimit())
301 + printk(KERN_WARNING "ESFQ: Unknown hash method. Falling back to classic.\n");
303 + return esfq_fold_hash_classic(q, h, h2);
306 +static inline void esfq_link(struct esfq_sched_data *q, esfq_index x)
309 + int d = q->qs[x].qlen + q->depth;
312 + n = q->dep[d].next;
313 + q->dep[x].next = n;
314 + q->dep[x].prev = p;
315 + q->dep[p].next = q->dep[n].prev = x;
318 +static inline void esfq_dec(struct esfq_sched_data *q, esfq_index x)
322 + n = q->dep[x].next;
323 + p = q->dep[x].prev;
324 + q->dep[p].next = n;
325 + q->dep[n].prev = p;
327 + if (n == p && q->max_depth == q->qs[x].qlen + 1)
333 +static inline void esfq_inc(struct esfq_sched_data *q, esfq_index x)
338 + n = q->dep[x].next;
339 + p = q->dep[x].prev;
340 + q->dep[p].next = n;
341 + q->dep[n].prev = p;
343 + if (q->max_depth < d)
349 +static unsigned int esfq_drop(struct Qdisc *sch)
351 + struct esfq_sched_data *q = qdisc_priv(sch);
352 + esfq_index d = q->max_depth;
353 + struct sk_buff *skb;
356 + /* Queue is full! Find the longest slot and
357 + drop a packet from it */
360 + esfq_index x = q->dep[d+q->depth].next;
361 + skb = q->qs[x].prev;
363 + __skb_unlink(skb, &q->qs[x]);
367 + sch->qstats.drops++;
372 + /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
373 + d = q->next[q->tail];
374 + q->next[q->tail] = q->next[d];
375 + q->allot[q->next[d]] += q->quantum;
376 + skb = q->qs[d].prev;
378 + __skb_unlink(skb, &q->qs[d]);
382 + q->ht[q->hash[d]] = q->depth;
383 + sch->qstats.drops++;
391 +esfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
393 + struct esfq_sched_data *q = qdisc_priv(sch);
394 + unsigned hash = esfq_hash(q, skb);
395 + unsigned depth = q->depth;
400 + q->ht[hash] = x = q->dep[depth].next;
403 + __skb_queue_tail(&q->qs[x], skb);
405 + if (q->qs[x].qlen == 1) { /* The flow is new */
406 + if (q->tail == depth) { /* It is the first flow */
409 + q->allot[x] = q->quantum;
411 + q->next[x] = q->next[q->tail];
412 + q->next[q->tail] = x;
416 + if (++sch->q.qlen < q->limit-1) {
417 + sch->bstats.bytes += skb->len;
418 + sch->bstats.packets++;
423 + return NET_XMIT_CN;
427 +esfq_requeue(struct sk_buff *skb, struct Qdisc* sch)
429 + struct esfq_sched_data *q = qdisc_priv(sch);
430 + unsigned hash = esfq_hash(q, skb);
431 + unsigned depth = q->depth;
436 + q->ht[hash] = x = q->dep[depth].next;
439 + __skb_queue_head(&q->qs[x], skb);
441 + if (q->qs[x].qlen == 1) { /* The flow is new */
442 + if (q->tail == depth) { /* It is the first flow */
445 + q->allot[x] = q->quantum;
447 + q->next[x] = q->next[q->tail];
448 + q->next[q->tail] = x;
452 + if (++sch->q.qlen < q->limit - 1) {
453 + sch->qstats.requeues++;
457 + sch->qstats.drops++;
459 + return NET_XMIT_CN;
465 +static struct sk_buff *
466 +esfq_dequeue(struct Qdisc* sch)
468 + struct esfq_sched_data *q = qdisc_priv(sch);
469 + struct sk_buff *skb;
470 + unsigned depth = q->depth;
471 + esfq_index a, old_a;
473 + /* No active slots */
474 + if (q->tail == depth)
477 + a = old_a = q->next[q->tail];
480 + skb = __skb_dequeue(&q->qs[a]);
484 + /* Is the slot empty? */
485 + if (q->qs[a].qlen == 0) {
486 + q->ht[q->hash[a]] = depth;
492 + q->next[q->tail] = a;
493 + q->allot[a] += q->quantum;
494 + } else if ((q->allot[a] -= skb->len) <= 0) {
497 + q->allot[a] += q->quantum;
504 +esfq_reset(struct Qdisc* sch)
506 + struct sk_buff *skb;
508 + while ((skb = esfq_dequeue(sch)) != NULL)
512 +static void esfq_perturbation(unsigned long arg)
514 + struct Qdisc *sch = (struct Qdisc*)arg;
515 + struct esfq_sched_data *q = qdisc_priv(sch);
517 + q->perturbation = net_random()&0x1F;
519 + if (q->perturb_period) {
520 + q->perturb_timer.expires = jiffies + q->perturb_period;
521 + add_timer(&q->perturb_timer);
525 +static int esfq_change(struct Qdisc *sch, struct rtattr *opt)
527 + struct esfq_sched_data *q = qdisc_priv(sch);
528 + struct tc_esfq_qopt *ctl = RTA_DATA(opt);
529 + int old_perturb = q->perturb_period;
531 + if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
534 + sch_tree_lock(sch);
535 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
536 + q->perturb_period = ctl->perturb_period*HZ;
537 +// q->hash_divisor = ctl->divisor;
538 +// q->tail = q->limit = q->depth = ctl->flows;
541 + q->limit = min_t(u32, ctl->limit, q->depth);
543 + if (ctl->hash_kind) {
544 + q->hash_kind = ctl->hash_kind;
545 + if (q->hash_kind != TCA_SFQ_HASH_CLASSIC)
546 + q->perturb_period = 0;
549 + // is sch_tree_lock enough to do this ?
550 + while (sch->q.qlen >= q->limit-1)
554 + del_timer(&q->perturb_timer);
555 + if (q->perturb_period) {
556 + q->perturb_timer.expires = jiffies + q->perturb_period;
557 + add_timer(&q->perturb_timer);
559 + q->perturbation = 0;
561 + sch_tree_unlock(sch);
565 +static int esfq_init(struct Qdisc *sch, struct rtattr *opt)
567 + struct esfq_sched_data *q = qdisc_priv(sch);
568 + struct tc_esfq_qopt *ctl;
569 + esfq_index p = ~0UL/2;
572 + if (opt && opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
575 + init_timer(&q->perturb_timer);
576 + q->perturb_timer.data = (unsigned long)sch;
577 + q->perturb_timer.function = esfq_perturbation;
578 + q->perturbation = 0;
579 + q->hash_kind = TCA_SFQ_HASH_CLASSIC;
581 + q->dyn_min = ~0U; /* maximum value for this type */
582 + q->dyn_max = 0; /* dyn_min/dyn_max will be set properly upon first packet */
584 + q->quantum = psched_mtu(sch->dev);
585 + q->perturb_period = 0;
586 + q->hash_divisor = 1024;
587 + q->tail = q->limit = q->depth = 128;
590 + ctl = RTA_DATA(opt);
591 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
592 + q->perturb_period = ctl->perturb_period*HZ;
593 + q->hash_divisor = ctl->divisor ? : 1024;
594 + q->tail = q->limit = q->depth = ctl->flows ? : 128;
596 + if ( q->depth > p - 1 )
600 + q->limit = min_t(u32, ctl->limit, q->depth);
602 + if (ctl->hash_kind) {
603 + q->hash_kind = ctl->hash_kind;
606 + if (q->perturb_period) {
607 + q->perturb_timer.expires = jiffies + q->perturb_period;
608 + add_timer(&q->perturb_timer);
612 + q->ht = kmalloc(q->hash_divisor*sizeof(esfq_index), GFP_KERNEL);
616 + q->dep = kmalloc((1+q->depth*2)*sizeof(struct esfq_head), GFP_KERNEL);
619 + q->next = kmalloc(q->depth*sizeof(esfq_index), GFP_KERNEL);
623 + q->allot = kmalloc(q->depth*sizeof(short), GFP_KERNEL);
626 + q->hash = kmalloc(q->depth*sizeof(unsigned short), GFP_KERNEL);
629 + q->qs = kmalloc(q->depth*sizeof(struct sk_buff_head), GFP_KERNEL);
633 + for (i=0; i< q->hash_divisor; i++)
634 + q->ht[i] = q->depth;
635 + for (i=0; i<q->depth; i++) {
636 + skb_queue_head_init(&q->qs[i]);
637 + q->dep[i+q->depth].next = i+q->depth;
638 + q->dep[i+q->depth].prev = i+q->depth;
641 + for (i=0; i<q->depth; i++)
645 + del_timer(&q->perturb_timer);
661 +static void esfq_destroy(struct Qdisc *sch)
663 + struct esfq_sched_data *q = qdisc_priv(sch);
664 + del_timer(&q->perturb_timer);
679 +static int esfq_dump(struct Qdisc *sch, struct sk_buff *skb)
681 + struct esfq_sched_data *q = qdisc_priv(sch);
682 + unsigned char *b = skb->tail;
683 + struct tc_esfq_qopt opt;
685 + opt.quantum = q->quantum;
686 + opt.perturb_period = q->perturb_period/HZ;
688 + opt.limit = q->limit;
689 + opt.divisor = q->hash_divisor;
690 + opt.flows = q->depth;
691 + opt.hash_kind = q->hash_kind;
693 + RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
698 + skb_trim(skb, b - skb->data);
702 +static struct Qdisc_ops esfq_qdisc_ops =
707 + .priv_size = sizeof(struct esfq_sched_data),
708 + .enqueue = esfq_enqueue,
709 + .dequeue = esfq_dequeue,
710 + .requeue = esfq_requeue,
713 + .reset = esfq_reset,
714 + .destroy = esfq_destroy,
715 + .change = NULL, /* esfq_change - needs more work */
717 + .owner = THIS_MODULE,
720 +static int __init esfq_module_init(void)
722 + return register_qdisc(&esfq_qdisc_ops);
724 +static void __exit esfq_module_exit(void)
726 + unregister_qdisc(&esfq_qdisc_ops);
728 +module_init(esfq_module_init)
729 +module_exit(esfq_module_exit)
730 +MODULE_LICENSE("GPL");