]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_netem.c
tb buffer initialization is now done in the parser
[mirror_iproute2.git] / tc / q_netem.c
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>
22 #include <errno.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26 #include "tc_common.h"
27
28 static void explain(void)
29 {
30 fprintf(stderr,
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");
37 }
38
39 static void explain1(const char *arg)
40 {
41 fprintf(stderr, "Illegal \"%s\"\n", arg);
42 }
43
44 #define usage() return(-1)
45
46 /*
47 * Simplistic file parser for distrbution data.
48 * Format is:
49 * # comment line(s)
50 * data0 data1
51 */
52 #define MAXDIST 65536
53 static int get_distribution(const char *type, __s16 *data)
54 {
55 FILE *f;
56 int n;
57 long x;
58 size_t len;
59 char *line;
60 char name[128];
61
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));
66 return -1;
67 }
68
69 n = 0;
70 while (getline(&line, &len, f) != -1) {
71 char *p, *endp;
72 if (*line == '\n' || *line == '#')
73 continue;
74
75 for (p = line; ; p = endp) {
76 x = strtol(p, &endp, 0);
77 if (endp == p)
78 break;
79
80 if (n >= MAXDIST) {
81 fprintf(stderr, "%s: too much data\n",
82 name);
83 n = -1;
84 goto error;
85 }
86 data[n++] = x;
87 }
88 }
89 error:
90 free(line);
91 fclose(f);
92 return n;
93 }
94
95 static int isnumber(const char *arg)
96 {
97 char *p;
98 (void) strtod(arg, &p);
99 return (p != arg);
100 }
101
102 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isnumber(argv[1]))
103
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)
107 {
108 unsigned t;
109
110 if(get_usecs(&t, str))
111 return -1;
112
113 *ticks = tc_core_usec2tick(t);
114 return 0;
115 }
116
117 static char *sprint_ticks(__u32 ticks, char *buf)
118 {
119 return sprint_usecs(tc_core_tick2usec(ticks), buf);
120 }
121
122
123 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
124 struct nlmsghdr *n)
125 {
126 size_t dist_size = 0;
127 struct rtattr *tail;
128 struct tc_netem_qopt opt;
129 struct tc_netem_corr cor;
130 __s16 dist_data[MAXDIST];
131
132 memset(&opt, 0, sizeof(opt));
133 opt.limit = 1000;
134 memset(&cor, 0, sizeof(cor));
135
136 while (argc > 0) {
137 if (matches(*argv, "limit") == 0) {
138 NEXT_ARG();
139 if (get_size(&opt.limit, *argv)) {
140 explain1("limit");
141 return -1;
142 }
143 } else if (matches(*argv, "latency") == 0 ||
144 matches(*argv, "delay") == 0) {
145 NEXT_ARG();
146 if (get_ticks(&opt.latency, *argv)) {
147 explain1("latency");
148 return -1;
149 }
150
151 if (NEXT_IS_NUMBER()) {
152 NEXT_ARG();
153 if (get_ticks(&opt.jitter, *argv)) {
154 explain1("latency");
155 return -1;
156 }
157
158 if (NEXT_IS_NUMBER()) {
159 NEXT_ARG();
160 if (get_percent(&cor.delay_corr,
161 *argv)) {
162 explain1("latency");
163 return -1;
164 }
165 }
166 }
167 } else if (matches(*argv, "loss") == 0 ||
168 matches(*argv, "drop") == 0) {
169 NEXT_ARG();
170 if (get_percent(&opt.loss, *argv)) {
171 explain1("loss");
172 return -1;
173 }
174 if (NEXT_IS_NUMBER()) {
175 NEXT_ARG();
176 if (get_percent(&cor.loss_corr, *argv)) {
177 explain1("loss");
178 return -1;
179 }
180 }
181 } else if (matches(*argv, "gap") == 0) {
182 NEXT_ARG();
183 if (get_u32(&opt.gap, *argv, 0)) {
184 explain1("gap");
185 return -1;
186 }
187 } else if (matches(*argv, "duplicate") == 0) {
188 NEXT_ARG();
189 if (get_percent(&opt.duplicate, *argv)) {
190 explain1("duplicate");
191 return -1;
192 }
193 if (NEXT_IS_NUMBER()) {
194 NEXT_ARG();
195 if (get_percent(&cor.dup_corr, *argv)) {
196 explain1("duplicate");
197 return -1;
198 }
199 }
200 } else if (matches(*argv, "distribution") == 0) {
201 NEXT_ARG();
202 dist_size = get_distribution(*argv, dist_data);
203 if (dist_size < 0)
204 return -1;
205 } else if (strcmp(*argv, "help") == 0) {
206 explain();
207 return -1;
208 } else {
209 fprintf(stderr, "What is \"%s\"?\n", *argv);
210 explain();
211 return -1;
212 }
213 argc--; argv++;
214 }
215
216 tail = NLMSG_TAIL(n);
217
218 addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
219 addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor));
220
221 if (dist_size > 0) {
222 addattr_l(n, 32768, TCA_NETEM_DELAY_DIST,
223 dist_data, dist_size*sizeof(dist_data[0]));
224 }
225 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
226 return 0;
227 }
228
229 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
230 {
231 const struct tc_netem_corr *cor = NULL;
232 struct tc_netem_qopt qopt;
233 int len = RTA_PAYLOAD(opt) - sizeof(qopt);
234 SPRINT_BUF(b1);
235
236 if (opt == NULL)
237 return 0;
238
239 if (len < 0) {
240 fprintf(stderr, "options size error\n");
241 return -1;
242 }
243 memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
244
245 if (len > 0) {
246 struct rtattr *tb[TCA_NETEM_MAX];
247 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
248 len);
249
250 if (tb[TCA_NETEM_CORR]) {
251 if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
252 return -1;
253 cor = RTA_DATA(tb[TCA_NETEM_CORR]);
254 }
255 }
256
257 fprintf(f, "limit %d", qopt.limit);
258
259 if (qopt.latency) {
260 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
261
262 if (qopt.jitter) {
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));
266 }
267 }
268
269 if (qopt.loss) {
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));
273 }
274
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));
280 }
281
282 if (qopt.gap)
283 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
284
285 return 0;
286 }
287
288 struct qdisc_util netem_qdisc_util = {
289 .id = "netem",
290 .parse_qopt = netem_parse_opt,
291 .print_qopt = netem_print_opt,
292 };
293