LISP EID virtualization support
[vpp.git] / vnet / vnet / lisp-gpe / decap.c
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 {
22   u32 next_index;
23   u32 tunnel_index;
24   u32 error;
25   lisp_gpe_header_t h;
26 } lisp_gpe_rx_trace_t;
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
51 next_proto_to_next_index[LISP_GPE_NEXT_PROTOS] = {
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
59 next_protocol_to_next_index (lisp_gpe_header_t * lgh, u8 * next_header)
60 {
61   /* lisp-gpe router */
62   if (PREDICT_TRUE((lgh->flags & LISP_GPE_FLAGS_P)
63       && lgh->next_protocol < LISP_GPE_NEXT_PROTOS))
64     return next_proto_to_next_index[lgh->next_protocol];
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
81 lisp_gpe_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
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;
86   lisp_gpe_main_t * lgm = &lisp_gpe_main;
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
119             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
120             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
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             {
138               vlib_buffer_advance (
139                   b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
140               vlib_buffer_advance (
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             {
155               vlib_buffer_advance (
156                   b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
157               vlib_buffer_advance (
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,
173                                                vlib_buffer_get_current (b0));
174           next1 = next_protocol_to_next_index (lh1,
175                                                vlib_buffer_get_current (b1));
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,
184                          clib_net_to_host_u32 (lh0->iid));
185           si1 = hash_get(lgm->tunnel_term_sw_if_index_by_vni,
186                          clib_net_to_host_u32 (lh1->iid));
187
188           if (si0)
189             {
190               vnet_buffer(b0)->sw_if_index[VLIB_RX] = si0[0];
191               pkts_decapsulated++;
192               error0 = 0;
193             }
194           else
195             {
196               next0 = LISP_GPE_INPUT_NEXT_DROP;
197               error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
198             }
199
200           if (si1)
201             {
202               vnet_buffer(b1)->sw_if_index[VLIB_RX] = si1[0];
203               pkts_decapsulated++;
204               error1 = 0;
205             }
206           else
207             {
208               next1 = LISP_GPE_INPUT_NEXT_DROP;
209               error1 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
210             }
211
212           b0->error = error0 ? node->errors[error0] : 0;
213           b1->error = error1 ? node->errors[error1] : 0;
214
215           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
216             {
217               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
218                                                         sizeof(*tr));
219               tr->next_index = next0;
220               tr->error = error0;
221               tr->h = lh0[0];
222             }
223
224           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
225             {
226               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1,
227                                                         sizeof(*tr));
228               tr->next_index = next1;
229               tr->error = error1;
230               tr->h = lh1[0];
231             }
232
233           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
234                                           n_left_to_next, bi0, bi1, next0,
235                                           next1);
236         }
237     
238       while (n_left_from > 0 && n_left_to_next > 0)
239         {
240           u32 bi0;
241           vlib_buffer_t * b0;
242           u32 next0;
243           ip4_udp_lisp_gpe_header_t * iul4_0;
244           ip6_udp_lisp_gpe_header_t * iul6_0;
245           lisp_gpe_header_t * lh0;
246           u32 error0;
247           uword * si0;
248
249           bi0 = from[0];
250           to_next[0] = bi0;
251           from += 1;
252           to_next += 1;
253           n_left_from -= 1;
254           n_left_to_next -= 1;
255
256           b0 = vlib_get_buffer (vm, bi0);
257
258           /* udp leaves current_data pointing at the lisp header
259            * TODO: there's no difference in processing between v4 and v6
260            * encapsulated packets so the code should be simplified if ip header
261            * info is not going to be used for dp smrs/dpsec */
262           if (is_v4)
263             {
264               vlib_buffer_advance (
265                   b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
266
267               iul4_0 = vlib_buffer_get_current (b0);
268
269               /* pop (ip, udp, lisp-gpe) */
270               vlib_buffer_advance (b0, sizeof(*iul4_0));
271
272               lh0 = &iul4_0->lisp;
273             }
274           else
275             {
276               vlib_buffer_advance (
277                   b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
278
279               iul6_0 = vlib_buffer_get_current (b0);
280
281               /* pop (ip, udp, lisp-gpe) */
282               vlib_buffer_advance (b0, sizeof(*iul6_0));
283
284               lh0 = &iul6_0->lisp;
285             }
286
287           /* TODO if security is to be implemented, something similar to RPF,
288            * probably we'd like to check that the peer is allowed to send us
289            * packets. For this, we should use the tunnel table OR check that
290            * we have a mapping for the source eid and that the outer source of
291            * the packet is one of its locators */
292
293           /* determine next_index from lisp-gpe header */
294           next0 = next_protocol_to_next_index (lh0,
295                                                vlib_buffer_get_current (b0));
296
297           /* Required to make the l2 tag push / pop code work on l2 subifs */
298           vnet_update_l2_len (b0);
299
300           /* map iid/vni to lisp-gpe sw_if_index which is used by ipx_input to
301            * decide the rx vrf and the input features to be applied */
302           si0 = hash_get(lgm->tunnel_term_sw_if_index_by_vni,
303                          clib_net_to_host_u32 (lh0->iid));
304
305           if (si0)
306             {
307               vnet_buffer(b0)->sw_if_index[VLIB_RX] = si0[0];
308               pkts_decapsulated++;
309               error0 = 0;
310             }
311           else
312             {
313               next0 = LISP_GPE_INPUT_NEXT_DROP;
314               error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
315             }
316
317           /* TODO error handling if security is implemented */
318           b0->error = error0 ? node->errors[error0] : 0;
319
320           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
321             {
322               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
323                                                         sizeof(*tr));
324               tr->next_index = next0;
325               tr->error = error0;
326               tr->h = lh0[0];
327             }
328
329           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
330                                           n_left_to_next, bi0, next0);
331         }
332
333       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
334     }
335   vlib_node_increment_counter (vm, lisp_gpe_ip4_input_node.index,
336                                LISP_GPE_ERROR_DECAPSULATED, 
337                                pkts_decapsulated);
338   return from_frame->n_vectors;
339 }
340
341 static uword
342 lisp_gpe_ip4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
343                     vlib_frame_t * from_frame)
344 {
345   return lisp_gpe_input_inline(vm, node, from_frame, 1);
346 }
347
348 static uword
349 lisp_gpe_ip6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
350                     vlib_frame_t * from_frame)
351 {
352   return lisp_gpe_input_inline(vm, node, from_frame, 0);
353 }
354
355 static char * lisp_gpe_error_strings[] = {
356 #define lisp_gpe_error(n,s) s,
357 #include <vnet/lisp-gpe/lisp_gpe_error.def>
358 #undef lisp_gpe_error
359 #undef _
360 };
361
362 VLIB_REGISTER_NODE (lisp_gpe_ip4_input_node) = {
363   .function = lisp_gpe_ip4_input,
364   .name = "lisp-gpe-ip4-input",
365   /* Takes a vector of packets. */
366   .vector_size = sizeof (u32),
367
368   .n_errors = LISP_GPE_N_ERROR,
369   .error_strings = lisp_gpe_error_strings,
370
371   .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
372   .next_nodes = {
373 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
374     foreach_lisp_gpe_ip_input_next
375 #undef _
376   },
377
378   .format_buffer = format_lisp_gpe_header_with_length,
379   .format_trace = format_lisp_gpe_rx_trace,
380   // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
381 };
382
383 VLIB_REGISTER_NODE (lisp_gpe_ip6_input_node) = {
384   .function = lisp_gpe_ip6_input,
385   .name = "lisp-gpe-ip6-input",
386   /* Takes a vector of packets. */
387   .vector_size = sizeof (u32),
388
389   .n_errors = LISP_GPE_N_ERROR,
390   .error_strings = lisp_gpe_error_strings,
391
392   .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
393   .next_nodes = {
394 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
395     foreach_lisp_gpe_ip_input_next
396 #undef _
397   },
398
399   .format_buffer = format_lisp_gpe_header_with_length,
400   .format_trace = format_lisp_gpe_rx_trace,
401   // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
402 };