FD.io VPP  v18.07.1-13-g909ba93
Vector Packet Processing
ip_packet.h
Go to the documentation of this file.
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 
46 typedef enum ip_protocol
47 {
48 #define ip_protocol(n,s) IP_PROTOCOL_##s = n,
49 #include "protocols.def"
50 #undef ip_protocol
52 
53 /* TCP/UDP ports. */
54 typedef enum
55 {
56 #define ip_port(s,n) IP_PORT_##s = n,
57 #include "ports.def"
58 #undef ip_port
59 } ip_port_t;
60 
61 /* Classifies protocols into UDP, ICMP or other. */
62 typedef enum
63 {
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 
80 typedef enum
81 {
82 #define _(n,f) IP_MULTICAST_GROUP_##f = n,
84 #undef _
86 
87 /* IP checksum support. */
88 
90 ip_csum (void *data, u16 n_left)
91 {
92  u32 sum;
93 #ifdef CLIB_HAVE_VEC256
94  u16x16 v1, v2;
95  u32x8 zero = { 0 };
96  u32x8 sum8 = { 0 };
97  u32x4 sum4;
98 #endif
99 
100  /* if there is odd number of bytes, pad by zero and store in sum */
101  sum = (n_left & 1) ? ((u8 *) data)[n_left - 1] << 8 : 0;
102 
103  /* we deal with words */
104  n_left >>= 1;
105 
106 #ifdef CLIB_HAVE_VEC256
107  while (n_left >= 32)
108  {
109  v1 = u16x16_load_unaligned (data);
110  v2 = u16x16_load_unaligned (data + 32);
111 
112 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
113  v1 = u16x16_byte_swap (v1);
114  v2 = u16x16_byte_swap (v2);
115 #endif
116  sum8 += u16x8_extend_to_u32x8 (u16x16_extract_lo (v1));
117  sum8 += u16x8_extend_to_u32x8 (u16x16_extract_hi (v1));
118  sum8 += u16x8_extend_to_u32x8 (u16x16_extract_lo (v2));
119  sum8 += u16x8_extend_to_u32x8 (u16x16_extract_hi (v2));
120  n_left -= 32;
121  data += 64;
122  }
123 
124  if (n_left >= 16)
125  {
126  v1 = u16x16_load_unaligned (data);
127 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
128  v1 = u16x16_byte_swap (v1);
129 #endif
130  v1 = u16x16_byte_swap (u16x16_load_unaligned (data));
131  sum8 += u16x8_extend_to_u32x8 (u16x16_extract_lo (v1));
132  sum8 += u16x8_extend_to_u32x8 (u16x16_extract_hi (v1));
133  n_left -= 16;
134  data += 32;
135  }
136 
137  if (n_left)
138  {
139  v1 = u16x16_load_unaligned (data);
140 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
141  v1 = u16x16_byte_swap (v1);
142 #endif
143  v1 = u16x16_mask_last (v1, 16 - n_left);
144  sum8 += u16x8_extend_to_u32x8 (u16x16_extract_lo (v1));
145  sum8 += u16x8_extend_to_u32x8 (u16x16_extract_hi (v1));
146  }
147 
148  sum8 = u32x8_hadd (sum8, zero);
149  sum4 = u32x8_extract_lo (sum8) + u32x8_extract_hi (sum8);
150  sum = sum4[0] + sum4[1];
151 
152 #else
153  /* scalar version */
154  while (n_left >= 8)
155  {
156  sum += clib_net_to_host_u16 (*((u16 *) data + 0));
157  sum += clib_net_to_host_u16 (*((u16 *) data + 1));
158  sum += clib_net_to_host_u16 (*((u16 *) data + 2));
159  sum += clib_net_to_host_u16 (*((u16 *) data + 3));
160  sum += clib_net_to_host_u16 (*((u16 *) data + 4));
161  sum += clib_net_to_host_u16 (*((u16 *) data + 5));
162  sum += clib_net_to_host_u16 (*((u16 *) data + 6));
163  sum += clib_net_to_host_u16 (*((u16 *) data + 7));
164  n_left -= 8;
165  data += 16;
166  }
167  while (n_left)
168  {
169  sum += clib_net_to_host_u16 (*(u16 *) data);
170  n_left -= 1;
171  data += 2;
172  }
173 #endif
174 
175  sum = (sum & 0xffff) + (sum >> 16);
176  sum = (sum & 0xffff) + (sum >> 16);
177  return ~((u16) sum);
178 }
179 
180 /* Incremental checksum update. */
181 typedef uword ip_csum_t;
182 
183 always_inline ip_csum_t
184 ip_csum_with_carry (ip_csum_t sum, ip_csum_t x)
185 {
186  ip_csum_t t = sum + x;
187  return t + (t < x);
188 }
189 
190 /* Update checksum changing field at even byte offset from x -> 0. */
191 always_inline ip_csum_t
192 ip_csum_add_even (ip_csum_t c, ip_csum_t x)
193 {
194  ip_csum_t d;
195 
196  d = c - x;
197 
198  /* Fold in carry from high bit. */
199  d -= d > c;
200 
201  ip_csum_t t = ip_csum_with_carry (d, x);
202  ASSERT ((t - c == 0) || (t - c == ~0));
203 
204  return d;
205 }
206 
207 /* Update checksum changing field at even byte offset from 0 -> x. */
208 always_inline ip_csum_t
209 ip_csum_sub_even (ip_csum_t c, ip_csum_t x)
210 {
211  return ip_csum_with_carry (c, x);
212 }
213 
214 always_inline ip_csum_t
215 ip_csum_update_inline (ip_csum_t sum, ip_csum_t old, ip_csum_t new,
216  u32 field_byte_offset, u32 field_n_bytes)
217 {
218  /* For even 1-byte fields on big-endian and odd 1-byte fields on little endian
219  we need to shift byte into place for checksum. */
220  if ((field_n_bytes % 2)
221  && (field_byte_offset % 2) == CLIB_ARCH_IS_LITTLE_ENDIAN)
222  {
223  old = old << 8;
224  new = new << 8;
225  }
226  sum = ip_csum_sub_even (sum, old);
227  sum = ip_csum_add_even (sum, new);
228  return sum;
229 }
230 
231 #define ip_csum_update(sum,old,new,type,field) \
232  ip_csum_update_inline ((sum), (old), (new), \
233  STRUCT_OFFSET_OF (type, field), \
234  STRUCT_SIZE_OF (type, field))
235 
237 ip_csum_fold (ip_csum_t c)
238 {
239  /* Reduce to 16 bits. */
240 #if uword_bits == 64
241  c = (c & (ip_csum_t) 0xffffffff) + (c >> (ip_csum_t) 32);
242  c = (c & 0xffff) + (c >> 16);
243 #endif
244 
245  c = (c & 0xffff) + (c >> 16);
246  c = (c & 0xffff) + (c >> 16);
247 
248  return c;
249 }
250 
252 
253 always_inline ip_csum_t
254 ip_incremental_checksum (ip_csum_t sum, void *_data, uword n_bytes)
255 {
256  return (*vnet_incremental_checksum_fp) (sum, _data, n_bytes);
257 }
258 
260 ip_csum_and_memcpy_fold (ip_csum_t sum, void *dst)
261 {
262  return ip_csum_fold (sum);
263 }
264 
265 /* Checksum routine. */
266 ip_csum_t ip_incremental_checksum (ip_csum_t sum, void *data, uword n_bytes);
267 
268 #endif /* included_ip_packet_h */
269 
270 /*
271  * fd.io coding-style-patch-verification: ON
272  *
273  * Local Variables:
274  * eval: (c-set-style "gnu")
275  * End:
276  */
#define CLIB_ARCH_IS_LITTLE_ENDIAN
Definition: byte_order.h:45
uword ip_csum_t
Definition: ip_packet.h:181
static ip_csum_t ip_csum_with_carry(ip_csum_t sum, ip_csum_t x)
Definition: ip_packet.h:184
static_always_inline u16x16 u16x16_mask_last(u16x16 v, u8 n_last)
Definition: vector_avx2.h:141
unsigned char u8
Definition: types.h:56
static u16 ip_csum_and_memcpy_fold(ip_csum_t sum, void *dst)
Definition: ip_packet.h:260
#define static_always_inline
Definition: clib.h:93
epu16_epi64 epu8_epi64 epu8_epi64 epi16_epi64 epi8_epi64 epi8_epi64 static_always_inline u16x16 u16x16_byte_swap(u16x16 v)
Definition: vector_avx2.h:125
#define always_inline
Definition: clib.h:92
unsigned long long u32x4
Definition: ixge.c:28
static ip_csum_t ip_csum_update_inline(ip_csum_t sum, ip_csum_t old, ip_csum_t new, u32 field_byte_offset, u32 field_n_bytes)
Definition: ip_packet.h:215
unsigned int u32
Definition: types.h:88
unsigned short u16
Definition: types.h:57
enum ip_protocol ip_protocol_t
static_always_inline u16 ip_csum(void *data, u16 n_left)
Definition: ip_packet.h:90
svmdb_client_t * c
static ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_packet.h:254
ip_protocol
Definition: ip_packet.h:46
ip_port_t
Definition: ip_packet.h:54
#define ASSERT(truth)
static_always_inline u32x8 u32x8_hadd(u32x8 v1, u32x8 v2)
Definition: vector_avx2.h:135
static ip_csum_t ip_csum_sub_even(ip_csum_t c, ip_csum_t x)
Definition: ip_packet.h:209
ip_csum_t(* vnet_incremental_checksum_fp)(ip_csum_t, void *, uword)
Definition: ip_checksum.c:145
u64 uword
Definition: types.h:112
#define foreach_ip_builtin_multicast_group
Definition: ip_packet.h:69
ip_multicast_group_t
Definition: ip_packet.h:80
epu16_epi64 u16x16
Definition: vector_avx2.h:112
ip_builtin_protocol_t
Definition: ip_packet.h:62
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:237
static ip_csum_t ip_csum_add_even(ip_csum_t c, ip_csum_t x)
Definition: ip_packet.h:192