]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_hfsc.c
tc: skbprio: add support for JSON output
[mirror_iproute2.git] / tc / q_hfsc.c
CommitLineData
aba5acdf 1/*
7518df00 2 * q_hfsc.c HFSC.
aba5acdf
SH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
7518df00 9 * Authors: Patrick McHardy, <kaber@trash.net>
aba5acdf
SH
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
aba5acdf
SH
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
7518df00 21#include <math.h>
aba5acdf
SH
22
23#include "utils.h"
24#include "tc_util.h"
25
859af0a5
SH
26static int hfsc_get_sc(int *, char ***,
27 struct tc_service_curve *, const char *);
7518df00 28
29static void
30explain_qdisc(void)
31{
32 fprintf(stderr,
33 "Usage: ... hfsc [ default CLASSID ]\n"
34 "\n"
35 " default: default class for unclassified packets\n"
36 );
37}
38
39static void
40explain_class(void)
41{
42 fprintf(stderr,
b7de67da 43 "Usage: ... hfsc [ [ rt SC ] [ ls SC ] | [ sc SC ] ] [ ul SC ]\n"
7518df00 44 "\n"
41f60041 45 "SC := [ [ m1 BPS ] d SEC ] m2 BPS\n"
7518df00 46 "\n"
47 " m1 : slope of first segment\n"
48 " d : x-coordinate of intersection\n"
49 " m2 : slope of second segment\n"
50 "\n"
51 "Alternative format:\n"
52 "\n"
53 "SC := [ [ umax BYTE ] dmax SEC ] rate BPS\n"
54 "\n"
55 " umax : maximum unit of work\n"
56 " dmax : maximum delay\n"
57 " rate : rate\n"
58 "\n"
41f60041
MS
59 "Remarks:\n"
60 " - at least one of 'rt', 'ls' or 'sc' must be specified\n"
61 " - 'ul' can only be specified with 'ls' or 'sc'\n"
62 "\n"
7518df00 63 );
64}
65
66static void
67explain1(char *arg)
68{
69 fprintf(stderr, "HFSC: Illegal \"%s\"\n", arg);
70}
71
72static int
859af0a5
SH
73hfsc_parse_opt(struct qdisc_util *qu, int argc, char **argv,
74 struct nlmsghdr *n, const char *dev)
7518df00 75{
d17b136f 76 struct tc_hfsc_qopt qopt = {};
7518df00 77
78 while (argc > 0) {
79 if (matches(*argv, "default") == 0) {
80 NEXT_ARG();
81 if (qopt.defcls != 0) {
82 fprintf(stderr, "HFSC: Double \"default\"\n");
83 return -1;
84 }
85 if (get_u16(&qopt.defcls, *argv, 16) < 0) {
86 explain1("default");
87 return -1;
88 }
89 } else if (matches(*argv, "help") == 0) {
90 explain_qdisc();
91 return -1;
92 } else {
93 fprintf(stderr, "HFSC: What is \"%s\" ?\n", *argv);
94 explain_qdisc();
95 return -1;
96 }
97 argc--, argv++;
98 }
99
100 addattr_l(n, 1024, TCA_OPTIONS, &qopt, sizeof(qopt));
101 return 0;
102}
103
104static int
105hfsc_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
aba5acdf 106{
7518df00 107 struct tc_hfsc_qopt *qopt;
108
109 if (opt == NULL)
110 return 0;
111 if (RTA_PAYLOAD(opt) < sizeof(*qopt))
112 return -1;
113 qopt = RTA_DATA(opt);
114
115 if (qopt->defcls != 0)
116 fprintf(f, "default %x ", qopt->defcls);
117
118 return 0;
aba5acdf
SH
119}
120
7518df00 121static int
122hfsc_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
aba5acdf 123{
7518df00 124 struct tc_hfsc_stats *st;
125
126 if (xstats == NULL)
127 return 0;
128 if (RTA_PAYLOAD(xstats) < sizeof(*st))
129 return -1;
130 st = RTA_DATA(xstats);
131
132 fprintf(f, " period %u ", st->period);
133 if (st->work != 0)
b906243b 134 fprintf(f, "work %llu bytes ", (unsigned long long) st->work);
7518df00 135 if (st->rtwork != 0)
b906243b 136 fprintf(f, "rtwork %llu bytes ", (unsigned long long) st->rtwork);
7518df00 137 fprintf(f, "level %u ", st->level);
138 fprintf(f, "\n");
139
140 return 0;
141}
142
143static int
144hfsc_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
927e3cfb 145 struct nlmsghdr *n, const char *dev)
7518df00 146{
d17b136f
PS
147 struct tc_service_curve rsc = {}, fsc = {}, usc = {};
148 int rsc_ok = 0, fsc_ok = 0, usc_ok = 0;
7518df00 149 struct rtattr *tail;
150
7518df00 151 while (argc > 0) {
152 if (matches(*argv, "rt") == 0) {
153 NEXT_ARG();
927e3cfb 154 if (hfsc_get_sc(&argc, &argv, &rsc, dev) < 0) {
7518df00 155 explain1("rt");
156 return -1;
157 }
158 rsc_ok = 1;
159 } else if (matches(*argv, "ls") == 0) {
160 NEXT_ARG();
927e3cfb 161 if (hfsc_get_sc(&argc, &argv, &fsc, dev) < 0) {
7518df00 162 explain1("ls");
163 return -1;
164 }
165 fsc_ok = 1;
b7de67da 166 } else if (matches(*argv, "sc") == 0) {
167 NEXT_ARG();
927e3cfb 168 if (hfsc_get_sc(&argc, &argv, &rsc, dev) < 0) {
b7de67da 169 explain1("sc");
170 return -1;
171 }
172 memcpy(&fsc, &rsc, sizeof(fsc));
173 rsc_ok = 1;
174 fsc_ok = 1;
7518df00 175 } else if (matches(*argv, "ul") == 0) {
176 NEXT_ARG();
927e3cfb 177 if (hfsc_get_sc(&argc, &argv, &usc, dev) < 0) {
7518df00 178 explain1("ul");
179 return -1;
180 }
181 usc_ok = 1;
182 } else if (matches(*argv, "help") == 0) {
183 explain_class();
184 return -1;
185 } else {
186 fprintf(stderr, "HFSC: What is \"%s\" ?\n", *argv);
187 explain_class();
188 return -1;
189 }
190 argc--, argv++;
191 }
192
193 if (!(rsc_ok || fsc_ok || usc_ok)) {
194 fprintf(stderr, "HFSC: no parameters given\n");
195 explain_class();
196 return -1;
197 }
198 if (usc_ok && !fsc_ok) {
32a121cb 199 fprintf(stderr, "HFSC: Upper-limit Service Curve without Link-Share Service Curve\n");
7518df00 200 explain_class();
201 return -1;
202 }
203
c14f9d92 204 tail = addattr_nest(n, 1024, TCA_OPTIONS);
7518df00 205 if (rsc_ok)
206 addattr_l(n, 1024, TCA_HFSC_RSC, &rsc, sizeof(rsc));
207 if (fsc_ok)
208 addattr_l(n, 1024, TCA_HFSC_FSC, &fsc, sizeof(fsc));
209 if (usc_ok)
210 addattr_l(n, 1024, TCA_HFSC_USC, &usc, sizeof(usc));
211
c14f9d92 212 addattr_nest_end(n, tail);
7518df00 213 return 0;
aba5acdf
SH
214}
215
7518df00 216static void
217hfsc_print_sc(FILE *f, char *name, struct tc_service_curve *sc)
aba5acdf 218{
7518df00 219 SPRINT_BUF(b1);
220
221 fprintf(f, "%s ", name);
222 fprintf(f, "m1 %s ", sprint_rate(sc->m1, b1));
8f34caaf 223 fprintf(f, "d %s ", sprint_time(tc_core_ktime2time(sc->d), b1));
7518df00 224 fprintf(f, "m2 %s ", sprint_rate(sc->m2, b1));
aba5acdf
SH
225}
226
7518df00 227static int
228hfsc_print_class_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
229{
230 struct rtattr *tb[TCA_HFSC_MAX+1];
231 struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
232
233 if (opt == NULL)
234 return 0;
235
ac2fc2df 236 parse_rtattr_nested(tb, TCA_HFSC_MAX, opt);
7518df00 237
238 if (tb[TCA_HFSC_RSC]) {
239 if (RTA_PAYLOAD(tb[TCA_HFSC_RSC]) < sizeof(*rsc))
240 fprintf(stderr, "HFSC: truncated realtime option\n");
241 else
242 rsc = RTA_DATA(tb[TCA_HFSC_RSC]);
243 }
244 if (tb[TCA_HFSC_FSC]) {
245 if (RTA_PAYLOAD(tb[TCA_HFSC_FSC]) < sizeof(*fsc))
246 fprintf(stderr, "HFSC: truncated linkshare option\n");
247 else
248 fsc = RTA_DATA(tb[TCA_HFSC_FSC]);
249 }
250 if (tb[TCA_HFSC_USC]) {
251 if (RTA_PAYLOAD(tb[TCA_HFSC_USC]) < sizeof(*usc))
252 fprintf(stderr, "HFSC: truncated upperlimit option\n");
253 else
254 usc = RTA_DATA(tb[TCA_HFSC_USC]);
255 }
256
ae665a52 257
b7de67da 258 if (rsc != NULL && fsc != NULL &&
259 memcmp(rsc, fsc, sizeof(*rsc)) == 0)
260 hfsc_print_sc(f, "sc", rsc);
261 else {
262 if (rsc != NULL)
263 hfsc_print_sc(f, "rt", rsc);
264 if (fsc != NULL)
265 hfsc_print_sc(f, "ls", fsc);
266 }
7518df00 267 if (usc != NULL)
268 hfsc_print_sc(f, "ul", usc);
269
270 return 0;
271}
ae665a52 272
95812b56 273struct qdisc_util hfsc_qdisc_util = {
f2f99e2e
SH
274 .id = "hfsc",
275 .parse_qopt = hfsc_parse_opt,
276 .print_qopt = hfsc_print_opt,
277 .print_xstats = hfsc_print_xstats,
278 .parse_copt = hfsc_parse_class_opt,
279 .print_copt = hfsc_print_class_opt,
aba5acdf
SH
280};
281
7518df00 282static int
859af0a5
SH
283hfsc_get_sc1(int *argcp, char ***argvp,
284 struct tc_service_curve *sc, const char *dev)
7518df00 285{
286 char **argv = *argvp;
287 int argc = *argcp;
288 unsigned int m1 = 0, d = 0, m2 = 0;
289
290 if (matches(*argv, "m1") == 0) {
291 NEXT_ARG();
927e3cfb
ND
292 if (strchr(*argv, '%')) {
293 if (get_percent_rate(&m1, *argv, dev)) {
294 explain1("m1");
295 return -1;
296 }
297 } else if (get_rate(&m1, *argv) < 0) {
7518df00 298 explain1("m1");
299 return -1;
300 }
301 NEXT_ARG();
302 }
303
304 if (matches(*argv, "d") == 0) {
305 NEXT_ARG();
8f34caaf 306 if (get_time(&d, *argv) < 0) {
7518df00 307 explain1("d");
308 return -1;
309 }
310 NEXT_ARG();
311 }
312
313 if (matches(*argv, "m2") == 0) {
314 NEXT_ARG();
927e3cfb
ND
315 if (strchr(*argv, '%')) {
316 if (get_percent_rate(&m2, *argv, dev)) {
317 explain1("m2");
318 return -1;
319 }
320 } else if (get_rate(&m2, *argv) < 0) {
7518df00 321 explain1("m2");
322 return -1;
323 }
324 } else
325 return -1;
326
327 sc->m1 = m1;
f0bda7e5 328 sc->d = tc_core_time2ktime(d);
7518df00 329 sc->m2 = m2;
330
331 *argvp = argv;
332 *argcp = argc;
333 return 0;
334}
335
336static int
927e3cfb 337hfsc_get_sc2(int *argcp, char ***argvp, struct tc_service_curve *sc, const char *dev)
7518df00 338{
339 char **argv = *argvp;
340 int argc = *argcp;
341 unsigned int umax = 0, dmax = 0, rate = 0;
342
343 if (matches(*argv, "umax") == 0) {
344 NEXT_ARG();
345 if (get_size(&umax, *argv) < 0) {
346 explain1("umax");
347 return -1;
348 }
349 NEXT_ARG();
350 }
351
352 if (matches(*argv, "dmax") == 0) {
353 NEXT_ARG();
8f34caaf 354 if (get_time(&dmax, *argv) < 0) {
7518df00 355 explain1("dmax");
356 return -1;
357 }
358 NEXT_ARG();
359 }
360
361 if (matches(*argv, "rate") == 0) {
362 NEXT_ARG();
927e3cfb
ND
363 if (strchr(*argv, '%')) {
364 if (get_percent_rate(&rate, *argv, dev)) {
365 explain1("rate");
366 return -1;
367 }
368 } else if (get_rate(&rate, *argv) < 0) {
7518df00 369 explain1("rate");
370 return -1;
371 }
372 } else
373 return -1;
374
375 if (umax != 0 && dmax == 0) {
376 fprintf(stderr, "HFSC: umax given but dmax is zero.\n");
377 return -1;
378 }
379
f0bda7e5 380 if (dmax != 0 && ceil(1.0 * umax * TIME_UNITS_PER_SEC / dmax) > rate) {
7518df00 381 /*
382 * concave curve, slope of first segment is umax/dmax,
383 * intersection is at dmax
384 */
f0bda7e5
PM
385 sc->m1 = ceil(1.0 * umax * TIME_UNITS_PER_SEC / dmax); /* in bps */
386 sc->d = tc_core_time2ktime(dmax);
7518df00 387 sc->m2 = rate;
388 } else {
389 /*
390 * convex curve, slope of first segment is 0, intersection
391 * is at dmax - umax / rate
392 */
393 sc->m1 = 0;
f0bda7e5 394 sc->d = tc_core_time2ktime(ceil(dmax - umax * TIME_UNITS_PER_SEC / rate));
7518df00 395 sc->m2 = rate;
396 }
397
398 *argvp = argv;
399 *argcp = argc;
400 return 0;
401}
402
403static int
927e3cfb 404hfsc_get_sc(int *argcp, char ***argvp, struct tc_service_curve *sc, const char *dev)
7518df00 405{
927e3cfb
ND
406 if (hfsc_get_sc1(argcp, argvp, sc, dev) < 0 &&
407 hfsc_get_sc2(argcp, argvp, sc, dev) < 0)
7518df00 408 return -1;
409
410 if (sc->m1 == 0 && sc->m2 == 0) {
411 fprintf(stderr, "HFSC: Service Curve has two zero slopes\n");
412 return -1;
413 }
414
415 return 0;
416}