@@ -79,6 +79,7 @@ struct kni_interface_stats {
7979
8080struct rte_ring * * kni_rp ;
8181struct kni_interface_stats * * kni_stat ;
82+ int kni_link = ETH_LINK_DOWN ;
8283
8384static void
8485set_bitmap (uint16_t port , unsigned char * bitmap )
@@ -129,12 +130,51 @@ kni_change_mtu(uint16_t port_id, unsigned new_mtu)
129130 return 0 ;
130131}
131132
133+ static void
134+ log_link_state (struct rte_kni * kni , int prev , struct rte_eth_link * link )
135+ {
136+ if (kni == NULL || link == NULL )
137+ return ;
138+
139+ if (prev == ETH_LINK_DOWN && link -> link_status == ETH_LINK_UP ) {
140+ kni_link = ETH_LINK_UP ;
141+ printf ("%s NIC Link is Up %d Mbps %s %s.\n" ,
142+ rte_kni_get_name (kni ),
143+ link -> link_speed ,
144+ link -> link_autoneg ? "(AutoNeg)" : "(Fixed)" ,
145+ link -> link_duplex ? "Full Duplex" : "Half Duplex" );
146+ } else if (prev == ETH_LINK_UP && link -> link_status == ETH_LINK_DOWN ) {
147+ kni_link = ETH_LINK_DOWN ;
148+ printf ("%s NIC Link is Down.\n" ,
149+ rte_kni_get_name (kni ));
150+ }
151+ }
152+
153+ /*
154+ * Monitor the link status of all ports and update the
155+ * corresponding KNI interface(s)
156+ */
157+ static void *
158+ monitor_all_ports_link_status (uint16_t port_id )
159+ {
160+ struct rte_eth_link link ;
161+ unsigned int i ;
162+ int prev ;
163+
164+ memset (& link , 0 , sizeof (link ));
165+ rte_eth_link_get_nowait (port_id , & link );
166+ prev = rte_kni_update_link (kni_stat [port_id ]-> kni , link .link_status );
167+ log_link_state (kni_stat [port_id ]-> kni , prev , & link );
168+
169+ return NULL ;
170+ }
171+
132172static int
133173kni_config_network_interface (uint16_t port_id , uint8_t if_up )
134174{
135175 int ret = 0 ;
136176
137- if (port_id >= rte_eth_dev_count_avail () || port_id >= RTE_MAX_ETHPORTS ) {
177+ if (! rte_eth_dev_is_valid_port ( port_id ) ) {
138178 printf ("Invalid port id %d\n" , port_id );
139179 return - EINVAL ;
140180 }
@@ -158,13 +198,46 @@ kni_config_network_interface(uint16_t port_id, uint8_t if_up)
158198 }
159199 }
160200
201+ if (!if_up )
202+ kni_link = ETH_LINK_DOWN ;
203+
161204 if (ret < 0 )
162205 printf ("Failed to Configure network interface of %d %s\n" ,
163206 port_id , if_up ? "up" : "down" );
164207
165208 return ret ;
166209}
167210
211+ static void
212+ print_ethaddr (const char * name , struct ether_addr * mac_addr )
213+ {
214+ char buf [ETHER_ADDR_FMT_SIZE ];
215+ ether_format_addr (buf , ETHER_ADDR_FMT_SIZE , mac_addr );
216+ printf ("\t%s%s\n" , name , buf );
217+ }
218+
219+
220+ /* Callback for request of configuring mac address */
221+ static int
222+ kni_config_mac_address (uint16_t port_id , uint8_t mac_addr [])
223+ {
224+ int ret = 0 ;
225+
226+ if (!rte_eth_dev_is_valid_port (port_id )) {
227+ printf ("Invalid port id %d\n" , port_id );
228+ return - EINVAL ;
229+ }
230+
231+ print_ethaddr ("Address:" , (struct ether_addr * )mac_addr );
232+
233+ ret = rte_eth_dev_default_mac_addr_set (port_id ,
234+ (struct ether_addr * )mac_addr );
235+ if (ret < 0 )
236+ printf ("Failed to config mac_addr for port %d\n" , port_id );
237+
238+ return ret ;
239+ }
240+
168241static int
169242kni_process_tx (uint16_t port_id , uint16_t queue_id ,
170243 struct rte_mbuf * * pkts_burst , unsigned count )
@@ -363,17 +436,22 @@ ff_kni_alloc(uint16_t port_id, unsigned socket_id,
363436 memset (& dev_info , 0 , sizeof (dev_info ));
364437 rte_eth_dev_info_get (port_id , & dev_info );
365438 if (dev_info .device )
366- bus = rte_bus_find_by_device (dev_info .device );
367- if (bus && !strcmp (bus -> name , "pci" )) {
368- pci_dev = RTE_DEV_TO_PCI (dev_info .device );
369- conf .addr = pci_dev -> addr ;
370- conf .id = pci_dev -> id ;
371- }
439+ bus = rte_bus_find_by_device (dev_info .device );
440+ if (bus && !strcmp (bus -> name , "pci" )) {
441+ pci_dev = RTE_DEV_TO_PCI (dev_info .device );
442+ conf .addr = pci_dev -> addr ;
443+ conf .id = pci_dev -> id ;
444+ }
445+
446+ /* Get the interface default mac address */
447+ rte_eth_macaddr_get (port_id ,
448+ (struct ether_addr * )& conf .mac_addr );
372449
373450 memset (& ops , 0 , sizeof (ops ));
374451 ops .port_id = port_id ;
375452 ops .change_mtu = kni_change_mtu ;
376453 ops .config_network_if = kni_config_network_interface ;
454+ ops .config_mac_address = kni_config_mac_address ;
377455
378456 kni_stat [port_id ]-> kni = rte_kni_alloc (mbuf_pool , & conf , & ops );
379457 if (kni_stat [port_id ]-> kni == NULL )
@@ -407,11 +485,13 @@ ff_kni_alloc(uint16_t port_id, unsigned socket_id,
407485 rte_ring_free_count (kni_rp [port_id ]));
408486}
409487
410-
411488void
412489ff_kni_process (uint16_t port_id , uint16_t queue_id ,
413490 struct rte_mbuf * * pkts_burst , unsigned count )
414491{
492+ if (unlikely (kni_link == ETH_LINK_DOWN )) {
493+ monitor_all_ports_link_status (port_id );
494+ }
415495 kni_process_tx (port_id , queue_id , pkts_burst , count );
416496 kni_process_rx (port_id , queue_id , pkts_burst , count );
417497}
0 commit comments