]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_fq.c
tc: util: constrain percentage in 0-100 interval
[mirror_iproute2.git] / tc / q_fq.c
CommitLineData
bc113e46
ED
1/*
2 * Fair Queue
3 *
8d5bd8c3 4 * Copyright (C) 2013-2015 Eric Dumazet <edumazet@google.com>
bc113e46
ED
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * Alternatively, provided that this notice is retained in full, this
19 * software may be distributed under the terms of the GNU General
20 * Public License ("GPL") version 2, in which case the provisions of the
21 * GPL apply INSTEAD OF those given above.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
34 * DAMAGE.
35 *
36 */
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
bc113e46
ED
41#include <fcntl.h>
42#include <sys/socket.h>
43#include <netinet/in.h>
44#include <arpa/inet.h>
45#include <string.h>
aeb199d5 46#include <stdbool.h>
bc113e46
ED
47
48#include "utils.h"
49#include "tc_util.h"
50
51static void explain(void)
52{
8589eb4e
MC
53 fprintf(stderr,
54 "Usage: ... fq [ limit PACKETS ] [ flow_limit PACKETS ]\n"
55 " [ quantum BYTES ] [ initial_quantum BYTES ]\n"
56 " [ maxrate RATE ] [ buckets NUMBER ]\n"
57 " [ [no]pacing ] [ refill_delay TIME ]\n"
58 " [ low_rate_threshold RATE ]\n"
59 " [ orphan_mask MASK]\n"
60 " [ ce_threshold TIME ]\n");
bc113e46
ED
61}
62
63static unsigned int ilog2(unsigned int val)
64{
65 unsigned int res = 0;
66
67 val--;
68 while (val) {
69 res++;
70 val >>= 1;
71 }
72 return res;
73}
74
75static int fq_parse_opt(struct qdisc_util *qu, int argc, char **argv,
927e3cfb 76 struct nlmsghdr *n, const char *dev)
bc113e46 77{
aeb199d5
YY
78 unsigned int plimit;
79 unsigned int flow_plimit;
80 unsigned int quantum;
81 unsigned int initial_quantum;
bc113e46 82 unsigned int buckets = 0;
aeb199d5 83 unsigned int maxrate;
ff28b751 84 unsigned int low_rate_threshold;
aeb199d5 85 unsigned int defrate;
565af7b8 86 unsigned int refill_delay;
8d5bd8c3 87 unsigned int orphan_mask;
55e106c4 88 unsigned int ce_threshold;
aeb199d5
YY
89 bool set_plimit = false;
90 bool set_flow_plimit = false;
91 bool set_quantum = false;
92 bool set_initial_quantum = false;
93 bool set_maxrate = false;
94 bool set_defrate = false;
565af7b8 95 bool set_refill_delay = false;
8d5bd8c3 96 bool set_orphan_mask = false;
ff28b751 97 bool set_low_rate_threshold = false;
55e106c4 98 bool set_ce_threshold = false;
bc113e46
ED
99 int pacing = -1;
100 struct rtattr *tail;
101
102 while (argc > 0) {
103 if (strcmp(*argv, "limit") == 0) {
104 NEXT_ARG();
105 if (get_unsigned(&plimit, *argv, 0)) {
106 fprintf(stderr, "Illegal \"limit\"\n");
107 return -1;
108 }
aeb199d5 109 set_plimit = true;
bc113e46
ED
110 } else if (strcmp(*argv, "flow_limit") == 0) {
111 NEXT_ARG();
112 if (get_unsigned(&flow_plimit, *argv, 0)) {
113 fprintf(stderr, "Illegal \"flow_limit\"\n");
114 return -1;
115 }
aeb199d5 116 set_flow_plimit = true;
bc113e46
ED
117 } else if (strcmp(*argv, "buckets") == 0) {
118 NEXT_ARG();
119 if (get_unsigned(&buckets, *argv, 0)) {
120 fprintf(stderr, "Illegal \"buckets\"\n");
121 return -1;
122 }
123 } else if (strcmp(*argv, "maxrate") == 0) {
124 NEXT_ARG();
927e3cfb
ND
125 if (strchr(*argv, '%')) {
126 if (get_percent_rate(&maxrate, *argv, dev)) {
127 fprintf(stderr, "Illegal \"maxrate\"\n");
128 return -1;
129 }
130 } else if (get_rate(&maxrate, *argv)) {
bc113e46
ED
131 fprintf(stderr, "Illegal \"maxrate\"\n");
132 return -1;
133 }
aeb199d5 134 set_maxrate = true;
ff28b751
ED
135 } else if (strcmp(*argv, "low_rate_threshold") == 0) {
136 NEXT_ARG();
137 if (get_rate(&low_rate_threshold, *argv)) {
138 fprintf(stderr, "Illegal \"low_rate_threshold\"\n");
139 return -1;
140 }
141 set_low_rate_threshold = true;
55e106c4
ED
142 } else if (strcmp(*argv, "ce_threshold") == 0) {
143 NEXT_ARG();
144 if (get_time(&ce_threshold, *argv)) {
145 fprintf(stderr, "Illegal \"ce_threshold\"\n");
146 return -1;
147 }
148 set_ce_threshold = true;
bc113e46
ED
149 } else if (strcmp(*argv, "defrate") == 0) {
150 NEXT_ARG();
927e3cfb
ND
151 if (strchr(*argv, '%')) {
152 if (get_percent_rate(&defrate, *argv, dev)) {
153 fprintf(stderr, "Illegal \"defrate\"\n");
154 return -1;
155 }
156 } else if (get_rate(&defrate, *argv)) {
bc113e46
ED
157 fprintf(stderr, "Illegal \"defrate\"\n");
158 return -1;
159 }
aeb199d5 160 set_defrate = true;
bc113e46
ED
161 } else if (strcmp(*argv, "quantum") == 0) {
162 NEXT_ARG();
163 if (get_unsigned(&quantum, *argv, 0)) {
164 fprintf(stderr, "Illegal \"quantum\"\n");
165 return -1;
166 }
aeb199d5 167 set_quantum = true;
bc113e46
ED
168 } else if (strcmp(*argv, "initial_quantum") == 0) {
169 NEXT_ARG();
170 if (get_unsigned(&initial_quantum, *argv, 0)) {
171 fprintf(stderr, "Illegal \"initial_quantum\"\n");
172 return -1;
173 }
aeb199d5 174 set_initial_quantum = true;
8d5bd8c3
ED
175 } else if (strcmp(*argv, "orphan_mask") == 0) {
176 NEXT_ARG();
177 if (get_unsigned(&orphan_mask, *argv, 0)) {
178 fprintf(stderr, "Illegal \"initial_quantum\"\n");
179 return -1;
180 }
181 set_orphan_mask = true;
565af7b8
PS
182 } else if (strcmp(*argv, "refill_delay") == 0) {
183 NEXT_ARG();
184 if (get_time(&refill_delay, *argv)) {
185 fprintf(stderr, "Illegal \"refill_delay\"\n");
186 return -1;
187 }
188 set_refill_delay = true;
bc113e46
ED
189 } else if (strcmp(*argv, "pacing") == 0) {
190 pacing = 1;
191 } else if (strcmp(*argv, "nopacing") == 0) {
192 pacing = 0;
193 } else if (strcmp(*argv, "help") == 0) {
194 explain();
195 return -1;
196 } else {
197 fprintf(stderr, "What is \"%s\"?\n", *argv);
198 explain();
199 return -1;
200 }
201 argc--; argv++;
202 }
203
c14f9d92 204 tail = addattr_nest(n, 1024, TCA_OPTIONS);
bc113e46
ED
205 if (buckets) {
206 unsigned int log = ilog2(buckets);
207
208 addattr_l(n, 1024, TCA_FQ_BUCKETS_LOG,
209 &log, sizeof(log));
210 }
aeb199d5 211 if (set_plimit)
bc113e46
ED
212 addattr_l(n, 1024, TCA_FQ_PLIMIT,
213 &plimit, sizeof(plimit));
aeb199d5 214 if (set_flow_plimit)
bc113e46
ED
215 addattr_l(n, 1024, TCA_FQ_FLOW_PLIMIT,
216 &flow_plimit, sizeof(flow_plimit));
aeb199d5 217 if (set_quantum)
bc113e46 218 addattr_l(n, 1024, TCA_FQ_QUANTUM, &quantum, sizeof(quantum));
aeb199d5 219 if (set_initial_quantum)
bc113e46
ED
220 addattr_l(n, 1024, TCA_FQ_INITIAL_QUANTUM,
221 &initial_quantum, sizeof(initial_quantum));
222 if (pacing != -1)
223 addattr_l(n, 1024, TCA_FQ_RATE_ENABLE,
224 &pacing, sizeof(pacing));
aeb199d5 225 if (set_maxrate)
bc113e46
ED
226 addattr_l(n, 1024, TCA_FQ_FLOW_MAX_RATE,
227 &maxrate, sizeof(maxrate));
ff28b751
ED
228 if (set_low_rate_threshold)
229 addattr_l(n, 1024, TCA_FQ_LOW_RATE_THRESHOLD,
230 &low_rate_threshold, sizeof(low_rate_threshold));
aeb199d5 231 if (set_defrate)
bc113e46
ED
232 addattr_l(n, 1024, TCA_FQ_FLOW_DEFAULT_RATE,
233 &defrate, sizeof(defrate));
565af7b8
PS
234 if (set_refill_delay)
235 addattr_l(n, 1024, TCA_FQ_FLOW_REFILL_DELAY,
8fe98398 236 &refill_delay, sizeof(refill_delay));
8d5bd8c3
ED
237 if (set_orphan_mask)
238 addattr_l(n, 1024, TCA_FQ_ORPHAN_MASK,
8fe98398 239 &orphan_mask, sizeof(refill_delay));
55e106c4
ED
240 if (set_ce_threshold)
241 addattr_l(n, 1024, TCA_FQ_CE_THRESHOLD,
242 &ce_threshold, sizeof(ce_threshold));
c14f9d92 243 addattr_nest_end(n, tail);
bc113e46
ED
244 return 0;
245}
246
247static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
248{
249 struct rtattr *tb[TCA_FQ_MAX + 1];
250 unsigned int plimit, flow_plimit;
251 unsigned int buckets_log;
252 int pacing;
253 unsigned int rate, quantum;
565af7b8 254 unsigned int refill_delay;
8d5bd8c3 255 unsigned int orphan_mask;
55e106c4 256 unsigned int ce_threshold;
32a121cb 257
bc113e46
ED
258 SPRINT_BUF(b1);
259
260 if (opt == NULL)
261 return 0;
262
263 parse_rtattr_nested(tb, TCA_FQ_MAX, opt);
264
265 if (tb[TCA_FQ_PLIMIT] &&
266 RTA_PAYLOAD(tb[TCA_FQ_PLIMIT]) >= sizeof(__u32)) {
267 plimit = rta_getattr_u32(tb[TCA_FQ_PLIMIT]);
268 fprintf(f, "limit %up ", plimit);
269 }
270 if (tb[TCA_FQ_FLOW_PLIMIT] &&
271 RTA_PAYLOAD(tb[TCA_FQ_FLOW_PLIMIT]) >= sizeof(__u32)) {
272 flow_plimit = rta_getattr_u32(tb[TCA_FQ_FLOW_PLIMIT]);
273 fprintf(f, "flow_limit %up ", flow_plimit);
274 }
275 if (tb[TCA_FQ_BUCKETS_LOG] &&
276 RTA_PAYLOAD(tb[TCA_FQ_BUCKETS_LOG]) >= sizeof(__u32)) {
277 buckets_log = rta_getattr_u32(tb[TCA_FQ_BUCKETS_LOG]);
278 fprintf(f, "buckets %u ", 1U << buckets_log);
279 }
8d5bd8c3
ED
280 if (tb[TCA_FQ_ORPHAN_MASK] &&
281 RTA_PAYLOAD(tb[TCA_FQ_ORPHAN_MASK]) >= sizeof(__u32)) {
282 orphan_mask = rta_getattr_u32(tb[TCA_FQ_ORPHAN_MASK]);
283 fprintf(f, "orphan_mask %u ", orphan_mask);
284 }
bc113e46
ED
285 if (tb[TCA_FQ_RATE_ENABLE] &&
286 RTA_PAYLOAD(tb[TCA_FQ_RATE_ENABLE]) >= sizeof(int)) {
287 pacing = rta_getattr_u32(tb[TCA_FQ_RATE_ENABLE]);
288 if (pacing == 0)
289 fprintf(f, "nopacing ");
290 }
291 if (tb[TCA_FQ_QUANTUM] &&
292 RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) {
293 quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]);
294 fprintf(f, "quantum %u ", quantum);
295 }
296 if (tb[TCA_FQ_INITIAL_QUANTUM] &&
297 RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) {
298 quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
299 fprintf(f, "initial_quantum %u ", quantum);
300 }
301 if (tb[TCA_FQ_FLOW_MAX_RATE] &&
302 RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) {
303 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
304
305 if (rate != ~0U)
306 fprintf(f, "maxrate %s ", sprint_rate(rate, b1));
307 }
308 if (tb[TCA_FQ_FLOW_DEFAULT_RATE] &&
309 RTA_PAYLOAD(tb[TCA_FQ_FLOW_DEFAULT_RATE]) >= sizeof(__u32)) {
310 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]);
311
312 if (rate != 0)
313 fprintf(f, "defrate %s ", sprint_rate(rate, b1));
314 }
ff28b751
ED
315 if (tb[TCA_FQ_LOW_RATE_THRESHOLD] &&
316 RTA_PAYLOAD(tb[TCA_FQ_LOW_RATE_THRESHOLD]) >= sizeof(__u32)) {
317 rate = rta_getattr_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]);
318
319 if (rate != 0)
320 fprintf(f, "low_rate_threshold %s ", sprint_rate(rate, b1));
321 }
565af7b8
PS
322 if (tb[TCA_FQ_FLOW_REFILL_DELAY] &&
323 RTA_PAYLOAD(tb[TCA_FQ_FLOW_REFILL_DELAY]) >= sizeof(__u32)) {
324 refill_delay = rta_getattr_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]);
325 fprintf(f, "refill_delay %s ", sprint_time(refill_delay, b1));
326 }
bc113e46 327
55e106c4
ED
328 if (tb[TCA_FQ_CE_THRESHOLD] &&
329 RTA_PAYLOAD(tb[TCA_FQ_CE_THRESHOLD]) >= sizeof(__u32)) {
330 ce_threshold = rta_getattr_u32(tb[TCA_FQ_CE_THRESHOLD]);
331 if (ce_threshold != ~0U)
332 fprintf(f, "ce_threshold %s ", sprint_time(ce_threshold, b1));
333 }
334
bc113e46
ED
335 return 0;
336}
337
338static int fq_print_xstats(struct qdisc_util *qu, FILE *f,
339 struct rtattr *xstats)
340{
55e106c4 341 struct tc_fq_qd_stats *st, _st;
bc113e46
ED
342
343 if (xstats == NULL)
344 return 0;
345
55e106c4
ED
346 memset(&_st, 0, sizeof(_st));
347 memcpy(&_st, RTA_DATA(xstats), min(RTA_PAYLOAD(xstats), sizeof(*st)));
bc113e46 348
55e106c4 349 st = &_st;
bc113e46
ED
350
351 fprintf(f, " %u flows (%u inactive, %u throttled)",
352 st->flows, st->inactive_flows, st->throttled_flows);
353
354 if (st->time_next_delayed_flow > 0)
355 fprintf(f, ", next packet delay %llu ns", st->time_next_delayed_flow);
356
357 fprintf(f, "\n %llu gc, %llu highprio",
358 st->gc_flows, st->highprio_packets);
359
360 if (st->tcp_retrans)
361 fprintf(f, ", %llu retrans", st->tcp_retrans);
362
363 fprintf(f, ", %llu throttled", st->throttled);
364
39f8caeb
ED
365 if (st->unthrottle_latency_ns)
366 fprintf(f, ", %u ns latency", st->unthrottle_latency_ns);
367
55e106c4
ED
368 if (st->ce_mark)
369 fprintf(f, ", %llu ce_mark", st->ce_mark);
370
bc113e46
ED
371 if (st->flows_plimit)
372 fprintf(f, ", %llu flows_plimit", st->flows_plimit);
373
374 if (st->pkts_too_long || st->allocation_errors)
375 fprintf(f, "\n %llu too long pkts, %llu alloc errors\n",
376 st->pkts_too_long, st->allocation_errors);
377
378 return 0;
379}
380
381struct qdisc_util fq_qdisc_util = {
382 .id = "fq",
383 .parse_qopt = fq_parse_opt,
384 .print_qopt = fq_print_opt,
385 .print_xstats = fq_print_xstats,
386};