Skip to contentSkip to conversation

macvtap: macvtap TX zero-copy support

PATCH Superseded Shirley Ma 2 participants 7 messages May 4, 2011 – May 5, 2011 1 message not in the archive

Subsystems (3)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Shirley Ma

author
#0 lore raw

PATCH v4 5/8 macvtap: macvtap TX zero-copy support

Only when buffer size is greater than GOODCOPY_LEN (256), macvtap enables zero-copy.

---

 drivers/net/macvtap.c |  126 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 115 insertions(+), 11 deletions(-)
drivers/net/macvtap.c +115 -11
index 6696e56..e8bc5ff 100644
@@ -60,6 +60,7 @@ static struct proto macvtap_proto = {
  */
 static dev_t macvtap_major;
 #define MACVTAP_NUM_DEVS 65536
+#define GOODCOPY_LEN 256
 static struct class *macvtap_class;
 static struct cdev macvtap_cdev;
 
@@ -340,6 +341,7 @@ static int macvtap_open(struct inode *inode, struct file *file)
 {
 	struct net *net = current->nsproxy->net_ns;
 	struct net_device *dev = dev_get_by_index(net, iminor(inode));
+	struct macvlan_dev *vlan = netdev_priv(dev);
 	struct macvtap_queue *q;
 	int err;
 
@@ -369,6 +371,16 @@ static int macvtap_open(struct inode *inode, struct file *file)
 	q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
 	q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
 
+	/*
+	 * so far only VM uses macvtap, enable zero copy between guest
+	 * kernel and host kernel when lower device supports high memory
+	 * DMA
+	 */
+	if (vlan) {
+		if (vlan->lowerdev->features & NETIF_F_ZEROCOPY)
+			sock_set_flag(&q->sk, SOCK_ZEROCOPY);
+	}
+
 	err = macvtap_set_queue(dev, file, q);
 	if (err)
 		sock_put(&q->sk);
@@ -433,6 +445,80 @@ static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
 	return skb;
 }
 
+/* set skb frags from iovec, this can move to core network code for reuse */
+static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
+				  int offset, size_t count)
+{
+	int len = iov_length(from, count) - offset;
+	int copy = skb_headlen(skb);
+	int size, offset1 = 0;
+	int i = 0;
+	skb_frag_t *f;
+
+	/* Skip over from offset */
+	while (offset >= from->iov_len) {
+		offset -= from->iov_len;
+		++from;
+		--count;
+	}
+
+	/* copy up to skb headlen */
+	while (copy > 0) {
+		size = min_t(unsigned int, copy, from->iov_len - offset);
+		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
+				   size))
+			return -EFAULT;
+		if (copy > size) {
+			++from;
+			--count;
+		}
+		copy -= size;
+		offset1 += size;
+		offset = 0;
+	}
+
+	if (len == offset1)
+		return 0;
+
+	while (count--) {
+		struct page *page[MAX_SKB_FRAGS];
+		int num_pages;
+		unsigned long base;
+
+		len = from->iov_len - offset1;
+		if (!len) {
+			offset1 = 0;
+			++from;
+			continue;
+		}
+		base = (unsigned long)from->iov_base + offset1;
+		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
+		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
+		if ((num_pages != size) ||
+		    (num_pages > MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags))
+			/* put_page is in skb free */
+			return -EFAULT;
+		skb->data_len += len;
+		skb->len += len;
+		skb->truesize += len;
+		while (len) {
+			f = &skb_shinfo(skb)->frags[i];
+			f->page = page[i];
+			f->page_offset = base & ~PAGE_MASK;
+			f->size = min_t(int, len, PAGE_SIZE - f->page_offset);
+			skb_shinfo(skb)->nr_frags++;
+			/* increase sk_wmem_alloc */
+			atomic_add(f->size, &skb->sk->sk_wmem_alloc);
+			base += f->size;
+			len -= f->size;
+			i++;
+		}
+		offset1 = 0;
+		++from;
+	}
+	return 0;
+}
+
 /*
  * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  * be shared with the tun/tap driver.
@@ -515,17 +601,19 @@ static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
 
 
 /* Get packet from user space buffer */
-static ssize_t macvtap_get_user(struct macvtap_queue *q,
-				const struct iovec *iv, size_t count,
-				int noblock)
+static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
+				const struct iovec *iv, unsigned long total_len,
+				size_t count, int noblock)
 {
 	struct sk_buff *skb;
 	struct macvlan_dev *vlan;
-	size_t len = count;
+	unsigned long len = total_len;
 	int err;
 	struct virtio_net_hdr vnet_hdr = { 0 };
 	int vnet_hdr_len = 0;
+	int copylen, zerocopy;
 
+	zerocopy = sock_flag(&q->sk, SOCK_ZEROCOPY) && (len > GOODCOPY_LEN);
 	if (q->flags & IFF_VNET_HDR) {
 		vnet_hdr_len = q->vnet_hdr_sz;
 
@@ -552,12 +640,28 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
 	if (unlikely(len < ETH_HLEN))
 		goto err;
 
-	skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len, vnet_hdr.hdr_len,
-				noblock, &err);
+	if (zerocopy)
+		/* There are 256 bytes to be copied in skb, so there is enough
+		 * room for skb expand head in case it is used.
+		 * The rest buffer is mapped from userspace.
+		 */
+		copylen = GOODCOPY_LEN;
+	else
+		copylen = len;
+
+	skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
+				vnet_hdr.hdr_len, noblock, &err);
 	if (!skb)
 		goto err;
 
-	err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len, len);
+	if (zerocopy)
+		err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
+	else
+		err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
+						   len);
+	if (sock_flag(&q->sk, SOCK_ZEROCOPY))
+		memcpy(&skb_shinfo(skb)->ubuf, m->msg_control,
+			sizeof(struct skb_ubuf_info));
 	if (err)
 		goto err_kfree;
 
@@ -579,7 +683,7 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
 		kfree_skb(skb);
 	rcu_read_unlock_bh();
 
-	return count;
+	return total_len;
 
 err_kfree:
 	kfree_skb(skb);
@@ -601,8 +705,8 @@ static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
 	ssize_t result = -ENOLINK;
 	struct macvtap_queue *q = file->private_data;
 
-	result = macvtap_get_user(q, iv, iov_length(iv, count),
-			      file->f_flags & O_NONBLOCK);
+	result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
+				  file->f_flags & O_NONBLOCK);
 	return result;
 }
 
@@ -815,7 +919,7 @@ static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
 			   struct msghdr *m, size_t total_len)
 {
 	struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
-	return macvtap_get_user(q, m->msg_iov, total_len,
+	return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
 			    m->msg_flags & MSG_DONTWAIT);
 }
 
#1 lore raw
Quoted 4 lines · Shirley Ma

Only when buffer size is greater than GOODCOPY_LEN (256), macvtap enables zero-copy.

Looks good. Some thoughts below.

Quoted 10 lines

---

 drivers/net/macvtap.c |  126 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 115 insertions(+), 11 deletions(-)
drivers/net/macvtap.c +0 -0
index 6696e56..e8bc5ff 100644

