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