source: rtems/cpukit/libnetworking/netinet/in_cksum_sparc.h @ 657e6c93

Last change on this file since 657e6c93 was 657e6c93, checked in by Christian Mauderer <Christian.Mauderer@…>, on Jun 24, 2016 at 5:57:17 AM

libnetworking: Import current <netinet/in.h>

Import the <netinet/in.h> from current FreeBSD. This allows to build
some current software (e.g. libressl).

Add legacy support like

  • prototype for in_cksum(),
  • IPPORT_USERRESERVED,
  • deprecated IPCTL_RT* defines,
  • ip_fw_chk_t and ip_fw_ctl_t,
  • ip_nat_... (IP NAT hooks), and
  • IP_NAT option for get/setsockopt()

to new <rtems/rtems_netinet_in.h>.

  • Property mode set to 100644
File size: 9.0 KB
Line 
1/*      $NetBSD: in_cksum.c,v 1.12.10.4 2005/12/11 10:28:36 christos Exp $ */
2
3/*
4 * Copyright (c) 1995 Matthew R. Green.
5 * Copyright (c) 1992, 1993
6 *      The Regents of the University of California.  All rights reserved.
7 *
8 * All advertising materials mentioning features or use of this software
9 * must display the following acknowledgement:
10 *      This product includes software developed by the University of
11 *      California, and it's contributors.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *      @(#)in_cksum.c  8.1 (Berkeley) 6/11/93
38 */
39
40/*
41 * Copyright (c) 1995 Zubin Dittia.
42 * Copyright (c) 1994, 1998 Charles M. Hannum.
43 *
44 * All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 *      This product includes software developed by the University of
47 *      California, and it's contributors.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 *    notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 *    notice, this list of conditions and the following disclaimer in the
56 *    documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 *    must display the following acknowledgement:
59 *      This product includes software developed by the University of
60 *      California, Berkeley and its contributors.
61 * 4. Neither the name of the University nor the names of its contributors
62 *    may be used to endorse or promote products derived from this software
63 *    without specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 * SUCH DAMAGE.
76 *
77 *      @(#)in_cksum.c  8.1 (Berkeley) 6/11/93
78 */
79
80#include <sys/cdefs.h>
81#include <sys/param.h>
82#include <sys/systm.h>
83#include <sys/mbuf.h>
84#include <netinet/in.h>
85#include <rtems/rtems_netinet_in.h>
86#include <netinet/in_systm.h>
87#include <netinet/ip.h>
88#include <netinet/ip_var.h>
89
90/*
91 * Checksum routine for Internet Protocol family headers.
92 *
93 * This routine is very heavily used in the network
94 * code and should be modified for each CPU to be as fast as possible.
95 *
96 * SPARC version.
97 */
98
99/*
100 * The checksum computation code here is significantly faster than its
101 * vanilla C counterpart (by significantly, I mean 2-3 times faster if
102 * the data is in cache, and 1.5-2 times faster if the data is not in
103 * cache).
104 * We optimize on three fronts:
105 *      1. By using the add-with-carry (addxcc) instruction, we can use
106 *         32-bit operations instead of 16-bit operations.
107 *      2. By unrolling the main loop to reduce branch overheads.
108 *      3. By doing a sequence of load,load,add,add,load,load,add,add,
109 *         we can avoid the extra stall cycle which is incurred if the
110 *         instruction immediately following a load tries to use the
111 *         target register of the load.
112 * Another possible optimization is to replace a pair of 32-bit loads
113 * with a single 64-bit load (ldd) instruction, but I found that although
114 * this improves performance somewhat on Sun4c machines, it actually
115 * reduces performance considerably on Sun4m machines (I don't know why).
116 * So I chose to leave it out.
117 *
118 * Zubin Dittia (zubin@dworkin.wustl.edu)
119 */
120
121#define Asm     __asm __volatile
122#define ADD64           Asm("   ld [%4+ 0],%1;   ld [%4+ 4],%2;         \
123                                addcc  %0,%1,%0; addxcc %0,%2,%0;       \
124                                ld [%4+ 8],%1;   ld [%4+12],%2;         \
125                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
126                                ld [%4+16],%1;   ld [%4+20],%2;         \
127                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
128                                ld [%4+24],%1;   ld [%4+28],%2;         \
129                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
130                                ld [%4+32],%1;   ld [%4+36],%2;         \
131                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
132                                ld [%4+40],%1;   ld [%4+44],%2;         \
133                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
134                                ld [%4+48],%1;   ld [%4+52],%2;         \
135                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
136                                ld [%4+56],%1;   ld [%4+60],%2;         \
137                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
138                                addxcc %0,0,%0"                         \
139                                : "=r" (sum), "=&r" (tmp1), "=&r" (tmp2)\
140                                : "0" (sum), "r" (w))
141#define ADD32           Asm("   ld [%4+ 0],%1;   ld [%4+ 4],%2;         \
142                                addcc  %0,%1,%0; addxcc %0,%2,%0;       \
143                                ld [%4+ 8],%1;   ld [%4+12],%2;         \
144                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
145                                ld [%4+16],%1;   ld [%4+20],%2;         \
146                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
147                                ld [%4+24],%1;   ld [%4+28],%2;         \
148                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
149                                addxcc %0,0,%0"                         \
150                                : "=r" (sum), "=&r" (tmp1), "=&r" (tmp2)\
151                                : "0" (sum), "r" (w))
152#define ADD16           Asm("   ld [%4+ 0],%1;   ld [%4+ 4],%2;         \
153                                addcc  %0,%1,%0; addxcc %0,%2,%0;       \
154                                ld [%4+ 8],%1;   ld [%4+12],%2;         \
155                                addxcc %0,%1,%0; addxcc %0,%2,%0;       \
156                                addxcc %0,0,%0"                         \
157                                : "=r" (sum), "=&r" (tmp1), "=&r" (tmp2)\
158                                : "0" (sum), "r" (w))
159#define ADD8            Asm("   ld [%4+ 0],%1;   ld [%4+ 4],%2;         \
160                                addcc  %0,%1,%0; addxcc %0,%2,%0;       \
161                                addxcc %0,0,%0"                         \
162                                : "=r" (sum), "=&r" (tmp1), "=&r" (tmp2)\
163                                : "0" (sum), "r" (w))
164#define ADD4            Asm("   ld [%3+ 0],%1;                          \
165                                addcc  %0,%1,%0;                        \
166                                addxcc %0,0,%0"                         \
167                                : "=r" (sum), "=&r" (tmp1)              \
168                                : "0" (sum), "r" (w))
169
170#define REDUCE          {sum = (sum & 0xffff) + (sum >> 16);}
171#define ADDCARRY        {if (sum > 0xffff) sum -= 0xffff;}
172#define ROL             {sum = sum << 8;}       /* depends on recent REDUCE */
173#define ADDBYTE         {ROL; sum += *w; byte_swapped ^= 1;}
174#define ADDSHORT        {sum += *(u_short *)w;}
175#define ADVANCE(n)      {w += n; mlen -= n;}
176
177static __inline__ int
178in_cksum_internal(struct mbuf *m, int off, int len, u_int sum)
179{
180        u_char *w;
181        int mlen = 0;
182        int byte_swapped = 0;
183
184        /*
185         * Declare two temporary registers for use by the asm code.  We
186         * allow the compiler to pick which specific machine registers to
187         * use, instead of hard-coding this in the asm code above.
188         */
189        u_int tmp1, tmp2;
190
191        for (; m && len; m = m->m_next) {
192                if (m->m_len == 0)
193                        continue;
194                w = mtod(m, u_char *) + off;
195                mlen = m->m_len - off;
196                off = 0;
197                if (len < mlen)
198                        mlen = len;
199                len -= mlen;
200
201                /*
202                 * Ensure that we're aligned on a word boundary here so
203                 * that we can do 32 bit operations below.
204                 */
205                if ((3 & (long)w) != 0) {
206                        REDUCE;
207                        if ((1 & (long)w) != 0 && mlen >= 1) {
208                                ADDBYTE;
209                                ADVANCE(1);
210                        }
211                        if ((2 & (long)w) != 0 && mlen >= 2) {
212                                ADDSHORT;
213                                ADVANCE(2);
214                        }
215                }
216
217                /*
218                 * Do as many 32 bit operations as possible using the
219                 * 64/32/16/8/4 macro's above, using as many as possible of
220                 * these.
221                 */
222                while (mlen >= 64) {
223                        ADD64;
224                        ADVANCE(64);
225                }
226                if (mlen >= 32) {
227                        ADD32;
228                        ADVANCE(32);
229                }
230                if (mlen >= 16) {
231                        ADD16;
232                        ADVANCE(16);
233                }
234                if (mlen >= 8) {
235                        ADD8;
236                        ADVANCE(8);
237                }
238                if (mlen >= 4) {
239                        ADD4;
240                        ADVANCE(4)
241                }
242                if (mlen == 0)
243                        continue;
244
245                REDUCE;
246                if (mlen >= 2) {
247                        ADDSHORT;
248                        ADVANCE(2);
249                }
250                if (mlen == 1) {
251                        ADDBYTE;
252                }
253        }
254        if (byte_swapped) {
255                REDUCE;
256                ROL;
257        }
258        REDUCE;
259        ADDCARRY;
260
261        return (0xffff ^ sum);
262}
263
264int
265in_cksum(struct mbuf *m, int len)
266{
267
268        return (in_cksum_internal(m, 0, len, 0));
269}
Note: See TracBrowser for help on using the repository browser.