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/amd64/amd64/in_cksum.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /* $NetBSD: in_cksum.c,v 1.7 1997/09/02 13:18:15 thorpej Exp $ */
    2 
    3 /*
    4  * Copyright (c) 1988, 1992, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  * Copyright (c) 1996
    7  *      Matt Thomas <matt@3am-software.com>
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 3. All advertising materials mentioning features or use of this software
   18  *    must display the following acknowledgement:
   19  *      This product includes software developed by the University of
   20  *      California, Berkeley and its contributors.
   21  * 4. Neither the name of the University nor the names of its contributors
   22  *    may be used to endorse or promote products derived from this software
   23  *    without specific prior written permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   35  * SUCH DAMAGE.
   36  *
   37  *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
   38  */
   39 
   40 #include <sys/cdefs.h>                  /* RCS ID & Copyright macro defns */
   41 __FBSDID("$FreeBSD: releng/5.2/sys/amd64/amd64/in_cksum.c 118031 2003-07-25 21:19:19Z obrien $");
   42 
   43 #include <sys/param.h>
   44 #include <sys/mbuf.h>
   45 #include <sys/systm.h>
   46 #include <netinet/in_systm.h>
   47 #include <netinet/in.h>
   48 #include <netinet/ip.h>
   49 #include <machine/in_cksum.h>
   50 
   51 /*
   52  * Checksum routine for Internet Protocol family headers
   53  *    (Portable Alpha version).
   54  *
   55  * This routine is very heavily used in the network
   56  * code and should be modified for each CPU to be as fast as possible.
   57  */
   58 
   59 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
   60 #define REDUCE32                                                          \
   61     {                                                                     \
   62         q_util.q = sum;                                                   \
   63         sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3];      \
   64     }
   65 #define REDUCE16                                                          \
   66     {                                                                     \
   67         q_util.q = sum;                                                   \
   68         l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
   69         sum = l_util.s[0] + l_util.s[1];                                  \
   70         ADDCARRY(sum);                                                    \
   71     }
   72 
   73 static const u_int32_t in_masks[] = {
   74         /*0 bytes*/ /*1 byte*/  /*2 bytes*/ /*3 bytes*/
   75         0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
   76         0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
   77         0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
   78         0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
   79 };
   80 
   81 union l_util {
   82         u_int16_t s[2];
   83         u_int32_t l;
   84 };
   85 union q_util {
   86         u_int16_t s[4];
   87         u_int32_t l[2];
   88         u_int64_t q;
   89 };
   90 
   91 static u_int64_t
   92 in_cksumdata(const void *buf, int len)
   93 {
   94         const u_int32_t *lw = (const u_int32_t *) buf;
   95         u_int64_t sum = 0;
   96         u_int64_t prefilled;
   97         int offset;
   98         union q_util q_util;
   99 
  100         if ((3 & (long) lw) == 0 && len == 20) {
  101              sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
  102              REDUCE32;
  103              return sum;
  104         }
  105 
  106         if ((offset = 3 & (long) lw) != 0) {
  107                 const u_int32_t *masks = in_masks + (offset << 2);
  108                 lw = (u_int32_t *) (((long) lw) - offset);
  109                 sum = *lw++ & masks[len >= 3 ? 3 : len];
  110                 len -= 4 - offset;
  111                 if (len <= 0) {
  112                         REDUCE32;
  113                         return sum;
  114                 }
  115         }
  116 #if 0
  117         /*
  118          * Force to cache line boundary.
  119          */
  120         offset = 32 - (0x1f & (long) lw);
  121         if (offset < 32 && len > offset) {
  122                 len -= offset;
  123                 if (4 & offset) {
  124                         sum += (u_int64_t) lw[0];
  125                         lw += 1;
  126                 }
  127                 if (8 & offset) {
  128                         sum += (u_int64_t) lw[0] + lw[1];
  129                         lw += 2;
  130                 }
  131                 if (16 & offset) {
  132                         sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
  133                         lw += 4;
  134                 }
  135         }
  136 #endif
  137         /*
  138          * access prefilling to start load of next cache line.
  139          * then add current cache line
  140          * save result of prefilling for loop iteration.
  141          */
  142         prefilled = lw[0];
  143         while ((len -= 32) >= 4) {
  144                 u_int64_t prefilling = lw[8];
  145                 sum += prefilled + lw[1] + lw[2] + lw[3]
  146                         + lw[4] + lw[5] + lw[6] + lw[7];
  147                 lw += 8;
  148                 prefilled = prefilling;
  149         }
  150         if (len >= 0) {
  151                 sum += prefilled + lw[1] + lw[2] + lw[3]
  152                         + lw[4] + lw[5] + lw[6] + lw[7];
  153                 lw += 8;
  154         } else {
  155                 len += 32;
  156         }
  157         while ((len -= 16) >= 0) {
  158                 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
  159                 lw += 4;
  160         }
  161         len += 16;
  162         while ((len -= 4) >= 0) {
  163                 sum += (u_int64_t) *lw++;
  164         }
  165         len += 4;
  166         if (len > 0)
  167                 sum += (u_int64_t) (in_masks[len] & *lw);
  168         REDUCE32;
  169         return sum;
  170 }
  171 
  172 u_short
  173 in_addword(u_short a, u_short b)
  174 {
  175         u_int64_t sum = a + b;
  176 
  177         ADDCARRY(sum);
  178         return (sum);
  179 }
  180 
  181 u_short
  182 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
  183 {
  184         u_int64_t sum;
  185         union q_util q_util;
  186         union l_util l_util;
  187                     
  188         sum = (u_int64_t) a + b + c;
  189         REDUCE16;
  190         return (sum);
  191 }
  192 
  193 u_short
  194 in_cksum_skip(struct mbuf *m, int len, int skip)
  195 {
  196         u_int64_t sum = 0;
  197         int mlen = 0;
  198         int clen = 0;
  199         caddr_t addr;
  200         union q_util q_util;
  201         union l_util l_util;
  202 
  203         len -= skip;
  204         for (; skip && m; m = m->m_next) {
  205                 if (m->m_len > skip) {
  206                         mlen = m->m_len - skip;
  207                         addr = mtod(m, caddr_t) + skip;
  208                         goto skip_start;
  209                 } else {
  210                         skip -= m->m_len;
  211                 }
  212         }
  213 
  214         for (; m && len; m = m->m_next) {
  215                 if (m->m_len == 0)
  216                         continue;
  217                 mlen = m->m_len;
  218                 addr = mtod(m, caddr_t);
  219 skip_start:
  220                 if (len < mlen)
  221                         mlen = len;
  222                 if ((clen ^ (long) addr) & 1)
  223                     sum += in_cksumdata(addr, mlen) << 8;
  224                 else
  225                     sum += in_cksumdata(addr, mlen);
  226 
  227                 clen += mlen;
  228                 len -= mlen;
  229         }
  230         REDUCE16;
  231         return (~sum & 0xffff);
  232 }
  233 
  234 u_int in_cksum_hdr(const struct ip *ip)
  235 {
  236     u_int64_t sum = in_cksumdata(ip, sizeof(struct ip));
  237     union q_util q_util;
  238     union l_util l_util;
  239     REDUCE16;
  240     return (~sum & 0xffff);
  241 }

Cache object: f26c720fbaa67bd4fc11ee414f85ef1a


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