Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | /* |
| 16 | * ip4/ip_checksum.c: ip/tcp/udp checksums |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 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: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 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. |
| 38 | */ |
| 39 | |
| 40 | #include <vnet/ip/ip.h> |
| 41 | |
| 42 | ip_csum_t |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 43 | ip_incremental_checksum (ip_csum_t sum, void *_data, uword n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | { |
| 45 | uword data = pointer_to_uword (_data); |
| 46 | ip_csum_t sum0, sum1; |
| 47 | |
| 48 | sum0 = 0; |
| 49 | sum1 = sum; |
| 50 | |
| 51 | /* Align data pointer to 64 bits. */ |
| 52 | #define _(t) \ |
| 53 | do { \ |
| 54 | if (n_bytes >= sizeof (t) \ |
| 55 | && sizeof (t) < sizeof (ip_csum_t) \ |
| 56 | && (data % (2 * sizeof (t))) != 0) \ |
| 57 | { \ |
| 58 | sum0 += * uword_to_pointer (data, t *); \ |
| 59 | data += sizeof (t); \ |
| 60 | n_bytes -= sizeof (t); \ |
| 61 | } \ |
| 62 | } while (0) |
| 63 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 64 | _(u8); |
| 65 | _(u16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | if (BITS (ip_csum_t) > 32) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 67 | _(u32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
| 69 | #undef _ |
| 70 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 71 | { |
| 72 | ip_csum_t *d = uword_to_pointer (data, ip_csum_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 74 | while (n_bytes >= 2 * sizeof (d[0])) |
| 75 | { |
| 76 | sum0 = ip_csum_with_carry (sum0, d[0]); |
| 77 | sum1 = ip_csum_with_carry (sum1, d[1]); |
| 78 | d += 2; |
| 79 | n_bytes -= 2 * sizeof (d[0]); |
| 80 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 82 | data = pointer_to_uword (d); |
| 83 | } |
| 84 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | #define _(t) \ |
| 86 | do { \ |
| 87 | if (n_bytes >= sizeof (t) && sizeof (t) <= sizeof (ip_csum_t)) \ |
| 88 | { \ |
| 89 | sum0 = ip_csum_with_carry (sum0, * uword_to_pointer (data, t *)); \ |
| 90 | data += sizeof (t); \ |
| 91 | n_bytes -= sizeof (t); \ |
| 92 | } \ |
| 93 | } while (0) |
| 94 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 95 | if (BITS (ip_csum_t) > 32) |
| 96 | _(u64); |
| 97 | _(u32); |
| 98 | _(u16); |
| 99 | _(u8); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
| 101 | #undef _ |
| 102 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 103 | /* Combine even and odd sums. */ |
| 104 | sum0 = ip_csum_with_carry (sum0, sum1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 106 | return sum0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | ip_csum_t |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 110 | ip_csum_and_memcpy (ip_csum_t sum, void *dst, void *src, uword n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | { |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 112 | uword n_left; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | ip_csum_t sum0 = sum, sum1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | n_left = n_bytes; |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 115 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 116 | if (n_left && (pointer_to_uword (dst) & sizeof (u8))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 118 | u8 *d8, val; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 120 | d8 = dst; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 121 | val = ((u8 *) src)[0]; |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 122 | d8[0] = val; |
| 123 | dst += 1; |
| 124 | src += 1; |
| 125 | n_left -= 1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 126 | sum0 = |
| 127 | ip_csum_with_carry (sum0, val << (8 * CLIB_ARCH_IS_LITTLE_ENDIAN)); |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 128 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 130 | while ((n_left >= sizeof (u16)) |
| 131 | && (pointer_to_uword (dst) & (sizeof (sum) - sizeof (u16)))) |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 132 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 133 | u16 *d16, *s16; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 135 | d16 = dst; |
| 136 | s16 = src; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 137 | |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 138 | d16[0] = clib_mem_unaligned (&s16[0], u16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 140 | sum0 = ip_csum_with_carry (sum0, d16[0]); |
| 141 | dst += sizeof (u16); |
| 142 | src += sizeof (u16); |
| 143 | n_left -= sizeof (u16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | sum1 = 0; |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 147 | while (n_left >= 2 * sizeof (sum)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | { |
| 149 | ip_csum_t dst0, dst1; |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 150 | ip_csum_t *dst_even, *src_even; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 152 | dst_even = dst; |
| 153 | src_even = src; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | dst0 = clib_mem_unaligned (&src_even[0], ip_csum_t); |
| 155 | dst1 = clib_mem_unaligned (&src_even[1], ip_csum_t); |
| 156 | |
| 157 | dst_even[0] = dst0; |
| 158 | dst_even[1] = dst1; |
| 159 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 160 | dst += 2 * sizeof (dst_even[0]); |
| 161 | src += 2 * sizeof (dst_even[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | n_left -= 2 * sizeof (dst_even[0]); |
| 163 | |
| 164 | sum0 = ip_csum_with_carry (sum0, dst0); |
| 165 | sum1 = ip_csum_with_carry (sum1, dst1); |
| 166 | } |
| 167 | |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 168 | sum0 = ip_csum_with_carry (sum0, sum1); |
| 169 | while (n_left >= 1 * sizeof (sum)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | { |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 171 | ip_csum_t dst0, *dst_even, *src_even; |
| 172 | |
| 173 | dst_even = dst; |
| 174 | src_even = src; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
| 176 | dst0 = clib_mem_unaligned (&src_even[0], ip_csum_t); |
| 177 | |
| 178 | dst_even[0] = dst0; |
| 179 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 180 | dst += 1 * sizeof (sum); |
| 181 | src += 1 * sizeof (sum); |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 182 | n_left -= 1 * sizeof (sum); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | |
| 184 | sum0 = ip_csum_with_carry (sum0, dst0); |
| 185 | } |
| 186 | |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 187 | while (n_left >= sizeof (u16)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | { |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 189 | u16 dst0, *dst_short, *src_short; |
| 190 | |
| 191 | dst_short = dst; |
| 192 | src_short = src; |
| 193 | |
| 194 | dst0 = clib_mem_unaligned (&src_short[0], u16); |
| 195 | |
| 196 | dst_short[0] = dst0; |
| 197 | |
| 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); |
| 202 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 205 | if (n_left == 1) |
| 206 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 207 | u8 *d8, *s8, val; |
Kevin Paul Herbert | cf121e3 | 2015-12-08 19:53:45 -0800 | [diff] [blame] | 208 | |
| 209 | d8 = dst; |
| 210 | s8 = src; |
| 211 | |
| 212 | d8[0] = val = s8[0]; |
| 213 | d8 += 1; |
| 214 | s8 += 1; |
| 215 | n_left -= 1; |
| 216 | sum0 = ip_csum_with_carry (sum0, val << (8 * CLIB_ARCH_IS_BIG_ENDIAN)); |
| 217 | } |
| 218 | |
| 219 | return sum0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * fd.io coding-style-patch-verification: ON |
| 224 | * |
| 225 | * Local Variables: |
| 226 | * eval: (c-set-style "gnu") |
| 227 | * End: |
| 228 | */ |