prepare for the transition to linux 2.6.22 - make it possible to override the kernel...
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.22 / 200-sched_esfq.patch
1 diff -urN linux-2.6.21.1.old/include/linux/pkt_sched.h linux-2.6.21.1.dev/include/linux/pkt_sched.h
2 --- linux-2.6.21.1.old/include/linux/pkt_sched.h        2007-04-27 23:49:26.000000000 +0200
3 +++ linux-2.6.21.1.dev/include/linux/pkt_sched.h        2007-05-26 20:43:12.530587320 +0200
4 @@ -146,8 +146,40 @@
5   *
6   *     The only reason for this is efficiency, it is possible
7   *     to change these parameters in compile time.
8 + *     
9 + *     If you need to play with these values use esfq instead.
10   */
11  
12 +/* ESFQ section */
13 +
14 +enum
15 +{
16 +        /* traditional */
17 +       TCA_SFQ_HASH_CLASSIC,
18 +       TCA_SFQ_HASH_DST,
19 +       TCA_SFQ_HASH_SRC,
20 +       TCA_SFQ_HASH_FWMARK,
21 +        /* direct */
22 +       TCA_SFQ_HASH_DSTDIR,
23 +       TCA_SFQ_HASH_SRCDIR,
24 +       TCA_SFQ_HASH_FWMARKDIR,
25 +       /* conntrack */
26 +       TCA_SFQ_HASH_CTORIGDST,
27 +       TCA_SFQ_HASH_CTORIGSRC,
28 +       TCA_SFQ_HASH_CTREPLDST,
29 +       TCA_SFQ_HASH_CTREPLSRC,
30 +};
31 +
32 +struct tc_esfq_qopt
33 +{
34 +       unsigned        quantum;        /* Bytes per round allocated to flow */
35 +       int             perturb_period; /* Period of hash perturbation */
36 +       __u32           limit;          /* Maximal packets in queue */
37 +       unsigned        divisor;        /* Hash divisor  */
38 +       unsigned        flows;          /* Maximal number of flows  */
39 +       unsigned        hash_kind;      /* Hash function to use for flow identification */
40 +};
41 +
42  /* RED section */
43  
44  enum
45 diff -urN linux-2.6.21.1.old/net/sched/Kconfig linux-2.6.21.1.dev/net/sched/Kconfig
46 --- linux-2.6.21.1.old/net/sched/Kconfig        2007-04-27 23:49:26.000000000 +0200
47 +++ linux-2.6.21.1.dev/net/sched/Kconfig        2007-05-26 20:43:12.572580936 +0200
48 @@ -133,6 +133,26 @@
49           To compile this code as a module, choose M here: the
50           module will be called sch_sfq.
51  
52 +config NET_SCH_ESFQ
53 +       tristate "Enhanced Stochastic Fairness Queueing (ESFQ)"
54 +       ---help---
55 +         Say Y here if you want to use the Enhanced Stochastic Fairness
56 +         Queueing (ESFQ) packet scheduling algorithm for some of your network
57 +         devices or as a leaf discipline for a classful qdisc such as HTB or
58 +         CBQ (see the top of <file:net/sched/sch_esfq.c> for details and
59 +         references to the SFQ algorithm).
60 +
61 +         This is an enchanced SFQ version which allows you to control some
62 +         hardcoded values in the SFQ scheduler.
63 +
64 +         ESFQ also adds control of the hash function used to identify packet
65 +         flows. The original SFQ discipline hashes by connection; ESFQ add
66 +         several other hashing methods, such as by src IP or by dst IP, which
67 +         can be more fair to users in some networking situations.
68 +         
69 +         To compile this code as a module, choose M here: the
70 +         module will be called sch_esfq.
71 +
72  config NET_SCH_TEQL
73         tristate "True Link Equalizer (TEQL)"
74         ---help---
75 diff -urN linux-2.6.21.1.old/net/sched/Makefile linux-2.6.21.1.dev/net/sched/Makefile
76 --- linux-2.6.21.1.old/net/sched/Makefile       2007-04-27 23:49:26.000000000 +0200
77 +++ linux-2.6.21.1.dev/net/sched/Makefile       2007-05-26 20:43:12.577580176 +0200
78 @@ -23,6 +23,7 @@
79  obj-$(CONFIG_NET_SCH_INGRESS)  += sch_ingress.o 
80  obj-$(CONFIG_NET_SCH_DSMARK)   += sch_dsmark.o
81  obj-$(CONFIG_NET_SCH_SFQ)      += sch_sfq.o
82 +obj-$(CONFIG_NET_SCH_ESFQ)     += sch_esfq.o
83  obj-$(CONFIG_NET_SCH_TBF)      += sch_tbf.o
84  obj-$(CONFIG_NET_SCH_TEQL)     += sch_teql.o
85  obj-$(CONFIG_NET_SCH_PRIO)     += sch_prio.o
86 diff -urN linux-2.6.21.1.old/net/sched/sch_esfq.c linux-2.6.21.1.dev/net/sched/sch_esfq.c
87 --- linux-2.6.21.1.old/net/sched/sch_esfq.c     1970-01-01 01:00:00.000000000 +0100
88 +++ linux-2.6.21.1.dev/net/sched/sch_esfq.c     2007-05-26 20:43:12.578580024 +0200
89 @@ -0,0 +1,704 @@
90 +/*
91 + * net/sched/sch_esfq.c        Extended Stochastic Fairness Queueing discipline.
92 + *
93 + *             This program is free software; you can redistribute it and/or
94 + *             modify it under the terms of the GNU General Public License
95 + *             as published by the Free Software Foundation; either version
96 + *             2 of the License, or (at your option) any later version.
97 + *
98 + * Authors:    Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
99 + *
100 + * Changes:    Alexander Atanasov, <alex@ssi.bg>
101 + *             Added dynamic depth,limit,divisor,hash_kind options.
102 + *             Added dst and src hashes.
103 + *
104 + *             Alexander Clouter, <alex@digriz.org.uk>
105 + *             Ported ESFQ to Linux 2.6.
106 + *
107 + *             Corey Hickey, <bugfood-c@fatooh.org>
108 + *             Maintenance of the Linux 2.6 port.
109 + *             Added fwmark hash (thanks to Robert Kurjata).
110 + *             Added direct hashing for src, dst, and fwmark.
111 + *             Added usage of jhash.
112 + *             
113 + */
114 +
115 +#include <linux/module.h>
116 +#include <asm/uaccess.h>
117 +#include <asm/system.h>
118 +#include <linux/bitops.h>
119 +#include <linux/types.h>
120 +#include <linux/kernel.h>
121 +#include <linux/jiffies.h>
122 +#include <linux/string.h>
123 +#include <linux/mm.h>
124 +#include <linux/socket.h>
125 +#include <linux/sockios.h>
126 +#include <linux/in.h>
127 +#include <linux/errno.h>
128 +#include <linux/interrupt.h>
129 +#include <linux/if_ether.h>
130 +#include <linux/inet.h>
131 +#include <linux/netdevice.h>
132 +#include <linux/etherdevice.h>
133 +#include <linux/notifier.h>
134 +#include <linux/init.h>
135 +#include <net/ip.h>
136 +#include <linux/ipv6.h>
137 +#include <net/route.h>
138 +#include <linux/skbuff.h>
139 +#include <net/sock.h>
140 +#include <net/pkt_sched.h>
141 +#include <linux/jhash.h>
142 +
143 +#ifdef CONFIG_NF_CONNTRACK_ENABLED
144 +#include <net/netfilter/nf_conntrack.h>
145 +#endif
146 +
147 +/*     Stochastic Fairness Queuing algorithm.
148 +       For more comments look at sch_sfq.c.
149 +       The difference is that you can change limit, depth,
150 +       hash table size and choose alternate hash types.
151 +       
152 +       classic:        same as in sch_sfq.c
153 +       dst:            destination IP address
154 +       src:            source IP address
155 +       fwmark:         netfilter mark value
156 +       dst_direct:
157 +       src_direct:
158 +       fwmark_direct:  direct hashing of the above sources
159 +       ctorigdst:      original destination IP address
160 +       ctorigsrc:      original source IP address
161 +       ctrepldst:      reply destination IP address
162 +       ctreplsrc:      reply source IP 
163 +       
164 +*/
165 +
166 +
167 +/* This type should contain at least SFQ_DEPTH*2 values */
168 +typedef unsigned int esfq_index;
169 +
170 +struct esfq_head
171 +{
172 +       esfq_index      next;
173 +       esfq_index      prev;
174 +};
175 +
176 +struct esfq_sched_data
177 +{
178 +/* Parameters */
179 +       int             perturb_period;
180 +       unsigned        quantum;        /* Allotment per round: MUST BE >= MTU */
181 +       int             limit;
182 +       unsigned        depth;
183 +       unsigned        hash_divisor;
184 +       unsigned        hash_kind;
185 +/* Variables */
186 +       struct timer_list perturb_timer;
187 +       int             perturbation;
188 +       esfq_index      tail;           /* Index of current slot in round */
189 +       esfq_index      max_depth;      /* Maximal depth */
190 +
191 +       esfq_index      *ht;                    /* Hash table */
192 +       esfq_index      *next;                  /* Active slots link */
193 +       short           *allot;                 /* Current allotment per slot */
194 +       unsigned short  *hash;                  /* Hash value indexed by slots */
195 +       struct sk_buff_head     *qs;            /* Slot queue */
196 +       struct esfq_head        *dep;           /* Linked list of slots, indexed by depth */
197 +       unsigned        dyn_min;        /* For dynamic divisor adjustment; minimum value seen */
198 +       unsigned        dyn_max;        /*                                 maximum value seen */
199 +       unsigned        dyn_range;      /*                                 saved range */
200 +};
201 +
202 +/* This contains the info we will hash. */
203 +struct esfq_packet_info
204 +{
205 +       u32     proto;          /* protocol or port */
206 +       u32     src;            /* source from packet header */
207 +       u32     dst;            /* destination from packet header */
208 +       u32     ctorigsrc;      /* original source from conntrack */
209 +       u32     ctorigdst;      /* original destination from conntrack */
210 +       u32     ctreplsrc;      /* reply source from conntrack */
211 +       u32     ctrepldst;      /* reply destination from conntrack */
212 +       u32     mark;           /* netfilter mark (fwmark) */
213 +};
214 +
215 +/* Hash input values directly into the "nearest" slot, taking into account the
216 + * range of input values seen. This is most useful when the hash table is at
217 + * least as large as the range of possible values.
218 + * Note: this functionality was added before the change to using jhash, and may
219 + * no longer be useful. */
220 +static __inline__ unsigned esfq_hash_direct(struct esfq_sched_data *q, u32 h)
221 +{
222 +       /* adjust minimum and maximum */
223 +       if (h < q->dyn_min || h > q->dyn_max) {
224 +               q->dyn_min = h < q->dyn_min ? h : q->dyn_min;
225 +               q->dyn_max = h > q->dyn_max ? h : q->dyn_max;
226 +       
227 +               /* find new range */
228 +               if ((q->dyn_range = q->dyn_max - q->dyn_min) >= q->hash_divisor)
229 +                       printk(KERN_WARNING "ESFQ: (direct hash) Input range %u is larger than hash "
230 +                                       "table. See ESFQ README for details.\n", q->dyn_range);
231 +       }
232 +       
233 +       /* hash input values into slot numbers */
234 +       if (q->dyn_min == q->dyn_max)
235 +               return 0; /* only one value seen; avoid division by 0 */
236 +       else
237 +               return (h - q->dyn_min) * (q->hash_divisor - 1) / q->dyn_range;
238 +}
239 +
240 +static __inline__ unsigned esfq_jhash_1word(struct esfq_sched_data *q,u32 a)
241 +{
242 +       return jhash_1word(a, q->perturbation) & (q->hash_divisor-1);
243 +}
244 +
245 +static __inline__ unsigned esfq_jhash_2words(struct esfq_sched_data *q, u32 a, u32 b)
246 +{
247 +       return jhash_2words(a, b, q->perturbation) & (q->hash_divisor-1);
248 +}
249 +
250 +static __inline__ unsigned esfq_jhash_3words(struct esfq_sched_data *q, u32 a, u32 b, u32 c)
251 +{
252 +       return jhash_3words(a, b, c, q->perturbation) & (q->hash_divisor-1);
253 +}
254 +
255 +
256 +static unsigned esfq_hash(struct esfq_sched_data *q, struct sk_buff *skb)
257 +{
258 +       struct esfq_packet_info info;
259 +#ifdef CONFIG_NF_CONNTRACK_ENABLED
260 +       enum ip_conntrack_info ctinfo;
261 +       struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
262 +#endif
263 +       
264 +       switch (skb->protocol) {
265 +       case __constant_htons(ETH_P_IP):
266 +       {
267 +               struct iphdr *iph = ip_hdr(skb);
268 +               info.dst = iph->daddr;
269 +               info.src = iph->saddr;
270 +               if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
271 +                   (iph->protocol == IPPROTO_TCP ||
272 +                    iph->protocol == IPPROTO_UDP ||
273 +                    iph->protocol == IPPROTO_SCTP ||
274 +                    iph->protocol == IPPROTO_DCCP ||
275 +                    iph->protocol == IPPROTO_ESP))
276 +                       info.proto = *(((u32*)iph) + iph->ihl);
277 +               else
278 +                       info.proto = iph->protocol;
279 +               break;
280 +       }
281 +       case __constant_htons(ETH_P_IPV6):
282 +       {
283 +               struct ipv6hdr *iph = ipv6_hdr(skb);
284 +               /* Hash ipv6 addresses into a u32. This isn't ideal,
285 +                * but the code is simple. */
286 +               info.dst = jhash2(iph->daddr.s6_addr32, 4, q->perturbation);
287 +               info.src = jhash2(iph->saddr.s6_addr32, 4, q->perturbation);
288 +               if (iph->nexthdr == IPPROTO_TCP ||
289 +                   iph->nexthdr == IPPROTO_UDP ||
290 +                   iph->nexthdr == IPPROTO_SCTP ||
291 +                   iph->nexthdr == IPPROTO_DCCP ||
292 +                   iph->nexthdr == IPPROTO_ESP)
293 +                       info.proto = *(u32*)&iph[1];
294 +               else
295 +                       info.proto = iph->nexthdr;
296 +               break;
297 +       }
298 +       default:
299 +               info.dst   = (u32)(unsigned long)skb->dst;
300 +               info.src   = (u32)(unsigned long)skb->sk;
301 +               info.proto = skb->protocol;
302 +       }
303 +
304 +       info.mark = skb->mark;
305 +
306 +#ifdef CONFIG_NF_CONNTRACK_ENABLED
307 +       /* defaults if there is no conntrack info */
308 +       info.ctorigsrc = info.src;
309 +       info.ctorigdst = info.dst;
310 +       info.ctreplsrc = info.dst;
311 +       info.ctrepldst = info.src;
312 +       /* collect conntrack info */
313 +       if (ct && ct != &nf_conntrack_untracked) {
314 +               if (skb->protocol == __constant_htons(ETH_P_IP)) {
315 +                       info.ctorigsrc = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
316 +                       info.ctorigdst = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip;
317 +                       info.ctreplsrc = ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip;
318 +                       info.ctrepldst = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
319 +               }
320 +               else if (skb->protocol == __constant_htons(ETH_P_IPV6)) {
321 +                       /* Again, hash ipv6 addresses into a single u32. */
322 +                       info.ctorigsrc = jhash2(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip6, 4, q->perturbation);
323 +                       info.ctorigdst = jhash2(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip6, 4, q->perturbation);
324 +                       info.ctreplsrc = jhash2(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip6, 4, q->perturbation);
325 +                       info.ctrepldst = jhash2(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip6, 4, q->perturbation);
326 +               }
327 +
328 +       }
329 +#endif
330 +
331 +       switch(q->hash_kind)
332 +       {
333 +       case TCA_SFQ_HASH_CLASSIC:
334 +               return esfq_jhash_3words(q, info.dst, info.src, info.proto);
335 +       case TCA_SFQ_HASH_DST:
336 +               return esfq_jhash_1word(q, info.dst);
337 +       case TCA_SFQ_HASH_DSTDIR:
338 +               return esfq_hash_direct(q, ntohl(info.dst));
339 +       case TCA_SFQ_HASH_SRC:
340 +               return esfq_jhash_1word(q, info.src);
341 +       case TCA_SFQ_HASH_SRCDIR:
342 +               return esfq_hash_direct(q, ntohl(info.src));
343 +       case TCA_SFQ_HASH_FWMARK:
344 +               return esfq_jhash_1word(q, info.mark);
345 +       case TCA_SFQ_HASH_FWMARKDIR:
346 +               return esfq_hash_direct(q, info.mark);
347 +#ifdef CONFIG_NF_CONNTRACK_ENABLED
348 +       case TCA_SFQ_HASH_CTORIGDST:
349 +               return esfq_jhash_1word(q, info.ctorigdst);
350 +       case TCA_SFQ_HASH_CTORIGSRC:
351 +               return esfq_jhash_1word(q, info.ctorigsrc);
352 +       case TCA_SFQ_HASH_CTREPLDST:
353 +               return esfq_jhash_1word(q, info.ctrepldst);
354 +       case TCA_SFQ_HASH_CTREPLSRC:
355 +               return esfq_jhash_1word(q, info.ctreplsrc);
356 +#endif
357 +       default:
358 +               if (net_ratelimit())
359 +                       printk(KERN_WARNING "ESFQ: Unknown hash method. Falling back to classic.\n");
360 +       }
361 +       return esfq_jhash_3words(q, info.dst, info.src, info.proto);
362 +}
363 +
364 +static inline void esfq_link(struct esfq_sched_data *q, esfq_index x)
365 +{
366 +       esfq_index p, n;
367 +       int d = q->qs[x].qlen + q->depth;
368 +
369 +       p = d;
370 +       n = q->dep[d].next;
371 +       q->dep[x].next = n;
372 +       q->dep[x].prev = p;
373 +       q->dep[p].next = q->dep[n].prev = x;
374 +}
375 +
376 +static inline void esfq_dec(struct esfq_sched_data *q, esfq_index x)
377 +{
378 +       esfq_index p, n;
379 +
380 +       n = q->dep[x].next;
381 +       p = q->dep[x].prev;
382 +       q->dep[p].next = n;
383 +       q->dep[n].prev = p;
384 +
385 +       if (n == p && q->max_depth == q->qs[x].qlen + 1)
386 +               q->max_depth--;
387 +
388 +       esfq_link(q, x);
389 +}
390 +
391 +static inline void esfq_inc(struct esfq_sched_data *q, esfq_index x)
392 +{
393 +       esfq_index p, n;
394 +       int d;
395 +
396 +       n = q->dep[x].next;
397 +       p = q->dep[x].prev;
398 +       q->dep[p].next = n;
399 +       q->dep[n].prev = p;
400 +       d = q->qs[x].qlen;
401 +       if (q->max_depth < d)
402 +               q->max_depth = d;
403 +
404 +       esfq_link(q, x);
405 +}
406 +
407 +static unsigned int esfq_drop(struct Qdisc *sch)
408 +{
409 +       struct esfq_sched_data *q = qdisc_priv(sch);
410 +       esfq_index d = q->max_depth;
411 +       struct sk_buff *skb;
412 +       unsigned int len;
413 +
414 +       /* Queue is full! Find the longest slot and
415 +          drop a packet from it */
416 +
417 +       if (d > 1) {
418 +               esfq_index x = q->dep[d+q->depth].next;
419 +               skb = q->qs[x].prev;
420 +               len = skb->len;
421 +               __skb_unlink(skb, &q->qs[x]);
422 +               kfree_skb(skb);
423 +               esfq_dec(q, x);
424 +               sch->q.qlen--;
425 +               sch->qstats.drops++;
426 +               sch->qstats.backlog -= len;
427 +               return len;
428 +       }
429 +
430 +       if (d == 1) {
431 +               /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
432 +               d = q->next[q->tail];
433 +               q->next[q->tail] = q->next[d];
434 +               q->allot[q->next[d]] += q->quantum;
435 +               skb = q->qs[d].prev;
436 +               len = skb->len;
437 +               __skb_unlink(skb, &q->qs[d]);
438 +               kfree_skb(skb);
439 +               esfq_dec(q, d);
440 +               sch->q.qlen--;
441 +               q->ht[q->hash[d]] = q->depth;
442 +               sch->qstats.drops++;
443 +               sch->qstats.backlog -= len;
444 +               return len;
445 +       }
446 +
447 +       return 0;
448 +}
449 +
450 +static int
451 +esfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
452 +{
453 +       struct esfq_sched_data *q = qdisc_priv(sch);
454 +       unsigned hash = esfq_hash(q, skb);
455 +       unsigned depth = q->depth;
456 +       esfq_index x;
457 +
458 +       x = q->ht[hash];
459 +       if (x == depth) {
460 +               q->ht[hash] = x = q->dep[depth].next;
461 +               q->hash[x] = hash;
462 +       }
463 +       sch->qstats.backlog += skb->len;
464 +       __skb_queue_tail(&q->qs[x], skb);
465 +       esfq_inc(q, x);
466 +       if (q->qs[x].qlen == 1) {               /* The flow is new */
467 +               if (q->tail == depth) { /* It is the first flow */
468 +                       q->tail = x;
469 +                       q->next[x] = x;
470 +                       q->allot[x] = q->quantum;
471 +               } else {
472 +                       q->next[x] = q->next[q->tail];
473 +                       q->next[q->tail] = x;
474 +                       q->tail = x;
475 +               }
476 +       }
477 +       if (++sch->q.qlen < q->limit-1) {
478 +               sch->bstats.bytes += skb->len;
479 +               sch->bstats.packets++;
480 +               return 0;
481 +       }
482 +
483 +       esfq_drop(sch);
484 +       return NET_XMIT_CN;
485 +}
486 +
487 +static int
488 +esfq_requeue(struct sk_buff *skb, struct Qdisc* sch)
489 +{
490 +       struct esfq_sched_data *q = qdisc_priv(sch);
491 +       unsigned hash = esfq_hash(q, skb);
492 +       unsigned depth = q->depth;
493 +       esfq_index x;
494 +
495 +       x = q->ht[hash];
496 +       if (x == depth) {
497 +               q->ht[hash] = x = q->dep[depth].next;
498 +               q->hash[x] = hash;
499 +       }
500 +       sch->qstats.backlog += skb->len;
501 +       __skb_queue_head(&q->qs[x], skb);
502 +       esfq_inc(q, x);
503 +       if (q->qs[x].qlen == 1) {               /* The flow is new */
504 +               if (q->tail == depth) { /* It is the first flow */
505 +                       q->tail = x;
506 +                       q->next[x] = x;
507 +                       q->allot[x] = q->quantum;
508 +               } else {
509 +                       q->next[x] = q->next[q->tail];
510 +                       q->next[q->tail] = x;
511 +                       q->tail = x;
512 +               }
513 +       }
514 +       if (++sch->q.qlen < q->limit - 1) {
515 +               sch->qstats.requeues++;
516 +               return 0;
517 +       }
518 +
519 +       sch->qstats.drops++;
520 +       esfq_drop(sch);
521 +       return NET_XMIT_CN;
522 +}
523 +
524 +
525 +
526 +
527 +static struct sk_buff *
528 +esfq_dequeue(struct Qdisc* sch)
529 +{
530 +       struct esfq_sched_data *q = qdisc_priv(sch);
531 +       struct sk_buff *skb;
532 +       unsigned depth = q->depth;
533 +       esfq_index a, old_a;
534 +
535 +       /* No active slots */
536 +       if (q->tail == depth)
537 +               return NULL;
538 +       
539 +       a = old_a = q->next[q->tail];
540 +       
541 +       /* Grab packet */
542 +       skb = __skb_dequeue(&q->qs[a]);
543 +       esfq_dec(q, a);
544 +       sch->q.qlen--;
545 +       sch->qstats.backlog -= skb->len;
546 +       
547 +       /* Is the slot empty? */
548 +       if (q->qs[a].qlen == 0) {
549 +               q->ht[q->hash[a]] = depth;
550 +               a = q->next[a];
551 +               if (a == old_a) {
552 +                       q->tail = depth;
553 +                       return skb;
554 +               }
555 +               q->next[q->tail] = a;
556 +               q->allot[a] += q->quantum;
557 +       } else if ((q->allot[a] -= skb->len) <= 0) {
558 +               q->tail = a;
559 +               a = q->next[a];
560 +               q->allot[a] += q->quantum;
561 +       }
562 +       
563 +       return skb;
564 +}
565 +
566 +static void
567 +esfq_reset(struct Qdisc* sch)
568 +{
569 +       struct sk_buff *skb;
570 +
571 +       while ((skb = esfq_dequeue(sch)) != NULL)
572 +               kfree_skb(skb);
573 +}
574 +
575 +static void esfq_perturbation(unsigned long arg)
576 +{
577 +       struct Qdisc *sch = (struct Qdisc*)arg;
578 +       struct esfq_sched_data *q = qdisc_priv(sch);
579 +
580 +       q->perturbation = net_random()&0x1F;
581 +
582 +       if (q->perturb_period) {
583 +               q->perturb_timer.expires = jiffies + q->perturb_period;
584 +               add_timer(&q->perturb_timer);
585 +       }
586 +}
587 +
588 +static int esfq_change(struct Qdisc *sch, struct rtattr *opt)
589 +{
590 +       struct esfq_sched_data *q = qdisc_priv(sch);
591 +       struct tc_esfq_qopt *ctl = RTA_DATA(opt);
592 +       int old_perturb = q->perturb_period;
593 +       
594 +       if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
595 +               return -EINVAL;
596 +       
597 +       sch_tree_lock(sch);
598 +       q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
599 +       q->perturb_period = ctl->perturb_period*HZ;
600 +//     q->hash_divisor = ctl->divisor;
601 +//     q->tail = q->limit = q->depth = ctl->flows;
602 +       
603 +       if (ctl->limit)
604 +               q->limit = min_t(u32, ctl->limit, q->depth);
605 +       
606 +       if (ctl->hash_kind) {
607 +               q->hash_kind = ctl->hash_kind;
608 +               if (q->hash_kind != TCA_SFQ_HASH_CLASSIC)
609 +                       q->perturb_period = 0;
610 +       }
611 +       
612 +       // is sch_tree_lock enough to do this ?
613 +       while (sch->q.qlen >= q->limit-1)
614 +               esfq_drop(sch);
615 +       
616 +       if (old_perturb)
617 +               del_timer(&q->perturb_timer);
618 +       if (q->perturb_period) {
619 +               q->perturb_timer.expires = jiffies + q->perturb_period;
620 +               add_timer(&q->perturb_timer);
621 +       } else {
622 +               q->perturbation = 0;
623 +       }
624 +       sch_tree_unlock(sch);
625 +       return 0;
626 +}
627 +
628 +static int esfq_init(struct Qdisc *sch, struct rtattr *opt)
629 +{
630 +       struct esfq_sched_data *q = qdisc_priv(sch);
631 +       struct tc_esfq_qopt *ctl;
632 +       esfq_index p = ~0U/2;
633 +       int i;
634 +       
635 +       if (opt && opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
636 +               return -EINVAL;
637 +
638 +       init_timer(&q->perturb_timer);
639 +       q->perturb_timer.data = (unsigned long)sch;
640 +       q->perturb_timer.function = esfq_perturbation;
641 +       q->perturbation = 0;
642 +       q->hash_kind = TCA_SFQ_HASH_CLASSIC;
643 +       q->max_depth = 0;
644 +       q->dyn_min = ~0U; /* maximum value for this type */
645 +       q->dyn_max = 0;  /* dyn_min/dyn_max will be set properly upon first packet */
646 +       if (opt == NULL) {
647 +               q->quantum = psched_mtu(sch->dev);
648 +               q->perturb_period = 0;
649 +               q->hash_divisor = 1024;
650 +               q->tail = q->limit = q->depth = 128;
651 +               
652 +       } else {
653 +               ctl = RTA_DATA(opt);
654 +               q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
655 +               q->perturb_period = ctl->perturb_period*HZ;
656 +               q->hash_divisor = ctl->divisor ? : 1024;
657 +               q->tail = q->limit = q->depth = ctl->flows ? : 128;
658 +               
659 +               if ( q->depth > p - 1 )
660 +                       return -EINVAL;
661 +               
662 +               if (ctl->limit)
663 +                       q->limit = min_t(u32, ctl->limit, q->depth);
664 +               
665 +               if (ctl->hash_kind) {
666 +                       q->hash_kind = ctl->hash_kind;
667 +               }
668 +               
669 +               if (q->perturb_period) {
670 +                       q->perturb_timer.expires = jiffies + q->perturb_period;
671 +                       add_timer(&q->perturb_timer);
672 +               }
673 +       }
674 +       
675 +       q->ht = kmalloc(q->hash_divisor*sizeof(esfq_index), GFP_KERNEL);
676 +       if (!q->ht)
677 +               goto err_case;
678 +               
679 +       q->dep = kmalloc((1+q->depth*2)*sizeof(struct esfq_head), GFP_KERNEL);
680 +       if (!q->dep)
681 +               goto err_case;
682 +       q->next = kmalloc(q->depth*sizeof(esfq_index), GFP_KERNEL);
683 +       if (!q->next)
684 +               goto err_case;
685 +       
686 +       q->allot = kmalloc(q->depth*sizeof(short), GFP_KERNEL);
687 +       if (!q->allot)
688 +               goto err_case;
689 +       q->hash = kmalloc(q->depth*sizeof(unsigned short), GFP_KERNEL);
690 +       if (!q->hash)
691 +               goto err_case;
692 +       q->qs = kmalloc(q->depth*sizeof(struct sk_buff_head), GFP_KERNEL);
693 +       if (!q->qs)
694 +               goto err_case;
695 +       
696 +       for (i=0; i< q->hash_divisor; i++)
697 +               q->ht[i] = q->depth;
698 +       for (i=0; i<q->depth; i++) {
699 +               skb_queue_head_init(&q->qs[i]);
700 +               q->dep[i+q->depth].next = i+q->depth;
701 +               q->dep[i+q->depth].prev = i+q->depth;
702 +       }
703 +       
704 +       for (i=0; i<q->depth; i++)
705 +               esfq_link(q, i);
706 +       return 0;
707 +err_case:
708 +       del_timer(&q->perturb_timer);
709 +       if (q->ht)
710 +               kfree(q->ht);
711 +       if (q->dep)
712 +               kfree(q->dep);
713 +       if (q->next)
714 +               kfree(q->next);
715 +       if (q->allot)
716 +               kfree(q->allot);
717 +       if (q->hash)
718 +               kfree(q->hash);
719 +       if (q->qs)
720 +               kfree(q->qs);
721 +       return -ENOBUFS;
722 +}
723 +
724 +static void esfq_destroy(struct Qdisc *sch)
725 +{
726 +       struct esfq_sched_data *q = qdisc_priv(sch);
727 +       del_timer(&q->perturb_timer);
728 +       if(q->ht)
729 +               kfree(q->ht);
730 +       if(q->dep)
731 +               kfree(q->dep);
732 +       if(q->next)
733 +               kfree(q->next);
734 +       if(q->allot)
735 +               kfree(q->allot);
736 +       if(q->hash)
737 +               kfree(q->hash);
738 +       if(q->qs)
739 +               kfree(q->qs);
740 +}
741 +
742 +static int esfq_dump(struct Qdisc *sch, struct sk_buff *skb)
743 +{
744 +       struct esfq_sched_data *q = qdisc_priv(sch);
745 +       unsigned char    *b = skb->tail;
746 +       struct tc_esfq_qopt opt;
747 +
748 +       opt.quantum = q->quantum;
749 +       opt.perturb_period = q->perturb_period/HZ;
750 +
751 +       opt.limit = q->limit;
752 +       opt.divisor = q->hash_divisor;
753 +       opt.flows = q->depth;
754 +       opt.hash_kind = q->hash_kind;
755 +
756 +       RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
757 +
758 +       return skb->len;
759 +
760 +rtattr_failure:
761 +       skb_trim(skb, b - skb->data);
762 +       return -1;
763 +}
764 +
765 +static struct Qdisc_ops esfq_qdisc_ops =
766 +{
767 +       .next           =       NULL,
768 +       .cl_ops         =       NULL,
769 +       .id             =       "esfq",
770 +       .priv_size      =       sizeof(struct esfq_sched_data),
771 +       .enqueue        =       esfq_enqueue,
772 +       .dequeue        =       esfq_dequeue,
773 +       .requeue        =       esfq_requeue,
774 +       .drop           =       esfq_drop,
775 +       .init           =       esfq_init,
776 +       .reset          =       esfq_reset,
777 +       .destroy        =       esfq_destroy,
778 +       .change         =       NULL, /* esfq_change - needs more work */
779 +       .dump           =       esfq_dump,
780 +       .owner          =       THIS_MODULE,
781 +};
782 +
783 +static int __init esfq_module_init(void)
784 +{
785 +       return register_qdisc(&esfq_qdisc_ops);
786 +}
787 +static void __exit esfq_module_exit(void) 
788 +{
789 +       unregister_qdisc(&esfq_qdisc_ops);
790 +}
791 +module_init(esfq_module_init)
792 +module_exit(esfq_module_exit)
793 +MODULE_LICENSE("GPL");