in_cksum.c revision 115683
1227825Stheraven/*- 2227825Stheraven * Copyright (c) 1990 The Regents of the University of California. 3227825Stheraven * All rights reserved. 4227825Stheraven * 5227825Stheraven * Redistribution and use in source and binary forms, with or without 6227825Stheraven * modification, are permitted provided that the following conditions 7227825Stheraven * are met: 8227825Stheraven * 1. Redistributions of source code must retain the above copyright 9227825Stheraven * notice, this list of conditions and the following disclaimer. 10227825Stheraven * 2. Redistributions in binary form must reproduce the above copyright 11227825Stheraven * notice, this list of conditions and the following disclaimer in the 12227825Stheraven * documentation and/or other materials provided with the distribution. 13227825Stheraven * 3. All advertising materials mentioning features or use of this software 14227825Stheraven * must display the following acknowledgement: 15227825Stheraven * This product includes software developed by the University of 16227825Stheraven * California, Berkeley and its contributors. 17227825Stheraven * 4. Neither the name of the University nor the names of its contributors 18227825Stheraven * may be used to endorse or promote products derived from this software 19227825Stheraven * without specific prior written permission. 20227825Stheraven * 21227825Stheraven * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22227825Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23227825Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24227825Stheraven * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25227825Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26227825Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27227825Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28262801Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29227825Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30262801Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31227825Stheraven * SUCH DAMAGE. 32262801Sdim * 33227825Stheraven * from tahoe: in_cksum.c 1.2 86/01/05 34262801Sdim * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91 35227825Stheraven */ 36262801Sdim 37227825Stheraven#include <sys/cdefs.h> 38262801Sdim__FBSDID("$FreeBSD: head/sys/i386/i386/in_cksum.c 115683 2003-06-02 06:43:15Z obrien $"); 39227825Stheraven 40227825Stheraven/* 41227825Stheraven * MPsafe: alfred 42227825Stheraven */ 43227825Stheraven#include <sys/param.h> 44227825Stheraven#include <sys/systm.h> 45227825Stheraven#include <sys/mbuf.h> 46227825Stheraven 47227825Stheraven#include <netinet/in.h> 48227825Stheraven#include <netinet/in_systm.h> 49227825Stheraven#include <netinet/ip.h> 50227825Stheraven 51227825Stheraven#include <machine/in_cksum.h> 52227825Stheraven 53227825Stheraven/* 54227825Stheraven * Checksum routine for Internet Protocol family headers. 55227825Stheraven * 56227825Stheraven * This routine is very heavily used in the network 57227825Stheraven * code and should be modified for each CPU to be as fast as possible. 58227825Stheraven * 59227825Stheraven * This implementation is 386 version. 60227825Stheraven */ 61227825Stheraven 62227825Stheraven#undef ADDCARRY 63227825Stheraven#define ADDCARRY(x) if ((x) > 0xffff) (x) -= 0xffff 64227825Stheraven#define REDUCE {sum = (sum & 0xffff) + (sum >> 16); ADDCARRY(sum);} 65227825Stheraven 66227825Stheraven/* 67227825Stheraven * These asm statements require __volatile because they pass information 68227825Stheraven * via the condition codes. GCC does not currently provide a way to specify 69227825Stheraven * the condition codes as an input or output operand. 70227825Stheraven * 71227825Stheraven * The LOAD macro below is effectively a prefetch into cache. GCC will 72227825Stheraven * load the value into a register but will not use it. Since modern CPUs 73227825Stheraven * reorder operations, this will generally take place in parallel with 74227825Stheraven * other calculations. 75227825Stheraven */ 76227825Stheraven#define ADD(n) __asm __volatile \ 77227825Stheraven ("addl %1, %0" : "+r" (sum) : \ 78227825Stheraven "g" (((const u_int32_t *)w)[n / 4])) 79227825Stheraven#define ADDC(n) __asm __volatile \ 80227825Stheraven ("adcl %1, %0" : "+r" (sum) : \ 81227825Stheraven "g" (((const u_int32_t *)w)[n / 4])) 82227825Stheraven#define LOAD(n) __asm __volatile \ 83227825Stheraven ("" : : "r" (((const u_int32_t *)w)[n / 4])) 84227825Stheraven#define MOP __asm __volatile \ 85227825Stheraven ("adcl $0, %0" : "+r" (sum)) 86227825Stheraven 87227825Stheravenu_short 88227825Stheravenin_cksum_skip(m, len, skip) 89227825Stheraven struct mbuf *m; 90227825Stheraven int len; 91227825Stheraven int skip; 92227825Stheraven{ 93227825Stheraven register u_short *w; 94227825Stheraven register unsigned sum = 0; 95227825Stheraven register int mlen = 0; 96227825Stheraven int byte_swapped = 0; 97227825Stheraven union { char c[2]; u_short s; } su; 98227825Stheraven 99227825Stheraven len -= skip; 100227825Stheraven for (; skip && m; m = m->m_next) { 101227825Stheraven if (m->m_len > skip) { 102227825Stheraven mlen = m->m_len - skip; 103227825Stheraven w = (u_short *)(mtod(m, u_char *) + skip); 104227825Stheraven goto skip_start; 105227825Stheraven } else { 106227825Stheraven skip -= m->m_len; 107227825Stheraven } 108227825Stheraven } 109227825Stheraven 110227825Stheraven for (;m && len; m = m->m_next) { 111227825Stheraven if (m->m_len == 0) 112227825Stheraven continue; 113227825Stheraven w = mtod(m, u_short *); 114227825Stheraven if (mlen == -1) { 115227825Stheraven /* 116227825Stheraven * The first byte of this mbuf is the continuation 117227825Stheraven * of a word spanning between this mbuf and the 118227825Stheraven * last mbuf. 119227825Stheraven */ 120227825Stheraven 121227825Stheraven /* su.c[0] is already saved when scanning previous 122227825Stheraven * mbuf. sum was REDUCEd when we found mlen == -1 123227825Stheraven */ 124227825Stheraven su.c[1] = *(u_char *)w; 125227825Stheraven sum += su.s; 126227825Stheraven w = (u_short *)((char *)w + 1); 127227825Stheraven mlen = m->m_len - 1; 128227825Stheraven len--; 129227825Stheraven } else 130227825Stheraven mlen = m->m_len; 131227825Stheravenskip_start: 132227825Stheraven if (len < mlen) 133227825Stheraven mlen = len; 134227825Stheraven len -= mlen; 135227825Stheraven /* 136227825Stheraven * Force to long boundary so we do longword aligned 137227825Stheraven * memory operations 138227825Stheraven */ 139227825Stheraven if (3 & (int) w) { 140227825Stheraven REDUCE; 141227825Stheraven if ((1 & (int) w) && (mlen > 0)) { 142227825Stheraven sum <<= 8; 143227825Stheraven su.c[0] = *(char *)w; 144227825Stheraven w = (u_short *)((char *)w + 1); 145227825Stheraven mlen--; 146227825Stheraven byte_swapped = 1; 147227825Stheraven } 148227825Stheraven if ((2 & (int) w) && (mlen >= 2)) { 149227825Stheraven sum += *w++; 150227825Stheraven mlen -= 2; 151227825Stheraven } 152227825Stheraven } 153227825Stheraven /* 154227825Stheraven * Advance to a 486 cache line boundary. 155227825Stheraven */ 156227825Stheraven if (4 & (int) w && mlen >= 4) { 157227825Stheraven ADD(0); 158227825Stheraven MOP; 159227825Stheraven w += 2; 160227825Stheraven mlen -= 4; 161227825Stheraven } 162227825Stheraven if (8 & (int) w && mlen >= 8) { 163227825Stheraven ADD(0); 164227825Stheraven ADDC(4); 165227825Stheraven MOP; 166227825Stheraven w += 4; 167227825Stheraven mlen -= 8; 168227825Stheraven } 169227825Stheraven /* 170227825Stheraven * Do as much of the checksum as possible 32 bits at at time. 171227825Stheraven * In fact, this loop is unrolled to make overhead from 172227825Stheraven * branches &c small. 173227825Stheraven */ 174227825Stheraven mlen -= 1; 175227825Stheraven while ((mlen -= 32) >= 0) { 176227825Stheraven /* 177227825Stheraven * Add with carry 16 words and fold in the last 178227825Stheraven * carry by adding a 0 with carry. 179227825Stheraven * 180227825Stheraven * The early ADD(16) and the LOAD(32) are to load 181227825Stheraven * the next 2 cache lines in advance on 486's. The 182227825Stheraven * 486 has a penalty of 2 clock cycles for loading 183227825Stheraven * a cache line, plus whatever time the external 184227825Stheraven * memory takes to load the first word(s) addressed. 185227825Stheraven * These penalties are unavoidable. Subsequent 186227825Stheraven * accesses to a cache line being loaded (and to 187227825Stheraven * other external memory?) are delayed until the 188227825Stheraven * whole load finishes. These penalties are mostly 189227825Stheraven * avoided by not accessing external memory for 190227825Stheraven * 8 cycles after the ADD(16) and 12 cycles after 191227825Stheraven * the LOAD(32). The loop terminates when mlen 192227825Stheraven * is initially 33 (not 32) to guaranteed that 193227825Stheraven * the LOAD(32) is within bounds. 194227825Stheraven */ 195227825Stheraven ADD(16); 196227825Stheraven ADDC(0); 197227825Stheraven ADDC(4); 198227825Stheraven ADDC(8); 199227825Stheraven ADDC(12); 200227825Stheraven LOAD(32); 201227825Stheraven ADDC(20); 202227825Stheraven ADDC(24); 203227825Stheraven ADDC(28); 204227825Stheraven MOP; 205227825Stheraven w += 16; 206227825Stheraven } 207227825Stheraven mlen += 32 + 1; 208227825Stheraven if (mlen >= 32) { 209227825Stheraven ADD(16); 210227825Stheraven ADDC(0); 211227825Stheraven ADDC(4); 212227825Stheraven ADDC(8); 213227825Stheraven ADDC(12); 214227825Stheraven ADDC(20); 215227825Stheraven ADDC(24); 216227825Stheraven ADDC(28); 217227825Stheraven MOP; 218227825Stheraven w += 16; 219227825Stheraven mlen -= 32; 220227825Stheraven } 221227825Stheraven if (mlen >= 16) { 222227825Stheraven ADD(0); 223227825Stheraven ADDC(4); 224227825Stheraven ADDC(8); 225227825Stheraven ADDC(12); 226227825Stheraven MOP; 227227825Stheraven w += 8; 228227825Stheraven mlen -= 16; 229227825Stheraven } 230227825Stheraven if (mlen >= 8) { 231227825Stheraven ADD(0); 232227825Stheraven ADDC(4); 233227825Stheraven MOP; 234227825Stheraven w += 4; 235227825Stheraven mlen -= 8; 236227825Stheraven } 237227825Stheraven if (mlen == 0 && byte_swapped == 0) 238227825Stheraven continue; /* worth 1% maybe ?? */ 239227825Stheraven REDUCE; 240227825Stheraven while ((mlen -= 2) >= 0) { 241227825Stheraven sum += *w++; 242227825Stheraven } 243227825Stheraven if (byte_swapped) { 244227825Stheraven sum <<= 8; 245227825Stheraven byte_swapped = 0; 246227825Stheraven if (mlen == -1) { 247227825Stheraven su.c[1] = *(char *)w; 248227825Stheraven sum += su.s; 249227825Stheraven mlen = 0; 250227825Stheraven } else 251227825Stheraven mlen = -1; 252227825Stheraven } else if (mlen == -1) 253227825Stheraven /* 254227825Stheraven * This mbuf has odd number of bytes. 255227825Stheraven * There could be a word split betwen 256227825Stheraven * this mbuf and the next mbuf. 257227825Stheraven * Save the last byte (to prepend to next mbuf). 258227825Stheraven */ 259227825Stheraven su.c[0] = *(char *)w; 260227825Stheraven } 261227825Stheraven 262227825Stheraven if (len) 263227825Stheraven printf("%s: out of data by %d\n", __func__, len); 264227825Stheraven if (mlen == -1) { 265227825Stheraven /* The last mbuf has odd # of bytes. Follow the 266227825Stheraven standard (the odd byte is shifted left by 8 bits) */ 267227825Stheraven su.c[1] = 0; 268227825Stheraven sum += su.s; 269227825Stheraven } 270227825Stheraven REDUCE; 271227825Stheraven return (~sum & 0xffff); 272227825Stheraven} 273227825Stheraven