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 * 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.
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
29 * from tahoe: in_cksum.c 1.2 86/01/05
30 * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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
58 * icc needs to be special cased here, as the asm code below results
59 * in broken code if compiled with icc.
61 #if !defined(__GNUCLIKE_ASM) || defined(__INTEL_COMPILER)
62 /* non gcc parts stolen from sys/alpha/alpha/in_cksum.c */
66 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
71 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
72 sum = l_util.s[0] + l_util.s[1]; \
76 #define REDUCE {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY(sum);}
78 #if !defined(__GNUCLIKE_ASM) || defined(__INTEL_COMPILER)
79 static const u_int32_t in_masks[] = {
80 /*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/
81 0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
82 0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
83 0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
84 0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
98 in_cksumdata(const u_int32_t *lw, int len)
105 if ((3 & (long) lw) == 0 && len == 20) {
106 sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
111 if ((offset = 3 & (long) lw) != 0) {
112 const u_int32_t *masks = in_masks + (offset << 2);
113 lw = (u_int32_t *) (((long) lw) - offset);
114 sum = *lw++ & masks[len >= 3 ? 3 : len];
123 * Force to cache line boundary.
125 offset = 32 - (0x1f & (long) lw);
126 if (offset < 32 && len > offset) {
129 sum += (u_int64_t) lw[0];
133 sum += (u_int64_t) lw[0] + lw[1];
137 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
143 * access prefilling to start load of next cache line.
144 * then add current cache line
145 * save result of prefilling for loop iteration.
148 while ((len -= 32) >= 4) {
149 u_int64_t prefilling = lw[8];
150 sum += prefilled + lw[1] + lw[2] + lw[3]
151 + lw[4] + lw[5] + lw[6] + lw[7];
153 prefilled = prefilling;
156 sum += prefilled + lw[1] + lw[2] + lw[3]
157 + lw[4] + lw[5] + lw[6] + lw[7];
162 while ((len -= 16) >= 0) {
163 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
167 while ((len -= 4) >= 0) {
168 sum += (u_int64_t) *lw++;
172 sum += (u_int64_t) (in_masks[len] & *lw);
178 in_addword(u_short a, u_short b)
180 u_int64_t sum = a + b;
187 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
193 sum = (u_int64_t) a + b + c;
199 in_cksum_skip(struct mbuf *m, int len, int skip)
209 for (; skip && m; m = m->m_next) {
210 if (m->m_len > skip) {
211 mlen = m->m_len - skip;
212 addr = mtod(m, caddr_t) + skip;
219 for (; m && len; m = m->m_next) {
223 addr = mtod(m, caddr_t);
227 if ((clen ^ (long) addr) & 1)
228 sum += in_cksumdata((const u_int32_t *)addr, mlen) << 8;
230 sum += in_cksumdata((const u_int32_t *)addr, mlen);
236 return (~sum & 0xffff);
239 u_int in_cksum_hdr(const struct ip *ip)
241 u_int64_t sum = in_cksumdata((const u_int32_t *)ip, sizeof(struct ip));
246 return (~sum & 0xffff);
251 * These asm statements require __volatile because they pass information
252 * via the condition codes. GCC does not currently provide a way to specify
253 * the condition codes as an input or output operand.
255 * The LOAD macro below is effectively a prefetch into cache. GCC will
256 * load the value into a register but will not use it. Since modern CPUs
257 * reorder operations, this will generally take place in parallel with
258 * other calculations.
261 in_cksum_skip(m, len, skip)
267 register unsigned sum = 0;
268 register int mlen = 0;
269 int byte_swapped = 0;
270 union { char c[2]; u_short s; } su;
273 for (; skip && m; m = m->m_next) {
274 if (m->m_len > skip) {
275 mlen = m->m_len - skip;
276 w = (u_short *)(mtod(m, u_char *) + skip);
283 for (;m && len; m = m->m_next) {
286 w = mtod(m, u_short *);
289 * The first byte of this mbuf is the continuation
290 * of a word spanning between this mbuf and the
294 /* su.c[0] is already saved when scanning previous
295 * mbuf. sum was REDUCEd when we found mlen == -1
297 su.c[1] = *(u_char *)w;
299 w = (u_short *)((char *)w + 1);
309 * Force to long boundary so we do longword aligned
314 if ((1 & (int) w) && (mlen > 0)) {
316 su.c[0] = *(char *)w;
317 w = (u_short *)((char *)w + 1);
321 if ((2 & (int) w) && (mlen >= 2)) {
327 * Advance to a 486 cache line boundary.
329 if (4 & (int) w && mlen >= 4) {
334 : "g" (((const u_int32_t *)w)[0])
339 if (8 & (int) w && mlen >= 8) {
345 : "g" (((const u_int32_t *)w)[0]),
346 "g" (((const u_int32_t *)w)[1])
352 * Do as much of the checksum as possible 32 bits at at time.
353 * In fact, this loop is unrolled to make overhead from
357 while ((mlen -= 32) >= 0) {
359 * Add with carry 16 words and fold in the last
360 * carry by adding a 0 with carry.
362 * The early ADD(16) and the LOAD(32) are to load
363 * the next 2 cache lines in advance on 486's. The
364 * 486 has a penalty of 2 clock cycles for loading
365 * a cache line, plus whatever time the external
366 * memory takes to load the first word(s) addressed.
367 * These penalties are unavoidable. Subsequent
368 * accesses to a cache line being loaded (and to
369 * other external memory?) are delayed until the
370 * whole load finishes. These penalties are mostly
371 * avoided by not accessing external memory for
372 * 8 cycles after the ADD(16) and 12 cycles after
373 * the LOAD(32). The loop terminates when mlen
374 * is initially 33 (not 32) to guaranteed that
375 * the LOAD(32) is within bounds.
389 : "g" (((const u_int32_t *)w)[4]),
390 "g" (((const u_int32_t *)w)[0]),
391 "g" (((const u_int32_t *)w)[1]),
392 "g" (((const u_int32_t *)w)[2]),
393 "g" (((const u_int32_t *)w)[3]),
394 "g" (((const u_int32_t *)w)[8]),
395 "g" (((const u_int32_t *)w)[5]),
396 "g" (((const u_int32_t *)w)[6]),
397 "g" (((const u_int32_t *)w)[7])
415 : "g" (((const u_int32_t *)w)[4]),
416 "g" (((const u_int32_t *)w)[0]),
417 "g" (((const u_int32_t *)w)[1]),
418 "g" (((const u_int32_t *)w)[2]),
419 "g" (((const u_int32_t *)w)[3]),
420 "g" (((const u_int32_t *)w)[5]),
421 "g" (((const u_int32_t *)w)[6]),
422 "g" (((const u_int32_t *)w)[7])
435 : "g" (((const u_int32_t *)w)[0]),
436 "g" (((const u_int32_t *)w)[1]),
437 "g" (((const u_int32_t *)w)[2]),
438 "g" (((const u_int32_t *)w)[3])
449 : "g" (((const u_int32_t *)w)[0]),
450 "g" (((const u_int32_t *)w)[1])
455 if (mlen == 0 && byte_swapped == 0)
456 continue; /* worth 1% maybe ?? */
458 while ((mlen -= 2) >= 0) {
465 su.c[1] = *(char *)w;
470 } else if (mlen == -1)
472 * This mbuf has odd number of bytes.
473 * There could be a word split betwen
474 * this mbuf and the next mbuf.
475 * Save the last byte (to prepend to next mbuf).
477 su.c[0] = *(char *)w;
481 printf("%s: out of data by %d\n", __func__, len);
483 /* The last mbuf has odd # of bytes. Follow the
484 standard (the odd byte is shifted left by 8 bits) */
489 return (~sum & 0xffff);