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>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
26 #include "tc_common.h"
28 static void explain(void)
31 "Usage: ... netem [ limit PACKETS ] \n" \
32 " [ delay TIME [ JITTER [CORRELATION]]]\n" \
33 " [ distribution {uniform|normal|pareto|paretonormal} ]\n" \
34 " [ drop PERCENT [CORRELATION]] \n" \
35 " [ corrupt PERCENT [CORRELATION]] \n" \
36 " [ duplicate PERCENT [CORRELATION]]\n" \
37 " [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n");
40 static void explain1(const char *arg)
42 fprintf(stderr, "Illegal \"%s\"\n", arg);
45 /* Upper bound on size of distribution
46 * really (TCA_BUF_MAX - other headers) / sizeof (__s16)
48 #define MAX_DIST (16*1024)
51 * Simplistic file parser for distrbution data.
56 static int get_distribution(const char *type, __s16 *data, int maxdata)
65 snprintf(name, sizeof(name), "%s/%s.dist", get_tc_lib(), type);
66 if ((f = fopen(name, "r")) == NULL) {
67 fprintf(stderr, "No distribution data for %s (%s: %s)\n",
68 type, name, strerror(errno));
73 while (getline(&line, &len, f) != -1) {
75 if (*line == '\n' || *line == '#')
78 for (p = line; ; p = endp) {
79 x = strtol(p, &endp, 0);
84 fprintf(stderr, "%s: too much data\n",
98 static int isnumber(const char *arg)
102 return strtod(arg, &p) != 0 || p != arg;
105 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isnumber(argv[1]))
107 /* Adjust for the fact that psched_ticks aren't always usecs
108 (based on kernel PSCHED_CLOCK configuration */
109 static int get_ticks(__u32 *ticks, const char *str)
113 if(get_time(&t, str))
116 if (tc_core_time2big(t)) {
117 fprintf(stderr, "Illegal %u time (too large)\n", t);
121 *ticks = tc_core_time2tick(t);
125 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
130 struct tc_netem_qopt opt;
131 struct tc_netem_corr cor;
132 struct tc_netem_reorder reorder;
133 struct tc_netem_corrupt corrupt;
134 __s16 *dist_data = NULL;
135 int present[__TCA_NETEM_MAX];
137 memset(&opt, 0, sizeof(opt));
139 memset(&cor, 0, sizeof(cor));
140 memset(&reorder, 0, sizeof(reorder));
141 memset(&corrupt, 0, sizeof(corrupt));
142 memset(present, 0, sizeof(present));
145 if (matches(*argv, "limit") == 0) {
147 if (get_size(&opt.limit, *argv)) {
151 } else if (matches(*argv, "latency") == 0 ||
152 matches(*argv, "delay") == 0) {
154 if (get_ticks(&opt.latency, *argv)) {
159 if (NEXT_IS_NUMBER()) {
161 if (get_ticks(&opt.jitter, *argv)) {
166 if (NEXT_IS_NUMBER()) {
168 ++present[TCA_NETEM_CORR];
169 if (get_percent(&cor.delay_corr, *argv)) {
175 } else if (matches(*argv, "loss") == 0 ||
176 matches(*argv, "drop") == 0) {
178 if (get_percent(&opt.loss, *argv)) {
182 if (NEXT_IS_NUMBER()) {
184 ++present[TCA_NETEM_CORR];
185 if (get_percent(&cor.loss_corr, *argv)) {
190 } else if (matches(*argv, "reorder") == 0) {
192 present[TCA_NETEM_REORDER] = 1;
193 if (get_percent(&reorder.probability, *argv)) {
197 if (NEXT_IS_NUMBER()) {
199 ++present[TCA_NETEM_CORR];
200 if (get_percent(&reorder.correlation, *argv)) {
205 } else if (matches(*argv, "corrupt") == 0) {
207 present[TCA_NETEM_CORRUPT] = 1;
208 if (get_percent(&corrupt.probability, *argv)) {
212 if (NEXT_IS_NUMBER()) {
214 ++present[TCA_NETEM_CORR];
215 if (get_percent(&corrupt.correlation, *argv)) {
220 } else if (matches(*argv, "gap") == 0) {
222 if (get_u32(&opt.gap, *argv, 0)) {
226 } else if (matches(*argv, "duplicate") == 0) {
228 if (get_percent(&opt.duplicate, *argv)) {
229 explain1("duplicate");
232 if (NEXT_IS_NUMBER()) {
234 if (get_percent(&cor.dup_corr, *argv)) {
235 explain1("duplicate");
239 } else if (matches(*argv, "distribution") == 0) {
241 dist_data = calloc(sizeof(dist_data[0]), MAX_DIST);
242 dist_size = get_distribution(*argv, dist_data, MAX_DIST);
243 if (dist_size <= 0) {
247 } else if (strcmp(*argv, "help") == 0) {
251 fprintf(stderr, "What is \"%s\"?\n", *argv);
258 tail = NLMSG_TAIL(n);
260 if (reorder.probability) {
261 if (opt.latency == 0) {
262 fprintf(stderr, "reordering not possible without specifying some delay\n");
266 } else if (opt.gap > 0) {
267 fprintf(stderr, "gap specified without reorder probability\n");
272 if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
273 fprintf(stderr, "distribution specified but no latency and jitter values\n");
278 if (addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)) < 0)
281 if (present[TCA_NETEM_CORR] &&
282 addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
285 if (present[TCA_NETEM_REORDER] &&
286 addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
289 if (present[TCA_NETEM_CORRUPT] &&
290 addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
294 if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]),
295 TCA_NETEM_DELAY_DIST,
296 dist_data, dist_size * sizeof(dist_data[0])) < 0)
300 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
304 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
306 const struct tc_netem_corr *cor = NULL;
307 const struct tc_netem_reorder *reorder = NULL;
308 const struct tc_netem_corrupt *corrupt = NULL;
309 struct tc_netem_qopt qopt;
310 int len = RTA_PAYLOAD(opt) - sizeof(qopt);
317 fprintf(stderr, "options size error\n");
320 memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
323 struct rtattr *tb[TCA_NETEM_MAX+1];
324 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
327 if (tb[TCA_NETEM_CORR]) {
328 if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
330 cor = RTA_DATA(tb[TCA_NETEM_CORR]);
332 if (tb[TCA_NETEM_REORDER]) {
333 if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder))
335 reorder = RTA_DATA(tb[TCA_NETEM_REORDER]);
337 if (tb[TCA_NETEM_CORRUPT]) {
338 if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt))
340 corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]);
344 fprintf(f, "limit %d", qopt.limit);
347 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
350 fprintf(f, " %s", sprint_ticks(qopt.jitter, b1));
351 if (cor && cor->delay_corr)
352 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
357 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
358 if (cor && cor->loss_corr)
359 fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
362 if (qopt.duplicate) {
363 fprintf(f, " duplicate %s",
364 sprint_percent(qopt.duplicate, b1));
365 if (cor && cor->dup_corr)
366 fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
369 if (reorder && reorder->probability) {
370 fprintf(f, " reorder %s",
371 sprint_percent(reorder->probability, b1));
372 if (reorder->correlation)
374 sprint_percent(reorder->correlation, b1));
377 if (corrupt && corrupt->probability) {
378 fprintf(f, " corrupt %s",
379 sprint_percent(corrupt->probability, b1));
380 if (corrupt->correlation)
382 sprint_percent(corrupt->correlation, b1));
386 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
391 struct qdisc_util netem_qdisc_util = {
393 .parse_qopt = netem_parse_opt,
394 .print_qopt = netem_print_opt,