Skip to contentSkip to conversation

Add 2 functions to skbuff for code sharing

PATCH New Christian Hopps 3 participants 8 messages Aug 9, 2024 – Aug 15, 2024

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

cover Add 2 functions to skbuff for code sharing

* Summary of Changes:

This patchset contains 2 commits which add 2 functions to skbuff.[ch].

- skb_copy_seq_read() - copy from a skb_seq_state to a buffer
- ___copy_skb_header() - factored existing code

these are used in a followup patchset implementing IP-TFS/AggFrag encapsulation (https://www.rfc-editor.org/rfc/rfc9347.txt)

Patchset History:
-----------------

v1 (8/9/2024)
- Created from IP-TFS patchset v9

v2 (8/9/2024)
- resend with corrected CC list.

Patchset Changes:
-----------------

  include/linux/skbuff.h |  2 ++
  net/core/skbuff.c      | 43 ++++++++++++++++++++++++++++++++++++++++++-
  2 files changed, 44 insertions(+), 1 deletion(-)
#1 lore raw

PATCH v2 1/2 net: refactor common skb header copy code for re-use

From: Christian Hopps <[email protected]>

Factor out some common skb header copying code so that it can be re-used outside of skbuff.

---
 include/linux/skbuff.h | 1 +
 net/core/skbuff.c      | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
include/linux/skbuff.h +1 -0
index 29c3ea5b6e93..8626f9a343db 100644
@@ -1374,6 +1374,7 @@ struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
 void skb_headers_offset_update(struct sk_buff *skb, int off);
 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority);
+void ___copy_skb_header(struct sk_buff *new, const struct sk_buff *old);
 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old);
 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority);
 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
net/core/skbuff.c +7 -1
index 83f8cd8aa2d1..da5a47d2c9ab 100644
@@ -1515,7 +1515,7 @@ EXPORT_SYMBOL(napi_consume_skb);
 	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
 		     offsetof(struct sk_buff, headers.field));	\
 
-static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+void ___copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
 {
 	new->tstamp		= old->tstamp;
 	/* We do not copy old->sk */
@@ -1524,6 +1524,12 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
 	skb_dst_copy(new, old);
 	__skb_ext_copy(new, old);
 	__nf_copy(new, old, false);
+}
+EXPORT_SYMBOL_GPL(___copy_skb_header);
+
+static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+{
+	___copy_skb_header(new, old);
 
 	/* Note : this field could be in the headers group.
 	 * It is not yet because we do not want to have a 16 bit hole
Signature · 1 line
2.46.0
maintainer
#2 lore raw

Re: [PATCH ipsec-next v2 1/2] net: refactor common skb header copy code for re-use

Quoted 38 lines · Christian Hopps

From: Christian Hopps <[email protected]>

Factor out some common skb header copying code so that it can be re-used outside of skbuff.

---
  include/linux/skbuff.h | 1 +
  net/core/skbuff.c      | 8 +++++++-
  2 files changed, 8 insertions(+), 1 deletion(-)
include/linux/skbuff.h +1 -0
index 29c3ea5b6e93..8626f9a343db 100644
@@ -1374,6 +1374,7 @@ struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
  void skb_headers_offset_update(struct sk_buff *skb, int off);
  int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
  struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority);
+void ___copy_skb_header(struct sk_buff *new, const struct sk_buff *old);
  void skb_copy_header(struct sk_buff *new, const struct sk_buff *old);
  struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority);
  struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
net/core/skbuff.c +1 -1
index 83f8cd8aa2d1..da5a47d2c9ab 100644
@@ -1515,7 +1515,7 @@ EXPORT_SYMBOL(napi_consume_skb);
  	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
  		     offsetof(struct sk_buff, headers.field));	\
 
-static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+void ___copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  {
  	new->tstamp		= old->tstamp;
  	/* We do not copy old->sk */
@@ -1524,6 +1524,12 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  	skb_dst_copy(new, old);

net/core/skbuff.c static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) · line 1534

Quoted 10 lines
net/core/skbuff.c +6 -0
  	__skb_ext_copy(new, old);
  	__nf_copy(new, old, false);
