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(struct mbuf *m, int len, int skip)
78 union { char c[2]; u_short s; } su;
81 for (; skip && m; m = m->m_next) {
82 if (m->m_len > skip) {
83 mlen = m->m_len - skip;
84 w = (u_short *)(mtod(m, u_char *) + skip);
91 for (;m && len; m = m->m_next) {
94 w = mtod(m, u_short *);
97 * The first byte of this mbuf is the continuation
98 * of a word spanning between this mbuf and the
102 /* su.c[0] is already saved when scanning previous
103 * mbuf. sum was REDUCEd when we found mlen == -1
105 su.c[1] = *(u_char *)w;
107 w = (u_short *)((char *)w + 1);
117 * Force to long boundary so we do longword aligned
122 if ((1 & (int) w) && (mlen > 0)) {
124 su.c[0] = *(char *)w;
125 w = (u_short *)((char *)w + 1);
129 if ((2 & (int) w) && (mlen >= 2)) {
135 * Advance to a 486 cache line boundary.
137 if (4 & (int) w && mlen >= 4) {
142 : "g" (((const u_int32_t *)w)[0])
147 if (8 & (int) w && mlen >= 8) {
153 : "g" (((const u_int32_t *)w)[0]),
154 "g" (((const u_int32_t *)w)[1])
160 * Do as much of the checksum as possible 32 bits at at time.
161 * In fact, this loop is unrolled to make overhead from
165 while ((mlen -= 32) >= 0) {
167 * Add with carry 16 words and fold in the last
168 * carry by adding a 0 with carry.
170 * The early ADD(16) and the LOAD(32) are to load
171 * the next 2 cache lines in advance on 486's. The
172 * 486 has a penalty of 2 clock cycles for loading
173 * a cache line, plus whatever time the external
174 * memory takes to load the first word(s) addressed.
175 * These penalties are unavoidable. Subsequent
176 * accesses to a cache line being loaded (and to
177 * other external memory?) are delayed until the
178 * whole load finishes. These penalties are mostly
179 * avoided by not accessing external memory for
180 * 8 cycles after the ADD(16) and 12 cycles after
181 * the LOAD(32). The loop terminates when mlen
182 * is initially 33 (not 32) to guaranteed that
183 * the LOAD(32) is within bounds.
197 : "g" (((const u_int32_t *)w)[4]),
198 "g" (((const u_int32_t *)w)[0]),
199 "g" (((const u_int32_t *)w)[1]),
200 "g" (((const u_int32_t *)w)[2]),
201 "g" (((const u_int32_t *)w)[3]),
202 "g" (((const u_int32_t *)w)[8]),
203 "g" (((const u_int32_t *)w)[5]),
204 "g" (((const u_int32_t *)w)[6]),
205 "g" (((const u_int32_t *)w)[7])
223 : "g" (((const u_int32_t *)w)[4]),
224 "g" (((const u_int32_t *)w)[0]),
225 "g" (((const u_int32_t *)w)[1]),
226 "g" (((const u_int32_t *)w)[2]),
227 "g" (((const u_int32_t *)w)[3]),
228 "g" (((const u_int32_t *)w)[5]),
229 "g" (((const u_int32_t *)w)[6]),
230 "g" (((const u_int32_t *)w)[7])
243 : "g" (((const u_int32_t *)w)[0]),
244 "g" (((const u_int32_t *)w)[1]),
245 "g" (((const u_int32_t *)w)[2]),
246 "g" (((const u_int32_t *)w)[3])
257 : "g" (((const u_int32_t *)w)[0]),
258 "g" (((const u_int32_t *)w)[1])
263 if (mlen == 0 && byte_swapped == 0)
264 continue; /* worth 1% maybe ?? */
266 while ((mlen -= 2) >= 0) {
273 su.c[1] = *(char *)w;
278 } else if (mlen == -1)
280 * This mbuf has odd number of bytes.
281 * There could be a word split between
282 * this mbuf and the next mbuf.
283 * Save the last byte (to prepend to next mbuf).
285 su.c[0] = *(char *)w;
289 printf("%s: out of data by %d\n", __func__, len);
291 /* The last mbuf has odd # of bytes. Follow the
292 standard (the odd byte is shifted left by 8 bits) */
297 return (~sum & 0xffff);