Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

authored by

Puranjay Mohan and committed by
Daniel Borkmann
db71aae7 0ab7cd1f

+9 -21
+2 -11
arch/parisc/lib/checksum.c
··· 25 25 : "=r"(_t) \ 26 26 : "r"(_r), "0"(_t)); 27 27 28 - static inline unsigned short from32to16(unsigned int x) 29 - { 30 - /* 32 bits --> 16 bits + carry */ 31 - x = (x & 0xffff) + (x >> 16); 32 - /* 16 bits + carry --> 16 bits including carry */ 33 - x = (x & 0xffff) + (x >> 16); 34 - return (unsigned short)x; 35 - } 36 - 37 28 static inline unsigned int do_csum(const unsigned char * buff, int len) 38 29 { 39 30 int odd, count; ··· 76 85 } 77 86 if (len & 1) 78 87 result += le16_to_cpu(*buff); 79 - result = from32to16(result); 88 + result = csum_from32to16(result); 80 89 if (odd) 81 90 result = swab16(result); 82 91 out: ··· 93 102 { 94 103 unsigned int result = do_csum(buff, len); 95 104 addc(result, sum); 96 - return (__force __wsum)from32to16(result); 105 + return (__force __wsum)csum_from32to16(result); 97 106 } 98 107 99 108 EXPORT_SYMBOL(csum_partial);
+6
include/net/checksum.h
··· 151 151 *csum = csum_add(csum_sub(*csum, old), new); 152 152 } 153 153 154 + static inline unsigned short csum_from32to16(unsigned int sum) 155 + { 156 + sum += (sum >> 16) | (sum << 16); 157 + return (unsigned short)(sum >> 16); 158 + } 159 + 154 160 struct sk_buff; 155 161 void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb, 156 162 __be32 from, __be32 to, bool pseudohdr);
+1 -10
lib/checksum.c
··· 34 34 #include <asm/byteorder.h> 35 35 36 36 #ifndef do_csum 37 - static inline unsigned short from32to16(unsigned int x) 38 - { 39 - /* add up 16-bit and 16-bit for 16+c bit */ 40 - x = (x & 0xffff) + (x >> 16); 41 - /* add up carry.. */ 42 - x = (x & 0xffff) + (x >> 16); 43 - return x; 44 - } 45 - 46 37 static unsigned int do_csum(const unsigned char *buff, int len) 47 38 { 48 39 int odd; ··· 81 90 #else 82 91 result += (*buff << 8); 83 92 #endif 84 - result = from32to16(result); 93 + result = csum_from32to16(result); 85 94 if (odd) 86 95 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8); 87 96 out: