51
51
#include <sys/ioctl.h>
52
52
#include <unistd.h>
53
53
#include <signal.h>
54
+ #include <net/if.h>
55
+ #include <net/if_arp.h>
54
56
55
57
#include <rte_common.h>
56
58
#include <rte_log.h>
92
94
93
95
struct kni_port_params
94
96
{
95
- uint8_t port_id ; /* Port ID */
96
- unsigned lcore_id ; /* lcore ID bind to port */
97
- struct rte_kni * kni ; /* KNI context pointers */
98
- struct rte_ring * ring ; /* Ring used to recieve packets from other cores */
97
+ uint8_t port_id ; /* Port ID */
98
+ unsigned lcore_id ; /* lcore ID bind to port */
99
+ uint8_t queue_id ; /* tx queue id of lcore of the port */
100
+
101
+ struct rte_kni * kni ; /* KNI context pointers */
102
+ struct rte_ring * ring ; /* Ring used to recieve packets from other cores */
99
103
100
104
} __rte_cache_aligned ;
101
105
@@ -167,6 +171,40 @@ int ans_kni_send_burst(struct rte_mbuf ** mbufs, unsigned nb_mbufs, unsigned por
167
171
return rte_ring_enqueue_bulk (ring ,(void * * )mbufs , nb_mbufs , NULL );
168
172
}
169
173
174
+ /* Set kni interface mac */
175
+ int ans_kni_set_mac (char * name , uint8_t port )
176
+ {
177
+ int fd = 0 ;
178
+ int ret = -1 ;
179
+ struct ifreq temp ;
180
+ struct sockaddr * addr ;
181
+ struct ether_addr eth_addr ;
182
+
183
+ rte_eth_macaddr_get (port , & eth_addr );
184
+
185
+ if ((fd = socket (AF_INET , SOCK_DGRAM , 0 )) < 0 )
186
+ {
187
+ printf ("create fd failed when set kni MAC \n" );
188
+ return -1 ;
189
+ }
190
+
191
+ strcpy (temp .ifr_name , name );
192
+ addr = (struct sockaddr * )& temp .ifr_hwaddr ;
193
+
194
+ addr -> sa_family = ARPHRD_ETHER ;
195
+ memcpy (addr -> sa_data , eth_addr .addr_bytes , ETHER_ADDR_LEN );
196
+
197
+ ret = ioctl (fd , SIOCSIFHWADDR , & temp );
198
+ if (ret != 0 )
199
+ {
200
+ printf ("set mac failed, ret %d \n" , ret );
201
+ }
202
+
203
+ close (fd );
204
+ return ret ;
205
+ }
206
+
207
+
170
208
/**********************************************************************
171
209
*@description:
172
210
* Alloc KNI Devices for PORT_ID
@@ -180,51 +218,53 @@ int ans_kni_send_burst(struct rte_mbuf ** mbufs, unsigned nb_mbufs, unsigned por
180
218
**********************************************************************/
181
219
static int ans_kni_alloc (uint8_t port_id )
182
220
{
183
- uint8_t i ;
184
- struct rte_kni * kni ;
185
- struct rte_kni_conf conf ;
186
- struct kni_port_params * * params = kni_port_params_array ;
221
+ uint8_t i ;
222
+ struct rte_kni * kni ;
223
+ struct rte_kni_conf conf ;
224
+ struct kni_port_params * * params = kni_port_params_array ;
187
225
188
- unsigned lcore_id = params [port_id ]-> lcore_id ;
189
- unsigned lcore_socket = rte_lcore_to_socket_id (lcore_id );
190
- struct rte_mempool * kni_mempool = kni_pktmbuf_pool [lcore_socket ];
226
+ unsigned lcore_id = params [port_id ]-> lcore_id ;
227
+ unsigned lcore_socket = rte_lcore_to_socket_id (lcore_id );
228
+ struct rte_mempool * kni_mempool = kni_pktmbuf_pool [lcore_socket ];
191
229
192
- if (port_id >= RTE_MAX_ETHPORTS || !params [port_id ])
193
- return -1 ;
230
+ if (port_id >= RTE_MAX_ETHPORTS || !params [port_id ])
231
+ return -1 ;
232
+
233
+ memset (& conf , 0 , sizeof (conf ));
234
+ snprintf (conf .name , RTE_KNI_NAMESIZE , "veth%u" , port_id );
235
+ conf .group_id = (uint16_t )port_id ;
236
+ conf .mbuf_size = MAX_PACKET_SZ ;
194
237
195
- memset (& conf , 0 , sizeof (conf ));
196
- snprintf (conf .name , RTE_KNI_NAMESIZE , "veth%u" , port_id );
197
- conf .group_id = (uint16_t )port_id ;
198
- conf .mbuf_size = MAX_PACKET_SZ ;
238
+ struct rte_kni_ops ops ;
239
+ struct rte_eth_dev_info dev_info ;
199
240
200
- struct rte_kni_ops ops ;
201
- struct rte_eth_dev_info dev_info ;
241
+ memset (& dev_info , 0 , sizeof (dev_info ));
242
+ rte_eth_dev_info_get (port_id , & dev_info );
243
+ conf .addr = dev_info .pci_dev -> addr ;
244
+ conf .id = dev_info .pci_dev -> id ;
202
245
203
- memset (& dev_info , 0 , sizeof (dev_info ));
204
- rte_eth_dev_info_get ( port_id , & dev_info ) ;
205
- conf . addr = dev_info . pci_dev -> addr ;
206
- conf . id = dev_info . pci_dev -> id ;
246
+ memset (& ops , 0 , sizeof (ops ));
247
+ ops . port_id = port_id ;
248
+ ops . change_mtu = ans_kni_change_mtu ;
249
+ ops . config_network_if = ans_kni_config_iface ;
207
250
208
- memset (& ops , 0 , sizeof (ops ));
209
- ops .port_id = port_id ;
210
- ops .change_mtu = ans_kni_change_mtu ;
211
- ops .config_network_if = ans_kni_config_iface ;
251
+ kni = rte_kni_alloc (kni_mempool , & conf , & ops );
212
252
213
- kni = rte_kni_alloc (kni_mempool , & conf , & ops );
253
+ if (!kni )
254
+ rte_exit (EXIT_FAILURE , "Fail to create kni for " "port: %d\n" , port_id );
214
255
215
- if (!kni )
216
- rte_exit (EXIT_FAILURE , "Fail to create kni for " "port: %d\n" , port_id );
256
+ params [port_id ]-> kni = kni ;
217
257
218
- params [port_id ]-> kni = kni ;
258
+ /* Create Ring to recieve the pkts from other cores */
259
+ char ring_name [32 ];
260
+ snprintf (ring_name ,sizeof (ring_name ),"kni_ring_s%u_p%u" ,lcore_socket ,port_id );
219
261
220
- /* Create Ring to recieve the pkts from other cores */
221
- char ring_name [32 ];
222
- snprintf (ring_name ,sizeof (ring_name ),"kni_ring_s%u_p%u" ,lcore_socket ,port_id );
262
+ params [port_id ]-> ring = rte_ring_create (ring_name ,ANS_KNI_RING_SIZE , lcore_socket ,RING_F_SC_DEQ );
223
263
224
- params [port_id ]-> ring = rte_ring_create (ring_name ,ANS_KNI_RING_SIZE , lcore_socket ,RING_F_SC_DEQ );
264
+ if (!params [port_id ]-> ring )
265
+ rte_exit (EXIT_FAILURE , "Fail to create ring for kni %s" ,ring_name );
225
266
226
- if (!params [port_id ]-> ring )
227
- rte_exit (EXIT_FAILURE , "Fail to create ring for kni %s" ,ring_name );
267
+ ans_kni_set_mac (conf .name , port_id );
228
268
229
269
return 0 ;
230
270
}
@@ -265,11 +305,13 @@ int ans_kni_init()
265
305
*@return values:
266
306
*
267
307
**********************************************************************/
268
- int ans_kni_config (struct ans_user_config * common_config , struct rte_mempool * pktmbuf_pool [])
308
+ int ans_kni_config (struct ans_user_config * common_config , struct ans_lcore_queue * lcore_conf , struct rte_mempool * pktmbuf_pool [])
269
309
{
270
310
uint32_t portmask = common_config -> port_mask ;
271
311
unsigned lcore_item = 0 ;
272
312
unsigned port_id = 0 ;
313
+ unsigned lcore_id ;
314
+ uint8_t queue_id ;
273
315
274
316
// Link mbufs pool from outside modules.
275
317
kni_pktmbuf_pool = pktmbuf_pool ;
@@ -282,14 +324,16 @@ int ans_kni_config(struct ans_user_config * common_config, struct rte_mempool *
282
324
283
325
assert (kni_port_params_array [port_id ] == NULL );
284
326
285
- unsigned lcore_id = lcore_item >= common_config -> lcore_param_nb
327
+ lcore_id = lcore_item >= common_config -> lcore_param_nb
286
328
? common_config -> lcore_param [lcore_item = 0 ].lcore_id : common_config -> lcore_param [lcore_item ++ ].lcore_id ;
287
329
288
330
kni_port_params_array [port_id ] = rte_zmalloc (NULL ,sizeof (struct kni_port_params ),0 );
289
331
kni_port_params_array [port_id ]-> port_id = port_id ;
290
332
kni_port_params_array [port_id ]-> lcore_id = lcore_id ;
333
+ queue_id = lcore_conf [lcore_id ].tx_queue [port_id ].queue_id ;
334
+ kni_port_params_array [port_id ]-> queue_id = queue_id ;
291
335
292
- printf ("ans_kni: port_id=%d,lcore_id=%d\n" ,port_id ,lcore_id );
336
+ printf ("ans_kni: port_id=%d, lcore_id=%d, tx queue id=%d \n" ,port_id ,lcore_id , queue_id );
293
337
}
294
338
295
339
for (int i = 0 ; i < RTE_MAX_ETHPORTS ; i ++ )
@@ -438,17 +482,14 @@ static int ans_kni_free(uint8_t port_id)
438
482
**********************************************************************/
439
483
static inline void ans_kni_to_linux (struct kni_port_params * port_param )
440
484
{
441
- uint8_t i , port_id ;
442
485
unsigned nb_rx , num ;
443
486
struct rte_mbuf * pkts_burst [PKT_BURST_SZ ];
444
487
445
488
/* handle kin request event */
446
489
rte_kni_handle_request (port_param -> kni );
447
490
448
- port_id = port_param -> port_id ;
449
-
450
491
/* Burst rx from ring */
451
- nb_rx = rte_ring_dequeue_burst (port_param -> ring ,(void * * )& pkts_burst , PKT_BURST_SZ , NULL );
492
+ nb_rx = rte_ring_dequeue_burst (port_param -> ring , (void * * )& pkts_burst , PKT_BURST_SZ , NULL );
452
493
if (nb_rx == 0 )
453
494
{
454
495
return ;
@@ -480,13 +521,10 @@ static inline void ans_kni_to_linux(struct kni_port_params *port_param)
480
521
**********************************************************************/
481
522
static inline void ans_kni_to_eth (struct kni_port_params * port_param )
482
523
{
483
- uint8_t i , port_id ;
484
524
unsigned nb_tx , num ;
485
525
uint32_t nb_kni ;
486
526
struct rte_mbuf * pkts_burst [PKT_BURST_SZ ];
487
527
488
- port_id = port_param -> port_id ;
489
-
490
528
/* Burst rx from kni */
491
529
num = rte_kni_rx_burst (port_param -> kni , pkts_burst , PKT_BURST_SZ );
492
530
if (unlikely (num > PKT_BURST_SZ ))
@@ -501,7 +539,7 @@ static inline void ans_kni_to_eth(struct kni_port_params *port_param)
501
539
}
502
540
503
541
/* Burst tx to eth, only send to the first queue */
504
- nb_tx = rte_eth_tx_burst (port_id , 0 , pkts_burst , (uint16_t )num );
542
+ nb_tx = rte_eth_tx_burst (port_param -> port_id , port_param -> queue_id , pkts_burst , (uint16_t )num );
505
543
// kni_stats[port_id].tx_packets += nb_tx;
506
544
507
545
if (unlikely (nb_tx < num ))
0 commit comments