5151#include <sys/ioctl.h>
5252#include <unistd.h>
5353#include <signal.h>
54+ #include <net/if.h>
55+ #include <net/if_arp.h>
5456
5557#include <rte_common.h>
5658#include <rte_log.h>
9294
9395struct kni_port_params
9496{
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 */
99103
100104} __rte_cache_aligned ;
101105
@@ -167,6 +171,40 @@ int ans_kni_send_burst(struct rte_mbuf ** mbufs, unsigned nb_mbufs, unsigned por
167171 return rte_ring_enqueue_bulk (ring ,(void * * )mbufs , nb_mbufs , NULL );
168172}
169173
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+
170208/**********************************************************************
171209*@description:
172210* Alloc KNI Devices for PORT_ID
@@ -180,51 +218,53 @@ int ans_kni_send_burst(struct rte_mbuf ** mbufs, unsigned nb_mbufs, unsigned por
180218**********************************************************************/
181219static int ans_kni_alloc (uint8_t port_id )
182220{
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 ;
187225
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 ];
191229
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 ;
194237
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 ;
199240
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 ;
202245
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 ;
207250
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 );
212252
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 );
214255
215- if (!kni )
216- rte_exit (EXIT_FAILURE , "Fail to create kni for " "port: %d\n" , port_id );
256+ params [port_id ]-> kni = kni ;
217257
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 );
219261
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 );
223263
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 );
225266
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 );
228268
229269 return 0 ;
230270}
@@ -265,11 +305,13 @@ int ans_kni_init()
265305*@return values:
266306*
267307**********************************************************************/
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 [])
269309{
270310 uint32_t portmask = common_config -> port_mask ;
271311 unsigned lcore_item = 0 ;
272312 unsigned port_id = 0 ;
313+ unsigned lcore_id ;
314+ uint8_t queue_id ;
273315
274316 // Link mbufs pool from outside modules.
275317 kni_pktmbuf_pool = pktmbuf_pool ;
@@ -282,14 +324,16 @@ int ans_kni_config(struct ans_user_config * common_config, struct rte_mempool *
282324
283325 assert (kni_port_params_array [port_id ] == NULL );
284326
285- unsigned lcore_id = lcore_item >= common_config -> lcore_param_nb
327+ lcore_id = lcore_item >= common_config -> lcore_param_nb
286328 ? common_config -> lcore_param [lcore_item = 0 ].lcore_id : common_config -> lcore_param [lcore_item ++ ].lcore_id ;
287329
288330 kni_port_params_array [port_id ] = rte_zmalloc (NULL ,sizeof (struct kni_port_params ),0 );
289331 kni_port_params_array [port_id ]-> port_id = port_id ;
290332 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 ;
291335
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 );
293337 }
294338
295339 for (int i = 0 ; i < RTE_MAX_ETHPORTS ; i ++ )
@@ -438,17 +482,14 @@ static int ans_kni_free(uint8_t port_id)
438482**********************************************************************/
439483static inline void ans_kni_to_linux (struct kni_port_params * port_param )
440484{
441- uint8_t i , port_id ;
442485 unsigned nb_rx , num ;
443486 struct rte_mbuf * pkts_burst [PKT_BURST_SZ ];
444487
445488 /* handle kin request event */
446489 rte_kni_handle_request (port_param -> kni );
447490
448- port_id = port_param -> port_id ;
449-
450491 /* 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 );
452493 if (nb_rx == 0 )
453494 {
454495 return ;
@@ -480,13 +521,10 @@ static inline void ans_kni_to_linux(struct kni_port_params *port_param)
480521**********************************************************************/
481522static inline void ans_kni_to_eth (struct kni_port_params * port_param )
482523{
483- uint8_t i , port_id ;
484524 unsigned nb_tx , num ;
485525 uint32_t nb_kni ;
486526 struct rte_mbuf * pkts_burst [PKT_BURST_SZ ];
487527
488- port_id = port_param -> port_id ;
489-
490528 /* Burst rx from kni */
491529 num = rte_kni_rx_burst (port_param -> kni , pkts_burst , PKT_BURST_SZ );
492530 if (unlikely (num > PKT_BURST_SZ ))
@@ -501,7 +539,7 @@ static inline void ans_kni_to_eth(struct kni_port_params *port_param)
501539 }
502540
503541 /* 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 );
505543 // kni_stats[port_id].tx_packets += nb_tx;
506544
507545 if (unlikely (nb_tx < num ))
0 commit comments