Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Fair Queue CoDel 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 | * |
Eric Dumazet | 80ba92f | 2015-05-08 15:05:12 -0700 | [diff] [blame] | 9 | * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com> |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/jiffies.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/in.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/jhash.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | #include <net/netlink.h> |
| 25 | #include <net/pkt_sched.h> |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 26 | #include <net/codel.h> |
Michal Kazior | d068ca2 | 2016-04-22 14:15:59 +0200 | [diff] [blame^] | 27 | #include <net/codel_impl.h> |
| 28 | #include <net/codel_qdisc.h> |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 29 | |
| 30 | /* Fair Queue CoDel. |
| 31 | * |
| 32 | * Principles : |
| 33 | * Packets are classified (internal classifier or external) on flows. |
| 34 | * This is a Stochastic model (as we use a hash, several flows |
| 35 | * might be hashed on same slot) |
| 36 | * Each flow has a CoDel managed queue. |
| 37 | * Flows are linked onto two (Round Robin) lists, |
| 38 | * so that new flows have priority on old ones. |
| 39 | * |
| 40 | * For a given flow, packets are not reordered (CoDel uses a FIFO) |
| 41 | * head drops only. |
| 42 | * ECN capability is on by default. |
| 43 | * Low memory footprint (64 bytes per flow) |
| 44 | */ |
| 45 | |
| 46 | struct fq_codel_flow { |
| 47 | struct sk_buff *head; |
| 48 | struct sk_buff *tail; |
| 49 | struct list_head flowchain; |
| 50 | int deficit; |
| 51 | u32 dropped; /* number of drops (or ECN marks) on this flow */ |
| 52 | struct codel_vars cvars; |
| 53 | }; /* please try to keep this structure <= 64 bytes */ |
| 54 | |
| 55 | struct fq_codel_sched_data { |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 56 | struct tcf_proto __rcu *filter_list; /* optional external classifier */ |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 57 | struct fq_codel_flow *flows; /* Flows table [flows_cnt] */ |
| 58 | u32 *backlogs; /* backlog table [flows_cnt] */ |
| 59 | u32 flows_cnt; /* number of flows */ |
| 60 | u32 perturbation; /* hash perturbation */ |
| 61 | u32 quantum; /* psched_mtu(qdisc_dev(sch)); */ |
| 62 | struct codel_params cparams; |
| 63 | struct codel_stats cstats; |
| 64 | u32 drop_overlimit; |
| 65 | u32 new_flow_count; |
| 66 | |
| 67 | struct list_head new_flows; /* list of new flows */ |
| 68 | struct list_head old_flows; /* list of old flows */ |
| 69 | }; |
| 70 | |
| 71 | static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q, |
Tom Herbert | 342db22 | 2015-05-01 11:30:13 -0700 | [diff] [blame] | 72 | struct sk_buff *skb) |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 73 | { |
Tom Herbert | 342db22 | 2015-05-01 11:30:13 -0700 | [diff] [blame] | 74 | u32 hash = skb_get_hash_perturb(skb, q->perturbation); |
Daniel Borkmann | 8fc54f6 | 2014-08-23 20:58:54 +0200 | [diff] [blame] | 75 | |
| 76 | return reciprocal_scale(hash, q->flows_cnt); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch, |
| 80 | int *qerr) |
| 81 | { |
| 82 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 83 | struct tcf_proto *filter; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 84 | struct tcf_result res; |
| 85 | int result; |
| 86 | |
| 87 | if (TC_H_MAJ(skb->priority) == sch->handle && |
| 88 | TC_H_MIN(skb->priority) > 0 && |
| 89 | TC_H_MIN(skb->priority) <= q->flows_cnt) |
| 90 | return TC_H_MIN(skb->priority); |
| 91 | |
Valdis.Kletnieks@vt.edu | 69204cf | 2014-12-09 16:15:50 -0500 | [diff] [blame] | 92 | filter = rcu_dereference_bh(q->filter_list); |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 93 | if (!filter) |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 94 | return fq_codel_hash(q, skb) + 1; |
| 95 | |
| 96 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; |
Daniel Borkmann | 3b3ae88 | 2015-08-26 23:00:06 +0200 | [diff] [blame] | 97 | result = tc_classify(skb, filter, &res, false); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 98 | if (result >= 0) { |
| 99 | #ifdef CONFIG_NET_CLS_ACT |
| 100 | switch (result) { |
| 101 | case TC_ACT_STOLEN: |
| 102 | case TC_ACT_QUEUED: |
| 103 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; |
| 104 | case TC_ACT_SHOT: |
| 105 | return 0; |
| 106 | } |
| 107 | #endif |
| 108 | if (TC_H_MIN(res.classid) <= q->flows_cnt) |
| 109 | return TC_H_MIN(res.classid); |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* helper functions : might be changed when/if skb use a standard list_head */ |
| 115 | |
| 116 | /* remove one skb from head of slot queue */ |
| 117 | static inline struct sk_buff *dequeue_head(struct fq_codel_flow *flow) |
| 118 | { |
| 119 | struct sk_buff *skb = flow->head; |
| 120 | |
| 121 | flow->head = skb->next; |
| 122 | skb->next = NULL; |
| 123 | return skb; |
| 124 | } |
| 125 | |
| 126 | /* add skb to flow queue (tail add) */ |
| 127 | static inline void flow_queue_add(struct fq_codel_flow *flow, |
| 128 | struct sk_buff *skb) |
| 129 | { |
| 130 | if (flow->head == NULL) |
| 131 | flow->head = skb; |
| 132 | else |
| 133 | flow->tail->next = skb; |
| 134 | flow->tail = skb; |
| 135 | skb->next = NULL; |
| 136 | } |
| 137 | |
| 138 | static unsigned int fq_codel_drop(struct Qdisc *sch) |
| 139 | { |
| 140 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 141 | struct sk_buff *skb; |
| 142 | unsigned int maxbacklog = 0, idx = 0, i, len; |
| 143 | struct fq_codel_flow *flow; |
| 144 | |
| 145 | /* Queue is full! Find the fat flow and drop packet from it. |
| 146 | * This might sound expensive, but with 1024 flows, we scan |
| 147 | * 4KB of memory, and we dont need to handle a complex tree |
| 148 | * in fast path (packet queue/enqueue) with many cache misses. |
| 149 | */ |
| 150 | for (i = 0; i < q->flows_cnt; i++) { |
| 151 | if (q->backlogs[i] > maxbacklog) { |
| 152 | maxbacklog = q->backlogs[i]; |
| 153 | idx = i; |
| 154 | } |
| 155 | } |
| 156 | flow = &q->flows[idx]; |
| 157 | skb = dequeue_head(flow); |
| 158 | len = qdisc_pkt_len(skb); |
| 159 | q->backlogs[idx] -= len; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 160 | sch->q.qlen--; |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 161 | qdisc_qstats_drop(sch); |
| 162 | qdisc_qstats_backlog_dec(sch, skb); |
WANG Cong | 052cbda | 2015-07-13 12:30:07 -0700 | [diff] [blame] | 163 | kfree_skb(skb); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 164 | flow->dropped++; |
| 165 | return idx; |
| 166 | } |
| 167 | |
WANG Cong | c0afd9c | 2015-07-14 11:21:58 -0700 | [diff] [blame] | 168 | static unsigned int fq_codel_qdisc_drop(struct Qdisc *sch) |
| 169 | { |
| 170 | unsigned int prev_backlog; |
| 171 | |
| 172 | prev_backlog = sch->qstats.backlog; |
| 173 | fq_codel_drop(sch); |
| 174 | return prev_backlog - sch->qstats.backlog; |
| 175 | } |
| 176 | |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 177 | static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch) |
| 178 | { |
| 179 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 180 | unsigned int idx, prev_backlog; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 181 | struct fq_codel_flow *flow; |
| 182 | int uninitialized_var(ret); |
| 183 | |
| 184 | idx = fq_codel_classify(skb, sch, &ret); |
| 185 | if (idx == 0) { |
| 186 | if (ret & __NET_XMIT_BYPASS) |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 187 | qdisc_qstats_drop(sch); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 188 | kfree_skb(skb); |
| 189 | return ret; |
| 190 | } |
| 191 | idx--; |
| 192 | |
| 193 | codel_set_enqueue_time(skb); |
| 194 | flow = &q->flows[idx]; |
| 195 | flow_queue_add(flow, skb); |
| 196 | q->backlogs[idx] += qdisc_pkt_len(skb); |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 197 | qdisc_qstats_backlog_inc(sch, skb); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 198 | |
| 199 | if (list_empty(&flow->flowchain)) { |
| 200 | list_add_tail(&flow->flowchain, &q->new_flows); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 201 | q->new_flow_count++; |
| 202 | flow->deficit = q->quantum; |
| 203 | flow->dropped = 0; |
| 204 | } |
Vijay Subramanian | cd68ddd | 2013-03-28 13:52:00 +0000 | [diff] [blame] | 205 | if (++sch->q.qlen <= sch->limit) |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 206 | return NET_XMIT_SUCCESS; |
| 207 | |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 208 | prev_backlog = sch->qstats.backlog; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 209 | q->drop_overlimit++; |
| 210 | /* Return Congestion Notification only if we dropped a packet |
| 211 | * from this flow. |
| 212 | */ |
| 213 | if (fq_codel_drop(sch) == idx) |
| 214 | return NET_XMIT_CN; |
| 215 | |
| 216 | /* As we dropped a packet, better let upper stack know this */ |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 217 | qdisc_tree_reduce_backlog(sch, 1, prev_backlog - sch->qstats.backlog); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 218 | return NET_XMIT_SUCCESS; |
| 219 | } |
| 220 | |
| 221 | /* This is the specific function called from codel_dequeue() |
| 222 | * to dequeue a packet from queue. Note: backlog is handled in |
| 223 | * codel, we dont need to reduce it here. |
| 224 | */ |
Michal Kazior | 79bdc4c | 2016-04-22 14:15:58 +0200 | [diff] [blame] | 225 | static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx) |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 226 | { |
Michal Kazior | 79bdc4c | 2016-04-22 14:15:58 +0200 | [diff] [blame] | 227 | struct Qdisc *sch = ctx; |
Eric Dumazet | 865ec55 | 2012-05-16 04:39:09 +0000 | [diff] [blame] | 228 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 229 | struct fq_codel_flow *flow; |
| 230 | struct sk_buff *skb = NULL; |
| 231 | |
| 232 | flow = container_of(vars, struct fq_codel_flow, cvars); |
| 233 | if (flow->head) { |
| 234 | skb = dequeue_head(flow); |
Eric Dumazet | 865ec55 | 2012-05-16 04:39:09 +0000 | [diff] [blame] | 235 | q->backlogs[flow - q->flows] -= qdisc_pkt_len(skb); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 236 | sch->q.qlen--; |
Michal Kazior | 79bdc4c | 2016-04-22 14:15:58 +0200 | [diff] [blame] | 237 | sch->qstats.backlog -= qdisc_pkt_len(skb); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 238 | } |
| 239 | return skb; |
| 240 | } |
| 241 | |
Michal Kazior | 79bdc4c | 2016-04-22 14:15:58 +0200 | [diff] [blame] | 242 | static void drop_func(struct sk_buff *skb, void *ctx) |
| 243 | { |
| 244 | struct Qdisc *sch = ctx; |
| 245 | |
| 246 | qdisc_drop(skb, sch); |
| 247 | } |
| 248 | |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 249 | static struct sk_buff *fq_codel_dequeue(struct Qdisc *sch) |
| 250 | { |
| 251 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 252 | struct sk_buff *skb; |
| 253 | struct fq_codel_flow *flow; |
| 254 | struct list_head *head; |
| 255 | u32 prev_drop_count, prev_ecn_mark; |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 256 | unsigned int prev_backlog; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 257 | |
| 258 | begin: |
| 259 | head = &q->new_flows; |
| 260 | if (list_empty(head)) { |
| 261 | head = &q->old_flows; |
| 262 | if (list_empty(head)) |
| 263 | return NULL; |
| 264 | } |
| 265 | flow = list_first_entry(head, struct fq_codel_flow, flowchain); |
| 266 | |
| 267 | if (flow->deficit <= 0) { |
| 268 | flow->deficit += q->quantum; |
| 269 | list_move_tail(&flow->flowchain, &q->old_flows); |
| 270 | goto begin; |
| 271 | } |
| 272 | |
| 273 | prev_drop_count = q->cstats.drop_count; |
| 274 | prev_ecn_mark = q->cstats.ecn_mark; |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 275 | prev_backlog = sch->qstats.backlog; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 276 | |
Michal Kazior | 79bdc4c | 2016-04-22 14:15:58 +0200 | [diff] [blame] | 277 | skb = codel_dequeue(sch, &sch->qstats.backlog, &q->cparams, |
| 278 | &flow->cvars, &q->cstats, qdisc_pkt_len, |
| 279 | codel_get_enqueue_time, drop_func, dequeue_func); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 280 | |
| 281 | flow->dropped += q->cstats.drop_count - prev_drop_count; |
| 282 | flow->dropped += q->cstats.ecn_mark - prev_ecn_mark; |
| 283 | |
| 284 | if (!skb) { |
| 285 | /* force a pass through old_flows to prevent starvation */ |
| 286 | if ((head == &q->new_flows) && !list_empty(&q->old_flows)) |
| 287 | list_move_tail(&flow->flowchain, &q->old_flows); |
| 288 | else |
| 289 | list_del_init(&flow->flowchain); |
| 290 | goto begin; |
| 291 | } |
| 292 | qdisc_bstats_update(sch, skb); |
| 293 | flow->deficit -= qdisc_pkt_len(skb); |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 294 | /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0, |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 295 | * or HTB crashes. Defer it for next round. |
| 296 | */ |
| 297 | if (q->cstats.drop_count && sch->q.qlen) { |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 298 | qdisc_tree_reduce_backlog(sch, q->cstats.drop_count, |
| 299 | q->cstats.drop_len); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 300 | q->cstats.drop_count = 0; |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 301 | q->cstats.drop_len = 0; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 302 | } |
| 303 | return skb; |
| 304 | } |
| 305 | |
| 306 | static void fq_codel_reset(struct Qdisc *sch) |
| 307 | { |
Eric Dumazet | 3d0e0af | 2015-07-31 17:53:39 -0700 | [diff] [blame] | 308 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 309 | int i; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 310 | |
Eric Dumazet | 3d0e0af | 2015-07-31 17:53:39 -0700 | [diff] [blame] | 311 | INIT_LIST_HEAD(&q->new_flows); |
| 312 | INIT_LIST_HEAD(&q->old_flows); |
| 313 | for (i = 0; i < q->flows_cnt; i++) { |
| 314 | struct fq_codel_flow *flow = q->flows + i; |
| 315 | |
| 316 | while (flow->head) { |
| 317 | struct sk_buff *skb = dequeue_head(flow); |
| 318 | |
| 319 | qdisc_qstats_backlog_dec(sch, skb); |
| 320 | kfree_skb(skb); |
| 321 | } |
| 322 | |
| 323 | INIT_LIST_HEAD(&flow->flowchain); |
| 324 | codel_vars_init(&flow->cvars); |
| 325 | } |
| 326 | memset(q->backlogs, 0, q->flows_cnt * sizeof(u32)); |
| 327 | sch->q.qlen = 0; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static const struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = { |
| 331 | [TCA_FQ_CODEL_TARGET] = { .type = NLA_U32 }, |
| 332 | [TCA_FQ_CODEL_LIMIT] = { .type = NLA_U32 }, |
| 333 | [TCA_FQ_CODEL_INTERVAL] = { .type = NLA_U32 }, |
| 334 | [TCA_FQ_CODEL_ECN] = { .type = NLA_U32 }, |
| 335 | [TCA_FQ_CODEL_FLOWS] = { .type = NLA_U32 }, |
| 336 | [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 }, |
Eric Dumazet | 80ba92f | 2015-05-08 15:05:12 -0700 | [diff] [blame] | 337 | [TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 }, |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt) |
| 341 | { |
| 342 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 343 | struct nlattr *tb[TCA_FQ_CODEL_MAX + 1]; |
| 344 | int err; |
| 345 | |
| 346 | if (!opt) |
| 347 | return -EINVAL; |
| 348 | |
| 349 | err = nla_parse_nested(tb, TCA_FQ_CODEL_MAX, opt, fq_codel_policy); |
| 350 | if (err < 0) |
| 351 | return err; |
| 352 | if (tb[TCA_FQ_CODEL_FLOWS]) { |
| 353 | if (q->flows) |
| 354 | return -EINVAL; |
| 355 | q->flows_cnt = nla_get_u32(tb[TCA_FQ_CODEL_FLOWS]); |
| 356 | if (!q->flows_cnt || |
| 357 | q->flows_cnt > 65536) |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | sch_tree_lock(sch); |
| 361 | |
| 362 | if (tb[TCA_FQ_CODEL_TARGET]) { |
| 363 | u64 target = nla_get_u32(tb[TCA_FQ_CODEL_TARGET]); |
| 364 | |
| 365 | q->cparams.target = (target * NSEC_PER_USEC) >> CODEL_SHIFT; |
| 366 | } |
| 367 | |
Eric Dumazet | 80ba92f | 2015-05-08 15:05:12 -0700 | [diff] [blame] | 368 | if (tb[TCA_FQ_CODEL_CE_THRESHOLD]) { |
| 369 | u64 val = nla_get_u32(tb[TCA_FQ_CODEL_CE_THRESHOLD]); |
| 370 | |
| 371 | q->cparams.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT; |
| 372 | } |
| 373 | |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 374 | if (tb[TCA_FQ_CODEL_INTERVAL]) { |
| 375 | u64 interval = nla_get_u32(tb[TCA_FQ_CODEL_INTERVAL]); |
| 376 | |
| 377 | q->cparams.interval = (interval * NSEC_PER_USEC) >> CODEL_SHIFT; |
| 378 | } |
| 379 | |
| 380 | if (tb[TCA_FQ_CODEL_LIMIT]) |
| 381 | sch->limit = nla_get_u32(tb[TCA_FQ_CODEL_LIMIT]); |
| 382 | |
| 383 | if (tb[TCA_FQ_CODEL_ECN]) |
| 384 | q->cparams.ecn = !!nla_get_u32(tb[TCA_FQ_CODEL_ECN]); |
| 385 | |
| 386 | if (tb[TCA_FQ_CODEL_QUANTUM]) |
| 387 | q->quantum = max(256U, nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM])); |
| 388 | |
| 389 | while (sch->q.qlen > sch->limit) { |
| 390 | struct sk_buff *skb = fq_codel_dequeue(sch); |
| 391 | |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 392 | q->cstats.drop_len += qdisc_pkt_len(skb); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 393 | kfree_skb(skb); |
| 394 | q->cstats.drop_count++; |
| 395 | } |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 396 | qdisc_tree_reduce_backlog(sch, q->cstats.drop_count, q->cstats.drop_len); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 397 | q->cstats.drop_count = 0; |
WANG Cong | 2ccccf5 | 2016-02-25 14:55:01 -0800 | [diff] [blame] | 398 | q->cstats.drop_len = 0; |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 399 | |
| 400 | sch_tree_unlock(sch); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static void *fq_codel_zalloc(size_t sz) |
| 405 | { |
| 406 | void *ptr = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN); |
| 407 | |
| 408 | if (!ptr) |
| 409 | ptr = vzalloc(sz); |
| 410 | return ptr; |
| 411 | } |
| 412 | |
| 413 | static void fq_codel_free(void *addr) |
| 414 | { |
WANG Cong | 4cb2897 | 2014-06-02 15:55:22 -0700 | [diff] [blame] | 415 | kvfree(addr); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static void fq_codel_destroy(struct Qdisc *sch) |
| 419 | { |
| 420 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 421 | |
| 422 | tcf_destroy_chain(&q->filter_list); |
| 423 | fq_codel_free(q->backlogs); |
| 424 | fq_codel_free(q->flows); |
| 425 | } |
| 426 | |
| 427 | static int fq_codel_init(struct Qdisc *sch, struct nlattr *opt) |
| 428 | { |
| 429 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 430 | int i; |
| 431 | |
| 432 | sch->limit = 10*1024; |
| 433 | q->flows_cnt = 1024; |
| 434 | q->quantum = psched_mtu(qdisc_dev(sch)); |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 435 | q->perturbation = prandom_u32(); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 436 | INIT_LIST_HEAD(&q->new_flows); |
| 437 | INIT_LIST_HEAD(&q->old_flows); |
Michal Kazior | 79bdc4c | 2016-04-22 14:15:58 +0200 | [diff] [blame] | 438 | codel_params_init(&q->cparams); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 439 | codel_stats_init(&q->cstats); |
| 440 | q->cparams.ecn = true; |
Michal Kazior | 79bdc4c | 2016-04-22 14:15:58 +0200 | [diff] [blame] | 441 | q->cparams.mtu = psched_mtu(qdisc_dev(sch)); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 442 | |
| 443 | if (opt) { |
| 444 | int err = fq_codel_change(sch, opt); |
| 445 | if (err) |
| 446 | return err; |
| 447 | } |
| 448 | |
| 449 | if (!q->flows) { |
| 450 | q->flows = fq_codel_zalloc(q->flows_cnt * |
| 451 | sizeof(struct fq_codel_flow)); |
| 452 | if (!q->flows) |
| 453 | return -ENOMEM; |
| 454 | q->backlogs = fq_codel_zalloc(q->flows_cnt * sizeof(u32)); |
| 455 | if (!q->backlogs) { |
| 456 | fq_codel_free(q->flows); |
| 457 | return -ENOMEM; |
| 458 | } |
| 459 | for (i = 0; i < q->flows_cnt; i++) { |
| 460 | struct fq_codel_flow *flow = q->flows + i; |
| 461 | |
| 462 | INIT_LIST_HEAD(&flow->flowchain); |
Eric Dumazet | b379135 | 2012-09-01 03:19:57 +0000 | [diff] [blame] | 463 | codel_vars_init(&flow->cvars); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | if (sch->limit >= 1) |
| 467 | sch->flags |= TCQ_F_CAN_BYPASS; |
| 468 | else |
| 469 | sch->flags &= ~TCQ_F_CAN_BYPASS; |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static int fq_codel_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 474 | { |
| 475 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 476 | struct nlattr *opts; |
| 477 | |
| 478 | opts = nla_nest_start(skb, TCA_OPTIONS); |
| 479 | if (opts == NULL) |
| 480 | goto nla_put_failure; |
| 481 | |
| 482 | if (nla_put_u32(skb, TCA_FQ_CODEL_TARGET, |
| 483 | codel_time_to_us(q->cparams.target)) || |
| 484 | nla_put_u32(skb, TCA_FQ_CODEL_LIMIT, |
| 485 | sch->limit) || |
| 486 | nla_put_u32(skb, TCA_FQ_CODEL_INTERVAL, |
| 487 | codel_time_to_us(q->cparams.interval)) || |
| 488 | nla_put_u32(skb, TCA_FQ_CODEL_ECN, |
| 489 | q->cparams.ecn) || |
| 490 | nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM, |
| 491 | q->quantum) || |
| 492 | nla_put_u32(skb, TCA_FQ_CODEL_FLOWS, |
| 493 | q->flows_cnt)) |
| 494 | goto nla_put_failure; |
| 495 | |
Eric Dumazet | 80ba92f | 2015-05-08 15:05:12 -0700 | [diff] [blame] | 496 | if (q->cparams.ce_threshold != CODEL_DISABLED_THRESHOLD && |
| 497 | nla_put_u32(skb, TCA_FQ_CODEL_CE_THRESHOLD, |
| 498 | codel_time_to_us(q->cparams.ce_threshold))) |
| 499 | goto nla_put_failure; |
| 500 | |
Yang Yingliang | d59b7d8 | 2014-03-12 10:20:32 +0800 | [diff] [blame] | 501 | return nla_nest_end(skb, opts); |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 502 | |
| 503 | nla_put_failure: |
| 504 | return -1; |
| 505 | } |
| 506 | |
| 507 | static int fq_codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d) |
| 508 | { |
| 509 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 510 | struct tc_fq_codel_xstats st = { |
| 511 | .type = TCA_FQ_CODEL_XSTATS_QDISC, |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 512 | }; |
| 513 | struct list_head *pos; |
| 514 | |
Sasha Levin | 669d67b | 2012-05-14 11:57:06 +0000 | [diff] [blame] | 515 | st.qdisc_stats.maxpacket = q->cstats.maxpacket; |
| 516 | st.qdisc_stats.drop_overlimit = q->drop_overlimit; |
| 517 | st.qdisc_stats.ecn_mark = q->cstats.ecn_mark; |
| 518 | st.qdisc_stats.new_flow_count = q->new_flow_count; |
Eric Dumazet | 80ba92f | 2015-05-08 15:05:12 -0700 | [diff] [blame] | 519 | st.qdisc_stats.ce_mark = q->cstats.ce_mark; |
Sasha Levin | 669d67b | 2012-05-14 11:57:06 +0000 | [diff] [blame] | 520 | |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 521 | list_for_each(pos, &q->new_flows) |
| 522 | st.qdisc_stats.new_flows_len++; |
| 523 | |
| 524 | list_for_each(pos, &q->old_flows) |
| 525 | st.qdisc_stats.old_flows_len++; |
| 526 | |
| 527 | return gnet_stats_copy_app(d, &st, sizeof(st)); |
| 528 | } |
| 529 | |
| 530 | static struct Qdisc *fq_codel_leaf(struct Qdisc *sch, unsigned long arg) |
| 531 | { |
| 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | static unsigned long fq_codel_get(struct Qdisc *sch, u32 classid) |
| 536 | { |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static unsigned long fq_codel_bind(struct Qdisc *sch, unsigned long parent, |
| 541 | u32 classid) |
| 542 | { |
| 543 | /* we cannot bypass queue discipline anymore */ |
| 544 | sch->flags &= ~TCQ_F_CAN_BYPASS; |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static void fq_codel_put(struct Qdisc *q, unsigned long cl) |
| 549 | { |
| 550 | } |
| 551 | |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 552 | static struct tcf_proto __rcu **fq_codel_find_tcf(struct Qdisc *sch, |
| 553 | unsigned long cl) |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 554 | { |
| 555 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 556 | |
| 557 | if (cl) |
| 558 | return NULL; |
| 559 | return &q->filter_list; |
| 560 | } |
| 561 | |
| 562 | static int fq_codel_dump_class(struct Qdisc *sch, unsigned long cl, |
| 563 | struct sk_buff *skb, struct tcmsg *tcm) |
| 564 | { |
| 565 | tcm->tcm_handle |= TC_H_MIN(cl); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int fq_codel_dump_class_stats(struct Qdisc *sch, unsigned long cl, |
| 570 | struct gnet_dump *d) |
| 571 | { |
| 572 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 573 | u32 idx = cl - 1; |
| 574 | struct gnet_stats_queue qs = { 0 }; |
| 575 | struct tc_fq_codel_xstats xstats; |
| 576 | |
| 577 | if (idx < q->flows_cnt) { |
| 578 | const struct fq_codel_flow *flow = &q->flows[idx]; |
| 579 | const struct sk_buff *skb = flow->head; |
| 580 | |
| 581 | memset(&xstats, 0, sizeof(xstats)); |
| 582 | xstats.type = TCA_FQ_CODEL_XSTATS_CLASS; |
| 583 | xstats.class_stats.deficit = flow->deficit; |
| 584 | xstats.class_stats.ldelay = |
| 585 | codel_time_to_us(flow->cvars.ldelay); |
| 586 | xstats.class_stats.count = flow->cvars.count; |
| 587 | xstats.class_stats.lastcount = flow->cvars.lastcount; |
| 588 | xstats.class_stats.dropping = flow->cvars.dropping; |
| 589 | if (flow->cvars.dropping) { |
| 590 | codel_tdiff_t delta = flow->cvars.drop_next - |
| 591 | codel_get_time(); |
| 592 | |
| 593 | xstats.class_stats.drop_next = (delta >= 0) ? |
| 594 | codel_time_to_us(delta) : |
| 595 | -codel_time_to_us(-delta); |
| 596 | } |
| 597 | while (skb) { |
| 598 | qs.qlen++; |
| 599 | skb = skb->next; |
| 600 | } |
| 601 | qs.backlog = q->backlogs[idx]; |
| 602 | qs.drops = flow->dropped; |
| 603 | } |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 604 | if (gnet_stats_copy_queue(d, NULL, &qs, 0) < 0) |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 605 | return -1; |
| 606 | if (idx < q->flows_cnt) |
| 607 | return gnet_stats_copy_app(d, &xstats, sizeof(xstats)); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static void fq_codel_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 612 | { |
| 613 | struct fq_codel_sched_data *q = qdisc_priv(sch); |
| 614 | unsigned int i; |
| 615 | |
| 616 | if (arg->stop) |
| 617 | return; |
| 618 | |
| 619 | for (i = 0; i < q->flows_cnt; i++) { |
| 620 | if (list_empty(&q->flows[i].flowchain) || |
| 621 | arg->count < arg->skip) { |
| 622 | arg->count++; |
| 623 | continue; |
| 624 | } |
| 625 | if (arg->fn(sch, i + 1, arg) < 0) { |
| 626 | arg->stop = 1; |
| 627 | break; |
| 628 | } |
| 629 | arg->count++; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | static const struct Qdisc_class_ops fq_codel_class_ops = { |
| 634 | .leaf = fq_codel_leaf, |
| 635 | .get = fq_codel_get, |
| 636 | .put = fq_codel_put, |
| 637 | .tcf_chain = fq_codel_find_tcf, |
| 638 | .bind_tcf = fq_codel_bind, |
| 639 | .unbind_tcf = fq_codel_put, |
| 640 | .dump = fq_codel_dump_class, |
| 641 | .dump_stats = fq_codel_dump_class_stats, |
| 642 | .walk = fq_codel_walk, |
| 643 | }; |
| 644 | |
| 645 | static struct Qdisc_ops fq_codel_qdisc_ops __read_mostly = { |
| 646 | .cl_ops = &fq_codel_class_ops, |
| 647 | .id = "fq_codel", |
| 648 | .priv_size = sizeof(struct fq_codel_sched_data), |
| 649 | .enqueue = fq_codel_enqueue, |
| 650 | .dequeue = fq_codel_dequeue, |
| 651 | .peek = qdisc_peek_dequeued, |
WANG Cong | c0afd9c | 2015-07-14 11:21:58 -0700 | [diff] [blame] | 652 | .drop = fq_codel_qdisc_drop, |
Eric Dumazet | 4b549a2 | 2012-05-11 09:30:50 +0000 | [diff] [blame] | 653 | .init = fq_codel_init, |
| 654 | .reset = fq_codel_reset, |
| 655 | .destroy = fq_codel_destroy, |
| 656 | .change = fq_codel_change, |
| 657 | .dump = fq_codel_dump, |
| 658 | .dump_stats = fq_codel_dump_stats, |
| 659 | .owner = THIS_MODULE, |
| 660 | }; |
| 661 | |
| 662 | static int __init fq_codel_module_init(void) |
| 663 | { |
| 664 | return register_qdisc(&fq_codel_qdisc_ops); |
| 665 | } |
| 666 | |
| 667 | static void __exit fq_codel_module_exit(void) |
| 668 | { |
| 669 | unregister_qdisc(&fq_codel_qdisc_ops); |
| 670 | } |
| 671 | |
| 672 | module_init(fq_codel_module_init) |
| 673 | module_exit(fq_codel_module_exit) |
| 674 | MODULE_AUTHOR("Eric Dumazet"); |
| 675 | MODULE_LICENSE("GPL"); |