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