-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlcore_rx.c
212 lines (171 loc) · 5.01 KB
/
lcore_rx.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
#include "main.h"
extern struct rte_mempool *mbuf_pool;
extern struct app_config global_config;
extern __thread __typeof__( struct lcore_conf ) lcore;
void
rx_process_messages( struct worker_ring_conf *wrks )
{
struct ipc_msg *msg;
if( rte_ring_dequeue( lcore.from_main, ( void ** )&msg ) != 0 )
return;
RTE_LOG( INFO, LCORE_RX , "Got a ipc message with type 0x%x\n", msg->type );
switch( msg->type )
{
case IPC_RX_START_WORKER:
RTE_LOG( INFO, LCORE_RX, "Worker %d enabled\n", msg->data[0] );
wrks[ msg->data[0] ].enabled = 1;
msg->type = IPC_RX_STARTED_WORKER;
rte_ring_enqueue( lcore.to_main, (void *)msg );
break;
case IPC_RX_STOP_WORKER:
RTE_LOG( INFO, LCORE_RX, "Worker %d disabled\n", msg->data[0] );
wrks[ msg->data[0] ].enabled = 0;
msg->type = IPC_RX_STOPPED_WORKER;
rte_ring_enqueue( lcore.to_main, (void *)msg );
break;
default:
RTE_LOG( INFO, LCORE_RX , "Lcore RX %d got unknown message with type 0x%x\n", lcore.id, msg->type );
rte_free( msg );
}
}
int lcore_rx_per_port( void * p )
{
uint16_t port = *(uint16_t *)p;
RTE_LOG( INFO, LCORE_RX, "Started on core %d for port %u\n", rte_lcore_id(), port );
//sleep( 3 );
lcore_rx_init_conf( &lcore, LCORE_RX );
void *packets[ RX_BURST ];
uint16_t rx_pkts;
char buf[128];
uint16_t count_of_workers = 0;
uint16_t current_worker = 0;
int j = 0;
struct worker_ring_conf *wrks;
wrks = rte_malloc( NULL, sizeof( struct worker_ring_conf ) * global_config.workers_count, 0 );
port_init( port, mbuf_pool );
for( int i = 0; i < global_config.workers_count; i++ )
{
memset( buf, 0, 128);
snprintf( buf, 128, "worker_%d", i );
wrks[ count_of_workers ].ring = ring_wait_and_lookup( buf );
wrks[ count_of_workers ].enabled = 0;
count_of_workers++;
}
while( 1 )
{
rx_pkts = rte_eth_rx_burst( port, 0, (struct rte_mbuf **)packets, RX_BURST );
if( unlikely( rx_pkts == 0 ) )
{
rx_process_messages( wrks );
continue;
}
RTE_LOG( DEBUG, LCORE_RX, "Received %d packets.\n", rx_pkts );
while( wrks[ current_worker ].enabled != 1 )
{
current_worker++;
if( current_worker >= count_of_workers )
{
current_worker = 0;
RTE_LOG( ALERT, LCORE_RX, "No active workers!\n" );
rx_process_messages( wrks );
}
}
int res = rte_ring_enqueue_bulk( wrks[ current_worker ].ring, (void**)packets, rx_pkts, NULL );
if( res == -ENOBUFS )
{
RTE_LOG( ALERT, LCORE_RX, "Ring for worker %d is overflowed. Dropping %d packets.\n", current_worker, rx_pkts );
for( int i = 0; i < rx_pkts; i += 1 )
{
rte_pktmbuf_free( packets[ i ] );
}
}
current_worker++;
if( current_worker >= count_of_workers )
{
current_worker = 0;
//rx_process_messages( wrks );
RTE_LOG( DEBUG, LCORE_RX, "Iterate over workers again\n" );
}
j++;
if( unlikely( j == 10000 ) )
{
rx_process_messages( wrks );
}
}
return 0;
}
int lcore_rx( void )
{
// uint16_t cur_port = 0;
uint16_t nb_ports = rte_eth_dev_count_avail();
void *packets[ RX_BURST ];
uint16_t rx_pkts;
char buf[128];
RTE_LOG( INFO, LCORE_RX, "Started on core %d for all ports\n", rte_lcore_id() );
lcore_rx_init_conf( &lcore, LCORE_RX );
sleep( 3 );
uint16_t count_of_workers = 0;
uint16_t current_worker = 0;
int j = 0;
struct worker_ring_conf *wrks;
wrks = rte_malloc( NULL, sizeof( struct worker_ring_conf ) * global_config.workers_count, 0 );
for( int i = 0; i < nb_ports; i++ )
{
port_init( i, mbuf_pool );
}
RTE_LOG( INFO, LCORE_RX, "Port is inited\n" );
for( int i = 0; i < global_config.workers_count; i++ )
{
memset( buf, 0, 128);
snprintf( buf, 128, "worker_%d", i );
RTE_LOG( INFO, LCORE_RX, "Looking up for a worker_%d ring\n", i );
wrks[ count_of_workers ].ring = ring_wait_and_lookup( buf );
wrks[ count_of_workers ].enabled = 0;
count_of_workers++;
}
RTE_LOG( INFO, LCORE_RX, "Starting the loop\n" );
while( 1 )
{
for( int i = 0; i < nb_ports; i++ )
{
rx_pkts = rte_eth_rx_burst( i, 0, (struct rte_mbuf **)packets, RX_BURST );
if( unlikely( rx_pkts == 0 ) )
{
rx_process_messages( wrks );
continue;
}
RTE_LOG( DEBUG, LCORE_RX, "Received %d packets.\n", rx_pkts );
while( wrks[ current_worker ].enabled != 1 )
{
current_worker += 1;
if( current_worker >= count_of_workers )
{
current_worker = 0;
RTE_LOG( ALERT, LCORE_RX, "No active workers!\n" );
rx_process_messages( wrks );
}
}
int res = rte_ring_enqueue_bulk( wrks[ current_worker ].ring, (void**)packets, rx_pkts, NULL );
if( res == 0 )
{
RTE_LOG( ALERT, LCORE_RX, "Ring for worker %d is overflowed. Dropping %d packets.\n", current_worker, rx_pkts );
for( int i = 0; i < rx_pkts; i += 1 )
{
rte_pktmbuf_free( packets[ i ] );
}
}
current_worker++;
if( current_worker >= count_of_workers )
{
current_worker = 0;
//rx_process_messages( wrks );
RTE_LOG( DEBUG, LCORE_RX, "Iterate over workers again\n" );
}
if( unlikely( j++ == 100000 ) )
{
rx_process_messages( wrks );
}
}
}
return 0;
}