OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [or32/] [lib/] [checksum.c] - Diff between revs 1765 and 1782

Only display areas with differences | Details | Blame | View Log

Rev 1765 Rev 1782
/*
/*
 * INET         An implementation of the TCP/IP protocol suite for the LINUX
 * INET         An implementation of the TCP/IP protocol suite for the LINUX
 *              operating system.  INET is implemented using the  BSD Socket
 *              operating system.  INET is implemented using the  BSD Socket
 *              interface as the means of communication with the user level.
 *              interface as the means of communication with the user level.
 *
 *
 *              IP/TCP/UDP checksumming routines
 *              IP/TCP/UDP checksumming routines
 *
 *
 * Authors:     Jorge Cwik, <jorge@laser.satlink.net>
 * Authors:     Jorge Cwik, <jorge@laser.satlink.net>
 *              Arnt Gulbrandsen, <agulbra@nvg.unit.no>
 *              Arnt Gulbrandsen, <agulbra@nvg.unit.no>
 *              Tom May, <ftom@netcom.com>
 *              Tom May, <ftom@netcom.com>
 *              Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>
 *              Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>
 *              Lots of code moved from tcp.c and ip.c; see those files
 *              Lots of code moved from tcp.c and ip.c; see those files
 *              for more names.
 *              for more names.
 *
 *
 * 03/02/96     Jes Sorensen, Andreas Schwab, Roman Hodek:
 * 03/02/96     Jes Sorensen, Andreas Schwab, Roman Hodek:
 *              Fixed some nasty bugs, causing some horrible crashes.
 *              Fixed some nasty bugs, causing some horrible crashes.
 *              A: At some points, the sum (%0) was used as
 *              A: At some points, the sum (%0) was used as
 *              length-counter instead of the length counter
 *              length-counter instead of the length counter
 *              (%1). Thanks to Roman Hodek for pointing this out.
 *              (%1). Thanks to Roman Hodek for pointing this out.
 *              B: GCC seems to mess up if one uses too many
 *              B: GCC seems to mess up if one uses too many
 *              data-registers to hold input values and one tries to
 *              data-registers to hold input values and one tries to
 *              specify d0 and d1 as scratch registers. Letting gcc choose these
 *              specify d0 and d1 as scratch registers. Letting gcc choose these
 *      registers itself solves the problem.
 *      registers itself solves the problem.
 *
 *
 *              This program is free software; you can redistribute it and/or
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 *              2 of the License, or (at your option) any later version.
 */
 */
 
 
/* Revised by Kenneth Albanowski for m68knommu. Basic problem: unaligned access kills, so most
/* Revised by Kenneth Albanowski for m68knommu. Basic problem: unaligned access kills, so most
   of the assembly has to go. */
   of the assembly has to go. */
 
 
