This patch implements MAC address setting with Vhost-kernel
backends.

With this, it is possible to set the TAP interface MAC address
using the Ethdev API.

Signed-off-by: Maxime Coquelin <maxime.coque...@redhat.com>
---

Change from RFC: Rebased on latest main

---

 drivers/net/virtio/virtio_user/vhost_kernel.c | 23 +++++++++++++++++++
 .../net/virtio/virtio_user/vhost_kernel_tap.c |  2 +-
 .../net/virtio/virtio_user/vhost_kernel_tap.h |  2 +-
 .../net/virtio/virtio_user/virtio_user_dev.c  |  2 +-
 4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/drivers/net/virtio/virtio_user/vhost_kernel.c 
b/drivers/net/virtio/virtio_user/vhost_kernel.c
index e42bb35935..4ff47c2327 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel.c
@@ -12,6 +12,7 @@
 #include <rte_memory.h>

 #include "vhost.h"
+#include "virtio.h"
 #include "virtio_user_dev.h"
 #include "vhost_kernel_tap.h"

@@ -152,6 +153,9 @@ vhost_kernel_get_features(struct virtio_user_dev *dev, 
uint64_t *features)
        if (tap_flags & IFF_MULTI_QUEUE)
                *features |= (1ull << VIRTIO_NET_F_MQ);

+       /* vhost_kernel supports setting tap MAC address. */
+       *features |= (1ull << VIRTIO_NET_F_MAC);
+
        return 0;
 }

@@ -371,6 +375,24 @@ vhost_kernel_set_status(struct virtio_user_dev *dev 
__rte_unused, uint8_t status
        return -ENOTSUP;
 }

+static int
+vhost_kernel_set_config(struct virtio_user_dev *dev,
+                       const uint8_t *data, uint32_t off, uint32_t len)
+{
+       struct vhost_kernel_data *backend_data = dev->backend_data;
+
+       if (off == offsetof(struct virtio_net_config, mac)) {
+               if (len != RTE_ETHER_ADDR_LEN) {
+                       PMD_DRV_LOG(ERR, "Invalid MAC address length");
+                       return -EINVAL;
+               }
+
+               return tap_set_mac(backend_data->tapfds[0], data);
+       }
+
+       return -ENOTSUP;
+}
+
 /**
  * Set up environment to talk with a vhost kernel backend.
  *
@@ -613,6 +635,7 @@ struct virtio_user_backend_ops virtio_ops_kernel = {
        .set_vring_addr = vhost_kernel_set_vring_addr,
        .get_status = vhost_kernel_get_status,
        .set_status = vhost_kernel_set_status,
+       .set_config = vhost_kernel_set_config,
        .enable_qp = vhost_kernel_enable_queue_pair,
        .update_link_state = vhost_kernel_update_link_state,
        .get_intr_fd = vhost_kernel_get_intr_fd,
diff --git a/drivers/net/virtio/virtio_user/vhost_kernel_tap.c 
b/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
index 611e2e25ec..3e13f8f65b 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
@@ -112,7 +112,7 @@ tap_get_flags(int tapfd, unsigned int *tap_flags)
 }

 int
-tap_set_mac(int tapfd, uint8_t *mac)
+tap_set_mac(int tapfd, const uint8_t *mac)
 {
        struct ifreq ifr;

diff --git a/drivers/net/virtio/virtio_user/vhost_kernel_tap.h 
b/drivers/net/virtio/virtio_user/vhost_kernel_tap.h
index 636a0481be..0b9258dcb7 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel_tap.h
+++ b/drivers/net/virtio/virtio_user/vhost_kernel_tap.h
@@ -40,6 +40,6 @@ int tap_support_features(unsigned int *tap_features);
 int tap_open(const char *ifname, unsigned int r_flags, bool multi_queue);
 int tap_get_name(int tapfd, char **ifname);
 int tap_get_flags(int tapfd, unsigned int *tap_flags);
-int tap_set_mac(int tapfd, uint8_t *mac);
+int tap_set_mac(int tapfd, const uint8_t *mac);

 #endif
diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c 
b/drivers/net/virtio/virtio_user/virtio_user_dev.c
index 187f81b066..d5475bc4eb 100644
--- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
+++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
@@ -790,7 +790,7 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char 
*path, uint16_t queues,
        if (!packed_vq)
                dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);

-       if (dev->mac_specified)
+       if (dev->mac_specified || (dev->device_features & (1ull << 
VIRTIO_NET_F_MAC)))
                dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
        else
                dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
--
2.49.0

Reply via email to