+}
+EXPORT_SYMBOL_GPL(___copy_skb_header);
+
+static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+{
+	___copy_skb_header(new, old); >
  	/* Note : this field could be in the headers group.
  	 * It is not yet because we do not want to have a 16 bit hole

Could you please point where/how are you going to use this helper? factoring out this very core bits of skbuff copy looks quite bug prone - and exporting the helper could introduce additional unneeded function calls in the core code.

Thanks,

Paolo

#3 lore raw

Re: [PATCH ipsec-next v2 1/2] net: refactor common skb header copy code for re-use

Quoted 21 lines · Paolo Abeni
Quoted 20 lines · Christian Hopps

From: Christian Hopps <[email protected]>

net/core/skbuff.c +6 -1
@@ -1515,7 +1515,7 @@ EXPORT_SYMBOL(napi_consume_skb);
  	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
  		     offsetof(struct sk_buff, headers.field));	\
-static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+void ___copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  {
  	new->tstamp		= old->tstamp;
  	/* We do not copy old->sk */
@@ -1524,6 +1524,12 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  	skb_dst_copy(new, old);
  	__skb_ext_copy(new, old);
  	__nf_copy(new, old, false);
+}
+EXPORT_SYMBOL_GPL(___copy_skb_header);
+
+static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+{
Quoted 8 lines

net/core/skbuff.c static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) · line 1535

Quoted 3 lines
net/core/skbuff.c +1 -0
+	___copy_skb_header(new, old); >
  	/* Note : this field could be in the headers group.
  	 * It is not yet because we do not want to have a 16 bit hole

Could you please point where/how are you going to use this helper? factoring out this very core bits of skbuff copy looks quite bug prone - and exporting the helper could introduce additional unneeded function calls in the core code.

It is supposed to be used in the IPTFS pachset:

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

It was open coded before, but there were some concerns that IPTFS won't get updated if __copy_skb_header changes.

maintainer
#4 lore raw

Re: [PATCH ipsec-next v2 1/2] net: refactor common skb header copy code for re-use

Quoted 31 lines · Steffen Klassert
Quoted 21 lines · Paolo Abeni
Quoted 20 lines · Christian Hopps

From: Christian Hopps <[email protected]>

net/core/skbuff.c +6 -1
@@ -1515,7 +1515,7 @@ EXPORT_SYMBOL(napi_consume_skb);
   	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
   		     offsetof(struct sk_buff, headers.field));	\
-static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+void ___copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
   {
   	new->tstamp		= old->tstamp;
   	/* We do not copy old->sk */
@@ -1524,6 +1524,12 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
   	skb_dst_copy(new, old);
   	__skb_ext_copy(new, old);
   	__nf_copy(new, old, false);
+}
+EXPORT_SYMBOL_GPL(___copy_skb_header);
+
+static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+{
Quoted 8 lines

net/core/skbuff.c static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) · line 1535

Quoted 3 lines
net/core/skbuff.c +1 -0
+	___copy_skb_header(new, old); >
   	/* Note : this field could be in the headers group.
   	 * It is not yet because we do not want to have a 16 bit hole

Could you please point where/how are you going to use this helper? factoring out this very core bits of skbuff copy looks quite bug prone - and exporting the helper could introduce additional unneeded function calls in the core code.

Quoted 6 lines

It is supposed to be used in the IPTFS pachset:

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

It was open coded before, but there were some concerns that IPTFS won't get updated if __copy_skb_header changes.

The code is copying a subset of the skb header from a 'template' skb to a newly allocated skbuff.
It's unclear to me why would be useful to copy only a subset of the skb header, excluding queue_mapping, priority, etc..
I think we need a good justification for that, otherwise we could end-up with a large amount of "almost copy" skb header slicing the skb in many different ways.

Cheers,

Paolo

#5 lore raw

Re: [PATCH ipsec-next v2 1/2] net: refactor common skb header copy code for re-use

Quoted 36 lines · Paolo Abeni
Quoted 30 lines · Steffen Klassert
Quoted 21 lines · Paolo Abeni
Quoted 20 lines · Christian Hopps

From: Christian Hopps <[email protected]>

net/core/skbuff.c +6 -1
@@ -1515,7 +1515,7 @@ EXPORT_SYMBOL(napi_consume_skb);
   	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
   		     offsetof(struct sk_buff, headers.field));	\
-static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+void ___copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
   {
   	new->tstamp		= old->tstamp;
   	/* We do not copy old->sk */
