forked from zartbot/zmonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_parser.c
231 lines (224 loc) · 7.17 KB
/
cli_parser.c
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
#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;
}
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"
"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: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'},
{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 'a':
break;
case 'x':
break;
case 'h':
zmonkey_usage();
return -1;
default:
zmonkey_usage();
return -1;
}
}
return 0;
}