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