@@ -1524,6 +1524,12 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
   	skb_dst_copy(new, old);
   	__skb_ext_copy(new, old);
   	__nf_copy(new, old, false);
+}
+EXPORT_SYMBOL_GPL(___copy_skb_header);
+
+static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+{
Quoted 8 lines

net/core/skbuff.c static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) · line 1535

Quoted 3 lines
net/core/skbuff.c +1 -0
+	___copy_skb_header(new, old); >
   	/* Note : this field could be in the headers group.
   	 * It is not yet because we do not want to have a 16 bit hole

Could you please point where/how are you going to use this helper? factoring out this very core bits of skbuff copy looks quite bug prone - and exporting the helper could introduce additional unneeded function calls in the core code.

Quoted 4 lines

It is supposed to be used in the IPTFS pachset:
https://lore.kernel.org/netdev/[email protected]/
It was open coded before, but there were some concerns that IPTFS won't get updated if __copy_skb_header changes.

Quoted 6 lines

The code is copying a subset of the skb header from a 'template' skb to a newly allocated skbuff.
It's unclear to me why would be useful to copy only a subset of the skb header, excluding queue_mapping, priority, etc..
I think we need a good justification for that, otherwise we could end-up with a large amount of "almost copy" skb header slicing the skb in many different ways.

IP-TFS sometimes needs to allocate new skb[s] to fragment a too-large tunnel ingress user packet. IP-TFS may also need to extract multiple aggregated user packets for tunnel egress from inside a single IPTFS tunnel packet. For these 1 to N cases (which are different from regular IPsec which is always 1-1 and thus re-using the existing skb) we need to create multiple skbs from a single source skb and we need to replicate the work done to the existing skb so far in the netdev/xfrm infrastructure (e.g. the _refdst and _nfct are expected to be there and refcounted as they are dropped later in the stack). This work is captured in those first few values that we are copying. The headers and other field values; however, are not appropriate to be copied (or clobbered e.g., alloc_cpu) into the new allocated skb.

I originally had this code local to the IP-TFS implementation, but I was persuaded to move it in skbuff.c to track any possible changes to these fields in the future.

Thanks,
Chris.

Quoted 3 lines

Cheers,

Paolo

#6 lore raw

Re: [PATCH ipsec-next v2 1/2] net: refactor common skb header copy code for re-use

Quoted 57 lines · Christian Hopps
Quoted 36 lines · Paolo Abeni
Quoted 30 lines · Steffen Klassert
Quoted 21 lines · Paolo Abeni
Quoted 20 lines · Christian Hopps

From: Christian Hopps <[email protected]>

net/core/skbuff.c +6 -1
@@ -1515,7 +1515,7 @@ EXPORT_SYMBOL(napi_consume_skb);
   	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
   		     offsetof(struct sk_buff, headers.field));	\
-static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+void ___copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
   {
   	new->tstamp		= old->tstamp;
   	/* We do not copy old->sk */
@@ -1524,6 +1524,12 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
   	skb_dst_copy(new, old);
   	__skb_ext_copy(new, old);
   	__nf_copy(new, old, false);
+}
+EXPORT_SYMBOL_GPL(___copy_skb_header);
+
+static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
+{
Quoted 8 lines

net/core/skbuff.c static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) · line 1535

