| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* q_cbs.c CBS. |
| 4 |
* |
| 5 |
* Authors: Vinicius Costa Gomes <[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 |
|
| 17 |
#include "utils.h" |
| 18 |
#include "tc_util.h" |
| 19 |
|
| 20 |
static void explain(void) |
| 21 |
{ |
| 22 |
fprintf(stderr, |
| 23 |
"Usage: ... cbs hicredit BYTES locredit BYTES sendslope BPS idleslope BPS\n" |
| 24 |
" [offload 0|1]\n"); |
| 25 |
} |
| 26 |
|
| 27 |
static void explain1(const char *arg, const char *val) |
| 28 |
{ |
| 29 |
fprintf(stderr, "cbs: illegal value for \"%s\": \"%s\"\n", arg, val); |
| 30 |
} |
| 31 |
|
| 32 |
static int cbs_parse_opt(const struct qdisc_util *qu, int argc, |
| 33 |
char **argv, struct nlmsghdr *n, const char *dev) |
| 34 |
{ |
| 35 |
struct tc_cbs_qopt opt = {}; |
| 36 |
struct rtattr *tail; |
| 37 |
|
| 38 |
while (argc > 0) { |
| 39 |
if (matches(*argv, "offload") == 0) { |
| 40 |
NEXT_ARG(); |
| 41 |
if (opt.offload) { |
| 42 |
fprintf(stderr, "cbs: duplicate \"offload\" specification\n"); |
| 43 |
return -1; |
| 44 |
} |
| 45 |
if (get_u8(&opt.offload, *argv, 0)) { |
| 46 |
explain1("offload", *argv); |
| 47 |
return -1; |
| 48 |
} |
| 49 |
} else if (matches(*argv, "hicredit") == 0) { |
| 50 |
NEXT_ARG(); |
| 51 |
if (opt.hicredit) { |
| 52 |
fprintf(stderr, "cbs: duplicate \"hicredit\" specification\n"); |
| 53 |
return -1; |
| 54 |
} |
| 55 |
if (get_s32(&opt.hicredit, *argv, 0)) { |
| 56 |
explain1("hicredit", *argv); |
| 57 |
return -1; |
| 58 |
} |
| 59 |
} else if (matches(*argv, "locredit") == 0) { |
| 60 |
NEXT_ARG(); |
| 61 |
if (opt.locredit) { |
| 62 |
fprintf(stderr, "cbs: duplicate \"locredit\" specification\n"); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
if (get_s32(&opt.locredit, *argv, 0)) { |
| 66 |
explain1("locredit", *argv); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
} else if (matches(*argv, "sendslope") == 0) { |
| 70 |
NEXT_ARG(); |
| 71 |
if (opt.sendslope) { |
| 72 |
fprintf(stderr, "cbs: duplicate \"sendslope\" specification\n"); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
if (get_s32(&opt.sendslope, *argv, 0)) { |
| 76 |
explain1("sendslope", *argv); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
} else if (matches(*argv, "idleslope") == 0) { |
| 80 |
NEXT_ARG(); |
| 81 |
if (opt.idleslope) { |
| 82 |
fprintf(stderr, "cbs: duplicate \"idleslope\" specification\n"); |
| 83 |
return -1; |
| 84 |
} |
| 85 |
if (get_s32(&opt.idleslope, *argv, 0)) { |
| 86 |
explain1("idleslope", *argv); |
| 87 |
return -1; |
| 88 |
} |
| 89 |
} else if (strcmp(*argv, "help") == 0) { |
| 90 |
explain(); |
| 91 |
return -1; |
| 92 |
} else { |
| 93 |
fprintf(stderr, "cbs: unknown parameter \"%s\"\n", *argv); |
| 94 |
explain(); |
| 95 |
return -1; |
| 96 |
} |
| 97 |
argc--; argv++; |
| 98 |
} |
| 99 |
|
| 100 |
tail = addattr_nest(n, 1024, TCA_OPTIONS); |
| 101 |
addattr_l(n, 2024, TCA_CBS_PARMS, &opt, sizeof(opt)); |
| 102 |
addattr_nest_end(n, tail); |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
static int cbs_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
| 107 |
{ |
| 108 |
struct rtattr *tb[TCA_CBS_MAX+1]; |
| 109 |
struct tc_cbs_qopt *qopt; |
| 110 |
|
| 111 |
if (opt == NULL) |
| 112 |
return 0; |
| 113 |
|
| 114 |
parse_rtattr_nested(tb, TCA_CBS_MAX, opt); |
| 115 |
|
| 116 |
if (tb[TCA_CBS_PARMS] == NULL) |
| 117 |
return -1; |
| 118 |
|
| 119 |
qopt = RTA_DATA(tb[TCA_CBS_PARMS]); |
| 120 |
if (RTA_PAYLOAD(tb[TCA_CBS_PARMS]) < sizeof(*qopt)) |
| 121 |
return -1; |
| 122 |
|
| 123 |
print_int(PRINT_ANY, "hicredit", "hicredit %d ", qopt->hicredit); |
| 124 |
print_int(PRINT_ANY, "locredit", "locredit %d ", qopt->locredit); |
| 125 |
print_int(PRINT_ANY, "sendslope", "sendslope %d ", qopt->sendslope); |
| 126 |
print_int(PRINT_ANY, "idleslope", "idleslope %d ", qopt->idleslope); |
| 127 |
print_int(PRINT_ANY, "offload", "offload %d ", qopt->offload); |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
struct qdisc_util cbs_qdisc_util = { |
| 133 |
.id = "cbs", |
| 134 |
.parse_qopt = cbs_parse_opt, |
| 135 |
.print_qopt = cbs_print_opt, |
| 136 |
}; |