[RFC] net/virtio-user: implement MAC setting for Vhost-kernel

Message ID 20250103144127.1438904-1-maxime.coquelin@redhat.com (mailing list archive)
State New
Delegated to: Maxime Coquelin
Headers
Series [RFC] net/virtio-user: implement MAC setting for Vhost-kernel |

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/loongarch-compilation success Compilation OK
ci/iol-broadcom-Performance success Performance Testing PASS
ci/loongarch-unit-testing success Unit Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-sample-apps-testing success Testing PASS
ci/iol-intel-Functional fail Functional Testing issues
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-compile-amd64-testing success Testing PASS

Commit Message

Maxime Coquelin Jan. 3, 2025, 2:41 p.m. UTC
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>
---
 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(-)
  

Patch

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 2997d2bd26..d1b3f986ef 100644
--- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
+++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
@@ -789,7 +789,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);