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