Skip to contentSkip to conversation

vhost: add interface name to virtio-net struct

PATCH In review [email protected] 5 participants 6 messages Dec 18, 2014 – Jan 25, 2015 3 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vhost: add interface name to virtio-net struct

From: Ciara Loftus <[email protected]>

This patch fixes the issue whereby when using userspace vhost ports in the context of vSwitching, the name provided to the hypervisor/QEMU of the vhost tap device needs to be exposed in the library, in order for the vSwitch to be able to direct packets to the correct device. This patch introduces an 'ifname' member to the virtio-net structure which is populated with the tap device name when QEMU is brought up with a vhost device.

---
 lib/librte_vhost/rte_virtio_net.h |    3 ++
 lib/librte_vhost/virtio-net.c     |   48 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletions(-)
lib/librte_vhost/rte_virtio_net.h +3 -0
index 00b1328..0bf07c7 100644
@@ -43,6 +43,8 @@
 #include <linux/virtio_ring.h>
 #include <linux/virtio_net.h>
 #include <sys/eventfd.h>
+#include <sys/socket.h>
+#include <linux/if.h>
 
 #include <rte_memory.h>
 #include <rte_mempool.h>
@@ -96,6 +98,7 @@ struct virtio_net {
 	uint64_t		features;	/**< Negotiated feature set. */
 	uint64_t		device_fh;	/**< device identifier. */
 	uint32_t		flags;		/**< Device flags. Only used to check if device is running on data core. */
+	char			ifname[IFNAMSIZ];	/**< Name of the tap device. */
 	void			*priv;		/**< private context */
 } __rte_cache_aligned;
 
lib/librte_vhost/virtio-net.c +47 -1

67 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#2 lore raw
In reply to a message not in the archive

2014-12-18 18:07, [email protected]:

Quoted 11 lines

This patch fixes the issue whereby when using userspace vhost ports in the context of vSwitching, the name provided to the hypervisor/QEMU of the vhost tap device needs to be exposed in the library, in order for the vSwitch to be able to direct packets to the correct device. This patch introduces an 'ifname' member to the virtio-net structure which is populated with the tap device name when QEMU is brought up with a vhost device.

Seems important to make this new library working in a common scenario. Applied

Thanks

Signature · 1 line
Thomas
#3 lore raw
In reply to a message not in the archive

Hi Ciara,

Quoted 85 lines

-----Original Message-----
From: Loftus, Ciara
Sent: Thursday, December 18, 2014 6:07 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; Ananyev, Konstantin; Czesnowicz, Przemyslaw; Loftus, Ciara; Anthony Fee
Subject: [PATCH v2] vhost: add interface name to virtio-net struct

From: Ciara Loftus <[email protected]>

This patch fixes the issue whereby when using userspace vhost ports in the context of vSwitching, the name provided to the hypervisor/QEMU of the vhost tap device needs to be exposed in the library, in order for the vSwitch to be able to direct packets to the correct device. This patch introduces an 'ifname' member to the virtio-net structure which is populated with the tap device name when QEMU is brought up with a vhost device.

---
 lib/librte_vhost/rte_virtio_net.h |    3 ++
 lib/librte_vhost/virtio-net.c     |   48 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletions(-)
lib/librte_vhost/rte_virtio_net.h +3 -0
index 00b1328..0bf07c7 100644
@@ -43,6 +43,8 @@
 #include <linux/virtio_ring.h>
 #include <linux/virtio_net.h>
 #include <sys/eventfd.h>
+#include <sys/socket.h>
+#include <linux/if.h>
 
 #include <rte_memory.h>
 #include <rte_mempool.h>
@@ -96,6 +98,7 @@ struct virtio_net {
 	uint64_t		features;	/**< Negotiated feature set. */
 	uint64_t		device_fh;	/**< device identifier. */
 	uint32_t		flags;		/**< Device flags. Only used to check if device is running on data core. */
+	char			ifname[IFNAMSIZ];	/**< Name of the tap device. */
 	void			*priv;		/**< private context */
 } __rte_cache_aligned;
 
