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