xref: /onnv-gate/usr/src/uts/common/inet/ip/ipcsum.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1992,1997 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <sys/stream.h>
33*0Sstevel@tonic-gate #include <sys/ddi.h>
34*0Sstevel@tonic-gate #include <sys/isa_defs.h>
35*0Sstevel@tonic-gate #include <inet/common.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #define	FOLD_SUM(sum) \
38*0Sstevel@tonic-gate { sum = (sum >> 16) + (sum & 0xFFFF); sum = (sum >> 16) + (sum & 0xFFFF); }
39*0Sstevel@tonic-gate #define	U16AM(p, i, m)	((((uint16_t *)(p))[i]) & (uint32_t)(m))
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  * For maximum efficiency, these access macros should be redone for
43*0Sstevel@tonic-gate  * machines that can access unaligned data.  NOTE: these assume
44*0Sstevel@tonic-gate  * ability to fetch from a zero extended 'uint8_t' and 'uint16_t'.  Add explicit
45*0Sstevel@tonic-gate  * masks in the U8_FETCH, U16_FETCH, PREV_TWO and NEXT_TWO as needed.
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #ifdef	_LITTLE_ENDIAN
49*0Sstevel@tonic-gate #define	U8_FETCH_FIRST(p)	((p)[0])
50*0Sstevel@tonic-gate #define	U8_FETCH_SECOND(p)	(((uint32_t)(p)[0]) << 8)
51*0Sstevel@tonic-gate #define	PREV_ONE(p)		U16AM(p, -1, 0xFF00)
52*0Sstevel@tonic-gate #define	NEXT_ONE(p)		U16AM(p, 0, 0xFF)
53*0Sstevel@tonic-gate #else
54*0Sstevel@tonic-gate #define	U8_FETCH_FIRST(p)	((uint32_t)((p)[0]) << 8)
55*0Sstevel@tonic-gate #define	U8_FETCH_SECOND(p)	((p)[0])
56*0Sstevel@tonic-gate #define	PREV_ONE(p)		U16AM(p, -1, 0xFF)
57*0Sstevel@tonic-gate #define	NEXT_ONE(p)		U16AM(p, 0, 0xFF00)
58*0Sstevel@tonic-gate #endif
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #define	U16_FETCH(p)		U8_FETCH_FIRST(p) + U8_FETCH_SECOND(p+1)
61*0Sstevel@tonic-gate #define	PREV_TWO(p)		((uint32_t)(((uint16_t *)(p))[-1]))
62*0Sstevel@tonic-gate #define	NEXT_TWO(p)		((uint32_t)(((uint16_t *)(p))[0]))
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate /*
65*0Sstevel@tonic-gate  * Return the ones complement checksum from the mblk chain at mp,
66*0Sstevel@tonic-gate  * after skipping offset bytes, and adding in the supplied partial
67*0Sstevel@tonic-gate  * sum.  Note that a final complement of the return value is needed
68*0Sstevel@tonic-gate  * if no further contributions to the checksum are forthcoming.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate uint16_t
ip_csum(mp,offset,sum)71*0Sstevel@tonic-gate ip_csum(mp, offset, sum)
72*0Sstevel@tonic-gate 	mblk_t *mp;
73*0Sstevel@tonic-gate 	int	offset;
74*0Sstevel@tonic-gate 	uint32_t	sum;
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	uint8_t	*startp = mp->b_rptr + offset;
77*0Sstevel@tonic-gate 	uint8_t	*endp = mp->b_wptr;
78*0Sstevel@tonic-gate /* >= 0x2 means flipped for memory align, 0x1 means last count was odd */
79*0Sstevel@tonic-gate 	int	odd_total = 0;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #ifdef	TEST_COVERAGE
82*0Sstevel@tonic-gate 	mblk_t *safe_mp;
83*0Sstevel@tonic-gate #define	INIT_COVERAGE()	(safe_mp = mp, safe_mp->b_next = NULL)
84*0Sstevel@tonic-gate #define	MARK_COVERAGE(flag) (safe_mp->b_next = \
85*0Sstevel@tonic-gate 	(mblk_t *)((uint32_t)safe_mp->b_next | flag))
86*0Sstevel@tonic-gate #else
87*0Sstevel@tonic-gate #define	INIT_COVERAGE()	/* */
88*0Sstevel@tonic-gate #define	MARK_COVERAGE(flag)	/* */
89*0Sstevel@tonic-gate #endif
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	for (;;) {
92*0Sstevel@tonic-gate 		INIT_COVERAGE();
93*0Sstevel@tonic-gate 		if ((endp - startp) < 10) {
94*0Sstevel@tonic-gate 			MARK_COVERAGE(0x1);
95*0Sstevel@tonic-gate 			while ((endp - startp) >= 2) {
96*0Sstevel@tonic-gate 				MARK_COVERAGE(0x2);
97*0Sstevel@tonic-gate 				sum += U16_FETCH(startp);
98*0Sstevel@tonic-gate 				startp += 2;
99*0Sstevel@tonic-gate 			}
100*0Sstevel@tonic-gate 			if ((endp - startp) >= 1) {
101*0Sstevel@tonic-gate 				MARK_COVERAGE(0x4);
102*0Sstevel@tonic-gate 				odd_total = 1;
103*0Sstevel@tonic-gate 				sum += U8_FETCH_FIRST(startp);
104*0Sstevel@tonic-gate 			}
105*0Sstevel@tonic-gate 			MARK_COVERAGE(0x8);
106*0Sstevel@tonic-gate 			FOLD_SUM(sum);
107*0Sstevel@tonic-gate 			goto next_frag;
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 		if ((uint32_t)startp & 0x1) {
110*0Sstevel@tonic-gate 			MARK_COVERAGE(0x10);
111*0Sstevel@tonic-gate 			odd_total = 3;
112*0Sstevel@tonic-gate 			startp++;
113*0Sstevel@tonic-gate 			sum = (sum << 8) + PREV_ONE(startp);
114*0Sstevel@tonic-gate 		}
115*0Sstevel@tonic-gate 		if ((uint32_t)startp & 0x2) {
116*0Sstevel@tonic-gate 			MARK_COVERAGE(0x20);
117*0Sstevel@tonic-gate 			startp += 2;
118*0Sstevel@tonic-gate 			sum += PREV_TWO(startp);
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 		if ((uint32_t)endp & 0x1) {
121*0Sstevel@tonic-gate 			MARK_COVERAGE(0x40);
122*0Sstevel@tonic-gate 			odd_total ^= 0x1;
123*0Sstevel@tonic-gate 			endp--;
124*0Sstevel@tonic-gate 			sum += NEXT_ONE(endp);
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 		if ((uint32_t)endp & 0x2) {
127*0Sstevel@tonic-gate 			MARK_COVERAGE(0x80);
128*0Sstevel@tonic-gate 			endp -= 2;
129*0Sstevel@tonic-gate 			sum += NEXT_TWO(endp);
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		{
133*0Sstevel@tonic-gate #ifdef	NOT_ALL_PTRS_EQUAL
134*0Sstevel@tonic-gate #define	INC_PTR(cnt)	ptr += cnt
135*0Sstevel@tonic-gate #define	INC_ENDPTR(cnt)	endptr += cnt
136*0Sstevel@tonic-gate 			uint32_t	*ptr = (uint32_t *)startp;
137*0Sstevel@tonic-gate 			uint32_t	*endptr = (uint32_t *)endp;
138*0Sstevel@tonic-gate #else
139*0Sstevel@tonic-gate #define	INC_PTR(cnt)	startp += (cnt * sizeof (uint32_t))
140*0Sstevel@tonic-gate #define	INC_ENDPTR(cnt)	endp += (cnt * sizeof (uint32_t))
141*0Sstevel@tonic-gate #define	ptr		((uint32_t *)startp)
142*0Sstevel@tonic-gate #define	endptr		((uint32_t *)endp)
143*0Sstevel@tonic-gate #endif
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate #ifdef	USE_FETCH_AND_SHIFT
147*0Sstevel@tonic-gate 			uint32_t	u1, u2;
148*0Sstevel@tonic-gate 			uint32_t	mask = 0xFFFF;
149*0Sstevel@tonic-gate #define	LOAD1(i)	u1 = ptr[i]
150*0Sstevel@tonic-gate #define	LOAD2(i)	u2 = ptr[i]
151*0Sstevel@tonic-gate #define	SUM1(i)		sum += (u1 & mask) + (u1 >> 16)
152*0Sstevel@tonic-gate #define	SUM2(i)		sum += (u2 & mask) + (u2 >> 16)
153*0Sstevel@tonic-gate #endif
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate #ifdef	USE_FETCH_AND_ADDC
156*0Sstevel@tonic-gate 			uint32_t	u1, u2;
157*0Sstevel@tonic-gate #define	LOAD1(i)	u1 = ptr[i]
158*0Sstevel@tonic-gate #define	LOAD2(i)	u2 = ptr[i]
159*0Sstevel@tonic-gate #define	SUM1(i)		sum += u1
160*0Sstevel@tonic-gate #define	SUM2(i)		sum += u2
161*0Sstevel@tonic-gate #endif
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate #ifdef	USE_ADDC
164*0Sstevel@tonic-gate #define	SUM1(i)		sum += ptr[i]
165*0Sstevel@tonic-gate #endif
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate #ifdef	USE_POSTINC
168*0Sstevel@tonic-gate #define	SUM1(i)		sum += *((uint16_t *)ptr)++; sum += *((uint16_t *)ptr)++
169*0Sstevel@tonic-gate #undef	INC_PTR
170*0Sstevel@tonic-gate #define	INC_PTR(i)	/* */
171*0Sstevel@tonic-gate #endif
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate #ifndef	LOAD1
174*0Sstevel@tonic-gate #define	LOAD1(i)	/* */
175*0Sstevel@tonic-gate #endif
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate #ifndef	LOAD2
178*0Sstevel@tonic-gate #define	LOAD2(i)	/* */
179*0Sstevel@tonic-gate #endif
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate #ifndef	SUM2
182*0Sstevel@tonic-gate #define	SUM2(i)		SUM1(i)
183*0Sstevel@tonic-gate #endif
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate /* USE_INDEXING is the default */
186*0Sstevel@tonic-gate #ifndef	SUM1
187*0Sstevel@tonic-gate #define	SUM1(i)
188*0Sstevel@tonic-gate 	sum += ((uint16_t *)ptr)[i * 2]; sum += ((uint16_t *)ptr)[(i * 2) + 1]
189*0Sstevel@tonic-gate #endif
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		LOAD1(0);
192*0Sstevel@tonic-gate 		INC_ENDPTR(-8);
193*0Sstevel@tonic-gate 		if (ptr <= endptr) {
194*0Sstevel@tonic-gate 			MARK_COVERAGE(0x100);
195*0Sstevel@tonic-gate 			do {
196*0Sstevel@tonic-gate 				LOAD2(1); SUM1(0);
197*0Sstevel@tonic-gate 				LOAD1(2); SUM2(1);
198*0Sstevel@tonic-gate 				LOAD2(3); SUM1(2);
199*0Sstevel@tonic-gate 				LOAD1(4); SUM2(3);
200*0Sstevel@tonic-gate 				LOAD2(5); SUM1(4);
201*0Sstevel@tonic-gate 				LOAD1(6); SUM2(5);
202*0Sstevel@tonic-gate 				LOAD2(7); SUM1(6);
203*0Sstevel@tonic-gate 				LOAD1(8); SUM2(7);
204*0Sstevel@tonic-gate 				INC_PTR(8);
205*0Sstevel@tonic-gate 			} while (ptr <= endptr);
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate #ifdef USE_TAIL_SWITCH
208*0Sstevel@tonic-gate 		switch ((endptr + 8) - ptr) {
209*0Sstevel@tonic-gate 		case 7:	LOAD2(6); SUM2(6);
210*0Sstevel@tonic-gate 		case 6:	LOAD2(5); SUM2(5);
211*0Sstevel@tonic-gate 		case 5:	LOAD2(4); SUM2(4);
212*0Sstevel@tonic-gate 		case 4:	LOAD2(3); SUM2(3);
213*0Sstevel@tonic-gate 		case 3:	LOAD2(2); SUM2(2);
214*0Sstevel@tonic-gate 		case 2:	LOAD2(1); SUM2(1);
215*0Sstevel@tonic-gate 		case 1:	SUM1(0);
216*0Sstevel@tonic-gate 		case 0:	break;
217*0Sstevel@tonic-gate 		}
218*0Sstevel@tonic-gate #else
219*0Sstevel@tonic-gate 		INC_ENDPTR(4);
220*0Sstevel@tonic-gate 		if (ptr <= endptr) {
221*0Sstevel@tonic-gate 			MARK_COVERAGE(0x200);
222*0Sstevel@tonic-gate 			LOAD2(1); SUM1(0);
223*0Sstevel@tonic-gate 			LOAD1(2); SUM2(1);
224*0Sstevel@tonic-gate 			LOAD2(3); SUM1(2);
225*0Sstevel@tonic-gate 			LOAD1(4); SUM2(3);
226*0Sstevel@tonic-gate 			INC_PTR(4);
227*0Sstevel@tonic-gate 		}
228*0Sstevel@tonic-gate 		INC_ENDPTR(4);
229*0Sstevel@tonic-gate 		if (ptr < endptr) {
230*0Sstevel@tonic-gate 			MARK_COVERAGE(0x400);
231*0Sstevel@tonic-gate 			do {
232*0Sstevel@tonic-gate 				SUM1(0); LOAD1(1);
233*0Sstevel@tonic-gate 				INC_PTR(1);
234*0Sstevel@tonic-gate 			} while (ptr < endptr);
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate #endif
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 		FOLD_SUM(sum);
240*0Sstevel@tonic-gate 		if (odd_total > 1) {
241*0Sstevel@tonic-gate 			MARK_COVERAGE(0x800);
242*0Sstevel@tonic-gate 			sum = ((sum << 8) | (sum >> 8)) & 0xFFFF;
243*0Sstevel@tonic-gate 			odd_total -= 2;
244*0Sstevel@tonic-gate 		}
245*0Sstevel@tonic-gate next_frag:
246*0Sstevel@tonic-gate 		mp = mp->b_cont;
247*0Sstevel@tonic-gate 		if (!mp) {
248*0Sstevel@tonic-gate 			MARK_COVERAGE(0x1000);
249*0Sstevel@tonic-gate 			{
250*0Sstevel@tonic-gate 			uint32_t	u1 = sum;
251*0Sstevel@tonic-gate 			return ((uint16_t)u1);
252*0Sstevel@tonic-gate 			}
253*0Sstevel@tonic-gate 		}
254*0Sstevel@tonic-gate 		MARK_COVERAGE(0x4000);
255*0Sstevel@tonic-gate 		startp = mp->b_rptr;
256*0Sstevel@tonic-gate 		endp = mp->b_wptr;
257*0Sstevel@tonic-gate 		if (odd_total && (endp > startp)) {
258*0Sstevel@tonic-gate 			MARK_COVERAGE(0x8000);
259*0Sstevel@tonic-gate 			odd_total = 0;
260*0Sstevel@tonic-gate 			sum += U8_FETCH_SECOND(startp);
261*0Sstevel@tonic-gate 			startp++;
262*0Sstevel@tonic-gate 		}
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate #undef	endptr
266*0Sstevel@tonic-gate #undef	INIT_COVERAGE
267*0Sstevel@tonic-gate #undef	INC_PTR
268*0Sstevel@tonic-gate #undef	INC_ENDPTR
269*0Sstevel@tonic-gate #undef	LOAD1
270*0Sstevel@tonic-gate #undef	LOAD2
271*0Sstevel@tonic-gate #undef	MARK_COVERAGE
272*0Sstevel@tonic-gate #undef	ptr
273*0Sstevel@tonic-gate #undef	SUM1
274*0Sstevel@tonic-gate #undef	SUM2
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate #undef	FOLD_SUM
279*0Sstevel@tonic-gate #undef	NEXT_ONE
280*0Sstevel@tonic-gate #undef	NEXT_TWO
281*0Sstevel@tonic-gate #undef	PREV_ONE
282*0Sstevel@tonic-gate #undef	PREV_TWO
283*0Sstevel@tonic-gate #undef	U8_FETCH_FIRST
284*0Sstevel@tonic-gate #undef	U8_FETCH_SECOND
285*0Sstevel@tonic-gate #undef	U16AM
286*0Sstevel@tonic-gate #undef	U16_FETCH
287