patches for DPDK stable branches
 help / color / mirror / Atom feed
From: Sarosh Arif <sarosh.arif@emumba.com>
To: dev@dpdk.org, bruce.richardson@intel.com,
	john.mcnamara@intel.com, reshma.pattan@intel.com,
	vipin.varghese@intel.com
Cc: stable@dpdk.org, Sarosh Arif <sarosh.arif@emumba.com>
Subject: [dpdk-stable] [PATCH 2/3] examples/rxtx_callbacks: free resources on exit
Date: Mon, 15 Jun 2020 13:41:31 +0500	[thread overview]
Message-ID: <20200615084132.9553-2-sarosh.arif@emumba.com> (raw)
In-Reply-To: <20200615084132.9553-1-sarosh.arif@emumba.com>

Resources should be cleared while exiting the application.

Bugzilla ID: 437
Signed-off-by: Sarosh Arif <sarosh.arif@emumba.com>
---
 examples/rxtx_callbacks/main.c | 45 ++++++++++++++++++++++++++++------
 1 file changed, 38 insertions(+), 7 deletions(-)

diff --git a/examples/rxtx_callbacks/main.c b/examples/rxtx_callbacks/main.c
index 54d124b00..bc594dd20 100644
--- a/examples/rxtx_callbacks/main.c
+++ b/examples/rxtx_callbacks/main.c
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: BSD-3-Clause
  * Copyright(c) 2010-2015 Intel Corporation
  */
-
+#include <signal.h>
 #include <stdint.h>
 #include <inttypes.h>
 #include <getopt.h>
@@ -34,6 +34,7 @@ static struct {
 } latency_numbers;
 
 int hw_timestamping;
+static volatile bool quit_signal;
 
 #define TICKS_PER_CYCLE_SHIFT 16
 static uint64_t ticks_per_cycle_mult;
@@ -215,7 +216,7 @@ port_init(uint16_t port, struct rte_mempool *mbuf_pool)
  * Main thread that does the work, reading from INPUT_PORT
  * and writing to OUTPUT_PORT
  */
-static  __rte_noreturn void
+static void
 lcore_main(void)
 {
 	uint16_t port;
@@ -230,7 +231,7 @@ lcore_main(void)
 
 	printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
 			rte_lcore_id());
-	for (;;) {
+	while (!quit_signal) {
 		RTE_ETH_FOREACH_DEV(port) {
 			struct rte_mbuf *bufs[BURST_SIZE];
 			const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
@@ -249,6 +250,24 @@ lcore_main(void)
 	}
 }
 
+static void
+stop_and_close_eth_dev(uint16_t port_id)
+{
+	RTE_ETH_FOREACH_DEV(port_id) {
+		printf("Closing port %d...", port_id);
+		rte_eth_dev_stop(port_id);
+		rte_eth_dev_close(port_id);
+		printf(" Done\n");
+	}
+}
+
+static void
+int_handler(int sig_num)
+{
+	printf("Exiting on signal %d\n", sig_num);
+	quit_signal = true;
+}
+
 /* Main function, does initialisation and calls the per-lcore functions */
 int
 main(int argc, char *argv[])
@@ -260,7 +279,9 @@ main(int argc, char *argv[])
 		{ NULL,  0, 0, 0 }
 	};
 	int opt, option_index;
-
+	quit_signal = false;
+	/* catch ctrl-c so we can clear resources on exit */
+	signal(SIGINT, int_handler);
 
 	/* init EAL */
 	int ret = rte_eal_init(argc, argv);
@@ -278,25 +299,33 @@ main(int argc, char *argv[])
 			break;
 		default:
 			printf(usage, argv[0]);
+			stop_and_close_eth_dev(portid);
+			rte_eal_cleanup();
 			return -1;
 		}
 	optind = 1; /* reset getopt lib */
 
 	nb_ports = rte_eth_dev_count_avail();
-	if (nb_ports < 2 || (nb_ports & 1))
+	if (nb_ports < 2 || (nb_ports & 1)) {
+		stop_and_close_eth_dev(portid);
 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
+	}
 
 	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
 		NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
 		RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
-	if (mbuf_pool == NULL)
+	if (mbuf_pool == NULL) {
+		stop_and_close_eth_dev(portid);
 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
+	}
 
 	/* initialize all ports */
 	RTE_ETH_FOREACH_DEV(portid)
-		if (port_init(portid, mbuf_pool) != 0)
+		if (port_init(portid, mbuf_pool) != 0) {
+			stop_and_close_eth_dev(portid);
 			rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n",
 					portid);
+		}
 
 	if (rte_lcore_count() > 1)
 		printf("\nWARNING: Too much enabled lcores - "
@@ -304,5 +333,7 @@ main(int argc, char *argv[])
 
 	/* call lcore_main on master core only */
 	lcore_main();
+	stop_and_close_eth_dev(portid);
+	rte_eal_cleanup();
 	return 0;
 }
-- 
2.17.1


  reply	other threads:[~2020-06-15  8:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-15  8:41 [dpdk-stable] [PATCH 1/3] examples/packet_ordering: " Sarosh Arif
2020-06-15  8:41 ` Sarosh Arif [this message]
2020-06-15  8:41 ` [dpdk-stable] [PATCH 3/3] examples/skeleton: " Sarosh Arif

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200615084132.9553-2-sarosh.arif@emumba.com \
    --to=sarosh.arif@emumba.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=john.mcnamara@intel.com \
    --cc=reshma.pattan@intel.com \
    --cc=stable@dpdk.org \
    --cc=vipin.varghese@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).