2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990 The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * from tahoe: in_cksum.c 1.2 86/01/05
32 * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
46 #include <machine/in_cksum.h>
49 * Checksum routine for Internet Protocol family headers.
51 * This routine is very heavily used in the network
52 * code and should be modified for each CPU to be as fast as possible.
54 * This implementation is 386 version.
58 #define ADDCARRY(x) if ((x) > 0xffff) (x) -= 0xffff
59 #define REDUCE {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY(sum);}
62 * These asm statements require __volatile because they pass information
63 * via the condition codes. GCC does not currently provide a way to specify
64 * the condition codes as an input or output operand.
66 * The LOAD macro below is effectively a prefetch into cache. GCC will
67 * load the value into a register but will not use it. Since modern CPUs
68 * reorder operations, this will generally take place in parallel with
72 in_cksum_skip(m, len, skip)
81 union { char c[2]; u_short s; } su;
84 for (; skip && m; m = m->m_next) {
85 if (m->m_len > skip) {
86 mlen = m->m_len - skip;
87 w = (u_short *)(mtod(m, u_char *) + skip);
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);
120 * Force to long boundary so we do longword aligned
125 if ((1 & (int) w) && (mlen > 0)) {
127 su.c[0] = *(char *)w;
128 w = (u_short *)((char *)w + 1);
132 if ((2 & (int) w) && (mlen >= 2)) {
138 * Advance to a 486 cache line boundary.
140 if (4 & (int) w && mlen >= 4) {
145 : "g" (((const u_int32_t *)w)[0])
150 if (8 & (int) w && mlen >= 8) {
156 : "g" (((const u_int32_t *)w)[0]),
157 "g" (((const u_int32_t *)w)[1])
163 * Do as much of the checksum as possible 32 bits at at time.
164 * In fact, this loop is unrolled to make overhead from
168 while ((mlen -= 32) >= 0) {
170 * Add with carry 16 words and fold in the last
171 * carry by adding a 0 with carry.
173 * The early ADD(16) and the LOAD(32) are to load
174 * the next 2 cache lines in advance on 486's. The
175 * 486 has a penalty of 2 clock cycles for loading
176 * a cache line, plus whatever time the external
177 * memory takes to load the first word(s) addressed.
178 * These penalties are unavoidable. Subsequent
179 * accesses to a cache line being loaded (and to
180 * other external memory?) are delayed until the
181 * whole load finishes. These penalties are mostly
182 * avoided by not accessing external memory for
183 * 8 cycles after the ADD(16) and 12 cycles after
184 * the LOAD(32). The loop terminates when mlen
185 * is initially 33 (not 32) to guaranteed that
186 * the LOAD(32) is within bounds.
200 : "g" (((const u_int32_t *)w)[4]),
201 "g" (((const u_int32_t *)w)[0]),
202 "g" (((const u_int32_t *)w)[1]),
203 "g" (((const u_int32_t *)w)[2]),
204 "g" (((const u_int32_t *)w)[3]),
205 "g" (((const u_int32_t *)w)[8]),
206 "g" (((const u_int32_t *)w)[5]),
207 "g" (((const u_int32_t *)w)[6]),
208 "g" (((const u_int32_t *)w)[7])
226 : "g" (((const u_int32_t *)w)[4]),
227 "g" (((const u_int32_t *)w)[0]),
228 "g" (((const u_int32_t *)w)[1]),
229 "g" (((const u_int32_t *)w)[2]),
230 "g" (((const u_int32_t *)w)[3]),
231 "g" (((const u_int32_t *)w)[5]),
232 "g" (((const u_int32_t *)w)[6]),
233 "g" (((const u_int32_t *)w)[7])
246 : "g" (((const u_int32_t *)w)[0]),
247 "g" (((const u_int32_t *)w)[1]),
248 "g" (((const u_int32_t *)w)[2]),
249 "g" (((const u_int32_t *)w)[3])
260 : "g" (((const u_int32_t *)w)[0]),
261 "g" (((const u_int32_t *)w)[1])
266 if (mlen == 0 && byte_swapped == 0)
267 continue; /* worth 1% maybe ?? */
269 while ((mlen -= 2) >= 0) {
276 su.c[1] = *(char *)w;
281 } else if (mlen == -1)
283 * This mbuf has odd number of bytes.
284 * There could be a word split between
285 * this mbuf and the next mbuf.
286 * Save the last byte (to prepend to next mbuf).
288 su.c[0] = *(char *)w;
292 printf("%s: out of data by %d\n", __func__, len);
294 /* The last mbuf has odd # of bytes. Follow the
295 standard (the odd byte is shifted left by 8 bits) */
300 return (~sum & 0xffff);