Skip to contentSkip to conversation

xdp: add xdp_skb_reserve_put helper

PATCH In review Jon Kohler 3 participants 6 messages Apr 30, 2025

Subsystems (5)
Networking Network drivers BPF XDP +1 more subsystems
lore
Share & tools
lore

Jon Kohler

author
#0 lore raw

PATCH 1/1 xdp: add xdp_skb_reserve_put helper

Add helper for calling skb_{put|reserve} to reduce repetitive pattern across various drivers.

Plumb into tap and tun to start.

No functional change intended.

---
 drivers/net/tap.c | 3 +--
 drivers/net/tun.c | 3 +--
 include/net/xdp.h | 8 ++++++++
 net/core/xdp.c    | 3 +--
 4 files changed, 11 insertions(+), 6 deletions(-)
drivers/net/tap.c +1 -2
index d4ece538f1b2..54ce492da5e9 100644
@@ -1062,8 +1062,7 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
 		goto err;
 	}
 
-	skb_reserve(skb, xdp->data - xdp->data_hard_start);
-	skb_put(skb, xdp->data_end - xdp->data);
+	xdp_skb_reserve_put(xdp, skb);
 
 	skb_set_network_header(skb, ETH_HLEN);
 	skb_reset_mac_header(skb);
drivers/net/tun.c +1 -2
index 7babd1e9a378..30701ad5c27d 100644
@@ -2415,8 +2415,7 @@ static int tun_xdp_one(struct tun_struct *tun,
 		goto out;
 	}
 
