Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / vax / vax / in4_cksum.c
blob40aa0a4bb00b3a4c2550d68c8ab589c7698bf7e9
1 /* $NetBSD: in4_cksum.c,v 1.11 2006/12/31 10:52:52 ragge Exp $ */
3 /*
4 * Copyright (C) 1999 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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 project 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 PROJECT 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 PROJECT 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
29 * SUCH DAMAGE.
33 * Copyright (c) 1988, 1992, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
60 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.11 2006/12/31 10:52:52 ragge Exp $");
66 #include <sys/param.h>
67 #include <sys/mbuf.h>
68 #include <sys/systm.h>
69 #include <sys/socket.h>
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
76 #ifdef CKSUMDEBUG
77 int in4_cksum_md_debug(struct mbuf *m, uint8_t nxt, int off, int len);
78 #define in4_cksum in4_cksum_md_debug
79 #include <netinet/in4_cksum.c>
80 #undef in4_cksum
81 #undef ADDCARRY
82 #undef REDUCE
83 #endif
86 * Checksum routine for Internet Protocol family headers.
87 * This is only for IPv4 pseudo header checksum.
88 * No need to clear non-pseudo-header fields in IPv4 header.
89 * len is for actual payload size, and does not include IPv4 header and
90 * skipped header chain (off + len should be equal to the whole packet).
92 * This implementation is VAX version.
96 #define REDUCE {sum = (sum & 0xffff) + (sum >> 16);}
97 #define ADDCARRY {if (sum > 0xffff) sum -= 0xffff;}
98 #define ADVANCE(n) {w += n; mlen -= n;}
99 #define SWAP {sum <<= 8;} /* depends on recent REDUCE */
101 #define Asm __asm volatile
102 #define ADDL Asm("addl2 (%0)+,%1": "=r" (w), "=r" (sum): "0" (w), "1" (sum))
103 #define ADWC Asm("adwc (%0)+,%1": "=r" (w), "=r" (sum): "0" (w), "1" (sum))
104 #define ADDC Asm("adwc $0,%0" : "=r" (sum) : "0" (sum))
105 #define UNSWAP Asm("rotl $8,%0,%0" : "=r" (sum) : "0" (sum))
106 #define ADDBYTE {sum += *w; SWAP; byte_swapped ^= 1;}
107 #define ADDWORD {sum += *(uint16_t *)w;}
110 in4_cksum(struct mbuf *m, uint8_t nxt, int off, int len)
112 uint8_t *w;
113 uint32_t sum = 0;
114 int mlen = 0;
115 int byte_swapped = 0;
116 #ifdef CKSUMDEBUG
117 int debugrv = in4_cksum_md_debug(m, nxt, off, len);
118 #endif
120 if (nxt != 0) {
121 #ifdef DIAGNOSTIC
122 if (off < sizeof(struct ipovly))
123 panic("in4_cksum: offset too short");
124 if (m->m_len < sizeof(struct ip))
125 panic("in4_cksum: bad mbuf chain");
126 #endif
128 __asm volatile(
129 "movzwl 16(%%ap),%0;" /* mov len to sum */
130 "addb2 8(%%ap),%0;" /* add proto to sum */
131 "rotl $8,%0,%0;" /* htons, carry is preserved */
132 "adwc 12(%2),%0;" /* add src ip */
133 "adwc 16(%2),%0;" /* add dst ip */
134 "adwc $0,%0;" /* clean up carry */
135 : "=r" (sum)
136 : "0" (sum), "r" (mtod(m, void *)));
139 /* skip unnecessary part */
140 while (m && off > 0) {
141 if (m->m_len > off)
142 break;
143 off -= m->m_len;
144 m = m->m_next;
147 for (;m && len; m = m->m_next) {
148 if ((mlen = m->m_len) == 0)
149 continue;
150 w = mtod(m, uint8_t *);
151 if (off) {
152 w += off;
153 mlen -= off;
154 off = 0;
156 if (len < mlen)
157 mlen = len;
158 len -= mlen;
159 if (mlen < 16)
160 goto short_mbuf;
162 * Ensure that we're aligned on a word boundary here so
163 * that we can do 32 bit operations below.
165 if ((3 & (intptr_t) w) != 0) {
166 REDUCE;
167 if ((1 & (intptr_t) w) != 0) {
168 ADDBYTE;
169 ADVANCE(1);
171 if ((2 & (intptr_t) w) != 0) {
172 ADDWORD;
173 ADVANCE(2);
177 * Do as much of the checksum as possible 32 bits at at time.
178 * In fact, this loop is unrolled to make overhead from
179 * branches &c small.
181 while ((mlen -= 32) >= 0) {
183 * Add with carry 16 words and fold in the last carry
184 * by adding a 0 with carry.
186 ADDL; ADWC; ADWC; ADWC;
187 ADWC; ADWC; ADWC; ADWC;
188 ADDC;
190 mlen += 32;
191 if (mlen >= 16) {
192 ADDL; ADWC; ADWC; ADWC;
193 ADDC;
194 mlen -= 16;
196 short_mbuf:
197 if (mlen >= 8) {
198 ADDL; ADWC;
199 ADDC;
200 mlen -= 8;
202 if (mlen >= 4) {
203 ADDL;
204 ADDC;
205 mlen -= 4;
207 if (mlen > 0) {
208 REDUCE;
209 if (mlen >= 2) {
210 ADDWORD;
211 ADVANCE(2);
213 if (mlen >= 1) {
214 ADDBYTE;
219 if (len)
220 printf("cksum4: out of data\n");
221 if (byte_swapped) {
222 UNSWAP;
224 REDUCE;
225 ADDCARRY;
226 #ifdef CKSUMDEBUG
227 if ((sum ^ 0xffff) != debugrv)
228 printf("in4_cksum: rv != debugrv (rv %x debugrv %x)\n",
229 (sum ^ 0xffff), debugrv);
230 #endif
231 return (sum ^ 0xffff);