lib/librte_vhost/virtio-net.c +23 -0
index 852b6d1..7eae5ee 100644
@@ -43,6 +43,10 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
+#include <sys/socket.h>
+#include <linux/if_tun.h>
+#include <linux/if.h>
+
 #include <rte_ethdev.h>
 #include <rte_log.h>
 #include <rte_string_fns.h>
@@ -1000,6 +1004,46 @@ set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
 }
 
 /*
+ * Function to get the tap device name from the provided file descriptor and
+ * save it in the device structure.
+ */
+static int
+get_ifname(struct virtio_net *dev, int tap_fd, int pid)
+{
+	struct eventfd_copy fd_tap;
+	struct ifreq ifr;
+	uint32_t size, ifr_size;
+	int ret;
+
+	fd_tap.source_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
+	fd_tap.target_fd = tap_fd;
+	fd_tap.target_pid = pid;
+
+	if (eventfd_copy(dev, &fd_tap))
+		return -1;
+
+	ret = ioctl(fd_tap.source_fd, TUNGETIFF, &ifr);

lib/librte_vhost/virtio-net.c set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) · line 1035

Quoted 10 lines
lib/librte_vhost/virtio-net.c +10 -0
+
+	if (close(fd_tap.source_fd) < 0)
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"(%"PRIu64") fd close failed\n",
+			dev->device_fh);
+
+	if (ret >= 0) {
+		ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name));
+		size = ifr_size > sizeof(dev->ifname)?
+				sizeof(dev->ifname): ifr_size;

If you saying that sizeof(ifr.ifr_name) would always be equal to sizeof(dev->ifname), then why are you doing the comparison above?
"ifr_size <= sizeof(dev->ifname)" would always be true, right?

Quoted 1 line

From other side, if if strlen(ifr.ifr_name) < sizeof(dev->ifname), you wouldn't copy over terminating 0.

I think it needs to be something like:

size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name)) + 1; size = RTE_MIN(size, sizeof(dev->ifname)); strncpy(dev->ifname, ifr.ifr_name, size); dev->ifname[sizeof(dev->ifname) - 1] = 0;

lib/librte_vhost/virtio-net.c set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) · line 1043

Quoted 8 lines
lib/librte_vhost/virtio-net.c +8 -0
+
+		strncpy(dev->ifname, ifr.ifr_name, size);
+	} else
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"(%"PRIu64") TUNGETIFF ioctl failed\n",
+			dev->device_fh);
+
+	return 0;

Shouldn't we return a failure if ret is negative (ioclt failed)? Konstantin

Quoted 20 lines
lib/librte_vhost/virtio-net.c +6 -2
+}
+
+/*
  * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
  * To complete device initialisation when the virtio driver is loaded,
  * we are provided with a valid fd for a tap device (not used by us).
@@ -1026,8 +1070,10 @@ set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
 	 */
 	if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
 		if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
-			((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED))
+			((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) {
+			get_ifname(dev, file->fd, ctx.pid);
 			return notify_ops->new_device(dev);
+		}
 	/* Otherwise we remove it. */
 	} else
 		if (file->fd == VIRTIO_DEV_STOPPED)
--

1.7.4.1

#5 lore raw
In reply to a message not in the archive

Hi Konstantin,

2014-12-19 15:39, Ananyev, Konstantin:

Quoted 23 lines

Hi Ciara,

Quoted 21 lines

-----Original Message-----
From: Loftus, Ciara
Sent: Thursday, December 18, 2014 6:07 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; Ananyev, Konstantin; Czesnowicz, Przemyslaw; Loftus, Ciara; Anthony Fee
Subject: [PATCH v2] vhost: add interface name to virtio-net struct

From: Ciara Loftus <[email protected]>

This patch fixes the issue whereby when using userspace vhost ports in the context of vSwitching, the name provided to the hypervisor/QEMU of the vhost tap device needs to be exposed in the library, in order for the vSwitch to be able to direct packets to the correct device. This patch introduces an 'ifname' member to the virtio-net structure which is populated with the tap device name when QEMU is brought up with a vhost device.

[...]

Quoted 37 lines
Quoted 20 lines

/*
+ * Function to get the tap device name from the provided file descriptor and
+ * save it in the device structure.
+ */

