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