2 /* $NetBSD: in_cksum.c,v 1.7 1997/09/02 13:18:15 thorpej Exp $ */
5 * Copyright (c) 1988, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Matt Thomas <matt@3am-software.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
41 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
43 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/in.h>
48 #include <netinet/ip.h>
49 #include <machine/in_cksum.h>
52 * Checksum routine for Internet Protocol family headers
53 * (Portable Alpha version).
55 * This routine is very heavily used in the network
56 * code and should be modified for each CPU to be as fast as possible.
59 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
63 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
68 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
69 sum = l_util.s[0] + l_util.s[1]; \
73 static const u_int32_t in_masks[] = {
74 /*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/
75 0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
76 0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
77 0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
78 0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
91 u_int64_t in_cksumdata __P((caddr_t buf, int len));
94 in_cksumdata(buf, len)
98 const u_int32_t *lw = (u_int32_t *) buf;
104 if ((3 & (long) lw) == 0 && len == 20) {
105 sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
110 if ((offset = 3 & (long) lw) != 0) {
111 const u_int32_t *masks = in_masks + (offset << 2);
112 lw = (u_int32_t *) (((long) lw) - offset);
113 sum = *lw++ & masks[len >= 3 ? 3 : len];
122 * Force to cache line boundary.
124 offset = 32 - (0x1f & (long) lw);
125 if (offset < 32 && len > offset) {
128 sum += (u_int64_t) lw[0];
132 sum += (u_int64_t) lw[0] + lw[1];
136 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
142 * access prefilling to start load of next cache line.
143 * then add current cache line
144 * save result of prefilling for loop iteration.
147 while ((len -= 32) >= 4) {
148 u_int64_t prefilling = lw[8];
149 sum += prefilled + lw[1] + lw[2] + lw[3]
150 + lw[4] + lw[5] + lw[6] + lw[7];
152 prefilled = prefilling;
155 sum += prefilled + lw[1] + lw[2] + lw[3]
156 + lw[4] + lw[5] + lw[6] + lw[7];
161 while ((len -= 16) >= 0) {
162 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
166 while ((len -= 4) >= 0) {
167 sum += (u_int64_t) *lw++;
171 sum += (u_int64_t) (in_masks[len] & *lw);
178 register struct mbuf *m;
181 register u_int64_t sum = 0;
182 register int mlen = 0;
183 register int clen = 0;
184 register caddr_t addr;
188 for (; m && len; m = m->m_next) {
194 addr = mtod(m, caddr_t);
195 if ((clen ^ (long) addr) & 1)
196 sum += in_cksumdata(addr, mlen) << 8;
198 sum += in_cksumdata(addr, mlen);
204 return (~sum & 0xffff);
207 u_int in_cksum_hdr(ip)
210 u_int64_t sum = in_cksumdata((caddr_t) ip, sizeof(struct ip));
214 return (~sum & 0xffff);