]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_netem.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_netem.c
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 *
9 * Authors: Stephen Hemminger <shemminger@linux-foundation.org>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <math.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <stdint.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "utils.h"
27 #include "tc_util.h"
28 #include "tc_common.h"
29
30 static void explain(void)
31 {
32 fprintf(stderr,
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" \
42 " [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n" \
43 " [ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]\n");
44 }
45
46 static void explain1(const char *arg)
47 {
48 fprintf(stderr, "Illegal \"%s\"\n", arg);
49 }
50
51 /* Upper bound on size of distribution
52 * really (TCA_BUF_MAX - other headers) / sizeof (__s16)
53 */
54 #define MAX_DIST (16*1024)
55
56 /* scaled value used to percent of maximum. */
57 static void set_percent(__u32 *percent, double per)
58 {
59 *percent = rint(per * UINT32_MAX);
60 }
61
62 static int get_percent(__u32 *percent, const char *str)
63 {
64 double per;
65
66 if (parse_percent(&per, str))
67 return -1;
68
69 set_percent(percent, per);
70 return 0;
71 }
72
73 static void print_percent(char *buf, int len, __u32 per)
74 {
75 snprintf(buf, len, "%g%%", (100. * per) / UINT32_MAX);
76 }
77
78 static char *sprint_percent(__u32 per, char *buf)
79 {
80 print_percent(buf, SPRINT_BSIZE-1, per);
81 return buf;
82 }
83
84 /*
85 * Simplistic file parser for distrbution data.
86 * Format is:
87 * # comment line(s)
88 * data0 data1 ...
89 */
90 static int get_distribution(const char *type, __s16 *data, int maxdata)
91 {
92 FILE *f;
93 int n;
94 long x;
95 size_t len;
96 char *line = NULL;
97 char name[128];
98
99 snprintf(name, sizeof(name), "%s/%s.dist", get_tc_lib(), type);
100 if ((f = fopen(name, "r")) == NULL) {
101 fprintf(stderr, "No distribution data for %s (%s: %s)\n",
102 type, name, strerror(errno));
103 return -1;
104 }
105
106 n = 0;
107 while (getline(&line, &len, f) != -1) {
108 char *p, *endp;
109
110 if (*line == '\n' || *line == '#')
111 continue;
112
113 for (p = line; ; p = endp) {
114 x = strtol(p, &endp, 0);
115 if (endp == p)
116 break;
117
118 if (n >= maxdata) {
119 fprintf(stderr, "%s: too much data\n",
120 name);
121 n = -1;
122 goto error;
123 }
124 data[n++] = x;
125 }
126 }
127 error:
128 free(line);
129 fclose(f);
130 return n;
131 }
132
133 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isdigit(argv[1][0]))
134 #define NEXT_IS_SIGNED_NUMBER() \
135 (NEXT_ARG_OK() && (isdigit(argv[1][0]) || argv[1][0] == '-'))
136
137 /* Adjust for the fact that psched_ticks aren't always usecs
138 (based on kernel PSCHED_CLOCK configuration */
139 static int get_ticks(__u32 *ticks, const char *str)
140 {
141 unsigned int t;
142
143 if (get_time(&t, str))
144 return -1;
145
146 if (tc_core_time2big(t)) {
147 fprintf(stderr, "Illegal %u time (too large)\n", t);
148 return -1;
149 }
150
151 *ticks = tc_core_time2tick(t);
152 return 0;
153 }
154
155 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
156 struct nlmsghdr *n, const char *dev)
157 {
158 int dist_size = 0;
159 struct rtattr *tail;
160 struct tc_netem_qopt opt = { .limit = 1000 };
161 struct tc_netem_corr cor = {};
162 struct tc_netem_reorder reorder = {};
163 struct tc_netem_corrupt corrupt = {};
164 struct tc_netem_gimodel gimodel;
165 struct tc_netem_gemodel gemodel;
166 struct tc_netem_rate rate = {};
167 __s16 *dist_data = NULL;
168 __u16 loss_type = NETEM_LOSS_UNSPEC;
169 int present[__TCA_NETEM_MAX] = {};
170 __u64 rate64 = 0;
171
172 for ( ; argc > 0; --argc, ++argv) {
173 if (matches(*argv, "limit") == 0) {
174 NEXT_ARG();
175 if (get_size(&opt.limit, *argv)) {
176 explain1("limit");
177 return -1;
178 }
179 } else if (matches(*argv, "latency") == 0 ||
180 matches(*argv, "delay") == 0) {
181 NEXT_ARG();
182 if (get_ticks(&opt.latency, *argv)) {
183 explain1("latency");
184 return -1;
185 }
186
187 if (NEXT_IS_NUMBER()) {
188 NEXT_ARG();
189 if (get_ticks(&opt.jitter, *argv)) {
190 explain1("latency");
191 return -1;
192 }
193
194 if (NEXT_IS_NUMBER()) {
195 NEXT_ARG();
196 ++present[TCA_NETEM_CORR];
197 if (get_percent(&cor.delay_corr, *argv)) {
198 explain1("latency");
199 return -1;
200 }
201 }
202 }
203 } else if (matches(*argv, "loss") == 0 ||
204 matches(*argv, "drop") == 0) {
205 if (opt.loss > 0 || loss_type != NETEM_LOSS_UNSPEC) {
206 explain1("duplicate loss argument\n");
207 return -1;
208 }
209
210 NEXT_ARG();
211 /* Old (deprecated) random loss model syntax */
212 if (isdigit(argv[0][0]))
213 goto random_loss_model;
214
215 if (!strcmp(*argv, "random")) {
216 NEXT_ARG();
217 random_loss_model:
218 if (get_percent(&opt.loss, *argv)) {
219 explain1("loss percent");
220 return -1;
221 }
222 if (NEXT_IS_NUMBER()) {
223 NEXT_ARG();
224 ++present[TCA_NETEM_CORR];
225 if (get_percent(&cor.loss_corr, *argv)) {
226 explain1("loss correllation");
227 return -1;
228 }
229 }
230 } else if (!strcmp(*argv, "state")) {
231 double p13;
232
233 NEXT_ARG();
234 if (parse_percent(&p13, *argv)) {
235 explain1("loss p13");
236 return -1;
237 }
238
239 /* set defaults */
240 set_percent(&gimodel.p13, p13);
241 set_percent(&gimodel.p31, 1. - p13);
242 set_percent(&gimodel.p32, 0);
243 set_percent(&gimodel.p23, 1.);
244 set_percent(&gimodel.p14, 0);
245 loss_type = NETEM_LOSS_GI;
246
247 if (!NEXT_IS_NUMBER())
248 continue;
249 NEXT_ARG();
250 if (get_percent(&gimodel.p31, *argv)) {
251 explain1("loss p31");
252 return -1;
253 }
254
255 if (!NEXT_IS_NUMBER())
256 continue;
257 NEXT_ARG();
258 if (get_percent(&gimodel.p32, *argv)) {
259 explain1("loss p32");
260 return -1;
261 }
262
263 if (!NEXT_IS_NUMBER())
264 continue;
265 NEXT_ARG();
266 if (get_percent(&gimodel.p23, *argv)) {
267 explain1("loss p23");
268 return -1;
269 }
270 if (!NEXT_IS_NUMBER())
271 continue;
272 NEXT_ARG();
273 if (get_percent(&gimodel.p14, *argv)) {
274 explain1("loss p14");
275 return -1;
276 }
277
278 } else if (!strcmp(*argv, "gemodel")) {
279 NEXT_ARG();
280 if (get_percent(&gemodel.p, *argv)) {
281 explain1("loss gemodel p");
282 return -1;
283 }
284
285 /* set defaults */
286 set_percent(&gemodel.r, 1.);
287 set_percent(&gemodel.h, 0);
288 set_percent(&gemodel.k1, 0);
289 loss_type = NETEM_LOSS_GE;
290
291 if (!NEXT_IS_NUMBER())
292 continue;
293 NEXT_ARG();
294 if (get_percent(&gemodel.r, *argv)) {
295 explain1("loss gemodel r");
296 return -1;
297 }
298
299 if (!NEXT_IS_NUMBER())
300 continue;
301 NEXT_ARG();
302 if (get_percent(&gemodel.h, *argv)) {
303 explain1("loss gemodel h");
304 return -1;
305 }
306 /* netem option is "1-h" but kernel
307 * expects "h".
308 */
309 gemodel.h = UINT32_MAX - gemodel.h;
310
311 if (!NEXT_IS_NUMBER())
312 continue;
313 NEXT_ARG();
314 if (get_percent(&gemodel.k1, *argv)) {
315 explain1("loss gemodel k");
316 return -1;
317 }
318 } else {
319 fprintf(stderr, "Unknown loss parameter: %s\n",
320 *argv);
321 return -1;
322 }
323 } else if (matches(*argv, "ecn") == 0) {
324 present[TCA_NETEM_ECN] = 1;
325 } else if (matches(*argv, "reorder") == 0) {
326 NEXT_ARG();
327 present[TCA_NETEM_REORDER] = 1;
328 if (get_percent(&reorder.probability, *argv)) {
329 explain1("reorder");
330 return -1;
331 }
332 if (NEXT_IS_NUMBER()) {
333 NEXT_ARG();
334 ++present[TCA_NETEM_CORR];
335 if (get_percent(&reorder.correlation, *argv)) {
336 explain1("reorder");
337 return -1;
338 }
339 }
340 } else if (matches(*argv, "corrupt") == 0) {
341 NEXT_ARG();
342 present[TCA_NETEM_CORRUPT] = 1;
343 if (get_percent(&corrupt.probability, *argv)) {
344 explain1("corrupt");
345 return -1;
346 }
347 if (NEXT_IS_NUMBER()) {
348 NEXT_ARG();
349 ++present[TCA_NETEM_CORR];
350 if (get_percent(&corrupt.correlation, *argv)) {
351 explain1("corrupt");
352 return -1;
353 }
354 }
355 } else if (matches(*argv, "gap") == 0) {
356 NEXT_ARG();
357 if (get_u32(&opt.gap, *argv, 0)) {
358 explain1("gap");
359 return -1;
360 }
361 } else if (matches(*argv, "duplicate") == 0) {
362 NEXT_ARG();
363 if (get_percent(&opt.duplicate, *argv)) {
364 explain1("duplicate");
365 return -1;
366 }
367 if (NEXT_IS_NUMBER()) {
368 NEXT_ARG();
369 if (get_percent(&cor.dup_corr, *argv)) {
370 explain1("duplicate");
371 return -1;
372 }
373 }
374 } else if (matches(*argv, "distribution") == 0) {
375 NEXT_ARG();
376 dist_data = calloc(sizeof(dist_data[0]), MAX_DIST);
377 dist_size = get_distribution(*argv, dist_data, MAX_DIST);
378 if (dist_size <= 0) {
379 free(dist_data);
380 return -1;
381 }
382 } else if (matches(*argv, "rate") == 0) {
383 ++present[TCA_NETEM_RATE];
384 NEXT_ARG();
385 if (strchr(*argv, '%')) {
386 if (get_percent_rate64(&rate64, *argv, dev)) {
387 explain1("rate");
388 return -1;
389 }
390 } else if (get_rate64(&rate64, *argv)) {
391 explain1("rate");
392 return -1;
393 }
394 if (NEXT_IS_SIGNED_NUMBER()) {
395 NEXT_ARG();
396 if (get_s32(&rate.packet_overhead, *argv, 0)) {
397 explain1("rate");
398 return -1;
399 }
400 }
401 if (NEXT_IS_NUMBER()) {
402 NEXT_ARG();
403 if (get_u32(&rate.cell_size, *argv, 0)) {
404 explain1("rate");
405 return -1;
406 }
407 }
408 if (NEXT_IS_SIGNED_NUMBER()) {
409 NEXT_ARG();
410 if (get_s32(&rate.cell_overhead, *argv, 0)) {
411 explain1("rate");
412 return -1;
413 }
414 }
415 } else if (strcmp(*argv, "help") == 0) {
416 explain();
417 return -1;
418 } else {
419 fprintf(stderr, "What is \"%s\"?\n", *argv);
420 explain();
421 return -1;
422 }
423 }
424
425 tail = NLMSG_TAIL(n);
426
427 if (reorder.probability) {
428 if (opt.latency == 0) {
429 fprintf(stderr, "reordering not possible without specifying some delay\n");
430 explain();
431 return -1;
432 }
433 if (opt.gap == 0)
434 opt.gap = 1;
435 } else if (opt.gap > 0) {
436 fprintf(stderr, "gap specified without reorder probability\n");
437 explain();
438 return -1;
439 }
440
441 if (present[TCA_NETEM_ECN]) {
442 if (opt.loss <= 0 && loss_type == NETEM_LOSS_UNSPEC) {
443 fprintf(stderr, "ecn requested without loss model\n");
444 explain();
445 return -1;
446 }
447 }
448
449 if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
450 fprintf(stderr, "distribution specified but no latency and jitter values\n");
451 explain();
452 return -1;
453 }
454
455 if (addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)) < 0)
456 return -1;
457
458 if (present[TCA_NETEM_CORR] &&
459 addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
460 return -1;
461
462 if (present[TCA_NETEM_REORDER] &&
463 addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
464 return -1;
465
466 if (present[TCA_NETEM_ECN] &&
467 addattr_l(n, 1024, TCA_NETEM_ECN, &present[TCA_NETEM_ECN],
468 sizeof(present[TCA_NETEM_ECN])) < 0)
469 return -1;
470
471 if (present[TCA_NETEM_CORRUPT] &&
472 addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
473 return -1;
474
475 if (loss_type != NETEM_LOSS_UNSPEC) {
476 struct rtattr *start;
477
478 start = addattr_nest(n, 1024, TCA_NETEM_LOSS | NLA_F_NESTED);
479 if (loss_type == NETEM_LOSS_GI) {
480 if (addattr_l(n, 1024, NETEM_LOSS_GI,
481 &gimodel, sizeof(gimodel)) < 0)
482 return -1;
483 } else if (loss_type == NETEM_LOSS_GE) {
484 if (addattr_l(n, 1024, NETEM_LOSS_GE,
485 &gemodel, sizeof(gemodel)) < 0)
486 return -1;
487 } else {
488 fprintf(stderr, "loss in the weeds!\n");
489 return -1;
490 }
491
492 addattr_nest_end(n, start);
493 }
494
495 if (present[TCA_NETEM_RATE]) {
496 if (rate64 >= (1ULL << 32)) {
497 if (addattr_l(n, 1024,
498 TCA_NETEM_RATE64, &rate64, sizeof(rate64)) < 0)
499 return -1;
500 rate.rate = ~0U;
501 } else {
502 rate.rate = rate64;
503 }
504 if (addattr_l(n, 1024, TCA_NETEM_RATE, &rate, sizeof(rate)) < 0)
505 return -1;
506 }
507
508 if (dist_data) {
509 if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]),
510 TCA_NETEM_DELAY_DIST,
511 dist_data, dist_size * sizeof(dist_data[0])) < 0)
512 return -1;
513 free(dist_data);
514 }
515 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
516 return 0;
517 }
518
519 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
520 {
521 const struct tc_netem_corr *cor = NULL;
522 const struct tc_netem_reorder *reorder = NULL;
523 const struct tc_netem_corrupt *corrupt = NULL;
524 const struct tc_netem_gimodel *gimodel = NULL;
525 const struct tc_netem_gemodel *gemodel = NULL;
526 int *ecn = NULL;
527 struct tc_netem_qopt qopt;
528 const struct tc_netem_rate *rate = NULL;
529 int len;
530 __u64 rate64 = 0;
531
532 SPRINT_BUF(b1);
533
534 if (opt == NULL)
535 return 0;
536
537 len = RTA_PAYLOAD(opt) - sizeof(qopt);
538 if (len < 0) {
539 fprintf(stderr, "options size error\n");
540 return -1;
541 }
542 memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
543
544 if (len > 0) {
545 struct rtattr *tb[TCA_NETEM_MAX+1];
546
547 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
548 len);
549
550 if (tb[TCA_NETEM_CORR]) {
551 if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
552 return -1;
553 cor = RTA_DATA(tb[TCA_NETEM_CORR]);
554 }
555 if (tb[TCA_NETEM_REORDER]) {
556 if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder))
557 return -1;
558 reorder = RTA_DATA(tb[TCA_NETEM_REORDER]);
559 }
560 if (tb[TCA_NETEM_CORRUPT]) {
561 if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt))
562 return -1;
563 corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]);
564 }
565 if (tb[TCA_NETEM_LOSS]) {
566 struct rtattr *lb[NETEM_LOSS_MAX + 1];
567
568 parse_rtattr_nested(lb, NETEM_LOSS_MAX, tb[TCA_NETEM_LOSS]);
569 if (lb[NETEM_LOSS_GI])
570 gimodel = RTA_DATA(lb[NETEM_LOSS_GI]);
571 if (lb[NETEM_LOSS_GE])
572 gemodel = RTA_DATA(lb[NETEM_LOSS_GE]);
573 }
574 if (tb[TCA_NETEM_RATE]) {
575 if (RTA_PAYLOAD(tb[TCA_NETEM_RATE]) < sizeof(*rate))
576 return -1;
577 rate = RTA_DATA(tb[TCA_NETEM_RATE]);
578 }
579 if (tb[TCA_NETEM_ECN]) {
580 if (RTA_PAYLOAD(tb[TCA_NETEM_ECN]) < sizeof(*ecn))
581 return -1;
582 ecn = RTA_DATA(tb[TCA_NETEM_ECN]);
583 }
584 if (tb[TCA_NETEM_RATE64]) {
585 if (RTA_PAYLOAD(tb[TCA_NETEM_RATE64]) < sizeof(rate64))
586 return -1;
587 rate64 = rta_getattr_u64(tb[TCA_NETEM_RATE64]);
588 }
589 }
590
591 fprintf(f, "limit %d", qopt.limit);
592
593 if (qopt.latency) {
594 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
595
596 if (qopt.jitter) {
597 fprintf(f, " %s", sprint_ticks(qopt.jitter, b1));
598 if (cor && cor->delay_corr)
599 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
600 }
601 }
602
603 if (qopt.loss) {
604 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
605 if (cor && cor->loss_corr)
606 fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
607 }
608
609 if (gimodel) {
610 fprintf(f, " loss state p13 %s", sprint_percent(gimodel->p13, b1));
611 fprintf(f, " p31 %s", sprint_percent(gimodel->p31, b1));
612 fprintf(f, " p32 %s", sprint_percent(gimodel->p32, b1));
613 fprintf(f, " p23 %s", sprint_percent(gimodel->p23, b1));
614 fprintf(f, " p14 %s", sprint_percent(gimodel->p14, b1));
615 }
616
617 if (gemodel) {
618 fprintf(f, " loss gemodel p %s",
619 sprint_percent(gemodel->p, b1));
620 fprintf(f, " r %s", sprint_percent(gemodel->r, b1));
621 fprintf(f, " 1-h %s", sprint_percent(UINT32_MAX -
622 gemodel->h, b1));
623 fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1));
624 }
625
626 if (qopt.duplicate) {
627 fprintf(f, " duplicate %s",
628 sprint_percent(qopt.duplicate, b1));
629 if (cor && cor->dup_corr)
630 fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
631 }
632
633 if (reorder && reorder->probability) {
634 fprintf(f, " reorder %s",
635 sprint_percent(reorder->probability, b1));
636 if (reorder->correlation)
637 fprintf(f, " %s",
638 sprint_percent(reorder->correlation, b1));
639 }
640
641 if (corrupt && corrupt->probability) {
642 fprintf(f, " corrupt %s",
643 sprint_percent(corrupt->probability, b1));
644 if (corrupt->correlation)
645 fprintf(f, " %s",
646 sprint_percent(corrupt->correlation, b1));
647 }
648
649 if (rate && rate->rate) {
650 if (rate64)
651 fprintf(f, " rate %s", sprint_rate(rate64, b1));
652 else
653 fprintf(f, " rate %s", sprint_rate(rate->rate, b1));
654 if (rate->packet_overhead)
655 fprintf(f, " packetoverhead %d", rate->packet_overhead);
656 if (rate->cell_size)
657 fprintf(f, " cellsize %u", rate->cell_size);
658 if (rate->cell_overhead)
659 fprintf(f, " celloverhead %d", rate->cell_overhead);
660 }
661
662 if (ecn)
663 fprintf(f, " ecn ");
664
665 if (qopt.gap)
666 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
667
668
669 return 0;
670 }
671
672 struct qdisc_util netem_qdisc_util = {
673 .id = "netem",
674 .parse_qopt = netem_parse_opt,
675 .print_qopt = netem_print_opt,
676 };