-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
311 lines (266 loc) · 8.61 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
#include "main.h"
struct app_config global_config;
struct rte_mempool *mbuf_pool;
volatile sig_atomic_t reload;
rte_atomic64_t timestamp;
void init_ipc_to_lcores( void )
{
char buf[128];
for( int i = 0; i < global_config.rxs_count; i++ )
{
memset( buf, 0, sizeof( buf ) );
snprintf( buf, 128, "rx_from_main_%d", global_config.lcore_rx[i].id );
global_config.lcore_rx[i].from_main = ring_wait_and_lookup( buf );
snprintf( buf, 128, "rx_to_main_%d", global_config.lcore_rx[i].id );
global_config.lcore_rx[i].to_main = ring_wait_and_lookup( buf );
}
for( int i = 0; i < global_config.workers_count; i++ )
{
memset( buf, 0, sizeof( buf ) );
snprintf( buf, 128, "worker_from_main_%d", i);
global_config.lcore_worker[i].from_main = ring_wait_and_lookup( buf );
snprintf( buf, 128, "worker_to_main_%d", i );
global_config.lcore_worker[i].to_main = ring_wait_and_lookup( buf );
}
}
void run_worker( uint8_t id )
{
RTE_LOG( INFO, MAIN, "Starting worker %d...\n", id );
for( int i = 0; i < global_config.rxs_count; i++ )
{
struct ipc_msg *msg = rte_malloc( NULL, sizeof( struct ipc_msg ), 0 );
msg->type = IPC_RX_START_WORKER;
msg->data[0] = id;
int ret = rte_ring_enqueue( global_config.lcore_rx[i].from_main, (void*)msg );
if( ret != 0 )
RTE_LOG( ERR, MAIN, "IPC between threads is broken!\n" );
while( rte_ring_dequeue( global_config.lcore_rx[i].to_main, (void*)&msg ) != 0 )
{}
if( msg->type == IPC_RX_STARTED_WORKER )
RTE_LOG( INFO, MAIN, "Worker %d started on lcore_rx %d\n", id, i );
else
RTE_LOG( INFO, MAIN, "Incorrect response %x, from lcore_rx %d\n", msg->type, i );
rte_free( msg );
}
}
void stop_worker( uint8_t id )
{
RTE_LOG( INFO, MAIN, "Stopping worker %d...\n", id );
for( int i = 0; i < global_config.rxs_count; i++ )
{
struct ipc_msg *msg = rte_malloc( NULL, sizeof( struct ipc_msg ), 0 );
msg->type = IPC_RX_STOP_WORKER;
msg->data[0] = id;
int ret = rte_ring_enqueue( global_config.lcore_rx[i].from_main, (void*)msg );
if( ret != 0 )
RTE_LOG( ERR, MAIN, "IPC between threads is broken!\n" );
while( rte_ring_dequeue( global_config.lcore_rx[i].to_main, (void*)&msg ) != 0 )
{}
if( msg->type == IPC_RX_STOPPED_WORKER )
RTE_LOG( INFO, MAIN, "Worker %d stopped on lcore_rx %d\n", id, i );
else
RTE_LOG( INFO, MAIN, "Incorrect response %x, from lcore_rx %d\n", msg->type, i );
rte_free( msg );
}
}
void reload_worker( uint8_t id )
{
RTE_LOG( INFO, MAIN, "Reloading worker %d...", id );
if( id >= global_config.workers_count )
return;
struct ipc_msg *msg = rte_malloc( NULL, sizeof( struct ipc_msg ), 0 );
msg->type = IPC_WORKER_RELOAD;
int ret = rte_ring_enqueue( global_config.lcore_worker[id].from_main, (void*)msg );
if( ret != 0 )
RTE_LOG( ERR, MAIN, "IPC between threads is broken!\n" );
while( rte_ring_dequeue( global_config.lcore_worker[id].to_main, (void*)&msg ) != 0 )
{}
if( msg->type == IPC_WORKER_RESPONSE )
RTE_LOG( INFO, MAIN, "Worker %d successfully reloaded\n", id );
else
RTE_LOG( INFO, MAIN, "Incorrect response %x, from worker %d\n", msg->type, id );
rte_free( msg );
}
void reload_all_workers( void )
{
for( int i = 0; i < global_config.workers_count; i++ )
{
stop_worker( i );
reload_worker( i );
run_worker( i );
}
}
void reload_handler( __attribute__((unused)) int sig )
{
reload = 1;
}
void lcore_main( void )
{
rte_atomic64_init( ×tamp );
init_ipc_to_lcores();
for( int i = 0; i < global_config.workers_count; i++ )
{
run_worker( i );
}
struct sigaction act;
memset( &act, 0, sizeof( act ) );
act.sa_handler = reload_handler;
if( sigaction( SIGHUP, &act, NULL ) < 0 )
rte_exit( EXIT_FAILURE, "Cannot init handle SIGHUP" );
while( 1 )
{
if( reload )
{
reload = 0;
reload_all_workers();
}
sleep( 1 );
rte_atomic64_set( ×tamp, time( NULL ) );
}
}
void print_version( void )
{
printf( "Current version: %s\n", VERSION );
exit( 0 );
}
void print_help( void )
{
printf( "To run use with this options:\n" );
printf( " url_filter -c /path/to/config -d\n" );
printf( "Argument description:\n" );
printf( " -h, --help : print this message\n" );
printf( " -c, --config : path to config file\n" );
printf( " -d, --daemonize : run as daemon\n" );
printf( " -v, --version : print version\n" );
exit( 0 );
}
int parse_args( int argc, char ** argv )
{
if( argc <= 1 )
print_help();
if( strcmp( "-h", argv[ 1 ] ) == 0 )
print_help();
if( strcmp( "--help", argv[ 1 ] ) == 0 )
print_help();
if( strcmp( "-v", argv[ 1 ] ) == 0 )
print_version();
if( strcmp( "--version", argv[ 1 ] ) == 0 )
print_version();
for( int i = 0; i < argc; i++ )
{
if( strcmp( "-c", argv[ i ] ) == 0 )
load_config( argv[ i + 1 ] );
}
return 0;
}
int main( int argc, char ** argv )
{
parse_args( argc, argv );
int ret = rte_eal_init( global_config.eal_args_count, global_config.eal_args );
if( ret < 0 )
rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
rte_log_set_global_level( RTE_LOG_INFO );
mbuf_pool = rte_pktmbuf_pool_create( "mbuf_pool", NUM_MBUFS, MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id() );
if( mbuf_pool == NULL )
rte_exit( EXIT_FAILURE, "Cannot create mbuf pool\n" );
if( rte_lcore_count() > 1 )
RTE_LOG( INFO, MAIN, "%d cores is enabled.\n", rte_lcore_count() );
ret = cgnat_init_pool( &global_config.pool_conf, &global_config.pool, 0 );
if( ret < 0 )
rte_exit(EXIT_FAILURE, "Error NAT POOL initialization with code %d\n", ret );
struct five_tuple tmp =
{
.src_address = 0x64100000,
.dst_address = 0x08080808,
.src_port = 2556,
.dst_port = 80,
.proto = IPPROTO_TCP
};
cgnat_allocate_inside_translation( &global_config.pool, tmp );
cgnat_allocate_inside_translation( &global_config.pool, tmp );
tmp.src_port= 2557;
cgnat_allocate_inside_translation( &global_config.pool, tmp );
schedule_lcores( global_config.mode );
lcore_main();
return 0;
}
int schedule_lcores( uint8_t lcore_sched_scheme )
{
RTE_LOG( INFO, MAIN, "Schedule cores scheme is: %u\n", lcore_sched_scheme );
uint16_t port_count = rte_eth_dev_count_avail();
uint16_t current_lcore = 0;
uint16_t lcore_txrx_count = 0;
uint16_t lcore_tx_count = 0;
uint16_t lcore_rx_count = 0;
uint16_t lcore_worker_count = 0;
switch( lcore_sched_scheme )
{
case ONE_TXRX:
lcore_txrx_count = 1;
break;
case MANY_TXRX:
lcore_txrx_count = port_count;
break;
case ONE_TX_ONE_RX:
lcore_tx_count = 1;
lcore_rx_count = 1;
break;
case MANY_TX_MANY_RX:
lcore_tx_count = port_count;
lcore_rx_count = port_count;
break;
}
global_config.rxs_count = lcore_rx_count;
global_config.lcore_rx = rte_malloc( NULL, sizeof( struct lcore_conf ) * global_config.rxs_count, 0 );
if( rte_get_next_lcore( rte_lcore_id(), 1, 0 ) == RTE_MAX_LCORE )
rte_exit( EXIT_FAILURE, "No lcores available\n" );
int iterator = 0;
// for( current_lcore = rte_get_next_lcore( rte_lcore_id(), 1, 0 ); current_lcore < RTE_MAX_LCORE; current_lcore = rte_get_next_lcore( current_lcore, 1, 0 ) )
RTE_LCORE_FOREACH_SLAVE( current_lcore )
{
RTE_LOG( DEBUG, MAIN, "Schedule core %d\n", current_lcore );
rte_eal_wait_lcore( current_lcore );
if( lcore_txrx_count > 0 )
{
lcore_txrx_count--;
port_count--;
if( lcore_sched_scheme == ONE_TXRX )
{
rte_eal_remote_launch( (lcore_function_t *)lcore_txrx, NULL, current_lcore );
}
else
rte_eal_remote_launch( (lcore_function_t *)lcore_txrx, ( void * )&port_count, current_lcore );
continue;
}
if( lcore_tx_count > 0 )
{
lcore_tx_count--;
port_count--;
if( lcore_sched_scheme == ONE_TX_ONE_RX )
rte_eal_remote_launch( (lcore_function_t *)lcore_tx, NULL, current_lcore );
else
rte_eal_remote_launch( (lcore_function_t *)lcore_tx_per_port, ( void * )&port_count, current_lcore );
continue;
}
if( port_count == 0 )
port_count = rte_eth_dev_count_avail();
if( lcore_rx_count > 0 )
{
lcore_rx_count--;
port_count--;
global_config.lcore_rx[ iterator++ ].id = current_lcore;
if( lcore_sched_scheme == ONE_TX_ONE_RX )
rte_eal_remote_launch( (lcore_function_t *)lcore_rx, NULL, current_lcore );
else
rte_eal_remote_launch( (lcore_function_t *)lcore_rx_per_port, ( void * )&port_count, current_lcore );
continue;
}
rte_eal_remote_launch( (lcore_function_t *)lcore_worker, ( void * )&lcore_worker_count, current_lcore );
usleep( 10 );
lcore_worker_count++;
}
global_config.workers_count = lcore_worker_count;
RTE_LOG( INFO, MAIN, "Count of workerks: %d\n", lcore_worker_count );
global_config.lcore_worker = rte_malloc( NULL, sizeof( struct lcore_conf ) * global_config.workers_count, 0 );
return lcore_worker_count;
}