-	skb_reserve(skb, xdp->data - xdp->data_hard_start);
-	skb_put(skb, xdp->data_end - xdp->data);
+	xdp_skb_reserve_put(xdp, skb);
 
 	/* The externally provided xdp_buff may have no metadata support, which
 	 * is marked by xdp->data_meta being xdp->data + 1. This will lead to a
include/net/xdp.h +8 -0
index 48efacbaa35d..0e7414472464 100644
@@ -345,6 +345,14 @@ struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
 					 struct net_device *dev);
 struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
 
+static __always_inline
+void xdp_skb_reserve_put(const struct xdp_buff *xdp,
+			 struct sk_buff *skb)
+{
+	skb_reserve(skb, xdp->data - xdp->data_hard_start);
+	__skb_put(skb, xdp->data_end - xdp->data);
+}
+
 static inline
 void xdp_convert_frame_to_buff(const struct xdp_frame *frame,
 			       struct xdp_buff *xdp)
net/core/xdp.c +1 -2
index f86eedad586a..1fca2aa1d1fe 100644
@@ -646,8 +646,7 @@ struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
 	if (unlikely(!skb))
 		return NULL;
 
-	skb_reserve(skb, xdp->data - xdp->data_hard_start);
-	__skb_put(skb, xdp->data_end - xdp->data);
+	xdp_skb_reserve_put(xdp, skb);
 
 	metalen = xdp->data - xdp->data_meta;
 	if (metalen > 0)
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 7 lines · Jon Kohler

Add helper for calling skb_{put|reserve} to reduce repetitive pattern across various drivers.

Plumb into tap and tun to start.

No functional change intended.

Quoted 7 lines
---
 drivers/net/tap.c | 3 +--
 drivers/net/tun.c | 3 +--
 include/net/xdp.h | 8 ++++++++
 net/core/xdp.c    | 3 +--
 4 files changed, 11 insertions(+), 6 deletions(-)

Subjective, but I prefer the existing code. I understand what skb_reserve and skb_put do. While xdp_skb_reserve_put adds a layer of indirection that I'd have to follow.

Sometimes deduplication makes sense, sometimes the indirection adds more mental load than it's worth. In this case the code savings are small. As said, subjective. Happy to hear other opinions.

Quoted 65 lines
drivers/net/tap.c +1 -2
index d4ece538f1b2..54ce492da5e9 100644
@@ -1062,8 +1062,7 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
 		goto err;
 	}
 
-	skb_reserve(skb, xdp->data - xdp->data_hard_start);
-	skb_put(skb, xdp->data_end - xdp->data);
+	xdp_skb_reserve_put(xdp, skb);
 
 	skb_set_network_header(skb, ETH_HLEN);
 	skb_reset_mac_header(skb);
drivers/net/tun.c +1 -2
index 7babd1e9a378..30701ad5c27d 100644
@@ -2415,8 +2415,7 @@ static int tun_xdp_one(struct tun_struct *tun,
 		goto out;
 	}
 
-	skb_reserve(skb, xdp->data - xdp->data_hard_start);
-	skb_put(skb, xdp->data_end - xdp->data);
+	xdp_skb_reserve_put(xdp, skb);
 
 	/* The externally provided xdp_buff may have no metadata support, which
 	 * is marked by xdp->data_meta being xdp->data + 1. This will lead to a
include/net/xdp.h +8 -0
index 48efacbaa35d..0e7414472464 100644
@@ -345,6 +345,14 @@ struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
 					 struct net_device *dev);
 struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
 
+static __always_inline
+void xdp_skb_reserve_put(const struct xdp_buff *xdp,
+			 struct sk_buff *skb)
+{
+	skb_reserve(skb, xdp->data - xdp->data_hard_start);
+	__skb_put(skb, xdp->data_end - xdp->data);
+}
+
 static inline
 void xdp_convert_frame_to_buff(const struct xdp_frame *frame,
 			       struct xdp_buff *xdp)
net/core/xdp.c +1 -2
index f86eedad586a..1fca2aa1d1fe 100644
@@ -646,8 +646,7 @@ struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
 	if (unlikely(!skb))
 		return NULL;
 
-	skb_reserve(skb, xdp->data - xdp->data_hard_start);
-	__skb_put(skb, xdp->data_end - xdp->data);
+	xdp_skb_reserve_put(xdp, skb);
 
 	metalen = xdp->data - xdp->data_meta;
 	if (metalen > 0)
Signature · 1 line
2.43.0
maintainer
#2 lore raw
Quoted 16 lines · Willem de Bruijn
Quoted 7 lines · Jon Kohler

Add helper for calling skb_{put|reserve} to reduce repetitive pattern across various drivers.

Plumb into tap and tun to start.

No functional change intended.

Quoted 7 lines
---
  drivers/net/tap.c | 3 +--
  drivers/net/tun.c | 3 +--
  include/net/xdp.h | 8 ++++++++
  net/core/xdp.c    | 3 +--
  4 files changed, 11 insertions(+), 6 deletions(-)
Quoted 7 lines

Subjective, but I prefer the existing code. I understand what skb_reserve and skb_put do. While xdp_skb_reserve_put adds a layer of indirection that I'd have to follow.

Sometimes deduplication makes sense, sometimes the indirection adds more mental load than it's worth. In this case the code savings are small. As said, subjective. Happy to hear other opinions.

+1, agree with Willem

author
#3 lore raw
Quoted 25 lines · Daniel Borkmann

!-------------------------------------------------------------------|
CAUTION: External Email

|-------------------------------------------------------------------!

Quoted 15 lines · Willem de Bruijn
Quoted 14 lines · Jon Kohler

Add helper for calling skb_{put|reserve} to reduce repetitive pattern across various drivers.

Plumb into tap and tun to start.

No functional change intended.

---
 drivers/net/tap.c | 3 +--
 drivers/net/tun.c | 3 +--
 include/net/xdp.h | 8 ++++++++
 net/core/xdp.c    | 3 +--
 4 files changed, 11 insertions(+), 6 deletions(-)
Quoted 8 lines
Quoted 6 lines

Subjective, but I prefer the existing code. I understand what skb_reserve and skb_put do. While xdp_skb_reserve_put adds a layer of indirection that I'd have to follow.
Sometimes deduplication makes sense, sometimes the indirection adds more mental load than it's worth. In this case the code savings are small. As said, subjective. Happy to hear other opinions.

+1, agree with Willem

That’s a fair point. I was also toying with the idea of something like this instead:

e.g.
xdp_headroom(xdp) == xdp->data - xdp->data_hard_start … similar to skb_headroom

xdp_length_base(xdp) == xdp->data_end - xdp->data … similar to xdp_get_buff_len, but doesn’t look at frags

then we could do:
skb_reserve(skb, xdp_headroom(xdp));
skb_put(skb, xdp_length_base(xdp));

Names TBD of course, but thoughts?

That way we keep skb_reserve/put just the same, but have a nice helper like we do for skb_headroom() already

maintainer
#4 lore raw
Quoted 44 lines · Jon Kohler
Quoted 25 lines · Daniel Borkmann

!-------------------------------------------------------------------|
CAUTION: External Email

|-------------------------------------------------------------------!

Quoted 15 lines · Willem de Bruijn
Quoted 14 lines · Jon Kohler

Add helper for calling skb_{put|reserve} to reduce repetitive pattern across various drivers.

Plumb into tap and tun to start.

No functional change intended.

---
 drivers/net/tap.c | 3 +--
 drivers/net/tun.c | 3 +--
 include/net/xdp.h | 8 ++++++++
 net/core/xdp.c    | 3 +--
 4 files changed, 11 insertions(+), 6 deletions(-)
Quoted 8 lines
Quoted 6 lines

Subjective, but I prefer the existing code. I understand what skb_reserve and skb_put do. While xdp_skb_reserve_put adds a layer of indirection that I'd have to follow.
Sometimes deduplication makes sense, sometimes the indirection adds more mental load than it's worth. In this case the code savings are small. As said, subjective. Happy to hear other opinions.

+1, agree with Willem

That’s a fair point. I was also toying with the idea of something like this instead:

e.g.
xdp_headroom(xdp) == xdp->data - xdp->data_hard_start … similar to skb_headroom

xdp_length_base(xdp) == xdp->data_end - xdp->data … similar to xdp_get_buff_len, but doesn’t look at frags

Quoted 8 lines

then we could do:
skb_reserve(skb, xdp_headroom(xdp));
skb_put(skb, xdp_length_base(xdp));

Names TBD of course, but thoughts?

That way we keep skb_reserve/put just the same, but have a nice helper like we do for skb_headroom() already

I like the idea of xdp_headroom and xdk_headlen, similar to skb_headroom and skb_headlen.

author
#5 lore raw
Quoted 58 lines · Willem de Bruijn

!-------------------------------------------------------------------|
CAUTION: External Email

|-------------------------------------------------------------------!

Quoted 48 lines · Jon Kohler
Quoted 25 lines · Daniel Borkmann

!-------------------------------------------------------------------|
CAUTION: External Email

|-------------------------------------------------------------------!

Quoted 15 lines · Willem de Bruijn
Quoted 14 lines · Jon Kohler

Add helper for calling skb_{put|reserve} to reduce repetitive pattern across various drivers.

Plumb into tap and tun to start.

No functional change intended.

---

drivers/net/tap.c | 3 +--
drivers/net/tun.c | 3 +--
include/net/xdp.h | 8 ++++++++
net/core/xdp.c    | 3 +--
4 files changed, 11 insertions(+), 6 deletions(-)
Quoted 8 lines
Quoted 6 lines

Subjective, but I prefer the existing code. I understand what skb_reserve and skb_put do. While xdp_skb_reserve_put adds a layer of indirection that I'd have to follow.
Sometimes deduplication makes sense, sometimes the indirection adds more mental load than it's worth. In this case the code savings are small. As said, subjective. Happy to hear other opinions.

+1, agree with Willem

That’s a fair point. I was also toying with the idea of something like this instead:

e.g.
xdp_headroom(xdp) == xdp->data - xdp->data_hard_start … similar to skb_headroom

xdp_length_base(xdp) == xdp->data_end - xdp->data … similar to xdp_get_buff_len, but doesn’t look at frags

then we could do:
skb_reserve(skb, xdp_headroom(xdp));
skb_put(skb, xdp_length_base(xdp));

Quoted 8 lines
Quoted 4 lines

Names TBD of course, but thoughts?

That way we keep skb_reserve/put just the same, but have a nice helper like we do for skb_headroom() already

I like the idea of xdp_headroom and xdk_headlen, similar to skb_headroom and skb_headlen.

Sold! I’ll cook it up