1 /* $OpenBSD: in_cksum.c,v 1.4 2010/04/26 19:43:23 kettenis Exp $ */
2
3 /*
4 * Copyright (c) 2000 Michael Shalayeff
5 * All rights reserved.
6 *
7 * based on a sparc version of Zubin Dittia.
8 * Copyright (c) 1995 Zubin Dittia.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mbuf.h>
35 #include <netinet/in.h>
36
37 /*
38 * Checksum routine for Internet Protocol family headers.
39 *
40 * This routine is very heavily used in the network
41 * code and should be modified for each CPU to be as fast as possible.
42 *
43 * HPPA version.
44 */
45
46 #define ADD32 asm volatile( "ldw 0x00(%1), %%r19! ldw 0x04(%1), %%r20\n\t" \
47 "add %0, %%r19, %0 ! addc %0, %%r20, %0\n\t" \
48 "ldw 0x08(%1), %%r19! ldw 0x0c(%1), %%r20\n\t" \
49 "addc %0, %%r19, %0 ! addc %0, %%r20, %0\n\t" \
50 "ldw 0x10(%1), %%r19! ldw 0x14(%1), %%r20\n\t" \
51 "addc %0, %%r19, %0 ! addc %0, %%r20, %0\n\t" \
52 "ldw 0x18(%1), %%r19! ldw 0x1c(%1), %%r20\n\t" \
53 "addc %0, %%r19, %0 ! addc %0, %%r20, %0\n\t" \
54 "ldo 0x20(%1), %1 ! addc %0, %%r0 , %0" \
55 : "+r" (sum), "+r" (w) :: "r20", "r19")
56 #define ADD16 asm volatile( "ldw 0x00(%1), %%r19! ldw 0x04(%1), %%r20\n\t" \
57 "add %0, %%r19, %0! addc %0, %%r20, %0\n\t" \
58 "ldw 0x08(%1), %%r19! ldw 0x0c(%1), %%r20\n\t" \
59 "addc %0, %%r19, %0! addc %0, %%r20, %0\n\t" \
60 "ldo 0x10(%1), %1 ! addc %0, %%r0 , %0" \
61 : "+r" (sum), "+r" (w) :: "r20", "r19")
62
63 #define ADDCARRY {if (sum > 0xffff) sum -= 0xffff;}
64 #define REDUCE {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY}
65 #define ROL asm volatile ("shd %0, %0, 8, %0" : "+r" (sum))
66 #define ADDBYTE {ROL; sum += *w++; bins++; mlen--;}
67 #define ADDSHORT {sum += *(u_short *)w; w += 2; mlen -= 2;}
68 #define ADDWORD asm volatile( "ldwm 4(%1), %%r19! add %0, %%r19, %0\n\t" \
69 "ldo -4(%2), %2 ! addc %0, 0, %0" \
70 : "+r" (sum), "+r" (w), "+r" (mlen) :: "r19")
71
72 int
in_cksum(m,len)73 in_cksum(m, len)
74 register struct mbuf *m;
75 register int len;
76 {
77 register u_int sum = 0;
78 register u_int bins = 0;
79
80 for (; m && len; m = m->m_next) {
81 register int mlen = m->m_len;
82 register u_char *w;
83
84 if (!mlen)
85 continue;
86 if (len < mlen)
87 mlen = len;
88 len -= mlen;
89 w = mtod(m, u_char *);
90
91 if (mlen > 16) {
92 /*
93 * If we are aligned on a doubleword boundary
94 * do 32 bit bundled operations
95 */
96 if ((7 & (u_long)w) != 0) {
97 if ((1 & (u_long)w) != 0)
98 ADDBYTE;
99 if ((2 & (u_long)w) != 0)
100 ADDSHORT;
101 if ((4 & (u_long)w) != 0)
102 ADDWORD;
103 }
104
105 while ((mlen -= 32) >= 0)
106 ADD32;
107
108 mlen += 32;
109 if (mlen >= 16) {
110 ADD16;
111 mlen -= 16;
112 }
113 }
114
115 while (mlen > 0)
116 ADDBYTE;
117 }
118 if (bins & 1)
119 ROL;
120 REDUCE;
121
122 return (0xffff ^ sum);
123 }
124