[5/5] examples/l2fwd-crypto: free resources in case of error
diff mbox series

Message ID 20200519085444.4562-5-m.bilal@emumba.com
State New
Delegated to: Thomas Monjalon
Headers show
Series
  • [1/5] examples/l2fwd-event: free resources in case of error
Related show

Checks

Context Check Description
ci/Intel-compilation fail Compilation issues
ci/checkpatch success coding style OK

Commit Message

Muhammad Bilal May 19, 2020, 8:54 a.m. UTC
Freeing the resources and call rte_eal_cleanup in case of error exit.
Signed-off-by: Muhammad Bilal <m.bilal@emumba.com>
---
 examples/l2fwd-crypto/main.c | 54 ++++++++++++++++++++++++++++--------
 1 file changed, 43 insertions(+), 11 deletions(-)

Patch
diff mbox series

diff --git a/examples/l2fwd-crypto/main.c b/examples/l2fwd-crypto/main.c
index fcb55c370..169f011f8 100644
--- a/examples/l2fwd-crypto/main.c
+++ b/examples/l2fwd-crypto/main.c
@@ -2660,6 +2660,18 @@  reserve_key_memory(struct l2fwd_crypto_options *options)
 	options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
 }
 
+static void
+stop_and_close_eth_dev(uint16_t portid)
+{
+	RTE_ETH_FOREACH_DEV(portid) {
+		printf("Closing port %d...", portid);
+		rte_eth_dev_stop(portid);
+		rte_eth_dev_close(portid);
+		printf(" Done\n");
+	}
+	rte_eal_cleanup();
+}
+
 int
 main(int argc, char **argv)
 {
@@ -2667,7 +2679,7 @@  main(int argc, char **argv)
 	struct l2fwd_crypto_options options;
 
 	uint8_t nb_cryptodevs, cdev_id;
-	uint16_t portid;
+	uint16_t portid = 0;
 	unsigned lcore_id, rx_lcore_id = 0;
 	int ret, enabled_cdevcount, enabled_portcount;
 	uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
@@ -2684,8 +2696,10 @@  main(int argc, char **argv)
 
 	/* parse application arguments (after the EAL ones) */
 	ret = l2fwd_crypto_parse_args(&options, argc, argv);
-	if (ret < 0)
+	if (ret < 0) {
+		stop_and_close_eth_dev(portid);
 		rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
+	}
 
 	printf("MAC updating %s\n",
 			options.mac_updating ? "enabled" : "disabled");
@@ -2694,20 +2708,26 @@  main(int argc, char **argv)
 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
 			sizeof(struct rte_crypto_op),
 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
-	if (l2fwd_pktmbuf_pool == NULL)
+	if (l2fwd_pktmbuf_pool == NULL) {
+		stop_and_close_eth_dev(portid);
 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
+	}
 
 	/* create crypto op pool */
 	l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
 			rte_socket_id());
-	if (l2fwd_crypto_op_pool == NULL)
+	if (l2fwd_crypto_op_pool == NULL) {
+		stop_and_close_eth_dev(portid);
 		rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
+	}
 
 	/* Enable Ethernet ports */
 	enabled_portcount = initialize_ports(&options);
-	if (enabled_portcount < 1)
+	if (enabled_portcount < 1) {
+		stop_and_close_eth_dev(portid);
 		rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
+	}
 
 	/* Initialize the port/queue configuration of each logical core */
 	RTE_ETH_FOREACH_DEV(portid) {
@@ -2719,9 +2739,11 @@  main(int argc, char **argv)
 		if (options.single_lcore && qconf == NULL) {
 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
 				rx_lcore_id++;
-				if (rx_lcore_id >= RTE_MAX_LCORE)
+				if (rx_lcore_id >= RTE_MAX_LCORE) {
+					stop_and_close_eth_dev(portid);
 					rte_exit(EXIT_FAILURE,
 							"Not enough cores\n");
+				}
 			}
 		} else if (!options.single_lcore) {
 			/* get the lcore_id for this port */
@@ -2729,9 +2751,11 @@  main(int argc, char **argv)
 			       lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
 			       options.nb_ports_per_lcore) {
 				rx_lcore_id++;
-				if (rx_lcore_id >= RTE_MAX_LCORE)
+				if (rx_lcore_id >= RTE_MAX_LCORE) {
+					stop_and_close_eth_dev(portid);
 					rte_exit(EXIT_FAILURE,
 							"Not enough cores\n");
+				}
 			}
 		}
 
@@ -2748,13 +2772,17 @@  main(int argc, char **argv)
 	/* Enable Crypto devices */
 	enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
 			enabled_cdevs);
-	if (enabled_cdevcount < 0)
+	if (enabled_cdevcount < 0) {
+		stop_and_close_eth_dev(portid);
 		rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
+	}
 
-	if (enabled_cdevcount < enabled_portcount)
+	if (enabled_cdevcount < enabled_portcount) {
+		stop_and_close_eth_dev(portid);
 		rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
 				"has to be more or equal to number of ports (%d)\n",
 				enabled_cdevcount, enabled_portcount);
+	}
 
 	nb_cryptodevs = rte_cryptodev_count();
 
@@ -2769,9 +2797,11 @@  main(int argc, char **argv)
 		if (options.single_lcore && qconf == NULL) {
 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
 				rx_lcore_id++;
-				if (rx_lcore_id >= RTE_MAX_LCORE)
+				if (rx_lcore_id >= RTE_MAX_LCORE) {
+					stop_and_close_eth_dev(portid);
 					rte_exit(EXIT_FAILURE,
 							"Not enough cores\n");
+				}
 			}
 		} else if (!options.single_lcore) {
 			/* get the lcore_id for this port */
@@ -2779,9 +2809,11 @@  main(int argc, char **argv)
 			       lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
 			       options.nb_ports_per_lcore) {
 				rx_lcore_id++;
-				if (rx_lcore_id >= RTE_MAX_LCORE)
+				if (rx_lcore_id >= RTE_MAX_LCORE) {
+					stop_and_close_eth_dev(portid);
 					rte_exit(EXIT_FAILURE,
 							"Not enough cores\n");
+				}
 			}
 		}