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
30 * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
46 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 * from tahoe: in_cksum.c 1.2 86/01/05
54 * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91
55 * from: FreeBSD: src/sys/i386/i386/in_cksum.c,v 1.22 2000/11/25
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/systm.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
69 #include <machine/in_cksum.h>
72 * Checksum routine for Internet Protocol family headers.
74 * This routine is very heavily used in the network
75 * code and should be modified for each CPU to be as fast as possible.
77 * This implementation is a sparc64 version. Most code was taken over
78 * and adapted from the i386. Some optimizations were changed to achieve
79 * (hopefully) better performance.
80 * This uses 64-bit loads, but 32-bit additions due to the lack of a 64-bit
81 * add-with-carry operation.
85 * REDUCE() is actually not used that frequently... maybe a C implementation
88 #define REDUCE(sum, tmp) __asm( \
90 "addcc %2, %1, %0\n" \
92 "addc %0, 0, %0" : "=r" (sum), "=&r" (tmp) : "0" (sum) : "cc")
95 * Note that some of these macros depend on the flags being preserved
96 * between calls, thus they have to be used within a single __asm().
98 #define LD64_ADD32(n, mod) \
99 "ldx [%3 + " #n "], %1\n" \
100 "add" #mod " %2, %1, %0\n" \
101 "srlx %1, 32, %1\n" \
102 "addccc %0, %1, %0\n"
104 #define LD32_ADD32(n, mod) \
105 "lduw [%3 + " #n "], %1\n" \
106 "add" #mod " %2, %1, %0\n"
108 #define MOP(sum, tmp, addr) \
110 : "=r" (sum), "=&r" (tmp) : "0" (sum), "r" (addr) : "cc"
113 in_cksum_skip(struct mbuf *m, int len, int skip)
116 unsigned long tmp, sum = 0;
118 int byte_swapped = 0;
122 for (; skip > 0 && m != NULL; m = m->m_next) {
123 if (m->m_len > skip) {
124 mlen = m->m_len - skip;
125 w = (u_short *)(mtod(m, u_char *) + skip);
131 for (; m != NULL && len > 0; m = m->m_next) {
134 w = mtod(m, u_short *);
137 * The first byte of this mbuf is the continuation
138 * of a word spanning between this mbuf and the
141 * The high order byte of su is already saved when
142 * scanning previous mbuf. sum was REDUCEd when we
145 sum += su | *(u_char *)w;
146 w = (u_short *)((u_char *)w + 1);
156 * Force to a 8-byte boundary first so that we can use
159 if (((u_long)w & 7) != 0) {
161 if (((u_long)w & 1) != 0 && mlen >= 1) {
163 su = *(u_char *)w << 8;
164 w = (u_short *)((u_char *)w + 1);
168 if (((u_long)w & 2) != 0 && mlen >= 2) {
172 if (((u_long)w & 4) != 0 && mlen >= 4) {
182 * Do as much of the checksum as possible 64 bits at at time.
183 * In fact, this loop is unrolled to make overhead from
186 for (; mlen >= 64; mlen -= 64) {
229 while ((mlen -= 2) >= 0)
240 } else if (mlen == -1) {
242 * This mbuf has odd number of bytes.
243 * There could be a word split between
244 * this mbuf and the next mbuf.
245 * Save the last byte (to prepend to next mbuf).
247 su = *(u_char *)w << 8;
252 printf("%s: out of data by %d\n", __func__, len);
255 * The last mbuf has odd # of bytes. Follow the
256 * standard (the odd byte is shifted left by 8 bits).
261 return (~sum & 0xffff);