Skip to contentSkip to conversation

Open vSwitch zerocopy upcall

PATCH New Thomas Graf 4 participants 6 messages Nov 8, 2013 – Nov 11, 2013 2 messages not in the archive

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover Open vSwitch zerocopy upcall

Respin of the zerocopy patches for the openvswitch upcall.

V3: - Removed unneeded alignment of nlmsg_len after padding
V2: - Added skb_zerocopy_headlen() to calculate headroom of destination
buffer. This also takes care of the from->head_frag issue.
- Attribute alignment for frag_list case
- API documentation
- performance data for CHECKSUM_PARTIAL tx case

Thomas Graf (2):
net: Export skb_zerocopy() to zerocopy from one skb to another
openvswitch: Use skb_zerocopy() for upcall

 include/linux/skbuff.h               |  3 ++
 net/core/skbuff.c                    | 85 ++++++++++++++++++++++++++++++++++++
 net/netfilter/nfnetlink_queue_core.c | 59 ++-----------------------
 net/openvswitch/datapath.c           | 52 +++++++++++++++++++---
 4 files changed, 137 insertions(+), 62 deletions(-)
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v3 1/2 net: Export skb_zerocopy() to zerocopy from one skb to another

In reply to a message not in the archive

Make the skb zerocopy logic written for nfnetlink queue available for use by other modules.

---
 include/linux/skbuff.h               |  3 ++
 net/core/skbuff.c                    | 85 ++++++++++++++++++++++++++++++++++++
 net/netfilter/nfnetlink_queue_core.c | 59 ++-----------------------
 3 files changed, 92 insertions(+), 55 deletions(-)
include/linux/skbuff.h +3 -0
index 2e153b6..bf26616 100644
@@ -2371,6 +2371,9 @@ int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
 		    struct pipe_inode_info *pipe, unsigned int len,
 		    unsigned int flags);
 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
+extern unsigned int    skb_zerocopy_headlen(const struct sk_buff *from);
+extern void	       skb_zerocopy(struct sk_buff *to, const struct sk_buff *from,
+				    int len, int hlen);
 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len);
 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen);
 void skb_scrub_packet(struct sk_buff *skb, bool xnet);
net/core/skbuff.c +85 -0

91 lines not on this page · raw message

net/netfilter/nfnetlink_queue_core.c +4 -55

83 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 2/2 openvswitch: Use skb_zerocopy() for upcall

In reply to Thomas Graf

Use of skb_zerocopy() avoids the expensive call to memcpy() when copying the packet data into the Netlink skb. Completes checksum through skb_checksum_help() if needed.

Netlink messaged must be properly padded and aligned to meet sanity checks of the user space counterpart.

Cost of memcpy is significantly reduced from:

+   7.48%       vhost-8471  [k] memcpy
+   5.57%     ovs-vswitchd  [k] memcpy
+   2.81%       vhost-8471  [k] csum_partial_copy_generic

to:

+   5.72%     ovs-vswitchd  [k] memcpy
+   3.32%       vhost-5153  [k] memcpy
+   0.68%       vhost-5153  [k] skb_zerocopy

(megaflows disabled)

---
 net/openvswitch/datapath.c | 52 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 7 deletions(-)
net/openvswitch/datapath.c +45 -7
index 1408adc..3f170e3 100644
@@ -381,10 +381,11 @@ static size_t key_attr_size(void)
 }
 
 static size_t upcall_msg_size(const struct sk_buff *skb,
-			      const struct nlattr *userdata)
+			      const struct nlattr *userdata,
+			      unsigned int hdrlen)
 {
 	size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
-		+ nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
+		+ nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
 		+ nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
 
 	/* OVS_PACKET_ATTR_USERDATA */
@@ -402,6 +403,7 @@ static int queue_userspace_packet(struct net *net, int dp_ifindex,
 	struct sk_buff *nskb = NULL;
 	struct sk_buff *user_skb; /* to be queued to userspace */
 	struct nlattr *nla;
+	unsigned int plen, hlen;
 	int err;
 
 	if (vlan_tx_tag_present(skb)) {
@@ -422,7 +424,13 @@ static int queue_userspace_packet(struct net *net, int dp_ifindex,
 		goto out;
 	}
 
-	user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
+	/* Complete checksum if needed */
+	if (skb->ip_summed == CHECKSUM_PARTIAL &&
+	    (err = skb_checksum_help(skb)))
+		goto out;
+
+	hlen = skb_zerocopy_headlen(skb);
+	user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata, hlen), GFP_ATOMIC);
 	if (!user_skb) {
 		err = -ENOMEM;
 		goto out;
@@ -441,13 +449,43 @@ static int queue_userspace_packet(struct net *net, int dp_ifindex,
 			  nla_len(upcall_info->userdata),
 			  nla_data(upcall_info->userdata));
 
-	nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
+	/* Only reserve room for attribute header, packet data is added
+	 * in skb_zerocopy() */
+	if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0)))
+		goto out;
+	nla->nla_len = nla_attr_size(skb->len);
 
