Skip to contentSkip to conversation

kvm tools: pass virtio header size to uip_init

PATCH Applied Sasha Levin 2 participants 9 messages May 3, 2013 – May 14, 2013 KVM

lore
Share & tools
lore
#1 lore raw

PATCH 2/8 kvm tools: fix vhost interaction with ctrl vq

In reply to Sasha Levin

We broke networking using vhost with the introduction of a ctrl vq, make sure that that queue get treated like a special case.

---
 tools/kvm/virtio/net.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
tools/kvm/virtio/net.c +12 -5
index 2de9222..bef0039 100644
@@ -357,6 +357,11 @@ static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
 	ndev->features = features;
 }
 
+static bool is_ctrl_vq(struct net_dev *ndev, u32 vq)
+{
+	return vq == (u32)(ndev->queue_pairs * 2);
+}
+
 static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align,
 		   u32 pfn)
 {
@@ -377,10 +382,12 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align,
 
 	mutex_init(&ndev->io_lock[vq]);
 	pthread_cond_init(&ndev->io_cond[vq], NULL);
-	if (ndev->vhost_fd == 0) {
-		if (vq == (u32)(ndev->queue_pairs * 2))
-			pthread_create(&ndev->io_thread[vq], NULL, virtio_net_ctrl_thread, ndev);
-		else if (vq & 1)
+	if (is_ctrl_vq(ndev, vq)) {
+		pthread_create(&ndev->io_thread[vq], NULL, virtio_net_ctrl_thread, ndev);
+
+		return 0;
+	} else if (ndev->vhost_fd == 0 ) {
+		if (vq & 1)
 			pthread_create(&ndev->io_thread[vq], NULL, virtio_net_tx_thread, ndev);
 		else
 			pthread_create(&ndev->io_thread[vq], NULL, virtio_net_rx_thread, ndev);
@@ -453,7 +460,7 @@ static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd)
 	};
 	int r;
 
-	if (ndev->vhost_fd == 0)
+	if (ndev->vhost_fd == 0 || is_ctrl_vq(ndev, vq))
 		return;
 
 	r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_KICK, &file);
Signature · 1 line
1.8.2.1
#0 lore raw

PATCH 1/8 kvm tools: pass virtio header size to uip_init

We want to make the size of the virtio net header opaque to uip.

---
 tools/kvm/include/kvm/uip.h | 1 +
 tools/kvm/net/uip/core.c    | 8 ++++----
 tools/kvm/net/uip/tcp.c     | 2 +-
 tools/kvm/net/uip/udp.c     | 2 +-
 tools/kvm/virtio/net.c      | 1 +
 5 files changed, 8 insertions(+), 6 deletions(-)
