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