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

Cache object: 05889cb23d3c2bb2eab7678138c19559


[ 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.