netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steffen Klassert <steffen.klassert@secunet.com>
To: David Miller <davem@davemloft.net>, Jakub Kicinski <kuba@kernel.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	Steffen Klassert <steffen.klassert@secunet.com>,
	<netdev@vger.kernel.org>
Subject: [PATCH 02/11] net: add copy from skb_seq_state to buffer function
Date: Mon, 9 Sep 2024 12:03:19 +0200	[thread overview]
Message-ID: <20240909100328.1838963-3-steffen.klassert@secunet.com> (raw)
In-Reply-To: <20240909100328.1838963-1-steffen.klassert@secunet.com>

From: Christian Hopps <chopps@labn.net>

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

Signed-off-by: Christian Hopps <chopps@labn.net>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
---
 include/linux/skbuff.h |  1 +
 net/core/skbuff.c      | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 29c3ea5b6e93..a871533b8568 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -1433,6 +1433,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);
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 83f8cd8aa2d1..fe4b2dc5c19b 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -4409,6 +4409,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 -EINVAL if the copy ended early
+ */
+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 -EINVAL;
+		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,
-- 
2.34.1


  parent reply	other threads:[~2024-09-09 10:03 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-09 10:03 [PATCH 0/11] pull request (net-next): ipsec-next 2024-09-09 Steffen Klassert
2024-09-09 10:03 ` [PATCH 01/11] xfrm: Remove documentation WARN_ON to limit return values for offloaded SA Steffen Klassert
2024-09-09 10:03 ` Steffen Klassert [this message]
2024-09-09 10:03 ` [PATCH 03/11] xfrm: Correct spelling in xfrm.h Steffen Klassert
2024-09-09 10:03 ` [PATCH 04/11] selftests: add xfrm policy insertion speed test script Steffen Klassert
2024-09-09 10:03 ` [PATCH 05/11] xfrm: policy: don't iterate inexact policies twice at insert time Steffen Klassert
2024-09-09 10:03 ` [PATCH 06/11] xfrm: switch migrate to xfrm_policy_lookup_bytype Steffen Klassert
2024-09-09 11:01   ` Florian Westphal
2024-09-09 13:18     ` Steffen Klassert
2024-09-09 10:03 ` [PATCH 07/11] xfrm: policy: remove remaining use of inexact list Steffen Klassert
2024-09-09 10:03 ` [PATCH 08/11] xfrm: add SA information to the offloaded packet Steffen Klassert
2024-09-09 10:03 ` [PATCH 09/11] xfrm: policy: use recently added helper in more places Steffen Klassert
2024-09-09 10:59   ` Florian Westphal
2024-09-09 13:19     ` Steffen Klassert
2024-09-09 10:03 ` [PATCH 10/11] xfrm: minor update to sdb and xfrm_policy comments Steffen Klassert
2024-09-09 10:03 ` [PATCH 11/11] Revert "xfrm: add SA information to the offloaded packet" Steffen Klassert
2024-09-09 13:21 ` [PATCH 0/11] pull request (net-next): ipsec-next 2024-09-09 Steffen Klassert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240909100328.1838963-3-steffen.klassert@secunet.com \
    --to=steffen.klassert@secunet.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).