FD.io VPP  v16.09
Vector Packet Processing
decap.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 #include <vlib/vlib.h>
17 #include <vnet/pg/pg.h>
18 #include <vnet/lisp-gpe/lisp_gpe.h>
19 
20 typedef struct
21 {
27 
28 static u8 *
29 format_lisp_gpe_rx_trace (u8 * s, va_list * args)
30 {
31  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33  lisp_gpe_rx_trace_t *t = va_arg (*args, lisp_gpe_rx_trace_t *);
34 
35  if (t->tunnel_index != ~0)
36  {
37  s = format (s, "LISP-GPE: tunnel %d next %d error %d", t->tunnel_index,
38  t->next_index, t->error);
39  }
40  else
41  {
42  s = format (s, "LISP-GPE: no tunnel next %d error %d\n", t->next_index,
43  t->error);
44  }
45  s = format (s, "\n %U", format_lisp_gpe_header_with_length, &t->h,
46  (u32) sizeof (t->h) /* max size */ );
47  return s;
48 }
49 
51  LISP_GPE_INPUT_NEXT_DROP,
52  LISP_GPE_INPUT_NEXT_IP4_INPUT,
53  LISP_GPE_INPUT_NEXT_IP6_INPUT,
54  LISP_GPE_INPUT_NEXT_L2_INPUT,
55  LISP_GPE_INPUT_NEXT_DROP
56 };
57 
60 {
61  /* lisp-gpe router */
62  if (PREDICT_TRUE ((lgh->flags & LISP_GPE_FLAGS_P)
65  /* legacy lisp router */
66  else if ((lgh->flags & LISP_GPE_FLAGS_P) == 0)
67  {
68  ip4_header_t *iph = (ip4_header_t *) next_header;
69  if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
70  return LISP_GPE_INPUT_NEXT_IP4_INPUT;
71  else if ((iph->ip_version_and_header_length & 0xF0) == 0x60)
72  return LISP_GPE_INPUT_NEXT_IP6_INPUT;
73  else
74  return LISP_GPE_INPUT_NEXT_DROP;
75  }
76  else
77  return LISP_GPE_INPUT_NEXT_DROP;
78 }
79 
82 {
83  if (LISP_GPE_INPUT_NEXT_IP4_INPUT == next_index
84  || LISP_GPE_INPUT_NEXT_IP6_INPUT == next_index)
85  return &lgm->l3_ifaces;
86  else if (LISP_GPE_INPUT_NEXT_L2_INPUT == next_index)
87  return &lgm->l2_ifaces;
88  clib_warning ("next_index not associated to an interface!");
89  return 0;
90 }
91 
93 incr_decap_stats (vnet_main_t * vnm, u32 cpu_index, u32 length,
94  u32 sw_if_index, u32 * last_sw_if_index, u32 * n_packets,
95  u32 * n_bytes)
96 {
98 
99  if (PREDICT_TRUE (sw_if_index == *last_sw_if_index))
100  {
101  *n_packets += 1;
102  *n_bytes += length;
103  }
104  else
105  {
106  if (PREDICT_TRUE (*last_sw_if_index != ~0))
107  {
108  im = &vnm->interface_main;
109 
112  cpu_index, *last_sw_if_index,
113  *n_packets, *n_bytes);
114  }
115  *last_sw_if_index = sw_if_index;
116  *n_packets = 1;
117  *n_bytes = length;
118  }
119 }
120 
121 static uword
123  vlib_frame_t * from_frame, u8 is_v4)
124 {
125  u32 n_left_from, next_index, *from, *to_next, cpu_index;
126  u32 n_bytes = 0, n_packets = 0, last_sw_if_index = ~0, drops = 0;
128 
129  cpu_index = os_get_cpu_number ();
130  from = vlib_frame_vector_args (from_frame);
131  n_left_from = from_frame->n_vectors;
132 
133  next_index = node->cached_next_index;
134 
135  while (n_left_from > 0)
136  {
137  u32 n_left_to_next;
138 
139  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
140 
141  while (n_left_from >= 4 && n_left_to_next >= 2)
142  {
143  u32 bi0, bi1;
144  vlib_buffer_t *b0, *b1;
145  ip4_udp_lisp_gpe_header_t *iul4_0, *iul4_1;
146  ip6_udp_lisp_gpe_header_t *iul6_0, *iul6_1;
147  lisp_gpe_header_t *lh0, *lh1;
148  u32 next0, next1, error0, error1;
149  uword *si0, *si1;
150  tunnel_lookup_t *tl0, *tl1;
151 
152  /* Prefetch next iteration. */
153  {
154  vlib_buffer_t *p2, *p3;
155 
156  p2 = vlib_get_buffer (vm, from[2]);
157  p3 = vlib_get_buffer (vm, from[3]);
158 
159  vlib_prefetch_buffer_header (p2, LOAD);
160  vlib_prefetch_buffer_header (p3, LOAD);
161 
162  CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
163  CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
164  }
165 
166  bi0 = from[0];
167  bi1 = from[1];
168  to_next[0] = bi0;
169  to_next[1] = bi1;
170  from += 2;
171  to_next += 2;
172  n_left_to_next -= 2;
173  n_left_from -= 2;
174 
175  b0 = vlib_get_buffer (vm, bi0);
176  b1 = vlib_get_buffer (vm, bi1);
177 
178  /* udp leaves current_data pointing at the lisp header */
179  if (is_v4)
180  {
182  -(word) (sizeof (udp_header_t) +
183  sizeof (ip4_header_t)));
185  -(word) (sizeof (udp_header_t) +
186  sizeof (ip4_header_t)));
187 
188  iul4_0 = vlib_buffer_get_current (b0);
189  iul4_1 = vlib_buffer_get_current (b1);
190 
191  /* pop (ip, udp, lisp-gpe) */
192  vlib_buffer_advance (b0, sizeof (*iul4_0));
193  vlib_buffer_advance (b1, sizeof (*iul4_1));
194 
195  lh0 = &iul4_0->lisp;
196  lh1 = &iul4_1->lisp;
197  }
198  else
199  {
201  -(word) (sizeof (udp_header_t) +
202  sizeof (ip6_header_t)));
204  -(word) (sizeof (udp_header_t) +
205  sizeof (ip6_header_t)));
206 
207  iul6_0 = vlib_buffer_get_current (b0);
208  iul6_1 = vlib_buffer_get_current (b1);
209 
210  /* pop (ip, udp, lisp-gpe) */
211  vlib_buffer_advance (b0, sizeof (*iul6_0));
212  vlib_buffer_advance (b1, sizeof (*iul6_1));
213 
214  lh0 = &iul6_0->lisp;
215  lh1 = &iul6_1->lisp;
216  }
217 
218  /* determine next_index from lisp-gpe header */
219  next0 = next_protocol_to_next_index (lh0,
221  next1 = next_protocol_to_next_index (lh1,
223 
224  /* determine if tunnel is l2 or l3 */
225  tl0 = next_index_to_iface (lgm, next0);
226  tl1 = next_index_to_iface (lgm, next1);
227 
228  /* map iid/vni to lisp-gpe sw_if_index which is used by ipx_input to
229  * decide the rx vrf and the input features to be applied */
230  si0 = hash_get (tl0->sw_if_index_by_vni,
231  clib_net_to_host_u32 (lh0->iid));
232  si1 = hash_get (tl1->sw_if_index_by_vni,
233  clib_net_to_host_u32 (lh1->iid));
234 
235 
236  /* Required to make the l2 tag push / pop code work on l2 subifs */
237  vnet_update_l2_len (b0);
238  vnet_update_l2_len (b1);
239 
240  if (si0)
241  {
242  incr_decap_stats (lgm->vnet_main, cpu_index,
243  vlib_buffer_length_in_chain (vm, b0), si0[0],
244  &last_sw_if_index, &n_packets, &n_bytes);
245  vnet_buffer (b0)->sw_if_index[VLIB_RX] = si0[0];
246  error0 = 0;
247  }
248  else
249  {
250  next0 = LISP_GPE_INPUT_NEXT_DROP;
251  error0 = LISP_GPE_ERROR_NO_TUNNEL;
252  drops++;
253  }
254 
255  if (si1)
256  {
257  incr_decap_stats (lgm->vnet_main, cpu_index,
258  vlib_buffer_length_in_chain (vm, b1), si1[0],
259  &last_sw_if_index, &n_packets, &n_bytes);
260  vnet_buffer (b1)->sw_if_index[VLIB_RX] = si1[0];
261  error1 = 0;
262  }
263  else
264  {
265  next1 = LISP_GPE_INPUT_NEXT_DROP;
266  error1 = LISP_GPE_ERROR_NO_TUNNEL;
267  drops++;
268  }
269 
270  b0->error = error0 ? node->errors[error0] : 0;
271  b1->error = error1 ? node->errors[error1] : 0;
272 
274  {
275  lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
276  sizeof (*tr));
277  tr->next_index = next0;
278  tr->error = error0;
279  tr->h = lh0[0];
280  }
281 
283  {
284  lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1,
285  sizeof (*tr));
286  tr->next_index = next1;
287  tr->error = error1;
288  tr->h = lh1[0];
289  }
290 
291  vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
292  n_left_to_next, bi0, bi1, next0,
293  next1);
294  }
295 
296  while (n_left_from > 0 && n_left_to_next > 0)
297  {
298  u32 bi0;
299  vlib_buffer_t *b0;
300  u32 next0;
301  ip4_udp_lisp_gpe_header_t *iul4_0;
302  ip6_udp_lisp_gpe_header_t *iul6_0;
303  lisp_gpe_header_t *lh0;
304  u32 error0;
305  uword *si0;
306  tunnel_lookup_t *tl0;
307 
308  bi0 = from[0];
309  to_next[0] = bi0;
310  from += 1;
311  to_next += 1;
312  n_left_from -= 1;
313  n_left_to_next -= 1;
314 
315  b0 = vlib_get_buffer (vm, bi0);
316 
317  /* udp leaves current_data pointing at the lisp header
318  * TODO: there's no difference in processing between v4 and v6
319  * encapsulated packets so the code should be simplified if ip header
320  * info is not going to be used for dp smrs/dpsec */
321  if (is_v4)
322  {
324  -(word) (sizeof (udp_header_t) +
325  sizeof (ip4_header_t)));
326 
327  iul4_0 = vlib_buffer_get_current (b0);
328 
329  /* pop (ip, udp, lisp-gpe) */
330  vlib_buffer_advance (b0, sizeof (*iul4_0));
331 
332  lh0 = &iul4_0->lisp;
333  }
334  else
335  {
337  -(word) (sizeof (udp_header_t) +
338  sizeof (ip6_header_t)));
339 
340  iul6_0 = vlib_buffer_get_current (b0);
341 
342  /* pop (ip, udp, lisp-gpe) */
343  vlib_buffer_advance (b0, sizeof (*iul6_0));
344 
345  lh0 = &iul6_0->lisp;
346  }
347 
348  /* TODO if security is to be implemented, something similar to RPF,
349  * probably we'd like to check that the peer is allowed to send us
350  * packets. For this, we should use the tunnel table OR check that
351  * we have a mapping for the source eid and that the outer source of
352  * the packet is one of its locators */
353 
354  /* determine next_index from lisp-gpe header */
355  next0 = next_protocol_to_next_index (lh0,
357 
358  /* determine if tunnel is l2 or l3 */
359  tl0 = next_index_to_iface (lgm, next0);
360 
361  /* map iid/vni to lisp-gpe sw_if_index which is used by ipx_input to
362  * decide the rx vrf and the input features to be applied */
363  si0 = hash_get (tl0->sw_if_index_by_vni,
364  clib_net_to_host_u32 (lh0->iid));
365 
366  /* Required to make the l2 tag push / pop code work on l2 subifs */
367  vnet_update_l2_len (b0);
368 
369  if (si0)
370  {
371  incr_decap_stats (lgm->vnet_main, cpu_index,
372  vlib_buffer_length_in_chain (vm, b0), si0[0],
373  &last_sw_if_index, &n_packets, &n_bytes);
374  vnet_buffer (b0)->sw_if_index[VLIB_RX] = si0[0];
375  error0 = 0;
376  }
377  else
378  {
379  next0 = LISP_GPE_INPUT_NEXT_DROP;
380  error0 = LISP_GPE_ERROR_NO_TUNNEL;
381  drops++;
382  }
383 
384  /* TODO error handling if security is implemented */
385  b0->error = error0 ? node->errors[error0] : 0;
386 
388  {
389  lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
390  sizeof (*tr));
391  tr->next_index = next0;
392  tr->error = error0;
393  tr->h = lh0[0];
394  }
395 
396  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
397  n_left_to_next, bi0, next0);
398  }
399 
400  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
401  }
402 
403  /* flush iface stats */
404  incr_decap_stats (lgm->vnet_main, cpu_index, 0, ~0, &last_sw_if_index,
405  &n_packets, &n_bytes);
407  LISP_GPE_ERROR_NO_TUNNEL, drops);
408  return from_frame->n_vectors;
409 }
410 
411 static uword
413  vlib_frame_t * from_frame)
414 {
415  return lisp_gpe_input_inline (vm, node, from_frame, 1);
416 }
417 
418 static uword
420  vlib_frame_t * from_frame)
421 {
422  return lisp_gpe_input_inline (vm, node, from_frame, 0);
423 }
424 
426 #define lisp_gpe_error(n,s) s,
428 #undef lisp_gpe_error
429 };
430 
431 /* *INDENT-OFF* */
433  .function = lisp_gpe_ip4_input,
434  .name = "lisp-gpe-ip4-input",
435  /* Takes a vector of packets. */
436  .vector_size = sizeof (u32),
437  .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
438  .next_nodes = {
439 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
441 #undef _
442  },
443 
445  .error_strings = lisp_gpe_ip4_input_error_strings,
446 
447  .format_buffer = format_lisp_gpe_header_with_length,
448  .format_trace = format_lisp_gpe_rx_trace,
449  // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
450 };
451 /* *INDENT-ON* */
452 
453 /* *INDENT-OFF* */
455  .function = lisp_gpe_ip6_input,
456  .name = "lisp-gpe-ip6-input",
457  /* Takes a vector of packets. */
458  .vector_size = sizeof (u32),
459  .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
460  .next_nodes = {
461 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
463 #undef _
464  },
465 
467  .error_strings = lisp_gpe_ip4_input_error_strings,
468 
469  .format_buffer = format_lisp_gpe_header_with_length,
470  .format_trace = format_lisp_gpe_rx_trace,
471  // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
472 };
473 /* *INDENT-ON* */
474 
475 /*
476  * fd.io coding-style-patch-verification: ON
477  *
478  * Local Variables:
479  * eval: (c-set-style "gnu")
480  * End:
481  */
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:457
#define foreach_lisp_gpe_ip_input_next
Definition: lisp_gpe.h:120
vlib_node_registration_t lisp_gpe_ip4_input_node
(constructor) VLIB_REGISTER_NODE (lisp_gpe_ip4_input_node)
Definition: decap.c:432
#define CLIB_UNUSED(x)
Definition: clib.h:79
vnet_interface_main_t interface_main
Definition: vnet.h:64
#define PREDICT_TRUE(x)
Definition: clib.h:98
static tunnel_lookup_t * next_index_to_iface(lisp_gpe_main_t *lgm, u32 next_index)
Definition: decap.c:81
static uword lisp_gpe_ip4_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: decap.c:412
static_always_inline void incr_decap_stats(vnet_main_t *vnm, u32 cpu_index, u32 length, u32 sw_if_index, u32 *last_sw_if_index, u32 *n_packets, u32 *n_bytes)
Definition: decap.c:93
vlib_error_t * errors
Definition: node.h:418
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:112
static lisp_gpe_main_t * vnet_lisp_gpe_get_main()
Definition: lisp_gpe.h:222
#define static_always_inline
Definition: clib.h:85
static uword lisp_gpe_ip6_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: decap.c:419
#define always_inline
Definition: clib.h:84
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:187
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:501
#define clib_warning(format, args...)
Definition: error.h:59
u8 * format_lisp_gpe_header_with_length(u8 *s, va_list *args)
Definition: interface.c:346
static char * lisp_gpe_ip4_input_error_strings[]
Definition: decap.c:425
vlib_node_registration_t lisp_gpe_ip6_input_node
(constructor) VLIB_REGISTER_NODE (lisp_gpe_ip6_input_node)
Definition: decap.c:454
#define hash_get(h, key)
Definition: hash.h:248
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
#define PREDICT_FALSE(x)
Definition: clib.h:97
#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:130
#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:348
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:118
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1111
uword * sw_if_index_by_vni
Definition: lisp_gpe.h:168
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
tunnel_lookup_t l2_ifaces
Definition: lisp_gpe.h:208
vnet_main_t * vnet_main
Definition: lisp_gpe.h:212
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:200
#define ARRAY_LEN(x)
Definition: clib.h:59
static uword lisp_gpe_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, u8 is_v4)
Definition: decap.c:122
u16 cached_next_index
Definition: node.h:462
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Increment a combined counter.
Definition: counter.h:241
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:335
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
lisp_gpe_header_t h
Definition: decap.c:25
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
u64 uword
Definition: types.h:112
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
static u32 next_proto_to_next_index[LISP_GPE_NEXT_PROTOS]
Definition: decap.c:50
i64 word
Definition: types.h:111
unsigned char u8
Definition: types.h:56
tunnel_lookup_t l3_ifaces
Definition: lisp_gpe.h:192
static void vnet_update_l2_len(vlib_buffer_t *b)
Definition: l2_input.h:230
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:251
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:163
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:151
static u32 next_protocol_to_next_index(lisp_gpe_header_t *lgh, u8 *next_header)
Definition: decap.c:59
u8 ip_version_and_header_length
Definition: ip4_packet.h:108
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
Definition: defs.h:46
static u8 * format_lisp_gpe_rx_trace(u8 *s, va_list *args)
Definition: decap.c:29