]>
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 | { | |
ffb79d06 | 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) { | |
b7be3d0c | 65 | fprintf(stderr, "No distribution data for %s (%s: %s)\n", |
2e21655e | 66 | type, name, strerror(errno)); |
b7be3d0c SH |
67 | return -1; |
68 | } | |
69 | ||
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); | |
78 | if (endp == p) | |
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 | ||
96 | static int isnumber(const char *arg) | |
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 | ||
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) | |
108 | { | |
109 | unsigned t; | |
110 | ||
111 | if(get_usecs(&t, str)) | |
112 | return -1; | |
113 | ||
114 | *ticks = tc_core_usec2tick(t); | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static char *sprint_ticks(__u32 ticks, char *buf) | |
119 | { | |
120 | return sprint_usecs(tc_core_tick2usec(ticks), buf); | |
121 | } | |
122 | ||
123 | ||
309a4c90 SH |
124 | static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv, |
125 | struct nlmsghdr *n) | |
126 | { | |
2e21655e SH |
127 | size_t dist_size = 0; |
128 | struct rtattr *tail; | |
129 | struct tc_netem_qopt opt; | |
130 | struct tc_netem_corr cor; | |
ea8fc104 | 131 | struct tc_netem_reorder reorder; |
a31a5d59 SH |
132 | struct tc_netem_corrupt corrupt; |
133 | __s16 *dist_data = NULL; | |
309a4c90 | 134 | |
2e21655e SH |
135 | memset(&opt, 0, sizeof(opt)); |
136 | opt.limit = 1000; | |
137 | memset(&cor, 0, sizeof(cor)); | |
ea8fc104 | 138 | memset(&reorder, 0, sizeof(reorder)); |
a31a5d59 | 139 | memset(&corrupt, 0, sizeof(corrupt)); |
309a4c90 SH |
140 | |
141 | while (argc > 0) { | |
142 | if (matches(*argv, "limit") == 0) { | |
143 | NEXT_ARG(); | |
2e21655e | 144 | if (get_size(&opt.limit, *argv)) { |
309a4c90 SH |
145 | explain1("limit"); |
146 | return -1; | |
147 | } | |
b7be3d0c SH |
148 | } else if (matches(*argv, "latency") == 0 || |
149 | matches(*argv, "delay") == 0) { | |
309a4c90 | 150 | NEXT_ARG(); |
2e21655e | 151 | if (get_ticks(&opt.latency, *argv)) { |
309a4c90 SH |
152 | explain1("latency"); |
153 | return -1; | |
154 | } | |
b7be3d0c SH |
155 | |
156 | if (NEXT_IS_NUMBER()) { | |
157 | NEXT_ARG(); | |
2e21655e | 158 | if (get_ticks(&opt.jitter, *argv)) { |
b7be3d0c SH |
159 | explain1("latency"); |
160 | return -1; | |
161 | } | |
162 | ||
163 | if (NEXT_IS_NUMBER()) { | |
164 | NEXT_ARG(); | |
2e21655e | 165 | if (get_percent(&cor.delay_corr, |
b7be3d0c SH |
166 | *argv)) { |
167 | explain1("latency"); | |
168 | return -1; | |
169 | } | |
170 | } | |
171 | } | |
172 | } else if (matches(*argv, "loss") == 0 || | |
173 | matches(*argv, "drop") == 0) { | |
309a4c90 | 174 | NEXT_ARG(); |
2e21655e | 175 | if (get_percent(&opt.loss, *argv)) { |
309a4c90 SH |
176 | explain1("loss"); |
177 | return -1; | |
178 | } | |
b7be3d0c SH |
179 | if (NEXT_IS_NUMBER()) { |
180 | NEXT_ARG(); | |
2e21655e | 181 | if (get_percent(&cor.loss_corr, *argv)) { |
b7be3d0c SH |
182 | explain1("loss"); |
183 | return -1; | |
184 | } | |
185 | } | |
ea8fc104 SH |
186 | } else if (matches(*argv, "reorder") == 0) { |
187 | NEXT_ARG(); | |
188 | if (get_percent(&reorder.probability, *argv)) { | |
189 | explain1("reorder"); | |
190 | return -1; | |
191 | } | |
192 | if (NEXT_IS_NUMBER()) { | |
193 | NEXT_ARG(); | |
194 | if (get_percent(&reorder.correlation, *argv)) { | |
195 | explain1("reorder"); | |
196 | return -1; | |
197 | } | |
198 | } | |
a31a5d59 SH |
199 | } else if (matches(*argv, "corrupt") == 0) { |
200 | NEXT_ARG(); | |
201 | if (get_percent(&corrupt.probability, *argv)) { | |
202 | explain1("corrupt"); | |
203 | return -1; | |
204 | } | |
205 | if (NEXT_IS_NUMBER()) { | |
206 | NEXT_ARG(); | |
207 | if (get_percent(&corrupt.correlation, *argv)) { | |
208 | explain1("corrupt"); | |
209 | return -1; | |
210 | } | |
211 | } | |
309a4c90 | 212 | } else if (matches(*argv, "gap") == 0) { |
309a4c90 | 213 | NEXT_ARG(); |
2e21655e | 214 | if (get_u32(&opt.gap, *argv, 0)) { |
309a4c90 SH |
215 | explain1("gap"); |
216 | return -1; | |
217 | } | |
ffb79d06 | 218 | } else if (matches(*argv, "duplicate") == 0) { |
309a4c90 | 219 | NEXT_ARG(); |
2e21655e | 220 | if (get_percent(&opt.duplicate, *argv)) { |
ffb79d06 | 221 | explain1("duplicate"); |
309a4c90 SH |
222 | return -1; |
223 | } | |
b7be3d0c SH |
224 | if (NEXT_IS_NUMBER()) { |
225 | NEXT_ARG(); | |
2e21655e | 226 | if (get_percent(&cor.dup_corr, *argv)) { |
b7be3d0c SH |
227 | explain1("duplicate"); |
228 | return -1; | |
229 | } | |
230 | } | |
231 | } else if (matches(*argv, "distribution") == 0) { | |
309a4c90 | 232 | NEXT_ARG(); |
a31a5d59 | 233 | dist_data = alloca(MAXDIST); |
2e21655e SH |
234 | dist_size = get_distribution(*argv, dist_data); |
235 | if (dist_size < 0) | |
309a4c90 | 236 | return -1; |
309a4c90 SH |
237 | } else if (strcmp(*argv, "help") == 0) { |
238 | explain(); | |
239 | return -1; | |
240 | } else { | |
241 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
242 | explain(); | |
243 | return -1; | |
244 | } | |
245 | argc--; argv++; | |
246 | } | |
247 | ||
1a1d22a7 | 248 | tail = NLMSG_TAIL(n); |
025dc69a | 249 | |
ea8fc104 SH |
250 | if (reorder.probability) { |
251 | if (opt.latency == 0) { | |
252 | fprintf(stderr, "reordering not possible without specifying some delay\n"); | |
253 | } | |
254 | if (opt.gap == 0) | |
255 | opt.gap = 1; | |
256 | } else if (opt.gap > 0) { | |
257 | fprintf(stderr, "gap specified without reorder probability\n"); | |
258 | explain(); | |
259 | return -1; | |
260 | } | |
261 | ||
a31a5d59 | 262 | if (dist_data && (opt.latency == 0 || opt.jitter == 0)) { |
ea8fc104 SH |
263 | fprintf(stderr, "distribution specified but no latency and jitter values\n"); |
264 | explain(); | |
265 | return -1; | |
266 | } | |
267 | ||
a31a5d59 SH |
268 | if (addattr_l(n, TCA_BUF_MAX, TCA_OPTIONS, &opt, sizeof(opt)) < 0) |
269 | return -1; | |
2e21655e | 270 | |
a31a5d59 SH |
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) | |
273 | return -1; | |
274 | } | |
275 | ||
276 | if (reorder.probability) { | |
277 | if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0) | |
278 | return -1; | |
279 | } | |
280 | ||
281 | if (corrupt.probability) { | |
282 | if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0) | |
283 | return -1; | |
284 | } | |
285 | ||
286 | if (dist_data) { | |
287 | if (addattr_l(n, 32768, TCA_NETEM_DELAY_DIST, | |
288 | dist_data, dist_size*sizeof(dist_data[0])) < 0) | |
289 | return -1; | |
2e21655e | 290 | } |
1a1d22a7 | 291 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
b7be3d0c | 292 | return 0; |
309a4c90 SH |
293 | } |
294 | ||
295 | static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
296 | { | |
2e21655e | 297 | const struct tc_netem_corr *cor = NULL; |
ea8fc104 | 298 | const struct tc_netem_reorder *reorder = NULL; |
a31a5d59 | 299 | const struct tc_netem_corrupt *corrupt = NULL; |
2e21655e SH |
300 | struct tc_netem_qopt qopt; |
301 | int len = RTA_PAYLOAD(opt) - sizeof(qopt); | |
309a4c90 | 302 | SPRINT_BUF(b1); |
309a4c90 SH |
303 | |
304 | if (opt == NULL) | |
305 | return 0; | |
306 | ||
2e21655e SH |
307 | if (len < 0) { |
308 | fprintf(stderr, "options size error\n"); | |
309a4c90 | 309 | return -1; |
b7be3d0c | 310 | } |
2e21655e SH |
311 | memcpy(&qopt, RTA_DATA(opt), sizeof(qopt)); |
312 | ||
313 | if (len > 0) { | |
1d2d1cb5 | 314 | struct rtattr *tb[TCA_NETEM_MAX+1]; |
2e21655e SH |
315 | parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt), |
316 | len); | |
317 | ||
318 | if (tb[TCA_NETEM_CORR]) { | |
319 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor)) | |
320 | return -1; | |
321 | cor = RTA_DATA(tb[TCA_NETEM_CORR]); | |
322 | } | |
ea8fc104 SH |
323 | if (tb[TCA_NETEM_REORDER]) { |
324 | if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder)) | |
325 | return -1; | |
326 | reorder = RTA_DATA(tb[TCA_NETEM_REORDER]); | |
327 | } | |
a31a5d59 SH |
328 | if (tb[TCA_NETEM_CORRUPT]) { |
329 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt)) | |
330 | return -1; | |
331 | corrupt = RTA_DATA(tb[TCA_NETEM_REORDER]); | |
332 | } | |
2e21655e | 333 | } |
309a4c90 | 334 | |
2e21655e | 335 | fprintf(f, "limit %d", qopt.limit); |
31fa60e0 | 336 | |
2e21655e SH |
337 | if (qopt.latency) { |
338 | fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1)); | |
b7be3d0c | 339 | |
2e21655e SH |
340 | if (qopt.jitter) { |
341 | fprintf(f, " %s", sprint_ticks(qopt.jitter, b1)); | |
342 | if (cor && cor->delay_corr) | |
343 | fprintf(f, " %s", sprint_percent(cor->delay_corr, b1)); | |
b7be3d0c SH |
344 | } |
345 | } | |
346 | ||
2e21655e SH |
347 | if (qopt.loss) { |
348 | fprintf(f, " loss %s", sprint_percent(qopt.loss, b1)); | |
349 | if (cor && cor->loss_corr) | |
350 | fprintf(f, " %s", sprint_percent(cor->loss_corr, b1)); | |
b7be3d0c SH |
351 | } |
352 | ||
2e21655e | 353 | if (qopt.duplicate) { |
b7be3d0c | 354 | fprintf(f, " duplicate %s", |
2e21655e SH |
355 | sprint_percent(qopt.duplicate, b1)); |
356 | if (cor && cor->dup_corr) | |
357 | fprintf(f, " %s", sprint_percent(cor->dup_corr, b1)); | |
b7be3d0c | 358 | } |
ea8fc104 SH |
359 | |
360 | if (reorder && reorder->probability) { | |
361 | fprintf(f, " reorder %s", | |
362 | sprint_percent(reorder->probability, b1)); | |
363 | if (reorder->correlation) | |
364 | fprintf(f, " %s", | |
365 | sprint_percent(reorder->correlation, b1)); | |
366 | } | |
b7be3d0c | 367 | |
a31a5d59 SH |
368 | if (corrupt && corrupt->probability) { |
369 | fprintf(f, " corrupt %s", | |
370 | sprint_percent(corrupt->probability, b1)); | |
371 | if (corrupt->correlation) | |
372 | fprintf(f, " %s", | |
373 | sprint_percent(corrupt->correlation, b1)); | |
374 | } | |
375 | ||
2e21655e SH |
376 | if (qopt.gap) |
377 | fprintf(f, " gap %lu", (unsigned long)qopt.gap); | |
309a4c90 SH |
378 | |
379 | return 0; | |
380 | } | |
381 | ||
95812b56 | 382 | struct qdisc_util netem_qdisc_util = { |
31fa60e0 SH |
383 | .id = "netem", |
384 | .parse_qopt = netem_parse_opt, | |
385 | .print_qopt = netem_print_opt, | |
309a4c90 | 386 | }; |
b7be3d0c | 387 |