-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.c
285 lines (249 loc) · 11.9 KB
/
config.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
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
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2019 Brno University of Technology
*
* tcpgen - a simple DPDK TCP DNS traffic generator
* Author: Matej Postolka <[email protected]>
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <rte_common.h>
#include <rte_log.h>
#include "config.h"
#include "common.h"
#define CONF_MAC_MASK (CONF_OPT_NUM_SRC_MAC | CONF_OPT_NUM_DST_MAC)
#define CONF_IP4_MASK (CONF_OPT_NUM_IP4_SRC_NET | CONF_OPT_NUM_IP4_SRC_MASK | CONF_OPT_NUM_IP4_DST_IP)
#define CONF_IP6_MASK (CONF_OPT_NUM_IP6_SRC_NET | CONF_OPT_NUM_IP6_SRC_MASK | CONF_OPT_NUM_IP6_DST_IP)
#define CONF_OPTIONAL_OPTS (CONF_OPT_NUM_IP_IPV6_PROBABILITY | CONF_OPT_NUM_DST_PORT | CONF_OPT_NUM_UDP_PROBABILITY | CONF_OPT_NUM_TCP_KEEPALIVE_PROBABILITY)
#define CONF_STRIP_OPTIONAL(opts) ((opts) & (~CONF_OPTIONAL_OPTS))
#define CONF_VALID(supplied_opts) ((CONF_STRIP_OPTIONAL(supplied_opts) == (CONF_MAC_MASK | CONF_IP4_MASK)) || (CONF_STRIP_OPTIONAL(supplied_opts) == (CONF_MAC_MASK | CONF_IP6_MASK)) || (CONF_STRIP_OPTIONAL(supplied_opts) == (CONF_MAC_MASK | CONF_IP4_MASK | CONF_IP6_MASK)))
#define CONF_OPT_SRC_MAC "source-mac"
#define CONF_OPT_DST_MAC "destination-mac"
#define CONF_OPT_IP4_SRC_NET "ipv4-source-network"
#define CONF_OPT_IP4_SRC_MASK "ipv4-source-netmask"
#define CONF_OPT_IP4_DST_IP "ipv4-destination-ip"
#define CONF_OPT_IP6_SRC_NET "ipv6-source-network"
#define CONF_OPT_IP6_SRC_MASK "ipv6-source-netmask"
#define CONF_OPT_IP6_DST_IP "ipv6-destination-ip"
#define CONF_OPT_DST_PORT "destination-port"
#define CONF_OPT_IP_IPV6_PROBABILITY "ip-ipv6-probability"
#define CONF_OPT_UDP_PROBABILITY "udp-probability"
#define CONF_OPT_TCP_KEEPALIVE_PROBABILITY "tcp-keepalive-probability"
#define STR_EQUAL(str1, str2, len) (strncmp((str1), (str2), (len)) == 0)
#define CHAR_IS_WHITESPACE(ch) (((ch) == ' ') || ((ch) == '\t') || ((ch) == '\r') || ((ch) == '\n') || ((ch) == EOF))
enum {
CONF_AUTOMATON_STATE_KEYWORD,
CONF_AUTOMATON_STATE_SRC_MAC,
CONF_AUTOMATON_STATE_DST_MAC,
CONF_AUTOMATON_STATE_IP4_SRC_NET,
CONF_AUTOMATON_STATE_IP4_SRC_MASK,
CONF_AUTOMATON_STATE_IP4_DST_IP,
CONF_AUTOMATON_STATE_IP6_SRC_NET,
CONF_AUTOMATON_STATE_IP6_SRC_MASK,
CONF_AUTOMATON_STATE_IP6_DST_IP,
CONF_AUTOMATON_STATE_DST_PORT,
CONF_AUTOMATON_STATE_IP_IPV6_PROBABILITY,
CONF_AUTOMATON_STATE_UDP_PROBABILITY,
CONF_AUTOMATON_STATE_TCP_KEEPALIVE_PROBABILITY,
};
#define AUTOMATON_EXIT_FAIL(param) do {RTE_LOG(ERR, TCPGEN, "config_file_parse: failed to parse value of %s\n", (param)); return -1;} while (0)
static int parse_mac_addr_str(const char *mac_addr_str, uint8_t *dest);
static uint64_t probability_conv(double probability);
static int parse_mac_addr_str(const char *mac_addr_str, uint8_t *dest) {
char buf[128];
int buf_index = 0;
int str_index = 0;
int mac_addr_byte_index = 0;
char c;
do {
c = mac_addr_str[str_index];
if (buf_index > 127 || mac_addr_byte_index > 5) {
return -1;
}
if (c == ':' || c == '\0') {
buf[buf_index] = '\0';
char *endptr;
dest[mac_addr_byte_index] = strtoul(buf, &endptr, 16);
if (*endptr != '\0') {
return -1;
}
mac_addr_byte_index++;
buf_index = 0;
} else {
buf[buf_index] = c;
buf_index++;
}
str_index++;
} while (c != '\0');
if (mac_addr_byte_index == 6)
return 0;
else
return -1;
}
static uint64_t probability_conv(double probability) {
if (probability >= 1.0f)
return UINT64_MAX;
if (probability <= 0.0f)
return 0;
return (uint64_t) (probability * (double) UINT64_MAX);
}
int config_file_parse(const char *filename, struct user_config *config) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
RTE_LOG(ERR, TCPGEN, "config_file_parse: failed to open configuration file\n");
return -1;
}
char buf[1024];
uint32_t buf_pos = 0;
uint8_t conf_automaton_state = CONF_AUTOMATON_STATE_KEYWORD;
uint8_t skip_line = 0;
int ch;
do {
if (buf_pos > 1023) {
RTE_LOG(ERR, TCPGEN, "config_file_parse: invalid data in configuration file\n");
return -1;
}
ch = fgetc(fp);
if (ch == '\n')
skip_line = 0;
if (skip_line)
continue;
if (ch == '#') {
skip_line = 1;
} else if (CHAR_IS_WHITESPACE(ch) && buf_pos > 0 && conf_automaton_state == CONF_AUTOMATON_STATE_KEYWORD) {
buf[buf_pos] = '\0';
if (STR_EQUAL(buf, CONF_OPT_SRC_MAC, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_SRC_MAC;
} else if (STR_EQUAL(buf, CONF_OPT_DST_MAC, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_DST_MAC;
} else if (STR_EQUAL(buf, CONF_OPT_IP4_SRC_NET, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_IP4_SRC_NET;
} else if (STR_EQUAL(buf, CONF_OPT_IP4_SRC_MASK, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_IP4_SRC_MASK;
} else if (STR_EQUAL(buf, CONF_OPT_IP4_DST_IP, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_IP4_DST_IP;
} else if (STR_EQUAL(buf, CONF_OPT_IP6_SRC_NET, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_IP6_SRC_NET;
} else if (STR_EQUAL(buf, CONF_OPT_IP6_SRC_MASK, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_IP6_SRC_MASK;
} else if (STR_EQUAL(buf, CONF_OPT_IP6_DST_IP, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_IP6_DST_IP;
} else if (STR_EQUAL(buf, CONF_OPT_DST_PORT, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_DST_PORT;
} else if (STR_EQUAL(buf, CONF_OPT_IP_IPV6_PROBABILITY, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_IP_IPV6_PROBABILITY;
} else if (STR_EQUAL(buf, CONF_OPT_UDP_PROBABILITY, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_UDP_PROBABILITY;
} else if (STR_EQUAL(buf, CONF_OPT_TCP_KEEPALIVE_PROBABILITY, buf_pos)) {
conf_automaton_state = CONF_AUTOMATON_STATE_TCP_KEEPALIVE_PROBABILITY;
} else {
RTE_LOG(ERR, TCPGEN, "config_file_parse: unknown configuration key: %s\n", buf);
return -1;
}
buf_pos = 0;
} else if (CHAR_IS_WHITESPACE(ch) && buf_pos > 0 && conf_automaton_state != CONF_AUTOMATON_STATE_KEYWORD) {
buf[buf_pos] = '\0';
char *endptr; // strtoul
switch (conf_automaton_state) {
case CONF_AUTOMATON_STATE_SRC_MAC:
if (parse_mac_addr_str(buf, config->src_mac) != 0) {
AUTOMATON_EXIT_FAIL(CONF_OPT_SRC_MAC);
}
config->supplied_config_opts |= CONF_OPT_NUM_SRC_MAC;
break;
case CONF_AUTOMATON_STATE_DST_MAC:
if (parse_mac_addr_str(buf, config->dst_mac) != 0) {
AUTOMATON_EXIT_FAIL(CONF_OPT_DST_MAC);
}
config->supplied_config_opts |= CONF_OPT_NUM_DST_MAC;
break;
case CONF_AUTOMATON_STATE_IP4_SRC_NET:
if (inet_pton(AF_INET, buf, config->ip4_src_subnet) != 1) {
AUTOMATON_EXIT_FAIL(CONF_OPT_IP4_SRC_NET);
}
config->supplied_config_opts |= CONF_OPT_NUM_IP4_SRC_NET;
break;
case CONF_AUTOMATON_STATE_IP4_SRC_MASK:
if (inet_pton(AF_INET, buf, config->ip4_src_netmask) != 1) {
AUTOMATON_EXIT_FAIL(CONF_OPT_IP4_SRC_MASK);
}
config->ip4_src_rand_bit_mask = rte_be_to_cpu_32(~*(uint32_t *) config->ip4_src_netmask);
config->supplied_config_opts |= CONF_OPT_NUM_IP4_SRC_MASK;
break;
case CONF_AUTOMATON_STATE_IP4_DST_IP:
if (inet_pton(AF_INET, buf, config->ip4_dst_addr) != 1) {
AUTOMATON_EXIT_FAIL(CONF_OPT_IP4_DST_IP);
}
config->supplied_config_opts |= CONF_OPT_NUM_IP4_DST_IP;
break;
case CONF_AUTOMATON_STATE_IP6_SRC_NET:
if (inet_pton(AF_INET6, buf, config->ip6_src_subnet) != 1) {
AUTOMATON_EXIT_FAIL(CONF_OPT_IP6_SRC_NET);
}
config->supplied_config_opts |= CONF_OPT_NUM_IP6_SRC_NET;
break;
case CONF_AUTOMATON_STATE_IP6_SRC_MASK:
if (inet_pton(AF_INET6, buf, config->ip6_src_netmask) != 1) {
AUTOMATON_EXIT_FAIL(CONF_OPT_IP6_SRC_MASK);
}
config->ip6_src_rand_bit_mask[0] = rte_be_to_cpu_64(~*(uint64_t *) &config->ip6_src_netmask[0]);
config->ip6_src_rand_bit_mask[1] = rte_be_to_cpu_64(~*(uint64_t *) &config->ip6_src_netmask[8]);
config->supplied_config_opts |= CONF_OPT_NUM_IP6_SRC_MASK;
break;
case CONF_AUTOMATON_STATE_IP6_DST_IP:
if (inet_pton(AF_INET6, buf, config->ip6_dst_addr) != 1) {
AUTOMATON_EXIT_FAIL(CONF_OPT_IP6_DST_IP);
}
config->supplied_config_opts |= CONF_OPT_NUM_IP6_DST_IP;
break;
case CONF_AUTOMATON_STATE_DST_PORT:
config->dst_port = strtoul(buf, &endptr, 10);
if (*endptr != '\0') {
AUTOMATON_EXIT_FAIL(CONF_OPT_DST_PORT);
}
config->supplied_config_opts |= CONF_OPT_NUM_DST_PORT;
break;
case CONF_AUTOMATON_STATE_IP_IPV6_PROBABILITY:
config->ip_ipv6_probability = strtod(buf, &endptr);
if (endptr == buf) {
AUTOMATON_EXIT_FAIL(CONF_OPT_IP_IPV6_PROBABILITY);
}
config->supplied_config_opts |= CONF_OPT_NUM_IP_IPV6_PROBABILITY;
break;
case CONF_AUTOMATON_STATE_UDP_PROBABILITY:
config->udp_probability = probability_conv(strtod(buf, &endptr));
if (endptr == buf) {
AUTOMATON_EXIT_FAIL(CONF_OPT_UDP_PROBABILITY);
}
config->supplied_config_opts |= CONF_OPT_NUM_UDP_PROBABILITY;
break;
case CONF_AUTOMATON_STATE_TCP_KEEPALIVE_PROBABILITY:
config->tcp_keepalive_probability = probability_conv(strtod(buf, &endptr));
if (endptr == buf) {
AUTOMATON_EXIT_FAIL(CONF_OPT_TCP_KEEPALIVE_PROBABILITY);
}
config->supplied_config_opts |= CONF_OPT_NUM_TCP_KEEPALIVE_PROBABILITY;
break;
default:
RTE_LOG(ERR, TCPGEN, "config_file_parse: invalid conf automaton state\n");
return -1;
}
conf_automaton_state = CONF_AUTOMATON_STATE_KEYWORD;
buf_pos = 0;
} else if (!CHAR_IS_WHITESPACE(ch)) {
buf[buf_pos] = ch;
buf_pos++;
}
} while (ch != EOF);
fclose(fp);
if (conf_automaton_state != CONF_AUTOMATON_STATE_KEYWORD || buf_pos != 0) {
RTE_LOG(ERR, TCPGEN, "config_file_parse: malformed configuration file\n");
return -1;
}
if (!CONF_VALID(config->supplied_config_opts)) {
RTE_LOG(ERR, TCPGEN, "config_file_parse: invalid combination of configuration options (see documentation)\n");
return -1;
}
return 0;
}