in_cksum.h revision 86144
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/*- 34 * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>. 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 50 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 52 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 53 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 55 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 * 57 * from tahoe: in_cksum.c 1.2 86/01/05 58 * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91 59 * from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp 60 * from: FreeBSD: src/sys/alpha/include/in_cksum.h,v 1.5 2000/05/06 61 * 62 * $FreeBSD: head/sys/sparc64/include/in_cksum.h 86144 2001-11-06 20:05:01Z tmm $ 63 */ 64 65#ifndef _MACHINE_IN_CKSUM_H_ 66#define _MACHINE_IN_CKSUM_H_ 1 67 68#include <sys/cdefs.h> 69 70#define in_cksum(m, len) in_cksum_skip(m, len, 0) 71 72static __inline void 73in_cksum_update(struct ip *ip) 74{ 75 int __tmp; 76 77 __tmp = (int)ip->ip_sum + 1; 78 ip->ip_sum = __tmp + (__tmp >> 16); 79} 80 81static __inline u_short 82in_addword(u_short sum, u_short b) 83{ 84 u_long __ret, __tmp; 85 86 __asm( 87 "sll %2, 16, %0\n" 88 "sll %3, 16, %1\n" 89 "addcc %0, %1, %0\n" 90 "srl %0, 16, %0\n" 91 "addc %0, 0, %0\n" 92 : "=r" (__ret), "=r" (__tmp) : "r" (sum), "r" (b)); 93 return (__ret); 94} 95 96static __inline u_short 97in_pseudo(u_int sum, u_int b, u_int c) 98{ 99 u_long __tmp; 100 101 __asm( 102 "addcc %0, %3, %0\n" 103 "addccc %0, %4, %0\n" 104 "addc %0, 0, %0\n" 105 "sll %0, 16, %1\n" 106 "addcc %0, %1, %0\n" 107 "srl %0, 16, %0\n" 108 "addc %0, 0, %0\n" 109 : "=r" (sum), "=r" (__tmp) : "0" (sum), "r" (b), "r" (c)); 110 return (sum); 111} 112 113static __inline u_int 114in_cksum_hdr(struct ip *ip) 115{ 116 u_long __ret, __tmp1, __tmp2, __tmp3, __tmp4; 117 118 /* 119 * Use 32 bit memory accesses and additions - addition with carry only 120 * works for 32 bits, and fixing up alignent issues for 64 is probably 121 * more trouble than it's worth. 122 * This may read outside of the ip header, but does not cross a page 123 * boundary in doing so, so that should be OK. 124 * Actually, this specialized implementation might be overkill - using 125 * a generic implementation for both in_cksum_skip and in_cksum_hdr 126 * should not be too much more expensive. 127 */ 128#define __LD_ADD(addr, tmp, sum, offs, mod) \ 129 "lduw [" #addr " + " #offs "], " #tmp "\n" \ 130 "add" # mod " " #sum ", " #tmp ", " #sum "\n" 131 132 __asm( 133 "and %5, 3, %3\n" 134 "andn %5, 3, %1\n" 135 "brz,pt %3, 0f\n" 136 " lduw [%1], %0\n" 137 "mov 4, %4\n" 138 "sub %4, %3, %4\n" 139 "sll %4, 3, %4\n" /* fix up unaligned buffers */ 140 "mov 1, %2\n" 141 "sll %2, %4, %4\n" 142 "sub %4, 1, %4\n" 143 "lduw [%1 + 20], %2\n" 144 "andn %2, %4, %2\n" 145 "and %0, %4, %0\n" 146 "or %0, %2, %0\n" 147 "0:\n" 148 __LD_ADD(%1, %2, %0, 4, cc) 149 __LD_ADD(%1, %2, %0, 8, ccc) 150 __LD_ADD(%1, %2, %0, 12, ccc) 151 __LD_ADD(%1, %2, %0, 16, ccc) 152 "addc %0, 0, %0\n" /* reduce */ 153 "1:\n" 154 "sll %0, 16, %2\n" 155 "addcc %0, %2, %0\n" 156 "srl %0, 16, %0\n" 157 "addc %0, 0, %0\n" 158 "andcc %3, 1, %3\n" /* need to byte-swap? */ 159 "clr %3\n" 160 "bne,a,pn %%xcc, 1b\n" 161 " sll %0, 8, %0\n" 162 "not %0\n" 163 "sll %0, 16, %0\n" 164 "srl %0, 16, %0\n" 165 : "=r" (__ret), "=r" (__tmp1), "=r" (__tmp2), "=r" (__tmp3), 166 "=r" (__tmp4) : "1" (ip)); 167#undef __LD_ADD 168 return (__ret); 169} 170 171u_short in_cksum_skip(struct mbuf *m, int len, int skip); 172 173#endif /* _MACHINE_IN_CKSUM_H_ */ 174