add preliminary 3.14 support
[openwrt.git] / target / linux / generic / patches-3.14 / 620-sched_esfq.patch
1 --- a/include/uapi/linux/pkt_sched.h
2 +++ b/include/uapi/linux/pkt_sched.h
3 @@ -226,6 +226,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 <linux/bitops.h>
118 +#include <linux/types.h>
119 +#include <linux/kernel.h>
120 +#include <linux/jiffies.h>
121 +#include <linux/string.h>
122 +#include <linux/mm.h>
123 +#include <linux/socket.h>
124 +#include <linux/sockios.h>
125 +#include <linux/in.h>
126 +#include <linux/errno.h>
127 +#include <linux/interrupt.h>
128 +#include <linux/if_ether.h>
129 +#include <linux/inet.h>
130 +#include <linux/netdevice.h>
131 +#include <linux/etherdevice.h>
132 +#include <linux/notifier.h>
133 +#include <linux/init.h>
134 +#include <net/ip.h>
135 +#include <net/netlink.h>
136 +#include <linux/ipv6.h>
137 +#include <net/route.h>
138 +#include <linux/skbuff.h>
139 +#include <net/sock.h>
140 +#include <net/pkt_sched.h>
141 +#include <linux/jhash.h>
142 +#ifdef CONFIG_NET_SCH_ESFQ_NFCT
143 +#include <net/netfilter/nf_conntrack.h>
144 +#endif
145 +
146 +/*     Stochastic Fairness Queuing algorithm.
147 +       For more comments look at sch_sfq.c.
148 +       The difference is that you can change limit, depth,
149 +       hash table size and choose alternate hash types.
150 +
151 +       classic:        same as in sch_sfq.c
152 +       dst:            destination IP address
153 +       src:            source IP address
154 +       fwmark:         netfilter mark value
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 +#define ESFQ_HEAD 0
163 +#define ESFQ_TAIL 1
164 +
165 +/* This type should contain at least SFQ_DEPTH*2 values */
166 +typedef unsigned int esfq_index;
167 +
168 +struct esfq_head
169 +{
170 +       esfq_index      next;
171 +       esfq_index      prev;
172 +};
173 +
174 +struct esfq_sched_data
175 +{
176 +/* Parameters */
177 +       int             perturb_period;
178 +       unsigned        quantum;        /* Allotment per round: MUST BE >= MTU */
179 +       int             limit;
180 +       unsigned        depth;
181 +       unsigned        hash_divisor;
182 +       unsigned        hash_kind;
183 +/* Variables */
184 +       struct timer_list perturb_timer;
185 +       int             perturbation;
186 +       esfq_index      tail;           /* Index of current slot in round */
187 +       esfq_index      max_depth;      /* Maximal depth */
188 +
189 +       esfq_index      *ht;                    /* Hash table */
190 +       esfq_index      *next;                  /* Active slots link */
191 +       short           *allot;                 /* Current allotment per slot */
192 +       unsigned short  *hash;                  /* Hash value indexed by slots */
193 +       struct sk_buff_head     *qs;            /* Slot queue */
194 +       struct esfq_head        *dep;           /* Linked list of slots, indexed by depth */
195 +};
196 +
197 +/* This contains the info we will hash. */
198 +struct esfq_packet_info
199 +{
200 +       u32     proto;          /* protocol or port */
201 +       u32     src;            /* source from packet header */
202 +       u32     dst;            /* destination from packet header */
203 +       u32     ctorigsrc;      /* original source from conntrack */
204 +       u32     ctorigdst;      /* original destination from conntrack */
205 +       u32     ctreplsrc;      /* reply source from conntrack */
206 +       u32     ctrepldst;      /* reply destination from conntrack */
207 +       u32     mark;           /* netfilter mark (fwmark) */
208 +};
209 +
210 +static __inline__ unsigned esfq_jhash_1word(struct esfq_sched_data *q,u32 a)
211 +{
212 +       return jhash_1word(a, q->perturbation) & (q->hash_divisor-1);
213 +}
214 +
215 +static __inline__ unsigned esfq_jhash_2words(struct esfq_sched_data *q, u32 a, u32 b)
216 +{
217 +       return jhash_2words(a, b, q->perturbation) & (q->hash_divisor-1);
218 +}
219 +
220 +static __inline__ unsigned esfq_jhash_3words(struct esfq_sched_data *q, u32 a, u32 b, u32 c)
221 +{
222 +       return jhash_3words(a, b, c, q->perturbation) & (q->hash_divisor-1);
223 +}
224 +
225 +static unsigned esfq_hash(struct esfq_sched_data *q, struct sk_buff *skb)
226 +{
227 +       struct esfq_packet_info info;
228 +#ifdef CONFIG_NET_SCH_ESFQ_NFCT
229 +       enum ip_conntrack_info ctinfo;
230 +       struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
231 +#endif
232 +
233 +       switch (skb->protocol) {
234 +       case __constant_htons(ETH_P_IP):
235 +       {
236 +               struct iphdr *iph = ip_hdr(skb);
237 +               info.dst = iph->daddr;
238 +               info.src = iph->saddr;
239 +               if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
240 +                   (iph->protocol == IPPROTO_TCP ||
241 +                    iph->protocol == IPPROTO_UDP ||
242 +                    iph->protocol == IPPROTO_SCTP ||
243 +                    iph->protocol == IPPROTO_DCCP ||
244 +                    iph->protocol == IPPROTO_ESP))
245 +                       info.proto = *(((u32*)iph) + iph->ihl);
246 +               else
247 +                       info.proto = iph->protocol;
248 +               break;
249 +       }
250 +       case __constant_htons(ETH_P_IPV6):
251 +       {
252 +               struct ipv6hdr *iph = ipv6_hdr(skb);
253 +               /* Hash ipv6 addresses into a u32. This isn't ideal,
254 +                * but the code is simple. */
255 +               info.dst = jhash2(iph->daddr.s6_addr32, 4, q->perturbation);
256 +               info.src = jhash2(iph->saddr.s6_addr32, 4, q->perturbation);
257 +               if (iph->nexthdr == IPPROTO_TCP ||
258 +                   iph->nexthdr == IPPROTO_UDP ||
259 +                   iph->nexthdr == IPPROTO_SCTP ||
260 +                   iph->nexthdr == IPPROTO_DCCP ||
261 +                   iph->nexthdr == IPPROTO_ESP)
262 +                       info.proto = *(u32*)&iph[1];
263 +               else
264 +                       info.proto = iph->nexthdr;
265 +               break;
266 +       }
267 +       default:
268 +               info.dst   = (u32)(unsigned long)skb_dst(skb);
269 +               info.src   = (u32)(unsigned long)skb->sk;
270 +               info.proto = skb->protocol;
271 +       }
272 +
273 +       info.mark = skb->mark;
274 +
275 +#ifdef CONFIG_NET_SCH_ESFQ_NFCT
276 +       /* defaults if there is no conntrack info */
277 +       info.ctorigsrc = info.src;
278 +       info.ctorigdst = info.dst;
279 +       info.ctreplsrc = info.dst;
280 +       info.ctrepldst = info.src;
281 +       /* collect conntrack info */
282 +       if (ct && ct != &nf_conntrack_untracked) {
283 +               if (skb->protocol == __constant_htons(ETH_P_IP)) {
284 +                       info.ctorigsrc = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
285 +                       info.ctorigdst = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip;
286 +                       info.ctreplsrc = ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip;
287 +                       info.ctrepldst = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
288 +               }
289 +               else if (skb->protocol == __constant_htons(ETH_P_IPV6)) {
290 +                       /* Again, hash ipv6 addresses into a single u32. */
291 +                       info.ctorigsrc = jhash2(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip6, 4, q->perturbation);
292 +                       info.ctorigdst = jhash2(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip6, 4, q->perturbation);
293 +                       info.ctreplsrc = jhash2(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip6, 4, q->perturbation);
294 +                       info.ctrepldst = jhash2(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip6, 4, q->perturbation);
295 +               }
296 +
297 +       }
298 +#endif
299 +
300 +       switch(q->hash_kind) {
301 +       case TCA_SFQ_HASH_CLASSIC:
302 +               return esfq_jhash_3words(q, info.dst, info.src, info.proto);
303 +       case TCA_SFQ_HASH_DST:
304 +               return esfq_jhash_1word(q, info.dst);
305 +       case TCA_SFQ_HASH_SRC:
306 +               return esfq_jhash_1word(q, info.src);
307 +       case TCA_SFQ_HASH_FWMARK:
308 +               return esfq_jhash_1word(q, info.mark);
309 +#ifdef CONFIG_NET_SCH_ESFQ_NFCT
310 +       case TCA_SFQ_HASH_CTORIGDST:
311 +               return esfq_jhash_1word(q, info.ctorigdst);
312 +       case TCA_SFQ_HASH_CTORIGSRC:
313 +               return esfq_jhash_1word(q, info.ctorigsrc);
314 +       case TCA_SFQ_HASH_CTREPLDST:
315 +               return esfq_jhash_1word(q, info.ctrepldst);
316 +       case TCA_SFQ_HASH_CTREPLSRC:
317 +               return esfq_jhash_1word(q, info.ctreplsrc);
318 +       case TCA_SFQ_HASH_CTNATCHG:
319 +       {
320 +               if (info.ctorigdst == info.ctreplsrc)
321 +                       return esfq_jhash_1word(q, info.ctorigsrc);
322 +               return esfq_jhash_1word(q, info.ctreplsrc);
323 +       }
324 +#endif
325 +       default:
326 +               if (net_ratelimit())
327 +                       printk(KERN_WARNING "ESFQ: Unknown hash method. Falling back to classic.\n");
328 +       }
329 +       return esfq_jhash_3words(q, info.dst, info.src, info.proto);
330 +}
331 +
332 +static inline void esfq_link(struct esfq_sched_data *q, esfq_index x)
333 +{
334 +       esfq_index p, n;
335 +       int d = q->qs[x].qlen + q->depth;
336 +
337 +       p = d;
338 +       n = q->dep[d].next;
339 +       q->dep[x].next = n;
340 +       q->dep[x].prev = p;
341 +       q->dep[p].next = q->dep[n].prev = x;
342 +}
343 +
344 +static inline void esfq_dec(struct esfq_sched_data *q, esfq_index x)
345 +{
346 +       esfq_index p, n;
347 +
348 +       n = q->dep[x].next;
349 +       p = q->dep[x].prev;
350 +       q->dep[p].next = n;
351 +       q->dep[n].prev = p;
352 +
353 +       if (n == p && q->max_depth == q->qs[x].qlen + 1)
354 +               q->max_depth--;
355 +
356 +       esfq_link(q, x);
357 +}
358 +
359 +static inline void esfq_inc(struct esfq_sched_data *q, esfq_index x)
360 +{
361 +       esfq_index p, n;
362 +       int d;
363 +
364 +       n = q->dep[x].next;
365 +       p = q->dep[x].prev;
366 +       q->dep[p].next = n;
367 +       q->dep[n].prev = p;
368 +       d = q->qs[x].qlen;
369 +       if (q->max_depth < d)
370 +               q->max_depth = d;
371 +
372 +       esfq_link(q, x);
373 +}
374 +
375 +static unsigned int esfq_drop(struct Qdisc *sch)
376 +{
377 +       struct esfq_sched_data *q = qdisc_priv(sch);
378 +       esfq_index d = q->max_depth;
379 +       struct sk_buff *skb;
380 +       unsigned int len;
381 +
382 +       /* Queue is full! Find the longest slot and
383 +          drop a packet from it */
384 +
385 +       if (d > 1) {
386 +               esfq_index x = q->dep[d+q->depth].next;
387 +               skb = q->qs[x].prev;
388 +               len = skb->len;
389 +               __skb_unlink(skb, &q->qs[x]);
390 +               kfree_skb(skb);
391 +               esfq_dec(q, x);
392 +               sch->q.qlen--;
393 +               sch->qstats.drops++;
394 +               sch->qstats.backlog -= len;
395 +               return len;
396 +       }
397 +
398 +       if (d == 1) {
399 +               /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
400 +               d = q->next[q->tail];
401 +               q->next[q->tail] = q->next[d];
402 +               q->allot[q->next[d]] += q->quantum;
403 +               skb = q->qs[d].prev;
404 +               len = skb->len;
405 +               __skb_unlink(skb, &q->qs[d]);
406 +               kfree_skb(skb);
407 +               esfq_dec(q, d);
408 +               sch->q.qlen--;
409 +               q->ht[q->hash[d]] = q->depth;
410 +               sch->qstats.drops++;
411 +               sch->qstats.backlog -= len;
412 +               return len;
413 +       }
414 +
415 +       return 0;
416 +}
417 +
418 +static void esfq_q_enqueue(struct sk_buff *skb, struct esfq_sched_data *q, unsigned int end)
419 +{
420 +       unsigned hash = esfq_hash(q, skb);
421 +       unsigned depth = q->depth;
422 +       esfq_index x;
423 +
424 +       x = q->ht[hash];
425 +       if (x == depth) {
426 +               q->ht[hash] = x = q->dep[depth].next;
427 +               q->hash[x] = hash;
428 +       }
429 +
430 +       if (end == ESFQ_TAIL)
431 +               __skb_queue_tail(&q->qs[x], skb);
432 +       else
433 +               __skb_queue_head(&q->qs[x], skb);
434 +
435 +       esfq_inc(q, x);
436 +       if (q->qs[x].qlen == 1) {               /* The flow is new */
437 +               if (q->tail == depth) { /* It is the first flow */
438 +                       q->tail = x;
439 +                       q->next[x] = x;
440 +                       q->allot[x] = q->quantum;
441 +               } else {
442 +                       q->next[x] = q->next[q->tail];
443 +                       q->next[q->tail] = x;
444 +                       q->tail = x;
445 +               }
446 +       }
447 +}
448 +
449 +static int esfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
450 +{
451 +       struct esfq_sched_data *q = qdisc_priv(sch);
452 +       esfq_q_enqueue(skb, q, ESFQ_TAIL);
453 +       sch->qstats.backlog += skb->len;
454 +       if (++sch->q.qlen < q->limit-1) {
455 +               sch->bstats.bytes += skb->len;
456 +               sch->bstats.packets++;
457 +               return 0;
458 +       }
459 +
460 +       sch->qstats.drops++;
461 +       esfq_drop(sch);
462 +       return NET_XMIT_CN;
463 +}
464 +
465 +static struct sk_buff *esfq_peek(struct Qdisc* sch)
466 +{
467 +       struct esfq_sched_data *q = qdisc_priv(sch);
468 +       esfq_index a;
469 +
470 +       /* No active slots */
471 +       if (q->tail == q->depth)
472 +               return NULL;
473 +
474 +       a = q->next[q->tail];
475 +       return skb_peek(&q->qs[a]);
476 +}
477 +
478 +static struct sk_buff *esfq_q_dequeue(struct esfq_sched_data *q)
479 +{
480 +       struct sk_buff *skb;
481 +       unsigned depth = q->depth;
482 +       esfq_index a, old_a;
483 +
484 +       /* No active slots */
485 +       if (q->tail == depth)
486 +               return NULL;
487 +
488 +       a = old_a = q->next[q->tail];
489 +
490 +       /* Grab packet */
491 +       skb = __skb_dequeue(&q->qs[a]);
492 +       esfq_dec(q, a);
493 +
494 +       /* Is the slot empty? */
495 +       if (q->qs[a].qlen == 0) {
496 +               q->ht[q->hash[a]] = depth;
497 +               a = q->next[a];
498 +               if (a == old_a) {
499 +                       q->tail = depth;
500 +                       return skb;
501 +               }
502 +               q->next[q->tail] = a;
503 +               q->allot[a] += q->quantum;
504 +       } else if ((q->allot[a] -= skb->len) <= 0) {
505 +               q->tail = a;
506 +               a = q->next[a];
507 +               q->allot[a] += q->quantum;
508 +       }
509 +
510 +       return skb;
511 +}
512 +
513 +static struct sk_buff *esfq_dequeue(struct Qdisc* sch)
514 +{
515 +       struct esfq_sched_data *q = qdisc_priv(sch);
516 +       struct sk_buff *skb;
517 +
518 +       skb = esfq_q_dequeue(q);
519 +       if (skb == NULL)
520 +               return NULL;
521 +       sch->q.qlen--;
522 +       sch->qstats.backlog -= skb->len;
523 +       return skb;
524 +}
525 +
526 +static void esfq_q_destroy(struct esfq_sched_data *q)
527 +{
528 +       del_timer(&q->perturb_timer);
529 +       if(q->ht)
530 +               kfree(q->ht);
531 +       if(q->dep)
532 +               kfree(q->dep);
533 +       if(q->next)
534 +               kfree(q->next);
535 +       if(q->allot)
536 +               kfree(q->allot);
537 +       if(q->hash)
538 +               kfree(q->hash);
539 +       if(q->qs)
540 +               kfree(q->qs);
541 +}
542 +
543 +static void esfq_destroy(struct Qdisc *sch)
544 +{
545 +       struct esfq_sched_data *q = qdisc_priv(sch);
546 +       esfq_q_destroy(q);
547 +}
548 +
549 +
550 +static void esfq_reset(struct Qdisc* sch)
551 +{
552 +       struct sk_buff *skb;
553 +
554 +       while ((skb = esfq_dequeue(sch)) != NULL)
555 +               kfree_skb(skb);
556 +}
557 +
558 +static void esfq_perturbation(unsigned long arg)
559 +{
560 +       struct Qdisc *sch = (struct Qdisc*)arg;
561 +       struct esfq_sched_data *q = qdisc_priv(sch);
562 +
563 +       q->perturbation = net_random()&0x1F;
564 +
565 +       if (q->perturb_period) {
566 +               q->perturb_timer.expires = jiffies + q->perturb_period;
567 +               add_timer(&q->perturb_timer);
568 +       }
569 +}
570 +
571 +static unsigned int esfq_check_hash(unsigned int kind)
572 +{
573 +       switch (kind) {
574 +       case TCA_SFQ_HASH_CTORIGDST:
575 +       case TCA_SFQ_HASH_CTORIGSRC:
576 +       case TCA_SFQ_HASH_CTREPLDST:
577 +       case TCA_SFQ_HASH_CTREPLSRC:
578 +       case TCA_SFQ_HASH_CTNATCHG:
579 +#ifndef CONFIG_NET_SCH_ESFQ_NFCT
580 +       {
581 +               if (net_ratelimit())
582 +                       printk(KERN_WARNING "ESFQ: Conntrack hash types disabled in kernel config. Falling back to classic.\n");
583 +               return TCA_SFQ_HASH_CLASSIC;
584 +       }
585 +#endif
586 +       case TCA_SFQ_HASH_CLASSIC:
587 +       case TCA_SFQ_HASH_DST:
588 +       case TCA_SFQ_HASH_SRC:
589 +       case TCA_SFQ_HASH_FWMARK:
590 +               return kind;
591 +       default:
592 +       {
593 +               if (net_ratelimit())
594 +                       printk(KERN_WARNING "ESFQ: Unknown hash type. Falling back to classic.\n");
595 +               return TCA_SFQ_HASH_CLASSIC;
596 +       }
597 +       }
598 +}
599 +
600 +static int esfq_q_init(struct esfq_sched_data *q, struct nlattr *opt)
601 +{
602 +       struct tc_esfq_qopt *ctl = nla_data(opt);
603 +       esfq_index p = ~0U/2;
604 +       int i;
605 +
606 +       if (opt && opt->nla_len < nla_attr_size(sizeof(*ctl)))
607 +               return -EINVAL;
608 +
609 +       q->perturbation = 0;
610 +       q->hash_kind = TCA_SFQ_HASH_CLASSIC;
611 +       q->max_depth = 0;
612 +       if (opt == NULL) {
613 +               q->perturb_period = 0;
614 +               q->hash_divisor = 1024;
615 +               q->tail = q->limit = q->depth = 128;
616 +
617 +       } else {
618 +               struct tc_esfq_qopt *ctl = nla_data(opt);
619 +               if (ctl->quantum)
620 +                       q->quantum = ctl->quantum;
621 +               q->perturb_period = ctl->perturb_period*HZ;
622 +               q->hash_divisor = ctl->divisor ? : 1024;
623 +               q->tail = q->limit = q->depth = ctl->flows ? : 128;
624 +
625 +               if ( q->depth > p - 1 )
626 +                       return -EINVAL;
627 +
628 +               if (ctl->limit)
629 +                       q->limit = min_t(u32, ctl->limit, q->depth);
630 +
631 +               if (ctl->hash_kind) {
632 +                       q->hash_kind = esfq_check_hash(ctl->hash_kind);
633 +               }
634 +       }
635 +
636 +       q->ht = kmalloc(q->hash_divisor*sizeof(esfq_index), GFP_KERNEL);
637 +       if (!q->ht)
638 +               goto err_case;
639 +       q->dep = kmalloc((1+q->depth*2)*sizeof(struct esfq_head), GFP_KERNEL);
640 +       if (!q->dep)
641 +               goto err_case;
642 +       q->next = kmalloc(q->depth*sizeof(esfq_index), GFP_KERNEL);
643 +       if (!q->next)
644 +               goto err_case;
645 +       q->allot = kmalloc(q->depth*sizeof(short), GFP_KERNEL);
646 +       if (!q->allot)
647 +               goto err_case;
648 +       q->hash = kmalloc(q->depth*sizeof(unsigned short), GFP_KERNEL);
649 +       if (!q->hash)
650 +               goto err_case;
651 +       q->qs = kmalloc(q->depth*sizeof(struct sk_buff_head), GFP_KERNEL);
652 +       if (!q->qs)
653 +               goto err_case;
654 +
655 +       for (i=0; i< q->hash_divisor; i++)
656 +               q->ht[i] = q->depth;
657 +       for (i=0; i<q->depth; i++) {
658 +               skb_queue_head_init(&q->qs[i]);
659 +               q->dep[i+q->depth].next = i+q->depth;
660 +               q->dep[i+q->depth].prev = i+q->depth;
661 +       }
662 +
663 +       for (i=0; i<q->depth; i++)
664 +               esfq_link(q, i);
665 +       return 0;
666 +err_case:
667 +       esfq_q_destroy(q);
668 +       return -ENOBUFS;
669 +}
670 +
671 +static int esfq_init(struct Qdisc *sch, struct nlattr *opt)
672 +{
673 +       struct esfq_sched_data *q = qdisc_priv(sch);
674 +       int err;
675 +
676 +       q->quantum = psched_mtu(qdisc_dev(sch)); /* default */
677 +       if ((err = esfq_q_init(q, opt)))
678 +               return err;
679 +
680 +       init_timer(&q->perturb_timer);
681 +       q->perturb_timer.data = (unsigned long)sch;
682 +       q->perturb_timer.function = esfq_perturbation;
683 +       if (q->perturb_period) {
684 +               q->perturb_timer.expires = jiffies + q->perturb_period;
685 +               add_timer(&q->perturb_timer);
686 +       }
687 +
688 +       return 0;
689 +}
690 +
691 +static int esfq_change(struct Qdisc *sch, struct nlattr *opt)
692 +{
693 +       struct esfq_sched_data *q = qdisc_priv(sch);
694 +       struct esfq_sched_data new;
695 +       struct sk_buff *skb;
696 +       int err;
697 +
698 +       /* set up new queue */
699 +       memset(&new, 0, sizeof(struct esfq_sched_data));
700 +       new.quantum = psched_mtu(qdisc_dev(sch)); /* default */
701 +       if ((err = esfq_q_init(&new, opt)))
702 +               return err;
703 +
704 +       /* copy all packets from the old queue to the new queue */
705 +       sch_tree_lock(sch);
706 +       while ((skb = esfq_q_dequeue(q)) != NULL)
707 +               esfq_q_enqueue(skb, &new, ESFQ_TAIL);
708 +
709 +       /* clean up the old queue */
710 +       esfq_q_destroy(q);
711 +
712 +       /* copy elements of the new queue into the old queue */
713 +       q->perturb_period = new.perturb_period;
714 +       q->quantum        = new.quantum;
715 +       q->limit          = new.limit;
716 +       q->depth          = new.depth;
717 +       q->hash_divisor   = new.hash_divisor;
718 +       q->hash_kind      = new.hash_kind;
719 +       q->tail           = new.tail;
720 +       q->max_depth      = new.max_depth;
721 +       q->ht    = new.ht;
722 +       q->dep   = new.dep;
723 +       q->next  = new.next;
724 +       q->allot = new.allot;
725 +       q->hash  = new.hash;
726 +       q->qs    = new.qs;
727 +
728 +       /* finish up */
729 +       if (q->perturb_period) {
730 +               q->perturb_timer.expires = jiffies + q->perturb_period;
731 +               add_timer(&q->perturb_timer);
732 +       } else {
733 +               q->perturbation = 0;
734 +       }
735 +       sch_tree_unlock(sch);
736 +       return 0;
737 +}
738 +
739 +static int esfq_dump(struct Qdisc *sch, struct sk_buff *skb)
740 +{
741 +       struct esfq_sched_data *q = qdisc_priv(sch);
742 +       unsigned char *b = skb_tail_pointer(skb);
743 +       struct tc_esfq_qopt opt;
744 +
745 +       opt.quantum = q->quantum;
746 +       opt.perturb_period = q->perturb_period/HZ;
747 +
748 +       opt.limit = q->limit;
749 +       opt.divisor = q->hash_divisor;
750 +       opt.flows = q->depth;
751 +       opt.hash_kind = q->hash_kind;
752 +
753 +       if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
754 +               goto nla_put_failure;
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");