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.
9 * Authors: Stephen Hemminger <shemminger@linux-foundation.org>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
28 #include "tc_common.h"
30 static void explain(void)
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 " [ reorder PERCENT [CORRELATION] [ gap DISTANCE ]]\n");
44 static void explain1(const char *arg)
46 fprintf(stderr, "Illegal \"%s\"\n", arg);
49 /* Upper bound on size of distribution
50 * really (TCA_BUF_MAX - other headers) / sizeof (__s16)
52 #define MAX_DIST (16*1024)
54 static const double max_percent_value = 0xffffffff;
56 /* scaled value used to percent of maximum. */
57 static void set_percent(__u32 *percent, double per)
59 *percent = (unsigned) rint(per * max_percent_value);
63 /* Parse either a fraction '.3' or percent '30%
64 * return: 0 = ok, -1 = error, 1 = out of range
66 static int parse_percent(double *val, const char *str)
70 *val = strtod(str, &p) / 100.;
71 if (*p && strcmp(p, "%") )
77 static int get_percent(__u32 *percent, const char *str)
81 if (parse_percent(&per, str))
84 set_percent(percent, per);
88 void print_percent(char *buf, int len, __u32 per)
90 snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value);
93 char * sprint_percent(__u32 per, char *buf)
95 print_percent(buf, SPRINT_BSIZE-1, per);
100 * Simplistic file parser for distrbution data.
105 static int get_distribution(const char *type, __s16 *data, int maxdata)
114 snprintf(name, sizeof(name), "%s/%s.dist", get_tc_lib(), type);
115 if ((f = fopen(name, "r")) == NULL) {
116 fprintf(stderr, "No distribution data for %s (%s: %s)\n",
117 type, name, strerror(errno));
122 while (getline(&line, &len, f) != -1) {
124 if (*line == '\n' || *line == '#')
127 for (p = line; ; p = endp) {
128 x = strtol(p, &endp, 0);
133 fprintf(stderr, "%s: too much data\n",
147 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isdigit(argv[1][0]))
149 /* Adjust for the fact that psched_ticks aren't always usecs
150 (based on kernel PSCHED_CLOCK configuration */
151 static int get_ticks(__u32 *ticks, const char *str)
155 if(get_time(&t, str))
158 if (tc_core_time2big(t)) {
159 fprintf(stderr, "Illegal %u time (too large)\n", t);
163 *ticks = tc_core_time2tick(t);
167 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
172 struct tc_netem_qopt opt = { .limit = 1000 };
173 struct tc_netem_corr cor;
174 struct tc_netem_reorder reorder;
175 struct tc_netem_corrupt corrupt;
176 struct tc_netem_gimodel gimodel;
177 struct tc_netem_gemodel gemodel;
178 __s16 *dist_data = NULL;
179 __u16 loss_type = NETEM_LOSS_UNSPEC;
180 int present[__TCA_NETEM_MAX];
182 memset(&cor, 0, sizeof(cor));
183 memset(&reorder, 0, sizeof(reorder));
184 memset(&corrupt, 0, sizeof(corrupt));
185 memset(present, 0, sizeof(present));
187 for( ; argc > 0; --argc, ++argv) {
188 if (matches(*argv, "limit") == 0) {
190 if (get_size(&opt.limit, *argv)) {
194 } else if (matches(*argv, "latency") == 0 ||
195 matches(*argv, "delay") == 0) {
197 if (get_ticks(&opt.latency, *argv)) {
202 if (NEXT_IS_NUMBER()) {
204 if (get_ticks(&opt.jitter, *argv)) {
209 if (NEXT_IS_NUMBER()) {
211 ++present[TCA_NETEM_CORR];
212 if (get_percent(&cor.delay_corr, *argv)) {
218 } else if (matches(*argv, "loss") == 0 ||
219 matches(*argv, "drop") == 0) {
220 if (opt.loss > 0 || loss_type != NETEM_LOSS_UNSPEC) {
221 explain1("duplicate loss argument\n");
226 /* Old (deprecated) random loss model syntax */
227 if (isdigit(argv[0][0]))
228 goto random_loss_model;
230 if (!strcmp(*argv, "random")) {
233 if (get_percent(&opt.loss, *argv)) {
234 explain1("loss percent");
237 if (NEXT_IS_NUMBER()) {
239 ++present[TCA_NETEM_CORR];
240 if (get_percent(&cor.loss_corr, *argv)) {
241 explain1("loss correllation");
245 } else if (!strcmp(*argv, "state")) {
249 if (parse_percent(&p13, *argv)) {
250 explain1("loss p13");
255 set_percent(&gimodel.p13, p13);
256 set_percent(&gimodel.p31, 1. - p13);
257 set_percent(&gimodel.p32, 0);
258 set_percent(&gimodel.p23, 1.);
259 loss_type = NETEM_LOSS_GI;
261 if (!NEXT_IS_NUMBER())
264 if (get_percent(&gimodel.p31, *argv)) {
265 explain1("loss p31");
269 if (!NEXT_IS_NUMBER())
272 if (get_percent(&gimodel.p32, *argv)) {
273 explain1("loss p32");
277 if (!NEXT_IS_NUMBER())
280 if (get_percent(&gimodel.p23, *argv)) {
281 explain1("loss p23");
285 } else if (!strcmp(*argv, "gemodel")) {
287 if (get_percent(&gemodel.p, *argv)) {
288 explain1("loss gemodel p");
293 set_percent(&gemodel.r, 1.);
294 set_percent(&gemodel.h, 0);
295 set_percent(&gemodel.k1, 1.);
296 loss_type = NETEM_LOSS_GE;
298 if (!NEXT_IS_NUMBER())
301 if (get_percent(&gemodel.r, *argv)) {
302 explain1("loss gemodel r");
306 if (!NEXT_IS_NUMBER())
309 if (get_percent(&gemodel.h, *argv)) {
310 explain1("loss gemodel h");
314 if (!NEXT_IS_NUMBER())
317 if (get_percent(&gemodel.k1, *argv)) {
318 explain1("loss gemodel k");
322 fprintf(stderr, "Unknown loss parameter: %s\n",
326 } else if (matches(*argv, "reorder") == 0) {
328 present[TCA_NETEM_REORDER] = 1;
329 if (get_percent(&reorder.probability, *argv)) {
333 if (NEXT_IS_NUMBER()) {
335 ++present[TCA_NETEM_CORR];
336 if (get_percent(&reorder.correlation, *argv)) {
341 } else if (matches(*argv, "corrupt") == 0) {
343 present[TCA_NETEM_CORRUPT] = 1;
344 if (get_percent(&corrupt.probability, *argv)) {
348 if (NEXT_IS_NUMBER()) {
350 ++present[TCA_NETEM_CORR];
351 if (get_percent(&corrupt.correlation, *argv)) {
356 } else if (matches(*argv, "gap") == 0) {
358 if (get_u32(&opt.gap, *argv, 0)) {
362 } else if (matches(*argv, "duplicate") == 0) {
364 if (get_percent(&opt.duplicate, *argv)) {
365 explain1("duplicate");
368 if (NEXT_IS_NUMBER()) {
370 if (get_percent(&cor.dup_corr, *argv)) {
371 explain1("duplicate");
375 } else if (matches(*argv, "distribution") == 0) {
377 dist_data = calloc(sizeof(dist_data[0]), MAX_DIST);
378 dist_size = get_distribution(*argv, dist_data, MAX_DIST);
379 if (dist_size <= 0) {
383 } else if (strcmp(*argv, "help") == 0) {
387 fprintf(stderr, "What is \"%s\"?\n", *argv);
393 tail = NLMSG_TAIL(n);
395 if (reorder.probability) {
396 if (opt.latency == 0) {
397 fprintf(stderr, "reordering not possible without specifying some delay\n");
401 } else if (opt.gap > 0) {
402 fprintf(stderr, "gap specified without reorder probability\n");
407 if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
408 fprintf(stderr, "distribution specified but no latency and jitter values\n");
413 if (addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)) < 0)
416 if (present[TCA_NETEM_CORR] &&
417 addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
420 if (present[TCA_NETEM_REORDER] &&
421 addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
424 if (present[TCA_NETEM_CORRUPT] &&
425 addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
428 if (loss_type != NETEM_LOSS_UNSPEC) {
429 struct rtattr *start;
431 start = addattr_nest(n, 1024, TCA_NETEM_LOSS | NLA_F_NESTED);
432 if (loss_type == NETEM_LOSS_GI) {
433 if (addattr_l(n, 1024, NETEM_LOSS_GI,
434 &gimodel, sizeof(gimodel)) < 0)
436 } else if (loss_type == NETEM_LOSS_GE) {
437 if (addattr_l(n, 1024, NETEM_LOSS_GE,
438 &gemodel, sizeof(gemodel)) < 0)
441 fprintf(stderr, "loss in the weeds!\n");
445 addattr_nest_end(n, start);
449 if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]),
450 TCA_NETEM_DELAY_DIST,
451 dist_data, dist_size * sizeof(dist_data[0])) < 0)
455 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
459 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
461 const struct tc_netem_corr *cor = NULL;
462 const struct tc_netem_reorder *reorder = NULL;
463 const struct tc_netem_corrupt *corrupt = NULL;
464 const struct tc_netem_gimodel *gimodel = NULL;
465 const struct tc_netem_gemodel *gemodel = NULL;
466 struct tc_netem_qopt qopt;
467 int len = RTA_PAYLOAD(opt) - sizeof(qopt);
474 fprintf(stderr, "options size error\n");
477 memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
480 struct rtattr *tb[TCA_NETEM_MAX+1];
481 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
484 if (tb[TCA_NETEM_CORR]) {
485 if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
487 cor = RTA_DATA(tb[TCA_NETEM_CORR]);
489 if (tb[TCA_NETEM_REORDER]) {
490 if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder))
492 reorder = RTA_DATA(tb[TCA_NETEM_REORDER]);
494 if (tb[TCA_NETEM_CORRUPT]) {
495 if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt))
497 corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]);
499 if (tb[TCA_NETEM_LOSS]) {
500 struct rtattr *lb[NETEM_LOSS_MAX + 1];
502 parse_rtattr_nested(lb, NETEM_LOSS_MAX, tb[TCA_NETEM_LOSS]);
503 if (lb[NETEM_LOSS_GI])
504 gemodel = RTA_DATA(lb[NETEM_LOSS_GI]);
505 if (lb[NETEM_LOSS_GE])
506 gemodel = RTA_DATA(lb[NETEM_LOSS_GE]);
510 fprintf(f, "limit %d", qopt.limit);
513 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
516 fprintf(f, " %s", sprint_ticks(qopt.jitter, b1));
517 if (cor && cor->delay_corr)
518 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
523 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
524 if (cor && cor->loss_corr)
525 fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
529 fprintf(f, " loss state p13 %s", sprint_percent(gimodel->p13, b1));
530 fprintf(f, " p31 %s", sprint_percent(gimodel->p31, b1));
531 fprintf(f, " p32 %s", sprint_percent(gimodel->p32, b1));
532 fprintf(f, " p23 %s", sprint_percent(gimodel->p23, b1));
533 fprintf(f, " p14 %s", sprint_percent(gimodel->p14, b1));
537 fprintf(f, "loss gemodel p %s",
538 sprint_percent(gemodel->p, b1));
539 fprintf(f, " r %s", sprint_percent(gemodel->r, b1));
540 fprintf(f, " 1-h %s", sprint_percent(gemodel->h, b1));
541 fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1));
544 if (qopt.duplicate) {
545 fprintf(f, " duplicate %s",
546 sprint_percent(qopt.duplicate, b1));
547 if (cor && cor->dup_corr)
548 fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
551 if (reorder && reorder->probability) {
552 fprintf(f, " reorder %s",
553 sprint_percent(reorder->probability, b1));
554 if (reorder->correlation)
556 sprint_percent(reorder->correlation, b1));
559 if (corrupt && corrupt->probability) {
560 fprintf(f, " corrupt %s",
561 sprint_percent(corrupt->probability, b1));
562 if (corrupt->correlation)
564 sprint_percent(corrupt->correlation, b1));
568 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
573 struct qdisc_util netem_qdisc_util = {
575 .parse_qopt = netem_parse_opt,
576 .print_qopt = netem_print_opt,