Skip to contentSkip to conversation

tun: trim per-packet work on the write path

PATCH New Antonio Quartulli 1 participant 4 messages Nov 27

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

v3 cover tun: trim per-packet work on the write path

Profiling a link-bonding daemon that writes every packet to a tun device showed tun doing per-packet work its packets do not need:

- patch 1: a BH-disabled RCU section, only to check for tun's XDP program, which only TAP devices can have
- patch 3: the parsing of an all-zero virtio-net header without IFF_VNET_HDR

Patch 2 reads tun->flags once in tun_get_user(), so that patch 3 and the existing checks all see the same flags. The other paths read each flag only once and are left as they are.

Together they cut the instructions per 1400-byte UDP packet written to a TUN device by 3.3% when an iptables raw-table rule drops it on ingress, and by 1.9% when it is forwarded to a dummy device. Counted under qemu -icount on x86_64, with ERMS/FSRM hidden so that user copies are not counted per byte; the counts include the syscall, so tun's own share of the saving is larger. The tun selftests pass.

v3:
- Add patch 2, reading tun->flags once in tun_get_user(), and base the vnet header check on it (Willem)

v2: https://lore.kernel.org/netdev/[email protected]/
- Drop the patch that stopped probing the transport header: tc ematches and generic XDP rely on it (Sashiko)
- Rename the XDP check variable, which shadowed another one (Sashiko)
- Patch 2: adapt to the has_eth argument tun_vnet_hdr_tnl_to_skb() gained in 73e08d50eb13

v1: https://lore.kernel.org/netdev/[email protected]/

Antonio Quartulli (3):
tun: don't disable BHs for the XDP program check of every packet
tun: read tun->flags once in tun_get_user()
tun: skip the virtio-net header parsing without IFF_VNET_HDR

 drivers/net/tun.c | 54 +++++++++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 21 deletions(-)

base-commit: f89726ffabef1b08c52e6163b7ab94b0d5f15503

Signature · 1 line
2.56.1
#1 lore raw

PATCH v3 1/3 tun: don't disable BHs for the XDP program check of every packet

For every packet, tun_get_user() disables BHs and enters an RCU section only to find out whether tun's XDP program is attached, which only TAP devices can have. tun_build_skb() takes the RCU read lock for the same NULL test.

Test the pointer with rcu_access_pointer() instead, and enter the BH-disabled RCU section in tun_get_user() only with a program attached. A program attached concurrently could already miss the packets in flight.

1.2% fewer instructions per 1400-byte UDP packet written and dropped at the raw table, 0.7% fewer when forwarded (qemu -icount, x86_64).

---
 drivers/net/tun.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/net/tun.c +8 -6
index 7894dbdf0369..e2fe7174e438 100644
@@ -1721,14 +1721,13 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 	size_t copied;
 	int pad = TUN_RX_PAD;
 	int metasize = 0;
+	bool has_xdp;
 	int err = 0;
 
-	rcu_read_lock();
-	xdp_prog = rcu_dereference(tun->xdp_prog);
-	if (xdp_prog)
+	has_xdp = rcu_access_pointer(tun->xdp_prog);
+	if (has_xdp)
 		pad += XDP_PACKET_HEADROOM;
 	buflen += SKB_DATA_ALIGN(len + pad);
-	rcu_read_unlock();
 
 	alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
 	if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
@@ -1745,7 +1744,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 	 * of xdp_prog above, this should be rare and for simplicity
 	 * we do XDP on skb in case the headroom is not enough.
 	 */
