From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 61C2FA0546; Sun, 14 Feb 2021 03:16:53 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2409322A238; Sun, 14 Feb 2021 03:16:33 +0100 (CET) Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) by mails.dpdk.org (Postfix) with ESMTP id E92A51CC502 for ; Sun, 14 Feb 2021 03:16:29 +0100 (CET) Received: by mail-lf1-f46.google.com with SMTP id z11so5160491lfb.9 for ; Sat, 13 Feb 2021 18:16:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=INuMMPX7mPyXPcRHrhvrdRfHk3dCHnteBdjEwxYTTeQ=; b=Pv6MXs3Onp3tLZQ7lOGUDgdicAHsi3yzWRN1nXu2vKBGcEFLykvC++Ozemm1HmqMX6 PuqdpRbGBdNN5Zh1ADGrjA1EOI2fMbNW5X3QtHh8gfSGB1N7Smyo3pZh0DevK8cHhJnj rY3yb0x5/8rLrMg37ua467WMuLze2yvJl59rZ4JdZ0efOB8bM2hvMT+VEDt5ZMb8iq3g a/USTszMlfvDdAoF2BBezAa5FcWA6iUT9UkPHU4bktoPJIyDcdVgUP/WpKKkD7Q7lHIg COU8dO7+QLPXvbhwcWVjWiJ74+VFXzBXMmTtm/rUqzr6m/iRl4Oha766UhlqLnZ412C7 MZ7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=INuMMPX7mPyXPcRHrhvrdRfHk3dCHnteBdjEwxYTTeQ=; b=GVdnxMFVriaACNN/oRC8kgvfGYfhGcycdAQL6JR/iOKvbuMC1pjvRlfzblAqeJ5EE0 pVFOUi0Q9uTO+V0RpP+Qst3r9RHLF8yGCYyYUzCuEUOznmc1MZfChEuW7bNBeauq53ky K4yYrHBb8jT1LeQSJHil68WcDvZtU01azFmPOGBXbm+i0IAxP0OAlmtj5A7NOUpzOtsQ Zkzh78YdYgYsI6Fa7ComXeHFktrXa4Nskkan9dzgu1Cwc9w2N9nhQ9kvF+CkW584WmQj A60C2YUBmeVhY8VumY3X5wyHvoMRy5osoTcXKIRd5I6TqzfmQnV27ooH8NPfkx9p5GZJ 2PzA== X-Gm-Message-State: AOAM5325vIjkAEDO4BC5kEJdlhX/EZ/KM37Gbqwm9WQ7KwD5Ki6trBZt 2zbu1WHyXlwTkKB5JtJEenx+9rLqa47hHg== X-Google-Smtp-Source: ABdhPJxEpLhRokK8E6vmCCB1JddfTaPbTrBfznuWqGHikDRaedbZGGtSLOuDbQ4ua89eQsphfr+ynQ== X-Received: by 2002:a05:6512:33c2:: with SMTP id d2mr4350984lfg.429.1613268989245; Sat, 13 Feb 2021 18:16:29 -0800 (PST) Received: from localhost.localdomain (broadband-37-110-65-23.ip.moscow.rt.ru. [37.110.65.23]) by smtp.gmail.com with ESMTPSA id l19sm3017158lji.53.2021.02.13.18.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Feb 2021 18:16:28 -0800 (PST) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: Tyler Retzlaff , Mike Wells , Dmitry Kozlyuk , Ferruh Yigit Date: Sun, 14 Feb 2021 05:16:13 +0300 Message-Id: <20210214021616.26970-4-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210214021616.26970-1-dmitry.kozliuk@gmail.com> References: <20210214012013.23165-1-dmitry.kozliuk@gmail.com> <20210214021616.26970-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2 3/6] net/pcap: move OS-dependent code to separate files X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" PCAP PMD queries interface information differently for Linux and FreeBSD, OS-specific code is guarded by #ifdef. In preparation to add Windows-specific part and libpcap wrapper, extract OS-independent interface and move implementations to separate files. Rename rte_eth_pcap.c to pcap_ethdev.c for consistency with the rest of DPDK. Signed-off-by: Dmitry Kozlyuk --- drivers/net/pcap/meson.build | 5 +- .../pcap/{rte_eth_pcap.c => pcap_ethdev.c} | 88 ++----------------- drivers/net/pcap/pcap_osdep.h | 17 ++++ drivers/net/pcap/pcap_osdep_freebsd.c | 59 +++++++++++++ drivers/net/pcap/pcap_osdep_linux.c | 42 +++++++++ 5 files changed, 130 insertions(+), 81 deletions(-) rename drivers/net/pcap/{rte_eth_pcap.c => pcap_ethdev.c} (95%) create mode 100644 drivers/net/pcap/pcap_osdep.h create mode 100644 drivers/net/pcap/pcap_osdep_freebsd.c create mode 100644 drivers/net/pcap/pcap_osdep_linux.c diff --git a/drivers/net/pcap/meson.build b/drivers/net/pcap/meson.build index b65d91e70..26b606699 100644 --- a/drivers/net/pcap/meson.build +++ b/drivers/net/pcap/meson.build @@ -11,5 +11,8 @@ if not dpdk_conf.has('RTE_PORT_PCAP') build = false reason = 'missing dependency, "libpcap"' endif -sources = files('rte_eth_pcap.c') +sources = files( + 'pcap_ethdev.c', + 'pcap_osdep_@0@.c'.format(exec_env), +) ext_deps += pcap_dep diff --git a/drivers/net/pcap/rte_eth_pcap.c b/drivers/net/pcap/pcap_ethdev.c similarity index 95% rename from drivers/net/pcap/rte_eth_pcap.c rename to drivers/net/pcap/pcap_ethdev.c index 176928da3..a102897e9 100644 --- a/drivers/net/pcap/rte_eth_pcap.c +++ b/drivers/net/pcap/pcap_ethdev.c @@ -6,16 +6,6 @@ #include -#include -#include -#include -#include - -#if defined(RTE_EXEC_ENV_FREEBSD) -#include -#include -#endif - #include #include @@ -25,7 +15,8 @@ #include #include #include -#include + +#include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535 #define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN @@ -1191,84 +1182,20 @@ static int eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev, const unsigned int numa_node) { -#if defined(RTE_EXEC_ENV_LINUX) void *mac_addrs; - struct ifreq ifr; - int if_fd = socket(AF_INET, SOCK_DGRAM, 0); - - if (if_fd == -1) - return -1; + struct rte_ether_addr mac; - rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name)); - if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) { - close(if_fd); + if (osdep_iface_mac_get(if_name, &mac) < 0) return -1; - } mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node); - if (!mac_addrs) { - close(if_fd); + if (mac_addrs == NULL) return -1; - } PMD_LOG(INFO, "Setting phy MAC for %s", if_name); + rte_memcpy(mac_addrs, mac.addr_bytes, RTE_ETHER_ADDR_LEN); eth_dev->data->mac_addrs = mac_addrs; - rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes, - ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN); - - close(if_fd); - - return 0; - -#elif defined(RTE_EXEC_ENV_FREEBSD) - void *mac_addrs; - struct if_msghdr *ifm; - struct sockaddr_dl *sdl; - int mib[6]; - size_t len = 0; - char *buf; - - mib[0] = CTL_NET; - mib[1] = AF_ROUTE; - mib[2] = 0; - mib[3] = AF_LINK; - mib[4] = NET_RT_IFLIST; - mib[5] = if_nametoindex(if_name); - - if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) - return -1; - - if (len == 0) - return -1; - - buf = rte_malloc(NULL, len, 0); - if (!buf) - return -1; - - if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { - rte_free(buf); - return -1; - } - ifm = (struct if_msghdr *)buf; - sdl = (struct sockaddr_dl *)(ifm + 1); - - mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node); - if (!mac_addrs) { - rte_free(buf); - return -1; - } - - PMD_LOG(INFO, "Setting phy MAC for %s", if_name); - eth_dev->data->mac_addrs = mac_addrs; - rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes, - LLADDR(sdl), RTE_ETHER_ADDR_LEN); - - rte_free(buf); - return 0; -#else - return -1; -#endif } static int @@ -1330,7 +1257,8 @@ eth_from_pcaps(struct rte_vdev_device *vdev, internals->single_iface = single_iface; if (single_iface) { - internals->if_index = if_nametoindex(rx_queues->queue[0].name); + internals->if_index = + osdep_iface_index_get(rx_queues->queue[0].name); /* phy_mac arg is applied only only if "iface" devarg is provided */ if (rx_queues->phy_mac) { diff --git a/drivers/net/pcap/pcap_osdep.h b/drivers/net/pcap/pcap_osdep.h new file mode 100644 index 000000000..46810d86f --- /dev/null +++ b/drivers/net/pcap/pcap_osdep.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2021 Dmitry Kozlyuk + */ + +#ifndef _RTE_PCAP_OSDEP_ +#define _RTE_PCAP_OSDEP_ + +#include + +/* + * Interface manipulation is always OS-specific. + */ + +int osdep_iface_index_get(const char *name); +int osdep_iface_mac_get(const char *name, struct rte_ether_addr *mac); + +#endif diff --git a/drivers/net/pcap/pcap_osdep_freebsd.c b/drivers/net/pcap/pcap_osdep_freebsd.c new file mode 100644 index 000000000..20556b3e9 --- /dev/null +++ b/drivers/net/pcap/pcap_osdep_freebsd.c @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2016 Intel Corporation. + * Copyright(c) 2014 6WIND S.A. + * All rights reserved. + */ + +#include +#include +#include + +#include +#include + +#include "pcap_osdep.h" + +int +osdep_iface_index_get(const char *name) +{ + return if_nametoindex(name); +} + +int +osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac) +{ + struct if_msghdr *ifm; + struct sockaddr_dl *sdl; + int mib[6]; + size_t len = 0; + char *buf; + + mib[0] = CTL_NET; + mib[1] = AF_ROUTE; + mib[2] = 0; + mib[3] = AF_LINK; + mib[4] = NET_RT_IFLIST; + mib[5] = if_nametoindex(if_name); + + if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) + return -1; + + if (len == 0) + return -1; + + buf = rte_malloc(NULL, len, 0); + if (!buf) + return -1; + + if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { + rte_free(buf); + return -1; + } + ifm = (struct if_msghdr *)buf; + sdl = (struct sockaddr_dl *)(ifm + 1); + + rte_memcpy(mac->addr_bytes, LLADDR(sdl), RTE_ETHER_ADDR_LEN); + + rte_free(buf); + return 0; +} diff --git a/drivers/net/pcap/pcap_osdep_linux.c b/drivers/net/pcap/pcap_osdep_linux.c new file mode 100644 index 000000000..97033f57c --- /dev/null +++ b/drivers/net/pcap/pcap_osdep_linux.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2016 Intel Corporation. + * Copyright(c) 2014 6WIND S.A. + * All rights reserved. + */ + +#include +#include +#include +#include + +#include +#include + +#include "pcap_osdep.h" + +int +osdep_iface_index_get(const char *name) +{ + return if_nametoindex(name); +} + +int +osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac) +{ + struct ifreq ifr; + int if_fd = socket(AF_INET, SOCK_DGRAM, 0); + + if (if_fd == -1) + return -1; + + rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name)); + if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) { + close(if_fd); + return -1; + } + + rte_memcpy(mac->addr_bytes, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN); + + close(if_fd); + return 0; +} -- 2.29.2