From patchwork Tue Sep 26 06:00:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 114215 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp3452452qgf; Mon, 25 Sep 2017 23:02:56 -0700 (PDT) X-Google-Smtp-Source: AOwi7QCpP3wtAPTXJXPeHNNx6fksC+U8ssROAAP1NQJoNrsymKvascIgsfSiiASj1lw67ayo+PhA X-Received: by 10.107.28.129 with SMTP id c123mr12465810ioc.303.1506405776672; Mon, 25 Sep 2017 23:02:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506405776; cv=none; d=google.com; s=arc-20160816; b=XQ7B/Nq9+g9gzNtGTIBCt5K7lXYYl+D4tuQXg0o05UEdlX6M7d7IJM0b8Me08ofFsb I+NQg47k1eNarV/B2K2QXME0V0yxtY8CKzY8e3Crs76c8lYkCzoBsL5bvsuUTsBRxG9E ZKGtKH630DkX0B/Onev5+39D1NP6p1wry4JnmiiKPwq+/bNh6QQUQ3nc/r5ofQlzC6Vi NQ4Zt53xDt5qagB7jNxIRZkp6okGGuFM8QLlN7T16K5Lg/ud2wdSwIWvdXXFnUSnxgqZ jK95hteA0B/9JpoSDNl6eq5kPyM+llBSC7MBIT1gbxNsf77lqVf/ui9hl89yvEbyO/yJ 9eQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:github-pr-num :references:in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=/erOvSgWmZnQEI36XLHep2Cq8zOxwlTmXbEZ2g4znNE=; b=fi4KKw8JfVOqG2c7iGRxmL5prZjRkFdizD2gEVOjtm2IOH5+9ONXnq9V4M4CVI5XML JXG04DvbkbGQmtAH/3Jglsb/USLt40MUFbraE75QaBi+VigUokewL6ILekBAVoI2FfW5 6mDF8DWdABylnYW6F7wk+CRkSPgGTu0KYkTj5bXdlCLy9+nlGWo++qGEsSQ39hwWU1tR 8jNBgZQTikrbzV7eNPXzlV2jmtCDNucO+5jFHKdIuTnBTcd60N1U0oV4B3rsYWauCHmt PUZiwmU8ifa4tL1aIidl9lPx3VUdUdLyI78onljk+3HRa6QEcFhhYeFxDyJr8k28XAWR lFIg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id n66si6731158iof.171.2017.09.25.23.02.56; Mon, 25 Sep 2017 23:02:56 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: by lists.linaro.org (Postfix, from userid 109) id 257BE60D00; Tue, 26 Sep 2017 06:02:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW,URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 5D2F060CDE; Tue, 26 Sep 2017 06:00:42 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 45F8E60A59; Tue, 26 Sep 2017 06:00:21 +0000 (UTC) Received: from forward104o.mail.yandex.net (forward104o.mail.yandex.net [37.140.190.179]) by lists.linaro.org (Postfix) with ESMTPS id 0E71D60AEF for ; Tue, 26 Sep 2017 06:00:13 +0000 (UTC) Received: from mxback9g.mail.yandex.net (mxback9g.mail.yandex.net [IPv6:2a02:6b8:0:1472:2741:0:8b7:170]) by forward104o.mail.yandex.net (Yandex) with ESMTP id 08F3A701B73 for ; Tue, 26 Sep 2017 09:00:12 +0300 (MSK) Received: from smtp2p.mail.yandex.net (smtp2p.mail.yandex.net [2a02:6b8:0:1472:2741:0:8b6:7]) by mxback9g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id jkaZHW1sCh-0Bcqpgwg; Tue, 26 Sep 2017 09:00:11 +0300 Received: by smtp2p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id 72uMtCPv0h-0Bm8QKsb; Tue, 26 Sep 2017 09:00:11 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Github ODP bot To: lng-odp@lists.linaro.org Date: Tue, 26 Sep 2017 09:00:02 +0300 Message-Id: <1506405608-3507-4-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1506405608-3507-1-git-send-email-odpbot@yandex.ru> References: <1506405608-3507-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 196 Subject: [lng-odp] [PATCH CLOUD-DEV v1 3/9] linux-gen: pktio: socket: move common code away from socket pktios X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Bogdan Pricope Signed-off-by: Bogdan Pricope --- /** Email created from pull request 196 (bogdanPricope:cloud_dev_pktio_ops_data_pr) ** https://github.com/Linaro/odp/pull/196 ** Patch: https://github.com/Linaro/odp/pull/196.patch ** Base sha: e89a0ed9c4cd6d7dc947b978ad1dcabc6d5a21a2 ** Merge commit sha: 160ad8615ce66c98231aa400863c82e89045d491 **/ platform/linux-generic/Makefile.am | 2 + .../linux-generic/include/odp_pktio_ops_socket.h | 76 ----- platform/linux-generic/pktio/common.c | 368 +++++++++++++++++++++ platform/linux-generic/pktio/common.h | 81 +++++ platform/linux-generic/pktio/dpdk.c | 1 + platform/linux-generic/pktio/ethtool.c | 1 + platform/linux-generic/pktio/ethtool.h | 15 + platform/linux-generic/pktio/netmap.c | 2 + platform/linux-generic/pktio/socket.c | 354 +------------------- platform/linux-generic/pktio/socket_mmap.c | 2 + platform/linux-generic/pktio/tap.c | 1 + 11 files changed, 475 insertions(+), 428 deletions(-) create mode 100644 platform/linux-generic/pktio/common.h create mode 100644 platform/linux-generic/pktio/ethtool.h diff --git a/platform/linux-generic/Makefile.am b/platform/linux-generic/Makefile.am index 7d1066f38..6ede2d55b 100644 --- a/platform/linux-generic/Makefile.am +++ b/platform/linux-generic/Makefile.am @@ -186,6 +186,8 @@ noinst_HEADERS = \ ${srcdir}/include/odp_packet_internal.h \ ${srcdir}/include/odp_packet_io_internal.h \ ${srcdir}/include/odp_packet_io_ring_internal.h \ + ${srcdir}/pktio/ethtool.h \ + ${srcdir}/pktio/common.h \ ${srcdir}/pktio/dpdk.h \ ${srcdir}/include/odp_pktio_ops_ipc.h \ ${srcdir}/include/odp_pktio_ops_loopback.h \ diff --git a/platform/linux-generic/include/odp_pktio_ops_socket.h b/platform/linux-generic/include/odp_pktio_ops_socket.h index 32c49c08e..5ed444df7 100644 --- a/platform/linux-generic/include/odp_pktio_ops_socket.h +++ b/platform/linux-generic/include/odp_pktio_ops_socket.h @@ -93,80 +93,4 @@ ethaddrs_equal(unsigned char mac_a[], unsigned char mac_b[]) return !memcmp(mac_a, mac_b, ETH_ALEN); } -/** - * Read the MAC address from a packet socket - */ -int mac_addr_get_fd(int fd, const char *name, unsigned char mac_dst[]); - -/** - * Read the MTU from a packet socket - */ -uint32_t mtu_get_fd(int fd, const char *name); - -/** - * Enable/Disable promisc mode for a packet socket - */ -int promisc_mode_set_fd(int fd, const char *name, int enable); - -/** - * Return promisc mode of a packet socket - */ -int promisc_mode_get_fd(int fd, const char *name); - -/** - * Return link status of a packet socket (up/down) - */ -int link_status_fd(int fd, const char *name); - -/** - * Get enabled RSS hash protocols of a packet socket - * - * @param fd Socket file descriptor - * @param name Interface name - * @param hash_proto[out] Hash protocols - * - * @returns Number enabled hash protocols - */ -int rss_conf_get_fd(int fd, const char *name, - odp_pktin_hash_proto_t *hash_proto); - -/** - * Get supported RSS hash protocols of a packet socket - * - * Can be both read and modified. - * - * @param fd Socket file descriptor - * @param name Interface name - * @param hash_proto[out] Hash protocols - * - * @returns Number of supported hash protocols - */ -int rss_conf_get_supported_fd(int fd, const char *name, - odp_pktin_hash_proto_t *hash_proto); - -/** - * Set RSS hash protocols of a packet socket - * - * @param fd Socket file descriptor - * @param name Interface name - * @param hash_proto Hash protocols - * - * @retval 0 on success - * @retval <0 on failure - */ -int rss_conf_set_fd(int fd, const char *name, - const odp_pktin_hash_proto_t *proto); - -/** - * Print enabled RSS hash protocols - * - * @param hash_proto Hash protocols - */ -void rss_conf_print(const odp_pktin_hash_proto_t *hash_proto); - -/** - * Get ethtool statistics of a packet socket - */ -int ethtool_stats_get_fd(int fd, const char *name, odp_pktio_stats_t *stats); - #endif diff --git a/platform/linux-generic/pktio/common.c b/platform/linux-generic/pktio/common.c index 611bb451a..caefdc3a1 100644 --- a/platform/linux-generic/pktio/common.c +++ b/platform/linux-generic/pktio/common.c @@ -7,7 +7,18 @@ #include #include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include int sock_stats_reset_fd(pktio_entry_t *pktio_entry, int fd) { @@ -80,3 +91,360 @@ int sock_stats_fd(pktio_entry_t *pktio_entry, return ret; } + +/* + * ODP_PACKET_SOCKET_MMSG: + * ODP_PACKET_SOCKET_MMAP: + * ODP_PACKET_NETMAP: + */ +uint32_t mtu_get_fd(int fd, const char *name) +{ + struct ifreq ifr; + int ret; + + snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); + ret = ioctl(fd, SIOCGIFMTU, &ifr); + if (ret < 0) { + __odp_errno = errno; + ODP_DBG("ioctl(SIOCGIFMTU): %s: \"%s\".\n", strerror(errno), + ifr.ifr_name); + return 0; + } + return ifr.ifr_mtu; +} + +/** + * ODP_PACKET_SOCKET_MMSG: + * ODP_PACKET_SOCKET_MMAP: + * ODP_PACKET_NETMAP: + */ +int mac_addr_get_fd(int fd, const char *name, unsigned char mac_dst[]) +{ + struct ifreq ethreq; + int ret; + + memset(ðreq, 0, sizeof(ethreq)); + snprintf(ethreq.ifr_name, IF_NAMESIZE, "%s", name); + ret = ioctl(fd, SIOCGIFHWADDR, ðreq); + if (ret != 0) { + __odp_errno = errno; + ODP_ERR("ioctl(SIOCGIFHWADDR): %s: \"%s\".\n", strerror(errno), + ethreq.ifr_name); + return -1; + } + + memcpy(mac_dst, (unsigned char *)ethreq.ifr_ifru.ifru_hwaddr.sa_data, + ETH_ALEN); + return 0; +} + +/* + * ODP_PACKET_SOCKET_MMSG: + * ODP_PACKET_SOCKET_MMAP: + * ODP_PACKET_NETMAP: + */ +int promisc_mode_set_fd(int fd, const char *name, int enable) +{ + struct ifreq ifr; + int ret; + + snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); + ret = ioctl(fd, SIOCGIFFLAGS, &ifr); + if (ret < 0) { + __odp_errno = errno; + ODP_DBG("ioctl(SIOCGIFFLAGS): %s: \"%s\".\n", strerror(errno), + ifr.ifr_name); + return -1; + } + + if (enable) + ifr.ifr_flags |= IFF_PROMISC; + else + ifr.ifr_flags &= ~(IFF_PROMISC); + + ret = ioctl(fd, SIOCSIFFLAGS, &ifr); + if (ret < 0) { + __odp_errno = errno; + ODP_DBG("ioctl(SIOCSIFFLAGS): %s: \"%s\".\n", strerror(errno), + ifr.ifr_name); + return -1; + } + return 0; +} + +/* + * ODP_PACKET_SOCKET_MMSG: + * ODP_PACKET_SOCKET_MMAP: + * ODP_PACKET_NETMAP: + */ +int promisc_mode_get_fd(int fd, const char *name) +{ + struct ifreq ifr; + int ret; + + snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); + ret = ioctl(fd, SIOCGIFFLAGS, &ifr); + if (ret < 0) { + __odp_errno = errno; + ODP_DBG("ioctl(SIOCGIFFLAGS): %s: \"%s\".\n", strerror(errno), + ifr.ifr_name); + return -1; + } + + return !!(ifr.ifr_flags & IFF_PROMISC); +} + +/* + * ODP_PACKET_SOCKET_MMSG: + * ODP_PACKET_SOCKET_MMAP: + * ODP_PACKET_NETMAP: + */ +int link_status_fd(int fd, const char *name) +{ + struct ifreq ifr; + int ret; + + snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); + ret = ioctl(fd, SIOCGIFFLAGS, &ifr); + if (ret < 0) { + __odp_errno = errno; + ODP_DBG("ioctl(SIOCGIFFLAGS): %s: \"%s\".\n", strerror(errno), + ifr.ifr_name); + return -1; + } + + return !!(ifr.ifr_flags & IFF_RUNNING); +} + +/** + * Get enabled hash options of a packet socket + * + * @param fd Socket file descriptor + * @param name Interface name + * @param flow_type Packet flow type + * @param options[out] Enabled hash options + * + * @retval 0 on success + * @retval <0 on failure + */ +static inline int get_rss_hash_options(int fd, const char *name, + uint32_t flow_type, uint64_t *options) +{ + struct ifreq ifr; + struct ethtool_rxnfc rsscmd; + + memset(&ifr, 0, sizeof(ifr)); + memset(&rsscmd, 0, sizeof(rsscmd)); + *options = 0; + + snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); + + rsscmd.cmd = ETHTOOL_GRXFH; + rsscmd.flow_type = flow_type; + + ifr.ifr_data = &rsscmd; + + if (ioctl(fd, SIOCETHTOOL, &ifr) < 0) + return -1; + + *options = rsscmd.data; + return 0; +} + +int rss_conf_get_fd(int fd, const char *name, + odp_pktin_hash_proto_t *hash_proto) +{ + uint64_t options; + int rss_enabled = 0; + + memset(hash_proto, 0, sizeof(odp_pktin_hash_proto_t)); + + get_rss_hash_options(fd, name, IPV4_FLOW, &options); + if ((options & RXH_IP_SRC) && (options & RXH_IP_DST)) { + hash_proto->proto.ipv4 = 1; + rss_enabled++; + } + get_rss_hash_options(fd, name, TCP_V4_FLOW, &options); + if ((options & RXH_IP_SRC) && (options & RXH_IP_DST) && + (options & RXH_L4_B_0_1) && (options & RXH_L4_B_2_3)) { + hash_proto->proto.ipv4_tcp = 1; + rss_enabled++; + } + get_rss_hash_options(fd, name, UDP_V4_FLOW, &options); + if ((options & RXH_IP_SRC) && (options & RXH_IP_DST) && + (options & RXH_L4_B_0_1) && (options & RXH_L4_B_2_3)) { + hash_proto->proto.ipv4_udp = 1; + rss_enabled++; + } + get_rss_hash_options(fd, name, IPV6_FLOW, &options); + if ((options & RXH_IP_SRC) && (options & RXH_IP_DST)) { + hash_proto->proto.ipv6 = 1; + rss_enabled++; + } + get_rss_hash_options(fd, name, TCP_V6_FLOW, &options); + if ((options & RXH_IP_SRC) && (options & RXH_IP_DST) && + (options & RXH_L4_B_0_1) && (options & RXH_L4_B_2_3)) { + hash_proto->proto.ipv6_tcp = 1; + rss_enabled++; + } + get_rss_hash_options(fd, name, UDP_V6_FLOW, &options); + if ((options & RXH_IP_SRC) && (options & RXH_IP_DST) && + (options & RXH_L4_B_0_1) && (options & RXH_L4_B_2_3)) { + hash_proto->proto.ipv6_udp = 1; + rss_enabled++; + } + return rss_enabled; +} + +/** + * Set hash options of a packet socket + * + * @param fd Socket file descriptor + * @param name Interface name + * @param flow_type Packet flow type + * @param options Hash options + * + * @retval 0 on success + * @retval <0 on failure + */ +static inline int set_rss_hash(int fd, const char *name, + uint32_t flow_type, uint64_t options) +{ + struct ifreq ifr; + struct ethtool_rxnfc rsscmd; + + memset(&rsscmd, 0, sizeof(rsscmd)); + + snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); + + rsscmd.cmd = ETHTOOL_SRXFH; + rsscmd.flow_type = flow_type; + rsscmd.data = options; + + ifr.ifr_data = &rsscmd; + + if (ioctl(fd, SIOCETHTOOL, &ifr) < 0) + return -1; + + return 0; +} + +int rss_conf_set_fd(int fd, const char *name, + const odp_pktin_hash_proto_t *hash_proto) +{ + uint64_t options; + odp_pktin_hash_proto_t cur_hash; + + /* Compare to currently set hash protocols */ + rss_conf_get_fd(fd, name, &cur_hash); + + if (hash_proto->proto.ipv4_udp && !cur_hash.proto.ipv4_udp) { + options = RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3; + if (set_rss_hash(fd, name, UDP_V4_FLOW, options)) + return -1; + } + if (hash_proto->proto.ipv4_tcp && !cur_hash.proto.ipv4_tcp) { + options = RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3; + if (set_rss_hash(fd, name, TCP_V4_FLOW, options)) + return -1; + } + if (hash_proto->proto.ipv6_udp && !cur_hash.proto.ipv6_udp) { + options = RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3; + if (set_rss_hash(fd, name, UDP_V6_FLOW, options)) + return -1; + } + if (hash_proto->proto.ipv6_tcp && !cur_hash.proto.ipv6_tcp) { + options = RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3; + if (set_rss_hash(fd, name, TCP_V6_FLOW, options)) + return -1; + } + if (hash_proto->proto.ipv4 && !cur_hash.proto.ipv4) { + options = RXH_IP_SRC | RXH_IP_DST; + if (set_rss_hash(fd, name, IPV4_FLOW, options)) + return -1; + } + if (hash_proto->proto.ipv6 && !cur_hash.proto.ipv6) { + options = RXH_IP_SRC | RXH_IP_DST; + if (set_rss_hash(fd, name, IPV6_FLOW, options)) + return -1; + } + return 0; +} + +int rss_conf_get_supported_fd(int fd, const char *name, + odp_pktin_hash_proto_t *hash_proto) +{ + uint64_t options; + int rss_supported = 0; + + memset(hash_proto, 0, sizeof(odp_pktin_hash_proto_t)); + + if (!get_rss_hash_options(fd, name, IPV4_FLOW, &options)) { + if (!set_rss_hash(fd, name, IPV4_FLOW, options)) { + hash_proto->proto.ipv4 = 1; + rss_supported++; + } + } + if (!get_rss_hash_options(fd, name, TCP_V4_FLOW, &options)) { + if (!set_rss_hash(fd, name, TCP_V4_FLOW, options)) { + hash_proto->proto.ipv4_tcp = 1; + rss_supported++; + } + } + if (!get_rss_hash_options(fd, name, UDP_V4_FLOW, &options)) { + if (!set_rss_hash(fd, name, UDP_V4_FLOW, options)) { + hash_proto->proto.ipv4_udp = 1; + rss_supported++; + } + } + if (!get_rss_hash_options(fd, name, IPV6_FLOW, &options)) { + if (!set_rss_hash(fd, name, IPV6_FLOW, options)) { + hash_proto->proto.ipv6 = 1; + rss_supported++; + } + } + if (!get_rss_hash_options(fd, name, TCP_V6_FLOW, &options)) { + if (!set_rss_hash(fd, name, TCP_V6_FLOW, options)) { + hash_proto->proto.ipv6_tcp = 1; + rss_supported++; + } + } + if (!get_rss_hash_options(fd, name, UDP_V6_FLOW, &options)) { + if (!set_rss_hash(fd, name, UDP_V6_FLOW, options)) { + hash_proto->proto.ipv6_udp = 1; + rss_supported++; + } + } + return rss_supported; +} + +void rss_conf_print(const odp_pktin_hash_proto_t *hash_proto) +{ int max_len = 512; + char str[max_len]; + int len = 0; + int n = max_len - 1; + + len += snprintf(&str[len], n - len, " rss conf\n"); + + if (hash_proto->proto.ipv4) + len += snprintf(&str[len], n - len, + " IPV4\n"); + if (hash_proto->proto.ipv4_tcp) + len += snprintf(&str[len], n - len, + " IPV4 TCP\n"); + if (hash_proto->proto.ipv4_udp) + len += snprintf(&str[len], n - len, + " IPV4 UDP\n"); + if (hash_proto->proto.ipv6) + len += snprintf(&str[len], n - len, + " IPV6\n"); + if (hash_proto->proto.ipv6_tcp) + len += snprintf(&str[len], n - len, + " IPV6 TCP\n"); + if (hash_proto->proto.ipv6_udp) + len += snprintf(&str[len], n - len, + " IPV6 UDP\n"); + str[len] = '\0'; + + ODP_PRINT("%s\n", str); +} diff --git a/platform/linux-generic/pktio/common.h b/platform/linux-generic/pktio/common.h new file mode 100644 index 000000000..2940af143 --- /dev/null +++ b/platform/linux-generic/pktio/common.h @@ -0,0 +1,81 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef ODP_PKTIO_COMMON_H_ +#define ODP_PKTIO_COMMON_H_ + +/** + * Read the MTU from a packet socket + */ +uint32_t mtu_get_fd(int fd, const char *name); + +/** + * Read the MAC address from a packet socket + */ +int mac_addr_get_fd(int fd, const char *name, unsigned char mac_dst[]); + +/** + * Enable/Disable promisc mode for a packet socket + */ +int promisc_mode_set_fd(int fd, const char *name, int enable); + +/** + * Return promisc mode of a packet socket + */ +int promisc_mode_get_fd(int fd, const char *name); + +/** + * Return link status of a packet socket (up/down) + */ +int link_status_fd(int fd, const char *name); + +/** + * Get enabled RSS hash protocols of a packet socket + * + * @param fd Socket file descriptor + * @param name Interface name + * @param hash_proto[out] Hash protocols + * + * @returns Number enabled hash protocols + */ +int rss_conf_get_fd(int fd, const char *name, + odp_pktin_hash_proto_t *hash_proto); + +/** + * Get supported RSS hash protocols of a packet socket + * + * Can be both read and modified. + * + * @param fd Socket file descriptor + * @param name Interface name + * @param hash_proto[out] Hash protocols + * + * @returns Number of supported hash protocols + */ +int rss_conf_get_supported_fd(int fd, const char *name, + odp_pktin_hash_proto_t *hash_proto); + +/** + * Set RSS hash protocols of a packet socket + * + * @param fd Socket file descriptor + * @param name Interface name + * @param hash_proto Hash protocols + * + * @retval 0 on success + * @retval <0 on failure + */ +int rss_conf_set_fd(int fd, const char *name, + const odp_pktin_hash_proto_t *proto); + +/** + * Print enabled RSS hash protocols + * + * @param hash_proto Hash protocols + */ +void rss_conf_print(const odp_pktin_hash_proto_t *hash_proto); + +#endif /*ODP_PKTIO_COMMON_H_*/ diff --git a/platform/linux-generic/pktio/dpdk.c b/platform/linux-generic/pktio/dpdk.c index 511c77783..21c9fb0f7 100644 --- a/platform/linux-generic/pktio/dpdk.c +++ b/platform/linux-generic/pktio/dpdk.c @@ -22,6 +22,7 @@ #include #include +#include #include #include diff --git a/platform/linux-generic/pktio/ethtool.c b/platform/linux-generic/pktio/ethtool.c index ed2875c5b..3659004be 100644 --- a/platform/linux-generic/pktio/ethtool.c +++ b/platform/linux-generic/pktio/ethtool.c @@ -15,6 +15,7 @@ #include #include #include +#include static struct ethtool_gstrings *get_stringset(int fd, struct ifreq *ifr) { diff --git a/platform/linux-generic/pktio/ethtool.h b/platform/linux-generic/pktio/ethtool.h new file mode 100644 index 000000000..c5a811238 --- /dev/null +++ b/platform/linux-generic/pktio/ethtool.h @@ -0,0 +1,15 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef ODP_PKTIO_ETHTOOL_H_ +#define ODP_PKTIO_ETHTOOL_H_ + +/** + * Get ethtool statistics of a packet socket + */ +int ethtool_stats_get_fd(int fd, const char *name, odp_pktio_stats_t *stats); + +#endif /*ODP_PKTIO_ETHTOOL_H_*/ diff --git a/platform/linux-generic/pktio/netmap.c b/platform/linux-generic/pktio/netmap.c index 07d6d9e9b..bef3ae3a3 100644 --- a/platform/linux-generic/pktio/netmap.c +++ b/platform/linux-generic/pktio/netmap.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include diff --git a/platform/linux-generic/pktio/socket.c b/platform/linux-generic/pktio/socket.c index c7c3c5a8c..f4939ce13 100644 --- a/platform/linux-generic/pktio/socket.c +++ b/platform/linux-generic/pktio/socket.c @@ -41,6 +41,8 @@ #include #include #include +#include +#include #include #include @@ -97,358 +99,6 @@ int sendmmsg(int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags) #define ETHBUF_ALIGN(buf_ptr) ((uint8_t *)ODP_ALIGN_ROUNDUP_PTR((buf_ptr), \ sizeof(uint32_t)) + ETHBUF_OFFSET) -/** - * ODP_PACKET_SOCKET_MMSG: - * ODP_PACKET_SOCKET_MMAP: - * ODP_PACKET_NETMAP: - */ -int mac_addr_get_fd(int fd, const char *name, unsigned char mac_dst[]) -{ - struct ifreq ethreq; - int ret; - - memset(ðreq, 0, sizeof(ethreq)); - snprintf(ethreq.ifr_name, IF_NAMESIZE, "%s", name); - ret = ioctl(fd, SIOCGIFHWADDR, ðreq); - if (ret != 0) { - __odp_errno = errno; - ODP_ERR("ioctl(SIOCGIFHWADDR): %s: \"%s\".\n", strerror(errno), - ethreq.ifr_name); - return -1; - } - - memcpy(mac_dst, (unsigned char *)ethreq.ifr_ifru.ifru_hwaddr.sa_data, - ETH_ALEN); - return 0; -} - -/* - * ODP_PACKET_SOCKET_MMSG: - * ODP_PACKET_SOCKET_MMAP: - * ODP_PACKET_NETMAP: - */ -uint32_t mtu_get_fd(int fd, const char *name) -{ - struct ifreq ifr; - int ret; - - snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); - ret = ioctl(fd, SIOCGIFMTU, &ifr); - if (ret < 0) { - __odp_errno = errno; - ODP_DBG("ioctl(SIOCGIFMTU): %s: \"%s\".\n", strerror(errno), - ifr.ifr_name); - return 0; - } - return ifr.ifr_mtu; -} - -/* - * ODP_PACKET_SOCKET_MMSG: - * ODP_PACKET_SOCKET_MMAP: - * ODP_PACKET_NETMAP: - */ -int promisc_mode_set_fd(int fd, const char *name, int enable) -{ - struct ifreq ifr; - int ret; - - snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); - ret = ioctl(fd, SIOCGIFFLAGS, &ifr); - if (ret < 0) { - __odp_errno = errno; - ODP_DBG("ioctl(SIOCGIFFLAGS): %s: \"%s\".\n", strerror(errno), - ifr.ifr_name); - return -1; - } - - if (enable) - ifr.ifr_flags |= IFF_PROMISC; - else - ifr.ifr_flags &= ~(IFF_PROMISC); - - ret = ioctl(fd, SIOCSIFFLAGS, &ifr); - if (ret < 0) { - __odp_errno = errno; - ODP_DBG("ioctl(SIOCSIFFLAGS): %s: \"%s\".\n", strerror(errno), - ifr.ifr_name); - return -1; - } - return 0; -} - -/* - * ODP_PACKET_SOCKET_MMSG: - * ODP_PACKET_SOCKET_MMAP: - * ODP_PACKET_NETMAP: - */ -int promisc_mode_get_fd(int fd, const char *name) -{ - struct ifreq ifr; - int ret; - - snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); - ret = ioctl(fd, SIOCGIFFLAGS, &ifr); - if (ret < 0) { - __odp_errno = errno; - ODP_DBG("ioctl(SIOCGIFFLAGS): %s: \"%s\".\n", strerror(errno), - ifr.ifr_name); - return -1; - } - - return !!(ifr.ifr_flags & IFF_PROMISC); -} - -int link_status_fd(int fd, const char *name) -{ - struct ifreq ifr; - int ret; - - snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); - ret = ioctl(fd, SIOCGIFFLAGS, &ifr); - if (ret < 0) { - __odp_errno = errno; - ODP_DBG("ioctl(SIOCGIFFLAGS): %s: \"%s\".\n", strerror(errno), - ifr.ifr_name); - return -1; - } - - return !!(ifr.ifr_flags & IFF_RUNNING); -} - -/** - * Get enabled hash options of a packet socket - * - * @param fd Socket file descriptor - * @param name Interface name - * @param flow_type Packet flow type - * @param options[out] Enabled hash options - * - * @retval 0 on success - * @retval <0 on failure - */ -static inline int get_rss_hash_options(int fd, const char *name, - uint32_t flow_type, uint64_t *options) -{ - struct ifreq ifr; - struct ethtool_rxnfc rsscmd; - - memset(&ifr, 0, sizeof(ifr)); - memset(&rsscmd, 0, sizeof(rsscmd)); - *options = 0; - - snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); - - rsscmd.cmd = ETHTOOL_GRXFH; - rsscmd.flow_type = flow_type; - - ifr.ifr_data = (caddr_t)&rsscmd; - - if (ioctl(fd, SIOCETHTOOL, &ifr) < 0) - return -1; - - *options = rsscmd.data; - return 0; -} - -int rss_conf_get_fd(int fd, const char *name, - odp_pktin_hash_proto_t *hash_proto) -{ - uint64_t options; - int rss_enabled = 0; - - memset(hash_proto, 0, sizeof(odp_pktin_hash_proto_t)); - - get_rss_hash_options(fd, name, IPV4_FLOW, &options); - if ((options & RXH_IP_SRC) && (options & RXH_IP_DST)) { - hash_proto->proto.ipv4 = 1; - rss_enabled++; - } - get_rss_hash_options(fd, name, TCP_V4_FLOW, &options); - if ((options & RXH_IP_SRC) && (options & RXH_IP_DST) && - (options & RXH_L4_B_0_1) && (options & RXH_L4_B_2_3)) { - hash_proto->proto.ipv4_tcp = 1; - rss_enabled++; - } - get_rss_hash_options(fd, name, UDP_V4_FLOW, &options); - if ((options & RXH_IP_SRC) && (options & RXH_IP_DST) && - (options & RXH_L4_B_0_1) && (options & RXH_L4_B_2_3)) { - hash_proto->proto.ipv4_udp = 1; - rss_enabled++; - } - get_rss_hash_options(fd, name, IPV6_FLOW, &options); - if ((options & RXH_IP_SRC) && (options & RXH_IP_DST)) { - hash_proto->proto.ipv6 = 1; - rss_enabled++; - } - get_rss_hash_options(fd, name, TCP_V6_FLOW, &options); - if ((options & RXH_IP_SRC) && (options & RXH_IP_DST) && - (options & RXH_L4_B_0_1) && (options & RXH_L4_B_2_3)) { - hash_proto->proto.ipv6_tcp = 1; - rss_enabled++; - } - get_rss_hash_options(fd, name, UDP_V6_FLOW, &options); - if ((options & RXH_IP_SRC) && (options & RXH_IP_DST) && - (options & RXH_L4_B_0_1) && (options & RXH_L4_B_2_3)) { - hash_proto->proto.ipv6_udp = 1; - rss_enabled++; - } - return rss_enabled; -} - -/** - * Set hash options of a packet socket - * - * @param fd Socket file descriptor - * @param name Interface name - * @param flow_type Packet flow type - * @param options Hash options - * - * @retval 0 on success - * @retval <0 on failure - */ -static inline int set_rss_hash(int fd, const char *name, - uint32_t flow_type, uint64_t options) -{ - struct ifreq ifr; - struct ethtool_rxnfc rsscmd; - - memset(&rsscmd, 0, sizeof(rsscmd)); - - snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", name); - - rsscmd.cmd = ETHTOOL_SRXFH; - rsscmd.flow_type = flow_type; - rsscmd.data = options; - - ifr.ifr_data = (caddr_t)&rsscmd; - - if (ioctl(fd, SIOCETHTOOL, &ifr) < 0) - return -1; - - return 0; -} - -int rss_conf_set_fd(int fd, const char *name, - const odp_pktin_hash_proto_t *hash_proto) -{ - uint64_t options; - odp_pktin_hash_proto_t cur_hash; - - /* Compare to currently set hash protocols */ - rss_conf_get_fd(fd, name, &cur_hash); - - if (hash_proto->proto.ipv4_udp && !cur_hash.proto.ipv4_udp) { - options = RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3; - if (set_rss_hash(fd, name, UDP_V4_FLOW, options)) - return -1; - } - if (hash_proto->proto.ipv4_tcp && !cur_hash.proto.ipv4_tcp) { - options = RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3; - if (set_rss_hash(fd, name, TCP_V4_FLOW, options)) - return -1; - } - if (hash_proto->proto.ipv6_udp && !cur_hash.proto.ipv6_udp) { - options = RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3; - if (set_rss_hash(fd, name, UDP_V6_FLOW, options)) - return -1; - } - if (hash_proto->proto.ipv6_tcp && !cur_hash.proto.ipv6_tcp) { - options = RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3; - if (set_rss_hash(fd, name, TCP_V6_FLOW, options)) - return -1; - } - if (hash_proto->proto.ipv4 && !cur_hash.proto.ipv4) { - options = RXH_IP_SRC | RXH_IP_DST; - if (set_rss_hash(fd, name, IPV4_FLOW, options)) - return -1; - } - if (hash_proto->proto.ipv6 && !cur_hash.proto.ipv6) { - options = RXH_IP_SRC | RXH_IP_DST; - if (set_rss_hash(fd, name, IPV6_FLOW, options)) - return -1; - } - return 0; -} - -int rss_conf_get_supported_fd(int fd, const char *name, - odp_pktin_hash_proto_t *hash_proto) -{ - uint64_t options; - int rss_supported = 0; - - memset(hash_proto, 0, sizeof(odp_pktin_hash_proto_t)); - - if (!get_rss_hash_options(fd, name, IPV4_FLOW, &options)) { - if (!set_rss_hash(fd, name, IPV4_FLOW, options)) { - hash_proto->proto.ipv4 = 1; - rss_supported++; - } - } - if (!get_rss_hash_options(fd, name, TCP_V4_FLOW, &options)) { - if (!set_rss_hash(fd, name, TCP_V4_FLOW, options)) { - hash_proto->proto.ipv4_tcp = 1; - rss_supported++; - } - } - if (!get_rss_hash_options(fd, name, UDP_V4_FLOW, &options)) { - if (!set_rss_hash(fd, name, UDP_V4_FLOW, options)) { - hash_proto->proto.ipv4_udp = 1; - rss_supported++; - } - } - if (!get_rss_hash_options(fd, name, IPV6_FLOW, &options)) { - if (!set_rss_hash(fd, name, IPV6_FLOW, options)) { - hash_proto->proto.ipv6 = 1; - rss_supported++; - } - } - if (!get_rss_hash_options(fd, name, TCP_V6_FLOW, &options)) { - if (!set_rss_hash(fd, name, TCP_V6_FLOW, options)) { - hash_proto->proto.ipv6_tcp = 1; - rss_supported++; - } - } - if (!get_rss_hash_options(fd, name, UDP_V6_FLOW, &options)) { - if (!set_rss_hash(fd, name, UDP_V6_FLOW, options)) { - hash_proto->proto.ipv6_udp = 1; - rss_supported++; - } - } - return rss_supported; -} - -void rss_conf_print(const odp_pktin_hash_proto_t *hash_proto) -{ int max_len = 512; - char str[max_len]; - int len = 0; - int n = max_len - 1; - - len += snprintf(&str[len], n - len, " rss conf\n"); - - if (hash_proto->proto.ipv4) - len += snprintf(&str[len], n - len, - " IPV4\n"); - if (hash_proto->proto.ipv4_tcp) - len += snprintf(&str[len], n - len, - " IPV4 TCP\n"); - if (hash_proto->proto.ipv4_udp) - len += snprintf(&str[len], n - len, - " IPV4 UDP\n"); - if (hash_proto->proto.ipv6) - len += snprintf(&str[len], n - len, - " IPV6\n"); - if (hash_proto->proto.ipv6_tcp) - len += snprintf(&str[len], n - len, - " IPV6 TCP\n"); - if (hash_proto->proto.ipv6_udp) - len += snprintf(&str[len], n - len, - " IPV6 UDP\n"); - str[len] = '\0'; - - ODP_PRINT("%s\n", str); -} - /* * ODP_PACKET_SOCKET_MMSG: */ diff --git a/platform/linux-generic/pktio/socket_mmap.c b/platform/linux-generic/pktio/socket_mmap.c index 3dd03e66b..2bb51c4bb 100644 --- a/platform/linux-generic/pktio/socket_mmap.c +++ b/platform/linux-generic/pktio/socket_mmap.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include diff --git a/platform/linux-generic/pktio/tap.c b/platform/linux-generic/pktio/tap.c index 090e51b66..e8823850c 100644 --- a/platform/linux-generic/pktio/tap.c +++ b/platform/linux-generic/pktio/tap.c @@ -43,6 +43,7 @@ #include #include #include +#include #define BUF_SIZE 65536