]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_fq.c
tc: replace magic constant 16 with #define
[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{
53 fprintf(stderr, "Usage: ... fq [ limit PACKETS ] [ flow_limit PACKETS ]\n");
54 fprintf(stderr, " [ quantum BYTES ] [ initial_quantum BYTES ]\n");
55 fprintf(stderr, " [ maxrate RATE ] [ buckets NUMBER ]\n");
565af7b8 56 fprintf(stderr, " [ [no]pacing ] [ refill_delay TIME ]\n");
ff28b751 57 fprintf(stderr, " [ low_rate_threshold RATE ]\n");
8d5bd8c3 58 fprintf(stderr, " [ orphan_mask MASK]\n");
bc113e46
ED
59}
60
61static unsigned int ilog2(unsigned int val)
62{
63 unsigned int res = 0;
64
65 val--;
66 while (val) {
67 res++;
68 val >>= 1;
69 }
70 return res;
71}
72
73static int fq_parse_opt(struct qdisc_util *qu, int argc, char **argv,
74 struct nlmsghdr *n)
75{
aeb199d5
YY
76 unsigned int plimit;
77 unsigned int flow_plimit;
78 unsigned int quantum;
79 unsigned int initial_quantum;
bc113e46 80 unsigned int buckets = 0;
aeb199d5 81 unsigned int maxrate;
ff28b751 82 unsigned int low_rate_threshold;
aeb199d5 83 unsigned int defrate;
565af7b8 84 unsigned int refill_delay;
8d5bd8c3 85 unsigned int orphan_mask;
aeb199d5
YY
86 bool set_plimit = false;
87 bool set_flow_plimit = false;
88 bool set_quantum = false;
89 bool set_initial_quantum = false;
90 bool set_maxrate = false;
91 bool set_defrate = false;
565af7b8 92 bool set_refill_delay = false;
8d5bd8c3 93 bool set_orphan_mask = false;
ff28b751 94 bool set_low_rate_threshold = false;
bc113e46
ED
95 int pacing = -1;
96 struct rtattr *tail;
97
98 while (argc > 0) {
99 if (strcmp(*argv, "limit") == 0) {
100 NEXT_ARG();
101 if (get_unsigned(&plimit, *argv, 0)) {
102 fprintf(stderr, "Illegal \"limit\"\n");
103 return -1;
104 }
aeb199d5 105 set_plimit = true;
bc113e46
ED
106 } else if (strcmp(*argv, "flow_limit") == 0) {
107 NEXT_ARG();
108 if (get_unsigned(&flow_plimit, *argv, 0)) {
109 fprintf(stderr, "Illegal \"flow_limit\"\n");
110 return -1;
111 }
aeb199d5 112 set_flow_plimit = true;
bc113e46
ED
113 } else if (strcmp(*argv, "buckets") == 0) {
114 NEXT_ARG();
115 if (get_unsigned(&buckets, *argv, 0)) {
116 fprintf(stderr, "Illegal \"buckets\"\n");
117 return -1;
118 }
119 } else if (strcmp(*argv, "maxrate") == 0) {
120 NEXT_ARG();
121 if (get_rate(&maxrate, *argv)) {
122 fprintf(stderr, "Illegal \"maxrate\"\n");
123 return -1;
124 }
aeb199d5 125 set_maxrate = true;
ff28b751
ED
126 } else if (strcmp(*argv, "low_rate_threshold") == 0) {
127 NEXT_ARG();
128 if (get_rate(&low_rate_threshold, *argv)) {
129 fprintf(stderr, "Illegal \"low_rate_threshold\"\n");
130 return -1;
131 }
132 set_low_rate_threshold = true;
bc113e46
ED
133 } else if (strcmp(*argv, "defrate") == 0) {
134 NEXT_ARG();
135 if (get_rate(&defrate, *argv)) {
136 fprintf(stderr, "Illegal \"defrate\"\n");
137 return -1;
138 }
aeb199d5 139 set_defrate = true;
bc113e46
ED
140 } else if (strcmp(*argv, "quantum") == 0) {
141 NEXT_ARG();
142 if (get_unsigned(&quantum, *argv, 0)) {
143 fprintf(stderr, "Illegal \"quantum\"\n");
144 return -1;
145 }
aeb199d5 146 set_quantum = true;
bc113e46
ED
147 } else if (strcmp(*argv, "initial_quantum") == 0) {
148 NEXT_ARG();
149 if (get_unsigned(&initial_quantum, *argv, 0)) {
150 fprintf(stderr, "Illegal \"initial_quantum\"\n");
151 return -1;
152 }
aeb199d5 153 set_initial_quantum = true;
8d5bd8c3
ED
154 } else if (strcmp(*argv, "orphan_mask") == 0) {
155 NEXT_ARG();
156 if (get_unsigned(&orphan_mask, *argv, 0)) {
157 fprintf(stderr, "Illegal \"initial_quantum\"\n");
158 return -1;
159 }
160 set_orphan_mask = true;
565af7b8
PS
161 } else if (strcmp(*argv, "refill_delay") == 0) {
162 NEXT_ARG();
163 if (get_time(&refill_delay, *argv)) {
164 fprintf(stderr, "Illegal \"refill_delay\"\n");
165 return -1;
166 }
167 set_refill_delay = true;
bc113e46
ED
168 } else if (strcmp(*argv, "pacing") == 0) {
169 pacing = 1;
170 } else if (strcmp(*argv, "nopacing") == 0) {
171 pacing = 0;
172 } else if (strcmp(*argv, "help") == 0) {
173 explain();
174 return -1;
175 } else {
176 fprintf(stderr, "What is \"%s\"?\n", *argv);
177 explain();
178 return -1;
179 }
180 argc--; argv++;
181 }
182
183 tail = NLMSG_TAIL(n);
184 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
185 if (buckets) {
186 unsigned int log = ilog2(buckets);
187
188 addattr_l(n, 1024, TCA_FQ_BUCKETS_LOG,
189 &log, sizeof(log));
190 }
aeb199d5 191 if (set_plimit)
bc113e46
ED
192 addattr_l(n, 1024, TCA_FQ_PLIMIT,
193 &plimit, sizeof(plimit));
aeb199d5 194 if (set_flow_plimit)
bc113e46
ED
195 addattr_l(n, 1024, TCA_FQ_FLOW_PLIMIT,
196 &flow_plimit, sizeof(flow_plimit));
aeb199d5 197 if (set_quantum)
bc113e46 198 addattr_l(n, 1024, TCA_FQ_QUANTUM, &quantum, sizeof(quantum));
aeb199d5 199 if (set_initial_quantum)
bc113e46
ED
200 addattr_l(n, 1024, TCA_FQ_INITIAL_QUANTUM,
201 &initial_quantum, sizeof(initial_quantum));
202 if (pacing != -1)
203 addattr_l(n, 1024, TCA_FQ_RATE_ENABLE,
204 &pacing, sizeof(pacing));
aeb199d5 205 if (set_maxrate)
bc113e46
ED
206 addattr_l(n, 1024, TCA_FQ_FLOW_MAX_RATE,
207 &maxrate, sizeof(maxrate));
ff28b751
ED
208 if (set_low_rate_threshold)
209 addattr_l(n, 1024, TCA_FQ_LOW_RATE_THRESHOLD,
210 &low_rate_threshold, sizeof(low_rate_threshold));
aeb199d5 211 if (set_defrate)
bc113e46
ED
212 addattr_l(n, 1024, TCA_FQ_FLOW_DEFAULT_RATE,
213 &defrate, sizeof(defrate));
565af7b8
PS
214 if (set_refill_delay)
215 addattr_l(n, 1024, TCA_FQ_FLOW_REFILL_DELAY,
8fe98398 216 &refill_delay, sizeof(refill_delay));
8d5bd8c3
ED
217 if (set_orphan_mask)
218 addattr_l(n, 1024, TCA_FQ_ORPHAN_MASK,
8fe98398 219 &orphan_mask, sizeof(refill_delay));
bc113e46
ED
220 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
221 return 0;
222}
223
224static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
225{
226 struct rtattr *tb[TCA_FQ_MAX + 1];
227 unsigned int plimit, flow_plimit;
228 unsigned int buckets_log;
229 int pacing;
230 unsigned int rate, quantum;
565af7b8 231 unsigned int refill_delay;
8d5bd8c3 232 unsigned int orphan_mask;
32a121cb 233
bc113e46
ED
234 SPRINT_BUF(b1);
235
236 if (opt == NULL)
237 return 0;
238
239 parse_rtattr_nested(tb, TCA_FQ_MAX, opt);
240
241 if (tb[TCA_FQ_PLIMIT] &&
242 RTA_PAYLOAD(tb[TCA_FQ_PLIMIT]) >= sizeof(__u32)) {
243 plimit = rta_getattr_u32(tb[TCA_FQ_PLIMIT]);
244 fprintf(f, "limit %up ", plimit);
245 }
246 if (tb[TCA_FQ_FLOW_PLIMIT] &&
247 RTA_PAYLOAD(tb[TCA_FQ_FLOW_PLIMIT]) >= sizeof(__u32)) {
248 flow_plimit = rta_getattr_u32(tb[TCA_FQ_FLOW_PLIMIT]);
249 fprintf(f, "flow_limit %up ", flow_plimit);
250 }
251 if (tb[TCA_FQ_BUCKETS_LOG] &&
252 RTA_PAYLOAD(tb[TCA_FQ_BUCKETS_LOG]) >= sizeof(__u32)) {
253 buckets_log = rta_getattr_u32(tb[TCA_FQ_BUCKETS_LOG]);
254 fprintf(f, "buckets %u ", 1U << buckets_log);
255 }
8d5bd8c3
ED
256 if (tb[TCA_FQ_ORPHAN_MASK] &&
257 RTA_PAYLOAD(tb[TCA_FQ_ORPHAN_MASK]) >= sizeof(__u32)) {
258 orphan_mask = rta_getattr_u32(tb[TCA_FQ_ORPHAN_MASK]);
259 fprintf(f, "orphan_mask %u ", orphan_mask);
260 }
bc113e46
ED
261 if (tb[TCA_FQ_RATE_ENABLE] &&
262 RTA_PAYLOAD(tb[TCA_FQ_RATE_ENABLE]) >= sizeof(int)) {
263 pacing = rta_getattr_u32(tb[TCA_FQ_RATE_ENABLE]);
264 if (pacing == 0)
265 fprintf(f, "nopacing ");
266 }
267 if (tb[TCA_FQ_QUANTUM] &&
268 RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) {
269 quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]);
270 fprintf(f, "quantum %u ", quantum);
271 }
272 if (tb[TCA_FQ_INITIAL_QUANTUM] &&
273 RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) {
274 quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
275 fprintf(f, "initial_quantum %u ", quantum);
276 }
277 if (tb[TCA_FQ_FLOW_MAX_RATE] &&
278 RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) {
279 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
280
281 if (rate != ~0U)
282 fprintf(f, "maxrate %s ", sprint_rate(rate, b1));
283 }
284 if (tb[TCA_FQ_FLOW_DEFAULT_RATE] &&
285 RTA_PAYLOAD(tb[TCA_FQ_FLOW_DEFAULT_RATE]) >= sizeof(__u32)) {
286 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]);
287
288 if (rate != 0)
289 fprintf(f, "defrate %s ", sprint_rate(rate, b1));
290 }
ff28b751
ED
291 if (tb[TCA_FQ_LOW_RATE_THRESHOLD] &&
292 RTA_PAYLOAD(tb[TCA_FQ_LOW_RATE_THRESHOLD]) >= sizeof(__u32)) {
293 rate = rta_getattr_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]);
294
295 if (rate != 0)
296 fprintf(f, "low_rate_threshold %s ", sprint_rate(rate, b1));
297 }
565af7b8
PS
298 if (tb[TCA_FQ_FLOW_REFILL_DELAY] &&
299 RTA_PAYLOAD(tb[TCA_FQ_FLOW_REFILL_DELAY]) >= sizeof(__u32)) {
300 refill_delay = rta_getattr_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]);
301 fprintf(f, "refill_delay %s ", sprint_time(refill_delay, b1));
302 }
bc113e46
ED
303
304 return 0;
305}
306
307static int fq_print_xstats(struct qdisc_util *qu, FILE *f,
308 struct rtattr *xstats)
309{
310 struct tc_fq_qd_stats *st;
311
312 if (xstats == NULL)
313 return 0;
314
315 if (RTA_PAYLOAD(xstats) < sizeof(*st))
316 return -1;
317
318 st = RTA_DATA(xstats);
319
320 fprintf(f, " %u flows (%u inactive, %u throttled)",
321 st->flows, st->inactive_flows, st->throttled_flows);
322
323 if (st->time_next_delayed_flow > 0)
324 fprintf(f, ", next packet delay %llu ns", st->time_next_delayed_flow);
325
326 fprintf(f, "\n %llu gc, %llu highprio",
327 st->gc_flows, st->highprio_packets);
328
329 if (st->tcp_retrans)
330 fprintf(f, ", %llu retrans", st->tcp_retrans);
331
332 fprintf(f, ", %llu throttled", st->throttled);
333
39f8caeb
ED
334 if (st->unthrottle_latency_ns)
335 fprintf(f, ", %u ns latency", st->unthrottle_latency_ns);
336
bc113e46
ED
337 if (st->flows_plimit)
338 fprintf(f, ", %llu flows_plimit", st->flows_plimit);
339
340 if (st->pkts_too_long || st->allocation_errors)
341 fprintf(f, "\n %llu too long pkts, %llu alloc errors\n",
342 st->pkts_too_long, st->allocation_errors);
343
344 return 0;
345}
346
347struct qdisc_util fq_qdisc_util = {
348 .id = "fq",
349 .parse_qopt = fq_parse_opt,
350 .print_qopt = fq_print_opt,
351 .print_xstats = fq_print_xstats,
352};