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$");
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
61 * icc needs to be special cased here, as the asm code below results
62 * in broken code if compiled with icc.
64 #if !defined(__GNUCLIKE_ASM) || defined(__INTEL_COMPILER)
65 /* non gcc parts stolen from sys/alpha/alpha/in_cksum.c */
69 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
74 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
75 sum = l_util.s[0] + l_util.s[1]; \
79 #define REDUCE {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY(sum);}
81 #if !defined(__GNUCLIKE_ASM) || defined(__INTEL_COMPILER)
82 static const u_int32_t in_masks[] = {
83 /*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/
84 0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
85 0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
86 0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
87 0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
101 in_cksumdata(const u_int32_t *lw, int len)
108 if ((3 & (long) lw) == 0 && len == 20) {
109 sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
114 if ((offset = 3 & (long) lw) != 0) {
115 const u_int32_t *masks = in_masks + (offset << 2);
116 lw = (u_int32_t *) (((long) lw) - offset);
117 sum = *lw++ & masks[len >= 3 ? 3 : len];
126 * Force to cache line boundary.
128 offset = 32 - (0x1f & (long) lw);
129 if (offset < 32 && len > offset) {
132 sum += (u_int64_t) lw[0];
136 sum += (u_int64_t) lw[0] + lw[1];
140 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
146 * access prefilling to start load of next cache line.
147 * then add current cache line
148 * save result of prefilling for loop iteration.
151 while ((len -= 32) >= 4) {
152 u_int64_t prefilling = lw[8];
153 sum += prefilled + lw[1] + lw[2] + lw[3]
154 + lw[4] + lw[5] + lw[6] + lw[7];
156 prefilled = prefilling;
159 sum += prefilled + lw[1] + lw[2] + lw[3]
160 + lw[4] + lw[5] + lw[6] + lw[7];
165 while ((len -= 16) >= 0) {
166 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
170 while ((len -= 4) >= 0) {
171 sum += (u_int64_t) *lw++;
175 sum += (u_int64_t) (in_masks[len] & *lw);
181 in_addword(u_short a, u_short b)
183 u_int64_t sum = a + b;
190 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
196 sum = (u_int64_t) a + b + c;
202 in_cksum_skip(struct mbuf *m, int len, int skip)
212 for (; skip && m; m = m->m_next) {
213 if (m->m_len > skip) {
214 mlen = m->m_len - skip;
215 addr = mtod(m, caddr_t) + skip;
222 for (; m && len; m = m->m_next) {
226 addr = mtod(m, caddr_t);
230 if ((clen ^ (long) addr) & 1)
231 sum += in_cksumdata((const u_int32_t *)addr, mlen) << 8;
233 sum += in_cksumdata((const u_int32_t *)addr, mlen);
239 return (~sum & 0xffff);
242 u_int in_cksum_hdr(const struct ip *ip)
244 u_int64_t sum = in_cksumdata((const u_int32_t *)ip, sizeof(struct ip));
249 return (~sum & 0xffff);
254 * These asm statements require __volatile because they pass information
255 * via the condition codes. GCC does not currently provide a way to specify
256 * the condition codes as an input or output operand.
258 * The LOAD macro below is effectively a prefetch into cache. GCC will
259 * load the value into a register but will not use it. Since modern CPUs
260 * reorder operations, this will generally take place in parallel with
261 * other calculations.
263 #define ADD(n) __asm __volatile \
264 ("addl %1, %0" : "+r" (sum) : \
265 "g" (((const u_int32_t *)w)[n / 4]))
266 #define ADDC(n) __asm __volatile \
267 ("adcl %1, %0" : "+r" (sum) : \
268 "g" (((const u_int32_t *)w)[n / 4]))
269 #define LOAD(n) __asm __volatile \
270 ("" : : "r" (((const u_int32_t *)w)[n / 4]))
271 #define MOP __asm __volatile \
272 ("adcl $0, %0" : "+r" (sum))
275 in_cksum_skip(m, len, skip)
281 register unsigned sum = 0;
282 register int mlen = 0;
283 int byte_swapped = 0;
284 union { char c[2]; u_short s; } su;
287 for (; skip && m; m = m->m_next) {
288 if (m->m_len > skip) {
289 mlen = m->m_len - skip;
290 w = (u_short *)(mtod(m, u_char *) + skip);
297 for (;m && len; m = m->m_next) {
300 w = mtod(m, u_short *);
303 * The first byte of this mbuf is the continuation
304 * of a word spanning between this mbuf and the
308 /* su.c[0] is already saved when scanning previous
309 * mbuf. sum was REDUCEd when we found mlen == -1
311 su.c[1] = *(u_char *)w;
313 w = (u_short *)((char *)w + 1);
323 * Force to long boundary so we do longword aligned
328 if ((1 & (int) w) && (mlen > 0)) {
330 su.c[0] = *(char *)w;
331 w = (u_short *)((char *)w + 1);
335 if ((2 & (int) w) && (mlen >= 2)) {
341 * Advance to a 486 cache line boundary.
343 if (4 & (int) w && mlen >= 4) {
349 if (8 & (int) w && mlen >= 8) {
357 * Do as much of the checksum as possible 32 bits at at time.
358 * In fact, this loop is unrolled to make overhead from
362 while ((mlen -= 32) >= 0) {
364 * Add with carry 16 words and fold in the last
365 * carry by adding a 0 with carry.
367 * The early ADD(16) and the LOAD(32) are to load
368 * the next 2 cache lines in advance on 486's. The
369 * 486 has a penalty of 2 clock cycles for loading
370 * a cache line, plus whatever time the external
371 * memory takes to load the first word(s) addressed.
372 * These penalties are unavoidable. Subsequent
373 * accesses to a cache line being loaded (and to
374 * other external memory?) are delayed until the
375 * whole load finishes. These penalties are mostly
376 * avoided by not accessing external memory for
377 * 8 cycles after the ADD(16) and 12 cycles after
378 * the LOAD(32). The loop terminates when mlen
379 * is initially 33 (not 32) to guaranteed that
380 * the LOAD(32) is within bounds.
424 if (mlen == 0 && byte_swapped == 0)
425 continue; /* worth 1% maybe ?? */
427 while ((mlen -= 2) >= 0) {
434 su.c[1] = *(char *)w;
439 } else if (mlen == -1)
441 * This mbuf has odd number of bytes.
442 * There could be a word split betwen
443 * this mbuf and the next mbuf.
444 * Save the last byte (to prepend to next mbuf).
446 su.c[0] = *(char *)w;
450 printf("%s: out of data by %d\n", __func__, len);
452 /* The last mbuf has odd # of bytes. Follow the
453 standard (the odd byte is shifted left by 8 bits) */
458 return (~sum & 0xffff);