DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH v2] vhost: add interface name to virtio-net struct
@ 2014-12-18 18:07 ciara.loftus
  2014-12-18 22:02 ` Thomas Monjalon
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: ciara.loftus @ 2014-12-18 18:07 UTC (permalink / raw)
  To: dev; +Cc: Anthony Fee

From: Ciara Loftus <ciara.loftus@intel.com>

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.

Signed-off-by: Ciara Loftus <ciara.loftus@intel.com>
Signed-off-by: Anthony Fee <anthonyx.fee@intel.com>
Acked-by: Huawei Xie <huawei.xie@intel.com>
---
 lib/librte_vhost/rte_virtio_net.h |    3 ++
 lib/librte_vhost/virtio-net.c     |   48 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletions(-)

diff --git a/lib/librte_vhost/rte_virtio_net.h b/lib/librte_vhost/rte_virtio_net.h
index 00b1328..0bf07c7 100644
--- a/lib/librte_vhost/rte_virtio_net.h
+++ b/lib/librte_vhost/rte_virtio_net.h
@@ -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;
 
diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c
index 852b6d1..7eae5ee 100644
--- a/lib/librte_vhost/virtio-net.c
+++ b/lib/librte_vhost/virtio-net.c
@@ -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)
-- 
1.7.4.1

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

end of thread, other threads:[~2015-01-25  8:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-18 18:07 [dpdk-dev] [PATCH v2] vhost: add interface name to virtio-net struct ciara.loftus
2014-12-18 22:02 ` Thomas Monjalon
2014-12-19 15:39 ` Ananyev, Konstantin
2014-12-19 16:02   ` Thomas Monjalon
2015-01-25  4:41 ` Linhaifeng
2015-01-25  8:34   ` Xie, Huawei

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