FD.io VPP  v18.07-rc0-415-g6c78436
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/pg/pg.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 
31 typedef enum
32 {
33 #define _(s, n) IPIP_INPUT_NEXT_##s,
35 #undef _
38 
39 typedef struct
40 {
43  ip46_address_t src;
44  ip46_address_t dst;
47 
48 u8 *
49 format_ipip_rx_trace (u8 * s, va_list * args)
50 {
51  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53  ipip_rx_trace_t *t = va_arg (*args, ipip_rx_trace_t *);
54 
55  s = format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id,
56  clib_net_to_host_u16 (t->length), format_ip46_address, &t->src,
58  return s;
59 }
60 
63  vlib_frame_t * from_frame, bool is_ipv6)
64 {
65  ipip_main_t *gm = &ipip_main;
66  u32 n_left_from, next_index, *from, *to_next, n_left_to_next;
67  u32 tunnel_sw_if_index = ~0;
68  u32 thread_index = vlib_get_thread_index ();
69  u32 len;
71 
72  from = vlib_frame_vector_args (from_frame);
73  n_left_from = from_frame->n_vectors;
74  next_index = node->cached_next_index;
75  while (n_left_from > 0)
76  {
77  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
78 
79  while (n_left_from > 0 && n_left_to_next > 0)
80  {
81  u32 bi0;
82  vlib_buffer_t *b0;
83  ip4_header_t *ip40;
84  ip6_header_t *ip60;
85  u32 next0 = IPIP_INPUT_NEXT_DROP;
86  ip46_address_t src0 = ip46_address_initializer, dst0 =
88  ipip_transport_t transport0;
89  u8 inner_protocol0;
90 
91  bi0 = to_next[0] = from[0];
92  from += 1;
93  n_left_from -= 1;
94  to_next += 1;
95  n_left_to_next -= 1;
96 
97  b0 = vlib_get_buffer (vm, bi0);
98 
99  if (is_ipv6)
100  {
101  ip60 = vlib_buffer_get_current (b0);
102  vlib_buffer_advance (b0, sizeof (*ip60));
103  ip_set (&src0, &ip60->src_address, false);
104  ip_set (&dst0, &ip60->dst_address, false);
105  inner_protocol0 = ip60->protocol;
106  transport0 = IPIP_TRANSPORT_IP6;
107  }
108  else
109  {
110  ip40 = vlib_buffer_get_current (b0);
111  vlib_buffer_advance (b0, sizeof (*ip40));
112  ip_set (&src0, &ip40->src_address, true);
113  ip_set (&dst0, &ip40->dst_address, true);
114  inner_protocol0 = ip40->protocol;
115  transport0 = IPIP_TRANSPORT_IP4;
116  }
117 
118  /*
119  * Find tunnel. First a lookup for P2P tunnels, then a lookup
120  * for multipoint tunnels
121  */
122  ipip_tunnel_key_t key0 = {.transport = transport0,
123  .fib_index = vnet_buffer (b0)->ip.fib_index,
124  .src = dst0,
125  .dst = src0
126  };
127  ipip_tunnel_t *t0 = ipip_tunnel_db_find (&key0);
128  if (!t0)
129  {
130  ip46_address_reset (&key0.dst);
131  t0 = ipip_tunnel_db_find (&key0);
132  if (!t0)
133  {
134  next0 = IPIP_INPUT_NEXT_DROP;
135  b0->error = node->errors[IPIP_ERROR_NO_TUNNEL];
136  goto drop;
137  }
138  }
139  tunnel_sw_if_index = t0->sw_if_index;
140 
141  len = vlib_buffer_length_in_chain (vm, b0);
142  vnet_buffer (b0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
143 
144  if (inner_protocol0 == IP_PROTOCOL_IPV6)
145  next0 = IPIP_INPUT_NEXT_IP6_INPUT;
146  else if (inner_protocol0 == IP_PROTOCOL_IP_IN_IP)
147  next0 = IPIP_INPUT_NEXT_IP4_INPUT;
148 
149  if (!is_ipv6 && t0->mode == IPIP_MODE_6RD
150  && t0->sixrd.security_check)
151  {
152  ip6_header_t *inner_ip60 = vlib_buffer_get_current (b0);
153  if (sixrd_get_addr_net (t0, inner_ip60->src_address.as_u64[0])
154  != ip40->src_address.as_u32)
155  {
156  next0 = IPIP_INPUT_NEXT_DROP;
157  b0->error = node->errors[IPIP_ERROR_NO_TUNNEL];
158  goto drop;
159  }
160  }
161 
164  thread_index, tunnel_sw_if_index,
165  1 /* packets */ ,
166  len /* bytes */ );
167 
168  drop:
169  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
170  {
171  ipip_rx_trace_t *tr =
172  vlib_add_trace (vm, node, b0, sizeof (*tr));
173  tr->tunnel_id = tunnel_sw_if_index;
174  if (is_ipv6)
175  {
176  tr->length = ip60->payload_length;
177  tr->src.ip6.as_u64[0] = ip60->src_address.as_u64[0];
178  tr->src.ip6.as_u64[1] = ip60->src_address.as_u64[1];
179  tr->dst.ip6.as_u64[0] = ip60->dst_address.as_u64[0];
180  tr->dst.ip6.as_u64[1] = ip60->dst_address.as_u64[1];
181  }
182  else
183  {
184  tr->length = ip40->length;
185  tr->src.ip4.as_u32 = ip40->src_address.as_u32;
186  tr->dst.ip4.as_u32 = ip40->dst_address.as_u32;
187  }
188  }
189 
190  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
191  n_left_to_next, bi0, next0);
192  }
193 
194  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
195  }
197  !is_ipv6 ? ipip4_input_node.index :
198  ipip6_input_node.index, IPIP_ERROR_DECAP_PKTS,
199  from_frame->n_vectors);
200  return from_frame->n_vectors;
201 }
202 
203 static uword
205  vlib_frame_t * from_frame)
206 {
207  return ipip_input (vm, node, from_frame, /* is_ip6 */ false);
208 }
209 
210 static uword
212  vlib_frame_t * from_frame)
213 {
214  return ipip_input (vm, node, from_frame, /* is_ip6 */ true);
215 }
216 
217 static char *ipip_error_strings[] = {
218 #define _(sym,string) string,
220 #undef _
221 };
222 
223 /* *INDENT-OFF* */
225  .function = ipip4_input,
226  .name = "ipip4-input",
227  /* Takes a vector of packets. */
228  .vector_size = sizeof(u32),
229  .n_errors = IPIP_N_ERROR,
230  .error_strings = ipip_error_strings,
231  .n_next_nodes = IPIP_INPUT_N_NEXT,
232  .next_nodes =
233  {
234 #define _(s, n) [IPIP_INPUT_NEXT_##s] = n,
236 #undef _
237  },
238  .format_trace = format_ipip_rx_trace,
239 };
240 
242  .function = ipip6_input,
243  .name = "ipip6-input",
244  /* Takes a vector of packets. */
245  .vector_size = sizeof(u32),
246  .n_errors = IPIP_N_ERROR,
247  .error_strings = ipip_error_strings,
248  .n_next_nodes = IPIP_INPUT_N_NEXT,
249  .next_nodes =
250  {
251 #define _(s, n) [IPIP_INPUT_NEXT_##s] = n,
253 #undef _
254  },
255  .format_trace = format_ipip_rx_trace,
256 };
257 
260 /* *INDENT-ON* */
261 
262 /*
263  * fd.io coding-style-patch-verification: ON
264  *
265  * Local Variables:
266  * eval: (c-set-style "gnu")
267  * End:
268  */
#define CLIB_UNUSED(x)
Definition: clib.h:79
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:213
ip4_address_t src_address
Definition: ip4_packet.h:169
vlib_node_registration_t ipip6_input_node
(constructor) VLIB_REGISTER_NODE (ipip6_input_node)
Definition: node.c:241
A representation of a IPIP tunnel.
Definition: ipip.h:69
vnet_interface_main_t interface_main
Definition: vnet.h:56
u64 as_u64[2]
Definition: ip6_packet.h:51
void ip_set(ip46_address_t *dst, void *src, u8 is_ip4)
Definition: ip.c:90
#define foreach_ipip_input_next
Definition: node.c:25
static char * ipip_error_strings[]
Definition: node.c:217
format_function_t format_ip46_address
Definition: format.h:61
u32 length
Definition: node.c:42
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:451
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:250
ip6_address_t src_address
Definition: ip6_packet.h:347
unsigned char u8
Definition: types.h:56
static_always_inline u32 sixrd_get_addr_net(const ipip_tunnel_t *t, u64 dal)
Definition: ipip.h:132
u8 is_ipv6
Definition: node.c:45
static uword ipip4_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:204
#define always_inline
Definition: clib.h:92
ip4_address_t dst_address
Definition: ip4_packet.h:169
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:810
u8 * format_ipip_rx_trace(u8 *s, va_list *args)
Definition: node.c:49
ip46_address_t dst
Definition: ipip.h:57
unsigned int u32
Definition: types.h:88
ipip_transport_t transport
Definition: ipip.h:54
#define foreach_ipip_error
Definition: ipip.h:29
vlib_node_registration_t ipip4_input_node
(constructor) VLIB_REGISTER_NODE (ipip4_input_node)
Definition: node.c:224
ipip_tunnel_t * ipip_tunnel_db_find(ipip_tunnel_key_t *key)
Definition: ipip.c:343
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:202
#define PREDICT_FALSE(x)
Definition: clib.h:105
#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:218
#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:364
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:135
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1168
#define ip46_address_initializer
Definition: ip6_packet.h:89
ipip_input_next_t
Definition: node.c:31
ipip_transport_t
IPIP Tunnel key.
Definition: ipip.h:46
ipip_main_t ipip_main
Definition: ipip.c:28
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:153
u16 n_vectors
Definition: node.h:380
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:294
ip46_address_t dst
Definition: node.c:44
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:454
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:492
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:215
u32 tunnel_id
Definition: node.c:41
u32 sw_if_index
Definition: ipip.h:81
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
#define ip46_address_reset(ip46)
Definition: ip6_packet.h:84
u16 payload_length
Definition: ip6_packet.h:338
vnet_main_t * vnet_main
Definition: ipip.h:115
u64 uword
Definition: types.h:112
ipip_mode_t mode
Definition: ipip.h:74
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
#define vnet_buffer(b)
Definition: buffer.h:360
static uword ipip_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, bool is_ipv6)
Definition: node.c:62
struct ipip_tunnel_t::@227::@230 sixrd
ip46_address_t src
Definition: node.c:43
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:111
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
VLIB_NODE_FUNCTION_MULTIARCH(ethernet_input_not_l2_node, ethernet_input_not_l2)
Definition: node.c:1207
static uword ipip6_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:211
Definition: defs.h:46
ip6_address_t dst_address
Definition: ip6_packet.h:347