-	if (hdr->gso_type || !xdp_prog) {
+	if (hdr->gso_type || !has_xdp) {
 		*skb_xdp = 1;
 		return __tun_build_skb(tfile, alloc_frag, buf, buflen, len,
 				       pad, metasize);
@@ -1988,7 +1987,10 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	skb_probe_transport_header(skb);
 	skb_record_rx_queue(skb, tfile->queue_index);
 
-	if (skb_xdp) {
+	/* Don't disable BHs and enter an RCU section for every packet only
+	 * to find that no program is attached, the common case.
+	 */
+	if (skb_xdp && rcu_access_pointer(tun->xdp_prog)) {
 		struct bpf_prog *xdp_prog;
 		int ret;
 
Signature · 1 line
2.56.1
#2 lore raw

PATCH v3 2/3 tun: read tun->flags once in tun_get_user()

In reply to Antonio Quartulli

tun_get_user() reads tun->flags several times. tun_set_iff() can still rewrite them after attaching a queue, and TUNSETVNETLE/BE can change the vnet header byte order at any time, so a packet being written can see a flag change halfway through: the PI header consumed under one IFF_NO_PI and interpreted under the other, or the vnet header validated in one byte order and applied to the skb in the other.

Read the flags once, pairing with the WRITE_ONCE() of their writers, and use that copy throughout, including for the build_skb eligibility check.

---
 drivers/net/tun.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
drivers/net/tun.c +15 -12
index e2fe7174e438..09f31faaced2 100644
@@ -1625,10 +1625,10 @@ static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
 	}
 }
 
-static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
+static bool tun_can_build_skb(unsigned int flags, struct tun_file *tfile,
 			      int len, int noblock, bool zerocopy)
 {
-	if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
+	if ((flags & TUN_TYPE_MASK) != IFF_TAP)
 		return false;
 
 	if (tfile->socket.sk->sk_sndbuf != INT_MAX)
@@ -1823,6 +1823,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	bool frags = tun_napi_frags_enabled(tfile);
 	enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
 	netdev_features_t features = 0;
+	unsigned int flags;
+
+	flags = READ_ONCE(tun->flags);
 
 	/*
 	 * Keep it easy and always zero the whole buffer, even if the
@@ -1832,7 +1835,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	memset(&hdr, 0, sizeof(hdr));
 	gso = (struct virtio_net_hdr *)&hdr;
 
-	if (!(tun->flags & IFF_NO_PI)) {
+	if (!(flags & IFF_NO_PI)) {
 		if (len < sizeof(pi))
 			return -EINVAL;
 		len -= sizeof(pi);
@@ -1841,11 +1844,11 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 			return -EFAULT;
 	}
 
-	if (tun->flags & IFF_VNET_HDR) {
+	if (flags & IFF_VNET_HDR) {
 		int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
 
 		features = tun_vnet_hdr_guest_features(vnet_hdr_sz);
-		hdr_len = __tun_vnet_hdr_get(vnet_hdr_sz, tun->flags,
+		hdr_len = __tun_vnet_hdr_get(vnet_hdr_sz, flags,
 					     features, from, gso);
 		if (hdr_len < 0)
 			return hdr_len;
@@ -1853,7 +1856,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 		len -= vnet_hdr_sz;
 	}
 
-	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
+	if ((flags & TUN_TYPE_MASK) == IFF_TAP) {
 		align += NET_IP_ALIGN;
 		if (unlikely(len < ETH_HLEN || (hdr_len && hdr_len < ETH_HLEN)))
 			return -EINVAL;
@@ -1875,7 +1878,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 			zerocopy = true;
 	}
 
-	if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
+	if (!frags && tun_can_build_skb(flags, tfile, len, noblock, zerocopy)) {
 		/* For the packet that is not easy to be processed
 		 * (e.g gso or jumbo packet), we will do it at after
 		 * skb was created with generic XDP routine.
@@ -1929,9 +1932,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	}
 
 	skb->dev = tun->dev;
-	switch (tun->flags & TUN_TYPE_MASK) {
+	switch (flags & TUN_TYPE_MASK) {
 	case IFF_TUN:
-		if (tun->flags & IFF_NO_PI) {
+		if (flags & IFF_NO_PI) {
 			u8 ip_version;
 
 			drop_reason = pskb_may_pull_reason(skb, 1);
@@ -1967,14 +1970,14 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 		break;
 	}
 
-	if (tun_vnet_hdr_tnl_to_skb(tun->flags, features, skb, &hdr,
-				    (tun->flags & TUN_TYPE_MASK) == IFF_TAP)) {
+	if (tun_vnet_hdr_tnl_to_skb(flags, features, skb, &hdr,
+				    (flags & TUN_TYPE_MASK) == IFF_TAP)) {
 		atomic_long_inc(&tun->rx_frame_errors);
 		err = -EINVAL;
 		goto free_skb;
 	}
 
-	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP)
+	if ((flags & TUN_TYPE_MASK) == IFF_TAP)
 		skb->protocol = eth_type_trans(skb, tun->dev);
 
 	/* copy skb_ubuf_info for callback when skb has no error */
Signature · 1 line
2.56.1
#3 lore raw

PATCH v3 3/3 tun: skip the virtio-net header parsing without IFF_VNET_HDR

In reply to Antonio Quartulli

Without IFF_VNET_HDR, tun_get_user() still passes an all-zero header to tun_vnet_hdr_tnl_to_skb(), which then only resets the MAC header. The parser is not inlined, so that costs two nested calls per packet. Reset the MAC header directly, as tap_get_user() already does. The check uses the same copy of the flags that decided whether the header was read.

1.9% fewer instructions per 1400-byte UDP packet written and dropped at the raw table, 1.1% fewer when forwarded (qemu -icount, x86_64).

---
 drivers/net/tun.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/net/tun.c +12 -5
index 09f31faaced2..a644fab4321f 100644
@@ -1970,11 +1970,18 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 		break;
 	}
 
-	if (tun_vnet_hdr_tnl_to_skb(flags, features, skb, &hdr,
-				    (flags & TUN_TYPE_MASK) == IFF_TAP)) {
-		atomic_long_inc(&tun->rx_frame_errors);
-		err = -EINVAL;
-		goto free_skb;
+	if (flags & IFF_VNET_HDR) {
+		bool has_eth = (flags & TUN_TYPE_MASK) == IFF_TAP;
+
+		if (tun_vnet_hdr_tnl_to_skb(flags, features, skb, &hdr,
+					    has_eth)) {
+			atomic_long_inc(&tun->rx_frame_errors);
+			err = -EINVAL;
+			goto free_skb;
+		}
+	} else {
+		/* a zero hdr would only reset the MAC header */
+		skb_reset_mac_header(skb);
 	}
 
 	if ((flags & TUN_TYPE_MASK) == IFF_TAP)
Signature · 1 line
2.56.1