[dpdk-dev,v3,1/6] net/tap: add MAC address management ops

Message ID 1488904298-31395-2-git-send-email-pascal.mazon@6wind.com (mailing list archive)
State Superseded, archived
Delegated to: Ferruh Yigit
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Pascal Mazon March 7, 2017, 4:31 p.m. UTC
  Set a random MAC address when probing the device, as to not leave an
empty MAC in pmd->eth_addr.

This MAC will be set on the tap netdevice as soon as it's been created
using tun_alloc(). As the tap_mac_add() function depend on the fd in
the first rxq, move code from tun_alloc() to tap_setup_queue(),
after it's been set.

Signed-off-by: Pascal Mazon <pascal.mazon@6wind.com>
---
 doc/guides/nics/features/tap.ini |  1 +
 drivers/net/tap/rte_eth_tap.c    | 97 ++++++++++++++++++++++++++++++++++------
 2 files changed, 85 insertions(+), 13 deletions(-)
  

Comments

Ferruh Yigit March 9, 2017, 2:05 p.m. UTC | #1
On 3/7/2017 4:31 PM, Pascal Mazon wrote:
> Set a random MAC address when probing the device, as to not leave an
> empty MAC in pmd->eth_addr.
> 
> This MAC will be set on the tap netdevice as soon as it's been created
> using tun_alloc(). As the tap_mac_add() function depend on the fd in
> the first rxq, move code from tun_alloc() to tap_setup_queue(),
> after it's been set.
> 
> Signed-off-by: Pascal Mazon <pascal.mazon@6wind.com>
> ---
>  doc/guides/nics/features/tap.ini |  1 +
>  drivers/net/tap/rte_eth_tap.c    | 97 ++++++++++++++++++++++++++++++++++------
>  2 files changed, 85 insertions(+), 13 deletions(-)
> 
> diff --git a/doc/guides/nics/features/tap.ini b/doc/guides/nics/features/tap.ini
> index f4aca6921ddc..d9b47a003654 100644
> --- a/doc/guides/nics/features/tap.ini
> +++ b/doc/guides/nics/features/tap.ini
> @@ -9,6 +9,7 @@ Jumbo frame          = Y
>  Promiscuous mode     = Y
>  Allmulticast mode    = Y
>  Basic stats          = Y
> +Unicast MAC filter   = Y
>  Other kdrv           = Y
>  ARMv7                = Y
>  ARMv8                = Y
> diff --git a/drivers/net/tap/rte_eth_tap.c b/drivers/net/tap/rte_eth_tap.c
> index ece3a5fcc897..1e46ee36efa2 100644
> --- a/drivers/net/tap/rte_eth_tap.c
> +++ b/drivers/net/tap/rte_eth_tap.c
> @@ -63,6 +63,8 @@
>  #define RTE_PMD_TAP_MAX_QUEUES	1
>  #endif
>  
> +#define RTE_PMD_TAP_MAX_MAC_ADDRS 1

mac_addr_add and mac_addr_remove not really supported, because only one
MAC is supported. For mac_addr_add() all indexes other than 0 will give
an error. So only mac_addr_set is supported.

For this case what is the benefit of implementing these functions and
claim support, instead of just leaving mac_addr_add and mac_addr_remove
NULL?


<...>

