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@osdl.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 #define usage() return(-1)
48 * Simplistic file parser for distrbution data.
54 static int get_distribution(const char *type, __s16 *data)
63 snprintf(name, sizeof(name), "/usr/lib/tc/%s.dist", type);
64 if ((f = fopen(name, "r")) == NULL) {
65 fprintf(stderr, "No distribution data for %s (%s: %s)\n",
66 type, name, strerror(errno));
71 while (getline(&line, &len, f) != -1) {
73 if (*line == '\n' || *line == '#')
76 for (p = line; ; p = endp) {
77 x = strtol(p, &endp, 0);
82 fprintf(stderr, "%s: too much data\n",
96 static int isnumber(const char *arg)
99 (void) strtod(arg, &p);
103 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isnumber(argv[1]))
105 /* Adjust for the fact that psched_ticks aren't always usecs
106 (based on kernel PSCHED_CLOCK configuration */
107 static int get_ticks(__u32 *ticks, const char *str)
111 if(get_usecs(&t, str))
114 *ticks = tc_core_usec2tick(t);
118 static char *sprint_ticks(__u32 ticks, char *buf)
120 return sprint_usecs(tc_core_tick2usec(ticks), buf);
124 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
127 size_t dist_size = 0;
129 struct tc_netem_qopt opt;
130 struct tc_netem_corr cor;
131 struct tc_netem_reorder reorder;
132 struct tc_netem_corrupt corrupt;
133 __s16 *dist_data = NULL;
135 memset(&opt, 0, sizeof(opt));
137 memset(&cor, 0, sizeof(cor));
138 memset(&reorder, 0, sizeof(reorder));
139 memset(&corrupt, 0, sizeof(corrupt));
142 if (matches(*argv, "limit") == 0) {
144 if (get_size(&opt.limit, *argv)) {
148 } else if (matches(*argv, "latency") == 0 ||
149 matches(*argv, "delay") == 0) {
151 if (get_ticks(&opt.latency, *argv)) {
156 if (NEXT_IS_NUMBER()) {
158 if (get_ticks(&opt.jitter, *argv)) {
163 if (NEXT_IS_NUMBER()) {
165 if (get_percent(&cor.delay_corr,
172 } else if (matches(*argv, "loss") == 0 ||
173 matches(*argv, "drop") == 0) {
175 if (get_percent(&opt.loss, *argv)) {
179 if (NEXT_IS_NUMBER()) {
181 if (get_percent(&cor.loss_corr, *argv)) {
186 } else if (matches(*argv, "reorder") == 0) {
188 if (get_percent(&reorder.probability, *argv)) {
192 if (NEXT_IS_NUMBER()) {
194 if (get_percent(&reorder.correlation, *argv)) {
199 } else if (matches(*argv, "corrupt") == 0) {
201 if (get_percent(&corrupt.probability, *argv)) {
205 if (NEXT_IS_NUMBER()) {
207 if (get_percent(&corrupt.correlation, *argv)) {
212 } else if (matches(*argv, "gap") == 0) {
214 if (get_u32(&opt.gap, *argv, 0)) {
218 } else if (matches(*argv, "duplicate") == 0) {
220 if (get_percent(&opt.duplicate, *argv)) {
221 explain1("duplicate");
224 if (NEXT_IS_NUMBER()) {
226 if (get_percent(&cor.dup_corr, *argv)) {
227 explain1("duplicate");
231 } else if (matches(*argv, "distribution") == 0) {
233 dist_data = alloca(MAXDIST);
234 dist_size = get_distribution(*argv, dist_data);
237 } else if (strcmp(*argv, "help") == 0) {
241 fprintf(stderr, "What is \"%s\"?\n", *argv);
248 tail = NLMSG_TAIL(n);
250 if (reorder.probability) {
251 if (opt.latency == 0) {
252 fprintf(stderr, "reordering not possible without specifying some delay\n");
256 } else if (opt.gap > 0) {
257 fprintf(stderr, "gap specified without reorder probability\n");
262 if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
263 fprintf(stderr, "distribution specified but no latency and jitter values\n");
268 if (addattr_l(n, TCA_BUF_MAX, TCA_OPTIONS, &opt, sizeof(opt)) < 0)
271 if (cor.delay_corr || cor.loss_corr || cor.dup_corr) {
272 if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
276 if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
279 if (corrupt.probability) {
280 if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
285 if (addattr_l(n, 32768, TCA_NETEM_DELAY_DIST,
286 dist_data, dist_size*sizeof(dist_data[0])) < 0)
289 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
293 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
295 const struct tc_netem_corr *cor = NULL;
296 const struct tc_netem_reorder *reorder = NULL;
297 const struct tc_netem_corrupt *corrupt = NULL;
298 struct tc_netem_qopt qopt;
299 int len = RTA_PAYLOAD(opt) - sizeof(qopt);
306 fprintf(stderr, "options size error\n");
309 memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
312 struct rtattr *tb[TCA_NETEM_MAX+1];
313 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
316 if (tb[TCA_NETEM_CORR]) {
317 if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
319 cor = RTA_DATA(tb[TCA_NETEM_CORR]);
321 if (tb[TCA_NETEM_REORDER]) {
322 if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder))
324 reorder = RTA_DATA(tb[TCA_NETEM_REORDER]);
326 if (tb[TCA_NETEM_CORRUPT]) {
327 if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt))
329 corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]);
333 fprintf(f, "limit %d", qopt.limit);
336 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
339 fprintf(f, " %s", sprint_ticks(qopt.jitter, b1));
340 if (cor && cor->delay_corr)
341 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
346 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
347 if (cor && cor->loss_corr)
348 fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
351 if (qopt.duplicate) {
352 fprintf(f, " duplicate %s",
353 sprint_percent(qopt.duplicate, b1));
354 if (cor && cor->dup_corr)
355 fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
358 if (reorder && reorder->probability) {
359 fprintf(f, " reorder %s",
360 sprint_percent(reorder->probability, b1));
361 if (reorder->correlation)
363 sprint_percent(reorder->correlation, b1));
366 if (corrupt && corrupt->probability) {
367 fprintf(f, " corrupt %s",
368 sprint_percent(corrupt->probability, b1));
369 if (corrupt->correlation)
371 sprint_percent(corrupt->correlation, b1));
375 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
380 struct qdisc_util netem_qdisc_util = {
382 .parse_qopt = netem_parse_opt,
383 .print_qopt = netem_print_opt,