FD.io VPP  v16.06
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 #define ip_protocol(n,s) IP_PROTOCOL_##s = n,
48 #include "protocols.def"
49 #undef ip_protocol
51 
52 /* TCP/UDP ports. */
53 typedef enum {
54 #define ip_port(s,n) IP_PORT_##s = n,
55 #include "ports.def"
56 #undef ip_port
57 } ip_port_t;
58 
59 /* Classifies protocols into UDP, ICMP or other. */
60 typedef enum {
65 
66 #define foreach_ip_builtin_multicast_group \
67  _ (1, all_hosts_on_subnet) \
68  _ (2, all_routers_on_subnet) \
69  _ (4, dvmrp) \
70  _ (5, ospf_all_routers) \
71  _ (6, ospf_designated_routers) \
72  _ (13, pim) \
73  _ (18, vrrp) \
74  _ (102, hsrp) \
75  _ (22, igmp_v3)
76 
77 typedef enum {
78 #define _(n,f) IP_MULTICAST_GROUP_##f = n,
80 #undef _
82 
83 /* IP checksum support. */
84 
85 /* Incremental checksum update. */
86 typedef uword ip_csum_t;
87 
90 {
91  ip_csum_t t = sum + x;
92  return t + (t < x);
93 }
94 
95 /* Update checksum changing field at even byte offset from x -> 0. */
98 {
99  ip_csum_t d;
100 
101  d = c - x;
102 
103  /* Fold in carry from high bit. */
104  d -= d > c;
105 
106  ASSERT (ip_csum_with_carry (d, x) == c);
107 
108  return d;
109 }
110 
111 /* Update checksum changing field at even byte offset from 0 -> x. */
114 { return ip_csum_with_carry (c, x); }
115 
118  u32 field_byte_offset, u32 field_n_bytes)
119 {
120  /* For even 1-byte fields on big-endian and odd 1-byte fields on little endian
121  we need to shift byte into place for checksum. */
122  if ((field_n_bytes % 2)
123  && (field_byte_offset % 2) == CLIB_ARCH_IS_LITTLE_ENDIAN)
124  {
125  old = old << 8;
126  new = new << 8;
127  }
128  sum = ip_csum_sub_even (sum, old);
129  sum = ip_csum_add_even (sum, new);
130  return sum;
131 }
132 
133 #define ip_csum_update(sum,old,new,type,field) \
134  ip_csum_update_inline ((sum), (old), (new), \
135  STRUCT_OFFSET_OF (type, field), \
136  STRUCT_SIZE_OF (type, field))
137 
139 {
140  /* Reduce to 16 bits. */
141 #if uword_bits == 64
142  c = (c & (ip_csum_t) 0xffffffff) + (c >> (ip_csum_t) 32);
143  c = (c & 0xffff) + (c >> 16);
144 #endif
145 
146  c = (c & 0xffff) + (c >> 16);
147  c = (c & 0xffff) + (c >> 16);
148 
149  return c;
150 }
151 
152 /* Copy data and checksum at the same time. */
153 ip_csum_t ip_csum_and_memcpy (ip_csum_t sum, void * dst, void * src, uword n_bytes);
154 
157 {
158  return ip_csum_fold (sum);
159 }
160 
161 /* Checksum routine. */
162 ip_csum_t ip_incremental_checksum (ip_csum_t sum, void * data, uword n_bytes);
163 
164 #endif /* included_ip_packet_h */
always_inline 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:117
#define CLIB_ARCH_IS_LITTLE_ENDIAN
Definition: byte_order.h:45
uword ip_csum_t
Definition: ip_packet.h:86
#define always_inline
Definition: clib.h:84
always_inline u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:138
enum ip_protocol ip_protocol_t
always_inline ip_csum_t ip_csum_sub_even(ip_csum_t c, ip_csum_t x)
Definition: ip_packet.h:113
ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *data, uword n_bytes)
Definition: ip_checksum.c:43
ip_protocol
Definition: ip_packet.h:46
always_inline u16 ip_csum_and_memcpy_fold(ip_csum_t sum, void *dst)
Definition: ip_packet.h:156
always_inline ip_csum_t ip_csum_add_even(ip_csum_t c, ip_csum_t x)
Definition: ip_packet.h:97
ip_port_t
Definition: ip_packet.h:53
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
ip_csum_t ip_csum_and_memcpy(ip_csum_t sum, void *dst, void *src, uword n_bytes)
Definition: ip_checksum.c:110
always_inline ip_csum_t ip_csum_with_carry(ip_csum_t sum, ip_csum_t x)
Definition: ip_packet.h:89
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
#define foreach_ip_builtin_multicast_group
Definition: ip_packet.h:66
ip_multicast_group_t
Definition: ip_packet.h:77
ip_builtin_protocol_t
Definition: ip_packet.h:60