DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH 1/3] examples/packet_ordering: free resources on exit
@ 2020-06-15  8:41 Sarosh Arif
  2020-06-15  8:41 ` [dpdk-dev] [PATCH 2/3] examples/rxtx_callbacks: " Sarosh Arif
  2020-06-15  8:41 ` [dpdk-dev] [PATCH 3/3] examples/skeleton: " Sarosh Arif
  0 siblings, 2 replies; 7+ messages in thread
From: Sarosh Arif @ 2020-06-15  8:41 UTC (permalink / raw)
  To: dev, bruce.richardson, john.mcnamara, reshma.pattan, vipin.varghese
  Cc: stable, Sarosh Arif

Resources should be cleared while exiting the application.

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

diff --git a/examples/packet_ordering/main.c b/examples/packet_ordering/main.c
index b5fc6c54b..49326d015 100644
--- a/examples/packet_ordering/main.c
+++ b/examples/packet_ordering/main.c
@@ -653,6 +653,17 @@ tx_thread(struct rte_ring *ring_in)
 	return 0;
 }
 
+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");
+	}
+}
+
 int
 main(int argc, char **argv)
 {
@@ -683,25 +694,31 @@ main(int argc, char **argv)
 		rte_exit(EXIT_FAILURE, "Invalid packet_ordering arguments\n");
 
 	/* Check if we have enought cores */
-	if (rte_lcore_count() < 3)
+	if (rte_lcore_count() < 3) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "Error, This application needs at "
 				"least 3 logical cores to run:\n"
 				"1 lcore for packet RX\n"
 				"1 lcore for packet TX\n"
 				"and at least 1 lcore for worker threads\n");
+	}
 
 	nb_ports = rte_eth_dev_count_avail();
 	if (nb_ports == 0)
-		rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
-	if (nb_ports != 1 && (nb_ports & 1))
+		rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
+	if (nb_ports != 1 && (nb_ports & 1)) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
 				"when using a single port\n");
+	}
 
 	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
 			MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
 			rte_socket_id());
-	if (mbuf_pool == NULL)
+	if (mbuf_pool == NULL) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
+	}
 
 	nb_ports_available = nb_ports;
 
@@ -716,12 +733,15 @@ main(int argc, char **argv)
 		/* init port */
 		printf("Initializing port %u... done\n", port_id);
 
-		if (configure_eth_port(port_id) != 0)
+		if (configure_eth_port(port_id) != 0) {
+			stop_and_close_eth_dev(port_id);
 			rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
 					port_id);
+		}
 	}
 
 	if (!nb_ports_available) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE,
 			"All available ports are disabled. Please set portmask.\n");
 	}
@@ -729,19 +749,25 @@ main(int argc, char **argv)
 	/* Create rings for inter core communication */
 	rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
 			RING_F_SP_ENQ);
-	if (rx_to_workers == NULL)
+	if (rx_to_workers == NULL) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
+	}
 
 	workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
 			RING_F_SC_DEQ);
-	if (workers_to_tx == NULL)
+	if (workers_to_tx == NULL) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
+	}
 
 	if (!disable_reorder) {
 		send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
 				REORDER_BUFFER_SIZE);
-		if (send_args.buffer == NULL)
+		if (send_args.buffer == NULL) {
+			stop_and_close_eth_dev(port_id);
 			rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
+		}
 	}
 
 	last_lcore_id   = get_last_lcore_id();
@@ -769,12 +795,17 @@ main(int argc, char **argv)
 
 	/* Start rx_thread() on the master core */
 	rx_thread(rx_to_workers);
-
+
 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
-		if (rte_eal_wait_lcore(lcore_id) < 0)
+		if (rte_eal_wait_lcore(lcore_id) < 0) {
+			stop_and_close_eth_dev(port_id);
+			rte_eal_cleanup();
 			return -1;
+		}
 	}
 
 	print_stats();
+	stop_and_close_eth_dev(port_id);
+	rte_eal_cleanup();
 	return 0;
 }
