-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
320 lines (260 loc) · 8.49 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <signal.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_lcore.h>
#include <rte_cfgfile.h>
#include <rte_log.h>
#include <rte_malloc.h>
#include "common.h"
#include "parser.h"
#include "sfc_classifier.h"
#include "sfc_proxy.h"
#include "sfc_forwarder.h"
#include "nsh.h"
/* Remove later */
static uint8_t nb_ports;
struct sfcapp_config sfcapp_cfg;
char* cfg_filename;
struct rte_mempool *sfcapp_pktmbuf_pool;
static const struct rte_eth_conf port_cfg = {
.rxmode = {
.header_split = 0, /* Header Split disabled*/
.split_hdr_size = 0,
.hw_ip_checksum = 0, /* Disable IP Checksum */
.hw_vlan_filter = 0, /* Disable VLAN filtering */
.jumbo_frame = 0, /* No jumbo frames */
.hw_strip_crc = 1, /* Enable HW CRC strip*/
},
.txmode = {
.mq_mode = ETH_MQ_TX_NONE,
},
};
static void sfcapp_assoc_ports(int portmask){
uint8_t i;
int count = 2; /* We'll only setup 2 ports */
nb_ports = rte_eth_dev_count();
if(nb_ports < 2)
rte_exit(EXIT_FAILURE,"Not enough ports! 2 needed.\n");
for(i = 0 ; i < nb_ports && count > 0 ; i++, count--){
if((portmask & (1 << i)) == 0)
continue;
switch(count){
case 2:
sfcapp_cfg.port1 = i;
break;
case 1:
sfcapp_cfg.port2 = i;
break;
default:
break;
}
}
}
static const char sfcapp_options[] = {
'p', /* Port mask */
't', /* SFC entity type*/
'f', /* Configuration file */
'h', /* Print usage */
'H' /* Hash table size*/
};
static void
parse_args(int argc, char **argv){
/* List of possible arguments
* -t : Type (classifier, proxy, SFF)
* -f : Configuration file (with rules, list of SFs, etc )
* -H : Hash table size
* -h : Print usage information
*/
int sfcapp_opt;
int pm;
enum sfcapp_type type;
while( (sfcapp_opt = getopt(argc,argv,"p:t:hH:f:")) != -1){
switch(sfcapp_opt){
case 'p':
pm = parse_portmask(optarg);
if(pm < 0)
rte_exit(EXIT_FAILURE,"Failed to parse portmask\n");
else
sfcapp_assoc_ports(pm);
break;
case 't':
type = parse_apptype(optarg);
if(type == NONE)
rte_exit(EXIT_FAILURE,"Unrecognized type parameter.\n");
else
sfcapp_cfg.type = type;
break;
case 'f':
cfg_filename = optarg;
break;
case 'h':
break;
case 'H':
break;
case '?':
break;
default:
rte_exit(EXIT_FAILURE,"Unrecognized option: %c\n",sfcapp_opt);
break;
}
}
}
static void setup_app(void){
switch(sfcapp_cfg.type){
case SFC_CLASSIFIER:
classifier_setup();
break;
case SFC_FORWARDER:
forwarder_setup();
break;
case SFC_PROXY:
proxy_setup();
break;
case NONE:
rte_exit(EXIT_FAILURE,"App type not detected, something is wrong!\n");
break;
};
}
static void
signal_handler(int signum)
{
if (signum == SIGINT || signum == SIGTERM) {
printf("\n\n%ld packets received\n%ld packets transmitted\n"
"%ld packets dropped\n",
sfcapp_cfg.rx_pkts,sfcapp_cfg.rx_pkts,sfcapp_cfg.dropped_pkts);
}
exit(0);
}
/* Function to allocate memory to be used by the application */
static void
alloc_mem(unsigned n_mbuf){
unsigned lcore_id;
const char* pool_name = "mbuf_pool";
for(lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++){
if(!rte_lcore_is_enabled(lcore_id))
continue;
if(sfcapp_pktmbuf_pool == NULL){
sfcapp_pktmbuf_pool = rte_pktmbuf_pool_create(
pool_name,
n_mbuf,
MEMPOOL_CACHE_SIZE,
0,
RTE_MBUF_DEFAULT_BUF_SIZE,
rte_socket_id());
if(sfcapp_pktmbuf_pool == NULL)
rte_exit(EXIT_FAILURE,
"Failed to allocate mbuf pool\n");
else
printf("Successfully allocated mbuf pool\n");
}
}
}
static int
init_port(uint8_t port, struct rte_mempool *mbuf_pool){
struct rte_eth_conf port_conf = port_cfg;
int ret;
uint16_t q;
if(port >= rte_eth_dev_count())
return -1;
ret = rte_eth_dev_configure(port,NB_RX_QS,NB_TX_QS,&port_conf);
if(ret != 0)
return ret;
/* Setup TX queues */
for(q = 0 ; q < NB_TX_QS ; q++){
ret = rte_eth_tx_queue_setup(port, q, NB_TX_DESC,
rte_eth_dev_socket_id(port), NULL);
if(ret < 0)
return ret;
}
/* Setup RX queues */
for(q = 0 ; q < NB_RX_QS ; q++){
ret = rte_eth_rx_queue_setup(port, q, NB_RX_DESC,
rte_eth_dev_socket_id(port), NULL, mbuf_pool);
if(ret < 0)
return ret;
}
ret = rte_eth_dev_start(port);
if(ret > 0)
return ret;
struct ether_addr eth_addr;
rte_eth_macaddr_get(port,ð_addr);
printf("MAC of port %u: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8
":%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
(unsigned) port,
eth_addr.addr_bytes[0],eth_addr.addr_bytes[1],
eth_addr.addr_bytes[2],eth_addr.addr_bytes[3],
eth_addr.addr_bytes[4],eth_addr.addr_bytes[5]);
/* Remove this later! */
switch(rte_eth_promiscuous_get(port)){
case 1:
printf("Promiscuous mode initially ENABLED for port %" PRIu16 "\n",port);
break;
case 0:
printf("Promiscuous mode initially DISABLED for port %" PRIu16 "\n",port);
break;
default:
printf("Error while trying to define prosmic status por fort %" PRIu16 " \n",port);
break;
}
rte_eth_promiscuous_disable(port);
return 0;
}
int main(int argc, char **argv){
int ret=0;
unsigned nb_lcores;
ret = rte_eal_init(argc,argv);
if(ret < 0)
rte_exit(EXIT_FAILURE, "Invalid EAL arguments.\n");
rte_openlog_stream(stderr);
argc -= ret;
argv += ret;
parse_args(argc,argv);
nb_lcores = rte_lcore_count();
SFCAPP_CHECK_FAIL_LT(nb_lcores,1,"Not enough lcores! At least 1 needed.\n");
alloc_mem(RTE_MAX(2*NB_RX_DESC +
2*nb_lcores*BURST_SIZE +
2*NB_TX_DESC +
nb_lcores*MEMPOOL_CACHE_SIZE,
(unsigned) 8192));
/* Set signal handlers */
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
/* Setup interfaces */
ret = init_port(sfcapp_cfg.port1,sfcapp_pktmbuf_pool);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to setup RX port.\n");
ret = init_port(sfcapp_cfg.port2,sfcapp_pktmbuf_pool);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to setup TX port.\n");
/* Save port MACs */
rte_eth_macaddr_get(sfcapp_cfg.port1,&sfcapp_cfg.port1_mac);
rte_eth_macaddr_get(sfcapp_cfg.port2,&sfcapp_cfg.port2_mac);
/* Init TX buffers */
sfcapp_cfg.tx_buffer1 = rte_zmalloc(NULL, RTE_ETH_TX_BUFFER_SIZE(TX_BUFFER_SIZE), 0);
ret = rte_eth_tx_buffer_init(sfcapp_cfg.tx_buffer1,BURST_SIZE);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to create TX buffer1.\n");
rte_eth_tx_buffer_set_err_callback(sfcapp_cfg.tx_buffer1,
rte_eth_tx_buffer_count_callback,&sfcapp_cfg.dropped_pkts);
sfcapp_cfg.tx_buffer2 = rte_zmalloc(NULL, RTE_ETH_TX_BUFFER_SIZE(TX_BUFFER_SIZE), 0);
ret = rte_eth_tx_buffer_init(sfcapp_cfg.tx_buffer2,BURST_SIZE);
SFCAPP_CHECK_FAIL_LT(ret,0,"Failed to create TX buffer2.\n");
rte_eth_tx_buffer_set_err_callback(sfcapp_cfg.tx_buffer2,
rte_eth_tx_buffer_count_callback,&sfcapp_cfg.dropped_pkts);
/* Initialize corresponding tables */
setup_app();
/* Read config file and setup app*/
parse_config_file(cfg_filename);
/* Print SFF's MAC read from config files */
char mac[64];
ether_format_addr(mac,64,&sfcapp_cfg.sff_addr);
printf("SFF MAC: %s\n",mac);
/* Reset stats */
sfcapp_cfg.tx_pkts = 0;
sfcapp_cfg.rx_pkts = 0;
sfcapp_cfg.dropped_pkts = 0;
/* Start application (single core) */
printf("Running...\n");
(sfcapp_cfg.main_loop)();
return 0;
}