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 | * ip/ip_packet.h: packet format common between ip4 & ip6 |
| 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 | #ifndef included_ip_packet_h |
| 41 | #define included_ip_packet_h |
| 42 | |
| 43 | #include <vppinfra/byte_order.h> |
| 44 | #include <vppinfra/error.h> |
| 45 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 46 | typedef enum ip_protocol |
| 47 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | #define ip_protocol(n,s) IP_PROTOCOL_##s = n, |
| 49 | #include "protocols.def" |
| 50 | #undef ip_protocol |
Neale Ranns | 3ec09e9 | 2020-02-24 13:32:30 +0000 | [diff] [blame] | 51 | } __clib_packed ip_protocol_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | |
| 53 | /* TCP/UDP ports. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 54 | typedef enum |
| 55 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | #define ip_port(s,n) IP_PORT_##s = n, |
| 57 | #include "ports.def" |
| 58 | #undef ip_port |
| 59 | } ip_port_t; |
| 60 | |
Kevin Paul Herbert | 8f9e7d4 | 2016-01-26 18:32:24 -0800 | [diff] [blame] | 61 | /* Classifies protocols into UDP, ICMP or other. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 62 | typedef enum |
| 63 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | IP_BUILTIN_PROTOCOL_UDP, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | IP_BUILTIN_PROTOCOL_ICMP, |
| 66 | IP_BUILTIN_PROTOCOL_UNKNOWN, |
| 67 | } ip_builtin_protocol_t; |
| 68 | |
| 69 | #define foreach_ip_builtin_multicast_group \ |
| 70 | _ (1, all_hosts_on_subnet) \ |
| 71 | _ (2, all_routers_on_subnet) \ |
| 72 | _ (4, dvmrp) \ |
| 73 | _ (5, ospf_all_routers) \ |
| 74 | _ (6, ospf_designated_routers) \ |
| 75 | _ (13, pim) \ |
| 76 | _ (18, vrrp) \ |
| 77 | _ (102, hsrp) \ |
| 78 | _ (22, igmp_v3) |
| 79 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 80 | typedef enum |
| 81 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | #define _(n,f) IP_MULTICAST_GROUP_##f = n, |
| 83 | foreach_ip_builtin_multicast_group |
| 84 | #undef _ |
| 85 | } ip_multicast_group_t; |
| 86 | |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * The set of RFC defined DSCP values. |
| 90 | */ |
| 91 | #define foreach_ip_dscp \ |
| 92 | _(0, CS0) \ |
| 93 | _(8, CS1) \ |
| 94 | _(10, AF11) \ |
| 95 | _(12, AF12) \ |
| 96 | _(14, AF13) \ |
| 97 | _(16, CS2) \ |
| 98 | _(18, AF21) \ |
| 99 | _(20, AF22) \ |
| 100 | _(22, AF23) \ |
| 101 | _(24, CS3) \ |
| 102 | _(26, AF31) \ |
| 103 | _(28, AF32) \ |
| 104 | _(30, AF33) \ |
| 105 | _(32, CS4) \ |
| 106 | _(34, AF41) \ |
| 107 | _(36, AF42) \ |
| 108 | _(38, AF43) \ |
| 109 | _(40, CS5) \ |
| 110 | _(46, EF) \ |
| 111 | _(48, CS6) \ |
| 112 | _(50, CS7) |
| 113 | |
| 114 | typedef enum ip_dscp_t_ |
| 115 | { |
| 116 | #define _(n,f) IP_DSCP_##f = n, |
| 117 | foreach_ip_dscp |
| 118 | #undef _ |
| 119 | } __clib_packed ip_dscp_t; |
| 120 | |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 121 | extern u8 *format_ip_dscp (u8 * s, va_list * va); |
| 122 | |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 123 | /** |
| 124 | * IP DSCP bit shift |
| 125 | * The ECN occupies the 2 least significant bits of the TC field |
| 126 | */ |
| 127 | #define IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT 2 |
| 128 | #define IP_PACKET_TC_FIELD_ECN_MASK 0x03 |
| 129 | |
| 130 | /** |
| 131 | * The set of RFC defined DSCP values. |
| 132 | */ |
| 133 | #define foreach_ip_ecn \ |
| 134 | _(0, NON_ECN) \ |
| 135 | _(1, ECT_0) \ |
| 136 | _(2, ECT_1) \ |
| 137 | _(3, CE) |
| 138 | |
| 139 | typedef enum ip_ecn_t_ |
| 140 | { |
| 141 | #define _(n,f) IP_ECN_##f = n, |
| 142 | foreach_ip_ecn |
| 143 | #undef _ |
| 144 | } __clib_packed ip_ecn_t; |
| 145 | |
| 146 | STATIC_ASSERT_SIZEOF (ip_ecn_t, 1); |
| 147 | |
| 148 | extern u8 *format_ip_ecn (u8 * s, va_list * va); |
| 149 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | /* IP checksum support. */ |
| 151 | |
Damjan Marion | 08bca80 | 2018-06-18 22:21:40 +0200 | [diff] [blame] | 152 | static_always_inline u16 |
| 153 | ip_csum (void *data, u16 n_left) |
| 154 | { |
| 155 | u32 sum; |
| 156 | #ifdef CLIB_HAVE_VEC256 |
| 157 | u16x16 v1, v2; |
| 158 | u32x8 zero = { 0 }; |
| 159 | u32x8 sum8 = { 0 }; |
| 160 | u32x4 sum4; |
| 161 | #endif |
| 162 | |
| 163 | /* if there is odd number of bytes, pad by zero and store in sum */ |
| 164 | sum = (n_left & 1) ? ((u8 *) data)[n_left - 1] << 8 : 0; |
| 165 | |
| 166 | /* we deal with words */ |
| 167 | n_left >>= 1; |
| 168 | |
| 169 | #ifdef CLIB_HAVE_VEC256 |
| 170 | while (n_left >= 32) |
| 171 | { |
| 172 | v1 = u16x16_load_unaligned (data); |
| 173 | v2 = u16x16_load_unaligned (data + 32); |
| 174 | |
| 175 | #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN |
| 176 | v1 = u16x16_byte_swap (v1); |
| 177 | v2 = u16x16_byte_swap (v2); |
| 178 | #endif |
Damjan Marion | 90d05bc | 2020-08-31 17:18:26 +0200 | [diff] [blame^] | 179 | sum8 += u32x8_from_u16x8 (u16x16_extract_lo (v1)); |
| 180 | sum8 += u32x8_from_u16x8 (u16x16_extract_hi (v1)); |
| 181 | sum8 += u32x8_from_u16x8 (u16x16_extract_lo (v2)); |
| 182 | sum8 += u32x8_from_u16x8 (u16x16_extract_hi (v2)); |
Damjan Marion | 08bca80 | 2018-06-18 22:21:40 +0200 | [diff] [blame] | 183 | n_left -= 32; |
| 184 | data += 64; |
| 185 | } |
| 186 | |
| 187 | if (n_left >= 16) |
| 188 | { |
| 189 | v1 = u16x16_load_unaligned (data); |
| 190 | #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN |
| 191 | v1 = u16x16_byte_swap (v1); |
| 192 | #endif |
| 193 | v1 = u16x16_byte_swap (u16x16_load_unaligned (data)); |
Damjan Marion | 90d05bc | 2020-08-31 17:18:26 +0200 | [diff] [blame^] | 194 | sum8 += u32x8_from_u16x8 (u16x16_extract_lo (v1)); |
| 195 | sum8 += u32x8_from_u16x8 (u16x16_extract_hi (v1)); |
Damjan Marion | 08bca80 | 2018-06-18 22:21:40 +0200 | [diff] [blame] | 196 | n_left -= 16; |
| 197 | data += 32; |
| 198 | } |
| 199 | |
| 200 | if (n_left) |
| 201 | { |
| 202 | v1 = u16x16_load_unaligned (data); |
| 203 | #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN |
| 204 | v1 = u16x16_byte_swap (v1); |
| 205 | #endif |
| 206 | v1 = u16x16_mask_last (v1, 16 - n_left); |
Damjan Marion | 90d05bc | 2020-08-31 17:18:26 +0200 | [diff] [blame^] | 207 | sum8 += u32x8_from_u16x8 (u16x16_extract_lo (v1)); |
| 208 | sum8 += u32x8_from_u16x8 (u16x16_extract_hi (v1)); |
Damjan Marion | 08bca80 | 2018-06-18 22:21:40 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | sum8 = u32x8_hadd (sum8, zero); |
| 212 | sum4 = u32x8_extract_lo (sum8) + u32x8_extract_hi (sum8); |
| 213 | sum = sum4[0] + sum4[1]; |
| 214 | |
| 215 | #else |
| 216 | /* scalar version */ |
| 217 | while (n_left >= 8) |
| 218 | { |
| 219 | sum += clib_net_to_host_u16 (*((u16 *) data + 0)); |
| 220 | sum += clib_net_to_host_u16 (*((u16 *) data + 1)); |
| 221 | sum += clib_net_to_host_u16 (*((u16 *) data + 2)); |
| 222 | sum += clib_net_to_host_u16 (*((u16 *) data + 3)); |
| 223 | sum += clib_net_to_host_u16 (*((u16 *) data + 4)); |
| 224 | sum += clib_net_to_host_u16 (*((u16 *) data + 5)); |
| 225 | sum += clib_net_to_host_u16 (*((u16 *) data + 6)); |
| 226 | sum += clib_net_to_host_u16 (*((u16 *) data + 7)); |
| 227 | n_left -= 8; |
| 228 | data += 16; |
| 229 | } |
| 230 | while (n_left) |
| 231 | { |
| 232 | sum += clib_net_to_host_u16 (*(u16 *) data); |
| 233 | n_left -= 1; |
| 234 | data += 2; |
| 235 | } |
| 236 | #endif |
| 237 | |
| 238 | sum = (sum & 0xffff) + (sum >> 16); |
| 239 | sum = (sum & 0xffff) + (sum >> 16); |
| 240 | return ~((u16) sum); |
| 241 | } |
| 242 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | /* Incremental checksum update. */ |
| 244 | typedef uword ip_csum_t; |
| 245 | |
| 246 | always_inline ip_csum_t |
| 247 | ip_csum_with_carry (ip_csum_t sum, ip_csum_t x) |
| 248 | { |
| 249 | ip_csum_t t = sum + x; |
| 250 | return t + (t < x); |
| 251 | } |
| 252 | |
| 253 | /* Update checksum changing field at even byte offset from x -> 0. */ |
| 254 | always_inline ip_csum_t |
| 255 | ip_csum_add_even (ip_csum_t c, ip_csum_t x) |
| 256 | { |
| 257 | ip_csum_t d; |
| 258 | |
| 259 | d = c - x; |
| 260 | |
| 261 | /* Fold in carry from high bit. */ |
| 262 | d -= d > c; |
| 263 | |
Hongjun Ni | 79c38af | 2018-06-15 05:32:23 +0800 | [diff] [blame] | 264 | ip_csum_t t = ip_csum_with_carry (d, x); |
| 265 | ASSERT ((t - c == 0) || (t - c == ~0)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | |
| 267 | return d; |
| 268 | } |
| 269 | |
| 270 | /* Update checksum changing field at even byte offset from 0 -> x. */ |
| 271 | always_inline ip_csum_t |
| 272 | ip_csum_sub_even (ip_csum_t c, ip_csum_t x) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 273 | { |
| 274 | return ip_csum_with_carry (c, x); |
| 275 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | |
| 277 | always_inline ip_csum_t |
| 278 | ip_csum_update_inline (ip_csum_t sum, ip_csum_t old, ip_csum_t new, |
| 279 | u32 field_byte_offset, u32 field_n_bytes) |
| 280 | { |
| 281 | /* For even 1-byte fields on big-endian and odd 1-byte fields on little endian |
| 282 | we need to shift byte into place for checksum. */ |
| 283 | if ((field_n_bytes % 2) |
| 284 | && (field_byte_offset % 2) == CLIB_ARCH_IS_LITTLE_ENDIAN) |
| 285 | { |
| 286 | old = old << 8; |
| 287 | new = new << 8; |
| 288 | } |
| 289 | sum = ip_csum_sub_even (sum, old); |
| 290 | sum = ip_csum_add_even (sum, new); |
| 291 | return sum; |
| 292 | } |
| 293 | |
| 294 | #define ip_csum_update(sum,old,new,type,field) \ |
| 295 | ip_csum_update_inline ((sum), (old), (new), \ |
| 296 | STRUCT_OFFSET_OF (type, field), \ |
| 297 | STRUCT_SIZE_OF (type, field)) |
| 298 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 299 | always_inline u16 |
| 300 | ip_csum_fold (ip_csum_t c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | { |
| 302 | /* Reduce to 16 bits. */ |
| 303 | #if uword_bits == 64 |
| 304 | c = (c & (ip_csum_t) 0xffffffff) + (c >> (ip_csum_t) 32); |
| 305 | c = (c & 0xffff) + (c >> 16); |
| 306 | #endif |
| 307 | |
| 308 | c = (c & 0xffff) + (c >> 16); |
| 309 | c = (c & 0xffff) + (c >> 16); |
| 310 | |
| 311 | return c; |
| 312 | } |
| 313 | |
Dave Barach | c6215d9 | 2018-06-14 18:05:30 -0400 | [diff] [blame] | 314 | extern ip_csum_t (*vnet_incremental_checksum_fp) (ip_csum_t, void *, uword); |
| 315 | |
BenoƮt Ganne | 5b1379b | 2019-10-07 15:06:52 +0200 | [diff] [blame] | 316 | /* Checksum routine. */ |
Dave Barach | c6215d9 | 2018-06-14 18:05:30 -0400 | [diff] [blame] | 317 | always_inline ip_csum_t |
| 318 | ip_incremental_checksum (ip_csum_t sum, void *_data, uword n_bytes) |
| 319 | { |
| 320 | return (*vnet_incremental_checksum_fp) (sum, _data, n_bytes); |
| 321 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | |
| 323 | always_inline u16 |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 324 | ip_csum_and_memcpy_fold (ip_csum_t sum, void *dst) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 325 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 326 | return ip_csum_fold (sum); |
| 327 | } |
| 328 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | #endif /* included_ip_packet_h */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * fd.io coding-style-patch-verification: ON |
| 333 | * |
| 334 | * Local Variables: |
| 335 | * eval: (c-set-style "gnu") |
| 336 | * End: |
| 337 | */ |