#include <net/checksum.h>
#include <net/checksum.h>
 
 
extern unsigned short _ip_fast_csum(unsigned char *buf);
extern unsigned short _ip_fast_csum(unsigned char *buf);
 
 
static inline unsigned short from32to16(unsigned long x)
static inline unsigned short from32to16(unsigned long x)
{
{
        /* add up 16-bit and 16-bit for 16+c bit */
        /* add up 16-bit and 16-bit for 16+c bit */
        x = (x & 0xffff) + (x >> 16);
        x = (x & 0xffff) + (x >> 16);
        /* add up carry.. */
        /* add up carry.. */
        x = (x & 0xffff) + (x >> 16);
        x = (x & 0xffff) + (x >> 16);
        return x;
        return x;
}
}
 
 
static unsigned long do_csum(const unsigned char * buff, int len)
static unsigned long do_csum(const unsigned char * buff, int len)
{
{
        int odd, count;
        int odd, count;
        unsigned long result = 0;
        unsigned long result = 0;
 
 
        if (len <= 0)
        if (len <= 0)
                goto out;
                goto out;
        odd = 1 & (unsigned long) buff;
        odd = 1 & (unsigned long) buff;
        if (odd) {
        if (odd) {
                result = *buff;
                result = *buff;
                len--;
                len--;
                buff++;
                buff++;
        }
        }
        count = len >> 1;               /* nr of 16-bit words.. */
        count = len >> 1;               /* nr of 16-bit words.. */
        if (count) {
        if (count) {
                if (2 & (unsigned long) buff) {
                if (2 & (unsigned long) buff) {
                        result += *(unsigned short *) buff;
                        result += *(unsigned short *) buff;
                        count--;
                        count--;
                        len -= 2;
                        len -= 2;
                        buff += 2;
                        buff += 2;
                }
                }
                count >>= 1;            /* nr of 32-bit words.. */
                count >>= 1;            /* nr of 32-bit words.. */
                if (count) {
                if (count) {
                        unsigned long carry = 0;
                        unsigned long carry = 0;
                        do {
                        do {
                                unsigned long w = *(unsigned long *) buff;
                                unsigned long w = *(unsigned long *) buff;
                                count--;
                                count--;
                                buff += 4;
                                buff += 4;
                                result += carry;
                                result += carry;
                                result += w;
                                result += w;
                                carry = (w > result);
                                carry = (w > result);
                        } while (count);
                        } while (count);
                        result += carry;
                        result += carry;
                        result = (result & 0xffff) + (result >> 16);
                        result = (result & 0xffff) + (result >> 16);
                }
                }
                if (len & 2) {
                if (len & 2) {
                        result += *(unsigned short *) buff;
                        result += *(unsigned short *) buff;
                        buff += 2;
                        buff += 2;
                }
                }
        }
        }
        if (len & 1)
        if (len & 1)
                result += (*buff << 8);
                result += (*buff << 8);
        result = from32to16(result);
        result = from32to16(result);
        if (odd)
        if (odd)
                result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
                result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
out:
out:
        return result;
        return result;
}
}
 
 
/*
/*
 *      This is a version of ip_compute_csum() optimized for IP headers,
 *      This is a version of ip_compute_csum() optimized for IP headers,
 *      which always checksum on 4 octet boundaries.
 *      which always checksum on 4 octet boundaries.
 */
 */
unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl)
unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl)
{
{
        return ~do_csum(iph,ihl*4);
        return ~do_csum(iph,ihl*4);
}
}
 
 
/*
/*
 * computes the checksum of a memory block at buff, length len,
 * computes the checksum of a memory block at buff, length len,
 * and adds in "sum" (32-bit)
 * and adds in "sum" (32-bit)
 *
 *
 * returns a 32-bit number suitable for feeding into itself
 * returns a 32-bit number suitable for feeding into itself
 * or csum_tcpudp_magic
 * or csum_tcpudp_magic
 *
 *
 * this function must be called with even lengths, except
 * this function must be called with even lengths, except
 * for the last fragment, which may be odd
 * for the last fragment, which may be odd
 *
 *
 * it's best to have buff aligned on a 32-bit boundary
 * it's best to have buff aligned on a 32-bit boundary
 */
 */
unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
{
{
        unsigned int result = do_csum(buff, len);
        unsigned int result = do_csum(buff, len);
 
 
        /* add in old sum, and carry.. */
        /* add in old sum, and carry.. */
        result += sum;
        result += sum;
        /* 16+c bits -> 16 bits */
        /* 16+c bits -> 16 bits */
        result = (result & 0xffff) + (result >> 16);
        result = (result & 0xffff) + (result >> 16);
        return result;
        return result;
}
}
 
 
/*
/*
 * 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(const unsigned char * buff, int len)
unsigned short ip_compute_csum(const unsigned char * buff, int len)
{
{
        return ~do_csum(buff,len);
        return ~do_csum(buff,len);
}
}
 
 
/*
/*
 * copy from fs while checksumming, otherwise like csum_partial
 * copy from fs while checksumming, otherwise like csum_partial
 */
 */
 
 
unsigned int
unsigned int
csum_partial_copy_fromuser(const char *src, char *dst, int len, int sum)
csum_partial_copy_fromuser(const char *src, char *dst, int len, int sum)
{
{
        memcpy(dst, src, len);
        memcpy(dst, src, len);
        return csum_partial(dst, len, sum);
        return csum_partial(dst, len, sum);
}
}
 
 
/*
/*
 * copy from ds while checksumming, otherwise like csum_partial
 * copy from ds while checksumming, otherwise like csum_partial
 */
 */
 
 
unsigned int
unsigned int
csum_partial_copy(const char *src, char *dst, int len, int sum)
csum_partial_copy(const char *src, char *dst, int len, int sum)
{
{
        memcpy(dst, src, len);
        memcpy(dst, src, len);
        return csum_partial(dst, len, sum);
        return csum_partial(dst, len, sum);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.