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