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