-- 
2.17.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [dpdk-dev] [PATCH 2/3] examples/rxtx_callbacks: free resources on exit
  2020-06-15  8:41 [dpdk-dev] [PATCH 1/3] examples/packet_ordering: free resources on exit Sarosh Arif
@ 2020-06-15  8:41 ` Sarosh Arif
  2020-06-15  8:41 ` [dpdk-dev] [PATCH 3/3] examples/skeleton: " Sarosh Arif
  1 sibling, 0 replies; 7+ messages in thread
From: Sarosh Arif @ 2020-06-15  8:41 UTC (permalink / raw)
  To: dev, bruce.richardson, john.mcnamara, reshma.pattan, vipin.varghese
  Cc: stable, Sarosh Arif

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [dpdk-dev] [PATCH 3/3] examples/skeleton: free resources on exit
  2020-06-15  8:41 [dpdk-dev] [PATCH 1/3] examples/packet_ordering: free resources on exit Sarosh Arif
  2020-06-15  8:41 ` [dpdk-dev] [PATCH 2/3] examples/rxtx_callbacks: " Sarosh Arif
@ 2020-06-15  8:41 ` Sarosh Arif
  2020-06-17  4:01   ` [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: " Sarosh Arif
  1 sibling, 1 reply; 7+ messages in thread
From: Sarosh Arif @ 2020-06-15  8:41 UTC (permalink / raw)
  To: dev, bruce.richardson, john.mcnamara, reshma.pattan, vipin.varghese
  Cc: stable, Sarosh Arif

Resources should be cleared while exiting the application.

Bugzilla ID: 437
Signed-off-by: Sarosh Arif <sarosh.arif@emumba.com>
---
 examples/skeleton/basicfwd.c | 39 +++++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)

diff --git a/examples/skeleton/basicfwd.c b/examples/skeleton/basicfwd.c
index 72ba85fa1..3b40e04d7 100644
--- a/examples/skeleton/basicfwd.c
+++ b/examples/skeleton/basicfwd.c
@@ -9,6 +9,7 @@
 #include <rte_cycles.h>
 #include <rte_lcore.h>
 #include <rte_mbuf.h>
+#include <signal.h>
 
 #define RX_RING_SIZE 1024
 #define TX_RING_SIZE 1024
@@ -17,6 +18,7 @@
 #define MBUF_CACHE_SIZE 250
 #define BURST_SIZE 32
 
+static volatile bool force_quit;
 static const struct rte_eth_conf port_conf_default = {
 	.rxmode = {
 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
@@ -162,6 +164,26 @@ 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 signum)
+{
+	printf("\n\nSignal %d received, preparing to exit...\n",
+				signum);
+	force_quit = true;
+
+}
+
 /*
  * The main function, which does initialization and calls the per-lcore
  * functions.
@@ -173,6 +195,9 @@ main(int argc, char *argv[])
 	unsigned nb_ports;
 	uint16_t portid;
 
+	force_quit = false;
+	signal(SIGINT, int_handler);
+
 	/* Initialize the Environment Abstraction Layer (EAL). */
 	int ret = rte_eal_init(argc, argv);
 	if (ret < 0)
@@ -183,21 +208,27 @@ main(int argc, char *argv[])
 
 	/* Check that there is an even number of ports to send/receive on. */
 	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");
+	}
 
 	/* Creates a new mempool in memory to hold the mbufs. */
 	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 %"PRIu16 "\n",
 					portid);
+		}
 
 	if (rte_lcore_count() > 1)
 		printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
@@ -205,5 +236,7 @@ main(int argc, char *argv[])
 	/* Call lcore_main on the master core only. */
 	lcore_main();
 
+	stop_and_close_eth_dev(portid);
+	rte_eal_cleanup();
 	return 0;
 }
-- 
2.17.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: free resources on exit
  2020-06-15  8:41 ` [dpdk-dev] [PATCH 3/3] examples/skeleton: " Sarosh Arif
