[dpdk-dev] [PATCH] app/testpmd: change port detach interface

Nithin Dabilpuram nithind1988 at gmail.com
Mon May 13 13:21:12 CEST 2019


With the latest published interface of
rte_eal_hotplug_[add,remove](), and rte_eth_dev_close(),
rte_eth_dev_close() would cleanup all the data structures of
port's eth dev leaving the device common resource intact
if RTE_ETH_DEV_CLOSE_REMOVE is set in dev flags.
So "port detach" (~hotplug remove) should be able to work,
with device identifier like "port attach" as eth_dev could have
been closed already and rte_eth_devices[port_id] reused.

This change alters "port detach" cmdline interface to
work with device identifier like "port attach".

Signed-off-by: Nithin Dabilpuram <ndabilpuram at marvell.com>
---
 app/test-pmd/cmdline.c | 15 ++++++++-------
 app/test-pmd/testpmd.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 app/test-pmd/testpmd.h |  1 +
 3 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index c1042dd..c11b9d2 100644
--- a/app/test-pmd/cmdline.c
+++ b/app/test-pmd/cmdline.c
@@ -1456,7 +1456,7 @@ cmdline_parse_inst_t cmd_operate_attach_port = {
 struct cmd_operate_detach_port_result {
 	cmdline_fixed_string_t port;
 	cmdline_fixed_string_t keyword;
-	portid_t port_id;
+	cmdline_fixed_string_t identifier;
 };
 
 static void cmd_operate_detach_port_parsed(void *parsed_result,
@@ -1466,7 +1466,7 @@ static void cmd_operate_detach_port_parsed(void *parsed_result,
 	struct cmd_operate_detach_port_result *res = parsed_result;
 
 	if (!strcmp(res->keyword, "detach"))
-		detach_port_device(res->port_id);
+		detach_port(res->identifier);
 	else
 		printf("Unknown parameter\n");
 }
@@ -1477,18 +1477,19 @@ cmdline_parse_token_string_t cmd_operate_detach_port_port =
 cmdline_parse_token_string_t cmd_operate_detach_port_keyword =
 	TOKEN_STRING_INITIALIZER(struct cmd_operate_detach_port_result,
 			keyword, "detach");
-cmdline_parse_token_num_t cmd_operate_detach_port_port_id =
-	TOKEN_NUM_INITIALIZER(struct cmd_operate_detach_port_result,
-			port_id, UINT16);
+cmdline_parse_token_string_t cmd_operate_detach_port_identifier =
+	TOKEN_STRING_INITIALIZER(struct cmd_operate_detach_port_result,
+			identifier, NULL);
 
 cmdline_parse_inst_t cmd_operate_detach_port = {
 	.f = cmd_operate_detach_port_parsed,
 	.data = NULL,
-	.help_str = "port detach <port_id>",
+	.help_str = "port detach <identifier>:"
+		"(identifier: pci address or virtual dev name)",
 	.tokens = {
 		(void *)&cmd_operate_detach_port_port,
 		(void *)&cmd_operate_detach_port_keyword,
-		(void *)&cmd_operate_detach_port_port_id,
+		(void *)&cmd_operate_detach_port_identifier,
 		NULL,
 	},
 };
diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index 6fbfd29..f4ddd94 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -2453,6 +2453,51 @@ detach_port_device(portid_t port_id)
 }
 
 void
+detach_port(char *identifier)
+{
+	struct rte_dev_iterator iterator;
+	struct rte_devargs da;
+	portid_t port_id;
+
+	printf("Removing a device...\n");
+
+	memset(&da, 0, sizeof(da));
+	if (rte_devargs_parsef(&da, "%s", identifier)) {
+		printf("cannot parse identifier\n");
+		if (da.args)
+			free(da.args);
+		return;
+	}
+
+	RTE_ETH_FOREACH_MATCHING_DEV(port_id, identifier, &iterator) {
+		if (ports[port_id].port_status != RTE_PORT_CLOSED) {
+			if (ports[port_id].port_status != RTE_PORT_STOPPED) {
+				printf("Port %u not stopped\n", port_id);
+				return;
+			}
+
+			/* sibling ports are forced to be closed */
+			if (ports[port_id].flow_list)
+				port_flow_flush(port_id);
+			ports[port_id].port_status = RTE_PORT_CLOSED;
+			printf("Port %u is now closed\n", port_id);
+		}
+	}
+
+	if (rte_eal_hotplug_remove(da.bus->name, da.name) != 0) {
+		TESTPMD_LOG(ERR, "Failed to detach device %s(%s)\n",
+			    da.name, da.bus->name);
+		return;
+	}
+
+	remove_invalid_ports();
+
+	printf("Device %s is detached\n", identifier);
+	printf("Now total ports is %d\n", nb_ports);
+	printf("Done\n");
+}
+
+void
 pmd_test_exit(void)
 {
 	struct rte_device *device;
diff --git a/app/test-pmd/testpmd.h b/app/test-pmd/testpmd.h
index 1d9b7a2..3f2e06d 100644
--- a/app/test-pmd/testpmd.h
+++ b/app/test-pmd/testpmd.h
@@ -788,6 +788,7 @@ void stop_port(portid_t pid);
 void close_port(portid_t pid);
 void reset_port(portid_t pid);
 void attach_port(char *identifier);
+void detach_port(char *identifier);
 void detach_port_device(portid_t port_id);
 int all_ports_stopped(void);
 int port_is_stopped(portid_t port_id);
-- 
2.8.4



More information about the dev mailing list