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

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

unix.superglobalmegacorp.com

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