LISP GPE: initial CP commit and DP improvements
[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     LISP_GPE_INPUT_NEXT_DROP
57 };
58
59 static u32
60 next_protocol_to_next_index (lisp_gpe_header_t * lgh, u8 * next_header)
61 {
62   /* legay lisp router */
63   if (PREDICT_FALSE((lgh->flags & LISP_GPE_FLAGS_P) == 0))
64     {
65       ip4_header_t * iph = (ip4_header_t *) next_header;
66       if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
67         return LISP_GPE_INPUT_NEXT_IP4_INPUT;
68       else if ((iph->ip_version_and_header_length & 0xF0) == 0x60)
69         return LISP_GPE_INPUT_NEXT_IP6_INPUT;
70       else
71         return LISP_GPE_INPUT_NEXT_DROP;
72     }
73   /* lisp-gpe router */
74   else if ((lgh->flags & LISP_GPE_FLAGS_P)
75       && lgh->next_protocol < LISP_GPE_NEXT_PROTOS)
76     return next_proto_to_next_index[lgh->next_protocol];
77   else
78     return LISP_GPE_INPUT_NEXT_DROP;
79 }
80
81 static uword
82 lisp_gpe_input (vlib_main_t * vm, vlib_node_runtime_t * node,
83                 vlib_frame_t * from_frame)
84 {
85   u32 n_left_from, next_index, * from, * to_next;
86   lisp_gpe_tunnel_key_t last_key;
87   u32 pkts_decapsulated = 0;
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 * iul0, * iul1;
107           u32 error0, error1;
108           u32 next0, next1;
109
110           next0 = next1 = LISP_GPE_INPUT_NEXT_IP4_INPUT;
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           vlib_buffer_advance (b0, - IP_UDP_HDR_LEN);
140           vlib_buffer_advance (b1, - IP_UDP_HDR_LEN);
141
142           iul0 = vlib_buffer_get_current (b0);
143           iul1 = vlib_buffer_get_current (b1);
144
145           /* pop (ip, udp, lisp-gpe) */
146           vlib_buffer_advance (b0, sizeof (*iul0));
147           vlib_buffer_advance (b1, sizeof (*iul1));
148
149           /* determine next_index from lisp-gpe header */
150           next0 = next_protocol_to_next_index (&iul0->lisp,
151                                                vlib_buffer_get_current (b0));
152           next1 = next_protocol_to_next_index (&iul1->lisp,
153                                                vlib_buffer_get_current (b1));
154
155           /* Required to make the l2 tag push / pop code work on l2 subifs */
156           vnet_update_l2_len (b0);
157           vnet_update_l2_len (b1);
158
159           /* TODO hash to map iid to fib */
160           vnet_buffer(b0)->sw_if_index[VLIB_TX] = iul0->lisp.iid;
161           vnet_buffer(b1)->sw_if_index[VLIB_TX] = iul1->lisp.iid;
162
163           pkts_decapsulated += 2;
164
165           /* TODO error handling if security is implemented */
166           error0 = error1 = 0;
167           b0->error = error0 ? node->errors[error0] : 0;
168           b1->error = error1 ? node->errors[error1] : 0;
169
170           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
171             {
172               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
173                                                         sizeof(*tr));
174               tr->next_index = next0;
175               tr->error = error0;
176               tr->h = iul0->lisp;
177             }
178
179           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
180             {
181               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1,
182                                                         sizeof(*tr));
183               tr->next_index = next1;
184               tr->error = error1;
185               tr->h = iul1->lisp;
186             }
187
188           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
189                                           n_left_to_next, bi0, bi1, next0,
190                                           next1);
191         }
192     
193       while (n_left_from > 0 && n_left_to_next > 0)
194         {
195           u32 bi0;
196           vlib_buffer_t * b0;
197           u32 next0;
198           ip4_udp_lisp_gpe_header_t * iul0;
199           u32 error0;
200
201           bi0 = from[0];
202           to_next[0] = bi0;
203           from += 1;
204           to_next += 1;
205           n_left_from -= 1;
206           n_left_to_next -= 1;
207
208           b0 = vlib_get_buffer (vm, bi0);
209
210           /* udp leaves current_data pointing at the lisp header */
211           vlib_buffer_advance (b0, - IP_UDP_HDR_LEN);
212
213           iul0 = vlib_buffer_get_current (b0);
214
215           /* pop (ip, udp, lisp-gpe) */
216           vlib_buffer_advance (b0, sizeof (*iul0));
217
218           /* TODO if security is to be implemented, something similar to RPF,
219            * probably we'd like to check that the peer is allowed to send us
220            * packets. For this, we should use the tunnel table OR check that
221            * we have a mapping for the source eid and that the outer source of
222            * the packet is one of its locators */
223
224           /* determine next_index from lisp-gpe header */
225           next0 = next_protocol_to_next_index (&iul0->lisp,
226                                                vlib_buffer_get_current (b0));
227
228           /* Required to make the l2 tag push / pop code work on l2 subifs */
229           vnet_update_l2_len (b0);
230
231           /* TODO hash to map iid to fib */
232           vnet_buffer(b0)->sw_if_index[VLIB_TX] = iul0->lisp.iid;
233           pkts_decapsulated ++;
234
235           /* TODO error handling if security is implemented */
236           error0 = 0;
237           b0->error = error0 ? node->errors[error0] : 0;
238
239           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
240             {
241               lisp_gpe_rx_trace_t *tr 
242                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
243               tr->next_index = next0;
244               tr->error = error0;
245               tr->h = iul0->lisp;
246             }
247
248           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
249                                           n_left_to_next, bi0, next0);
250         }
251
252       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
253     }
254   vlib_node_increment_counter (vm, lisp_gpe_input_node.index,
255                                LISP_GPE_ERROR_DECAPSULATED, 
256                                pkts_decapsulated);
257   return from_frame->n_vectors;
258 }
259
260 static char * lisp_gpe_error_strings[] = {
261 #define lisp_gpe_error(n,s) s,
262 #include <vnet/lisp-gpe/lisp_gpe_error.def>
263 #undef lisp_gpe_error
264 #undef _
265 };
266
267 VLIB_REGISTER_NODE (lisp_gpe_input_node) = {
268   .function = lisp_gpe_input,
269   .name = "lisp-gpe-input",
270   /* Takes a vector of packets. */
271   .vector_size = sizeof (u32),
272
273   .n_errors = LISP_GPE_N_ERROR,
274   .error_strings = lisp_gpe_error_strings,
275
276   .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
277   .next_nodes = {
278 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
279     foreach_lisp_gpe_input_next
280 #undef _
281   },
282
283   .format_buffer = format_lisp_gpe_header_with_length,
284   .format_trace = format_lisp_gpe_rx_trace,
285   // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
286 };