FD.io VPP  v19.08-27-gf4dcae4
Vector Packet Processing
ip.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.h: ip generic (4 or 6) main
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_main_h
41 #define included_ip_main_h
42 
43 #include <vppinfra/hash.h>
44 #include <vppinfra/heap.h> /* adjacency heap */
45 #include <vppinfra/ptclosure.h>
46 
47 #include <vnet/vnet.h>
48 
49 #include <vnet/ip/format.h>
50 #include <vnet/ip/ip_packet.h>
51 #include <vnet/ip/lookup.h>
52 
53 #include <vnet/tcp/tcp_packet.h>
54 #include <vnet/udp/udp_packet.h>
55 #include <vnet/ip/icmp46_packet.h>
56 
57 #include <vnet/ip/ip4.h>
58 #include <vnet/ip/ip4_error.h>
59 #include <vnet/ip/ip4_packet.h>
60 #include <vnet/ip/icmp4.h>
61 
62 #include <vnet/ip/ip6.h>
63 #include <vnet/ip/ip6_packet.h>
64 #include <vnet/ip/ip6_error.h>
65 #include <vnet/ip/icmp6.h>
67 
69 {
73 
75  va_list * args);
76 extern u8 *format_ip_address_family (u8 * s, va_list * args);
77 
78 #define FOR_EACH_IP_ADDRESS_FAMILY(_af) \
79  for (_af = AF_IP4; _af <= AF_IP6; _af++)
80 
81 #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index)
82 #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val))
83 
84 /* Per protocol info. */
85 typedef struct
86 {
87  /* Protocol name (also used as hash key). */
88  u8 *name;
89 
90  /* Protocol number. */
92 
93  /* Format function for this IP protocol. */
95 
96  /* Parser for header. */
98 
99  /* Parser for per-protocol matches. */
101 
102  /* Parser for packet generator edits for this protocol. */
105 
106 /* Per TCP/UDP port info. */
107 typedef struct
108 {
109  /* Port name (used as hash key). */
111 
112  /* UDP/TCP port number in network byte order. */
114 
115  /* Port specific format function. */
117 
118  /* Parser for packet generator edits for this protocol. */
121 
122 typedef struct
123 {
124  /* Per IP protocol info. */
126 
127  /* Protocol info index hashed by 8 bit IP protocol. */
129 
130  /* Hash table mapping IP protocol name (see protocols.def)
131  to protocol number. */
133 
134  /* Per TCP/UDP port info. */
136 
137  /* Hash table from network-byte-order port to port info index. */
139 
140  /* Hash table mapping TCP/UDP name to port info index. */
142 } ip_main_t;
143 
144 extern ip_main_t ip_main;
145 
147 
148 static inline ip_protocol_info_t *
150 {
151  uword *p;
152 
153  p = hash_get (im->protocol_info_by_protocol, protocol);
154  return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0;
155 }
156 
157 static inline tcp_udp_port_info_t *
159 {
160  uword *p;
161 
162  p = hash_get (im->port_info_by_port, port);
163  return p ? vec_elt_at_index (im->port_infos, p[0]) : 0;
164 }
165 
168  vlib_buffer_t * first_buffer,
169  u32 first_buffer_offset,
170  u32 n_bytes_to_checksum, ip_csum_t sum)
171 {
172  vlib_buffer_t *b = first_buffer;
173  u32 n_bytes_left = n_bytes_to_checksum;
174  ASSERT (b->current_length >= first_buffer_offset);
175  void *h;
176  u32 n;
177 
178  n = clib_min (n_bytes_left, b->current_length - first_buffer_offset);
179  h = vlib_buffer_get_current (b) + first_buffer_offset;
180  sum = ip_incremental_checksum (sum, h, n);
181  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
182  {
183  while (1)
184  {
185  n_bytes_left -= n;
186  if (n_bytes_left == 0)
187  break;
188  b = vlib_get_buffer (vm, b->next_buffer);
189  n = clib_min (n_bytes_left, b->current_length);
190  h = vlib_buffer_get_current (b);
191  sum = ip_incremental_checksum (sum, h, n);
192  }
193  }
194 
195  return sum;
196 }
197 
199 
202 
203 void ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api,
204  const u8 * name);
205 
206 void ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api);
207 
209  u32 table_id, u8 is_api);
210 
211 u8 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4);
212 u8 ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4);
213 u8 ip4_is_local_host (ip4_address_t * ip4_address);
214 u8 ip6_is_local_host (ip6_address_t * ip6_address);
215 u8 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4);
216 u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4);
217 void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4);
218 void ip_set (ip46_address_t * dst, void *src, u8 is_ip4);
220 void ip4_address_normalize (ip4_address_t * ip4, u8 preflen);
221 void ip6_address_normalize (ip6_address_t * ip6, u8 preflen);
222 void ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip);
225  ip4_address_t * res);
227  ip6_address_t * res);
228 void ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask);
230 
231 #endif /* included_ip_main_h */
232 
233 /*
234  * fd.io coding-style-patch-verification: ON
235  *
236  * Local Variables:
237  * eval: (c-set-style "gnu")
238  * End:
239  */
uword * protocol_info_by_name
Definition: ip.h:132
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
uword * protocol_info_by_protocol
Definition: ip.h:128
static ip_csum_t ip_incremental_checksum_buffer(vlib_main_t *vm, vlib_buffer_t *first_buffer, u32 first_buffer_offset, u32 n_bytes_to_checksum, ip_csum_t sum)
Definition: ip.h:167
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:233
u32 ip4_mask_to_preflen(ip4_address_t *mask)
Definition: ip.c:206
u8 ip6_is_local_host(ip6_address_t *ip6_address)
Definition: ip.c:45
void ip_table_create(fib_protocol_t fproto, u32 table_id, u8 is_api, const u8 *name)
Definition: ip_api.c:719
#define clib_min(x, y)
Definition: clib.h:295
enum ip_address_family_t_ ip_address_family_t
void ip6_address_normalize(ip6_address_t *ip6, u8 preflen)
Definition: ip.c:177
void * ip_interface_get_first_ip(u32 sw_if_index, u8 is_ip4)
Definition: ip.c:134
void ip_set(ip46_address_t *dst, void *src, u8 is_ip4)
Definition: ip.c:90
void ip6_preflen_to_mask(u8 pref_len, ip6_address_t *mask)
Definition: ip.c:223
Definitions for all things IP (v4|v6) unicast and multicast lookup related.
uword * port_info_by_name
Definition: ip.h:141
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
u8 ip_is_local(u32 fib_index, ip46_address_t *ip46_address, u8 is_ip4)
Checks that an ip is local to the requested fib.
Definition: ip.c:55
vl_api_address_t src
Definition: gre.api:51
u8 ip_interface_has_address(u32 sw_if_index, ip46_address_t *ip, u8 is_ip4)
Definition: ip.c:100
uword ip_csum_t
Definition: ip_packet.h:219
Definition: ip.h:122
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
unsigned char u8
Definition: types.h:56
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
ip_protocol_t protocol
Definition: ip.h:91
void ip6_prefix_max_address_host_order(ip6_address_t *ip, u8 plen, ip6_address_t *res)
Definition: ip.c:245
void ip4_address_normalize(ip4_address_t *ip4, u8 preflen)
Definition: ip.c:167
Definition: ip.h:71
unformat_function_t * unformat_pg_edit
Definition: ip.h:103
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define always_inline
Definition: clib.h:98
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
unsigned int u32
Definition: types.h:88
uword * port_info_by_port
Definition: ip.h:138
#define hash_get(h, key)
Definition: hash.h:249
uword unformat_ip_address_family(unformat_input_t *input, va_list *args)
ip_address_family_t_
Definition: ip.h:68
static ip_protocol_info_t * ip_get_protocol_info(ip_main_t *im, u32 protocol)
Definition: ip.h:149
format_function_t * format_header
Definition: ip.h:94
vl_api_ip_proto_t protocol
Definition: punt.api:39
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
void ip_copy(ip46_address_t *dst, ip46_address_t *src, u8 is_ip4)
Definition: ip.c:81
u8 ip4_is_local_host(ip4_address_t *ip4_address)
Definition: ip.c:39
u32 ip6_mask_to_preflen(ip6_address_t *mask)
Definition: ip.c:268
#define PREDICT_FALSE(x)
Definition: clib.h:111
enum ip_protocol ip_protocol_t
u16 port
Definition: punt.api:40
void ip_del_all_interface_addresses(vlib_main_t *vm, u32 sw_if_index)
Definition: ip46_cli.c:82
int ip_table_bind(fib_protocol_t fproto, u32 sw_if_index, u32 table_id, u8 is_api)
vl_api_address_t dst
Definition: gre.api:52
u8 name[64]
Definition: memclnt.api:152
vlib_node_registration_t ip4_inacl_node
(constructor) VLIB_REGISTER_NODE (ip4_inacl_node)
ip_protocol_info_t * protocol_infos
Definition: ip.h:125
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
vlib_node_registration_t ip6_inacl_node
(constructor) VLIB_REGISTER_NODE (ip6_inacl_node)
ip_main_t ip_main
Definition: ip_init.c:42
void ip_table_delete(fib_protocol_t fproto, u32 table_id, u8 is_api)
Definition: ip_api.c:597
static ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_packet.h:292
static tcp_udp_port_info_t * ip_get_tcp_udp_port_info(ip_main_t *im, u32 port)
Definition: ip.h:158
#define ASSERT(truth)
unformat_function_t * unformat_pg_edit
Definition: ip.h:119
unformat_function_t * unformat_header
Definition: ip.h:97
format_function_t * format_header
Definition: ip.h:116
unformat_function_t * unformat_match
Definition: ip.h:100
Definition: ip.h:70
struct _vlib_node_registration vlib_node_registration_t
void ip4_prefix_max_address_host_order(ip4_address_t *ip, u8 plen, ip4_address_t *res)
Definition: ip.c:214
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
void ip4_preflen_to_mask(u8 pref_len, ip4_address_t *ip)
Definition: ip.c:197
u8 ip_is_zero(ip46_address_t *ip46_address, u8 is_ip4)
Definition: ip.c:20
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
u8 * name
Definition: ip.h:88
tcp_udp_port_info_t * port_infos
Definition: ip.h:135
u8 ip_is_local_host(ip46_address_t *ip46_address, u8 is_ip4)
Definition: ip.c:29
u8 * format_ip_address_family(u8 *s, va_list *args)
Definition: ip.c:283
u32 table_id
Definition: fib_types.api:118
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85