Annotation of kernel/bsd/netns/ns_cksum.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
        !             3:  *
        !             4:  * @APPLE_LICENSE_HEADER_START@
        !             5:  * 
        !             6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
        !             7:  * Reserved.  This file contains Original Code and/or Modifications of
        !             8:  * Original Code as defined in and that are subject to the Apple Public
        !             9:  * Source License Version 1.1 (the "License").  You may not use this file
        !            10:  * except in compliance with the License.  Please obtain a copy of the
        !            11:  * License at http://www.apple.com/publicsource and read it before using
        !            12:  * this file.
        !            13:  * 
        !            14:  * The Original Code and all software distributed under the License are
        !            15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
        !            16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
        !            17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
        !            18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
        !            19:  * License for the specific language governing rights and limitations
        !            20:  * under the License.
        !            21:  * 
        !            22:  * @APPLE_LICENSE_HEADER_END@
        !            23:  */
        !            24: 
        !            25: /*
        !            26:  * Copyright (c) 1982, 1992, 1993
        !            27:  *     The Regents of the University of California.  All rights reserved.
        !            28:  *
        !            29:  * Redistribution and use in source and binary forms, with or without
        !            30:  * modification, are permitted provided that the following conditions
        !            31:  * are met:
        !            32:  * 1. Redistributions of source code must retain the above copyright
        !            33:  *    notice, this list of conditions and the following disclaimer.
        !            34:  * 2. Redistributions in binary form must reproduce the above copyright
        !            35:  *    notice, this list of conditions and the following disclaimer in the
        !            36:  *    documentation and/or other materials provided with the distribution.
        !            37:  * 3. All advertising materials mentioning features or use of this software
        !            38:  *    must display the following acknowledgement:
        !            39:  *     This product includes software developed by the University of
        !            40:  *     California, Berkeley and its contributors.
        !            41:  * 4. Neither the name of the University nor the names of its contributors
        !            42:  *    may be used to endorse or promote products derived from this software
        !            43:  *    without specific prior written permission.
        !            44:  *
        !            45:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            46:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            47:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            48:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            49:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            50:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            51:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            52:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            53:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            54:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            55:  * SUCH DAMAGE.
        !            56:  *
        !            57:  *     @(#)ns_cksum.c  8.1 (Berkeley) 6/10/93
        !            58:  */
        !            59: 
        !            60: #include <sys/param.h>
        !            61: #include <sys/mbuf.h>
        !            62: 
        !            63: /*
        !            64:  * Checksum routine for Network Systems Protocol Packets (Big-Endian).
        !            65:  *
        !            66:  * This routine is very heavily used in the network
        !            67:  * code and should be modified for each CPU to be as fast as possible.
        !            68:  */
        !            69: 
        !            70: #define ADDCARRY(x)  { if ((x) > 65535) (x) -= 65535; }
        !            71: #define FOLD(x) {l_util.l = (x); (x) = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
        !            72: 
        !            73: u_short
        !            74: ns_cksum(m, len)
        !            75:        register struct mbuf *m;
        !            76:        register int len;
        !            77: {
        !            78:        register u_short *w;
        !            79:        register int sum = 0;
        !            80:        register int mlen = 0;
        !            81:        register int sum2;
        !            82: 
        !            83:        union {
        !            84:                u_short s[2];
        !            85:                long    l;
        !            86:        } l_util;
        !            87: 
        !            88:        for (;m && len; m = m->m_next) {
        !            89:                if (m->m_len == 0)
        !            90:                        continue;
        !            91:                /*
        !            92:                 * Each trip around loop adds in
        !            93:                 * word from one mbuf segment.
        !            94:                 */
        !            95:                w = mtod(m, u_short *);
        !            96:                if (mlen == -1) {
        !            97:                        /*
        !            98:                         * There is a byte left from the last segment;
        !            99:                         * ones-complement add it into the checksum.
        !           100:                         */
        !           101: #if BYTE_ORDER == BIG_ENDIAN
        !           102:                        sum  += *(u_char *)w;
        !           103: #else
        !           104:                        sum  += *(u_char *)w << 8;
        !           105: #endif
        !           106:                        sum += sum;
        !           107:                        w = (u_short *)(1 + (char *)w);
        !           108:                        mlen = m->m_len - 1;
        !           109:                        len--;
        !           110:                        FOLD(sum);
        !           111:                } else
        !           112:                        mlen = m->m_len;
        !           113:                if (len < mlen)
        !           114:                        mlen = len;
        !           115:                len -= mlen;
        !           116:                /*
        !           117:                 * We can do a 16 bit ones complement sum using
        !           118:                 * 32 bit arithmetic registers for adding,
        !           119:                 * with carries from the low added
        !           120:                 * into the high (by normal carry-chaining)
        !           121:                 * so long as we fold back before 16 carries have occured.
        !           122:                 */
        !           123:                if (1 & (int) w)
        !           124:                        goto uuuuglyy;
        !           125: #ifndef TINY
        !           126: /* -DTINY reduces the size from 1250 to 550, but slows it down by 22% */
        !           127:                while ((mlen -= 32) >= 0) {
        !           128:                        sum += w[0]; sum += sum; sum += w[1]; sum += sum;
        !           129:                        sum += w[2]; sum += sum; sum += w[3]; sum += sum;
        !           130:                        sum += w[4]; sum += sum; sum += w[5]; sum += sum;
        !           131:                        sum += w[6]; sum += sum; sum += w[7]; sum += sum;
        !           132:                        FOLD(sum);
        !           133:                        sum += w[8]; sum += sum; sum += w[9]; sum += sum;
        !           134:                        sum += w[10]; sum += sum; sum += w[11]; sum += sum;
        !           135:                        sum += w[12]; sum += sum; sum += w[13]; sum += sum;
        !           136:                        sum += w[14]; sum += sum; sum += w[15]; sum += sum;
        !           137:                        FOLD(sum);
        !           138:                        w += 16;
        !           139:                }
        !           140:                mlen += 32;
        !           141: #endif
        !           142:                while ((mlen -= 8) >= 0) {
        !           143:                        sum += w[0]; sum += sum; sum += w[1]; sum += sum;
        !           144:                        sum += w[2]; sum += sum; sum += w[3]; sum += sum;
        !           145:                        FOLD(sum);
        !           146:                        w += 4;
        !           147:                }
        !           148:                mlen += 8;
        !           149:                while ((mlen -= 2) >= 0) {
        !           150:                        sum += *w++; sum += sum;
        !           151:                }
        !           152:                goto commoncase;
        !           153: uuuuglyy:
        !           154: #if BYTE_ORDER == BIG_ENDIAN
        !           155: #define ww(n) (((u_char *)w)[n + n + 1])
        !           156: #define vv(n) (((u_char *)w)[n + n])
        !           157: #else
        !           158: #if BYTE_ORDER == LITTLE_ENDIAN
        !           159: #define vv(n) (((u_char *)w)[n + n + 1])
        !           160: #define ww(n) (((u_char *)w)[n + n])
        !           161: #endif
        !           162: #endif
        !           163:                sum2 = 0;
        !           164: #ifndef TINY
        !           165:                while ((mlen -= 32) >= 0) {
        !           166:                    sum += ww(0); sum += sum; sum += ww(1); sum += sum;
        !           167:                    sum += ww(2); sum += sum; sum += ww(3); sum += sum;
        !           168:                    sum += ww(4); sum += sum; sum += ww(5); sum += sum;
        !           169:                    sum += ww(6); sum += sum; sum += ww(7); sum += sum;
        !           170:                    FOLD(sum);
        !           171:                    sum += ww(8); sum += sum; sum += ww(9); sum += sum;
        !           172:                    sum += ww(10); sum += sum; sum += ww(11); sum += sum;
        !           173:                    sum += ww(12); sum += sum; sum += ww(13); sum += sum;
        !           174:                    sum += ww(14); sum += sum; sum += ww(15); sum += sum;
        !           175:                    FOLD(sum);
        !           176:                    sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
        !           177:                    sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
        !           178:                    sum2 += vv(4); sum2 += sum2; sum2 += vv(5); sum2 += sum2;
        !           179:                    sum2 += vv(6); sum2 += sum2; sum2 += vv(7); sum2 += sum2;
        !           180:                    FOLD(sum2);
        !           181:                    sum2 += vv(8); sum2 += sum2; sum2 += vv(9); sum2 += sum2;
        !           182:                    sum2 += vv(10); sum2 += sum2; sum2 += vv(11); sum2 += sum2;
        !           183:                    sum2 += vv(12); sum2 += sum2; sum2 += vv(13); sum2 += sum2;
        !           184:                    sum2 += vv(14); sum2 += sum2; sum2 += vv(15); sum2 += sum2;
        !           185:                    FOLD(sum2);
        !           186:                    w += 16;
        !           187:                }
        !           188:                mlen += 32;
        !           189: #endif
        !           190:                while ((mlen -= 8) >= 0) {
        !           191:                    sum += ww(0); sum += sum; sum += ww(1); sum += sum;
        !           192:                    sum += ww(2); sum += sum; sum += ww(3); sum += sum;
        !           193:                    FOLD(sum);
        !           194:                    sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
        !           195:                    sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
        !           196:                    FOLD(sum2);
        !           197:                    w += 4;
        !           198:                }
        !           199:                mlen += 8;
        !           200:                while ((mlen -= 2) >= 0) {
        !           201:                        sum += ww(0); sum += sum;
        !           202:                        sum2 += vv(0); sum2 += sum2;
        !           203:                        w++;
        !           204:                }
        !           205:                sum += (sum2 << 8);
        !           206: commoncase:
        !           207:                if (mlen == -1) {
        !           208: #if BYTE_ORDER == BIG_ENDIAN
        !           209:                        sum += *(u_char *)w << 8;
        !           210: #else
        !           211:                        sum += *(u_char *)w;
        !           212: #endif
        !           213:                }
        !           214:                FOLD(sum);
        !           215:        }
        !           216:        if (mlen == -1) {
        !           217:                /* We had an odd number of bytes to sum; assume a garbage
        !           218:                   byte of zero and clean up */
        !           219:                sum += sum;
        !           220:                FOLD(sum);
        !           221:        }
        !           222:        /*
        !           223:         * sum has already been kept to low sixteen bits.
        !           224:         * just examine result and exit.
        !           225:         */
        !           226:        if(sum==0xffff) sum = 0;
        !           227:        return (sum);
        !           228: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.