DPDK patches and discussions
 help / color / mirror / Atom feed
From: Pascal Mazon <pascal.mazon@6wind.com>
To: keith.wiles@intel.com
Cc: dev@dpdk.org, Pascal Mazon <pascal.mazon@6wind.com>
Subject: [dpdk-dev] [PATCH v2 3/4] net/tap: use the remote MAC address if available
Date: Tue,  7 Mar 2017 17:38:53 +0100	[thread overview]
Message-ID: <1aefeb7b85a62236b650c0baf11a261ae9b7c3bd.1488904606.git.pascal.mazon@6wind.com> (raw)
In-Reply-To: <cover.1488904606.git.pascal.mazon@6wind.com>

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 <pascal.mazon@6wind.com>
Acked-by: Olga Shern <olgas@mellanox.com>
---
 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 ded8bb0f18d9..ed71cddfe0b3 100644
--- a/drivers/net/tap/rte_eth_tap.c
+++ b/drivers/net/tap/rte_eth_tap.c
@@ -529,6 +529,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)
@@ -545,7 +604,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,
@@ -565,19 +623,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
@@ -977,6 +1027,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;
-- 
2.8.0.rc0

  parent reply	other threads:[~2017-03-07 16:39 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-03 12:27 [dpdk-dev] [PATCH 0/4] net/tap: remote netdevice traffic capture Pascal Mazon
2017-03-03 12:27 ` [dpdk-dev] [PATCH 1/4] net/tap: add " Pascal Mazon
2017-03-03 12:27 ` [dpdk-dev] [PATCH 2/4] net/tap: reflect tap flags on the remote Pascal Mazon
2017-03-03 12:27 ` [dpdk-dev] [PATCH 3/4] net/tap: use the remote MAC address if available Pascal Mazon
2017-03-03 12:27 ` [dpdk-dev] [PATCH 4/4] net/tap: set MTU on the remote Pascal Mazon
2017-03-03 15:54 ` [dpdk-dev] [PATCH 0/4] net/tap: remote netdevice traffic capture Wiles, Keith
2017-03-07 16:38 ` [dpdk-dev] [PATCH v2 0/4] " Pascal Mazon
2017-03-07 16:38   ` [dpdk-dev] [PATCH v2 1/4] net/tap: add " Pascal Mazon
2017-03-07 16:38   ` [dpdk-dev] [PATCH v2 2/4] net/tap: reflect tap flags on the remote Pascal Mazon
2017-03-07 16:38   ` Pascal Mazon [this message]
2017-03-07 16:38   ` [dpdk-dev] [PATCH v2 4/4] net/tap: set MTU " Pascal Mazon
2017-03-08 10:06   ` [dpdk-dev] [PATCH v3 0/4] net/tap: remote netdevice traffic capture Pascal Mazon
2017-03-08 10:06     ` [dpdk-dev] [PATCH v3 1/4] net/tap: add " Pascal Mazon
2017-03-08 10:06     ` [dpdk-dev] [PATCH v3 2/4] net/tap: reflect tap flags on the remote Pascal Mazon
2017-03-08 10:06     ` [dpdk-dev] [PATCH v3 3/4] net/tap: use the remote MAC address if available Pascal Mazon
2017-03-08 10:06     ` [dpdk-dev] [PATCH v3 4/4] net/tap: set MTU on the remote Pascal Mazon
2017-03-14  8:34     ` [dpdk-dev] [PATCH v4] net/tap: remote netdevice traffic capture Pascal Mazon
2017-03-14  8:34       ` [dpdk-dev] [PATCH v4] net/tap: add " Pascal Mazon
2017-03-14 14:00         ` Wiles, Keith
2017-03-15 15:03     ` [dpdk-dev] [PATCH v5] net/tap: " Pascal Mazon
2017-03-15 15:03       ` [dpdk-dev] [PATCH v5] net/tap: add " Pascal Mazon
2017-03-23  8:42       ` [dpdk-dev] [PATCH v6 0/1] net/tap: " Pascal Mazon
2017-03-23  8:42         ` [dpdk-dev] [PATCH v6 1/1] net/tap: add " Pascal Mazon
2017-03-24 15:48           ` Ferruh Yigit

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1aefeb7b85a62236b650c0baf11a261ae9b7c3bd.1488904606.git.pascal.mazon@6wind.com \
    --to=pascal.mazon@6wind.com \
    --cc=dev@dpdk.org \
    --cc=keith.wiles@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).