FIB: store the node type not the function pointer.
[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
37
38 geneve_main_t geneve_main;
39
40 static u8 *
41 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 GENEVE_INPUT_NEXT_DROP:
48       return format (s, "drop");
49     case GENEVE_INPUT_NEXT_L2_INPUT:
50       return format (s, "l2");
51     default:
52       return format (s, "index %d", next_index);
53     }
54   return s;
55 }
56
57 u8 *
58 format_geneve_tunnel (u8 * s, va_list * args)
59 {
60   geneve_tunnel_t *t = va_arg (*args, geneve_tunnel_t *);
61   geneve_main_t *ngm = &geneve_main;
62
63   s = format (s, "[%d] local %U remote %U vni %d sw_if_index %d ",
64               t - ngm->tunnels,
65               format_ip46_address, &t->local, IP46_TYPE_ANY,
66               format_ip46_address, &t->remote, IP46_TYPE_ANY,
67               t->vni, t->sw_if_index);
68
69   if (ip46_address_is_multicast (&t->remote))
70     s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
71
72   s = format (s, "encap_fib_index %d fib_entry_index %d decap_next %U\n",
73               t->encap_fib_index, t->fib_entry_index,
74               format_decap_next, t->decap_next_index);
75   return s;
76 }
77
78 static u8 *
79 format_geneve_name (u8 * s, va_list * args)
80 {
81   u32 dev_instance = va_arg (*args, u32);
82   return format (s, "geneve_tunnel%d", dev_instance);
83 }
84
85 static uword
86 dummy_interface_tx (vlib_main_t * vm,
87                     vlib_node_runtime_t * node, vlib_frame_t * frame)
88 {
89   clib_warning ("you shouldn't be here, leaking buffers...");
90   return frame->n_vectors;
91 }
92
93 static clib_error_t *
94 geneve_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
95 {
96   u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
97     VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
98   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
99
100   return /* no error */ 0;
101 }
102
103 /* *INDENT-OFF* */
104 VNET_DEVICE_CLASS (geneve_device_class, static) = {
105   .name = "GENEVE",
106   .format_device_name = format_geneve_name,
107   .format_tx_trace = format_geneve_encap_trace,
108   .tx_function = dummy_interface_tx,
109   .admin_up_down_function = geneve_interface_admin_up_down,
110 };
111 /* *INDENT-ON* */
112
113 static u8 *
114 format_geneve_header_with_length (u8 * s, va_list * args)
115 {
116   u32 dev_instance = va_arg (*args, u32);
117   s = format (s, "unimplemented dev %u", dev_instance);
118   return s;
119 }
120
121 /* *INDENT-OFF* */
122 VNET_HW_INTERFACE_CLASS (geneve_hw_class) = {
123   .name = "GENEVE",
124   .format_header = format_geneve_header_with_length,
125   .build_rewrite = default_build_rewrite,
126 };
127 /* *INDENT-ON* */
128
129 static void
130 geneve_tunnel_restack_dpo (geneve_tunnel_t * t)
131 {
132   dpo_id_t dpo = DPO_INVALID;
133   u32 encap_index = ip46_address_is_ip4 (&t->remote) ?
134     geneve4_encap_node.index : geneve6_encap_node.index;
135   fib_forward_chain_type_t forw_type = ip46_address_is_ip4 (&t->remote) ?
136     FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
137
138   fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
139   dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
140   dpo_reset (&dpo);
141 }
142
143 static geneve_tunnel_t *
144 geneve_tunnel_from_fib_node (fib_node_t * node)
145 {
146   ASSERT (FIB_NODE_TYPE_GENEVE_TUNNEL == node->fn_type);
147   return ((geneve_tunnel_t *) (((char *) node) -
148                                STRUCT_OFFSET_OF (geneve_tunnel_t, node)));
149 }
150
151 /**
152  * Function definition to backwalk a FIB node -
153  * Here we will restack the new dpo of GENEVE DIP to encap node.
154  */
155 static fib_node_back_walk_rc_t
156 geneve_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
157 {
158   geneve_tunnel_restack_dpo (geneve_tunnel_from_fib_node (node));
159   return (FIB_NODE_BACK_WALK_CONTINUE);
160 }
161
162 /**
163  * Function definition to get a FIB node from its index
164  */
165 static fib_node_t *
166 geneve_tunnel_fib_node_get (fib_node_index_t index)
167 {
168   geneve_tunnel_t *t;
169   geneve_main_t *vxm = &geneve_main;
170
171   t = pool_elt_at_index (vxm->tunnels, index);
172
173   return (&t->node);
174 }
175
176 /**
177  * Function definition to inform the FIB node that its last lock has gone.
178  */
179 static void
180 geneve_tunnel_last_lock_gone (fib_node_t * node)
181 {
182   /*
183    * The GENEVE tunnel is a root of the graph. As such
184    * it never has children and thus is never locked.
185    */
186   ASSERT (0);
187 }
188
189 /*
190  * Virtual function table registered by GENEVE tunnels
191  * for participation in the FIB object graph.
192  */
193 const static fib_node_vft_t geneve_vft = {
194   .fnv_get = geneve_tunnel_fib_node_get,
195   .fnv_last_lock = geneve_tunnel_last_lock_gone,
196   .fnv_back_walk = geneve_tunnel_back_walk,
197 };
198
199
200 #define foreach_copy_field                      \
201 _(vni)                                          \
202 _(mcast_sw_if_index)                            \
203 _(encap_fib_index)                              \
204 _(decap_next_index)                             \
205 _(local)                                          \
206 _(remote)
207
208 static int
209 geneve_rewrite (geneve_tunnel_t * t, bool is_ip6)
210 {
211   union
212   {
213     ip4_geneve_header_t *h4;
214     ip6_geneve_header_t *h6;
215     u8 *rw;
216   } r =
217   {
218   .rw = 0};
219   int len = is_ip6 ? sizeof *r.h6 : sizeof *r.h4;
220 #if SUPPORT_OPTIONS_HEADER==1
221   len += t->options_len;
222 #endif
223
224   vec_validate_aligned (r.rw, len - 1, CLIB_CACHE_LINE_BYTES);
225
226   udp_header_t *udp;
227   geneve_header_t *geneve;
228   /* Fixed portion of the (outer) ip header */
229   if (!is_ip6)
230     {
231       ip4_header_t *ip = &r.h4->ip4;
232       udp = &r.h4->udp, geneve = &r.h4->geneve;
233       ip->ip_version_and_header_length = 0x45;
234       ip->ttl = 254;
235       ip->protocol = IP_PROTOCOL_UDP;
236
237       ip->src_address = t->local.ip4;
238       ip->dst_address = t->remote.ip4;
239
240       /* we fix up the ip4 header length and checksum after-the-fact */
241       ip->checksum = ip4_header_checksum (ip);
242     }
243   else
244     {
245       ip6_header_t *ip = &r.h6->ip6;
246       udp = &r.h6->udp, geneve = &r.h6->geneve;
247       ip->ip_version_traffic_class_and_flow_label =
248         clib_host_to_net_u32 (6 << 28);
249       ip->hop_limit = 255;
250       ip->protocol = IP_PROTOCOL_UDP;
251
252       ip->src_address = t->local.ip6;
253       ip->dst_address = t->remote.ip6;
254     }
255
256   /* UDP header, randomize local port on something, maybe? */
257   udp->src_port = clib_host_to_net_u16 (5251);
258   udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_geneve);
259
260   /* GENEVE header */
261   vnet_set_geneve_version (geneve, GENEVE_VERSION);
262 #if SUPPORT_OPTIONS_HEADER==1
263   vnet_set_geneve_options_len (geneve, t->options_len);
264 #else
265   vnet_set_geneve_options_len (geneve, 0);
266 #endif
267   vnet_set_geneve_oamframe_bit (geneve, 0);
268   vnet_set_geneve_critical_bit (geneve, 0);
269   vnet_set_geneve_protocol (geneve, GENEVE_ETH_PROTOCOL);
270
271   vnet_geneve_hdr_1word_hton (geneve);
272
273   vnet_set_geneve_vni (geneve, t->vni);
274
275   t->rewrite = r.rw;
276   return (0);
277 }
278
279 static bool
280 geneve_decap_next_is_valid (geneve_main_t * vxm, u32 is_ip6,
281                             u32 decap_next_index)
282 {
283   vlib_main_t *vm = vxm->vlib_main;
284   u32 input_idx =
285     (!is_ip6) ? geneve4_input_node.index : geneve6_input_node.index;
286   vlib_node_runtime_t *r = vlib_node_get_runtime (vm, input_idx);
287
288   return decap_next_index < r->n_next_nodes;
289 }
290
291 static void
292 hash_set_key_copy (uword ** h, void *key, uword v)
293 {
294   size_t ksz = hash_header (*h)->user;
295   void *copy = clib_mem_alloc (ksz);
296   clib_memcpy (copy, key, ksz);
297   hash_set_mem (*h, copy, v);
298 }
299
300 static void
301 hash_unset_key_free (uword ** h, void *key)
302 {
303   hash_pair_t *hp = hash_get_pair_mem (*h, key);
304   ASSERT (hp);
305   key = uword_to_pointer (hp->key, void *);
306   hash_unset_mem (*h, key);
307   clib_mem_free (key);
308 }
309
310 static uword
311 vtep_addr_ref (ip46_address_t * ip)
312 {
313   uword *vtep = ip46_address_is_ip4 (ip) ?
314     hash_get (geneve_main.vtep4, ip->ip4.as_u32) :
315     hash_get_mem (geneve_main.vtep6, &ip->ip6);
316   if (vtep)
317     return ++(*vtep);
318   ip46_address_is_ip4 (ip) ?
319     hash_set (geneve_main.vtep4, ip->ip4.as_u32, 1) :
320     hash_set_key_copy (&geneve_main.vtep6, &ip->ip6, 1);
321   return 1;
322 }
323
324 static uword
325 vtep_addr_unref (ip46_address_t * ip)
326 {
327   uword *vtep = ip46_address_is_ip4 (ip) ?
328     hash_get (geneve_main.vtep4, ip->ip4.as_u32) :
329     hash_get_mem (geneve_main.vtep6, &ip->ip6);
330   ASSERT (vtep);
331   if (--(*vtep) != 0)
332     return *vtep;
333   ip46_address_is_ip4 (ip) ?
334     hash_unset (geneve_main.vtep4, ip->ip4.as_u32) :
335     hash_unset_key_free (&geneve_main.vtep6, &ip->ip6);
336   return 0;
337 }
338
339 typedef CLIB_PACKED (union
340                      {
341                      struct
342                      {
343                      fib_node_index_t mfib_entry_index;
344                      adj_index_t mcast_adj_index;
345                      }; u64 as_u64;
346                      }) mcast_shared_t;
347
348 static inline mcast_shared_t
349 mcast_shared_get (ip46_address_t * ip)
350 {
351   ASSERT (ip46_address_is_multicast (ip));
352   uword *p = hash_get_mem (geneve_main.mcast_shared, ip);
353   ASSERT (p);
354   return (mcast_shared_t)
355   {
356   .as_u64 = *p};
357 }
358
359 static inline void
360 mcast_shared_add (ip46_address_t * remote,
361                   fib_node_index_t mfei, adj_index_t ai)
362 {
363   mcast_shared_t new_ep = {
364     .mcast_adj_index = ai,
365     .mfib_entry_index = mfei,
366   };
367
368   hash_set_key_copy (&geneve_main.mcast_shared, remote, new_ep.as_u64);
369 }
370
371 static inline void
372 mcast_shared_remove (ip46_address_t * remote)
373 {
374   mcast_shared_t ep = mcast_shared_get (remote);
375
376   adj_unlock (ep.mcast_adj_index);
377   mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_GENEVE);
378
379   hash_unset_key_free (&geneve_main.mcast_shared, remote);
380 }
381
382 static inline fib_protocol_t
383 fib_ip_proto (bool is_ip6)
384 {
385   return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
386 }
387
388 int vnet_geneve_add_del_tunnel
389   (vnet_geneve_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
390 {
391   geneve_main_t *vxm = &geneve_main;
392   geneve_tunnel_t *t = 0;
393   vnet_main_t *vnm = vxm->vnet_main;
394   uword *p;
395   u32 hw_if_index = ~0;
396   u32 sw_if_index = ~0;
397   int rv;
398   geneve4_tunnel_key_t key4;
399   geneve6_tunnel_key_t key6;
400   u32 is_ip6 = a->is_ip6;
401
402   if (!is_ip6)
403     {
404       key4.remote = a->remote.ip4.as_u32;
405       key4.vni =
406         clib_host_to_net_u32 ((a->vni << GENEVE_VNI_SHIFT) & GENEVE_VNI_MASK);
407       p = hash_get (vxm->geneve4_tunnel_by_key, key4.as_u64);
408     }
409   else
410     {
411       key6.remote = a->remote.ip6;
412       key6.vni =
413         clib_host_to_net_u32 ((a->vni << GENEVE_VNI_SHIFT) & GENEVE_VNI_MASK);
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  */