FD.io VPP  v16.06
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 
50 static u32
52  LISP_GPE_INPUT_NEXT_DROP,
53  LISP_GPE_INPUT_NEXT_IP4_INPUT,
54  LISP_GPE_INPUT_NEXT_IP6_INPUT,
55  LISP_GPE_INPUT_NEXT_DROP
56 };
57 
58 static u32
60 {
61  /* lisp-gpe router */
62  if (PREDICT_TRUE((lgh->flags & LISP_GPE_FLAGS_P)
65  /* legay 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 
80 static uword
82  vlib_frame_t * from_frame, u8 is_v4)
83 {
84  u32 n_left_from, next_index, * from, * to_next;
85  u32 pkts_decapsulated = 0;
87 
88  from = vlib_frame_vector_args (from_frame);
89  n_left_from = from_frame->n_vectors;
90 
91  next_index = node->cached_next_index;
92 
93  while (n_left_from > 0)
94  {
95  u32 n_left_to_next;
96 
97  vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
98 
99  while (n_left_from >= 4 && n_left_to_next >= 2)
100  {
101  u32 bi0, bi1;
102  vlib_buffer_t * b0, * b1;
103  ip4_udp_lisp_gpe_header_t * iul4_0, * iul4_1;
104  ip6_udp_lisp_gpe_header_t * iul6_0, * iul6_1;
105  lisp_gpe_header_t * lh0, * lh1;
106  u32 next0, next1, error0, error1;
107  uword * si0, * si1;
108 
109  /* Prefetch next iteration. */
110  {
111  vlib_buffer_t * p2, * p3;
112 
113  p2 = vlib_get_buffer (vm, from[2]);
114  p3 = vlib_get_buffer (vm, from[3]);
115 
116  vlib_prefetch_buffer_header (p2, LOAD);
117  vlib_prefetch_buffer_header (p3, LOAD);
118 
121  }
122 
123  bi0 = from[0];
124  bi1 = from[1];
125  to_next[0] = bi0;
126  to_next[1] = bi1;
127  from += 2;
128  to_next += 2;
129  n_left_to_next -= 2;
130  n_left_from -= 2;
131 
132  b0 = vlib_get_buffer (vm, bi0);
133  b1 = vlib_get_buffer (vm, bi1);
134 
135  /* udp leaves current_data pointing at the lisp header */
136  if (is_v4)
137  {
139  b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
141  b1, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
142 
143  iul4_0 = vlib_buffer_get_current (b0);
144  iul4_1 = vlib_buffer_get_current (b1);
145 
146  /* pop (ip, udp, lisp-gpe) */
147  vlib_buffer_advance (b0, sizeof(*iul4_0));
148  vlib_buffer_advance (b1, sizeof(*iul4_1));
149 
150  lh0 = &iul4_0->lisp;
151  lh1 = &iul4_1->lisp;
152  }
153  else
154  {
156  b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
158  b1, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
159 
160  iul6_0 = vlib_buffer_get_current (b0);
161  iul6_1 = vlib_buffer_get_current (b1);
162 
163  /* pop (ip, udp, lisp-gpe) */
164  vlib_buffer_advance (b0, sizeof(*iul6_0));
165  vlib_buffer_advance (b1, sizeof(*iul6_1));
166 
167  lh0 = &iul6_0->lisp;
168  lh1 = &iul6_1->lisp;
169  }
170 
171  /* determine next_index from lisp-gpe header */
172  next0 = next_protocol_to_next_index (lh0,
174  next1 = next_protocol_to_next_index (lh1,
176 
177  /* Required to make the l2 tag push / pop code work on l2 subifs */
178  vnet_update_l2_len (b0);
179  vnet_update_l2_len (b1);
180 
181  /* map iid/vni to lisp-gpe sw_if_index which is used by ipx_input to
182  * decide the rx vrf and the input features to be applied */
183  si0 = hash_get(lgm->tunnel_term_sw_if_index_by_vni, lh0->iid);
184  si1 = hash_get(lgm->tunnel_term_sw_if_index_by_vni, lh1->iid);
185 
186  if (si0)
187  {
188  vnet_buffer(b0)->sw_if_index[VLIB_RX] = si0[0];
189  pkts_decapsulated++;
190  error0 = 0;
191  }
192  else
193  {
194  next0 = LISP_GPE_INPUT_NEXT_DROP;
195  error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
196  }
197 
198  if (si1)
199  {
200  vnet_buffer(b1)->sw_if_index[VLIB_RX] = si1[0];
201  pkts_decapsulated++;
202  error1 = 0;
203  }
204  else
205  {
206  next1 = LISP_GPE_INPUT_NEXT_DROP;
207  error1 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
208  }
209 
210  b0->error = error0 ? node->errors[error0] : 0;
211  b1->error = error1 ? node->errors[error1] : 0;
212 
214  {
215  lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
216  sizeof(*tr));
217  tr->next_index = next0;
218  tr->error = error0;
219  tr->h = lh0[0];
220  }
221 
223  {
224  lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1,
225  sizeof(*tr));
226  tr->next_index = next1;
227  tr->error = error1;
228  tr->h = lh1[0];
229  }
230 
231  vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
232  n_left_to_next, bi0, bi1, next0,
233  next1);
234  }
235 
236  while (n_left_from > 0 && n_left_to_next > 0)
237  {
238  u32 bi0;
239  vlib_buffer_t * b0;
240  u32 next0;
241  ip4_udp_lisp_gpe_header_t * iul4_0;
242  ip6_udp_lisp_gpe_header_t * iul6_0;
243  lisp_gpe_header_t * lh0;
244  u32 error0;
245  uword * si0;
246 
247  bi0 = from[0];
248  to_next[0] = bi0;
249  from += 1;
250  to_next += 1;
251  n_left_from -= 1;
252  n_left_to_next -= 1;
253 
254  b0 = vlib_get_buffer (vm, bi0);
255 
256  /* udp leaves current_data pointing at the lisp header
257  * TODO: there's no difference in processing between v4 and v6
258  * encapsulated packets so the code should be simplified if ip header
259  * info is not going to be used for dp smrs/dpsec */
260  if (is_v4)
261  {
263  b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
264 
265  iul4_0 = vlib_buffer_get_current (b0);
266 
267  /* pop (ip, udp, lisp-gpe) */
268  vlib_buffer_advance (b0, sizeof(*iul4_0));
269 
270  lh0 = &iul4_0->lisp;
271  }
272  else
273  {
275  b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
276 
277  iul6_0 = vlib_buffer_get_current (b0);
278 
279  /* pop (ip, udp, lisp-gpe) */
280  vlib_buffer_advance (b0, sizeof(*iul6_0));
281 
282  lh0 = &iul6_0->lisp;
283  }
284 
285  /* TODO if security is to be implemented, something similar to RPF,
286  * probably we'd like to check that the peer is allowed to send us
287  * packets. For this, we should use the tunnel table OR check that
288  * we have a mapping for the source eid and that the outer source of
289  * the packet is one of its locators */
290 
291  /* determine next_index from lisp-gpe header */
292  next0 = next_protocol_to_next_index (lh0,
294 
295  /* Required to make the l2 tag push / pop code work on l2 subifs */
296  vnet_update_l2_len (b0);
297 
298  /* map iid/vni to lisp-gpe sw_if_index which is used by ipx_input to
299  * decide the rx vrf and the input features to be applied */
300  si0 = hash_get(lgm->tunnel_term_sw_if_index_by_vni, lh0->iid);
301 
302  if (si0)
303  {
304  vnet_buffer(b0)->sw_if_index[VLIB_RX] = si0[0];
305  pkts_decapsulated++;
306  error0 = 0;
307  }
308  else
309  {
310  next0 = LISP_GPE_INPUT_NEXT_DROP;
311  error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
312  }
313 
314  /* TODO error handling if security is implemented */
315  b0->error = error0 ? node->errors[error0] : 0;
316 
318  {
319  lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
320  sizeof(*tr));
321  tr->next_index = next0;
322  tr->error = error0;
323  tr->h = lh0[0];
324  }
325 
326  vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
327  n_left_to_next, bi0, next0);
328  }
329 
330  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
331  }
333  LISP_GPE_ERROR_DECAPSULATED,
334  pkts_decapsulated);
335  return from_frame->n_vectors;
336 }
337 
338 static uword
340  vlib_frame_t * from_frame)
341 {
342  return lisp_gpe_input_inline(vm, node, from_frame, 1);
343 }
344 
345 static uword
347  vlib_frame_t * from_frame)
348 {
349  return lisp_gpe_input_inline(vm, node, from_frame, 0);
350 }
351 
352 static char * lisp_gpe_error_strings[] = {
353 #define lisp_gpe_error(n,s) s,
355 #undef lisp_gpe_error
356 #undef _
357 };
358 
360  .function = lisp_gpe_ip4_input,
361  .name = "lisp-gpe-ip4-input",
362  /* Takes a vector of packets. */
363  .vector_size = sizeof (u32),
364 
365  .n_errors = LISP_GPE_N_ERROR,
366  .error_strings = lisp_gpe_error_strings,
367 
368  .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
369  .next_nodes = {
370 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
372 #undef _
373  },
374 
375  .format_buffer = format_lisp_gpe_header_with_length,
376  .format_trace = format_lisp_gpe_rx_trace,
377  // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
378 };
379 
381  .function = lisp_gpe_ip6_input,
382  .name = "lisp-gpe-ip6-input",
383  /* Takes a vector of packets. */
384  .vector_size = sizeof (u32),
385 
386  .n_errors = LISP_GPE_N_ERROR,
387  .error_strings = lisp_gpe_error_strings,
388 
389  .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
390  .next_nodes = {
391 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
393 #undef _
394  },
395 
396  .format_buffer = format_lisp_gpe_header_with_length,
397  .format_trace = format_lisp_gpe_rx_trace,
398  // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
399 };
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 foreach_lisp_gpe_ip_input_next
Definition: lisp_gpe.h:84
vlib_node_registration_t lisp_gpe_ip4_input_node
(constructor) VLIB_REGISTER_NODE (lisp_gpe_ip4_input_node)
Definition: decap.c:359
#define CLIB_UNUSED(x)
Definition: clib.h:79
uword * tunnel_term_sw_if_index_by_vni
Definition: lisp_gpe.h:137
#define PREDICT_TRUE(x)
Definition: clib.h:98
static uword lisp_gpe_ip4_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: decap.c:339
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
static uword lisp_gpe_ip6_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: decap.c:346
u8 * format_lisp_gpe_header_with_length(u8 *s, va_list *args)
Definition: interface.c:326
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
vlib_node_registration_t lisp_gpe_ip6_input_node
(constructor) VLIB_REGISTER_NODE (lisp_gpe_ip6_input_node)
Definition: decap.c:380
#define hash_get(h, key)
Definition: hash.h:231
static char * lisp_gpe_error_strings[]
Definition: decap.c:352
#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
lisp_gpe_main_t lisp_gpe_main
Definition: lisp_gpe.c:18
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:81
u16 cached_next_index
Definition: node.h:422
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
lisp_gpe_header_t h
Definition: decap.c:25
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
u64 uword
Definition: types.h:112
static u32 next_proto_to_next_index[LISP_GPE_NEXT_PROTOS]
Definition: decap.c:51
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
#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
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:84
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
Definition: defs.h:45
static u8 * format_lisp_gpe_rx_trace(u8 *s, va_list *args)
Definition: decap.c:29