-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.c
434 lines (352 loc) · 12.6 KB
/
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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <stdio.h>
#include <stdlib.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include "parser.h"
#include "common.h"
#include "sfc_classifier.h"
#include "sfc_proxy.h"
#include "sfc_forwarder.h"
extern struct sfcapp_config sfcapp_cfg;
int parse_portmask(const char *portmask){
char *end = NULL;
unsigned long pm;
/* parse hexadecimal string */
pm = strtoul(portmask, &end, 16);
if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
return -1;
if (pm == 0)
return -1;
return pm;
}
enum sfcapp_type parse_apptype(const char *type){
if(strcmp(type,"proxy") == 0)
return SFC_PROXY;
if(strcmp(type,"classifier") == 0)
return SFC_CLASSIFIER;
if(strcmp(type,"forwarder") == 0)
return SFC_FORWARDER;
return NONE;
}
int parse_uint8(const char *str, uint8_t *res, int radix){
char *end;
errno = 0;
intmax_t val = strtoimax(str, &end, radix);
if (errno == ERANGE || val < 0 || val > 0xFF || end == str || *end != '\0')
return -1;
*res = (uint8_t) val;
return 0;
}
int parse_uint16(const char *str, uint16_t *res, int radix){
char *end;
errno = 0;
intmax_t val = strtoimax(str, &end, radix);
if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == str || *end != '\0')
return -1;
*res = (uint16_t) val;
return 0;
}
int parse_uint32(const char* str, uint32_t *res, int radix){
char *end;
errno = 0;
uint32_t val = strtoul(str, &end, radix);
if (errno == ERANGE || errno == EINVAL || end == str || *end != '\0')
return -1;
*res = val;
return 0;
}
int parse_uint64(const char* str, uint64_t *res, int radix){
char *end;
errno = 0;
uint64_t val = strtoull(str, &end, radix);
if (errno == ERANGE || errno == EINVAL || end == str || *end != '\0')
return -1;
*res = val;
return 0;
}
int parse_ether(const char *str, struct ether_addr *eth_addr){
int i,cnt;
uint8_t vals[6];
if(str == NULL || eth_addr == NULL || strlen(str) < (ETHER_ADDR_FMT_SIZE-1))
return -1;
cnt = sscanf(str,"%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":%" SCNx8 ":%" SCNx8,
&vals[0], &vals[1], &vals[2], &vals[3], &vals[4], &vals[5]);
if(cnt == 6){
for(i = 0 ; i < 6 ; i++)
eth_addr->addr_bytes[i] = vals[i];
return 0;
}
return -1;
}
int parse_ipv4(const char *str, uint32_t *ipv4){
int cnt;
uint32_t a,b,c,d;
cnt = sscanf(str,"%" SCNu32 ".%" SCNu32 ".%" SCNu32 ".%" SCNu32,&a,&b,&c,&d);
if(cnt != 4 || a > 255 || b > 255 || c > 255 || d > 255)
return -1;
*ipv4 = IPv4(a,b,c,d);
return 0;
}
static void parse_global_section(struct rte_cfgfile_entry *entries, int nb_entries){
int ret;
const char* SECTION_NAME = "GLOBAL";
int NARGS = 1;
if(nb_entries != 1)
rte_exit(EXIT_FAILURE,
"Wrong number of arguments in global section in config file. Expected %d, found %d",
NARGS,nb_entries);
if(strcmp(entries[0].name,"sff_mac") == 0){
ret = parse_ether(entries[0].value,&sfcapp_cfg.sff_addr);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to parse mac address from config file\n");
}else{
rte_exit(EXIT_FAILURE,
"Entry %s unknown in section %s, please check config file.\n",
entries[0].name,SECTION_NAME);
}
}
static void parse_sf_section(struct rte_cfgfile_entry *entries, int nb_entries){
int j,ret;
int sfid_ok, mac_ok;
struct ether_addr sfmac;
uint16_t sfid;
const char* SECTION_NAME = "SF";
sfid_ok = 0;
mac_ok = 0;
sfid = 0;
if(nb_entries != 2)
rte_exit(EXIT_FAILURE,
"Wrong argument number in SF section in config file. Expected 2, found %d",
nb_entries);
for(j = 0 ; j < nb_entries ; j++){
//sfid
if(strcmp(entries[j].name,"sfid") == 0){
if(sfid_ok)
printf("Duplicated sfid entry in SF section. Ignoring...\n");
else{
ret = parse_uint16(entries[j].value,&sfid,10);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to parse SF ID from config file\n");
sfid_ok = 1;
}
}else if(strcmp(entries[j].name,"mac") == 0){
if(mac_ok)
printf("Duplicated mac entry in SF section. Ignoring...\n");
else{
ret = parse_ether(entries[j].value,&sfmac);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to parse mac address from config file\n");
mac_ok = 1;
}
}else{
rte_exit(EXIT_FAILURE,
"Entry %s unknown in section %s, please check config file.\n",
entries[j].name,SECTION_NAME);
}
}
if(mac_ok && sfid_ok){
switch(sfcapp_cfg.type){
case SFC_FORWARDER:
forwarder_add_sf_address_entry(sfid,&sfmac);
break;
case SFC_PROXY:
proxy_add_sf_address_entry(sfid,&sfmac);
break;
default:
rte_exit(EXIT_FAILURE,
"Config file parsing failed. \"SF\" sections do not"
"apply to this type of application.\n");
}
}
}
static void parse_sfc_node_section(struct rte_cfgfile_entry *entries, int nb_entries){
int j,ret;
int sfid_ok,sph_ok;
uint16_t sfid;
uint32_t sph;
const char* SECTION_NAME = "SFC_NODE";
if(nb_entries != 2)
rte_exit(EXIT_FAILURE,
"Wrong argument number in \"SFC_NODE\" section in config file."
"Expected 2, found %d",
nb_entries);
sph = sfid = 0;
sfid_ok = sph_ok = 0;
for(j = 0 ; j < nb_entries ; j++){
if(strcmp(entries[j].name,"sfid") == 0){
if(sfid_ok)
printf("Duplicated sfid entry in PATH_NODE section. Ignoring...\n");
else{
ret = parse_uint16(entries[j].value,&sfid,10);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to parse SF ID from config file\n");
sfid_ok = 1;
}
}else if(strcmp(entries[j].name,"sph") == 0){
if(sph_ok)
printf("Duplicated mac entry in PATH_NODE section. Ignoring...\n");
else{
ret = parse_uint32(entries[j].value,&sph,16);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to parse service path info from config file\n");
sph_ok = 1;
}
}else{
rte_exit(EXIT_FAILURE,
"Entry %s unknown in section %s, please check config file.\n",
entries[j].name,SECTION_NAME);
}
}
if(sph_ok && sfid_ok){
switch(sfcapp_cfg.type){
case SFC_FORWARDER:
forwarder_add_sph_entry(sph,sfid);
break;
case SFC_PROXY:
proxy_add_sph_entry(sph,sfid);
break;
default:
rte_exit(EXIT_FAILURE,
"Config file parsing failed. \"SFC_NODE\" sections do not"
"apply to this type of application.\n");
}
}
}
static void parse_flow_class_section(struct rte_cfgfile_entry *entries, int nb_entries){
struct ipv4_5tuple tuple;
int ipsrc_ok,ipdst_ok;
int dport_ok,sport_ok;
int proto_ok,sfp_ok;
int dup,ret,j;
uint32_t sfp;
const char* SECTION_NAME = "FLOW_CLASS";
const int N_ENTRIES = 6;
if(nb_entries != 6)
rte_exit(EXIT_FAILURE,
"Wrong argument number in \"%s\" section in config file."
"Expected %d, found %d",
SECTION_NAME,
N_ENTRIES,
nb_entries);
dup = 0;
ret = 0;
ipsrc_ok = ipdst_ok = 0;
dport_ok = sport_ok = 0;
sfp = 0;
proto_ok = sfp_ok = 0;
for(j = 0 ; j < nb_entries ; j++){
if(strcmp(entries[j].name,"ipsrc") == 0){
if(ipsrc_ok)
dup = -1;
else{
ret = parse_ipv4(entries[j].value,&tuple.src_ip);
if(ret<0) printf("Failed to parse IP src\n");
ipsrc_ok = 1;
}
}else if(strcmp(entries[j].name,"ipdst") == 0){
if(ipdst_ok)
dup = -1;
else{
ret = parse_ipv4(entries[j].value,&tuple.dst_ip);
if(ret<0) printf("Failed to parse IP dst\n");
ipdst_ok = 1;
}
}else if(strcmp(entries[j].name,"sport") == 0){
if(sport_ok)
dup = -1;
else{
ret = parse_uint16(entries[j].value,&tuple.src_port,10);
if(ret<0) printf("Failed to parse source port\n");
sport_ok = 1;
}
}else if(strcmp(entries[j].name,"dport") == 0){
if(dport_ok)
dup = -1;
else{
ret = parse_uint16(entries[j].value,&tuple.dst_port,10);
if(ret<0) printf("Failed to parse dest port\n");
dport_ok = 1;
}
}else if(strcmp(entries[j].name,"proto") == 0){
if(proto_ok)
dup = -1;
else{
ret = parse_uint8(entries[j].value,&tuple.proto,10);
if(ret<0) printf("Failed to parse protocol\n");
proto_ok = 1;
}
}else if(strcmp(entries[j].name,"sfp") == 0){
if(sfp_ok)
dup = -1;
else{
ret = parse_uint32(entries[j].value,&sfp,10);
if(ret<0) printf("Failed to parse sfp\n");
sfp_ok = 1;
}
}else{
rte_exit(EXIT_FAILURE,
"Entry %s unknown in section %s, please check config file.\n",
entries[j].name,SECTION_NAME);
}
if(ret < 0) rte_exit(EXIT_FAILURE,"Failed to parse params in %s section from config file\n",
SECTION_NAME);
if(dup < 0) rte_exit(EXIT_FAILURE,"Found duplicate entries in %s section from config file.\n",
SECTION_NAME);
}
if(ipsrc_ok && ipdst_ok && sport_ok && dport_ok && proto_ok && sfp_ok){
if(sfcapp_cfg.type == SFC_CLASSIFIER){
if(sfp <= 0xFFFFFF)
classifier_add_flow_class_entry(&tuple,sfp);
else
rte_exit(EXIT_FAILURE,
"SFP id too big. Maximum is 0xFFFFFF");
}
else rte_exit(EXIT_FAILURE,
"Config file parsing failed. \"%s\" sections do not"
" apply to this type of application.\n",SECTION_NAME);
}else
rte_exit(EXIT_FAILURE,"Missing parameters in \"%s\" section from config file\n",SECTION_NAME);
}
void parse_config_file(char* cfg_filename){
int nb_entries;
int i;
struct rte_cfgfile_entry entries[10];
struct rte_cfgfile *cfgfile;
char** sections;
int nb_sections;
struct rte_cfgfile_parameters cfg_params = {.comment_character = '#'};
cfgfile = rte_cfgfile_load_with_params(cfg_filename,CFG_FLAG_GLOBAL_SECTION,&cfg_params);
if(cfgfile == NULL)
rte_exit(EXIT_FAILURE,
"Failed to load proxy config file\n");
nb_sections = rte_cfgfile_num_sections(cfgfile,NULL,0);
if(nb_sections <= 0)
rte_exit(EXIT_FAILURE,
"Not enough sections in config file\n");
sections = (char**) malloc(nb_sections*sizeof(char*));
if(sections == NULL)
rte_exit(EXIT_FAILURE,
"Failed to allocate memory when parsing proxy config file.\n");
for(i = 0 ; i < nb_sections ; i++){
sections[i] = (char*) malloc(CFG_NAME_LEN*sizeof(char));
if(sections[i] == NULL)
rte_exit(EXIT_FAILURE,
"Failed to allocate memory when parsing proxy config file.\n");
}
rte_cfgfile_sections(cfgfile,sections,CFG_FILE_MAX_SECTIONS);
/* Parse sections */
for(i = 0 ; i < nb_sections ; i++){
nb_entries = rte_cfgfile_section_entries_by_index(cfgfile,i,sections[i],
entries,10);
/* Parse SF Sections */
if(strcmp(sections[i],"SF") == 0)
parse_sf_section(entries,nb_entries);
else if(strcmp(sections[i],"SFC_NODE") == 0)
parse_sfc_node_section(entries,nb_entries);
else if(strcmp(sections[i],"FLOW_CLASS") == 0)
parse_flow_class_section(entries,nb_entries);
else if(strcmp(sections[i],"GLOBAL") == 0)
parse_global_section(entries,nb_entries);
else
rte_exit(EXIT_FAILURE,
"Section %s unknown, please check config file.\n",
sections[i]);
}
free(sections);
rte_cfgfile_close(cfgfile);
}