Fix compile errors reported by clang
[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 (vlib_main_t * vm, vlib_node_runtime_t * node,
82                 vlib_frame_t * from_frame)
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 * iul0, * iul1;
107           u32 next0, next1, error0 = 0, error1 = 0;
108           uword * si0, * si1;
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           /* map iid/vni to lisp-gpe sw_if_index which is used by ipx_input to
160            * decide the rx vrf and the input features to be applied */
161           si0 = hash_get(lgm->tunnel_term_sw_if_index_by_vni, iul0->lisp.iid);
162           si1 = hash_get(lgm->tunnel_term_sw_if_index_by_vni, iul1->lisp.iid);
163
164           if (si0)
165             {
166               vnet_buffer(b0)->sw_if_index[VLIB_RX] = si0[0];
167               pkts_decapsulated++;
168               error0 = 0;
169             }
170           else
171             {
172               next0 = LISP_GPE_INPUT_NEXT_DROP;
173               error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
174             }
175
176           if (si1)
177             {
178               vnet_buffer(b1)->sw_if_index[VLIB_RX] = si1[0];
179               pkts_decapsulated++;
180               error0 = 0;
181             }
182           else
183             {
184               next1 = LISP_GPE_INPUT_NEXT_DROP;
185               error1 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
186             }
187
188           b0->error = error0 ? node->errors[error0] : 0;
189           b1->error = error1 ? node->errors[error1] : 0;
190
191           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
192             {
193               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
194                                                         sizeof(*tr));
195               tr->next_index = next0;
196               tr->error = error0;
197               tr->h = iul0->lisp;
198             }
199
200           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
201             {
202               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1,
203                                                         sizeof(*tr));
204               tr->next_index = next1;
205               tr->error = error1;
206               tr->h = iul1->lisp;
207             }
208
209           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
210                                           n_left_to_next, bi0, bi1, next0,
211                                           next1);
212         }
213     
214       while (n_left_from > 0 && n_left_to_next > 0)
215         {
216           u32 bi0;
217           vlib_buffer_t * b0;
218           u32 next0;
219           ip4_udp_lisp_gpe_header_t * iul0;
220           u32 error0;
221           uword * si0;
222
223           bi0 = from[0];
224           to_next[0] = bi0;
225           from += 1;
226           to_next += 1;
227           n_left_from -= 1;
228           n_left_to_next -= 1;
229
230           b0 = vlib_get_buffer (vm, bi0);
231
232           /* udp leaves current_data pointing at the lisp header */
233           vlib_buffer_advance (b0, - IP_UDP_HDR_LEN);
234
235           iul0 = vlib_buffer_get_current (b0);
236
237           /* pop (ip, udp, lisp-gpe) */
238           vlib_buffer_advance (b0, sizeof (*iul0));
239
240           /* TODO if security is to be implemented, something similar to RPF,
241            * probably we'd like to check that the peer is allowed to send us
242            * packets. For this, we should use the tunnel table OR check that
243            * we have a mapping for the source eid and that the outer source of
244            * the packet is one of its locators */
245
246           /* determine next_index from lisp-gpe header */
247           next0 = next_protocol_to_next_index (&iul0->lisp,
248                                                vlib_buffer_get_current (b0));
249
250           /* Required to make the l2 tag push / pop code work on l2 subifs */
251           vnet_update_l2_len (b0);
252
253           /* map iid/vni to lisp-gpe sw_if_index which is used by ipx_input to
254            * decide the rx vrf and the input features to be applied */
255           si0 = hash_get(lgm->tunnel_term_sw_if_index_by_vni, iul0->lisp.iid);
256
257           if (si0)
258             {
259               vnet_buffer(b0)->sw_if_index[VLIB_RX] = si0[0];
260               pkts_decapsulated++;
261               error0 = 0;
262             }
263           else
264             {
265               next0 = LISP_GPE_INPUT_NEXT_DROP;
266               error0 = LISP_GPE_ERROR_NO_SUCH_TUNNEL;
267             }
268
269           /* TODO error handling if security is implemented */
270           b0->error = error0 ? node->errors[error0] : 0;
271
272           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
273             {
274               lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
275                                                         sizeof(*tr));
276               tr->next_index = next0;
277               tr->error = error0;
278               tr->h = iul0->lisp;
279             }
280
281           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
282                                           n_left_to_next, bi0, next0);
283         }
284
285       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
286     }
287   vlib_node_increment_counter (vm, lisp_gpe_input_node.index,
288                                LISP_GPE_ERROR_DECAPSULATED, 
289                                pkts_decapsulated);
290   return from_frame->n_vectors;
291 }
292
293 static char * lisp_gpe_error_strings[] = {
294 #define lisp_gpe_error(n,s) s,
295 #include <vnet/lisp-gpe/lisp_gpe_error.def>
296 #undef lisp_gpe_error
297 #undef _
298 };
299
300 VLIB_REGISTER_NODE (lisp_gpe_input_node) = {
301   .function = lisp_gpe_input,
302   .name = "lisp-gpe-input",
303   /* Takes a vector of packets. */
304   .vector_size = sizeof (u32),
305
306   .n_errors = LISP_GPE_N_ERROR,
307   .error_strings = lisp_gpe_error_strings,
308
309   .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
310   .next_nodes = {
311 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
312     foreach_lisp_gpe_input_next
313 #undef _
314   },
315
316   .format_buffer = format_lisp_gpe_header_with_length,
317   .format_trace = format_lisp_gpe_rx_trace,
318   // $$$$ .unformat_buffer = unformat_lisp_gpe_header,
319 };