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