OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [ecos-2.0/] [packages/] [net/] [tcpip/] [v2_0/] [src/] [sys/] [netinet/] [in_cksum.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1254 phoenix
//==========================================================================
2
//
3
//      sys/netinet/in_cksum.c
4
//
5
//     
6
//
7
//==========================================================================
8
//####BSDCOPYRIGHTBEGIN####
9
//
10
// -------------------------------------------
11
//
12
// Portions of this software may have been derived from OpenBSD or other sources,
13
// and are covered by the appropriate copyright disclaimers included herein.
14
//
15
// -------------------------------------------
16
//
17
//####BSDCOPYRIGHTEND####
18
//==========================================================================
19
//#####DESCRIPTIONBEGIN####
20
//
21
// Author(s):    gthomas
22
// Contributors: gthomas
23
// Date:         2000-01-10
24
// Purpose:      
25
// Description:  
26
//              
27
//
28
//####DESCRIPTIONEND####
29
//
30
//==========================================================================
31
 
32
 
33
/*      $OpenBSD: in_cksum.c,v 1.3 1997/02/24 14:06:35 niklas Exp $     */
34
/*      $NetBSD: in_cksum.c,v 1.11 1996/04/08 19:55:37 jonathan Exp $   */
35
 
36
/*
37
 * Copyright (c) 1988, 1992, 1993
38
 *      The Regents of the University of California.  All rights reserved.
39
 *
40
 * Redistribution and use in source and binary forms, with or without
41
 * modification, are permitted provided that the following conditions
42
 * are met:
43
 * 1. Redistributions of source code must retain the above copyright
44
 *    notice, this list of conditions and the following disclaimer.
45
 * 2. Redistributions in binary form must reproduce the above copyright
46
 *    notice, this list of conditions and the following disclaimer in the
47
 *    documentation and/or other materials provided with the distribution.
48
 * 3. All advertising materials mentioning features or use of this software
49
 *    must display the following acknowledgement:
50
 *      This product includes software developed by the University of
51
 *      California, Berkeley and its contributors.
52
 * 4. Neither the name of the University nor the names of its contributors
53
 *    may be used to endorse or promote products derived from this software
54
 *    without specific prior written permission.
55
 *
56
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66
 * SUCH DAMAGE.
67
 *
68
 *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
69
 */
70
 
71
#include <sys/param.h>
72
#include <sys/mbuf.h>
73
#ifndef __ECOS
74
#include <sys/systm.h>
75
#endif
76
#include <netinet/in.h>
77
 
78
struct net_stats stats_in_cksum;
79
 
80
/*
81
 * Checksum routine for Internet Protocol family headers (Portable Version).
82
 *
83
 * This routine is very heavily used in the network
84
 * code and should be modified for each CPU to be as fast as possible.
85
 */
86
 
87
#define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
88
#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
89
 
90
int
91
in_cksum(m, len)
92
        register struct mbuf *m;
93
        register int len;
94
{
95
        register u_int16_t *w;
96
        register int sum = 0;
97
        register int mlen = 0;
98
        int byte_swapped = 0;
99
 
100
        union {
101
                u_int8_t  c[2];
102
                u_int16_t s;
103
        } s_util;
104
        union {
105
                u_int16_t s[2];
106
                u_int32_t l;
107
        } l_util;
108
 
109
        START_STATS();
110
 
111
        for (;m && len; m = m->m_next) {
112
                if (m->m_len == 0)
113
                        continue;
114
                w = mtod(m, u_int16_t *);
115
                if (mlen == -1) {
116
                        /*
117
                         * The first byte of this mbuf is the continuation
118
                         * of a word spanning between this mbuf and the
119
                         * last mbuf.
120
                         *
121
                         * s_util.c[0] is already saved when scanning previous
122
                         * mbuf.
123
                         */
124
                        s_util.c[1] = *(u_int8_t *)w;
125
                        sum += s_util.s;
126
                        w = (u_int16_t *)((u_int8_t *)w + 1);
127
                        mlen = m->m_len - 1;
128
                        len--;
129
                } else
130
                        mlen = m->m_len;
131
                if (len < mlen)
132
                        mlen = len;
133
                len -= mlen;
134
                /*
135
                 * Force to even boundary.
136
                 */
137
                if ((1 & (long) w) && (mlen > 0)) {
138
                        REDUCE;
139
                        sum <<= 8;
140
                        s_util.c[0] = *(u_int8_t *)w;
141
                        w = (u_int16_t *)((int8_t *)w + 1);
142
                        mlen--;
143
                        byte_swapped = 1;
144
                }
145
                /*
146
                 * Unroll the loop to make overhead from
147
                 * branches &c small.
148
                 */
149
                while ((mlen -= 32) >= 0) {
150
                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
151
                        sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
152
                        sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
153
                        sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
154
                        w += 16;
155
                }
156
                mlen += 32;
157
                while ((mlen -= 8) >= 0) {
158
                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
159
                        w += 4;
160
                }
161
                mlen += 8;
162
                if (mlen == 0 && byte_swapped == 0)
163
                        continue;
164
                REDUCE;
165
                while ((mlen -= 2) >= 0) {
166
                        sum += *w++;
167
                }
168
                if (byte_swapped) {
169
                        REDUCE;
170
                        sum <<= 8;
171
                        byte_swapped = 0;
172
                        if (mlen == -1) {
173
                                s_util.c[1] = *(u_int8_t *)w;
174
                                sum += s_util.s;
175
                                mlen = 0;
176
                        } else
177
                                mlen = -1;
178
                } else if (mlen == -1)
179
                        s_util.c[0] = *(u_int8_t *)w;
180
        }
181
        if (len)
182
#ifdef __ECOS
183
                diag_printf("cksum: out of data\n");
184
#else
185
                printf("cksum: out of data\n");
186
#endif
187
        if (mlen == -1) {
188
                /* The last mbuf has odd # of bytes. Follow the
189
                   standard (the odd byte may be shifted left by 8 bits
190
                   or not as determined by endian-ness of the machine) */
191
                s_util.c[1] = 0;
192
                sum += s_util.s;
193
        }
194
        REDUCE;
195
 
196
        FINISH_STATS(stats_in_cksum);
197
 
198
        return (~sum & 0xffff);
199
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.