FD.io VPP  v16.06
Vector Packet Processing
decap.c
Go to the documentation of this file.
1 /*
2  * decap.c - decapsulate VXLAN GPE
3  *
4  * Copyright (c) 2013 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 agreed 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/pg/pg.h>
21 
22 typedef struct {
27 
28 static u8 * format_vxlan_gpe_rx_trace (u8 * s, va_list * args)
29 {
30  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
31  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
32  vxlan_gpe_rx_trace_t * t = va_arg (*args, vxlan_gpe_rx_trace_t *);
33 
34  if (t->tunnel_index != ~0)
35  {
36  s = format (s, "VXLAN-GPE: tunnel %d next %d error %d", t->tunnel_index,
37  t->next_index, t->error);
38  }
39  else
40  {
41  s = format (s, "VXLAN-GPE: no tunnel next %d error %d\n", t->next_index,
42  t->error);
43  }
44  return s;
45 }
46 
47 
48 static u8 * format_vxlan_gpe_with_length (u8 * s, va_list * args)
49 {
50  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 
53 
54  return s;
55 }
56 
57 static uword
59  vlib_node_runtime_t * node,
60  vlib_frame_t * from_frame)
61 {
62  u32 n_left_from, next_index, * from, * to_next;
64  vnet_main_t * vnm = ngm->vnet_main;
66  u32 last_tunnel_index = ~0;
67  vxlan_gpe_tunnel_key_t last_key;
68  u32 pkts_decapsulated = 0;
69  u32 cpu_index = os_get_cpu_number();
70  u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
71 
72  memset (&last_key, 0xff, sizeof (last_key));
73 
74  from = vlib_frame_vector_args (from_frame);
75  n_left_from = from_frame->n_vectors;
76 
77  next_index = node->cached_next_index;
78  stats_sw_if_index = node->runtime_data[0];
79  stats_n_packets = stats_n_bytes = 0;
80 
81  while (n_left_from > 0)
82  {
83  u32 n_left_to_next;
84 
85  vlib_get_next_frame (vm, node, next_index,
86  to_next, n_left_to_next);
87 
88  while (n_left_from >= 4 && n_left_to_next >= 2)
89  {
90  u32 bi0, bi1;
91  vlib_buffer_t * b0, * b1;
92  u32 next0, next1;
93  ip4_vxlan_gpe_header_t * iuvn0, * iuvn1;
94  uword * p0, * p1;
95  u32 tunnel_index0, tunnel_index1;
96  vxlan_gpe_tunnel_t * t0, * t1;
97  vxlan_gpe_tunnel_key_t key0, key1;
98  u32 error0, error1;
99  u32 sw_if_index0, sw_if_index1, len0, len1;
100 
101  /* Prefetch next iteration. */
102  {
103  vlib_buffer_t * p2, * p3;
104 
105  p2 = vlib_get_buffer (vm, from[2]);
106  p3 = vlib_get_buffer (vm, from[3]);
107 
108  vlib_prefetch_buffer_header (p2, LOAD);
109  vlib_prefetch_buffer_header (p3, LOAD);
110 
113  }
114 
115  bi0 = from[0];
116  bi1 = from[1];
117  to_next[0] = bi0;
118  to_next[1] = bi1;
119  from += 2;
120  to_next += 2;
121  n_left_to_next -= 2;
122  n_left_from -= 2;
123 
124  b0 = vlib_get_buffer (vm, bi0);
125  b1 = vlib_get_buffer (vm, bi1);
126 
127  /* udp leaves current_data pointing at the vxlan header */
129  (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
131  (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
132 
133  iuvn0 = vlib_buffer_get_current (b0);
134  iuvn1 = vlib_buffer_get_current (b1);
135 
136  /* pop (ip, udp, vxlan) */
137  vlib_buffer_advance (b0, sizeof (*iuvn0));
138  vlib_buffer_advance (b1, sizeof (*iuvn1));
139 
140  tunnel_index0 = ~0;
141  tunnel_index1 = ~0;
142  error0 = 0;
143  error1 = 0;
144 
145  next0 = (iuvn0->vxlan.protocol < node->n_next_nodes) ? iuvn0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
146  next1 = (iuvn1->vxlan.protocol < node->n_next_nodes) ? iuvn1->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
147 
148 
149 
150 
151  key0.local = iuvn0->ip4.dst_address.as_u32;
152  key1.local = iuvn1->ip4.dst_address.as_u32;
153 
154  key0.remote = iuvn0->ip4.src_address.as_u32;
155  key1.remote = iuvn1->ip4.src_address.as_u32;
156 
157  key0.vni = iuvn0->vxlan.vni_res;
158  key1.vni = iuvn1->vxlan.vni_res;
159 
160  key0.pad = 0;
161  key1.pad = 0;
162 
163  /* Processing for key0 */
164  if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
165  || (key0.as_u64[1] != last_key.as_u64[1])))
166  {
167  p0 = hash_get_mem (ngm->vxlan_gpe_tunnel_by_key, &key0);
168 
169  if (p0 == 0)
170  {
171  error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
172  goto trace0;
173  }
174 
175  last_key.as_u64[0] = key0.as_u64[0];
176  last_key.as_u64[1] = key0.as_u64[1];
177  tunnel_index0 = last_tunnel_index = p0[0];
178  }
179  else
180  tunnel_index0 = last_tunnel_index;
181 
182  t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
183 
184  next0 = t0->protocol;
185 
186  sw_if_index0 = t0->sw_if_index;
187  len0 = vlib_buffer_length_in_chain(vm, b0);
188 
189  /* Required to make the l2 tag push / pop code work on l2 subifs */
190  vnet_update_l2_len (b0);
191 
192  /*
193  * ip[46] lookup in the configured FIB
194  */
195  vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
196 
197  pkts_decapsulated++;
198  stats_n_packets += 1;
199  stats_n_bytes += len0;
200 
201  if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
202  {
203  stats_n_packets -= 1;
204  stats_n_bytes -= len0;
205  if (stats_n_packets)
208  cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
209  stats_n_packets = 1;
210  stats_n_bytes = len0;
211  stats_sw_if_index = sw_if_index0;
212  }
213 
214  trace0:
215  b0->error = error0 ? node->errors[error0] : 0;
216 
218  {
220  = vlib_add_trace (vm, node, b0, sizeof (*tr));
221  tr->next_index = next0;
222  tr->error = error0;
223  tr->tunnel_index = tunnel_index0;
224  }
225 
226 
227  /* Processing for key1 */
228  if (PREDICT_FALSE ((key1.as_u64[0] != last_key.as_u64[0])
229  || (key1.as_u64[1] != last_key.as_u64[1])))
230  {
231  p1 = hash_get_mem (ngm->vxlan_gpe_tunnel_by_key, &key1);
232 
233  if (p1 == 0)
234  {
235  error1 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
236  goto trace1;
237  }
238 
239  last_key.as_u64[0] = key1.as_u64[0];
240  last_key.as_u64[1] = key1.as_u64[1];
241  tunnel_index1 = last_tunnel_index = p1[0];
242  }
243  else
244  tunnel_index1 = last_tunnel_index;
245 
246  t1 = pool_elt_at_index (ngm->tunnels, tunnel_index1);
247 
248  next1 = t1->protocol;
249  sw_if_index1 = t1->sw_if_index;
250  len1 = vlib_buffer_length_in_chain(vm, b1);
251 
252  /* Required to make the l2 tag push / pop code work on l2 subifs */
253  vnet_update_l2_len (b1);
254 
255  /*
256  * ip[46] lookup in the configured FIB
257  */
258  vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
259 
260 
261  pkts_decapsulated++;
262  stats_n_packets += 1;
263  stats_n_bytes += len1;
264 
265  /* Batch stats increment on the same vxlan tunnel so counter
266  is not incremented per packet */
267  if (PREDICT_FALSE(sw_if_index1 != stats_sw_if_index))
268  {
269  stats_n_packets -= 1;
270  stats_n_bytes -= len1;
271  if (stats_n_packets)
274  cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
275  stats_n_packets = 1;
276  stats_n_bytes = len1;
277  stats_sw_if_index = sw_if_index1;
278  }
279  vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
280 
281  trace1:
282  b1->error = error1 ? node->errors[error1] : 0;
283 
285  {
287  = vlib_add_trace (vm, node, b1, sizeof (*tr));
288  tr->next_index = next1;
289  tr->error = error1;
290  tr->tunnel_index = tunnel_index1;
291  }
292 
293  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
294  to_next, n_left_to_next,
295  bi0, bi1, next0, next1);
296  }
297 
298  while (n_left_from > 0 && n_left_to_next > 0)
299  {
300  u32 bi0;
301  vlib_buffer_t * b0;
302  u32 next0;
303  ip4_vxlan_gpe_header_t * iuvn0;
304  uword * p0;
305  u32 tunnel_index0;
306  vxlan_gpe_tunnel_t * t0;
307  vxlan_gpe_tunnel_key_t key0;
308  u32 error0;
309  u32 sw_if_index0, len0;
310 
311  bi0 = from[0];
312  to_next[0] = bi0;
313  from += 1;
314  to_next += 1;
315  n_left_from -= 1;
316  n_left_to_next -= 1;
317 
318  b0 = vlib_get_buffer (vm, bi0);
319 
320  /* udp leaves current_data pointing at the vxlan header */
322  (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
323 
324  iuvn0 = vlib_buffer_get_current (b0);
325 
326  /* pop (ip, udp, vxlan) */
327  vlib_buffer_advance (b0, sizeof (*iuvn0));
328 
329  tunnel_index0 = ~0;
330  error0 = 0;
331  next0 = (iuvn0->vxlan.protocol < node->n_next_nodes) ? iuvn0->vxlan.protocol : VXLAN_GPE_INPUT_NEXT_DROP;
332 
333  key0.local = iuvn0->ip4.dst_address.as_u32;
334  key0.remote = iuvn0->ip4.src_address.as_u32;
335  key0.vni = iuvn0->vxlan.vni_res;
336  key0.pad = 0;
337 
338  if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
339  || (key0.as_u64[1] != last_key.as_u64[1])))
340  {
341  p0 = hash_get_mem (ngm->vxlan_gpe_tunnel_by_key, &key0);
342 
343  if (p0 == 0)
344  {
345  error0 = VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
346  goto trace00;
347  }
348 
349  last_key.as_u64[0] = key0.as_u64[0];
350  last_key.as_u64[1] = key0.as_u64[1];
351  tunnel_index0 = last_tunnel_index = p0[0];
352  }
353  else
354  tunnel_index0 = last_tunnel_index;
355 
356  t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
357 
358  next0 = t0->protocol;
359 
360  sw_if_index0 = t0->sw_if_index;
361  len0 = vlib_buffer_length_in_chain(vm, b0);
362 
363  /* Required to make the l2 tag push / pop code work on l2 subifs */
364  vnet_update_l2_len (b0);
365 
366  /*
367  * ip[46] lookup in the configured FIB
368  */
369  vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
370 
371  pkts_decapsulated ++;
372  stats_n_packets += 1;
373  stats_n_bytes += len0;
374 
375  /* Batch stats increment on the same vxlan-gpe tunnel so counter
376  is not incremented per packet */
377  if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
378  {
379  stats_n_packets -= 1;
380  stats_n_bytes -= len0;
381  if (stats_n_packets)
384  cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
385  stats_n_packets = 1;
386  stats_n_bytes = len0;
387  stats_sw_if_index = sw_if_index0;
388  }
389 
390  trace00:
391  b0->error = error0 ? node->errors[error0] : 0;
392 
394  {
396  = vlib_add_trace (vm, node, b0, sizeof (*tr));
397  tr->next_index = next0;
398  tr->error = error0;
399  tr->tunnel_index = tunnel_index0;
400  }
401  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
402  to_next, n_left_to_next,
403  bi0, next0);
404  }
405 
406  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
407  }
409  VXLAN_GPE_ERROR_DECAPSULATED,
410  pkts_decapsulated);
411  /* Increment any remaining batch stats */
412  if (stats_n_packets)
413  {
416  stats_sw_if_index, stats_n_packets, stats_n_bytes);
417  node->runtime_data[0] = stats_sw_if_index;
418  }
419  return from_frame->n_vectors;
420 }
421 
422 static char * vxlan_gpe_error_strings[] = {
423 #define vxlan_gpe_error(n,s) s,
425 #undef vxlan_gpe_error
426 #undef _
427 };
428 
430  .function = vxlan_gpe_input,
431  .name = "vxlan-gpe-input",
432  /* Takes a vector of packets. */
433  .vector_size = sizeof (u32),
435  .n_errors = ARRAY_LEN(vxlan_gpe_error_strings),
436  .error_strings = vxlan_gpe_error_strings,
437 
438  .n_next_nodes = VXLAN_GPE_INPUT_N_NEXT,
439  .next_nodes = {
440 #define _(s,n) [VXLAN_GPE_INPUT_NEXT_##s] = n,
442 #undef _
443  },
444 
445  .format_buffer = format_vxlan_gpe_with_length,
446  .format_trace = format_vxlan_gpe_rx_trace,
447  // $$$$ .unformat_buffer = unformat_vxlan_gpe_header,
448 };
449 
450 
static uword vxlan_gpe_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: decap.c:58
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Definition: main.c:459
#define CLIB_UNUSED(x)
Definition: clib.h:79
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
vnet_interface_main_t interface_main
Definition: vnet.h:62
vlib_node_registration_t vxlan_gpe_input_node
(constructor) VLIB_REGISTER_NODE (vxlan_gpe_input_node)
Definition: decap.c:429
vlib_error_t * errors
Definition: node.h:378
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
vnet_main_t * vnet_main
Definition: vxlan_gpe.h:110
static char * vxlan_gpe_error_strings[]
Definition: decap.c:422
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:458
always_inline void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Definition: counter.h:210
always_inline 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:112
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
#define pool_elt_at_index(p, i)
Definition: pool.h:346
uword os_get_cpu_number(void)
Definition: unix-misc.c:206
vxlan_gpe_main_t vxlan_gpe_main
Definition: vxlan_gpe.c:17
#define PREDICT_FALSE(x)
Definition: clib.h:97
always_inline void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:970
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Definition: buffer_node.h:43
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Definition: buffer_node.h:83
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:129
u16 n_vectors
Definition: node.h:307
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
#define ARRAY_LEN(x)
Definition: clib.h:59
#define foreach_vxlan_gpe_input_next
Definition: vxlan_gpe.h:75
u16 cached_next_index
Definition: node.h:422
unsigned int u32
Definition: types.h:88
static u8 * format_vxlan_gpe_with_length(u8 *s, va_list *args)
Definition: decap.c:48
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
vxlan_gpe_tunnel_t * tunnels
Definition: vxlan_gpe.h:97
u64 uword
Definition: types.h:112
Definition: defs.h:46
i64 word
Definition: types.h:111
always_inline void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:197
unsigned char u8
Definition: types.h:56
static void vnet_update_l2_len(vlib_buffer_t *b)
Definition: l2_input.h:226
always_inline 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
uword * vxlan_gpe_tunnel_by_key
Definition: vxlan_gpe.h:100
#define hash_get_mem(h, key)
Definition: hash.h:251
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u8 data[0]
Packet data.
Definition: buffer.h:150
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
static u8 * format_vxlan_gpe_rx_trace(u8 *s, va_list *args)
Definition: decap.c:28
uword runtime_data[(128-1 *sizeof(vlib_node_function_t *)-1 *sizeof(vlib_error_t *)-11 *sizeof(u32)-5 *sizeof(u16))/sizeof(uword)]
Definition: node.h:432
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