lib/librte_vhost/virtio-net.c +16 -0
+static int
+get_ifname(struct virtio_net *dev, int tap_fd, int pid)
+{
+	struct eventfd_copy fd_tap;
+	struct ifreq ifr;
+	uint32_t size, ifr_size;
+	int ret;
+
+	fd_tap.source_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
+	fd_tap.target_fd = tap_fd;
+	fd_tap.target_pid = pid;
+
+	if (eventfd_copy(dev, &fd_tap))
+		return -1;
+
+	ret = ioctl(fd_tap.source_fd, TUNGETIFF, &ifr);

lib/librte_vhost/virtio-net.c set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) · line 1035

Quoted 10 lines
lib/librte_vhost/virtio-net.c +10 -0
+
+	if (close(fd_tap.source_fd) < 0)
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"(%"PRIu64") fd close failed\n",
+			dev->device_fh);
+
+	if (ret >= 0) {
+		ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name));
+		size = ifr_size > sizeof(dev->ifname)?
+				sizeof(dev->ifname): ifr_size;

If you saying that sizeof(ifr.ifr_name) would always be equal to sizeof(dev->ifname), then why are you doing the comparison above?
"ifr_size <= sizeof(dev->ifname)" would always be true, right?
From other side, if if strlen(ifr.ifr_name) < sizeof(dev->ifname), you wouldn't copy over terminating 0. I think it needs to be something like:

Quoted 4 lines

size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name)) + 1; size = RTE_MIN(size, sizeof(dev->ifname)); strncpy(dev->ifname, ifr.ifr_name, size); dev->ifname[sizeof(dev->ifname) - 1] = 0;

This patch has been applied as hot fix.
If you think you can improve it, please submit a patch. We are releasing in few hours (probably tomorrow morning).

Quoted 11 lines

lib/librte_vhost/virtio-net.c set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) · line 1043

Quoted 8 lines
lib/librte_vhost/virtio-net.c +8 -0
+
+		strncpy(dev->ifname, ifr.ifr_name, size);
+	} else
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"(%"PRIu64") TUNGETIFF ioctl failed\n",
+			dev->device_fh);
+
+	return 0;

Shouldn't we return a failure if ret is negative (ioclt failed)? Konstantin

#6 lore raw
In reply to a message not in the archive
Quoted 5 lines · On 2014/12/19 2:07, [email protected] wrote:

From: Ciara Loftus <[email protected]>

This patch fixes the issue whereby when using userspace vhost ports in the context of vSwitching, the name provided to the hypervisor/QEMU of the vhost tap device needs to be exposed in the library, in order

Who create the tap device?When using vhost-user QEMU will not create it.

Quoted 1 line

for the vSwitch to be able to direct packets to the correct device.

Why not use unix domain socket path?

Quoted 108 lines

This patch introduces an 'ifname' member to the virtio-net structure which is populated with the tap device name when QEMU is brought up with a vhost device.

---
 lib/librte_vhost/rte_virtio_net.h |    3 ++
 lib/librte_vhost/virtio-net.c     |   48 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletions(-)
lib/librte_vhost/rte_virtio_net.h +3 -0
index 00b1328..0bf07c7 100644
@@ -43,6 +43,8 @@
 #include <linux/virtio_ring.h>
 #include <linux/virtio_net.h>
 #include <sys/eventfd.h>
+#include <sys/socket.h>
+#include <linux/if.h>
 
 #include <rte_memory.h>
 #include <rte_mempool.h>
@@ -96,6 +98,7 @@ struct virtio_net {
 	uint64_t		features;	/**< Negotiated feature set. */
 	uint64_t		device_fh;	/**< device identifier. */
 	uint32_t		flags;		/**< Device flags. Only used to check if device is running on data core. */
+	char			ifname[IFNAMSIZ];	/**< Name of the tap device. */
 	void			*priv;		/**< private context */
 } __rte_cache_aligned;
 
lib/librte_vhost/virtio-net.c +47 -1
index 852b6d1..7eae5ee 100644
@@ -43,6 +43,10 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
+#include <sys/socket.h>
+#include <linux/if_tun.h>
+#include <linux/if.h>
+
 #include <rte_ethdev.h>
 #include <rte_log.h>
 #include <rte_string_fns.h>
