source: rtems/cpukit/libnetworking/netinet/in_cksum.c @ d8e44ec

4.115
Last change on this file since d8e44ec was d8e44ec, checked in by Jeffrey O. Hill <hill@…>, on 02/05/13 at 17:03:30

nios2: Add optimized IP checksum support

  • Property mode set to 100644
File size: 4.7 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#else
65
66#include <stdio.h> /* for puts */
67
68/*
69 * Checksum routine for Internet Protocol family headers (Portable Version).
70 *
71 * This routine is very heavily used in the network
72 * code and should be modified for each CPU to be as fast as possible.
73 */
74
75#define ADDCARRY(x)  (x > 65535L ? x -= 65535L : x)
76#define REDUCE \
77  {l_util.l = sum; sum = l_util.s[0] + l_util.s[1];  ADDCARRY(sum);}
78
79int
80in_cksum(
81        struct mbuf *m,
82        uint32_t len )
83{
84        register u_short *w;
85        register int32_t sum = 0;
86        register int32_t mlen = 0;
87        int byte_swapped = 0;
88
89        union {
90                char    c[2];
91                u_short s;
92        } s_util;
93        union {
94                u_short s[2];
95                long    l;
96        } l_util;
97
98        for (;m && len; m = m->m_next) {
99                if (m->m_len == 0)
100                        continue;
101                w = mtod(m, u_short *);
102                if (mlen == -1) {
103                        /*
104                         * The first byte of this mbuf is the continuation
105                         * of a word spanning between this mbuf and the
106                         * last mbuf.
107                         *
108                         * s_util.c[0] is already saved when scanning previous
109                         * mbuf.
110                         */
111                        s_util.c[1] = *(char *)w;
112                        sum += s_util.s;
113                        w = (u_short *)((char *)w + 1);
114                        mlen = m->m_len - 1;
115                        len--;
116                } else
117                        mlen = m->m_len;
118                if (len < mlen)
119                        mlen = len;
120                len -= mlen;
121                /*
122                 * Force to even boundary.
123                 */
124                if ((1 & (intptr_t) w) && (mlen > 0)) {
125                        REDUCE;
126                        sum <<= 8;
127                        s_util.c[0] = *(u_char *)w;
128                        w = (u_short *)((char *)w + 1);
129                        mlen--;
130                        byte_swapped = 1;
131                }
132                /*
133                 * Unroll the loop to make overhead from
134                 * branches &c small.
135                 */
136                while ((mlen -= 32) >= 0) {
137                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
138                        sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
139                        sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
140                        sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
141                        w += 16;
142                }
143                mlen += 32;
144                while ((mlen -= 8) >= 0) {
145                        sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
146                        w += 4;
147                }
148                mlen += 8;
149                if (mlen == 0 && byte_swapped == 0)
150                        continue;
151                REDUCE;
152                while ((mlen -= 2) >= 0) {
153                        sum += *w++;
154                }
155                if (byte_swapped) {
156                        REDUCE;
157                        sum <<= 8;
158                        byte_swapped = 0;
159                        if (mlen == -1) {
160                                s_util.c[1] = *(char *)w;
161                                sum += s_util.s;
162                                mlen = 0;
163                        } else
164                                mlen = -1;
165                } else if (mlen == -1)
166                        s_util.c[0] = *(char *)w;
167        }
168        if (len)
169                puts("cksum: out of data");
170        if (mlen == -1) {
171                /* The last mbuf has odd # of bytes. Follow the
172                   standard (the odd byte may be shifted left by 8 bits
173                   or not as determined by endian-ness of the machine) */
174                s_util.c[1] = 0;
175                sum += s_util.s;
176        }
177        REDUCE;
178        return (~sum & 0xffff);
179}
180#endif
Note: See TracBrowser for help on using the repository browser.