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