a583666fcf4c8ae3a654774f420e2763f2194e78
[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, 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
213           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
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
222           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
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             {
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 };