]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/cpu_in_cksum_gen.c
2 * Copyright (c) 2012-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
44 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
47 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
48 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
50 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
52 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
60 #include <machine/endian.h>
61 #include <sys/mcache.h>
63 #include <kern/debug.h>
64 #include <libkern/libkern.h>
65 #include <mach/boolean.h>
66 #include <pexpert/pexpert.h>
67 #define CKSUM_ERR(fmt, args...) kprintf(fmt, ## args)
69 #ifndef LIBSYSCALL_INTERFACE
70 #error "LIBSYSCALL_INTERFACE not defined"
71 #endif /* !LIBSYSCALL_INTERFACE */
77 #include <mach/boolean.h>
80 /* compile time assert */
82 #define _CASSERT(x) _Static_assert(x, "compile-time assertion failed")
83 #endif /* !_CASSERT */
86 #define VERIFY(EX) ((void)0)
90 #define CKSUM_ERR(fmt, args...) ((void)0)
91 #endif /* !CKSUM_ERR */
93 #define PREDICT_TRUE(x) __builtin_expect(!!((long)(x)), 1L)
94 #define PREDICT_FALSE(x) __builtin_expect(!!((long)(x)), 0L)
96 /* fake mbuf struct used only for calling os_cpu_in_cksum_mbuf() */
98 struct _mbuf
*_m_next
;
104 extern uint32_t os_cpu_in_cksum(const void *, uint32_t, uint32_t);
105 extern uint32_t os_cpu_in_cksum_mbuf(struct _mbuf
*, int, int, uint32_t);
108 os_cpu_in_cksum(const void *data
, uint32_t len
, uint32_t initial_sum
)
111 * If data is 4-bytes aligned, length is multiple of 4-bytes,
112 * and the amount to checksum is small, this would be quicker;
113 * this is suitable for IPv4 header.
115 if (IS_P2ALIGNED(data
, sizeof (uint32_t)) &&
116 len
<= 64 && (len
& 3) == 0) {
117 uint8_t *p
= __DECONST(uint8_t *, data
);
118 uint64_t sum
= initial_sum
;
120 if (PREDICT_TRUE(len
== 20)) { /* simple IPv4 header */
121 sum
+= *(uint32_t *)(void *)p
;
122 sum
+= *(uint32_t *)(void *)(p
+ 4);
123 sum
+= *(uint32_t *)(void *)(p
+ 8);
124 sum
+= *(uint32_t *)(void *)(p
+ 12);
125 sum
+= *(uint32_t *)(void *)(p
+ 16);
128 sum
+= *(uint32_t *)(void *)p
;
134 /* fold 64-bit to 16-bit (deferred carries) */
135 sum
= (sum
>> 32) + (sum
& 0xffffffff); /* 33-bit */
136 sum
= (sum
>> 16) + (sum
& 0xffff); /* 17-bit + carry */
137 sum
= (sum
>> 16) + (sum
& 0xffff); /* 16-bit + carry */
138 sum
= (sum
>> 16) + (sum
& 0xffff); /* final carry */
140 return (sum
& 0xffff);
144 * Otherwise, let os_cpu_in_cksum_mbuf() handle it; it only looks
145 * at 3 fields: {next,data,len}, and since it doesn't care about
146 * the authenticity of the mbuf, we use a fake one here. Make
147 * sure the offsets are as expected.
149 #if defined(__LP64__)
150 _CASSERT(offsetof(struct _mbuf
, _m_next
) == 0);
151 _CASSERT(offsetof(struct _mbuf
, _m_data
) == 16);
152 _CASSERT(offsetof(struct _mbuf
, _m_len
) == 24);
153 #else /* !__LP64__ */
154 _CASSERT(offsetof(struct _mbuf
, _m_next
) == 0);
155 _CASSERT(offsetof(struct _mbuf
, _m_data
) == 8);
156 _CASSERT(offsetof(struct _mbuf
, _m_len
) == 12);
157 #endif /* !__LP64__ */
159 _CASSERT(offsetof(struct _mbuf
, _m_next
) ==
160 offsetof(struct mbuf
, m_next
));
161 _CASSERT(offsetof(struct _mbuf
, _m_data
) ==
162 offsetof(struct mbuf
, m_data
));
163 _CASSERT(offsetof(struct _mbuf
, _m_len
) ==
164 offsetof(struct mbuf
, m_len
));
168 ._m_data
= __DECONST(uint8_t *, data
),
172 return (os_cpu_in_cksum_mbuf(&m
, len
, 0, initial_sum
));
175 #if defined(__i386__) || defined(__x86_64__)
178 * Checksum routine for Internet Protocol family headers (Portable Version).
180 * This routine is very heavily used in the network
181 * code and should be modified for each CPU to be as fast as possible.
183 * A discussion of different implementation techniques can be found in
186 * The default implementation for 32-bit architectures is using
187 * a 32-bit accumulator and operating on 16-bit operands.
189 * The default implementation for 64-bit architectures is using
190 * a 64-bit accumulator and operating on 32-bit operands.
192 * Both versions are unrolled to handle 32 Byte / 64 Byte fragments as core
193 * of the inner loop. After each iteration of the inner loop, a partial
194 * reduction is done to avoid carry in long packets.
197 #if !defined(__LP64__)
200 os_cpu_in_cksum_mbuf(struct _mbuf
*m
, int len
, int off
, uint32_t initial_sum
)
203 uint32_t sum
, partial
;
204 unsigned int final_acc
;
206 boolean_t needs_swap
, started_on_odd
;
212 started_on_odd
= FALSE
;
213 sum
= (initial_sum
>> 16) + (initial_sum
& 0xffff);
216 if (PREDICT_FALSE(m
== NULL
)) {
217 CKSUM_ERR("%s: out of data\n", __func__
);
218 return ((uint32_t)-1);
223 data
= m
->_m_data
+ off
;
224 goto post_initial_offset
;
232 for (; len
> 0; m
= m
->_m_next
) {
233 if (PREDICT_FALSE(m
== NULL
)) {
234 CKSUM_ERR("%s: out of data\n", __func__
);
235 return ((uint32_t)-1);
247 if ((uintptr_t)data
& 1) {
248 /* Align on word boundary */
249 started_on_odd
= !started_on_odd
;
250 #if BYTE_ORDER == LITTLE_ENDIAN
251 partial
= *data
<< 8;
258 needs_swap
= started_on_odd
;
260 __builtin_prefetch(data
+ 32);
261 partial
+= *(uint16_t *)(void *)data
;
262 partial
+= *(uint16_t *)(void *)(data
+ 2);
263 partial
+= *(uint16_t *)(void *)(data
+ 4);
264 partial
+= *(uint16_t *)(void *)(data
+ 6);
265 partial
+= *(uint16_t *)(void *)(data
+ 8);
266 partial
+= *(uint16_t *)(void *)(data
+ 10);
267 partial
+= *(uint16_t *)(void *)(data
+ 12);
268 partial
+= *(uint16_t *)(void *)(data
+ 14);
269 partial
+= *(uint16_t *)(void *)(data
+ 16);
270 partial
+= *(uint16_t *)(void *)(data
+ 18);
271 partial
+= *(uint16_t *)(void *)(data
+ 20);
272 partial
+= *(uint16_t *)(void *)(data
+ 22);
273 partial
+= *(uint16_t *)(void *)(data
+ 24);
274 partial
+= *(uint16_t *)(void *)(data
+ 26);
275 partial
+= *(uint16_t *)(void *)(data
+ 28);
276 partial
+= *(uint16_t *)(void *)(data
+ 30);
279 if (PREDICT_FALSE(partial
& 0xc0000000)) {
281 partial
= (partial
<< 8) +
283 sum
+= (partial
>> 16);
284 sum
+= (partial
& 0xffff);
289 partial
+= *(uint16_t *)(void *)data
;
290 partial
+= *(uint16_t *)(void *)(data
+ 2);
291 partial
+= *(uint16_t *)(void *)(data
+ 4);
292 partial
+= *(uint16_t *)(void *)(data
+ 6);
293 partial
+= *(uint16_t *)(void *)(data
+ 8);
294 partial
+= *(uint16_t *)(void *)(data
+ 10);
295 partial
+= *(uint16_t *)(void *)(data
+ 12);
296 partial
+= *(uint16_t *)(void *)(data
+ 14);
301 * mlen is not updated below as the remaining tests
302 * are using bit masks, which are not affected.
305 partial
+= *(uint16_t *)(void *)data
;
306 partial
+= *(uint16_t *)(void *)(data
+ 2);
307 partial
+= *(uint16_t *)(void *)(data
+ 4);
308 partial
+= *(uint16_t *)(void *)(data
+ 6);
312 partial
+= *(uint16_t *)(void *)data
;
313 partial
+= *(uint16_t *)(void *)(data
+ 2);
317 partial
+= *(uint16_t *)(void *)data
;
321 #if BYTE_ORDER == LITTLE_ENDIAN
324 partial
+= *data
<< 8;
326 started_on_odd
= !started_on_odd
;
330 partial
= (partial
<< 8) + (partial
>> 24);
331 sum
+= (partial
>> 16) + (partial
& 0xffff);
333 * Reduce sum to allow potential byte swap
334 * in the next iteration without carry.
336 sum
= (sum
>> 16) + (sum
& 0xffff);
338 final_acc
= ((sum
>> 16) & 0xffff) + (sum
& 0xffff);
339 final_acc
= (final_acc
>> 16) + (final_acc
& 0xffff);
340 return (final_acc
& 0xffff);
346 os_cpu_in_cksum_mbuf(struct _mbuf
*m
, int len
, int off
, uint32_t initial_sum
)
349 uint64_t sum
, partial
;
350 unsigned int final_acc
;
352 boolean_t needs_swap
, started_on_odd
;
358 started_on_odd
= FALSE
;
362 if (PREDICT_FALSE(m
== NULL
)) {
363 CKSUM_ERR("%s: out of data\n", __func__
);
364 return ((uint32_t)-1);
369 data
= m
->_m_data
+ off
;
370 goto post_initial_offset
;
378 for (; len
> 0; m
= m
->_m_next
) {
379 if (PREDICT_FALSE(m
== NULL
)) {
380 CKSUM_ERR("%s: out of data\n", __func__
);
381 return ((uint32_t)-1);
393 if ((uintptr_t)data
& 1) {
394 /* Align on word boundary */
395 started_on_odd
= !started_on_odd
;
396 #if BYTE_ORDER == LITTLE_ENDIAN
397 partial
= *data
<< 8;
404 needs_swap
= started_on_odd
;
405 if ((uintptr_t)data
& 2) {
408 partial
+= *(uint16_t *)(void *)data
;
413 __builtin_prefetch(data
+ 32);
414 __builtin_prefetch(data
+ 64);
415 partial
+= *(uint32_t *)(void *)data
;
416 partial
+= *(uint32_t *)(void *)(data
+ 4);
417 partial
+= *(uint32_t *)(void *)(data
+ 8);
418 partial
+= *(uint32_t *)(void *)(data
+ 12);
419 partial
+= *(uint32_t *)(void *)(data
+ 16);
420 partial
+= *(uint32_t *)(void *)(data
+ 20);
421 partial
+= *(uint32_t *)(void *)(data
+ 24);
422 partial
+= *(uint32_t *)(void *)(data
+ 28);
423 partial
+= *(uint32_t *)(void *)(data
+ 32);
424 partial
+= *(uint32_t *)(void *)(data
+ 36);
425 partial
+= *(uint32_t *)(void *)(data
+ 40);
426 partial
+= *(uint32_t *)(void *)(data
+ 44);
427 partial
+= *(uint32_t *)(void *)(data
+ 48);
428 partial
+= *(uint32_t *)(void *)(data
+ 52);
429 partial
+= *(uint32_t *)(void *)(data
+ 56);
430 partial
+= *(uint32_t *)(void *)(data
+ 60);
433 if (PREDICT_FALSE(partial
& (3ULL << 62))) {
435 partial
= (partial
<< 8) +
437 sum
+= (partial
>> 32);
438 sum
+= (partial
& 0xffffffff);
443 * mlen is not updated below as the remaining tests
444 * are using bit masks, which are not affected.
447 partial
+= *(uint32_t *)(void *)data
;
448 partial
+= *(uint32_t *)(void *)(data
+ 4);
449 partial
+= *(uint32_t *)(void *)(data
+ 8);
450 partial
+= *(uint32_t *)(void *)(data
+ 12);
451 partial
+= *(uint32_t *)(void *)(data
+ 16);
452 partial
+= *(uint32_t *)(void *)(data
+ 20);
453 partial
+= *(uint32_t *)(void *)(data
+ 24);
454 partial
+= *(uint32_t *)(void *)(data
+ 28);
458 partial
+= *(uint32_t *)(void *)data
;
459 partial
+= *(uint32_t *)(void *)(data
+ 4);
460 partial
+= *(uint32_t *)(void *)(data
+ 8);
461 partial
+= *(uint32_t *)(void *)(data
+ 12);
465 partial
+= *(uint32_t *)(void *)data
;
466 partial
+= *(uint32_t *)(void *)(data
+ 4);
470 partial
+= *(uint32_t *)(void *)data
;
474 partial
+= *(uint16_t *)(void *)data
;
479 #if BYTE_ORDER == LITTLE_ENDIAN
482 partial
+= *data
<< 8;
484 started_on_odd
= !started_on_odd
;
488 partial
= (partial
<< 8) + (partial
>> 56);
489 sum
+= (partial
>> 32) + (partial
& 0xffffffff);
491 * Reduce sum to allow potential byte swap
492 * in the next iteration without carry.
494 sum
= (sum
>> 32) + (sum
& 0xffffffff);
496 final_acc
= (sum
>> 48) + ((sum
>> 32) & 0xffff) +
497 ((sum
>> 16) & 0xffff) + (sum
& 0xffff);
498 final_acc
= (final_acc
>> 16) + (final_acc
& 0xffff);
499 final_acc
= (final_acc
>> 16) + (final_acc
& 0xffff);
500 return (final_acc
& 0xffff);
504 #endif /* __i386__ || __x86_64__ */