source: rtems/cpukit/libnetworking/netinet/in_cksum.c @ 6520aef1

4.115
Last change on this file since 6520aef1 was 6520aef1, checked in by Joel Sherrill <joel.sherrill@…>, on 06/20/13 at 15:52:16

sparc in_cksum: Use sparc which is available in -ansi mode

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 1988, 1992, 1993
3 *      The Regents of the University of California.  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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
30 */
31
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36#include <sys/param.h>
37#include <sys/mbuf.h>
38
39/*
40 *  Try to use a CPU specific version, then punt to the portable C one.
41 */
42
43
44#if (defined(__GNUC__) && (defined(__arm__) && !defined(__thumb__)))
45
46/* This currently does not support Thumb assembly */
47#include "in_cksum_arm.h"
48
49#elif (defined(__GNUC__) && defined(__i386__))
50
51#include "in_cksum_i386.h"
52
53#elif (defined(__GNUC__) && (defined(__mc68000__) || defined(__m68k__)))
54
55#include "in_cksum_m68k.h"
56#elif (defined(__GNUC__) && defined(__PPC__))
57
58#include "in_cksum_powerpc.h"
59
60#elif (defined(__GNUC__) && defined(__nios2__))
61
62#include "in_cksum_nios2.h"
63
64#elif (defined(__GNUC__) && defined(__sparc__))
65
66#include "in_cksum_sparc.h"
67
68#else
69
70#include <stdio.h> /* for puts */
71
72/*
73 * Checksum routine for Internet Protocol family headers (Portable Version).
74 *
75 * This routine is very heavily used in the network
76 * code and should be modified for each CPU to be as fast as possible.
77 */
78
79#define ADDCARRY(x)  (x > 65535L ? x -= 65535L : x)
80#define REDUCE \
81  {l_util.l = sum; sum = l_util.s[0] + l_util.s[1];  ADDCARRY(sum);}
82
83int
84in_cksum(
85        struct mbuf *m,
86        uint32_t len )
87{
88        register u_short *w;
89        register int32_t sum = 0;
90        register int32_t mlen = 0;
91        int byte_swapped = 0;
92
93        union {
94                char    c[2];
95                u_short s;
96        } s_util;
97        union {
98                u_short s[2];
99                long    l;
100        } l_util;
101
102        for (;m && len; m = m->m_next) {
103                if (m->m_len == 0)
104                        continue;
105                w = mtod(m, u_short *);
106                if (mlen == -1) {
107                        /*
108                         * The first byte of this mbuf is the continuation
109                         * of a word spanning between this mbuf and the
110                         * last mbuf.
111                         *
112                         * s_util.c[0] is already saved when scanning previous
113                         * mbuf.
114                         */
115                        s_util.c[1] = *(char *)w;
116                        sum += s_util.s;
117                        w = (u_short *)((char *)w + 1);
118                        mlen = m->m_len - 1;
119                        len--;
120                } else
121                        mlen = m->m_len;
122                if (len < mlen)
123                        mlen = len;
124                len -= mlen;
125                /*
126                 * Force to even boundary.
127                 */
128                if ((1 & (intptr_t) w) && (mlen > 0)) {
129                        REDUCE;
130                        sum <<= 8;
131                        s_util.c[0] = *(u_char *)w;
132                        w = (u_short *)((char *)w + 1);
133                        mlen--;
134                        byte_swapped = 1;
135                }
136                /*
137                 * Unroll the loop to make overhead from
138                 * branches &c small.
139                 */
140                while ((mlen -= 32) >= 0) {
141                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
142                        sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
143                        sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
144                        sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
145                        w += 16;
146                }
147                mlen += 32;
148                while ((mlen -= 8) >= 0) {
149                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
150                        w += 4;
151                }
152                mlen += 8;
153                if (mlen == 0 && byte_swapped == 0)
154                        continue;
155                REDUCE;
156                while ((mlen -= 2) >= 0) {
157                        sum += *w++;
158                }
159                if (byte_swapped) {
160                        REDUCE;
161                        sum <<= 8;
162                        byte_swapped = 0;
163                        if (mlen == -1) {
164                                s_util.c[1] = *(char *)w;
165                                sum += s_util.s;
166                                mlen = 0;
167                        } else
168                                mlen = -1;
169                } else if (mlen == -1)
170                        s_util.c[0] = *(char *)w;
171        }
172        if (len)
173                puts("cksum: out of data");
174        if (mlen == -1) {
175                /* The last mbuf has odd # of bytes. Follow the
176                   standard (the odd byte may be shifted left by 8 bits
177                   or not as determined by endian-ness of the machine) */
178                s_util.c[1] = 0;
179                sum += s_util.s;
180        }
181        REDUCE;
182        return (~sum & 0xffff);
183}
184#endif
Note: See TracBrowser for help on using the repository browser.