From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR03-AM5-obe.outbound.protection.outlook.com (mail-eopbgr30061.outbound.protection.outlook.com [40.107.3.61]) by dpdk.org (Postfix) with ESMTP id 9A47BA490 for ; Mon, 22 Jan 2018 17:38:48 +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=7xxrCcDOTPAHuXdjDB8n5ps5SsEoK5KwQ1nHm/ottJ4=; b=ZLRUUPBY4Bq9dWXW6Mej0hl8EzKz0D2wR+ydQ9j5VrjEUCt2mzufLO+rw9thmr7hPCUE9EqHPimWFpmVvfuf3csiqnwWI7Sb7A0Cu6pzjBp3zQ26PbU0oiWVhvCWLWKg0un8cERi6SMEt57QAsjZhaVzovR+Yo3ECfzbxb+EW68= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=matan@mellanox.com; Received: from mellanox.com (37.142.13.130) by HE1PR0502MB3802.eurprd05.prod.outlook.com (2603:10a6:7:86::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.428.17; Mon, 22 Jan 2018 16:38:44 +0000 From: Matan Azrad To: Thomas Monjalon , Gaetan Rivet , Jingjing Wu Cc: dev@dpdk.org, Neil Horman , Bruce Richardson , Konstantin Ananyev Date: Mon, 22 Jan 2018 16:38:19 +0000 Message-Id: <1516639103-27166-4-git-send-email-matan@mellanox.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1516639103-27166-1-git-send-email-matan@mellanox.com> References: <1516483468-9048-1-git-send-email-matan@mellanox.com> <1516639103-27166-1-git-send-email-matan@mellanox.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [37.142.13.130] X-ClientProxiedBy: HE1PR0102CA0058.eurprd01.prod.exchangelabs.com (2603:10a6:7:7d::35) To HE1PR0502MB3802.eurprd05.prod.outlook.com (2603:10a6:7:86::28) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4d1e75e3-2d03-437b-d7c4-08d561b69b6f X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(4604075)(48565401081)(2017052603307)(7153060)(7193020); SRVR:HE1PR0502MB3802; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0502MB3802; 3:H/YjNVlcxFvxSGaqbLBc6a/bkk/ZmED+Zjld6N+alozgdAzaFXj0DI7sAuLfkIgHq5gdcyMM1W+YB0t8DQdzJRk/pLdzt97J3XuNXdtFltlyc3WjhePv1WIIPzIZF15IfuZFp1EqH5qOGC6GiGVDx8Zxu3sfdetSyK7OrqfwyhYSb1ydJLhZk6ib+JMB8zgSSSxKi/+OOnYTFqcy3qiLJNaV/v637DOYzE1k7h4/E2wxfnpLstnEFJuSAKYmLe22; 25:EOEqNd0ZinTs7GyZNMDKCPOH7Y9CXpQPkx3+PfboggyyKHmzLWrw+2dFKX9nvOx+er9bGZNn3T1w2ucliuCj22wcNw5AjuPcbtzWstdhdWbk/qvYEgS7Jz5S0qyZ2gH2h1VoXWeIp2G9gjMF3KyWOoKx5Tof/z4mBf99XLZcsUAxayKUErTQ8wVy+ls4Jq0/5PMAkIdbJedX5pVJyR+ZpayPnsOyONt8aPBzfLH1MERbiIQAAQ/I441Aug2J6yUDpPD1Qjsv9y292egdtu9TpWMiGe54SvR3+xBkRZkatA9cT6yS/YIEf4Zi8ZDuoqQclVvhNf6W45tfh10o7ZmLqg==; 31:CYiAZfapf/QvY7EIFbrRTtuGLOt6s/bf+Fm086AuMx29yot7qM1FEigxQIMi/QHT8QSGOxDolK23l3y5V9a5RSw0nBPPZvac2Xn4lOkoVQ3pO1rdvnpzrpJdSouYoNSgrHJTu8J3EZ6eL+Wh7xYkSaWR3X8HGS4s4P9TY+v3j6Io5UNTUy2jg6+RYD3tYQzrPLCY7uvqlFqwUvr8jSI362An317z8DUstcOeSYnahSc= X-MS-TrafficTypeDiagnostic: HE1PR0502MB3802: X-LD-Processed: a652971c-7d2e-4d9b-a6a4-d149256f461b,ExtAddr X-Microsoft-Exchange-Diagnostics: 1; HE1PR0502MB3802; 20:dv/a95iZ8xJ+D6sH5wkWXSfXu+z89HBxhBOt0PQyU7UWOcL8gMwMfOt41cnIOjIRwAQySlLTT9kT/zUyyaMtDoJKKzA2aQz5JOnAAOlosO2yJn379ki7EaSkvI/dOvvjOAQmPhE3Aw7c4ENuhQqYh0BTPjUyjinkkiSPtbO6I1Oa2opeKtw2dEdN+jqJQVkCSpbeyUiw3IEt4sCaSisn7r/uHrJpdFki1QFF2dIT9FCKZ9q3fqEYzwOW1eNId71WqGlRN7dKKauyMq4sox/4G1YGRMES7PWkDzz+anpaUM9lxn9g69IbtP8ojxGkH4p3cx4Q1blnmos17bQI2pMy7oGwEesllVm2Odovc7/0EywJUniFWRgUcaVwnr1jZZJmlz8gRPhdX6nm61PpAV+296l/GwKiqHkta4NkzRTQ24tritw5v4M0wo1sAB9lIsmF0LpTgD7b6W0gGCAseanZMabPpg0nWEhOYUa+hFxS+itm8nZ+r7h5PBXi6c96rCnm; 4:BXIts7pITeYG0S+OQ2r55AsOJtD2O7Mh3Ms+NrFtC33764Xt36UtkMdz34gIpeCnLz+GSdIjb3D97DqwEvl+/liy9xKWgchJ3z4AEz+LPRHyBKhU+mHvLy+z+zXrHcjsv2JJYEEPuaIpwKWkhDPafII7yb6hoDKHqTpU4GXUEACLyjtbttjIhniZ4pQdrenLqyfE6VRMIBwhb2pNdiKOPJd2iI2gaAyXNfx7341AliuGYl8DxA/GmAtr9w8DAfaGrBulobTHD4J0gLcJqBhd58GsBCekwqOQr9zZjP/2NDQKQ3yxMADRWrgLYI7stAek0SGi0opuzrYfSEDKWXaOVXgghjwrB/kx5H00oMnh8t8= 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)(5005006)(8121501046)(93006095)(93001095)(3231023)(2400081)(944501161)(3002001)(10201501046)(6055026)(6041288)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123562045)(20161123560045)(20161123564045)(6072148)(201708071742011); SRVR:HE1PR0502MB3802; BCL:0; PCL:0; RULEID:(100000803101)(100110400095); SRVR:HE1PR0502MB3802; X-Forefront-PRVS: 0560A2214D X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(346002)(39380400002)(366004)(396003)(376002)(39860400002)(199004)(189003)(5660300001)(53936002)(47776003)(8936002)(50226002)(2906002)(68736007)(4720700003)(55016002)(6666003)(2950100002)(69596002)(478600001)(76176011)(7696005)(316002)(52116002)(386003)(16586007)(36756003)(51416003)(59450400001)(6116002)(3846002)(16526018)(8676002)(81166006)(81156014)(66066001)(110136005)(48376002)(26005)(54906003)(86362001)(25786009)(106356001)(5890100001)(105586002)(50466002)(305945005)(4326008)(7736002)(97736004)(54075001)(21086003)(33026002); DIR:OUT; SFP:1101; SCL:1; SRVR:HE1PR0502MB3802; 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; HE1PR0502MB3802; 23:VJuQvYASoT8kyygghbs+S/8OAQfuDH/CmSTFxbc?= =?us-ascii?Q?K2cPlPeAc62oZFgYcDpbMO4Lyhw8Flec/c/R4Y+qj8PGPxkMz3b8VxhWnwfU?= =?us-ascii?Q?HuOLjwTrbhh4gC6cl2bvFyBl/WsAwhb0elxt7PFPl8fEzBWsu9QzmObFFSq2?= =?us-ascii?Q?zyqxejm7GxqtRszdp9Vvwp9oi9hLtfExsfKO7+unA69fXdDagkUGFqhZEeL+?= =?us-ascii?Q?27pJkaHNuOki0zXkYF93wEFcGKJ1KfDcWycqU4J4chM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sm50crUkrMMP9P1YbfDu3qmdM5+I75aYIY3BmVnLzp36cSrIXE SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: Mellanox.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jan 2018 16:38:44.9564 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4d1e75e3-2d03-437b-d7c4-08d561b69b6f X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: a652971c-7d2e-4d9b-a6a4-d149256f461b X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0502MB3802 Subject: [dpdk-dev] [PATCH v5 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: Mon, 22 Jan 2018 16:38:49 -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 | 233 ++++++++++++++++++++++++++++---- lib/librte_ether/rte_ethdev.h | 115 +++++++++++++++- lib/librte_ether/rte_ethdev_core.h | 2 + lib/librte_ether/rte_ethdev_version.map | 6 + 5 files changed, 333 insertions(+), 37 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 e348b63..3d2b90c 100644 --- a/lib/librte_ether/rte_ethdev.c +++ b/lib/librte_ether/rte_ethdev.c @@ -43,7 +43,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 */ @@ -55,12 +54,22 @@ /* spinlock for add/remove tx callbacks */ static rte_spinlock_t rte_eth_tx_cb_lock = RTE_SPINLOCK_INITIALIZER; +/* spinlock for shared data allocation */ +static rte_spinlock_t rte_eth_shared_data_lock = RTE_SPINLOCK_INITIALIZER; + /* store statistics names and its offset in stats structure */ struct rte_eth_xstats_name_off { char name[RTE_ETH_XSTATS_NAME_SIZE]; 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)}, @@ -182,24 +191,35 @@ enum { } static void -rte_eth_dev_data_alloc(void) +rte_eth_dev_shared_data_prepare(void) { const unsigned flags = 0; const struct rte_memzone *mz; - if (rte_eal_process_type() == RTE_PROC_PRIMARY) { - mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA, - RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_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_spinlock_lock(&rte_eth_shared_data_lock); + + if (rte_eth_dev_shared_data == NULL) { + if (rte_eal_process_type() == RTE_PROC_PRIMARY) { + /* Allocate port data and ownership shared memory. */ + mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA, + 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 ethdev shared data\n"); + + 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)); + } + } - 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_spinlock_unlock(&rte_eth_shared_data_lock); } struct rte_eth_dev * @@ -222,7 +242,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; @@ -236,7 +256,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; @@ -256,8 +276,7 @@ struct rte_eth_dev * return NULL; } - if (rte_eth_dev_data == NULL) - rte_eth_dev_data_alloc(); + rte_eth_dev_shared_data_prepare(); if (rte_eth_dev_allocated(name) != NULL) { RTE_PMD_DEBUG_TRACE("Ethernet Device with name %s already allocated!\n", @@ -286,11 +305,10 @@ struct rte_eth_dev * uint16_t i; struct rte_eth_dev *eth_dev; - if (rte_eth_dev_data == NULL) - rte_eth_dev_data_alloc(); + rte_eth_dev_shared_data_prepare(); 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) { @@ -312,9 +330,16 @@ struct rte_eth_dev * if (eth_dev == NULL) return -EINVAL; - memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data)); + rte_eth_dev_shared_data_prepare(); + + 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; @@ -330,6 +355,154 @@ 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_PMD_DEBUG_TRACE("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_eth_dev_shared_data_prepare(); + + 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_PMD_DEBUG_TRACE("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_PMD_DEBUG_TRACE("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_eth_dev_shared_data_prepare(); + + 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_eth_dev_shared_data_prepare(); + + 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_eth_dev_shared_data_prepare(); + + 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_eth_dev_shared_data_prepare(); + + 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) { @@ -372,7 +545,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; } @@ -380,22 +553,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 ccf4a15..db3199f 100644 --- a/lib/librte_ether/rte_ethdev.h +++ b/lib/librte_ether/rte_ethdev.h @@ -1221,6 +1221,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. */ +}; + /** Device supports link state interrupt */ #define RTE_ETH_DEV_INTR_LSC 0x0002 /** Device is a bonded slave */ @@ -1229,6 +1238,30 @@ struct rte_eth_dev_sriov { #define RTE_ETH_DEV_INTR_RMV 0x0008 /** + * @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 @@ -1239,12 +1272,84 @@ struct rte_eth_dev_sriov { 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. + */ +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_id + * 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_id + * 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.. */ -#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_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_core.h b/lib/librte_ether/rte_ethdev_core.h index f44b40e..f9fccc4 100644 --- a/lib/librte_ether/rte_ethdev_core.h +++ b/lib/librte_ether/rte_ethdev_core.h @@ -544,6 +544,7 @@ struct rte_eth_dev { } __rte_cache_aligned; struct rte_eth_dev_sriov; +struct rte_eth_dev_owner; /** * @internal @@ -595,6 +596,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. */ }; /** diff --git a/lib/librte_ether/rte_ethdev_version.map b/lib/librte_ether/rte_ethdev_version.map index 5c61563..a5cb372 100644 --- a/lib/librte_ether/rte_ethdev_version.map +++ b/lib/librte_ether/rte_ethdev_version.map @@ -209,8 +209,14 @@ 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_dev_rx_offload_name; rte_eth_dev_tx_offload_name; + rte_eth_find_next_owned_by; rte_mtr_capabilities_get; rte_mtr_create; rte_mtr_destroy; -- 1.8.3.1