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.
9 * Authors: Patrick McHardy, <kaber@trash.net>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
26 static int hfsc_get_sc(int *, char ***,
27 struct tc_service_curve
*, const char *);
33 "Usage: ... hfsc [ default CLASSID ]\n"
35 " default: default class for unclassified packets\n"
43 "Usage: ... hfsc [ [ rt SC ] [ ls SC ] | [ sc SC ] ] [ ul SC ]\n"
45 "SC := [ [ m1 BPS ] d SEC ] m2 BPS\n"
47 " m1 : slope of first segment\n"
48 " d : x-coordinate of intersection\n"
49 " m2 : slope of second segment\n"
51 "Alternative format:\n"
53 "SC := [ [ umax BYTE ] dmax SEC ] rate BPS\n"
55 " umax : maximum unit of work\n"
56 " dmax : maximum delay\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"
69 fprintf(stderr
, "HFSC: Illegal \"%s\"\n", arg
);
73 hfsc_parse_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
74 struct nlmsghdr
*n
, const char *dev
)
76 struct tc_hfsc_qopt qopt
= {};
79 if (matches(*argv
, "default") == 0) {
81 if (qopt
.defcls
!= 0) {
82 fprintf(stderr
, "HFSC: Double \"default\"\n");
85 if (get_u16(&qopt
.defcls
, *argv
, 16) < 0) {
89 } else if (matches(*argv
, "help") == 0) {
93 fprintf(stderr
, "HFSC: What is \"%s\" ?\n", *argv
);
100 addattr_l(n
, 1024, TCA_OPTIONS
, &qopt
, sizeof(qopt
));
105 hfsc_print_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
107 struct tc_hfsc_qopt
*qopt
;
111 if (RTA_PAYLOAD(opt
) < sizeof(*qopt
))
113 qopt
= RTA_DATA(opt
);
115 if (qopt
->defcls
!= 0)
116 fprintf(f
, "default %x ", qopt
->defcls
);
122 hfsc_print_xstats(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*xstats
)
124 struct tc_hfsc_stats
*st
;
128 if (RTA_PAYLOAD(xstats
) < sizeof(*st
))
130 st
= RTA_DATA(xstats
);
132 fprintf(f
, " period %u ", st
->period
);
134 fprintf(f
, "work %llu bytes ", (unsigned long long) st
->work
);
136 fprintf(f
, "rtwork %llu bytes ", (unsigned long long) st
->rtwork
);
137 fprintf(f
, "level %u ", st
->level
);
144 hfsc_parse_class_opt(struct qdisc_util
*qu
, int argc
, char **argv
,
145 struct nlmsghdr
*n
, const char *dev
)
147 struct tc_service_curve rsc
= {}, fsc
= {}, usc
= {};
148 int rsc_ok
= 0, fsc_ok
= 0, usc_ok
= 0;
152 if (matches(*argv
, "rt") == 0) {
154 if (hfsc_get_sc(&argc
, &argv
, &rsc
, dev
) < 0) {
159 } else if (matches(*argv
, "ls") == 0) {
161 if (hfsc_get_sc(&argc
, &argv
, &fsc
, dev
) < 0) {
166 } else if (matches(*argv
, "sc") == 0) {
168 if (hfsc_get_sc(&argc
, &argv
, &rsc
, dev
) < 0) {
172 memcpy(&fsc
, &rsc
, sizeof(fsc
));
175 } else if (matches(*argv
, "ul") == 0) {
177 if (hfsc_get_sc(&argc
, &argv
, &usc
, dev
) < 0) {
182 } else if (matches(*argv
, "help") == 0) {
186 fprintf(stderr
, "HFSC: What is \"%s\" ?\n", *argv
);
193 if (!(rsc_ok
|| fsc_ok
|| usc_ok
)) {
194 fprintf(stderr
, "HFSC: no parameters given\n");
198 if (usc_ok
&& !fsc_ok
) {
199 fprintf(stderr
, "HFSC: Upper-limit Service Curve without Link-Share Service Curve\n");
204 tail
= NLMSG_TAIL(n
);
206 addattr_l(n
, 1024, TCA_OPTIONS
, NULL
, 0);
208 addattr_l(n
, 1024, TCA_HFSC_RSC
, &rsc
, sizeof(rsc
));
210 addattr_l(n
, 1024, TCA_HFSC_FSC
, &fsc
, sizeof(fsc
));
212 addattr_l(n
, 1024, TCA_HFSC_USC
, &usc
, sizeof(usc
));
214 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
219 hfsc_print_sc(FILE *f
, char *name
, struct tc_service_curve
*sc
)
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
));
230 hfsc_print_class_opt(struct qdisc_util
*qu
, FILE *f
, struct rtattr
*opt
)
232 struct rtattr
*tb
[TCA_HFSC_MAX
+1];
233 struct tc_service_curve
*rsc
= NULL
, *fsc
= NULL
, *usc
= NULL
;
238 parse_rtattr_nested(tb
, TCA_HFSC_MAX
, opt
);
240 if (tb
[TCA_HFSC_RSC
]) {
241 if (RTA_PAYLOAD(tb
[TCA_HFSC_RSC
]) < sizeof(*rsc
))
242 fprintf(stderr
, "HFSC: truncated realtime option\n");
244 rsc
= RTA_DATA(tb
[TCA_HFSC_RSC
]);
246 if (tb
[TCA_HFSC_FSC
]) {
247 if (RTA_PAYLOAD(tb
[TCA_HFSC_FSC
]) < sizeof(*fsc
))
248 fprintf(stderr
, "HFSC: truncated linkshare option\n");
250 fsc
= RTA_DATA(tb
[TCA_HFSC_FSC
]);
252 if (tb
[TCA_HFSC_USC
]) {
253 if (RTA_PAYLOAD(tb
[TCA_HFSC_USC
]) < sizeof(*usc
))
254 fprintf(stderr
, "HFSC: truncated upperlimit option\n");
256 usc
= RTA_DATA(tb
[TCA_HFSC_USC
]);
260 if (rsc
!= NULL
&& fsc
!= NULL
&&
261 memcmp(rsc
, fsc
, sizeof(*rsc
)) == 0)
262 hfsc_print_sc(f
, "sc", rsc
);
265 hfsc_print_sc(f
, "rt", rsc
);
267 hfsc_print_sc(f
, "ls", fsc
);
270 hfsc_print_sc(f
, "ul", usc
);
275 struct qdisc_util hfsc_qdisc_util
= {
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
,
285 hfsc_get_sc1(int *argcp
, char ***argvp
,
286 struct tc_service_curve
*sc
, const char *dev
)
288 char **argv
= *argvp
;
290 unsigned int m1
= 0, d
= 0, m2
= 0;
292 if (matches(*argv
, "m1") == 0) {
294 if (strchr(*argv
, '%')) {
295 if (get_percent_rate(&m1
, *argv
, dev
)) {
299 } else if (get_rate(&m1
, *argv
) < 0) {
306 if (matches(*argv
, "d") == 0) {
308 if (get_time(&d
, *argv
) < 0) {
315 if (matches(*argv
, "m2") == 0) {
317 if (strchr(*argv
, '%')) {
318 if (get_percent_rate(&m2
, *argv
, dev
)) {
322 } else if (get_rate(&m2
, *argv
) < 0) {
330 sc
->d
= tc_core_time2ktime(d
);
339 hfsc_get_sc2(int *argcp
, char ***argvp
, struct tc_service_curve
*sc
, const char *dev
)
341 char **argv
= *argvp
;
343 unsigned int umax
= 0, dmax
= 0, rate
= 0;
345 if (matches(*argv
, "umax") == 0) {
347 if (get_size(&umax
, *argv
) < 0) {
354 if (matches(*argv
, "dmax") == 0) {
356 if (get_time(&dmax
, *argv
) < 0) {
363 if (matches(*argv
, "rate") == 0) {
365 if (strchr(*argv
, '%')) {
366 if (get_percent_rate(&rate
, *argv
, dev
)) {
370 } else if (get_rate(&rate
, *argv
) < 0) {
377 if (umax
!= 0 && dmax
== 0) {
378 fprintf(stderr
, "HFSC: umax given but dmax is zero.\n");
382 if (dmax
!= 0 && ceil(1.0 * umax
* TIME_UNITS_PER_SEC
/ dmax
) > rate
) {
384 * concave curve, slope of first segment is umax/dmax,
385 * intersection is at dmax
387 sc
->m1
= ceil(1.0 * umax
* TIME_UNITS_PER_SEC
/ dmax
); /* in bps */
388 sc
->d
= tc_core_time2ktime(dmax
);
392 * convex curve, slope of first segment is 0, intersection
393 * is at dmax - umax / rate
396 sc
->d
= tc_core_time2ktime(ceil(dmax
- umax
* TIME_UNITS_PER_SEC
/ rate
));
406 hfsc_get_sc(int *argcp
, char ***argvp
, struct tc_service_curve
*sc
, const char *dev
)
408 if (hfsc_get_sc1(argcp
, argvp
, sc
, dev
) < 0 &&
409 hfsc_get_sc2(argcp
, argvp
, sc
, dev
) < 0)
412 if (sc
->m1
== 0 && sc
->m2
== 0) {
413 fprintf(stderr
, "HFSC: Service Curve has two zero slopes\n");