1 From: Eric Dumazet <edumazet@google.com>
2 Date: Fri, 6 May 2016 08:55:12 -0700
3 Subject: [PATCH] fq_codel: add memory limitation per queue
5 Content-Type: text/plain; charset=UTF-8
6 Content-Transfer-Encoding: 8bit
8 On small embedded routers, one wants to control maximal amount of
9 memory used by fq_codel, instead of controlling number of packets or
10 bytes, since GRO/TSO make these not practical.
12 Assuming skb->truesize is accurate, we have to keep track of
13 skb->truesize sum for skbs in queue.
15 This patch adds a new TCA_FQ_CODEL_MEMORY_LIMIT attribute.
17 I chose a default value of 32 MBytes, which looks reasonable even
18 for heavy duty usages. (Prior fq_codel users should not be hurt
19 when they upgrade their kernels)
21 Two fields are added to tc_fq_codel_qd_stats to report :
22 - Current memory usage
23 - Number of drops caused by memory limits
25 # tc qd replace dev eth1 root est 1sec 4sec fq_codel memory_limit 4M
27 # tc -s -d qd sh dev eth1
28 qdisc fq_codel 8008: root refcnt 257 limit 10240p flows 1024
29 quantum 1514 target 5.0ms interval 100.0ms memory_limit 4Mb ecn
30 Sent 2083566791363 bytes 1376214889 pkt (dropped 4994406, overlimits 0
32 rate 9841Mbit 812549pps backlog 3906120b 376p requeues 21705223
33 maxpacket 68130 drop_overlimit 4994406 new_flow_count 28855414
34 ecn_mark 0 memory_used 4190048 drop_overmemory 4994406
35 new_flows_len 1 old_flows_len 177
37 Signed-off-by: Eric Dumazet <edumazet@google.com>
38 Cc: Jesper Dangaard Brouer <brouer@redhat.com>
39 Cc: Dave Täht <dave.taht@gmail.com>
40 Cc: Sebastian Möller <moeller0@gmx.de>
41 Signed-off-by: David S. Miller <davem@davemloft.net>
44 --- a/include/uapi/linux/pkt_sched.h
45 +++ b/include/uapi/linux/pkt_sched.h
46 @@ -712,6 +712,7 @@ enum {
48 TCA_FQ_CODEL_CE_THRESHOLD,
49 TCA_FQ_CODEL_DROP_BATCH_SIZE,
50 + TCA_FQ_CODEL_MEMORY_LIMIT,
54 @@ -736,6 +737,8 @@ struct tc_fq_codel_qd_stats {
55 __u32 new_flows_len; /* count of flows in new list */
56 __u32 old_flows_len; /* count of flows in old list */
57 __u32 ce_mark; /* packets above ce_threshold */
58 + __u32 memory_usage; /* in bytes */
59 + __u32 drop_overmemory;
62 struct tc_fq_codel_cl_stats {
63 --- a/net/sched/sch_fq_codel.c
64 +++ b/net/sched/sch_fq_codel.c
65 @@ -58,8 +58,11 @@ struct fq_codel_sched_data {
66 u32 perturbation; /* hash perturbation */
67 u32 quantum; /* psched_mtu(qdisc_dev(sch)); */
70 struct codel_params cparams;
71 struct codel_stats cstats;
73 + u32 drop_overmemory;
77 @@ -141,6 +144,7 @@ static unsigned int fq_codel_drop(struct
78 unsigned int maxbacklog = 0, idx = 0, i, len;
79 struct fq_codel_flow *flow;
80 unsigned int threshold;
81 + unsigned int mem = 0;
83 /* Queue is full! Find the fat flow and drop packet(s) from it.
84 * This might sound expensive, but with 1024 flows, we scan
85 @@ -165,11 +169,13 @@ static unsigned int fq_codel_drop(struct
87 skb = dequeue_head(flow);
88 len += qdisc_pkt_len(skb);
89 + mem += skb->truesize;
91 } while (++i < max_packets && len < threshold);
94 q->backlogs[idx] -= len;
95 + q->memory_usage -= mem;
96 sch->qstats.drops += i;
97 sch->qstats.backlog -= len;
99 @@ -191,6 +197,7 @@ static int fq_codel_enqueue(struct sk_bu
100 unsigned int idx, prev_backlog, prev_qlen;
101 struct fq_codel_flow *flow;
102 int uninitialized_var(ret);
103 + bool memory_limited;
105 idx = fq_codel_classify(skb, sch, &ret);
107 @@ -213,7 +220,9 @@ static int fq_codel_enqueue(struct sk_bu
108 flow->deficit = q->quantum;
111 - if (++sch->q.qlen <= sch->limit)
112 + q->memory_usage += skb->truesize;
113 + memory_limited = q->memory_usage > q->memory_limit;
114 + if (++sch->q.qlen <= sch->limit && !memory_limited)
115 return NET_XMIT_SUCCESS;
117 prev_backlog = sch->qstats.backlog;
118 @@ -227,7 +236,8 @@ static int fq_codel_enqueue(struct sk_bu
119 ret = fq_codel_drop(sch, q->drop_batch_size);
121 q->drop_overlimit += prev_qlen - sch->q.qlen;
123 + if (memory_limited)
124 + q->drop_overmemory += prev_qlen - sch->q.qlen;
125 /* As we dropped packet(s), better let upper stack know this */
126 qdisc_tree_reduce_backlog(sch, prev_qlen - sch->q.qlen,
127 prev_backlog - sch->qstats.backlog);
128 @@ -296,6 +306,7 @@ begin:
129 list_del_init(&flow->flowchain);
132 + q->memory_usage -= skb->truesize;
133 qdisc_bstats_update(sch, skb);
134 flow->deficit -= qdisc_pkt_len(skb);
135 /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
136 @@ -343,6 +354,7 @@ static const struct nla_policy fq_codel_
137 [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
138 [TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 },
139 [TCA_FQ_CODEL_DROP_BATCH_SIZE] = { .type = NLA_U32 },
140 + [TCA_FQ_CODEL_MEMORY_LIMIT] = { .type = NLA_U32 },
143 static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt)
144 @@ -397,7 +409,11 @@ static int fq_codel_change(struct Qdisc
145 if (tb[TCA_FQ_CODEL_DROP_BATCH_SIZE])
146 q->drop_batch_size = min(1U, nla_get_u32(tb[TCA_FQ_CODEL_DROP_BATCH_SIZE]));
148 - while (sch->q.qlen > sch->limit) {
149 + if (tb[TCA_FQ_CODEL_MEMORY_LIMIT])
150 + q->memory_limit = min(1U << 31, nla_get_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT]));
152 + while (sch->q.qlen > sch->limit ||
153 + q->memory_usage > q->memory_limit) {
154 struct sk_buff *skb = fq_codel_dequeue(sch);
156 q->cstats.drop_len += qdisc_pkt_len(skb);
157 @@ -442,6 +458,7 @@ static int fq_codel_init(struct Qdisc *s
159 sch->limit = 10*1024;
161 + q->memory_limit = 32 << 20; /* 32 MBytes */
162 q->drop_batch_size = 64;
163 q->quantum = psched_mtu(qdisc_dev(sch));
164 q->perturbation = prandom_u32();
165 @@ -502,6 +519,8 @@ static int fq_codel_dump(struct Qdisc *s
167 nla_put_u32(skb, TCA_FQ_CODEL_DROP_BATCH_SIZE,
168 q->drop_batch_size) ||
169 + nla_put_u32(skb, TCA_FQ_CODEL_MEMORY_LIMIT,
170 + q->memory_limit) ||
171 nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
173 goto nla_put_failure;
174 @@ -530,6 +549,8 @@ static int fq_codel_dump_stats(struct Qd
175 st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
176 st.qdisc_stats.new_flow_count = q->new_flow_count;
177 st.qdisc_stats.ce_mark = q->cstats.ce_mark;
178 + st.qdisc_stats.memory_usage = q->memory_usage;
179 + st.qdisc_stats.drop_overmemory = q->drop_overmemory;
181 list_for_each(pos, &q->new_flows)
182 st.qdisc_stats.new_flows_len++;