@@ -1000,6 +1004,46 @@ set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
 }
 
 /*
+ * Function to get the tap device name from the provided file descriptor and
+ * save it in the device structure.
+ */
+static int
+get_ifname(struct virtio_net *dev, int tap_fd, int pid)
+{
+	struct eventfd_copy fd_tap;
+	struct ifreq ifr;
+	uint32_t size, ifr_size;
+	int ret;
+
+	fd_tap.source_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
+	fd_tap.target_fd = tap_fd;
+	fd_tap.target_pid = pid;
+
+	if (eventfd_copy(dev, &fd_tap))
+		return -1;
+
+	ret = ioctl(fd_tap.source_fd, TUNGETIFF, &ifr);
+
+	if (close(fd_tap.source_fd) < 0)
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"(%"PRIu64") fd close failed\n",
+			dev->device_fh);
+
+	if (ret >= 0) {
+		ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name));
+		size = ifr_size > sizeof(dev->ifname)?
+				sizeof(dev->ifname): ifr_size;
+
+		strncpy(dev->ifname, ifr.ifr_name, size);
+	} else
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"(%"PRIu64") TUNGETIFF ioctl failed\n",
+			dev->device_fh);
+
+	return 0;
+}
+
+/*
  * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
  * To complete device initialisation when the virtio driver is loaded,
  * we are provided with a valid fd for a tap device (not used by us).
@@ -1026,8 +1070,10 @@ set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
 	 */
 	if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
 		if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
-			((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED))
+			((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) {
+			get_ifname(dev, file->fd, ctx.pid);
 			return notify_ops->new_device(dev);
+		}
 	/* Otherwise we remove it. */
 	} else
 		if (file->fd == VIRTIO_DEV_STOPPED)
Signature · 2 lines
Regards,
Haifeng
#8 lore raw
In reply to a message not in the archive
Quoted 17 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Linhaifeng
Sent: Sunday, January 25, 2015 12:42 PM
To: Loftus, Ciara; [email protected]
Cc: Anthony Fee
Subject: Re: [dpdk-dev] [PATCH v2] vhost: add interface name to virtio-net struct

Quoted 5 lines · On 2014/12/19 2:07, [email protected] wrote:

From: Ciara Loftus <[email protected]>

This patch fixes the issue whereby when using userspace vhost ports in the context of vSwitching, the name provided to the hypervisor/QEMU of the vhost tap device needs to be exposed in the library, in order

Quoted 5 lines

Who create the tap device?When using vhost-user QEMU will not create it.

Quoted 1 line

for the vSwitch to be able to direct packets to the correct device.

Why not use unix domain socket path?

In this patch, for vhost-cuse, the ifname comes from the name of the tap device. For vhost-user, ifname is the name of the socket path.

Quoted 122 lines
Quoted 11 lines

This patch introduces an 'ifname' member to the virtio-net structure which is populated with the tap device name when QEMU is brought up with a vhost device.

---
 lib/librte_vhost/rte_virtio_net.h |    3 ++
 lib/librte_vhost/virtio-net.c     |   48

++++++++++++++++++++++++++++++++++++-

Quoted 16 lines
 2 files changed, 50 insertions(+), 1 deletions(-)
lib/librte_vhost/rte_virtio_net.h +2 -0
index 00b1328..0bf07c7 100644
@@ -43,6 +43,8 @@
 #include <linux/virtio_ring.h>
 #include <linux/virtio_net.h>
 #include <sys/eventfd.h>
+#include <sys/socket.h>
+#include <linux/if.h>
 
 #include <rte_memory.h>
 #include <rte_mempool.h>

