-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcli_parser.c
More file actions
316 lines (303 loc) · 9.65 KB
/
cli_parser.c
File metadata and controls
316 lines (303 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "cli_parser.h"
#include <getopt.h>
int powerOfTwo(int n)
{
return n && (!(n & (n - 1)));
}
static int64_t
parse_int(const char *arg)
{
char *end = NULL;
int64_t result = strtoll(arg, &end, 10);
if ((arg[0] == '\0') || (end == NULL) || (*end != '\0'))
return -1;
return result;
}
/*
* helper function for parse_mac, parse one section of the ether addr.
*/
static const char *
parse_uint8x16(const char *s, uint8_t *v, uint8_t ls)
{
char *end;
unsigned long t;
errno = 0;
t = strtoul(s, &end, 16);
if (errno != 0 || end[0] != ls || t > UINT8_MAX)
return NULL;
v[0] = t;
return end + 1;
}
static int
parse_mac(const char *str, struct rte_ether_addr *addr)
{
uint32_t i;
static const uint8_t stop_sym[RTE_DIM(addr->addr_bytes)] = {
[0] = ':',
[1] = ':',
[2] = ':',
[3] = ':',
[4] = ':',
[5] = 0,
};
for (i = 0; i != RTE_DIM(addr->addr_bytes); i++)
{
str = parse_uint8x16(str, addr->addr_bytes + i, stop_sym[i]);
if (str == NULL)
return -EINVAL;
}
return 0;
}
void zmonkey_usage()
{
printf("\n\nzmonkey [EAL options] -- <Parameters>\n\n");
printf(" -f --first_lcore First lcore used for forwarding thread\n"
" -n --core_num Number of lcore used for forwarding\n"
" -m --mbuf_size Number of elements in mbuf\n"
" -r --ring_size Number of elements in mbuf\n"
" -c --control_port Remote control udp port(default:6666)\n"
"\nzMonkey chaos config\n\n"
" -d --l2r_latency Left -> Right Delay time [us]\n"
" -D --r2l_latency Right -> Left Delay time [us]\n"
" -j --l2r_jitter Left -> Right Jitter time [us]\n"
" -J --r2l_jitter Right -> Left Jitter time [us]\n"
" -l --l2r_loss Left -> Right Loss rate [%%%%]\n"
" -L --r2l_loss Right -> Left Loss rate [%%%%]\n"
" -u --l2r_dup Left -> Right Duplicate rate [%%%%]\n"
" -U --r2l_dup Right -> Left Duplicate rate [%%%%]\n\n\n"
" -y --r2l_src_mac Right -> Left Source MAC[xx:xx:xx:xx:xx:xx]\n"
" -Y --r2l_dst_mac Right -> Left Destination MAC[xx:xx:xx:xx:xx:xx]\n"
" -z --l2r_src_mac Left -> Right Source MAC[xx:xx:xx:xx:xx:xx]\n"
" -Z --l2r_dst_mac Left -> Right Destination MAC[xx:xx:xx:xx:xx:xx]\n\n\n"
"Example:\n\n"
"8-Thread to handle 100G/100ms latency with 12.34%% packet drop_rate\n\n"
" zmonkey -- --first_lcore 24 --core_num 8 --mbuf_size 2097152 --l2r_latency 100000 --l2r_loss 1234\n\n"
"16-Thread with 1M element buffer per thread,first lcore start at core.24\n\n"
" zmonkey -- --first_lcore 24 --core_num 16 --mbuf_size 1048576 --l2r_latency 10000 \n\n"
"Short parameters\n\n"
" zmonkey -- -f 24 -n 12 -m 2097152 -d 100000 -D 100000\n\n\n");
}
int zmonkey_args_parser(int argc, char **argv, struct config *config)
{
char *l_opt_arg;
char *const short_options = "a:f:n:m:r:c:d:D:j:J:l:L:u:U:y:Y:z:Z:h";
struct option long_options[] = {
{"file-prefix", 1, NULL, 'x'},
{"first_lcore", 1, NULL, 'f'},
{"core_num", 1, NULL, 'n'},
{"mbuf_size", 1, NULL, 'm'},
{"ring_size", 1, NULL, 'r'},
{"control_port", 1, NULL, 'c'},
{"l2r_latency", 1, NULL, 'd'},
{"r2l_latency", 1, NULL, 'D'},
{"l2r_jitter", 1, NULL, 'j'},
{"r2l_jitter", 1, NULL, 'J'},
{"l2r_loss", 1, NULL, 'l'},
{"r2l_loss", 1, NULL, 'L'},
{"l2r_dup", 1, NULL, 'u'},
{"r2l_dup", 1, NULL, 'U'},
{"r2l_src_mac", 1, NULL, 'y'},
{"r2l_dst_mac", 1, NULL, 'Y'},
{"l2r_src_mac", 1, NULL, 'z'},
{"l2r_dst_mac", 1, NULL, 'Z'},
{0, 0, 0, 0},
};
int c;
int64_t val;
while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1)
{
switch (c)
{
// first lcore
case 'f':
val = parse_int(optarg);
if (val <= 0 || val > MAX_SERVICE_CORE)
{
printf("Invalid firstcore value: %ld\n", val);
zmonkey_usage();
return -1;
}
config->first_lcore = val;
break;
// num of core
case 'n':
val = parse_int(optarg);
if (val <= 0 || val > MAX_SERVICE_CORE)
{
printf("Invalid num of core value: %ld\n", val);
zmonkey_usage();
return -1;
}
config->num_service_core = val;
break;
// mbuf size
case 'm':
val = parse_int(optarg);
if ((val <= 0) || !powerOfTwo(val))
{
printf("Invalid mbuf size: %ld , must be the power of two\n", val);
zmonkey_usage();
return -1;
}
config->mbuf_size = val;
break;
// ring size
case 'r':
val = parse_int(optarg);
if ((val <= 0) || !powerOfTwo(val))
{
printf("Invalid ring size: %ld , must be the power of two\n", val);
zmonkey_usage();
return -1;
}
config->delay_ring_size = val;
break;
// control port
case 'c':
val = parse_int(optarg);
if ((val < 1) || (val > 65535))
{
printf("Invalid control port: %ld\n", val);
zmonkey_usage();
return -1;
}
config->ctrl_udp_port = val;
break;
// Latency
case 'd':
val = parse_int(optarg);
if (val < 1)
{
printf("Invalid latency: %ld\n", val);
zmonkey_usage();
return -1;
}
config->latency[0] = val;
break;
// Latency
case 'D':
val = parse_int(optarg);
if (val < 1)
{
printf("Invalid latency: %ld\n", val);
zmonkey_usage();
return -1;
}
config->latency[1] = val;
break;
// Jitter
case 'j':
val = parse_int(optarg);
if (val < 1)
{
printf("Invalid jitter: %ld\n", val);
zmonkey_usage();
return -1;
}
config->jitter[0] = val;
break;
// Jitter
case 'J':
val = parse_int(optarg);
if (val < 1)
{
printf("Invalid jitter: %ld\n", val);
zmonkey_usage();
return -1;
}
config->jitter[1] = val;
break;
// Loss
case 'l':
val = parse_int(optarg);
if ((val < 1) || (val > 10000))
{
printf("Invalid loss rate: %ld\n", val);
zmonkey_usage();
return -1;
}
config->drop_rate[0] = val;
break;
// Loss
case 'L':
val = parse_int(optarg);
if ((val < 1) || (val > 10000))
{
printf("Invalid loss rate: %ld\n", val);
zmonkey_usage();
return -1;
}
config->drop_rate[1] = val;
break;
// Duplicate
case 'u':
val = parse_int(optarg);
if ((val < 1) || (val > 10000))
{
printf("Invalid duplicate rate: %ld\n", val);
zmonkey_usage();
return -1;
}
config->dup_rate[0] = val;
break;
// Loss
case 'U':
val = parse_int(optarg);
if ((val < 1) || (val > 10000))
{
printf("Invalid duplicate rate: %ld\n", val);
zmonkey_usage();
return -1;
}
config->dup_rate[1] = val;
break;
case 'y':
config->enable_mac_update = 1;
val = parse_mac(optarg, &config->src_mac[0]);
if (val != 0)
{
printf("Invalid r2l Source MAC address: %s\n", optarg);
zmonkey_usage();
return -1;
}
case 'Y':
config->enable_mac_update = 1;
val = parse_mac(optarg, &config->dst_mac[0]);
if (val != 0)
{
printf("Invalid r2l Destination MAC address: %s\n", optarg);
zmonkey_usage();
return -1;
}
case 'z':
config->enable_mac_update = 1;
val = parse_mac(optarg, &config->src_mac[1]);
if (val != 0)
{
printf("Invalid l2r Source MAC address: %s\n", optarg);
zmonkey_usage();
return -1;
}
case 'Z':
config->enable_mac_update = 1;
val = parse_mac(optarg, &config->dst_mac[1]);
if (val != 0)
{
printf("Invalid l2r Destination MAC address: %s\n", optarg);
zmonkey_usage();
return -1;
}
case 'a':
break;
case 'x':
break;
case 'h':
zmonkey_usage();
return -1;
default:
zmonkey_usage();
return -1;
}
}
return 0;
}