]>
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 | * | |
59a935d2 | 9 | * Authors: Stephen Hemminger <shemminger@linux-foundation.org> |
309a4c90 SH |
10 | * |
11 | */ | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
3c7950af SH |
15 | #include <math.h> |
16 | #include <ctype.h> | |
309a4c90 SH |
17 | #include <unistd.h> |
18 | #include <syslog.h> | |
19 | #include <fcntl.h> | |
e4beb527 | 20 | #include <stdint.h> |
309a4c90 SH |
21 | #include <sys/socket.h> |
22 | #include <netinet/in.h> | |
23 | #include <arpa/inet.h> | |
24 | #include <string.h> | |
b7be3d0c | 25 | #include <errno.h> |
309a4c90 SH |
26 | |
27 | #include "utils.h" | |
28 | #include "tc_util.h" | |
b7be3d0c | 29 | #include "tc_common.h" |
309a4c90 SH |
30 | |
31 | static void explain(void) | |
32 | { | |
ae665a52 | 33 | fprintf(stderr, |
32a121cb | 34 | "Usage: ... netem [ limit PACKETS ]\n" \ |
ea8fc104 SH |
35 | " [ delay TIME [ JITTER [CORRELATION]]]\n" \ |
36 | " [ distribution {uniform|normal|pareto|paretonormal} ]\n" \ | |
32a121cb | 37 | " [ corrupt PERCENT [CORRELATION]]\n" \ |
b7be3d0c | 38 | " [ duplicate PERCENT [CORRELATION]]\n" \ |
3c7950af SH |
39 | " [ loss random PERCENT [CORRELATION]]\n" \ |
40 | " [ loss state P13 [P31 [P32 [P23 P14]]]\n" \ | |
41 | " [ loss gemodel PERCENT [R [1-H [1-K]]]\n" \ | |
1070205d | 42 | " [ ecn ]\n" \ |
6b8dc4de HPP |
43 | " [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n" \ |
44 | " [ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]\n"); | |
309a4c90 SH |
45 | } |
46 | ||
47 | static void explain1(const char *arg) | |
48 | { | |
49 | fprintf(stderr, "Illegal \"%s\"\n", arg); | |
50 | } | |
51 | ||
3c7950af | 52 | /* Upper bound on size of distribution |
c1b81cb5 SH |
53 | * really (TCA_BUF_MAX - other headers) / sizeof (__s16) |
54 | */ | |
55 | #define MAX_DIST (16*1024) | |
56 | ||
3c7950af SH |
57 | /* scaled value used to percent of maximum. */ |
58 | static void set_percent(__u32 *percent, double per) | |
59 | { | |
e4beb527 | 60 | *percent = rint(per * UINT32_MAX); |
3c7950af SH |
61 | } |
62 | ||
3c7950af SH |
63 | /* Parse either a fraction '.3' or percent '30% |
64 | * return: 0 = ok, -1 = error, 1 = out of range | |
65 | */ | |
66 | static int parse_percent(double *val, const char *str) | |
67 | { | |
68 | char *p; | |
69 | ||
70 | *val = strtod(str, &p) / 100.; | |
32a121cb | 71 | if (*p && strcmp(p, "%")) |
3c7950af SH |
72 | return -1; |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
77 | static int get_percent(__u32 *percent, const char *str) | |
78 | { | |
79 | double per; | |
80 | ||
81 | if (parse_percent(&per, str)) | |
82 | return -1; | |
83 | ||
84 | set_percent(percent, per); | |
85 | return 0; | |
86 | } | |
87 | ||
d1f28cf1 | 88 | static void print_percent(char *buf, int len, __u32 per) |
3c7950af | 89 | { |
e4beb527 | 90 | snprintf(buf, len, "%g%%", (100. * per) / UINT32_MAX); |
3c7950af SH |
91 | } |
92 | ||
32a121cb | 93 | static char *sprint_percent(__u32 per, char *buf) |
3c7950af SH |
94 | { |
95 | print_percent(buf, SPRINT_BSIZE-1, per); | |
96 | return buf; | |
97 | } | |
98 | ||
2e21655e SH |
99 | /* |
100 | * Simplistic file parser for distrbution data. | |
101 | * Format is: | |
102 | * # comment line(s) | |
c1b81cb5 | 103 | * data0 data1 ... |
2e21655e | 104 | */ |
c1b81cb5 | 105 | static int get_distribution(const char *type, __s16 *data, int maxdata) |
b7be3d0c SH |
106 | { |
107 | FILE *f; | |
108 | int n; | |
2e21655e SH |
109 | long x; |
110 | size_t len; | |
fb9b1d0f | 111 | char *line = NULL; |
2e21655e | 112 | char name[128]; |
b7be3d0c | 113 | |
aa27f88c | 114 | snprintf(name, sizeof(name), "%s/%s.dist", get_tc_lib(), type); |
2e21655e | 115 | if ((f = fopen(name, "r")) == NULL) { |
ae665a52 | 116 | fprintf(stderr, "No distribution data for %s (%s: %s)\n", |
2e21655e | 117 | type, name, strerror(errno)); |
b7be3d0c SH |
118 | return -1; |
119 | } | |
ae665a52 | 120 | |
b7be3d0c | 121 | n = 0; |
2e21655e SH |
122 | while (getline(&line, &len, f) != -1) { |
123 | char *p, *endp; | |
32a121cb | 124 | |
2e21655e | 125 | if (*line == '\n' || *line == '#') |
b7be3d0c SH |
126 | continue; |
127 | ||
2e21655e SH |
128 | for (p = line; ; p = endp) { |
129 | x = strtol(p, &endp, 0); | |
ae665a52 | 130 | if (endp == p) |
2e21655e SH |
131 | break; |
132 | ||
c1b81cb5 | 133 | if (n >= maxdata) { |
2e21655e SH |
134 | fprintf(stderr, "%s: too much data\n", |
135 | name); | |
136 | n = -1; | |
137 | goto error; | |
138 | } | |
b7be3d0c SH |
139 | data[n++] = x; |
140 | } | |
141 | } | |
2e21655e SH |
142 | error: |
143 | free(line); | |
b7be3d0c | 144 | fclose(f); |
b7be3d0c SH |
145 | return n; |
146 | } | |
147 | ||
3c7950af | 148 | #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isdigit(argv[1][0])) |
e72ca3fb JN |
149 | #define NEXT_IS_SIGNED_NUMBER() \ |
150 | (NEXT_ARG_OK() && (isdigit(argv[1][0]) || argv[1][0] == '-')) | |
b7be3d0c | 151 | |
ae665a52 | 152 | /* Adjust for the fact that psched_ticks aren't always usecs |
b7be3d0c SH |
153 | (based on kernel PSCHED_CLOCK configuration */ |
154 | static int get_ticks(__u32 *ticks, const char *str) | |
155 | { | |
32a121cb | 156 | unsigned int t; |
b7be3d0c | 157 | |
32a121cb | 158 | if (get_time(&t, str)) |
b7be3d0c | 159 | return -1; |
ae665a52 | 160 | |
8f34caaf PM |
161 | if (tc_core_time2big(t)) { |
162 | fprintf(stderr, "Illegal %u time (too large)\n", t); | |
fa565130 SH |
163 | return -1; |
164 | } | |
165 | ||
8f34caaf | 166 | *ticks = tc_core_time2tick(t); |
b7be3d0c SH |
167 | return 0; |
168 | } | |
169 | ||
ae665a52 | 170 | static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv, |
309a4c90 SH |
171 | struct nlmsghdr *n) |
172 | { | |
fcbd0165 | 173 | int dist_size = 0; |
2e21655e | 174 | struct rtattr *tail; |
3c7950af | 175 | struct tc_netem_qopt opt = { .limit = 1000 }; |
d17b136f PS |
176 | struct tc_netem_corr cor = {}; |
177 | struct tc_netem_reorder reorder = {}; | |
178 | struct tc_netem_corrupt corrupt = {}; | |
3c7950af SH |
179 | struct tc_netem_gimodel gimodel; |
180 | struct tc_netem_gemodel gemodel; | |
d17b136f | 181 | struct tc_netem_rate rate = {}; |
a31a5d59 | 182 | __s16 *dist_data = NULL; |
3c7950af | 183 | __u16 loss_type = NETEM_LOSS_UNSPEC; |
d17b136f | 184 | int present[__TCA_NETEM_MAX] = {}; |
dad2f72b | 185 | __u64 rate64 = 0; |
309a4c90 | 186 | |
32a121cb | 187 | for ( ; argc > 0; --argc, ++argv) { |
309a4c90 SH |
188 | if (matches(*argv, "limit") == 0) { |
189 | NEXT_ARG(); | |
2e21655e | 190 | if (get_size(&opt.limit, *argv)) { |
309a4c90 SH |
191 | explain1("limit"); |
192 | return -1; | |
193 | } | |
b7be3d0c SH |
194 | } else if (matches(*argv, "latency") == 0 || |
195 | matches(*argv, "delay") == 0) { | |
309a4c90 | 196 | NEXT_ARG(); |
2e21655e | 197 | if (get_ticks(&opt.latency, *argv)) { |
309a4c90 SH |
198 | explain1("latency"); |
199 | return -1; | |
200 | } | |
b7be3d0c SH |
201 | |
202 | if (NEXT_IS_NUMBER()) { | |
203 | NEXT_ARG(); | |
2e21655e | 204 | if (get_ticks(&opt.jitter, *argv)) { |
b7be3d0c SH |
205 | explain1("latency"); |
206 | return -1; | |
207 | } | |
208 | ||
209 | if (NEXT_IS_NUMBER()) { | |
210 | NEXT_ARG(); | |
40076f62 | 211 | ++present[TCA_NETEM_CORR]; |
3c7950af | 212 | if (get_percent(&cor.delay_corr, *argv)) { |
b7be3d0c SH |
213 | explain1("latency"); |
214 | return -1; | |
215 | } | |
216 | } | |
217 | } | |
218 | } else if (matches(*argv, "loss") == 0 || | |
219 | matches(*argv, "drop") == 0) { | |
3c7950af SH |
220 | if (opt.loss > 0 || loss_type != NETEM_LOSS_UNSPEC) { |
221 | explain1("duplicate loss argument\n"); | |
309a4c90 SH |
222 | return -1; |
223 | } | |
3c7950af SH |
224 | |
225 | NEXT_ARG(); | |
226 | /* Old (deprecated) random loss model syntax */ | |
227 | if (isdigit(argv[0][0])) | |
228 | goto random_loss_model; | |
229 | ||
230 | if (!strcmp(*argv, "random")) { | |
b7be3d0c | 231 | NEXT_ARG(); |
268a9eee | 232 | random_loss_model: |
3c7950af SH |
233 | if (get_percent(&opt.loss, *argv)) { |
234 | explain1("loss percent"); | |
235 | return -1; | |
236 | } | |
237 | if (NEXT_IS_NUMBER()) { | |
238 | NEXT_ARG(); | |
239 | ++present[TCA_NETEM_CORR]; | |
240 | if (get_percent(&cor.loss_corr, *argv)) { | |
241 | explain1("loss correllation"); | |
242 | return -1; | |
243 | } | |
244 | } | |
245 | } else if (!strcmp(*argv, "state")) { | |
246 | double p13; | |
247 | ||
248 | NEXT_ARG(); | |
249 | if (parse_percent(&p13, *argv)) { | |
250 | explain1("loss p13"); | |
251 | return -1; | |
252 | } | |
253 | ||
254 | /* set defaults */ | |
255 | set_percent(&gimodel.p13, p13); | |
256 | set_percent(&gimodel.p31, 1. - p13); | |
257 | set_percent(&gimodel.p32, 0); | |
258 | set_percent(&gimodel.p23, 1.); | |
8f9672af | 259 | set_percent(&gimodel.p14, 0); |
3c7950af SH |
260 | loss_type = NETEM_LOSS_GI; |
261 | ||
262 | if (!NEXT_IS_NUMBER()) | |
263 | continue; | |
264 | NEXT_ARG(); | |
265 | if (get_percent(&gimodel.p31, *argv)) { | |
266 | explain1("loss p31"); | |
267 | return -1; | |
268 | } | |
269 | ||
270 | if (!NEXT_IS_NUMBER()) | |
271 | continue; | |
272 | NEXT_ARG(); | |
273 | if (get_percent(&gimodel.p32, *argv)) { | |
274 | explain1("loss p32"); | |
275 | return -1; | |
276 | } | |
277 | ||
278 | if (!NEXT_IS_NUMBER()) | |
279 | continue; | |
280 | NEXT_ARG(); | |
281 | if (get_percent(&gimodel.p23, *argv)) { | |
282 | explain1("loss p23"); | |
283 | return -1; | |
284 | } | |
8f9672af JV |
285 | if (!NEXT_IS_NUMBER()) |
286 | continue; | |
287 | NEXT_ARG(); | |
288 | if (get_percent(&gimodel.p14, *argv)) { | |
289 | explain1("loss p14"); | |
290 | return -1; | |
291 | } | |
3c7950af SH |
292 | |
293 | } else if (!strcmp(*argv, "gemodel")) { | |
294 | NEXT_ARG(); | |
295 | if (get_percent(&gemodel.p, *argv)) { | |
296 | explain1("loss gemodel p"); | |
297 | return -1; | |
298 | } | |
299 | ||
300 | /* set defaults */ | |
301 | set_percent(&gemodel.r, 1.); | |
302 | set_percent(&gemodel.h, 0); | |
3757185b | 303 | set_percent(&gemodel.k1, 0); |
3c7950af SH |
304 | loss_type = NETEM_LOSS_GE; |
305 | ||
306 | if (!NEXT_IS_NUMBER()) | |
307 | continue; | |
308 | NEXT_ARG(); | |
309 | if (get_percent(&gemodel.r, *argv)) { | |
310 | explain1("loss gemodel r"); | |
311 | return -1; | |
312 | } | |
313 | ||
314 | if (!NEXT_IS_NUMBER()) | |
315 | continue; | |
316 | NEXT_ARG(); | |
317 | if (get_percent(&gemodel.h, *argv)) { | |
318 | explain1("loss gemodel h"); | |
319 | return -1; | |
320 | } | |
3757185b JV |
321 | /* netem option is "1-h" but kernel |
322 | * expects "h". | |
323 | */ | |
e4beb527 | 324 | gemodel.h = UINT32_MAX - gemodel.h; |
3c7950af SH |
325 | |
326 | if (!NEXT_IS_NUMBER()) | |
327 | continue; | |
328 | NEXT_ARG(); | |
329 | if (get_percent(&gemodel.k1, *argv)) { | |
330 | explain1("loss gemodel k"); | |
b7be3d0c SH |
331 | return -1; |
332 | } | |
3c7950af SH |
333 | } else { |
334 | fprintf(stderr, "Unknown loss parameter: %s\n", | |
335 | *argv); | |
336 | return -1; | |
b7be3d0c | 337 | } |
1070205d | 338 | } else if (matches(*argv, "ecn") == 0) { |
268a9eee | 339 | present[TCA_NETEM_ECN] = 1; |
ea8fc104 SH |
340 | } else if (matches(*argv, "reorder") == 0) { |
341 | NEXT_ARG(); | |
40076f62 | 342 | present[TCA_NETEM_REORDER] = 1; |
ea8fc104 SH |
343 | if (get_percent(&reorder.probability, *argv)) { |
344 | explain1("reorder"); | |
345 | return -1; | |
346 | } | |
347 | if (NEXT_IS_NUMBER()) { | |
348 | NEXT_ARG(); | |
40076f62 | 349 | ++present[TCA_NETEM_CORR]; |
ea8fc104 SH |
350 | if (get_percent(&reorder.correlation, *argv)) { |
351 | explain1("reorder"); | |
352 | return -1; | |
353 | } | |
354 | } | |
a31a5d59 SH |
355 | } else if (matches(*argv, "corrupt") == 0) { |
356 | NEXT_ARG(); | |
40076f62 | 357 | present[TCA_NETEM_CORRUPT] = 1; |
a31a5d59 SH |
358 | if (get_percent(&corrupt.probability, *argv)) { |
359 | explain1("corrupt"); | |
360 | return -1; | |
361 | } | |
362 | if (NEXT_IS_NUMBER()) { | |
363 | NEXT_ARG(); | |
40076f62 | 364 | ++present[TCA_NETEM_CORR]; |
a31a5d59 SH |
365 | if (get_percent(&corrupt.correlation, *argv)) { |
366 | explain1("corrupt"); | |
367 | return -1; | |
368 | } | |
369 | } | |
309a4c90 | 370 | } else if (matches(*argv, "gap") == 0) { |
309a4c90 | 371 | NEXT_ARG(); |
2e21655e | 372 | if (get_u32(&opt.gap, *argv, 0)) { |
309a4c90 SH |
373 | explain1("gap"); |
374 | return -1; | |
375 | } | |
ffb79d06 | 376 | } else if (matches(*argv, "duplicate") == 0) { |
309a4c90 | 377 | NEXT_ARG(); |
2e21655e | 378 | if (get_percent(&opt.duplicate, *argv)) { |
ffb79d06 | 379 | explain1("duplicate"); |
309a4c90 SH |
380 | return -1; |
381 | } | |
b7be3d0c SH |
382 | if (NEXT_IS_NUMBER()) { |
383 | NEXT_ARG(); | |
2e21655e | 384 | if (get_percent(&cor.dup_corr, *argv)) { |
b7be3d0c SH |
385 | explain1("duplicate"); |
386 | return -1; | |
387 | } | |
388 | } | |
389 | } else if (matches(*argv, "distribution") == 0) { | |
309a4c90 | 390 | NEXT_ARG(); |
c1b81cb5 SH |
391 | dist_data = calloc(sizeof(dist_data[0]), MAX_DIST); |
392 | dist_size = get_distribution(*argv, dist_data, MAX_DIST); | |
393 | if (dist_size <= 0) { | |
394 | free(dist_data); | |
309a4c90 | 395 | return -1; |
c1b81cb5 | 396 | } |
6b8dc4de HPP |
397 | } else if (matches(*argv, "rate") == 0) { |
398 | ++present[TCA_NETEM_RATE]; | |
399 | NEXT_ARG(); | |
dad2f72b | 400 | if (get_rate64(&rate64, *argv)) { |
6b8dc4de HPP |
401 | explain1("rate"); |
402 | return -1; | |
403 | } | |
e72ca3fb | 404 | if (NEXT_IS_SIGNED_NUMBER()) { |
6b8dc4de HPP |
405 | NEXT_ARG(); |
406 | if (get_s32(&rate.packet_overhead, *argv, 0)) { | |
407 | explain1("rate"); | |
408 | return -1; | |
409 | } | |
410 | } | |
411 | if (NEXT_IS_NUMBER()) { | |
412 | NEXT_ARG(); | |
413 | if (get_u32(&rate.cell_size, *argv, 0)) { | |
414 | explain1("rate"); | |
415 | return -1; | |
416 | } | |
417 | } | |
e72ca3fb | 418 | if (NEXT_IS_SIGNED_NUMBER()) { |
6b8dc4de HPP |
419 | NEXT_ARG(); |
420 | if (get_s32(&rate.cell_overhead, *argv, 0)) { | |
421 | explain1("rate"); | |
422 | return -1; | |
423 | } | |
424 | } | |
309a4c90 SH |
425 | } else if (strcmp(*argv, "help") == 0) { |
426 | explain(); | |
427 | return -1; | |
428 | } else { | |
429 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
430 | explain(); | |
431 | return -1; | |
432 | } | |
309a4c90 SH |
433 | } |
434 | ||
1a1d22a7 | 435 | tail = NLMSG_TAIL(n); |
025dc69a | 436 | |
ea8fc104 SH |
437 | if (reorder.probability) { |
438 | if (opt.latency == 0) { | |
439 | fprintf(stderr, "reordering not possible without specifying some delay\n"); | |
14a1c164 VS |
440 | explain(); |
441 | return -1; | |
ea8fc104 SH |
442 | } |
443 | if (opt.gap == 0) | |
444 | opt.gap = 1; | |
445 | } else if (opt.gap > 0) { | |
446 | fprintf(stderr, "gap specified without reorder probability\n"); | |
447 | explain(); | |
448 | return -1; | |
449 | } | |
450 | ||
1070205d VS |
451 | if (present[TCA_NETEM_ECN]) { |
452 | if (opt.loss <= 0 && loss_type == NETEM_LOSS_UNSPEC) { | |
453 | fprintf(stderr, "ecn requested without loss model\n"); | |
454 | explain(); | |
455 | return -1; | |
456 | } | |
457 | } | |
458 | ||
a31a5d59 | 459 | if (dist_data && (opt.latency == 0 || opt.jitter == 0)) { |
ea8fc104 SH |
460 | fprintf(stderr, "distribution specified but no latency and jitter values\n"); |
461 | explain(); | |
462 | return -1; | |
463 | } | |
464 | ||
c1b81cb5 | 465 | if (addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)) < 0) |
a31a5d59 | 466 | return -1; |
2e21655e | 467 | |
40076f62 | 468 | if (present[TCA_NETEM_CORR] && |
c1b81cb5 | 469 | addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0) |
268a9eee | 470 | return -1; |
a31a5d59 | 471 | |
3c7950af | 472 | if (present[TCA_NETEM_REORDER] && |
c1b81cb5 | 473 | addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0) |
e9bc3c40 | 474 | return -1; |
a31a5d59 | 475 | |
1070205d VS |
476 | if (present[TCA_NETEM_ECN] && |
477 | addattr_l(n, 1024, TCA_NETEM_ECN, &present[TCA_NETEM_ECN], | |
478 | sizeof(present[TCA_NETEM_ECN])) < 0) | |
268a9eee | 479 | return -1; |
1070205d | 480 | |
40076f62 | 481 | if (present[TCA_NETEM_CORRUPT] && |
c1b81cb5 | 482 | addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0) |
40076f62 | 483 | return -1; |
a31a5d59 | 484 | |
3c7950af SH |
485 | if (loss_type != NETEM_LOSS_UNSPEC) { |
486 | struct rtattr *start; | |
487 | ||
488 | start = addattr_nest(n, 1024, TCA_NETEM_LOSS | NLA_F_NESTED); | |
489 | if (loss_type == NETEM_LOSS_GI) { | |
490 | if (addattr_l(n, 1024, NETEM_LOSS_GI, | |
491 | &gimodel, sizeof(gimodel)) < 0) | |
268a9eee | 492 | return -1; |
3c7950af SH |
493 | } else if (loss_type == NETEM_LOSS_GE) { |
494 | if (addattr_l(n, 1024, NETEM_LOSS_GE, | |
495 | &gemodel, sizeof(gemodel)) < 0) | |
268a9eee | 496 | return -1; |
3c7950af SH |
497 | } else { |
498 | fprintf(stderr, "loss in the weeds!\n"); | |
499 | return -1; | |
500 | } | |
3d0b7439 | 501 | |
3c7950af SH |
502 | addattr_nest_end(n, start); |
503 | } | |
504 | ||
dad2f72b YY |
505 | if (present[TCA_NETEM_RATE]) { |
506 | if (rate64 >= (1ULL << 32)) { | |
507 | if (addattr_l(n, 1024, | |
508 | TCA_NETEM_RATE64, &rate64, sizeof(rate64)) < 0) | |
509 | return -1; | |
510 | rate.rate = ~0U; | |
511 | } else { | |
512 | rate.rate = rate64; | |
513 | } | |
514 | if (addattr_l(n, 1024, TCA_NETEM_RATE, &rate, sizeof(rate)) < 0) | |
515 | return -1; | |
516 | } | |
6b8dc4de | 517 | |
a31a5d59 | 518 | if (dist_data) { |
c1b81cb5 SH |
519 | if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]), |
520 | TCA_NETEM_DELAY_DIST, | |
521 | dist_data, dist_size * sizeof(dist_data[0])) < 0) | |
a31a5d59 | 522 | return -1; |
c1b81cb5 | 523 | free(dist_data); |
2e21655e | 524 | } |
1a1d22a7 | 525 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
b7be3d0c | 526 | return 0; |
309a4c90 SH |
527 | } |
528 | ||
529 | static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
530 | { | |
2e21655e | 531 | const struct tc_netem_corr *cor = NULL; |
ea8fc104 | 532 | const struct tc_netem_reorder *reorder = NULL; |
a31a5d59 | 533 | const struct tc_netem_corrupt *corrupt = NULL; |
3c7950af SH |
534 | const struct tc_netem_gimodel *gimodel = NULL; |
535 | const struct tc_netem_gemodel *gemodel = NULL; | |
1070205d | 536 | int *ecn = NULL; |
2e21655e | 537 | struct tc_netem_qopt qopt; |
6b8dc4de | 538 | const struct tc_netem_rate *rate = NULL; |
a754de3c | 539 | int len; |
dad2f72b | 540 | __u64 rate64 = 0; |
32a121cb | 541 | |
309a4c90 | 542 | SPRINT_BUF(b1); |
309a4c90 SH |
543 | |
544 | if (opt == NULL) | |
545 | return 0; | |
546 | ||
a754de3c | 547 | len = RTA_PAYLOAD(opt) - sizeof(qopt); |
2e21655e SH |
548 | if (len < 0) { |
549 | fprintf(stderr, "options size error\n"); | |
309a4c90 | 550 | return -1; |
b7be3d0c | 551 | } |
2e21655e SH |
552 | memcpy(&qopt, RTA_DATA(opt), sizeof(qopt)); |
553 | ||
554 | if (len > 0) { | |
1d2d1cb5 | 555 | struct rtattr *tb[TCA_NETEM_MAX+1]; |
32a121cb | 556 | |
2e21655e SH |
557 | parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt), |
558 | len); | |
ae665a52 | 559 | |
2e21655e SH |
560 | if (tb[TCA_NETEM_CORR]) { |
561 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor)) | |
562 | return -1; | |
563 | cor = RTA_DATA(tb[TCA_NETEM_CORR]); | |
564 | } | |
ea8fc104 SH |
565 | if (tb[TCA_NETEM_REORDER]) { |
566 | if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder)) | |
567 | return -1; | |
568 | reorder = RTA_DATA(tb[TCA_NETEM_REORDER]); | |
569 | } | |
a31a5d59 SH |
570 | if (tb[TCA_NETEM_CORRUPT]) { |
571 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt)) | |
572 | return -1; | |
e9bc3c40 | 573 | corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]); |
a31a5d59 | 574 | } |
3c7950af SH |
575 | if (tb[TCA_NETEM_LOSS]) { |
576 | struct rtattr *lb[NETEM_LOSS_MAX + 1]; | |
577 | ||
578 | parse_rtattr_nested(lb, NETEM_LOSS_MAX, tb[TCA_NETEM_LOSS]); | |
579 | if (lb[NETEM_LOSS_GI]) | |
8f9672af | 580 | gimodel = RTA_DATA(lb[NETEM_LOSS_GI]); |
3c7950af SH |
581 | if (lb[NETEM_LOSS_GE]) |
582 | gemodel = RTA_DATA(lb[NETEM_LOSS_GE]); | |
3d0b7439 | 583 | } |
6b8dc4de HPP |
584 | if (tb[TCA_NETEM_RATE]) { |
585 | if (RTA_PAYLOAD(tb[TCA_NETEM_RATE]) < sizeof(*rate)) | |
586 | return -1; | |
587 | rate = RTA_DATA(tb[TCA_NETEM_RATE]); | |
588 | } | |
1070205d VS |
589 | if (tb[TCA_NETEM_ECN]) { |
590 | if (RTA_PAYLOAD(tb[TCA_NETEM_ECN]) < sizeof(*ecn)) | |
591 | return -1; | |
592 | ecn = RTA_DATA(tb[TCA_NETEM_ECN]); | |
593 | } | |
dad2f72b YY |
594 | if (tb[TCA_NETEM_RATE64]) { |
595 | if (RTA_PAYLOAD(tb[TCA_NETEM_RATE64]) < sizeof(rate64)) | |
596 | return -1; | |
597 | rate64 = rta_getattr_u64(tb[TCA_NETEM_RATE64]); | |
598 | } | |
2e21655e | 599 | } |
309a4c90 | 600 | |
2e21655e | 601 | fprintf(f, "limit %d", qopt.limit); |
31fa60e0 | 602 | |
2e21655e SH |
603 | if (qopt.latency) { |
604 | fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1)); | |
b7be3d0c | 605 | |
2e21655e SH |
606 | if (qopt.jitter) { |
607 | fprintf(f, " %s", sprint_ticks(qopt.jitter, b1)); | |
608 | if (cor && cor->delay_corr) | |
609 | fprintf(f, " %s", sprint_percent(cor->delay_corr, b1)); | |
b7be3d0c SH |
610 | } |
611 | } | |
612 | ||
2e21655e SH |
613 | if (qopt.loss) { |
614 | fprintf(f, " loss %s", sprint_percent(qopt.loss, b1)); | |
615 | if (cor && cor->loss_corr) | |
616 | fprintf(f, " %s", sprint_percent(cor->loss_corr, b1)); | |
b7be3d0c SH |
617 | } |
618 | ||
3c7950af SH |
619 | if (gimodel) { |
620 | fprintf(f, " loss state p13 %s", sprint_percent(gimodel->p13, b1)); | |
621 | fprintf(f, " p31 %s", sprint_percent(gimodel->p31, b1)); | |
622 | fprintf(f, " p32 %s", sprint_percent(gimodel->p32, b1)); | |
623 | fprintf(f, " p23 %s", sprint_percent(gimodel->p23, b1)); | |
624 | fprintf(f, " p14 %s", sprint_percent(gimodel->p14, b1)); | |
625 | } | |
626 | ||
627 | if (gemodel) { | |
3757185b | 628 | fprintf(f, " loss gemodel p %s", |
3c7950af SH |
629 | sprint_percent(gemodel->p, b1)); |
630 | fprintf(f, " r %s", sprint_percent(gemodel->r, b1)); | |
e4beb527 | 631 | fprintf(f, " 1-h %s", sprint_percent(UINT32_MAX - |
3757185b | 632 | gemodel->h, b1)); |
3c7950af SH |
633 | fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1)); |
634 | } | |
635 | ||
2e21655e | 636 | if (qopt.duplicate) { |
b7be3d0c | 637 | fprintf(f, " duplicate %s", |
2e21655e SH |
638 | sprint_percent(qopt.duplicate, b1)); |
639 | if (cor && cor->dup_corr) | |
640 | fprintf(f, " %s", sprint_percent(cor->dup_corr, b1)); | |
b7be3d0c | 641 | } |
ae665a52 | 642 | |
ea8fc104 | 643 | if (reorder && reorder->probability) { |
ae665a52 | 644 | fprintf(f, " reorder %s", |
ea8fc104 SH |
645 | sprint_percent(reorder->probability, b1)); |
646 | if (reorder->correlation) | |
ae665a52 | 647 | fprintf(f, " %s", |
ea8fc104 SH |
648 | sprint_percent(reorder->correlation, b1)); |
649 | } | |
b7be3d0c | 650 | |
a31a5d59 | 651 | if (corrupt && corrupt->probability) { |
ae665a52 | 652 | fprintf(f, " corrupt %s", |
a31a5d59 SH |
653 | sprint_percent(corrupt->probability, b1)); |
654 | if (corrupt->correlation) | |
ae665a52 | 655 | fprintf(f, " %s", |
a31a5d59 SH |
656 | sprint_percent(corrupt->correlation, b1)); |
657 | } | |
658 | ||
6b8dc4de | 659 | if (rate && rate->rate) { |
dad2f72b YY |
660 | if (rate64) |
661 | fprintf(f, " rate %s", sprint_rate(rate64, b1)); | |
662 | else | |
663 | fprintf(f, " rate %s", sprint_rate(rate->rate, b1)); | |
6b8dc4de HPP |
664 | if (rate->packet_overhead) |
665 | fprintf(f, " packetoverhead %d", rate->packet_overhead); | |
666 | if (rate->cell_size) | |
667 | fprintf(f, " cellsize %u", rate->cell_size); | |
668 | if (rate->cell_overhead) | |
669 | fprintf(f, " celloverhead %d", rate->cell_overhead); | |
670 | } | |
671 | ||
1070205d VS |
672 | if (ecn) |
673 | fprintf(f, " ecn "); | |
674 | ||
2e21655e SH |
675 | if (qopt.gap) |
676 | fprintf(f, " gap %lu", (unsigned long)qopt.gap); | |
309a4c90 | 677 | |
1070205d | 678 | |
309a4c90 SH |
679 | return 0; |
680 | } | |
681 | ||
95812b56 | 682 | struct qdisc_util netem_qdisc_util = { |
32a121cb | 683 | .id = "netem", |
31fa60e0 SH |
684 | .parse_qopt = netem_parse_opt, |
685 | .print_qopt = netem_print_opt, | |
309a4c90 | 686 | }; |