From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from dpdk.org (dpdk.org [92.243.14.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 9A140A04C0;
	Tue, 29 Sep 2020 01:18:31 +0200 (CEST)
Received: from [92.243.14.124] (localhost [127.0.0.1])
	by dpdk.org (Postfix) with ESMTP id 29D051D93E;
	Tue, 29 Sep 2020 01:15:27 +0200 (CEST)
Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com
 [66.111.4.26]) by dpdk.org (Postfix) with ESMTP id 02A561D71E
 for <dev@dpdk.org>; Tue, 29 Sep 2020 01:15:13 +0200 (CEST)
Received: from compute7.internal (compute7.nyi.internal [10.202.2.47])
 by mailout.nyi.internal (Postfix) with ESMTP id 9D6B35C01B2;
 Mon, 28 Sep 2020 19:15:11 -0400 (EDT)
Received: from mailfrontend2 ([10.202.2.163])
 by compute7.internal (MEProxy); Mon, 28 Sep 2020 19:15:11 -0400
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monjalon.net; h=
 from:to:cc:subject:date:message-id:in-reply-to:references
 :mime-version:content-transfer-encoding; s=fm2; bh=+Lx4ifXdi7TDI
 HH8wHj4jB8JIWzEGB+dNUMF3BBzW0c=; b=o7zNbuUAzJFCi25+QQNafThPjYdnF
 QivJJnB21wq/C0B7LgV3RxP4dzs3CQJW0mYewKMpp0lbdkD4LQ1o0j8vDuGqw6Yg
 TTLlHOAqUgWNFeTCZO+iOx3hd3MBunbINEOFO4YGU8eZrCsldSnufO5W/JwixnOD
 EkGwh6gmwJB1H6678GbMEubsdub2LY/comEf6Ovf7YgvOmr9FGjx00B6H0ZiVKwc
 gbqKj0GKUOVELRIUydzWt80Rcb6dO1aeFUl5DKUlqoV+rXfeMp+mi0XpTFcakbTn
 Xye2g9rk+Vs4EKQ3hAn86lc7P7F48rS4E2XTeBEAch3cGN3O89ipTg4kA==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
 messagingengine.com; h=cc:content-transfer-encoding:date:from
 :in-reply-to:message-id:mime-version:references:subject:to
 :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=
 fm3; bh=+Lx4ifXdi7TDIHH8wHj4jB8JIWzEGB+dNUMF3BBzW0c=; b=iobhXdmH
 5OPoD/KKu+GorLJDX9vjXtNJ0sYqETEMUpquDsmj7cn/rAXyHS8DN14288+sUe2g
 d5tM7b/yYXUA0Ch/llRPSObi9diOIY8Nm4VBCShueEbOYivIba+kg2DFffFWutEp
 ocvosAtVmwYO8kA/Fl2jWVTy/lG4ERHt+8aa6HFVoZc1Pi2ZlzDXkYFV34msGKzP
 yPj/g8WbL6VOt0mC4hhSoIestsksG5GKDR3URoqGQ041dRw5Rq1mVF+rJUTqgVZe
 ibxermOTA9Wh2GbyBMohBJfK44a/1HAPUqaSaQxiwjPnBfoi8XurnRXy22iJpO4b
 5Y435ZNaAKnouw==
X-ME-Sender: <xms:f25yX25VFlG-KMxRaDQFQtFYy396GDq2zRjOUS6p-2Duy2y4WIuK1g>
 <xme:f25yX_5PoQRjzt-lE8EMLUeA3OI6H4YyNhRdVpjKzsmXBX8ZuYSIkgujA_6N4I-8x
 mo101GwaQ3nULy-9g>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvdejgddulecutefuodetggdotefrodftvf
 curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu
 uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc
 fjughrpefhvffufffkofgjfhgggfestdekredtredttdenucfhrhhomhepvfhhohhmrghs
 ucfoohhnjhgrlhhonhcuoehthhhomhgrshesmhhonhhjrghlohhnrdhnvghtqeenucggtf
 frrghtthgvrhhnpedvhefgiedvjeegtdevheefhfetleefgfeivefgffevfeejgedtgfeu
 tdehtdegveenucfkphepjeejrddufeegrddvtdefrddukeegnecuvehluhhsthgvrhfuih
 iivgepuddvnecurfgrrhgrmhepmhgrihhlfhhrohhmpehthhhomhgrshesmhhonhhjrghl
 ohhnrdhnvght
X-ME-Proxy: <xmx:f25yX1ebFeD457X1tFmvPmTxLGkTrm4KpheaDADE3bQ7Udiw56ZA3w>
 <xmx:f25yXzK62r1Ql1VJIKvckCsiI97Ri6c8XVDFVwhAkpKiXJBIKCH8ug>
 <xmx:f25yX6L3xZehiQbfEPdfjP4xnhmkxY0axTg6nFDfX3mwttNF4SoijQ>
 <xmx:f25yXxWXh6oKTOr1Cg8cyTKuqbQkU1Q-EBYA3tiUUKYgzNCgWH8f8Q>
Received: from xps.monjalon.net (184.203.134.77.rev.sfr.net [77.134.203.184])
 by mail.messagingengine.com (Postfix) with ESMTPA id AA442306467D;
 Mon, 28 Sep 2020 19:15:10 -0400 (EDT)
From: Thomas Monjalon <thomas@monjalon.net>
To: dev@dpdk.org
Cc: ferruh.yigit@intel.com, arybchenko@solarflare.com,
 Gaetan Rivet <grive@u256.net>, Anatoly Burakov <anatoly.burakov@intel.com>
Date: Tue, 29 Sep 2020 01:14:19 +0200
Message-Id: <20200928231437.414489-12-thomas@monjalon.net>
X-Mailer: git-send-email 2.28.0
In-Reply-To: <20200928231437.414489-1-thomas@monjalon.net>
References: <20200913220711.3768597-1-thomas@monjalon.net>
 <20200928231437.414489-1-thomas@monjalon.net>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: [dpdk-dev] [PATCH v3 11/29] net/failsafe: release port upon close
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

The flag RTE_ETH_DEV_CLOSE_REMOVE is set so all port resources
can be freed by rte_eth_dev_close().

Freeing of private port resources is moved
from the ".remove(device)" to the ".dev_close(port)" operation.

Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
---
 drivers/net/failsafe/failsafe.c         | 25 ++--------
 drivers/net/failsafe/failsafe_ops.c     | 65 ++++++++++++++++---------
 drivers/net/failsafe/failsafe_private.h |  1 +
 3 files changed, 46 insertions(+), 45 deletions(-)

diff --git a/drivers/net/failsafe/failsafe.c b/drivers/net/failsafe/failsafe.c
index 4a4b7ceab6..44d47e8f72 100644
--- a/drivers/net/failsafe/failsafe.c
+++ b/drivers/net/failsafe/failsafe.c
@@ -60,12 +60,6 @@ fs_sub_device_alloc(struct rte_eth_dev *dev,
 	return 0;
 }
 
-static void
-fs_sub_device_free(struct rte_eth_dev *dev)
-{
-	rte_free(PRIV(dev)->subs);
-}
-
 static void fs_hotplug_alarm(void *arg);
 
 int
@@ -186,6 +180,7 @@ fs_eth_dev_create(struct rte_vdev_device *vdev)
 		ERROR("Unable to allocate rte_eth_dev");
 		return -1;
 	}
