net: checksum: Move from32to16() to generic header

from32to16() is used by lib/checksum.c and also by
arch/parisc/lib/checksum.c. The next patch will use it in the
bpf_csum_diff helper.

Move from32to16() to the include/net/checksum.h as csum_from32to16() and
remove other implementations.

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20241026125339.26459-2-puranjay@kernel.org
This commit is contained in:
Puranjay Mohan 2024-10-26 12:53:36 +00:00 committed by Daniel Borkmann
parent 0ab7cd1f18
commit db71aae70e
3 changed files with 9 additions and 21 deletions

View File

@ -25,15 +25,6 @@
: "=r"(_t) \ : "=r"(_t) \
: "r"(_r), "0"(_t)); : "r"(_r), "0"(_t));
static inline unsigned short from32to16(unsigned int x)
{
/* 32 bits --> 16 bits + carry */
x = (x & 0xffff) + (x >> 16);
/* 16 bits + carry --> 16 bits including carry */
x = (x & 0xffff) + (x >> 16);
return (unsigned short)x;
}
static inline unsigned int do_csum(const unsigned char * buff, int len) static inline unsigned int do_csum(const unsigned char * buff, int len)
{ {
int odd, count; int odd, count;
@ -85,7 +76,7 @@ static inline unsigned int do_csum(const unsigned char * buff, int len)
} }
if (len & 1) if (len & 1)
result += le16_to_cpu(*buff); result += le16_to_cpu(*buff);
result = from32to16(result); result = csum_from32to16(result);
if (odd) if (odd)
result = swab16(result); result = swab16(result);
out: out:
@ -102,7 +93,7 @@ __wsum csum_partial(const void *buff, int len, __wsum sum)
{ {
unsigned int result = do_csum(buff, len); unsigned int result = do_csum(buff, len);
addc(result, sum); addc(result, sum);
return (__force __wsum)from32to16(result); return (__force __wsum)csum_from32to16(result);
} }
EXPORT_SYMBOL(csum_partial); EXPORT_SYMBOL(csum_partial);

View File

@ -151,6 +151,12 @@ static inline void csum_replace(__wsum *csum, __wsum old, __wsum new)
*csum = csum_add(csum_sub(*csum, old), new); *csum = csum_add(csum_sub(*csum, old), new);
} }
static inline unsigned short csum_from32to16(unsigned int sum)
{
sum += (sum >> 16) | (sum << 16);
return (unsigned short)(sum >> 16);
}
struct sk_buff; struct sk_buff;
void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb, void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
__be32 from, __be32 to, bool pseudohdr); __be32 from, __be32 to, bool pseudohdr);

View File

@ -34,15 +34,6 @@
#include <asm/byteorder.h> #include <asm/byteorder.h>
#ifndef do_csum #ifndef do_csum
static inline unsigned short from32to16(unsigned int x)
{
/* add up 16-bit and 16-bit for 16+c bit */
x = (x & 0xffff) + (x >> 16);
/* add up carry.. */
x = (x & 0xffff) + (x >> 16);
return x;
}
static unsigned int do_csum(const unsigned char *buff, int len) static unsigned int do_csum(const unsigned char *buff, int len)
{ {
int odd; int odd;
@ -90,7 +81,7 @@ static unsigned int do_csum(const unsigned char *buff, int len)
#else #else
result += (*buff << 8); result += (*buff << 8);
#endif #endif
result = from32to16(result); result = csum_from32to16(result);
if (odd) if (odd)
result = ((result >> 8) & 0xff) | ((result & 0xff) << 8); result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
out: out: