| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Author: Stephen Hemminger <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <math.h> |
| 9 |
#include <ctype.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <sys/socket.h> |
| 14 |
#include <netinet/in.h> |
| 15 |
#include <arpa/inet.h> |
| 16 |
#include <string.h> |
| 17 |
#include <errno.h> |
| 18 |
|
| 19 |
#include "utils.h" |
| 20 |
#include "tc_util.h" |
| 21 |
#include "tc_common.h" |
| 22 |
|
| 23 |
static void explain(void) |
| 24 |
{ |
| 25 |
fprintf(stderr, |
| 26 |
"Usage: ... netem [ limit PACKETS ]\n" |
| 27 |
" [ delay TIME [ JITTER [ CORRELATION ] ] ]\n" |
| 28 |
" [ distribution {uniform|normal|pareto|paretonormal} ]\n" |
| 29 |
" [ corrupt PERCENT [ CORRELATION ] ]\n" |
| 30 |
" [ duplicate PERCENT [ CORRELATION ] ]\n" |
| 31 |
" [ loss random PERCENT [ CORRELATION ] ]\n" |
| 32 |
" [ loss state P13 [ P31 [ P32 [ P23 [ P14 ] ] ] ] ]\n" |
| 33 |
" [ loss gemodel PERCENT [ R [ 1-H [ 1-K ] ] ] ]\n" |
| 34 |
" [ seed SEED ]\n" |
| 35 |
" [ ecn ]\n" |
| 36 |
" [ reorder PERCENT [ CORRELATION ] [ gap DISTANCE ] ]\n" |
| 37 |
" [ rate RATE [ PACKETOVERHEAD ] [ CELLSIZE ] [ CELLOVERHEAD ] ]\n" |
| 38 |
" [ slot MIN_DELAY [ MAX_DELAY ] [ packets MAX_PACKETS ] [ bytes MAX_BYTES ] ]\n" |
| 39 |
" [ slot distribution {uniform|normal|pareto|paretonormal|custom}\n" |
| 40 |
" DELAY JITTER [ packets MAX_PACKETS ] [ bytes MAX_BYTES ] ]\n"); |
| 41 |
} |
| 42 |
|
| 43 |
static void explain1(const char *arg) |
| 44 |
{ |
| 45 |
fprintf(stderr, "Illegal \"%s\"\n", arg); |
| 46 |
} |
| 47 |
|
| 48 |
/* Upper bound on size of distribution |
| 49 |
* really (TCA_BUF_MAX - other headers) / sizeof (__s16) |
| 50 |
*/ |
| 51 |
#define MAX_DIST (16*1024) |
| 52 |
|
| 53 |
/* Print values only if they are non-zero */ |
| 54 |
static void __attribute__((format(printf, 2, 0))) |
| 55 |
__print_int_opt(const char *label_json, const char *label_fp, int val) |
| 56 |
{ |
| 57 |
print_int(PRINT_JSON, label_json, NULL, val); |
| 58 |
if (val != 0) |
| 59 |
print_int(PRINT_FP, NULL, label_fp, val); |
| 60 |
} |
| 61 |
#define PRINT_INT_OPT(label, val) \ |
| 62 |
__print_int_opt(label, " " label " %d", (val)) |
| 63 |
|
| 64 |
/* Time print prints normally with varying units, but for JSON prints |
| 65 |
* in seconds (1ms vs 0.001). |
| 66 |
*/ |
| 67 |
static void __attribute__((format(printf, 2, 0))) |
| 68 |
__print_time64(const char *label_json, const char *label_fp, __u64 val) |
| 69 |
{ |
| 70 |
SPRINT_BUF(b1); |
| 71 |
|
| 72 |
print_string(PRINT_FP, NULL, label_fp, sprint_time64(val, b1)); |
| 73 |
print_float(PRINT_JSON, label_json, NULL, val / 1000000000.); |
| 74 |
} |
| 75 |
#define __PRINT_TIME64(label_json, label_fp, val) \ |
| 76 |
__print_time64(label_json, label_fp " %s", (val)) |
| 77 |
#define PRINT_TIME64(label, val) __PRINT_TIME64(label, " " label, (val)) |
| 78 |
|
| 79 |
/* Percent print prints normally in percentage points, but for JSON prints |
| 80 |
* an absolute value (1% vs 0.01). |
| 81 |
*/ |
| 82 |
static void __attribute__((format(printf, 2, 0))) |
| 83 |
__print_percent(const char *label_json, const char *label_fp, __u32 per) |
| 84 |
{ |
| 85 |
print_float(PRINT_FP, NULL, label_fp, (100. * per) / UINT32_MAX); |
| 86 |
print_float(PRINT_JSON, label_json, NULL, (1. * per) / UINT32_MAX); |
| 87 |
} |
| 88 |
#define __PRINT_PERCENT(label_json, label_fp, per) \ |
| 89 |
__print_percent(label_json, label_fp " %g%%", (per)) |
| 90 |
#define PRINT_PERCENT(label, per) __PRINT_PERCENT(label, " " label, (per)) |
| 91 |
|
| 92 |
/* scaled value used to percent of maximum. */ |
| 93 |
static void set_percent(__u32 *percent, double per) |
| 94 |
{ |
| 95 |
*percent = rint(per * UINT32_MAX); |
| 96 |
} |
| 97 |
|
| 98 |
static int get_percent(__u32 *percent, const char *str) |
| 99 |
{ |
| 100 |
double per; |
| 101 |
|
| 102 |
if (parse_percent(&per, str)) |
| 103 |
return -1; |
| 104 |
|
| 105 |
set_percent(percent, per); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
static void print_corr(bool present, __u32 value) |
| 110 |
{ |
| 111 |
if (!is_json_context()) { |
| 112 |
if (present) |
| 113 |
__PRINT_PERCENT("", "", value); |
| 114 |
} else { |
| 115 |
PRINT_PERCENT("correlation", value); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/* |
| 120 |
* Simplistic file parser for distribution data. |
| 121 |
* Format is: |
| 122 |
* # comment line(s) |
| 123 |
* data0 data1 ... |
| 124 |
*/ |
| 125 |
static int get_distribution(const char *type, __s16 *data, int maxdata) |
| 126 |
{ |
| 127 |
FILE *f; |
| 128 |
int n; |
| 129 |
long x; |
| 130 |
size_t len; |
| 131 |
char *line = NULL; |
| 132 |
char name[128]; |
| 133 |
|
| 134 |
snprintf(name, sizeof(name), "%s/%s.dist", get_tc_lib(), type); |
| 135 |
f = fopen(name, "r"); |
| 136 |
if (f == NULL) { |
| 137 |
fprintf(stderr, "No distribution data for %s (%s: %s)\n", |
| 138 |
type, name, strerror(errno)); |
| 139 |
return -1; |
| 140 |
} |
| 141 |
|
| 142 |
n = 0; |
| 143 |
while (getline(&line, &len, f) != -1) { |
| 144 |
char *p, *endp; |
| 145 |
|
| 146 |
if (*line == '\n' || *line == '#') |
| 147 |
continue; |
| 148 |
|
| 149 |
for (p = line; ; p = endp) { |
| 150 |
x = strtol(p, &endp, 0); |
| 151 |
if (endp == p) |
| 152 |
break; |
| 153 |
|
| 154 |
if (n >= maxdata) { |
| 155 |
fprintf(stderr, "%s: too much data\n", |
| 156 |
name); |
| 157 |
n = -1; |
| 158 |
goto error; |
| 159 |
} |
| 160 |
data[n++] = x; |
| 161 |
} |
| 162 |
} |
| 163 |
error: |
| 164 |
free(line); |
| 165 |
fclose(f); |
| 166 |
return n; |
| 167 |
} |
| 168 |
|
| 169 |
#define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isdigit(argv[1][0])) |
| 170 |
#define NEXT_IS_SIGNED_NUMBER() \ |
| 171 |
(NEXT_ARG_OK() && (isdigit(argv[1][0]) || argv[1][0] == '-')) |
| 172 |
|
| 173 |
|
| 174 |
static int netem_parse_opt(const struct qdisc_util *qu, int argc, char **argv, |
| 175 |
struct nlmsghdr *n, const char *dev) |
| 176 |
{ |
| 177 |
int dist_size = 0; |
| 178 |
int slot_dist_size = 0; |
| 179 |
struct rtattr *tail; |
| 180 |
struct tc_netem_qopt opt = { .limit = 1000 }; |
| 181 |
struct tc_netem_corr cor = {}; |
| 182 |
struct tc_netem_reorder reorder = {}; |
| 183 |
struct tc_netem_corrupt corrupt = {}; |
| 184 |
struct tc_netem_gimodel gimodel; |
| 185 |
struct tc_netem_gemodel gemodel; |
| 186 |
struct tc_netem_rate rate = {}; |
| 187 |
struct tc_netem_slot slot = {}; |
| 188 |
__s16 *dist_data = NULL; |
| 189 |
__s16 *slot_dist_data = NULL; |
| 190 |
__u16 loss_type = NETEM_LOSS_UNSPEC; |
| 191 |
int present[__TCA_NETEM_MAX] = {}; |
| 192 |
__s64 latency64 = 0; |
| 193 |
__s64 jitter64 = 0; |
| 194 |
__u64 rate64 = 0; |
| 195 |
__u64 seed = 0; |
| 196 |
|
| 197 |
for ( ; argc > 0; --argc, ++argv) { |
| 198 |
if (matches(*argv, "limit") == 0) { |
| 199 |
NEXT_ARG(); |
| 200 |
if (get_size(&opt.limit, *argv)) { |
| 201 |
explain1("limit"); |
| 202 |
return -1; |
| 203 |
} |
| 204 |
} else if (matches(*argv, "latency") == 0 || |
| 205 |
matches(*argv, "delay") == 0) { |
| 206 |
NEXT_ARG(); |
| 207 |
|
| 208 |
/* Old latency value in opt is no longer used. */ |
| 209 |
present[TCA_NETEM_LATENCY64] = 1; |
| 210 |
|
| 211 |
if (get_time64(&latency64, *argv)) { |
| 212 |
explain1("latency"); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
|
| 216 |
if (NEXT_IS_NUMBER()) { |
| 217 |
NEXT_ARG(); |
| 218 |
|
| 219 |
present[TCA_NETEM_JITTER64] = 1; |
| 220 |
if (get_time64(&jitter64, *argv)) { |
| 221 |
explain1("latency"); |
| 222 |
return -1; |
| 223 |
} |
| 224 |
|
| 225 |
if (NEXT_IS_NUMBER()) { |
| 226 |
NEXT_ARG(); |
| 227 |
++present[TCA_NETEM_CORR]; |
| 228 |
if (get_percent(&cor.delay_corr, *argv)) { |
| 229 |
explain1("latency"); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} else if (matches(*argv, "loss") == 0 || |
| 235 |
matches(*argv, "drop") == 0) { |
| 236 |
if (opt.loss > 0 || loss_type != NETEM_LOSS_UNSPEC) { |
| 237 |
explain1("duplicate loss argument\n"); |
| 238 |
return -1; |
| 239 |
} |
| 240 |
|
| 241 |
NEXT_ARG(); |
| 242 |
/* Old (deprecated) random loss model syntax */ |
| 243 |
if (isdigit(argv[0][0])) |
| 244 |
goto random_loss_model; |
| 245 |
|
| 246 |
if (!strcmp(*argv, "random")) { |
| 247 |
NEXT_ARG(); |
| 248 |
random_loss_model: |
| 249 |
if (get_percent(&opt.loss, *argv)) { |
| 250 |
explain1("loss percent"); |
| 251 |
return -1; |
| 252 |
} |
| 253 |
if (NEXT_IS_NUMBER()) { |
| 254 |
NEXT_ARG(); |
| 255 |
++present[TCA_NETEM_CORR]; |
| 256 |
if (get_percent(&cor.loss_corr, *argv)) { |
| 257 |
explain1("loss correlation"); |
| 258 |
return -1; |
| 259 |
} |
| 260 |
} |
| 261 |
} else if (!strcmp(*argv, "state")) { |
| 262 |
double p13; |
| 263 |
|
| 264 |
NEXT_ARG(); |
| 265 |
if (parse_percent(&p13, *argv)) { |
| 266 |
explain1("loss p13"); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
/* set defaults */ |
| 271 |
set_percent(&gimodel.p13, p13); |
| 272 |
set_percent(&gimodel.p31, 1. - p13); |
| 273 |
set_percent(&gimodel.p32, 0); |
| 274 |
set_percent(&gimodel.p23, 1.); |
| 275 |
set_percent(&gimodel.p14, 0); |
| 276 |
loss_type = NETEM_LOSS_GI; |
| 277 |
|
| 278 |
if (!NEXT_IS_NUMBER()) |
| 279 |
continue; |
| 280 |
NEXT_ARG(); |
| 281 |
if (get_percent(&gimodel.p31, *argv)) { |
| 282 |
explain1("loss p31"); |
| 283 |
return -1; |
| 284 |
} |
| 285 |
|
| 286 |
if (!NEXT_IS_NUMBER()) |
| 287 |
continue; |
| 288 |
NEXT_ARG(); |
| 289 |
if (get_percent(&gimodel.p32, *argv)) { |
| 290 |
explain1("loss p32"); |
| 291 |
return -1; |
| 292 |
} |
| 293 |
|
| 294 |
if (!NEXT_IS_NUMBER()) |
| 295 |
continue; |
| 296 |
NEXT_ARG(); |
| 297 |
if (get_percent(&gimodel.p23, *argv)) { |
| 298 |
explain1("loss p23"); |
| 299 |
return -1; |
| 300 |
} |
| 301 |
if (!NEXT_IS_NUMBER()) |
| 302 |
continue; |
| 303 |
NEXT_ARG(); |
| 304 |
if (get_percent(&gimodel.p14, *argv)) { |
| 305 |
explain1("loss p14"); |
| 306 |
return -1; |
| 307 |
} |
| 308 |
|
| 309 |
} else if (!strcmp(*argv, "gemodel")) { |
| 310 |
double p; |
| 311 |
|
| 312 |
NEXT_ARG(); |
| 313 |
if (parse_percent(&p, *argv)) { |
| 314 |
explain1("loss gemodel p"); |
| 315 |
return -1; |
| 316 |
} |
| 317 |
set_percent(&gemodel.p, p); |
| 318 |
|
| 319 |
/* set defaults */ |
| 320 |
set_percent(&gemodel.r, 1. - p); |
| 321 |
set_percent(&gemodel.h, 0); |
| 322 |
set_percent(&gemodel.k1, 0); |
| 323 |
loss_type = NETEM_LOSS_GE; |
| 324 |
|
| 325 |
if (!NEXT_IS_NUMBER()) |
| 326 |
continue; |
| 327 |
NEXT_ARG(); |
| 328 |
if (get_percent(&gemodel.r, *argv)) { |
| 329 |
explain1("loss gemodel r"); |
| 330 |
return -1; |
| 331 |
} |
| 332 |
|
| 333 |
if (!NEXT_IS_NUMBER()) |
| 334 |
continue; |
| 335 |
NEXT_ARG(); |
| 336 |
if (get_percent(&gemodel.h, *argv)) { |
| 337 |
explain1("loss gemodel h"); |
| 338 |
return -1; |
| 339 |
} |
| 340 |
/* netem option is "1-h" but kernel |
| 341 |
* expects "h". |
| 342 |
*/ |
| 343 |
gemodel.h = UINT32_MAX - gemodel.h; |
| 344 |
|
| 345 |
if (!NEXT_IS_NUMBER()) |
| 346 |
continue; |
| 347 |
NEXT_ARG(); |
| 348 |
if (get_percent(&gemodel.k1, *argv)) { |
| 349 |
explain1("loss gemodel k"); |
| 350 |
return -1; |
| 351 |
} |
| 352 |
} else { |
| 353 |
fprintf(stderr, "Unknown loss parameter: %s\n", |
| 354 |
*argv); |
| 355 |
return -1; |
| 356 |
} |
| 357 |
} else if (matches(*argv, "seed") == 0) { |
| 358 |
NEXT_ARG(); |
| 359 |
present[TCA_NETEM_PRNG_SEED] = 1; |
| 360 |
if (get_u64(&seed, *argv, 10)) { |
| 361 |
explain1("seed"); |
| 362 |
return -1; |
| 363 |
} |
| 364 |
} else if (matches(*argv, "ecn") == 0) { |
| 365 |
present[TCA_NETEM_ECN] = 1; |
| 366 |
} else if (matches(*argv, "reorder") == 0) { |
| 367 |
NEXT_ARG(); |
| 368 |
present[TCA_NETEM_REORDER] = 1; |
| 369 |
if (get_percent(&reorder.probability, *argv)) { |
| 370 |
explain1("reorder"); |
| 371 |
return -1; |
| 372 |
} |
| 373 |
if (NEXT_IS_NUMBER()) { |
| 374 |
NEXT_ARG(); |
| 375 |
++present[TCA_NETEM_CORR]; |
| 376 |
if (get_percent(&reorder.correlation, *argv)) { |
| 377 |
explain1("reorder"); |
| 378 |
return -1; |
| 379 |
} |
| 380 |
} |
| 381 |
} else if (matches(*argv, "corrupt") == 0) { |
| 382 |
NEXT_ARG(); |
| 383 |
present[TCA_NETEM_CORRUPT] = 1; |
| 384 |
if (get_percent(&corrupt.probability, *argv)) { |
| 385 |
explain1("corrupt"); |
| 386 |
return -1; |
| 387 |
} |
| 388 |
if (NEXT_IS_NUMBER()) { |
| 389 |
NEXT_ARG(); |
| 390 |
++present[TCA_NETEM_CORR]; |
| 391 |
if (get_percent(&corrupt.correlation, *argv)) { |
| 392 |
explain1("corrupt"); |
| 393 |
return -1; |
| 394 |
} |
| 395 |
} |
| 396 |
} else if (matches(*argv, "gap") == 0) { |
| 397 |
NEXT_ARG(); |
| 398 |
if (get_u32(&opt.gap, *argv, 0)) { |
| 399 |
explain1("gap"); |
| 400 |
return -1; |
| 401 |
} |
| 402 |
} else if (matches(*argv, "duplicate") == 0) { |
| 403 |
NEXT_ARG(); |
| 404 |
if (get_percent(&opt.duplicate, *argv)) { |
| 405 |
explain1("duplicate"); |
| 406 |
return -1; |
| 407 |
} |
| 408 |
if (NEXT_IS_NUMBER()) { |
| 409 |
NEXT_ARG(); |
| 410 |
if (get_percent(&cor.dup_corr, *argv)) { |
| 411 |
explain1("duplicate"); |
| 412 |
return -1; |
| 413 |
} |
| 414 |
} |
| 415 |
} else if (matches(*argv, "distribution") == 0) { |
| 416 |
NEXT_ARG(); |
| 417 |
dist_data = calloc(MAX_DIST, sizeof(dist_data[0])); |
| 418 |
if (dist_data == NULL) |
| 419 |
return -1; |
| 420 |
|
| 421 |
dist_size = get_distribution(*argv, dist_data, MAX_DIST); |
| 422 |
if (dist_size <= 0) { |
| 423 |
free(dist_data); |
| 424 |
return -1; |
| 425 |
} |
| 426 |
} else if (matches(*argv, "rate") == 0) { |
| 427 |
++present[TCA_NETEM_RATE]; |
| 428 |
NEXT_ARG(); |
| 429 |
if (strchr(*argv, '%')) { |
| 430 |
if (get_percent_rate64(&rate64, *argv, dev)) { |
| 431 |
explain1("rate"); |
| 432 |
return -1; |
| 433 |
} |
| 434 |
} else if (get_rate64(&rate64, *argv)) { |
| 435 |
explain1("rate"); |
| 436 |
return -1; |
| 437 |
} |
| 438 |
if (NEXT_IS_SIGNED_NUMBER()) { |
| 439 |
NEXT_ARG(); |
| 440 |
if (get_s32(&rate.packet_overhead, *argv, 0)) { |
| 441 |
explain1("rate"); |
| 442 |
return -1; |
| 443 |
} |
| 444 |
} |
| 445 |
if (NEXT_IS_NUMBER()) { |
| 446 |
NEXT_ARG(); |
| 447 |
if (get_u32(&rate.cell_size, *argv, 0)) { |
| 448 |
explain1("rate"); |
| 449 |
return -1; |
| 450 |
} |
| 451 |
} |
| 452 |
if (NEXT_IS_SIGNED_NUMBER()) { |
| 453 |
NEXT_ARG(); |
| 454 |
if (get_s32(&rate.cell_overhead, *argv, 0)) { |
| 455 |
explain1("rate"); |
| 456 |
return -1; |
| 457 |
} |
| 458 |
} |
| 459 |
} else if (matches(*argv, "slot") == 0) { |
| 460 |
if (NEXT_IS_NUMBER()) { |
| 461 |
NEXT_ARG(); |
| 462 |
present[TCA_NETEM_SLOT] = 1; |
| 463 |
if (get_time64(&slot.min_delay, *argv)) { |
| 464 |
explain1("slot min_delay"); |
| 465 |
return -1; |
| 466 |
} |
| 467 |
if (NEXT_IS_NUMBER()) { |
| 468 |
NEXT_ARG(); |
| 469 |
if (get_time64(&slot.max_delay, *argv) || |
| 470 |
slot.max_delay < slot.min_delay) { |
| 471 |
explain1("slot max_delay"); |
| 472 |
return -1; |
| 473 |
} |
| 474 |
} else { |
| 475 |
slot.max_delay = slot.min_delay; |
| 476 |
} |
| 477 |
} else { |
| 478 |
NEXT_ARG(); |
| 479 |
if (strcmp(*argv, "distribution") == 0) { |
| 480 |
present[TCA_NETEM_SLOT] = 1; |
| 481 |
NEXT_ARG(); |
| 482 |
slot_dist_data = calloc(MAX_DIST, sizeof(slot_dist_data[0])); |
| 483 |
if (!slot_dist_data) |
| 484 |
return -1; |
| 485 |
slot_dist_size = get_distribution(*argv, slot_dist_data, MAX_DIST); |
| 486 |
if (slot_dist_size <= 0) { |
| 487 |
free(slot_dist_data); |
| 488 |
return -1; |
| 489 |
} |
| 490 |
NEXT_ARG(); |
| 491 |
if (get_time64(&slot.dist_delay, *argv)) { |
| 492 |
explain1("slot delay"); |
| 493 |
return -1; |
| 494 |
} |
| 495 |
NEXT_ARG(); |
| 496 |
if (get_time64(&slot.dist_jitter, *argv)) { |
| 497 |
explain1("slot jitter"); |
| 498 |
return -1; |
| 499 |
} |
| 500 |
if (slot.dist_jitter <= 0) { |
| 501 |
fprintf(stderr, "Non-positive jitter\n"); |
| 502 |
return -1; |
| 503 |
} |
| 504 |
} else { |
| 505 |
fprintf(stderr, "Unknown slot parameter: %s\n", |
| 506 |
*argv); |
| 507 |
return -1; |
| 508 |
} |
| 509 |
} |
| 510 |
if (NEXT_ARG_OK() && |
| 511 |
matches(*(argv+1), "packets") == 0) { |
| 512 |
NEXT_ARG(); |
| 513 |
if (!NEXT_ARG_OK() || |
| 514 |
get_s32(&slot.max_packets, *(argv+1), 0)) { |
| 515 |
explain1("slot packets"); |
| 516 |
return -1; |
| 517 |
} |
| 518 |
NEXT_ARG(); |
| 519 |
} |
| 520 |
if (NEXT_ARG_OK() && |
| 521 |
matches(*(argv+1), "bytes") == 0) { |
| 522 |
unsigned int max_bytes; |
| 523 |
|
| 524 |
NEXT_ARG(); |
| 525 |
if (!NEXT_ARG_OK() || |
| 526 |
get_size(&max_bytes, *(argv+1))) { |
| 527 |
explain1("slot bytes"); |
| 528 |
return -1; |
| 529 |
} |
| 530 |
slot.max_bytes = (int) max_bytes; |
| 531 |
NEXT_ARG(); |
| 532 |
} |
| 533 |
} else { |
| 534 |
if (strcmp(*argv, "help") != 0) |
| 535 |
fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 536 |
explain(); |
| 537 |
return -1; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
tail = NLMSG_TAIL(n); |
| 542 |
|
| 543 |
if (reorder.probability) { |
| 544 |
if (latency64 == 0) { |
| 545 |
fprintf(stderr, "reordering not possible without specifying some delay\n"); |
| 546 |
explain(); |
| 547 |
return -1; |
| 548 |
} |
| 549 |
if (opt.gap == 0) |
| 550 |
opt.gap = 1; |
| 551 |
} else if (opt.gap > 0) { |
| 552 |
fprintf(stderr, "gap specified without reorder probability\n"); |
| 553 |
explain(); |
| 554 |
return -1; |
| 555 |
} |
| 556 |
|
| 557 |
if (present[TCA_NETEM_ECN]) { |
| 558 |
if (opt.loss <= 0 && loss_type == NETEM_LOSS_UNSPEC) { |
| 559 |
fprintf(stderr, "ecn requested without loss model\n"); |
| 560 |
explain(); |
| 561 |
return -1; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
if (dist_data && (latency64 == 0 || jitter64 == 0)) { |
| 566 |
fprintf(stderr, "distribution specified but no latency and jitter values\n"); |
| 567 |
explain(); |
| 568 |
return -1; |
| 569 |
} |
| 570 |
|
| 571 |
if (addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)) < 0) |
| 572 |
return -1; |
| 573 |
|
| 574 |
if (present[TCA_NETEM_LATENCY64] && |
| 575 |
addattr_l(n, 1024, TCA_NETEM_LATENCY64, &latency64, sizeof(latency64)) < 0) |
| 576 |
return -1; |
| 577 |
|
| 578 |
if (present[TCA_NETEM_JITTER64] && |
| 579 |
addattr_l(n, 1024, TCA_NETEM_JITTER64, &jitter64, sizeof(jitter64)) < 0) |
| 580 |
return -1; |
| 581 |
|
| 582 |
if (present[TCA_NETEM_CORR] && |
| 583 |
addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0) |
| 584 |
return -1; |
| 585 |
|
| 586 |
if (present[TCA_NETEM_REORDER] && |
| 587 |
addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0) |
| 588 |
return -1; |
| 589 |
|
| 590 |
if (present[TCA_NETEM_ECN] && |
| 591 |
addattr_l(n, 1024, TCA_NETEM_ECN, &present[TCA_NETEM_ECN], |
| 592 |
sizeof(present[TCA_NETEM_ECN])) < 0) |
| 593 |
return -1; |
| 594 |
|
| 595 |
if (present[TCA_NETEM_CORRUPT] && |
| 596 |
addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0) |
| 597 |
return -1; |
| 598 |
|
| 599 |
if (present[TCA_NETEM_SLOT] && |
| 600 |
addattr_l(n, 1024, TCA_NETEM_SLOT, &slot, sizeof(slot)) < 0) |
| 601 |
return -1; |
| 602 |
|
| 603 |
if (loss_type != NETEM_LOSS_UNSPEC) { |
| 604 |
struct rtattr *start; |
| 605 |
|
| 606 |
start = addattr_nest(n, 1024, TCA_NETEM_LOSS | NLA_F_NESTED); |
| 607 |
if (loss_type == NETEM_LOSS_GI) { |
| 608 |
if (addattr_l(n, 1024, NETEM_LOSS_GI, |
| 609 |
&gimodel, sizeof(gimodel)) < 0) |
| 610 |
return -1; |
| 611 |
} else if (loss_type == NETEM_LOSS_GE) { |
| 612 |
if (addattr_l(n, 1024, NETEM_LOSS_GE, |
| 613 |
&gemodel, sizeof(gemodel)) < 0) |
| 614 |
return -1; |
| 615 |
} else { |
| 616 |
fprintf(stderr, "loss in the weeds!\n"); |
| 617 |
return -1; |
| 618 |
} |
| 619 |
|
| 620 |
addattr_nest_end(n, start); |
| 621 |
} |
| 622 |
|
| 623 |
if (present[TCA_NETEM_RATE]) { |
| 624 |
if (rate64 >= (1ULL << 32)) { |
| 625 |
if (addattr_l(n, 1024, |
| 626 |
TCA_NETEM_RATE64, &rate64, sizeof(rate64)) < 0) |
| 627 |
return -1; |
| 628 |
rate.rate = ~0U; |
| 629 |
} else { |
| 630 |
rate.rate = rate64; |
| 631 |
} |
| 632 |
if (addattr_l(n, 1024, TCA_NETEM_RATE, &rate, sizeof(rate)) < 0) |
| 633 |
return -1; |
| 634 |
} |
| 635 |
|
| 636 |
if (present[TCA_NETEM_PRNG_SEED] && |
| 637 |
addattr_l(n, 1024, TCA_NETEM_PRNG_SEED, &seed, |
| 638 |
sizeof(seed)) < 0) |
| 639 |
return -1; |
| 640 |
|
| 641 |
|
| 642 |
if (dist_data) { |
| 643 |
if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]), |
| 644 |
TCA_NETEM_DELAY_DIST, |
| 645 |
dist_data, dist_size * sizeof(dist_data[0])) < 0) |
| 646 |
return -1; |
| 647 |
free(dist_data); |
| 648 |
} |
| 649 |
|
| 650 |
if (slot_dist_data) { |
| 651 |
if (addattr_l(n, MAX_DIST * sizeof(slot_dist_data[0]), |
| 652 |
TCA_NETEM_SLOT_DIST, |
| 653 |
slot_dist_data, slot_dist_size * sizeof(slot_dist_data[0])) < 0) |
| 654 |
return -1; |
| 655 |
free(slot_dist_data); |
| 656 |
} |
| 657 |
tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
static int netem_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
| 662 |
{ |
| 663 |
const struct tc_netem_corr *cor = NULL; |
| 664 |
const struct tc_netem_reorder *reorder = NULL; |
| 665 |
const struct tc_netem_corrupt *corrupt = NULL; |
| 666 |
const struct tc_netem_gimodel *gimodel = NULL; |
| 667 |
const struct tc_netem_gemodel *gemodel = NULL; |
| 668 |
int *ecn = NULL; |
| 669 |
struct tc_netem_qopt qopt; |
| 670 |
const struct tc_netem_rate *rate = NULL; |
| 671 |
const struct tc_netem_slot *slot = NULL; |
| 672 |
bool seed_present = false; |
| 673 |
__u64 seed = 0; |
| 674 |
int len; |
| 675 |
__u64 rate64 = 0; |
| 676 |
__u64 latency64 = 0; |
| 677 |
__u64 jitter64 = 0; |
| 678 |
|
| 679 |
SPRINT_BUF(b1); |
| 680 |
|
| 681 |
if (opt == NULL) |
| 682 |
return 0; |
| 683 |
|
| 684 |
len = RTA_PAYLOAD(opt) - sizeof(qopt); |
| 685 |
if (len < 0) { |
| 686 |
fprintf(stderr, "options size error\n"); |
| 687 |
return -1; |
| 688 |
} |
| 689 |
memcpy(&qopt, RTA_DATA(opt), sizeof(qopt)); |
| 690 |
|
| 691 |
if (len > 0) { |
| 692 |
struct rtattr *tb[TCA_NETEM_MAX+1]; |
| 693 |
|
| 694 |
parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt), |
| 695 |
len); |
| 696 |
|
| 697 |
if (tb[TCA_NETEM_CORR]) { |
| 698 |
if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor)) |
| 699 |
return -1; |
| 700 |
cor = RTA_DATA(tb[TCA_NETEM_CORR]); |
| 701 |
} |
| 702 |
if (tb[TCA_NETEM_REORDER]) { |
| 703 |
if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder)) |
| 704 |
return -1; |
| 705 |
reorder = RTA_DATA(tb[TCA_NETEM_REORDER]); |
| 706 |
} |
| 707 |
if (tb[TCA_NETEM_CORRUPT]) { |
| 708 |
if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt)) |
| 709 |
return -1; |
| 710 |
corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]); |
| 711 |
} |
| 712 |
if (tb[TCA_NETEM_LOSS]) { |
| 713 |
struct rtattr *lb[NETEM_LOSS_MAX + 1]; |
| 714 |
|
| 715 |
parse_rtattr_nested(lb, NETEM_LOSS_MAX, tb[TCA_NETEM_LOSS]); |
| 716 |
if (lb[NETEM_LOSS_GI]) |
| 717 |
gimodel = RTA_DATA(lb[NETEM_LOSS_GI]); |
| 718 |
if (lb[NETEM_LOSS_GE]) |
| 719 |
gemodel = RTA_DATA(lb[NETEM_LOSS_GE]); |
| 720 |
} |
| 721 |
if (tb[TCA_NETEM_RATE]) { |
| 722 |
if (RTA_PAYLOAD(tb[TCA_NETEM_RATE]) < sizeof(*rate)) |
| 723 |
return -1; |
| 724 |
rate = RTA_DATA(tb[TCA_NETEM_RATE]); |
| 725 |
} |
| 726 |
if (tb[TCA_NETEM_ECN]) { |
| 727 |
if (RTA_PAYLOAD(tb[TCA_NETEM_ECN]) < sizeof(*ecn)) |
| 728 |
return -1; |
| 729 |
ecn = RTA_DATA(tb[TCA_NETEM_ECN]); |
| 730 |
} |
| 731 |
if (tb[TCA_NETEM_RATE64]) { |
| 732 |
if (RTA_PAYLOAD(tb[TCA_NETEM_RATE64]) < sizeof(rate64)) |
| 733 |
return -1; |
| 734 |
rate64 = rta_getattr_u64(tb[TCA_NETEM_RATE64]); |
| 735 |
} |
| 736 |
if (tb[TCA_NETEM_LATENCY64]) { |
| 737 |
if (RTA_PAYLOAD(tb[TCA_NETEM_LATENCY64]) < sizeof(latency64)) |
| 738 |
return -1; |
| 739 |
latency64 = rta_getattr_u64(tb[TCA_NETEM_LATENCY64]); |
| 740 |
|
| 741 |
} |
| 742 |
if (tb[TCA_NETEM_JITTER64]) { |
| 743 |
if (RTA_PAYLOAD(tb[TCA_NETEM_JITTER64]) < sizeof(jitter64)) |
| 744 |
return -1; |
| 745 |
jitter64 = rta_getattr_u64(tb[TCA_NETEM_JITTER64]); |
| 746 |
|
| 747 |
} |
| 748 |
if (tb[TCA_NETEM_SLOT]) { |
| 749 |
if (RTA_PAYLOAD(tb[TCA_NETEM_SLOT]) < sizeof(*slot)) |
| 750 |
return -1; |
| 751 |
slot = RTA_DATA(tb[TCA_NETEM_SLOT]); |
| 752 |
} |
| 753 |
if (tb[TCA_NETEM_PRNG_SEED]) { |
| 754 |
if (RTA_PAYLOAD(tb[TCA_NETEM_PRNG_SEED]) < sizeof(seed)) |
| 755 |
return -1; |
| 756 |
seed_present = true; |
| 757 |
seed = rta_getattr_u64(tb[TCA_NETEM_PRNG_SEED]); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
print_uint(PRINT_ANY, "limit", "limit %d", qopt.limit); |
| 762 |
|
| 763 |
|
| 764 |
if (latency64 != 0) { |
| 765 |
open_json_object("delay"); |
| 766 |
|
| 767 |
if (is_json_context()) { |
| 768 |
print_float(PRINT_JSON, "delay", NULL, |
| 769 |
(double)latency64 / 1000000000.); |
| 770 |
print_float(PRINT_JSON, "jitter", NULL, |
| 771 |
(double)jitter64 / 1000000000.); |
| 772 |
} else { |
| 773 |
print_string(PRINT_FP, NULL, " delay %s", |
| 774 |
sprint_time64(latency64, b1)); |
| 775 |
if (jitter64 != 0) |
| 776 |
print_string(PRINT_FP, NULL, " %s", |
| 777 |
sprint_time64(jitter64, b1)); |
| 778 |
} |
| 779 |
print_corr(jitter64 && cor && cor->delay_corr, |
| 780 |
cor ? cor->delay_corr : 0); |
| 781 |
close_json_object(); |
| 782 |
} |
| 783 |
|
| 784 |
if (qopt.loss) { |
| 785 |
open_json_object("loss-random"); |
| 786 |
PRINT_PERCENT("loss", qopt.loss); |
| 787 |
print_corr(cor && cor->loss_corr, cor ? cor->loss_corr : 0); |
| 788 |
close_json_object(); |
| 789 |
} |
| 790 |
|
| 791 |
if (gimodel) { |
| 792 |
open_json_object("loss-state"); |
| 793 |
__PRINT_PERCENT("p13", " loss state p13", gimodel->p13); |
| 794 |
PRINT_PERCENT("p31", gimodel->p31); |
| 795 |
PRINT_PERCENT("p32", gimodel->p32); |
| 796 |
PRINT_PERCENT("p23", gimodel->p23); |
| 797 |
PRINT_PERCENT("p14", gimodel->p14); |
| 798 |
close_json_object(); |
| 799 |
} |
| 800 |
|
| 801 |
if (gemodel) { |
| 802 |
open_json_object("loss-gemodel"); |
| 803 |
__PRINT_PERCENT("p", " loss gemodel p", gemodel->p); |
| 804 |
PRINT_PERCENT("r", gemodel->r); |
| 805 |
PRINT_PERCENT("1-h", UINT32_MAX - gemodel->h); |
| 806 |
PRINT_PERCENT("1-k", gemodel->k1); |
| 807 |
close_json_object(); |
| 808 |
} |
| 809 |
|
| 810 |
if (qopt.duplicate) { |
| 811 |
open_json_object("duplicate"); |
| 812 |
PRINT_PERCENT("duplicate", qopt.duplicate); |
| 813 |
print_corr(cor && cor->dup_corr, cor ? cor->dup_corr : 0); |
| 814 |
close_json_object(); |
| 815 |
} |
| 816 |
|
| 817 |
if (reorder && reorder->probability) { |
| 818 |
open_json_object("reorder"); |
| 819 |
PRINT_PERCENT("reorder", reorder->probability); |
| 820 |
print_corr(reorder->correlation, reorder->correlation); |
| 821 |
close_json_object(); |
| 822 |
} |
| 823 |
|
| 824 |
if (corrupt && corrupt->probability) { |
| 825 |
open_json_object("corrupt"); |
| 826 |
PRINT_PERCENT("corrupt", corrupt->probability); |
| 827 |
print_corr(corrupt->correlation, corrupt->correlation); |
| 828 |
close_json_object(); |
| 829 |
} |
| 830 |
|
| 831 |
if (rate && rate->rate) { |
| 832 |
open_json_object("rate"); |
| 833 |
rate64 = rate64 ? : rate->rate; |
| 834 |
tc_print_rate(PRINT_ANY, "rate", " rate %s", rate64); |
| 835 |
PRINT_INT_OPT("packetoverhead", rate->packet_overhead); |
| 836 |
|
| 837 |
print_uint(PRINT_JSON, "cellsize", NULL, rate->cell_size); |
| 838 |
if (rate->cell_size) |
| 839 |
print_uint(PRINT_FP, NULL, " cellsize %u", rate->cell_size); |
| 840 |
PRINT_INT_OPT("celloverhead", rate->cell_overhead); |
| 841 |
close_json_object(); |
| 842 |
} |
| 843 |
|
| 844 |
if (slot) { |
| 845 |
open_json_object("slot"); |
| 846 |
if (slot->dist_jitter > 0) { |
| 847 |
__PRINT_TIME64("distribution", " slot distribution", |
| 848 |
slot->dist_delay); |
| 849 |
__PRINT_TIME64("jitter", "", slot->dist_jitter); |
| 850 |
} else { |
| 851 |
__PRINT_TIME64("min-delay", " slot", slot->min_delay); |
| 852 |
__PRINT_TIME64("max-delay", "", slot->max_delay); |
| 853 |
} |
| 854 |
PRINT_INT_OPT("packets", slot->max_packets); |
| 855 |
PRINT_INT_OPT("bytes", slot->max_bytes); |
| 856 |
close_json_object(); |
| 857 |
} |
| 858 |
|
| 859 |
if (seed_present) |
| 860 |
print_u64(PRINT_ANY, "seed", " seed %llu", seed); |
| 861 |
|
| 862 |
print_bool(PRINT_JSON, "ecn", NULL, ecn); |
| 863 |
if (ecn) |
| 864 |
print_bool(PRINT_FP, NULL, " ecn ", ecn); |
| 865 |
|
| 866 |
print_luint(PRINT_JSON, "gap", NULL, qopt.gap); |
| 867 |
if (qopt.gap) |
| 868 |
print_luint(PRINT_FP, NULL, " gap %lu", qopt.gap); |
| 869 |
|
| 870 |
return 0; |
| 871 |
} |
| 872 |
|
| 873 |
struct qdisc_util netem_qdisc_util = { |
| 874 |
.id = "netem", |
| 875 |
.parse_qopt = netem_parse_opt, |
| 876 |
.print_qopt = netem_print_opt, |
| 877 |
}; |