diff mbox

[dpdk-dev,v2,3/3] examples/l3fwd: Handle SIGINT and SIGTERM in l3fwd

Message ID 1451011032-83106-4-git-send-email-zhihong.wang@intel.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Zhihong Wang Dec. 25, 2015, 2:37 a.m. UTC
Handle SIGINT and SIGTERM in l3fwd.

Signed-off-by: Zhihong Wang <zhihong.wang@intel.com>
---
 examples/l3fwd/main.c | 110 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 90 insertions(+), 20 deletions(-)
diff mbox

Patch

diff --git a/examples/l3fwd/main.c b/examples/l3fwd/main.c
index 5b0c2dd..b9f3232 100644
--- a/examples/l3fwd/main.c
+++ b/examples/l3fwd/main.c
@@ -41,6 +41,8 @@ 
 #include <stdarg.h>
 #include <errno.h>
 #include <getopt.h>
+#include <signal.h>
+#include <unistd.h>
 
 #include <rte_common.h>
 #include <rte_vect.h>
@@ -75,6 +77,9 @@ 
 #include <cmdline_parse.h>
 #include <cmdline_parse_etheraddr.h>
 
+static int force_quit = -1;
+static int signo_quit = -1;
+
 #define APP_LOOKUP_EXACT_MATCH          0
 #define APP_LOOKUP_LPM                  1
 #define DO_RFC_1812_CHECKS
@@ -1554,6 +1559,8 @@  main_loop(__attribute__((unused)) void *dummy)
 	}
 
 	while (1) {
+		if (unlikely(force_quit != 0))
+			break;
 
 		cur_tsc = rte_rdtsc();
 
@@ -2559,6 +2566,74 @@  check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
 	}
 }
 
+static void
+start_ports(void)
+{
+	unsigned portid, nb_ports;
+	int ret;
+
+	nb_ports = rte_eth_dev_count();
+	for (portid = 0; portid < nb_ports; portid++) {
+		if ((enabled_port_mask & (1 << portid)) == 0) {
+			continue;
+		}
+		printf("Starting port %d...", portid);
+		ret = rte_eth_dev_start(portid);
+		if (ret < 0)
+			rte_exit(EXIT_FAILURE,
+					"rte_eth_dev_start: err=%d, port=%d\n",
+					ret, portid);
+		/*
+		 * If enabled, put device in promiscuous mode.
+		 * This allows IO forwarding mode to forward packets
+		 * to itself through 2 cross-connected  ports of the
+		 * target machine.
+		 */
+		if (promiscuous_on)
+			rte_eth_promiscuous_enable(portid);
+		printf(" Done\n");
+	}
+}
+
+static void
+stop_ports(void)
+{
+	unsigned portid, nb_ports;
+
+	nb_ports = rte_eth_dev_count();
+	for (portid = 0; portid < nb_ports; portid++) {
+		if ((enabled_port_mask & (1 << portid)) == 0) {
+			continue;
+		}
+		printf("Stopping port %d...", portid);
+		rte_eth_dev_stop(portid);
+		rte_eth_dev_close(portid);
+		printf(" Done\n");
+	}
+}
+
+static void
+signal_handler(__rte_unused int signum)
+{
+	if (signum == SIGINT || signum == SIGTERM) {
+		printf("\nSignal %d received, preparing to exit...\n",
+				signum);
+		if (force_quit < 0) {
+			printf("Forwarding not started yet...\n");
+			/* stop ports */
+			stop_ports();
+			printf("Bye...\n");
+			/* inform if there's a caller */
+			signal(signum, SIG_DFL);
+			kill(getpid(), signum);
+		} else {
+			printf("Forwarding started already...\n");
+			signo_quit = signum;
+			force_quit = 1;
+		}
+	}
+}
+
 int
 main(int argc, char **argv)
 {
@@ -2572,6 +2647,9 @@  main(int argc, char **argv)
 	uint32_t n_tx_queue, nb_lcores;
 	uint8_t portid, nb_rx_queue, queue, socketid;
 
+	signal(SIGINT, signal_handler);
+	signal(SIGTERM, signal_handler);
+
 	/* init EAL */
 	ret = rte_eal_init(argc, argv);
 	if (ret < 0)
@@ -2711,34 +2789,26 @@  main(int argc, char **argv)
 	printf("\n");
 
 	/* start ports */
-	for (portid = 0; portid < nb_ports; portid++) {
-		if ((enabled_port_mask & (1 << portid)) == 0) {
-			continue;
-		}
-		/* Start device */
-		ret = rte_eth_dev_start(portid);
-		if (ret < 0)
-			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
-				ret, portid);
-
-		/*
-		 * If enabled, put device in promiscuous mode.
-		 * This allows IO forwarding mode to forward packets
-		 * to itself through 2 cross-connected  ports of the
-		 * target machine.
-		 */
-		if (promiscuous_on)
-			rte_eth_promiscuous_enable(portid);
-	}
-
+	start_ports();
 	check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
 
 	/* launch per-lcore init on every lcore */
+	force_quit = 0;
 	rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
 		if (rte_eal_wait_lcore(lcore_id) < 0)
 			return -1;
 	}
 
+	printf("Stopping forwarding... Done\n");
+	/* stop ports */
+	stop_ports();
+	printf("Bye...\n");
+	/* inform if there's a caller */
+	if (force_quit != 0) {
+		signal(signo_quit, SIG_DFL);
+		kill(getpid(), signo_quit);
+	}
+
 	return 0;
 }