Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/jiffies.h> |
| 16 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/in.h> |
| 18 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
Patrick McHardy | 0ba4805 | 2007-07-02 22:49:07 -0700 | [diff] [blame] | 20 | #include <linux/skbuff.h> |
Alexey Kuznetsov | 32740dd | 2007-09-30 17:51:33 -0700 | [diff] [blame] | 21 | #include <linux/jhash.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 23 | #include <linux/vmalloc.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 24 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <net/pkt_sched.h> |
Eric Dumazet | 11fca93 | 2011-11-29 03:40:45 +0000 | [diff] [blame] | 26 | #include <net/flow_keys.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 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 Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 45 | This is not the thing that is usually called (W)FQ nowadays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | 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 Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 55 | - "Stochastic" -> It is not 100% fair. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | 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: |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 69 | This implementation limits : |
| 70 | - maximal queue length per flow to 127 packets. |
| 71 | - max mtu to 2^18-1; |
| 72 | - max 65408 flows, |
| 73 | - number of hash buckets to 65536. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | It is easy to increase these values, but not in flight. */ |
| 76 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 77 | #define SFQ_MAX_DEPTH 127 /* max number of packets per flow */ |
| 78 | #define SFQ_DEFAULT_FLOWS 128 |
| 79 | #define SFQ_MAX_FLOWS (0x10000 - SFQ_MAX_DEPTH - 1) /* max number of flows */ |
| 80 | #define SFQ_EMPTY_SLOT 0xffff |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 81 | #define SFQ_DEFAULT_HASH_DIVISOR 1024 |
| 82 | |
Eric Dumazet | eeaeb06 | 2010-12-28 21:53:33 +0000 | [diff] [blame] | 83 | /* We use 16 bits to store allot, and want to handle packets up to 64K |
| 84 | * Scale allot by 8 (1<<3) so that no overflow occurs. |
| 85 | */ |
| 86 | #define SFQ_ALLOT_SHIFT 3 |
| 87 | #define SFQ_ALLOT_SIZE(X) DIV_ROUND_UP(X, 1 << SFQ_ALLOT_SHIFT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 89 | /* This type should contain at least SFQ_MAX_DEPTH + 1 + SFQ_MAX_FLOWS values */ |
| 90 | typedef u16 sfq_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 92 | /* |
| 93 | * We dont use pointers to save space. |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 94 | * Small indexes [0 ... SFQ_MAX_FLOWS - 1] are 'pointers' to slots[] array |
| 95 | * while following values [SFQ_MAX_FLOWS ... SFQ_MAX_FLOWS + SFQ_MAX_DEPTH] |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 96 | * are 'pointers' to dep[] array |
| 97 | */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 98 | struct sfq_head { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | sfq_index next; |
| 100 | sfq_index prev; |
| 101 | }; |
| 102 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 103 | struct sfq_slot { |
| 104 | struct sk_buff *skblist_next; |
| 105 | struct sk_buff *skblist_prev; |
| 106 | sfq_index qlen; /* number of skbs in skblist */ |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 107 | sfq_index next; /* next slot in sfq RR chain */ |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 108 | struct sfq_head dep; /* anchor in dep[] chains */ |
| 109 | unsigned short hash; /* hash value (index in ht[]) */ |
| 110 | short allot; /* credit for this slot */ |
| 111 | }; |
| 112 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 113 | struct sfq_sched_data { |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 114 | /* frequently used fields */ |
| 115 | int limit; /* limit of total number of packets in this qdisc */ |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 116 | unsigned int divisor; /* number of slots in hash table */ |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 117 | unsigned int maxflows; /* number of flows in flows array */ |
| 118 | int headdrop; |
| 119 | int maxdepth; /* limit of packets per flow */ |
| 120 | |
Alexey Kuznetsov | 32740dd | 2007-09-30 17:51:33 -0700 | [diff] [blame] | 121 | u32 perturbation; |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 122 | struct tcf_proto *filter_list; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 123 | sfq_index cur_depth; /* depth of longest slot */ |
Eric Dumazet | eeaeb06 | 2010-12-28 21:53:33 +0000 | [diff] [blame] | 124 | unsigned short scaled_quantum; /* SFQ_ALLOT_SIZE(quantum) */ |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 125 | struct sfq_slot *tail; /* current slot in round */ |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 126 | sfq_index *ht; /* Hash table ('divisor' slots) */ |
| 127 | struct sfq_slot *slots; /* Flows table ('maxflows' entries) */ |
| 128 | |
| 129 | struct sfq_head dep[SFQ_MAX_DEPTH + 1]; |
| 130 | /* Linked lists of slots, indexed by depth |
| 131 | * dep[0] : list of unused flows |
| 132 | * dep[1] : list of flows with 1 packet |
| 133 | * dep[X] : list of flows with X packets |
| 134 | */ |
| 135 | |
| 136 | int perturb_period; |
| 137 | unsigned int quantum; /* Allotment per round: MUST BE >= MTU */ |
| 138 | struct timer_list perturb_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 141 | /* |
| 142 | * sfq_head are either in a sfq_slot or in dep[] array |
| 143 | */ |
| 144 | static inline struct sfq_head *sfq_dep_head(struct sfq_sched_data *q, sfq_index val) |
| 145 | { |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 146 | if (val < SFQ_MAX_FLOWS) |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 147 | return &q->slots[val].dep; |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 148 | return &q->dep[val - SFQ_MAX_FLOWS]; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 151 | /* |
| 152 | * In order to be able to quickly rehash our queue when timer changes |
| 153 | * q->perturbation, we store flow_keys in skb->cb[] |
| 154 | */ |
| 155 | struct sfq_skb_cb { |
| 156 | struct flow_keys keys; |
| 157 | }; |
| 158 | |
| 159 | static inline struct sfq_skb_cb *sfq_skb_cb(const struct sk_buff *skb) |
| 160 | { |
| 161 | BUILD_BUG_ON(sizeof(skb->cb) < |
| 162 | sizeof(struct qdisc_skb_cb) + sizeof(struct sfq_skb_cb)); |
| 163 | return (struct sfq_skb_cb *)qdisc_skb_cb(skb)->data; |
| 164 | } |
| 165 | |
Eric Dumazet | 11fca93 | 2011-11-29 03:40:45 +0000 | [diff] [blame] | 166 | static unsigned int sfq_hash(const struct sfq_sched_data *q, |
| 167 | const struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 169 | const struct flow_keys *keys = &sfq_skb_cb(skb)->keys; |
Eric Dumazet | 11fca93 | 2011-11-29 03:40:45 +0000 | [diff] [blame] | 170 | unsigned int hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 172 | hash = jhash_3words((__force u32)keys->dst, |
| 173 | (__force u32)keys->src ^ keys->ip_proto, |
| 174 | (__force u32)keys->ports, q->perturbation); |
Eric Dumazet | 11fca93 | 2011-11-29 03:40:45 +0000 | [diff] [blame] | 175 | return hash & (q->divisor - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 178 | static unsigned int sfq_classify(struct sk_buff *skb, struct Qdisc *sch, |
| 179 | int *qerr) |
| 180 | { |
| 181 | struct sfq_sched_data *q = qdisc_priv(sch); |
| 182 | struct tcf_result res; |
| 183 | int result; |
| 184 | |
| 185 | if (TC_H_MAJ(skb->priority) == sch->handle && |
| 186 | TC_H_MIN(skb->priority) > 0 && |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 187 | TC_H_MIN(skb->priority) <= q->divisor) |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 188 | return TC_H_MIN(skb->priority); |
| 189 | |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 190 | if (!q->filter_list) { |
| 191 | skb_flow_dissect(skb, &sfq_skb_cb(skb)->keys); |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 192 | return sfq_hash(q, skb) + 1; |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 193 | } |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 194 | |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 195 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 196 | result = tc_classify(skb, q->filter_list, &res); |
| 197 | if (result >= 0) { |
| 198 | #ifdef CONFIG_NET_CLS_ACT |
| 199 | switch (result) { |
| 200 | case TC_ACT_STOLEN: |
| 201 | case TC_ACT_QUEUED: |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 202 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 203 | case TC_ACT_SHOT: |
| 204 | return 0; |
| 205 | } |
| 206 | #endif |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 207 | if (TC_H_MIN(res.classid) <= q->divisor) |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 208 | return TC_H_MIN(res.classid); |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 213 | /* |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 214 | * x : slot number [0 .. SFQ_MAX_FLOWS - 1] |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 215 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | static inline void sfq_link(struct sfq_sched_data *q, sfq_index x) |
| 217 | { |
| 218 | sfq_index p, n; |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 219 | struct sfq_slot *slot = &q->slots[x]; |
| 220 | int qlen = slot->qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 222 | p = qlen + SFQ_MAX_FLOWS; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 223 | n = q->dep[qlen].next; |
| 224 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 225 | slot->dep.next = n; |
| 226 | slot->dep.prev = p; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 227 | |
| 228 | q->dep[qlen].next = x; /* sfq_dep_head(q, p)->next = x */ |
| 229 | sfq_dep_head(q, n)->prev = x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 232 | #define sfq_unlink(q, x, n, p) \ |
| 233 | n = q->slots[x].dep.next; \ |
| 234 | p = q->slots[x].dep.prev; \ |
| 235 | sfq_dep_head(q, p)->next = n; \ |
| 236 | sfq_dep_head(q, n)->prev = p |
| 237 | |
| 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | static inline void sfq_dec(struct sfq_sched_data *q, sfq_index x) |
| 240 | { |
| 241 | sfq_index p, n; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 242 | int d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 244 | sfq_unlink(q, x, n, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 246 | d = q->slots[x].qlen--; |
| 247 | if (n == p && q->cur_depth == d) |
| 248 | q->cur_depth--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | sfq_link(q, x); |
| 250 | } |
| 251 | |
| 252 | static inline void sfq_inc(struct sfq_sched_data *q, sfq_index x) |
| 253 | { |
| 254 | sfq_index p, n; |
| 255 | int d; |
| 256 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 257 | sfq_unlink(q, x, n, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 259 | d = ++q->slots[x].qlen; |
| 260 | if (q->cur_depth < d) |
| 261 | q->cur_depth = d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | sfq_link(q, x); |
| 263 | } |
| 264 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 265 | /* helper functions : might be changed when/if skb use a standard list_head */ |
| 266 | |
| 267 | /* remove one skb from tail of slot queue */ |
| 268 | static inline struct sk_buff *slot_dequeue_tail(struct sfq_slot *slot) |
| 269 | { |
| 270 | struct sk_buff *skb = slot->skblist_prev; |
| 271 | |
| 272 | slot->skblist_prev = skb->prev; |
Eric Dumazet | ee09b3c | 2010-12-22 11:39:59 -0800 | [diff] [blame] | 273 | skb->prev->next = (struct sk_buff *)slot; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 274 | skb->next = skb->prev = NULL; |
| 275 | return skb; |
| 276 | } |
| 277 | |
| 278 | /* remove one skb from head of slot queue */ |
| 279 | static inline struct sk_buff *slot_dequeue_head(struct sfq_slot *slot) |
| 280 | { |
| 281 | struct sk_buff *skb = slot->skblist_next; |
| 282 | |
| 283 | slot->skblist_next = skb->next; |
Eric Dumazet | 18c8d82 | 2010-12-31 12:48:55 -0800 | [diff] [blame] | 284 | skb->next->prev = (struct sk_buff *)slot; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 285 | skb->next = skb->prev = NULL; |
| 286 | return skb; |
| 287 | } |
| 288 | |
| 289 | static inline void slot_queue_init(struct sfq_slot *slot) |
| 290 | { |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 291 | memset(slot, 0, sizeof(*slot)); |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 292 | slot->skblist_prev = slot->skblist_next = (struct sk_buff *)slot; |
| 293 | } |
| 294 | |
| 295 | /* add skb to slot queue (tail add) */ |
| 296 | static inline void slot_queue_add(struct sfq_slot *slot, struct sk_buff *skb) |
| 297 | { |
| 298 | skb->prev = slot->skblist_prev; |
| 299 | skb->next = (struct sk_buff *)slot; |
| 300 | slot->skblist_prev->next = skb; |
| 301 | slot->skblist_prev = skb; |
| 302 | } |
| 303 | |
| 304 | #define slot_queue_walk(slot, skb) \ |
| 305 | for (skb = slot->skblist_next; \ |
| 306 | skb != (struct sk_buff *)slot; \ |
| 307 | skb = skb->next) |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | static unsigned int sfq_drop(struct Qdisc *sch) |
| 310 | { |
| 311 | struct sfq_sched_data *q = qdisc_priv(sch); |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 312 | sfq_index x, d = q->cur_depth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | struct sk_buff *skb; |
| 314 | unsigned int len; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 315 | struct sfq_slot *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 317 | /* Queue is full! Find the longest slot and drop tail packet from it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | if (d > 1) { |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 319 | x = q->dep[d].next; |
| 320 | slot = &q->slots[x]; |
| 321 | drop: |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 322 | skb = q->headdrop ? slot_dequeue_head(slot) : slot_dequeue_tail(slot); |
Jussi Kivilinna | 0abf77e | 2008-07-20 00:08:27 -0700 | [diff] [blame] | 323 | len = qdisc_pkt_len(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | sfq_dec(q, x); |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 325 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | sch->q.qlen--; |
| 327 | sch->qstats.drops++; |
Patrick McHardy | f5539eb | 2006-03-20 19:01:38 -0800 | [diff] [blame] | 328 | sch->qstats.backlog -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | return len; |
| 330 | } |
| 331 | |
| 332 | if (d == 1) { |
| 333 | /* It is difficult to believe, but ALL THE SLOTS HAVE LENGTH 1. */ |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 334 | x = q->tail->next; |
| 335 | slot = &q->slots[x]; |
| 336 | q->tail->next = slot->next; |
| 337 | q->ht[slot->hash] = SFQ_EMPTY_SLOT; |
| 338 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int |
Stephen Hemminger | 6f9e98f | 2008-01-20 17:20:56 -0800 | [diff] [blame] | 345 | sfq_enqueue(struct sk_buff *skb, struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | struct sfq_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 348 | unsigned int hash; |
Eric Dumazet | 8efa885 | 2011-05-23 11:02:42 +0000 | [diff] [blame] | 349 | sfq_index x, qlen; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 350 | struct sfq_slot *slot; |
Jarek Poplawski | 7f3ff4f | 2008-12-21 20:14:48 -0800 | [diff] [blame] | 351 | int uninitialized_var(ret); |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 352 | |
| 353 | hash = sfq_classify(skb, sch, &ret); |
| 354 | if (hash == 0) { |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 355 | if (ret & __NET_XMIT_BYPASS) |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 356 | sch->qstats.drops++; |
| 357 | kfree_skb(skb); |
| 358 | return ret; |
| 359 | } |
| 360 | hash--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | x = q->ht[hash]; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 363 | slot = &q->slots[x]; |
| 364 | if (x == SFQ_EMPTY_SLOT) { |
| 365 | x = q->dep[0].next; /* get a free slot */ |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 366 | if (x >= SFQ_MAX_FLOWS) |
| 367 | return qdisc_drop(skb, sch); |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 368 | q->ht[hash] = x; |
| 369 | slot = &q->slots[x]; |
| 370 | slot->hash = hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
Stephen Hemminger | 6f9e98f | 2008-01-20 17:20:56 -0800 | [diff] [blame] | 372 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 373 | if (slot->qlen >= q->maxdepth) { |
| 374 | struct sk_buff *head; |
| 375 | |
| 376 | if (!q->headdrop) |
| 377 | return qdisc_drop(skb, sch); |
| 378 | |
| 379 | head = slot_dequeue_head(slot); |
| 380 | sch->qstats.backlog -= qdisc_pkt_len(head); |
| 381 | qdisc_drop(head, sch); |
| 382 | |
| 383 | sch->qstats.backlog += qdisc_pkt_len(skb); |
| 384 | slot_queue_add(slot, skb); |
| 385 | return NET_XMIT_CN; |
| 386 | } |
Alexey Kuznetsov | 32740dd | 2007-09-30 17:51:33 -0700 | [diff] [blame] | 387 | |
Jussi Kivilinna | 0abf77e | 2008-07-20 00:08:27 -0700 | [diff] [blame] | 388 | sch->qstats.backlog += qdisc_pkt_len(skb); |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 389 | slot_queue_add(slot, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | sfq_inc(q, x); |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 391 | if (slot->qlen == 1) { /* The flow is new */ |
| 392 | if (q->tail == NULL) { /* It is the first flow */ |
| 393 | slot->next = x; |
Eric Dumazet | d47a0ac | 2012-01-01 18:33:31 +0000 | [diff] [blame] | 394 | q->tail = slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } else { |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 396 | slot->next = q->tail->next; |
| 397 | q->tail->next = x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
Eric Dumazet | eeaeb06 | 2010-12-28 21:53:33 +0000 | [diff] [blame] | 399 | slot->allot = q->scaled_quantum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 401 | if (++sch->q.qlen <= q->limit) |
Ben Greear | 9871e50 | 2010-08-10 01:45:40 -0700 | [diff] [blame] | 402 | return NET_XMIT_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Eric Dumazet | 8efa885 | 2011-05-23 11:02:42 +0000 | [diff] [blame] | 404 | qlen = slot->qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | sfq_drop(sch); |
Eric Dumazet | 8efa885 | 2011-05-23 11:02:42 +0000 | [diff] [blame] | 406 | /* Return Congestion Notification only if we dropped a packet |
| 407 | * from this flow. |
| 408 | */ |
Eric Dumazet | e1738bd | 2011-07-29 19:22:42 +0000 | [diff] [blame] | 409 | if (qlen != slot->qlen) |
| 410 | return NET_XMIT_CN; |
| 411 | |
| 412 | /* As we dropped a packet, better let upper stack know this */ |
| 413 | qdisc_tree_decrease_qlen(sch, 1); |
| 414 | return NET_XMIT_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 417 | static struct sk_buff * |
Stephen Hemminger | 6f9e98f | 2008-01-20 17:20:56 -0800 | [diff] [blame] | 418 | sfq_dequeue(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
| 420 | struct sfq_sched_data *q = qdisc_priv(sch); |
| 421 | struct sk_buff *skb; |
Eric Dumazet | aa3e219 | 2010-12-20 13:18:16 -0800 | [diff] [blame] | 422 | sfq_index a, next_a; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 423 | struct sfq_slot *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | /* No active slots */ |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 426 | if (q->tail == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | return NULL; |
| 428 | |
Eric Dumazet | eeaeb06 | 2010-12-28 21:53:33 +0000 | [diff] [blame] | 429 | next_slot: |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 430 | a = q->tail->next; |
| 431 | slot = &q->slots[a]; |
Eric Dumazet | eeaeb06 | 2010-12-28 21:53:33 +0000 | [diff] [blame] | 432 | if (slot->allot <= 0) { |
| 433 | q->tail = slot; |
| 434 | slot->allot += q->scaled_quantum; |
| 435 | goto next_slot; |
| 436 | } |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 437 | skb = slot_dequeue_head(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | sfq_dec(q, a); |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 439 | qdisc_bstats_update(sch, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | sch->q.qlen--; |
Jussi Kivilinna | 0abf77e | 2008-07-20 00:08:27 -0700 | [diff] [blame] | 441 | sch->qstats.backlog -= qdisc_pkt_len(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
| 443 | /* Is the slot empty? */ |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 444 | if (slot->qlen == 0) { |
| 445 | q->ht[slot->hash] = SFQ_EMPTY_SLOT; |
| 446 | next_a = slot->next; |
Eric Dumazet | aa3e219 | 2010-12-20 13:18:16 -0800 | [diff] [blame] | 447 | if (a == next_a) { |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 448 | q->tail = NULL; /* no more active slots */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return skb; |
| 450 | } |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 451 | q->tail->next = next_a; |
Eric Dumazet | eeaeb06 | 2010-12-28 21:53:33 +0000 | [diff] [blame] | 452 | } else { |
| 453 | slot->allot -= SFQ_ALLOT_SIZE(qdisc_pkt_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | return skb; |
| 456 | } |
| 457 | |
| 458 | static void |
Stephen Hemminger | 6f9e98f | 2008-01-20 17:20:56 -0800 | [diff] [blame] | 459 | sfq_reset(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
| 461 | struct sk_buff *skb; |
| 462 | |
| 463 | while ((skb = sfq_dequeue(sch)) != NULL) |
| 464 | kfree_skb(skb); |
| 465 | } |
| 466 | |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 467 | /* |
| 468 | * When q->perturbation is changed, we rehash all queued skbs |
| 469 | * to avoid OOO (Out Of Order) effects. |
| 470 | * We dont use sfq_dequeue()/sfq_enqueue() because we dont want to change |
| 471 | * counters. |
| 472 | */ |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 473 | static void sfq_rehash(struct Qdisc *sch) |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 474 | { |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 475 | struct sfq_sched_data *q = qdisc_priv(sch); |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 476 | struct sk_buff *skb; |
| 477 | int i; |
| 478 | struct sfq_slot *slot; |
| 479 | struct sk_buff_head list; |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 480 | int dropped = 0; |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 481 | |
| 482 | __skb_queue_head_init(&list); |
| 483 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 484 | for (i = 0; i < q->maxflows; i++) { |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 485 | slot = &q->slots[i]; |
| 486 | if (!slot->qlen) |
| 487 | continue; |
| 488 | while (slot->qlen) { |
| 489 | skb = slot_dequeue_head(slot); |
| 490 | sfq_dec(q, i); |
| 491 | __skb_queue_tail(&list, skb); |
| 492 | } |
| 493 | q->ht[slot->hash] = SFQ_EMPTY_SLOT; |
| 494 | } |
| 495 | q->tail = NULL; |
| 496 | |
| 497 | while ((skb = __skb_dequeue(&list)) != NULL) { |
| 498 | unsigned int hash = sfq_hash(q, skb); |
| 499 | sfq_index x = q->ht[hash]; |
| 500 | |
| 501 | slot = &q->slots[x]; |
| 502 | if (x == SFQ_EMPTY_SLOT) { |
| 503 | x = q->dep[0].next; /* get a free slot */ |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 504 | if (x >= SFQ_MAX_FLOWS) { |
| 505 | drop: sch->qstats.backlog -= qdisc_pkt_len(skb); |
| 506 | kfree_skb(skb); |
| 507 | dropped++; |
| 508 | continue; |
| 509 | } |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 510 | q->ht[hash] = x; |
| 511 | slot = &q->slots[x]; |
| 512 | slot->hash = hash; |
| 513 | } |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 514 | if (slot->qlen >= q->maxdepth) |
| 515 | goto drop; |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 516 | slot_queue_add(slot, skb); |
| 517 | sfq_inc(q, x); |
| 518 | if (slot->qlen == 1) { /* The flow is new */ |
| 519 | if (q->tail == NULL) { /* It is the first flow */ |
| 520 | slot->next = x; |
| 521 | } else { |
| 522 | slot->next = q->tail->next; |
| 523 | q->tail->next = x; |
| 524 | } |
| 525 | q->tail = slot; |
| 526 | slot->allot = q->scaled_quantum; |
| 527 | } |
| 528 | } |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 529 | sch->q.qlen -= dropped; |
| 530 | qdisc_tree_decrease_qlen(sch, dropped); |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | static void sfq_perturbation(unsigned long arg) |
| 534 | { |
Stephen Hemminger | 6f9e98f | 2008-01-20 17:20:56 -0800 | [diff] [blame] | 535 | struct Qdisc *sch = (struct Qdisc *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | struct sfq_sched_data *q = qdisc_priv(sch); |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 537 | spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 539 | spin_lock(root_lock); |
Stephen Hemminger | d46f8dd | 2008-01-20 17:19:43 -0800 | [diff] [blame] | 540 | q->perturbation = net_random(); |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 541 | if (!q->filter_list && q->tail) |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 542 | sfq_rehash(sch); |
Eric Dumazet | 225d9b8 | 2011-12-21 03:30:11 +0000 | [diff] [blame] | 543 | spin_unlock(root_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
Alexey Kuznetsov | 32740dd | 2007-09-30 17:51:33 -0700 | [diff] [blame] | 545 | if (q->perturb_period) |
| 546 | mod_timer(&q->perturb_timer, jiffies + q->perturb_period); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 549 | static int sfq_change(struct Qdisc *sch, struct nlattr *opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
| 551 | struct sfq_sched_data *q = qdisc_priv(sch); |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 552 | struct tc_sfq_qopt *ctl = nla_data(opt); |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 553 | struct tc_sfq_qopt_v1 *ctl_v1 = NULL; |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 554 | unsigned int qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 556 | if (opt->nla_len < nla_attr_size(sizeof(*ctl))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return -EINVAL; |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 558 | if (opt->nla_len >= nla_attr_size(sizeof(*ctl_v1))) |
| 559 | ctl_v1 = nla_data(opt); |
stephen hemminger | 119b3d3 | 2011-02-02 15:19:51 +0000 | [diff] [blame] | 560 | if (ctl->divisor && |
| 561 | (!is_power_of_2(ctl->divisor) || ctl->divisor > 65536)) |
| 562 | return -EINVAL; |
| 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | sch_tree_lock(sch); |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 565 | if (ctl->quantum) { |
| 566 | q->quantum = ctl->quantum; |
| 567 | q->scaled_quantum = SFQ_ALLOT_SIZE(q->quantum); |
| 568 | } |
Stephen Hemminger | 6f9e98f | 2008-01-20 17:20:56 -0800 | [diff] [blame] | 569 | q->perturb_period = ctl->perturb_period * HZ; |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 570 | if (ctl->flows) |
| 571 | q->maxflows = min_t(u32, ctl->flows, SFQ_MAX_FLOWS); |
| 572 | if (ctl->divisor) { |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 573 | q->divisor = ctl->divisor; |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 574 | q->maxflows = min_t(u32, q->maxflows, q->divisor); |
| 575 | } |
| 576 | if (ctl_v1) { |
| 577 | if (ctl_v1->depth) |
| 578 | q->maxdepth = min_t(u32, ctl_v1->depth, SFQ_MAX_DEPTH); |
| 579 | q->headdrop = ctl_v1->headdrop; |
| 580 | } |
| 581 | if (ctl->limit) { |
| 582 | q->limit = min_t(u32, ctl->limit, q->maxdepth * q->maxflows); |
| 583 | q->maxflows = min_t(u32, q->maxflows, q->limit); |
| 584 | } |
| 585 | |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 586 | qlen = sch->q.qlen; |
Alexey Kuznetsov | 5588b40 | 2007-09-19 10:42:03 -0700 | [diff] [blame] | 587 | while (sch->q.qlen > q->limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | sfq_drop(sch); |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 589 | qdisc_tree_decrease_qlen(sch, qlen - sch->q.qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | del_timer(&q->perturb_timer); |
| 592 | if (q->perturb_period) { |
Alexey Kuznetsov | 32740dd | 2007-09-30 17:51:33 -0700 | [diff] [blame] | 593 | mod_timer(&q->perturb_timer, jiffies + q->perturb_period); |
Stephen Hemminger | d46f8dd | 2008-01-20 17:19:43 -0800 | [diff] [blame] | 594 | q->perturbation = net_random(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | sch_tree_unlock(sch); |
| 597 | return 0; |
| 598 | } |
| 599 | |
Eric Dumazet | bd16a6c | 2012-01-04 06:22:24 +0000 | [diff] [blame] | 600 | static void *sfq_alloc(size_t sz) |
| 601 | { |
| 602 | void *ptr = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN); |
| 603 | |
| 604 | if (!ptr) |
| 605 | ptr = vmalloc(sz); |
| 606 | return ptr; |
| 607 | } |
| 608 | |
| 609 | static void sfq_free(void *addr) |
| 610 | { |
| 611 | if (addr) { |
| 612 | if (is_vmalloc_addr(addr)) |
| 613 | vfree(addr); |
| 614 | else |
| 615 | kfree(addr); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static void sfq_destroy(struct Qdisc *sch) |
| 620 | { |
| 621 | struct sfq_sched_data *q = qdisc_priv(sch); |
| 622 | |
| 623 | tcf_destroy_chain(&q->filter_list); |
| 624 | q->perturb_period = 0; |
| 625 | del_timer_sync(&q->perturb_timer); |
| 626 | sfq_free(q->ht); |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 627 | sfq_free(q->slots); |
Eric Dumazet | bd16a6c | 2012-01-04 06:22:24 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 630 | static int sfq_init(struct Qdisc *sch, struct nlattr *opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
| 632 | struct sfq_sched_data *q = qdisc_priv(sch); |
| 633 | int i; |
| 634 | |
Stephen Hemminger | d3e9948 | 2008-01-20 17:18:45 -0800 | [diff] [blame] | 635 | q->perturb_timer.function = sfq_perturbation; |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 636 | q->perturb_timer.data = (unsigned long)sch; |
Stephen Hemminger | d3e9948 | 2008-01-20 17:18:45 -0800 | [diff] [blame] | 637 | init_timer_deferrable(&q->perturb_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 639 | for (i = 0; i < SFQ_MAX_DEPTH + 1; i++) { |
| 640 | q->dep[i].next = i + SFQ_MAX_FLOWS; |
| 641 | q->dep[i].prev = i + SFQ_MAX_FLOWS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
Stephen Hemminger | 6f9e98f | 2008-01-20 17:20:56 -0800 | [diff] [blame] | 643 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 644 | q->limit = SFQ_MAX_DEPTH; |
| 645 | q->maxdepth = SFQ_MAX_DEPTH; |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 646 | q->cur_depth = 0; |
| 647 | q->tail = NULL; |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 648 | q->divisor = SFQ_DEFAULT_HASH_DIVISOR; |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 649 | q->maxflows = SFQ_DEFAULT_FLOWS; |
Eric Dumazet | 02a9098 | 2012-01-04 06:23:01 +0000 | [diff] [blame] | 650 | q->quantum = psched_mtu(qdisc_dev(sch)); |
| 651 | q->scaled_quantum = SFQ_ALLOT_SIZE(q->quantum); |
| 652 | q->perturb_period = 0; |
| 653 | q->perturbation = net_random(); |
| 654 | |
| 655 | if (opt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | int err = sfq_change(sch, opt); |
| 657 | if (err) |
| 658 | return err; |
| 659 | } |
Stephen Hemminger | 6f9e98f | 2008-01-20 17:20:56 -0800 | [diff] [blame] | 660 | |
Eric Dumazet | bd16a6c | 2012-01-04 06:22:24 +0000 | [diff] [blame] | 661 | q->ht = sfq_alloc(sizeof(q->ht[0]) * q->divisor); |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 662 | q->slots = sfq_alloc(sizeof(q->slots[0]) * q->maxflows); |
| 663 | if (!q->ht || !q->slots) { |
Eric Dumazet | bd16a6c | 2012-01-04 06:22:24 +0000 | [diff] [blame] | 664 | sfq_destroy(sch); |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 665 | return -ENOMEM; |
Eric Dumazet | bd16a6c | 2012-01-04 06:22:24 +0000 | [diff] [blame] | 666 | } |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 667 | for (i = 0; i < q->divisor; i++) |
| 668 | q->ht[i] = SFQ_EMPTY_SLOT; |
| 669 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 670 | for (i = 0; i < q->maxflows; i++) { |
Eric Dumazet | 18c8d82 | 2010-12-31 12:48:55 -0800 | [diff] [blame] | 671 | slot_queue_init(&q->slots[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | sfq_link(q, i); |
Eric Dumazet | 18c8d82 | 2010-12-31 12:48:55 -0800 | [diff] [blame] | 673 | } |
Eric Dumazet | 2362493 | 2011-01-21 16:26:09 -0800 | [diff] [blame] | 674 | if (q->limit >= 1) |
| 675 | sch->flags |= TCQ_F_CAN_BYPASS; |
| 676 | else |
| 677 | sch->flags &= ~TCQ_F_CAN_BYPASS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | static int sfq_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 682 | { |
| 683 | struct sfq_sched_data *q = qdisc_priv(sch); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 684 | unsigned char *b = skb_tail_pointer(skb); |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 685 | struct tc_sfq_qopt_v1 opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Eric Dumazet | 18cb809 | 2012-01-04 14:18:38 +0000 | [diff] [blame] | 687 | memset(&opt, 0, sizeof(opt)); |
| 688 | opt.v0.quantum = q->quantum; |
| 689 | opt.v0.perturb_period = q->perturb_period / HZ; |
| 690 | opt.v0.limit = q->limit; |
| 691 | opt.v0.divisor = q->divisor; |
| 692 | opt.v0.flows = q->maxflows; |
| 693 | opt.depth = q->maxdepth; |
| 694 | opt.headdrop = q->headdrop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 696 | NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | return skb->len; |
| 699 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 700 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 701 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | return -1; |
| 703 | } |
| 704 | |
Jarek Poplawski | 41065fb | 2010-08-10 22:31:02 +0000 | [diff] [blame] | 705 | static struct Qdisc *sfq_leaf(struct Qdisc *sch, unsigned long arg) |
| 706 | { |
| 707 | return NULL; |
| 708 | } |
| 709 | |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 710 | static unsigned long sfq_get(struct Qdisc *sch, u32 classid) |
| 711 | { |
| 712 | return 0; |
| 713 | } |
| 714 | |
Jarek Poplawski | eb4a552 | 2010-08-06 00:22:35 +0000 | [diff] [blame] | 715 | static unsigned long sfq_bind(struct Qdisc *sch, unsigned long parent, |
| 716 | u32 classid) |
| 717 | { |
Eric Dumazet | 2362493 | 2011-01-21 16:26:09 -0800 | [diff] [blame] | 718 | /* we cannot bypass queue discipline anymore */ |
| 719 | sch->flags &= ~TCQ_F_CAN_BYPASS; |
Jarek Poplawski | eb4a552 | 2010-08-06 00:22:35 +0000 | [diff] [blame] | 720 | return 0; |
| 721 | } |
| 722 | |
Jarek Poplawski | da7115d | 2010-08-09 12:18:17 +0000 | [diff] [blame] | 723 | static void sfq_put(struct Qdisc *q, unsigned long cl) |
| 724 | { |
| 725 | } |
| 726 | |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 727 | static struct tcf_proto **sfq_find_tcf(struct Qdisc *sch, unsigned long cl) |
| 728 | { |
| 729 | struct sfq_sched_data *q = qdisc_priv(sch); |
| 730 | |
| 731 | if (cl) |
| 732 | return NULL; |
| 733 | return &q->filter_list; |
| 734 | } |
| 735 | |
Patrick McHardy | 94de78d | 2008-01-31 18:37:16 -0800 | [diff] [blame] | 736 | static int sfq_dump_class(struct Qdisc *sch, unsigned long cl, |
| 737 | struct sk_buff *skb, struct tcmsg *tcm) |
| 738 | { |
| 739 | tcm->tcm_handle |= TC_H_MIN(cl); |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static int sfq_dump_class_stats(struct Qdisc *sch, unsigned long cl, |
| 744 | struct gnet_dump *d) |
| 745 | { |
| 746 | struct sfq_sched_data *q = qdisc_priv(sch); |
Eric Dumazet | ee09b3c | 2010-12-22 11:39:59 -0800 | [diff] [blame] | 747 | sfq_index idx = q->ht[cl - 1]; |
| 748 | struct gnet_stats_queue qs = { 0 }; |
| 749 | struct tc_sfq_xstats xstats = { 0 }; |
Eric Dumazet | c426626 | 2010-12-15 08:18:36 +0000 | [diff] [blame] | 750 | struct sk_buff *skb; |
| 751 | |
Eric Dumazet | ee09b3c | 2010-12-22 11:39:59 -0800 | [diff] [blame] | 752 | if (idx != SFQ_EMPTY_SLOT) { |
| 753 | const struct sfq_slot *slot = &q->slots[idx]; |
Patrick McHardy | 94de78d | 2008-01-31 18:37:16 -0800 | [diff] [blame] | 754 | |
Eric Dumazet | eeaeb06 | 2010-12-28 21:53:33 +0000 | [diff] [blame] | 755 | xstats.allot = slot->allot << SFQ_ALLOT_SHIFT; |
Eric Dumazet | ee09b3c | 2010-12-22 11:39:59 -0800 | [diff] [blame] | 756 | qs.qlen = slot->qlen; |
| 757 | slot_queue_walk(slot, skb) |
| 758 | qs.backlog += qdisc_pkt_len(skb); |
| 759 | } |
Patrick McHardy | 94de78d | 2008-01-31 18:37:16 -0800 | [diff] [blame] | 760 | if (gnet_stats_copy_queue(d, &qs) < 0) |
| 761 | return -1; |
| 762 | return gnet_stats_copy_app(d, &xstats, sizeof(xstats)); |
| 763 | } |
| 764 | |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 765 | static void sfq_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 766 | { |
Patrick McHardy | 94de78d | 2008-01-31 18:37:16 -0800 | [diff] [blame] | 767 | struct sfq_sched_data *q = qdisc_priv(sch); |
| 768 | unsigned int i; |
| 769 | |
| 770 | if (arg->stop) |
| 771 | return; |
| 772 | |
Eric Dumazet | 817fb15 | 2011-01-20 00:14:58 +0000 | [diff] [blame] | 773 | for (i = 0; i < q->divisor; i++) { |
Eric Dumazet | eda83e3 | 2010-12-20 12:54:58 +0000 | [diff] [blame] | 774 | if (q->ht[i] == SFQ_EMPTY_SLOT || |
Patrick McHardy | 94de78d | 2008-01-31 18:37:16 -0800 | [diff] [blame] | 775 | arg->count < arg->skip) { |
| 776 | arg->count++; |
| 777 | continue; |
| 778 | } |
| 779 | if (arg->fn(sch, i + 1, arg) < 0) { |
| 780 | arg->stop = 1; |
| 781 | break; |
| 782 | } |
| 783 | arg->count++; |
| 784 | } |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | static const struct Qdisc_class_ops sfq_class_ops = { |
Jarek Poplawski | 41065fb | 2010-08-10 22:31:02 +0000 | [diff] [blame] | 788 | .leaf = sfq_leaf, |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 789 | .get = sfq_get, |
Jarek Poplawski | da7115d | 2010-08-09 12:18:17 +0000 | [diff] [blame] | 790 | .put = sfq_put, |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 791 | .tcf_chain = sfq_find_tcf, |
Jarek Poplawski | eb4a552 | 2010-08-06 00:22:35 +0000 | [diff] [blame] | 792 | .bind_tcf = sfq_bind, |
Jarek Poplawski | da7115d | 2010-08-09 12:18:17 +0000 | [diff] [blame] | 793 | .unbind_tcf = sfq_put, |
Patrick McHardy | 94de78d | 2008-01-31 18:37:16 -0800 | [diff] [blame] | 794 | .dump = sfq_dump_class, |
| 795 | .dump_stats = sfq_dump_class_stats, |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 796 | .walk = sfq_walk, |
| 797 | }; |
| 798 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 799 | static struct Qdisc_ops sfq_qdisc_ops __read_mostly = { |
Patrick McHardy | 7d2681a | 2008-01-31 18:36:52 -0800 | [diff] [blame] | 800 | .cl_ops = &sfq_class_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | .id = "sfq", |
| 802 | .priv_size = sizeof(struct sfq_sched_data), |
| 803 | .enqueue = sfq_enqueue, |
| 804 | .dequeue = sfq_dequeue, |
Eric Dumazet | 07bd8df | 2011-05-25 04:40:11 +0000 | [diff] [blame] | 805 | .peek = qdisc_peek_dequeued, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | .drop = sfq_drop, |
| 807 | .init = sfq_init, |
| 808 | .reset = sfq_reset, |
| 809 | .destroy = sfq_destroy, |
| 810 | .change = NULL, |
| 811 | .dump = sfq_dump, |
| 812 | .owner = THIS_MODULE, |
| 813 | }; |
| 814 | |
| 815 | static int __init sfq_module_init(void) |
| 816 | { |
| 817 | return register_qdisc(&sfq_qdisc_ops); |
| 818 | } |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 819 | static void __exit sfq_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
| 821 | unregister_qdisc(&sfq_qdisc_ops); |
| 822 | } |
| 823 | module_init(sfq_module_init) |
| 824 | module_exit(sfq_module_exit) |
| 825 | MODULE_LICENSE("GPL"); |