FD.io VPP  v16.06
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 
46 #include <vnet/vnet.h>
47 
48 #include <vnet/ip/format.h>
49 #include <vnet/ip/ip_packet.h>
50 #include <vnet/ip/lookup.h>
51 
52 #include <vnet/ip/tcp_packet.h>
53 #include <vnet/ip/udp_packet.h>
54 #include <vnet/ip/icmp46_packet.h>
55 
56 #include <vnet/ip/ip4.h>
57 #include <vnet/ip/ip4_error.h>
58 #include <vnet/ip/ip4_packet.h>
59 #include <vnet/ip/icmp4.h>
60 
61 #include <vnet/ip/ip6.h>
62 #include <vnet/ip/ip6_packet.h>
63 #include <vnet/ip/ip6_error.h>
64 #include <vnet/ip/icmp6.h>
65 
66 #if DPDK > 0
67 #include <vnet/devices/dpdk/dpdk.h>
68 #endif
69 
71 
72 /* Per protocol info. */
73 typedef struct {
74  /* Protocol name (also used as hash key). */
75  u8 * name;
76 
77  /* Protocol number. */
79 
80  /* Format function for this IP protocol. */
82 
83  /* Parser for header. */
85 
86  /* Parser for per-protocol matches. */
88 
89  /* Parser for packet generator edits for this protocol. */
92 
93 /* Per TCP/UDP port info. */
94 typedef struct {
95  /* Port name (used as hash key). */
96  u8 * name;
97 
98  /* UDP/TCP port number in network byte order. */
100 
101  /* Port specific format function. */
103 
104  /* Parser for packet generator edits for this protocol. */
107 
108 typedef struct {
109  /* Per IP protocol info. */
111 
112  /* Protocol info index hashed by 8 bit IP protocol. */
114 
115  /* Hash table mapping IP protocol name (see protocols.def)
116  to protocol number. */
118 
119  /* Per TCP/UDP port info. */
121 
122  /* Hash table from network-byte-order port to port info index. */
124 
125  /* Hash table mapping TCP/UDP name to port info index. */
127 } ip_main_t;
128 
129 extern ip_main_t ip_main;
130 
131 clib_error_t *
133 
134 static inline ip_protocol_info_t *
136 {
137  uword * p;
138 
139  p = hash_get (im->protocol_info_by_protocol, protocol);
140  return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0;
141 }
142 
143 static inline tcp_udp_port_info_t *
145 {
146  uword * p;
147 
148  p = hash_get (im->port_info_by_port, port);
149  return p ? vec_elt_at_index (im->port_infos, p[0]) : 0;
150 }
151 
154  u32 first_buffer_offset,
155  u32 n_bytes_to_checksum,
156  ip_csum_t sum)
157 #if DPDK > 0
158 {
159  u32 n_bytes_left = n_bytes_to_checksum;
160  struct rte_mbuf * mb = rte_mbuf_from_vlib_buffer(first_buffer);
161  u8 nb_segs = mb->nb_segs;
162  ASSERT(mb->data_len >= first_buffer_offset);
163  void * h;
164  u32 n;
165 
166  n = clib_min (n_bytes_left, mb->data_len);
167  h = vlib_buffer_get_current (first_buffer) + first_buffer_offset;
168  while (n_bytes_left)
169  {
170  sum = ip_incremental_checksum (sum, h, n);
171  n_bytes_left -= n;
172  nb_segs--;
173  mb = mb->next;
174  if ((nb_segs == 0) || (mb == 0))
175  break;
176 
177  n = clib_min (n_bytes_left, mb->data_len);
178  h = rte_ctrlmbuf_data(mb);
179  }
180 
181  ASSERT(n_bytes_left == 0);
182  ASSERT(nb_segs == 0);
183  return sum;
184 }
185 #else
186 {
187  vlib_buffer_t * b = first_buffer;
188  u32 n_bytes_left = n_bytes_to_checksum;
189  ASSERT (b->current_length >= first_buffer_offset);
190  void * h;
191  u32 n;
192 
193  n = clib_min (n_bytes_left, b->current_length);
194  h = vlib_buffer_get_current (b) + first_buffer_offset;
195  sum = ip_incremental_checksum (sum, h, n);
197  {
198  while (1)
199  {
200  n_bytes_left -= n;
201  if (n_bytes_left == 0)
202  break;
203  b = vlib_get_buffer (vm, b->next_buffer);
204  n = clib_min (n_bytes_left, b->current_length);
205  h = vlib_buffer_get_current (b);
206  sum = ip_incremental_checksum (sum, h, n);
207  }
208  }
209 
210  return sum;
211 }
212 #endif /* DPDK */
213 
214 void ip_del_all_interface_addresses (vlib_main_t *vm, u32 sw_if_index);
215 
218 
219 #endif /* included_ip_main_h */
uword * protocol_info_by_name
Definition: ip.h:117
uword * protocol_info_by_protocol
Definition: ip.h:113
#define clib_min(x, y)
Definition: clib.h:295
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:229
uword * port_info_by_name
Definition: ip.h:126
struct _vlib_node_registration vlib_node_registration_t
uword ip_csum_t
Definition: ip_packet.h:86
always_inline 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:153
Definition: ip.h:108
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
ip_protocol_t protocol
Definition: ip.h:78
unformat_function_t * unformat_pg_edit
Definition: ip.h:90
ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_checksum.c:43
#define always_inline
Definition: clib.h:84
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:93
uword * port_info_by_port
Definition: ip.h:123
#define hash_get(h, key)
Definition: hash.h:231
static ip_protocol_info_t * ip_get_protocol_info(ip_main_t *im, u32 protocol)
Definition: ip.h:135
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:81
format_function_t * format_header
Definition: ip.h:81
#define PREDICT_FALSE(x)
Definition: clib.h:97
enum ip_protocol ip_protocol_t
void ip_del_all_interface_addresses(vlib_main_t *vm, u32 sw_if_index)
Definition: ip46_cli.c:62
vlib_node_registration_t ip4_inacl_node
(constructor) VLIB_REGISTER_NODE (ip4_inacl_node)
Definition: ip_input_acl.c:358
ip_protocol_info_t * protocol_infos
Definition: ip.h:110
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)
Definition: ip_input_acl.c:381
ip_main_t ip_main
Definition: ip_init.c:42
static tcp_udp_port_info_t * ip_get_tcp_udp_port_info(ip_main_t *im, u32 port)
Definition: ip.h:144
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
unformat_function_t * unformat_pg_edit
Definition: ip.h:105
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:112
unformat_function_t * unformat_header
Definition: ip.h:84
format_function_t * format_header
Definition: ip.h:102
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
unformat_function_t * unformat_match
Definition: ip.h:87
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
u8 * name
Definition: ip.h:75
tcp_udp_port_info_t * port_infos
Definition: ip.h:120
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69