]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/in_cksum.c
Optionally bind ktls threads to NUMA domains
[FreeBSD/FreeBSD.git] / sys / powerpc / powerpc / in_cksum.c
1 /* $FreeBSD$ */
2 /* $NetBSD: in_cksum.c,v 1.7 1997/09/02 13:18:15 thorpej Exp $ */
3
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (c) 1988, 1992, 1993
8  *      The Regents of the University of California.  All rights reserved.
9  * Copyright (c) 1996
10  *      Matt Thomas <matt@3am-software.com>
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the University of
23  *      California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
41  */
42
43 #include <sys/cdefs.h>                  /* RCS ID & Copyright macro defns */
44
45 #include <sys/endian.h>
46 #include <sys/param.h>
47 #include <sys/mbuf.h>
48 #include <sys/systm.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <machine/in_cksum.h>
53
54 /*
55  * Checksum routine for Internet Protocol family headers
56  *    (Portable Alpha version).
57  *
58  * This routine is very heavily used in the network
59  * code and should be modified for each CPU to be as fast as possible.
60  */
61
62 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
63 #define REDUCE32                                                          \
64     {                                                                     \
65         q_util.q = sum;                                                   \
66         sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3];      \
67     }
68 #define REDUCE16                                                          \
69     {                                                                     \
70         q_util.q = sum;                                                   \
71         l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
72         sum = l_util.s[0] + l_util.s[1];                                  \
73         ADDCARRY(sum);                                                    \
74     }
75
76 static const u_int32_t in_masks[] = {
77 #if _BYTE_ORDER == _LITTLE_ENDIAN
78         /*0 bytes*/ /*1 byte*/  /*2 bytes*/ /*3 bytes*/
79         0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
80         0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
81         0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
82         0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
83 #else
84         /*0 bytes*/ /*1 byte*/  /*2 bytes*/ /*3 bytes*/
85         0x00000000, 0xFF000000, 0xFFFF0000, 0xFFFFFF00, /* offset 0 */
86         0x00000000, 0x00FF0000, 0x00FFFF00, 0x00FFFFFF, /* offset 1 */
87         0x00000000, 0x0000FF00, 0x0000FFFF, 0x0000FFFF, /* offset 2 */
88         0x00000000, 0x000000FF, 0x000000FF, 0x000000FF, /* offset 3 */
89 #endif
90 };
91
92 union l_util {
93         u_int16_t s[2];
94         u_int32_t l;
95 };
96 union q_util {
97         u_int16_t s[4];
98         u_int32_t l[2];
99         u_int64_t q;
100 };
101
102 static u_int64_t
103 in_cksumdata(const void *buf, int len)
104 {
105         const u_int32_t *lw = (const u_int32_t *) buf;
106         u_int64_t sum = 0;
107         u_int64_t prefilled;
108         int offset;
109         union q_util q_util;
110
111         if ((3 & (long) lw) == 0 && len == 20) {
112              sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
113              REDUCE32;
114              return sum;
115         }
116
117         if ((offset = 3 & (long) lw) != 0) {
118                 const u_int32_t *masks = in_masks + (offset << 2);
119                 lw = (u_int32_t *) (((long) lw) - offset);
120                 sum = *lw++ & masks[len >= 3 ? 3 : len];
121                 len -= 4 - offset;
122                 if (len <= 0) {
123                         REDUCE32;
124                         return sum;
125                 }
126         }
127 #if 0
128         /*
129          * Force to cache line boundary.
130          */
131         offset = 32 - (0x1f & (long) lw);
132         if (offset < 32 && len > offset) {
133                 len -= offset;
134                 if (4 & offset) {
135                         sum += (u_int64_t) lw[0];
136                         lw += 1;
137                 }
138                 if (8 & offset) {
139                         sum += (u_int64_t) lw[0] + lw[1];
140                         lw += 2;
141                 }
142                 if (16 & offset) {
143                         sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
144                         lw += 4;
145                 }
146         }
147 #endif
148         /*
149          * access prefilling to start load of next cache line.
150          * then add current cache line
151          * save result of prefilling for loop iteration.
152          */
153         prefilled = lw[0];
154         while ((len -= 32) >= 4) {
155                 u_int64_t prefilling = lw[8];
156                 sum += prefilled + lw[1] + lw[2] + lw[3]
157                         + lw[4] + lw[5] + lw[6] + lw[7];
158                 lw += 8;
159                 prefilled = prefilling;
160         }
161         if (len >= 0) {
162                 sum += prefilled + lw[1] + lw[2] + lw[3]
163                         + lw[4] + lw[5] + lw[6] + lw[7];
164                 lw += 8;
165         } else {
166                 len += 32;
167         }
168         while ((len -= 16) >= 0) {
169                 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
170                 lw += 4;
171         }
172         len += 16;
173         while ((len -= 4) >= 0) {
174                 sum += (u_int64_t) *lw++;
175         }
176         len += 4;
177         if (len > 0)
178                 sum += (u_int64_t) (in_masks[len] & *lw);
179         REDUCE32;
180         return sum;
181 }
182
183 u_short
184 in_addword(u_short a, u_short b)
185 {
186         u_int64_t sum = a + b;
187
188         ADDCARRY(sum);
189         return (sum);
190 }
191
192 u_short
193 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
194 {
195         u_int64_t sum;
196         union q_util q_util;
197         union l_util l_util;
198                     
199         sum = (u_int64_t) a + b + c;
200         REDUCE16;
201         return (sum);
202 }
203
204 u_short
205 in_cksum_skip(struct mbuf *m, int len, int skip)
206 {
207         u_int64_t sum = 0;
208         int mlen = 0;
209         int clen = 0;
210         caddr_t addr;
211         union q_util q_util;
212         union l_util l_util;
213
214         len -= skip;
215         for (; skip && m; m = m->m_next) {
216                 if (m->m_len > skip) {
217                         mlen = m->m_len - skip;
218                         addr = mtod(m, caddr_t) + skip;
219                         goto skip_start;
220                 } else {
221                         skip -= m->m_len;
222                 }
223         }
224
225         for (; m && len; m = m->m_next) {
226                 if (m->m_len == 0)
227                         continue;
228                 mlen = m->m_len;
229                 addr = mtod(m, caddr_t);
230 skip_start:
231                 if (len < mlen)
232                         mlen = len;
233
234                 if ((clen ^ (long) addr) & 1)
235                     sum += in_cksumdata(addr, mlen) << 8;
236                 else
237                     sum += in_cksumdata(addr, mlen);
238
239                 clen += mlen;
240                 len -= mlen;
241         }
242         REDUCE16;
243         return (~sum & 0xffff);
244 }
245
246 u_int in_cksum_hdr(const struct ip *ip)
247 {
248     u_int64_t sum = in_cksumdata(ip, sizeof(struct ip));
249     union q_util q_util;
250     union l_util l_util;
251     REDUCE16;
252     return (~sum & 0xffff);
253 }