DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH v1] net/virtio-user: implement MAC setting for Vhost-kernel
@ 2025-05-21 14:23 Maxime Coquelin
  2025-05-21 14:52 ` Stephen Hemminger
  0 siblings, 1 reply; 2+ messages in thread
From: Maxime Coquelin @ 2025-05-21 14:23 UTC (permalink / raw)
  To: dev, david.marchand, chenbox; +Cc: Maxime Coquelin

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.coquelin@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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-05-21 14:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-21 14:23 [PATCH v1] net/virtio-user: implement MAC setting for Vhost-kernel Maxime Coquelin
2025-05-21 14:52 ` Stephen Hemminger

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).