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/i386/i386/in_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) 1990 The Regents of the University of California.
    3  * 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  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      from tahoe:     in_cksum.c      1.2     86/01/05
   30  *      from:           @(#)in_cksum.c  1.3 (Berkeley) 1/19/91
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD$");
   35 
   36 /*
   37  * MPsafe: alfred
   38  */
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/mbuf.h>
   42 
   43 #include <netinet/in.h>
   44 #include <netinet/in_systm.h>
   45 #include <netinet/ip.h>
   46 
   47 #include <machine/in_cksum.h>
   48 
   49 /*
   50  * Checksum routine for Internet Protocol family headers.
   51  *
   52  * This routine is very heavily used in the network
   53  * code and should be modified for each CPU to be as fast as possible.
   54  *
   55  * This implementation is 386 version.
   56  */
   57 
   58 #undef  ADDCARRY
   59 #define ADDCARRY(x)     if ((x) > 0xffff) (x) -= 0xffff
   60 #if !defined(__GNUC__) || defined(__INTEL_COMPILER)
   61 /* non gcc parts stolen from sys/alpha/alpha/in_cksum.c */
   62 #define REDUCE32                                                          \
   63     {                                                                     \
   64         q_util.q = sum;                                                   \
   65         sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3];      \
   66     }
   67 #define REDUCE16                                                          \
   68     {                                                                     \
   69         q_util.q = sum;                                                   \
   70         l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
   71         sum = l_util.s[0] + l_util.s[1];                                  \
   72         ADDCARRY(sum);                                                    \
   73     }
   74 #endif
   75 #define REDUCE          {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY(sum);}
   76 
   77 #if !defined(__GNUC__) || defined(__INTEL_COMPILER)
   78 static const u_int32_t in_masks[] = {
   79         /*0 bytes*/ /*1 byte*/  /*2 bytes*/ /*3 bytes*/
   80         0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
   81         0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
   82         0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
   83         0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
   84 };
   85 
   86 union l_util {
   87         u_int16_t s[2];
   88         u_int32_t l;
   89 };
   90 union q_util {
   91         u_int16_t s[4];
   92         u_int32_t l[2];
   93         u_int64_t q;
   94 };
   95 
   96 static u_int64_t
   97 in_cksumdata(const u_int32_t *lw, int len)
   98 {
   99         u_int64_t sum = 0;
  100         u_int64_t prefilled;
  101         int offset;
  102         union q_util q_util;
  103 
  104         if ((3 & (long) lw) == 0 && len == 20) {
  105              sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
  106              REDUCE32;
  107              return sum;
  108         }
  109 
  110         if ((offset = 3 & (long) lw) != 0) {
  111                 const u_int32_t *masks = in_masks + (offset << 2);
  112                 lw = (u_int32_t *) (((long) lw) - offset);
  113                 sum = *lw++ & masks[len >= 3 ? 3 : len];
  114                 len -= 4 - offset;
  115                 if (len <= 0) {
  116                         REDUCE32;
  117                         return sum;
  118                 }
  119         }
  120 #if 0
  121         /*
  122          * Force to cache line boundary.
  123          */
  124         offset = 32 - (0x1f & (long) lw);
  125         if (offset < 32 && len > offset) {
  126                 len -= offset;
  127                 if (4 & offset) {
  128                         sum += (u_int64_t) lw[0];
  129                         lw += 1;
  130                 }
  131                 if (8 & offset) {
  132                         sum += (u_int64_t) lw[0] + lw[1];
  133                         lw += 2;
  134                 }
  135                 if (16 & offset) {
  136                         sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
  137                         lw += 4;
  138                 }
  139         }
  140 #endif
  141         /*
  142          * access prefilling to start load of next cache line.
  143          * then add current cache line
  144          * save result of prefilling for loop iteration.
  145          */
  146         prefilled = lw[0];
  147         while ((len -= 32) >= 4) {
  148                 u_int64_t prefilling = lw[8];
  149                 sum += prefilled + lw[1] + lw[2] + lw[3]
  150                         + lw[4] + lw[5] + lw[6] + lw[7];
  151                 lw += 8;
  152                 prefilled = prefilling;
  153         }
  154         if (len >= 0) {
  155                 sum += prefilled + lw[1] + lw[2] + lw[3]
  156                         + lw[4] + lw[5] + lw[6] + lw[7];
  157                 lw += 8;
  158         } else {
  159                 len += 32;
  160         }
  161         while ((len -= 16) >= 0) {
  162                 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
  163                 lw += 4;
  164         }
  165         len += 16;
  166         while ((len -= 4) >= 0) {
  167                 sum += (u_int64_t) *lw++;
  168         }
  169         len += 4;
  170         if (len > 0)
  171                 sum += (u_int64_t) (in_masks[len] & *lw);
  172         REDUCE32;
  173         return sum;
  174 }
  175 
  176 u_short
  177 in_addword(u_short a, u_short b)
  178 {
  179         u_int64_t sum = a + b;
  180 
  181         ADDCARRY(sum);
  182         return (sum);
  183 }
  184 
  185 u_short
  186 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
  187 {
  188         u_int64_t sum;
  189         union q_util q_util;
  190         union l_util l_util;
  191                     
  192         sum = (u_int64_t) a + b + c;
  193         REDUCE16;
  194         return (sum);
  195 }
  196 
  197 u_short
  198 in_cksum_skip(struct mbuf *m, int len, int skip)
  199 {
  200         u_int64_t sum = 0;
  201         int mlen = 0;
  202         int clen = 0;
  203         caddr_t addr;
  204         union q_util q_util;
  205         union l_util l_util;
  206 
  207         len -= skip;
  208         for (; skip && m; m = m->m_next) {
  209                 if (m->m_len > skip) {
  210                         mlen = m->m_len - skip;
  211                         addr = mtod(m, caddr_t) + skip;
  212                         goto skip_start;
  213                 } else {
  214                         skip -= m->m_len;
  215                 }
  216         }
  217 
  218         for (; m && len; m = m->m_next) {
  219                 if (m->m_len == 0)
  220                         continue;
  221                 mlen = m->m_len;
  222                 addr = mtod(m, caddr_t);
  223 skip_start:
  224                 if (len < mlen)
  225                         mlen = len;
  226                 if ((clen ^ (long) addr) & 1)
  227                     sum += in_cksumdata((const u_int32_t *)addr, mlen) << 8;
  228                 else
  229                     sum += in_cksumdata((const u_int32_t *)addr, mlen);
  230 
  231                 clen += mlen;
  232                 len -= mlen;
  233         }
  234         REDUCE16;
  235         return (~sum & 0xffff);
  236 }
  237 
  238 u_int in_cksum_hdr(const struct ip *ip)
  239 {
  240     u_int64_t sum = in_cksumdata((const u_int32_t *)ip, sizeof(struct ip));
  241     union q_util q_util;
  242     union l_util l_util;
  243 
  244     REDUCE16;
  245     return (~sum & 0xffff);
  246 }
  247 #else
  248 
  249 /*
  250  * These asm statements require __volatile because they pass information
  251  * via the condition codes.  GCC does not currently provide a way to specify
  252  * the condition codes as an input or output operand.
  253  *
  254  * The LOAD macro below is effectively a prefetch into cache.  GCC will
  255  * load the value into a register but will not use it.  Since modern CPUs
  256  * reorder operations, this will generally take place in parallel with
  257  * other calculations.
  258  */
  259 #define ADD(n)  __asm __volatile \
  260                 ("addl %1, %0" : "+r" (sum) : \
  261                 "g" (((const u_int32_t *)w)[n / 4]))
  262 #define ADDC(n) __asm __volatile \
  263                 ("adcl %1, %0" : "+r" (sum) : \
  264                 "g" (((const u_int32_t *)w)[n / 4]))
  265 #define LOAD(n) __asm __volatile \
  266                 ("" : : "r" (((const u_int32_t *)w)[n / 4]))
  267 #define MOP     __asm __volatile \
  268                 ("adcl         $0, %0" : "+r" (sum))
  269 
  270 u_short
  271 in_cksum_skip(m, len, skip)
  272         struct mbuf *m;
  273         int len;
  274         int skip;
  275 {
  276         register u_short *w;
  277         register unsigned sum = 0;
  278         register int mlen = 0;
  279         int byte_swapped = 0;
  280         union { char    c[2]; u_short   s; } su;
  281 
  282         len -= skip;
  283         for (; skip && m; m = m->m_next) {
  284                 if (m->m_len > skip) {
  285                         mlen = m->m_len - skip;
  286                         w = (u_short *)(mtod(m, u_char *) + skip);
  287                         goto skip_start;
  288                 } else {
  289                         skip -= m->m_len;
  290                 }
  291         }
  292 
  293         for (;m && len; m = m->m_next) {
  294                 if (m->m_len == 0)
  295                         continue;
  296                 w = mtod(m, u_short *);
  297                 if (mlen == -1) {
  298                         /*
  299                          * The first byte of this mbuf is the continuation
  300                          * of a word spanning between this mbuf and the
  301                          * last mbuf.
  302                          */
  303 
  304                         /* su.c[0] is already saved when scanning previous
  305                          * mbuf.  sum was REDUCEd when we found mlen == -1
  306                          */
  307                         su.c[1] = *(u_char *)w;
  308                         sum += su.s;
  309                         w = (u_short *)((char *)w + 1);
  310                         mlen = m->m_len - 1;
  311                         len--;
  312                 } else
  313                         mlen = m->m_len;
  314 skip_start:
  315                 if (len < mlen)
  316                         mlen = len;
  317                 len -= mlen;
  318                 /*
  319                  * Force to long boundary so we do longword aligned
  320                  * memory operations
  321                  */
  322                 if (3 & (int) w) {
  323                         REDUCE;
  324                         if ((1 & (int) w) && (mlen > 0)) {
  325                                 sum <<= 8;
  326                                 su.c[0] = *(char *)w;
  327                                 w = (u_short *)((char *)w + 1);
  328                                 mlen--;
  329                                 byte_swapped = 1;
  330                         }
  331                         if ((2 & (int) w) && (mlen >= 2)) {
  332                                 sum += *w++;
  333                                 mlen -= 2;
  334                         }
  335                 }
  336                 /*
  337                  * Advance to a 486 cache line boundary.
  338                  */
  339                 if (4 & (int) w && mlen >= 4) {
  340                         ADD(0);
  341                         MOP;
  342                         w += 2;
  343                         mlen -= 4;
  344                 }
  345                 if (8 & (int) w && mlen >= 8) {
  346                         ADD(0);
  347                         ADDC(4);
  348                         MOP;
  349                         w += 4;
  350                         mlen -= 8;
  351                 }
  352                 /*
  353                  * Do as much of the checksum as possible 32 bits at at time.
  354                  * In fact, this loop is unrolled to make overhead from
  355                  * branches &c small.
  356                  */
  357                 mlen -= 1;
  358                 while ((mlen -= 32) >= 0) {
  359                         /*
  360                          * Add with carry 16 words and fold in the last
  361                          * carry by adding a 0 with carry.
  362                          *
  363                          * The early ADD(16) and the LOAD(32) are to load
  364                          * the next 2 cache lines in advance on 486's.  The
  365                          * 486 has a penalty of 2 clock cycles for loading
  366                          * a cache line, plus whatever time the external
  367                          * memory takes to load the first word(s) addressed.
  368                          * These penalties are unavoidable.  Subsequent
  369                          * accesses to a cache line being loaded (and to
  370                          * other external memory?) are delayed until the
  371                          * whole load finishes.  These penalties are mostly
  372                          * avoided by not accessing external memory for
  373                          * 8 cycles after the ADD(16) and 12 cycles after
  374                          * the LOAD(32).  The loop terminates when mlen
  375                          * is initially 33 (not 32) to guaranteed that
  376                          * the LOAD(32) is within bounds.
  377                          */
  378                         ADD(16);
  379                         ADDC(0);
  380                         ADDC(4);
  381                         ADDC(8);
  382                         ADDC(12);
  383                         LOAD(32);
  384                         ADDC(20);
  385                         ADDC(24);
  386                         ADDC(28);
  387                         MOP;
  388                         w += 16;
  389                 }
  390                 mlen += 32 + 1;
  391                 if (mlen >= 32) {
  392                         ADD(16);
  393                         ADDC(0);
  394                         ADDC(4);
  395                         ADDC(8);
  396                         ADDC(12);
  397                         ADDC(20);
  398                         ADDC(24);
  399                         ADDC(28);
  400                         MOP;
  401                         w += 16;
  402                         mlen -= 32;
  403                 }
  404                 if (mlen >= 16) {
  405                         ADD(0);
  406                         ADDC(4);
  407                         ADDC(8);
  408                         ADDC(12);
  409                         MOP;
  410                         w += 8;
  411                         mlen -= 16;
  412                 }
  413                 if (mlen >= 8) {
  414                         ADD(0);
  415                         ADDC(4);
  416                         MOP;
  417                         w += 4;
  418                         mlen -= 8;
  419                 }
  420                 if (mlen == 0 && byte_swapped == 0)
  421                         continue;       /* worth 1% maybe ?? */
  422                 REDUCE;
  423                 while ((mlen -= 2) >= 0) {
  424                         sum += *w++;
  425                 }
  426                 if (byte_swapped) {
  427                         sum <<= 8;
  428                         byte_swapped = 0;
  429                         if (mlen == -1) {
  430                                 su.c[1] = *(char *)w;
  431                                 sum += su.s;
  432                                 mlen = 0;
  433                         } else
  434                                 mlen = -1;
  435                 } else if (mlen == -1)
  436                         /*
  437                          * This mbuf has odd number of bytes.
  438                          * There could be a word split betwen
  439                          * this mbuf and the next mbuf.
  440                          * Save the last byte (to prepend to next mbuf).
  441                          */
  442                         su.c[0] = *(char *)w;
  443         }
  444 
  445         if (len)
  446                 printf("%s: out of data by %d\n", __func__, len);
  447         if (mlen == -1) {
  448                 /* The last mbuf has odd # of bytes. Follow the
  449                    standard (the odd byte is shifted left by 8 bits) */
  450                 su.c[1] = 0;
  451                 sum += su.s;
  452         }
  453         REDUCE;
  454         return (~sum & 0xffff);
  455 }
  456 #endif

Cache object: f16be6cb34b39514fce3f048e321485a


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