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 " [ drop PERCENT [CORRELATION]] \n" \
34 " [ duplicate PERCENT [CORRELATION]]\n" \
35 " [ distribution {uniform|normal|pareto|paretonormal} ]\n" \
36 " [ gap PACKETS ]\n");
39 static void explain1(const char *arg)
41 fprintf(stderr, "Illegal \"%s\"\n", arg);
44 #define usage() return(-1)
47 * Simplistic file parser for distrbution data.
53 static int get_distribution(const char *type, __s16 *data)
62 snprintf(name, sizeof(name), "/usr/lib/tc/%s.dist", type);
63 if ((f = fopen(name, "r")) == NULL) {
64 fprintf(stderr, "No distribution data for %s (%s: %s)\n",
65 type, name, strerror(errno));
70 while (getline(&line, &len, f) != -1) {
72 if (*line == '\n' || *line == '#')
75 for (p = line; ; p = endp) {
76 x = strtol(p, &endp, 0);
81 fprintf(stderr, "%s: too much data\n",
95 static int isnumber(const char *arg)
98 (void) strtod(arg, &p);
102 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isnumber(argv[1]))
104 /* Adjust for the fact that psched_ticks aren't always usecs
105 (based on kernel PSCHED_CLOCK configuration */
106 static int get_ticks(__u32 *ticks, const char *str)
110 if(get_usecs(&t, str))
113 *ticks = tc_core_usec2tick(t);
117 static char *sprint_ticks(__u32 ticks, char *buf)
119 return sprint_usecs(tc_core_tick2usec(ticks), buf);
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 __s16 dist_data[MAXDIST];
132 memset(&opt, 0, sizeof(opt));
134 memset(&cor, 0, sizeof(cor));
137 if (matches(*argv, "limit") == 0) {
139 if (get_size(&opt.limit, *argv)) {
143 } else if (matches(*argv, "latency") == 0 ||
144 matches(*argv, "delay") == 0) {
146 if (get_ticks(&opt.latency, *argv)) {
151 if (NEXT_IS_NUMBER()) {
153 if (get_ticks(&opt.jitter, *argv)) {
158 if (NEXT_IS_NUMBER()) {
160 if (get_percent(&cor.delay_corr,
167 } else if (matches(*argv, "loss") == 0 ||
168 matches(*argv, "drop") == 0) {
170 if (get_percent(&opt.loss, *argv)) {
174 if (NEXT_IS_NUMBER()) {
176 if (get_percent(&cor.loss_corr, *argv)) {
181 } else if (matches(*argv, "gap") == 0) {
183 if (get_u32(&opt.gap, *argv, 0)) {
187 } else if (matches(*argv, "duplicate") == 0) {
189 if (get_percent(&opt.duplicate, *argv)) {
190 explain1("duplicate");
193 if (NEXT_IS_NUMBER()) {
195 if (get_percent(&cor.dup_corr, *argv)) {
196 explain1("duplicate");
200 } else if (matches(*argv, "distribution") == 0) {
202 dist_size = get_distribution(*argv, dist_data);
205 } else if (strcmp(*argv, "help") == 0) {
209 fprintf(stderr, "What is \"%s\"?\n", *argv);
216 tail = NLMSG_TAIL(n);
218 addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
219 addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor));
222 addattr_l(n, 32768, TCA_NETEM_DELAY_DIST,
223 dist_data, dist_size*sizeof(dist_data[0]));
225 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
229 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
231 const struct tc_netem_corr *cor = NULL;
232 struct tc_netem_qopt qopt;
233 int len = RTA_PAYLOAD(opt) - sizeof(qopt);
240 fprintf(stderr, "options size error\n");
243 memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
246 struct rtattr *tb[TCA_NETEM_MAX+1];
247 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
250 if (tb[TCA_NETEM_CORR]) {
251 if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
253 cor = RTA_DATA(tb[TCA_NETEM_CORR]);
257 fprintf(f, "limit %d", qopt.limit);
260 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
263 fprintf(f, " %s", sprint_ticks(qopt.jitter, b1));
264 if (cor && cor->delay_corr)
265 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
270 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
271 if (cor && cor->loss_corr)
272 fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
275 if (qopt.duplicate) {
276 fprintf(f, " duplicate %s",
277 sprint_percent(qopt.duplicate, b1));
278 if (cor && cor->dup_corr)
279 fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
283 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
288 struct qdisc_util netem_qdisc_util = {
290 .parse_qopt = netem_parse_opt,
291 .print_qopt = netem_print_opt,