FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
encap.c
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  * @file
17  * @brief Functions for encapsulating VXLAN GPE tunnels
18  *
19 */
20 #include <vppinfra/error.h>
21 #include <vppinfra/hash.h>
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
26 
27 /** Statistics (not really errors) */
28 #define foreach_vxlan_gpe_encap_error \
29 _(ENCAPSULATED, "good packets encapsulated")
30 
31 /**
32  * @brief VXLAN GPE encap error strings
33  */
35 #define _(sym,string) string,
37 #undef _
38 };
39 
40 /**
41  * @brief Struct for VXLAN GPE errors/counters
42  */
43 typedef enum
44 {
45 #define _(sym,str) VXLAN_GPE_ENCAP_ERROR_##sym,
47 #undef _
50 
51 /**
52  * @brief Struct for tracing VXLAN GPE encapsulated packets
53  */
54 typedef struct
55 {
58 
59 /**
60  * @brief Trace of packets encapsulated in VXLAN GPE
61  *
62  * @param *s
63  * @param *args
64  *
65  * @return *s
66  *
67  */
68 u8 *
69 format_vxlan_gpe_encap_trace (u8 * s, va_list * args)
70 {
71  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
72  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
73  vxlan_gpe_encap_trace_t *t = va_arg (*args, vxlan_gpe_encap_trace_t *);
74 
75  s = format (s, "VXLAN-GPE-ENCAP: tunnel %d", t->tunnel_index);
76  return s;
77 }
78 
79 /**
80  * @brief Instantiates UDP + VXLAN-GPE header then set next node to IP4|6 lookup
81  *
82  * @param *ngm
83  * @param *b0
84  * @param *t0 contains rewrite header
85  * @param *next0 relative index of next dispatch function (next node)
86  * @param is_v4 Is this IPv4? (or IPv6)
87  *
88  */
89 always_inline void
91  vxlan_gpe_tunnel_t * t0, u32 * next0, u8 is_v4)
92 {
93  ASSERT (sizeof (ip4_vxlan_gpe_header_t) == 36);
94  ASSERT (sizeof (ip6_vxlan_gpe_header_t) == 56);
95 
96  ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, t0->rewrite_size, is_v4);
97  next0[0] = t0->encap_next_node;
98 }
99 
100 /**
101  * @brief Instantiates UDP + VXLAN-GPE header then set next node to IP4|6 lookup for two packets
102  *
103  * @param *ngm
104  * @param *b0 Packet0
105  * @param *b1 Packet1
106  * @param *t0 contains rewrite header for Packet0
107  * @param *t1 contains rewrite header for Packet1
108  * @param *next0 relative index of next dispatch function (next node) for Packet0
109  * @param *next1 relative index of next dispatch function (next node) for Packet1
110  * @param is_v4 Is this IPv4? (or IPv6)
111  *
112  */
113 always_inline void
116  vxlan_gpe_tunnel_t * t1, u32 * next0,
117  u32 * next1, u8 is_v4)
118 {
119  ASSERT (sizeof (ip4_vxlan_gpe_header_t) == 36);
120  ASSERT (sizeof (ip6_vxlan_gpe_header_t) == 56);
121 
122  ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, t0->rewrite_size, is_v4);
123  ip_udp_encap_one (ngm->vlib_main, b1, t1->rewrite, t1->rewrite_size, is_v4);
124  next0[0] = next1[0] = t0->encap_next_node;
125 }
126 
127 /**
128  * @brief Common processing for IPv4 and IPv6 VXLAN GPE encap dispatch functions
129  *
130  * It is worth noting that other than trivial UDP forwarding (transit), VXLAN GPE
131  * tunnels are "establish local". This means that we don't have a TX interface as yet
132  * as we need to look up where the outer-header dest is. By setting the TX index in the
133  * buffer metadata to the encap FIB, we can do a lookup to get the adjacency and real TX.
134  *
135  * vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
136  *
137  * @node vxlan-gpe-input
138  * @param *vm
139  * @param *node
140  * @param *from_frame
141  *
142  * @return from_frame->n_vectors
143  *
144  */
145 static uword
147  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
148 {
149  u32 n_left_from, next_index, *from, *to_next;
151  vnet_main_t *vnm = ngm->vnet_main;
153  u32 pkts_encapsulated = 0;
154  u32 thread_index = vlib_get_thread_index ();
155  u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
156 
157  from = vlib_frame_vector_args (from_frame);
158  n_left_from = from_frame->n_vectors;
159 
160  next_index = node->cached_next_index;
161  stats_sw_if_index = node->runtime_data[0];
162  stats_n_packets = stats_n_bytes = 0;
163 
164  while (n_left_from > 0)
165  {
166  u32 n_left_to_next;
167 
168  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
169 
170  while (n_left_from >= 4 && n_left_to_next >= 2)
171  {
172  u32 bi0, bi1;
173  vlib_buffer_t *b0, *b1;
174  u32 next0, next1;
175  u32 sw_if_index0, sw_if_index1, len0, len1;
176  vnet_hw_interface_t *hi0, *hi1;
177  vxlan_gpe_tunnel_t *t0, *t1;
178  u8 is_ip4_0, is_ip4_1;
179 
180  next0 = next1 = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
181 
182  /* Prefetch next iteration. */
183  {
184  vlib_buffer_t *p2, *p3;
185 
186  p2 = vlib_get_buffer (vm, from[2]);
187  p3 = vlib_get_buffer (vm, from[3]);
188 
189  vlib_prefetch_buffer_header (p2, LOAD);
190  vlib_prefetch_buffer_header (p3, LOAD);
191 
192  CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
193  CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
194  }
195 
196  bi0 = from[0];
197  bi1 = from[1];
198  to_next[0] = bi0;
199  to_next[1] = bi1;
200  from += 2;
201  to_next += 2;
202  n_left_to_next -= 2;
203  n_left_from -= 2;
204 
205  b0 = vlib_get_buffer (vm, bi0);
206  b1 = vlib_get_buffer (vm, bi1);
207 
208  /* 1-wide cache? */
209  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
210  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
211  hi0 =
213  vnet_buffer (b0)->sw_if_index
214  [VLIB_TX]);
215  hi1 =
217  vnet_buffer (b1)->sw_if_index
218  [VLIB_TX]);
219 
220  t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
221  t1 = pool_elt_at_index (ngm->tunnels, hi1->dev_instance);
222 
223  is_ip4_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
224  is_ip4_1 = (t1->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
225 
226  if (PREDICT_TRUE (is_ip4_0 == is_ip4_1))
227  {
228  vxlan_gpe_encap_two_inline (ngm, b0, b1, t0, t1, &next0, &next1,
229  is_ip4_0);
230  }
231  else
232  {
233  vxlan_gpe_encap_one_inline (ngm, b0, t0, &next0, is_ip4_0);
234  vxlan_gpe_encap_one_inline (ngm, b1, t1, &next1, is_ip4_1);
235  }
236 
237  /* Reset to look up tunnel partner in the configured FIB */
238  vnet_buffer (b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
239  vnet_buffer (b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
240  vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index0;
241  vnet_buffer (b1)->sw_if_index[VLIB_RX] = sw_if_index1;
242  pkts_encapsulated += 2;
243 
244  len0 = vlib_buffer_length_in_chain (vm, b0);
245  len1 = vlib_buffer_length_in_chain (vm, b0);
246  stats_n_packets += 2;
247  stats_n_bytes += len0 + len1;
248 
249  /* Batch stats increment on the same vxlan tunnel so counter is not
250  incremented per packet. Note stats are still incremented for deleted
251  and admin-down tunnel where packets are dropped. It is not worthwhile
252  to check for this rare case and affect normal path performance. */
253  if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index)
254  || (sw_if_index1 != stats_sw_if_index)))
255  {
256  stats_n_packets -= 2;
257  stats_n_bytes -= len0 + len1;
258  if (sw_if_index0 == sw_if_index1)
259  {
260  if (stats_n_packets)
263  VNET_INTERFACE_COUNTER_TX, thread_index,
264  stats_sw_if_index, stats_n_packets, stats_n_bytes);
265  stats_sw_if_index = sw_if_index0;
266  stats_n_packets = 2;
267  stats_n_bytes = len0 + len1;
268  }
269  else
270  {
272  +
274  thread_index, sw_if_index0,
275  1, len0);
277  +
279  thread_index, sw_if_index1,
280  1, len1);
281  }
282  }
283 
284  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
285  {
287  vlib_add_trace (vm, node, b0, sizeof (*tr));
288  tr->tunnel_index = t0 - ngm->tunnels;
289  }
290 
291  if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
292  {
293  vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b1,
294  sizeof (*tr));
295  tr->tunnel_index = t1 - ngm->tunnels;
296  }
297 
298  vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
299  n_left_to_next, bi0, bi1, next0,
300  next1);
301  }
302 
303  while (n_left_from > 0 && n_left_to_next > 0)
304  {
305  u32 bi0;
306  vlib_buffer_t *b0;
308  u32 sw_if_index0, len0;
309  vnet_hw_interface_t *hi0;
310  vxlan_gpe_tunnel_t *t0;
311  u8 is_ip4_0;
312 
313  bi0 = from[0];
314  to_next[0] = bi0;
315  from += 1;
316  to_next += 1;
317  n_left_from -= 1;
318  n_left_to_next -= 1;
319 
320  b0 = vlib_get_buffer (vm, bi0);
321 
322  /* 1-wide cache? */
323  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
324  hi0 =
326  vnet_buffer (b0)->sw_if_index
327  [VLIB_TX]);
328 
329  t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
330 
331  is_ip4_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
332 
333  vxlan_gpe_encap_one_inline (ngm, b0, t0, &next0, is_ip4_0);
334 
335  /* Reset to look up tunnel partner in the configured FIB */
336  vnet_buffer (b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
337  vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index0;
338  pkts_encapsulated++;
339 
340  len0 = vlib_buffer_length_in_chain (vm, b0);
341  stats_n_packets += 1;
342  stats_n_bytes += len0;
343 
344  /* Batch stats increment on the same vxlan tunnel so counter is not
345  * incremented per packet. Note stats are still incremented for deleted
346  * and admin-down tunnel where packets are dropped. It is not worthwhile
347  * to check for this rare case and affect normal path performance. */
348  if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
349  {
350  stats_n_packets -= 1;
351  stats_n_bytes -= len0;
352  if (stats_n_packets)
355  thread_index,
356  stats_sw_if_index,
357  stats_n_packets,
358  stats_n_bytes);
359  stats_n_packets = 1;
360  stats_n_bytes = len0;
361  stats_sw_if_index = sw_if_index0;
362  }
363  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
364  {
365  vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0,
366  sizeof (*tr));
367  tr->tunnel_index = t0 - ngm->tunnels;
368  }
369  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
370  n_left_to_next, bi0, next0);
371  }
372 
373  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
374  }
376  VXLAN_GPE_ENCAP_ERROR_ENCAPSULATED,
377  pkts_encapsulated);
378  /* Increment any remaining batch stats */
379  if (stats_n_packets)
380  {
383  thread_index, stats_sw_if_index,
384  stats_n_packets, stats_n_bytes);
385  node->runtime_data[0] = stats_sw_if_index;
386  }
387 
388  return from_frame->n_vectors;
389 }
390 
391 /* *INDENT-OFF* */
393  .function = vxlan_gpe_encap,
394  .name = "vxlan-gpe-encap",
395  .vector_size = sizeof (u32),
396  .format_trace = format_vxlan_gpe_encap_trace,
397  .type = VLIB_NODE_TYPE_INTERNAL,
398 
400  .error_strings = vxlan_gpe_encap_error_strings,
401 
402  .n_next_nodes = VXLAN_GPE_ENCAP_N_NEXT,
403 
404  .next_nodes = {
405  [VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
406  [VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
407  [VXLAN_GPE_ENCAP_NEXT_DROP] = "error-drop",
408  },
409 };
410 /* *INDENT-ON* */
411 
412 
413 /*
414  * fd.io coding-style-patch-verification: ON
415  *
416  * Local Variables:
417  * eval: (c-set-style "gnu")
418  * End:
419  */
#define CLIB_UNUSED(x)
Definition: clib.h:79
u32 flags
flags
Definition: vxlan_gpe.h:137
#define foreach_vxlan_gpe_encap_error
Statistics (not really errors)
Definition: encap.c:28
Struct for tracing VXLAN GPE encapsulated packets.
Definition: encap.c:54
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
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:500
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
vnet_interface_main_t interface_main
Definition: vnet.h:56
#define PREDICT_TRUE(x)
Definition: clib.h:106
VXLAN GPE definitions.
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
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
unsigned char u8
Definition: types.h:56
vnet_main_t * vnet_main
State convenience vnet_main_t.
Definition: vxlan_gpe.h:221
static uword vxlan_gpe_encap(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Common processing for IPv4 and IPv6 VXLAN GPE encap dispatch functions.
Definition: encap.c:146
static void vxlan_gpe_encap_one_inline(vxlan_gpe_main_t *ngm, vlib_buffer_t *b0, vxlan_gpe_tunnel_t *t0, u32 *next0, u8 is_v4)
Instantiates UDP + VXLAN-GPE header then set next node to IP4|6 lookup.
Definition: encap.c:90
#define always_inline
Definition: clib.h:92
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:810
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:184
u8 * format_vxlan_gpe_encap_trace(u8 *s, va_list *args)
Trace of packets encapsulated in VXLAN GPE.
Definition: encap.c:69
unsigned int u32
Definition: types.h:88
u8 * rewrite
Rewrite string.
Definition: vxlan_gpe.h:108
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
Struct for VXLAN GPE tunnel.
Definition: vxlan_gpe.h:102
static void vxlan_gpe_encap_two_inline(vxlan_gpe_main_t *ngm, vlib_buffer_t *b0, vlib_buffer_t *b1, vxlan_gpe_tunnel_t *t0, vxlan_gpe_tunnel_t *t1, u32 *next0, u32 *next1, u8 is_v4)
Instantiates UDP + VXLAN-GPE header then set next node to IP4|6 lookup for two packets.
Definition: encap.c:114
vxlan_gpe_encap_error_t
Struct for VXLAN GPE errors/counters.
Definition: encap.c:43
vxlan_gpe_main_t vxlan_gpe_main
Definition: vxlan_gpe.c:45
vlib_main_t * vlib_main
State convenience vlib_main_t.
Definition: vxlan_gpe.h:219
#define PREDICT_FALSE(x)
Definition: clib.h:105
static void ip_udp_encap_one(vlib_main_t *vm, vlib_buffer_t *b0, u8 *ec0, word ec_len, u8 is_ip4)
Definition: udp.h:326
u32 node_index
Node index.
Definition: node.h:473
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
#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
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1168
#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
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:77
vlib_main_t * vm
Definition: buffer.c:294
#define ARRAY_LEN(x)
Definition: clib.h:59
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
#define ASSERT(truth)
u8 rewrite_size
rewrite size for dynamic plugins like iOAM
Definition: vxlan_gpe.h:140
Struct for VXLAN GPE node state.
Definition: vxlan_gpe.h:196
vxlan_gpe_tunnel_t * tunnels
vector of encap tunnel instances
Definition: vxlan_gpe.h:199
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
Definition: defs.h:47
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
static char * vxlan_gpe_encap_error_strings[]
VXLAN GPE encap error strings.
Definition: encap.c:34
u32 encap_fib_index
FIB indices - tunnel partner lookup here.
Definition: vxlan_gpe.h:124
#define vnet_buffer(b)
Definition: buffer.h:360
uword encap_next_node
Next node after VxLAN-GPE encap.
Definition: vxlan_gpe.h:143
u8 data[0]
Packet data.
Definition: buffer.h:172
vlib_node_registration_t vxlan_gpe_encap_node
(constructor) VLIB_REGISTER_NODE (vxlan_gpe_encap_node)
Definition: encap.c:392
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:62
#define VXLAN_GPE_TUNNEL_IS_IPV4
Flags for vxlan_gpe_tunnel_t.
Definition: vxlan_gpe.h:168
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
Definition: defs.h:46