1 /*
2 * q_hfsc.c HFSC.
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 *
9 * Authors: Patrick McHardy, <kaber@trash.net>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <math.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26
27 static int hfsc_get_sc(int *, char ***, struct tc_service_curve *);
28
29
30 static void
explain_qdisc(void)31 explain_qdisc(void)
32 {
33 fprintf(stderr,
34 "Usage: ... hfsc [ default CLASSID ]\n"
35 "\n"
36 " default: default class for unclassified packets\n"
37 );
38 }
39
40 static void
explain_class(void)41 explain_class(void)
42 {
43 fprintf(stderr,
44 "Usage: ... hfsc [ [ rt SC ] [ ls SC ] | [ sc SC ] ] [ ul SC ]\n"
45 "\n"
46 "SC := [ [ m1 BPS ] d SEC ] m2 BPS\n"
47 "\n"
48 " m1 : slope of first segment\n"
49 " d : x-coordinate of intersection\n"
50 " m2 : slope of second segment\n"
51 "\n"
52 "Alternative format:\n"
53 "\n"
54 "SC := [ [ umax BYTE ] dmax SEC ] rate BPS\n"
55 "\n"
56 " umax : maximum unit of work\n"
57 " dmax : maximum delay\n"
58 " rate : rate\n"
59 "\n"
60 "Remarks:\n"
61 " - at least one of 'rt', 'ls' or 'sc' must be specified\n"
62 " - 'ul' can only be specified with 'ls' or 'sc'\n"
63 "\n"
64 );
65 }
66
67 static void
explain1(char * arg)68 explain1(char *arg)
69 {
70 fprintf(stderr, "HFSC: Illegal \"%s\"\n", arg);
71 }
72
73 static int
hfsc_parse_opt(struct qdisc_util * qu,int argc,char ** argv,struct nlmsghdr * n)74 hfsc_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
75 {
76 struct tc_hfsc_qopt qopt = {};
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
104 static int
hfsc_print_opt(struct qdisc_util * qu,FILE * f,struct rtattr * opt)105 hfsc_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
106 {
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;
119 }
120
121 static int
hfsc_print_xstats(struct qdisc_util * qu,FILE * f,struct rtattr * xstats)122 hfsc_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
123 {
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)
134 fprintf(f, "work %llu bytes ", (unsigned long long) st->work);
135 if (st->rtwork != 0)
136 fprintf(f, "rtwork %llu bytes ", (unsigned long long) st->rtwork);
137 fprintf(f, "level %u ", st->level);
138 fprintf(f, "\n");
139
140 return 0;
141 }
142
143 static int
hfsc_parse_class_opt(struct qdisc_util * qu,int argc,char ** argv,struct nlmsghdr * n)144 hfsc_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
145 struct nlmsghdr *n)
146 {
147 struct tc_service_curve rsc = {}, fsc = {}, usc = {};
148 int rsc_ok = 0, fsc_ok = 0, usc_ok = 0;
149 struct rtattr *tail;
150
151 while (argc > 0) {
152 if (matches(*argv, "rt") == 0) {
153 NEXT_ARG();
154 if (hfsc_get_sc(&argc, &argv, &rsc) < 0) {
155 explain1("rt");
156 return -1;
157 }
158 rsc_ok = 1;
159 } else if (matches(*argv, "ls") == 0) {
160 NEXT_ARG();
161 if (hfsc_get_sc(&argc, &argv, &fsc) < 0) {
162 explain1("ls");
163 return -1;
164 }
165 fsc_ok = 1;
166 } else if (matches(*argv, "sc") == 0) {
167 NEXT_ARG();
168 if (hfsc_get_sc(&argc, &argv, &rsc) < 0) {
169 explain1("sc");
170 return -1;
171 }
172 memcpy(&fsc, &rsc, sizeof(fsc));
173 rsc_ok = 1;
174 fsc_ok = 1;
175 } else if (matches(*argv, "ul") == 0) {
176 NEXT_ARG();
177 if (hfsc_get_sc(&argc, &argv, &usc) < 0) {
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) {
199 fprintf(stderr, "HFSC: Upper-limit Service Curve without Link-Share Service Curve\n");
200 explain_class();
201 return -1;
202 }
203
204 tail = NLMSG_TAIL(n);
205
206 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
207 if (rsc_ok)
208 addattr_l(n, 1024, TCA_HFSC_RSC, &rsc, sizeof(rsc));
209 if (fsc_ok)
210 addattr_l(n, 1024, TCA_HFSC_FSC, &fsc, sizeof(fsc));
211 if (usc_ok)
212 addattr_l(n, 1024, TCA_HFSC_USC, &usc, sizeof(usc));
213
214 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
215 return 0;
216 }
217
218 static void
hfsc_print_sc(FILE * f,char * name,struct tc_service_curve * sc)219 hfsc_print_sc(FILE *f, char *name, struct tc_service_curve *sc)
220 {
221 SPRINT_BUF(b1);
222
223 fprintf(f, "%s ", name);
224 fprintf(f, "m1 %s ", sprint_rate(sc->m1, b1));
225 fprintf(f, "d %s ", sprint_time(tc_core_ktime2time(sc->d), b1));
226 fprintf(f, "m2 %s ", sprint_rate(sc->m2, b1));
227 }
228
229 static int
hfsc_print_class_opt(struct qdisc_util * qu,FILE * f,struct rtattr * opt)230 hfsc_print_class_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
231 {
232 struct rtattr *tb[TCA_HFSC_MAX+1];
233 struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
234
235 if (opt == NULL)
236 return 0;
237
238 parse_rtattr_nested(tb, TCA_HFSC_MAX, opt);
239
240 if (tb[TCA_HFSC_RSC]) {
241 if (RTA_PAYLOAD(tb[TCA_HFSC_RSC]) < sizeof(*rsc))
242 fprintf(stderr, "HFSC: truncated realtime option\n");
243 else
244 rsc = RTA_DATA(tb[TCA_HFSC_RSC]);
245 }
246 if (tb[TCA_HFSC_FSC]) {
247 if (RTA_PAYLOAD(tb[TCA_HFSC_FSC]) < sizeof(*fsc))
248 fprintf(stderr, "HFSC: truncated linkshare option\n");
249 else
250 fsc = RTA_DATA(tb[TCA_HFSC_FSC]);
251 }
252 if (tb[TCA_HFSC_USC]) {
253 if (RTA_PAYLOAD(tb[TCA_HFSC_USC]) < sizeof(*usc))
254 fprintf(stderr, "HFSC: truncated upperlimit option\n");
255 else
256 usc = RTA_DATA(tb[TCA_HFSC_USC]);
257 }
258
259
260 if (rsc != NULL && fsc != NULL &&
261 memcmp(rsc, fsc, sizeof(*rsc)) == 0)
262 hfsc_print_sc(f, "sc", rsc);
263 else {
264 if (rsc != NULL)
265 hfsc_print_sc(f, "rt", rsc);
266 if (fsc != NULL)
267 hfsc_print_sc(f, "ls", fsc);
268 }
269 if (usc != NULL)
270 hfsc_print_sc(f, "ul", usc);
271
272 return 0;
273 }
274
275 struct qdisc_util hfsc_qdisc_util = {
276 .id = "hfsc",
277 .parse_qopt = hfsc_parse_opt,
278 .print_qopt = hfsc_print_opt,
279 .print_xstats = hfsc_print_xstats,
280 .parse_copt = hfsc_parse_class_opt,
281 .print_copt = hfsc_print_class_opt,
282 };
283
284 static int
hfsc_get_sc1(int * argcp,char *** argvp,struct tc_service_curve * sc)285 hfsc_get_sc1(int *argcp, char ***argvp, struct tc_service_curve *sc)
286 {
287 char **argv = *argvp;
288 int argc = *argcp;
289 unsigned int m1 = 0, d = 0, m2 = 0;
290
291 if (matches(*argv, "m1") == 0) {
292 NEXT_ARG();
293 if (get_rate(&m1, *argv) < 0) {
294 explain1("m1");
295 return -1;
296 }
297 NEXT_ARG();
298 }
299
300 if (matches(*argv, "d") == 0) {
301 NEXT_ARG();
302 if (get_time(&d, *argv) < 0) {
303 explain1("d");
304 return -1;
305 }
306 NEXT_ARG();
307 }
308
309 if (matches(*argv, "m2") == 0) {
310 NEXT_ARG();
311 if (get_rate(&m2, *argv) < 0) {
312 explain1("m2");
313 return -1;
314 }
315 } else
316 return -1;
317
318 sc->m1 = m1;
319 sc->d = tc_core_time2ktime(d);
320 sc->m2 = m2;
321
322 *argvp = argv;
323 *argcp = argc;
324 return 0;
325 }
326
327 static int
hfsc_get_sc2(int * argcp,char *** argvp,struct tc_service_curve * sc)328 hfsc_get_sc2(int *argcp, char ***argvp, struct tc_service_curve *sc)
329 {
330 char **argv = *argvp;
331 int argc = *argcp;
332 unsigned int umax = 0, dmax = 0, rate = 0;
333
334 if (matches(*argv, "umax") == 0) {
335 NEXT_ARG();
336 if (get_size(&umax, *argv) < 0) {
337 explain1("umax");
338 return -1;
339 }
340 NEXT_ARG();
341 }
342
343 if (matches(*argv, "dmax") == 0) {
344 NEXT_ARG();
345 if (get_time(&dmax, *argv) < 0) {
346 explain1("dmax");
347 return -1;
348 }
349 NEXT_ARG();
350 }
351
352 if (matches(*argv, "rate") == 0) {
353 NEXT_ARG();
354 if (get_rate(&rate, *argv) < 0) {
355 explain1("rate");
356 return -1;
357 }
358 } else
359 return -1;
360
361 if (umax != 0 && dmax == 0) {
362 fprintf(stderr, "HFSC: umax given but dmax is zero.\n");
363 return -1;
364 }
365
366 if (dmax != 0 && ceil(1.0 * umax * TIME_UNITS_PER_SEC / dmax) > rate) {
367 /*
368 * concave curve, slope of first segment is umax/dmax,
369 * intersection is at dmax
370 */
371 sc->m1 = ceil(1.0 * umax * TIME_UNITS_PER_SEC / dmax); /* in bps */
372 sc->d = tc_core_time2ktime(dmax);
373 sc->m2 = rate;
374 } else {
375 /*
376 * convex curve, slope of first segment is 0, intersection
377 * is at dmax - umax / rate
378 */
379 sc->m1 = 0;
380 sc->d = tc_core_time2ktime(ceil(dmax - umax * TIME_UNITS_PER_SEC / rate));
381 sc->m2 = rate;
382 }
383
384 *argvp = argv;
385 *argcp = argc;
386 return 0;
387 }
388
389 static int
hfsc_get_sc(int * argcp,char *** argvp,struct tc_service_curve * sc)390 hfsc_get_sc(int *argcp, char ***argvp, struct tc_service_curve *sc)
391 {
392 if (hfsc_get_sc1(argcp, argvp, sc) < 0 &&
393 hfsc_get_sc2(argcp, argvp, sc) < 0)
394 return -1;
395
396 if (sc->m1 == 0 && sc->m2 == 0) {
397 fprintf(stderr, "HFSC: Service Curve has two zero slopes\n");
398 return -1;
399 }
400
401 return 0;
402 }
403