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