Add lisp-gpe ip6 data-plane 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   /* legay lisp router */
62   if (PREDICT_FALSE((lgh->flags & LISP_GPE_FLAGS_P) == 0))
63     {
64       ip4_header_t * iph = (ip4_header_t *) next_header;
65       if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
66         return LISP_GPE_INPUT_NEXT_IP4_INPUT;
67       else if ((iph->ip_version_and_header_length & 0xF0) == 0x60)
68         return LISP_GPE_INPUT_NEXT_IP6_INPUT;
69       else
70         return LISP_GPE_INPUT_NEXT_DROP;
71     }
72   /* lisp-gpe router */
73   else if ((lgh->flags & LISP_GPE_FLAGS_P)
74       && lgh->next_protocol < LISP_GPE_NEXT_PROTOS)
75     return next_proto_to_next_index[lgh->next_protocol];
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   lisp_gpe_tunnel_key_t last_key;
86   u32 pkts_decapsulated = 0;
87   lisp_gpe_main_t * lgm = &lisp_gpe_main;
88
89   memset (&last_key, 0xff, sizeof (last_key));
90
91   from = vlib_frame_vector_args (from_frame);
92   n_left_from = from_frame->n_vectors;
93
94   next_index = node->cached_next_index;
95
96   while (n_left_from > 0)
97     {
98       u32 n_left_to_next;
99
100       vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
101
102       while (n_left_from >= 4 && n_left_to_next >= 2)
103         {
104           u32 bi0, bi1;
105           vlib_buffer_t * b0, * b1;
106           ip4_udp_lisp_gpe_header_t * iul4_0, * iul4_1;
107           ip6_udp_lisp_gpe_header_t * iul6_0, * iul6_1;
108           lisp_gpe_header_t * lh0, * lh1;
109           u32 next0, next1, error0, error1;
110           uword * si0, * si1;
111
112           /* Prefetch next iteration. */
113           {
114             vlib_buffer_t * p2, * p3;
115
116             p2 = vlib_get_buffer (vm, from[2]);
117             p3 = vlib_get_buffer (vm, from[3]);
118
119             vlib_prefetch_buffer_header (p2, LOAD);
120             vlib_prefetch_buffer_header (p3, LOAD);
121
122             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
123             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
124           }
125
126           bi0 = from[0];
127           bi1 = from[1];
128           to_next[0] = bi0;
129           to_next[1] = bi1;
130           from += 2;
131           to_next += 2;
132           n_left_to_next -= 2;
133           n_left_from -= 2;
134
135           b0 = vlib_get_buffer (vm, bi0);
136           b1 = vlib_get_buffer (vm, bi1);
137
138           /* udp leaves current_data pointing at the lisp header */
139           if (is_v4)
140             {
141               vlib_buffer_advance (
142                   b0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
143               vlib_buffer_advance (
144                   b1, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
145
146               iul4_0 = vlib_buffer_get_current (b0);
147               iul4_1 = vlib_buffer_get_current (b1);
148
149               /* pop (ip, udp, lisp-gpe) */
150               vlib_buffer_advance (b0, sizeof(*iul4_0));
151               vlib_buffer_advance (b1, sizeof(*iul4_1));
152
153               lh0 = &iul4_0->lisp;
154               lh1 = &iul4_1->lisp;
155             }
156           else
157             {
158               vlib_buffer_advance (
159                   b0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
160               vlib_buffer_advance (
161                   b1, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
162
163               iul6_0 = vlib_buffer_get_current (b0);
164               iul6_1 = vlib_buffer_get_current (b1);
165
166               /* pop (ip, udp, lisp-gpe) */
167               vlib_buffer_advance (b0, sizeof(*iul6_0));
168               vlib_buffer_advance (b1, sizeof(*iul6_1));
169
170               lh0 = &iul6_0->lisp;
171               lh1 = &iul6_1->lisp;
172             }
173
174           /* determine next_index from lisp-gpe header */
175           next0 = next_protocol_to_next_index (lh0,
176                                                vlib_buffer_get_current (b0));
177           next1 = next_protocol_to_next_index (lh1,
178                                                vlib_buffer_get_current (b1));
179
180           /* Required to make the l2 tag push / pop code work on l2 subifs */
181           vnet_update_l2_len (b0);
182           vnet_update_l2_len (b1);
183
184           /* map iid/vni to lisp-gpe sw_if_index which is used by ipx_input to
185            * decide the rx vrf and the input features to be applied */
186           si0 = hash_get(lgm->tunnel_term_sw_if_index_by_vni, lh0->iid);
187           si1 = hash_get(lgm->tunnel_term_sw_if_index_by_vni, lh1->iid);
188
189           if (si0)
190             {
191               vnet_buffer(b0)->sw_if_index[VLIB_RX] = si0[0];
192               pkts_decapsulated++;
193               error0 = 0;
194             }
195           else
196             {
197               next0 = LISP_GPE_INPUT_NEXT_DROP;
198               error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
199             }
200
201           if (si1)
202             {
203               vnet_buffer(b1)->sw_if_index[VLIB_RX] = si1[0];
204               pkts_decapsulated++;
205               error0 = 0;
206             }
207           else
208             {
209               next1 = LISP_GPE_INPUT_NEXT_DROP;
210               error1 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
211             }
212
213           b0->error = error0 ? node->errors[error0] : 0;
214           b1->error = error1 ? node->errors[error1] : 0;
215
216           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
217             {
218               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
219                                                         sizeof(*tr));
220               tr->next_index = next0;
221               tr->error = error0;
222               tr->h = lh0[0];
223             }
224
225           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
226             {
227               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1,
228                                                         sizeof(*tr));
229               tr->next_index = next1;
230               tr->error = error1;
231               tr->h = lh1[0];
232             }
233
234           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
235                                           n_left_to_next, bi0, bi1, next0,
236                                           next1);
237         }
238     
239       while (n_left_from > 0 && n_left_to_next > 0)
240         {
241           u32 bi0;
242           vlib_buffer_t * b0;
243           u32 next0;
244           ip4_udp_lisp_gpe_header_t * iul4_0;
245           ip6_udp_lisp_gpe_header_t * iul6_0;
246           lisp_gpe_header_t * lh0;
247           u32 error0;
248           uword * si0;
249
250           bi0 = from[0];
251           to_next[0] = bi0;
252           from += 1;
253           to_next += 1;
254           n_left_from -= 1;
255           n_left_to_next -= 1;
256
257           b0 = vlib_get_buffer (vm, bi0);
258
259           /* udp leaves current_data pointing at the lisp header */
260           if (is_v4)
261             {
262               vlib_buffer_advance (
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             {
274               vlib_buffer_advance (
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,
293                                                vlib_buffer_get_current (b0));
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
317           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
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     }
332   vlib_node_increment_counter (vm, lisp_gpe_ip4_input_node.index,
333                                LISP_GPE_ERROR_DECAPSULATED, 
334                                pkts_decapsulated);
335   return from_frame->n_vectors;
336 }
337
338 static uword
339 lisp_gpe_ip4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
340                     vlib_frame_t * from_frame)
341 {
342   return lisp_gpe_input_inline(vm, node, from_frame, 1);
343 }
344
345 static uword
346 lisp_gpe_ip6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
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,
354 #include <vnet/lisp-gpe/lisp_gpe_error.def>
355 #undef lisp_gpe_error
356 #undef _
357 };
358
359 VLIB_REGISTER_NODE (lisp_gpe_ip4_input_node) = {
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,
371     foreach_lisp_gpe_ip_input_next
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
380 VLIB_REGISTER_NODE (lisp_gpe_ip6_input_node) = {
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,
392     foreach_lisp_gpe_ip_input_next
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 };