Commit a4f89fb7 authored by Al Viro's avatar Al Viro Committed by David S. Miller

[NET]: X86_64 checksum annotations and cleanups.

* sanitize prototypes, annotate
* usual ntohs->shift
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 9d3d4195
...@@ -132,9 +132,10 @@ static __force_inline unsigned do_csum(const unsigned char *buff, unsigned len) ...@@ -132,9 +132,10 @@ static __force_inline unsigned do_csum(const unsigned char *buff, unsigned len)
* *
* it's best to have buff aligned on a 64-bit boundary * it's best to have buff aligned on a 64-bit boundary
*/ */
unsigned csum_partial(const unsigned char *buff, unsigned len, unsigned sum) __wsum csum_partial(const void *buff, int len, __wsum sum)
{ {
return add32_with_carry(do_csum(buff, len), sum); return (__force __wsum)add32_with_carry(do_csum(buff, len),
(__force u32)sum);
} }
EXPORT_SYMBOL(csum_partial); EXPORT_SYMBOL(csum_partial);
...@@ -143,7 +144,7 @@ EXPORT_SYMBOL(csum_partial); ...@@ -143,7 +144,7 @@ EXPORT_SYMBOL(csum_partial);
* this routine is used for miscellaneous IP-like checksums, mainly * this routine is used for miscellaneous IP-like checksums, mainly
* in icmp.c * in icmp.c
*/ */
unsigned short ip_compute_csum(unsigned char * buff, int len) __sum16 ip_compute_csum(const void *buff, int len)
{ {
return csum_fold(csum_partial(buff,len,0)); return csum_fold(csum_partial(buff,len,0));
} }
......
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
* Returns an 32bit unfolded checksum of the buffer. * Returns an 32bit unfolded checksum of the buffer.
* src and dst are best aligned to 64bits. * src and dst are best aligned to 64bits.
*/ */
unsigned int __wsum
csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst, csum_partial_copy_from_user(const void __user *src, void *dst,
int len, unsigned int isum, int *errp) int len, __wsum isum, int *errp)
{ {
might_sleep(); might_sleep();
*errp = 0; *errp = 0;
...@@ -34,17 +34,19 @@ csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst, ...@@ -34,17 +34,19 @@ csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst,
if (unlikely((unsigned long)src & 6)) { if (unlikely((unsigned long)src & 6)) {
while (((unsigned long)src & 6) && len >= 2) { while (((unsigned long)src & 6) && len >= 2) {
__u16 val16; __u16 val16;
*errp = __get_user(val16, (__u16 __user *)src); *errp = __get_user(val16, (const __u16 __user *)src);
if (*errp) if (*errp)
return isum; return isum;
*(__u16 *)dst = val16; *(__u16 *)dst = val16;
isum = add32_with_carry(isum, val16); isum = (__force __wsum)add32_with_carry(
(__force unsigned)isum, val16);
src += 2; src += 2;
dst += 2; dst += 2;
len -= 2; len -= 2;
} }
} }
isum = csum_partial_copy_generic((__force void *)src,dst,len,isum,errp,NULL); isum = csum_partial_copy_generic((__force const void *)src,
dst, len, isum, errp, NULL);
if (likely(*errp == 0)) if (likely(*errp == 0))
return isum; return isum;
} }
...@@ -66,9 +68,9 @@ EXPORT_SYMBOL(csum_partial_copy_from_user); ...@@ -66,9 +68,9 @@ EXPORT_SYMBOL(csum_partial_copy_from_user);
* Returns an 32bit unfolded checksum of the buffer. * Returns an 32bit unfolded checksum of the buffer.
* src and dst are best aligned to 64bits. * src and dst are best aligned to 64bits.
*/ */
unsigned int __wsum
csum_partial_copy_to_user(unsigned const char *src, unsigned char __user *dst, csum_partial_copy_to_user(const void *src, void __user *dst,
int len, unsigned int isum, int *errp) int len, __wsum isum, int *errp)
{ {
might_sleep(); might_sleep();
if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) { if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) {
...@@ -79,7 +81,8 @@ csum_partial_copy_to_user(unsigned const char *src, unsigned char __user *dst, ...@@ -79,7 +81,8 @@ csum_partial_copy_to_user(unsigned const char *src, unsigned char __user *dst,
if (unlikely((unsigned long)dst & 6)) { if (unlikely((unsigned long)dst & 6)) {
while (((unsigned long)dst & 6) && len >= 2) { while (((unsigned long)dst & 6) && len >= 2) {
__u16 val16 = *(__u16 *)src; __u16 val16 = *(__u16 *)src;
isum = add32_with_carry(isum, val16); isum = (__force __wsum)add32_with_carry(
(__force unsigned)isum, val16);
*errp = __put_user(val16, (__u16 __user *)dst); *errp = __put_user(val16, (__u16 __user *)dst);
if (*errp) if (*errp)
return isum; return isum;
...@@ -104,19 +107,21 @@ EXPORT_SYMBOL(csum_partial_copy_to_user); ...@@ -104,19 +107,21 @@ EXPORT_SYMBOL(csum_partial_copy_to_user);
* *
* Returns an 32bit unfolded checksum of the buffer. * Returns an 32bit unfolded checksum of the buffer.
*/ */
unsigned int __wsum
csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len, unsigned int sum) csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
{ {
return csum_partial_copy_generic(src,dst,len,sum,NULL,NULL); return csum_partial_copy_generic(src,dst,len,sum,NULL,NULL);
} }
EXPORT_SYMBOL(csum_partial_copy_nocheck); EXPORT_SYMBOL(csum_partial_copy_nocheck);
unsigned short csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr, __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
__u32 len, unsigned short proto, unsigned int sum) const struct in6_addr *daddr,
__u32 len, unsigned short proto, __wsum sum)
{ {
__u64 rest, sum64; __u64 rest, sum64;
rest = (__u64)htonl(len) + (__u64)htons(proto) + (__u64)sum; rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) +
(__force __u64)sum;
asm(" addq (%[saddr]),%[sum]\n" asm(" addq (%[saddr]),%[sum]\n"
" adcq 8(%[saddr]),%[sum]\n" " adcq 8(%[saddr]),%[sum]\n"
" adcq (%[daddr]),%[sum]\n" " adcq (%[daddr]),%[sum]\n"
...@@ -124,7 +129,7 @@ unsigned short csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr, ...@@ -124,7 +129,7 @@ unsigned short csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr,
" adcq $0,%[sum]\n" " adcq $0,%[sum]\n"
: [sum] "=r" (sum64) : [sum] "=r" (sum64)
: "[sum]" (rest),[saddr] "r" (saddr), [daddr] "r" (daddr)); : "[sum]" (rest),[saddr] "r" (saddr), [daddr] "r" (daddr));
return csum_fold(add32_with_carry(sum64 & 0xffffffff, sum64>>32)); return csum_fold((__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32));
} }
EXPORT_SYMBOL(csum_ipv6_magic); EXPORT_SYMBOL(csum_ipv6_magic);
...@@ -19,15 +19,16 @@ ...@@ -19,15 +19,16 @@
* the last step before putting a checksum into a packet. * the last step before putting a checksum into a packet.
* Make sure not to mix with 64bit checksums. * Make sure not to mix with 64bit checksums.
*/ */
static inline unsigned int csum_fold(unsigned int sum) static inline __sum16 csum_fold(__wsum sum)
{ {
__asm__( __asm__(
" addl %1,%0\n" " addl %1,%0\n"
" adcl $0xffff,%0" " adcl $0xffff,%0"
: "=r" (sum) : "=r" (sum)
: "r" (sum << 16), "0" (sum & 0xffff0000) : "r" ((__force u32)sum << 16),
"0" ((__force u32)sum & 0xffff0000)
); );
return (~sum) >> 16; return (__force __sum16)(~(__force u32)sum >> 16);
} }
/* /*
...@@ -43,7 +44,7 @@ static inline unsigned int csum_fold(unsigned int sum) ...@@ -43,7 +44,7 @@ static inline unsigned int csum_fold(unsigned int sum)
* iph: ipv4 header * iph: ipv4 header
* ihl: length of header / 4 * ihl: length of header / 4
*/ */
static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl) static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
{ {
unsigned int sum; unsigned int sum;
...@@ -70,7 +71,7 @@ static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl) ...@@ -70,7 +71,7 @@ static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
: "=r" (sum), "=r" (iph), "=r" (ihl) : "=r" (sum), "=r" (iph), "=r" (ihl)
: "1" (iph), "2" (ihl) : "1" (iph), "2" (ihl)
: "memory"); : "memory");
return(sum); return (__force __sum16)sum;
} }
/** /**
...@@ -84,16 +85,17 @@ static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl) ...@@ -84,16 +85,17 @@ static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
* Returns the pseudo header checksum the input data. Result is * Returns the pseudo header checksum the input data. Result is
* 32bit unfolded. * 32bit unfolded.
*/ */
static inline unsigned long static inline __wsum
csum_tcpudp_nofold(unsigned saddr, unsigned daddr, unsigned short len, csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
unsigned short proto, unsigned int sum) unsigned short proto, __wsum sum)
{ {
asm(" addl %1, %0\n" asm(" addl %1, %0\n"
" adcl %2, %0\n" " adcl %2, %0\n"
" adcl %3, %0\n" " adcl %3, %0\n"
" adcl $0, %0\n" " adcl $0, %0\n"
: "=r" (sum) : "=r" (sum)
: "g" (daddr), "g" (saddr), "g" ((ntohs(len)<<16)+proto*256), "0" (sum)); : "g" (daddr), "g" (saddr),
"g" ((len + proto)<<8), "0" (sum));
return sum; return sum;
} }
...@@ -109,9 +111,9 @@ csum_tcpudp_nofold(unsigned saddr, unsigned daddr, unsigned short len, ...@@ -109,9 +111,9 @@ csum_tcpudp_nofold(unsigned saddr, unsigned daddr, unsigned short len,
* Returns the 16bit pseudo header checksum the input data already * Returns the 16bit pseudo header checksum the input data already
* complemented and ready to be filled in. * complemented and ready to be filled in.
*/ */
static inline unsigned short int static inline __sum16
csum_tcpudp_magic(unsigned long saddr, unsigned long daddr, csum_tcpudp_magic(__be32 saddr, __be32 daddr,
unsigned short len, unsigned short proto, unsigned int sum) unsigned short len, unsigned short proto, __wsum sum)
{ {
return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum)); return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
} }
...@@ -126,25 +128,25 @@ csum_tcpudp_magic(unsigned long saddr, unsigned long daddr, ...@@ -126,25 +128,25 @@ csum_tcpudp_magic(unsigned long saddr, unsigned long daddr,
* Before filling it in it needs to be csum_fold()'ed. * Before filling it in it needs to be csum_fold()'ed.
* buff should be aligned to a 64bit boundary if possible. * buff should be aligned to a 64bit boundary if possible.
*/ */
extern unsigned int csum_partial(const unsigned char *buff, unsigned len, unsigned int sum); extern __wsum csum_partial(const void *buff, int len, __wsum sum);
#define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER 1 #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER 1
#define HAVE_CSUM_COPY_USER 1 #define HAVE_CSUM_COPY_USER 1
/* Do not call this directly. Use the wrappers below */ /* Do not call this directly. Use the wrappers below */
extern unsigned long csum_partial_copy_generic(const unsigned char *src, const unsigned char *dst, extern __wsum csum_partial_copy_generic(const void *src, const void *dst,
unsigned len, int len,
unsigned sum, __wsum sum,
int *src_err_ptr, int *dst_err_ptr); int *src_err_ptr, int *dst_err_ptr);
extern unsigned int csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst, extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
int len, unsigned int isum, int *errp); int len, __wsum isum, int *errp);
extern unsigned int csum_partial_copy_to_user(const unsigned char *src, unsigned char __user *dst, extern __wsum csum_partial_copy_to_user(const void *src, void __user *dst,
int len, unsigned int isum, int *errp); int len, __wsum isum, int *errp);
extern unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len, extern __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len,
unsigned int sum); __wsum sum);
/* Old names. To be removed. */ /* Old names. To be removed. */
#define csum_and_copy_to_user csum_partial_copy_to_user #define csum_and_copy_to_user csum_partial_copy_to_user
...@@ -158,7 +160,7 @@ extern unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned ...@@ -158,7 +160,7 @@ extern unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned
* Returns the 16bit folded/inverted checksum of the passed buffer. * Returns the 16bit folded/inverted checksum of the passed buffer.
* Ready to fill in. * Ready to fill in.
*/ */
extern unsigned short ip_compute_csum(unsigned char * buff, int len); extern __sum16 ip_compute_csum(const void *buff, int len);
/** /**
* csum_ipv6_magic - Compute checksum of an IPv6 pseudo header. * csum_ipv6_magic - Compute checksum of an IPv6 pseudo header.
...@@ -176,9 +178,9 @@ extern unsigned short ip_compute_csum(unsigned char * buff, int len); ...@@ -176,9 +178,9 @@ extern unsigned short ip_compute_csum(unsigned char * buff, int len);
struct in6_addr; struct in6_addr;
#define _HAVE_ARCH_IPV6_CSUM 1 #define _HAVE_ARCH_IPV6_CSUM 1
extern unsigned short extern __sum16
csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr, csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
__u32 len, unsigned short proto, unsigned int sum); __u32 len, unsigned short proto, __wsum sum);
static inline unsigned add32_with_carry(unsigned a, unsigned b) static inline unsigned add32_with_carry(unsigned a, unsigned b)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment