blob: 9ed42875a35d17460f4b577bd95adc9bf74f4441 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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>
Chris Luke99cb3352016-04-26 10:49:53 -040016#include <vnet/ip/format.h>
John Loc42912d2016-11-07 18:30:47 -050017#include <vnet/fib/fib_entry.h>
18#include <vnet/fib/fib_table.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000019#include <vnet/mfib/mfib_table.h>
20#include <vnet/adj/adj_mcast.h>
Hongjun Nibeb4bf72016-11-25 00:03:46 +080021#include <vlib/vlib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
Billy McFallc5d9cda2016-09-14 10:39:58 -040023/**
24 * @file
25 * @brief VXLAN.
26 *
27 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
28 * to span multiple servers. This is done by building an L2 overlay on
29 * top of an L3 network underlay using VXLAN tunnels.
30 *
31 * This makes it possible for servers to be co-located in the same data
32 * center or be separated geographically as long as they are reachable
33 * through the underlay L3 network.
34 *
35 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
36 * (Virtual eXtensible VLAN) segment.
37 */
38
39
Ed Warnickecb9cada2015-12-08 15:45:58 -070040vxlan_main_t vxlan_main;
41
Hongjun Nibeb4bf72016-11-25 00:03:46 +080042static u8 * format_decap_next (u8 * s, va_list * args)
43{
44 u32 next_index = va_arg (*args, u32);
45
46 switch (next_index)
47 {
48 case VXLAN_INPUT_NEXT_DROP:
49 return format (s, "drop");
50 case VXLAN_INPUT_NEXT_L2_INPUT:
51 return format (s, "l2");
52 default:
John Lo56912c82016-12-08 16:10:02 -050053 return format (s, "index %d", next_index);
Hongjun Nibeb4bf72016-11-25 00:03:46 +080054 }
55 return s;
56}
57
Ed Warnickecb9cada2015-12-08 15:45:58 -070058u8 * format_vxlan_tunnel (u8 * s, va_list * args)
59{
60 vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
61 vxlan_main_t * ngm = &vxlan_main;
62
John Lo56912c82016-12-08 16:10:02 -050063 s = format (s, "[%d] src %U dst %U vni %d sw_if_index %d ",
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 t - ngm->tunnels,
Damjan Marion86be4872016-05-24 23:19:11 +020065 format_ip46_address, &t->src, IP46_TYPE_ANY,
66 format_ip46_address, &t->dst, IP46_TYPE_ANY,
John Lo56912c82016-12-08 16:10:02 -050067 t->vni, t->sw_if_index);
68
69 if (ip46_address_is_multicast (&t->dst))
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);
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 return s;
76}
77
78static u8 * format_vxlan_name (u8 * s, va_list * args)
79{
80 u32 dev_instance = va_arg (*args, u32);
81 return format (s, "vxlan_tunnel%d", dev_instance);
82}
83
84static uword dummy_interface_tx (vlib_main_t * vm,
85 vlib_node_runtime_t * node,
86 vlib_frame_t * frame)
87{
88 clib_warning ("you shouldn't be here, leaking buffers...");
89 return frame->n_vectors;
90}
91
Pavel Kotucek988a7c42016-02-29 15:03:08 +010092static clib_error_t *
93vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
94{
95 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
96 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
97 else
98 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
99
100 return /* no error */ 0;
101}
102
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103VNET_DEVICE_CLASS (vxlan_device_class,static) = {
104 .name = "VXLAN",
105 .format_device_name = format_vxlan_name,
106 .format_tx_trace = format_vxlan_encap_trace,
107 .tx_function = dummy_interface_tx,
Pavel Kotucek988a7c42016-02-29 15:03:08 +0100108 .admin_up_down_function = vxlan_interface_admin_up_down,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109};
110
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
112{
113 u32 dev_instance = va_arg (*args, u32);
114 s = format (s, "unimplemented dev %u", dev_instance);
115 return s;
116}
117
118VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
119 .name = "VXLAN",
120 .format_header = format_vxlan_header_with_length,
Neale Rannsb80c5362016-10-08 13:03:40 +0100121 .build_rewrite = default_build_rewrite,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122};
123
Eyal Baric5b13602016-11-24 19:42:43 +0200124static void
125vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t)
126{
127 dpo_id_t dpo = DPO_INVALID;
128 u32 encap_index = ip46_address_is_ip4(&t->dst) ?
129 vxlan4_encap_node.index : vxlan6_encap_node.index;
130 fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->dst) ?
131 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
132
133 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
134 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
135 dpo_reset(&dpo);
136}
John Loc42912d2016-11-07 18:30:47 -0500137
138static vxlan_tunnel_t *
139vxlan_tunnel_from_fib_node (fib_node_t *node)
140{
141#if (CLIB_DEBUG > 0)
142 ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type);
143#endif
144 return ((vxlan_tunnel_t*) (((char*)node) -
145 STRUCT_OFFSET_OF(vxlan_tunnel_t, node)));
146}
147
148/**
149 * Function definition to backwalk a FIB node -
150 * Here we will restack the new dpo of VXLAN DIP to encap node.
151 */
152static fib_node_back_walk_rc_t
153vxlan_tunnel_back_walk (fib_node_t *node,
154 fib_node_back_walk_ctx_t *ctx)
155{
Eyal Baric5b13602016-11-24 19:42:43 +0200156 vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node));
John Loc42912d2016-11-07 18:30:47 -0500157 return (FIB_NODE_BACK_WALK_CONTINUE);
158}
159
160/**
161 * Function definition to get a FIB node from its index
162 */
163static fib_node_t*
164vxlan_tunnel_fib_node_get (fib_node_index_t index)
165{
166 vxlan_tunnel_t * t;
167 vxlan_main_t * vxm = &vxlan_main;
168
169 t = pool_elt_at_index(vxm->tunnels, index);
170
171 return (&t->node);
172}
173
174/**
175 * Function definition to inform the FIB node that its last lock has gone.
176 */
177static void
178vxlan_tunnel_last_lock_gone (fib_node_t *node)
179{
180 /*
181 * The VXLAN tunnel is a root of the graph. As such
182 * it never has children and thus is never locked.
183 */
184 ASSERT(0);
185}
186
187/*
188 * Virtual function table registered by VXLAN tunnels
189 * for participation in the FIB object graph.
190 */
191const static fib_node_vft_t vxlan_vft = {
192 .fnv_get = vxlan_tunnel_fib_node_get,
193 .fnv_last_lock = vxlan_tunnel_last_lock_gone,
194 .fnv_back_walk = vxlan_tunnel_back_walk,
195};
196
197
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198#define foreach_copy_field \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199_(vni) \
Eyal Baric5b13602016-11-24 19:42:43 +0200200_(mcast_sw_if_index) \
201_(encap_fib_index) \
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800202_(decap_next_index) \
Eyal Baric5b13602016-11-24 19:42:43 +0200203_(src) \
204_(dst)
Chris Luke99cb3352016-04-26 10:49:53 -0400205
206static int vxlan4_rewrite (vxlan_tunnel_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207{
208 u8 *rw = 0;
209 ip4_header_t * ip0;
210 ip4_vxlan_header_t * h0;
211 int len = sizeof (*h0);
212
213 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
214
215 h0 = (ip4_vxlan_header_t *) rw;
216
217 /* Fixed portion of the (outer) ip4 header */
218 ip0 = &h0->ip4;
219 ip0->ip_version_and_header_length = 0x45;
220 ip0->ttl = 254;
221 ip0->protocol = IP_PROTOCOL_UDP;
222
223 /* we fix up the ip4 header length and checksum after-the-fact */
Chris Luke99cb3352016-04-26 10:49:53 -0400224 ip0->src_address.as_u32 = t->src.ip4.as_u32;
225 ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 ip0->checksum = ip4_header_checksum (ip0);
227
228 /* UDP header, randomize src port on something, maybe? */
229 h0->udp.src_port = clib_host_to_net_u16 (4789);
230 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
231
232 /* VXLAN header */
233 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
234
235 t->rewrite = rw;
236 return (0);
237}
238
Chris Luke99cb3352016-04-26 10:49:53 -0400239static int vxlan6_rewrite (vxlan_tunnel_t * t)
240{
241 u8 *rw = 0;
242 ip6_header_t * ip0;
243 ip6_vxlan_header_t * h0;
244 int len = sizeof (*h0);
245
246 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
247
248 h0 = (ip6_vxlan_header_t *) rw;
249
250 /* Fixed portion of the (outer) ip6 header */
251 ip0 = &h0->ip6;
252 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
253 ip0->hop_limit = 255;
254 ip0->protocol = IP_PROTOCOL_UDP;
255
256 ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
257 ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
258 ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
259 ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
260
261 /* UDP header, randomize src port on something, maybe? */
262 h0->udp.src_port = clib_host_to_net_u16 (4789);
263 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
264
265 /* VXLAN header */
266 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
267
268 t->rewrite = rw;
269 return (0);
270}
271
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800272static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index)
273{
274 vlib_main_t * vm = vxm->vlib_main;
275 vlib_node_runtime_t *r;
276
277 if(!is_ip6)
278 {
279 r = vlib_node_get_runtime (vm, vxlan4_input_node.index);
280 if(decap_next_index >= r->n_next_nodes)
281 return 1;
282 }
283 else
284 {
285 r = vlib_node_get_runtime (vm, vxlan6_input_node.index);
286 if(decap_next_index >= r->n_next_nodes)
287 return 1;
288 }
289
290 return 0;
291}
292
Eyal Bari0ded8512017-01-19 17:01:09 +0200293static void
294hash_set_key_copy (uword ** h, void * key, uword v) {
295 size_t ksz = hash_header(*h)->user;
296 void * copy = clib_mem_alloc (ksz);
297 clib_memcpy (copy, key, ksz);
298 hash_set_mem (*h, copy, v);
299}
300
301static void
302hash_unset_key_free (uword ** h, void * key) {
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
Eyal Barifff67c82016-12-21 12:45:47 +0200310static uword
311vtep_addr_ref(ip46_address_t *ip)
312{
Eyal Bari0ded8512017-01-19 17:01:09 +0200313 uword *vtep = ip46_address_is_ip4(ip) ?
314 hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
315 hash_get_mem (vxlan_main.vtep6, &ip->ip6);
316 if (vtep)
317 return ++(*vtep);
318 ip46_address_is_ip4(ip) ?
319 hash_set (vxlan_main.vtep4, ip->ip4.as_u32, 1) :
320 hash_set_key_copy (&vxlan_main.vtep6, &ip->ip6, 1);
Eyal Barifff67c82016-12-21 12:45:47 +0200321 return 1;
322}
323
324static uword
325vtep_addr_unref(ip46_address_t *ip)
326{
Eyal Bari0ded8512017-01-19 17:01:09 +0200327 uword *vtep = ip46_address_is_ip4(ip) ?
328 hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
329 hash_get_mem (vxlan_main.vtep6, &ip->ip6);
330 ASSERT(vtep);
331 if (--(*vtep) != 0)
332 return *vtep;
333 ip46_address_is_ip4(ip) ?
334 hash_unset (vxlan_main.vtep4, ip->ip4.as_u32) :
335 hash_unset_key_free (&vxlan_main.vtep6, &ip->ip6);
336 return 0;
Eyal Barifff67c82016-12-21 12:45:47 +0200337}
338
Eyal Bari0ded8512017-01-19 17:01:09 +0200339typedef CLIB_PACKED(union {
340 struct {
Neale Ranns32e1c012016-11-22 17:07:28 +0000341 fib_node_index_t mfib_entry_index;
Eyal Bari0ded8512017-01-19 17:01:09 +0200342 adj_index_t mcast_adj_index;
343 };
344 u64 as_u64;
345}) mcast_shared_t;
346
347static inline mcast_shared_t
348mcast_shared_get(ip46_address_t * ip)
Eyal Barifff67c82016-12-21 12:45:47 +0200349{
350 ASSERT(ip46_address_is_multicast(ip));
Eyal Bari0ded8512017-01-19 17:01:09 +0200351 uword * p = hash_get_mem (vxlan_main.mcast_shared, ip);
352 ASSERT(p);
353 return (mcast_shared_t) { .as_u64 = *p };
Eyal Barifff67c82016-12-21 12:45:47 +0200354}
355
Eyal Bari0ded8512017-01-19 17:01:09 +0200356static inline void
Neale Ranns32e1c012016-11-22 17:07:28 +0000357mcast_shared_add(ip46_address_t *dst,
358 fib_node_index_t mfei,
359 adj_index_t ai)
360{
361 mcast_shared_t new_ep = {
362 .mcast_adj_index = ai,
363 .mfib_entry_index = mfei,
364 };
365
366 hash_set_key_copy (&vxlan_main.mcast_shared, dst, new_ep.as_u64);
367}
368
369static inline void
370mcast_shared_remove(ip46_address_t *dst)
371{
372 mcast_shared_t ep = mcast_shared_get(dst);
373
374 adj_unlock(ep.mcast_adj_index);
375 mfib_table_entry_delete_index(ep.mfib_entry_index,
376 MFIB_SOURCE_VXLAN);
377
378 hash_unset_key_free (&vxlan_main.mcast_shared, dst);
Eyal Barifff67c82016-12-21 12:45:47 +0200379}
380
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381int vnet_vxlan_add_del_tunnel
382(vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
383{
384 vxlan_main_t * vxm = &vxlan_main;
385 vxlan_tunnel_t *t = 0;
386 vnet_main_t * vnm = vxm->vnet_main;
Eyal Barifff67c82016-12-21 12:45:47 +0200387 uword * p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 u32 hw_if_index = ~0;
389 u32 sw_if_index = ~0;
390 int rv;
Chris Luke99cb3352016-04-26 10:49:53 -0400391 vxlan4_tunnel_key_t key4;
392 vxlan6_tunnel_key_t key6;
John Loc42912d2016-11-07 18:30:47 -0500393 u32 is_ip6 = a->is_ip6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
John Lo56912c82016-12-08 16:10:02 -0500395 if (!is_ip6)
396 {
397 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
398 key4.vni = clib_host_to_net_u32 (a->vni << 8);
399 p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
John Lo56912c82016-12-08 16:10:02 -0500400 }
401 else
402 {
403 key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
404 key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
405 key6.vni = clib_host_to_net_u32 (a->vni << 8);
406 p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
John Lo56912c82016-12-08 16:10:02 -0500407 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
409 if (a->is_add)
410 {
John Loc42912d2016-11-07 18:30:47 -0500411 l2input_main_t * l2im = &l2input_main;
412
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 /* adding a tunnel: tunnel must not already exist */
414 if (p)
415 return VNET_API_ERROR_TUNNEL_EXIST;
416
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800417 /*if not set explicitly, default to l2 */
418 if(a->decap_next_index == ~0)
419 a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
420 if (vxlan_check_decap_next(vxm, is_ip6, a->decap_next_index))
421 return VNET_API_ERROR_INVALID_DECAP_NEXT;
422
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
424 memset (t, 0, sizeof (*t));
425
426 /* copy from arg structure */
427#define _(x) t->x = a->x;
428 foreach_copy_field;
429#undef _
Chris Luke99cb3352016-04-26 10:49:53 -0400430
John Lo56912c82016-12-08 16:10:02 -0500431 if (!is_ip6)
Chris Luke99cb3352016-04-26 10:49:53 -0400432 rv = vxlan4_rewrite (t);
John Lo56912c82016-12-08 16:10:02 -0500433 else
434 rv = vxlan6_rewrite (t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
436 if (rv)
437 {
438 pool_put (vxm->tunnels, t);
439 return rv;
440 }
441
Eyal Bari5ac9bf52017-01-02 14:29:21 +0200442 /* copy the key */
443 if (is_ip6)
Eyal Bari0ded8512017-01-19 17:01:09 +0200444 hash_set_key_copy (&vxm->vxlan6_tunnel_by_key, &key6, t - vxm->tunnels);
Chris Luke99cb3352016-04-26 10:49:53 -0400445 else
Eyal Bari0ded8512017-01-19 17:01:09 +0200446 hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
Eyal Barifff67c82016-12-21 12:45:47 +0200447
448 vnet_hw_interface_t * hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
450 {
451 vnet_interface_main_t * im = &vnm->interface_main;
452 hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
453 [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
454 _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
455
456 hi = vnet_get_hw_interface (vnm, hw_if_index);
457 hi->dev_instance = t - vxm->tunnels;
458 hi->hw_instance = hi->dev_instance;
459
460 /* clear old stats of freed tunnel before reuse */
461 sw_if_index = hi->sw_if_index;
462 vnet_interface_counter_lock(im);
463 vlib_zero_combined_counter
464 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
465 vlib_zero_combined_counter
466 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
467 vlib_zero_simple_counter
468 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
469 vnet_interface_counter_unlock(im);
470 }
471 else
472 {
473 hw_if_index = vnet_register_interface
474 (vnm, vxlan_device_class.index, t - vxm->tunnels,
475 vxlan_hw_class.index, t - vxm->tunnels);
476 hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 }
478
479 t->hw_if_index = hw_if_index;
480 t->sw_if_index = sw_if_index = hi->sw_if_index;
481
Dave Wallace60231f32015-12-17 21:04:30 -0500482 vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
483 vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
484
John Loc42912d2016-11-07 18:30:47 -0500485 /* setup l2 input config with l2 feature and bd 0 to drop packet */
486 vec_validate (l2im->configs, sw_if_index);
487 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
488 l2im->configs[sw_if_index].bd_index = 0;
John Lo3ef822e2016-06-07 09:14:07 -0400489
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 vnet_sw_interface_set_flags (vnm, sw_if_index,
491 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
John Loc42912d2016-11-07 18:30:47 -0500492 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL);
Eyal Baric5b13602016-11-24 19:42:43 +0200493 fib_prefix_t tun_dst_pfx;
494 u32 encap_index = !is_ip6 ?
495 vxlan4_encap_node.index : vxlan6_encap_node.index;
496 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
John Loc42912d2016-11-07 18:30:47 -0500497
Eyal Baric5b13602016-11-24 19:42:43 +0200498 fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
John Lo56912c82016-12-08 16:10:02 -0500499 if (!ip46_address_is_multicast(&t->dst))
500 {
501 /* Unicast tunnel -
502 * source the FIB entry for the tunnel's destination
503 * and become a child thereof. The tunnel will then get poked
504 * when the forwarding for the entry updates, and the tunnel can
505 * re-stack accordingly
506 */
Eyal Bari0ded8512017-01-19 17:01:09 +0200507 vtep_addr_ref(&t->src);
John Lo56912c82016-12-08 16:10:02 -0500508 t->fib_entry_index = fib_table_entry_special_add
509 (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
510 FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID);
511 t->sibling_index = fib_entry_child_add
512 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels);
513 vxlan_tunnel_restack_dpo(t);
514 }
Eyal Barifff67c82016-12-21 12:45:47 +0200515 else
John Lo56912c82016-12-08 16:10:02 -0500516 {
517 /* Multicast tunnel -
518 * as the same mcast group can be used for mutiple mcast tunnels
519 * with different VNIs, create the output fib adjecency only if
520 * it does not already exist
521 */
Eyal Barifff67c82016-12-21 12:45:47 +0200522 fib_protocol_t fp = (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
Eyal Baric5b13602016-11-24 19:42:43 +0200523 dpo_id_t dpo = DPO_INVALID;
Neale Ranns32e1c012016-11-22 17:07:28 +0000524 mcast_shared_t ep;
Eyal Baric5b13602016-11-24 19:42:43 +0200525
Eyal Barifff67c82016-12-21 12:45:47 +0200526 if (vtep_addr_ref(&t->dst) == 1)
Neale Ranns32e1c012016-11-22 17:07:28 +0000527 {
528 fib_node_index_t mfei;
529 adj_index_t ai;
530 fib_route_path_t path = {
531 .frp_proto = fp,
532 .frp_addr = zero_addr,
533 .frp_sw_if_index = 0xffffffff,
534 .frp_fib_index = ~0,
535 .frp_weight = 0,
536 .frp_flags = FIB_ROUTE_PATH_LOCAL,
537 };
538 const mfib_prefix_t mpfx = {
539 .fp_proto = fp,
540 .fp_len = (is_ip6 ? 128 : 32),
541 .fp_grp_addr = tun_dst_pfx.fp_addr,
542 };
Eyal Barifff67c82016-12-21 12:45:47 +0200543
Neale Ranns32e1c012016-11-22 17:07:28 +0000544 /*
545 * Setup the (*,G) to receive traffic on the mcast group
546 * - the forwarding interface is for-us
547 * - the accepting interface is that from the API
548 */
549 mfib_table_entry_path_update(t->encap_fib_index,
550 &mpfx,
551 MFIB_SOURCE_VXLAN,
552 &path,
553 MFIB_ITF_FLAG_FORWARD);
554
555 path.frp_sw_if_index = a->mcast_sw_if_index;
556 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
557 mfei = mfib_table_entry_path_update(t->encap_fib_index,
558 &mpfx,
559 MFIB_SOURCE_VXLAN,
560 &path,
561 MFIB_ITF_FLAG_ACCEPT);
562
563 /*
564 * Create the mcast adjacency to send traffic to the group
565 */
566 ai = adj_mcast_add_or_lock(fp,
567 fib_proto_to_link(fp),
568 a->mcast_sw_if_index);
569
570 /*
571 * create a new end-point
572 */
573 mcast_shared_add(&t->dst, mfei, ai);
574 }
575
576 ep = mcast_shared_get(&t->dst);
577
Eyal Barifff67c82016-12-21 12:45:47 +0200578 /* Stack shared mcast dst mac addr rewrite on encap */
Neale Ranns32e1c012016-11-22 17:07:28 +0000579 dpo_set (&dpo, DPO_ADJACENCY,
580 fib_proto_to_dpo(fp),
581 ep.mcast_adj_index);
582
Eyal Baric5b13602016-11-24 19:42:43 +0200583 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
Eyal Barifff67c82016-12-21 12:45:47 +0200584 dpo_reset (&dpo);
585 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
John Lo56912c82016-12-08 16:10:02 -0500586 }
587
Eyal Baric5b13602016-11-24 19:42:43 +0200588 /* Set vxlan tunnel output node */
589 hi->output_node_index = encap_index;
590
591 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 }
593 else
594 {
595 /* deleting a tunnel: tunnel must exist */
John Lo37682e12016-11-30 12:51:39 -0500596 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 return VNET_API_ERROR_NO_SUCH_ENTRY;
598
599 t = pool_elt_at_index (vxm->tunnels, p[0]);
600
601 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
602 /* make sure tunnel is removed from l2 bd or xconnect */
603 set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
604 vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
605
Dave Wallace60231f32015-12-17 21:04:30 -0500606 vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
607
John Loc42912d2016-11-07 18:30:47 -0500608 if (!is_ip6)
Eyal Barifff67c82016-12-21 12:45:47 +0200609 hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
Chris Luke99cb3352016-04-26 10:49:53 -0400610 else
Eyal Bari0ded8512017-01-19 17:01:09 +0200611 hash_unset_key_free (&vxm->vxlan6_tunnel_by_key, &key6);
Eyal Barifff67c82016-12-21 12:45:47 +0200612
613 if (!ip46_address_is_multicast(&t->dst))
614 {
Eyal Bari5ac9bf52017-01-02 14:29:21 +0200615 vtep_addr_unref(&t->src);
Eyal Barifff67c82016-12-21 12:45:47 +0200616 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
617 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
618 }
619 else if (vtep_addr_unref(&t->dst) == 0)
620 {
Neale Ranns32e1c012016-11-22 17:07:28 +0000621 mcast_shared_remove(&t->dst);
Eyal Barifff67c82016-12-21 12:45:47 +0200622 }
623
624 fib_node_deinit(&t->node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 vec_free (t->rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626 pool_put (vxm->tunnels, t);
627 }
628
629 if (sw_if_indexp)
630 *sw_if_indexp = sw_if_index;
631
632 return 0;
633}
634
Chris Luke99cb3352016-04-26 10:49:53 -0400635static u32 fib4_index_from_fib_id (u32 fib_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636{
637 ip4_main_t * im = &ip4_main;
638 uword * p;
639
640 p = hash_get (im->fib_index_by_table_id, fib_id);
641 if (!p)
642 return ~0;
643
644 return p[0];
645}
646
Chris Luke99cb3352016-04-26 10:49:53 -0400647static u32 fib6_index_from_fib_id (u32 fib_id)
648{
649 ip6_main_t * im = &ip6_main;
650 uword * p;
651
652 p = hash_get (im->fib_index_by_table_id, fib_id);
653 if (!p)
654 return ~0;
655
656 return p[0];
657}
658
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800659static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set)
660{
661 vxlan_main_t * vxm = &vxlan_main;
662 vlib_main_t * vm = vxm->vlib_main;
663 uword next_index = ~0;
664
665 if (ipv4_set)
666 {
667 next_index = vlib_node_add_next (vm, vxlan4_input_node.index, node_index);
668 }
669 else
670 {
671 next_index = vlib_node_add_next (vm, vxlan6_input_node.index, node_index);
672 }
673
674 return next_index;
675}
676
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677static uword unformat_decap_next (unformat_input_t * input, va_list * args)
678{
679 u32 * result = va_arg (*args, u32 *);
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800680 u32 ipv4_set = va_arg (*args, int);
681 vxlan_main_t * vxm = &vxlan_main;
682 vlib_main_t * vm = vxm->vlib_main;
683 u32 node_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684 u32 tmp;
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800685
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 if (unformat (input, "l2"))
687 *result = VXLAN_INPUT_NEXT_L2_INPUT;
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800688 else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
689 *result = get_decap_next_for_node(node_index, ipv4_set);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690 else if (unformat (input, "%d", &tmp))
691 *result = tmp;
692 else
693 return 0;
694 return 1;
695}
696
697static clib_error_t *
698vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
699 unformat_input_t * input,
700 vlib_cli_command_t * cmd)
701{
702 unformat_input_t _line_input, * line_input = &_line_input;
Eyal Baric5b13602016-11-24 19:42:43 +0200703 ip46_address_t src , dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 u8 is_add = 1;
705 u8 src_set = 0;
706 u8 dst_set = 0;
Eyal Baric5b13602016-11-24 19:42:43 +0200707 u8 grp_set = 0;
Chris Luke99cb3352016-04-26 10:49:53 -0400708 u8 ipv4_set = 0;
709 u8 ipv6_set = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 u32 encap_fib_index = 0;
Eyal Baric5b13602016-11-24 19:42:43 +0200711 u32 mcast_sw_if_index = ~0;
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800712 u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 u32 vni = 0;
714 u32 tmp;
715 int rv;
716 vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
Eyal Baric5b13602016-11-24 19:42:43 +0200717 u32 tunnel_sw_if_index;
718
719 /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
720 memset(&src, 0, sizeof src);
721 memset(&dst, 0, sizeof dst);
722
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723 /* Get a line of input. */
724 if (! unformat_user (input, unformat_line_input, line_input))
725 return 0;
726
727 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
728 if (unformat (line_input, "del"))
Chris Luke99cb3352016-04-26 10:49:53 -0400729 {
730 is_add = 0;
731 }
732 else if (unformat (line_input, "src %U",
733 unformat_ip4_address, &src.ip4))
734 {
735 src_set = 1;
736 ipv4_set = 1;
737 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738 else if (unformat (line_input, "dst %U",
Chris Luke99cb3352016-04-26 10:49:53 -0400739 unformat_ip4_address, &dst.ip4))
740 {
741 dst_set = 1;
742 ipv4_set = 1;
743 }
744 else if (unformat (line_input, "src %U",
745 unformat_ip6_address, &src.ip6))
746 {
747 src_set = 1;
748 ipv6_set = 1;
749 }
750 else if (unformat (line_input, "dst %U",
751 unformat_ip6_address, &dst.ip6))
752 {
753 dst_set = 1;
754 ipv6_set = 1;
755 }
Eyal Baric5b13602016-11-24 19:42:43 +0200756 else if (unformat (line_input, "group %U %U",
757 unformat_ip4_address, &dst.ip4,
758 unformat_vnet_sw_interface,
759 vnet_get_main(), &mcast_sw_if_index))
760 {
761 grp_set = dst_set = 1;
762 ipv4_set = 1;
763 }
764 else if (unformat (line_input, "group %U %U",
765 unformat_ip6_address, &dst.ip6,
766 unformat_vnet_sw_interface,
767 vnet_get_main(), &mcast_sw_if_index))
768 {
769 grp_set = dst_set = 1;
770 ipv6_set = 1;
771 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
773 {
Chris Luke99cb3352016-04-26 10:49:53 -0400774 if (ipv6_set)
775 encap_fib_index = fib6_index_from_fib_id (tmp);
776 else
777 encap_fib_index = fib4_index_from_fib_id (tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 if (encap_fib_index == ~0)
779 return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
780 }
781 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800782 &decap_next_index, ipv4_set))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783 ;
784 else if (unformat (line_input, "vni %d", &vni))
785 {
786 if (vni >> 24)
787 return clib_error_return (0, "vni %d out of range", vni);
788 }
789 else
790 return clib_error_return (0, "parse error: '%U'",
791 format_unformat_error, line_input);
792 }
793
794 unformat_free (line_input);
795
796 if (src_set == 0)
797 return clib_error_return (0, "tunnel src address not specified");
798
799 if (dst_set == 0)
800 return clib_error_return (0, "tunnel dst address not specified");
801
Eyal Baric5b13602016-11-24 19:42:43 +0200802 if (grp_set && !ip46_address_is_multicast(&dst))
803 return clib_error_return (0, "tunnel group address not multicast");
804
John Lo56912c82016-12-08 16:10:02 -0500805 if (grp_set == 0 && ip46_address_is_multicast(&dst))
806 return clib_error_return (0, "dst address must be unicast");
807
Eyal Baric5b13602016-11-24 19:42:43 +0200808 if (grp_set && mcast_sw_if_index == ~0)
809 return clib_error_return (0, "tunnel nonexistent multicast device");
810
Chris Luke99cb3352016-04-26 10:49:53 -0400811 if (ipv4_set && ipv6_set)
812 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
813
Eyal Baric5b13602016-11-24 19:42:43 +0200814 if (ip46_address_cmp(&src, &dst) == 0)
Chris Luke99cb3352016-04-26 10:49:53 -0400815 return clib_error_return (0, "src and dst addresses are identical");
816
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800817 if (decap_next_index == ~0)
818 return clib_error_return (0, "next node not found");
819
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820 if (vni == 0)
821 return clib_error_return (0, "vni not specified");
822
823 memset (a, 0, sizeof (*a));
824
825 a->is_add = is_add;
Chris Luke99cb3352016-04-26 10:49:53 -0400826 a->is_ip6 = ipv6_set;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827
828#define _(x) a->x = x;
829 foreach_copy_field;
830#undef _
831
Eyal Baric5b13602016-11-24 19:42:43 +0200832 rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833
834 switch(rv)
835 {
836 case 0:
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100837 if (is_add)
John Loc42912d2016-11-07 18:30:47 -0500838 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name,
Eyal Baric5b13602016-11-24 19:42:43 +0200839 vnet_get_main(), tunnel_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841
842 case VNET_API_ERROR_TUNNEL_EXIST:
843 return clib_error_return (0, "tunnel already exists...");
844
845 case VNET_API_ERROR_NO_SUCH_ENTRY:
846 return clib_error_return (0, "tunnel does not exist...");
847
848 default:
849 return clib_error_return
850 (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
851 }
852
853 return 0;
854}
855
Billy McFallc5d9cda2016-09-14 10:39:58 -0400856/*?
857 * Add or delete a VXLAN Tunnel.
858 *
859 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
860 * to span multiple servers. This is done by building an L2 overlay on
861 * top of an L3 network underlay using VXLAN tunnels.
862 *
863 * This makes it possible for servers to be co-located in the same data
864 * center or be separated geographically as long as they are reachable
865 * through the underlay L3 network.
866 *
867 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
868 * (Virtual eXtensible VLAN) segment.
869 *
870 * @cliexpar
871 * Example of how to create a VXLAN Tunnel:
John Loc42912d2016-11-07 18:30:47 -0500872 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7}
Billy McFallc5d9cda2016-09-14 10:39:58 -0400873 * Example of how to delete a VXLAN Tunnel:
874 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
875 ?*/
876/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
878 .path = "create vxlan tunnel",
879 .short_help =
Eyal Baric5b13602016-11-24 19:42:43 +0200880 "create vxlan tunnel src <local-vtep-addr>"
881 " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800882 " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883 .function = vxlan_add_del_tunnel_command_fn,
884};
Billy McFallc5d9cda2016-09-14 10:39:58 -0400885/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886
887static clib_error_t *
888show_vxlan_tunnel_command_fn (vlib_main_t * vm,
889 unformat_input_t * input,
890 vlib_cli_command_t * cmd)
891{
892 vxlan_main_t * vxm = &vxlan_main;
893 vxlan_tunnel_t * t;
894
895 if (pool_elts (vxm->tunnels) == 0)
896 vlib_cli_output (vm, "No vxlan tunnels configured...");
897
898 pool_foreach (t, vxm->tunnels,
899 ({
900 vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
901 }));
902
903 return 0;
904}
905
Billy McFallc5d9cda2016-09-14 10:39:58 -0400906/*?
907 * Display all the VXLAN Tunnel entries.
908 *
909 * @cliexpar
910 * Example of how to display the VXLAN Tunnel entries:
911 * @cliexstart{show vxlan tunnel}
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800912 * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
Billy McFallc5d9cda2016-09-14 10:39:58 -0400913 * @cliexend
914 ?*/
915/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
917 .path = "show vxlan tunnel",
Billy McFallc5d9cda2016-09-14 10:39:58 -0400918 .short_help = "show vxlan tunnel",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 .function = show_vxlan_tunnel_command_fn,
920};
Billy McFallc5d9cda2016-09-14 10:39:58 -0400921/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922
Chris Luke99cb3352016-04-26 10:49:53 -0400923
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924clib_error_t *vxlan_init (vlib_main_t *vm)
925{
926 vxlan_main_t * vxm = &vxlan_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927
928 vxm->vnet_main = vnet_get_main();
929 vxm->vlib_main = vm;
930
Chris Luke99cb3352016-04-26 10:49:53 -0400931 /* initialize the ip6 hash */
932 vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
933 sizeof(vxlan6_tunnel_key_t),
934 sizeof(uword));
Eyal Bari0ded8512017-01-19 17:01:09 +0200935 vxm->vtep6 = hash_create_mem(0,
936 sizeof(ip6_address_t),
Eyal Barifff67c82016-12-21 12:45:47 +0200937 sizeof(uword));
Eyal Bari0ded8512017-01-19 17:01:09 +0200938 vxm->mcast_shared = hash_create_mem(0,
939 sizeof(ip46_address_t),
940 sizeof(mcast_shared_t));
Chris Luke99cb3352016-04-26 10:49:53 -0400941
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
Chris Luke99cb3352016-04-26 10:49:53 -0400943 vxlan4_input_node.index, /* is_ip4 */ 1);
944 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
945 vxlan6_input_node.index, /* is_ip4 */ 0);
John Loc42912d2016-11-07 18:30:47 -0500946
947 fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft);
948
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949 return 0;
950}
951
952VLIB_INIT_FUNCTION(vxlan_init);