-	skb_copy_and_csum_dev(skb, nla_data(nla));
+	skb_zerocopy(user_skb, skb, skb->len, hlen);
 
-	genlmsg_end(user_skb, upcall);
-	err = genlmsg_unicast(net, user_skb, upcall_info->portid);
+	/* OVS user space expects the size of the message to be aligned to
+	 * NLA_ALIGNTO. Aligning nlmsg_len is not enough, the actual bytes
+	 * read must match nlmsg_len.
+	 */
+	plen = NLA_ALIGN(user_skb->len) - user_skb->len;
+	if (plen > 0) {
+		int nr_frags = skb_shinfo(user_skb)->nr_frags;
+
+		if (nr_frags) {
+			skb_frag_t *frag;
+
+			frag = &skb_shinfo(user_skb)->frags[nr_frags -1];
+			skb_frag_size_add(frag, plen);
+			BUG_ON(frag->size > PAGE_SIZE);
+
+			user_skb->truesize += plen;
+			user_skb->len += plen;
+			user_skb->data_len += plen;
+		} else {
+			/* The linear headroom is aligned to NLMSG_ALIGN by
+			 * genlmsg_new(), room must be available.
+			 */
+			memset(skb_put(user_skb, plen), 0, plen);
+		}
+	}
 
+	((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
+
+	err = genlmsg_unicast(net, user_skb, upcall_info->portid);
 out:
 	kfree_skb(nskb);
 	return err;
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [PATCH 2/2 net-next] openvswitch: Use skb_zerocopy() for upcall

Quoted 28 lines · Thomas Graf

Use of skb_zerocopy() avoids the expensive call to memcpy() when copying the packet data into the Netlink skb. Completes checksum through skb_checksum_help() if needed.

Netlink messaged must be properly padded and aligned to meet sanity checks of the user space counterpart.

Cost of memcpy is significantly reduced from:

(quoted diff) +3 -0
+   7.48%       vhost-8471  [k] memcpy
+   5.57%     ovs-vswitchd  [k] memcpy
+   2.81%       vhost-8471  [k] csum_partial_copy_generic

to:

(quoted diff) +3 -0
+   5.72%     ovs-vswitchd  [k] memcpy
+   3.32%       vhost-5153  [k] memcpy
+   0.68%       vhost-5153  [k] skb_zerocopy

(megaflows disabled)

---
 net/openvswitch/datapath.c | 52 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 7 deletions(-)
net/openvswitch/datapath.c +0 -0
index 1408adc..3f170e3 100644

[...]

Quoted 19 lines
net/openvswitch/datapath.c +9 -4
@@ -441,13 +449,43 @@ static int queue_userspace_packet(struct net *net, int dp_ifindex,
 			  nla_len(upcall_info->userdata),
 			  nla_data(upcall_info->userdata));
 
-	nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
+	/* Only reserve room for attribute header, packet data is added
+	 * in skb_zerocopy() */
+	if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0)))
+		goto out;
+	nla->nla_len = nla_attr_size(skb->len);
 
-	skb_copy_and_csum_dev(skb, nla_data(nla));
+	skb_zerocopy(user_skb, skb, skb->len, hlen);
 
