894cc3dcbf26d5db996ada9c17e649f5552a4070
[vpp.git] / vnet / 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/dpo/receive_dpo.h>
20 #include <vlib/vlib.h>
21
22 /**
23  * @file
24  * @brief VXLAN.
25  *
26  * VXLAN provides the features needed to allow L2 bridge domains (BDs)
27  * to span multiple servers. This is done by building an L2 overlay on
28  * top of an L3 network underlay using VXLAN tunnels.
29  *
30  * This makes it possible for servers to be co-located in the same data
31  * center or be separated geographically as long as they are reachable
32  * through the underlay L3 network.
33  *
34  * You can refer to this kind of L2 overlay bridge domain as a VXLAN
35  * (Virtual eXtensible VLAN) segment.
36  */
37
38
39 vxlan_main_t vxlan_main;
40
41 static u8 * format_decap_next (u8 * s, va_list * args)
42 {
43   u32 next_index = va_arg (*args, u32);
44
45   switch (next_index)
46     {
47     case VXLAN_INPUT_NEXT_DROP:
48       return format (s, "drop");
49     case VXLAN_INPUT_NEXT_L2_INPUT:
50       return format (s, "l2");
51     default:
52       return format (s, "index %d", next_index);
53     }
54   return s;
55 }
56
57 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
58 {
59   vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
60   vxlan_main_t * ngm = &vxlan_main;
61
62   s = format (s, "[%d] src %U dst %U vni %d sw_if_index %d ",
63               t - ngm->tunnels,
64               format_ip46_address, &t->src, IP46_TYPE_ANY,
65               format_ip46_address, &t->dst, IP46_TYPE_ANY,
66               t->vni, t->sw_if_index);
67
68   if (ip46_address_is_multicast (&t->dst))
69     s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
70
71   s = format (s, "encap_fib_index %d fib_entry_index %d decap_next %U\n", 
72               t->encap_fib_index, t->fib_entry_index,
73               format_decap_next, t->decap_next_index);
74   return s;
75 }
76
77 static u8 * format_vxlan_name (u8 * s, va_list * args)
78 {
79   u32 dev_instance = va_arg (*args, u32);
80   return format (s, "vxlan_tunnel%d", dev_instance);
81 }
82
83 static uword dummy_interface_tx (vlib_main_t * vm,
84                                  vlib_node_runtime_t * node,
85                                  vlib_frame_t * frame)
86 {
87   clib_warning ("you shouldn't be here, leaking buffers...");
88   return frame->n_vectors;
89 }
90
91 static clib_error_t *
92 vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
93 {
94   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
95     vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
96   else
97     vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
98
99   return /* no error */ 0;
100 }
101
102 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
103   .name = "VXLAN",
104   .format_device_name = format_vxlan_name,
105   .format_tx_trace = format_vxlan_encap_trace,
106   .tx_function = dummy_interface_tx,
107   .admin_up_down_function = vxlan_interface_admin_up_down,
108 };
109
110 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
111 {
112   u32 dev_instance = va_arg (*args, u32);
113   s = format (s, "unimplemented dev %u", dev_instance);
114   return s;
115 }
116
117 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
118   .name = "VXLAN",
119   .format_header = format_vxlan_header_with_length,
120   .build_rewrite = default_build_rewrite,
121 };
122
123 static void
124 vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t)
125 {
126     dpo_id_t dpo = DPO_INVALID;
127     u32 encap_index = ip46_address_is_ip4(&t->dst) ?
128         vxlan4_encap_node.index : vxlan6_encap_node.index;
129     fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->dst) ?
130         FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
131
132     fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
133     dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
134     dpo_reset(&dpo);
135 }
136
137 static vxlan_tunnel_t *
138 vxlan_tunnel_from_fib_node (fib_node_t *node)
139 {
140 #if (CLIB_DEBUG > 0)
141     ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type);
142 #endif
143     return ((vxlan_tunnel_t*) (((char*)node) -
144                                STRUCT_OFFSET_OF(vxlan_tunnel_t, node)));
145 }
146
147 /**
148  * Function definition to backwalk a FIB node -
149  * Here we will restack the new dpo of VXLAN DIP to encap node.
150  */
151 static fib_node_back_walk_rc_t
152 vxlan_tunnel_back_walk (fib_node_t *node,
153                         fib_node_back_walk_ctx_t *ctx)
154 {
155     vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node));
156     return (FIB_NODE_BACK_WALK_CONTINUE);
157 }
158
159 /**
160  * Function definition to get a FIB node from its index
161  */
162 static fib_node_t*
163 vxlan_tunnel_fib_node_get (fib_node_index_t index)
164 {
165     vxlan_tunnel_t * t;
166     vxlan_main_t * vxm = &vxlan_main;
167
168     t = pool_elt_at_index(vxm->tunnels, index);
169
170     return (&t->node);
171 }
172
173 /**
174  * Function definition to inform the FIB node that its last lock has gone.
175  */
176 static void
177 vxlan_tunnel_last_lock_gone (fib_node_t *node)
178 {
179     /*
180      * The VXLAN tunnel is a root of the graph. As such
181      * it never has children and thus is never locked.
182      */
183     ASSERT(0);
184 }
185
186 /*
187  * Virtual function table registered by VXLAN tunnels
188  * for participation in the FIB object graph.
189  */
190 const static fib_node_vft_t vxlan_vft = {
191     .fnv_get = vxlan_tunnel_fib_node_get,
192     .fnv_last_lock = vxlan_tunnel_last_lock_gone,
193     .fnv_back_walk = vxlan_tunnel_back_walk,
194 };
195
196
197 #define foreach_copy_field                      \
198 _(vni)                                          \
199 _(mcast_sw_if_index)                            \
200 _(encap_fib_index)                              \
201 _(decap_next_index)                             \
202 _(src)                                          \
203 _(dst)
204
205 static int vxlan4_rewrite (vxlan_tunnel_t * t)
206 {
207   u8 *rw = 0;
208   ip4_header_t * ip0;
209   ip4_vxlan_header_t * h0;
210   int len = sizeof (*h0);
211
212   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
213
214   h0 = (ip4_vxlan_header_t *) rw;
215
216   /* Fixed portion of the (outer) ip4 header */
217   ip0 = &h0->ip4;
218   ip0->ip_version_and_header_length = 0x45;
219   ip0->ttl = 254;
220   ip0->protocol = IP_PROTOCOL_UDP;
221
222   /* we fix up the ip4 header length and checksum after-the-fact */
223   ip0->src_address.as_u32 = t->src.ip4.as_u32;
224   ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
225   ip0->checksum = ip4_header_checksum (ip0);
226
227   /* UDP header, randomize src port on something, maybe? */
228   h0->udp.src_port = clib_host_to_net_u16 (4789);
229   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
230
231   /* VXLAN header */
232   vnet_set_vni_and_flags(&h0->vxlan, t->vni);
233
234   t->rewrite = rw;
235   return (0);
236 }
237
238 static int vxlan6_rewrite (vxlan_tunnel_t * t)
239 {
240   u8 *rw = 0;
241   ip6_header_t * ip0;
242   ip6_vxlan_header_t * h0;
243   int len = sizeof (*h0);
244
245   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
246
247   h0 = (ip6_vxlan_header_t *) rw;
248
249   /* Fixed portion of the (outer) ip6 header */
250   ip0 = &h0->ip6;
251   ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
252   ip0->hop_limit = 255;
253   ip0->protocol = IP_PROTOCOL_UDP;
254
255   ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
256   ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
257   ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
258   ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
259
260   /* UDP header, randomize src port on something, maybe? */
261   h0->udp.src_port = clib_host_to_net_u16 (4789);
262   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
263
264   /* VXLAN header */
265   vnet_set_vni_and_flags(&h0->vxlan, t->vni);
266
267   t->rewrite = rw;
268   return (0);
269 }
270
271 static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index)
272 {
273   vlib_main_t * vm = vxm->vlib_main;
274   vlib_node_runtime_t *r;
275
276   if(!is_ip6)
277     {
278       r = vlib_node_get_runtime (vm, vxlan4_input_node.index);
279       if(decap_next_index >= r->n_next_nodes)
280         return 1;
281     }
282   else
283     {
284       r = vlib_node_get_runtime (vm, vxlan6_input_node.index);
285       if(decap_next_index >= r->n_next_nodes)
286         return 1;
287     }
288
289   return 0;
290 }
291
292 int vnet_vxlan_add_del_tunnel 
293 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
294 {
295   vxlan_main_t * vxm = &vxlan_main;
296   vxlan_tunnel_t *t = 0;
297   vnet_main_t * vnm = vxm->vnet_main;
298   vnet_hw_interface_t * hi;
299   uword * p, * pvtep;
300   u32 hw_if_index = ~0;
301   u32 sw_if_index = ~0;
302   int rv;
303   vxlan4_tunnel_key_t key4;
304   vxlan6_tunnel_key_t key6;
305   u32 is_ip6 = a->is_ip6;
306
307   if (!is_ip6)
308     {
309       key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
310       key4.vni = clib_host_to_net_u32 (a->vni << 8);
311       p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
312       if (ip4_address_is_multicast (&a->dst.ip4))
313         pvtep = hash_get (vxm->vtep4, a->dst.ip4.as_u32);
314       else
315         pvtep = hash_get (vxm->vtep4, a->src.ip4.as_u32);
316     } 
317   else 
318     {
319       key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
320       key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
321       key6.vni = clib_host_to_net_u32 (a->vni << 8);
322       p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
323       pvtep = NULL;  /* ip6 vxlan-bypass not yet implemented */
324     }
325   
326   if (a->is_add)
327     {
328       l2input_main_t * l2im = &l2input_main;
329
330       /* adding a tunnel: tunnel must not already exist */
331       if (p)
332         return VNET_API_ERROR_TUNNEL_EXIST;
333
334       /*if not set explicitly, default to l2 */
335       if(a->decap_next_index == ~0)
336         a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
337       if (vxlan_check_decap_next(vxm, is_ip6, a->decap_next_index))
338           return VNET_API_ERROR_INVALID_DECAP_NEXT;
339
340       pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
341       memset (t, 0, sizeof (*t));
342       
343       /* copy from arg structure */
344 #define _(x) t->x = a->x;
345       foreach_copy_field;
346 #undef _
347       
348       /* copy the key */
349       if (is_ip6)
350         {
351           t->key6 = clib_mem_alloc (sizeof(vxlan6_tunnel_key_t));
352           clib_memcpy (t->key6, &key6, sizeof(key6));
353         }
354       else
355         {
356           t->key4 = 0; /* not yet used */
357         }
358
359       if (!is_ip6) 
360         rv = vxlan4_rewrite (t);
361       else
362         rv = vxlan6_rewrite (t);
363
364       if (rv)
365         {
366           pool_put (vxm->tunnels, t);
367           return rv;
368         }
369
370       if (!is_ip6)
371         {
372           hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
373           if (pvtep) 
374             pvtep[0]++;
375           else
376             {
377               if (ip4_address_is_multicast (&a->dst.ip4))
378                 hash_set (vxm->vtep4, a->dst.ip4.as_u32, 1);
379               else
380                 hash_set (vxm->vtep4, a->src.ip4.as_u32, 1);
381             }
382         }
383       else
384         hash_set_mem (vxm->vxlan6_tunnel_by_key, t->key6, t - vxm->tunnels);
385       
386       if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
387         {
388           vnet_interface_main_t * im = &vnm->interface_main;
389           hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
390             [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
391           _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
392           
393           hi = vnet_get_hw_interface (vnm, hw_if_index);
394           hi->dev_instance = t - vxm->tunnels;
395           hi->hw_instance = hi->dev_instance;
396
397           /* clear old stats of freed tunnel before reuse */
398           sw_if_index = hi->sw_if_index;
399           vnet_interface_counter_lock(im);
400           vlib_zero_combined_counter 
401             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
402           vlib_zero_combined_counter 
403             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
404           vlib_zero_simple_counter 
405             (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
406           vnet_interface_counter_unlock(im);
407         }
408       else 
409         {
410           hw_if_index = vnet_register_interface
411             (vnm, vxlan_device_class.index, t - vxm->tunnels,
412              vxlan_hw_class.index, t - vxm->tunnels);
413           hi = vnet_get_hw_interface (vnm, hw_if_index);
414         }
415       
416       t->hw_if_index = hw_if_index;
417       t->sw_if_index = sw_if_index = hi->sw_if_index;
418       
419       vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
420       vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
421
422       /* setup l2 input config with l2 feature and bd 0 to drop packet */
423       vec_validate (l2im->configs, sw_if_index);
424       l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
425       l2im->configs[sw_if_index].bd_index = 0;
426       
427       vnet_sw_interface_set_flags (vnm, sw_if_index, 
428                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
429       fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL);
430       fib_prefix_t tun_dst_pfx;
431       u32 encap_index = !is_ip6 ?
432           vxlan4_encap_node.index : vxlan6_encap_node.index;
433       vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
434
435       fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
436       if (!ip46_address_is_multicast(&t->dst))
437         {
438           /* Unicast tunnel -
439            * source the FIB entry for the tunnel's destination
440            * and become a child thereof. The tunnel will then get poked
441            * when the forwarding for the entry updates, and the tunnel can
442            * re-stack accordingly
443            */
444           t->fib_entry_index = fib_table_entry_special_add
445             (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
446              FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID);
447           t->sibling_index = fib_entry_child_add
448             (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels);
449           vxlan_tunnel_restack_dpo(t);
450         } 
451       else if (pvtep == NULL)
452         {
453           /* Multicast tunnel -
454            * as the same mcast group can be used for mutiple mcast tunnels
455            * with different VNIs, create the output fib adjecency only if
456            * it does not already exist
457            */
458           fib_protocol_t fp;
459           u8 mcast_mac[6];
460           if (!is_ip6) {
461               ip4_multicast_ethernet_address(mcast_mac, &t->dst.ip4);
462               fp = FIB_PROTOCOL_IP4;
463           } else {
464               ip6_multicast_ethernet_address(mcast_mac, t->dst.ip6.as_u32[0]);
465               fp = FIB_PROTOCOL_IP6;
466           }
467           t->mcast_adj_index = adj_rewrite_add_and_lock
468               (fp, fib_proto_to_link(fp), t->mcast_sw_if_index, mcast_mac);
469
470           flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
471
472           /* Stack mcast dst mac addr rewrite on encap */
473           dpo_proto_t dproto = fib_proto_to_dpo(fp);
474           dpo_id_t dpo = DPO_INVALID;
475
476           dpo_set (&dpo, DPO_ADJACENCY, dproto, t->mcast_adj_index);
477           dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
478           dpo_reset(&dpo);
479
480           /* Add local mcast adj. */
481           receive_dpo_add_or_lock(dproto, ~0, NULL, &dpo);
482           t->fib_entry_index = fib_table_entry_special_dpo_add
483             (t->encap_fib_index, &tun_dst_pfx, 
484              FIB_SOURCE_SPECIAL, FIB_ENTRY_FLAG_NONE, &dpo);
485           dpo_reset(&dpo);
486         }
487
488       /* Set vxlan tunnel output node */
489       hi->output_node_index = encap_index;
490
491       vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
492     }
493   else
494     {
495       /* deleting a tunnel: tunnel must exist */
496       if (!p)
497         return VNET_API_ERROR_NO_SUCH_ENTRY;
498
499       t = pool_elt_at_index (vxm->tunnels, p[0]);
500
501       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
502       /* make sure tunnel is removed from l2 bd or xconnect */
503       set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
504       vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
505
506       vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
507
508       if (!ip46_address_is_multicast(&t->dst))
509         {
510           fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
511           fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
512           fib_node_deinit(&t->node);
513         }
514       else if (pvtep == NULL || pvtep[0] == 1)
515         {
516           adj_unlock(t->mcast_adj_index);
517           fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_SPECIAL);
518           fib_node_deinit(&t->node);
519         }
520
521       if (!is_ip6)
522         {
523           hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
524           if (pvtep)
525             {
526               pvtep[0]--;
527               if (pvtep[0] == 0)
528                 {
529                   if (ip4_address_is_multicast (&a->dst.ip4))
530                     hash_unset (vxm->vtep4, a->dst.ip4.as_u32);
531                   else
532                     hash_unset (vxm->vtep4, a->src.ip4.as_u32);
533                 }
534             }
535         }
536       else
537         {
538           hash_unset_mem (vxm->vxlan6_tunnel_by_key, t->key6);
539           clib_mem_free (t->key6);
540         }
541       vec_free (t->rewrite);
542       pool_put (vxm->tunnels, t);
543     }
544
545   if (sw_if_indexp)
546       *sw_if_indexp = sw_if_index;
547
548   return 0;
549 }
550
551 static u32 fib4_index_from_fib_id (u32 fib_id)
552 {
553   ip4_main_t * im = &ip4_main;
554   uword * p;
555
556   p = hash_get (im->fib_index_by_table_id, fib_id);
557   if (!p)
558     return ~0;
559
560   return p[0];
561 }
562
563 static u32 fib6_index_from_fib_id (u32 fib_id)
564 {
565   ip6_main_t * im = &ip6_main;
566   uword * p;
567
568   p = hash_get (im->fib_index_by_table_id, fib_id);
569   if (!p)
570     return ~0;
571
572   return p[0];
573 }
574
575 static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set)
576 {
577   vxlan_main_t * vxm = &vxlan_main;
578   vlib_main_t * vm = vxm->vlib_main;
579   uword next_index = ~0;
580
581   if (ipv4_set)
582     {
583       next_index = vlib_node_add_next (vm, vxlan4_input_node.index, node_index);
584     }
585   else
586     {
587       next_index = vlib_node_add_next (vm, vxlan6_input_node.index, node_index);
588     }
589
590   return next_index;
591 }
592
593 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
594 {
595   u32 * result = va_arg (*args, u32 *);
596   u32 ipv4_set = va_arg (*args, int);
597   vxlan_main_t * vxm = &vxlan_main;
598   vlib_main_t * vm = vxm->vlib_main;
599   u32 node_index;
600   u32 tmp;
601
602   if (unformat (input, "l2"))
603     *result = VXLAN_INPUT_NEXT_L2_INPUT;
604   else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
605     *result = get_decap_next_for_node(node_index, ipv4_set);
606   else if (unformat (input, "%d", &tmp))
607     *result = tmp;
608   else
609     return 0;
610   return 1;
611 }
612
613 static clib_error_t *
614 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
615                                    unformat_input_t * input,
616                                    vlib_cli_command_t * cmd)
617 {
618   unformat_input_t _line_input, * line_input = &_line_input;
619   ip46_address_t src , dst;
620   u8 is_add = 1;
621   u8 src_set = 0;
622   u8 dst_set = 0;
623   u8 grp_set = 0;
624   u8 ipv4_set = 0;
625   u8 ipv6_set = 0;
626   u32 encap_fib_index = 0;
627   u32 mcast_sw_if_index = ~0;
628   u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
629   u32 vni = 0;
630   u32 tmp;
631   int rv;
632   vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
633   u32 tunnel_sw_if_index;
634
635   /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
636   memset(&src, 0, sizeof src);
637   memset(&dst, 0, sizeof dst);
638
639   /* Get a line of input. */
640   if (! unformat_user (input, unformat_line_input, line_input))
641     return 0;
642
643   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
644     if (unformat (line_input, "del"))
645       {
646         is_add = 0;
647       }
648     else if (unformat (line_input, "src %U",
649                        unformat_ip4_address, &src.ip4))
650       {
651         src_set = 1;
652         ipv4_set = 1;
653       }
654     else if (unformat (line_input, "dst %U",
655                        unformat_ip4_address, &dst.ip4))
656       {
657         dst_set = 1;
658         ipv4_set = 1;
659       }
660     else if (unformat (line_input, "src %U", 
661                        unformat_ip6_address, &src.ip6))
662       {
663         src_set = 1;
664         ipv6_set = 1;
665       }
666     else if (unformat (line_input, "dst %U",
667                        unformat_ip6_address, &dst.ip6))
668       {
669         dst_set = 1;
670         ipv6_set = 1;
671       }
672     else if (unformat (line_input, "group %U %U",
673                        unformat_ip4_address, &dst.ip4,
674                        unformat_vnet_sw_interface,
675                        vnet_get_main(), &mcast_sw_if_index))
676       {
677         grp_set = dst_set = 1;
678         ipv4_set = 1;
679       }
680     else if (unformat (line_input, "group %U %U",
681                        unformat_ip6_address, &dst.ip6,
682                        unformat_vnet_sw_interface,
683                        vnet_get_main(), &mcast_sw_if_index))
684       {
685         grp_set = dst_set = 1;
686         ipv6_set = 1;
687       }
688     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
689       {
690         if (ipv6_set)
691           encap_fib_index = fib6_index_from_fib_id (tmp);
692         else
693           encap_fib_index = fib4_index_from_fib_id (tmp);
694         if (encap_fib_index == ~0)
695           return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
696       }
697     else if (unformat (line_input, "decap-next %U", unformat_decap_next, 
698                        &decap_next_index, ipv4_set))
699       ;
700     else if (unformat (line_input, "vni %d", &vni))
701       {
702         if (vni >> 24)  
703           return clib_error_return (0, "vni %d out of range", vni);
704       }
705     else 
706       return clib_error_return (0, "parse error: '%U'", 
707                                 format_unformat_error, line_input);
708   }
709
710   unformat_free (line_input);
711
712   if (src_set == 0)
713     return clib_error_return (0, "tunnel src address not specified");
714
715   if (dst_set == 0)
716     return clib_error_return (0, "tunnel dst address not specified");
717
718   if (grp_set && !ip46_address_is_multicast(&dst))
719     return clib_error_return (0, "tunnel group address not multicast");
720
721   if (grp_set == 0 && ip46_address_is_multicast(&dst))
722     return clib_error_return (0, "dst address must be unicast");
723
724   if (grp_set && mcast_sw_if_index == ~0)
725     return clib_error_return (0, "tunnel nonexistent multicast device");
726
727   if (ipv4_set && ipv6_set)
728     return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
729
730   if (ip46_address_cmp(&src, &dst) == 0)
731     return clib_error_return (0, "src and dst addresses are identical");
732
733   if (decap_next_index == ~0)
734     return clib_error_return (0, "next node not found");
735
736   if (vni == 0)
737     return clib_error_return (0, "vni not specified");
738
739   memset (a, 0, sizeof (*a));
740
741   a->is_add = is_add;
742   a->is_ip6 = ipv6_set;
743
744 #define _(x) a->x = x;
745   foreach_copy_field;
746 #undef _
747   
748   rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
749
750   switch(rv)
751     {
752     case 0:
753       if (is_add)
754         vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, 
755                         vnet_get_main(), tunnel_sw_if_index);
756       break;
757
758     case VNET_API_ERROR_TUNNEL_EXIST:
759       return clib_error_return (0, "tunnel already exists...");
760
761     case VNET_API_ERROR_NO_SUCH_ENTRY:
762       return clib_error_return (0, "tunnel does not exist...");
763
764     default:
765       return clib_error_return 
766         (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
767     }
768
769   return 0;
770 }
771
772 /*?
773  * Add or delete a VXLAN Tunnel.
774  *
775  * VXLAN provides the features needed to allow L2 bridge domains (BDs)
776  * to span multiple servers. This is done by building an L2 overlay on
777  * top of an L3 network underlay using VXLAN tunnels.
778  *
779  * This makes it possible for servers to be co-located in the same data
780  * center or be separated geographically as long as they are reachable
781  * through the underlay L3 network.
782  *
783  * You can refer to this kind of L2 overlay bridge domain as a VXLAN
784  * (Virtual eXtensible VLAN) segment.
785  *
786  * @cliexpar
787  * Example of how to create a VXLAN Tunnel:
788  * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7}
789  * Example of how to delete a VXLAN Tunnel:
790  * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
791  ?*/
792 /* *INDENT-OFF* */
793 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
794   .path = "create vxlan tunnel",
795   .short_help = 
796   "create vxlan tunnel src <local-vtep-addr>"
797   " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
798   " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
799   .function = vxlan_add_del_tunnel_command_fn,
800 };
801 /* *INDENT-ON* */
802
803 static clib_error_t *
804 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
805                                 unformat_input_t * input,
806                                 vlib_cli_command_t * cmd)
807 {
808   vxlan_main_t * vxm = &vxlan_main;
809   vxlan_tunnel_t * t;
810   
811   if (pool_elts (vxm->tunnels) == 0)
812     vlib_cli_output (vm, "No vxlan tunnels configured...");
813
814   pool_foreach (t, vxm->tunnels,
815   ({
816     vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
817   }));
818   
819   return 0;
820 }
821
822 /*?
823  * Display all the VXLAN Tunnel entries.
824  *
825  * @cliexpar
826  * Example of how to display the VXLAN Tunnel entries:
827  * @cliexstart{show vxlan tunnel}
828  * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
829  * @cliexend
830  ?*/
831 /* *INDENT-OFF* */
832 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
833     .path = "show vxlan tunnel",
834     .short_help = "show vxlan tunnel",
835     .function = show_vxlan_tunnel_command_fn,
836 };
837 /* *INDENT-ON* */
838
839
840 clib_error_t *vxlan_init (vlib_main_t *vm)
841 {
842   vxlan_main_t * vxm = &vxlan_main;
843   
844   vxm->vnet_main = vnet_get_main();
845   vxm->vlib_main = vm;
846
847   /* initialize the ip6 hash */
848   vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
849         sizeof(vxlan6_tunnel_key_t),
850         sizeof(uword));
851
852   udp_register_dst_port (vm, UDP_DST_PORT_vxlan, 
853                          vxlan4_input_node.index, /* is_ip4 */ 1);
854   udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
855                          vxlan6_input_node.index, /* is_ip4 */ 0);
856
857   fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft);
858
859   return 0;
860 }
861
862 VLIB_INIT_FUNCTION(vxlan_init);