From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-eopbgr60082.outbound.protection.outlook.com [40.107.6.82]) by dpdk.org (Postfix) with ESMTP id 1F48C271 for ; Sat, 20 Jan 2018 22:24:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Mellanox.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=uA5QMs8gb2HJ1T+LAeKmXU0zYp6B5IiI/1SHJGb6N2c=; b=B3SlLKBxkgTl0gZvM8UcKIP4D2z8J11M61tuF/EZoDtuRSLD47/vDdqn3lmiIo9BwFBGriQ9BQ/mmdwJWX0bxkXTRm+7Ozy0qWNqId/0XK4IkK/EwY5/c9buCq7G5ziToLiHHxIoFxagfnSRFKxxyWq3R/+EIw7gFrdwEnHNcUA= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=matan@mellanox.com; Received: from mellanox.com (37.142.13.130) by HE1PR0502MB3804.eurprd05.prod.outlook.com (2603:10a6:7:86::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.428.17; Sat, 20 Jan 2018 21:24:45 +0000 From: Matan Azrad To: Thomas Monjalon , Gaetan Rivet , Jingjing Wu Cc: dev@dpdk.org, Neil Horman , Bruce Richardson , Konstantin Ananyev Date: Sat, 20 Jan 2018 21:24:24 +0000 Message-Id: <1516483468-9048-4-git-send-email-matan@mellanox.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1516483468-9048-1-git-send-email-matan@mellanox.com> References: <1516293317-30748-1-git-send-email-matan@mellanox.com> <1516483468-9048-1-git-send-email-matan@mellanox.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [37.142.13.130] X-ClientProxiedBy: HE1PR0402CA0047.eurprd04.prod.outlook.com (2603:10a6:7:7c::36) To HE1PR0502MB3804.eurprd05.prod.outlook.com (2603:10a6:7:86::30) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f94a58a0-5cbf-451f-b3c2-08d5604c3b00 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(4604075)(48565401081)(2017052603307)(7153060)(7193020); SRVR:HE1PR0502MB3804; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0502MB3804; 3:7HqUvi3gDtTkLleOHZxudBY6Hx62kLxZTJCfUCNBBW1+wY1rBxXesb6C+u+XxIoJKivwZS12uUV5PB1h4S938tsO1wh513cUTwjQX2LPRtQw5pOLqHvrGjKh/w6Jxb8TegtHp04rIzSPVxcYzN2QqcvWGERy8sPYmer10cdeyXRAAvRrYa9NrJFeRVZFydK4ePmalujw+I7A5/vUmu/Luru3C+LveEFW4flswBSDi/jxrBMTtdi22Hi7gp3+IXO/; 25:fdhluH8TBZwxQ0WiUnF+mqSU1UVb/JUyGhGuFhvNUEBGO0J5zVmJ7CaEESY8XOpZiO7AhZCOl3cxm+ZypN03ZFLP3o5oGrxcinaUuvWaKzJ8/4UhKzZU8PLgskMy0vk9wyKH4EAITQdq6xveuVP76mrDnNx8c8KMXCc2+RDmyRRO5GWKCa/E+fYnm9dbw4laVwvmFhCQPOFVs+uXOHqAJ9aeFriCvYWPZijYpN3SD1rgCQBOZgT5UHWuRzNnbbYd6vqt5rl3AKQkhrl8XsJek8ziWRfHIiGSUwyT7lnQkRbN2BwU59msIo0n+fZ8V0kuHnPFOVX4CjOO9SLuEPSrbQ==; 31:7bx9o/2m2HKgq9RqB2k+ojIm33pn+qlHNIuBe6ldhW2WGDGBdRNtt9NZixqO8ejYXk+soqp3JZgDILPRwJx+eXNlsCcBPtNVwXPbgDPQ4+hCRWvrzhmbc2T5ovEL7EFMOmiskopIU7eid5iq1iKHCryDk9PMQeR81PvKzXWV81Ys9tLZ8YT0KvoD3cv9XwdrIReD+G7Zltcm4v3OzhZDxQMLsuS30TFTGsvitVFhzpM= X-MS-TrafficTypeDiagnostic: HE1PR0502MB3804: X-LD-Processed: a652971c-7d2e-4d9b-a6a4-d149256f461b,ExtAddr X-Microsoft-Exchange-Diagnostics: 1; HE1PR0502MB3804; 20:NHXi/ZtaPsn3qBIvrSFm5ec8LG9S7IHBa6BEHW64+xFw/aeLEiN0EWUDb4xWPs/lleuhbGNV5NhzBSjKkMC6daGlf9mmD67l9J9QQ2FaganWdqHiY9eDh28som0QWJQBodJwCCwVJSExIzBMoy82VtWXXeJYjGfZILG8f/L95ItUVTmrHjLKpmArIpR78usWyw5dibJAhytmshpM/JeEic5nCWORoBLNipEFvLvKuKEH3ZNLhtKHOu7YcoIu3WamC5sflOzn5V3RCjeN+Upf4VFX+q7l2O9Z/tBgUkq5rviXba3tpoGB/1rXbOVGxcUQzBUVtdiKs1DDTGtXqKwtigR87ZXpm7swcjw2dcBAsOZJ8gPgSqxk1aBR7XguYxe2359shRq78wC4bop1kAxFGJagVURNOyJqugI7EG6SBiRxq/xqHe24ADKVrMYqTZsjMwpufou/DaZ3tKqLDuX1ZY0hzJhQx4yYYWNGL5DmB78HbvsDwcEzbb9sdCHJ6rzD; 4:lkTvu/4SxxYJLowuvR9lzXxtLmxdYFhkY6kq/q5zwgda7f8gN3C2VbGzSy1yaZZUUSTOCkw6Oq4N1Rvq90f9nUN9HMKTykjhC6W/I66bTC+4p30c0c+PwLbCLqvGH7jOlty6y3mZ1NFLShSuj/HV2DkD0I4A8pP9K57lEa5cYFlU23neoo1qNXV8Nzu3HAQ7y5L8G1S7K6MzW1ryOKb0mu8Gu2dJVr+H20O/xpGA7bKidYXykYIGczNnslontX9FcYY+5sy0Q7Zv456CQvEh4eJyosV7TAEoDXO9XRkXSARNA3Az7DeWzz7gNJBMLdBda2uQu3SwpVs0Y/OcUtkdkAiwnr7Y/atXdpF8NjjL7jk= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(60795455431006)(228905959029699); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040501)(2401047)(8121501046)(5005006)(3231023)(2400081)(944501161)(3002001)(93006095)(93001095)(10201501046)(6055026)(6041288)(20161123562045)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123558120)(6072148)(201708071742011); SRVR:HE1PR0502MB3804; BCL:0; PCL:0; RULEID:(100000803101)(100110400095); SRVR:HE1PR0502MB3804; X-Forefront-PRVS: 0558D3C5AC X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(39860400002)(39380400002)(396003)(376002)(366004)(346002)(199004)(189003)(97736004)(53936002)(26005)(8676002)(54075001)(305945005)(66066001)(2950100002)(6666003)(69596002)(86362001)(55016002)(575784001)(68736007)(50466002)(5890100001)(4720700003)(21086003)(48376002)(6116002)(50226002)(478600001)(25786009)(36756003)(2906002)(5660300001)(4326008)(47776003)(110136005)(52116002)(54906003)(33026002)(105586002)(8936002)(81166006)(386003)(7736002)(106356001)(316002)(59450400001)(81156014)(16526018)(7696005)(51416003)(76176011)(3846002)(16586007); DIR:OUT; SFP:1101; SCL:1; SRVR:HE1PR0502MB3804; H:mellanox.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: mellanox.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0502MB3804; 23:WyOfGKBqNBHyHty5rnFuXgePiEzuY0ruFu8j7K+?= =?us-ascii?Q?Ic1yo1rfOZewle4+dekBzsGdCN4PN86ScJ13RbHJyCMWPL11dYSDu2TOZKCH?= =?us-ascii?Q?sJhNYf8HZqnAZwJqjl5tl+gZPs2Q9+WA+wIfZQbddfre7fWpJxxxAo7taGl1?= =?us-ascii?Q?NbjTri/2Jo9fh8EMTqbWpIS1ofCnWS7sz3+20MzHnwCvXr/0jG2KzEtsYqC4?= =?us-ascii?Q?DWKhQL98PGnMuGoHfBp7C1KJ26DB/zGBemlk00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Le+sC/E71+1S1ymGGLhTqhL/zg8bT3yqYWmqau42aUvnogIw19dlvwZBdOiBANtybS0nmG4zR/1kqlriAVHNZyqR1cafQe0j8 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: Mellanox.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jan 2018 21:24:45.3194 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f94a58a0-5cbf-451f-b3c2-08d5604c3b00 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: a652971c-7d2e-4d9b-a6a4-d149256f461b X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0502MB3804 Subject: [dpdk-dev] [PATCH v4 3/7] ethdev: add port ownership X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 20 Jan 2018 21:24:48 -0000 The ownership of a port is implicit in DPDK. Making it explicit is better from the next reasons: 1. It will define well who is in charge of the port usage synchronization. 2. A library could work on top of a port. 3. A port can work on top of another port. Also in the fail-safe case, an issue has been met in testpmd. We need to check that the application is not trying to use a port which is already managed by fail-safe. A port owner is built from owner id(number) and owner name(string) while the owner id must be unique to distinguish between two identical entity instances and the owner name can be any name. The name helps to logically recognize the owner by different DPDK entities and allows easy debug. Each DPDK entity can allocate an owner unique identifier and can use it and its preferred name to owns valid ethdev ports. Each DPDK entity can get any port owner status to decide if it can manage the port or not. The mechanism is synchronized for both the primary process threads and the secondary processes threads to allow secondary process entity to be a port owner. Add a synchronized ownership mechanism to DPDK Ethernet devices to avoid multiple management of a device by different DPDK entities. The current ethdev internal port management is not affected by this feature. Signed-off-by: Matan Azrad Acked-by: Thomas Monjalon Acked-by: Konstantin Ananyev --- doc/guides/prog_guide/poll_mode_drv.rst | 14 ++- lib/librte_ether/rte_ethdev.c | 202 ++++++++++++++++++++++++++++---- lib/librte_ether/rte_ethdev.h | 115 +++++++++++++++++- lib/librte_ether/rte_ethdev_version.map | 6 + 4 files changed, 306 insertions(+), 31 deletions(-) diff --git a/doc/guides/prog_guide/poll_mode_drv.rst b/doc/guides/prog_guide/poll_mode_drv.rst index d1d4b1c..d513ee3 100644 --- a/doc/guides/prog_guide/poll_mode_drv.rst +++ b/doc/guides/prog_guide/poll_mode_drv.rst @@ -156,8 +156,8 @@ concurrently on the same tx queue without SW lock. This PMD feature found in som See `Hardware Offload`_ for ``DEV_TX_OFFLOAD_MT_LOCKFREE`` capability probing details. -Device Identification and Configuration ---------------------------------------- +Device Identification, Ownership and Configuration +-------------------------------------------------- Device Identification ~~~~~~~~~~~~~~~~~~~~~ @@ -171,6 +171,16 @@ Based on their PCI identifier, NIC ports are assigned two other identifiers: * A port name used to designate the port in console messages, for administration or debugging purposes. For ease of use, the port name includes the port index. +Port Ownership +~~~~~~~~~~~~~~ +The Ethernet devices ports can be owned by a single DPDK entity (application, library, PMD, process, etc). +The ownership mechanism is controlled by ethdev APIs and allows to set/remove/get a port owner by DPDK entities. +Allowing this should prevent any multiple management of Ethernet port by different entities. + +.. note:: + + It is the DPDK entity responsibility to set the port owner before using it and to manage the port usage synchronization between different threads or processes. + Device Configuration ~~~~~~~~~~~~~~~~~~~~ diff --git a/lib/librte_ether/rte_ethdev.c b/lib/librte_ether/rte_ethdev.c index 3a25a64..af0e072 100644 --- a/lib/librte_ether/rte_ethdev.c +++ b/lib/librte_ether/rte_ethdev.c @@ -41,7 +41,6 @@ static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data"; struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS]; -static struct rte_eth_dev_data *rte_eth_dev_data; static uint8_t eth_dev_last_created_port; /* spinlock for eth device callbacks */ @@ -59,6 +58,13 @@ struct rte_eth_xstats_name_off { unsigned offset; }; +/* Shared memory between primary and secondary processes. */ +static struct { + uint64_t next_owner_id; + rte_spinlock_t ownership_lock; + struct rte_eth_dev_data data[RTE_MAX_ETHPORTS]; +} *rte_eth_dev_shared_data; + static const struct rte_eth_xstats_name_off rte_stats_strings[] = { {"rx_good_packets", offsetof(struct rte_eth_stats, ipackets)}, {"tx_good_packets", offsetof(struct rte_eth_stats, opackets)}, @@ -125,24 +131,29 @@ enum { } static void -rte_eth_dev_data_alloc(void) +rte_eth_dev_shared_data_alloc(void) { const unsigned flags = 0; const struct rte_memzone *mz; if (rte_eal_process_type() == RTE_PROC_PRIMARY) { + /* Allocate shared memory for port data and ownership. */ mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA, - RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data), - rte_socket_id(), flags); + sizeof(*rte_eth_dev_shared_data), + rte_socket_id(), flags); } else mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA); if (mz == NULL) rte_panic("Cannot allocate memzone for ethernet port data\n"); - rte_eth_dev_data = mz->addr; - if (rte_eal_process_type() == RTE_PROC_PRIMARY) - memset(rte_eth_dev_data, 0, - RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data)); + rte_eth_dev_shared_data = mz->addr; + if (rte_eal_process_type() == RTE_PROC_PRIMARY) { + rte_eth_dev_shared_data->next_owner_id = + RTE_ETH_DEV_NO_OWNER + 1; + rte_spinlock_init(&rte_eth_dev_shared_data->ownership_lock); + memset(rte_eth_dev_shared_data->data, 0, + sizeof(rte_eth_dev_shared_data->data)); + } } struct rte_eth_dev * @@ -165,7 +176,7 @@ struct rte_eth_dev * for (i = 0; i < RTE_MAX_ETHPORTS; i++) { /* Using shared name field to find a free port. */ - if (rte_eth_dev_data[i].name[0] == '\0') { + if (rte_eth_dev_shared_data->data[i].name[0] == '\0') { RTE_ASSERT(rte_eth_devices[i].state == RTE_ETH_DEV_UNUSED); return i; @@ -179,7 +190,7 @@ struct rte_eth_dev * { struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id]; - eth_dev->data = &rte_eth_dev_data[port_id]; + eth_dev->data = &rte_eth_dev_shared_data->data[port_id]; eth_dev->state = RTE_ETH_DEV_ATTACHED; eth_dev_last_created_port = port_id; @@ -199,8 +210,8 @@ struct rte_eth_dev * return NULL; } - if (rte_eth_dev_data == NULL) - rte_eth_dev_data_alloc(); + if (rte_eth_dev_shared_data == NULL) + rte_eth_dev_shared_data_alloc(); if (rte_eth_dev_allocated(name) != NULL) { RTE_PMD_DEBUG_TRACE("Ethernet Device with name %s already allocated!\n", @@ -229,11 +240,11 @@ struct rte_eth_dev * uint16_t i; struct rte_eth_dev *eth_dev; - if (rte_eth_dev_data == NULL) - rte_eth_dev_data_alloc(); + if (rte_eth_dev_shared_data == NULL) + rte_eth_dev_shared_data_alloc(); for (i = 0; i < RTE_MAX_ETHPORTS; i++) { - if (strcmp(rte_eth_dev_data[i].name, name) == 0) + if (strcmp(rte_eth_dev_shared_data->data[i].name, name) == 0) break; } if (i == RTE_MAX_ETHPORTS) { @@ -255,9 +266,14 @@ struct rte_eth_dev * if (eth_dev == NULL) return -EINVAL; - memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data)); + rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock); + eth_dev->state = RTE_ETH_DEV_UNUSED; + memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data)); + + rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock); + _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_DESTROY, NULL); return 0; @@ -273,6 +289,144 @@ struct rte_eth_dev * return 1; } +static int +rte_eth_is_valid_owner_id(uint64_t owner_id) +{ + if (owner_id == RTE_ETH_DEV_NO_OWNER || + rte_eth_dev_shared_data->next_owner_id <= owner_id) { + RTE_LOG(ERR, EAL, "Invalid owner_id=%016lX.\n", owner_id); + return 0; + } + return 1; +} + +uint64_t +rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id) +{ + while (port_id < RTE_MAX_ETHPORTS && + ((rte_eth_devices[port_id].state != RTE_ETH_DEV_ATTACHED && + rte_eth_devices[port_id].state != RTE_ETH_DEV_REMOVED) || + rte_eth_devices[port_id].data->owner.id != owner_id)) + port_id++; + + if (port_id >= RTE_MAX_ETHPORTS) + return RTE_MAX_ETHPORTS; + + return port_id; +} + +int +rte_eth_dev_owner_new(uint64_t *owner_id) +{ + rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock); + + *owner_id = rte_eth_dev_shared_data->next_owner_id++; + + rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock); + return 0; +} + +static int +_rte_eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id, + const struct rte_eth_dev_owner *new_owner) +{ + struct rte_eth_dev_owner *port_owner; + int sret; + + RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV); + + if (!rte_eth_is_valid_owner_id(new_owner->id) && + !rte_eth_is_valid_owner_id(old_owner_id)) + return -EINVAL; + + port_owner = &rte_eth_devices[port_id].data->owner; + if (port_owner->id != old_owner_id) { + RTE_LOG(ERR, EAL, "Cannot set owner to port %d already owned" + " by %s_%016lX.\n", port_id, port_owner->name, + port_owner->id); + return -EPERM; + } + + sret = snprintf(port_owner->name, RTE_ETH_MAX_OWNER_NAME_LEN, "%s", + new_owner->name); + if (sret < 0 || sret >= RTE_ETH_MAX_OWNER_NAME_LEN) + RTE_LOG(WARNING, EAL, "Port %d owner name was truncated.\n", + port_id); + + port_owner->id = new_owner->id; + + RTE_PMD_DEBUG_TRACE("Port %d owner is %s_%016lX.\n", port_id, + new_owner->name, new_owner->id); + + return 0; +} + +int +rte_eth_dev_owner_set(const uint16_t port_id, + const struct rte_eth_dev_owner *owner) +{ + int ret; + + rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock); + + ret = _rte_eth_dev_owner_set(port_id, RTE_ETH_DEV_NO_OWNER, owner); + + rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock); + return ret; +} + +int +rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id) +{ + const struct rte_eth_dev_owner new_owner = (struct rte_eth_dev_owner) + {.id = RTE_ETH_DEV_NO_OWNER, .name = ""}; + int ret; + + rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock); + + ret = _rte_eth_dev_owner_set(port_id, owner_id, &new_owner); + + rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock); + return ret; +} + +void +rte_eth_dev_owner_delete(const uint64_t owner_id) +{ + uint16_t port_id; + + rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock); + + if (rte_eth_is_valid_owner_id(owner_id)) { + RTE_ETH_FOREACH_DEV_OWNED_BY(port_id, owner_id) + memset(&rte_eth_devices[port_id].data->owner, 0, + sizeof(struct rte_eth_dev_owner)); + RTE_PMD_DEBUG_TRACE("All port owners owned by %016X identifier" + " have removed.\n", owner_id); + } + + rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock); +} + +int +rte_eth_dev_owner_get(const uint16_t port_id, struct rte_eth_dev_owner *owner) +{ + int ret = 0; + + rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock); + + if (!rte_eth_dev_is_valid_port(port_id)) { + RTE_PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id); + ret = -ENODEV; + } else { + rte_memcpy(owner, &rte_eth_devices[port_id].data->owner, + sizeof(*owner)); + } + + rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock); + return ret; +} + int rte_eth_dev_socket_id(uint16_t port_id) { @@ -315,7 +469,7 @@ struct rte_eth_dev * /* shouldn't check 'rte_eth_devices[i].data', * because it might be overwritten by VDEV PMD */ - tmp = rte_eth_dev_data[port_id].name; + tmp = rte_eth_dev_shared_data->data[port_id].name; strcpy(name, tmp); return 0; } @@ -323,22 +477,22 @@ struct rte_eth_dev * int rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id) { - int i; + uint32_t pid; if (name == NULL) { RTE_PMD_DEBUG_TRACE("Null pointer is specified\n"); return -EINVAL; } - RTE_ETH_FOREACH_DEV(i) { - if (!strncmp(name, - rte_eth_dev_data[i].name, strlen(name))) { - - *port_id = i; - + for (pid = 0; pid < RTE_MAX_ETHPORTS; pid++) { + if (rte_eth_devices[pid].state != RTE_ETH_DEV_UNUSED && + !strncmp(name, rte_eth_dev_shared_data->data[pid].name, + strlen(name))) { + *port_id = pid; return 0; } } + return -ENODEV; } diff --git a/lib/librte_ether/rte_ethdev.h b/lib/librte_ether/rte_ethdev.h index 084eeeb..cdeaf7a 100644 --- a/lib/librte_ether/rte_ethdev.h +++ b/lib/librte_ether/rte_ethdev.h @@ -1739,6 +1739,15 @@ struct rte_eth_dev_sriov { #define RTE_ETH_NAME_MAX_LEN RTE_DEV_NAME_MAX_LEN +#define RTE_ETH_DEV_NO_OWNER 0 + +#define RTE_ETH_MAX_OWNER_NAME_LEN 64 + +struct rte_eth_dev_owner { + uint64_t id; /**< The owner unique identifier. */ + char name[RTE_ETH_MAX_OWNER_NAME_LEN]; /**< The owner name. */ +}; + /** * @internal * The data part, with no function pointers, associated with each ethernet device. @@ -1789,6 +1798,7 @@ struct rte_eth_dev_data { int numa_node; /**< NUMA node connection */ struct rte_vlan_filter_conf vlan_filter_conf; /**< VLAN filter configuration. */ + struct rte_eth_dev_owner owner; /**< The port owner. */ }; /** Device supports link state interrupt */ @@ -1806,6 +1816,30 @@ struct rte_eth_dev_data { extern struct rte_eth_dev rte_eth_devices[]; /** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Iterates over valid ethdev ports owned by a specific owner. + * + * @param port_id + * The id of the next possible valid owned port. + * @param owner_id + * The owner identifier. + * RTE_ETH_DEV_NO_OWNER means iterate over all valid ownerless ports. + * @return + * Next valid port id owned by owner_id, RTE_MAX_ETHPORTS if there is none. + */ +uint64_t rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id); + +/** + * Macro to iterate over all enabled ethdev ports owned by a specific owner. + */ +#define RTE_ETH_FOREACH_DEV_OWNED_BY(p, o) \ + for (p = rte_eth_find_next_owned_by(0, o); \ + (unsigned int)p < (unsigned int)RTE_MAX_ETHPORTS; \ + p = rte_eth_find_next_owned_by(p + 1, o)) + +/** * Iterates over valid ethdev ports. * * @param port_id @@ -1816,13 +1850,84 @@ struct rte_eth_dev_data { uint16_t rte_eth_find_next(uint16_t port_id); /** - * Macro to iterate over all enabled ethdev ports. + * Macro to iterate over all enabled and ownerless ethdev ports. + */ +#define RTE_ETH_FOREACH_DEV(p) \ + RTE_ETH_FOREACH_DEV_OWNED_BY(p, RTE_ETH_DEV_NO_OWNER) + + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Get a new unique owner identifier. + * An owner identifier is used to owns Ethernet devices by only one DPDK entity + * to avoid multiple management of device by different entities. + * + * @param owner_id + * Owner identifier pointer. + * @return + * Negative errno value on error, 0 on success. */ -#define RTE_ETH_FOREACH_DEV(p) \ - for (p = rte_eth_find_next(0); \ - (unsigned int)p < (unsigned int)RTE_MAX_ETHPORTS; \ - p = rte_eth_find_next(p + 1)) +int rte_eth_dev_owner_new(uint64_t *owner_id); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Set an Ethernet device owner. + * + * @param port_id + * The identifier of the port to own. + * @param owner + * The owner pointer. + * @return + * Negative errno value on error, 0 on success. + */ +int rte_eth_dev_owner_set(const uint16_t port_id, + const struct rte_eth_dev_owner *owner); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Unset Ethernet device owner to make the device ownerless. + * + * @param port_id + * The identifier of port to make ownerless. + * @param owner + * The owner identifier. + * @return + * 0 on success, negative errno value on error. + */ +int rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Remove owner from all Ethernet devices owned by a specific owner. + * + * @param owner + * The owner identifier. + */ +void rte_eth_dev_owner_delete(const uint64_t owner_id); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Get the owner of an Ethernet device. + * + * @param port_id + * The port identifier. + * @param owner + * The owner structure pointer to fill. + * @return + * 0 on success, negative errno value on error.. + */ +int rte_eth_dev_owner_get(const uint16_t port_id, + struct rte_eth_dev_owner *owner); /** * Get the total number of Ethernet devices that have been successfully diff --git a/lib/librte_ether/rte_ethdev_version.map b/lib/librte_ether/rte_ethdev_version.map index 88b7908..545f7a6 100644 --- a/lib/librte_ether/rte_ethdev_version.map +++ b/lib/librte_ether/rte_ethdev_version.map @@ -202,6 +202,12 @@ EXPERIMENTAL { global: rte_eth_dev_is_removed; + rte_eth_dev_owner_delete; + rte_eth_dev_owner_get; + rte_eth_dev_owner_new; + rte_eth_dev_owner_set; + rte_eth_dev_owner_unset; + rte_eth_find_next_owned_by; rte_mtr_capabilities_get; rte_mtr_create; rte_mtr_destroy; -- 1.8.3.1