blob: 3c6d7bd9714175762ee3dabb46a0653d4cb4d967 [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>
Eyal Baric5b13602016-11-24 19:42:43 +020019#include <vnet/dpo/receive_dpo.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020
Billy McFallc5d9cda2016-09-14 10:39:58 -040021/**
22 * @file
23 * @brief VXLAN.
24 *
25 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
26 * to span multiple servers. This is done by building an L2 overlay on
27 * top of an L3 network underlay using VXLAN tunnels.
28 *
29 * This makes it possible for servers to be co-located in the same data
30 * center or be separated geographically as long as they are reachable
31 * through the underlay L3 network.
32 *
33 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
34 * (Virtual eXtensible VLAN) segment.
35 */
36
37
Ed Warnickecb9cada2015-12-08 15:45:58 -070038vxlan_main_t vxlan_main;
39
Ed Warnickecb9cada2015-12-08 15:45:58 -070040u8 * format_vxlan_tunnel (u8 * s, va_list * args)
41{
42 vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
43 vxlan_main_t * ngm = &vxlan_main;
44
45 s = format (s,
John Loc42912d2016-11-07 18:30:47 -050046 "[%d] src %U dst %U vni %d encap_fib_index %d sw_if_index %d "
47 "fib_entry_index %d\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 t - ngm->tunnels,
Damjan Marion86be4872016-05-24 23:19:11 +020049 format_ip46_address, &t->src, IP46_TYPE_ANY,
50 format_ip46_address, &t->dst, IP46_TYPE_ANY,
John Loc42912d2016-11-07 18:30:47 -050051 t->vni, t->encap_fib_index, t->sw_if_index, t->fib_entry_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 return s;
53}
54
55static u8 * format_vxlan_name (u8 * s, va_list * args)
56{
57 u32 dev_instance = va_arg (*args, u32);
58 return format (s, "vxlan_tunnel%d", dev_instance);
59}
60
61static uword dummy_interface_tx (vlib_main_t * vm,
62 vlib_node_runtime_t * node,
63 vlib_frame_t * frame)
64{
65 clib_warning ("you shouldn't be here, leaking buffers...");
66 return frame->n_vectors;
67}
68
Pavel Kotucek988a7c42016-02-29 15:03:08 +010069static clib_error_t *
70vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
71{
72 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
73 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
74 else
75 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
76
77 return /* no error */ 0;
78}
79
Ed Warnickecb9cada2015-12-08 15:45:58 -070080VNET_DEVICE_CLASS (vxlan_device_class,static) = {
81 .name = "VXLAN",
82 .format_device_name = format_vxlan_name,
83 .format_tx_trace = format_vxlan_encap_trace,
84 .tx_function = dummy_interface_tx,
Pavel Kotucek988a7c42016-02-29 15:03:08 +010085 .admin_up_down_function = vxlan_interface_admin_up_down,
Ed Warnickecb9cada2015-12-08 15:45:58 -070086};
87
Ed Warnickecb9cada2015-12-08 15:45:58 -070088static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
89{
90 u32 dev_instance = va_arg (*args, u32);
91 s = format (s, "unimplemented dev %u", dev_instance);
92 return s;
93}
94
95VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
96 .name = "VXLAN",
97 .format_header = format_vxlan_header_with_length,
Neale Rannsb80c5362016-10-08 13:03:40 +010098 .build_rewrite = default_build_rewrite,
Ed Warnickecb9cada2015-12-08 15:45:58 -070099};
100
Eyal Baric5b13602016-11-24 19:42:43 +0200101static void
102vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t)
103{
104 dpo_id_t dpo = DPO_INVALID;
105 u32 encap_index = ip46_address_is_ip4(&t->dst) ?
106 vxlan4_encap_node.index : vxlan6_encap_node.index;
107 fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->dst) ?
108 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
109
110 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
111 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
112 dpo_reset(&dpo);
113}
John Loc42912d2016-11-07 18:30:47 -0500114
115static vxlan_tunnel_t *
116vxlan_tunnel_from_fib_node (fib_node_t *node)
117{
118#if (CLIB_DEBUG > 0)
119 ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type);
120#endif
121 return ((vxlan_tunnel_t*) (((char*)node) -
122 STRUCT_OFFSET_OF(vxlan_tunnel_t, node)));
123}
124
125/**
126 * Function definition to backwalk a FIB node -
127 * Here we will restack the new dpo of VXLAN DIP to encap node.
128 */
129static fib_node_back_walk_rc_t
130vxlan_tunnel_back_walk (fib_node_t *node,
131 fib_node_back_walk_ctx_t *ctx)
132{
Eyal Baric5b13602016-11-24 19:42:43 +0200133 vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node));
John Loc42912d2016-11-07 18:30:47 -0500134 return (FIB_NODE_BACK_WALK_CONTINUE);
135}
136
137/**
138 * Function definition to get a FIB node from its index
139 */
140static fib_node_t*
141vxlan_tunnel_fib_node_get (fib_node_index_t index)
142{
143 vxlan_tunnel_t * t;
144 vxlan_main_t * vxm = &vxlan_main;
145
146 t = pool_elt_at_index(vxm->tunnels, index);
147
148 return (&t->node);
149}
150
151/**
152 * Function definition to inform the FIB node that its last lock has gone.
153 */
154static void
155vxlan_tunnel_last_lock_gone (fib_node_t *node)
156{
157 /*
158 * The VXLAN tunnel is a root of the graph. As such
159 * it never has children and thus is never locked.
160 */
161 ASSERT(0);
162}
163
164/*
165 * Virtual function table registered by VXLAN tunnels
166 * for participation in the FIB object graph.
167 */
168const static fib_node_vft_t vxlan_vft = {
169 .fnv_get = vxlan_tunnel_fib_node_get,
170 .fnv_last_lock = vxlan_tunnel_last_lock_gone,
171 .fnv_back_walk = vxlan_tunnel_back_walk,
172};
173
174
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175#define foreach_copy_field \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176_(vni) \
Eyal Baric5b13602016-11-24 19:42:43 +0200177_(mcast_sw_if_index) \
178_(encap_fib_index) \
179_(src) \
180_(dst)
Chris Luke99cb3352016-04-26 10:49:53 -0400181
182static int vxlan4_rewrite (vxlan_tunnel_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183{
184 u8 *rw = 0;
185 ip4_header_t * ip0;
186 ip4_vxlan_header_t * h0;
187 int len = sizeof (*h0);
188
189 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
190
191 h0 = (ip4_vxlan_header_t *) rw;
192
193 /* Fixed portion of the (outer) ip4 header */
194 ip0 = &h0->ip4;
195 ip0->ip_version_and_header_length = 0x45;
196 ip0->ttl = 254;
197 ip0->protocol = IP_PROTOCOL_UDP;
198
199 /* we fix up the ip4 header length and checksum after-the-fact */
Chris Luke99cb3352016-04-26 10:49:53 -0400200 ip0->src_address.as_u32 = t->src.ip4.as_u32;
201 ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 ip0->checksum = ip4_header_checksum (ip0);
203
204 /* UDP header, randomize src port on something, maybe? */
205 h0->udp.src_port = clib_host_to_net_u16 (4789);
206 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
207
208 /* VXLAN header */
209 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
210
211 t->rewrite = rw;
212 return (0);
213}
214
Chris Luke99cb3352016-04-26 10:49:53 -0400215static int vxlan6_rewrite (vxlan_tunnel_t * t)
216{
217 u8 *rw = 0;
218 ip6_header_t * ip0;
219 ip6_vxlan_header_t * h0;
220 int len = sizeof (*h0);
221
222 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
223
224 h0 = (ip6_vxlan_header_t *) rw;
225
226 /* Fixed portion of the (outer) ip6 header */
227 ip0 = &h0->ip6;
228 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
229 ip0->hop_limit = 255;
230 ip0->protocol = IP_PROTOCOL_UDP;
231
232 ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
233 ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
234 ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
235 ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
236
237 /* UDP header, randomize src port on something, maybe? */
238 h0->udp.src_port = clib_host_to_net_u16 (4789);
239 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
240
241 /* VXLAN header */
242 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
243
244 t->rewrite = rw;
245 return (0);
246}
247
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248int vnet_vxlan_add_del_tunnel
249(vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
250{
251 vxlan_main_t * vxm = &vxlan_main;
252 vxlan_tunnel_t *t = 0;
253 vnet_main_t * vnm = vxm->vnet_main;
254 vnet_hw_interface_t * hi;
255 uword * p;
256 u32 hw_if_index = ~0;
257 u32 sw_if_index = ~0;
258 int rv;
Chris Luke99cb3352016-04-26 10:49:53 -0400259 vxlan4_tunnel_key_t key4;
260 vxlan6_tunnel_key_t key6;
John Loc42912d2016-11-07 18:30:47 -0500261 u32 is_ip6 = a->is_ip6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
John Loc42912d2016-11-07 18:30:47 -0500263 if (!is_ip6) {
Chris Luke99cb3352016-04-26 10:49:53 -0400264 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
265 key4.vni = clib_host_to_net_u32 (a->vni << 8);
266
267 p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
268 } else {
269 key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
270 key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
271 key6.vni = clib_host_to_net_u32 (a->vni << 8);
272
Chris Lukec7949152016-05-31 10:42:14 -0400273 p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
Chris Luke99cb3352016-04-26 10:49:53 -0400274 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275
276 if (a->is_add)
277 {
John Loc42912d2016-11-07 18:30:47 -0500278 l2input_main_t * l2im = &l2input_main;
279
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 /* adding a tunnel: tunnel must not already exist */
281 if (p)
282 return VNET_API_ERROR_TUNNEL_EXIST;
283
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
285 memset (t, 0, sizeof (*t));
286
287 /* copy from arg structure */
288#define _(x) t->x = a->x;
289 foreach_copy_field;
290#undef _
291
Chris Lukec7949152016-05-31 10:42:14 -0400292 /* copy the key */
John Loc42912d2016-11-07 18:30:47 -0500293 if (is_ip6)
Chris Lukec7949152016-05-31 10:42:14 -0400294 {
295 t->key6 = clib_mem_alloc (sizeof(vxlan6_tunnel_key_t));
296 clib_memcpy (t->key6, &key6, sizeof(key6));
297 }
298 else
299 {
300 t->key4 = 0; /* not yet used */
301 }
Chris Luke99cb3352016-04-26 10:49:53 -0400302
John Loc42912d2016-11-07 18:30:47 -0500303 if (!is_ip6) {
Chris Luke99cb3352016-04-26 10:49:53 -0400304 rv = vxlan4_rewrite (t);
305 } else {
306 rv = vxlan6_rewrite (t);
307 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
309 if (rv)
310 {
311 pool_put (vxm->tunnels, t);
312 return rv;
313 }
314
John Loc42912d2016-11-07 18:30:47 -0500315 if (!is_ip6)
Chris Luke99cb3352016-04-26 10:49:53 -0400316 hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
317 else
Chris Lukec7949152016-05-31 10:42:14 -0400318 hash_set_mem (vxm->vxlan6_tunnel_by_key, t->key6, t - vxm->tunnels);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
320 if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
321 {
322 vnet_interface_main_t * im = &vnm->interface_main;
323 hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
324 [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
325 _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
326
327 hi = vnet_get_hw_interface (vnm, hw_if_index);
328 hi->dev_instance = t - vxm->tunnels;
329 hi->hw_instance = hi->dev_instance;
330
331 /* clear old stats of freed tunnel before reuse */
332 sw_if_index = hi->sw_if_index;
333 vnet_interface_counter_lock(im);
334 vlib_zero_combined_counter
335 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
336 vlib_zero_combined_counter
337 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
338 vlib_zero_simple_counter
339 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
340 vnet_interface_counter_unlock(im);
341 }
342 else
343 {
344 hw_if_index = vnet_register_interface
345 (vnm, vxlan_device_class.index, t - vxm->tunnels,
346 vxlan_hw_class.index, t - vxm->tunnels);
347 hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 }
349
350 t->hw_if_index = hw_if_index;
351 t->sw_if_index = sw_if_index = hi->sw_if_index;
352
Dave Wallace60231f32015-12-17 21:04:30 -0500353 vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
354 vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
355
John Loc42912d2016-11-07 18:30:47 -0500356 /* setup l2 input config with l2 feature and bd 0 to drop packet */
357 vec_validate (l2im->configs, sw_if_index);
358 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
359 l2im->configs[sw_if_index].bd_index = 0;
John Lo3ef822e2016-06-07 09:14:07 -0400360
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 vnet_sw_interface_set_flags (vnm, sw_if_index,
362 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
John Loc42912d2016-11-07 18:30:47 -0500363 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL);
Eyal Baric5b13602016-11-24 19:42:43 +0200364 fib_prefix_t tun_dst_pfx;
365 u32 encap_index = !is_ip6 ?
366 vxlan4_encap_node.index : vxlan6_encap_node.index;
367 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
John Loc42912d2016-11-07 18:30:47 -0500368
Eyal Baric5b13602016-11-24 19:42:43 +0200369 fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
370 if (ip46_address_is_multicast(&t->dst))
371 {
372 fib_protocol_t fp;
373 u8 mcast_mac[6];
374 if (!is_ip6) {
375 ip4_multicast_ethernet_address(mcast_mac, &t->dst.ip4);
376 fp = FIB_PROTOCOL_IP4;
377 } else {
378 ip6_multicast_ethernet_address(mcast_mac, t->dst.ip6.as_u32[0]);
379 fp = FIB_PROTOCOL_IP6;
380 }
381 t->mcast_adj_index = adj_rewrite_add_and_lock
382 (fp, fib_proto_to_link(fp), t->mcast_sw_if_index, mcast_mac);
John Loc42912d2016-11-07 18:30:47 -0500383
Eyal Baric5b13602016-11-24 19:42:43 +0200384 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
John Loc42912d2016-11-07 18:30:47 -0500385
Eyal Baric5b13602016-11-24 19:42:43 +0200386 /* Stack mcast dst mac addr rewrite on encap */
387 dpo_proto_t dproto = fib_proto_to_dpo(fp);
388 dpo_id_t dpo = DPO_INVALID;
389
390 dpo_set (&dpo, DPO_ADJACENCY, dproto, t->mcast_adj_index);
391 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
392 dpo_reset(&dpo);
393
394 /* Add local mcast adj. */
395 receive_dpo_add_or_lock(dproto, ~0, NULL, &dpo);
396 t->fib_entry_index = fib_table_entry_special_dpo_add
397 (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_SPECIAL, FIB_ENTRY_FLAG_NONE, &dpo);
398 dpo_reset(&dpo);
399 } else {
400 /*
401 * source the FIB entry for the tunnel's destination
402 * and become a child thereof. The tunnel will then get poked
403 * when the forwarding for the entry updates, and the tunnel can
404 * re-stack accordingly
405 */
406 t->fib_entry_index = fib_table_entry_special_add
407 (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID);
408 t->sibling_index = fib_entry_child_add
409 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels);
410 vxlan_tunnel_restack_dpo(t);
411 }
412 /* Set vxlan tunnel output node */
413 hi->output_node_index = encap_index;
414
415 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 }
417 else
418 {
419 /* deleting a tunnel: tunnel must exist */
420 if (!p)
421 return VNET_API_ERROR_NO_SUCH_ENTRY;
422
423 t = pool_elt_at_index (vxm->tunnels, p[0]);
424
425 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
426 /* make sure tunnel is removed from l2 bd or xconnect */
427 set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
428 vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
429
Dave Wallace60231f32015-12-17 21:04:30 -0500430 vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
431
Eyal Baric5b13602016-11-24 19:42:43 +0200432 if (ip46_address_is_multicast(&t->dst))
433 {
434 adj_unlock(t->mcast_adj_index);
435 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_SPECIAL);
436 }
437 else
438 {
439 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
440 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
441 }
John Loc42912d2016-11-07 18:30:47 -0500442 fib_node_deinit(&t->node);
443
444 if (!is_ip6)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100445 {
446 hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100447 }
Chris Luke99cb3352016-04-26 10:49:53 -0400448 else
John Locb9f3d72016-08-30 00:10:09 -0400449 {
450 hash_unset_mem (vxm->vxlan6_tunnel_by_key, t->key6);
451 clib_mem_free (t->key6);
452 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 vec_free (t->rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454 pool_put (vxm->tunnels, t);
455 }
456
457 if (sw_if_indexp)
458 *sw_if_indexp = sw_if_index;
459
460 return 0;
461}
462
Chris Luke99cb3352016-04-26 10:49:53 -0400463static u32 fib4_index_from_fib_id (u32 fib_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464{
465 ip4_main_t * im = &ip4_main;
466 uword * p;
467
468 p = hash_get (im->fib_index_by_table_id, fib_id);
469 if (!p)
470 return ~0;
471
472 return p[0];
473}
474
Chris Luke99cb3352016-04-26 10:49:53 -0400475static u32 fib6_index_from_fib_id (u32 fib_id)
476{
477 ip6_main_t * im = &ip6_main;
478 uword * p;
479
480 p = hash_get (im->fib_index_by_table_id, fib_id);
481 if (!p)
482 return ~0;
483
484 return p[0];
485}
486
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487static uword unformat_decap_next (unformat_input_t * input, va_list * args)
488{
489 u32 * result = va_arg (*args, u32 *);
490 u32 tmp;
491
492 if (unformat (input, "l2"))
493 *result = VXLAN_INPUT_NEXT_L2_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 else if (unformat (input, "%d", &tmp))
495 *result = tmp;
496 else
497 return 0;
498 return 1;
499}
500
501static clib_error_t *
502vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
503 unformat_input_t * input,
504 vlib_cli_command_t * cmd)
505{
506 unformat_input_t _line_input, * line_input = &_line_input;
Eyal Baric5b13602016-11-24 19:42:43 +0200507 ip46_address_t src , dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 u8 is_add = 1;
509 u8 src_set = 0;
510 u8 dst_set = 0;
Eyal Baric5b13602016-11-24 19:42:43 +0200511 u8 grp_set = 0;
Chris Luke99cb3352016-04-26 10:49:53 -0400512 u8 ipv4_set = 0;
513 u8 ipv6_set = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 u32 encap_fib_index = 0;
Eyal Baric5b13602016-11-24 19:42:43 +0200515 u32 mcast_sw_if_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 u32 decap_next_index = ~0;
517 u32 vni = 0;
518 u32 tmp;
519 int rv;
520 vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
Eyal Baric5b13602016-11-24 19:42:43 +0200521 u32 tunnel_sw_if_index;
522
523 /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
524 memset(&src, 0, sizeof src);
525 memset(&dst, 0, sizeof dst);
526
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 /* Get a line of input. */
528 if (! unformat_user (input, unformat_line_input, line_input))
529 return 0;
530
531 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
532 if (unformat (line_input, "del"))
Chris Luke99cb3352016-04-26 10:49:53 -0400533 {
534 is_add = 0;
535 }
536 else if (unformat (line_input, "src %U",
537 unformat_ip4_address, &src.ip4))
538 {
539 src_set = 1;
540 ipv4_set = 1;
541 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 else if (unformat (line_input, "dst %U",
Chris Luke99cb3352016-04-26 10:49:53 -0400543 unformat_ip4_address, &dst.ip4))
544 {
545 dst_set = 1;
546 ipv4_set = 1;
547 }
548 else if (unformat (line_input, "src %U",
549 unformat_ip6_address, &src.ip6))
550 {
551 src_set = 1;
552 ipv6_set = 1;
553 }
554 else if (unformat (line_input, "dst %U",
555 unformat_ip6_address, &dst.ip6))
556 {
557 dst_set = 1;
558 ipv6_set = 1;
559 }
Eyal Baric5b13602016-11-24 19:42:43 +0200560 else if (unformat (line_input, "group %U %U",
561 unformat_ip4_address, &dst.ip4,
562 unformat_vnet_sw_interface,
563 vnet_get_main(), &mcast_sw_if_index))
564 {
565 grp_set = dst_set = 1;
566 ipv4_set = 1;
567 }
568 else if (unformat (line_input, "group %U %U",
569 unformat_ip6_address, &dst.ip6,
570 unformat_vnet_sw_interface,
571 vnet_get_main(), &mcast_sw_if_index))
572 {
573 grp_set = dst_set = 1;
574 ipv6_set = 1;
575 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
577 {
Chris Luke99cb3352016-04-26 10:49:53 -0400578 if (ipv6_set)
579 encap_fib_index = fib6_index_from_fib_id (tmp);
580 else
581 encap_fib_index = fib4_index_from_fib_id (tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 if (encap_fib_index == ~0)
583 return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
584 }
585 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
586 &decap_next_index))
587 ;
588 else if (unformat (line_input, "vni %d", &vni))
589 {
590 if (vni >> 24)
591 return clib_error_return (0, "vni %d out of range", vni);
592 }
593 else
594 return clib_error_return (0, "parse error: '%U'",
595 format_unformat_error, line_input);
596 }
597
598 unformat_free (line_input);
599
600 if (src_set == 0)
601 return clib_error_return (0, "tunnel src address not specified");
602
603 if (dst_set == 0)
604 return clib_error_return (0, "tunnel dst address not specified");
605
Eyal Baric5b13602016-11-24 19:42:43 +0200606 if (grp_set && !ip46_address_is_multicast(&dst))
607 return clib_error_return (0, "tunnel group address not multicast");
608
609 if (grp_set && mcast_sw_if_index == ~0)
610 return clib_error_return (0, "tunnel nonexistent multicast device");
611
Chris Luke99cb3352016-04-26 10:49:53 -0400612 if (ipv4_set && ipv6_set)
613 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
614
Eyal Baric5b13602016-11-24 19:42:43 +0200615 if (ip46_address_cmp(&src, &dst) == 0)
Chris Luke99cb3352016-04-26 10:49:53 -0400616 return clib_error_return (0, "src and dst addresses are identical");
617
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618 if (vni == 0)
619 return clib_error_return (0, "vni not specified");
620
621 memset (a, 0, sizeof (*a));
622
623 a->is_add = is_add;
Chris Luke99cb3352016-04-26 10:49:53 -0400624 a->is_ip6 = ipv6_set;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625
626#define _(x) a->x = x;
627 foreach_copy_field;
628#undef _
629
Eyal Baric5b13602016-11-24 19:42:43 +0200630 rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
632 switch(rv)
633 {
634 case 0:
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100635 if (is_add)
John Loc42912d2016-11-07 18:30:47 -0500636 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name,
Eyal Baric5b13602016-11-24 19:42:43 +0200637 vnet_get_main(), tunnel_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639
640 case VNET_API_ERROR_TUNNEL_EXIST:
641 return clib_error_return (0, "tunnel already exists...");
642
643 case VNET_API_ERROR_NO_SUCH_ENTRY:
644 return clib_error_return (0, "tunnel does not exist...");
645
646 default:
647 return clib_error_return
648 (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
649 }
650
651 return 0;
652}
653
Billy McFallc5d9cda2016-09-14 10:39:58 -0400654/*?
655 * Add or delete a VXLAN Tunnel.
656 *
657 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
658 * to span multiple servers. This is done by building an L2 overlay on
659 * top of an L3 network underlay using VXLAN tunnels.
660 *
661 * This makes it possible for servers to be co-located in the same data
662 * center or be separated geographically as long as they are reachable
663 * through the underlay L3 network.
664 *
665 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
666 * (Virtual eXtensible VLAN) segment.
667 *
668 * @cliexpar
669 * Example of how to create a VXLAN Tunnel:
John Loc42912d2016-11-07 18:30:47 -0500670 * @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 -0400671 * Example of how to delete a VXLAN Tunnel:
672 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
673 ?*/
674/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
676 .path = "create vxlan tunnel",
677 .short_help =
Eyal Baric5b13602016-11-24 19:42:43 +0200678 "create vxlan tunnel src <local-vtep-addr>"
679 " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
John Loc42912d2016-11-07 18:30:47 -0500680 " [encap-vrf-id <nn>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681 .function = vxlan_add_del_tunnel_command_fn,
682};
Billy McFallc5d9cda2016-09-14 10:39:58 -0400683/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684
685static clib_error_t *
686show_vxlan_tunnel_command_fn (vlib_main_t * vm,
687 unformat_input_t * input,
688 vlib_cli_command_t * cmd)
689{
690 vxlan_main_t * vxm = &vxlan_main;
691 vxlan_tunnel_t * t;
692
693 if (pool_elts (vxm->tunnels) == 0)
694 vlib_cli_output (vm, "No vxlan tunnels configured...");
695
696 pool_foreach (t, vxm->tunnels,
697 ({
698 vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
699 }));
700
701 return 0;
702}
703
Billy McFallc5d9cda2016-09-14 10:39:58 -0400704/*?
705 * Display all the VXLAN Tunnel entries.
706 *
707 * @cliexpar
708 * Example of how to display the VXLAN Tunnel entries:
709 * @cliexstart{show vxlan tunnel}
John Loc42912d2016-11-07 18:30:47 -0500710 * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5
Billy McFallc5d9cda2016-09-14 10:39:58 -0400711 * @cliexend
712 ?*/
713/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
715 .path = "show vxlan tunnel",
Billy McFallc5d9cda2016-09-14 10:39:58 -0400716 .short_help = "show vxlan tunnel",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 .function = show_vxlan_tunnel_command_fn,
718};
Billy McFallc5d9cda2016-09-14 10:39:58 -0400719/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
Chris Luke99cb3352016-04-26 10:49:53 -0400721
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722clib_error_t *vxlan_init (vlib_main_t *vm)
723{
724 vxlan_main_t * vxm = &vxlan_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725
726 vxm->vnet_main = vnet_get_main();
727 vxm->vlib_main = vm;
728
Chris Luke99cb3352016-04-26 10:49:53 -0400729 /* initialize the ip6 hash */
730 vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
731 sizeof(vxlan6_tunnel_key_t),
732 sizeof(uword));
733
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
Chris Luke99cb3352016-04-26 10:49:53 -0400735 vxlan4_input_node.index, /* is_ip4 */ 1);
736 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
737 vxlan6_input_node.index, /* is_ip4 */ 0);
John Loc42912d2016-11-07 18:30:47 -0500738
739 fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft);
740
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741 return 0;
742}
743
744VLIB_INIT_FUNCTION(vxlan_init);