in_cksum.c revision 330897
1274578Sngie/*-
2274578Sngie * SPDX-License-Identifier: BSD-3-Clause
3274578Sngie *
4274578Sngie * Copyright (c) 1990 The Regents of the University of California.
5274578Sngie * All rights reserved.
6274578Sngie *
7274578Sngie * Redistribution and use in source and binary forms, with or without
8274578Sngie * modification, are permitted provided that the following conditions
9274578Sngie * are met:
10274578Sngie * 1. Redistributions of source code must retain the above copyright
11274578Sngie *    notice, this list of conditions and the following disclaimer.
12274578Sngie * 2. Redistributions in binary form must reproduce the above copyright
13274578Sngie *    notice, this list of conditions and the following disclaimer in the
14274578Sngie *    documentation and/or other materials provided with the distribution.
15274578Sngie * 4. Neither the name of the University nor the names of its contributors
16274578Sngie *    may be used to endorse or promote products derived from this software
17274578Sngie *    without specific prior written permission.
18274578Sngie *
19274578Sngie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20274578Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21274578Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22274578Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23274578Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24274578Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25313535Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26274578Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27274578Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28274578Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29274578Sngie * SUCH DAMAGE.
30274578Sngie */
31274578Sngie/*-
32274578Sngie * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>.
33274578Sngie * All rights reserved.
34274578Sngie *
35274578Sngie * Redistribution and use in source and binary forms, with or without
36274578Sngie * modification, are permitted provided that the following conditions
37274578Sngie * are met:
38274578Sngie * 1. Redistributions of source code must retain the above copyright
39274578Sngie *    notice, this list of conditions and the following disclaimer.
40274578Sngie * 2. Redistributions in binary form must reproduce the above copyright
41274578Sngie *    notice, this list of conditions and the following disclaimer in the
42274578Sngie *    documentation and/or other materials provided with the distribution.
43274578Sngie *
44274578Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45274578Sngie * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46274578Sngie * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47274578Sngie * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
48274578Sngie * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49274578Sngie * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50274578Sngie * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51274578Sngie * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52274578Sngie * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
53274578Sngie * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54274578Sngie *
55274578Sngie *	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
58 */
59
60#include <sys/cdefs.h>
61__FBSDID("$FreeBSD: stable/11/sys/sparc64/sparc64/in_cksum.c 330897 2018-03-14 03:19:51Z eadler $");
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/mbuf.h>
66
67#include <netinet/in.h>
68#include <netinet/in_systm.h>
69#include <netinet/ip.h>
70
71#include <machine/in_cksum.h>
72
73/*
74 * Checksum routine for Internet Protocol family headers.
75 *
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.
78 *
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.
84 */
85
86/*
87 * REDUCE() is actually not used that frequently... maybe a C implementation
88 * would suffice.
89 */
90#define	REDUCE(sum, tmp) __asm(						\
91	"sll %2, 16, %1\n"						\
92	"addcc %2, %1, %0\n"						\
93	"srl %0, 16, %0\n"						\
94	"addc %0, 0, %0" : "=r" (sum), "=&r" (tmp) : "0" (sum) : "cc")
95
96/*
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().
99 */
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"
105
106#define	LD32_ADD32(n, mod)						\
107	"lduw [%3 + " #n "], %1\n"					\
108	"add" #mod " %2, %1, %0\n"
109
110#define	MOP(sum, tmp, addr)						\
111	"addc %2, 0, %0"						\
112	: "=r" (sum), "=&r" (tmp) : "0" (sum), "r" (addr) : "cc"
113
114u_short
115in_cksum_skip(struct mbuf *m, int len, int skip)
116{
117	u_short *w;
118	unsigned long tmp, sum = 0;
119	int mlen = 0;
120	int byte_swapped = 0;
121	u_short	su = 0;
122
123	len -= skip;
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);
128			goto skip_start;
129		} else
130			skip -= m->m_len;
131	}
132
133	for (; m != NULL && len > 0; m = m->m_next) {
134		if (m->m_len == 0)
135			continue;
136		w = mtod(m, u_short *);
137		if (mlen == -1) {
138			/*
139			 * The first byte of this mbuf is the continuation
140			 * of a word spanning between this mbuf and the
141			 * last mbuf.
142			 *
143			 * The high order byte of su is already saved when
144			 * scanning previous mbuf.  sum was REDUCEd when we
145			 * found mlen == -1
146			 */
147			sum += su | *(u_char *)w;
148			w = (u_short *)((u_char *)w + 1);
149			mlen = m->m_len - 1;
150			len--;
151		} else
152			mlen = m->m_len;
153skip_start:
154		if (len < mlen)
155			mlen = len;
156		len -= mlen;
157		/*
158		 * Force to a 8-byte boundary first so that we can use
159		 * LD64_ADD32.
160		 */
161		if (((u_long)w & 7) != 0) {
162			REDUCE(sum, tmp);
163			if (((u_long)w & 1) != 0 && mlen >= 1) {
164				sum <<= 8;
165				su = *(u_char *)w << 8;
166				w = (u_short *)((u_char *)w + 1);
167				mlen--;
168				byte_swapped = 1;
169			}
170			if (((u_long)w & 2) != 0 && mlen >= 2) {
171				sum += *w++;
172				mlen -= 2;
173			}
174			if (((u_long)w & 4) != 0 && mlen >= 4) {
175				__asm(
176				    LD32_ADD32(0, cc)
177				    MOP(sum, tmp, w)
178				);
179				w += 2;
180				mlen -= 4;
181			}
182		}
183		/*
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
186		 * branches &c small.
187		 */
188		for (; mlen >= 64; mlen -= 64) {
189			__asm(
190			    LD64_ADD32(0, cc)
191			    LD64_ADD32(8, ccc)
192			    LD64_ADD32(16, ccc)
193			    LD64_ADD32(24, ccc)
194			    LD64_ADD32(32, ccc)
195			    LD64_ADD32(40, ccc)
196			    LD64_ADD32(48, ccc)
197			    LD64_ADD32(56, ccc)
198			    MOP(sum, tmp, w)
199			);
200			w += 32;
201		}
202		if (mlen >= 32) {
203			__asm(
204			    LD64_ADD32(0, cc)
205			    LD64_ADD32(8, ccc)
206			    LD64_ADD32(16, ccc)
207			    LD64_ADD32(24, ccc)
208			    MOP(sum, tmp, w)
209			);
210			w += 16;
211			mlen -= 32;
212		}
213		if (mlen >= 16) {
214			__asm(
215			    LD64_ADD32(0, cc)
216			    LD64_ADD32(8, ccc)
217			    MOP(sum, tmp, w)
218			);
219			w += 8;
220			mlen -= 16;
221		}
222		if (mlen >= 8) {
223			__asm(
224			    LD64_ADD32(0, cc)
225			    MOP(sum, tmp, w)
226			);
227			w += 4;
228			mlen -= 8;
229		}
230		REDUCE(sum, tmp);
231		while ((mlen -= 2) >= 0)
232			sum += *w++;
233		if (byte_swapped) {
234			sum <<= 8;
235			byte_swapped = 0;
236			if (mlen == -1) {
237				su |= *(u_char *)w;
238				sum += su;
239				mlen = 0;
240			} else
241				mlen = -1;
242		} else if (mlen == -1) {
243			/*
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).
248			 */
249			su = *(u_char *)w << 8;
250		}
251	}
252
253	if (len)
254		printf("%s: out of data by %d\n", __func__, len);
255	if (mlen == -1) {
256		/*
257		 * The last mbuf has odd # of bytes.  Follow the
258		 * standard (the odd byte is shifted left by 8 bits).
259		 */
260		sum += su & 0xff00;
261	}
262	REDUCE(sum, tmp);
263	return (~sum & 0xffff);
264}
265