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