Add vxlan-bypass feature to IP6 forwarding path
[vpp.git] / src / vnet / vxlan / vxlan.c
1 /*
2  * Copyright (c) 2015 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 #include <vnet/vxlan/vxlan.h>
16 #include <vnet/ip/format.h>
17 #include <vnet/fib/fib_entry.h>
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/mfib/mfib_table.h>
20 #include <vnet/adj/adj_mcast.h>
21 #include <vlib/vlib.h>
22
23 /**
24  * @file
25  * @brief VXLAN.
26  *
27  * VXLAN provides the features needed to allow L2 bridge domains (BDs)
28  * to span multiple servers. This is done by building an L2 overlay on
29  * top of an L3 network underlay using VXLAN tunnels.
30  *
31  * This makes it possible for servers to be co-located in the same data
32  * center or be separated geographically as long as they are reachable
33  * through the underlay L3 network.
34  *
35  * You can refer to this kind of L2 overlay bridge domain as a VXLAN
36  * (Virtual eXtensible VLAN) segment.
37  */
38
39
40 vxlan_main_t vxlan_main;
41
42 static u8 * format_decap_next (u8 * s, va_list * args)
43 {
44   u32 next_index = va_arg (*args, u32);
45
46   switch (next_index)
47     {
48     case VXLAN_INPUT_NEXT_DROP:
49       return format (s, "drop");
50     case VXLAN_INPUT_NEXT_L2_INPUT:
51       return format (s, "l2");
52     default:
53       return format (s, "index %d", next_index);
54     }
55   return s;
56 }
57
58 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
59 {
60   vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
61   vxlan_main_t * ngm = &vxlan_main;
62
63   s = format (s, "[%d] src %U dst %U vni %d sw_if_index %d ",
64               t - ngm->tunnels,
65               format_ip46_address, &t->src, IP46_TYPE_ANY,
66               format_ip46_address, &t->dst, IP46_TYPE_ANY,
67               t->vni, t->sw_if_index);
68
69   if (ip46_address_is_multicast (&t->dst))
70     s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
71
72   s = format (s, "encap_fib_index %d fib_entry_index %d decap_next %U\n", 
73               t->encap_fib_index, t->fib_entry_index,
74               format_decap_next, t->decap_next_index);
75   return s;
76 }
77
78 static u8 * format_vxlan_name (u8 * s, va_list * args)
79 {
80   u32 dev_instance = va_arg (*args, u32);
81   return format (s, "vxlan_tunnel%d", dev_instance);
82 }
83
84 static uword dummy_interface_tx (vlib_main_t * vm,
85                                  vlib_node_runtime_t * node,
86                                  vlib_frame_t * frame)
87 {
88   clib_warning ("you shouldn't be here, leaking buffers...");
89   return frame->n_vectors;
90 }
91
92 static clib_error_t *
93 vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
94 {
95   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
96     vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
97   else
98     vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
99
100   return /* no error */ 0;
101 }
102
103 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
104   .name = "VXLAN",
105   .format_device_name = format_vxlan_name,
106   .format_tx_trace = format_vxlan_encap_trace,
107   .tx_function = dummy_interface_tx,
108   .admin_up_down_function = vxlan_interface_admin_up_down,
109 };
110
111 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
112 {
113   u32 dev_instance = va_arg (*args, u32);
114   s = format (s, "unimplemented dev %u", dev_instance);
115   return s;
116 }
117
118 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
119   .name = "VXLAN",
120   .format_header = format_vxlan_header_with_length,
121   .build_rewrite = default_build_rewrite,
122 };
123
124 static void
125 vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t)
126 {
127     dpo_id_t dpo = DPO_INVALID;
128     u32 encap_index = ip46_address_is_ip4(&t->dst) ?
129         vxlan4_encap_node.index : vxlan6_encap_node.index;
130     fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->dst) ?
131         FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
132
133     fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
134     dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
135     dpo_reset(&dpo);
136 }
137
138 static vxlan_tunnel_t *
139 vxlan_tunnel_from_fib_node (fib_node_t *node)
140 {
141 #if (CLIB_DEBUG > 0)
142     ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type);
143 #endif
144     return ((vxlan_tunnel_t*) (((char*)node) -
145                                STRUCT_OFFSET_OF(vxlan_tunnel_t, node)));
146 }
147
148 /**
149  * Function definition to backwalk a FIB node -
150  * Here we will restack the new dpo of VXLAN DIP to encap node.
151  */
152 static fib_node_back_walk_rc_t
153 vxlan_tunnel_back_walk (fib_node_t *node,
154                         fib_node_back_walk_ctx_t *ctx)
155 {
156     vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node));
157     return (FIB_NODE_BACK_WALK_CONTINUE);
158 }
159
160 /**
161  * Function definition to get a FIB node from its index
162  */
163 static fib_node_t*
164 vxlan_tunnel_fib_node_get (fib_node_index_t index)
165 {
166     vxlan_tunnel_t * t;
167     vxlan_main_t * vxm = &vxlan_main;
168
169     t = pool_elt_at_index(vxm->tunnels, index);
170
171     return (&t->node);
172 }
173
174 /**
175  * Function definition to inform the FIB node that its last lock has gone.
176  */
177 static void
178 vxlan_tunnel_last_lock_gone (fib_node_t *node)
179 {
180     /*
181      * The VXLAN tunnel is a root of the graph. As such
182      * it never has children and thus is never locked.
183      */
184     ASSERT(0);
185 }
186
187 /*
188  * Virtual function table registered by VXLAN tunnels
189  * for participation in the FIB object graph.
190  */
191 const static fib_node_vft_t vxlan_vft = {
192     .fnv_get = vxlan_tunnel_fib_node_get,
193     .fnv_last_lock = vxlan_tunnel_last_lock_gone,
194     .fnv_back_walk = vxlan_tunnel_back_walk,
195 };
196
197
198 #define foreach_copy_field                      \
199 _(vni)                                          \
200 _(mcast_sw_if_index)                            \
201 _(encap_fib_index)                              \
202 _(decap_next_index)                             \
203 _(src)                                          \
204 _(dst)
205
206 static int vxlan4_rewrite (vxlan_tunnel_t * t)
207 {
208   u8 *rw = 0;
209   ip4_header_t * ip0;
210   ip4_vxlan_header_t * h0;
211   int len = sizeof (*h0);
212
213   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
214
215   h0 = (ip4_vxlan_header_t *) rw;
216
217   /* Fixed portion of the (outer) ip4 header */
218   ip0 = &h0->ip4;
219   ip0->ip_version_and_header_length = 0x45;
220   ip0->ttl = 254;
221   ip0->protocol = IP_PROTOCOL_UDP;
222
223   /* we fix up the ip4 header length and checksum after-the-fact */
224   ip0->src_address.as_u32 = t->src.ip4.as_u32;
225   ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
226   ip0->checksum = ip4_header_checksum (ip0);
227
228   /* UDP header, randomize src port on something, maybe? */
229   h0->udp.src_port = clib_host_to_net_u16 (4789);
230   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
231
232   /* VXLAN header */
233   vnet_set_vni_and_flags(&h0->vxlan, t->vni);
234
235   t->rewrite = rw;
236   return (0);
237 }
238
239 static int vxlan6_rewrite (vxlan_tunnel_t * t)
240 {
241   u8 *rw = 0;
242   ip6_header_t * ip0;
243   ip6_vxlan_header_t * h0;
244   int len = sizeof (*h0);
245
246   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
247
248   h0 = (ip6_vxlan_header_t *) rw;
249
250   /* Fixed portion of the (outer) ip6 header */
251   ip0 = &h0->ip6;
252   ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
253   ip0->hop_limit = 255;
254   ip0->protocol = IP_PROTOCOL_UDP;
255
256   ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
257   ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
258   ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
259   ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
260
261   /* UDP header, randomize src port on something, maybe? */
262   h0->udp.src_port = clib_host_to_net_u16 (4789);
263   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
264
265   /* VXLAN header */
266   vnet_set_vni_and_flags(&h0->vxlan, t->vni);
267
268   t->rewrite = rw;
269   return (0);
270 }
271
272 static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index)
273 {
274   vlib_main_t * vm = vxm->vlib_main;
275   vlib_node_runtime_t *r;
276
277   if(!is_ip6)
278     {
279       r = vlib_node_get_runtime (vm, vxlan4_input_node.index);
280       if(decap_next_index >= r->n_next_nodes)
281         return 1;
282     }
283   else
284     {
285       r = vlib_node_get_runtime (vm, vxlan6_input_node.index);
286       if(decap_next_index >= r->n_next_nodes)
287         return 1;
288     }
289
290   return 0;
291 }
292
293 static void
294 hash_set_key_copy (uword ** h, void * key, uword v) {
295         size_t ksz = hash_header(*h)->user;
296         void * copy = clib_mem_alloc (ksz);
297         clib_memcpy (copy, key, ksz);
298         hash_set_mem (*h, copy, v);
299 }
300
301 static void
302 hash_unset_key_free (uword ** h, void * key) {
303         hash_pair_t * hp = hash_get_pair_mem (*h, key);
304         ASSERT (hp);
305         key = uword_to_pointer (hp->key, void *);
306         hash_unset_mem (*h, key);
307         clib_mem_free (key);
308 }
309
310 static uword
311 vtep_addr_ref(ip46_address_t *ip)
312 {
313         uword *vtep = ip46_address_is_ip4(ip) ?
314                          hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
315                          hash_get_mem (vxlan_main.vtep6, &ip->ip6);
316         if (vtep) 
317                 return ++(*vtep);
318         ip46_address_is_ip4(ip) ?
319                          hash_set (vxlan_main.vtep4, ip->ip4.as_u32, 1) :
320                          hash_set_key_copy (&vxlan_main.vtep6, &ip->ip6, 1);
321         return 1;
322 }
323
324 static uword
325 vtep_addr_unref(ip46_address_t *ip)
326 {
327         uword *vtep = ip46_address_is_ip4(ip) ?
328                          hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
329                          hash_get_mem (vxlan_main.vtep6, &ip->ip6);
330         ASSERT(vtep);
331         if (--(*vtep) != 0)
332                 return *vtep;
333         ip46_address_is_ip4(ip) ?
334                 hash_unset (vxlan_main.vtep4, ip->ip4.as_u32) :
335                 hash_unset_key_free (&vxlan_main.vtep6, &ip->ip6);
336         return 0;
337 }
338
339 typedef CLIB_PACKED(union {
340   struct {
341     fib_node_index_t mfib_entry_index;
342     adj_index_t mcast_adj_index;
343   };
344   u64 as_u64;
345 }) mcast_shared_t;
346
347 static inline mcast_shared_t
348 mcast_shared_get(ip46_address_t * ip)
349 {
350         ASSERT(ip46_address_is_multicast(ip));
351         uword * p = hash_get_mem (vxlan_main.mcast_shared, ip);
352         ASSERT(p);
353         return (mcast_shared_t) { .as_u64 = *p };
354 }
355
356 static inline void
357 mcast_shared_add(ip46_address_t *dst,
358                  fib_node_index_t mfei,
359                  adj_index_t ai)
360 {
361     mcast_shared_t new_ep = {
362         .mcast_adj_index = ai,
363         .mfib_entry_index = mfei,
364     };
365
366     hash_set_key_copy (&vxlan_main.mcast_shared, dst, new_ep.as_u64);
367 }
368
369 static inline void
370 mcast_shared_remove(ip46_address_t *dst)
371 {
372     mcast_shared_t ep = mcast_shared_get(dst);
373
374     adj_unlock(ep.mcast_adj_index);
375     mfib_table_entry_delete_index(ep.mfib_entry_index,
376                                   MFIB_SOURCE_VXLAN);
377
378     hash_unset_key_free (&vxlan_main.mcast_shared, dst);
379 }
380
381 int vnet_vxlan_add_del_tunnel 
382 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
383 {
384   vxlan_main_t * vxm = &vxlan_main;
385   vxlan_tunnel_t *t = 0;
386   vnet_main_t * vnm = vxm->vnet_main;
387   uword * p;
388   u32 hw_if_index = ~0;
389   u32 sw_if_index = ~0;
390   int rv;
391   vxlan4_tunnel_key_t key4;
392   vxlan6_tunnel_key_t key6;
393   u32 is_ip6 = a->is_ip6;
394
395   if (!is_ip6)
396     {
397       key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
398       key4.vni = clib_host_to_net_u32 (a->vni << 8);
399       p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
400     } 
401   else 
402     {
403       key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
404       key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
405       key6.vni = clib_host_to_net_u32 (a->vni << 8);
406       p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
407     }
408   
409   if (a->is_add)
410     {
411       l2input_main_t * l2im = &l2input_main;
412
413       /* adding a tunnel: tunnel must not already exist */
414       if (p)
415         return VNET_API_ERROR_TUNNEL_EXIST;
416
417       /*if not set explicitly, default to l2 */
418       if(a->decap_next_index == ~0)
419         a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
420       if (vxlan_check_decap_next(vxm, is_ip6, a->decap_next_index))
421           return VNET_API_ERROR_INVALID_DECAP_NEXT;
422
423       pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
424       memset (t, 0, sizeof (*t));
425       
426       /* copy from arg structure */
427 #define _(x) t->x = a->x;
428       foreach_copy_field;
429 #undef _
430
431       if (!is_ip6) 
432         rv = vxlan4_rewrite (t);
433       else
434         rv = vxlan6_rewrite (t);
435
436       if (rv)
437         {
438           pool_put (vxm->tunnels, t);
439           return rv;
440         }
441
442       /* copy the key */
443       if (is_ip6)
444         hash_set_key_copy (&vxm->vxlan6_tunnel_by_key, &key6, t - vxm->tunnels);
445       else
446         hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
447
448       vnet_hw_interface_t * hi;
449       if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
450         {
451           vnet_interface_main_t * im = &vnm->interface_main;
452           hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
453             [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
454           _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
455           
456           hi = vnet_get_hw_interface (vnm, hw_if_index);
457           hi->dev_instance = t - vxm->tunnels;
458           hi->hw_instance = hi->dev_instance;
459
460           /* clear old stats of freed tunnel before reuse */
461           sw_if_index = hi->sw_if_index;
462           vnet_interface_counter_lock(im);
463           vlib_zero_combined_counter 
464             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
465           vlib_zero_combined_counter 
466             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
467           vlib_zero_simple_counter 
468             (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
469           vnet_interface_counter_unlock(im);
470         }
471       else 
472         {
473           hw_if_index = vnet_register_interface
474             (vnm, vxlan_device_class.index, t - vxm->tunnels,
475              vxlan_hw_class.index, t - vxm->tunnels);
476           hi = vnet_get_hw_interface (vnm, hw_if_index);
477         }
478       
479       t->hw_if_index = hw_if_index;
480       t->sw_if_index = sw_if_index = hi->sw_if_index;
481       
482       vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
483       vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
484
485       /* setup l2 input config with l2 feature and bd 0 to drop packet */
486       vec_validate (l2im->configs, sw_if_index);
487       l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
488       l2im->configs[sw_if_index].bd_index = 0;
489       
490       vnet_sw_interface_set_flags (vnm, sw_if_index, 
491                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
492       fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL);
493       fib_prefix_t tun_dst_pfx;
494       u32 encap_index = !is_ip6 ?
495           vxlan4_encap_node.index : vxlan6_encap_node.index;
496       vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
497
498       fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
499       if (!ip46_address_is_multicast(&t->dst))
500         {
501           /* Unicast tunnel -
502            * source the FIB entry for the tunnel's destination
503            * and become a child thereof. The tunnel will then get poked
504            * when the forwarding for the entry updates, and the tunnel can
505            * re-stack accordingly
506            */
507           vtep_addr_ref(&t->src);
508           t->fib_entry_index = fib_table_entry_special_add
509             (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
510              FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID);
511           t->sibling_index = fib_entry_child_add
512             (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels);
513           vxlan_tunnel_restack_dpo(t);
514         } 
515       else
516         {
517           /* Multicast tunnel -
518            * as the same mcast group can be used for mutiple mcast tunnels
519            * with different VNIs, create the output fib adjecency only if
520            * it does not already exist
521            */
522           fib_protocol_t fp = (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
523           dpo_id_t dpo = DPO_INVALID;
524           mcast_shared_t ep;
525
526           if (vtep_addr_ref(&t->dst) == 1)
527           {
528               fib_node_index_t mfei;
529               adj_index_t ai;
530               fib_route_path_t path = {
531                   .frp_proto = fp,
532                   .frp_addr = zero_addr,
533                   .frp_sw_if_index = 0xffffffff,
534                   .frp_fib_index = ~0,
535                   .frp_weight = 0,
536                   .frp_flags = FIB_ROUTE_PATH_LOCAL,
537               };
538               const mfib_prefix_t mpfx = {
539                   .fp_proto = fp,
540                   .fp_len = (is_ip6 ? 128 : 32),
541                   .fp_grp_addr = tun_dst_pfx.fp_addr,
542               };
543
544               /*
545                * Setup the (*,G) to receive traffic on the mcast group
546                *  - the forwarding interface is for-us
547                *  - the accepting interface is that from the API
548                */
549               mfib_table_entry_path_update(t->encap_fib_index,
550                                            &mpfx,
551                                            MFIB_SOURCE_VXLAN,
552                                            &path,
553                                            MFIB_ITF_FLAG_FORWARD);
554
555               path.frp_sw_if_index = a->mcast_sw_if_index;
556               path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
557               mfei = mfib_table_entry_path_update(t->encap_fib_index,
558                                                   &mpfx,
559                                                   MFIB_SOURCE_VXLAN,
560                                                   &path,
561                                                   MFIB_ITF_FLAG_ACCEPT);
562
563               /*
564                * Create the mcast adjacency to send traffic to the group
565                */
566               ai = adj_mcast_add_or_lock(fp,
567                                          fib_proto_to_link(fp),
568                                          a->mcast_sw_if_index);
569
570               /*
571                * create a new end-point
572                */
573               mcast_shared_add(&t->dst, mfei, ai);
574           }
575
576           ep = mcast_shared_get(&t->dst);
577
578           /* Stack shared mcast dst mac addr rewrite on encap */
579           dpo_set (&dpo, DPO_ADJACENCY,
580                    fib_proto_to_dpo(fp),
581                    ep.mcast_adj_index);
582
583           dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
584           dpo_reset (&dpo);
585           flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
586         }
587
588       /* Set vxlan tunnel output node */
589       hi->output_node_index = encap_index;
590
591       vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
592     }
593   else
594     {
595       /* deleting a tunnel: tunnel must exist */
596       if (!p)
597         return VNET_API_ERROR_NO_SUCH_ENTRY;
598
599       t = pool_elt_at_index (vxm->tunnels, p[0]);
600
601       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
602       /* make sure tunnel is removed from l2 bd or xconnect */
603       set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
604       vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
605
606       vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
607
608       if (!is_ip6)
609         hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
610       else
611         hash_unset_key_free (&vxm->vxlan6_tunnel_by_key, &key6);
612
613       if (!ip46_address_is_multicast(&t->dst))
614         {
615           vtep_addr_unref(&t->src);
616           fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
617           fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
618         }
619       else if (vtep_addr_unref(&t->dst) == 0)
620         {
621           mcast_shared_remove(&t->dst);
622         }
623
624       fib_node_deinit(&t->node);
625       vec_free (t->rewrite);
626       pool_put (vxm->tunnels, t);
627     }
628
629   if (sw_if_indexp)
630       *sw_if_indexp = sw_if_index;
631
632   return 0;
633 }
634
635 static u32 fib4_index_from_fib_id (u32 fib_id)
636 {
637   ip4_main_t * im = &ip4_main;
638   uword * p;
639
640   p = hash_get (im->fib_index_by_table_id, fib_id);
641   if (!p)
642     return ~0;
643
644   return p[0];
645 }
646
647 static u32 fib6_index_from_fib_id (u32 fib_id)
648 {
649   ip6_main_t * im = &ip6_main;
650   uword * p;
651
652   p = hash_get (im->fib_index_by_table_id, fib_id);
653   if (!p)
654     return ~0;
655
656   return p[0];
657 }
658
659 static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set)
660 {
661   vxlan_main_t * vxm = &vxlan_main;
662   vlib_main_t * vm = vxm->vlib_main;
663   uword next_index = ~0;
664
665   if (ipv4_set)
666     {
667       next_index = vlib_node_add_next (vm, vxlan4_input_node.index, node_index);
668     }
669   else
670     {
671       next_index = vlib_node_add_next (vm, vxlan6_input_node.index, node_index);
672     }
673
674   return next_index;
675 }
676
677 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
678 {
679   u32 * result = va_arg (*args, u32 *);
680   u32 ipv4_set = va_arg (*args, int);
681   vxlan_main_t * vxm = &vxlan_main;
682   vlib_main_t * vm = vxm->vlib_main;
683   u32 node_index;
684   u32 tmp;
685
686   if (unformat (input, "l2"))
687     *result = VXLAN_INPUT_NEXT_L2_INPUT;
688   else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
689     *result = get_decap_next_for_node(node_index, ipv4_set);
690   else if (unformat (input, "%d", &tmp))
691     *result = tmp;
692   else
693     return 0;
694   return 1;
695 }
696
697 static clib_error_t *
698 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
699                                    unformat_input_t * input,
700                                    vlib_cli_command_t * cmd)
701 {
702   unformat_input_t _line_input, * line_input = &_line_input;
703   ip46_address_t src , dst;
704   u8 is_add = 1;
705   u8 src_set = 0;
706   u8 dst_set = 0;
707   u8 grp_set = 0;
708   u8 ipv4_set = 0;
709   u8 ipv6_set = 0;
710   u32 encap_fib_index = 0;
711   u32 mcast_sw_if_index = ~0;
712   u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
713   u32 vni = 0;
714   u32 tmp;
715   int rv;
716   vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
717   u32 tunnel_sw_if_index;
718
719   /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
720   memset(&src, 0, sizeof src);
721   memset(&dst, 0, sizeof dst);
722
723   /* Get a line of input. */
724   if (! unformat_user (input, unformat_line_input, line_input))
725     return 0;
726
727   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
728     if (unformat (line_input, "del"))
729       {
730         is_add = 0;
731       }
732     else if (unformat (line_input, "src %U",
733                        unformat_ip4_address, &src.ip4))
734       {
735         src_set = 1;
736         ipv4_set = 1;
737       }
738     else if (unformat (line_input, "dst %U",
739                        unformat_ip4_address, &dst.ip4))
740       {
741         dst_set = 1;
742         ipv4_set = 1;
743       }
744     else if (unformat (line_input, "src %U", 
745                        unformat_ip6_address, &src.ip6))
746       {
747         src_set = 1;
748         ipv6_set = 1;
749       }
750     else if (unformat (line_input, "dst %U",
751                        unformat_ip6_address, &dst.ip6))
752       {
753         dst_set = 1;
754         ipv6_set = 1;
755       }
756     else if (unformat (line_input, "group %U %U",
757                        unformat_ip4_address, &dst.ip4,
758                        unformat_vnet_sw_interface,
759                        vnet_get_main(), &mcast_sw_if_index))
760       {
761         grp_set = dst_set = 1;
762         ipv4_set = 1;
763       }
764     else if (unformat (line_input, "group %U %U",
765                        unformat_ip6_address, &dst.ip6,
766                        unformat_vnet_sw_interface,
767                        vnet_get_main(), &mcast_sw_if_index))
768       {
769         grp_set = dst_set = 1;
770         ipv6_set = 1;
771       }
772     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
773       {
774         if (ipv6_set)
775           encap_fib_index = fib6_index_from_fib_id (tmp);
776         else
777           encap_fib_index = fib4_index_from_fib_id (tmp);
778         if (encap_fib_index == ~0)
779           return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
780       }
781     else if (unformat (line_input, "decap-next %U", unformat_decap_next, 
782                        &decap_next_index, ipv4_set))
783       ;
784     else if (unformat (line_input, "vni %d", &vni))
785       {
786         if (vni >> 24)  
787           return clib_error_return (0, "vni %d out of range", vni);
788       }
789     else 
790       return clib_error_return (0, "parse error: '%U'", 
791                                 format_unformat_error, line_input);
792   }
793
794   unformat_free (line_input);
795
796   if (src_set == 0)
797     return clib_error_return (0, "tunnel src address not specified");
798
799   if (dst_set == 0)
800     return clib_error_return (0, "tunnel dst address not specified");
801
802   if (grp_set && !ip46_address_is_multicast(&dst))
803     return clib_error_return (0, "tunnel group address not multicast");
804
805   if (grp_set == 0 && ip46_address_is_multicast(&dst))
806     return clib_error_return (0, "dst address must be unicast");
807
808   if (grp_set && mcast_sw_if_index == ~0)
809     return clib_error_return (0, "tunnel nonexistent multicast device");
810
811   if (ipv4_set && ipv6_set)
812     return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
813
814   if (ip46_address_cmp(&src, &dst) == 0)
815     return clib_error_return (0, "src and dst addresses are identical");
816
817   if (decap_next_index == ~0)
818     return clib_error_return (0, "next node not found");
819
820   if (vni == 0)
821     return clib_error_return (0, "vni not specified");
822
823   memset (a, 0, sizeof (*a));
824
825   a->is_add = is_add;
826   a->is_ip6 = ipv6_set;
827
828 #define _(x) a->x = x;
829   foreach_copy_field;
830 #undef _
831   
832   rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
833
834   switch(rv)
835     {
836     case 0:
837       if (is_add)
838         vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, 
839                         vnet_get_main(), tunnel_sw_if_index);
840       break;
841
842     case VNET_API_ERROR_TUNNEL_EXIST:
843       return clib_error_return (0, "tunnel already exists...");
844
845     case VNET_API_ERROR_NO_SUCH_ENTRY:
846       return clib_error_return (0, "tunnel does not exist...");
847
848     default:
849       return clib_error_return 
850         (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
851     }
852
853   return 0;
854 }
855
856 /*?
857  * Add or delete a VXLAN Tunnel.
858  *
859  * VXLAN provides the features needed to allow L2 bridge domains (BDs)
860  * to span multiple servers. This is done by building an L2 overlay on
861  * top of an L3 network underlay using VXLAN tunnels.
862  *
863  * This makes it possible for servers to be co-located in the same data
864  * center or be separated geographically as long as they are reachable
865  * through the underlay L3 network.
866  *
867  * You can refer to this kind of L2 overlay bridge domain as a VXLAN
868  * (Virtual eXtensible VLAN) segment.
869  *
870  * @cliexpar
871  * Example of how to create a VXLAN Tunnel:
872  * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7}
873  * Example of how to delete a VXLAN Tunnel:
874  * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
875  ?*/
876 /* *INDENT-OFF* */
877 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
878   .path = "create vxlan tunnel",
879   .short_help = 
880   "create vxlan tunnel src <local-vtep-addr>"
881   " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
882   " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
883   .function = vxlan_add_del_tunnel_command_fn,
884 };
885 /* *INDENT-ON* */
886
887 static clib_error_t *
888 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
889                                 unformat_input_t * input,
890                                 vlib_cli_command_t * cmd)
891 {
892   vxlan_main_t * vxm = &vxlan_main;
893   vxlan_tunnel_t * t;
894   
895   if (pool_elts (vxm->tunnels) == 0)
896     vlib_cli_output (vm, "No vxlan tunnels configured...");
897
898   pool_foreach (t, vxm->tunnels,
899   ({
900     vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
901   }));
902   
903   return 0;
904 }
905
906 /*?
907  * Display all the VXLAN Tunnel entries.
908  *
909  * @cliexpar
910  * Example of how to display the VXLAN Tunnel entries:
911  * @cliexstart{show vxlan tunnel}
912  * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
913  * @cliexend
914  ?*/
915 /* *INDENT-OFF* */
916 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
917     .path = "show vxlan tunnel",
918     .short_help = "show vxlan tunnel",
919     .function = show_vxlan_tunnel_command_fn,
920 };
921 /* *INDENT-ON* */
922
923
924 void vnet_int_vxlan_bypass_mode (u32 sw_if_index,
925                                  u8 is_ip6,
926                                  u8 is_enable)
927 {
928   if (is_ip6)
929     vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-bypass",
930                                  sw_if_index, is_enable, 0, 0);
931   else
932     vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-bypass",
933                                  sw_if_index, is_enable, 0, 0);
934 }
935
936
937 static clib_error_t *
938 set_ip_vxlan_bypass (u32 is_ip6,
939                      unformat_input_t * input,
940                      vlib_cli_command_t * cmd)
941 {
942   unformat_input_t _line_input, * line_input = &_line_input;
943   vnet_main_t * vnm = vnet_get_main();
944   clib_error_t * error = 0;
945   u32 sw_if_index, is_enable;
946
947   sw_if_index = ~0;
948   is_enable = 1;
949
950   if (! unformat_user (input, unformat_line_input, line_input))
951     return 0;
952
953   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
954     {
955       if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
956           ;
957       else if (unformat (line_input, "del"))
958         is_enable = 0;
959       else
960         {
961           error = unformat_parse_error (line_input);
962           goto done;
963         }
964     }
965
966   if (~0 == sw_if_index)
967     {
968       error = clib_error_return (0, "unknown interface `%U'",
969                                  format_unformat_error, line_input);
970       goto done;
971     }
972
973   vnet_int_vxlan_bypass_mode (sw_if_index, is_ip6, is_enable);
974
975  done:
976   return error;
977 }
978
979 static clib_error_t *
980 set_ip4_vxlan_bypass (vlib_main_t * vm,
981                       unformat_input_t * input,
982                       vlib_cli_command_t * cmd)
983 {
984   return set_ip_vxlan_bypass (0, input, cmd);
985 }
986
987 /*?
988  * This command adds the 'ip4-vxlan-bypass' graph node for a given interface. 
989  * By adding the IPv4 vxlan-bypass graph node to an interface, the node checks
990  *  for and validate input vxlan packet and bypass ip4-lookup, ip4-local, 
991  * ip4-udp-lookup nodes to speedup vxlan packet forwarding. This node will 
992  * cause extra overhead to for non-vxlan packets which is kept at a minimum.
993  *
994  * @cliexpar
995  * @parblock
996  * Example of graph node before ip4-vxlan-bypass is enabled:
997  * @cliexstart{show vlib graph ip4-vxlan-bypass}
998  *            Name                      Next                    Previous
999  * ip4-vxlan-bypass                error-drop [0]
1000  *                                vxlan4-input [1]
1001  *                                 ip4-lookup [2]      
1002  * @cliexend
1003  *
1004  * Example of how to enable ip4-vxlan-bypass on an interface:
1005  * @cliexcmd{set interface ip vxlan-bypass GigabitEthernet2/0/0}
1006  *
1007  * Example of graph node after ip4-vxlan-bypass is enabled:
1008  * @cliexstart{show vlib graph ip4-vxlan-bypass}
1009  *            Name                      Next                    Previous         
1010  * ip4-vxlan-bypass                error-drop [0]               ip4-input        
1011  *                                vxlan4-input [1]        ip4-input-no-checksum  
1012  *                                 ip4-lookup [2]      
1013  * @cliexend
1014  *
1015  * Example of how to display the feature enabed on an interface:
1016  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1017  * IP feature paths configured on GigabitEthernet2/0/0...
1018  * ...
1019  * ipv4 unicast:
1020  *   ip4-vxlan-bypass
1021  *   ip4-lookup
1022  * ...
1023  * @cliexend
1024  *
1025  * Example of how to disable ip4-vxlan-bypass on an interface:
1026  * @cliexcmd{set interface ip vxlan-bypass GigabitEthernet2/0/0 del}
1027  * @endparblock
1028 ?*/
1029 /* *INDENT-OFF* */
1030 VLIB_CLI_COMMAND (set_interface_ip_vxlan_bypass_command, static) = {
1031   .path = "set interface ip vxlan-bypass",
1032   .function = set_ip4_vxlan_bypass,
1033   .short_help = "set interface ip vxlan-bypass <interface> [del]",
1034 };
1035 /* *INDENT-ON* */
1036
1037 static clib_error_t *
1038 set_ip6_vxlan_bypass (vlib_main_t * vm,
1039                       unformat_input_t * input,
1040                       vlib_cli_command_t * cmd)
1041 {
1042   return set_ip_vxlan_bypass (1, input, cmd);
1043 }
1044
1045 /*?
1046  * This command adds the 'ip6-vxlan-bypass' graph node for a given interface. 
1047  * By adding the IPv6 vxlan-bypass graph node to an interface, the node checks
1048  *  for and validate input vxlan packet and bypass ip6-lookup, ip6-local, 
1049  * ip6-udp-lookup nodes to speedup vxlan packet forwarding. This node will 
1050  * cause extra overhead to for non-vxlan packets which is kept at a minimum.
1051  *
1052  * @cliexpar
1053  * @parblock
1054  * Example of graph node before ip6-vxlan-bypass is enabled:
1055  * @cliexstart{show vlib graph ip6-vxlan-bypass}
1056  *            Name                      Next                    Previous
1057  * ip6-vxlan-bypass                error-drop [0]
1058  *                                vxlan6-input [1]
1059  *                                 ip6-lookup [2]      
1060  * @cliexend
1061  *
1062  * Example of how to enable ip6-vxlan-bypass on an interface:
1063  * @cliexcmd{set interface ip6 vxlan-bypass GigabitEthernet2/0/0}
1064  *
1065  * Example of graph node after ip6-vxlan-bypass is enabled:
1066  * @cliexstart{show vlib graph ip6-vxlan-bypass}
1067  *            Name                      Next                    Previous         
1068  * ip6-vxlan-bypass                error-drop [0]               ip6-input        
1069  *                                vxlan6-input [1]        ip4-input-no-checksum  
1070  *                                 ip6-lookup [2]      
1071  * @cliexend
1072  *
1073  * Example of how to display the feature enabed on an interface:
1074  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1075  * IP feature paths configured on GigabitEthernet2/0/0...
1076  * ...
1077  * ipv6 unicast:
1078  *   ip6-vxlan-bypass
1079  *   ip6-lookup
1080  * ...
1081  * @cliexend
1082  *
1083  * Example of how to disable ip6-vxlan-bypass on an interface:
1084  * @cliexcmd{set interface ip6 vxlan-bypass GigabitEthernet2/0/0 del}
1085  * @endparblock
1086 ?*/
1087 /* *INDENT-OFF* */
1088 VLIB_CLI_COMMAND (set_interface_ip6_vxlan_bypass_command, static) = {
1089   .path = "set interface ip6 vxlan-bypass",
1090   .function = set_ip6_vxlan_bypass,
1091   .short_help = "set interface ip vxlan-bypass <interface> [del]",
1092 };
1093 /* *INDENT-ON* */
1094
1095 clib_error_t *vxlan_init (vlib_main_t *vm)
1096 {
1097   vxlan_main_t * vxm = &vxlan_main;
1098   
1099   vxm->vnet_main = vnet_get_main();
1100   vxm->vlib_main = vm;
1101
1102   /* initialize the ip6 hash */
1103   vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
1104         sizeof(vxlan6_tunnel_key_t),
1105         sizeof(uword));
1106   vxm->vtep6 = hash_create_mem(0,
1107         sizeof(ip6_address_t),
1108         sizeof(uword));
1109   vxm->mcast_shared = hash_create_mem(0,
1110         sizeof(ip46_address_t),
1111         sizeof(mcast_shared_t));
1112
1113   udp_register_dst_port (vm, UDP_DST_PORT_vxlan, 
1114                          vxlan4_input_node.index, /* is_ip4 */ 1);
1115   udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
1116                          vxlan6_input_node.index, /* is_ip4 */ 0);
1117
1118   fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft);
1119
1120   return 0;
1121 }
1122
1123 VLIB_INIT_FUNCTION(vxlan_init);