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), "%s/%s.dist", get_tc_lib(), 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_time(&t, str))
114 if (tc_core_time2big(t)) {
115 fprintf(stderr, "Illegal %u time (too large)\n", t);
119 *ticks = tc_core_time2tick(t);
123 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
126 size_t dist_size = 0;
128 struct tc_netem_qopt opt;
129 struct tc_netem_corr cor;
130 struct tc_netem_reorder reorder;
131 struct tc_netem_corrupt corrupt;
132 __s16 *dist_data = NULL;
133 int present[__TCA_NETEM_MAX];
135 memset(&opt, 0, sizeof(opt));
137 memset(&cor, 0, sizeof(cor));
138 memset(&reorder, 0, sizeof(reorder));
139 memset(&corrupt, 0, sizeof(corrupt));
140 memset(present, 0, sizeof(present));
143 if (matches(*argv, "limit") == 0) {
145 if (get_size(&opt.limit, *argv)) {
149 } else if (matches(*argv, "latency") == 0 ||
150 matches(*argv, "delay") == 0) {
152 if (get_ticks(&opt.latency, *argv)) {
157 if (NEXT_IS_NUMBER()) {
159 if (get_ticks(&opt.jitter, *argv)) {
164 if (NEXT_IS_NUMBER()) {
166 ++present[TCA_NETEM_CORR];
167 if (get_percent(&cor.delay_corr, *argv)) {
173 } else if (matches(*argv, "loss") == 0 ||
174 matches(*argv, "drop") == 0) {
176 if (get_percent(&opt.loss, *argv)) {
180 if (NEXT_IS_NUMBER()) {
182 ++present[TCA_NETEM_CORR];
183 if (get_percent(&cor.loss_corr, *argv)) {
188 } else if (matches(*argv, "reorder") == 0) {
190 present[TCA_NETEM_REORDER] = 1;
191 if (get_percent(&reorder.probability, *argv)) {
195 if (NEXT_IS_NUMBER()) {
197 ++present[TCA_NETEM_CORR];
198 if (get_percent(&reorder.correlation, *argv)) {
203 } else if (matches(*argv, "corrupt") == 0) {
205 present[TCA_NETEM_CORRUPT] = 1;
206 if (get_percent(&corrupt.probability, *argv)) {
210 if (NEXT_IS_NUMBER()) {
212 ++present[TCA_NETEM_CORR];
213 if (get_percent(&corrupt.correlation, *argv)) {
218 } else if (matches(*argv, "gap") == 0) {
220 if (get_u32(&opt.gap, *argv, 0)) {
224 } else if (matches(*argv, "duplicate") == 0) {
226 if (get_percent(&opt.duplicate, *argv)) {
227 explain1("duplicate");
230 if (NEXT_IS_NUMBER()) {
232 if (get_percent(&cor.dup_corr, *argv)) {
233 explain1("duplicate");
237 } else if (matches(*argv, "distribution") == 0) {
239 dist_data = alloca(MAXDIST);
240 dist_size = get_distribution(*argv, dist_data);
243 } else if (strcmp(*argv, "help") == 0) {
247 fprintf(stderr, "What is \"%s\"?\n", *argv);
254 tail = NLMSG_TAIL(n);
256 if (reorder.probability) {
257 if (opt.latency == 0) {
258 fprintf(stderr, "reordering not possible without specifying some delay\n");
262 } else if (opt.gap > 0) {
263 fprintf(stderr, "gap specified without reorder probability\n");
268 if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
269 fprintf(stderr, "distribution specified but no latency and jitter values\n");
274 if (addattr_l(n, TCA_BUF_MAX, TCA_OPTIONS, &opt, sizeof(opt)) < 0)
277 if (present[TCA_NETEM_CORR] &&
278 addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
281 if (present[TCA_NETEM_REORDER] &&
282 addattr_l(n, TCA_BUF_MAX, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
285 if (present[TCA_NETEM_CORRUPT] &&
286 addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
290 if (addattr_l(n, 32768, TCA_NETEM_DELAY_DIST,
291 dist_data, dist_size*sizeof(dist_data[0])) < 0)
294 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
298 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
300 const struct tc_netem_corr *cor = NULL;
301 const struct tc_netem_reorder *reorder = NULL;
302 const struct tc_netem_corrupt *corrupt = NULL;
303 struct tc_netem_qopt qopt;
304 int len = RTA_PAYLOAD(opt) - sizeof(qopt);
311 fprintf(stderr, "options size error\n");
314 memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
317 struct rtattr *tb[TCA_NETEM_MAX+1];
318 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
321 if (tb[TCA_NETEM_CORR]) {
322 if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
324 cor = RTA_DATA(tb[TCA_NETEM_CORR]);
326 if (tb[TCA_NETEM_REORDER]) {
327 if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder))
329 reorder = RTA_DATA(tb[TCA_NETEM_REORDER]);
331 if (tb[TCA_NETEM_CORRUPT]) {
332 if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt))
334 corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]);
338 fprintf(f, "limit %d", qopt.limit);
341 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
344 fprintf(f, " %s", sprint_ticks(qopt.jitter, b1));
345 if (cor && cor->delay_corr)
346 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
351 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
352 if (cor && cor->loss_corr)
353 fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
356 if (qopt.duplicate) {
357 fprintf(f, " duplicate %s",
358 sprint_percent(qopt.duplicate, b1));
359 if (cor && cor->dup_corr)
360 fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
363 if (reorder && reorder->probability) {
364 fprintf(f, " reorder %s",
365 sprint_percent(reorder->probability, b1));
366 if (reorder->correlation)
368 sprint_percent(reorder->correlation, b1));
371 if (corrupt && corrupt->probability) {
372 fprintf(f, " corrupt %s",
373 sprint_percent(corrupt->probability, b1));
374 if (corrupt->correlation)
376 sprint_percent(corrupt->correlation, b1));
380 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
385 struct qdisc_util netem_qdisc_util = {
387 .parse_qopt = netem_parse_opt,
388 .print_qopt = netem_print_opt,