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