Skip to content

Commit ea6342d

Browse files
ebiggerskuba-moo
authored andcommitted
net: add skb_copy_and_crc32c_datagram_iter()
Since skb_copy_and_hash_datagram_iter() is used only with CRC32C, the crypto_ahash abstraction provides no value. Add skb_copy_and_crc32c_datagram_iter() which just calls crc32c() directly. This is faster and simpler. It also doesn't have the weird dependency issue where skb_copy_and_hash_datagram_iter() depends on CONFIG_CRYPTO_HASH=y without that being expressed explicitly in the kconfig (presumably because it was too heavyweight for NET to select). The new function is conditional on the hidden boolean symbol NET_CRC32C, which selects CRC32. So it gets compiled only when something that actually needs CRC32C packet checksums is enabled, it has no implicit dependency, and it doesn't depend on the heavyweight crypto layer. Signed-off-by: Eric Biggers <ebiggers@google.com> Reviewed-by: Hannes Reinecke <hare@suse.de> Link: https://patch.msgid.link/20250519175012.36581-9-ebiggers@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent b82f722 commit ea6342d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

include/linux/skbuff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4137,6 +4137,8 @@ int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, int hlen,
41374137
int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset,
41384138
struct iov_iter *to, int len,
41394139
struct ahash_request *hash);
4140+
int skb_copy_and_crc32c_datagram_iter(const struct sk_buff *skb, int offset,
4141+
struct iov_iter *to, int len, u32 *crcp);
41404142
int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
41414143
struct iov_iter *from, int len);
41424144
int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *frm);

net/core/datagram.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <linux/pagemap.h>
5353
#include <linux/iov_iter.h>
5454
#include <linux/indirect_call_wrapper.h>
55+
#include <linux/crc32.h>
5556

5657
#include <net/protocol.h>
5758
#include <linux/skbuff.h>
@@ -519,6 +520,38 @@ int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset,
519520
}
520521
EXPORT_SYMBOL(skb_copy_and_hash_datagram_iter);
521522

523+
#ifdef CONFIG_NET_CRC32C
524+
static size_t crc32c_and_copy_to_iter(const void *addr, size_t bytes,
525+
void *_crcp, struct iov_iter *i)
526+
{
527+
u32 *crcp = _crcp;
528+
size_t copied;
529+
530+
copied = copy_to_iter(addr, bytes, i);
531+
*crcp = crc32c(*crcp, addr, copied);
532+
return copied;
533+
}
534+
535+
/**
536+
* skb_copy_and_crc32c_datagram_iter - Copy datagram to an iovec iterator
537+
* and update a CRC32C value.
538+
* @skb: buffer to copy
539+
* @offset: offset in the buffer to start copying from
540+
* @to: iovec iterator to copy to
541+
* @len: amount of data to copy from buffer to iovec
542+
* @crcp: pointer to CRC32C value to update
543+
*
544+
* Return: 0 on success, -EFAULT if there was a fault during copy.
545+
*/
546+
int skb_copy_and_crc32c_datagram_iter(const struct sk_buff *skb, int offset,
547+
struct iov_iter *to, int len, u32 *crcp)
548+
{
549+
return __skb_datagram_iter(skb, offset, to, len, true,
550+
crc32c_and_copy_to_iter, crcp);
551+
}
552+
EXPORT_SYMBOL(skb_copy_and_crc32c_datagram_iter);
553+
#endif /* CONFIG_NET_CRC32C */
554+
522555
static size_t simple_copy_to_iter(const void *addr, size_t bytes,
523556
void *data __always_unused, struct iov_iter *i)
524557
{

0 commit comments

Comments
 (0)