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