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