4444#include <errno.h>
4545#include <getopt.h>
4646#include <sched.h>
47+ #include <signal.h>
4748
4849#include <rte_common.h>
4950#include <rte_vect.h>
8990static struct ans_user_config ans_user_conf ;
9091static struct ans_lcore_queue g_lcore_queue [RTE_MAX_LCORE ];
9192static struct rte_mempool * ans_pktmbuf_pool [MAX_NB_SOCKETS ];
93+ static uint8_t ans_stopped = 0 ;
9294
9395static struct ans_lcore_params ans_lcore_params_default [] =
9496{
@@ -214,9 +216,15 @@ static void ans_check_ports_link_status(uint8_t port_num, uint32_t port_mask)
214216
215217 for (count = 0 ; count <= max_check_time ; count ++ )
216218 {
219+ if (ans_stopped )
220+ return ;
221+
217222 all_ports_up = 1 ;
218223 for (portid = 0 ; portid < port_num ; portid ++ )
219224 {
225+ if (ans_stopped )
226+ return ;
227+
220228 if ((port_mask & (1 << portid )) == 0 )
221229 continue ;
222230
@@ -563,8 +571,6 @@ static int ans_init_ports(unsigned short nb_ports, struct ans_user_config *user
563571 return 0 ;
564572}
565573
566-
567-
568574/**********************************************************************
569575*@description:
570576*
@@ -607,6 +613,35 @@ static int ans_start_ports(unsigned short nb_ports, struct ans_user_config *use
607613
608614 return 0 ;
609615}
616+
617+ /**********************************************************************
618+ *@description:
619+ *
620+ *
621+ *@parameters:
622+ * [in]:
623+ * [in]:
624+ *
625+ *@return values:
626+ *
627+ **********************************************************************/
628+ static int ans_stop_ports (unsigned short nb_ports , struct ans_user_config * user_conf )
629+ {
630+ int portid ;
631+ /* stop ports */
632+ for (portid = 0 ; portid < nb_ports ; portid ++ ) {
633+ if ((user_conf -> port_mask & (1 << portid )) == 0 )
634+ continue ;
635+
636+ printf ("Closing port %d..." , portid );
637+ rte_eth_dev_stop (portid );
638+ rte_eth_dev_close (portid );
639+ printf (" Done\n" );
640+ }
641+ printf ("Bye...\n" );
642+
643+ return 0 ;
644+ }
610645/**********************************************************************
611646*@description:
612647* Send burst of packets on an output interface
@@ -765,7 +800,7 @@ static int ans_main_loop(__attribute__((unused)) void *dummy)
765800
766801 timer_10ms_tsc = rte_get_tsc_hz () / 100 ;
767802
768- while (1 )
803+ while (! ans_stopped )
769804 {
770805
771806 cur_tsc = rte_rdtsc ();
@@ -834,6 +869,30 @@ static int ans_main_loop(__attribute__((unused)) void *dummy)
834869 }
835870}
836871
872+ /**********************************************************************
873+ *@description:
874+ *
875+ *
876+ *@parameters:
877+ * [in]:
878+ * [in]:
879+ *
880+ *@return values:
881+ *
882+ **********************************************************************/
883+ static void ans_signal_handler (int signum )
884+ {
885+ int nb_ports ;
886+ if (signum == SIGINT || signum == SIGTERM )
887+ {
888+ printf ("\nSignal %d received, preparing to exit...\n" , signum );
889+
890+ printf ("Telling cores to stop...\n" );
891+ ans_stopped = 1 ;
892+ }
893+
894+ return ;
895+ }
837896
838897/**********************************************************************
839898*@description:
@@ -877,6 +936,9 @@ int main(int argc, char **argv)
877936 if (ret < 0 )
878937 rte_exit (EXIT_FAILURE , "Invalid EAL parameters\n" );
879938
939+ signal (SIGINT , ans_signal_handler );
940+ signal (SIGTERM , ans_signal_handler );
941+
880942 argc -= ret ;
881943 argv += ret ;
882944
@@ -945,7 +1007,7 @@ int main(int argc, char **argv)
9451007
9461008 ret = ans_initialize (& init_conf );
9471009 if (ret != 0 )
948- rte_exit (EXIT_FAILURE , "Init ans failed \n" );
1010+ rte_exit (EXIT_FAILURE , "Init ans failed \n" );
9491011
9501012 /* add by ans_team ---end */
9511013
@@ -956,6 +1018,7 @@ int main(int argc, char **argv)
9561018 struct ether_addr eth_addr ;
9571019 uint16_t qmapping_nb ;
9581020 struct ans_port_qmapping qmapping [32 ];
1021+ struct rte_eth_dev_info dev_info ;
9591022
9601023 for (portid = 0 ; portid < nb_ports ; portid ++ )
9611024 {
@@ -965,10 +1028,23 @@ int main(int argc, char **argv)
9651028 printf ("\nSkipping disabled port %d\n" , portid );
9661029 continue ;
9671030 }
968-
1031+
1032+ rte_eth_dev_info_get (portid , & dev_info );
1033+
9691034 memset (ifname , 0 , sizeof (ifname ));
970-
971- sprintf (ifname , "veth%d" , portid );
1035+ if (strcmp (dev_info .driver_name , "net_vhost" ) == 0 )
1036+ {
1037+ sprintf (ifname , "vhost%d" , portid );
1038+ }
1039+ else if (strcmp (dev_info .driver_name , "net_virtio_user" ) == 0 )
1040+ {
1041+ sprintf (ifname , "virtio%d" , portid );
1042+ }
1043+ else
1044+ {
1045+ sprintf (ifname , "veth%d" , portid );
1046+ }
1047+
9721048 kni_id = ans_kni_id_get (portid );
9731049
9741050 printf ("add %s device, kni id %d \n" , ifname , kni_id );
@@ -1013,6 +1089,11 @@ int main(int argc, char **argv)
10131089 rte_eal_mp_remote_launch (ans_main_loop , NULL , CALL_MASTER );
10141090 RTE_LCORE_FOREACH_SLAVE (lcore_id ){
10151091 if (rte_eal_wait_lcore (lcore_id ) < 0 )
1016- return -1 ;
1092+ ret = -1 ;
1093+ break ;
10171094 }
1095+
1096+ ans_stop_ports (nb_ports , & ans_user_conf );
1097+
1098+ return ret ;
10181099}
0 commit comments