2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * from tahoe: in_cksum.c 1.2 86/01/05
34 * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91
35 * $FreeBSD: src/sys/i386/i386/in_cksum.c,v 1.17.2.3 2002/07/02 04:03:00 jdp Exp $
36 * $DragonFly: src/sys/i386/i386/Attic/in_cksum.c,v 1.3 2003/07/26 19:07:47 rob Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
47 #include <machine/in_cksum.h>
50 * Checksum routine for Internet Protocol family headers.
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.
55 * This implementation is 386 version.
59 #define ADDCARRY(x) if ((x) > 0xffff) (x) -= 0xffff
60 #define REDUCE {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY(sum);}
63 * These asm statements require __volatile because they pass information
64 * via the condition codes. GCC does not currently provide a way to specify
65 * the condition codes as an input or output operand.
67 * The LOAD macro below is effectively a prefetch into cache. GCC will
68 * load the value into a register but will not use it. Since modern CPUs
69 * reorder operations, this will generally take place in parallel with
72 #define ADD(n) __asm __volatile \
73 ("addl %1, %0" : "+r" (sum) : \
74 "g" (((const u_int32_t *)w)[n / 4]))
75 #define ADDC(n) __asm __volatile \
76 ("adcl %1, %0" : "+r" (sum) : \
77 "g" (((const u_int32_t *)w)[n / 4]))
78 #define LOAD(n) __asm __volatile \
79 ("" : : "r" (((const u_int32_t *)w)[n / 4]))
80 #define MOP __asm __volatile \
81 ("adcl $0, %0" : "+r" (sum))
92 union { char c[2]; u_short s; } su;
94 for (;m && len; m = m->m_next) {
97 w = mtod(m, u_short *);
100 * The first byte of this mbuf is the continuation
101 * of a word spanning between this mbuf and the
105 /* su.c[0] is already saved when scanning previous
106 * mbuf. sum was REDUCEd when we found mlen == -1
108 su.c[1] = *(u_char *)w;
110 w = (u_short *)((char *)w + 1);
119 * Force to long boundary so we do longword aligned
124 if ((1 & (int) w) && (mlen > 0)) {
126 su.c[0] = *(char *)w;
127 w = (u_short *)((char *)w + 1);
131 if ((2 & (int) w) && (mlen >= 2)) {
137 * Advance to a 486 cache line boundary.
139 if (4 & (int) w && mlen >= 4) {
145 if (8 & (int) w && mlen >= 8) {
153 * Do as much of the checksum as possible 32 bits at at time.
154 * In fact, this loop is unrolled to make overhead from
158 while ((mlen -= 32) >= 0) {
160 * Add with carry 16 words and fold in the last
161 * carry by adding a 0 with carry.
163 * The early ADD(16) and the LOAD(32) are to load
164 * the next 2 cache lines in advance on 486's. The
165 * 486 has a penalty of 2 clock cycles for loading
166 * a cache line, plus whatever time the external
167 * memory takes to load the first word(s) addressed.
168 * These penalties are unavoidable. Subsequent
169 * accesses to a cache line being loaded (and to
170 * other external memory?) are delayed until the
171 * whole load finishes. These penalties are mostly
172 * avoided by not accessing external memory for
173 * 8 cycles after the ADD(16) and 12 cycles after
174 * the LOAD(32). The loop terminates when mlen
175 * is initially 33 (not 32) to guaranteed that
176 * the LOAD(32) is within bounds.
220 if (mlen == 0 && byte_swapped == 0)
221 continue; /* worth 1% maybe ?? */
223 while ((mlen -= 2) >= 0) {
230 su.c[1] = *(char *)w;
235 } else if (mlen == -1)
237 * This mbuf has odd number of bytes.
238 * There could be a word split betwen
239 * this mbuf and the next mbuf.
240 * Save the last byte (to prepend to next mbuf).
242 su.c[0] = *(char *)w;
246 printf("%s: out of data by %d\n", __func__, len);
248 /* The last mbuf has odd # of bytes. Follow the
249 standard (the odd byte is shifted left by 8 bits) */
254 return (~sum & 0xffff);
258 in_cksum_skip(m, len, skip)
266 int byte_swapped = 0;
267 union { char c[2]; u_short s; } su;
270 for (; skip && m; m = m->m_next) {
271 if (m->m_len > skip) {
272 mlen = m->m_len - skip;
273 w = (u_short *)(mtod(m, u_char *) + skip);
280 for (;m && len; m = m->m_next) {
283 w = mtod(m, u_short *);
286 * The first byte of this mbuf is the continuation
287 * of a word spanning between this mbuf and the
291 /* su.c[0] is already saved when scanning previous
292 * mbuf. sum was REDUCEd when we found mlen == -1
294 su.c[1] = *(u_char *)w;
296 w = (u_short *)((char *)w + 1);
306 * Force to long boundary so we do longword aligned
311 if ((1 & (int) w) && (mlen > 0)) {
313 su.c[0] = *(char *)w;
314 w = (u_short *)((char *)w + 1);
318 if ((2 & (int) w) && (mlen >= 2)) {
324 * Advance to a 486 cache line boundary.
326 if (4 & (int) w && mlen >= 4) {
332 if (8 & (int) w && mlen >= 8) {
340 * Do as much of the checksum as possible 32 bits at at time.
341 * In fact, this loop is unrolled to make overhead from
345 while ((mlen -= 32) >= 0) {
347 * Add with carry 16 words and fold in the last
348 * carry by adding a 0 with carry.
350 * The early ADD(16) and the LOAD(32) are to load
351 * the next 2 cache lines in advance on 486's. The
352 * 486 has a penalty of 2 clock cycles for loading
353 * a cache line, plus whatever time the external
354 * memory takes to load the first word(s) addressed.
355 * These penalties are unavoidable. Subsequent
356 * accesses to a cache line being loaded (and to
357 * other external memory?) are delayed until the
358 * whole load finishes. These penalties are mostly
359 * avoided by not accessing external memory for
360 * 8 cycles after the ADD(16) and 12 cycles after
361 * the LOAD(32). The loop terminates when mlen
362 * is initially 33 (not 32) to guaranteed that
363 * the LOAD(32) is within bounds.
407 if (mlen == 0 && byte_swapped == 0)
408 continue; /* worth 1% maybe ?? */
410 while ((mlen -= 2) >= 0) {
417 su.c[1] = *(char *)w;
422 } else if (mlen == -1)
424 * This mbuf has odd number of bytes.
425 * There could be a word split betwen
426 * this mbuf and the next mbuf.
427 * Save the last byte (to prepend to next mbuf).
429 su.c[0] = *(char *)w;
433 printf("%s: out of data by %d\n", __func__, len);
435 /* The last mbuf has odd # of bytes. Follow the
436 standard (the odd byte is shifted left by 8 bits) */
441 return (~sum & 0xffff);
445 * This is the exact same algorithm as above with a few exceptions:
446 * (1) it is designed to operate on buffers, not mbufs
447 * (2) it returns an intermediate form of the sum which has to be
448 * explicitly finalized (but this can be delayed)
449 * (3) it accepts an intermediate sum
451 * This is particularly useful when building packets quickly,
452 * since one can compute the checksum of the pseudoheader ahead of
453 * time and then use this function to complete the work. That way,
454 * the pseudoheader never actually has to exist in the packet buffer,
455 * which avoids needless duplication of work.
458 in_cksum_partial(psum, w, len)
463 in_psum_t sum = psum;
464 int byte_swapped = 0;
465 union { char c[2]; u_short s; } su;
468 * Force to long boundary so we do longword aligned
473 if ((1 & (int) w) && (len > 0)) {
475 su.c[0] = *(const char *)w;
476 w = (const u_short *)((const char *)w + 1);
480 if ((2 & (int) w) && (len >= 2)) {
486 * Advance to a 486 cache line boundary.
488 if (4 & (int) w && len >= 4) {
494 if (8 & (int) w && len >= 8) {
502 * Do as much of the checksum as possible 32 bits at at time.
503 * In fact, this loop is unrolled to make overhead from
507 while ((len -= 32) >= 0) {
509 * Add with carry 16 words and fold in the last
510 * carry by adding a 0 with carry.
512 * The early ADD(16) and the LOAD(32) are to load
513 * the next 2 cache lines in advance on 486's. The
514 * 486 has a penalty of 2 clock cycles for loading
515 * a cache line, plus whatever time the external
516 * memory takes to load the first word(s) addressed.
517 * These penalties are unavoidable. Subsequent
518 * accesses to a cache line being loaded (and to
519 * other external memory?) are delayed until the
520 * whole load finishes. These penalties are mostly
521 * avoided by not accessing external memory for
522 * 8 cycles after the ADD(16) and 12 cycles after
523 * the LOAD(32). The loop terminates when len
524 * is initially 33 (not 32) to guaranteed that
525 * the LOAD(32) is within bounds.
569 if (len == 0 && byte_swapped == 0)
572 while ((len -= 2) >= 0) {
579 su.c[1] = *(const char *)w;
584 } else if (len == -1) {
586 * This buffer has odd number of bytes.
587 * There could be a word split betwen
588 * this buffer and the next.
590 su.c[0] = *(const char *)w;
594 /* The last buffer has odd # of bytes. Follow the
595 standard (the odd byte is shifted left by 8 bits) */
603 in_cksum_finalize(psum)
606 in_psum_t sum = psum;
608 return (~sum & 0xffff);