drivers/net/macvtap.c static struct proto macvtap_proto = { · line 63

Quoted 4 lines
drivers/net/macvtap.c +1 -0
@@ -60,6 +60,7 @@ static struct proto macvtap_proto = {
  */
 static dev_t macvtap_major;
 #define MACVTAP_NUM_DEVS 65536
+#define GOODCOPY_LEN 256

Scope with MACVTAP_ please.

For small packets, is it better to copy in vhost and skip all the back and forth with callbacks? If yes, does it make sense to put the constant above in some header shared with vhost-net?

Quoted 86 lines
drivers/net/macvtap.c +65 -0
 static struct class *macvtap_class;
 static struct cdev macvtap_cdev;
 
@@ -340,6 +341,7 @@ static int macvtap_open(struct inode *inode, struct file *file)
 {
 	struct net *net = current->nsproxy->net_ns;
 	struct net_device *dev = dev_get_by_index(net, iminor(inode));
+	struct macvlan_dev *vlan = netdev_priv(dev);
 	struct macvtap_queue *q;
 	int err;
 
@@ -369,6 +371,16 @@ static int macvtap_open(struct inode *inode, struct file *file)
 	q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
 	q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
 
+	/*
+	 * so far only VM uses macvtap, enable zero copy between guest
+	 * kernel and host kernel when lower device supports high memory
+	 * DMA
+	 */
+	if (vlan) {
+		if (vlan->lowerdev->features & NETIF_F_ZEROCOPY)
+			sock_set_flag(&q->sk, SOCK_ZEROCOPY);
+	}
+
 	err = macvtap_set_queue(dev, file, q);
 	if (err)
 		sock_put(&q->sk);
@@ -433,6 +445,80 @@ static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
 	return skb;
 }
 
+/* set skb frags from iovec, this can move to core network code for reuse */
+static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
+				  int offset, size_t count)
+{
+	int len = iov_length(from, count) - offset;
+	int copy = skb_headlen(skb);
+	int size, offset1 = 0;
+	int i = 0;
+	skb_frag_t *f;
+
+	/* Skip over from offset */
+	while (offset >= from->iov_len) {
+		offset -= from->iov_len;
+		++from;
+		--count;
+	}
+
+	/* copy up to skb headlen */
+	while (copy > 0) {
+		size = min_t(unsigned int, copy, from->iov_len - offset);
+		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
+				   size))
+			return -EFAULT;
+		if (copy > size) {
+			++from;
+			--count;
+		}
+		copy -= size;
+		offset1 += size;
+		offset = 0;
+	}
+
+	if (len == offset1)
+		return 0;
+
+	while (count--) {
+		struct page *page[MAX_SKB_FRAGS];
+		int num_pages;
+		unsigned long base;
+
+		len = from->iov_len - offset1;
+		if (!len) {
+			offset1 = 0;
+			++from;
+			continue;
+		}
+		base = (unsigned long)from->iov_base + offset1;
+		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
+		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
+		if ((num_pages != size) ||
+		    (num_pages > MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags))
+			/* put_page is in skb free */
+			return -EFAULT;
+		skb->data_len += len;

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad, · line 511

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+		skb->len += len;
+		skb->truesize += len;
+		while (len) {
+			f = &skb_shinfo(skb)->frags[i];
+			f->page = page[i];
+			f->page_offset = base & ~PAGE_MASK;
+			f->size = min_t(int, len, PAGE_SIZE - f->page_offset);
+			skb_shinfo(skb)->nr_frags++;
+			/* increase sk_wmem_alloc */
+			atomic_add(f->size, &skb->sk->sk_wmem_alloc);

One thing that gave me pause that we only accound for part of the page here. I think we should count the whole page, no?

Quoted 39 lines
drivers/net/macvtap.c +16 -4
+			base += f->size;
+			len -= f->size;
+			i++;
+		}
+		offset1 = 0;
+		++from;
+	}
+	return 0;
+}
+
 /*
  * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  * be shared with the tun/tap driver.
@@ -515,17 +601,19 @@ static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
 
 
 /* Get packet from user space buffer */
-static ssize_t macvtap_get_user(struct macvtap_queue *q,
-				const struct iovec *iv, size_t count,
-				int noblock)
+static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
+				const struct iovec *iv, unsigned long total_len,
+				size_t count, int noblock)
 {
 	struct sk_buff *skb;
 	struct macvlan_dev *vlan;
-	size_t len = count;
+	unsigned long len = total_len;
 	int err;
 	struct virtio_net_hdr vnet_hdr = { 0 };
 	int vnet_hdr_len = 0;
+	int copylen, zerocopy;
 
+	zerocopy = sock_flag(&q->sk, SOCK_ZEROCOPY) && (len > GOODCOPY_LEN);
 	if (q->flags & IFF_VNET_HDR) {
 		vnet_hdr_len = q->vnet_hdr_sz;
 
@@ -552,12 +640,28 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
 	if (unlikely(len < ETH_HLEN))

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q, · line 648

Quoted 10 lines
drivers/net/macvtap.c +6 -2
 		goto err;
 
-	skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len, vnet_hdr.hdr_len,
-				noblock, &err);
+	if (zerocopy)
+		/* There are 256 bytes to be copied in skb, so there is enough
+		 * room for skb expand head in case it is used.
+		 * The rest buffer is mapped from userspace.
+		 */
+		copylen = GOODCOPY_LEN;

Just curious: where does the number 256 come from? Also, as long as we are copying, should we care about alignment?

Quoted 50 lines
drivers/net/macvtap.c +17 -5
+	else
+		copylen = len;
+
+	skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
+				vnet_hdr.hdr_len, noblock, &err);
 	if (!skb)
 		goto err;
 
-	err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len, len);
+	if (zerocopy)
+		err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
+	else
+		err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
+						   len);
+	if (sock_flag(&q->sk, SOCK_ZEROCOPY))
+		memcpy(&skb_shinfo(skb)->ubuf, m->msg_control,
+			sizeof(struct skb_ubuf_info));
 	if (err)
 		goto err_kfree;
 
@@ -579,7 +683,7 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
 		kfree_skb(skb);
 	rcu_read_unlock_bh();
 
-	return count;
+	return total_len;
 
 err_kfree:
 	kfree_skb(skb);
@@ -601,8 +705,8 @@ static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
 	ssize_t result = -ENOLINK;
 	struct macvtap_queue *q = file->private_data;
 
-	result = macvtap_get_user(q, iv, iov_length(iv, count),
-			      file->f_flags & O_NONBLOCK);
+	result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
+				  file->f_flags & O_NONBLOCK);
 	return result;
 }
 
@@ -815,7 +919,7 @@ static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
 			   struct msghdr *m, size_t total_len)
 {
 	struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
-	return macvtap_get_user(q, m->msg_iov, total_len,
+	return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
 			    m->msg_flags & MSG_DONTWAIT);
 }
 
author
#2 lore raw
Quoted 15 lines · Michael S. Tsirkin
Quoted 4 lines · Shirley Ma

Only when buffer size is greater than GOODCOPY_LEN (256), macvtap enables zero-copy.

Looks good. Some thoughts below.

Quoted 3 lines

---

 drivers/net/macvtap.c |  126

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

Quoted 2 lines
 1 files changed, 115 insertions(+), 11 deletions(-)
Quoted 11 lines

