]>
Commit | Line | Data |
---|---|---|
309a4c90 SH |
1 | /* |
2 | * q_netem.c NETEM. | |
3 | * | |
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. | |
8 | * | |
9 | * Authors: Stephen Hemminger <shemminger@osdl.org> | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
16 | #include <syslog.h> | |
17 | #include <fcntl.h> | |
18 | #include <sys/socket.h> | |
19 | #include <netinet/in.h> | |
20 | #include <arpa/inet.h> | |
21 | #include <string.h> | |
b7be3d0c | 22 | #include <errno.h> |
309a4c90 SH |
23 | |
24 | #include "utils.h" | |
25 | #include "tc_util.h" | |
b7be3d0c | 26 | #include "tc_common.h" |
309a4c90 SH |
27 | |
28 | static void explain(void) | |
29 | { | |
ae665a52 | 30 | fprintf(stderr, |
b7be3d0c | 31 | "Usage: ... netem [ limit PACKETS ] \n" \ |
ea8fc104 SH |
32 | " [ delay TIME [ JITTER [CORRELATION]]]\n" \ |
33 | " [ distribution {uniform|normal|pareto|paretonormal} ]\n" \ | |
b7be3d0c | 34 | " [ drop PERCENT [CORRELATION]] \n" \ |
a31a5d59 | 35 | " [ corrupt PERCENT [CORRELATION]] \n" \ |
b7be3d0c | 36 | " [ duplicate PERCENT [CORRELATION]]\n" \ |
ea8fc104 | 37 | " [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n"); |
309a4c90 SH |
38 | } |
39 | ||
40 | static void explain1(const char *arg) | |
41 | { | |
42 | fprintf(stderr, "Illegal \"%s\"\n", arg); | |
43 | } | |
44 | ||
45 | #define usage() return(-1) | |
46 | ||
2e21655e SH |
47 | /* |
48 | * Simplistic file parser for distrbution data. | |
49 | * Format is: | |
50 | * # comment line(s) | |
51 | * data0 data1 | |
52 | */ | |
53 | #define MAXDIST 65536 | |
54 | static int get_distribution(const char *type, __s16 *data) | |
b7be3d0c SH |
55 | { |
56 | FILE *f; | |
57 | int n; | |
2e21655e SH |
58 | long x; |
59 | size_t len; | |
fb9b1d0f | 60 | char *line = NULL; |
2e21655e | 61 | char name[128]; |
b7be3d0c | 62 | |
2e21655e SH |
63 | snprintf(name, sizeof(name), "/usr/lib/tc/%s.dist", type); |
64 | if ((f = fopen(name, "r")) == NULL) { | |
ae665a52 | 65 | fprintf(stderr, "No distribution data for %s (%s: %s)\n", |
2e21655e | 66 | type, name, strerror(errno)); |
b7be3d0c SH |
67 | return -1; |
68 | } | |
ae665a52 | 69 | |
b7be3d0c | 70 | n = 0; |
2e21655e SH |
71 | while (getline(&line, &len, f) != -1) { |
72 | char *p, *endp; | |
73 | if (*line == '\n' || *line == '#') | |
b7be3d0c SH |
74 | continue; |
75 | ||
2e21655e SH |
76 | for (p = line; ; p = endp) { |
77 | x = strtol(p, &endp, 0); | |
ae665a52 | 78 | if (endp == p) |
2e21655e SH |
79 | break; |
80 | ||
81 | if (n >= MAXDIST) { | |
82 | fprintf(stderr, "%s: too much data\n", | |
83 | name); | |
84 | n = -1; | |
85 | goto error; | |
86 | } | |
b7be3d0c SH |
87 | data[n++] = x; |
88 | } | |
89 | } | |
2e21655e SH |
90 | error: |
91 | free(line); | |
b7be3d0c | 92 | fclose(f); |
b7be3d0c SH |
93 | return n; |
94 | } | |
95 | ||
ae665a52 | 96 | static int isnumber(const char *arg) |
b7be3d0c SH |
97 | { |
98 | char *p; | |
99 | (void) strtod(arg, &p); | |
100 | return (p != arg); | |
101 | } | |
102 | ||
103 | #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isnumber(argv[1])) | |
104 | ||
ae665a52 | 105 | /* Adjust for the fact that psched_ticks aren't always usecs |
b7be3d0c SH |
106 | (based on kernel PSCHED_CLOCK configuration */ |
107 | static int get_ticks(__u32 *ticks, const char *str) | |
108 | { | |
109 | unsigned t; | |
110 | ||
111 | if(get_usecs(&t, str)) | |
112 | return -1; | |
ae665a52 | 113 | |
fa565130 SH |
114 | if (tc_core_usec2big(t)) { |
115 | fprintf(stderr, "Illegal %d usecs (too large)\n", t); | |
116 | return -1; | |
117 | } | |
118 | ||
b7be3d0c SH |
119 | *ticks = tc_core_usec2tick(t); |
120 | return 0; | |
121 | } | |
122 | ||
123 | static char *sprint_ticks(__u32 ticks, char *buf) | |
124 | { | |
125 | return sprint_usecs(tc_core_tick2usec(ticks), buf); | |
126 | } | |
127 | ||
128 | ||
ae665a52 | 129 | static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv, |
309a4c90 SH |
130 | struct nlmsghdr *n) |
131 | { | |
2e21655e SH |
132 | size_t dist_size = 0; |
133 | struct rtattr *tail; | |
134 | struct tc_netem_qopt opt; | |
135 | struct tc_netem_corr cor; | |
ea8fc104 | 136 | struct tc_netem_reorder reorder; |
a31a5d59 SH |
137 | struct tc_netem_corrupt corrupt; |
138 | __s16 *dist_data = NULL; | |
309a4c90 | 139 | |
2e21655e SH |
140 | memset(&opt, 0, sizeof(opt)); |
141 | opt.limit = 1000; | |
142 | memset(&cor, 0, sizeof(cor)); | |
ea8fc104 | 143 | memset(&reorder, 0, sizeof(reorder)); |
a31a5d59 | 144 | memset(&corrupt, 0, sizeof(corrupt)); |
309a4c90 SH |
145 | |
146 | while (argc > 0) { | |
147 | if (matches(*argv, "limit") == 0) { | |
148 | NEXT_ARG(); | |
2e21655e | 149 | if (get_size(&opt.limit, *argv)) { |
309a4c90 SH |
150 | explain1("limit"); |
151 | return -1; | |
152 | } | |
b7be3d0c SH |
153 | } else if (matches(*argv, "latency") == 0 || |
154 | matches(*argv, "delay") == 0) { | |
309a4c90 | 155 | NEXT_ARG(); |
2e21655e | 156 | if (get_ticks(&opt.latency, *argv)) { |
309a4c90 SH |
157 | explain1("latency"); |
158 | return -1; | |
159 | } | |
b7be3d0c SH |
160 | |
161 | if (NEXT_IS_NUMBER()) { | |
162 | NEXT_ARG(); | |
2e21655e | 163 | if (get_ticks(&opt.jitter, *argv)) { |
b7be3d0c SH |
164 | explain1("latency"); |
165 | return -1; | |
166 | } | |
167 | ||
168 | if (NEXT_IS_NUMBER()) { | |
169 | NEXT_ARG(); | |
ae665a52 | 170 | if (get_percent(&cor.delay_corr, |
b7be3d0c SH |
171 | *argv)) { |
172 | explain1("latency"); | |
173 | return -1; | |
174 | } | |
175 | } | |
176 | } | |
177 | } else if (matches(*argv, "loss") == 0 || | |
178 | matches(*argv, "drop") == 0) { | |
309a4c90 | 179 | NEXT_ARG(); |
2e21655e | 180 | if (get_percent(&opt.loss, *argv)) { |
309a4c90 SH |
181 | explain1("loss"); |
182 | return -1; | |
183 | } | |
b7be3d0c SH |
184 | if (NEXT_IS_NUMBER()) { |
185 | NEXT_ARG(); | |
2e21655e | 186 | if (get_percent(&cor.loss_corr, *argv)) { |
b7be3d0c SH |
187 | explain1("loss"); |
188 | return -1; | |
189 | } | |
190 | } | |
ea8fc104 SH |
191 | } else if (matches(*argv, "reorder") == 0) { |
192 | NEXT_ARG(); | |
193 | if (get_percent(&reorder.probability, *argv)) { | |
194 | explain1("reorder"); | |
195 | return -1; | |
196 | } | |
197 | if (NEXT_IS_NUMBER()) { | |
198 | NEXT_ARG(); | |
199 | if (get_percent(&reorder.correlation, *argv)) { | |
200 | explain1("reorder"); | |
201 | return -1; | |
202 | } | |
203 | } | |
a31a5d59 SH |
204 | } else if (matches(*argv, "corrupt") == 0) { |
205 | NEXT_ARG(); | |
206 | if (get_percent(&corrupt.probability, *argv)) { | |
207 | explain1("corrupt"); | |
208 | return -1; | |
209 | } | |
210 | if (NEXT_IS_NUMBER()) { | |
211 | NEXT_ARG(); | |
212 | if (get_percent(&corrupt.correlation, *argv)) { | |
213 | explain1("corrupt"); | |
214 | return -1; | |
215 | } | |
216 | } | |
309a4c90 | 217 | } else if (matches(*argv, "gap") == 0) { |
309a4c90 | 218 | NEXT_ARG(); |
2e21655e | 219 | if (get_u32(&opt.gap, *argv, 0)) { |
309a4c90 SH |
220 | explain1("gap"); |
221 | return -1; | |
222 | } | |
ffb79d06 | 223 | } else if (matches(*argv, "duplicate") == 0) { |
309a4c90 | 224 | NEXT_ARG(); |
2e21655e | 225 | if (get_percent(&opt.duplicate, *argv)) { |
ffb79d06 | 226 | explain1("duplicate"); |
309a4c90 SH |
227 | return -1; |
228 | } | |
b7be3d0c SH |
229 | if (NEXT_IS_NUMBER()) { |
230 | NEXT_ARG(); | |
2e21655e | 231 | if (get_percent(&cor.dup_corr, *argv)) { |
b7be3d0c SH |
232 | explain1("duplicate"); |
233 | return -1; | |
234 | } | |
235 | } | |
236 | } else if (matches(*argv, "distribution") == 0) { | |
309a4c90 | 237 | NEXT_ARG(); |
a31a5d59 | 238 | dist_data = alloca(MAXDIST); |
2e21655e SH |
239 | dist_size = get_distribution(*argv, dist_data); |
240 | if (dist_size < 0) | |
309a4c90 | 241 | return -1; |
309a4c90 SH |
242 | } else if (strcmp(*argv, "help") == 0) { |
243 | explain(); | |
244 | return -1; | |
245 | } else { | |
246 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
247 | explain(); | |
248 | return -1; | |
249 | } | |
250 | argc--; argv++; | |
251 | } | |
252 | ||
1a1d22a7 | 253 | tail = NLMSG_TAIL(n); |
025dc69a | 254 | |
ea8fc104 SH |
255 | if (reorder.probability) { |
256 | if (opt.latency == 0) { | |
257 | fprintf(stderr, "reordering not possible without specifying some delay\n"); | |
258 | } | |
259 | if (opt.gap == 0) | |
260 | opt.gap = 1; | |
261 | } else if (opt.gap > 0) { | |
262 | fprintf(stderr, "gap specified without reorder probability\n"); | |
263 | explain(); | |
264 | return -1; | |
265 | } | |
266 | ||
a31a5d59 | 267 | if (dist_data && (opt.latency == 0 || opt.jitter == 0)) { |
ea8fc104 SH |
268 | fprintf(stderr, "distribution specified but no latency and jitter values\n"); |
269 | explain(); | |
270 | return -1; | |
271 | } | |
272 | ||
a31a5d59 SH |
273 | if (addattr_l(n, TCA_BUF_MAX, TCA_OPTIONS, &opt, sizeof(opt)) < 0) |
274 | return -1; | |
2e21655e | 275 | |
a31a5d59 SH |
276 | if (cor.delay_corr || cor.loss_corr || cor.dup_corr) { |
277 | if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0) | |
278 | return -1; | |
279 | } | |
280 | ||
e9bc3c40 SH |
281 | if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0) |
282 | return -1; | |
a31a5d59 SH |
283 | |
284 | if (corrupt.probability) { | |
285 | if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0) | |
286 | return -1; | |
287 | } | |
288 | ||
289 | if (dist_data) { | |
290 | if (addattr_l(n, 32768, TCA_NETEM_DELAY_DIST, | |
291 | dist_data, dist_size*sizeof(dist_data[0])) < 0) | |
292 | return -1; | |
2e21655e | 293 | } |
1a1d22a7 | 294 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
b7be3d0c | 295 | return 0; |
309a4c90 SH |
296 | } |
297 | ||
298 | static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
299 | { | |
2e21655e | 300 | const struct tc_netem_corr *cor = NULL; |
ea8fc104 | 301 | const struct tc_netem_reorder *reorder = NULL; |
a31a5d59 | 302 | const struct tc_netem_corrupt *corrupt = NULL; |
2e21655e SH |
303 | struct tc_netem_qopt qopt; |
304 | int len = RTA_PAYLOAD(opt) - sizeof(qopt); | |
309a4c90 | 305 | SPRINT_BUF(b1); |
309a4c90 SH |
306 | |
307 | if (opt == NULL) | |
308 | return 0; | |
309 | ||
2e21655e SH |
310 | if (len < 0) { |
311 | fprintf(stderr, "options size error\n"); | |
309a4c90 | 312 | return -1; |
b7be3d0c | 313 | } |
2e21655e SH |
314 | memcpy(&qopt, RTA_DATA(opt), sizeof(qopt)); |
315 | ||
316 | if (len > 0) { | |
1d2d1cb5 | 317 | struct rtattr *tb[TCA_NETEM_MAX+1]; |
2e21655e SH |
318 | parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt), |
319 | len); | |
ae665a52 | 320 | |
2e21655e SH |
321 | if (tb[TCA_NETEM_CORR]) { |
322 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor)) | |
323 | return -1; | |
324 | cor = RTA_DATA(tb[TCA_NETEM_CORR]); | |
325 | } | |
ea8fc104 SH |
326 | if (tb[TCA_NETEM_REORDER]) { |
327 | if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder)) | |
328 | return -1; | |
329 | reorder = RTA_DATA(tb[TCA_NETEM_REORDER]); | |
330 | } | |
a31a5d59 SH |
331 | if (tb[TCA_NETEM_CORRUPT]) { |
332 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt)) | |
333 | return -1; | |
e9bc3c40 | 334 | corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]); |
a31a5d59 | 335 | } |
2e21655e | 336 | } |
309a4c90 | 337 | |
2e21655e | 338 | fprintf(f, "limit %d", qopt.limit); |
31fa60e0 | 339 | |
2e21655e SH |
340 | if (qopt.latency) { |
341 | fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1)); | |
b7be3d0c | 342 | |
2e21655e SH |
343 | if (qopt.jitter) { |
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)); | |
b7be3d0c SH |
347 | } |
348 | } | |
349 | ||
2e21655e SH |
350 | if (qopt.loss) { |
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)); | |
b7be3d0c SH |
354 | } |
355 | ||
2e21655e | 356 | if (qopt.duplicate) { |
b7be3d0c | 357 | fprintf(f, " duplicate %s", |
2e21655e SH |
358 | sprint_percent(qopt.duplicate, b1)); |
359 | if (cor && cor->dup_corr) | |
360 | fprintf(f, " %s", sprint_percent(cor->dup_corr, b1)); | |
b7be3d0c | 361 | } |
ae665a52 | 362 | |
ea8fc104 | 363 | if (reorder && reorder->probability) { |
ae665a52 | 364 | fprintf(f, " reorder %s", |
ea8fc104 SH |
365 | sprint_percent(reorder->probability, b1)); |
366 | if (reorder->correlation) | |
ae665a52 | 367 | fprintf(f, " %s", |
ea8fc104 SH |
368 | sprint_percent(reorder->correlation, b1)); |
369 | } | |
b7be3d0c | 370 | |
a31a5d59 | 371 | if (corrupt && corrupt->probability) { |
ae665a52 | 372 | fprintf(f, " corrupt %s", |
a31a5d59 SH |
373 | sprint_percent(corrupt->probability, b1)); |
374 | if (corrupt->correlation) | |
ae665a52 | 375 | fprintf(f, " %s", |
a31a5d59 SH |
376 | sprint_percent(corrupt->correlation, b1)); |
377 | } | |
378 | ||
2e21655e SH |
379 | if (qopt.gap) |
380 | fprintf(f, " gap %lu", (unsigned long)qopt.gap); | |
309a4c90 SH |
381 | |
382 | return 0; | |
383 | } | |
384 | ||
95812b56 | 385 | struct qdisc_util netem_qdisc_util = { |
31fa60e0 SH |
386 | .id = "netem", |
387 | .parse_qopt = netem_parse_opt, | |
388 | .print_qopt = netem_print_opt, | |
309a4c90 | 389 | }; |
b7be3d0c | 390 |