]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_hfsc.c
dc9fed930fc03ac8afabf8480dfdb8b44ddddc01
[mirror_iproute2.git] / tc / q_hfsc.c
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 <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21 #include <math.h>
22
23 #include "utils.h"
24 #include "tc_util.h"
25
26 static int hfsc_get_sc(int *, char ***, struct tc_service_curve *);
27
28
29 static void
30 explain_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
39 static void
40 explain_class(void)
41 {
42 fprintf(stderr,
43 "Usage: ... hfsc [ [ rt SC ] [ ls SC ] | [ sc SC ] ] [ ul SC ]\n"
44 "\n"
45 "SC := [ [ m1 BPS ] d SEC ] m2 BPS\n"
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"
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"
63 );
64 }
65
66 static void
67 explain1(char *arg)
68 {
69 fprintf(stderr, "HFSC: Illegal \"%s\"\n", arg);
70 }
71
72 static int
73 hfsc_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
74 {
75 struct tc_hfsc_qopt qopt = {};
76
77 while (argc > 0) {
78 if (matches(*argv, "default") == 0) {
79 NEXT_ARG();
80 if (qopt.defcls != 0) {
81 fprintf(stderr, "HFSC: Double \"default\"\n");
82 return -1;
83 }
84 if (get_u16(&qopt.defcls, *argv, 16) < 0) {
85 explain1("default");
86 return -1;
87 }
88 } else if (matches(*argv, "help") == 0) {
89 explain_qdisc();
90 return -1;
91 } else {
92 fprintf(stderr, "HFSC: What is \"%s\" ?\n", *argv);
93 explain_qdisc();
94 return -1;
95 }
96 argc--, argv++;
97 }
98
99 addattr_l(n, 1024, TCA_OPTIONS, &qopt, sizeof(qopt));
100 return 0;
101 }
102
103 static int
104 hfsc_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
105 {
106 struct tc_hfsc_qopt *qopt;
107
108 if (opt == NULL)
109 return 0;
110 if (RTA_PAYLOAD(opt) < sizeof(*qopt))
111 return -1;
112 qopt = RTA_DATA(opt);
113
114 if (qopt->defcls != 0)
115 fprintf(f, "default %x ", qopt->defcls);
116
117 return 0;
118 }
119
120 static int
121 hfsc_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
122 {
123 struct tc_hfsc_stats *st;
124
125 if (xstats == NULL)
126 return 0;
127 if (RTA_PAYLOAD(xstats) < sizeof(*st))
128 return -1;
129 st = RTA_DATA(xstats);
130
131 fprintf(f, " period %u ", st->period);
132 if (st->work != 0)
133 fprintf(f, "work %llu bytes ", (unsigned long long) st->work);
134 if (st->rtwork != 0)
135 fprintf(f, "rtwork %llu bytes ", (unsigned long long) st->rtwork);
136 fprintf(f, "level %u ", st->level);
137 fprintf(f, "\n");
138
139 return 0;
140 }
141
142 static int
143 hfsc_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
144 struct nlmsghdr *n)
145 {
146 struct tc_service_curve rsc = {}, fsc = {}, usc = {};
147 int rsc_ok = 0, fsc_ok = 0, usc_ok = 0;
148 struct rtattr *tail;
149
150 while (argc > 0) {
151 if (matches(*argv, "rt") == 0) {
152 NEXT_ARG();
153 if (hfsc_get_sc(&argc, &argv, &rsc) < 0) {
154 explain1("rt");
155 return -1;
156 }
157 rsc_ok = 1;
158 } else if (matches(*argv, "ls") == 0) {
159 NEXT_ARG();
160 if (hfsc_get_sc(&argc, &argv, &fsc) < 0) {
161 explain1("ls");
162 return -1;
163 }
164 fsc_ok = 1;
165 } else if (matches(*argv, "sc") == 0) {
166 NEXT_ARG();
167 if (hfsc_get_sc(&argc, &argv, &rsc) < 0) {
168 explain1("sc");
169 return -1;
170 }
171 memcpy(&fsc, &rsc, sizeof(fsc));
172 rsc_ok = 1;
173 fsc_ok = 1;
174 } else if (matches(*argv, "ul") == 0) {
175 NEXT_ARG();
176 if (hfsc_get_sc(&argc, &argv, &usc) < 0) {
177 explain1("ul");
178 return -1;
179 }
180 usc_ok = 1;
181 } else if (matches(*argv, "help") == 0) {
182 explain_class();
183 return -1;
184 } else {
185 fprintf(stderr, "HFSC: What is \"%s\" ?\n", *argv);
186 explain_class();
187 return -1;
188 }
189 argc--, argv++;
190 }
191
192 if (!(rsc_ok || fsc_ok || usc_ok)) {
193 fprintf(stderr, "HFSC: no parameters given\n");
194 explain_class();
195 return -1;
196 }
197 if (usc_ok && !fsc_ok) {
198 fprintf(stderr, "HFSC: Upper-limit Service Curve without Link-Share Service Curve\n");
199 explain_class();
200 return -1;
201 }
202
203 tail = NLMSG_TAIL(n);
204
205 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
206 if (rsc_ok)
207 addattr_l(n, 1024, TCA_HFSC_RSC, &rsc, sizeof(rsc));
208 if (fsc_ok)
209 addattr_l(n, 1024, TCA_HFSC_FSC, &fsc, sizeof(fsc));
210 if (usc_ok)
211 addattr_l(n, 1024, TCA_HFSC_USC, &usc, sizeof(usc));
212
213 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
214 return 0;
215 }
216
217 static void
218 hfsc_print_sc(FILE *f, char *name, struct tc_service_curve *sc)
219 {
220 SPRINT_BUF(b1);
221
222 fprintf(f, "%s ", name);
223 fprintf(f, "m1 %s ", sprint_rate(sc->m1, b1));
224 fprintf(f, "d %s ", sprint_time(tc_core_ktime2time(sc->d), b1));
225 fprintf(f, "m2 %s ", sprint_rate(sc->m2, b1));
226 }
227
228 static int
229 hfsc_print_class_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
230 {
231 struct rtattr *tb[TCA_HFSC_MAX+1];
232 struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
233
234 if (opt == NULL)
235 return 0;
236
237 parse_rtattr_nested(tb, TCA_HFSC_MAX, opt);
238
239 if (tb[TCA_HFSC_RSC]) {
240 if (RTA_PAYLOAD(tb[TCA_HFSC_RSC]) < sizeof(*rsc))
241 fprintf(stderr, "HFSC: truncated realtime option\n");
242 else
243 rsc = RTA_DATA(tb[TCA_HFSC_RSC]);
244 }
245 if (tb[TCA_HFSC_FSC]) {
246 if (RTA_PAYLOAD(tb[TCA_HFSC_FSC]) < sizeof(*fsc))
247 fprintf(stderr, "HFSC: truncated linkshare option\n");
248 else
249 fsc = RTA_DATA(tb[TCA_HFSC_FSC]);
250 }
251 if (tb[TCA_HFSC_USC]) {
252 if (RTA_PAYLOAD(tb[TCA_HFSC_USC]) < sizeof(*usc))
253 fprintf(stderr, "HFSC: truncated upperlimit option\n");
254 else
255 usc = RTA_DATA(tb[TCA_HFSC_USC]);
256 }
257
258
259 if (rsc != NULL && fsc != NULL &&
260 memcmp(rsc, fsc, sizeof(*rsc)) == 0)
261 hfsc_print_sc(f, "sc", rsc);
262 else {
263 if (rsc != NULL)
264 hfsc_print_sc(f, "rt", rsc);
265 if (fsc != NULL)
266 hfsc_print_sc(f, "ls", fsc);
267 }
268 if (usc != NULL)
269 hfsc_print_sc(f, "ul", usc);
270
271 return 0;
272 }
273
274 struct qdisc_util hfsc_qdisc_util = {
275 .id = "hfsc",
276 .parse_qopt = hfsc_parse_opt,
277 .print_qopt = hfsc_print_opt,
278 .print_xstats = hfsc_print_xstats,
279 .parse_copt = hfsc_parse_class_opt,
280 .print_copt = hfsc_print_class_opt,
281 };
282
283 static int
284 hfsc_get_sc1(int *argcp, char ***argvp, struct tc_service_curve *sc)
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();
292 if (get_rate(&m1, *argv) < 0) {
293 explain1("m1");
294 return -1;
295 }
296 NEXT_ARG();
297 }
298
299 if (matches(*argv, "d") == 0) {
300 NEXT_ARG();
301 if (get_time(&d, *argv) < 0) {
302 explain1("d");
303 return -1;
304 }
305 NEXT_ARG();
306 }
307
308 if (matches(*argv, "m2") == 0) {
309 NEXT_ARG();
310 if (get_rate(&m2, *argv) < 0) {
311 explain1("m2");
312 return -1;
313 }
314 } else
315 return -1;
316
317 sc->m1 = m1;
318 sc->d = tc_core_time2ktime(d);
319 sc->m2 = m2;
320
321 *argvp = argv;
322 *argcp = argc;
323 return 0;
324 }
325
326 static int
327 hfsc_get_sc2(int *argcp, char ***argvp, struct tc_service_curve *sc)
328 {
329 char **argv = *argvp;
330 int argc = *argcp;
331 unsigned int umax = 0, dmax = 0, rate = 0;
332
333 if (matches(*argv, "umax") == 0) {
334 NEXT_ARG();
335 if (get_size(&umax, *argv) < 0) {
336 explain1("umax");
337 return -1;
338 }
339 NEXT_ARG();
340 }
341
342 if (matches(*argv, "dmax") == 0) {
343 NEXT_ARG();
344 if (get_time(&dmax, *argv) < 0) {
345 explain1("dmax");
346 return -1;
347 }
348 NEXT_ARG();
349 }
350
351 if (matches(*argv, "rate") == 0) {
352 NEXT_ARG();
353 if (get_rate(&rate, *argv) < 0) {
354 explain1("rate");
355 return -1;
356 }
357 } else
358 return -1;
359
360 if (umax != 0 && dmax == 0) {
361 fprintf(stderr, "HFSC: umax given but dmax is zero.\n");
362 return -1;
363 }
364
365 if (dmax != 0 && ceil(1.0 * umax * TIME_UNITS_PER_SEC / dmax) > rate) {
366 /*
367 * concave curve, slope of first segment is umax/dmax,
368 * intersection is at dmax
369 */
370 sc->m1 = ceil(1.0 * umax * TIME_UNITS_PER_SEC / dmax); /* in bps */
371 sc->d = tc_core_time2ktime(dmax);
372 sc->m2 = rate;
373 } else {
374 /*
375 * convex curve, slope of first segment is 0, intersection
376 * is at dmax - umax / rate
377 */
378 sc->m1 = 0;
379 sc->d = tc_core_time2ktime(ceil(dmax - umax * TIME_UNITS_PER_SEC / rate));
380 sc->m2 = rate;
381 }
382
383 *argvp = argv;
384 *argcp = argc;
385 return 0;
386 }
387
388 static int
389 hfsc_get_sc(int *argcp, char ***argvp, struct tc_service_curve *sc)
390 {
391 if (hfsc_get_sc1(argcp, argvp, sc) < 0 &&
392 hfsc_get_sc2(argcp, argvp, sc) < 0)
393 return -1;
394
395 if (sc->m1 == 0 && sc->m2 == 0) {
396 fprintf(stderr, "HFSC: Service Curve has two zero slopes\n");
397 return -1;
398 }
399
400 return 0;
401 }