Quoted 3 lines
net/core/skbuff.c +1 -0
+	___copy_skb_header(new, old); >
   	/* Note : this field could be in the headers group.
   	 * It is not yet because we do not want to have a 16 bit hole

Could you please point where/how are you going to use this helper? factoring out this very core bits of skbuff copy looks quite bug prone - and exporting the helper could introduce additional unneeded function calls in the core code.

Quoted 4 lines

It is supposed to be used in the IPTFS pachset:
https://lore.kernel.org/netdev/[email protected]/
It was open coded before, but there were some concerns that IPTFS won't get updated if __copy_skb_header changes.

Quoted 6 lines

The code is copying a subset of the skb header from a 'template' skb to a newly allocated skbuff.
It's unclear to me why would be useful to copy only a subset of the skb header, excluding queue_mapping, priority, etc..
I think we need a good justification for that, otherwise we could end-up with a large amount of "almost copy" skb header slicing the skb in many different ways.

IP-TFS sometimes needs to allocate new skb[s] to fragment a too-large tunnel ingress user packet. IP-TFS may also need to extract multiple aggregated user packets for tunnel egress from inside a single IPTFS tunnel packet. For these 1 to N cases (which are different from regular IPsec which is always 1-1 and thus re-using the existing skb) we need to create multiple skbs from a single source skb and we need to replicate the work done to the existing skb so far in the netdev/xfrm infrastructure (e.g. the _refdst and _nfct are expected to be there and refcounted as they are dropped later in the stack). This work is captured in those first few values that we are copying. The headers and other field values; however, are not appropriate to be copied (or clobbered e.g., alloc_cpu) into the new allocated skb.

Quoted 1 line

I originally had this code local to the IP-TFS implementation, but I was persuaded to move it in skbuff.c to track any possible changes to these fields in the future.

I've gone back through the skb use again for both tunnel ingress and egress and I need even less than the subset of fields being copied here.

I'm going to resend this patchset removing this somewhat controversial refactoring for now, and just leave the second commit with the useful copy from read_seq function.

Thanks,
Chris.

Quoted 7 lines

Thanks,
Chris.

Quoted 3 lines

Cheers,

Paolo

a

#7 lore raw

PATCH v2 2/2 net: add copy from skb_seq_state to buffer function

In reply to Christian Hopps

From: Christian Hopps <[email protected]>

Add an skb helper function to copy a range of bytes from within an existing skb_seq_state.

---
 include/linux/skbuff.h |  1 +
 net/core/skbuff.c      | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
include/linux/skbuff.h +1 -0
index 8626f9a343db..95f80b6d1d7c 100644
@@ -1434,6 +1434,7 @@ void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
 			  struct skb_seq_state *st);
 void skb_abort_seq_read(struct skb_seq_state *st);
+int skb_copy_seq_read(struct skb_seq_state *st, int offset, void *to, int len);
 
 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
 			   unsigned int to, struct ts_config *config);
net/core/skbuff.c +35 -0
index da5a47d2c9ab..2cc2d1b8d356 100644
@@ -4415,6 +4415,41 @@ void skb_abort_seq_read(struct skb_seq_state *st)
 }
 EXPORT_SYMBOL(skb_abort_seq_read);
 
+/**
+ * skb_copy_seq_read() - copy from a skb_seq_state to a buffer
+ * @st: source skb_seq_state
+ * @offset: offset in source
+ * @to: destination buffer
+ * @len: number of bytes to copy
+ *
+ * Copy @len bytes from @offset bytes into the source @st to the destination
+ * buffer @to. `offset` should increase (or be unchanged) with each subsequent
+ * call to this function. If offset needs to decrease from the previous use `st`
+ * should be reset first.
+ *
+ * Return: 0 on success or a negative error code on failure
+ */
+int skb_copy_seq_read(struct skb_seq_state *st, int offset, void *to, int len)
+{
+	const u8 *data;
+	u32 sqlen;
+
+	for (;;) {
+		sqlen = skb_seq_read(offset, &data, st);
+		if (sqlen == 0)
+			return -ENOMEM;
+		if (sqlen >= len) {
+			memcpy(to, data, len);
+			return 0;
+		}
+		memcpy(to, data, sqlen);
+		to += sqlen;
+		offset += sqlen;
+		len -= sqlen;
+	}
+}
+EXPORT_SYMBOL(skb_copy_seq_read);
+
 #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
 
 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
Signature · 1 line
2.46.0