FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  * node.c: ipip packet processing
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or aipiped to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vnet/ipip/ipip.h>
20 #include <vnet/ip/ip6_packet.h>
21 #include <vnet/mpls/mpls.h>
22 #include <vnet/tunnel/tunnel_dp.h>
23 #include <vppinfra/sparse_vec.h>
24 
25 #define foreach_ipip_input_next \
26  _ (PUNT, "error-punt") \
27  _ (DROP, "error-drop") \
28  _ (IP4_INPUT, "ip4-input") \
29  _ (IP6_INPUT, "ip6-input") \
30  _ (MPLS_INPUT, "mpls-input")
31 
32 typedef enum
33 {
34 #define _(s, n) IPIP_INPUT_NEXT_##s,
36 #undef _
39 
40 typedef struct
41 {
44  ip46_address_t src;
45  ip46_address_t dst;
48 
49 static u8 *
50 format_ipip_rx_trace (u8 * s, va_list * args)
51 {
52  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54  ipip_rx_trace_t *t = va_arg (*args, ipip_rx_trace_t *);
55 
56  s = format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id,
57  clib_net_to_host_u16 (t->length), format_ip46_address, &t->src,
59  return s;
60 }
61 
65 {
67  u32 n_left_from, next_index, *from, *to_next, n_left_to_next;
68  u32 tunnel_sw_if_index = ~0;
70  u32 len;
71  vnet_interface_main_t *im = &gm->vnet_main->interface_main;
72 
74  n_left_from = from_frame->n_vectors;
75  next_index = node->cached_next_index;
76  while (n_left_from > 0)
77  {
78  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
79 
80  while (n_left_from > 0 && n_left_to_next > 0)
81  {
82  u32 bi0;
83  vlib_buffer_t *b0;
84  ip4_header_t *ip40;
85  ip6_header_t *ip60;
86  u32 next0 = IPIP_INPUT_NEXT_DROP;
87  u8 inner_protocol0;
88 
89  bi0 = to_next[0] = from[0];
90  from += 1;
91  n_left_from -= 1;
92  to_next += 1;
93  n_left_to_next -= 1;
94 
95  b0 = vlib_get_buffer (vm, bi0);
96 
97  ipip_tunnel_key_t key0 = {
98  .fib_index = vnet_buffer (b0)->ip.fib_index,
99  .mode = IPIP_MODE_P2P,
100  };
101 
102  if (is_ipv6)
103  {
104  ip60 = vlib_buffer_get_current (b0);
105  /* Check for outer fragmentation */
106  if (ip60->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
107  {
108  next0 = IPIP_INPUT_NEXT_DROP;
109  b0->error = node->errors[IPIP_ERROR_FRAGMENTED_PACKET];
110  goto drop;
111  }
112 
113  vlib_buffer_advance (b0, sizeof (*ip60));
114  ip_set (&key0.dst, &ip60->src_address, false);
115  ip_set (&key0.src, &ip60->dst_address, false);
116  inner_protocol0 = ip60->protocol;
117  key0.transport = IPIP_TRANSPORT_IP6;
118  }
119  else
120  {
121  ip40 = vlib_buffer_get_current (b0);
122  /* Check for outer fragmentation */
123  if (ip40->flags_and_fragment_offset &
124  clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS))
125  {
126  next0 = IPIP_INPUT_NEXT_DROP;
127  b0->error = node->errors[IPIP_ERROR_FRAGMENTED_PACKET];
128  goto drop;
129  }
130  vlib_buffer_advance (b0, sizeof (*ip40));
131  ip_set (&key0.dst, &ip40->src_address, true);
132  ip_set (&key0.src, &ip40->dst_address, true);
133  inner_protocol0 = ip40->protocol;
134  key0.transport = IPIP_TRANSPORT_IP4;
135  }
136 
137  /*
138  * Find tunnel. First a lookup for P2P tunnels, then a lookup
139  * for multipoint tunnels
140  */
141  ipip_tunnel_t *t0 = ipip_tunnel_db_find (&key0);
142  if (!t0)
143  {
144  ip46_address_reset (&key0.dst);
145  key0.mode = IPIP_MODE_6RD;
146  t0 = ipip_tunnel_db_find (&key0);
147  if (!t0)
148  {
149  next0 = IPIP_INPUT_NEXT_DROP;
150  b0->error = node->errors[IPIP_ERROR_NO_TUNNEL];
151  goto drop;
152  }
153  }
154  tunnel_sw_if_index = t0->sw_if_index;
155 
157  vnet_buffer (b0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
158 
159  if (inner_protocol0 == IP_PROTOCOL_IPV6)
160  {
161  next0 = IPIP_INPUT_NEXT_IP6_INPUT;
162 
163  if (is_ipv6)
164  tunnel_decap_fixup_6o6 (t0->flags, (ip60 + 1), ip60);
165  else
167  (ip6_header_t *) (ip40 + 1), ip40);
168  }
169  else if (inner_protocol0 == IP_PROTOCOL_IP_IN_IP)
170  {
171  next0 = IPIP_INPUT_NEXT_IP4_INPUT;
172 
173  if (is_ipv6)
175  (ip4_header_t *) (ip60 + 1), ip60);
176  else
177  tunnel_decap_fixup_4o4 (t0->flags, ip40 + 1, ip40);
178  }
179  else if (inner_protocol0 == IP_PROTOCOL_MPLS_IN_IP)
180  {
181  next0 = IPIP_INPUT_NEXT_MPLS_INPUT;
182 
183  if (is_ipv6)
185  t0->flags, (mpls_unicast_header_t *) (ip60 + 1), ip60);
186  else
188  t0->flags, (mpls_unicast_header_t *) ip40 + 1, ip40);
189  }
190 
191  if (!is_ipv6 && t0->mode == IPIP_MODE_6RD
192  && t0->sixrd.security_check)
193  {
194  ip6_header_t *inner_ip60 = vlib_buffer_get_current (b0);
195  if (sixrd_get_addr_net (t0, inner_ip60->src_address.as_u64[0])
196  != ip40->src_address.as_u32)
197  {
198  next0 = IPIP_INPUT_NEXT_DROP;
199  b0->error = node->errors[IPIP_ERROR_NO_TUNNEL];
200  goto drop;
201  }
202  }
203 
206  thread_index, tunnel_sw_if_index,
207  1 /* packets */ ,
208  len /* bytes */ );
209 
210  drop:
211  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
212  {
213  ipip_rx_trace_t *tr =
214  vlib_add_trace (vm, node, b0, sizeof (*tr));
215  tr->tunnel_id = tunnel_sw_if_index;
216  if (is_ipv6)
217  {
218  tr->length = ip60->payload_length;
219  tr->src.ip6.as_u64[0] = ip60->src_address.as_u64[0];
220  tr->src.ip6.as_u64[1] = ip60->src_address.as_u64[1];
221  tr->dst.ip6.as_u64[0] = ip60->dst_address.as_u64[0];
222  tr->dst.ip6.as_u64[1] = ip60->dst_address.as_u64[1];
223  }
224  else
225  {
226  tr->length = ip40->length;
227  tr->src.ip4.as_u32 = ip40->src_address.as_u32;
228  tr->dst.ip4.as_u32 = ip40->dst_address.as_u32;
229  }
230  }
231 
233  n_left_to_next, bi0, next0);
234  }
235 
236  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
237  }
239  !is_ipv6 ? ipip4_input_node.index :
240  ipip6_input_node.index, IPIP_ERROR_DECAP_PKTS,
241  from_frame->n_vectors);
242  return from_frame->n_vectors;
243 }
244 
247 {
248  return ipip_input (vm, node, from_frame, /* is_ip6 */ false);
249 }
250 
253 {
254  return ipip_input (vm, node, from_frame, /* is_ip6 */ true);
255 }
256 
257 static char *ipip_error_strings[] = {
258 #define _(sym,string) string,
260 #undef _
261 };
262 
263 /* *INDENT-OFF* */
265  .name = "ipip4-input",
266  /* Takes a vector of packets. */
267  .vector_size = sizeof(u32),
268  .n_errors = IPIP_N_ERROR,
269  .error_strings = ipip_error_strings,
270  .n_next_nodes = IPIP_INPUT_N_NEXT,
271  .next_nodes =
272  {
273 #define _(s, n) [IPIP_INPUT_NEXT_##s] = n,
275 #undef _
276  },
277  .format_trace = format_ipip_rx_trace,
278 };
279 
281  .name = "ipip6-input",
282  /* Takes a vector of packets. */
283  .vector_size = sizeof(u32),
284  .n_errors = IPIP_N_ERROR,
285  .error_strings = ipip_error_strings,
286  .n_next_nodes = IPIP_INPUT_N_NEXT,
287  .next_nodes =
288  {
289 #define _(s, n) [IPIP_INPUT_NEXT_##s] = n,
291 #undef _
292  },
293  .format_trace = format_ipip_rx_trace,
294 };
295 
296 /* *INDENT-ON* */
297 
298 /*
299  * fd.io coding-style-patch-verification: ON
300  *
301  * Local Variables:
302  * eval: (c-set-style "gnu")
303  * End:
304  */
vlib.h
IPIP_N_ERROR
@ IPIP_N_ERROR
Definition: ipip.h:41
im
vnet_interface_main_t * im
Definition: interface_output.c:395
ip_set
void ip_set(ip46_address_t *dst, void *src, u8 is_ip4)
Definition: ip.c:95
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
tunnel_decap_fixup_4o6
static_always_inline void tunnel_decap_fixup_4o6(tunnel_encap_decap_flags_t flags, ip4_header_t *inner, const ip6_header_t *outer)
Definition: tunnel_dp.h:188
is_ipv6
bool is_ipv6
Definition: dhcp.api:202
foreach_ipip_error
#define foreach_ipip_error
Definition: ipip.h:29
ipip4_input_node
vlib_node_registration_t ipip4_input_node
(constructor) VLIB_REGISTER_NODE (ipip4_input_node)
Definition: node.c:264
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
ip46_address_reset
static void ip46_address_reset(ip46_address_t *ip46)
Definition: ip46_address.h:74
ip6_header_t::protocol
u8 protocol
Definition: ip6_packet.h:304
ipip6_input_node
vlib_node_registration_t ipip6_input_node
(constructor) VLIB_REGISTER_NODE (ipip6_input_node)
Definition: node.c:280
format_ipip_rx_trace
static u8 * format_ipip_rx_trace(u8 *s, va_list *args)
Definition: node.c:50
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
ip4_address_t::as_u32
u32 as_u32
Definition: ip4_packet.h:57
vnet_interface_main_t
Definition: interface.h:989
ipip_tunnel_t::sw_if_index
u32 sw_if_index
Definition: ipip.h:86
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
mpls_unicast_header_t
Definition: packet.h:28
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
ipip_rx_trace_t::length
u32 length
Definition: node.c:43
from_frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * from_frame
Definition: esp_encrypt.c:1328
mpls.h
ipip_tunnel_t::security_check
bool security_check
Definition: ipip.h:99
vlib_frame_t
Definition: node.h:372
vlib_buffer_length_in_chain
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
ip4_header_t
Definition: ip4_packet.h:87
ipip_tunnel_t
A representation of a IPIP tunnel.
Definition: ipip.h:75
ip4_header_t::length
u16 length
Definition: ip4_packet.h:99
ipip_input
static uword ipip_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, bool is_ipv6)
Definition: node.c:63
ip6_packet.h
vlib_buffer_advance
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
IPIP_TRANSPORT_IP4
@ IPIP_TRANSPORT_IP4
Definition: ipip.h:49
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
len
u8 len
Definition: ip_types.api:103
ipip_tunnel_t::mode
ipip_mode_t mode
Definition: ipip.h:80
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
IPIP_MODE_6RD
@ IPIP_MODE_6RD
Definition: ipip.h:57
IPIP_INPUT_N_NEXT
@ IPIP_INPUT_N_NEXT
Definition: node.c:37
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
ipip_error_strings
static char * ipip_error_strings[]
Definition: node.c:257
ipip_input_next_t
ipip_input_next_t
Definition: node.c:32
ipip_main_t
Definition: ipip.h:104
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
tunnel_decap_fixup_6o4
static_always_inline void tunnel_decap_fixup_6o4(tunnel_encap_decap_flags_t flags, ip6_header_t *inner, const ip4_header_t *outer)
Definition: tunnel_dp.h:204
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
ipip_rx_trace_t::dst
ip46_address_t dst
Definition: node.c:45
uword
u64 uword
Definition: types.h:112
sparse_vec.h
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:213
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
sixrd_get_addr_net
static_always_inline u32 sixrd_get_addr_net(const ipip_tunnel_t *t, u64 dal)
Definition: ipip.h:131
ip6_header_t::dst_address
ip6_address_t dst_address
Definition: ip6_packet.h:310
ipip_rx_trace_t::src
ip46_address_t src
Definition: node.c:44
VNET_INTERFACE_COUNTER_RX
@ VNET_INTERFACE_COUNTER_RX
Definition: interface.h:914
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
vnet_interface_main_t::combined_sw_if_counters
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:1023
ip4_header_t::dst_address
ip4_address_t dst_address
Definition: ip4_packet.h:125
gm
#define gm
Definition: dlmalloc.c:1219
ipip_tunnel_t::sixrd
struct ipip_tunnel_t::@420 sixrd
tunnel_decap_fixup_6o6
static_always_inline void tunnel_decap_fixup_6o6(tunnel_encap_decap_flags_t flags, ip6_header_t *inner, const ip6_header_t *outer)
Definition: tunnel_dp.h:196
vlib_validate_buffer_enqueue_x1
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
ip4_header_t::src_address
ip4_address_t src_address
Definition: ip4_packet.h:125
format
description fragment has unexpected format
Definition: map.api:433
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
format_ip46_address
format_function_t format_ip46_address
Definition: ip46_address.h:50
u32
unsigned int u32
Definition: types.h:88
ipip_rx_trace_t::is_ipv6
u8 is_ipv6
Definition: node.c:46
ipip_tunnel_db_find
ipip_tunnel_t * ipip_tunnel_db_find(const ipip_tunnel_key_t *key)
Definition: ipip.c:551
IPIP_MODE_P2P
@ IPIP_MODE_P2P
Definition: ipip.h:55
tunnel_decap_fixup_mplso6
static_always_inline void tunnel_decap_fixup_mplso6(tunnel_encap_decap_flags_t flags, mpls_unicast_header_t *inner, const ip6_header_t *outer)
Definition: tunnel_dp.h:220
ip6_header_t
Definition: ip6_packet.h:294
ip4_header_t::flags_and_fragment_offset
u16 flags_and_fragment_offset
Definition: ip4_packet.h:106
IPIP_TRANSPORT_IP6
@ IPIP_TRANSPORT_IP6
Definition: ipip.h:50
ip6_header_t::src_address
ip6_address_t src_address
Definition: ip6_packet.h:310
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
u8
unsigned char u8
Definition: types.h:56
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
ipip_main
ipip_main_t ipip_main
Definition: ipip.c:31
foreach_ipip_input_next
#define foreach_ipip_input_next
Definition: node.c:25
ipip.h
tunnel_decap_fixup_mplso4
static_always_inline void tunnel_decap_fixup_mplso4(tunnel_encap_decap_flags_t flags, mpls_unicast_header_t *inner, const ip4_header_t *outer)
Definition: tunnel_dp.h:227
IP46_TYPE_ANY
@ IP46_TYPE_ANY
Definition: ip46_address.h:24
ipip_tunnel_t::flags
tunnel_encap_decap_flags_t flags
Definition: ipip.h:89
ip6_header_t::payload_length
u16 payload_length
Definition: ip6_packet.h:301
vlib_node_runtime_t
Definition: node.h:454
ipip_rx_trace_t::tunnel_id
u32 tunnel_id
Definition: node.c:42
ipip_rx_trace_t
Definition: node.c:40
from
from
Definition: nat44_ei_hairpinning.c:415
vlib_get_next_frame
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:395
IP4_HEADER_FLAG_MORE_FRAGMENTS
#define IP4_HEADER_FLAG_MORE_FRAGMENTS
Definition: ip4_packet.h:107
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
ip4_header_t::protocol
u8 protocol
Definition: ip4_packet.h:115
vlib_increment_combined_counter
vlib_increment_combined_counter(ccm, ti, sw_if_index, n_buffers, n_bytes)
tunnel_dp.h
tunnel_decap_fixup_4o4
static_always_inline void tunnel_decap_fixup_4o4(tunnel_encap_decap_flags_t flags, ip4_header_t *inner, const ip4_header_t *outer)
Definition: tunnel_dp.h:212
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169