in_cksum.c revision 28270
1/*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * from tahoe: in_cksum.c 1.2 86/01/05 34 * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91 35 * $Id: in_cksum.c,v 1.10 1997/02/22 09:32:20 peter Exp $ 36 */ 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/mbuf.h> 41#include <sys/socket.h> 42 43#include <netinet/in.h> 44#include <netinet/in_systm.h> 45#include <netinet/ip.h> 46 47#include <machine/in_cksum.h> 48 49/* 50 * Checksum routine for Internet Protocol family headers. 51 * 52 * This routine is very heavily used in the network 53 * code and should be modified for each CPU to be as fast as possible. 54 * 55 * This implementation is 386 version. 56 */ 57 58#undef ADDCARRY 59#define ADDCARRY(x) if ((x) > 0xffff) (x) -= 0xffff 60#define REDUCE {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY(sum);} 61 62/* 63 * Thanks to gcc we don't have to guess 64 * which registers contain sum & w. 65 */ 66#define ADD(n) asm("addl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (w)) 67#define ADDC(n) asm("adcl " #n "(%2), %0" : "=r" (sum) : "0" (sum), "r" (w)) 68#define LOAD(n) asm volatile("movb " #n "(%1), %0" : "=r" (junk) : "r" (w)) 69#define MOP asm("adcl $0, %0" : "=r" (sum) : "0" (sum)) 70 71int 72in_cksum(m, len) 73 register struct mbuf *m; 74 register int len; 75{ 76 register u_short *w; 77 register unsigned sum = 0; 78 register int mlen = 0; 79 int byte_swapped = 0; 80 union { char c[2]; u_short s; } su; 81 82 for (;m && len; m = m->m_next) { 83 if (m->m_len == 0) 84 continue; 85 w = mtod(m, u_short *); 86 if (mlen == -1) { 87 /* 88 * The first byte of this mbuf is the continuation 89 * of a word spanning between this mbuf and the 90 * last mbuf. 91 */ 92 93 /* su.c[0] is already saved when scanning previous 94 * mbuf. sum was REDUCEd when we found mlen == -1 95 */ 96 su.c[1] = *(u_char *)w; 97 sum += su.s; 98 w = (u_short *)((char *)w + 1); 99 mlen = m->m_len - 1; 100 len--; 101 } else 102 mlen = m->m_len; 103 if (len < mlen) 104 mlen = len; 105 len -= mlen; 106 /* 107 * Force to long boundary so we do longword aligned 108 * memory operations 109 */ 110 if (3 & (int) w) { 111 REDUCE; 112 if ((1 & (int) w) && (mlen > 0)) { 113 sum <<= 8; 114 su.c[0] = *(char *)w; 115 w = (u_short *)((char *)w + 1); 116 mlen--; 117 byte_swapped = 1; 118 } 119 if ((2 & (int) w) && (mlen >= 2)) { 120 sum += *w++; 121 mlen -= 2; 122 } 123 } 124 /* 125 * Advance to a 486 cache line boundary. 126 */ 127 if (4 & (int) w && mlen >= 4) { 128 ADD(0); 129 MOP; 130 w += 2; 131 mlen -= 4; 132 } 133 if (8 & (int) w && mlen >= 8) { 134 ADD(0); 135 ADDC(4); 136 MOP; 137 w += 4; 138 mlen -= 8; 139 } 140 /* 141 * Do as much of the checksum as possible 32 bits at at time. 142 * In fact, this loop is unrolled to make overhead from 143 * branches &c small. 144 */ 145 mlen -= 1; 146 while ((mlen -= 32) >= 0) { 147 u_char junk; 148 /* 149 * Add with carry 16 words and fold in the last 150 * carry by adding a 0 with carry. 151 * 152 * The early ADD(16) and the LOAD(32) are to load 153 * the next 2 cache lines in advance on 486's. The 154 * 486 has a penalty of 2 clock cycles for loading 155 * a cache line, plus whatever time the external 156 * memory takes to load the first word(s) addressed. 157 * These penalties are unavoidable. Subsequent 158 * accesses to a cache line being loaded (and to 159 * other external memory?) are delayed until the 160 * whole load finishes. These penalties are mostly 161 * avoided by not accessing external memory for 162 * 8 cycles after the ADD(16) and 12 cycles after 163 * the LOAD(32). The loop terminates when mlen 164 * is initially 33 (not 32) to guaranteed that 165 * the LOAD(32) is within bounds. 166 */ 167 ADD(16); 168 ADDC(0); 169 ADDC(4); 170 ADDC(8); 171 ADDC(12); 172 LOAD(32); 173 ADDC(20); 174 ADDC(24); 175 ADDC(28); 176 MOP; 177 w += 16; 178 } 179 mlen += 32 + 1; 180 if (mlen >= 32) { 181 ADD(16); 182 ADDC(0); 183 ADDC(4); 184 ADDC(8); 185 ADDC(12); 186 ADDC(20); 187 ADDC(24); 188 ADDC(28); 189 MOP; 190 w += 16; 191 mlen -= 32; 192 } 193 if (mlen >= 16) { 194 ADD(0); 195 ADDC(4); 196 ADDC(8); 197 ADDC(12); 198 MOP; 199 w += 8; 200 mlen -= 16; 201 } 202 if (mlen >= 8) { 203 ADD(0); 204 ADDC(4); 205 MOP; 206 w += 4; 207 mlen -= 8; 208 } 209 if (mlen == 0 && byte_swapped == 0) 210 continue; /* worth 1% maybe ?? */ 211 REDUCE; 212 while ((mlen -= 2) >= 0) { 213 sum += *w++; 214 } 215 if (byte_swapped) { 216 sum <<= 8; 217 byte_swapped = 0; 218 if (mlen == -1) { 219 su.c[1] = *(char *)w; 220 sum += su.s; 221 mlen = 0; 222 } else 223 mlen = -1; 224 } else if (mlen == -1) 225 /* 226 * This mbuf has odd number of bytes. 227 * There could be a word split betwen 228 * this mbuf and the next mbuf. 229 * Save the last byte (to prepend to next mbuf). 230 */ 231 su.c[0] = *(char *)w; 232 } 233 234 if (len) 235 printf("cksum: out of data\n"); 236 if (mlen == -1) { 237 /* The last mbuf has odd # of bytes. Follow the 238 standard (the odd byte is shifted left by 8 bits) */ 239 su.c[1] = 0; 240 sum += su.s; 241 } 242 REDUCE; 243 return (~sum & 0xffff); 244} 245 246/* 247 * This is the exact same algorithm as above with a few exceptions: 248 * (1) it is designed to operate on buffers, not mbufs 249 * (2) it returns an intermediate form of the sum which has to be 250 * explicitly finalized (but this can be delayed) 251 * (3) it accepts an intermediate sum 252 * 253 * This is particularly useful when building packets quickly, 254 * since one can compute the checksum of the pseudoheader ahead of 255 * time and then use this function to complete the work. That way, 256 * the pseudoheader never actually has to exist in the packet buffer, 257 * which avoids needless duplication of work. 258 */ 259in_psum_t 260in_cksum_partial(psum, w, len) 261 in_psum_t psum; 262 const u_short *w; 263 int len; 264{ 265 register in_psum_t sum = psum; 266 int byte_swapped = 0; 267 union { char c[2]; u_short s; } su; 268 269 /* 270 * Force to long boundary so we do longword aligned 271 * memory operations 272 */ 273 if (3 & (int) w) { 274 REDUCE; 275 if ((1 & (int) w) && (len > 0)) { 276 sum <<= 8; 277 su.c[0] = *(char *)w; 278 w = (u_short *)((char *)w + 1); 279 len--; 280 byte_swapped = 1; 281 } 282 if ((2 & (int) w) && (len >= 2)) { 283 sum += *w++; 284 len -= 2; 285 } 286 } 287 /* 288 * Advance to a 486 cache line boundary. 289 */ 290 if (4 & (int) w && len >= 4) { 291 ADD(0); 292 MOP; 293 w += 2; 294 len -= 4; 295 } 296 if (8 & (int) w && len >= 8) { 297 ADD(0); 298 ADDC(4); 299 MOP; 300 w += 4; 301 len -= 8; 302 } 303 /* 304 * Do as much of the checksum as possible 32 bits at at time. 305 * In fact, this loop is unrolled to make overhead from 306 * branches &c small. 307 */ 308 len -= 1; 309 while ((len -= 32) >= 0) { 310 u_char junk; 311 /* 312 * Add with carry 16 words and fold in the last 313 * carry by adding a 0 with carry. 314 * 315 * The early ADD(16) and the LOAD(32) are to load 316 * the next 2 cache lines in advance on 486's. The 317 * 486 has a penalty of 2 clock cycles for loading 318 * a cache line, plus whatever time the external 319 * memory takes to load the first word(s) addressed. 320 * These penalties are unavoidable. Subsequent 321 * accesses to a cache line being loaded (and to 322 * other external memory?) are delayed until the 323 * whole load finishes. These penalties are mostly 324 * avoided by not accessing external memory for 325 * 8 cycles after the ADD(16) and 12 cycles after 326 * the LOAD(32). The loop terminates when len 327 * is initially 33 (not 32) to guaranteed that 328 * the LOAD(32) is within bounds. 329 */ 330 ADD(16); 331 ADDC(0); 332 ADDC(4); 333 ADDC(8); 334 ADDC(12); 335 LOAD(32); 336 ADDC(20); 337 ADDC(24); 338 ADDC(28); 339 MOP; 340 w += 16; 341 } 342 len += 32 + 1; 343 if (len >= 32) { 344 ADD(16); 345 ADDC(0); 346 ADDC(4); 347 ADDC(8); 348 ADDC(12); 349 ADDC(20); 350 ADDC(24); 351 ADDC(28); 352 MOP; 353 w += 16; 354 len -= 32; 355 } 356 if (len >= 16) { 357 ADD(0); 358 ADDC(4); 359 ADDC(8); 360 ADDC(12); 361 MOP; 362 w += 8; 363 len -= 16; 364 } 365 if (len >= 8) { 366 ADD(0); 367 ADDC(4); 368 MOP; 369 w += 4; 370 len -= 8; 371 } 372 if (len == 0 && byte_swapped == 0) 373 goto out; 374 REDUCE; 375 while ((len -= 2) >= 0) { 376 sum += *w++; 377 } 378 if (byte_swapped) { 379 sum <<= 8; 380 byte_swapped = 0; 381 if (len == -1) { 382 su.c[1] = *(char *)w; 383 sum += su.s; 384 len = 0; 385 } else 386 len = -1; 387 } else if (len == -1) { 388 /* 389 * This buffer has odd number of bytes. 390 * There could be a word split betwen 391 * this buffer and the next. 392 */ 393 su.c[0] = *(char *)w; 394 } 395out: 396 if (len == -1) { 397 /* The last buffer has odd # of bytes. Follow the 398 standard (the odd byte is shifted left by 8 bits) */ 399 su.c[1] = 0; 400 sum += su.s; 401 } 402 return sum; 403} 404 405int 406in_cksum_finalize(psum) 407 in_psum_t psum; 408{ 409 in_psum_t sum = psum; 410 REDUCE; 411 return (sum & 0xffff); 412} 413