@ 2020-06-17  4:01   ` Sarosh Arif
  2020-06-17  4:01     ` [dpdk-dev] [PATCH v2 2/3] examples/rxtx_callbacks: " Sarosh Arif
                       ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Sarosh Arif @ 2020-06-17  4:01 UTC (permalink / raw)
  To: dev, bruce.richardson, john.mcnamara, reshma.pattan, vipin.varghese
  Cc: Sarosh Arif

Resources should be cleared while exiting the application.

Bugzilla ID: 437
Signed-off-by: Sarosh Arif <sarosh.arif@emumba.com>
---
v2:
initialize portid
---
 examples/packet_ordering/main.c | 57 ++++++++++++++++++++++++++-------
 1 file changed, 46 insertions(+), 11 deletions(-)

diff --git a/examples/packet_ordering/main.c b/examples/packet_ordering/main.c
index b397b318e..8c106e91f 100644
--- a/examples/packet_ordering/main.c
+++ b/examples/packet_ordering/main.c
@@ -220,6 +220,17 @@ flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
 
 }
 
+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 inline int
 free_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[]) {
 	uint16_t port_id;
@@ -251,18 +262,22 @@ configure_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[])
 		tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
 				RTE_ETH_TX_BUFFER_SIZE(MAX_PKTS_BURST), 0,
 				rte_eth_dev_socket_id(port_id));
-		if (tx_buffer[port_id] == NULL)
+		if (tx_buffer[port_id] == NULL) {
+			stop_and_close_eth_dev(port_id);
 			rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
 				 port_id);
+		}
 
 		rte_eth_tx_buffer_init(tx_buffer[port_id], MAX_PKTS_BURST);
 
 		ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
 				flush_tx_error_callback, NULL);
-		if (ret < 0)
+		if (ret < 0) {
+			stop_and_close_eth_dev(port_id);
 			rte_exit(EXIT_FAILURE,
 			"Cannot set error callback for tx buffer on port %u\n",
 				 port_id);
+		}
 	}
 	return 0;
 }
@@ -662,7 +677,7 @@ main(int argc, char **argv)
 	int ret;
 	unsigned nb_ports;
 	unsigned int lcore_id, last_lcore_id, master_lcore_id;
-	uint16_t port_id;
+	uint16_t port_id = 0;
 	uint16_t nb_ports_available;
 	struct worker_thread_args worker_args = {NULL, NULL};
 	struct send_thread_args send_args = {NULL, NULL};
@@ -686,25 +701,31 @@ main(int argc, char **argv)
 		return -1;
 
 	/* Check if we have enought cores */
-	if (rte_lcore_count() < 3)
+	if (rte_lcore_count() < 3) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "Error, This application needs at "
 				"least 3 logical cores to run:\n"
 				"1 lcore for packet RX\n"
 				"1 lcore for packet TX\n"
 				"and at least 1 lcore for worker threads\n");
+	}
 
 	nb_ports = rte_eth_dev_count_avail();
 	if (nb_ports == 0)
 		rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
-	if (nb_ports != 1 && (nb_ports & 1))
+	if (nb_ports != 1 && (nb_ports & 1)) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
 				"when using a single port\n");
+	}
 
 	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
 			MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
 			rte_socket_id());
-	if (mbuf_pool == NULL)
+	if (mbuf_pool == NULL) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
+	}
 
 	nb_ports_available = nb_ports;
 
@@ -719,12 +740,15 @@ main(int argc, char **argv)
 		/* init port */
 		printf("Initializing port %u... done\n", port_id);
 
-		if (configure_eth_port(port_id) != 0)
+		if (configure_eth_port(port_id) != 0) {
+			stop_and_close_eth_dev(port_id);
 			rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
 					port_id);
+		}
 	}
 
 	if (!nb_ports_available) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE,
 			"All available ports are disabled. Please set portmask.\n");
 	}
@@ -732,19 +756,25 @@ main(int argc, char **argv)
 	/* Create rings for inter core communication */
 	rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
 			RING_F_SP_ENQ);
-	if (rx_to_workers == NULL)
+	if (rx_to_workers == NULL) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
+	}
 
 	workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
 			RING_F_SC_DEQ);
