Fix new LISP Coverity warnings
[vpp.git] / vnet / vnet / lisp-gpe / interface.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 <vppinfra/error.h>
17 #include <vppinfra/hash.h>
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/udp.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/lisp-gpe/lisp_gpe.h>
23
24 #define foreach_lisp_gpe_tx_next        \
25   _(DROP, "error-drop")                 \
26   _(IP4_LOOKUP, "ip4-lookup")           \
27   _(IP6_LOOKUP, "ip6-lookup")
28
29 typedef enum
30 {
31 #define _(sym,str) LISP_GPE_TX_NEXT_##sym,
32   foreach_lisp_gpe_tx_next
33 #undef _
34   LISP_GPE_TX_N_NEXT,
35 } lisp_gpe_tx_next_t;
36
37 typedef struct
38 {
39   u32 tunnel_index;
40 } lisp_gpe_tx_trace_t;
41
42 u8 *
43 format_lisp_gpe_tx_trace (u8 * s, va_list * args)
44 {
45   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47   lisp_gpe_tx_trace_t * t = va_arg (*args, lisp_gpe_tx_trace_t *);
48
49   s = format (s, "LISP-GPE-TX: tunnel %d", t->tunnel_index);
50   return s;
51 }
52
53 always_inline void
54 get_one_tunnel_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
55                        lisp_gpe_tunnel_t ** t0, u8 is_v4)
56 {
57   u32 adj_index0, tunnel_index0;
58   ip_adjacency_t * adj0;
59
60   /* Get adjacency and from it the tunnel_index */
61   adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
62
63   if (is_v4)
64     adj0 = ip_get_adjacency (lgm->lm4, adj_index0);
65   else
66     adj0 = ip_get_adjacency (lgm->lm6, adj_index0);
67
68   tunnel_index0 = adj0->if_address_index;
69   t0[0] = pool_elt_at_index(lgm->tunnels, tunnel_index0);
70
71   ASSERT(t0[0] != 0);
72 }
73
74 always_inline void
75 encap_one_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
76                   lisp_gpe_tunnel_t * t0, u32 * next0, u8 is_v4)
77 {
78   ASSERT(sizeof(ip4_udp_lisp_gpe_header_t) == 36);
79   ASSERT(sizeof(ip6_udp_lisp_gpe_header_t) == 56);
80
81   if (is_v4)
82     {
83       ip_udp_encap_one (lgm->vlib_main, b0, t0->rewrite, 36, 1);
84       next0[0] = LISP_GPE_TX_NEXT_IP4_LOOKUP;
85     }
86   else
87     {
88       ip_udp_encap_one (lgm->vlib_main, b0, t0->rewrite, 56, 0);
89       next0[0] = LISP_GPE_TX_NEXT_IP6_LOOKUP;
90     }
91
92   /* Reset to look up tunnel partner in the configured FIB */
93   vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
94 }
95
96 always_inline void
97 get_two_tunnels_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
98                         vlib_buffer_t * b1, lisp_gpe_tunnel_t ** t0,
99                         lisp_gpe_tunnel_t ** t1, u8 is_v4)
100 {
101   u32 adj_index0, adj_index1, tunnel_index0, tunnel_index1;
102   ip_adjacency_t * adj0, * adj1;
103
104   /* Get adjacency and from it the tunnel_index */
105   adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
106   adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
107
108   if (is_v4)
109     {
110       adj0 = ip_get_adjacency (lgm->lm4, adj_index0);
111       adj1 = ip_get_adjacency (lgm->lm4, adj_index1);
112     }
113   else
114     {
115       adj0 = ip_get_adjacency (lgm->lm6, adj_index0);
116       adj1 = ip_get_adjacency (lgm->lm6, adj_index1);
117     }
118
119   tunnel_index0 = adj0->if_address_index;
120   tunnel_index1 = adj1->if_address_index;
121
122   t0[0] = pool_elt_at_index(lgm->tunnels, tunnel_index0);
123   t1[0] = pool_elt_at_index(lgm->tunnels, tunnel_index1);
124
125   ASSERT(t0[0] != 0);
126   ASSERT(t1[0] != 0);
127 }
128
129 always_inline void
130 encap_two_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0, vlib_buffer_t * b1,
131                   lisp_gpe_tunnel_t * t0, lisp_gpe_tunnel_t * t1, u32 * next0,
132                   u32 * next1, u8 is_v4)
133 {
134   ASSERT(sizeof(ip4_udp_lisp_gpe_header_t) == 36);
135   ASSERT(sizeof(ip6_udp_lisp_gpe_header_t) == 56);
136
137   if (is_v4)
138     {
139       ip_udp_encap_one (lgm->vlib_main, b0, t0->rewrite, 36, 1);
140       ip_udp_encap_one (lgm->vlib_main, b1, t1->rewrite, 36, 1);
141       next0[0] = next1[0] = LISP_GPE_TX_NEXT_IP4_LOOKUP;
142     }
143   else
144     {
145       ip_udp_encap_one (lgm->vlib_main, b0, t0->rewrite, 56, 0);
146       ip_udp_encap_one (lgm->vlib_main, b1, t1->rewrite, 56, 0);
147       next0[0] = next1[0] = LISP_GPE_TX_NEXT_IP6_LOOKUP;
148     }
149
150   /* Reset to look up tunnel partner in the configured FIB */
151   vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
152   vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
153 }
154
155 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
156
157 static uword
158 lisp_gpe_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node,
159                        vlib_frame_t * from_frame)
160 {
161   u32 n_left_from, next_index, * from, * to_next;
162   lisp_gpe_main_t * lgm = &lisp_gpe_main;
163
164   from = vlib_frame_vector_args (from_frame);
165   n_left_from = from_frame->n_vectors;
166
167   next_index = node->cached_next_index;
168
169   while (n_left_from > 0)
170     {
171       u32 n_left_to_next;
172
173       vlib_get_next_frame (vm, node, next_index,
174                            to_next, n_left_to_next);
175
176       while (n_left_from >= 4 && n_left_to_next >= 2)
177         {
178           u32 bi0, bi1;
179           vlib_buffer_t * b0, * b1;
180           u32 next0, next1;
181           lisp_gpe_tunnel_t * t0 = 0, * t1 = 0;
182           u8 is_v4_eid0, is_v4_eid1;
183
184           next0 = next1 = LISP_GPE_TX_NEXT_IP4_LOOKUP;
185
186           /* Prefetch next iteration. */
187             {
188               vlib_buffer_t * p2, *p3;
189
190               p2 = vlib_get_buffer (vm, from[2]);
191               p3 = vlib_get_buffer (vm, from[3]);
192
193               vlib_prefetch_buffer_header(p2, LOAD);
194               vlib_prefetch_buffer_header(p3, LOAD);
195
196               CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
197               CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
198             }
199
200           bi0 = from[0];
201           bi1 = from[1];
202           to_next[0] = bi0;
203           to_next[1] = bi1;
204           from += 2;
205           to_next += 2;
206           n_left_to_next -= 2;
207           n_left_from -= 2;
208
209           b0 = vlib_get_buffer (vm, bi0);
210           b1 = vlib_get_buffer (vm, bi1);
211
212           is_v4_eid0 = is_v4_packet(vlib_buffer_get_current (b0));
213           is_v4_eid1 = is_v4_packet(vlib_buffer_get_current (b1));
214
215           if (PREDICT_TRUE(is_v4_eid0 == is_v4_eid1))
216             {
217               get_two_tunnels_inline (lgm, b0, b1, &t0, &t1,
218                                       is_v4_eid0 ? 1 : 0);
219             }
220           else
221             {
222               get_one_tunnel_inline (lgm, b0, &t0, is_v4_eid0 ? 1 : 0);
223               get_one_tunnel_inline (lgm, b1, &t1, is_v4_eid1 ? 1 : 0);
224             }
225
226           if (PREDICT_TRUE(
227               ip_addr_version(&t0->dst) == ip_addr_version(&t1->dst)))
228             {
229               encap_two_inline (lgm, b0, b1, t0, t1, &next0, &next1,
230                                 ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
231             }
232           else
233             {
234               encap_one_inline (lgm, b0, t0, &next0,
235                                 ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
236               encap_one_inline (lgm, b1, t1, &next1,
237                                 ip_addr_version(&t1->dst) == IP4 ? 1 : 0);
238             }
239
240           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
241             {
242               lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
243                                                            sizeof(*tr));
244               tr->tunnel_index = t0 - lgm->tunnels;
245             }
246           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
247             {
248               lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b1,
249                                                            sizeof(*tr));
250               tr->tunnel_index = t1 - lgm->tunnels;
251             }
252
253           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
254                                           n_left_to_next, bi0, bi1, next0,
255                                           next1);
256         }
257
258       while (n_left_from > 0 && n_left_to_next > 0)
259         {
260           vlib_buffer_t * b0;
261           u32 bi0, next0 = LISP_GPE_TX_NEXT_IP4_LOOKUP;
262           lisp_gpe_tunnel_t * t0 = 0;
263           u8 is_v4_0;
264
265           bi0 = from[0];
266           to_next[0] = bi0;
267           from += 1;
268           to_next += 1;
269           n_left_from -= 1;
270           n_left_to_next -= 1;
271
272           b0 = vlib_get_buffer (vm, bi0);
273
274           is_v4_0 = is_v4_packet(vlib_buffer_get_current (b0));
275           get_one_tunnel_inline (lgm, b0, &t0, is_v4_0 ? 1 : 0);
276
277           encap_one_inline (lgm, b0, t0, &next0,
278                             ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
279
280           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
281             {
282               lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
283                                                            sizeof(*tr));
284               tr->tunnel_index = t0 - lgm->tunnels;
285             }
286           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
287                                           n_left_to_next, bi0, next0);
288         }
289
290       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
291     }
292
293   return from_frame->n_vectors;
294 }
295
296 static u8 *
297 format_lisp_gpe_name (u8 * s, va_list * args)
298 {
299   u32 dev_instance = va_arg (*args, u32);
300   return format (s, "lisp_gpe%d", dev_instance);
301 }
302
303 VNET_DEVICE_CLASS (lisp_gpe_device_class,static) = {
304   .name = "LISP_GPE",
305   .format_device_name = format_lisp_gpe_name,
306   .format_tx_trace = format_lisp_gpe_tx_trace,
307   .tx_function = lisp_gpe_interface_tx,
308   .no_flatten_output_chains = 1,
309 };
310
311 static uword
312 dummy_set_rewrite (vnet_main_t * vnm, u32 sw_if_index, u32 l3_type,
313                    void * dst_address, void * rewrite, uword max_rewrite_bytes)
314 {
315   return 0;
316 }
317
318 u8 *
319 format_lisp_gpe_header_with_length (u8 * s, va_list * args)
320 {
321   lisp_gpe_header_t * h = va_arg (*args, lisp_gpe_header_t *);
322   u32 max_header_bytes = va_arg (*args, u32);
323   u32 header_bytes;
324
325   header_bytes = sizeof (h[0]);
326   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
327     return format (s, "lisp-gpe header truncated");
328
329   s = format (s, "flags: ");
330 #define _(n,v) if (h->flags & v) s = format (s, "%s ", #n);
331   foreach_lisp_gpe_flag_bit;
332 #undef _
333
334   s = format (s, "\n  ver_res %d res %d next_protocol %d iid %d(%x)",
335               h->ver_res, h->res, h->next_protocol,
336               clib_net_to_host_u32 (h->iid),
337               clib_net_to_host_u32 (h->iid));
338   return s;
339 }
340
341 VNET_HW_INTERFACE_CLASS (lisp_gpe_hw_class) = {
342   .name = "LISP_GPE",
343   .format_header = format_lisp_gpe_header_with_length,
344   .set_rewrite = dummy_set_rewrite,
345 };
346
347 int
348 add_del_ip_prefix_route (ip_prefix_t * dst_prefix, u32 table_id,
349                          ip_adjacency_t * add_adj, u8 is_add, u32 * adj_index)
350 {
351   uword * p;
352
353   if (ip_prefix_version(dst_prefix) == IP4)
354     {
355       ip4_main_t * im4 = &ip4_main;
356       ip4_add_del_route_args_t a;
357       ip4_address_t addr = ip_prefix_v4(dst_prefix);
358
359       memset(&a, 0, sizeof(a));
360       a.flags = IP4_ROUTE_FLAG_TABLE_ID;
361       a.table_index_or_table_id = table_id;
362       a.adj_index = ~0;
363       a.dst_address_length = ip_prefix_len(dst_prefix);
364       a.dst_address = addr;
365       a.flags |= is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL;
366       a.add_adj = add_adj;
367       a.n_add_adj = is_add ? 1 : 0;
368
369       ip4_add_del_route (im4, &a);
370
371       if (is_add)
372         {
373           p = ip4_get_route (im4, table_id, 0, addr.as_u8,
374                              ip_prefix_len(dst_prefix));
375           if (p == 0)
376             {
377               clib_warning("Failed to insert route for eid %U!",
378                            format_ip4_address_and_length, addr.as_u8,
379                            ip_prefix_len(dst_prefix));
380               return -1;
381             }
382           adj_index[0] = p[0];
383         }
384     }
385   else
386     {
387       ip6_main_t * im6 = &ip6_main;
388       ip6_add_del_route_args_t a;
389       ip6_address_t addr = ip_prefix_v6(dst_prefix);
390
391       memset(&a, 0, sizeof(a));
392       a.flags = IP6_ROUTE_FLAG_TABLE_ID;
393       a.table_index_or_table_id = table_id;
394       a.adj_index = ~0;
395       a.dst_address_length = ip_prefix_len(dst_prefix);
396       a.dst_address = addr;
397       a.flags |= is_add ? IP6_ROUTE_FLAG_ADD : IP6_ROUTE_FLAG_DEL;
398       a.add_adj = add_adj;
399       a.n_add_adj = is_add ? 1 : 0;
400
401       ip6_add_del_route (im6, &a);
402
403       if (is_add)
404         {
405           adj_index[0] = ip6_get_route (im6, table_id, 0, &addr,
406                                         ip_prefix_len(dst_prefix));
407           if (adj_index[0] == 0)
408             {
409               clib_warning("Failed to insert route for eid %U!",
410                            format_ip6_address_and_length, addr.as_u8,
411                            ip_prefix_len(dst_prefix));
412               return -1;
413             }
414         }
415     }
416   return 0;
417 }
418
419 static void
420 add_del_lisp_gpe_default_route (u32 table_id, u8 is_v4, u8 is_add)
421 {
422   lisp_gpe_main_t * lgm = &lisp_gpe_main;
423   ip_adjacency_t adj;
424   ip_prefix_t prefix;
425   u32 adj_index = 0;
426
427   /* setup adjacency */
428   memset (&adj, 0, sizeof(adj));
429
430   adj.n_adj = 1;
431   adj.explicit_fib_index = ~0;
432   adj.lookup_next_index = is_v4 ? lgm->ip4_lookup_next_lgpe_ip4_lookup :
433                                   lgm->ip6_lookup_next_lgpe_ip6_lookup;
434   /* default route has tunnel_index ~0 */
435   adj.rewrite_header.sw_if_index = ~0;
436
437   /* set prefix to 0/0 */
438   memset(&prefix, 0, sizeof(prefix));
439   ip_prefix_version(&prefix) = is_v4 ? IP4 : IP6;
440
441   /* add/delete route for prefix */
442   add_del_ip_prefix_route (&prefix, table_id, &adj, is_add, &adj_index);
443 }
444
445 static void
446 lisp_gpe_iface_set_table (u32 sw_if_index, u32 table_id, u8 is_ip4)
447 {
448   if (is_ip4)
449     {
450       ip4_main_t * im4 = &ip4_main;
451       ip4_fib_t * fib;
452       fib = find_ip4_fib_by_table_index_or_id (im4, table_id,
453                                                IP4_ROUTE_FLAG_TABLE_ID);
454
455       /* fib's created if it doesn't exist */
456       ASSERT(fib != 0);
457
458       vec_validate(im4->fib_index_by_sw_if_index, sw_if_index);
459       im4->fib_index_by_sw_if_index[sw_if_index] = fib->index;
460     }
461   else
462     {
463       ip6_main_t * im6 = &ip6_main;
464       ip6_fib_t * fib;
465       fib = find_ip6_fib_by_table_index_or_id (im6, table_id,
466                                                IP6_ROUTE_FLAG_TABLE_ID);
467
468       /* fib's created if it doesn't exist */
469       ASSERT(fib != 0);
470
471       vec_validate(im6->fib_index_by_sw_if_index, sw_if_index);
472       im6->fib_index_by_sw_if_index[sw_if_index] = fib->index;
473     }
474 }
475
476 #define foreach_l2_lisp_gpe_tx_next     \
477   _(DROP, "error-drop")                 \
478   _(IP4_LOOKUP, "ip4-lookup")           \
479   _(IP6_LOOKUP, "ip6-lookup")           \
480   _(LISP_CP_LOOKUP, "lisp-cp-lookup")
481
482 typedef enum
483 {
484 #define _(sym,str) L2_LISP_GPE_TX_NEXT_##sym,
485   foreach_l2_lisp_gpe_tx_next
486 #undef _
487   L2_LISP_GPE_TX_N_NEXT,
488 } l2_lisp_gpe_tx_next_t;
489
490 typedef struct
491 {
492   u32 tunnel_index;
493 } l2_lisp_gpe_tx_trace_t;
494
495 u8 *
496 format_l2_lisp_gpe_tx_trace (u8 * s, va_list * args)
497 {
498   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
499   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
500   l2_lisp_gpe_tx_trace_t * t = va_arg (*args, l2_lisp_gpe_tx_trace_t *);
501
502   s = format (s, "L2-LISP-GPE-TX: tunnel %d", t->tunnel_index);
503   return s;
504 }
505
506 always_inline void
507 l2_process_tunnel_action (vlib_buffer_t * b0, u8 action, u32 * next0)
508 {
509   if (LISP_SEND_MAP_REQUEST == action)
510     {
511       next0[0] = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
512       vnet_buffer(b0)->lisp.overlay_afi = LISP_AFI_MAC;
513     }
514   else
515     {
516       next0[0] = L2_LISP_GPE_TX_NEXT_DROP;
517     }
518 }
519
520 always_inline void
521 l2_process_one (lisp_gpe_main_t * lgm, vlib_buffer_t * b0, u32 ti0, u32 * next0)
522 {
523   lisp_gpe_tunnel_t * t0;
524
525   t0 = pool_elt_at_index(lgm->tunnels, ti0);
526   ASSERT(0 != t0);
527
528   if (PREDICT_TRUE(LISP_NO_ACTION == t0->action))
529     {
530       encap_one_inline (lgm, b0, t0, next0,
531                         ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
532     }
533   else
534     {
535       l2_process_tunnel_action(b0, t0->action, next0);
536     }
537 }
538
539 always_inline void
540 l2_process_two (lisp_gpe_main_t * lgm, vlib_buffer_t * b0, vlib_buffer_t * b1,
541                 u32 ti0, u32 ti1, u32 * next0, u32 * next1)
542 {
543   lisp_gpe_tunnel_t * t0, * t1;
544
545   t0 = pool_elt_at_index(lgm->tunnels, ti0);
546   t1 = pool_elt_at_index(lgm->tunnels, ti1);
547
548   ASSERT(0 != t0 && 0 != t1);
549
550   if (PREDICT_TRUE(LISP_NO_ACTION == t0->action
551                    && LISP_NO_ACTION == t1->action))
552     {
553       encap_two_inline (lgm, b0, b1, t0, t1, next0, next1,
554                         ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
555     }
556   else
557     {
558       if (LISP_NO_ACTION == t0->action)
559         {
560           encap_one_inline (lgm, b0, t0, next0,
561                             ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
562           l2_process_tunnel_action (b1, t1->action, next1);
563         }
564       else if (LISP_NO_ACTION == t1->action)
565         {
566           encap_one_inline (lgm, b1, t1, next1,
567                             ip_addr_version(&t1->dst) == IP4 ? 1 : 0);
568           l2_process_tunnel_action (b0, t0->action, next0);
569         }
570       else
571         {
572           l2_process_tunnel_action (b0, t0->action, next0);
573           l2_process_tunnel_action (b1, t1->action, next1);
574         }
575     }
576 }
577
578 static uword
579 l2_lisp_gpe_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node,
580                           vlib_frame_t * from_frame)
581 {
582   u32 n_left_from, next_index, * from, * to_next;
583   lisp_gpe_main_t * lgm = &lisp_gpe_main;
584
585   from = vlib_frame_vector_args (from_frame);
586   n_left_from = from_frame->n_vectors;
587
588   next_index = node->cached_next_index;
589
590   while (n_left_from > 0)
591     {
592       u32 n_left_to_next;
593
594       vlib_get_next_frame (vm, node, next_index,
595                            to_next, n_left_to_next);
596
597       while (n_left_from >= 4 && n_left_to_next >= 2)
598         {
599           u32 bi0, bi1;
600           vlib_buffer_t * b0, * b1;
601           u32 next0, next1, ti0, ti1;
602           lisp_gpe_tunnel_t * t0 = 0, * t1 = 0;
603           ethernet_header_t * e0, * e1;
604
605           next0 = next1 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
606
607           /* Prefetch next iteration. */
608             {
609               vlib_buffer_t * p2, *p3;
610
611               p2 = vlib_get_buffer (vm, from[2]);
612               p3 = vlib_get_buffer (vm, from[3]);
613
614               vlib_prefetch_buffer_header(p2, LOAD);
615               vlib_prefetch_buffer_header(p3, LOAD);
616
617               CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
618               CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
619             }
620
621           bi0 = from[0];
622           bi1 = from[1];
623           to_next[0] = bi0;
624           to_next[1] = bi1;
625           from += 2;
626           to_next += 2;
627           n_left_to_next -= 2;
628           n_left_from -= 2;
629
630           b0 = vlib_get_buffer (vm, bi0);
631           b1 = vlib_get_buffer (vm, bi1);
632
633           e0 = vlib_buffer_get_current (b0);
634           e1 = vlib_buffer_get_current (b1);
635
636           /* lookup dst + src mac */
637           ti0 = lisp_l2_fib_lookup (lgm, vnet_buffer(b0)->l2.bd_index,
638                                     e0->src_address, e0->dst_address);
639           ti1 = lisp_l2_fib_lookup (lgm, vnet_buffer(b1)->l2.bd_index,
640                                     e1->src_address, e1->dst_address);
641
642           if (PREDICT_TRUE((u32)~0 != ti0) && (u32)~0 != ti1)
643             {
644               /* process both tunnels */
645               l2_process_two (lgm, b0, b1, ti0, ti1, &next0, &next1);
646             }
647           else
648             {
649               if ((u32)~0 != ti0)
650                 {
651                   /* process tunnel for b0 */
652                   l2_process_one (lgm, b0, ti0, &next0);
653
654                   /* no tunnel found for b1, send to control plane */
655                   next1 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
656                   vnet_buffer(b1)->lisp.overlay_afi = LISP_AFI_MAC;
657                 }
658               else if ((u32)~0 != ti1)
659                 {
660                   /* process tunnel for b1 */
661                   l2_process_one (lgm, b1, ti1, &next1);
662
663                   /* no tunnel found b0, send to control plane */
664                   next0 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
665                   vnet_buffer(b0)->lisp.overlay_afi = LISP_AFI_MAC;
666                 }
667               else
668                 {
669                   /* no tunnels found */
670                   next0 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
671                   vnet_buffer(b0)->lisp.overlay_afi = LISP_AFI_MAC;
672                   next1 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
673                   vnet_buffer(b1)->lisp.overlay_afi = LISP_AFI_MAC;
674                 }
675             }
676
677           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
678             {
679               l2_lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
680                                                            sizeof(*tr));
681               tr->tunnel_index = t0 - lgm->tunnels;
682             }
683           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
684             {
685               l2_lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b1,
686                                                            sizeof(*tr));
687               tr->tunnel_index = t1 - lgm->tunnels;
688             }
689
690           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
691                                           n_left_to_next, bi0, bi1, next0,
692                                           next1);
693         }
694
695       while (n_left_from > 0 && n_left_to_next > 0)
696         {
697           vlib_buffer_t * b0;
698           u32 bi0, ti0, next0 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
699           ethernet_header_t * e0;
700
701           bi0 = from[0];
702           to_next[0] = bi0;
703           from += 1;
704           to_next += 1;
705           n_left_from -= 1;
706           n_left_to_next -= 1;
707
708           b0 = vlib_get_buffer (vm, bi0);
709           e0 = vlib_buffer_get_current (b0);
710
711           /* lookup dst + src mac */
712           ti0 = lisp_l2_fib_lookup (lgm, vnet_buffer(b0)->l2.bd_index,
713                                     e0->src_address, e0->dst_address);
714
715           if (PREDICT_TRUE((u32)~0 != ti0))
716             {
717               l2_process_one (lgm, b0, ti0, &next0);
718             }
719           else
720             {
721               /* no tunnel found send to control plane */
722               next0 = L2_LISP_GPE_TX_NEXT_LISP_CP_LOOKUP;
723               vnet_buffer(b0)->lisp.overlay_afi = LISP_AFI_MAC;
724             }
725
726           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
727             {
728               l2_lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
729                                                            sizeof(*tr));
730               tr->tunnel_index = ti0 ? ti0 : ~0;
731             }
732           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
733                                           n_left_to_next, bi0, next0);
734         }
735
736       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
737     }
738
739   return from_frame->n_vectors;
740 }
741
742 static u8 *
743 format_l2_lisp_gpe_name (u8 * s, va_list * args)
744 {
745   u32 dev_instance = va_arg (*args, u32);
746   return format (s, "l2_lisp_gpe%d", dev_instance);
747 }
748
749 VNET_DEVICE_CLASS (l2_lisp_gpe_device_class,static) = {
750   .name = "L2_LISP_GPE",
751   .format_device_name = format_l2_lisp_gpe_name,
752   .format_tx_trace = format_lisp_gpe_tx_trace,
753   .tx_function = l2_lisp_gpe_interface_tx,
754   .no_flatten_output_chains = 1,
755 };
756
757
758 static vnet_hw_interface_t *
759 create_lisp_gpe_iface (lisp_gpe_main_t * lgm, u32 vni, u32 dp_table,
760                        vnet_device_class_t * dev_class,
761                        tunnel_lookup_t * tuns)
762 {
763   u32 flen;
764   u32 hw_if_index = ~0;
765   u8 * new_name;
766   vnet_hw_interface_t * hi;
767   vnet_main_t * vnm = lgm->vnet_main;
768
769   /* create hw lisp_gpeX iface if needed, otherwise reuse existing */
770   flen = vec_len(lgm->free_tunnel_hw_if_indices);
771   if (flen > 0)
772     {
773       hw_if_index = lgm->free_tunnel_hw_if_indices[flen - 1];
774       _vec_len(lgm->free_tunnel_hw_if_indices) -= 1;
775
776       hi = vnet_get_hw_interface (vnm, hw_if_index);
777
778       /* rename interface */
779       new_name = format (0, "%U", dev_class->format_device_name,
780                          vni);
781
782       vec_add1(new_name, 0);
783       vnet_rename_interface (vnm, hw_if_index, (char *) new_name);
784       vec_free(new_name);
785
786       /* clear old stats of freed interface before reuse */
787       vnet_interface_main_t * im = &vnm->interface_main;
788       vnet_interface_counter_lock (im);
789       vlib_zero_combined_counter (
790           &im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
791           hi->sw_if_index);
792       vlib_zero_combined_counter (
793           &im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX],
794           hi->sw_if_index);
795       vlib_zero_simple_counter (
796           &im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP],
797           hi->sw_if_index);
798       vnet_interface_counter_unlock (im);
799     }
800   else
801     {
802       hw_if_index = vnet_register_interface (vnm, dev_class->index, vni,
803                                              lisp_gpe_hw_class.index, 0);
804       hi = vnet_get_hw_interface (vnm, hw_if_index);
805     }
806
807   hash_set(tuns->hw_if_index_by_dp_table, dp_table, hw_if_index);
808
809   /* set tunnel termination: post decap, packets are tagged as having been
810    * originated by lisp-gpe interface */
811   hash_set(tuns->sw_if_index_by_vni, vni, hi->sw_if_index);
812   hash_set(tuns->vni_by_sw_if_index, hi->sw_if_index, vni);
813
814   return hi;
815 }
816
817 static void
818 remove_lisp_gpe_iface (lisp_gpe_main_t * lgm, u32 hi_index, u32 dp_table,
819                        tunnel_lookup_t * tuns)
820 {
821   vnet_main_t * vnm = lgm->vnet_main;
822   vnet_hw_interface_t * hi;
823   uword * vnip;
824
825   hi = vnet_get_hw_interface (vnm, hi_index);
826
827   /* disable interface */
828   vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0/* down */);
829   vnet_hw_interface_set_flags (vnm, hi->hw_if_index, 0/* down */);
830   hash_unset(tuns->hw_if_index_by_dp_table, dp_table);
831   vec_add1(lgm->free_tunnel_hw_if_indices, hi->hw_if_index);
832
833   /* clean tunnel termination and vni to sw_if_index binding */
834   vnip = hash_get(tuns->vni_by_sw_if_index, hi->sw_if_index);
835   if (0 == vnip)
836     {
837       clib_warning ("No vni associated to interface %d", hi->sw_if_index);
838       return;
839     }
840   hash_unset(tuns->sw_if_index_by_vni, vnip[0]);
841   hash_unset(tuns->vni_by_sw_if_index, hi->sw_if_index);
842 }
843
844 static int
845 lisp_gpe_add_del_l3_iface (lisp_gpe_main_t * lgm,
846                            vnet_lisp_gpe_add_del_iface_args_t * a)
847 {
848   vnet_main_t * vnm = lgm->vnet_main;
849   tunnel_lookup_t * l3_ifaces = &lgm->l3_ifaces;
850   vnet_hw_interface_t * hi;
851   u32 lookup_next_index4, lookup_next_index6;
852   uword * hip, * si;
853
854   hip = hash_get(l3_ifaces->hw_if_index_by_dp_table, a->table_id);
855
856   if (a->is_add)
857     {
858       if (hip)
859         {
860           clib_warning ("vrf %d already mapped to a vni", a->table_id);
861           return -1;
862         }
863
864       si = hash_get(l3_ifaces->sw_if_index_by_vni, a->vni);
865       if (si)
866         {
867           clib_warning ("Interface for vni %d already exists", a->vni);
868           return -1;
869         }
870
871       /* create lisp iface and populate tunnel tables */
872       hi = create_lisp_gpe_iface (lgm, a->vni, a->table_id,
873                                   &lisp_gpe_device_class, l3_ifaces);
874
875       /* set ingress arc from lgpe_ipX_lookup */
876       lookup_next_index4 = vlib_node_add_next (lgm->vlib_main,
877                                                lgpe_ip4_lookup_node.index,
878                                                hi->output_node_index);
879       lookup_next_index6 = vlib_node_add_next (lgm->vlib_main,
880                                                lgpe_ip6_lookup_node.index,
881                                                hi->output_node_index);
882       hash_set(lgm->lgpe_ip4_lookup_next_index_by_table_id, a->table_id,
883                lookup_next_index4);
884       hash_set(lgm->lgpe_ip6_lookup_next_index_by_table_id, a->table_id,
885                lookup_next_index6);
886
887       /* insert default routes that point to lgpe-ipx-lookup */
888       add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */1, 1);
889       add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */0, 1);
890
891       /* set egress arcs */
892 #define _(sym,str) vlib_node_add_named_next_with_slot (vnm->vlib_main, \
893                     hi->tx_node_index, str, LISP_GPE_TX_NEXT_##sym);
894           foreach_lisp_gpe_tx_next
895 #undef _
896
897       /* set interface in appropriate v4 and v6 FIBs */
898       lisp_gpe_iface_set_table (hi->sw_if_index, a->table_id, 1);
899       lisp_gpe_iface_set_table (hi->sw_if_index, a->table_id, 0);
900
901       /* enable interface */
902       vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
903                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
904       vnet_hw_interface_set_flags (vnm, hi->hw_if_index,
905                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
906     }
907   else
908     {
909       if (hip == 0)
910         {
911           clib_warning("The interface for vrf %d doesn't exist", a->table_id);
912           return -1;
913         }
914
915       remove_lisp_gpe_iface (lgm, hip[0], a->table_id, &lgm->l3_ifaces);
916
917       /* unset default routes */
918       add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */1, 0);
919       add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */0, 0);
920     }
921
922   return 0;
923 }
924
925 static int
926 lisp_gpe_add_del_l2_iface (lisp_gpe_main_t * lgm,
927                            vnet_lisp_gpe_add_del_iface_args_t * a)
928 {
929   vnet_main_t * vnm = lgm->vnet_main;
930   tunnel_lookup_t * l2_ifaces = &lgm->l2_ifaces;
931   vnet_hw_interface_t * hi;
932   uword * hip, * si;
933   u16 bd_index;
934
935   bd_index = bd_find_or_add_bd_index(&bd_main, a->bd_id);
936   hip = hash_get(l2_ifaces->hw_if_index_by_dp_table, bd_index);
937
938   if (a->is_add)
939     {
940       if (hip)
941         {
942           clib_warning("bridge domain %d already mapped to a vni", a->bd_id);
943           return -1;
944         }
945
946       si = hash_get(l2_ifaces->sw_if_index_by_vni, a->vni);
947       if (si)
948         {
949           clib_warning ("Interface for vni %d already exists", a->vni);
950           return -1;
951         }
952
953       /* create lisp iface and populate tunnel tables */
954       hi = create_lisp_gpe_iface (lgm, a->vni, bd_index,
955                                   &l2_lisp_gpe_device_class, &lgm->l2_ifaces);
956
957       /* add iface to l2 bridge domain */
958       set_int_l2_mode (lgm->vlib_main, vnm, MODE_L2_BRIDGE, hi->sw_if_index,
959                        bd_index, 0, 0, 0);
960
961       /* set egress arcs */
962 #define _(sym,str) vlib_node_add_named_next_with_slot (vnm->vlib_main, \
963                     hi->tx_node_index, str, L2_LISP_GPE_TX_NEXT_##sym);
964           foreach_l2_lisp_gpe_tx_next
965 #undef _
966
967       /* enable interface */
968       vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
969                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
970       vnet_hw_interface_set_flags (vnm, hi->hw_if_index,
971                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
972     }
973   else
974     {
975       if (hip == 0)
976         {
977           clib_warning("The interface for bridge domain %d doesn't exist",
978                        a->bd_id);
979           return -1;
980         }
981       remove_lisp_gpe_iface (lgm, hip[0], bd_index, &lgm->l2_ifaces);
982     }
983
984   return 0;
985 }
986
987 int
988 vnet_lisp_gpe_add_del_iface (vnet_lisp_gpe_add_del_iface_args_t * a,
989                              u32 * hw_if_indexp)
990 {
991   lisp_gpe_main_t * lgm = &lisp_gpe_main;
992
993   if (vnet_lisp_gpe_enable_disable_status() == 0)
994     {
995       clib_warning ("LISP is disabled!");
996       return VNET_API_ERROR_LISP_DISABLED;
997     }
998
999   if (!a->is_l2)
1000     return lisp_gpe_add_del_l3_iface (lgm, a);
1001   else
1002     return lisp_gpe_add_del_l2_iface (lgm, a);
1003 }
1004
1005 static clib_error_t *
1006 lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
1007                                    vlib_cli_command_t * cmd)
1008 {
1009   unformat_input_t _line_input, * line_input = &_line_input;
1010   u8 is_add = 1;
1011   clib_error_t * error = 0;
1012   int rv = 0;
1013   u32 table_id, vni, bd_id;
1014   u8 vni_is_set = 0, vrf_is_set = 0, bd_index_is_set = 0;
1015
1016   vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
1017
1018   /* Get a line of input. */
1019   if (! unformat_user (input, unformat_line_input, line_input))
1020     return 0;
1021
1022   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1023     {
1024       if (unformat (line_input, "add"))
1025         is_add = 1;
1026       else if (unformat (line_input, "del"))
1027         is_add = 0;
1028       else if (unformat (line_input, "vrf %d", &table_id))
1029         {
1030           vrf_is_set = 1;
1031         }
1032       else if (unformat (line_input, "vni %d", &vni))
1033         {
1034           vni_is_set = 1;
1035         }
1036       else if (unformat (line_input, "bd %d", &bd_id))
1037         {
1038           bd_index_is_set = 1;
1039         }
1040       else
1041         {
1042           return clib_error_return (0, "parse error: '%U'",
1043                                    format_unformat_error, line_input);
1044         }
1045     }
1046
1047   if (vrf_is_set && bd_index_is_set)
1048     return clib_error_return(0, "Cannot set both vrf and brdige domain index!");
1049
1050   if (!vni_is_set)
1051     return clib_error_return(0, "vni must be set!");
1052
1053   if (!vrf_is_set && !bd_index_is_set)
1054     return clib_error_return(0, "vrf or bridge domain index must be set!");
1055
1056   a->is_add = is_add;
1057   a->dp_table = vrf_is_set ? table_id : bd_id;
1058   a->vni = vni;
1059   a->is_l2 = bd_index_is_set;
1060
1061   rv = vnet_lisp_gpe_add_del_iface (a, 0);
1062   if (0 != rv)
1063     {
1064       error = clib_error_return(0, "failed to %s gpe iface!",
1065                                 is_add ? "add" : "delete");
1066     }
1067
1068   return error;
1069 }
1070
1071 VLIB_CLI_COMMAND (add_del_lisp_gpe_iface_command, static) = {
1072   .path = "lisp gpe iface",
1073   .short_help = "lisp gpe iface add/del vni <vni> vrf <vrf>",
1074   .function = lisp_gpe_add_del_iface_command_fn,
1075 };