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 * ip/ip_packet.h: packet format common between ip4 & ip6
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 #ifndef included_ip_packet_h
41 #define included_ip_packet_h
43 #include <vppinfra/byte_order.h>
44 #include <vppinfra/error.h>
45 #include <vppinfra/format.h>
47 typedef enum ip_protocol
49 #define ip_protocol(n,s) IP_PROTOCOL_##s = n,
50 #include "protocols.def"
52 } __clib_packed ip_protocol_t;
57 #define ip_port(s,n) IP_PORT_##s = n,
62 /* Classifies protocols into UDP, ICMP or other. */
65 IP_BUILTIN_PROTOCOL_UDP,
66 IP_BUILTIN_PROTOCOL_ICMP,
67 IP_BUILTIN_PROTOCOL_UNKNOWN,
68 } ip_builtin_protocol_t;
70 #define foreach_ip_builtin_multicast_group \
71 _ (1, all_hosts_on_subnet) \
72 _ (2, all_routers_on_subnet) \
74 _ (5, ospf_all_routers) \
75 _ (6, ospf_designated_routers) \
83 #define _(n,f) IP_MULTICAST_GROUP_##f = n,
84 foreach_ip_builtin_multicast_group
86 } ip_multicast_group_t;
90 * The set of RFC defined DSCP values.
92 #define foreach_ip_dscp \
115 typedef enum ip_dscp_t_
117 #define _(n,f) IP_DSCP_##f = n,
120 } __clib_packed ip_dscp_t;
122 extern u8 *format_ip_dscp (u8 * s, va_list * va);
123 unformat_function_t unformat_ip_dscp;
127 * The ECN occupies the 2 least significant bits of the TC field
129 #define IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT 2
130 #define IP_PACKET_TC_FIELD_ECN_MASK 0x03
133 * The set of RFC defined DSCP values.
135 #define foreach_ip_ecn \
141 typedef enum ip_ecn_t_
143 #define _(n,f) IP_ECN_##f = n,
146 } __clib_packed ip_ecn_t;
148 STATIC_ASSERT_SIZEOF (ip_ecn_t, 1);
150 extern u8 *format_ip_ecn (u8 * s, va_list * va);
152 /* IP checksum support. */
154 static_always_inline u16
155 ip_csum (void *data, u16 n_left)
158 #ifdef CLIB_HAVE_VEC256
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;
168 /* we deal with words */
171 #ifdef CLIB_HAVE_VEC256
174 v1 = u16x16_load_unaligned (data);
175 v2 = u16x16_load_unaligned (data + 32);
177 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
178 v1 = u16x16_byte_swap (v1);
179 v2 = u16x16_byte_swap (v2);
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));
191 v1 = u16x16_load_unaligned (data);
192 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
193 v1 = u16x16_byte_swap (v1);
195 sum8 += u32x8_from_u16x8 (u16x16_extract_lo (v1));
196 sum8 += u32x8_from_u16x8 (u16x16_extract_hi (v1));
203 v1 = u16x16_load_unaligned (data);
204 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
205 v1 = u16x16_byte_swap (v1);
207 v1 = u16x16_mask_last (v1, 16 - n_left);
208 sum8 += u32x8_from_u16x8 (u16x16_extract_lo (v1));
209 sum8 += u32x8_from_u16x8 (u16x16_extract_hi (v1));
212 sum8 = u32x8_hadd (sum8, zero);
213 sum4 = u32x8_extract_lo (sum8) + u32x8_extract_hi (sum8);
214 sum += sum4[0] + sum4[1];
220 sum += clib_net_to_host_u16 (*((u16 *) data + 0));
221 sum += clib_net_to_host_u16 (*((u16 *) data + 1));
222 sum += clib_net_to_host_u16 (*((u16 *) data + 2));
223 sum += clib_net_to_host_u16 (*((u16 *) data + 3));
224 sum += clib_net_to_host_u16 (*((u16 *) data + 4));
225 sum += clib_net_to_host_u16 (*((u16 *) data + 5));
226 sum += clib_net_to_host_u16 (*((u16 *) data + 6));
227 sum += clib_net_to_host_u16 (*((u16 *) data + 7));
233 sum += clib_net_to_host_u16 (*(u16 *) data);
239 sum = (sum & 0xffff) + (sum >> 16);
240 sum = (sum & 0xffff) + (sum >> 16);
244 /* Incremental checksum update. */
245 typedef uword ip_csum_t;
247 always_inline ip_csum_t
248 ip_csum_with_carry (ip_csum_t sum, ip_csum_t x)
250 ip_csum_t t = sum + x;
254 /* Update checksum changing field at even byte offset from x -> 0. */
255 always_inline ip_csum_t
256 ip_csum_add_even (ip_csum_t c, ip_csum_t x)
262 /* Fold in carry from high bit. */
265 ip_csum_t t = ip_csum_with_carry (d, x);
266 ASSERT ((t - c == 0) || (t - c == ~0));
271 /* Update checksum changing field at even byte offset from 0 -> x. */
272 always_inline ip_csum_t
273 ip_csum_sub_even (ip_csum_t c, ip_csum_t x)
275 return ip_csum_with_carry (c, x);
278 always_inline ip_csum_t
279 ip_csum_update_inline (ip_csum_t sum, ip_csum_t old, ip_csum_t new,
280 u32 field_byte_offset, u32 field_n_bytes)
282 /* For even 1-byte fields on big-endian and odd 1-byte fields on little endian
283 we need to shift byte into place for checksum. */
284 if ((field_n_bytes % 2)
285 && (field_byte_offset % 2) == CLIB_ARCH_IS_LITTLE_ENDIAN)
290 sum = ip_csum_sub_even (sum, old);
291 sum = ip_csum_add_even (sum, new);
295 #define ip_csum_update(sum,old,new,type,field) \
296 ip_csum_update_inline ((sum), (old), (new), \
297 STRUCT_OFFSET_OF (type, field), \
298 STRUCT_SIZE_OF (type, field))
301 ip_csum_fold (ip_csum_t c)
303 /* Reduce to 16 bits. */
305 c = (c & (ip_csum_t) 0xffffffff) + (c >> (ip_csum_t) 32);
306 c = (c & 0xffff) + (c >> 16);
309 c = (c & 0xffff) + (c >> 16);
310 c = (c & 0xffff) + (c >> 16);
315 extern ip_csum_t (*vnet_incremental_checksum_fp) (ip_csum_t, void *, uword);
317 /* Checksum routine. */
318 always_inline ip_csum_t
319 ip_incremental_checksum (ip_csum_t sum, void *_data, uword n_bytes)
321 return (*vnet_incremental_checksum_fp) (sum, _data, n_bytes);
325 ip_csum_and_memcpy_fold (ip_csum_t sum, void *dst)
327 return ip_csum_fold (sum);
330 #endif /* included_ip_packet_h */
333 * fd.io coding-style-patch-verification: ON
336 * eval: (c-set-style "gnu")