ip_packet.h revision 5b1379be
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
46typedef enum ip_protocol
47{
48#define ip_protocol(n,s) IP_PROTOCOL_##s = n,
49#include "protocols.def"
50#undef ip_protocol
51} ip_protocol_t;
52
53/* TCP/UDP ports. */
54typedef 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. */
62typedef enum
63{
64  IP_BUILTIN_PROTOCOL_UDP,
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
80typedef enum
81{
82#define _(n,f) IP_MULTICAST_GROUP_##f = n,
83  foreach_ip_builtin_multicast_group
84#undef _
85} ip_multicast_group_t;
86
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
114typedef 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
121STATIC_ASSERT_SIZEOF (ip_dscp_t, 1);
122
123extern u8 *format_ip_dscp (u8 * s, va_list * va);
124
125/* IP checksum support. */
126
127static_always_inline u16
128ip_csum (void *data, u16 n_left)
129{
130  u32 sum;
131#ifdef CLIB_HAVE_VEC256
132  u16x16 v1, v2;
133  u32x8 zero = { 0 };
134  u32x8 sum8 = { 0 };
135  u32x4 sum4;
136#endif
137
138  /* if there is odd number of bytes, pad by zero and store in sum */
139  sum = (n_left & 1) ? ((u8 *) data)[n_left - 1] << 8 : 0;
140
141  /* we deal with words */
142  n_left >>= 1;
143
144#ifdef CLIB_HAVE_VEC256
145  while (n_left >= 32)
146    {
147      v1 = u16x16_load_unaligned (data);
148      v2 = u16x16_load_unaligned (data + 32);
149
150#ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
151      v1 = u16x16_byte_swap (v1);
152      v2 = u16x16_byte_swap (v2);
153#endif
154      sum8 += u16x8_extend_to_u32x8 (u16x16_extract_lo (v1));
155      sum8 += u16x8_extend_to_u32x8 (u16x16_extract_hi (v1));
156      sum8 += u16x8_extend_to_u32x8 (u16x16_extract_lo (v2));
157      sum8 += u16x8_extend_to_u32x8 (u16x16_extract_hi (v2));
158      n_left -= 32;
159      data += 64;
160    }
161
162  if (n_left >= 16)
163    {
164      v1 = u16x16_load_unaligned (data);
165#ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
166      v1 = u16x16_byte_swap (v1);
167#endif
168      v1 = u16x16_byte_swap (u16x16_load_unaligned (data));
169      sum8 += u16x8_extend_to_u32x8 (u16x16_extract_lo (v1));
170      sum8 += u16x8_extend_to_u32x8 (u16x16_extract_hi (v1));
171      n_left -= 16;
172      data += 32;
173    }
174
175  if (n_left)
176    {
177      v1 = u16x16_load_unaligned (data);
178#ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
179      v1 = u16x16_byte_swap (v1);
180#endif
181      v1 = u16x16_mask_last (v1, 16 - n_left);
182      sum8 += u16x8_extend_to_u32x8 (u16x16_extract_lo (v1));
183      sum8 += u16x8_extend_to_u32x8 (u16x16_extract_hi (v1));
184    }
185
186  sum8 = u32x8_hadd (sum8, zero);
187  sum4 = u32x8_extract_lo (sum8) + u32x8_extract_hi (sum8);
188  sum = sum4[0] + sum4[1];
189
190#else
191  /* scalar version */
192  while (n_left >= 8)
193    {
194      sum += clib_net_to_host_u16 (*((u16 *) data + 0));
195      sum += clib_net_to_host_u16 (*((u16 *) data + 1));
196      sum += clib_net_to_host_u16 (*((u16 *) data + 2));
197      sum += clib_net_to_host_u16 (*((u16 *) data + 3));
198      sum += clib_net_to_host_u16 (*((u16 *) data + 4));
199      sum += clib_net_to_host_u16 (*((u16 *) data + 5));
200      sum += clib_net_to_host_u16 (*((u16 *) data + 6));
201      sum += clib_net_to_host_u16 (*((u16 *) data + 7));
202      n_left -= 8;
203      data += 16;
204    }
205  while (n_left)
206    {
207      sum += clib_net_to_host_u16 (*(u16 *) data);
208      n_left -= 1;
209      data += 2;
210    }
211#endif
212
213  sum = (sum & 0xffff) + (sum >> 16);
214  sum = (sum & 0xffff) + (sum >> 16);
215  return ~((u16) sum);
216}
217
218/* Incremental checksum update. */
219typedef uword ip_csum_t;
220
221always_inline ip_csum_t
222ip_csum_with_carry (ip_csum_t sum, ip_csum_t x)
223{
224  ip_csum_t t = sum + x;
225  return t + (t < x);
226}
227
228/* Update checksum changing field at even byte offset from x -> 0. */
229always_inline ip_csum_t
230ip_csum_add_even (ip_csum_t c, ip_csum_t x)
231{
232  ip_csum_t d;
233
234  d = c - x;
235
236  /* Fold in carry from high bit. */
237  d -= d > c;
238
239  ip_csum_t t = ip_csum_with_carry (d, x);
240  ASSERT ((t - c == 0) || (t - c == ~0));
241
242  return d;
243}
244
245/* Update checksum changing field at even byte offset from 0 -> x. */
246always_inline ip_csum_t
247ip_csum_sub_even (ip_csum_t c, ip_csum_t x)
248{
249  return ip_csum_with_carry (c, x);
250}
251
252always_inline ip_csum_t
253ip_csum_update_inline (ip_csum_t sum, ip_csum_t old, ip_csum_t new,
254		       u32 field_byte_offset, u32 field_n_bytes)
255{
256  /* For even 1-byte fields on big-endian and odd 1-byte fields on little endian
257     we need to shift byte into place for checksum. */
258  if ((field_n_bytes % 2)
259      && (field_byte_offset % 2) == CLIB_ARCH_IS_LITTLE_ENDIAN)
260    {
261      old = old << 8;
262      new = new << 8;
263    }
264  sum = ip_csum_sub_even (sum, old);
265  sum = ip_csum_add_even (sum, new);
266  return sum;
267}
268
269#define ip_csum_update(sum,old,new,type,field)			\
270  ip_csum_update_inline ((sum), (old), (new),			\
271			 STRUCT_OFFSET_OF (type, field),	\
272			 STRUCT_SIZE_OF (type, field))
273
274always_inline u16
275ip_csum_fold (ip_csum_t c)
276{
277  /* Reduce to 16 bits. */
278#if uword_bits == 64
279  c = (c & (ip_csum_t) 0xffffffff) + (c >> (ip_csum_t) 32);
280  c = (c & 0xffff) + (c >> 16);
281#endif
282
283  c = (c & 0xffff) + (c >> 16);
284  c = (c & 0xffff) + (c >> 16);
285
286  return c;
287}
288
289extern ip_csum_t (*vnet_incremental_checksum_fp) (ip_csum_t, void *, uword);
290
291/* Checksum routine. */
292always_inline ip_csum_t
293ip_incremental_checksum (ip_csum_t sum, void *_data, uword n_bytes)
294{
295  return (*vnet_incremental_checksum_fp) (sum, _data, n_bytes);
296}
297
298always_inline u16
299ip_csum_and_memcpy_fold (ip_csum_t sum, void *dst)
300{
301  return ip_csum_fold (sum);
302}
303
304#endif /* included_ip_packet_h */
305
306/*
307 * fd.io coding-style-patch-verification: ON
308 *
309 * Local Variables:
310 * eval: (c-set-style "gnu")
311 * End:
312 */
313