tools/kvm/include/kvm/uip.h +1 -0
index ac248d2..338582d 100644
@@ -205,6 +205,7 @@ struct uip_info {
 	u32 dns_ip[UIP_DHCP_MAX_DNS_SERVER_NR];
 	char *domain_name;
 	u32 buf_nr;
+	u32 vnet_hdr_len;
 };
 
 struct uip_buf {
tools/kvm/net/uip/core.c +4 -4
index 4e5bb82..7a74261 100644
@@ -172,10 +172,10 @@ int uip_init(struct uip_info *info)
 	}
 
 	list_for_each_entry(buf, buf_head, list) {
-		buf->vnet	= malloc(sizeof(struct virtio_net_hdr));
-		buf->vnet_len	= sizeof(struct virtio_net_hdr);
-		buf->eth	= malloc(1024*64 + sizeof(struct uip_pseudo_hdr));
-		buf->eth_len	= 1024*64 + sizeof(struct uip_pseudo_hdr);
+		buf->vnet_len   = info->vnet_hdr_len;
+		buf->vnet	= malloc(buf->vnet_len);
+		buf->eth_len    = 1024*64 + sizeof(struct uip_pseudo_hdr);
+		buf->eth	= malloc(buf->eth_len);
 
 		memset(buf->vnet, 0, buf->vnet_len);
 		memset(buf->eth, 0, buf->eth_len);
tools/kvm/net/uip/tcp.c +1 -1
index 9044f40..3c30ade 100644
@@ -153,7 +153,7 @@ static int uip_tcp_payload_send(struct uip_tcp_socket *sk, u8 flag, u16 payload_
 	/*
 	 * virtio_net_hdr
 	 */
-	buf->vnet_len	= sizeof(struct virtio_net_hdr);
+	buf->vnet_len	= info->vnet_hdr_len;
 	memset(buf->vnet, 0, buf->vnet_len);
 
 	buf->eth_len	= ntohs(ip2->len) + uip_eth_hdrlen(&ip2->eth);
tools/kvm/net/uip/udp.c +1 -1
index 31c417c..dd288c5 100644
@@ -142,7 +142,7 @@ int uip_udp_make_pkg(struct uip_info *info, struct uip_udp_socket *sk, struct ui
 	/*
 	 * virtio_net_hdr
 	 */
-	buf->vnet_len	= sizeof(struct virtio_net_hdr);
+	buf->vnet_len	= info->vnet_hdr_len;
 	memset(buf->vnet, 0, buf->vnet_len);
 
 	buf->eth_len	= ntohs(ip2->len) + uip_eth_hdrlen(&ip2->eth);
tools/kvm/virtio/net.c +1 -0
index c0a8f12..2de9222 100644
@@ -668,6 +668,7 @@ static int virtio_net__init_one(struct virtio_net_params *params)
 		ndev->info.guest_ip		= ntohl(inet_addr(params->guest_ip));
 		ndev->info.guest_netmask	= ntohl(inet_addr("255.255.255.0"));
 		ndev->info.buf_nr		= 20,
+		ndev->info.vnet_hdr_len		= sizeof(struct virtio_net_hdr);
 		uip_init(&ndev->info);
 		ndev->ops = &uip_ops;
 	}
Signature · 1 line
1.8.2.1
#2 lore raw

PATCH 3/8 kvm tools: add status notification hook for virtio

Some devices want to know their status, use this hook to allow them to get that notification.

---
 tools/kvm/include/kvm/virtio.h | 1 +
 tools/kvm/virtio/pci.c         | 2 ++
 2 files changed, 3 insertions(+)
tools/kvm/include/kvm/virtio.h +1 -0
index 924279b..269ea4a 100644
@@ -80,6 +80,7 @@ struct virtio_ops {
 	void (*notify_vq_eventfd)(struct kvm *kvm, void *dev, u32 vq, u32 efd);
 	int (*signal_vq)(struct kvm *kvm, struct virtio_device *vdev, u32 queueid);
 	int (*signal_config)(struct kvm *kvm, struct virtio_device *vdev);
+	void (*notify_status)(struct kvm *kvm, void *dev, u8 status);
 	int (*init)(struct kvm *kvm, void *dev, struct virtio_device *vdev,
 		    int device_id, int subsys_id, int class);
 	int (*exit)(struct kvm *kvm, struct virtio_device *vdev);
tools/kvm/virtio/pci.c +2 -0
index 227d567..fec8ce0 100644
@@ -210,6 +210,8 @@ static bool virtio_pci__io_out(struct ioport *ioport, struct kvm *kvm, u16 port,
 		break;
 	case VIRTIO_PCI_STATUS:
 		vpci->status = ioport__read8(data);
+		if (vdev->ops->notify_status)
+			vdev->ops->notify_status(kvm, vpci->dev, vpci->status);
 		break;
 	default:
 		ret = virtio_pci__specific_io_out(kvm, vdev, port, data, size, offset);
Signature · 1 line
1.8.2.1
#6 lore raw

PATCH 7/8 kvm tools: use iovec functions in uip_rx

In reply to Sasha Levin

Simplifies the code a lot.

---
 tools/kvm/include/kvm/uip.h |  4 ++--
 tools/kvm/net/uip/core.c    | 47 +++------------------------------------------
 2 files changed, 5 insertions(+), 46 deletions(-)
tools/kvm/include/kvm/uip.h +2 -2
index 338582d..cb79e94 100644
@@ -214,8 +214,8 @@ struct uip_buf {
 	int vnet_len;
 	int eth_len;
 	int status;
-	char *vnet;
-	char *eth;
+	unsigned char *vnet;
+	unsigned char *eth;
 	int id;
 };
 
tools/kvm/net/uip/core.c +3 -44

70 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#4 lore raw

PATCH 5/8 kvm tools: use correct vnet header size for mergable rx buffers

In reply to Sasha Levin

vnet header size depends on whether we use mergable rx buffers.

---
 tools/kvm/virtio/net.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/kvm/virtio/net.c +6 -2
index 2dbca09..15dbde3 100644
@@ -243,7 +243,9 @@ static bool virtio_net__tap_init(struct net_dev *ndev)
 		goto fail;
 	}
 
-	hdr_len = sizeof(struct virtio_net_hdr);
+	hdr_len = (ndev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
+			sizeof(struct virtio_net_hdr_mrg_rxbuf) :
+			sizeof(struct virtio_net_hdr);
 	if (ioctl(ndev->tap_fd, TUNSETVNETHDRSZ, &hdr_len) < 0)
 		pr_warning("Config tap device TUNSETVNETHDRSZ error");
 
@@ -679,7 +681,6 @@ static int virtio_net__init_one(struct virtio_net_params *params)
 		ndev->info.guest_ip		= ntohl(inet_addr(params->guest_ip));
 		ndev->info.guest_netmask	= ntohl(inet_addr("255.255.255.0"));
 		ndev->info.buf_nr		= 20,
-		ndev->info.vnet_hdr_len		= sizeof(struct virtio_net_hdr);
 		ndev->ops = &uip_ops;
 	}
 
@@ -710,6 +711,9 @@ static void notify_status(struct kvm *kvm, void *dev, u8 status)
 		if (!virtio_net__tap_init(ndev))
 			die_perror("You have requested a TAP device, but creation of one has failed because");
 	} else {
+		ndev->info.vnet_hdr_len = (ndev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
+						sizeof(struct virtio_net_hdr_mrg_rxbuf) :
+						sizeof(struct virtio_net_hdr);
 		uip_init(&ndev->info);
 	}
 }
Signature · 1 line
1.8.2.1
#3 lore raw

PATCH 4/8 kvm tools: init network devices only when the virtio driver is ready to go

In reply to Sasha Levin

We may need to know what features are supported before we can init the network on the host side.

---
 tools/kvm/virtio/net.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)
tools/kvm/virtio/net.c +24 -6
index bef0039..2dbca09 100644
@@ -58,6 +58,8 @@ struct net_dev {
 	struct uip_info			info;
 	struct net_dev_operations	*ops;
 	struct kvm			*kvm;
+
+	struct virtio_net_params	*params;
 };
 
 static LIST_HEAD(ndevs);
@@ -207,13 +209,13 @@ static void virtio_net_handle_callback(struct kvm *kvm, struct net_dev *ndev, in
 	mutex_unlock(&ndev->io_lock[queue]);
 }
 
-static bool virtio_net__tap_init(const struct virtio_net_params *params,
-					struct net_dev *ndev)
+static bool virtio_net__tap_init(struct net_dev *ndev)
 {
 	int sock = socket(AF_INET, SOCK_STREAM, 0);
 	int pid, status, offload, hdr_len;
 	struct sockaddr_in sin = {0};
 	struct ifreq ifr;
+	const struct virtio_net_params *params = ndev->params;
 
 	/* Did the user already gave us the FD? */
 	if (params->fd) {
@@ -496,6 +498,8 @@ static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size)
 	return size;
 }
 
+static void notify_status(struct kvm *kvm, void *dev, u8 status);
+
 static struct virtio_ops net_dev_virtio_ops = (struct virtio_ops) {
 	.get_config		= get_config,
 	.get_host_features	= get_host_features,
@@ -507,6 +511,7 @@ static struct virtio_ops net_dev_virtio_ops = (struct virtio_ops) {
 	.notify_vq		= notify_vq,
 	.notify_vq_gsi		= notify_vq_gsi,
 	.notify_vq_eventfd	= notify_vq_eventfd,
+	.notify_status		= notify_status,
 };
 
 static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev)
@@ -652,6 +657,7 @@ static int virtio_net__init_one(struct virtio_net_params *params)
 	list_add_tail(&ndev->list, &ndevs);
 
 	ndev->kvm = params->kvm;
+	ndev->params = params;
 
 	mutex_init(&ndev->mutex);
 	ndev->queue_pairs = max(1, min(VIRTIO_NET_NUM_QUEUES, params->mq));
@@ -667,8 +673,6 @@ static int virtio_net__init_one(struct virtio_net_params *params)
 
 	ndev->mode = params->mode;
 	if (ndev->mode == NET_MODE_TAP) {
-		if (!virtio_net__tap_init(params, ndev))
-			die_perror("You have requested a TAP device, but creation of one has failed because");
 		ndev->ops = &tap_ops;
 	} else {
 		ndev->info.host_ip		= ntohl(inet_addr(params->host_ip));
@@ -676,7 +680,6 @@ static int virtio_net__init_one(struct virtio_net_params *params)
 		ndev->info.guest_netmask	= ntohl(inet_addr("255.255.255.0"));
 		ndev->info.buf_nr		= 20,
 		ndev->info.vnet_hdr_len		= sizeof(struct virtio_net_hdr);
-		uip_init(&ndev->info);
 		ndev->ops = &uip_ops;
 	}
 
@@ -696,6 +699,21 @@ static int virtio_net__init_one(struct virtio_net_params *params)
 	return 0;
 }
 