-	genlmsg_end(user_skb, upcall);
-	err = genlmsg_unicast(net, user_skb, upcall_info->portid);
+	/* OVS user space expects the size of the message to be aligned to
+	 * NLA_ALIGNTO. Aligning nlmsg_len is not enough, the actual bytes
+	 * read must match nlmsg_len.

net/openvswitch/datapath.c static int queue_userspace_packet(struct net *net, int dp_ifindex, · line 472

Quoted 10 lines
net/openvswitch/datapath.c +10 -0
+	 */
+	plen = NLA_ALIGN(user_skb->len) - user_skb->len;
+	if (plen > 0) {
+		int nr_frags = skb_shinfo(user_skb)->nr_frags;
+
+		if (nr_frags) {
+			skb_frag_t *frag;
+
+			frag = &skb_shinfo(user_skb)->frags[nr_frags -1];
+			skb_frag_size_add(frag, plen);

It looks like this is effectively padding with whatever happens to follow the original packet content. This could result in a small information leak. If the fragment has non-zero offset and already extends to the end of a page, this could result in a segfault as the next page may be unmapped.

Perhaps you could add the padding as an extra fragment pointing to a preallocated zero page. If the skb already has the maximum number of fragments, you would have to copy the last fragment in order to add padding.

net/openvswitch/datapath.c static int queue_userspace_packet(struct net *net, int dp_ifindex, · line 473

Quoted 1 line
net/openvswitch/datapath.c +1 -0
+			BUG_ON(frag->size > PAGE_SIZE);

[...]

I'm not sure that's a reasonable assumption either. We certainly allow fragments to be larger than PAGE_SIZE in the transmit path.

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
#6 lore raw

Re: [PATCH 2/2 net-next] openvswitch: Use skb_zerocopy() for upcall

In reply to a message not in the archive
Quoted 66 lines · Ben Hutchings
Quoted 28 lines · Thomas Graf

Use of skb_zerocopy() avoids the expensive call to memcpy() when copying the packet data into the Netlink skb. Completes checksum through skb_checksum_help() if needed.

Netlink messaged must be properly padded and aligned to meet sanity checks of the user space counterpart.

Cost of memcpy is significantly reduced from:

(quoted diff) +3 -0
+   7.48%       vhost-8471  [k] memcpy
+   5.57%     ovs-vswitchd  [k] memcpy
+   2.81%       vhost-8471  [k] csum_partial_copy_generic

to:

(quoted diff) +3 -0
+   5.72%     ovs-vswitchd  [k] memcpy
+   3.32%       vhost-5153  [k] memcpy
+   0.68%       vhost-5153  [k] skb_zerocopy

(megaflows disabled)

---
 net/openvswitch/datapath.c | 52 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 7 deletions(-)
net/openvswitch/datapath.c +0 -0
index 1408adc..3f170e3 100644

[...]

Quoted 19 lines
net/openvswitch/datapath.c +9 -4
@@ -441,13 +449,43 @@ static int queue_userspace_packet(struct net *net, int dp_ifindex,
 			  nla_len(upcall_info->userdata),
 			  nla_data(upcall_info->userdata));
 
-	nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
+	/* Only reserve room for attribute header, packet data is added
+	 * in skb_zerocopy() */
+	if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0)))
+		goto out;
+	nla->nla_len = nla_attr_size(skb->len);
 
-	skb_copy_and_csum_dev(skb, nla_data(nla));
+	skb_zerocopy(user_skb, skb, skb->len, hlen);
 
