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 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
44 #include <machine/in_cksum.h>
47 * Checksum routine for Internet Protocol family headers.
49 * This routine is very heavily used in the network
50 * code and should be modified for each CPU to be as fast as possible.
52 * This implementation is 386 version.
56 #define ADDCARRY(x) if ((x) > 0xffff) (x) -= 0xffff
57 #define REDUCE {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY(sum);}
60 * These asm statements require __volatile because they pass information
61 * via the condition codes. GCC does not currently provide a way to specify
62 * the condition codes as an input or output operand.
64 * The LOAD macro below is effectively a prefetch into cache. GCC will
65 * load the value into a register but will not use it. Since modern CPUs
66 * reorder operations, this will generally take place in parallel with
70 in_cksum_skip(struct mbuf *m, int len, int skip)
76 union { char c[2]; u_short s; } su;
79 for (; skip && m; m = m->m_next) {
80 if (m->m_len > skip) {
81 mlen = m->m_len - skip;
82 w = (u_short *)(mtod(m, u_char *) + skip);
89 for (;m && len; m = m->m_next) {
92 w = mtod(m, u_short *);
95 * The first byte of this mbuf is the continuation
96 * of a word spanning between this mbuf and the
100 /* su.c[0] is already saved when scanning previous
101 * mbuf. sum was REDUCEd when we found mlen == -1
103 su.c[1] = *(u_char *)w;
105 w = (u_short *)((char *)w + 1);
115 * Force to long boundary so we do longword aligned
120 if ((1 & (int) w) && (mlen > 0)) {
122 su.c[0] = *(char *)w;
123 w = (u_short *)((char *)w + 1);
127 if ((2 & (int) w) && (mlen >= 2)) {
133 * Advance to a 486 cache line boundary.
135 if (4 & (int) w && mlen >= 4) {
140 : "g" (((const u_int32_t *)w)[0])
145 if (8 & (int) w && mlen >= 8) {
151 : "g" (((const u_int32_t *)w)[0]),
152 "g" (((const u_int32_t *)w)[1])
158 * Do as much of the checksum as possible 32 bits at at time.
159 * In fact, this loop is unrolled to make overhead from
163 while ((mlen -= 32) >= 0) {
165 * Add with carry 16 words and fold in the last
166 * carry by adding a 0 with carry.
168 * The early ADD(16) and the LOAD(32) are to load
169 * the next 2 cache lines in advance on 486's. The
170 * 486 has a penalty of 2 clock cycles for loading
171 * a cache line, plus whatever time the external
172 * memory takes to load the first word(s) addressed.
173 * These penalties are unavoidable. Subsequent
174 * accesses to a cache line being loaded (and to
175 * other external memory?) are delayed until the
176 * whole load finishes. These penalties are mostly
177 * avoided by not accessing external memory for
178 * 8 cycles after the ADD(16) and 12 cycles after
179 * the LOAD(32). The loop terminates when mlen
180 * is initially 33 (not 32) to guaranteed that
181 * the LOAD(32) is within bounds.
195 : "g" (((const u_int32_t *)w)[4]),
196 "g" (((const u_int32_t *)w)[0]),
197 "g" (((const u_int32_t *)w)[1]),
198 "g" (((const u_int32_t *)w)[2]),
199 "g" (((const u_int32_t *)w)[3]),
200 "g" (((const u_int32_t *)w)[8]),
201 "g" (((const u_int32_t *)w)[5]),
202 "g" (((const u_int32_t *)w)[6]),
203 "g" (((const u_int32_t *)w)[7])
221 : "g" (((const u_int32_t *)w)[4]),
222 "g" (((const u_int32_t *)w)[0]),
223 "g" (((const u_int32_t *)w)[1]),
224 "g" (((const u_int32_t *)w)[2]),
225 "g" (((const u_int32_t *)w)[3]),
226 "g" (((const u_int32_t *)w)[5]),
227 "g" (((const u_int32_t *)w)[6]),
228 "g" (((const u_int32_t *)w)[7])
241 : "g" (((const u_int32_t *)w)[0]),
242 "g" (((const u_int32_t *)w)[1]),
243 "g" (((const u_int32_t *)w)[2]),
244 "g" (((const u_int32_t *)w)[3])
255 : "g" (((const u_int32_t *)w)[0]),
256 "g" (((const u_int32_t *)w)[1])
261 if (mlen == 0 && byte_swapped == 0)
262 continue; /* worth 1% maybe ?? */
264 while ((mlen -= 2) >= 0) {
271 su.c[1] = *(char *)w;
276 } else if (mlen == -1)
278 * This mbuf has odd number of bytes.
279 * There could be a word split between
280 * this mbuf and the next mbuf.
281 * Save the last byte (to prepend to next mbuf).
283 su.c[0] = *(char *)w;
287 printf("%s: out of data by %d\n", __func__, len);
289 /* The last mbuf has odd # of bytes. Follow the
290 standard (the odd byte is shifted left by 8 bits) */
295 return (~sum & 0xffff);