-	if (workers_to_tx == NULL)
+	if (workers_to_tx == NULL) {
+		stop_and_close_eth_dev(port_id);
 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
+	}
 
 	if (!disable_reorder) {
 		send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
 				REORDER_BUFFER_SIZE);
-		if (send_args.buffer == NULL)
+		if (send_args.buffer == NULL) {
+			stop_and_close_eth_dev(port_id);
 			rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
+		}
 	}
 
 	last_lcore_id   = get_last_lcore_id();
@@ -774,10 +804,15 @@ main(int argc, char **argv)
 	rx_thread(rx_to_workers);
 
 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
-		if (rte_eal_wait_lcore(lcore_id) < 0)
+		if (rte_eal_wait_lcore(lcore_id) < 0) {
+			stop_and_close_eth_dev(port_id);
+			rte_eal_cleanup();
 			return -1;
+		}
 	}
 
 	print_stats();
+	stop_and_close_eth_dev(port_id);
+	rte_eal_cleanup();
 	return 0;
 }
-- 
2.17.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [dpdk-dev] [PATCH v2 2/3] examples/rxtx_callbacks: free resources on exit
  2020-06-17  4:01   ` [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: " Sarosh Arif
@ 2020-06-17  4:01     ` Sarosh Arif
  2020-06-17  4:01     ` [dpdk-dev] [PATCH v2 3/3] examples/skeleton: " Sarosh Arif
  2020-06-23 10:48     ` [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: " Pattan, Reshma
  2 siblings, 0 replies; 7+ messages in thread
From: Sarosh Arif @ 2020-06-17  4:01 UTC (permalink / raw)
  To: dev, bruce.richardson, john.mcnamara, reshma.pattan, vipin.varghese
  Cc: Sarosh Arif

Resources should be cleared while exiting the application.

Bugzilla ID: 437
Signed-off-by: Sarosh Arif <sarosh.arif@emumba.com>
---
v2:
initialize portid
---
 examples/rxtx_callbacks/main.c | 47 ++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 8 deletions(-)

diff --git a/examples/rxtx_callbacks/main.c b/examples/rxtx_callbacks/main.c
index 54d124b00..b6de8c5a9 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,18 +250,38 @@ 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[])
 {
 	struct rte_mempool *mbuf_pool;
 	uint16_t nb_ports;
-	uint16_t portid;
+	uint16_t portid = 0;
 	struct option lgopts[] = {
 		{ 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [dpdk-dev] [PATCH v2 3/3] examples/skeleton: free resources on exit
  2020-06-17  4:01   ` [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: " Sarosh Arif
  2020-06-17  4:01     ` [dpdk-dev] [PATCH v2 2/3] examples/rxtx_callbacks: " Sarosh Arif
@ 2020-06-17  4:01     ` Sarosh Arif
  2020-06-23 10:48     ` [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: " Pattan, Reshma
  2 siblings, 0 replies; 7+ messages in thread
From: Sarosh Arif @ 2020-06-17  4:01 UTC (permalink / raw)
  To: dev, bruce.richardson, john.mcnamara, reshma.pattan, vipin.varghese
  Cc: Sarosh Arif

Resources should be cleared while exiting the application.

Bugzilla ID: 437
Signed-off-by: Sarosh Arif <sarosh.arif@emumba.com>
---
v2:
initialize portid
---
 examples/skeleton/basicfwd.c | 41 ++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/examples/skeleton/basicfwd.c b/examples/skeleton/basicfwd.c
index 72ba85fa1..7d240bddb 100644
--- a/examples/skeleton/basicfwd.c
+++ b/examples/skeleton/basicfwd.c
@@ -9,6 +9,7 @@
 #include <rte_cycles.h>
 #include <rte_lcore.h>
 #include <rte_mbuf.h>
+#include <signal.h>
 
 #define RX_RING_SIZE 1024
 #define TX_RING_SIZE 1024
@@ -17,6 +18,7 @@
 #define MBUF_CACHE_SIZE 250
 #define BURST_SIZE 32
 
+static volatile bool force_quit;
 static const struct rte_eth_conf port_conf_default = {
 	.rxmode = {
 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
@@ -162,6 +164,26 @@ 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 signum)
+{
+	printf("\n\nSignal %d received, preparing to exit...\n",
+				signum);
+	force_quit = true;
+
+}
+
 /*
  * The main function, which does initialization and calls the per-lcore
  * functions.
@@ -171,7 +193,10 @@ main(int argc, char *argv[])
 {
 	struct rte_mempool *mbuf_pool;
 	unsigned nb_ports;
-	uint16_t portid;
+	uint16_t portid = 0;
+
+	force_quit = false;
+	signal(SIGINT, int_handler);
 
 	/* Initialize the Environment Abstraction Layer (EAL). */
 	int ret = rte_eal_init(argc, argv);
@@ -183,21 +208,27 @@ main(int argc, char *argv[])
 
 	/* Check that there is an even number of ports to send/receive on. */
 	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");
+	}
 
 	/* Creates a new mempool in memory to hold the mbufs. */
 	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 %"PRIu16 "\n",
 					portid);
+		}
 
 	if (rte_lcore_count() > 1)
 		printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
@@ -205,5 +236,7 @@ main(int argc, char *argv[])
 	/* Call lcore_main on the master core only. */
 	lcore_main();
 
+	stop_and_close_eth_dev(portid);
+	rte_eal_cleanup();
 	return 0;
 }
-- 
2.17.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: free resources on exit
  2020-06-17  4:01   ` [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: " Sarosh Arif
  2020-06-17  4:01     ` [dpdk-dev] [PATCH v2 2/3] examples/rxtx_callbacks: " Sarosh Arif
  2020-06-17  4:01     ` [dpdk-dev] [PATCH v2 3/3] examples/skeleton: " Sarosh Arif
@ 2020-06-23 10:48     ` Pattan, Reshma
  2 siblings, 0 replies; 7+ messages in thread
From: Pattan, Reshma @ 2020-06-23 10:48 UTC (permalink / raw)
  To: Sarosh Arif, dev, Richardson, Bruce, Mcnamara, John, Varghese, Vipin



> -----Original Message-----
> From: Sarosh Arif <sarosh.arif@emumba.com>
> Sent: Wednesday, June 17, 2020 5:01 AM
> To: dev@dpdk.org; Richardson, Bruce <bruce.richardson@intel.com>;
> Mcnamara, John <john.mcnamara@intel.com>; Pattan, Reshma
> <reshma.pattan@intel.com>; Varghese, Vipin <vipin.varghese@intel.com>
> Cc: Sarosh Arif <sarosh.arif@emumba.com>
> Subject: [PATCH v2 1/3] examples/packet_ordering: free resources on exit
> 
> Resources should be cleared while exiting the application.
> 
> Bugzilla ID: 437
> Signed-off-by: Sarosh Arif <sarosh.arif@emumba.com>
> ---
> v2:
> initialize portid

This patch looks good to me

Acked-by: Reshma Pattan <reshma.pattan@intel.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-06-23 10:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-15  8:41 [dpdk-dev] [PATCH 1/3] examples/packet_ordering: free resources on exit Sarosh Arif
2020-06-15  8:41 ` [dpdk-dev] [PATCH 2/3] examples/rxtx_callbacks: " Sarosh Arif
2020-06-15  8:41 ` [dpdk-dev] [PATCH 3/3] examples/skeleton: " Sarosh Arif
2020-06-17  4:01   ` [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: " Sarosh Arif
2020-06-17  4:01     ` [dpdk-dev] [PATCH v2 2/3] examples/rxtx_callbacks: " Sarosh Arif
2020-06-17  4:01     ` [dpdk-dev] [PATCH v2 3/3] examples/skeleton: " Sarosh Arif
2020-06-23 10:48     ` [dpdk-dev] [PATCH v2 1/3] examples/packet_ordering: " Pattan, Reshma

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).