blob: 90147a6aa9f69de5d9a3cc01fff733c84beb3d7f [file] [log] [blame]
Eric Dumazetbc113e42013-08-29 19:30:36 -07001/*
2 * Fair Queue
3 *
Eric Dumazet8d5bd8c2015-09-24 04:43:26 -07004 * Copyright (C) 2013-2015 Eric Dumazet <edumazet@google.com>
Eric Dumazetbc113e42013-08-29 19:30:36 -07005 *
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>
41#include <syslog.h>
42#include <fcntl.h>
43#include <sys/socket.h>
44#include <netinet/in.h>
45#include <arpa/inet.h>
46#include <string.h>
Yang Yingliangaeb199d2014-05-29 12:04:34 +080047#include <stdbool.h>
Eric Dumazetbc113e42013-08-29 19:30:36 -070048
49#include "utils.h"
50#include "tc_util.h"
51
52static void explain(void)
53{
54 fprintf(stderr, "Usage: ... fq [ limit PACKETS ] [ flow_limit PACKETS ]\n");
55 fprintf(stderr, " [ quantum BYTES ] [ initial_quantum BYTES ]\n");
56 fprintf(stderr, " [ maxrate RATE ] [ buckets NUMBER ]\n");
Phil Sutter565af7b2015-09-10 16:25:47 +020057 fprintf(stderr, " [ [no]pacing ] [ refill_delay TIME ]\n");
Eric Dumazet8d5bd8c2015-09-24 04:43:26 -070058 fprintf(stderr, " [ orphan_mask MASK]\n");
Eric Dumazetbc113e42013-08-29 19:30:36 -070059}
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{
Yang Yingliangaeb199d2014-05-29 12:04:34 +080076 unsigned int plimit;
77 unsigned int flow_plimit;
78 unsigned int quantum;
79 unsigned int initial_quantum;
Eric Dumazetbc113e42013-08-29 19:30:36 -070080 unsigned int buckets = 0;
Yang Yingliangaeb199d2014-05-29 12:04:34 +080081 unsigned int maxrate;
82 unsigned int defrate;
Phil Sutter565af7b2015-09-10 16:25:47 +020083 unsigned int refill_delay;
Eric Dumazet8d5bd8c2015-09-24 04:43:26 -070084 unsigned int orphan_mask;
Yang Yingliangaeb199d2014-05-29 12:04:34 +080085 bool set_plimit = false;
86 bool set_flow_plimit = false;
87 bool set_quantum = false;
88 bool set_initial_quantum = false;
89 bool set_maxrate = false;
90 bool set_defrate = false;
Phil Sutter565af7b2015-09-10 16:25:47 +020091 bool set_refill_delay = false;
Eric Dumazet8d5bd8c2015-09-24 04:43:26 -070092 bool set_orphan_mask = false;
Eric Dumazetbc113e42013-08-29 19:30:36 -070093 int pacing = -1;
94 struct rtattr *tail;
95
96 while (argc > 0) {
97 if (strcmp(*argv, "limit") == 0) {
98 NEXT_ARG();
99 if (get_unsigned(&plimit, *argv, 0)) {
100 fprintf(stderr, "Illegal \"limit\"\n");
101 return -1;
102 }
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800103 set_plimit = true;
Eric Dumazetbc113e42013-08-29 19:30:36 -0700104 } else if (strcmp(*argv, "flow_limit") == 0) {
105 NEXT_ARG();
106 if (get_unsigned(&flow_plimit, *argv, 0)) {
107 fprintf(stderr, "Illegal \"flow_limit\"\n");
108 return -1;
109 }
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800110 set_flow_plimit = true;
Eric Dumazetbc113e42013-08-29 19:30:36 -0700111 } else if (strcmp(*argv, "buckets") == 0) {
112 NEXT_ARG();
113 if (get_unsigned(&buckets, *argv, 0)) {
114 fprintf(stderr, "Illegal \"buckets\"\n");
115 return -1;
116 }
117 } else if (strcmp(*argv, "maxrate") == 0) {
118 NEXT_ARG();
119 if (get_rate(&maxrate, *argv)) {
120 fprintf(stderr, "Illegal \"maxrate\"\n");
121 return -1;
122 }
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800123 set_maxrate = true;
Eric Dumazetbc113e42013-08-29 19:30:36 -0700124 } else if (strcmp(*argv, "defrate") == 0) {
125 NEXT_ARG();
126 if (get_rate(&defrate, *argv)) {
127 fprintf(stderr, "Illegal \"defrate\"\n");
128 return -1;
129 }
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800130 set_defrate = true;
Eric Dumazetbc113e42013-08-29 19:30:36 -0700131 } else if (strcmp(*argv, "quantum") == 0) {
132 NEXT_ARG();
133 if (get_unsigned(&quantum, *argv, 0)) {
134 fprintf(stderr, "Illegal \"quantum\"\n");
135 return -1;
136 }
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800137 set_quantum = true;
Eric Dumazetbc113e42013-08-29 19:30:36 -0700138 } else if (strcmp(*argv, "initial_quantum") == 0) {
139 NEXT_ARG();
140 if (get_unsigned(&initial_quantum, *argv, 0)) {
141 fprintf(stderr, "Illegal \"initial_quantum\"\n");
142 return -1;
143 }
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800144 set_initial_quantum = true;
Eric Dumazet8d5bd8c2015-09-24 04:43:26 -0700145 } else if (strcmp(*argv, "orphan_mask") == 0) {
146 NEXT_ARG();
147 if (get_unsigned(&orphan_mask, *argv, 0)) {
148 fprintf(stderr, "Illegal \"initial_quantum\"\n");
149 return -1;
150 }
151 set_orphan_mask = true;
Phil Sutter565af7b2015-09-10 16:25:47 +0200152 } else if (strcmp(*argv, "refill_delay") == 0) {
153 NEXT_ARG();
154 if (get_time(&refill_delay, *argv)) {
155 fprintf(stderr, "Illegal \"refill_delay\"\n");
156 return -1;
157 }
158 set_refill_delay = true;
Eric Dumazetbc113e42013-08-29 19:30:36 -0700159 } else if (strcmp(*argv, "pacing") == 0) {
160 pacing = 1;
161 } else if (strcmp(*argv, "nopacing") == 0) {
162 pacing = 0;
163 } else if (strcmp(*argv, "help") == 0) {
164 explain();
165 return -1;
166 } else {
167 fprintf(stderr, "What is \"%s\"?\n", *argv);
168 explain();
169 return -1;
170 }
171 argc--; argv++;
172 }
173
174 tail = NLMSG_TAIL(n);
175 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
176 if (buckets) {
177 unsigned int log = ilog2(buckets);
178
179 addattr_l(n, 1024, TCA_FQ_BUCKETS_LOG,
180 &log, sizeof(log));
181 }
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800182 if (set_plimit)
Eric Dumazetbc113e42013-08-29 19:30:36 -0700183 addattr_l(n, 1024, TCA_FQ_PLIMIT,
184 &plimit, sizeof(plimit));
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800185 if (set_flow_plimit)
Eric Dumazetbc113e42013-08-29 19:30:36 -0700186 addattr_l(n, 1024, TCA_FQ_FLOW_PLIMIT,
187 &flow_plimit, sizeof(flow_plimit));
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800188 if (set_quantum)
Eric Dumazetbc113e42013-08-29 19:30:36 -0700189 addattr_l(n, 1024, TCA_FQ_QUANTUM, &quantum, sizeof(quantum));
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800190 if (set_initial_quantum)
Eric Dumazetbc113e42013-08-29 19:30:36 -0700191 addattr_l(n, 1024, TCA_FQ_INITIAL_QUANTUM,
192 &initial_quantum, sizeof(initial_quantum));
193 if (pacing != -1)
194 addattr_l(n, 1024, TCA_FQ_RATE_ENABLE,
195 &pacing, sizeof(pacing));
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800196 if (set_maxrate)
Eric Dumazetbc113e42013-08-29 19:30:36 -0700197 addattr_l(n, 1024, TCA_FQ_FLOW_MAX_RATE,
198 &maxrate, sizeof(maxrate));
Yang Yingliangaeb199d2014-05-29 12:04:34 +0800199 if (set_defrate)
Eric Dumazetbc113e42013-08-29 19:30:36 -0700200 addattr_l(n, 1024, TCA_FQ_FLOW_DEFAULT_RATE,
201 &defrate, sizeof(defrate));
Phil Sutter565af7b2015-09-10 16:25:47 +0200202 if (set_refill_delay)
203 addattr_l(n, 1024, TCA_FQ_FLOW_REFILL_DELAY,
Stephen Hemminger8fe98392015-09-25 12:40:00 -0700204 &refill_delay, sizeof(refill_delay));
Eric Dumazet8d5bd8c2015-09-24 04:43:26 -0700205 if (set_orphan_mask)
206 addattr_l(n, 1024, TCA_FQ_ORPHAN_MASK,
Stephen Hemminger8fe98392015-09-25 12:40:00 -0700207 &orphan_mask, sizeof(refill_delay));
Eric Dumazetbc113e42013-08-29 19:30:36 -0700208 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
209 return 0;
210}
211
212static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
213{
214 struct rtattr *tb[TCA_FQ_MAX + 1];
215 unsigned int plimit, flow_plimit;
216 unsigned int buckets_log;
217 int pacing;
218 unsigned int rate, quantum;
Phil Sutter565af7b2015-09-10 16:25:47 +0200219 unsigned int refill_delay;
Eric Dumazet8d5bd8c2015-09-24 04:43:26 -0700220 unsigned int orphan_mask;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700221
Eric Dumazetbc113e42013-08-29 19:30:36 -0700222 SPRINT_BUF(b1);
223
224 if (opt == NULL)
225 return 0;
226
227 parse_rtattr_nested(tb, TCA_FQ_MAX, opt);
228
229 if (tb[TCA_FQ_PLIMIT] &&
230 RTA_PAYLOAD(tb[TCA_FQ_PLIMIT]) >= sizeof(__u32)) {
231 plimit = rta_getattr_u32(tb[TCA_FQ_PLIMIT]);
232 fprintf(f, "limit %up ", plimit);
233 }
234 if (tb[TCA_FQ_FLOW_PLIMIT] &&
235 RTA_PAYLOAD(tb[TCA_FQ_FLOW_PLIMIT]) >= sizeof(__u32)) {
236 flow_plimit = rta_getattr_u32(tb[TCA_FQ_FLOW_PLIMIT]);
237 fprintf(f, "flow_limit %up ", flow_plimit);
238 }
239 if (tb[TCA_FQ_BUCKETS_LOG] &&
240 RTA_PAYLOAD(tb[TCA_FQ_BUCKETS_LOG]) >= sizeof(__u32)) {
241 buckets_log = rta_getattr_u32(tb[TCA_FQ_BUCKETS_LOG]);
242 fprintf(f, "buckets %u ", 1U << buckets_log);
243 }
Eric Dumazet8d5bd8c2015-09-24 04:43:26 -0700244 if (tb[TCA_FQ_ORPHAN_MASK] &&
245 RTA_PAYLOAD(tb[TCA_FQ_ORPHAN_MASK]) >= sizeof(__u32)) {
246 orphan_mask = rta_getattr_u32(tb[TCA_FQ_ORPHAN_MASK]);
247 fprintf(f, "orphan_mask %u ", orphan_mask);
248 }
Eric Dumazetbc113e42013-08-29 19:30:36 -0700249 if (tb[TCA_FQ_RATE_ENABLE] &&
250 RTA_PAYLOAD(tb[TCA_FQ_RATE_ENABLE]) >= sizeof(int)) {
251 pacing = rta_getattr_u32(tb[TCA_FQ_RATE_ENABLE]);
252 if (pacing == 0)
253 fprintf(f, "nopacing ");
254 }
255 if (tb[TCA_FQ_QUANTUM] &&
256 RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) {
257 quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]);
258 fprintf(f, "quantum %u ", quantum);
259 }
260 if (tb[TCA_FQ_INITIAL_QUANTUM] &&
261 RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) {
262 quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
263 fprintf(f, "initial_quantum %u ", quantum);
264 }
265 if (tb[TCA_FQ_FLOW_MAX_RATE] &&
266 RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) {
267 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
268
269 if (rate != ~0U)
270 fprintf(f, "maxrate %s ", sprint_rate(rate, b1));
271 }
272 if (tb[TCA_FQ_FLOW_DEFAULT_RATE] &&
273 RTA_PAYLOAD(tb[TCA_FQ_FLOW_DEFAULT_RATE]) >= sizeof(__u32)) {
274 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]);
275
276 if (rate != 0)
277 fprintf(f, "defrate %s ", sprint_rate(rate, b1));
278 }
Phil Sutter565af7b2015-09-10 16:25:47 +0200279 if (tb[TCA_FQ_FLOW_REFILL_DELAY] &&
280 RTA_PAYLOAD(tb[TCA_FQ_FLOW_REFILL_DELAY]) >= sizeof(__u32)) {
281 refill_delay = rta_getattr_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]);
282 fprintf(f, "refill_delay %s ", sprint_time(refill_delay, b1));
283 }
Eric Dumazetbc113e42013-08-29 19:30:36 -0700284
285 return 0;
286}
287
288static int fq_print_xstats(struct qdisc_util *qu, FILE *f,
289 struct rtattr *xstats)
290{
291 struct tc_fq_qd_stats *st;
292
293 if (xstats == NULL)
294 return 0;
295
296 if (RTA_PAYLOAD(xstats) < sizeof(*st))
297 return -1;
298
299 st = RTA_DATA(xstats);
300
301 fprintf(f, " %u flows (%u inactive, %u throttled)",
302 st->flows, st->inactive_flows, st->throttled_flows);
303
304 if (st->time_next_delayed_flow > 0)
305 fprintf(f, ", next packet delay %llu ns", st->time_next_delayed_flow);
306
307 fprintf(f, "\n %llu gc, %llu highprio",
308 st->gc_flows, st->highprio_packets);
309
310 if (st->tcp_retrans)
311 fprintf(f, ", %llu retrans", st->tcp_retrans);
312
313 fprintf(f, ", %llu throttled", st->throttled);
314
315 if (st->flows_plimit)
316 fprintf(f, ", %llu flows_plimit", st->flows_plimit);
317
318 if (st->pkts_too_long || st->allocation_errors)
319 fprintf(f, "\n %llu too long pkts, %llu alloc errors\n",
320 st->pkts_too_long, st->allocation_errors);
321
322 return 0;
323}
324
325struct qdisc_util fq_qdisc_util = {
326 .id = "fq",
327 .parse_qopt = fq_parse_opt,
328 .print_qopt = fq_print_opt,
329 .print_xstats = fq_print_xstats,
330};