-	genlmsg_end(user_skb, upcall);
-	err = genlmsg_unicast(net, user_skb, upcall_info->portid);
+	/* OVS user space expects the size of the message to be aligned to
+	 * NLA_ALIGNTO. Aligning nlmsg_len is not enough, the actual bytes
+	 * read must match nlmsg_len.

net/openvswitch/datapath.c static int queue_userspace_packet(struct net *net, int dp_ifindex, · line 472

Quoted 10 lines
net/openvswitch/datapath.c +10 -0
+	 */
+	plen = NLA_ALIGN(user_skb->len) - user_skb->len;
+	if (plen > 0) {
+		int nr_frags = skb_shinfo(user_skb)->nr_frags;
+
+		if (nr_frags) {
+			skb_frag_t *frag;
+
+			frag = &skb_shinfo(user_skb)->frags[nr_frags -1];
+			skb_frag_size_add(frag, plen);

It looks like this is effectively padding with whatever happens to follow the original packet content. This could result in a small information leak. If the fragment has non-zero offset and already extends to the end of a page, this could result in a segfault as the next page may be unmapped.

Quoted 4 lines

Perhaps you could add the padding as an extra fragment pointing to a preallocated zero page. If the skb already has the maximum number of fragments, you would have to copy the last fragment in order to add padding.

You are right and thanks for the review Ben.

Realizing how complex this becomes I'm leaning towards avoiding padding alltogether by fixing OVS user space to no longer enforce it, signal this capability via a flag to the kernel and only perform zerocopy for enabled OVS user space counterparts.

#7 lore raw

Re: [PATCH 2/2 net-next] openvswitch: Use skb_zerocopy() for upcall

Quoted 72 lines · Thomas Graf
Quoted 66 lines · Ben Hutchings
Quoted 28 lines · Thomas Graf

Use of skb_zerocopy() avoids the expensive call to memcpy() when copying the packet data into the Netlink skb. Completes checksum through skb_checksum_help() if needed.

Netlink messaged must be properly padded and aligned to meet sanity checks of the user space counterpart.

Cost of memcpy is significantly reduced from:

(quoted diff) +3 -0
+   7.48%       vhost-8471  [k] memcpy
+   5.57%     ovs-vswitchd  [k] memcpy
+   2.81%       vhost-8471  [k] csum_partial_copy_generic

to:

(quoted diff) +3 -0
+   5.72%     ovs-vswitchd  [k] memcpy
+   3.32%       vhost-5153  [k] memcpy
+   0.68%       vhost-5153  [k] skb_zerocopy

(megaflows disabled)

---
 net/openvswitch/datapath.c | 52 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 7 deletions(-)
net/openvswitch/datapath.c +0 -0
index 1408adc..3f170e3 100644

[...]

Quoted 19 lines
net/openvswitch/datapath.c +9 -4
@@ -441,13 +449,43 @@ static int queue_userspace_packet(struct net *net, int dp_ifindex,
                      nla_len(upcall_info->userdata),
                      nla_data(upcall_info->userdata));
 
-   nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
+   /* Only reserve room for attribute header, packet data is added
+    * in skb_zerocopy() */
+   if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0)))
+           goto out;
+   nla->nla_len = nla_attr_size(skb->len);
 
-   skb_copy_and_csum_dev(skb, nla_data(nla));
+   skb_zerocopy(user_skb, skb, skb->len, hlen);
 
-   genlmsg_end(user_skb, upcall);
-   err = genlmsg_unicast(net, user_skb, upcall_info->portid);
+   /* OVS user space expects the size of the message to be aligned to
+    * NLA_ALIGNTO. Aligning nlmsg_len is not enough, the actual bytes
+    * read must match nlmsg_len.

net/openvswitch/datapath.c static int queue_userspace_packet(struct net *net, int dp_ifindex, · line 472

Quoted 10 lines
net/openvswitch/datapath.c +10 -0
+    */
+   plen = NLA_ALIGN(user_skb->len) - user_skb->len;
+   if (plen > 0) {
+           int nr_frags = skb_shinfo(user_skb)->nr_frags;
+
+           if (nr_frags) {
+                   skb_frag_t *frag;
+
+                   frag = &skb_shinfo(user_skb)->frags[nr_frags -1];
+                   skb_frag_size_add(frag, plen);

It looks like this is effectively padding with whatever happens to follow the original packet content. This could result in a small information leak. If the fragment has non-zero offset and already extends to the end of a page, this could result in a segfault as the next page may be unmapped.

Quoted 4 lines

Perhaps you could add the padding as an extra fragment pointing to a preallocated zero page. If the skb already has the maximum number of fragments, you would have to copy the last fragment in order to add padding.

Quoted 6 lines

You are right and thanks for the review Ben.

Realizing how complex this becomes I'm leaning towards avoiding padding alltogether by fixing OVS user space to no longer enforce it, signal this capability via a flag to the kernel and only perform zerocopy for enabled OVS user space counterparts.

It seems like at a minimum it would be a good idea to start by patching userspace now. That would at least begin to limit the scope of the problem.