+static void notify_status(struct kvm *kvm, void *dev, u8 status)
+{
+	struct net_dev *ndev = dev;
+
+	if (!(status & VIRTIO_CONFIG_S_DRIVER_OK))
+		return;
+
+	if (ndev->mode == NET_MODE_TAP) {
+		if (!virtio_net__tap_init(ndev))
+			die_perror("You have requested a TAP device, but creation of one has failed because");
+	} else {
+		uip_init(&ndev->info);
+	}
+}
+
 int virtio_net__init(struct kvm *kvm)
 {
 	int i;
@@ -706,7 +724,7 @@ int virtio_net__init(struct kvm *kvm)
 	}
 
 	if (kvm->cfg.num_net_devices == 0 && kvm->cfg.no_net == 0) {
-		struct virtio_net_params net_params;
+		static struct virtio_net_params net_params;
 
 		net_params = (struct virtio_net_params) {
 			.guest_ip	= kvm->cfg.guest_ip,
Signature · 1 line
1.8.2.1
#5 lore raw

PATCH 6/8 kvm tools: steal iovec handling routines from the kernel

In reply to Sasha Levin

They're hidden inside net/core/iovec.c. It'd be nice to just link to that but they're not too generic and come with tons of net/ specific code we don't want. So we just copy over the relevant parts.

---
 tools/kvm/Makefile               |   1 +
 tools/kvm/include/kvm/iovec.h    |  21 +++++++
 tools/kvm/include/linux/kernel.h |  10 ++++
 tools/kvm/util/iovec.c           | 126 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 158 insertions(+)
 create mode 100644 tools/kvm/include/kvm/iovec.h
 create mode 100644 tools/kvm/util/iovec.c
tools/kvm/Makefile +1 -0
index a0a0a9b..70accaa 100644
@@ -86,6 +86,7 @@ OBJS	+= net/uip/csum.o
 OBJS	+= net/uip/dhcp.o
 OBJS	+= kvm-cmd.o
 OBJS	+= util/init.o
+OBJS    += util/iovec.o
 OBJS	+= util/rbtree.o
 OBJS	+= util/threadpool.o
 OBJS	+= util/parse-options.o
tools/kvm/include/kvm/iovec.h new file +21 -0
new file mode 100644
index 0000000..fe79dd4
@@ -0,0 +1,21 @@
+#ifndef KVM_UTIL_IOVEC_H_
+#define KVM_UTIL_IOVEC_H_
+
+extern int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len);
+extern int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
+				size_t offset, int len);
+extern int memcpy_toiovec(struct iovec *v, unsigned char *kdata, int len);
+extern int memcpy_toiovecend(const struct iovec *v, unsigned char *kdata,
+				size_t offset, int len);
+
+static inline size_t iov_size(const struct iovec *iovec, size_t len)
+{
+	size_t size = 0, i;
+
+	for (i = 0; i < len; i++)
+		size += iovec[i].iov_len;
+
+	return size;
+}
+
+#endif
tools/kvm/include/linux/kernel.h +10 -0
index 1e9abe9..f2bff5f 100644
@@ -36,6 +36,16 @@
 	(void) (&_max1 == &_max2);		\
 	_max1 > _max2 ? _max1 : _max2; })
 
