Skip to content

Commit

Permalink
[NET]: X86_64 checksum annotations and cleanups.
Browse files Browse the repository at this point in the history
* sanitize prototypes, annotate
* usual ntohs->shift

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Al Viro authored and David S. Miller committed Dec 3, 2006
1 parent 9d3d419 commit a4f89fb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
7 changes: 4 additions & 3 deletions arch/x86_64/lib/csum-partial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
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);
Expand All @@ -143,7 +144,7 @@ EXPORT_SYMBOL(csum_partial);
* this routine is used for miscellaneous IP-like checksums, mainly
* 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));
}
Expand Down
37 changes: 21 additions & 16 deletions arch/x86_64/lib/csum-wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* Returns an 32bit unfolded checksum of the buffer.
* src and dst are best aligned to 64bits.
*/
unsigned int
csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst,
int len, unsigned int isum, int *errp)
__wsum
csum_partial_copy_from_user(const void __user *src, void *dst,
int len, __wsum isum, int *errp)
{
might_sleep();
*errp = 0;
Expand All @@ -34,17 +34,19 @@ csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst,
if (unlikely((unsigned long)src & 6)) {
while (((unsigned long)src & 6) && len >= 2) {
__u16 val16;
*errp = __get_user(val16, (__u16 __user *)src);
*errp = __get_user(val16, (const __u16 __user *)src);
if (*errp)
return isum;
*(__u16 *)dst = val16;
isum = add32_with_carry(isum, val16);
isum = (__force __wsum)add32_with_carry(
(__force unsigned)isum, val16);
src += 2;
dst += 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))
return isum;
}
Expand All @@ -66,9 +68,9 @@ EXPORT_SYMBOL(csum_partial_copy_from_user);
* Returns an 32bit unfolded checksum of the buffer.
* src and dst are best aligned to 64bits.
*/
unsigned int
csum_partial_copy_to_user(unsigned const char *src, unsigned char __user *dst,
int len, unsigned int isum, int *errp)
__wsum
csum_partial_copy_to_user(const void *src, void __user *dst,
int len, __wsum isum, int *errp)
{
might_sleep();
if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) {
Expand All @@ -79,7 +81,8 @@ csum_partial_copy_to_user(unsigned const char *src, unsigned char __user *dst,
if (unlikely((unsigned long)dst & 6)) {
while (((unsigned long)dst & 6) && len >= 2) {
__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);
if (*errp)
return isum;
Expand All @@ -104,27 +107,29 @@ EXPORT_SYMBOL(csum_partial_copy_to_user);
*
* Returns an 32bit unfolded checksum of the buffer.
*/
unsigned int
csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len, unsigned int sum)
__wsum
csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
{
return csum_partial_copy_generic(src,dst,len,sum,NULL,NULL);
}
EXPORT_SYMBOL(csum_partial_copy_nocheck);

unsigned short csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr,
__u32 len, unsigned short proto, unsigned int sum)
__sum16 csum_ipv6_magic(const struct in6_addr *saddr,
const struct in6_addr *daddr,
__u32 len, unsigned short proto, __wsum sum)
{
__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"
" adcq 8(%[saddr]),%[sum]\n"
" adcq (%[daddr]),%[sum]\n"
" adcq 8(%[daddr]),%[sum]\n"
" adcq $0,%[sum]\n"
: [sum] "=r" (sum64)
: "[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);
54 changes: 28 additions & 26 deletions include/asm-x86_64/checksum.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
* the last step before putting a checksum into a packet.
* 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__(
" addl %1,%0\n"
" adcl $0xffff,%0"
: "=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);
}

/*
Expand All @@ -43,7 +44,7 @@ static inline unsigned int csum_fold(unsigned int sum)
* iph: ipv4 header
* 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;

Expand All @@ -70,7 +71,7 @@ static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
: "=r" (sum), "=r" (iph), "=r" (ihl)
: "1" (iph), "2" (ihl)
: "memory");
return(sum);
return (__force __sum16)sum;
}

/**
Expand All @@ -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
* 32bit unfolded.
*/
static inline unsigned long
csum_tcpudp_nofold(unsigned saddr, unsigned daddr, unsigned short len,
unsigned short proto, unsigned int sum)
static inline __wsum
csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
unsigned short proto, __wsum sum)
{
asm(" addl %1, %0\n"
" adcl %2, %0\n"
" adcl %3, %0\n"
" adcl $0, %0\n"
: "=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;
}

Expand All @@ -109,9 +111,9 @@ csum_tcpudp_nofold(unsigned saddr, unsigned daddr, unsigned short len,
* Returns the 16bit pseudo header checksum the input data already
* complemented and ready to be filled in.
*/
static inline unsigned short int
csum_tcpudp_magic(unsigned long saddr, unsigned long daddr,
unsigned short len, unsigned short proto, unsigned int sum)
static inline __sum16
csum_tcpudp_magic(__be32 saddr, __be32 daddr,
unsigned short len, unsigned short proto, __wsum sum)
{
return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
}
Expand All @@ -126,25 +128,25 @@ csum_tcpudp_magic(unsigned long saddr, unsigned long daddr,
* Before filling it in it needs to be csum_fold()'ed.
* 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_CSUM_COPY_USER 1


/* Do not call this directly. Use the wrappers below */
extern unsigned long csum_partial_copy_generic(const unsigned char *src, const unsigned char *dst,
unsigned len,
unsigned sum,
extern __wsum csum_partial_copy_generic(const void *src, const void *dst,
int len,
__wsum sum,
int *src_err_ptr, int *dst_err_ptr);


extern unsigned int csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst,
int len, unsigned int isum, int *errp);
extern unsigned int csum_partial_copy_to_user(const unsigned char *src, unsigned char __user *dst,
int len, unsigned int isum, int *errp);
extern unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len,
unsigned int sum);
extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
int len, __wsum isum, int *errp);
extern __wsum csum_partial_copy_to_user(const void *src, void __user *dst,
int len, __wsum isum, int *errp);
extern __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len,
__wsum sum);

/* Old names. To be removed. */
#define csum_and_copy_to_user csum_partial_copy_to_user
Expand All @@ -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.
* 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.
Expand All @@ -176,9 +178,9 @@ extern unsigned short ip_compute_csum(unsigned char * buff, int len);
struct in6_addr;

#define _HAVE_ARCH_IPV6_CSUM 1
extern unsigned short
csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr,
__u32 len, unsigned short proto, unsigned int sum);
extern __sum16
csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
__u32 len, unsigned short proto, __wsum sum);

static inline unsigned add32_with_carry(unsigned a, unsigned b)
{
Expand Down

0 comments on commit a4f89fb

Please sign in to comment.