+	dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
 	priv = PRIV(dev);
 	priv->data = dev->data;
 	priv->rxp = FS_RX_PROXY_INIT;
@@ -285,7 +280,7 @@ fs_eth_dev_create(struct rte_vdev_device *vdev)
 free_args:
 	failsafe_args_free(dev);
 free_subs:
-	fs_sub_device_free(dev);
+	rte_free(PRIV(dev)->subs);
 free_dev:
 	/* mac_addrs must not be freed alone because part of dev_private */
 	dev->data->mac_addrs = NULL;
@@ -301,20 +296,8 @@ fs_rte_eth_free(const char *name)
 
 	dev = rte_eth_dev_allocated(name);
 	if (dev == NULL)
-		return -ENODEV;
-	rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
-					failsafe_eth_new_event_callback, dev);
-	ret = failsafe_eal_uninit(dev);
-	if (ret)
-		ERROR("Error while uninitializing sub-EAL");
-	failsafe_args_free(dev);
-	fs_sub_device_free(dev);
-	ret = pthread_mutex_destroy(&PRIV(dev)->hotplug_mutex);
-	if (ret)
-		ERROR("Error while destroying hotplug mutex");
-	rte_free(PRIV(dev)->mcast_addrs);
-	/* mac_addrs must not be freed alone because part of dev_private */
-	dev->data->mac_addrs = NULL;
+		return 0; /* port already released */
+	ret = failsafe_eth_dev_close(dev);
 	rte_eth_dev_release_port(dev);
 	return ret;
 }
diff --git a/drivers/net/failsafe/failsafe_ops.c b/drivers/net/failsafe/failsafe_ops.c
index 93ebd09114..0ce7dfc8a6 100644
--- a/drivers/net/failsafe/failsafe_ops.c
+++ b/drivers/net/failsafe/failsafe_ops.c
@@ -239,29 +239,6 @@ fs_dev_set_link_down(struct rte_eth_dev *dev)
 	return 0;
 }
 
-static void fs_dev_free_queues(struct rte_eth_dev *dev);
-static int
-fs_dev_close(struct rte_eth_dev *dev)
-{
-	struct sub_device *sdev;
-	uint8_t i;
-
-	fs_lock(dev, 0);
-	failsafe_hotplug_alarm_cancel(dev);
-	if (PRIV(dev)->state == DEV_STARTED)
-		dev->dev_ops->dev_stop(dev);
-	PRIV(dev)->state = DEV_ACTIVE - 1;
-	FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
-		DEBUG("Closing sub_device %d", i);
-		failsafe_eth_dev_unregister_callbacks(sdev);
-		rte_eth_dev_close(PORT_ID(sdev));
-		sdev->state = DEV_ACTIVE - 1;
-	}
-	fs_dev_free_queues(dev);
-	fs_unlock(dev, 0);
-	return 0;
-}
-
 static int
 fs_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
 {
@@ -656,6 +633,46 @@ fs_dev_free_queues(struct rte_eth_dev *dev)
 	dev->data->nb_tx_queues = 0;
 }
 
+int
+failsafe_eth_dev_close(struct rte_eth_dev *dev)
+{
+	struct sub_device *sdev;
+	uint8_t i;
+	int ret;
+
+	fs_lock(dev, 0);
+	failsafe_hotplug_alarm_cancel(dev);
+	if (PRIV(dev)->state == DEV_STARTED)
+		dev->dev_ops->dev_stop(dev);
+	PRIV(dev)->state = DEV_ACTIVE - 1;
+	FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
+		DEBUG("Closing sub_device %d", i);
+		failsafe_eth_dev_unregister_callbacks(sdev);
+		rte_eth_dev_close(PORT_ID(sdev));
+		sdev->state = DEV_ACTIVE - 1;
+	}
+	rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
+					failsafe_eth_new_event_callback, dev);
+	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
+		fs_unlock(dev, 0);
+		return 0;
+	}
+	fs_dev_free_queues(dev);
+	ret = failsafe_eal_uninit(dev);
+	if (ret)
+		ERROR("Error while uninitializing sub-EAL");
+	failsafe_args_free(dev);
+	rte_free(PRIV(dev)->subs);
+	rte_free(PRIV(dev)->mcast_addrs);
+	/* mac_addrs must not be freed alone because part of dev_private */
+	dev->data->mac_addrs = NULL;
+	fs_unlock(dev, 0);
+	ret = pthread_mutex_destroy(&PRIV(dev)->hotplug_mutex);
+	if (ret)
+		ERROR("Error while destroying hotplug mutex");
+	return 0;
+}
+
 static int
 fs_promiscuous_enable(struct rte_eth_dev *dev)
 {
@@ -1484,7 +1501,7 @@ const struct eth_dev_ops failsafe_ops = {
 	.dev_stop = fs_dev_stop,
 	.dev_set_link_down = fs_dev_set_link_down,
 	.dev_set_link_up = fs_dev_set_link_up,
-	.dev_close = fs_dev_close,
+	.dev_close = failsafe_eth_dev_close,
 	.promiscuous_enable = fs_promiscuous_enable,
 	.promiscuous_disable = fs_promiscuous_disable,
 	.allmulticast_enable = fs_allmulticast_enable,
diff --git a/drivers/net/failsafe/failsafe_private.h b/drivers/net/failsafe/failsafe_private.h
index 651578a128..6af0ef8471 100644
--- a/drivers/net/failsafe/failsafe_private.h
+++ b/drivers/net/failsafe/failsafe_private.h
@@ -236,6 +236,7 @@ int failsafe_eal_uninit(struct rte_eth_dev *dev);
 
 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
 void failsafe_eth_dev_unregister_callbacks(struct sub_device *sdev);
+int failsafe_eth_dev_close(struct rte_eth_dev *dev);
 void failsafe_dev_remove(struct rte_eth_dev *dev);
 void failsafe_stats_increment(struct rte_eth_stats *to,
 				struct rte_eth_stats *from);
-- 
2.28.0