| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* q_choke.c CHOKE. |
| 4 |
* |
| 5 |
* Authors: Stephen Hemminger <[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 |
#include "tc_red.h" |
| 22 |
|
| 23 |
static void explain(void) |
| 24 |
{ |
| 25 |
fprintf(stderr, |
| 26 |
"Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n" |
| 27 |
" [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n"); |
| 28 |
} |
| 29 |
|
| 30 |
static int choke_parse_opt(const struct qdisc_util *qu, int argc, char **argv, |
| 31 |
struct nlmsghdr *n, const char *dev) |
| 32 |
{ |
| 33 |
struct tc_red_qopt opt = {}; |
| 34 |
unsigned int burst = 0; |
| 35 |
unsigned int avpkt = 1000; |
| 36 |
double probability = 0.02; |
| 37 |
unsigned int rate = 0; |
| 38 |
int ecn_ok = 0; |
| 39 |
int wlog; |
| 40 |
__u8 sbuf[256]; |
| 41 |
__u32 max_P; |
| 42 |
struct rtattr *tail; |
| 43 |
|
| 44 |
while (argc > 0) { |
| 45 |
if (strcmp(*argv, "limit") == 0) { |
| 46 |
NEXT_ARG(); |
| 47 |
if (get_unsigned(&opt.limit, *argv, 0)) { |
| 48 |
fprintf(stderr, "Illegal \"limit\"\n"); |
| 49 |
return -1; |
| 50 |
} |
| 51 |
} else if (strcmp(*argv, "bandwidth") == 0) { |
| 52 |
NEXT_ARG(); |
| 53 |
if (strchr(*argv, '%')) { |
| 54 |
if (get_percent_rate(&rate, *argv, dev)) { |
| 55 |
fprintf(stderr, "Illegal \"bandwidth\"\n"); |
| 56 |
return -1; |
| 57 |
} |
| 58 |
} else if (get_rate(&rate, *argv)) { |
| 59 |
fprintf(stderr, "Illegal \"bandwidth\"\n"); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
} else if (strcmp(*argv, "ecn") == 0) { |
| 63 |
ecn_ok = 1; |
| 64 |
} else if (strcmp(*argv, "min") == 0) { |
| 65 |
NEXT_ARG(); |
| 66 |
if (get_unsigned(&opt.qth_min, *argv, 0)) { |
| 67 |
fprintf(stderr, "Illegal \"min\"\n"); |
| 68 |
return -1; |
| 69 |
} |
| 70 |
} else if (strcmp(*argv, "max") == 0) { |
| 71 |
NEXT_ARG(); |
| 72 |
if (get_unsigned(&opt.qth_max, *argv, 0)) { |
| 73 |
fprintf(stderr, "Illegal \"max\"\n"); |
| 74 |
return -1; |
| 75 |
} |
| 76 |
} else if (strcmp(*argv, "burst") == 0) { |
| 77 |
NEXT_ARG(); |
| 78 |
if (get_unsigned(&burst, *argv, 0)) { |
| 79 |
fprintf(stderr, "Illegal \"burst\"\n"); |
| 80 |
return -1; |
| 81 |
} |
| 82 |
} else if (strcmp(*argv, "avpkt") == 0) { |
| 83 |
NEXT_ARG(); |
| 84 |
if (get_size(&avpkt, *argv)) { |
| 85 |
fprintf(stderr, "Illegal \"avpkt\"\n"); |
| 86 |
return -1; |
| 87 |
} |
| 88 |
} else if (strcmp(*argv, "probability") == 0) { |
| 89 |
NEXT_ARG(); |
| 90 |
if (sscanf(*argv, "%lg", &probability) != 1) { |
| 91 |
fprintf(stderr, "Illegal \"probability\"\n"); |
| 92 |
return -1; |
| 93 |
} |
| 94 |
} else if (strcmp(*argv, "help") == 0) { |
| 95 |
explain(); |
| 96 |
return -1; |
| 97 |
} else { |
| 98 |
fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 99 |
explain(); |
| 100 |
return -1; |
| 101 |
} |
| 102 |
argc--; argv++; |
| 103 |
} |
| 104 |
|
| 105 |
if (!rate || !opt.limit) { |
| 106 |
fprintf(stderr, "Required parameter (bandwidth, limit) is missing\n"); |
| 107 |
return -1; |
| 108 |
} |
| 109 |
|
| 110 |
/* Compute default min/max thresholds based on |
| 111 |
Sally Floyd's recommendations: |
| 112 |
http://www.icir.org/floyd/REDparameters.txt |
| 113 |
*/ |
| 114 |
if (!opt.qth_max) |
| 115 |
opt.qth_max = opt.limit / 4; |
| 116 |
if (!opt.qth_min) |
| 117 |
opt.qth_min = opt.qth_max / 3; |
| 118 |
if (!burst) |
| 119 |
burst = (2 * opt.qth_min + opt.qth_max) / 3; |
| 120 |
|
| 121 |
if (opt.qth_max > opt.limit) { |
| 122 |
fprintf(stderr, "\"max\" is larger than \"limit\"\n"); |
| 123 |
return -1; |
| 124 |
} |
| 125 |
|
| 126 |
if (opt.qth_min >= opt.qth_max) { |
| 127 |
fprintf(stderr, "\"min\" is not smaller than \"max\"\n"); |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
wlog = tc_red_eval_ewma(opt.qth_min*avpkt, burst, avpkt); |
| 132 |
if (wlog < 0) { |
| 133 |
fprintf(stderr, "CHOKE: failed to calculate EWMA constant.\n"); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
if (wlog >= 10) |
| 137 |
fprintf(stderr, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst); |
| 138 |
opt.Wlog = wlog; |
| 139 |
|
| 140 |
wlog = tc_red_eval_P(opt.qth_min*avpkt, opt.qth_max*avpkt, probability); |
| 141 |
if (wlog < 0) { |
| 142 |
fprintf(stderr, "CHOKE: failed to calculate probability.\n"); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
opt.Plog = wlog; |
| 146 |
|
| 147 |
wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf); |
| 148 |
if (wlog < 0) { |
| 149 |
fprintf(stderr, "CHOKE: failed to calculate idle damping table.\n"); |
| 150 |
return -1; |
| 151 |
} |
| 152 |
opt.Scell_log = wlog; |
| 153 |
if (ecn_ok) |
| 154 |
opt.flags |= TC_RED_ECN; |
| 155 |
|
| 156 |
tail = addattr_nest(n, 1024, TCA_OPTIONS); |
| 157 |
addattr_l(n, 1024, TCA_CHOKE_PARMS, &opt, sizeof(opt)); |
| 158 |
addattr_l(n, 1024, TCA_CHOKE_STAB, sbuf, 256); |
| 159 |
max_P = probability * pow(2, 32); |
| 160 |
addattr_l(n, 1024, TCA_CHOKE_MAX_P, &max_P, sizeof(max_P)); |
| 161 |
addattr_nest_end(n, tail); |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
static int choke_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
| 166 |
{ |
| 167 |
struct rtattr *tb[TCA_CHOKE_MAX+1]; |
| 168 |
const struct tc_red_qopt *qopt; |
| 169 |
__u32 max_P = 0; |
| 170 |
|
| 171 |
if (opt == NULL) |
| 172 |
return 0; |
| 173 |
|
| 174 |
parse_rtattr_nested(tb, TCA_CHOKE_MAX, opt); |
| 175 |
|
| 176 |
if (tb[TCA_CHOKE_PARMS] == NULL) |
| 177 |
return -1; |
| 178 |
qopt = RTA_DATA(tb[TCA_CHOKE_PARMS]); |
| 179 |
if (RTA_PAYLOAD(tb[TCA_CHOKE_PARMS]) < sizeof(*qopt)) |
| 180 |
return -1; |
| 181 |
if (tb[TCA_CHOKE_MAX_P] && |
| 182 |
RTA_PAYLOAD(tb[TCA_CHOKE_MAX_P]) >= sizeof(__u32)) |
| 183 |
max_P = rta_getattr_u32(tb[TCA_CHOKE_MAX_P]); |
| 184 |
|
| 185 |
print_uint(PRINT_ANY, "limit", "limit %up ", qopt->limit); |
| 186 |
print_uint(PRINT_ANY, "min", "min %up ", qopt->qth_min); |
| 187 |
print_uint(PRINT_ANY, "max", "max %up ", qopt->qth_max); |
| 188 |
|
| 189 |
tc_red_print_flags(qopt->flags); |
| 190 |
|
| 191 |
if (show_details) { |
| 192 |
print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog); |
| 193 |
|
| 194 |
if (max_P) |
| 195 |
print_float(PRINT_ANY, "probability", |
| 196 |
"probability %lg ", max_P / pow(2, 32)); |
| 197 |
else |
| 198 |
print_uint(PRINT_ANY, "Plog", "Plog %u ", qopt->Plog); |
| 199 |
|
| 200 |
print_uint(PRINT_ANY, "Scell_log", "Scell_log %u", |
| 201 |
qopt->Scell_log); |
| 202 |
} |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
static int choke_print_xstats(const struct qdisc_util *qu, FILE *f, |
| 207 |
struct rtattr *xstats) |
| 208 |
{ |
| 209 |
struct tc_choke_xstats *st; |
| 210 |
|
| 211 |
if (xstats == NULL) |
| 212 |
return 0; |
| 213 |
|
| 214 |
if (RTA_PAYLOAD(xstats) < sizeof(*st)) |
| 215 |
return -1; |
| 216 |
|
| 217 |
st = RTA_DATA(xstats); |
| 218 |
|
| 219 |
print_uint(PRINT_ANY, "marked", " marked %u", st->marked); |
| 220 |
print_uint(PRINT_ANY, "early", " early %u", st->early); |
| 221 |
print_uint(PRINT_ANY, "pdrop", " pdrop %u", st->pdrop); |
| 222 |
print_uint(PRINT_ANY, "other", " other %u", st->other); |
| 223 |
print_uint(PRINT_ANY, "matched", " matched %u", st->matched); |
| 224 |
|
| 225 |
return 0; |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
struct qdisc_util choke_qdisc_util = { |
| 230 |
.id = "choke", |
| 231 |
.parse_qopt = choke_parse_opt, |
| 232 |
.print_qopt = choke_print_opt, |
| 233 |
.print_xstats = choke_print_xstats, |
| 234 |
}; |