28fde51cc23bc57e4c9cedf84c3e8145e5c9c6a3
[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 = t->src.ip6;
257   ip0->dst_address = t->dst.ip6;
258
259   /* UDP header, randomize src port on something, maybe? */
260   h0->udp.src_port = clib_host_to_net_u16 (4789);
261   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
262
263   /* VXLAN header */
264   vnet_set_vni_and_flags(&h0->vxlan, t->vni);
265
266   t->rewrite = rw;
267   return (0);
268 }
269
270 static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index)
271 {
272   vlib_main_t * vm = vxm->vlib_main;
273   vlib_node_runtime_t *r;
274
275   if(!is_ip6)
276     {
277       r = vlib_node_get_runtime (vm, vxlan4_input_node.index);
278       if(decap_next_index >= r->n_next_nodes)
279         return 1;
280     }
281   else
282     {
283       r = vlib_node_get_runtime (vm, vxlan6_input_node.index);
284       if(decap_next_index >= r->n_next_nodes)
285         return 1;
286     }
287
288   return 0;
289 }
290
291 static void
292 hash_set_key_copy (uword ** h, void * key, uword v) {
293         size_t ksz = hash_header(*h)->user;
294         void * copy = clib_mem_alloc (ksz);
295         clib_memcpy (copy, key, ksz);
296         hash_set_mem (*h, copy, v);
297 }
298
299 static void
300 hash_unset_key_free (uword ** h, void * key) {
301         hash_pair_t * hp = hash_get_pair_mem (*h, key);
302         ASSERT (hp);
303         key = uword_to_pointer (hp->key, void *);
304         hash_unset_mem (*h, key);
305         clib_mem_free (key);
306 }
307
308 static uword
309 vtep_addr_ref(ip46_address_t *ip)
310 {
311         uword *vtep = ip46_address_is_ip4(ip) ?
312                          hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
313                          hash_get_mem (vxlan_main.vtep6, &ip->ip6);
314         if (vtep) 
315                 return ++(*vtep);
316         ip46_address_is_ip4(ip) ?
317                          hash_set (vxlan_main.vtep4, ip->ip4.as_u32, 1) :
318                          hash_set_key_copy (&vxlan_main.vtep6, &ip->ip6, 1);
319         return 1;
320 }
321
322 static uword
323 vtep_addr_unref(ip46_address_t *ip)
324 {
325         uword *vtep = ip46_address_is_ip4(ip) ?
326                          hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
327                          hash_get_mem (vxlan_main.vtep6, &ip->ip6);
328         ASSERT(vtep);
329         if (--(*vtep) != 0)
330                 return *vtep;
331         ip46_address_is_ip4(ip) ?
332                 hash_unset (vxlan_main.vtep4, ip->ip4.as_u32) :
333                 hash_unset_key_free (&vxlan_main.vtep6, &ip->ip6);
334         return 0;
335 }
336
337 typedef CLIB_PACKED(union {
338   struct {
339     fib_node_index_t mfib_entry_index;
340     adj_index_t mcast_adj_index;
341   };
342   u64 as_u64;
343 }) mcast_shared_t;
344
345 static inline mcast_shared_t
346 mcast_shared_get(ip46_address_t * ip)
347 {
348         ASSERT(ip46_address_is_multicast(ip));
349         uword * p = hash_get_mem (vxlan_main.mcast_shared, ip);
350         ASSERT(p);
351         return (mcast_shared_t) { .as_u64 = *p };
352 }
353
354 static inline void
355 mcast_shared_add(ip46_address_t *dst,
356                  fib_node_index_t mfei,
357                  adj_index_t ai)
358 {
359     mcast_shared_t new_ep = {
360         .mcast_adj_index = ai,
361         .mfib_entry_index = mfei,
362     };
363
364     hash_set_key_copy (&vxlan_main.mcast_shared, dst, new_ep.as_u64);
365 }
366
367 static inline void
368 mcast_shared_remove(ip46_address_t *dst)
369 {
370     mcast_shared_t ep = mcast_shared_get(dst);
371
372     adj_unlock(ep.mcast_adj_index);
373     mfib_table_entry_delete_index(ep.mfib_entry_index,
374                                   MFIB_SOURCE_VXLAN);
375
376     hash_unset_key_free (&vxlan_main.mcast_shared, dst);
377 }
378
379 static inline fib_protocol_t
380 fib_ip_proto(bool is_ip6)
381 {
382   return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
383 }
384
385 int vnet_vxlan_add_del_tunnel 
386 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
387 {
388   vxlan_main_t * vxm = &vxlan_main;
389   vxlan_tunnel_t *t = 0;
390   vnet_main_t * vnm = vxm->vnet_main;
391   uword * p;
392   u32 hw_if_index = ~0;
393   u32 sw_if_index = ~0;
394   int rv;
395   vxlan4_tunnel_key_t key4;
396   vxlan6_tunnel_key_t key6;
397   u32 is_ip6 = a->is_ip6;
398
399   if (!is_ip6)
400     {
401       key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
402       key4.vni = clib_host_to_net_u32 (a->vni << 8);
403       p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
404     } 
405   else 
406     {
407       key6.src = a->dst.ip6;
408       key6.vni = clib_host_to_net_u32 (a->vni << 8);
409       p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
410     }
411   
412   if (a->is_add)
413     {
414       l2input_main_t * l2im = &l2input_main;
415
416       /* adding a tunnel: tunnel must not already exist */
417       if (p)
418         return VNET_API_ERROR_TUNNEL_EXIST;
419
420       /*if not set explicitly, default to l2 */
421       if(a->decap_next_index == ~0)
422         a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
423       if (vxlan_check_decap_next(vxm, is_ip6, a->decap_next_index))
424           return VNET_API_ERROR_INVALID_DECAP_NEXT;
425
426       pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
427       memset (t, 0, sizeof (*t));
428       
429       /* copy from arg structure */
430 #define _(x) t->x = a->x;
431       foreach_copy_field;
432 #undef _
433
434       if (!is_ip6) 
435         rv = vxlan4_rewrite (t);
436       else
437         rv = vxlan6_rewrite (t);
438
439       if (rv)
440         {
441           pool_put (vxm->tunnels, t);
442           return rv;
443         }
444
445       /* copy the key */
446       if (is_ip6)
447         hash_set_key_copy (&vxm->vxlan6_tunnel_by_key, &key6, t - vxm->tunnels);
448       else
449         hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
450
451       vnet_hw_interface_t * hi;
452       if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
453         {
454           vnet_interface_main_t * im = &vnm->interface_main;
455           hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
456             [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
457           _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
458           
459           hi = vnet_get_hw_interface (vnm, hw_if_index);
460           hi->dev_instance = t - vxm->tunnels;
461           hi->hw_instance = hi->dev_instance;
462
463           /* clear old stats of freed tunnel before reuse */
464           sw_if_index = hi->sw_if_index;
465           vnet_interface_counter_lock(im);
466           vlib_zero_combined_counter 
467             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
468           vlib_zero_combined_counter 
469             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
470           vlib_zero_simple_counter 
471             (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
472           vnet_interface_counter_unlock(im);
473         }
474       else 
475         {
476           hw_if_index = vnet_register_interface
477             (vnm, vxlan_device_class.index, t - vxm->tunnels,
478              vxlan_hw_class.index, t - vxm->tunnels);
479           hi = vnet_get_hw_interface (vnm, hw_if_index);
480         }
481       
482       t->hw_if_index = hw_if_index;
483       t->sw_if_index = sw_if_index = hi->sw_if_index;
484       
485       vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
486       vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
487
488       /* setup l2 input config with l2 feature and bd 0 to drop packet */
489       vec_validate (l2im->configs, sw_if_index);
490       l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
491       l2im->configs[sw_if_index].bd_index = 0;
492       
493       vnet_sw_interface_set_flags (vnm, sw_if_index, 
494                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
495       fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL);
496       fib_prefix_t tun_dst_pfx;
497       u32 encap_index = !is_ip6 ?
498           vxlan4_encap_node.index : vxlan6_encap_node.index;
499       vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
500
501       fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
502       if (!ip46_address_is_multicast(&t->dst))
503         {
504           /* Unicast tunnel -
505            * source the FIB entry for the tunnel's destination
506            * and become a child thereof. The tunnel will then get poked
507            * when the forwarding for the entry updates, and the tunnel can
508            * re-stack accordingly
509            */
510           vtep_addr_ref(&t->src);
511           t->fib_entry_index = fib_table_entry_special_add
512             (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
513              FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID);
514           t->sibling_index = fib_entry_child_add
515             (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels);
516           vxlan_tunnel_restack_dpo(t);
517         } 
518       else
519         {
520           /* Multicast tunnel -
521            * as the same mcast group can be used for mutiple mcast tunnels
522            * with different VNIs, create the output fib adjecency only if
523            * it does not already exist
524            */
525           fib_protocol_t fp = fib_ip_proto(is_ip6);
526
527           if (vtep_addr_ref(&t->dst) == 1)
528           {
529               fib_node_index_t mfei;
530               adj_index_t ai;
531               fib_route_path_t path = {
532                   .frp_proto = fp,
533                   .frp_addr = zero_addr,
534                   .frp_sw_if_index = 0xffffffff,
535                   .frp_fib_index = ~0,
536                   .frp_weight = 0,
537                   .frp_flags = FIB_ROUTE_PATH_LOCAL,
538               };
539               const mfib_prefix_t mpfx = {
540                   .fp_proto = fp,
541                   .fp_len = (is_ip6 ? 128 : 32),
542                   .fp_grp_addr = tun_dst_pfx.fp_addr,
543               };
544
545               /*
546                * Setup the (*,G) to receive traffic on the mcast group
547                *  - the forwarding interface is for-us
548                *  - the accepting interface is that from the API
549                */
550               mfib_table_entry_path_update(t->encap_fib_index,
551                                            &mpfx,
552                                            MFIB_SOURCE_VXLAN,
553                                            &path,
554                                            MFIB_ITF_FLAG_FORWARD);
555
556               path.frp_sw_if_index = a->mcast_sw_if_index;
557               path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
558               mfei = mfib_table_entry_path_update(t->encap_fib_index,
559                                                   &mpfx,
560                                                   MFIB_SOURCE_VXLAN,
561                                                   &path,
562                                                   MFIB_ITF_FLAG_ACCEPT);
563
564               /*
565                * Create the mcast adjacency to send traffic to the group
566                */
567               ai = adj_mcast_add_or_lock(fp,
568                                          fib_proto_to_link(fp),
569                                          a->mcast_sw_if_index);
570
571               /*
572                * create a new end-point
573                */
574               mcast_shared_add(&t->dst, mfei, ai);
575           }
576
577           dpo_id_t dpo = DPO_INVALID;
578           mcast_shared_t ep = mcast_shared_get(&t->dst);
579
580           /* Stack shared mcast dst mac addr rewrite on encap */
581           dpo_set (&dpo, DPO_ADJACENCY,
582                    fib_proto_to_dpo(fp),
583                    ep.mcast_adj_index);
584
585           dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
586           dpo_reset (&dpo);
587           flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
588         }
589
590       /* Set vxlan tunnel output node */
591       hi->output_node_index = encap_index;
592
593       vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
594     }
595   else
596     {
597       /* deleting a tunnel: tunnel must exist */
598       if (!p)
599         return VNET_API_ERROR_NO_SUCH_ENTRY;
600
601       t = pool_elt_at_index (vxm->tunnels, p[0]);
602
603       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
604       /* make sure tunnel is removed from l2 bd or xconnect */
605       set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
606       vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
607
608       vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
609
610       if (!is_ip6)
611         hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
612       else
613         hash_unset_key_free (&vxm->vxlan6_tunnel_by_key, &key6);
614
615       if (!ip46_address_is_multicast(&t->dst))
616         {
617           vtep_addr_unref(&t->src);
618           fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
619           fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
620         }
621       else if (vtep_addr_unref(&t->dst) == 0)
622         {
623           mcast_shared_remove(&t->dst);
624         }
625
626       fib_node_deinit(&t->node);
627       vec_free (t->rewrite);
628       pool_put (vxm->tunnels, t);
629     }
630
631   if (sw_if_indexp)
632       *sw_if_indexp = sw_if_index;
633
634   return 0;
635 }
636
637 static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set)
638 {
639   vxlan_main_t * vxm = &vxlan_main;
640   vlib_main_t * vm = vxm->vlib_main;
641   uword input_node = (ipv4_set) ? vxlan4_input_node.index :
642     vxlan6_input_node.index;
643
644   return vlib_node_add_next (vm, input_node, node_index);
645 }
646
647 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
648 {
649   u32 * result = va_arg (*args, u32 *);
650   u32 ipv4_set = va_arg (*args, int);
651   vxlan_main_t * vxm = &vxlan_main;
652   vlib_main_t * vm = vxm->vlib_main;
653   u32 node_index;
654   u32 tmp;
655
656   if (unformat (input, "l2"))
657     *result = VXLAN_INPUT_NEXT_L2_INPUT;
658   else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
659     *result = get_decap_next_for_node(node_index, ipv4_set);
660   else if (unformat (input, "%d", &tmp))
661     *result = tmp;
662   else
663     return 0;
664   return 1;
665 }
666
667 static clib_error_t *
668 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
669                                    unformat_input_t * input,
670                                    vlib_cli_command_t * cmd)
671 {
672   unformat_input_t _line_input, * line_input = &_line_input;
673   ip46_address_t src , dst;
674   u8 is_add = 1;
675   u8 src_set = 0;
676   u8 dst_set = 0;
677   u8 grp_set = 0;
678   u8 ipv4_set = 0;
679   u8 ipv6_set = 0;
680   u32 encap_fib_index = 0;
681   u32 mcast_sw_if_index = ~0;
682   u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
683   u32 vni = 0;
684   u32 tmp;
685   int rv;
686   vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
687   u32 tunnel_sw_if_index;
688
689   /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
690   memset(&src, 0, sizeof src);
691   memset(&dst, 0, sizeof dst);
692
693   /* Get a line of input. */
694   if (! unformat_user (input, unformat_line_input, line_input))
695     return 0;
696
697   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
698     if (unformat (line_input, "del"))
699       {
700         is_add = 0;
701       }
702     else if (unformat (line_input, "src %U",
703                        unformat_ip4_address, &src.ip4))
704       {
705         src_set = 1;
706         ipv4_set = 1;
707       }
708     else if (unformat (line_input, "dst %U",
709                        unformat_ip4_address, &dst.ip4))
710       {
711         dst_set = 1;
712         ipv4_set = 1;
713       }
714     else if (unformat (line_input, "src %U", 
715                        unformat_ip6_address, &src.ip6))
716       {
717         src_set = 1;
718         ipv6_set = 1;
719       }
720     else if (unformat (line_input, "dst %U",
721                        unformat_ip6_address, &dst.ip6))
722       {
723         dst_set = 1;
724         ipv6_set = 1;
725       }
726     else if (unformat (line_input, "group %U %U",
727                        unformat_ip4_address, &dst.ip4,
728                        unformat_vnet_sw_interface,
729                        vnet_get_main(), &mcast_sw_if_index))
730       {
731         grp_set = dst_set = 1;
732         ipv4_set = 1;
733       }
734     else if (unformat (line_input, "group %U %U",
735                        unformat_ip6_address, &dst.ip6,
736                        unformat_vnet_sw_interface,
737                        vnet_get_main(), &mcast_sw_if_index))
738       {
739         grp_set = dst_set = 1;
740         ipv6_set = 1;
741       }
742     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
743       {
744         encap_fib_index = fib_table_find (fib_ip_proto (ipv6_set), tmp);
745         if (encap_fib_index == ~0)
746           return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
747       }
748     else if (unformat (line_input, "decap-next %U", unformat_decap_next, 
749                        &decap_next_index, ipv4_set))
750       ;
751     else if (unformat (line_input, "vni %d", &vni))
752       {
753         if (vni >> 24)  
754           return clib_error_return (0, "vni %d out of range", vni);
755       }
756     else 
757       return clib_error_return (0, "parse error: '%U'", 
758                                 format_unformat_error, line_input);
759   }
760
761   unformat_free (line_input);
762
763   if (src_set == 0)
764     return clib_error_return (0, "tunnel src address not specified");
765
766   if (dst_set == 0)
767     return clib_error_return (0, "tunnel dst address not specified");
768
769   if (grp_set && !ip46_address_is_multicast(&dst))
770     return clib_error_return (0, "tunnel group address not multicast");
771
772   if (grp_set == 0 && ip46_address_is_multicast(&dst))
773     return clib_error_return (0, "dst address must be unicast");
774
775   if (grp_set && mcast_sw_if_index == ~0)
776     return clib_error_return (0, "tunnel nonexistent multicast device");
777
778   if (ipv4_set && ipv6_set)
779     return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
780
781   if (ip46_address_cmp(&src, &dst) == 0)
782     return clib_error_return (0, "src and dst addresses are identical");
783
784   if (decap_next_index == ~0)
785     return clib_error_return (0, "next node not found");
786
787   if (vni == 0)
788     return clib_error_return (0, "vni not specified");
789
790   memset (a, 0, sizeof (*a));
791
792   a->is_add = is_add;
793   a->is_ip6 = ipv6_set;
794
795 #define _(x) a->x = x;
796   foreach_copy_field;
797 #undef _
798   
799   rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
800
801   switch(rv)
802     {
803     case 0:
804       if (is_add)
805         vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, 
806                         vnet_get_main(), tunnel_sw_if_index);
807       break;
808
809     case VNET_API_ERROR_TUNNEL_EXIST:
810       return clib_error_return (0, "tunnel already exists...");
811
812     case VNET_API_ERROR_NO_SUCH_ENTRY:
813       return clib_error_return (0, "tunnel does not exist...");
814
815     default:
816       return clib_error_return 
817         (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
818     }
819
820   return 0;
821 }
822
823 /*?
824  * Add or delete a VXLAN Tunnel.
825  *
826  * VXLAN provides the features needed to allow L2 bridge domains (BDs)
827  * to span multiple servers. This is done by building an L2 overlay on
828  * top of an L3 network underlay using VXLAN tunnels.
829  *
830  * This makes it possible for servers to be co-located in the same data
831  * center or be separated geographically as long as they are reachable
832  * through the underlay L3 network.
833  *
834  * You can refer to this kind of L2 overlay bridge domain as a VXLAN
835  * (Virtual eXtensible VLAN) segment.
836  *
837  * @cliexpar
838  * Example of how to create a VXLAN Tunnel:
839  * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7}
840  * Example of how to delete a VXLAN Tunnel:
841  * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
842  ?*/
843 /* *INDENT-OFF* */
844 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
845   .path = "create vxlan tunnel",
846   .short_help = 
847   "create vxlan tunnel src <local-vtep-addr>"
848   " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
849   " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
850   .function = vxlan_add_del_tunnel_command_fn,
851 };
852 /* *INDENT-ON* */
853
854 static clib_error_t *
855 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
856                                 unformat_input_t * input,
857                                 vlib_cli_command_t * cmd)
858 {
859   vxlan_main_t * vxm = &vxlan_main;
860   vxlan_tunnel_t * t;
861   
862   if (pool_elts (vxm->tunnels) == 0)
863     vlib_cli_output (vm, "No vxlan tunnels configured...");
864
865   pool_foreach (t, vxm->tunnels,
866   ({
867     vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
868   }));
869   
870   return 0;
871 }
872
873 /*?
874  * Display all the VXLAN Tunnel entries.
875  *
876  * @cliexpar
877  * Example of how to display the VXLAN Tunnel entries:
878  * @cliexstart{show vxlan tunnel}
879  * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
880  * @cliexend
881  ?*/
882 /* *INDENT-OFF* */
883 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
884     .path = "show vxlan tunnel",
885     .short_help = "show vxlan tunnel",
886     .function = show_vxlan_tunnel_command_fn,
887 };
888 /* *INDENT-ON* */
889
890
891 void vnet_int_vxlan_bypass_mode (u32 sw_if_index,
892                                  u8 is_ip6,
893                                  u8 is_enable)
894 {
895   if (is_ip6)
896     vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-bypass",
897                                  sw_if_index, is_enable, 0, 0);
898   else
899     vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-bypass",
900                                  sw_if_index, is_enable, 0, 0);
901 }
902
903
904 static clib_error_t *
905 set_ip_vxlan_bypass (u32 is_ip6,
906                      unformat_input_t * input,
907                      vlib_cli_command_t * cmd)
908 {
909   unformat_input_t _line_input, * line_input = &_line_input;
910   vnet_main_t * vnm = vnet_get_main();
911   clib_error_t * error = 0;
912   u32 sw_if_index, is_enable;
913
914   sw_if_index = ~0;
915   is_enable = 1;
916
917   if (! unformat_user (input, unformat_line_input, line_input))
918     return 0;
919
920   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
921     {
922       if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
923           ;
924       else if (unformat (line_input, "del"))
925         is_enable = 0;
926       else
927         {
928           error = unformat_parse_error (line_input);
929           goto done;
930         }
931     }
932
933   if (~0 == sw_if_index)
934     {
935       error = clib_error_return (0, "unknown interface `%U'",
936                                  format_unformat_error, line_input);
937       goto done;
938     }
939
940   vnet_int_vxlan_bypass_mode (sw_if_index, is_ip6, is_enable);
941
942  done:
943   return error;
944 }
945
946 static clib_error_t *
947 set_ip4_vxlan_bypass (vlib_main_t * vm,
948                       unformat_input_t * input,
949                       vlib_cli_command_t * cmd)
950 {
951   return set_ip_vxlan_bypass (0, input, cmd);
952 }
953
954 /*?
955  * This command adds the 'ip4-vxlan-bypass' graph node for a given interface. 
956  * By adding the IPv4 vxlan-bypass graph node to an interface, the node checks
957  *  for and validate input vxlan packet and bypass ip4-lookup, ip4-local, 
958  * ip4-udp-lookup nodes to speedup vxlan packet forwarding. This node will 
959  * cause extra overhead to for non-vxlan packets which is kept at a minimum.
960  *
961  * @cliexpar
962  * @parblock
963  * Example of graph node before ip4-vxlan-bypass is enabled:
964  * @cliexstart{show vlib graph ip4-vxlan-bypass}
965  *            Name                      Next                    Previous
966  * ip4-vxlan-bypass                error-drop [0]
967  *                                vxlan4-input [1]
968  *                                 ip4-lookup [2]      
969  * @cliexend
970  *
971  * Example of how to enable ip4-vxlan-bypass on an interface:
972  * @cliexcmd{set interface ip vxlan-bypass GigabitEthernet2/0/0}
973  *
974  * Example of graph node after ip4-vxlan-bypass is enabled:
975  * @cliexstart{show vlib graph ip4-vxlan-bypass}
976  *            Name                      Next                    Previous         
977  * ip4-vxlan-bypass                error-drop [0]               ip4-input        
978  *                                vxlan4-input [1]        ip4-input-no-checksum  
979  *                                 ip4-lookup [2]      
980  * @cliexend
981  *
982  * Example of how to display the feature enabed on an interface:
983  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
984  * IP feature paths configured on GigabitEthernet2/0/0...
985  * ...
986  * ipv4 unicast:
987  *   ip4-vxlan-bypass
988  *   ip4-lookup
989  * ...
990  * @cliexend
991  *
992  * Example of how to disable ip4-vxlan-bypass on an interface:
993  * @cliexcmd{set interface ip vxlan-bypass GigabitEthernet2/0/0 del}
994  * @endparblock
995 ?*/
996 /* *INDENT-OFF* */
997 VLIB_CLI_COMMAND (set_interface_ip_vxlan_bypass_command, static) = {
998   .path = "set interface ip vxlan-bypass",
999   .function = set_ip4_vxlan_bypass,
1000   .short_help = "set interface ip vxlan-bypass <interface> [del]",
1001 };
1002 /* *INDENT-ON* */
1003
1004 static clib_error_t *
1005 set_ip6_vxlan_bypass (vlib_main_t * vm,
1006                       unformat_input_t * input,
1007                       vlib_cli_command_t * cmd)
1008 {
1009   return set_ip_vxlan_bypass (1, input, cmd);
1010 }
1011
1012 /*?
1013  * This command adds the 'ip6-vxlan-bypass' graph node for a given interface. 
1014  * By adding the IPv6 vxlan-bypass graph node to an interface, the node checks
1015  *  for and validate input vxlan packet and bypass ip6-lookup, ip6-local, 
1016  * ip6-udp-lookup nodes to speedup vxlan packet forwarding. This node will 
1017  * cause extra overhead to for non-vxlan packets which is kept at a minimum.
1018  *
1019  * @cliexpar
1020  * @parblock
1021  * Example of graph node before ip6-vxlan-bypass is enabled:
1022  * @cliexstart{show vlib graph ip6-vxlan-bypass}
1023  *            Name                      Next                    Previous
1024  * ip6-vxlan-bypass                error-drop [0]
1025  *                                vxlan6-input [1]
1026  *                                 ip6-lookup [2]      
1027  * @cliexend
1028  *
1029  * Example of how to enable ip6-vxlan-bypass on an interface:
1030  * @cliexcmd{set interface ip6 vxlan-bypass GigabitEthernet2/0/0}
1031  *
1032  * Example of graph node after ip6-vxlan-bypass is enabled:
1033  * @cliexstart{show vlib graph ip6-vxlan-bypass}
1034  *            Name                      Next                    Previous         
1035  * ip6-vxlan-bypass                error-drop [0]               ip6-input        
1036  *                                vxlan6-input [1]        ip4-input-no-checksum  
1037  *                                 ip6-lookup [2]      
1038  * @cliexend
1039  *
1040  * Example of how to display the feature enabed on an interface:
1041  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1042  * IP feature paths configured on GigabitEthernet2/0/0...
1043  * ...
1044  * ipv6 unicast:
1045  *   ip6-vxlan-bypass
1046  *   ip6-lookup
1047  * ...
1048  * @cliexend
1049  *
1050  * Example of how to disable ip6-vxlan-bypass on an interface:
1051  * @cliexcmd{set interface ip6 vxlan-bypass GigabitEthernet2/0/0 del}
1052  * @endparblock
1053 ?*/
1054 /* *INDENT-OFF* */
1055 VLIB_CLI_COMMAND (set_interface_ip6_vxlan_bypass_command, static) = {
1056   .path = "set interface ip6 vxlan-bypass",
1057   .function = set_ip6_vxlan_bypass,
1058   .short_help = "set interface ip vxlan-bypass <interface> [del]",
1059 };
1060 /* *INDENT-ON* */
1061
1062 clib_error_t *vxlan_init (vlib_main_t *vm)
1063 {
1064   vxlan_main_t * vxm = &vxlan_main;
1065   
1066   vxm->vnet_main = vnet_get_main();
1067   vxm->vlib_main = vm;
1068
1069   /* initialize the ip6 hash */
1070   vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
1071         sizeof(vxlan6_tunnel_key_t),
1072         sizeof(uword));
1073   vxm->vtep6 = hash_create_mem(0,
1074         sizeof(ip6_address_t),
1075         sizeof(uword));
1076   vxm->mcast_shared = hash_create_mem(0,
1077         sizeof(ip46_address_t),
1078         sizeof(mcast_shared_t));
1079
1080   udp_register_dst_port (vm, UDP_DST_PORT_vxlan, 
1081                          vxlan4_input_node.index, /* is_ip4 */ 1);
1082   udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
1083                          vxlan6_input_node.index, /* is_ip4 */ 0);
1084
1085   fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft);
1086
1087   return 0;
1088 }
1089
1090 VLIB_INIT_FUNCTION(vxlan_init);