blob: d329a8a72357619fbe2b8a883377f4bbb3c2a738 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/sch_sfq.c Stochastic Fairness Queueing discipline.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/jiffies.h>
16#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/in.h>
18#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070020#include <linux/skbuff.h>
Alexey Kuznetsov32740dd2007-09-30 17:51:33 -070021#include <linux/jhash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Eric Dumazet817fb152011-01-20 00:14:58 +000023#include <linux/vmalloc.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070024#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <net/pkt_sched.h>
Eric Dumazet11fca932011-11-29 03:40:45 +000026#include <net/flow_keys.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28
29/* Stochastic Fairness Queuing algorithm.
30 =======================================
31
32 Source:
33 Paul E. McKenney "Stochastic Fairness Queuing",
34 IEEE INFOCOMM'90 Proceedings, San Francisco, 1990.
35
36 Paul E. McKenney "Stochastic Fairness Queuing",
37 "Interworking: Research and Experience", v.2, 1991, p.113-131.
38
39
40 See also:
41 M. Shreedhar and George Varghese "Efficient Fair
42 Queuing using Deficit Round Robin", Proc. SIGCOMM 95.
43
44
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090045 This is not the thing that is usually called (W)FQ nowadays.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 It does not use any timestamp mechanism, but instead
47 processes queues in round-robin order.
48
49 ADVANTAGE:
50
51 - It is very cheap. Both CPU and memory requirements are minimal.
52
53 DRAWBACKS:
54
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090055 - "Stochastic" -> It is not 100% fair.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 When hash collisions occur, several flows are considered as one.
57
58 - "Round-robin" -> It introduces larger delays than virtual clock
59 based schemes, and should not be used for isolating interactive
60 traffic from non-interactive. It means, that this scheduler
61 should be used as leaf of CBQ or P3, which put interactive traffic
62 to higher priority band.
63
64 We still need true WFQ for top level CSZ, but using WFQ
65 for the best effort traffic is absolutely pointless:
66 SFQ is superior for this purpose.
67
68 IMPLEMENTATION:
69 This implementation limits maximal queue length to 128;
Eric Dumazeteeaeb062010-12-28 21:53:33 +000070 max mtu to 2^18-1; max 128 flows, number of hash buckets to 1024.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 The only goal of this restrictions was that all data
Eric Dumazeteda83e32010-12-20 12:54:58 +000072 fit into one 4K page on 32bit arches.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 It is easy to increase these values, but not in flight. */
75
Eric Dumazeteda83e32010-12-20 12:54:58 +000076#define SFQ_DEPTH 128 /* max number of packets per flow */
77#define SFQ_SLOTS 128 /* max number of flows */
78#define SFQ_EMPTY_SLOT 255
Eric Dumazet817fb152011-01-20 00:14:58 +000079#define SFQ_DEFAULT_HASH_DIVISOR 1024
80
Eric Dumazeteeaeb062010-12-28 21:53:33 +000081/* We use 16 bits to store allot, and want to handle packets up to 64K
82 * Scale allot by 8 (1<<3) so that no overflow occurs.
83 */
84#define SFQ_ALLOT_SHIFT 3
85#define SFQ_ALLOT_SIZE(X) DIV_ROUND_UP(X, 1 << SFQ_ALLOT_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Eric Dumazeteda83e32010-12-20 12:54:58 +000087/* This type should contain at least SFQ_DEPTH + SFQ_SLOTS values */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088typedef unsigned char sfq_index;
89
Eric Dumazeteda83e32010-12-20 12:54:58 +000090/*
91 * We dont use pointers to save space.
92 * Small indexes [0 ... SFQ_SLOTS - 1] are 'pointers' to slots[] array
93 * while following values [SFQ_SLOTS ... SFQ_SLOTS + SFQ_DEPTH - 1]
94 * are 'pointers' to dep[] array
95 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +000096struct sfq_head {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 sfq_index next;
98 sfq_index prev;
99};
100
Eric Dumazeteda83e32010-12-20 12:54:58 +0000101struct sfq_slot {
102 struct sk_buff *skblist_next;
103 struct sk_buff *skblist_prev;
104 sfq_index qlen; /* number of skbs in skblist */
105 sfq_index next; /* next slot in sfq chain */
106 struct sfq_head dep; /* anchor in dep[] chains */
107 unsigned short hash; /* hash value (index in ht[]) */
108 short allot; /* credit for this slot */
109};
110
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000111struct sfq_sched_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* Parameters */
113 int perturb_period;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000114 unsigned int quantum; /* Allotment per round: MUST BE >= MTU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int limit;
Eric Dumazet817fb152011-01-20 00:14:58 +0000116 unsigned int divisor; /* number of slots in hash table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Variables */
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800118 struct tcf_proto *filter_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct timer_list perturb_timer;
Alexey Kuznetsov32740dd2007-09-30 17:51:33 -0700120 u32 perturbation;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000121 sfq_index cur_depth; /* depth of longest slot */
Eric Dumazeteeaeb062010-12-28 21:53:33 +0000122 unsigned short scaled_quantum; /* SFQ_ALLOT_SIZE(quantum) */
Eric Dumazeteda83e32010-12-20 12:54:58 +0000123 struct sfq_slot *tail; /* current slot in round */
Eric Dumazet817fb152011-01-20 00:14:58 +0000124 sfq_index *ht; /* Hash table (divisor slots) */
Eric Dumazeteda83e32010-12-20 12:54:58 +0000125 struct sfq_slot slots[SFQ_SLOTS];
126 struct sfq_head dep[SFQ_DEPTH]; /* Linked list of slots, indexed by depth */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Eric Dumazeteda83e32010-12-20 12:54:58 +0000129/*
130 * sfq_head are either in a sfq_slot or in dep[] array
131 */
132static inline struct sfq_head *sfq_dep_head(struct sfq_sched_data *q, sfq_index val)
133{
134 if (val < SFQ_SLOTS)
135 return &q->slots[val].dep;
136 return &q->dep[val - SFQ_SLOTS];
137}
138
Eric Dumazet225d9b82011-12-21 03:30:11 +0000139/*
140 * In order to be able to quickly rehash our queue when timer changes
141 * q->perturbation, we store flow_keys in skb->cb[]
142 */
143struct sfq_skb_cb {
144 struct flow_keys keys;
145};
146
147static inline struct sfq_skb_cb *sfq_skb_cb(const struct sk_buff *skb)
148{
149 BUILD_BUG_ON(sizeof(skb->cb) <
150 sizeof(struct qdisc_skb_cb) + sizeof(struct sfq_skb_cb));
151 return (struct sfq_skb_cb *)qdisc_skb_cb(skb)->data;
152}
153
Eric Dumazet11fca932011-11-29 03:40:45 +0000154static unsigned int sfq_hash(const struct sfq_sched_data *q,
155 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Eric Dumazet225d9b82011-12-21 03:30:11 +0000157 const struct flow_keys *keys = &sfq_skb_cb(skb)->keys;
Eric Dumazet11fca932011-11-29 03:40:45 +0000158 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Eric Dumazet225d9b82011-12-21 03:30:11 +0000160 hash = jhash_3words((__force u32)keys->dst,
161 (__force u32)keys->src ^ keys->ip_proto,
162 (__force u32)keys->ports, q->perturbation);
Eric Dumazet11fca932011-11-29 03:40:45 +0000163 return hash & (q->divisor - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800166static unsigned int sfq_classify(struct sk_buff *skb, struct Qdisc *sch,
167 int *qerr)
168{
169 struct sfq_sched_data *q = qdisc_priv(sch);
170 struct tcf_result res;
171 int result;
172
173 if (TC_H_MAJ(skb->priority) == sch->handle &&
174 TC_H_MIN(skb->priority) > 0 &&
Eric Dumazet817fb152011-01-20 00:14:58 +0000175 TC_H_MIN(skb->priority) <= q->divisor)
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800176 return TC_H_MIN(skb->priority);
177
Eric Dumazet225d9b82011-12-21 03:30:11 +0000178 if (!q->filter_list) {
179 skb_flow_dissect(skb, &sfq_skb_cb(skb)->keys);
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800180 return sfq_hash(q, skb) + 1;
Eric Dumazet225d9b82011-12-21 03:30:11 +0000181 }
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800182
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700183 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800184 result = tc_classify(skb, q->filter_list, &res);
185 if (result >= 0) {
186#ifdef CONFIG_NET_CLS_ACT
187 switch (result) {
188 case TC_ACT_STOLEN:
189 case TC_ACT_QUEUED:
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700190 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800191 case TC_ACT_SHOT:
192 return 0;
193 }
194#endif
Eric Dumazet817fb152011-01-20 00:14:58 +0000195 if (TC_H_MIN(res.classid) <= q->divisor)
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800196 return TC_H_MIN(res.classid);
197 }
198 return 0;
199}
200
Eric Dumazeteda83e32010-12-20 12:54:58 +0000201/*
202 * x : slot number [0 .. SFQ_SLOTS - 1]
203 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static inline void sfq_link(struct sfq_sched_data *q, sfq_index x)
205{
206 sfq_index p, n;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000207 int qlen = q->slots[x].qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Eric Dumazeteda83e32010-12-20 12:54:58 +0000209 p = qlen + SFQ_SLOTS;
210 n = q->dep[qlen].next;
211
212 q->slots[x].dep.next = n;
213 q->slots[x].dep.prev = p;
214
215 q->dep[qlen].next = x; /* sfq_dep_head(q, p)->next = x */
216 sfq_dep_head(q, n)->prev = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Eric Dumazeteda83e32010-12-20 12:54:58 +0000219#define sfq_unlink(q, x, n, p) \
220 n = q->slots[x].dep.next; \
221 p = q->slots[x].dep.prev; \
222 sfq_dep_head(q, p)->next = n; \
223 sfq_dep_head(q, n)->prev = p
224
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static inline void sfq_dec(struct sfq_sched_data *q, sfq_index x)
227{
228 sfq_index p, n;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000229 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Eric Dumazeteda83e32010-12-20 12:54:58 +0000231 sfq_unlink(q, x, n, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Eric Dumazeteda83e32010-12-20 12:54:58 +0000233 d = q->slots[x].qlen--;
234 if (n == p && q->cur_depth == d)
235 q->cur_depth--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 sfq_link(q, x);
237}
238
239static inline void sfq_inc(struct sfq_sched_data *q, sfq_index x)
240{
241 sfq_index p, n;
242 int d;
243
Eric Dumazeteda83e32010-12-20 12:54:58 +0000244 sfq_unlink(q, x, n, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Eric Dumazeteda83e32010-12-20 12:54:58 +0000246 d = ++q->slots[x].qlen;
247 if (q->cur_depth < d)
248 q->cur_depth = d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 sfq_link(q, x);
250}
251
Eric Dumazeteda83e32010-12-20 12:54:58 +0000252/* helper functions : might be changed when/if skb use a standard list_head */
253
254/* remove one skb from tail of slot queue */
255static inline struct sk_buff *slot_dequeue_tail(struct sfq_slot *slot)
256{
257 struct sk_buff *skb = slot->skblist_prev;
258
259 slot->skblist_prev = skb->prev;
Eric Dumazetee09b3c2010-12-22 11:39:59 -0800260 skb->prev->next = (struct sk_buff *)slot;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000261 skb->next = skb->prev = NULL;
262 return skb;
263}
264
265/* remove one skb from head of slot queue */
266static inline struct sk_buff *slot_dequeue_head(struct sfq_slot *slot)
267{
268 struct sk_buff *skb = slot->skblist_next;
269
270 slot->skblist_next = skb->next;
Eric Dumazet18c8d822010-12-31 12:48:55 -0800271 skb->next->prev = (struct sk_buff *)slot;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000272 skb->next = skb->prev = NULL;
273 return skb;
274}
275
276static inline void slot_queue_init(struct sfq_slot *slot)
277{
278 slot->skblist_prev = slot->skblist_next = (struct sk_buff *)slot;
279}
280
281/* add skb to slot queue (tail add) */
282static inline void slot_queue_add(struct sfq_slot *slot, struct sk_buff *skb)
283{
284 skb->prev = slot->skblist_prev;
285 skb->next = (struct sk_buff *)slot;
286 slot->skblist_prev->next = skb;
287 slot->skblist_prev = skb;
288}
289
290#define slot_queue_walk(slot, skb) \
291 for (skb = slot->skblist_next; \
292 skb != (struct sk_buff *)slot; \
293 skb = skb->next)
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static unsigned int sfq_drop(struct Qdisc *sch)
296{
297 struct sfq_sched_data *q = qdisc_priv(sch);
Eric Dumazeteda83e32010-12-20 12:54:58 +0000298 sfq_index x, d = q->cur_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct sk_buff *skb;
300 unsigned int len;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000301 struct sfq_slot *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Eric Dumazeteda83e32010-12-20 12:54:58 +0000303 /* Queue is full! Find the longest slot and drop tail packet from it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (d > 1) {
Eric Dumazeteda83e32010-12-20 12:54:58 +0000305 x = q->dep[d].next;
306 slot = &q->slots[x];
307drop:
308 skb = slot_dequeue_tail(slot);
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700309 len = qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 sfq_dec(q, x);
Eric Dumazeteda83e32010-12-20 12:54:58 +0000311 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 sch->q.qlen--;
313 sch->qstats.drops++;
Patrick McHardyf5539eb2006-03-20 19:01:38 -0800314 sch->qstats.backlog -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return len;
316 }
317
318 if (d == 1) {
319 /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */
Eric Dumazeteda83e32010-12-20 12:54:58 +0000320 x = q->tail->next;
321 slot = &q->slots[x];
322 q->tail->next = slot->next;
323 q->ht[slot->hash] = SFQ_EMPTY_SLOT;
324 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
327 return 0;
328}
329
330static int
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800331sfq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct sfq_sched_data *q = qdisc_priv(sch);
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800334 unsigned int hash;
Eric Dumazet8efa8852011-05-23 11:02:42 +0000335 sfq_index x, qlen;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000336 struct sfq_slot *slot;
Jarek Poplawski7f3ff4f2008-12-21 20:14:48 -0800337 int uninitialized_var(ret);
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800338
339 hash = sfq_classify(skb, sch, &ret);
340 if (hash == 0) {
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700341 if (ret & __NET_XMIT_BYPASS)
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800342 sch->qstats.drops++;
343 kfree_skb(skb);
344 return ret;
345 }
346 hash--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 x = q->ht[hash];
Eric Dumazeteda83e32010-12-20 12:54:58 +0000349 slot = &q->slots[x];
350 if (x == SFQ_EMPTY_SLOT) {
351 x = q->dep[0].next; /* get a free slot */
352 q->ht[hash] = x;
353 slot = &q->slots[x];
354 slot->hash = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800356
Eric Dumazeteda83e32010-12-20 12:54:58 +0000357 /* If selected queue has length q->limit, do simple tail drop,
Alexey Kuznetsov32740dd2007-09-30 17:51:33 -0700358 * i.e. drop _this_ packet.
359 */
Eric Dumazeteda83e32010-12-20 12:54:58 +0000360 if (slot->qlen >= q->limit)
Alexey Kuznetsov32740dd2007-09-30 17:51:33 -0700361 return qdisc_drop(skb, sch);
362
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700363 sch->qstats.backlog += qdisc_pkt_len(skb);
Eric Dumazeteda83e32010-12-20 12:54:58 +0000364 slot_queue_add(slot, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 sfq_inc(q, x);
Eric Dumazeteda83e32010-12-20 12:54:58 +0000366 if (slot->qlen == 1) { /* The flow is new */
367 if (q->tail == NULL) { /* It is the first flow */
368 slot->next = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 } else {
Eric Dumazeteda83e32010-12-20 12:54:58 +0000370 slot->next = q->tail->next;
371 q->tail->next = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Eric Dumazeteda83e32010-12-20 12:54:58 +0000373 q->tail = slot;
Eric Dumazeteeaeb062010-12-28 21:53:33 +0000374 slot->allot = q->scaled_quantum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800376 if (++sch->q.qlen <= q->limit)
Ben Greear9871e502010-08-10 01:45:40 -0700377 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Eric Dumazet8efa8852011-05-23 11:02:42 +0000379 qlen = slot->qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 sfq_drop(sch);
Eric Dumazet8efa8852011-05-23 11:02:42 +0000381 /* Return Congestion Notification only if we dropped a packet
382 * from this flow.
383 */
Eric Dumazete1738bd2011-07-29 19:22:42 +0000384 if (qlen != slot->qlen)
385 return NET_XMIT_CN;
386
387 /* As we dropped a packet, better let upper stack know this */
388 qdisc_tree_decrease_qlen(sch, 1);
389 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Patrick McHardy48a8f512008-10-31 00:44:18 -0700392static struct sk_buff *
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800393sfq_dequeue(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct sfq_sched_data *q = qdisc_priv(sch);
396 struct sk_buff *skb;
Eric Dumazetaa3e2192010-12-20 13:18:16 -0800397 sfq_index a, next_a;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000398 struct sfq_slot *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* No active slots */
Eric Dumazeteda83e32010-12-20 12:54:58 +0000401 if (q->tail == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return NULL;
403
Eric Dumazeteeaeb062010-12-28 21:53:33 +0000404next_slot:
Eric Dumazeteda83e32010-12-20 12:54:58 +0000405 a = q->tail->next;
406 slot = &q->slots[a];
Eric Dumazeteeaeb062010-12-28 21:53:33 +0000407 if (slot->allot <= 0) {
408 q->tail = slot;
409 slot->allot += q->scaled_quantum;
410 goto next_slot;
411 }
Eric Dumazeteda83e32010-12-20 12:54:58 +0000412 skb = slot_dequeue_head(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 sfq_dec(q, a);
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800414 qdisc_bstats_update(sch, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sch->q.qlen--;
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700416 sch->qstats.backlog -= qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* Is the slot empty? */
Eric Dumazeteda83e32010-12-20 12:54:58 +0000419 if (slot->qlen == 0) {
420 q->ht[slot->hash] = SFQ_EMPTY_SLOT;
421 next_a = slot->next;
Eric Dumazetaa3e2192010-12-20 13:18:16 -0800422 if (a == next_a) {
Eric Dumazeteda83e32010-12-20 12:54:58 +0000423 q->tail = NULL; /* no more active slots */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return skb;
425 }
Eric Dumazeteda83e32010-12-20 12:54:58 +0000426 q->tail->next = next_a;
Eric Dumazeteeaeb062010-12-28 21:53:33 +0000427 } else {
428 slot->allot -= SFQ_ALLOT_SIZE(qdisc_pkt_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 return skb;
431}
432
433static void
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800434sfq_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct sk_buff *skb;
437
438 while ((skb = sfq_dequeue(sch)) != NULL)
439 kfree_skb(skb);
440}
441
Eric Dumazet225d9b82011-12-21 03:30:11 +0000442/*
443 * When q->perturbation is changed, we rehash all queued skbs
444 * to avoid OOO (Out Of Order) effects.
445 * We dont use sfq_dequeue()/sfq_enqueue() because we dont want to change
446 * counters.
447 */
448static void sfq_rehash(struct sfq_sched_data *q)
449{
450 struct sk_buff *skb;
451 int i;
452 struct sfq_slot *slot;
453 struct sk_buff_head list;
454
455 __skb_queue_head_init(&list);
456
457 for (i = 0; i < SFQ_SLOTS; i++) {
458 slot = &q->slots[i];
459 if (!slot->qlen)
460 continue;
461 while (slot->qlen) {
462 skb = slot_dequeue_head(slot);
463 sfq_dec(q, i);
464 __skb_queue_tail(&list, skb);
465 }
466 q->ht[slot->hash] = SFQ_EMPTY_SLOT;
467 }
468 q->tail = NULL;
469
470 while ((skb = __skb_dequeue(&list)) != NULL) {
471 unsigned int hash = sfq_hash(q, skb);
472 sfq_index x = q->ht[hash];
473
474 slot = &q->slots[x];
475 if (x == SFQ_EMPTY_SLOT) {
476 x = q->dep[0].next; /* get a free slot */
477 q->ht[hash] = x;
478 slot = &q->slots[x];
479 slot->hash = hash;
480 }
481 slot_queue_add(slot, skb);
482 sfq_inc(q, x);
483 if (slot->qlen == 1) { /* The flow is new */
484 if (q->tail == NULL) { /* It is the first flow */
485 slot->next = x;
486 } else {
487 slot->next = q->tail->next;
488 q->tail->next = x;
489 }
490 q->tail = slot;
491 slot->allot = q->scaled_quantum;
492 }
493 }
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static void sfq_perturbation(unsigned long arg)
497{
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800498 struct Qdisc *sch = (struct Qdisc *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct sfq_sched_data *q = qdisc_priv(sch);
Eric Dumazet225d9b82011-12-21 03:30:11 +0000500 spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Eric Dumazet225d9b82011-12-21 03:30:11 +0000502 spin_lock(root_lock);
Stephen Hemmingerd46f8dd2008-01-20 17:19:43 -0800503 q->perturbation = net_random();
Eric Dumazet225d9b82011-12-21 03:30:11 +0000504 if (!q->filter_list && q->tail)
505 sfq_rehash(q);
506 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Alexey Kuznetsov32740dd2007-09-30 17:51:33 -0700508 if (q->perturb_period)
509 mod_timer(&q->perturb_timer, jiffies + q->perturb_period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Patrick McHardy1e904742008-01-22 22:11:17 -0800512static int sfq_change(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct sfq_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800515 struct tc_sfq_qopt *ctl = nla_data(opt);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800516 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Patrick McHardy1e904742008-01-22 22:11:17 -0800518 if (opt->nla_len < nla_attr_size(sizeof(*ctl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -EINVAL;
520
stephen hemminger119b3d32011-02-02 15:19:51 +0000521 if (ctl->divisor &&
522 (!is_power_of_2(ctl->divisor) || ctl->divisor > 65536))
523 return -EINVAL;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 sch_tree_lock(sch);
David S. Miller5ce2d482008-07-08 17:06:30 -0700526 q->quantum = ctl->quantum ? : psched_mtu(qdisc_dev(sch));
Eric Dumazeteeaeb062010-12-28 21:53:33 +0000527 q->scaled_quantum = SFQ_ALLOT_SIZE(q->quantum);
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800528 q->perturb_period = ctl->perturb_period * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (ctl->limit)
Alexey Kuznetsov32740dd2007-09-30 17:51:33 -0700530 q->limit = min_t(u32, ctl->limit, SFQ_DEPTH - 1);
stephen hemminger119b3d32011-02-02 15:19:51 +0000531 if (ctl->divisor)
Eric Dumazet817fb152011-01-20 00:14:58 +0000532 q->divisor = ctl->divisor;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800533 qlen = sch->q.qlen;
Alexey Kuznetsov5588b402007-09-19 10:42:03 -0700534 while (sch->q.qlen > q->limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 sfq_drop(sch);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800536 qdisc_tree_decrease_qlen(sch, qlen - sch->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 del_timer(&q->perturb_timer);
539 if (q->perturb_period) {
Alexey Kuznetsov32740dd2007-09-30 17:51:33 -0700540 mod_timer(&q->perturb_timer, jiffies + q->perturb_period);
Stephen Hemmingerd46f8dd2008-01-20 17:19:43 -0800541 q->perturbation = net_random();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 sch_tree_unlock(sch);
544 return 0;
545}
546
Patrick McHardy1e904742008-01-22 22:11:17 -0800547static int sfq_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct sfq_sched_data *q = qdisc_priv(sch);
Eric Dumazet817fb152011-01-20 00:14:58 +0000550 size_t sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int i;
552
Stephen Hemmingerd3e99482008-01-20 17:18:45 -0800553 q->perturb_timer.function = sfq_perturbation;
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800554 q->perturb_timer.data = (unsigned long)sch;
Stephen Hemmingerd3e99482008-01-20 17:18:45 -0800555 init_timer_deferrable(&q->perturb_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800557 for (i = 0; i < SFQ_DEPTH; i++) {
Eric Dumazeteda83e32010-12-20 12:54:58 +0000558 q->dep[i].next = i + SFQ_SLOTS;
559 q->dep[i].prev = i + SFQ_SLOTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800561
Alexey Kuznetsov32740dd2007-09-30 17:51:33 -0700562 q->limit = SFQ_DEPTH - 1;
Eric Dumazeteda83e32010-12-20 12:54:58 +0000563 q->cur_depth = 0;
564 q->tail = NULL;
Eric Dumazet817fb152011-01-20 00:14:58 +0000565 q->divisor = SFQ_DEFAULT_HASH_DIVISOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (opt == NULL) {
David S. Miller5ce2d482008-07-08 17:06:30 -0700567 q->quantum = psched_mtu(qdisc_dev(sch));
Eric Dumazeteeaeb062010-12-28 21:53:33 +0000568 q->scaled_quantum = SFQ_ALLOT_SIZE(q->quantum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 q->perturb_period = 0;
Stephen Hemmingerd46f8dd2008-01-20 17:19:43 -0800570 q->perturbation = net_random();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 } else {
572 int err = sfq_change(sch, opt);
573 if (err)
574 return err;
575 }
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800576
Eric Dumazet817fb152011-01-20 00:14:58 +0000577 sz = sizeof(q->ht[0]) * q->divisor;
578 q->ht = kmalloc(sz, GFP_KERNEL);
579 if (!q->ht && sz > PAGE_SIZE)
580 q->ht = vmalloc(sz);
581 if (!q->ht)
582 return -ENOMEM;
583 for (i = 0; i < q->divisor; i++)
584 q->ht[i] = SFQ_EMPTY_SLOT;
585
Eric Dumazet18c8d822010-12-31 12:48:55 -0800586 for (i = 0; i < SFQ_SLOTS; i++) {
587 slot_queue_init(&q->slots[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 sfq_link(q, i);
Eric Dumazet18c8d822010-12-31 12:48:55 -0800589 }
Eric Dumazet23624932011-01-21 16:26:09 -0800590 if (q->limit >= 1)
591 sch->flags |= TCQ_F_CAN_BYPASS;
592 else
593 sch->flags &= ~TCQ_F_CAN_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595}
596
597static void sfq_destroy(struct Qdisc *sch)
598{
599 struct sfq_sched_data *q = qdisc_priv(sch);
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800600
Patrick McHardyff31ab52008-07-01 19:52:38 -0700601 tcf_destroy_chain(&q->filter_list);
Jarek Poplawski980c4782008-04-29 03:29:03 -0700602 q->perturb_period = 0;
603 del_timer_sync(&q->perturb_timer);
Eric Dumazet817fb152011-01-20 00:14:58 +0000604 if (is_vmalloc_addr(q->ht))
605 vfree(q->ht);
606 else
607 kfree(q->ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610static int sfq_dump(struct Qdisc *sch, struct sk_buff *skb)
611{
612 struct sfq_sched_data *q = qdisc_priv(sch);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700613 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct tc_sfq_qopt opt;
615
616 opt.quantum = q->quantum;
Stephen Hemminger6f9e98f2008-01-20 17:20:56 -0800617 opt.perturb_period = q->perturb_period / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 opt.limit = q->limit;
Eric Dumazet817fb152011-01-20 00:14:58 +0000620 opt.divisor = q->divisor;
David S. Millercdec7e52008-07-26 02:28:09 -0700621 opt.flows = q->limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Patrick McHardy1e904742008-01-22 22:11:17 -0800623 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 return skb->len;
626
Patrick McHardy1e904742008-01-22 22:11:17 -0800627nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700628 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -1;
630}
631
Jarek Poplawski41065fb2010-08-10 22:31:02 +0000632static struct Qdisc *sfq_leaf(struct Qdisc *sch, unsigned long arg)
633{
634 return NULL;
635}
636
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800637static unsigned long sfq_get(struct Qdisc *sch, u32 classid)
638{
639 return 0;
640}
641
Jarek Poplawskieb4a5522010-08-06 00:22:35 +0000642static unsigned long sfq_bind(struct Qdisc *sch, unsigned long parent,
643 u32 classid)
644{
Eric Dumazet23624932011-01-21 16:26:09 -0800645 /* we cannot bypass queue discipline anymore */
646 sch->flags &= ~TCQ_F_CAN_BYPASS;
Jarek Poplawskieb4a5522010-08-06 00:22:35 +0000647 return 0;
648}
649
Jarek Poplawskida7115d2010-08-09 12:18:17 +0000650static void sfq_put(struct Qdisc *q, unsigned long cl)
651{
652}
653
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800654static struct tcf_proto **sfq_find_tcf(struct Qdisc *sch, unsigned long cl)
655{
656 struct sfq_sched_data *q = qdisc_priv(sch);
657
658 if (cl)
659 return NULL;
660 return &q->filter_list;
661}
662
Patrick McHardy94de78d2008-01-31 18:37:16 -0800663static int sfq_dump_class(struct Qdisc *sch, unsigned long cl,
664 struct sk_buff *skb, struct tcmsg *tcm)
665{
666 tcm->tcm_handle |= TC_H_MIN(cl);
667 return 0;
668}
669
670static int sfq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
671 struct gnet_dump *d)
672{
673 struct sfq_sched_data *q = qdisc_priv(sch);
Eric Dumazetee09b3c2010-12-22 11:39:59 -0800674 sfq_index idx = q->ht[cl - 1];
675 struct gnet_stats_queue qs = { 0 };
676 struct tc_sfq_xstats xstats = { 0 };
Eric Dumazetc4266262010-12-15 08:18:36 +0000677 struct sk_buff *skb;
678
Eric Dumazetee09b3c2010-12-22 11:39:59 -0800679 if (idx != SFQ_EMPTY_SLOT) {
680 const struct sfq_slot *slot = &q->slots[idx];
Patrick McHardy94de78d2008-01-31 18:37:16 -0800681
Eric Dumazeteeaeb062010-12-28 21:53:33 +0000682 xstats.allot = slot->allot << SFQ_ALLOT_SHIFT;
Eric Dumazetee09b3c2010-12-22 11:39:59 -0800683 qs.qlen = slot->qlen;
684 slot_queue_walk(slot, skb)
685 qs.backlog += qdisc_pkt_len(skb);
686 }
Patrick McHardy94de78d2008-01-31 18:37:16 -0800687 if (gnet_stats_copy_queue(d, &qs) < 0)
688 return -1;
689 return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
690}
691
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800692static void sfq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
693{
Patrick McHardy94de78d2008-01-31 18:37:16 -0800694 struct sfq_sched_data *q = qdisc_priv(sch);
695 unsigned int i;
696
697 if (arg->stop)
698 return;
699
Eric Dumazet817fb152011-01-20 00:14:58 +0000700 for (i = 0; i < q->divisor; i++) {
Eric Dumazeteda83e32010-12-20 12:54:58 +0000701 if (q->ht[i] == SFQ_EMPTY_SLOT ||
Patrick McHardy94de78d2008-01-31 18:37:16 -0800702 arg->count < arg->skip) {
703 arg->count++;
704 continue;
705 }
706 if (arg->fn(sch, i + 1, arg) < 0) {
707 arg->stop = 1;
708 break;
709 }
710 arg->count++;
711 }
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800712}
713
714static const struct Qdisc_class_ops sfq_class_ops = {
Jarek Poplawski41065fb2010-08-10 22:31:02 +0000715 .leaf = sfq_leaf,
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800716 .get = sfq_get,
Jarek Poplawskida7115d2010-08-09 12:18:17 +0000717 .put = sfq_put,
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800718 .tcf_chain = sfq_find_tcf,
Jarek Poplawskieb4a5522010-08-06 00:22:35 +0000719 .bind_tcf = sfq_bind,
Jarek Poplawskida7115d2010-08-09 12:18:17 +0000720 .unbind_tcf = sfq_put,
Patrick McHardy94de78d2008-01-31 18:37:16 -0800721 .dump = sfq_dump_class,
722 .dump_stats = sfq_dump_class_stats,
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800723 .walk = sfq_walk,
724};
725
Eric Dumazet20fea082007-11-14 01:44:41 -0800726static struct Qdisc_ops sfq_qdisc_ops __read_mostly = {
Patrick McHardy7d2681a2008-01-31 18:36:52 -0800727 .cl_ops = &sfq_class_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 .id = "sfq",
729 .priv_size = sizeof(struct sfq_sched_data),
730 .enqueue = sfq_enqueue,
731 .dequeue = sfq_dequeue,
Eric Dumazet07bd8df2011-05-25 04:40:11 +0000732 .peek = qdisc_peek_dequeued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 .drop = sfq_drop,
734 .init = sfq_init,
735 .reset = sfq_reset,
736 .destroy = sfq_destroy,
737 .change = NULL,
738 .dump = sfq_dump,
739 .owner = THIS_MODULE,
740};
741
742static int __init sfq_module_init(void)
743{
744 return register_qdisc(&sfq_qdisc_ops);
745}
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900746static void __exit sfq_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 unregister_qdisc(&sfq_qdisc_ops);
749}
750module_init(sfq_module_init)
751module_exit(sfq_module_exit)
752MODULE_LICENSE("GPL");