The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netns/ns_cksum.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: bfebac232ac471db97ae447bfe64e2b3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.