blob: 3a92c88fbb687b5607ce931dbeb3ef967a38f74e [file] [log] [blame]
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -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 */
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -070015/**
16 * @file
17 * @brief Common utility functions for IPv4 and IPv6 VXLAN GPE tunnels
18 *
19*/
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070020#include <vnet/vxlan-gpe/vxlan_gpe.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/fib/fib.h>
Hongjun Nidf921cc2016-05-25 01:16:19 +080022#include <vnet/ip/format.h>
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080023#include <vnet/fib/fib_entry.h>
24#include <vnet/fib/fib_table.h>
25#include <vnet/mfib/mfib_table.h>
26#include <vnet/adj/adj_mcast.h>
27#include <vnet/interface.h>
28#include <vlib/vlib.h>
29
30/**
31 * @file
32 * @brief VXLAN-GPE.
33 *
34 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
35 * to span multiple servers. This is done by building an L2 overlay on
36 * top of an L3 network underlay using VXLAN-GPE tunnels.
37 *
38 * This makes it possible for servers to be co-located in the same data
39 * center or be separated geographically as long as they are reachable
40 * through the underlay L3 network.
41 *
42 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE segment.
43 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070044
45vxlan_gpe_main_t vxlan_gpe_main;
46
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -070047/**
48 * @brief Tracing function for VXLAN GPE tunnel packets
49 *
50 * @param *s formatting string
51 * @param *args
52 *
53 * @return *s formatted string
54 *
55 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070056u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
57{
58 vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080059 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070060
61 s = format (s, "[%d] local: %U remote: %U ",
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080062 t - ngm->tunnels,
Hongjun Nic8af24f2016-08-05 00:58:20 +080063 format_ip46_address, &t->local, IP46_TYPE_ANY,
64 format_ip46_address, &t->remote, IP46_TYPE_ANY);
Hongjun Nidf921cc2016-05-25 01:16:19 +080065
66 s = format (s, " vxlan VNI %d ", t->vni);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070067
68 switch (t->protocol)
69 {
70 case VXLAN_GPE_PROTOCOL_IP4:
71 s = format (s, "next-protocol ip4");
Hongjun Ni4a713142016-05-24 02:54:10 +080072 break;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070073 case VXLAN_GPE_PROTOCOL_IP6:
74 s = format (s, "next-protocol ip6");
Hongjun Ni4a713142016-05-24 02:54:10 +080075 break;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070076 case VXLAN_GPE_PROTOCOL_ETHERNET:
77 s = format (s, "next-protocol ethernet");
Hongjun Ni4a713142016-05-24 02:54:10 +080078 break;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070079 case VXLAN_GPE_PROTOCOL_NSH:
80 s = format (s, "next-protocol nsh");
Hongjun Ni4a713142016-05-24 02:54:10 +080081 break;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070082 default:
83 s = format (s, "next-protocol unknown %d", t->protocol);
84 }
85
86 s = format (s, " fibs: (encap %d, decap %d)",
87 t->encap_fib_index,
88 t->decap_fib_index);
89
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070090 return s;
91}
92
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -070093/**
94 * @brief Naming for VXLAN GPE tunnel
95 *
96 * @param *s formatting string
97 * @param *args
98 *
99 * @return *s formatted string
100 *
101 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700102static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
103{
Hongjun Ni0e06e2b2016-05-30 19:45:51 +0800104 u32 dev_instance = va_arg (*args, u32);
105 return format (s, "vxlan_gpe_tunnel%d", dev_instance);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700106}
107
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700108static uword dummy_interface_tx (vlib_main_t * vm,
109 vlib_node_runtime_t * node,
110 vlib_frame_t * frame)
111{
112 clib_warning ("you shouldn't be here, leaking buffers...");
113 return frame->n_vectors;
114}
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700115
116/**
117 * @brief CLI function for VXLAN GPE admin up/down
118 *
119 * @param *vnm
120 * @param hw_if_index
121 * @param flag
122 *
123 * @return *rc
124 *
125 */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800126static clib_error_t *
127vxlan_gpe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
128{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800129 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
130 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
131 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800132
133 return 0;
134}
135
Hongjun Ni0e06e2b2016-05-30 19:45:51 +0800136VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
137 .name = "VXLAN_GPE",
138 .format_device_name = format_vxlan_gpe_name,
139 .format_tx_trace = format_vxlan_gpe_encap_trace,
140 .tx_function = dummy_interface_tx,
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800141 .admin_up_down_function = vxlan_gpe_interface_admin_up_down,
Hongjun Ni0e06e2b2016-05-30 19:45:51 +0800142};
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700143
Vengada Govindan6d403a02016-10-12 05:54:09 -0700144
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700145/**
146 * @brief Formatting function for tracing VXLAN GPE with length
147 *
148 * @param *s
149 * @param *args
150 *
151 * @return *s
152 *
153 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700154static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
155{
156 u32 dev_instance = va_arg (*args, u32);
157 s = format (s, "unimplemented dev %u", dev_instance);
158 return s;
159}
160
161VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
162 .name = "VXLAN_GPE",
163 .format_header = format_vxlan_gpe_header_with_length,
Neale Rannsb80c5362016-10-08 13:03:40 +0100164 .build_rewrite = default_build_rewrite,
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700165};
166
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800167static void
168vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_t * t)
169{
170 dpo_id_t dpo = DPO_INVALID;
171 u32 encap_index = vxlan_gpe_encap_node.index;
172 fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->remote) ?
173 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
174
175 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
176 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
177 dpo_reset(&dpo);
178}
179
180static vxlan_gpe_tunnel_t *
181vxlan_gpe_tunnel_from_fib_node (fib_node_t *node)
182{
183#if (CLIB_DEBUG > 0)
184 ASSERT(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL == node->fn_type);
185#endif
186 return ((vxlan_gpe_tunnel_t*) (((char*)node) -
187 STRUCT_OFFSET_OF(vxlan_gpe_tunnel_t, node)));
188}
189
190/**
191 * Function definition to backwalk a FIB node -
192 * Here we will restack the new dpo of VXLAN_GPE DIP to encap node.
193 */
194static fib_node_back_walk_rc_t
195vxlan_gpe_tunnel_back_walk (fib_node_t *node,
196 fib_node_back_walk_ctx_t *ctx)
197{
198 vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_from_fib_node(node));
199 return (FIB_NODE_BACK_WALK_CONTINUE);
200}
201
202/**
203 * Function definition to get a FIB node from its index
204 */
205static fib_node_t*
206vxlan_gpe_tunnel_fib_node_get (fib_node_index_t index)
207{
208 vxlan_gpe_tunnel_t * t;
209 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
210
211 t = pool_elt_at_index(ngm->tunnels, index);
212
213 return (&t->node);
214}
215
216/**
217 * Function definition to inform the FIB node that its last lock has gone.
218 */
219static void
220vxlan_gpe_tunnel_last_lock_gone (fib_node_t *node)
221{
222 /*
223 * The VXLAN_GPE tunnel is a root of the graph. As such
224 * it never has children and thus is never locked.
225 */
226 ASSERT(0);
227}
228
229/*
230 * Virtual function table registered by VXLAN_GPE tunnels
231 * for participation in the FIB object graph.
232 */
233const static fib_node_vft_t vxlan_gpe_vft = {
234 .fnv_get = vxlan_gpe_tunnel_fib_node_get,
235 .fnv_last_lock = vxlan_gpe_tunnel_last_lock_gone,
236 .fnv_back_walk = vxlan_gpe_tunnel_back_walk,
237};
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700238
239#define foreach_gpe_copy_field \
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700240_(vni) \
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800241_(protocol) \
242_(mcast_sw_if_index) \
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700243_(encap_fib_index) \
Hongjun Ni7deb1392016-06-15 22:49:23 +0800244_(decap_fib_index)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700245
Hongjun Nidf921cc2016-05-25 01:16:19 +0800246#define foreach_copy_ipv4 { \
247 _(local.ip4.as_u32) \
248 _(remote.ip4.as_u32) \
249}
250
251#define foreach_copy_ipv6 { \
252 _(local.ip6.as_u64[0]) \
253 _(local.ip6.as_u64[1]) \
254 _(remote.ip6.as_u64[0]) \
255 _(remote.ip6.as_u64[1]) \
256}
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700257
258
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700259/**
260 * @brief Calculate IPv4 VXLAN GPE rewrite header
261 *
262 * @param *t
263 *
264 * @return rc
265 *
266 */
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800267int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
Vengada Govindan6d403a02016-10-12 05:54:09 -0700268 u8 protocol_override, uword encap_next_node)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700269{
270 u8 *rw = 0;
271 ip4_header_t * ip0;
272 ip4_vxlan_gpe_header_t * h0;
273 int len;
274
Vengada Govindan6d403a02016-10-12 05:54:09 -0700275 len = sizeof (*h0) + extension_size;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700276
Vengada Govindan6d403a02016-10-12 05:54:09 -0700277 vec_free(t->rewrite);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700278 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
279
280 h0 = (ip4_vxlan_gpe_header_t *) rw;
281
282 /* Fixed portion of the (outer) ip4 header */
283 ip0 = &h0->ip4;
284 ip0->ip_version_and_header_length = 0x45;
285 ip0->ttl = 254;
286 ip0->protocol = IP_PROTOCOL_UDP;
287
288 /* we fix up the ip4 header length and checksum after-the-fact */
Hongjun Nidf921cc2016-05-25 01:16:19 +0800289 ip0->src_address.as_u32 = t->local.ip4.as_u32;
290 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700291 ip0->checksum = ip4_header_checksum (ip0);
292
293 /* UDP header, randomize src port on something, maybe? */
294 h0->udp.src_port = clib_host_to_net_u16 (4790);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800295 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700296
297 /* VXLAN header. Are we having fun yet? */
298 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
299 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700300 if (protocol_override)
301 {
302 h0->vxlan.protocol = protocol_override;
303 }
304 else
305 {
306 h0->vxlan.protocol = t->protocol;
307 }
308 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700309 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
310
311 t->rewrite = rw;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700312 t->encap_next_node = encap_next_node;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700313 return (0);
314}
315
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700316/**
317 * @brief Calculate IPv6 VXLAN GPE rewrite header
318 *
319 * @param *t
320 *
321 * @return rc
322 *
323 */
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800324int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
Vengada Govindan6d403a02016-10-12 05:54:09 -0700325 u8 protocol_override, uword encap_next_node)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800326{
327 u8 *rw = 0;
328 ip6_header_t * ip0;
329 ip6_vxlan_gpe_header_t * h0;
330 int len;
331
Vengada Govindan6d403a02016-10-12 05:54:09 -0700332 len = sizeof (*h0) + extension_size;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800333
Vengada Govindan6d403a02016-10-12 05:54:09 -0700334 vec_free(t->rewrite);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800335 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
336
337 h0 = (ip6_vxlan_gpe_header_t *) rw;
338
339 /* Fixed portion of the (outer) ip4 header */
340 ip0 = &h0->ip6;
341 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
342 ip0->hop_limit = 255;
343 ip0->protocol = IP_PROTOCOL_UDP;
344
345 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
346 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
347 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
348 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
349
350 /* UDP header, randomize src port on something, maybe? */
351 h0->udp.src_port = clib_host_to_net_u16 (4790);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800352 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800353
354 /* VXLAN header. Are we having fun yet? */
355 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
356 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700357 if (protocol_override)
358 {
359 h0->vxlan.protocol = t->protocol;
360 }
361 else
362 {
363 h0->vxlan.protocol = protocol_override;
364 }
365 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800366 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
367
368 t->rewrite = rw;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700369 t->encap_next_node = encap_next_node;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800370 return (0);
371}
372
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800373static void
374hash_set_key_copy (uword ** h, void * key, uword v) {
375 size_t ksz = hash_header(*h)->user;
376 void * copy = clib_mem_alloc (ksz);
377 clib_memcpy (copy, key, ksz);
378 hash_set_mem (*h, copy, v);
379}
380
381static void
382hash_unset_key_free (uword ** h, void * key) {
383 hash_pair_t * hp = hash_get_pair_mem (*h, key);
384 ASSERT (hp);
385 key = uword_to_pointer (hp->key, void *);
386 hash_unset_mem (*h, key);
387 clib_mem_free (key);
388}
389
390static uword
391vtep_addr_ref(ip46_address_t *ip)
392{
393 uword *vtep = ip46_address_is_ip4(ip) ?
394 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
395 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
396 if (vtep)
397 return ++(*vtep);
398 ip46_address_is_ip4(ip) ?
399 hash_set (vxlan_gpe_main.vtep4, ip->ip4.as_u32, 1) :
400 hash_set_key_copy (&vxlan_gpe_main.vtep6, &ip->ip6, 1);
401 return 1;
402}
403
404static uword
405vtep_addr_unref(ip46_address_t *ip)
406{
407 uword *vtep = ip46_address_is_ip4(ip) ?
408 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
409 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
410 ASSERT(vtep);
411 if (--(*vtep) != 0)
412 return *vtep;
413 ip46_address_is_ip4(ip) ?
414 hash_unset (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
415 hash_unset_key_free (&vxlan_gpe_main.vtep6, &ip->ip6);
416 return 0;
417}
418
419typedef CLIB_PACKED(union {
420 struct {
421 fib_node_index_t mfib_entry_index;
422 adj_index_t mcast_adj_index;
423 };
424 u64 as_u64;
425}) mcast_shared_t;
426
427static inline mcast_shared_t
428mcast_shared_get(ip46_address_t * ip)
429{
430 ASSERT(ip46_address_is_multicast(ip));
431 uword * p = hash_get_mem (vxlan_gpe_main.mcast_shared, ip);
432 ASSERT(p);
433 return (mcast_shared_t) { .as_u64 = *p };
434}
435
436static inline void
437mcast_shared_add(ip46_address_t *remote,
438 fib_node_index_t mfei,
439 adj_index_t ai)
440{
441 mcast_shared_t new_ep = {
442 .mcast_adj_index = ai,
443 .mfib_entry_index = mfei,
444 };
445
446 hash_set_key_copy (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
447}
448
449static inline void
450mcast_shared_remove(ip46_address_t *remote)
451{
452 mcast_shared_t ep = mcast_shared_get(remote);
453
454 adj_unlock(ep.mcast_adj_index);
455 mfib_table_entry_delete_index(ep.mfib_entry_index,
456 MFIB_SOURCE_VXLAN_GPE);
457
458 hash_unset_key_free (&vxlan_gpe_main.mcast_shared, remote);
459}
460
461static inline fib_protocol_t
462fib_ip_proto(bool is_ip6)
463{
464 return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
465}
466
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700467/**
468 * @brief Add or Del a VXLAN GPE tunnel
469 *
470 * @param *a
471 * @param *sw_if_index
472 *
473 * @return rc
474 *
475 */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800476int vnet_vxlan_gpe_add_del_tunnel
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700477(vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
478{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800479 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700480 vxlan_gpe_tunnel_t *t = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800481 vnet_main_t * vnm = ngm->vnet_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700482 vnet_hw_interface_t * hi;
483 uword * p;
484 u32 hw_if_index = ~0;
485 u32 sw_if_index = ~0;
486 int rv;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800487 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
488 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800489 u32 is_ip6 = a->is_ip6;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800490
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800491 if (!is_ip6)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800492 {
493 key4.local = a->local.ip4.as_u32;
494 key4.remote = a->remote.ip4.as_u32;
495 key4.vni = clib_host_to_net_u32 (a->vni << 8);
496 key4.pad = 0;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700497
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800498 p = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800499 }
500 else
501 {
502 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
503 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
504 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
505 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
506 key6.vni = clib_host_to_net_u32 (a->vni << 8);
507
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800508 p = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800509 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800510
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700511 if (a->is_add)
512 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800513 l2input_main_t * l2im = &l2input_main;
514
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700515 /* adding a tunnel: tunnel must not already exist */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800516 if (p)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800517 return VNET_API_ERROR_TUNNEL_EXIST;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800518
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800519 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700520 memset (t, 0, sizeof (*t));
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800521
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700522 /* copy from arg structure */
523#define _(x) t->x = a->x;
524 foreach_gpe_copy_field;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800525 if (!a->is_ip6) foreach_copy_ipv4
526 else foreach_copy_ipv6
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700527#undef _
528
Hongjun Nidf921cc2016-05-25 01:16:19 +0800529 if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
530
531 if (!a->is_ip6) {
Vengada Govindan6d403a02016-10-12 05:54:09 -0700532 rv = vxlan4_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800533 } else {
Vengada Govindan6d403a02016-10-12 05:54:09 -0700534 rv = vxlan6_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800535 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700536
537 if (rv)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800538 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800539 pool_put (ngm->tunnels, t);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700540 return rv;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800541 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700542
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800543 if (!is_ip6)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800544 {
545 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
546 clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800547 hash_set_mem (ngm->vxlan4_gpe_tunnel_by_key, key4_copy,
548 t - ngm->tunnels);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800549 }
550 else
551 {
552 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
Florin Coras1d3b2ab2016-05-31 15:53:44 +0300553 clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800554 hash_set_mem (ngm->vxlan6_gpe_tunnel_by_key, key6_copy,
555 t - ngm->tunnels);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800556 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800557
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800558 if (vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700559 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800560 vnet_interface_main_t * im = &vnm->interface_main;
561 hw_if_index = ngm->free_vxlan_gpe_tunnel_hw_if_indices
562 [vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
563 _vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800564
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700565 hi = vnet_get_hw_interface (vnm, hw_if_index);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800566 hi->dev_instance = t - ngm->tunnels;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700567 hi->hw_instance = hi->dev_instance;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800568 /* clear old stats of freed tunnel before reuse */
569 sw_if_index = hi->sw_if_index;
570 vnet_interface_counter_lock(im);
571 vlib_zero_combined_counter
572 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
573 vlib_zero_combined_counter
574 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
575 vlib_zero_simple_counter
576 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
577 vnet_interface_counter_unlock(im);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700578 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800579 else
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700580 {
581 hw_if_index = vnet_register_interface
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800582 (vnm, vxlan_gpe_device_class.index, t - ngm->tunnels,
583 vxlan_gpe_hw_class.index, t - ngm->tunnels);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700584 hi = vnet_get_hw_interface (vnm, hw_if_index);
585 hi->output_node_index = vxlan_gpe_encap_node.index;
586 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800587
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700588 t->hw_if_index = hw_if_index;
589 t->sw_if_index = sw_if_index = hi->sw_if_index;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800590 vec_validate_init_empty (ngm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
591 ngm->tunnel_index_by_sw_if_index[sw_if_index] = t - ngm->tunnels;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800592
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800593 /* setup l2 input config with l2 feature and bd 0 to drop packet */
594 vec_validate (l2im->configs, sw_if_index);
595 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
596 l2im->configs[sw_if_index].bd_index = 0;
597
598 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
599 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800600 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700601 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800602 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL);
603 fib_prefix_t tun_remote_pfx;
604 u32 encap_index = vxlan_gpe_encap_node.index;
605 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
606
607 fib_prefix_from_ip46_addr(&t->remote, &tun_remote_pfx);
608 if (!ip46_address_is_multicast(&t->remote))
609 {
610 /* Unicast tunnel -
611 * source the FIB entry for the tunnel's destination
612 * and become a child thereof. The tunnel will then get poked
613 * when the forwarding for the entry updates, and the tunnel can
614 * re-stack accordingly
615 */
616 vtep_addr_ref(&t->local);
617 t->fib_entry_index = fib_table_entry_special_add
618 (t->encap_fib_index, &tun_remote_pfx, FIB_SOURCE_RR,
619 FIB_ENTRY_FLAG_NONE);
620 t->sibling_index = fib_entry_child_add
621 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, t - ngm->tunnels);
622 vxlan_gpe_tunnel_restack_dpo(t);
623 }
624 else
625 {
626 /* Multicast tunnel -
627 * as the same mcast group can be used for mutiple mcast tunnels
628 * with different VNIs, create the output fib adjecency only if
629 * it does not already exist
630 */
631 fib_protocol_t fp = fib_ip_proto(is_ip6);
632
633 if (vtep_addr_ref(&t->remote) == 1)
634 {
635 fib_node_index_t mfei;
636 adj_index_t ai;
637 fib_route_path_t path = {
638 .frp_proto = fp,
639 .frp_addr = zero_addr,
640 .frp_sw_if_index = 0xffffffff,
641 .frp_fib_index = ~0,
642 .frp_weight = 0,
643 .frp_flags = FIB_ROUTE_PATH_LOCAL,
644 };
645 const mfib_prefix_t mpfx = {
646 .fp_proto = fp,
647 .fp_len = (is_ip6 ? 128 : 32),
648 .fp_grp_addr = tun_remote_pfx.fp_addr,
649 };
650
651 /*
652 * Setup the (*,G) to receive traffic on the mcast group
653 * - the forwarding interface is for-us
654 * - the accepting interface is that from the API
655 */
656 mfib_table_entry_path_update(t->encap_fib_index,
657 &mpfx,
658 MFIB_SOURCE_VXLAN_GPE,
659 &path,
660 MFIB_ITF_FLAG_FORWARD);
661
662 path.frp_sw_if_index = a->mcast_sw_if_index;
663 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
664 mfei = mfib_table_entry_path_update(t->encap_fib_index,
665 &mpfx,
666 MFIB_SOURCE_VXLAN_GPE,
667 &path,
668 MFIB_ITF_FLAG_ACCEPT);
669
670 /*
671 * Create the mcast adjacency to send traffic to the group
672 */
673 ai = adj_mcast_add_or_lock(fp,
674 fib_proto_to_link(fp),
675 a->mcast_sw_if_index);
676
677 /*
678 * create a new end-point
679 */
680 mcast_shared_add(&t->remote, mfei, ai);
681 }
682
683 dpo_id_t dpo = DPO_INVALID;
684 mcast_shared_t ep = mcast_shared_get(&t->remote);
685
686 /* Stack shared mcast remote mac addr rewrite on encap */
687 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
688 fib_proto_to_dpo(fp),
689 ep.mcast_adj_index);
690
691 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
692 dpo_reset (&dpo);
693 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
694 }
695
696 /* Set vxlan tunnel output node */
697 hi->output_node_index = encap_index;
698
699 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700700 }
701 else
702 {
703 /* deleting a tunnel: tunnel must exist */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800704 if (!p)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700705 return VNET_API_ERROR_NO_SUCH_ENTRY;
706
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800707 t = pool_elt_at_index (ngm->tunnels, p[0]);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700708
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800709 sw_if_index = t->sw_if_index;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700710 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800711 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, t->sw_if_index);
712 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
713 set_int_l2_mode(ngm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
714 vec_add1 (ngm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700715
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800716 ngm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
Hongjun Nic0959c92016-06-16 20:18:15 +0800717
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800718 if (!is_ip6)
719 hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800720 else
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800721 hash_unset_key_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700722
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800723 if (!ip46_address_is_multicast(&t->remote))
724 {
725 vtep_addr_unref(&t->local);
726 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
727 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
728 }
729 else if (vtep_addr_unref(&t->remote) == 0)
730 {
731 mcast_shared_remove(&t->remote);
732 }
733
734 fib_node_deinit(&t->node);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700735 vec_free (t->rewrite);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800736 pool_put (ngm->tunnels, t);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700737 }
738
739 if (sw_if_indexp)
740 *sw_if_indexp = sw_if_index;
741
742 return 0;
743}
744
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700745static clib_error_t *
746vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
747 unformat_input_t * input,
748 vlib_cli_command_t * cmd)
749{
750 unformat_input_t _line_input, * line_input = &_line_input;
751 u8 is_add = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800752 ip46_address_t local, remote;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700753 u8 local_set = 0;
754 u8 remote_set = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800755 u8 grp_set = 0;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800756 u8 ipv4_set = 0;
757 u8 ipv6_set = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800758 u32 mcast_sw_if_index = ~0;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700759 u32 encap_fib_index = 0;
760 u32 decap_fib_index = 0;
761 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700762 u32 vni;
763 u8 vni_set = 0;
764 int rv;
765 u32 tmp;
766 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100767 u32 sw_if_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500768 clib_error_t *error = NULL;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800769
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700770 /* Get a line of input. */
771 if (! unformat_user (input, unformat_line_input, line_input))
772 return 0;
773
774 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
775 if (unformat (line_input, "del"))
776 is_add = 0;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800777 else if (unformat (line_input, "local %U",
Hongjun Nidf921cc2016-05-25 01:16:19 +0800778 unformat_ip4_address, &local.ip4))
779 {
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700780 local_set = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800781 ipv4_set = 1;
782 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700783 else if (unformat (line_input, "remote %U",
Hongjun Nidf921cc2016-05-25 01:16:19 +0800784 unformat_ip4_address, &remote.ip4))
785 {
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700786 remote_set = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800787 ipv4_set = 1;
788 }
789 else if (unformat (line_input, "local %U",
790 unformat_ip6_address, &local.ip6))
791 {
792 local_set = 1;
793 ipv6_set = 1;
794 }
795 else if (unformat (line_input, "remote %U",
796 unformat_ip6_address, &remote.ip6))
797 {
798 remote_set = 1;
799 ipv6_set = 1;
800 }
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800801 else if (unformat (line_input, "group %U %U",
802 unformat_ip4_address, &remote.ip4,
803 unformat_vnet_sw_interface,
804 vnet_get_main(), &mcast_sw_if_index))
805 {
806 grp_set = remote_set = 1;
807 ipv4_set = 1;
808 }
809 else if (unformat (line_input, "group %U %U",
810 unformat_ip6_address, &remote.ip6,
811 unformat_vnet_sw_interface,
812 vnet_get_main(), &mcast_sw_if_index))
813 {
814 grp_set = remote_set = 1;
815 ipv6_set = 1;
816 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700817 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
818 {
Hongjun Nidf921cc2016-05-25 01:16:19 +0800819 if (ipv6_set)
Neale Ranns630198f2017-05-22 09:20:20 -0400820 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800821 else
Neale Ranns630198f2017-05-22 09:20:20 -0400822 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800823
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700824 if (encap_fib_index == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500825 {
826 error = clib_error_return (0, "nonexistent encap fib id %d", tmp);
827 goto done;
828 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700829 }
830 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
831 {
Hongjun Nidf921cc2016-05-25 01:16:19 +0800832 if (ipv6_set)
Neale Ranns630198f2017-05-22 09:20:20 -0400833 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800834 else
Neale Ranns630198f2017-05-22 09:20:20 -0400835 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800836
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700837 if (decap_fib_index == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500838 {
839 error = clib_error_return (0, "nonexistent decap fib id %d", tmp);
840 goto done;
841 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700842 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700843 else if (unformat (line_input, "vni %d", &vni))
844 vni_set = 1;
845 else if (unformat(line_input, "next-ip4"))
846 protocol = VXLAN_GPE_PROTOCOL_IP4;
847 else if (unformat(line_input, "next-ip6"))
848 protocol = VXLAN_GPE_PROTOCOL_IP6;
849 else if (unformat(line_input, "next-ethernet"))
850 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
851 else if (unformat(line_input, "next-nsh"))
852 protocol = VXLAN_GPE_PROTOCOL_NSH;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800853 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500854 {
855 error = clib_error_return (0, "parse error: '%U'",
856 format_unformat_error, line_input);
857 goto done;
858 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700859 }
860
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700861 if (local_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500862 {
863 error = clib_error_return (0, "tunnel local address not specified");
864 goto done;
865 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700866
867 if (remote_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500868 {
869 error = clib_error_return (0, "tunnel remote address not specified");
870 goto done;
871 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700872
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800873 if (grp_set && !ip46_address_is_multicast(&remote))
874 {
875 error = clib_error_return (0, "tunnel group address not multicast");
876 goto done;
877 }
878
879 if (grp_set == 0 && ip46_address_is_multicast(&remote))
880 {
881 error = clib_error_return (0, "remote address must be unicast");
882 goto done;
883 }
884
885 if (grp_set && mcast_sw_if_index == ~0)
886 {
887 error = clib_error_return (0, "tunnel nonexistent multicast device");
888 goto done;
889 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800890 if (ipv4_set && ipv6_set)
Billy McFalla9a20e72017-02-15 11:39:12 -0500891 {
892 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
893 goto done;
894 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800895
896 if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) ||
897 (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0))
Billy McFalla9a20e72017-02-15 11:39:12 -0500898 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800899 error = clib_error_return (0, "src and remote addresses are identical");
Billy McFalla9a20e72017-02-15 11:39:12 -0500900 goto done;
901 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800902
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700903 if (vni_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500904 {
905 error = clib_error_return (0, "vni not specified");
906 goto done;
907 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700908
909 memset (a, 0, sizeof (*a));
910
911 a->is_add = is_add;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800912 a->is_ip6 = ipv6_set;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700913
914#define _(x) a->x = x;
915 foreach_gpe_copy_field;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800916 if (ipv4_set) foreach_copy_ipv4
917 else foreach_copy_ipv6
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700918#undef _
919
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100920 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700921
922 switch(rv)
923 {
924 case 0:
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100925 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700926 break;
927 case VNET_API_ERROR_INVALID_DECAP_NEXT:
Billy McFalla9a20e72017-02-15 11:39:12 -0500928 error = clib_error_return (0, "invalid decap-next...");
929 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700930
931 case VNET_API_ERROR_TUNNEL_EXIST:
Billy McFalla9a20e72017-02-15 11:39:12 -0500932 error = clib_error_return (0, "tunnel already exists...");
933 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700934
935 case VNET_API_ERROR_NO_SUCH_ENTRY:
Billy McFalla9a20e72017-02-15 11:39:12 -0500936 error = clib_error_return (0, "tunnel does not exist...");
937 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700938
939 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500940 error = clib_error_return
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700941 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500942 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700943 }
944
Billy McFalla9a20e72017-02-15 11:39:12 -0500945done:
946 unformat_free (line_input);
947
948 return error;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700949}
950
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800951/*?
952 * Add or delete a VXLAN-GPE Tunnel.
953 *
954 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
955 * to span multiple servers. This is done by building an L2 overlay on
956 * top of an L3 network underlay using VXLAN-GPE tunnels.
957 *
958 * This makes it possible for servers to be co-located in the same data
959 * center or be separated geographically as long as they are reachable
960 * through the underlay L3 network.
961 *
962 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE sengment.
963 *
964 * @cliexpar
965 * Example of how to create a VXLAN-GPE Tunnel:
966 * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 local 10.0.3.3 vni 13 encap-vrf-id 7}
967 * Example of how to delete a VXLAN Tunnel:
968 * @cliexcmd{create vxlan tunnel src 10.0.3.1 remote 10.0.3.3 vni 13 del}
969 ?*/
970/* *INDENT-OFF* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700971VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
972 .path = "create vxlan-gpe tunnel",
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800973 .short_help =
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800974 "create vxlan-gpe tunnel local <local-addr> "
975 " {remote <remote-addr>|group <mcast-addr> <intf-name>}"
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700976 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800977 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [del]\n",
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700978 .function = vxlan_gpe_add_del_tunnel_command_fn,
979};
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800980/* *INDENT-ON* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700981
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700982/**
983 * @brief CLI function for showing VXLAN GPE tunnels
984 *
985 * @param *vm
986 * @param *input
987 * @param *cmd
988 *
989 * @return error
990 *
991 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700992static clib_error_t *
993show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
994 unformat_input_t * input,
995 vlib_cli_command_t * cmd)
996{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800997 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700998 vxlan_gpe_tunnel_t * t;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800999
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001000 if (pool_elts (ngm->tunnels) == 0)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001001 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
1002
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001003 pool_foreach (t, ngm->tunnels,
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001004 ({
1005 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
1006 }));
Hongjun Nicccd1bf2016-06-07 18:43:05 +08001007
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001008 return 0;
1009}
1010
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001011/*?
1012 * Display all the VXLAN-GPE Tunnel entries.
1013 *
1014 * @cliexpar
1015 * Example of how to display the VXLAN-GPE Tunnel entries:
1016 * @cliexstart{show vxlan-gpe tunnel}
1017 * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
1018 * @cliexend
1019 ?*/
1020/* *INDENT-OFF* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001021VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
1022 .path = "show vxlan-gpe",
1023 .function = show_vxlan_gpe_tunnel_command_fn,
1024};
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001025/* *INDENT-ON* */
1026
1027void vnet_int_vxlan_gpe_bypass_mode (u32 sw_if_index,
1028 u8 is_ip6,
1029 u8 is_enable)
1030{
1031 if (is_ip6)
1032 vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-gpe-bypass",
1033 sw_if_index, is_enable, 0, 0);
1034 else
1035 vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-gpe-bypass",
1036 sw_if_index, is_enable, 0, 0);
1037}
1038
1039
1040static clib_error_t *
1041set_ip_vxlan_gpe_bypass (u32 is_ip6,
1042 unformat_input_t * input,
1043 vlib_cli_command_t * cmd)
1044{
1045 unformat_input_t _line_input, * line_input = &_line_input;
1046 vnet_main_t * vnm = vnet_get_main();
1047 clib_error_t * error = 0;
1048 u32 sw_if_index, is_enable;
1049
1050 sw_if_index = ~0;
1051 is_enable = 1;
1052
1053 if (! unformat_user (input, unformat_line_input, line_input))
1054 return 0;
1055
1056 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1057 {
1058 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1059 ;
1060 else if (unformat (line_input, "del"))
1061 is_enable = 0;
1062 else
1063 {
1064 error = unformat_parse_error (line_input);
1065 goto done;
1066 }
1067 }
1068
1069 if (~0 == sw_if_index)
1070 {
1071 error = clib_error_return (0, "unknown interface `%U'",
1072 format_unformat_error, line_input);
1073 goto done;
1074 }
1075
1076 vnet_int_vxlan_gpe_bypass_mode (sw_if_index, is_ip6, is_enable);
1077
1078 done:
1079 unformat_free (line_input);
1080
1081 return error;
1082}
1083
1084static clib_error_t *
1085set_ip4_vxlan_gpe_bypass (vlib_main_t * vm,
1086 unformat_input_t * input,
1087 vlib_cli_command_t * cmd)
1088{
1089 return set_ip_vxlan_gpe_bypass (0, input, cmd);
1090}
1091
1092/*?
1093 * This command adds the 'ip4-vxlan-gpe-bypass' graph node for a given interface.
1094 * By adding the IPv4 vxlan-gpe-bypass graph node to an interface, the node checks
1095 * for and validate input vxlan_gpe packet and bypass ip4-lookup, ip4-local,
1096 * ip4-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1097 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1098 *
1099 * @cliexpar
1100 * @parblock
1101 * Example of graph node before ip4-vxlan-gpe-bypass is enabled:
1102 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1103 * Name Next Previous
1104 * ip4-vxlan-gpe-bypass error-drop [0]
1105 * vxlan4-gpe-input [1]
1106 * ip4-lookup [2]
1107 * @cliexend
1108 *
1109 * Example of how to enable ip4-vxlan-gpe-bypass on an interface:
1110 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0}
1111 *
1112 * Example of graph node after ip4-vxlan-gpe-bypass is enabled:
1113 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1114 * Name Next Previous
1115 * ip4-vxlan-gpe-bypass error-drop [0] ip4-input
1116 * vxlan4-gpe-input [1] ip4-input-no-checksum
1117 * ip4-lookup [2]
1118 * @cliexend
1119 *
1120 * Example of how to display the feature enabed on an interface:
1121 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1122 * IP feature paths configured on GigabitEthernet2/0/0...
1123 * ...
1124 * ipv4 unicast:
1125 * ip4-vxlan-gpe-bypass
1126 * ip4-lookup
1127 * ...
1128 * @cliexend
1129 *
1130 * Example of how to disable ip4-vxlan-gpe-bypass on an interface:
1131 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1132 * @endparblock
1133?*/
1134/* *INDENT-OFF* */
1135VLIB_CLI_COMMAND (set_interface_ip_vxlan_gpe_bypass_command, static) = {
1136 .path = "set interface ip vxlan-gpe-bypass",
1137 .function = set_ip4_vxlan_gpe_bypass,
1138 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1139};
1140/* *INDENT-ON* */
1141
1142static clib_error_t *
1143set_ip6_vxlan_gpe_bypass (vlib_main_t * vm,
1144 unformat_input_t * input,
1145 vlib_cli_command_t * cmd)
1146{
1147 return set_ip_vxlan_gpe_bypass (1, input, cmd);
1148}
1149
1150/*?
1151 * This command adds the 'ip6-vxlan-gpe-bypass' graph node for a given interface.
1152 * By adding the IPv6 vxlan-gpe-bypass graph node to an interface, the node checks
1153 * for and validate input vxlan_gpe packet and bypass ip6-lookup, ip6-local,
1154 * ip6-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1155 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1156 *
1157 * @cliexpar
1158 * @parblock
1159 * Example of graph node before ip6-vxlan-gpe-bypass is enabled:
1160 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1161 * Name Next Previous
1162 * ip6-vxlan-gpe-bypass error-drop [0]
1163 * vxlan6-gpe-input [1]
1164 * ip6-lookup [2]
1165 * @cliexend
1166 *
1167 * Example of how to enable ip6-vxlan-gpe-bypass on an interface:
1168 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0}
1169 *
1170 * Example of graph node after ip6-vxlan-gpe-bypass is enabled:
1171 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1172 * Name Next Previous
1173 * ip6-vxlan-gpe-bypass error-drop [0] ip6-input
1174 * vxlan6-gpe-input [1] ip4-input-no-checksum
1175 * ip6-lookup [2]
1176 * @cliexend
1177 *
1178 * Example of how to display the feature enabed on an interface:
1179 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1180 * IP feature paths configured on GigabitEthernet2/0/0...
1181 * ...
1182 * ipv6 unicast:
1183 * ip6-vxlan-gpe-bypass
1184 * ip6-lookup
1185 * ...
1186 * @cliexend
1187 *
1188 * Example of how to disable ip6-vxlan-gpe-bypass on an interface:
1189 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1190 * @endparblock
1191?*/
1192/* *INDENT-OFF* */
1193VLIB_CLI_COMMAND (set_interface_ip6_vxlan_gpe_bypass_command, static) = {
1194 .path = "set interface ip6 vxlan-gpe-bypass",
1195 .function = set_ip6_vxlan_gpe_bypass,
1196 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1197};
1198/* *INDENT-ON* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001199
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -07001200/**
1201 * @brief Feature init function for VXLAN GPE
1202 *
1203 * @param *vm
1204 *
1205 * @return error
1206 *
1207 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001208clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
1209{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001210 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
Hongjun Nicccd1bf2016-06-07 18:43:05 +08001211
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001212 ngm->vnet_main = vnet_get_main();
1213 ngm->vlib_main = vm;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001214
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001215 ngm->vxlan4_gpe_tunnel_by_key
Hongjun Nidf921cc2016-05-25 01:16:19 +08001216 = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword));
1217
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001218 ngm->vxlan6_gpe_tunnel_by_key
Hongjun Nidf921cc2016-05-25 01:16:19 +08001219 = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword));
1220
1221
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001222 ngm->mcast_shared = hash_create_mem(0,
1223 sizeof(ip46_address_t),
1224 sizeof(mcast_shared_t));
1225
1226 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN_GPE,
Hongjun Nidf921cc2016-05-25 01:16:19 +08001227 vxlan4_gpe_input_node.index, 1 /* is_ip4 */);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001228 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN6_GPE,
Hongjun Nidf921cc2016-05-25 01:16:19 +08001229 vxlan6_gpe_input_node.index, 0 /* is_ip4 */);
Vengada Govindan6d403a02016-10-12 05:54:09 -07001230
1231 /* Register the list of standard decap protocols supported */
1232 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP4,
1233 VXLAN_GPE_INPUT_NEXT_IP4_INPUT);
1234 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP6,
1235 VXLAN_GPE_INPUT_NEXT_IP6_INPUT);
1236 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_ETHERNET,
1237 VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001238
1239 fib_node_register_type(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, &vxlan_gpe_vft);
1240
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001241 return 0;
1242}
1243
1244VLIB_INIT_FUNCTION(vxlan_gpe_init);
1245