lib/librte_vhost/rte_virtio_net.h struct virtio_net { · line 98

Quoted 1 line
lib/librte_vhost/rte_virtio_net.h +0 -0
@@ -96,6 +98,7 @@ struct virtio_net {
 	uint64_t		features;	/**< Negotiated feature set.

*/

lib/librte_vhost/rte_virtio_net.h struct virtio_net { · line 100

Quoted 2 lines
lib/librte_vhost/rte_virtio_net.h +0 -0
 	uint64_t		device_fh;	/**< device identifier. */
 	uint32_t		flags;		/**< Device flags. Only used to

check if device is running on data core. */

lib/librte_vhost/rte_virtio_net.h struct virtio_net { · line 101

Quoted 1 line
lib/librte_vhost/rte_virtio_net.h +1 -0
+	char			ifname[IFNAMSIZ];	/**< Name of the tap

device. */

lib/librte_vhost/virtio-net.c set_vring_kick(struct vhost_device_ctx ctx, struct

Quoted 19 lines
lib/librte_vhost/rte_virtio_net.h +0 -0
 	void			*priv;		/**< private context */
 } __rte_cache_aligned;
 
lib/librte_vhost/virtio-net.c +4 -0
index 852b6d1..7eae5ee 100644
@@ -43,6 +43,10 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
+#include <sys/socket.h>
+#include <linux/if_tun.h>
+#include <linux/if.h>
+
 #include <rte_ethdev.h>
 #include <rte_log.h>
 #include <rte_string_fns.h>
@@ -1000,6 +1004,46 @@ set_vring_kick(struct vhost_device_ctx ctx, struct

vhost_vring_file *file)

lib/librte_vhost/virtio-net.c set_backend(struct vhost_device_ctx ctx, struct

Quoted 47 lines
lib/librte_vhost/virtio-net.c +40 -0
 }
 
 /*
+ * Function to get the tap device name from the provided file descriptor and
+ * save it in the device structure.
+ */
+static int
+get_ifname(struct virtio_net *dev, int tap_fd, int pid)
+{
+	struct eventfd_copy fd_tap;
+	struct ifreq ifr;
+	uint32_t size, ifr_size;
+	int ret;
+
+	fd_tap.source_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
+	fd_tap.target_fd = tap_fd;
+	fd_tap.target_pid = pid;
+
+	if (eventfd_copy(dev, &fd_tap))
+		return -1;
+
+	ret = ioctl(fd_tap.source_fd, TUNGETIFF, &ifr);
+
+	if (close(fd_tap.source_fd) < 0)
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"(%"PRIu64") fd close failed\n",
+			dev->device_fh);
+
+	if (ret >= 0) {
+		ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name));
+		size = ifr_size > sizeof(dev->ifname)?
+				sizeof(dev->ifname): ifr_size;
+
+		strncpy(dev->ifname, ifr.ifr_name, size);
+	} else
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"(%"PRIu64") TUNGETIFF ioctl failed\n",
+			dev->device_fh);
+
+	return 0;
+}
+
+/*
  * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
  * To complete device initialisation when the virtio driver is loaded,
  * we are provided with a valid fd for a tap device (not used by us).
@@ -1026,8 +1070,10 @@ set_backend(struct vhost_device_ctx ctx, struct

vhost_vring_file *file)

lib/librte_vhost/virtio-net.c set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)

Quoted 3 lines
lib/librte_vhost/virtio-net.c +0 -0
 	 */
 	if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
 		if (((int)dev->virtqueue[VIRTIO_TXQ]->backend !=

VIRTIO_DEV_STOPPED) &&

lib/librte_vhost/virtio-net.c set_backend(struct vhost_device_ctx ctx, struct · line 1029

Quoted 1 line
lib/librte_vhost/virtio-net.c +0 -1
-			((int)dev->virtqueue[VIRTIO_RXQ]->backend !=

VIRTIO_DEV_STOPPED))

lib/librte_vhost/virtio-net.c set_backend(struct vhost_device_ctx ctx, struct · line 1073

Quoted 1 line
lib/librte_vhost/virtio-net.c +1 -0
+			((int)dev->virtqueue[VIRTIO_RXQ]->backend !=

VIRTIO_DEV_STOPPED)) {

lib/librte_vhost/virtio-net.c set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file) · line 1079

Quoted 7 lines
lib/librte_vhost/virtio-net.c +2 -0
+			get_ifname(dev, file->fd, ctx.pid);
 			return notify_ops->new_device(dev);
+		}
 	/* Otherwise we remove it. */
 	} else
 		if (file->fd == VIRTIO_DEV_STOPPED)

--
Regards,
Haifeng