> +	if (qid == 0) {
> +		/*
> +		 * tap_setup_queue() is called for both tx and rx.
> +		 * Let's use dev->data->r/tx_queues[qid] to determine if init
> +		 * has already been done.
> +		 */
> +		if (dev->data->rx_queues[qid] && dev->data->tx_queues[qid])
> +			return fd;
> +
> +		tap_mac_set(dev, &pmd->eth_addr);

What is the reason of changing behavior here?

Tap devices assigned random MAC by kernel, and previous implementation
was reading that value and using it for DPDK.

Now kernel assigns a random MAC, and DPDK overwrites it another random
MAC, previous implementation was simpler I think.

It is OK to move this code tap_setup_queue(), I just missed the benefit
of overwriting with DPDK random MAC?

<...>
  
Pascal Mazon March 10, 2017, 9:01 a.m. UTC | #2
On Thu, 9 Mar 2017 14:05:51 +0000
Ferruh Yigit <ferruh.yigit@intel.com> wrote:

> On 3/7/2017 4:31 PM, Pascal Mazon wrote:
> > Set a random MAC address when probing the device, as to not leave an
> > empty MAC in pmd->eth_addr.
> > 
> > This MAC will be set on the tap netdevice as soon as it's been
> > created using tun_alloc(). As the tap_mac_add() function depend on
> > the fd in the first rxq, move code from tun_alloc() to
> > tap_setup_queue(), after it's been set.
> > 
> > Signed-off-by: Pascal Mazon <pascal.mazon@6wind.com>
> > ---
> >  doc/guides/nics/features/tap.ini |  1 +
> >  drivers/net/tap/rte_eth_tap.c    | 97
> > ++++++++++++++++++++++++++++++++++------ 2 files changed, 85
> > insertions(+), 13 deletions(-)
> > 
> > diff --git a/doc/guides/nics/features/tap.ini
> > b/doc/guides/nics/features/tap.ini index f4aca6921ddc..d9b47a003654
> > 100644 --- a/doc/guides/nics/features/tap.ini
> > +++ b/doc/guides/nics/features/tap.ini
> > @@ -9,6 +9,7 @@ Jumbo frame          = Y
> >  Promiscuous mode     = Y
> >  Allmulticast mode    = Y
> >  Basic stats          = Y
> > +Unicast MAC filter   = Y
> >  Other kdrv           = Y
> >  ARMv7                = Y
> >  ARMv8                = Y
> > diff --git a/drivers/net/tap/rte_eth_tap.c
> > b/drivers/net/tap/rte_eth_tap.c index ece3a5fcc897..1e46ee36efa2
> > 100644 --- a/drivers/net/tap/rte_eth_tap.c
> > +++ b/drivers/net/tap/rte_eth_tap.c
> > @@ -63,6 +63,8 @@
> >  #define RTE_PMD_TAP_MAX_QUEUES	1
> >  #endif
> >  
> > +#define RTE_PMD_TAP_MAX_MAC_ADDRS 1
> 
> mac_addr_add and mac_addr_remove not really supported, because only
> one MAC is supported. For mac_addr_add() all indexes other than 0
> will give an error. So only mac_addr_set is supported.
> 
> For this case what is the benefit of implementing these functions and
> claim support, instead of just leaving mac_addr_add and
> mac_addr_remove NULL?
> 

Well, I wanted to implement those along with the mac_addr_set, as they
all dealt with mac addresses. But you're right, I might as well leave
the ops NULL.

I'll send a new version reflecting this.

> 
> <...>
> 
> > +	if (qid == 0) {
> > +		/*
> > +		 * tap_setup_queue() is called for both tx and rx.
> > +		 * Let's use dev->data->r/tx_queues[qid] to
> > determine if init
> > +		 * has already been done.
> > +		 */
> > +		if (dev->data->rx_queues[qid] &&
> > dev->data->tx_queues[qid])
> > +			return fd;
> > +
> > +		tap_mac_set(dev, &pmd->eth_addr);
> 
> What is the reason of changing behavior here?
> 
> Tap devices assigned random MAC by kernel, and previous implementation
> was reading that value and using it for DPDK.
> 
> Now kernel assigns a random MAC, and DPDK overwrites it another random
> MAC, previous implementation was simpler I think.
> 
> It is OK to move this code tap_setup_queue(), I just missed the
> benefit of overwriting with DPDK random MAC?
> 
> <...>

As far as I remember, I did it because somewhere the mac_addr_set was
checked as part of reconfiguration, which happenened before queue setup
was done. The default mac address (dev->data->mac_addrs[0]) got set to 0
and later call for the default mac address tried using this mac address.
Or something along those lines.

I'll definitely re-take a closer look at all this for my next version.

Regards,
Pascal
  

Patch

diff --git a/doc/guides/nics/features/tap.ini b/doc/guides/nics/features/tap.ini
index f4aca6921ddc..d9b47a003654 100644
--- a/doc/guides/nics/features/tap.ini
+++ b/doc/guides/nics/features/tap.ini
@@ -9,6 +9,7 @@  Jumbo frame          = Y
 Promiscuous mode     = Y
 Allmulticast mode    = Y
 Basic stats          = Y
+Unicast MAC filter   = Y
 Other kdrv           = Y
 ARMv7                = Y
 ARMv8                = Y
diff --git a/drivers/net/tap/rte_eth_tap.c b/drivers/net/tap/rte_eth_tap.c
index ece3a5fcc897..1e46ee36efa2 100644
--- a/drivers/net/tap/rte_eth_tap.c
+++ b/drivers/net/tap/rte_eth_tap.c
@@ -63,6 +63,8 @@ 
 #define RTE_PMD_TAP_MAX_QUEUES	1
 #endif
 
+#define RTE_PMD_TAP_MAX_MAC_ADDRS 1
+
 static struct rte_vdev_driver pmd_tap_drv;
 
 static const char *valid_arguments[] = {
@@ -118,7 +120,7 @@  struct pmd_internals {
  * supplied name.
  */
 static int
-tun_alloc(struct pmd_internals *pmd, uint16_t qid)
+tun_alloc(struct pmd_internals *pmd)
 {
 	struct ifreq ifr;
 #ifdef IFF_MULTI_QUEUE
@@ -176,16 +178,6 @@  tun_alloc(struct pmd_internals *pmd, uint16_t qid)
 		goto error;
 	}
 
-	if (qid == 0) {
-		if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
-			RTE_LOG(ERR, PMD, "ioctl failed (SIOCGIFHWADDR) (%s)\n",
-				ifr.ifr_name);
-			goto error;
-		}
-
-		rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
-	}
-
 	return fd;
 
 error:
@@ -365,7 +357,7 @@  tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 	struct pmd_internals *internals = dev->data->dev_private;
 
 	dev_info->if_index = internals->if_index;
-	dev_info->max_mac_addrs = 1;
+	dev_info->max_mac_addrs = RTE_PMD_TAP_MAX_MAC_ADDRS;
 	dev_info->max_rx_pktlen = (uint32_t)ETHER_MAX_VLAN_FRAME_LEN;
 	dev_info->max_rx_queues = internals->nb_queues;
 	dev_info->max_tx_queues = internals->nb_queues;
@@ -502,6 +494,69 @@  tap_allmulti_disable(struct rte_eth_dev *dev)
 	tap_link_set_flags(pmd, IFF_ALLMULTI, 0);
 }
 
+static void
+tap_mac_remove(struct rte_eth_dev *dev __rte_unused,
+	       uint32_t index __rte_unused)
+{
+	/*
+	 * A single MAC is authorized on the device and it's not possible to
+	 * leave the device without a MAC. Don't do anything, then.
+	 */
+}
+
+static void
+tap_mac_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
+	    uint32_t index, uint32_t vmdq __rte_unused)
+{
+	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,
+			"%s: can't set MAC address: index %d > max %d\n",
+			dev->data->name, index, RTE_PMD_TAP_MAX_MAC_ADDRS - 1);
+		return;
+	}
+	if (is_zero_ether_addr(mac_addr)) {
+		RTE_LOG(ERR, PMD,
+			"%s: can't set an empty MAC address.\n",
+			dev->data->name);
+		return;
+	}
+	if (fd < 0) {
+		RTE_LOG(ERR, PMD,
+			"%s: can't set MAC address: device does not exist.\n",
+			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));
+		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);
+}
+
+static void
+tap_mac_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
+{
+	if (is_zero_ether_addr(mac_addr)) {
+		RTE_LOG(ERR, PMD, "%s: can't set an empty MAC address\n",
+			dev->data->name);
+		return;
+	}
+	tap_mac_remove(dev, 0);
+	tap_mac_add(dev, mac_addr, 0, 0);
+}
+
 static int
 tap_setup_queue(struct rte_eth_dev *dev,
 		struct pmd_internals *internals,
@@ -518,7 +573,7 @@  tap_setup_queue(struct rte_eth_dev *dev,
 		if (fd < 0) {
 			RTE_LOG(INFO, PMD, "Add queue to TAP %s for qid %d\n",
 				pmd->name, qid);
-			fd = tun_alloc(pmd, qid);
+			fd = tun_alloc(pmd);
 			if (fd < 0) {
 				RTE_LOG(ERR, PMD, "tun_alloc(%s, %d) failed\n",
 					pmd->name, qid);
@@ -530,6 +585,18 @@  tap_setup_queue(struct rte_eth_dev *dev,
 	rx->fd = fd;
 	tx->fd = fd;
 
+	if (qid == 0) {
+		/*
+		 * tap_setup_queue() is called for both tx and rx.
+		 * Let's use dev->data->r/tx_queues[qid] to determine if init
+		 * has already been done.
+		 */
+		if (dev->data->rx_queues[qid] && dev->data->tx_queues[qid])
+			return fd;
+
+		tap_mac_set(dev, &pmd->eth_addr);
+	}
+
 	return fd;
 }
 
@@ -636,6 +703,9 @@  static const struct eth_dev_ops ops = {
 	.promiscuous_disable    = tap_promisc_disable,
 	.allmulticast_enable    = tap_allmulti_enable,
 	.allmulticast_disable   = tap_allmulti_disable,
+	.mac_addr_remove        = tap_mac_remove,
+	.mac_addr_add           = tap_mac_add,
+	.mac_addr_set           = tap_mac_set,
 	.stats_get              = tap_stats_get,
 	.stats_reset            = tap_stats_reset,
 };
@@ -683,6 +753,7 @@  eth_dev_tap_create(const char *name, char *tap_name)
 	data->drv_name = pmd_tap_drv.driver.name;
 	data->numa_node = numa_node;
 
+	eth_random_addr((uint8_t *)&pmd->eth_addr);
 	data->dev_link = pmd_link;
 	data->mac_addrs = &pmd->eth_addr;
 	data->nb_rx_queues = pmd->nb_queues;