2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * ip4/ip_checksum.c: ip/tcp/udp checksums
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/ip/ip.h>
43 ip_incremental_checksum (ip_csum_t sum, void *_data, uword n_bytes)
45 uword data = pointer_to_uword (_data);
51 /* Align data pointer to 64 bits. */
54 if (n_bytes >= sizeof (t) \
55 && sizeof (t) < sizeof (ip_csum_t) \
56 && (data % (2 * sizeof (t))) != 0) \
58 sum0 += * uword_to_pointer (data, t *); \
60 n_bytes -= sizeof (t); \
66 if (BITS (ip_csum_t) > 32)
72 ip_csum_t *d = uword_to_pointer (data, ip_csum_t *);
74 while (n_bytes >= 2 * sizeof (d[0]))
76 sum0 = ip_csum_with_carry (sum0, d[0]);
77 sum1 = ip_csum_with_carry (sum1, d[1]);
79 n_bytes -= 2 * sizeof (d[0]);
82 data = pointer_to_uword (d);
87 if (n_bytes >= sizeof (t) && sizeof (t) <= sizeof (ip_csum_t)) \
89 sum0 = ip_csum_with_carry (sum0, * uword_to_pointer (data, t *)); \
91 n_bytes -= sizeof (t); \
95 if (BITS (ip_csum_t) > 32)
103 /* Combine even and odd sums. */
104 sum0 = ip_csum_with_carry (sum0, sum1);
110 ip_csum_and_memcpy (ip_csum_t sum, void *dst, void *src, uword n_bytes)
113 ip_csum_t sum0 = sum, sum1;
116 if (n_left && (pointer_to_uword (dst) & sizeof (u8)))
121 val = ((u8 *) src)[0];
127 ip_csum_with_carry (sum0, val << (8 * CLIB_ARCH_IS_LITTLE_ENDIAN));
130 while ((n_left >= sizeof (u16))
131 && (pointer_to_uword (dst) & (sizeof (sum) - sizeof (u16))))
138 d16[0] = clib_mem_unaligned (&s16[0], u16);
140 sum0 = ip_csum_with_carry (sum0, d16[0]);
143 n_left -= sizeof (u16);
147 while (n_left >= 2 * sizeof (sum))
149 ip_csum_t dst0, dst1;
150 ip_csum_t *dst_even, *src_even;
154 dst0 = clib_mem_unaligned (&src_even[0], ip_csum_t);
155 dst1 = clib_mem_unaligned (&src_even[1], ip_csum_t);
160 dst += 2 * sizeof (dst_even[0]);
161 src += 2 * sizeof (dst_even[0]);
162 n_left -= 2 * sizeof (dst_even[0]);
164 sum0 = ip_csum_with_carry (sum0, dst0);
165 sum1 = ip_csum_with_carry (sum1, dst1);
168 sum0 = ip_csum_with_carry (sum0, sum1);
169 while (n_left >= 1 * sizeof (sum))
171 ip_csum_t dst0, *dst_even, *src_even;
176 dst0 = clib_mem_unaligned (&src_even[0], ip_csum_t);
180 dst += 1 * sizeof (sum);
181 src += 1 * sizeof (sum);
182 n_left -= 1 * sizeof (sum);
184 sum0 = ip_csum_with_carry (sum0, dst0);
187 while (n_left >= sizeof (u16))
189 u16 dst0, *dst_short, *src_short;
194 dst0 = clib_mem_unaligned (&src_short[0], u16);
198 sum0 = ip_csum_with_carry (sum0, dst_short[0]);
199 dst += 1 * sizeof (dst0);
200 src += 1 * sizeof (dst0);
201 n_left -= 1 * sizeof (dst0);
216 sum0 = ip_csum_with_carry (sum0, val << (8 * CLIB_ARCH_IS_BIG_ENDIAN));
223 * fd.io coding-style-patch-verification: ON
226 * eval: (c-set-style "gnu")