+#define min_t(type, x, y) ({                    \
+	type __min1 = (x);                      \
+	type __min2 = (y);                      \
+	__min1 < __min2 ? __min1: __min2; })
+
+#define max_t(type, x, y) ({                    \
+	type __max1 = (x);                      \
+	type __max2 = (y);                      \
+	__max1 > __max2 ? __max1: __max2; })
+
 #define true 1
 
 #endif
tools/kvm/util/iovec.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#7 lore raw

PATCH 8/8 kvm tools: virtio-net mergable rx buffers

In reply to Sasha Levin

Support mergable rx buffers for virtio-net. This helps reduce the amount of memory the guest kernel has to allocate per rx vq.

---
 tools/kvm/include/kvm/uip.h |  2 +-
 tools/kvm/net/uip/core.c    |  2 +-
 tools/kvm/virtio/net.c      | 42 ++++++++++++++++++++++++++++++++++++------
 3 files changed, 38 insertions(+), 8 deletions(-)
tools/kvm/include/kvm/uip.h +1 -1
index cb79e94..4e63808 100644
@@ -253,7 +253,7 @@ struct uip_tcp_socket {
 };
 
 struct uip_tx_arg {
-	struct virtio_net_hdr *vnet;
+	void *vnet;
 	struct uip_info *info;
 	struct uip_eth *eth;
 	int vnet_len;
tools/kvm/net/uip/core.c +1 -1
index e31efc2..789b814 100644
@@ -8,7 +8,7 @@
 
 int uip_tx(struct iovec *iov, u16 out, struct uip_info *info)
 {
-	struct virtio_net_hdr *vnet;
+	void *vnet;
 	struct uip_tx_arg arg;
 	int eth_len, vnet_len;
 	struct uip_eth *eth;
tools/kvm/virtio/net.c +36 -6

85 lines not on this page · raw message

Signature · 1 line
1.8.2.1