xref: /csrg-svn/sys/netns/ns_cksum.c (revision 33597)
1 /*
2  * Copyright (c) 1982, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  *      @(#)ns_cksum.c	1.2 (Berkeley) 02/25/88
13  */
14 #include "types.h"
15 #include "mbuf.h"
16 
17 /*
18  * Checksum routine for Network Systems Protocol Packets.
19  *
20  * This routine is very heavily used in the network
21  * code and should be modified for each CPU to be as fast as possible.
22  */
23 
24 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
25 #define FOLD(x) {l_util.l = x; x = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
26 
27 u_short
28 ns_cksum(m, len)
29 	register struct mbuf *m;
30 	register int len;
31 {
32 	register u_short *w;
33 	register int sum = 0;
34 	register int mlen = 0;
35 	register int sum2;
36 
37 	union {
38 		u_short s[2];
39 		long	l;
40 	} l_util;
41 
42 	for (;m && len; m = m->m_next) {
43 		if (m->m_len == 0)
44 			continue;
45 		/*
46 		 * Each trip around loop adds in
47 		 * word from one mbuf segment.
48 		 */
49 		w = mtod(m, u_short *);
50 		if (mlen == -1) {
51 			/*
52 			 * There is a byte left from the last segment;
53 			 * ones-complement add it into the checksum.
54 			 */
55 			sum  += *(u_char *)w; /* Big-Endian, else << 8 */
56 			sum += sum;
57 			w = (u_short *)(1 + (char *)w);
58 			mlen = m->m_len - 1;
59 			len--;
60 			FOLD(sum);
61 		} else
62 			mlen = m->m_len;
63 		if (len < mlen)
64 			mlen = len;
65 		len -= mlen;
66 		/*
67 		 * We can do a 16 bit ones complement sum using
68 		 * 32 bit arithmetic registers for adding,
69 		 * with carries from the low added
70 		 * into the high (by normal carry-chaining)
71 		 * so long as we fold back before 16 carries have occured.
72 		 */
73 		if (1 & (int) w)
74 			goto uuuuglyy;
75 #ifndef TINY
76 /* -DTINY reduces the size from 1250 to 550, but slows it down by 22% */
77 		while ((mlen -= 32) >= 0) {
78 			sum += w[0]; sum += sum; sum += w[1]; sum += sum;
79 			sum += w[2]; sum += sum; sum += w[3]; sum += sum;
80 			sum += w[4]; sum += sum; sum += w[5]; sum += sum;
81 			sum += w[6]; sum += sum; sum += w[7]; sum += sum;
82 			FOLD(sum);
83 			sum += w[8]; sum += sum; sum += w[9]; sum += sum;
84 			sum += w[10]; sum += sum; sum += w[11]; sum += sum;
85 			sum += w[12]; sum += sum; sum += w[13]; sum += sum;
86 			sum += w[14]; sum += sum; sum += w[15]; sum += sum;
87 			FOLD(sum);
88 			w += 16;
89 		}
90 		mlen += 32;
91 #endif
92 		while ((mlen -= 8) >= 0) {
93 			sum += w[0]; sum += sum; sum += w[1]; sum += sum;
94 			sum += w[2]; sum += sum; sum += w[3]; sum += sum;
95 			FOLD(sum);
96 			w += 4;
97 		}
98 		mlen += 8;
99 		while ((mlen -= 2) >= 0) {
100 			sum += *w++; sum += sum;
101 		}
102 		goto commoncase;
103 uuuuglyy:
104 #define ww(n) (((char *)w)[n + n])
105 #define vv(n) (((char *)w)[n + n + 1])
106 /* Big-Endian; else reverse ww and vv */
107 		sum2 = 0;
108 #ifndef TINY
109 		while ((mlen -= 32) >= 0) {
110 		    sum += ww(0); sum += sum; sum += ww(1); sum += sum;
111 		    sum += ww(2); sum += sum; sum += ww(3); sum += sum;
112 		    sum += ww(4); sum += sum; sum += ww(5); sum += sum;
113 		    sum += ww(6); sum += sum; sum += ww(7); sum += sum;
114 		    FOLD(sum);
115 		    sum += ww(8); sum += sum; sum += ww(9); sum += sum;
116 		    sum += ww(10); sum += sum; sum += ww(11); sum += sum;
117 		    sum += ww(12); sum += sum; sum += ww(13); sum += sum;
118 		    sum += ww(14); sum += sum; sum += ww(15); sum += sum;
119 		    FOLD(sum);
120 		    sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
121 		    sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
122 		    sum2 += vv(4); sum2 += sum2; sum2 += vv(5); sum2 += sum2;
123 		    sum2 += vv(6); sum2 += sum2; sum2 += vv(7); sum2 += sum2;
124 		    FOLD(sum2);
125 		    sum2 += vv(8); sum2 += sum2; sum2 += vv(9); sum2 += sum2;
126 		    sum2 += vv(10); sum2 += sum2; sum2 += vv(11); sum2 += sum2;
127 		    sum2 += vv(12); sum2 += sum2; sum2 += vv(13); sum2 += sum2;
128 		    sum2 += vv(14); sum2 += sum2; sum2 += vv(15); sum2 += sum2;
129 		    FOLD(sum2);
130 		    w += 16;
131 		}
132 		mlen += 32;
133 #endif
134 		while ((mlen -= 8) >= 0) {
135 		    sum += ww(0); sum += sum; sum += ww(1); sum += sum;
136 		    sum += ww(2); sum += sum; sum += ww(3); sum += sum;
137 		    FOLD(sum);
138 		    sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
139 		    sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
140 		    FOLD(sum2);
141 		    w += 4;
142 		}
143 		mlen += 8;
144 		while ((mlen -= 2) >= 0) {
145 			sum += ww(0); sum += sum;
146 			sum2 += vv(0); sum2 += sum2;
147 			w++;
148 		}
149 		sum += (sum2 << 8);
150 commoncase:
151 		if (mlen == -1) {
152 			sum += *(u_char *)w << 8; /* Big-Endian, else no << 8 */
153 		}
154 		FOLD(sum);
155 	}
156 	if (mlen == -1) {
157 		/* We had an odd number of bytes to sum; assume a garbage
158 		   byte of zero and clean up */
159 		sum += sum;
160 		FOLD(sum);
161 	}
162 	/*
163 	 * sum has already been kept to low sixteen bits.
164 	 * just examine result and exit.
165 	 */
166 	if(sum==0xffff) sum = 0;
167 	return (sum);
168 }
169