drivers/net/macvtap.c static struct proto macvtap_proto = { · line 63

Quoted 9 lines
drivers/net/macvtap.c +1 -0
index 6696e56..e8bc5ff 100644
@@ -60,6 +60,7 @@ static struct proto macvtap_proto = {
  */
 static dev_t macvtap_major;
 #define MACVTAP_NUM_DEVS 65536
+#define GOODCOPY_LEN 256

Scope with MACVTAP_ please.

Ok.

Quoted 4 lines

For small packets, is it better to copy in vhost and skip all the back and forth with callbacks? If yes, does it make sense to put the constant above in some header shared with vhost-net?

skb is created in macvtap, the small packet copy is in skb, so I don't think we can do it in vhost here.

Quoted 104 lines

drivers/net/macvtap.c static int macvtap_open(struct inode *inode,

Quoted 4 lines
drivers/net/macvtap.c +0 -0
 static struct class *macvtap_class;
 static struct cdev macvtap_cdev;
 
@@ -340,6 +341,7 @@ static int macvtap_open(struct inode *inode,

struct file *file)

drivers/net/macvtap.c static int macvtap_open(struct inode *inode,

Quoted 8 lines
drivers/net/macvtap.c +1 -0
 {
      struct net *net = current->nsproxy->net_ns;
      struct net_device *dev = dev_get_by_index(net, iminor(inode));
+     struct macvlan_dev *vlan = netdev_priv(dev);
      struct macvtap_queue *q;
      int err;
 
@@ -369,6 +371,16 @@ static int macvtap_open(struct inode *inode,

struct file *file)

drivers/net/macvtap.c static int macvtap_open(struct inode *inode, struct file *file)

Quoted 6 lines
drivers/net/macvtap.c +3 -0
      q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
      q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
 
+     /*
+      * so far only VM uses macvtap, enable zero copy between guest
+      * kernel and host kernel when lower device supports high

memory

drivers/net/macvtap.c static inline struct sk_buff

Quoted 11 lines
drivers/net/macvtap.c +7 -0
+      * DMA
+      */
+     if (vlan) {
+             if (vlan->lowerdev->features & NETIF_F_ZEROCOPY)
+                     sock_set_flag(&q->sk, SOCK_ZEROCOPY);
+     }
+
      err = macvtap_set_queue(dev, file, q);
      if (err)
              sock_put(&q->sk);
@@ -433,6 +445,80 @@ static inline struct sk_buff

*macvtap_alloc_skb(struct sock *sk, size_t prepad,

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 4 lines
drivers/net/macvtap.c +1 -0
      return skb;
 }
 
+/* set skb frags from iovec, this can move to core network code for

reuse */

drivers/net/macvtap.c static inline struct sk_buff · line 449

Quoted 1 line
drivers/net/macvtap.c +1 -0
+static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct

iovec *from,

Quoted 8 lines
drivers/net/macvtap.c +8 -0
+                               int offset, size_t count)
+{
+     int len = iov_length(from, count) - offset;
+     int copy = skb_headlen(skb);
+     int size, offset1 = 0;
+     int i = 0;
+     skb_frag_t *f;
+

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+     /* Skip over from offset */
+     while (offset >= from->iov_len) {
+             offset -= from->iov_len;
+             ++from;
+             --count;
+     }
+
+     /* copy up to skb headlen */
+     while (copy > 0) {
+             size = min_t(unsigned int, copy, from->iov_len -

offset);

drivers/net/macvtap.c static inline struct sk_buff · line 468

Quoted 1 line
drivers/net/macvtap.c +1 -0
+             if (copy_from_user(skb->data + offset1, from->iov_base

+ offset,

Quoted 17 lines
drivers/net/macvtap.c +17 -0
+                                size))
+                     return -EFAULT;
+             if (copy > size) {
+                     ++from;
+                     --count;
+             }
+             copy -= size;
+             offset1 += size;
+             offset = 0;
+     }
+
+     if (len == offset1)
+             return 0;
+
+     while (count--) {
+             struct page *page[MAX_SKB_FRAGS];
+             int num_pages;

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+             unsigned long base;
+
+             len = from->iov_len - offset1;
+             if (!len) {
+                     offset1 = 0;
+                     ++from;
+                     continue;
+             }
+             base = (unsigned long)from->iov_base + offset1;
+             size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >>

PAGE_SHIFT;

drivers/net/macvtap.c static inline struct sk_buff · line 496

Quoted 1 line
drivers/net/macvtap.c +1 -0
+             num_pages = get_user_pages_fast(base, size, 0,

&page[i]);

drivers/net/macvtap.c static inline struct sk_buff · line 498

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+             if ((num_pages != size) ||
+                 (num_pages > MAX_SKB_FRAGS -

skb_shinfo(skb)->nr_frags))

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+                     /* put_page is in skb free */
+                     return -EFAULT;
+             skb->data_len += len;
+             skb->len += len;
+             skb->truesize += len;
+             while (len) {
+                     f = &skb_shinfo(skb)->frags[i];
+                     f->page = page[i];
+                     f->page_offset = base & ~PAGE_MASK;
+                     f->size = min_t(int, len, PAGE_SIZE -
Quoted 7 lines

f->page_offset);

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad, · line 511

Quoted 3 lines
drivers/net/macvtap.c +3 -0
+                     skb_shinfo(skb)->nr_frags++;
+                     /* increase sk_wmem_alloc */
+                     atomic_add(f->size, &skb->sk->sk_wmem_alloc);

One thing that gave me pause that we only accound for part of the page here. I think we should count the whole page, no?

The whole page is pinned, but it might not be for this sock only.

I think I should change to atomic_add to outside the loop to save cost.

Quoted 52 lines

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const

Quoted 14 lines
drivers/net/macvtap.c +10 -0
+                     base += f->size;
+                     len -= f->size;
+                     i++;
+             }
+             offset1 = 0;
+             ++from;
+     }
+     return 0;
+}
+
 /*
  * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  * be shared with the tun/tap driver.
@@ -515,17 +601,19 @@ static int macvtap_skb_to_vnet_hdr(const

struct sk_buff *skb,

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,

Quoted 7 lines
drivers/net/macvtap.c +1 -3
 /* Get packet from user space buffer */
-static ssize_t macvtap_get_user(struct macvtap_queue *q,
-                             const struct iovec *iv, size_t count,
-                             int noblock)
+static ssize_t macvtap_get_user(struct macvtap_queue *q, struct

msghdr *m,

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const · line 603

Quoted 1 line
drivers/net/macvtap.c +1 -0
+                             const struct iovec *iv, unsigned long

total_len,

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,

Quoted 12 lines
drivers/net/macvtap.c +4 -1
+                             size_t count, int noblock)
 {
      struct sk_buff *skb;
      struct macvlan_dev *vlan;
-     size_t len = count;
+     unsigned long len = total_len;
      int err;
      struct virtio_net_hdr vnet_hdr = { 0 };
      int vnet_hdr_len = 0;
+     int copylen, zerocopy;
 
+     zerocopy = sock_flag(&q->sk, SOCK_ZEROCOPY) && (len >

GOODCOPY_LEN);

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct

Quoted 4 lines
drivers/net/macvtap.c +0 -0
      if (q->flags & IFF_VNET_HDR) {
              vnet_hdr_len = q->vnet_hdr_sz;
 
@@ -552,12 +640,28 @@ static ssize_t macvtap_get_user(struct

macvtap_queue *q,

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q,

Quoted 4 lines
drivers/net/macvtap.c +0 -1
      if (unlikely(len < ETH_HLEN))
              goto err;
 
-     skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len,

vnet_hdr.hdr_len,

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q,

Quoted 3 lines
drivers/net/macvtap.c +2 -1
-                             noblock, &err);
+     if (zerocopy)
+             /* There are 256 bytes to be copied in skb, so there

is enough

Quoted 8 lines

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q, · line 648

Quoted 4 lines
drivers/net/macvtap.c +4 -0
+              * room for skb expand head in case it is used.
+              * The rest buffer is mapped from userspace.
+              */
+             copylen = GOODCOPY_LEN;

Just curious: where does the number 256 come from? Also, as long as we are copying, should we care about alignment?

256 makes the size big enough for any skb head expanding.

That's my concern before. But guest should alignment the buffer already, after moving the pointer 256 bytes. It should still alignment, right?

Quoted 61 lines

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q,

Quoted 9 lines
drivers/net/macvtap.c +5 -1
+     else
+             copylen = len;
+
+     skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
+                             vnet_hdr.hdr_len, noblock, &err);
      if (!skb)
              goto err;
 
-     err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,

len);

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct · line 658

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+     if (zerocopy)
+             err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len,

count);

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct · line 660

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+     else
+             err = skb_copy_datagram_from_iovec(skb, 0, iv,

vnet_hdr_len,

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct

Quoted 8 lines
drivers/net/macvtap.c +4 -0
+                                                len);
+     if (sock_flag(&q->sk, SOCK_ZEROCOPY))
+             memcpy(&skb_shinfo(skb)->ubuf, m->msg_control,
+                     sizeof(struct skb_ubuf_info));
      if (err)
              goto err_kfree;
 
@@ -579,7 +683,7 @@ static ssize_t macvtap_get_user(struct

macvtap_queue *q,

drivers/net/macvtap.c static ssize_t macvtap_aio_write(struct kiocb

Quoted 9 lines
drivers/net/macvtap.c +1 -1
              kfree_skb(skb);
      rcu_read_unlock_bh();
 
-     return count;
+     return total_len;
 
 err_kfree:
      kfree_skb(skb);
@@ -601,8 +705,8 @@ static ssize_t macvtap_aio_write(struct kiocb

*iocb, const struct iovec *iv,

drivers/net/macvtap.c static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,

Quoted 6 lines
drivers/net/macvtap.c +1 -2
      ssize_t result = -ENOLINK;
      struct macvtap_queue *q = file->private_data;
 
-     result = macvtap_get_user(q, iv, iov_length(iv, count),
-                           file->f_flags & O_NONBLOCK);
+     result = macvtap_get_user(q, NULL, iv, iov_length(iv, count),

count,

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb *iocb,

Quoted 5 lines
drivers/net/macvtap.c +1 -0
+                               file->f_flags & O_NONBLOCK);
      return result;
 }
 
@@ -815,7 +919,7 @@ static int macvtap_sendmsg(struct kiocb *iocb,

struct socket *sock,

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,

Quoted 3 lines
drivers/net/macvtap.c +0 -0
                         struct msghdr *m, size_t total_len)
 {
      struct macvtap_queue *q = container_of(sock, struct

macvtap_queue, sock);

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb *iocb, · line 922

Quoted 2 lines
drivers/net/macvtap.c +1 -1
-     return macvtap_get_user(q, m->msg_iov, total_len,
+     return macvtap_get_user(q, m, m->msg_iov, total_len,

m->msg_iovlen,

Quoted 4 lines
drivers/net/macvtap.c +0 -0
                          m->msg_flags & MSG_DONTWAIT);
 }
 
#3 lore raw
Quoted 29 lines · Shirley Ma
Quoted 15 lines · Michael S. Tsirkin
Quoted 4 lines · Shirley Ma

Only when buffer size is greater than GOODCOPY_LEN (256), macvtap enables zero-copy.

Looks good. Some thoughts below.

Quoted 3 lines

---

 drivers/net/macvtap.c |  126

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

Quoted 2 lines
 1 files changed, 115 insertions(+), 11 deletions(-)
Quoted 11 lines

drivers/net/macvtap.c static struct proto macvtap_proto = { · line 63

Quoted 9 lines
drivers/net/macvtap.c +1 -0
index 6696e56..e8bc5ff 100644
@@ -60,6 +60,7 @@ static struct proto macvtap_proto = {
  */
 static dev_t macvtap_major;
 #define MACVTAP_NUM_DEVS 65536
+#define GOODCOPY_LEN 256

Scope with MACVTAP_ please.

Ok.

Quoted 7 lines
Quoted 4 lines

For small packets, is it better to copy in vhost and skip all the back and forth with callbacks? If yes, does it make sense to put the constant above in some header shared with vhost-net?

skb is created in macvtap, the small packet copy is in skb, so I don't think we can do it in vhost here.

One simple way is to pass NULL instead of the pend pointer for when we want macvtap to copy unconditionally. vhost-net will know that packet is copied and can notify user unconditionally.

I think this will solve the small packet regression you see ... no?

Quoted 105 lines
Quoted 105 lines

drivers/net/macvtap.c static int macvtap_open(struct inode *inode,

Quoted 4 lines
drivers/net/macvtap.c +0 -0
 static struct class *macvtap_class;
 static struct cdev macvtap_cdev;
 
@@ -340,6 +341,7 @@ static int macvtap_open(struct inode *inode,

struct file *file)

drivers/net/macvtap.c static int macvtap_open(struct inode *inode,

Quoted 8 lines
drivers/net/macvtap.c +1 -0
 {
      struct net *net = current->nsproxy->net_ns;
      struct net_device *dev = dev_get_by_index(net, iminor(inode));
+     struct macvlan_dev *vlan = netdev_priv(dev);
      struct macvtap_queue *q;
      int err;
 
@@ -369,6 +371,16 @@ static int macvtap_open(struct inode *inode,

struct file *file)

drivers/net/macvtap.c static int macvtap_open(struct inode *inode, struct file *file)

Quoted 6 lines
drivers/net/macvtap.c +3 -0
      q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
      q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
 
+     /*
+      * so far only VM uses macvtap, enable zero copy between guest
+      * kernel and host kernel when lower device supports high

memory

drivers/net/macvtap.c static inline struct sk_buff

Quoted 11 lines
drivers/net/macvtap.c +7 -0
+      * DMA
+      */
+     if (vlan) {
+             if (vlan->lowerdev->features & NETIF_F_ZEROCOPY)
+                     sock_set_flag(&q->sk, SOCK_ZEROCOPY);
+     }
+
      err = macvtap_set_queue(dev, file, q);
      if (err)
              sock_put(&q->sk);
@@ -433,6 +445,80 @@ static inline struct sk_buff

*macvtap_alloc_skb(struct sock *sk, size_t prepad,

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 4 lines
drivers/net/macvtap.c +1 -0
      return skb;
 }
 
+/* set skb frags from iovec, this can move to core network code for

reuse */

drivers/net/macvtap.c static inline struct sk_buff · line 449

Quoted 1 line
drivers/net/macvtap.c +1 -0
+static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct

iovec *from,

Quoted 8 lines
drivers/net/macvtap.c +8 -0
+                               int offset, size_t count)
+{
+     int len = iov_length(from, count) - offset;
+     int copy = skb_headlen(skb);
+     int size, offset1 = 0;
+     int i = 0;
+     skb_frag_t *f;
+

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+     /* Skip over from offset */
+     while (offset >= from->iov_len) {
+             offset -= from->iov_len;
+             ++from;
+             --count;
+     }
+
+     /* copy up to skb headlen */
+     while (copy > 0) {
+             size = min_t(unsigned int, copy, from->iov_len -

offset);

drivers/net/macvtap.c static inline struct sk_buff · line 468

Quoted 1 line
drivers/net/macvtap.c +1 -0
+             if (copy_from_user(skb->data + offset1, from->iov_base

+ offset,

Quoted 17 lines
drivers/net/macvtap.c +17 -0
+                                size))
+                     return -EFAULT;
+             if (copy > size) {
+                     ++from;
+                     --count;
+             }
+             copy -= size;
+             offset1 += size;
+             offset = 0;
+     }
+
+     if (len == offset1)
+             return 0;
+
+     while (count--) {
+             struct page *page[MAX_SKB_FRAGS];
+             int num_pages;

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+             unsigned long base;
+
+             len = from->iov_len - offset1;
+             if (!len) {
+                     offset1 = 0;
+                     ++from;
+                     continue;
+             }
+             base = (unsigned long)from->iov_base + offset1;
+             size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >>

PAGE_SHIFT;

drivers/net/macvtap.c static inline struct sk_buff · line 496

Quoted 1 line
drivers/net/macvtap.c +1 -0
+             num_pages = get_user_pages_fast(base, size, 0,

&page[i]);

drivers/net/macvtap.c static inline struct sk_buff · line 498

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+             if ((num_pages != size) ||
+                 (num_pages > MAX_SKB_FRAGS -

skb_shinfo(skb)->nr_frags))

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+                     /* put_page is in skb free */
+                     return -EFAULT;
+             skb->data_len += len;
+             skb->len += len;
+             skb->truesize += len;
+             while (len) {
+                     f = &skb_shinfo(skb)->frags[i];
+                     f->page = page[i];
+                     f->page_offset = base & ~PAGE_MASK;
+                     f->size = min_t(int, len, PAGE_SIZE -

f->page_offset);

Quoted 8 lines
Quoted 6 lines

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad, · line 511

Quoted 3 lines
drivers/net/macvtap.c +3 -0
+                     skb_shinfo(skb)->nr_frags++;
+                     /* increase sk_wmem_alloc */
+                     atomic_add(f->size, &skb->sk->sk_wmem_alloc);

One thing that gave me pause that we only accound for part of the page here. I think we should count the whole page, no?

The whole page is pinned, but it might not be for this sock only.

Right, but worst-case it is, so I think we should stay on the safe side and limit what the user can do.

Quoted 1 line

I think I should change to atomic_add to outside the loop to save cost.

Sure, good idea.

Quoted 57 lines
Quoted 57 lines

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const

Quoted 14 lines
drivers/net/macvtap.c +10 -0
+                     base += f->size;
+                     len -= f->size;
+                     i++;
+             }
+             offset1 = 0;
+             ++from;
+     }
+     return 0;
+}
+
 /*
  * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  * be shared with the tun/tap driver.
@@ -515,17 +601,19 @@ static int macvtap_skb_to_vnet_hdr(const

struct sk_buff *skb,

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,

Quoted 7 lines
drivers/net/macvtap.c +1 -3
 /* Get packet from user space buffer */
-static ssize_t macvtap_get_user(struct macvtap_queue *q,
-                             const struct iovec *iv, size_t count,
-                             int noblock)
+static ssize_t macvtap_get_user(struct macvtap_queue *q, struct

msghdr *m,

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const · line 603

Quoted 1 line
drivers/net/macvtap.c +1 -0
+                             const struct iovec *iv, unsigned long

total_len,

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,

Quoted 12 lines
drivers/net/macvtap.c +4 -1
+                             size_t count, int noblock)
 {
      struct sk_buff *skb;
      struct macvlan_dev *vlan;
-     size_t len = count;
+     unsigned long len = total_len;
      int err;
      struct virtio_net_hdr vnet_hdr = { 0 };
      int vnet_hdr_len = 0;
+     int copylen, zerocopy;
 
+     zerocopy = sock_flag(&q->sk, SOCK_ZEROCOPY) && (len >

GOODCOPY_LEN);

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct

Quoted 4 lines
drivers/net/macvtap.c +0 -0
      if (q->flags & IFF_VNET_HDR) {
              vnet_hdr_len = q->vnet_hdr_sz;
 
@@ -552,12 +640,28 @@ static ssize_t macvtap_get_user(struct

macvtap_queue *q,

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q,

Quoted 4 lines
drivers/net/macvtap.c +0 -1
      if (unlikely(len < ETH_HLEN))
              goto err;
 
-     skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len,

vnet_hdr.hdr_len,

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q,

Quoted 3 lines
drivers/net/macvtap.c +2 -1
-                             noblock, &err);
+     if (zerocopy)
+             /* There are 256 bytes to be copied in skb, so there

is enough

Quoted 4 lines
drivers/net/macvtap.c +4 -0
+              * room for skb expand head in case it is used.
+              * The rest buffer is mapped from userspace.
+              */
+             copylen = GOODCOPY_LEN;
Quoted 7 lines
Quoted 3 lines

Just curious: where does the number 256 come from? Also, as long as we are copying, should we care about alignment?

256 makes the size big enough for any skb head expanding.

That's my concern before.

I'm not sure I understand.
Could you tell me how do we know 256 is big enough for any skb head expanding please?

Quoted 2 lines

But guest should alignment the buffer already,
after moving the pointer 256 bytes. It should still alignment, right?

I mean in the host. But whatever, it's not that important at this point.

Quoted 61 lines
Quoted 61 lines

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q,

Quoted 9 lines
drivers/net/macvtap.c +5 -1
+     else
+             copylen = len;
+
+     skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
+                             vnet_hdr.hdr_len, noblock, &err);
      if (!skb)
              goto err;
 
-     err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,

len);

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct · line 658

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+     if (zerocopy)
+             err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len,

count);

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct · line 660

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+     else
+             err = skb_copy_datagram_from_iovec(skb, 0, iv,

vnet_hdr_len,

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct

Quoted 8 lines
drivers/net/macvtap.c +4 -0
+                                                len);
+     if (sock_flag(&q->sk, SOCK_ZEROCOPY))
+             memcpy(&skb_shinfo(skb)->ubuf, m->msg_control,
+                     sizeof(struct skb_ubuf_info));
      if (err)
              goto err_kfree;
 
@@ -579,7 +683,7 @@ static ssize_t macvtap_get_user(struct

macvtap_queue *q,

drivers/net/macvtap.c static ssize_t macvtap_aio_write(struct kiocb

Quoted 9 lines
drivers/net/macvtap.c +1 -1
              kfree_skb(skb);
      rcu_read_unlock_bh();
 
-     return count;
+     return total_len;
 
 err_kfree:
      kfree_skb(skb);
@@ -601,8 +705,8 @@ static ssize_t macvtap_aio_write(struct kiocb

*iocb, const struct iovec *iv,

drivers/net/macvtap.c static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,

Quoted 6 lines
drivers/net/macvtap.c +1 -2
      ssize_t result = -ENOLINK;
      struct macvtap_queue *q = file->private_data;
 
-     result = macvtap_get_user(q, iv, iov_length(iv, count),
-                           file->f_flags & O_NONBLOCK);
+     result = macvtap_get_user(q, NULL, iv, iov_length(iv, count),

count,

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb *iocb,

Quoted 5 lines
drivers/net/macvtap.c +1 -0
+                               file->f_flags & O_NONBLOCK);
      return result;
 }
 
@@ -815,7 +919,7 @@ static int macvtap_sendmsg(struct kiocb *iocb,

struct socket *sock,

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,

Quoted 3 lines
drivers/net/macvtap.c +0 -0
                         struct msghdr *m, size_t total_len)
 {
      struct macvtap_queue *q = container_of(sock, struct

macvtap_queue, sock);

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb *iocb, · line 922

Quoted 2 lines
drivers/net/macvtap.c +1 -1
-     return macvtap_get_user(q, m->msg_iov, total_len,
+     return macvtap_get_user(q, m, m->msg_iov, total_len,

m->msg_iovlen,

Quoted 4 lines
drivers/net/macvtap.c +0 -0
                          m->msg_flags & MSG_DONTWAIT);
 }
 
author
#4 lore raw
Quoted 38 lines · Michael S. Tsirkin
Quoted 3 lines · Shirley Ma
Quoted 2 lines · Michael S. Tsirkin
Quoted 1 line · Shirley Ma

Only when buffer size is greater than GOODCOPY_LEN (256),

macvtap

Quoted 24 lines
Quoted 13 lines
Quoted 3 lines

enables zero-copy.

Looks good. Some thoughts below.

Quoted 3 lines

---

 drivers/net/macvtap.c |  126

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

Quoted 2 lines
 1 files changed, 115 insertions(+), 11 deletions(-)
Quoted 11 lines

drivers/net/macvtap.c static struct proto macvtap_proto = { · line 63

Quoted 9 lines
drivers/net/macvtap.c +1 -0
index 6696e56..e8bc5ff 100644
@@ -60,6 +60,7 @@ static struct proto macvtap_proto = {
  */
 static dev_t macvtap_major;
 #define MACVTAP_NUM_DEVS 65536
+#define GOODCOPY_LEN 256

Scope with MACVTAP_ please.

Quoted 8 lines

Ok.

Quoted 4 lines

For small packets, is it better to copy in vhost and skip all the back and forth with callbacks? If yes, does it make sense to put the constant above in some header shared with vhost-net?

skb is created in macvtap, the small packet copy is in skb, so I

don't

Quoted 8 lines
Quoted 1 line

think we can do it in vhost here.

One simple way is to pass NULL instead of the pend pointer for when we want macvtap to copy unconditionally. vhost-net will know that packet is copied and can notify user unconditionally.

I think this will solve the small packet regression you see ... no?

I can certainly test it out. The small packet regression seems from more guest exits. This patch doesn't do anything to cause more guest exits, but it did speed up sendmsg() path.

Quoted 117 lines
Quoted 8 lines
Quoted 8 lines

drivers/net/macvtap.c static int macvtap_open(struct inode *inode,

Quoted 4 lines
drivers/net/macvtap.c +0 -0
 static struct class *macvtap_class;
 static struct cdev macvtap_cdev;
 
@@ -340,6 +341,7 @@ static int macvtap_open(struct inode *inode,

struct file *file)

Quoted 3 lines
drivers/net/macvtap.c +0 -0
 {
      struct net *net = current->nsproxy->net_ns;
      struct net_device *dev = dev_get_by_index(net,

iminor(inode));

Quoted 5 lines
Quoted 5 lines

drivers/net/macvtap.c static int macvtap_open(struct inode

Quoted 5 lines
drivers/net/macvtap.c +1 -0
+     struct macvlan_dev *vlan = netdev_priv(dev);
      struct macvtap_queue *q;
      int err;
 
@@ -369,6 +371,16 @@ static int macvtap_open(struct inode

*inode,

Quoted 6 lines
Quoted 6 lines

struct file *file)

Quoted 5 lines
drivers/net/macvtap.c +2 -0
      q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
      q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
 
+     /*
+      * so far only VM uses macvtap, enable zero copy between

guest

Quoted 14 lines
Quoted 14 lines

drivers/net/macvtap.c static int macvtap_open(struct inode · line 376

Quoted 1 line
drivers/net/macvtap.c +1 -0
+      * kernel and host kernel when lower device supports high

memory

drivers/net/macvtap.c static inline struct sk_buff

Quoted 11 lines
drivers/net/macvtap.c +7 -0
+      * DMA
+      */
+     if (vlan) {
+             if (vlan->lowerdev->features & NETIF_F_ZEROCOPY)
+                     sock_set_flag(&q->sk, SOCK_ZEROCOPY);
+     }
+
      err = macvtap_set_queue(dev, file, q);
      if (err)
              sock_put(&q->sk);
@@ -433,6 +445,80 @@ static inline struct sk_buff

*macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 4 lines
Quoted 4 lines

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 4 lines
drivers/net/macvtap.c +1 -0
      return skb;
 }
 
+/* set skb frags from iovec, this can move to core network code

for

Quoted 2 lines
Quoted 2 lines

reuse */

Quoted 1 line
drivers/net/macvtap.c +1 -0
+static int zerocopy_sg_from_iovec(struct sk_buff *skb, const

struct

Quoted 20 lines
Quoted 20 lines

iovec *from,

Quoted 8 lines
drivers/net/macvtap.c +8 -0
+                               int offset, size_t count)
+{
+     int len = iov_length(from, count) - offset;
+     int copy = skb_headlen(skb);
+     int size, offset1 = 0;
+     int i = 0;
+     skb_frag_t *f;
+

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+     /* Skip over from offset */
+     while (offset >= from->iov_len) {
+             offset -= from->iov_len;
+             ++from;
+             --count;
+     }
+
+     /* copy up to skb headlen */
+     while (copy > 0) {
+             size = min_t(unsigned int, copy, from->iov_len -

offset);

Quoted 1 line
Quoted 1 line

drivers/net/macvtap.c static inline struct sk_buff · line 468

Quoted 1 line
drivers/net/macvtap.c +1 -0
+             if (copy_from_user(skb->data + offset1,

from->iov_base

Quoted 44 lines
Quoted 44 lines

+ offset,

Quoted 17 lines
drivers/net/macvtap.c +17 -0
+                                size))
+                     return -EFAULT;
+             if (copy > size) {
+                     ++from;
+                     --count;
+             }
+             copy -= size;
+             offset1 += size;
+             offset = 0;
+     }
+
+     if (len == offset1)
+             return 0;
+
+     while (count--) {
+             struct page *page[MAX_SKB_FRAGS];
+             int num_pages;

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+             unsigned long base;
+
+             len = from->iov_len - offset1;
+             if (!len) {
+                     offset1 = 0;
+                     ++from;
+                     continue;
+             }
+             base = (unsigned long)from->iov_base + offset1;
+             size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >>

PAGE_SHIFT;

drivers/net/macvtap.c static inline struct sk_buff · line 496

Quoted 1 line
drivers/net/macvtap.c +1 -0
+             num_pages = get_user_pages_fast(base, size, 0,

&page[i]);

drivers/net/macvtap.c static inline struct sk_buff · line 498

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+             if ((num_pages != size) ||
+                 (num_pages > MAX_SKB_FRAGS -

skb_shinfo(skb)->nr_frags))

Quoted 10 lines
drivers/net/macvtap.c +10 -0
+                     /* put_page is in skb free */
+                     return -EFAULT;
+             skb->data_len += len;
+             skb->len += len;
+             skb->truesize += len;
+             while (len) {
+                     f = &skb_shinfo(skb)->frags[i];
+                     f->page = page[i];
+                     f->page_offset = base & ~PAGE_MASK;
+                     f->size = min_t(int, len, PAGE_SIZE -
Quoted 4 lines
Quoted 4 lines

f->page_offset);

Quoted 3 lines
drivers/net/macvtap.c +3 -0
+                     skb_shinfo(skb)->nr_frags++;
+                     /* increase sk_wmem_alloc */
+                     atomic_add(f->size,

&skb->sk->sk_wmem_alloc);

Quoted 2 lines
Quoted 2 lines

One thing that gave me pause that we only accound for part of the

Quoted 8 lines

page

Quoted 3 lines
Quoted 1 line

here. I think we should count the whole page, no?

The whole page is pinned, but it might not be for this sock only.

Right, but worst-case it is, so I think we should stay on the safe side
and limit what the user can do.

Ok, let me try this out.

Quoted 69 lines
Quoted 1 line

I think I should change to atomic_add to outside the loop to save

cost.

Sure, good idea.

Quoted 17 lines
Quoted 17 lines

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const

Quoted 14 lines
drivers/net/macvtap.c +10 -0
+                     base += f->size;
+                     len -= f->size;
+                     i++;
+             }
+             offset1 = 0;
+             ++from;
+     }
+     return 0;
+}
+
 /*
  * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  * be shared with the tun/tap driver.
@@ -515,17 +601,19 @@ static int macvtap_skb_to_vnet_hdr(const

struct sk_buff *skb,

Quoted 2 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/macvtap.c

Quoted 3 lines

/* Get packet from user space buffer */

drivers/net/macvtap.c +0 -2
-static ssize_t macvtap_get_user(struct macvtap_queue *q,
-                             const struct iovec *iv, size_t

count,

Quoted 4 lines
Quoted 4 lines

drivers/net/macvtap.c

Quoted 2 lines
drivers/net/macvtap.c +1 -1
-                             int noblock)
+static ssize_t macvtap_get_user(struct macvtap_queue *q, struct

msghdr *m,

Quoted 1 line

+ const struct iovec *iv, unsigned

long

Quoted 19 lines
Quoted 19 lines

total_len,

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,

Quoted 12 lines
drivers/net/macvtap.c +4 -1
+                             size_t count, int noblock)
 {
      struct sk_buff *skb;
      struct macvlan_dev *vlan;
-     size_t len = count;
+     unsigned long len = total_len;
      int err;
      struct virtio_net_hdr vnet_hdr = { 0 };
      int vnet_hdr_len = 0;
+     int copylen, zerocopy;
 
+     zerocopy = sock_flag(&q->sk, SOCK_ZEROCOPY) && (len >

GOODCOPY_LEN);

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct

Quoted 4 lines
      if (q->flags & IFF_VNET_HDR) {
              vnet_hdr_len = q->vnet_hdr_sz;
drivers/net/macvtap.c +0 -0
@@ -552,12 +640,28 @@ static ssize_t macvtap_get_user(struct

macvtap_queue *q,

Quoted 8 lines
Quoted 8 lines

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q,

Quoted 4 lines
drivers/net/macvtap.c +0 -1
      if (unlikely(len < ETH_HLEN))
              goto err;
 
-     skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len,

vnet_hdr.hdr_len,

Quoted 3 lines
drivers/net/macvtap.c +2 -1
-                             noblock, &err);
+     if (zerocopy)
+             /* There are 256 bytes to be copied in skb, so

there

Quoted 10 lines
Quoted 9 lines

is enough

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q, · line 648

Quoted 4 lines
drivers/net/macvtap.c +4 -0
+              * room for skb expand head in case it is used.
+              * The rest buffer is mapped from userspace.
+              */
+             copylen = GOODCOPY_LEN;

Just curious: where does the number 256 come from? Also, as long as we are copying, should we care about alignment?

Quoted 7 lines
Quoted 3 lines

256 makes the size big enough for any skb head expanding.

That's my concern before.

I'm not sure I understand.
Could you tell me how do we know 256 is big enough for any skb head expanding please?

I meant to make sure in any case all headers will be in skb, not in frags.

Quoted 73 lines
Quoted 2 lines

But guest should alignment the buffer already,
after moving the pointer 256 bytes. It should still alignment,

right?

I mean in the host. But whatever, it's not that important at this point.

Quoted 9 lines
Quoted 9 lines

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q,

Quoted 9 lines
drivers/net/macvtap.c +5 -1
+     else
+             copylen = len;
+
+     skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
+                             vnet_hdr.hdr_len, noblock, &err);
      if (!skb)
              goto err;
 
-     err = skb_copy_datagram_from_iovec(skb, 0, iv,

vnet_hdr_len,

Quoted 3 lines
Quoted 3 lines

len);

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+     if (zerocopy)
+             err = zerocopy_sg_from_iovec(skb, iv,

vnet_hdr_len,

Quoted 16 lines
Quoted 16 lines

count);

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct · line 660

Quoted 2 lines
drivers/net/macvtap.c +2 -0
+     else
+             err = skb_copy_datagram_from_iovec(skb, 0, iv,

vnet_hdr_len,

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct

Quoted 8 lines
drivers/net/macvtap.c +4 -0
+                                                len);
+     if (sock_flag(&q->sk, SOCK_ZEROCOPY))
+             memcpy(&skb_shinfo(skb)->ubuf, m->msg_control,
+                     sizeof(struct skb_ubuf_info));
      if (err)
              goto err_kfree;
 
@@ -579,7 +683,7 @@ static ssize_t macvtap_get_user(struct

macvtap_queue *q,

Quoted 3 lines
drivers/net/macvtap.c +0 -0
              kfree_skb(skb);
      rcu_read_unlock_bh();
 
Quoted 6 lines
Quoted 6 lines

drivers/net/macvtap.c static ssize_t macvtap_aio_write(struct

Quoted 6 lines
drivers/net/macvtap.c +1 -1
-     return count;
+     return total_len;
 
 err_kfree:
      kfree_skb(skb);
@@ -601,8 +705,8 @@ static ssize_t macvtap_aio_write(struct

kiocb

Quoted 7 lines
Quoted 7 lines

*iocb, const struct iovec *iv,

Quoted 6 lines
drivers/net/macvtap.c +1 -2
      ssize_t result = -ENOLINK;
      struct macvtap_queue *q = file->private_data;
 
-     result = macvtap_get_user(q, iv, iov_length(iv, count),
-                           file->f_flags & O_NONBLOCK);
+     result = macvtap_get_user(q, NULL, iv, iov_length(iv,

count),

Quoted 6 lines
Quoted 6 lines

count,

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb

Quoted 5 lines
drivers/net/macvtap.c +1 -0
+                               file->f_flags & O_NONBLOCK);
      return result;
 }
 
@@ -815,7 +919,7 @@ static int macvtap_sendmsg(struct kiocb

*iocb,

Quoted 14 lines
Quoted 14 lines

struct socket *sock,

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,

Quoted 3 lines
drivers/net/macvtap.c +0 -0
                         struct msghdr *m, size_t total_len)
 {
      struct macvtap_queue *q = container_of(sock, struct

macvtap_queue, sock);

drivers/net/macvtap.c static int macvtap_sendmsg(struct kiocb · line 922

Quoted 2 lines
drivers/net/macvtap.c +1 -1
-     return macvtap_get_user(q, m->msg_iov, total_len,
+     return macvtap_get_user(q, m, m->msg_iov, total_len,

m->msg_iovlen,

Quoted 4 lines
drivers/net/macvtap.c +0 -0
                          m->msg_flags & MSG_DONTWAIT);
 }
 
#5 lore raw
In reply to Shirley Ma
Quoted 29 lines · Shirley Ma
Quoted 15 lines · Michael S. Tsirkin
Quoted 4 lines · Shirley Ma

Only when buffer size is greater than GOODCOPY_LEN (256), macvtap enables zero-copy.

Looks good. Some thoughts below.

Quoted 3 lines

---

 drivers/net/macvtap.c |  126

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

Quoted 2 lines
 1 files changed, 115 insertions(+), 11 deletions(-)
Quoted 11 lines

drivers/net/macvtap.c static struct proto macvtap_proto = { · line 63

Quoted 9 lines
drivers/net/macvtap.c +1 -0
index 6696e56..e8bc5ff 100644
@@ -60,6 +60,7 @@ static struct proto macvtap_proto = {
  */
 static dev_t macvtap_major;
 #define MACVTAP_NUM_DEVS 65536
+#define GOODCOPY_LEN 256

Scope with MACVTAP_ please.

Ok.

Quoted 7 lines
Quoted 4 lines

For small packets, is it better to copy in vhost and skip all the back and forth with callbacks? If yes, does it make sense to put the constant above in some header shared with vhost-net?

skb is created in macvtap, the small packet copy is in skb, so I don't think we can do it in vhost here.

BTW this is not very important, it might or might not result in some speedup. Let's focus on getting it working right.

Signature · 1 line
MST

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#7 lore raw
Quoted 34 lines · Sridhar Samudrala
Quoted 33 lines · Shirley Ma

Only when buffer size is greater than GOODCOPY_LEN (256), macvtap enables zero-copy.

---

 drivers/net/macvtap.c |  126 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 115 insertions(+), 11 deletions(-)
drivers/net/macvtap.c +2 -0
index 6696e56..e8bc5ff 100644
@@ -60,6 +60,7 @@ static struct proto macvtap_proto = {
  */
 static dev_t macvtap_major;
 #define MACVTAP_NUM_DEVS 65536
+#define GOODCOPY_LEN 256
 static struct class *macvtap_class;
 static struct cdev macvtap_cdev;
 
@@ -340,6 +341,7 @@ static int macvtap_open(struct inode *inode, struct file *file)
 {
 	struct net *net = current->nsproxy->net_ns;
 	struct net_device *dev = dev_get_by_index(net, iminor(inode));
+	struct macvlan_dev *vlan = netdev_priv(dev);
 	struct macvtap_queue *q;
 	int err;
 
@@ -369,6 +371,16 @@ static int macvtap_open(struct inode *inode, struct file *file)
 	q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
 	q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
 
Quoted 8 lines

drivers/net/macvtap.c static int macvtap_open(struct inode *inode, struct file *file) · line 378

Quoted 5 lines
drivers/net/macvtap.c +5 -0
+	/*
+	 * so far only VM uses macvtap, enable zero copy between guest
+	 * kernel and host kernel when lower device supports high memory
+	 * DMA
+	 */

Instead of VM uses macvtap, it makes it more clear to say 'KVM virtio uses macvtap for guest networking'.
Also you are checking if lower device allows zerocopy, not high dma.

Sure.

Quoted 9 lines

drivers/net/macvtap.c static int macvtap_open(struct inode *inode, struct file *file) · line 383

Quoted 6 lines
drivers/net/macvtap.c +5 -0
+	if (vlan) {
+		if (vlan->lowerdev->features & NETIF_F_ZEROCOPY)
+			sock_set_flag(&q->sk, SOCK_ZEROCOPY);
+	}
+

Do we need to check for vlan? If required, both if conditions can be combined.

Yes, I will run more test before enabling it.

Quoted 33 lines
Quoted 33 lines
drivers/net/macvtap.c +26 -0
 	err = macvtap_set_queue(dev, file, q);
 	if (err)
 		sock_put(&q->sk);
@@ -433,6 +445,80 @@ static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
 	return skb;
 }
 
+/* set skb frags from iovec, this can move to core network code for reuse */
+static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
+				  int offset, size_t count)
+{
+	int len = iov_length(from, count) - offset;
+	int copy = skb_headlen(skb);
+	int size, offset1 = 0;
+	int i = 0;
+	skb_frag_t *f;
+
+	/* Skip over from offset */
+	while (offset >= from->iov_len) {
+		offset -= from->iov_len;
+		++from;
+		--count;
+	}
+
+	/* copy up to skb headlen */
+	while (copy > 0) {
+		size = min_t(unsigned int, copy, from->iov_len - offset);
+		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
+				   size))
+			return -EFAULT;
+		if (copy > size) {
+			++from;
+			--count;
Quoted 8 lines

drivers/net/macvtap.c static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad, · line 478

Quoted 5 lines
drivers/net/macvtap.c +5 -0
+		}
+		copy -= size;
+		offset1 += size;
+		offset = 0;
+	}

I think you need to check for count reaching zero in the above 2 while loops.

Doesn't hurt to check count here, in this case, caller has copylen/len/offset less than count/len already.

Quoted 58 lines
Quoted 58 lines
drivers/net/macvtap.c +46 -3
+
+	if (len == offset1)
+		return 0;
+
+	while (count--) {
+		struct page *page[MAX_SKB_FRAGS];
+		int num_pages;
+		unsigned long base;
+
+		len = from->iov_len - offset1;
+		if (!len) {
+			offset1 = 0;
+			++from;
+			continue;
+		}
+		base = (unsigned long)from->iov_base + offset1;
+		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
+		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
+		if ((num_pages != size) ||
+		    (num_pages > MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags))
+			/* put_page is in skb free */
+			return -EFAULT;
+		skb->data_len += len;
+		skb->len += len;
+		skb->truesize += len;
+		while (len) {
+			f = &skb_shinfo(skb)->frags[i];
+			f->page = page[i];
+			f->page_offset = base & ~PAGE_MASK;
+			f->size = min_t(int, len, PAGE_SIZE - f->page_offset);
+			skb_shinfo(skb)->nr_frags++;
+			/* increase sk_wmem_alloc */
+			atomic_add(f->size, &skb->sk->sk_wmem_alloc);
+			base += f->size;
+			len -= f->size;
+			i++;
+		}
+		offset1 = 0;
+		++from;
+	}
+	return 0;
+}
+
 /*
  * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  * be shared with the tun/tap driver.
@@ -515,17 +601,19 @@ static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
 
 
 /* Get packet from user space buffer */
-static ssize_t macvtap_get_user(struct macvtap_queue *q,
-				const struct iovec *iv, size_t count,
-				int noblock)
+static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
+				const struct iovec *iv, unsigned long total_len,
+				size_t count, int noblock)
 {
 	struct sk_buff *skb;
Quoted 8 lines

drivers/net/macvtap.c static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb, · line 614

Quoted 7 lines
drivers/net/macvtap.c +2 -1
 	struct macvlan_dev *vlan;
-	size_t len = count;
+	unsigned long len = total_len;
 	int err;
 	struct virtio_net_hdr vnet_hdr = { 0 };
 	int vnet_hdr_len = 0;
+	int copylen, zerocopy;

zerocopy can be boolean

Yes, I have changed it in V5.

Quoted 24 lines
Quoted 24 lines
drivers/net/macvtap.c +12 -2
+	zerocopy = sock_flag(&q->sk, SOCK_ZEROCOPY) && (len > GOODCOPY_LEN);
 	if (q->flags & IFF_VNET_HDR) {
 		vnet_hdr_len = q->vnet_hdr_sz;
 
@@ -552,12 +640,28 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
 	if (unlikely(len < ETH_HLEN))
 		goto err;
 
-	skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len, vnet_hdr.hdr_len,
-				noblock, &err);
+	if (zerocopy)
+		/* There are 256 bytes to be copied in skb, so there is enough
+		 * room for skb expand head in case it is used.
+		 * The rest buffer is mapped from userspace.
+		 */
+		copylen = GOODCOPY_LEN;
+	else
+		copylen = len;
+
+	skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
+				vnet_hdr.hdr_len, noblock, &err);
 	if (!skb)
 		goto err;
 
Quoted 11 lines

drivers/net/macvtap.c static ssize_t macvtap_get_user(struct macvtap_queue *q, · line 664

Quoted 9 lines
drivers/net/macvtap.c +8 -1
-	err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len, len);
+	if (zerocopy)
+		err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
+	else
+		err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
+						   len);
+	if (sock_flag(&q->sk, SOCK_ZEROCOPY))
+		memcpy(&skb_shinfo(skb)->ubuf, m->msg_control,
+			sizeof(struct skb_ubuf_info));

Why are you doing a separate check for this memcpy? Is this required if the len < 256?

Yes. But I have changed it V5 based on MST's suggestion to leave copied buffer maintain in vhost.

Quoted 35 lines
Quoted 34 lines
drivers/net/macvtap.c +4 -4
 	if (err)
 		goto err_kfree;
 
@@ -579,7 +683,7 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
 		kfree_skb(skb);
 	rcu_read_unlock_bh();
 
-	return count;
+	return total_len;
 
 err_kfree:
 	kfree_skb(skb);
@@ -601,8 +705,8 @@ static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
 	ssize_t result = -ENOLINK;
 	struct macvtap_queue *q = file->private_data;
 
-	result = macvtap_get_user(q, iv, iov_length(iv, count),
-			      file->f_flags & O_NONBLOCK);
+	result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
+				  file->f_flags & O_NONBLOCK);
 	return result;
 }
 
@@ -815,7 +919,7 @@ static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
 			   struct msghdr *m, size_t total_len)
 {
 	struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
-	return macvtap_get_user(q, m->msg_iov, total_len,
+	return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
 			    m->msg_flags & MSG_DONTWAIT);
 }