From patchwork Fri Mar 3 12:27:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pascal Mazon X-Patchwork-Id: 21293 X-Patchwork-Delegate: ferruh.yigit@amd.com Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 16A5EF932; Fri, 3 Mar 2017 13:29:04 +0100 (CET) Received: from proxy.6wind.com (host.76.145.23.62.rev.coltfrance.com [62.23.145.76]) by dpdk.org (Postfix) with ESMTP id AFEA4591E for ; Fri, 3 Mar 2017 13:28:35 +0100 (CET) Received: from 6wind.com (unknown [10.16.0.184]) by proxy.6wind.com (Postfix) with SMTP id 0F5A024D74; Fri, 3 Mar 2017 13:28:30 +0100 (CET) Received: by 6wind.com (sSMTP sendmail emulation); Fri, 03 Mar 2017 13:28:28 +0100 From: Pascal Mazon To: keith.wiles@intel.com Cc: dev@dpdk.org, Pascal Mazon Date: Fri, 3 Mar 2017 13:27:40 +0100 Message-Id: X-Mailer: git-send-email 2.8.0.rc0 In-Reply-To: References: Subject: [dpdk-dev] [PATCH 3/4] net/tap: use the remote MAC address if available 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: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The remote on a tap is most likely used with netdevices that are not under DPDK control. Outgoing traffic is supposed to use the source MAC address of the remote netdevice. This commit synchronizes the MAC address of the local tap netdevice with the remote one. Of course, it is still possible to change the tap MAC address, using standard DPDK APIs. It sets that MAC address on the tap PMD and redirect any packets matching that destination MAC to the tap PMD. It also tries setting the MAC address directly on the remote, if supported, through ioctl() calls. Signed-off-by: Pascal Mazon Acked-by: Olga Shern --- drivers/net/tap/rte_eth_tap.c | 77 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/drivers/net/tap/rte_eth_tap.c b/drivers/net/tap/rte_eth_tap.c index c77f206b4f60..98b466aba223 100644 --- a/drivers/net/tap/rte_eth_tap.c +++ b/drivers/net/tap/rte_eth_tap.c @@ -523,6 +523,65 @@ tap_allmulti_disable(struct rte_eth_dev *dev) } } +static int +tap_netdev_set_mac(const char *iface, struct ether_addr *mac_addr) +{ + struct ifreq ifr; + int err, s; + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s < 0) { + RTE_LOG(ERR, PMD, + "Unable to get a socket to get MAC: %s\n", + strerror(errno)); + return -1; + } + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, iface, IFNAMSIZ); + err = ioctl(s, SIOCGIFHWADDR, &ifr); + if (err < 0) { + RTE_LOG(ERR, PMD, "%s: couldn't get current MAC address (%s)\n", + iface, strerror(errno)); + close(s); + return -1; + } + rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETHER_ADDR_LEN); + err = ioctl(s, SIOCSIFHWADDR, &ifr) == -1; + close(s); + if (err < 0) { + RTE_LOG(ERR, PMD, "%s: couldn't set current MAC address (%s)\n", + iface, strerror(errno)); + return -1; + } + return 0; +} + +static int +tap_netdev_get_mac(const char *iface, struct ether_addr *mac_addr) +{ + struct ifreq ifr; + int err, s; + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s < 0) { + RTE_LOG(ERR, PMD, + "Unable to get a socket to get MAC: %s\n", + strerror(errno)); + return -1; + } + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, iface, IFNAMSIZ); + err = ioctl(s, SIOCGIFHWADDR, &ifr); + close(s); + if (err < 0) { + RTE_LOG(ERR, PMD, "%s: couldn't get MAC address (%s)\n", + iface, strerror(errno)); + return -1; + } + rte_memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN); + return 0; +} + static void tap_mac_remove(struct rte_eth_dev *dev __rte_unused, uint32_t index __rte_unused) @@ -539,7 +598,6 @@ tap_mac_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr, { struct pmd_internals *internals = dev->data->dev_private; int fd = internals->rxq[0].fd; - struct ifreq ifr; if (index > RTE_PMD_TAP_MAX_MAC_ADDRS - 1) { RTE_LOG(ERR, PMD, @@ -559,19 +617,11 @@ tap_mac_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr, dev->data->name); return; } - memset(&ifr, 0, sizeof(struct ifreq)); - if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) { - RTE_LOG(ERR, PMD, "%s: couldn't get current MAC address (%s)\n", - dev->data->name, strerror(errno)); + if (tap_netdev_set_mac(internals->name, mac_addr) < 0) return; - } - rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETHER_ADDR_LEN); - if (ioctl(fd, SIOCSIFHWADDR, &ifr) == -1) { - RTE_LOG(ERR, PMD, "%s: couldn't set current MAC address (%s)\n", - dev->data->name, strerror(errno)); - return; - } rte_memcpy(&dev->data->mac_addrs[index], mac_addr, ETHER_ADDR_LEN); + if (internals->remote_if_index) + tap_netdev_set_mac(internals->remote_iface, mac_addr); } static void @@ -939,6 +989,9 @@ eth_dev_tap_create(const char *name, char *tap_name, char *remote_iface) RTE_LOG(ERR, PMD, "Could not find %s ifindex: " "remote interface will remain unconfigured\n", remote_iface); + else + /* Set the local mac address to the remote mac */ + tap_netdev_get_mac(remote_iface, &pmd->eth_addr); } return 0;