1 --- a/Documentation/Configure.help
2 +++ b/Documentation/Configure.help
3 @@ -11165,6 +11165,24 @@ CONFIG_NET_SCH_HFSC
4 whenever you want). If you want to compile it as a module, say M
5 here and read <file:Documentation/modules.txt>.
9 + Say Y here if you want to use the Stochastic Fairness Queueing (SFQ)
10 + packet scheduling algorithm for some of your network devices or as a
11 + leaf discipline for the CBQ scheduling algorithm (see the top of
12 + <file:net/sched/sch_esfq.c> for details and references about the SFQ
15 + This is an enchanced SFQ version which allows you to control the
16 + hardcoded values in the SFQ scheduler: queue depth, hash table size,
17 + queues limit. Also adds control to the hash function used to identify
18 + packet flows. Hash by src or dst ip and original sfq hash.
20 + This code is also available as a module called sch_esfq.o ( = code
21 + which can be inserted in and removed from the running kernel
22 + whenever you want). If you want to compile it as a module, say M
23 + here and read <file:Documentation/modules.txt>.
27 Say Y here if you want to use the Clark-Shenker-Zhang (CSZ) packet
28 --- a/include/linux/pkt_sched.h
29 +++ b/include/linux/pkt_sched.h
30 @@ -173,8 +173,36 @@ struct tc_sfq_qopt
32 * The only reason for this is efficiency, it is possible
33 * to change these parameters in compile time.
35 + * If you need to play with these values use esfq instead.
43 + TCA_SFQ_HASH_CLASSIC,
47 + TCA_SFQ_HASH_CTORIGDST,
48 + TCA_SFQ_HASH_CTORIGSRC,
49 + TCA_SFQ_HASH_CTREPLDST,
50 + TCA_SFQ_HASH_CTREPLSRC,
51 + TCA_SFQ_HASH_CTNATCHG,
56 + unsigned quantum; /* Bytes per round allocated to flow */
57 + int perturb_period; /* Period of hash perturbation */
58 + __u32 limit; /* Maximal packets in queue */
59 + unsigned divisor; /* Hash divisor */
60 + unsigned flows; /* Maximal number of flows */
61 + unsigned hash_kind; /* Hash function to use for flow identification */
67 --- a/net/sched/Config.in
68 +++ b/net/sched/Config.in
70 tristate ' The simplest PRIO pseudoscheduler' CONFIG_NET_SCH_PRIO
71 tristate ' RED queue' CONFIG_NET_SCH_RED
72 tristate ' SFQ queue' CONFIG_NET_SCH_SFQ
73 +tristate ' ESFQ queue' CONFIG_NET_SCH_ESFQ
74 tristate ' TEQL queue' CONFIG_NET_SCH_TEQL
75 tristate ' TBF queue' CONFIG_NET_SCH_TBF
76 tristate ' GRED queue' CONFIG_NET_SCH_GRED
77 --- a/net/sched/Makefile
78 +++ b/net/sched/Makefile
79 @@ -19,6 +19,7 @@ obj-$(CONFIG_NET_SCH_HPFQ) += sch_hpfq.o
80 obj-$(CONFIG_NET_SCH_HFSC) += sch_hfsc.o
81 obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o
82 obj-$(CONFIG_NET_SCH_SFQ) += sch_sfq.o
83 +obj-$(CONFIG_NET_SCH_ESFQ) += sch_esfq.o
84 obj-$(CONFIG_NET_SCH_RED) += sch_red.o
85 obj-$(CONFIG_NET_SCH_TBF) += sch_tbf.o
86 obj-$(CONFIG_NET_SCH_PRIO) += sch_prio.o
88 +++ b/net/sched/sch_esfq.c
91 + * net/sched/sch_esfq.c Extended Stochastic Fairness Queueing discipline.
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.
98 + * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
100 + * Changes: Alexander Atanasov, <alex@ssi.bg>
101 + * Added dynamic depth,limit,divisor,hash_kind options.
102 + * Added dst and src hashes.
104 + * Alexander Clouter, <alex@digriz.org.uk>
105 + * Ported ESFQ to Linux 2.6.
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.
114 +#include <linux/config.h>
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/sched.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>
136 +#include <net/route.h>
137 +#include <linux/skbuff.h>
138 +#include <net/sock.h>
139 +#include <net/pkt_sched.h>
140 +#include <linux/jhash.h>
142 +#define IPPROTO_DCCP 33
143 +#define qdisc_priv(q) ((void *)(q->data))
145 +#ifdef CONFIG_IP_NF_CONNTRACK
146 +/* #include <net/netfilter/nf_conntrack.h> */
147 +#include <linux/netfilter_ipv4/ip_conntrack.h>
150 +/* Stochastic Fairness Queuing algorithm.
151 + For more comments look at sch_sfq.c.
152 + The difference is that you can change limit, depth,
153 + hash table size and choose alternate hash types.
155 + classic: same as in sch_sfq.c
156 + dst: destination IP address
157 + src: source IP address
158 + ctorigdst: original destination IP address
159 + ctorigsrc: original source IP address
160 + ctrepldst: reply destination IP address
161 + ctreplsrc: reply source IP
162 + ctnatchg: use the address which changed via nat
167 +/* This type should contain at least SFQ_DEPTH*2 values */
168 +typedef unsigned int esfq_index;
176 +struct esfq_sched_data
179 + int perturb_period;
180 + unsigned quantum; /* Allotment per round: MUST BE >= MTU */
183 + unsigned hash_divisor;
184 + unsigned hash_kind;
186 + struct timer_list perturb_timer;
188 + esfq_index tail; /* Index of current slot in round */
189 + esfq_index max_depth; /* Maximal depth */
191 + esfq_index *ht; /* Hash table */
192 + esfq_index *next; /* Active slots link */
193 + short *allot; /* Current allotment per slot */
194 + unsigned short *hash; /* Hash value indexed by slots */
195 + struct sk_buff_head *qs; /* Slot queue */
196 + struct esfq_head *dep; /* Linked list of slots, indexed by depth */
197 + unsigned dyn_min; /* For dynamic divisor adjustment; minimum value seen */
198 + unsigned dyn_max; /* maximum value seen */
199 + unsigned dyn_range; /* saved range */
202 +/* This contains the info we will hash. */
203 +struct esfq_packet_info
205 + u32 proto; /* protocol or port */
206 + u32 src; /* source from packet header */
207 + u32 dst; /* destination from packet header */
208 + u32 ctorigsrc; /* original source from conntrack */
209 + u32 ctorigdst; /* original destination from conntrack */
210 + u32 ctreplsrc; /* reply source from conntrack */
211 + u32 ctrepldst; /* reply destination from conntrack */
214 +static __inline__ unsigned esfq_jhash_1word(struct esfq_sched_data *q,u32 a)
216 + return jhash_1word(a, q->perturbation) & (q->hash_divisor-1);
219 +static __inline__ unsigned esfq_jhash_2words(struct esfq_sched_data *q, u32 a, u32 b)
221 + return jhash_2words(a, b, q->perturbation) & (q->hash_divisor-1);
224 +static __inline__ unsigned esfq_jhash_3words(struct esfq_sched_data *q, u32 a, u32 b, u32 c)
226 + return jhash_3words(a, b, c, q->perturbation) & (q->hash_divisor-1);
230 +static unsigned esfq_hash(struct esfq_sched_data *q, struct sk_buff *skb)
232 + struct esfq_packet_info info;
233 +#ifdef CONFIG_IP_NF_CONNTRACK
234 + enum ip_conntrack_info ctinfo;
235 + struct ip_conntrack *ct = ip_conntrack_get(skb, &ctinfo);
238 + switch (skb->protocol) {
239 + case __constant_htons(ETH_P_IP):
241 + struct iphdr *iph = skb->nh.iph;
242 + info.dst = iph->daddr;
243 + info.src = iph->saddr;
244 + if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
245 + (iph->protocol == IPPROTO_TCP ||
246 + iph->protocol == IPPROTO_UDP ||
247 + iph->protocol == IPPROTO_SCTP ||
248 + iph->protocol == IPPROTO_DCCP ||
249 + iph->protocol == IPPROTO_ESP))
250 + info.proto = *(((u32*)iph) + iph->ihl);
252 + info.proto = iph->protocol;
256 + info.dst = (u32)(unsigned long)skb->dst;
257 + info.src = (u32)(unsigned long)skb->sk;
258 + info.proto = skb->protocol;
261 +#ifdef CONFIG_IP_NF_CONNTRACK
262 + /* defaults if there is no conntrack info */
263 + info.ctorigsrc = info.src;
264 + info.ctorigdst = info.dst;
265 + info.ctreplsrc = info.dst;
266 + info.ctrepldst = info.src;
267 + /* collect conntrack info */
270 + if (skb->protocol == __constant_htons(ETH_P_IP)) {
271 + info.ctorigsrc = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
272 + info.ctorigdst = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
273 + info.ctreplsrc = ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
274 + info.ctrepldst = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
279 + switch(q->hash_kind)
281 + case TCA_SFQ_HASH_CLASSIC:
282 + return esfq_jhash_3words(q, info.dst, info.src, info.proto);
283 + case TCA_SFQ_HASH_DST:
284 + return esfq_jhash_1word(q, info.dst);
285 + case TCA_SFQ_HASH_SRC:
286 + return esfq_jhash_1word(q, info.src);
287 +#ifdef CONFIG_IP_NF_CONNTRACK
288 + case TCA_SFQ_HASH_CTORIGDST:
289 + return esfq_jhash_1word(q, info.ctorigdst);
290 + case TCA_SFQ_HASH_CTORIGSRC:
291 + return esfq_jhash_1word(q, info.ctorigsrc);
292 + case TCA_SFQ_HASH_CTREPLDST:
293 + return esfq_jhash_1word(q, info.ctrepldst);
294 + case TCA_SFQ_HASH_CTREPLSRC:
295 + return esfq_jhash_1word(q, info.ctreplsrc);
296 + case TCA_SFQ_HASH_CTNATCHG:
298 + if (info.ctorigdst == info.ctreplsrc)
299 + return esfq_jhash_1word(q, info.ctorigsrc);
301 + return esfq_jhash_1word(q, info.ctreplsrc);
305 + if (net_ratelimit())
306 + printk(KERN_WARNING "ESFQ: Unknown hash method. Falling back to classic.\n");
308 + return esfq_jhash_3words(q, info.dst, info.src, info.proto);
311 +static inline void esfq_link(struct esfq_sched_data *q, esfq_index x)
314 + int d = q->qs[x].qlen + q->depth;
317 + n = q->dep[d].next;
318 + q->dep[x].next = n;
319 + q->dep[x].prev = p;
320 + q->dep[p].next = q->dep[n].prev = x;
323 +static inline void esfq_dec(struct esfq_sched_data *q, esfq_index x)
327 + n = q->dep[x].next;
328 + p = q->dep[x].prev;
329 + q->dep[p].next = n;
330 + q->dep[n].prev = p;
332 + if (n == p && q->max_depth == q->qs[x].qlen + 1)
338 +static inline void esfq_inc(struct esfq_sched_data *q, esfq_index x)
343 + n = q->dep[x].next;
344 + p = q->dep[x].prev;
345 + q->dep[p].next = n;
346 + q->dep[n].prev = p;
348 + if (q->max_depth < d)
354 +static unsigned int esfq_drop(struct Qdisc *sch)
356 + struct esfq_sched_data *q = qdisc_priv(sch);
357 + esfq_index d = q->max_depth;
358 + struct sk_buff *skb;
361 + /* Queue is full! Find the longest slot and
362 + drop a packet from it */
365 + esfq_index x = q->dep[d+q->depth].next;
366 + skb = q->qs[x].prev;
368 + __skb_unlink(skb, &q->qs[x]);
372 + sch->stats.drops++;
373 + sch->stats.backlog -= len;
378 + /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
379 + d = q->next[q->tail];
380 + q->next[q->tail] = q->next[d];
381 + q->allot[q->next[d]] += q->quantum;
382 + skb = q->qs[d].prev;
384 + __skb_unlink(skb, &q->qs[d]);
388 + q->ht[q->hash[d]] = q->depth;
389 + sch->stats.drops++;
390 + sch->stats.backlog -= len;
398 +esfq_enqueue(struct sk_buff *skb, struct Qdisc* sch)
400 + struct esfq_sched_data *q = qdisc_priv(sch);
401 + unsigned hash = esfq_hash(q, skb);
402 + unsigned depth = q->depth;
407 + q->ht[hash] = x = q->dep[depth].next;
410 + sch->stats.backlog += skb->len;
411 + __skb_queue_tail(&q->qs[x], skb);
413 + if (q->qs[x].qlen == 1) { /* The flow is new */
414 + if (q->tail == depth) { /* It is the first flow */
417 + q->allot[x] = q->quantum;
419 + q->next[x] = q->next[q->tail];
420 + q->next[q->tail] = x;
424 + if (++sch->q.qlen < q->limit-1) {
425 + sch->stats.bytes += skb->len;
426 + sch->stats.packets++;
431 + return NET_XMIT_CN;
435 +esfq_requeue(struct sk_buff *skb, struct Qdisc* sch)
437 + struct esfq_sched_data *q = qdisc_priv(sch);
438 + unsigned hash = esfq_hash(q, skb);
439 + unsigned depth = q->depth;
444 + q->ht[hash] = x = q->dep[depth].next;
447 + sch->stats.backlog += skb->len;
448 + __skb_queue_head(&q->qs[x], skb);
450 + if (q->qs[x].qlen == 1) { /* The flow is new */
451 + if (q->tail == depth) { /* It is the first flow */
454 + q->allot[x] = q->quantum;
456 + q->next[x] = q->next[q->tail];
457 + q->next[q->tail] = x;
461 + if (++sch->q.qlen < q->limit - 1) {
465 + sch->stats.drops++;
467 + return NET_XMIT_CN;
473 +static struct sk_buff *
474 +esfq_dequeue(struct Qdisc* sch)
476 + struct esfq_sched_data *q = qdisc_priv(sch);
477 + struct sk_buff *skb;
478 + unsigned depth = q->depth;
479 + esfq_index a, old_a;
481 + /* No active slots */
482 + if (q->tail == depth)
485 + a = old_a = q->next[q->tail];
488 + skb = __skb_dequeue(&q->qs[a]);
491 + sch->stats.backlog -= skb->len;
493 + /* Is the slot empty? */
494 + if (q->qs[a].qlen == 0) {
495 + q->ht[q->hash[a]] = depth;
501 + q->next[q->tail] = a;
502 + q->allot[a] += q->quantum;
503 + } else if ((q->allot[a] -= skb->len) <= 0) {
506 + q->allot[a] += q->quantum;
513 +esfq_reset(struct Qdisc* sch)
515 + struct sk_buff *skb;
517 + while ((skb = esfq_dequeue(sch)) != NULL)
521 +static void esfq_perturbation(unsigned long arg)
523 + struct Qdisc *sch = (struct Qdisc*)arg;
524 + struct esfq_sched_data *q = qdisc_priv(sch);
526 + q->perturbation = net_random()&0x1F;
528 + if (q->perturb_period) {
529 + q->perturb_timer.expires = jiffies + q->perturb_period;
530 + add_timer(&q->perturb_timer);
534 +static int esfq_change(struct Qdisc *sch, struct rtattr *opt)
536 + struct esfq_sched_data *q = qdisc_priv(sch);
537 + struct tc_esfq_qopt *ctl = RTA_DATA(opt);
538 + int old_perturb = q->perturb_period;
540 + if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
543 + sch_tree_lock(sch);
544 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
545 + q->perturb_period = ctl->perturb_period*HZ;
546 +// q->hash_divisor = ctl->divisor;
547 +// q->tail = q->limit = q->depth = ctl->flows;
550 + q->limit = min_t(u32, ctl->limit, q->depth);
552 + if (ctl->hash_kind) {
553 + q->hash_kind = ctl->hash_kind;
554 + if (q->hash_kind != TCA_SFQ_HASH_CLASSIC)
555 + q->perturb_period = 0;
558 + // is sch_tree_lock enough to do this ?
559 + while (sch->q.qlen >= q->limit-1)
563 + del_timer(&q->perturb_timer);
564 + if (q->perturb_period) {
565 + q->perturb_timer.expires = jiffies + q->perturb_period;
566 + add_timer(&q->perturb_timer);
568 + q->perturbation = 0;
570 + sch_tree_unlock(sch);
574 +static int esfq_init(struct Qdisc *sch, struct rtattr *opt)
576 + struct esfq_sched_data *q = qdisc_priv(sch);
577 + struct tc_esfq_qopt *ctl;
578 + esfq_index p = ~0U/2;
581 + if (opt && opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
584 + init_timer(&q->perturb_timer);
585 + q->perturb_timer.data = (unsigned long)sch;
586 + q->perturb_timer.function = esfq_perturbation;
587 + q->perturbation = 0;
588 + q->hash_kind = TCA_SFQ_HASH_CLASSIC;
590 + q->dyn_min = ~0U; /* maximum value for this type */
591 + q->dyn_max = 0; /* dyn_min/dyn_max will be set properly upon first packet */
593 + q->quantum = psched_mtu(sch->dev);
594 + q->perturb_period = 0;
595 + q->hash_divisor = 1024;
596 + q->tail = q->limit = q->depth = 128;
599 + ctl = RTA_DATA(opt);
600 + q->quantum = ctl->quantum ? : psched_mtu(sch->dev);
601 + q->perturb_period = ctl->perturb_period*HZ;
602 + q->hash_divisor = ctl->divisor ? : 1024;
603 + q->tail = q->limit = q->depth = ctl->flows ? : 128;
605 + if ( q->depth > p - 1 )
609 + q->limit = min_t(u32, ctl->limit, q->depth);
611 + if (ctl->hash_kind) {
612 + q->hash_kind = ctl->hash_kind;
615 + if (q->perturb_period) {
616 + q->perturb_timer.expires = jiffies + q->perturb_period;
617 + add_timer(&q->perturb_timer);
621 + q->ht = kmalloc(q->hash_divisor*sizeof(esfq_index), GFP_KERNEL);
625 + q->dep = kmalloc((1+q->depth*2)*sizeof(struct esfq_head), GFP_KERNEL);
628 + q->next = kmalloc(q->depth*sizeof(esfq_index), GFP_KERNEL);
632 + q->allot = kmalloc(q->depth*sizeof(short), GFP_KERNEL);
635 + q->hash = kmalloc(q->depth*sizeof(unsigned short), GFP_KERNEL);
638 + q->qs = kmalloc(q->depth*sizeof(struct sk_buff_head), GFP_KERNEL);
642 + for (i=0; i< q->hash_divisor; i++)
643 + q->ht[i] = q->depth;
644 + for (i=0; i<q->depth; i++) {
645 + skb_queue_head_init(&q->qs[i]);
646 + q->dep[i+q->depth].next = i+q->depth;
647 + q->dep[i+q->depth].prev = i+q->depth;
650 + for (i=0; i<q->depth; i++)
654 + del_timer(&q->perturb_timer);
670 +static void esfq_destroy(struct Qdisc *sch)
672 + struct esfq_sched_data *q = qdisc_priv(sch);
673 + del_timer(&q->perturb_timer);
688 +static int esfq_dump(struct Qdisc *sch, struct sk_buff *skb)
690 + struct esfq_sched_data *q = qdisc_priv(sch);
691 + unsigned char *b = skb->tail;
692 + struct tc_esfq_qopt opt;
694 + opt.quantum = q->quantum;
695 + opt.perturb_period = q->perturb_period/HZ;
697 + opt.limit = q->limit;
698 + opt.divisor = q->hash_divisor;
699 + opt.flows = q->depth;
700 + opt.hash_kind = q->hash_kind;
702 + RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
707 + skb_trim(skb, b - skb->data);
711 +static struct Qdisc_ops esfq_qdisc_ops =
716 + .priv_size = sizeof(struct esfq_sched_data),
717 + .enqueue = esfq_enqueue,
718 + .dequeue = esfq_dequeue,
719 + .requeue = esfq_requeue,
722 + .reset = esfq_reset,
723 + .destroy = esfq_destroy,
724 + .change = NULL, /* esfq_change - needs more work */
728 +static int __init esfq_module_init(void)
730 + return register_qdisc(&esfq_qdisc_ops);
732 +static void __exit esfq_module_exit(void)
734 + unregister_qdisc(&esfq_qdisc_ops);
736 +module_init(esfq_module_init)
737 +module_exit(esfq_module_exit)
738 +MODULE_LICENSE("GPL");