blob: 97bb1b158e66fcc2d0ee488551b0bf9da11c5ac7 [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
Hongjun Ni04ffd0ad2017-06-23 00:18:40 +080086 if (ip46_address_is_multicast (&t->remote))
87 s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
88
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070089 s = format (s, " fibs: (encap %d, decap %d)",
90 t->encap_fib_index,
91 t->decap_fib_index);
92
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -070093 return s;
94}
95
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -070096/**
97 * @brief Naming for VXLAN GPE tunnel
98 *
99 * @param *s formatting string
100 * @param *args
101 *
102 * @return *s formatted string
103 *
104 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700105static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
106{
Hongjun Ni0e06e2b2016-05-30 19:45:51 +0800107 u32 dev_instance = va_arg (*args, u32);
108 return format (s, "vxlan_gpe_tunnel%d", dev_instance);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700109}
110
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700111static uword dummy_interface_tx (vlib_main_t * vm,
112 vlib_node_runtime_t * node,
113 vlib_frame_t * frame)
114{
115 clib_warning ("you shouldn't be here, leaking buffers...");
116 return frame->n_vectors;
117}
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700118
119/**
120 * @brief CLI function for VXLAN GPE admin up/down
121 *
122 * @param *vnm
123 * @param hw_if_index
124 * @param flag
125 *
126 * @return *rc
127 *
128 */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800129static clib_error_t *
130vxlan_gpe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
131{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800132 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
133 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
134 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800135
136 return 0;
137}
138
Hongjun Ni0e06e2b2016-05-30 19:45:51 +0800139VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
140 .name = "VXLAN_GPE",
141 .format_device_name = format_vxlan_gpe_name,
142 .format_tx_trace = format_vxlan_gpe_encap_trace,
143 .tx_function = dummy_interface_tx,
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800144 .admin_up_down_function = vxlan_gpe_interface_admin_up_down,
Hongjun Ni0e06e2b2016-05-30 19:45:51 +0800145};
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700146
Vengada Govindan6d403a02016-10-12 05:54:09 -0700147
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700148/**
149 * @brief Formatting function for tracing VXLAN GPE with length
150 *
151 * @param *s
152 * @param *args
153 *
154 * @return *s
155 *
156 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700157static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
158{
159 u32 dev_instance = va_arg (*args, u32);
160 s = format (s, "unimplemented dev %u", dev_instance);
161 return s;
162}
163
164VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
165 .name = "VXLAN_GPE",
166 .format_header = format_vxlan_gpe_header_with_length,
Neale Rannsb80c5362016-10-08 13:03:40 +0100167 .build_rewrite = default_build_rewrite,
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700168};
169
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800170static void
171vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_t * t)
172{
173 dpo_id_t dpo = DPO_INVALID;
174 u32 encap_index = vxlan_gpe_encap_node.index;
175 fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->remote) ?
176 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
177
178 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
179 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
180 dpo_reset(&dpo);
181}
182
183static vxlan_gpe_tunnel_t *
184vxlan_gpe_tunnel_from_fib_node (fib_node_t *node)
185{
186#if (CLIB_DEBUG > 0)
187 ASSERT(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL == node->fn_type);
188#endif
189 return ((vxlan_gpe_tunnel_t*) (((char*)node) -
190 STRUCT_OFFSET_OF(vxlan_gpe_tunnel_t, node)));
191}
192
193/**
194 * Function definition to backwalk a FIB node -
195 * Here we will restack the new dpo of VXLAN_GPE DIP to encap node.
196 */
197static fib_node_back_walk_rc_t
198vxlan_gpe_tunnel_back_walk (fib_node_t *node,
199 fib_node_back_walk_ctx_t *ctx)
200{
201 vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_from_fib_node(node));
202 return (FIB_NODE_BACK_WALK_CONTINUE);
203}
204
205/**
206 * Function definition to get a FIB node from its index
207 */
208static fib_node_t*
209vxlan_gpe_tunnel_fib_node_get (fib_node_index_t index)
210{
211 vxlan_gpe_tunnel_t * t;
212 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
213
214 t = pool_elt_at_index(ngm->tunnels, index);
215
216 return (&t->node);
217}
218
219/**
220 * Function definition to inform the FIB node that its last lock has gone.
221 */
222static void
223vxlan_gpe_tunnel_last_lock_gone (fib_node_t *node)
224{
225 /*
226 * The VXLAN_GPE tunnel is a root of the graph. As such
227 * it never has children and thus is never locked.
228 */
229 ASSERT(0);
230}
231
232/*
233 * Virtual function table registered by VXLAN_GPE tunnels
234 * for participation in the FIB object graph.
235 */
236const static fib_node_vft_t vxlan_gpe_vft = {
237 .fnv_get = vxlan_gpe_tunnel_fib_node_get,
238 .fnv_last_lock = vxlan_gpe_tunnel_last_lock_gone,
239 .fnv_back_walk = vxlan_gpe_tunnel_back_walk,
240};
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700241
242#define foreach_gpe_copy_field \
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700243_(vni) \
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800244_(protocol) \
245_(mcast_sw_if_index) \
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700246_(encap_fib_index) \
Hongjun Ni7deb1392016-06-15 22:49:23 +0800247_(decap_fib_index)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700248
Hongjun Nidf921cc2016-05-25 01:16:19 +0800249#define foreach_copy_ipv4 { \
250 _(local.ip4.as_u32) \
251 _(remote.ip4.as_u32) \
252}
253
254#define foreach_copy_ipv6 { \
255 _(local.ip6.as_u64[0]) \
256 _(local.ip6.as_u64[1]) \
257 _(remote.ip6.as_u64[0]) \
258 _(remote.ip6.as_u64[1]) \
259}
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700260
261
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700262/**
263 * @brief Calculate IPv4 VXLAN GPE rewrite header
264 *
265 * @param *t
266 *
267 * @return rc
268 *
269 */
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800270int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
Vengada Govindan6d403a02016-10-12 05:54:09 -0700271 u8 protocol_override, uword encap_next_node)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700272{
273 u8 *rw = 0;
274 ip4_header_t * ip0;
275 ip4_vxlan_gpe_header_t * h0;
276 int len;
277
Vengada Govindan6d403a02016-10-12 05:54:09 -0700278 len = sizeof (*h0) + extension_size;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700279
Vengada Govindan6d403a02016-10-12 05:54:09 -0700280 vec_free(t->rewrite);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700281 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
282
283 h0 = (ip4_vxlan_gpe_header_t *) rw;
284
285 /* Fixed portion of the (outer) ip4 header */
286 ip0 = &h0->ip4;
287 ip0->ip_version_and_header_length = 0x45;
288 ip0->ttl = 254;
289 ip0->protocol = IP_PROTOCOL_UDP;
290
291 /* we fix up the ip4 header length and checksum after-the-fact */
Hongjun Nidf921cc2016-05-25 01:16:19 +0800292 ip0->src_address.as_u32 = t->local.ip4.as_u32;
293 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700294 ip0->checksum = ip4_header_checksum (ip0);
295
296 /* UDP header, randomize src port on something, maybe? */
297 h0->udp.src_port = clib_host_to_net_u16 (4790);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800298 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700299
300 /* VXLAN header. Are we having fun yet? */
301 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
302 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700303 if (protocol_override)
304 {
305 h0->vxlan.protocol = protocol_override;
306 }
307 else
308 {
309 h0->vxlan.protocol = t->protocol;
310 }
311 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700312 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
313
314 t->rewrite = rw;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700315 t->encap_next_node = encap_next_node;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700316 return (0);
317}
318
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700319/**
320 * @brief Calculate IPv6 VXLAN GPE rewrite header
321 *
322 * @param *t
323 *
324 * @return rc
325 *
326 */
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800327int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
Vengada Govindan6d403a02016-10-12 05:54:09 -0700328 u8 protocol_override, uword encap_next_node)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800329{
330 u8 *rw = 0;
331 ip6_header_t * ip0;
332 ip6_vxlan_gpe_header_t * h0;
333 int len;
334
Vengada Govindan6d403a02016-10-12 05:54:09 -0700335 len = sizeof (*h0) + extension_size;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800336
Vengada Govindan6d403a02016-10-12 05:54:09 -0700337 vec_free(t->rewrite);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800338 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
339
340 h0 = (ip6_vxlan_gpe_header_t *) rw;
341
342 /* Fixed portion of the (outer) ip4 header */
343 ip0 = &h0->ip6;
344 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
345 ip0->hop_limit = 255;
346 ip0->protocol = IP_PROTOCOL_UDP;
347
348 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
349 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
350 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
351 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
352
353 /* UDP header, randomize src port on something, maybe? */
354 h0->udp.src_port = clib_host_to_net_u16 (4790);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800355 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800356
357 /* VXLAN header. Are we having fun yet? */
358 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
359 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700360 if (protocol_override)
361 {
362 h0->vxlan.protocol = t->protocol;
363 }
364 else
365 {
366 h0->vxlan.protocol = protocol_override;
367 }
368 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800369 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
370
371 t->rewrite = rw;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700372 t->encap_next_node = encap_next_node;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800373 return (0);
374}
375
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800376static void
377hash_set_key_copy (uword ** h, void * key, uword v) {
378 size_t ksz = hash_header(*h)->user;
379 void * copy = clib_mem_alloc (ksz);
380 clib_memcpy (copy, key, ksz);
381 hash_set_mem (*h, copy, v);
382}
383
384static void
385hash_unset_key_free (uword ** h, void * key) {
386 hash_pair_t * hp = hash_get_pair_mem (*h, key);
387 ASSERT (hp);
388 key = uword_to_pointer (hp->key, void *);
389 hash_unset_mem (*h, key);
390 clib_mem_free (key);
391}
392
393static uword
394vtep_addr_ref(ip46_address_t *ip)
395{
396 uword *vtep = ip46_address_is_ip4(ip) ?
397 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
398 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
399 if (vtep)
400 return ++(*vtep);
401 ip46_address_is_ip4(ip) ?
402 hash_set (vxlan_gpe_main.vtep4, ip->ip4.as_u32, 1) :
403 hash_set_key_copy (&vxlan_gpe_main.vtep6, &ip->ip6, 1);
404 return 1;
405}
406
407static uword
408vtep_addr_unref(ip46_address_t *ip)
409{
410 uword *vtep = ip46_address_is_ip4(ip) ?
411 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
412 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
413 ASSERT(vtep);
414 if (--(*vtep) != 0)
415 return *vtep;
416 ip46_address_is_ip4(ip) ?
417 hash_unset (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
418 hash_unset_key_free (&vxlan_gpe_main.vtep6, &ip->ip6);
419 return 0;
420}
421
422typedef CLIB_PACKED(union {
423 struct {
424 fib_node_index_t mfib_entry_index;
425 adj_index_t mcast_adj_index;
426 };
427 u64 as_u64;
428}) mcast_shared_t;
429
430static inline mcast_shared_t
431mcast_shared_get(ip46_address_t * ip)
432{
433 ASSERT(ip46_address_is_multicast(ip));
434 uword * p = hash_get_mem (vxlan_gpe_main.mcast_shared, ip);
435 ASSERT(p);
436 return (mcast_shared_t) { .as_u64 = *p };
437}
438
439static inline void
440mcast_shared_add(ip46_address_t *remote,
441 fib_node_index_t mfei,
442 adj_index_t ai)
443{
444 mcast_shared_t new_ep = {
445 .mcast_adj_index = ai,
446 .mfib_entry_index = mfei,
447 };
448
449 hash_set_key_copy (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
450}
451
452static inline void
453mcast_shared_remove(ip46_address_t *remote)
454{
455 mcast_shared_t ep = mcast_shared_get(remote);
456
457 adj_unlock(ep.mcast_adj_index);
458 mfib_table_entry_delete_index(ep.mfib_entry_index,
459 MFIB_SOURCE_VXLAN_GPE);
460
461 hash_unset_key_free (&vxlan_gpe_main.mcast_shared, remote);
462}
463
464static inline fib_protocol_t
465fib_ip_proto(bool is_ip6)
466{
467 return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
468}
469
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700470/**
471 * @brief Add or Del a VXLAN GPE tunnel
472 *
473 * @param *a
474 * @param *sw_if_index
475 *
476 * @return rc
477 *
478 */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800479int vnet_vxlan_gpe_add_del_tunnel
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700480(vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
481{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800482 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700483 vxlan_gpe_tunnel_t *t = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800484 vnet_main_t * vnm = ngm->vnet_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700485 vnet_hw_interface_t * hi;
486 uword * p;
487 u32 hw_if_index = ~0;
488 u32 sw_if_index = ~0;
489 int rv;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800490 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
491 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800492 u32 is_ip6 = a->is_ip6;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800493
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800494 if (!is_ip6)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800495 {
496 key4.local = a->local.ip4.as_u32;
497 key4.remote = a->remote.ip4.as_u32;
498 key4.vni = clib_host_to_net_u32 (a->vni << 8);
499 key4.pad = 0;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700500
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800501 p = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800502 }
503 else
504 {
505 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
506 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
507 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
508 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
509 key6.vni = clib_host_to_net_u32 (a->vni << 8);
510
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800511 p = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800512 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800513
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700514 if (a->is_add)
515 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800516 l2input_main_t * l2im = &l2input_main;
517
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700518 /* adding a tunnel: tunnel must not already exist */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800519 if (p)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800520 return VNET_API_ERROR_TUNNEL_EXIST;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800521
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800522 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700523 memset (t, 0, sizeof (*t));
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800524
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700525 /* copy from arg structure */
526#define _(x) t->x = a->x;
527 foreach_gpe_copy_field;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800528 if (!a->is_ip6) foreach_copy_ipv4
529 else foreach_copy_ipv6
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700530#undef _
531
Hongjun Nidf921cc2016-05-25 01:16:19 +0800532 if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
533
534 if (!a->is_ip6) {
Vengada Govindan6d403a02016-10-12 05:54:09 -0700535 rv = vxlan4_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800536 } else {
Vengada Govindan6d403a02016-10-12 05:54:09 -0700537 rv = vxlan6_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800538 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700539
540 if (rv)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800541 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800542 pool_put (ngm->tunnels, t);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700543 return rv;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800544 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700545
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800546 if (!is_ip6)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800547 {
548 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
549 clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800550 hash_set_mem (ngm->vxlan4_gpe_tunnel_by_key, key4_copy,
551 t - ngm->tunnels);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800552 }
553 else
554 {
555 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
Florin Coras1d3b2ab2016-05-31 15:53:44 +0300556 clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800557 hash_set_mem (ngm->vxlan6_gpe_tunnel_by_key, key6_copy,
558 t - ngm->tunnels);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800559 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800560
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800561 if (vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700562 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800563 vnet_interface_main_t * im = &vnm->interface_main;
564 hw_if_index = ngm->free_vxlan_gpe_tunnel_hw_if_indices
565 [vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
566 _vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800567
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700568 hi = vnet_get_hw_interface (vnm, hw_if_index);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800569 hi->dev_instance = t - ngm->tunnels;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700570 hi->hw_instance = hi->dev_instance;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800571 /* clear old stats of freed tunnel before reuse */
572 sw_if_index = hi->sw_if_index;
573 vnet_interface_counter_lock(im);
574 vlib_zero_combined_counter
575 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
576 vlib_zero_combined_counter
577 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
578 vlib_zero_simple_counter
579 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
580 vnet_interface_counter_unlock(im);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700581 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800582 else
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700583 {
584 hw_if_index = vnet_register_interface
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800585 (vnm, vxlan_gpe_device_class.index, t - ngm->tunnels,
586 vxlan_gpe_hw_class.index, t - ngm->tunnels);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700587 hi = vnet_get_hw_interface (vnm, hw_if_index);
588 hi->output_node_index = vxlan_gpe_encap_node.index;
589 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800590
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700591 t->hw_if_index = hw_if_index;
592 t->sw_if_index = sw_if_index = hi->sw_if_index;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800593 vec_validate_init_empty (ngm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
594 ngm->tunnel_index_by_sw_if_index[sw_if_index] = t - ngm->tunnels;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800595
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800596 /* setup l2 input config with l2 feature and bd 0 to drop packet */
597 vec_validate (l2im->configs, sw_if_index);
598 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
599 l2im->configs[sw_if_index].bd_index = 0;
600
601 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
602 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800603 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700604 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800605 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL);
606 fib_prefix_t tun_remote_pfx;
607 u32 encap_index = vxlan_gpe_encap_node.index;
608 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
609
610 fib_prefix_from_ip46_addr(&t->remote, &tun_remote_pfx);
611 if (!ip46_address_is_multicast(&t->remote))
612 {
613 /* Unicast tunnel -
614 * source the FIB entry for the tunnel's destination
615 * and become a child thereof. The tunnel will then get poked
616 * when the forwarding for the entry updates, and the tunnel can
617 * re-stack accordingly
618 */
619 vtep_addr_ref(&t->local);
620 t->fib_entry_index = fib_table_entry_special_add
621 (t->encap_fib_index, &tun_remote_pfx, FIB_SOURCE_RR,
622 FIB_ENTRY_FLAG_NONE);
623 t->sibling_index = fib_entry_child_add
624 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, t - ngm->tunnels);
625 vxlan_gpe_tunnel_restack_dpo(t);
626 }
627 else
628 {
629 /* Multicast tunnel -
630 * as the same mcast group can be used for mutiple mcast tunnels
631 * with different VNIs, create the output fib adjecency only if
632 * it does not already exist
633 */
634 fib_protocol_t fp = fib_ip_proto(is_ip6);
635
636 if (vtep_addr_ref(&t->remote) == 1)
637 {
638 fib_node_index_t mfei;
639 adj_index_t ai;
640 fib_route_path_t path = {
641 .frp_proto = fp,
642 .frp_addr = zero_addr,
643 .frp_sw_if_index = 0xffffffff,
644 .frp_fib_index = ~0,
645 .frp_weight = 0,
646 .frp_flags = FIB_ROUTE_PATH_LOCAL,
647 };
648 const mfib_prefix_t mpfx = {
649 .fp_proto = fp,
650 .fp_len = (is_ip6 ? 128 : 32),
651 .fp_grp_addr = tun_remote_pfx.fp_addr,
652 };
653
654 /*
655 * Setup the (*,G) to receive traffic on the mcast group
656 * - the forwarding interface is for-us
657 * - the accepting interface is that from the API
658 */
659 mfib_table_entry_path_update(t->encap_fib_index,
660 &mpfx,
661 MFIB_SOURCE_VXLAN_GPE,
662 &path,
663 MFIB_ITF_FLAG_FORWARD);
664
665 path.frp_sw_if_index = a->mcast_sw_if_index;
666 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
667 mfei = mfib_table_entry_path_update(t->encap_fib_index,
668 &mpfx,
669 MFIB_SOURCE_VXLAN_GPE,
670 &path,
671 MFIB_ITF_FLAG_ACCEPT);
672
673 /*
674 * Create the mcast adjacency to send traffic to the group
675 */
676 ai = adj_mcast_add_or_lock(fp,
677 fib_proto_to_link(fp),
678 a->mcast_sw_if_index);
679
680 /*
681 * create a new end-point
682 */
683 mcast_shared_add(&t->remote, mfei, ai);
684 }
685
686 dpo_id_t dpo = DPO_INVALID;
687 mcast_shared_t ep = mcast_shared_get(&t->remote);
688
689 /* Stack shared mcast remote mac addr rewrite on encap */
690 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
691 fib_proto_to_dpo(fp),
692 ep.mcast_adj_index);
693
694 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
695 dpo_reset (&dpo);
696 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
697 }
698
699 /* Set vxlan tunnel output node */
700 hi->output_node_index = encap_index;
701
702 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700703 }
704 else
705 {
706 /* deleting a tunnel: tunnel must exist */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800707 if (!p)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700708 return VNET_API_ERROR_NO_SUCH_ENTRY;
709
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800710 t = pool_elt_at_index (ngm->tunnels, p[0]);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700711
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800712 sw_if_index = t->sw_if_index;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700713 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800714 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, t->sw_if_index);
715 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
716 set_int_l2_mode(ngm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
717 vec_add1 (ngm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700718
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800719 ngm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
Hongjun Nic0959c92016-06-16 20:18:15 +0800720
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800721 if (!is_ip6)
722 hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800723 else
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800724 hash_unset_key_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700725
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800726 if (!ip46_address_is_multicast(&t->remote))
727 {
728 vtep_addr_unref(&t->local);
729 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
730 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
731 }
732 else if (vtep_addr_unref(&t->remote) == 0)
733 {
734 mcast_shared_remove(&t->remote);
735 }
736
737 fib_node_deinit(&t->node);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700738 vec_free (t->rewrite);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800739 pool_put (ngm->tunnels, t);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700740 }
741
742 if (sw_if_indexp)
743 *sw_if_indexp = sw_if_index;
744
745 return 0;
746}
747
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700748static clib_error_t *
749vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
750 unformat_input_t * input,
751 vlib_cli_command_t * cmd)
752{
753 unformat_input_t _line_input, * line_input = &_line_input;
754 u8 is_add = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800755 ip46_address_t local, remote;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700756 u8 local_set = 0;
757 u8 remote_set = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800758 u8 grp_set = 0;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800759 u8 ipv4_set = 0;
760 u8 ipv6_set = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800761 u32 mcast_sw_if_index = ~0;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700762 u32 encap_fib_index = 0;
763 u32 decap_fib_index = 0;
764 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700765 u32 vni;
766 u8 vni_set = 0;
767 int rv;
768 u32 tmp;
769 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100770 u32 sw_if_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500771 clib_error_t *error = NULL;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800772
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700773 /* Get a line of input. */
774 if (! unformat_user (input, unformat_line_input, line_input))
775 return 0;
776
777 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
778 if (unformat (line_input, "del"))
779 is_add = 0;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800780 else if (unformat (line_input, "local %U",
Hongjun Nidf921cc2016-05-25 01:16:19 +0800781 unformat_ip4_address, &local.ip4))
782 {
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700783 local_set = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800784 ipv4_set = 1;
785 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700786 else if (unformat (line_input, "remote %U",
Hongjun Nidf921cc2016-05-25 01:16:19 +0800787 unformat_ip4_address, &remote.ip4))
788 {
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700789 remote_set = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800790 ipv4_set = 1;
791 }
792 else if (unformat (line_input, "local %U",
793 unformat_ip6_address, &local.ip6))
794 {
795 local_set = 1;
796 ipv6_set = 1;
797 }
798 else if (unformat (line_input, "remote %U",
799 unformat_ip6_address, &remote.ip6))
800 {
801 remote_set = 1;
802 ipv6_set = 1;
803 }
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800804 else if (unformat (line_input, "group %U %U",
805 unformat_ip4_address, &remote.ip4,
806 unformat_vnet_sw_interface,
807 vnet_get_main(), &mcast_sw_if_index))
808 {
809 grp_set = remote_set = 1;
810 ipv4_set = 1;
811 }
812 else if (unformat (line_input, "group %U %U",
813 unformat_ip6_address, &remote.ip6,
814 unformat_vnet_sw_interface,
815 vnet_get_main(), &mcast_sw_if_index))
816 {
817 grp_set = remote_set = 1;
818 ipv6_set = 1;
819 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700820 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
821 {
Hongjun Nidf921cc2016-05-25 01:16:19 +0800822 if (ipv6_set)
Neale Ranns630198f2017-05-22 09:20:20 -0400823 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800824 else
Neale Ranns630198f2017-05-22 09:20:20 -0400825 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800826
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700827 if (encap_fib_index == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500828 {
829 error = clib_error_return (0, "nonexistent encap fib id %d", tmp);
830 goto done;
831 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700832 }
833 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
834 {
Hongjun Nidf921cc2016-05-25 01:16:19 +0800835 if (ipv6_set)
Neale Ranns630198f2017-05-22 09:20:20 -0400836 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800837 else
Neale Ranns630198f2017-05-22 09:20:20 -0400838 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800839
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700840 if (decap_fib_index == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500841 {
842 error = clib_error_return (0, "nonexistent decap fib id %d", tmp);
843 goto done;
844 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700845 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700846 else if (unformat (line_input, "vni %d", &vni))
847 vni_set = 1;
848 else if (unformat(line_input, "next-ip4"))
849 protocol = VXLAN_GPE_PROTOCOL_IP4;
850 else if (unformat(line_input, "next-ip6"))
851 protocol = VXLAN_GPE_PROTOCOL_IP6;
852 else if (unformat(line_input, "next-ethernet"))
853 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
854 else if (unformat(line_input, "next-nsh"))
855 protocol = VXLAN_GPE_PROTOCOL_NSH;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800856 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500857 {
858 error = clib_error_return (0, "parse error: '%U'",
859 format_unformat_error, line_input);
860 goto done;
861 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700862 }
863
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700864 if (local_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500865 {
866 error = clib_error_return (0, "tunnel local address not specified");
867 goto done;
868 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700869
870 if (remote_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500871 {
872 error = clib_error_return (0, "tunnel remote address not specified");
873 goto done;
874 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700875
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800876 if (grp_set && !ip46_address_is_multicast(&remote))
877 {
878 error = clib_error_return (0, "tunnel group address not multicast");
879 goto done;
880 }
881
882 if (grp_set == 0 && ip46_address_is_multicast(&remote))
883 {
884 error = clib_error_return (0, "remote address must be unicast");
885 goto done;
886 }
887
888 if (grp_set && mcast_sw_if_index == ~0)
889 {
890 error = clib_error_return (0, "tunnel nonexistent multicast device");
891 goto done;
892 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800893 if (ipv4_set && ipv6_set)
Billy McFalla9a20e72017-02-15 11:39:12 -0500894 {
895 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
896 goto done;
897 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800898
899 if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) ||
900 (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0))
Billy McFalla9a20e72017-02-15 11:39:12 -0500901 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800902 error = clib_error_return (0, "src and remote addresses are identical");
Billy McFalla9a20e72017-02-15 11:39:12 -0500903 goto done;
904 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800905
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700906 if (vni_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500907 {
908 error = clib_error_return (0, "vni not specified");
909 goto done;
910 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700911
912 memset (a, 0, sizeof (*a));
913
914 a->is_add = is_add;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800915 a->is_ip6 = ipv6_set;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700916
917#define _(x) a->x = x;
918 foreach_gpe_copy_field;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800919 if (ipv4_set) foreach_copy_ipv4
920 else foreach_copy_ipv6
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700921#undef _
922
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100923 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700924
925 switch(rv)
926 {
927 case 0:
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100928 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 -0700929 break;
930 case VNET_API_ERROR_INVALID_DECAP_NEXT:
Billy McFalla9a20e72017-02-15 11:39:12 -0500931 error = clib_error_return (0, "invalid decap-next...");
932 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700933
934 case VNET_API_ERROR_TUNNEL_EXIST:
Billy McFalla9a20e72017-02-15 11:39:12 -0500935 error = clib_error_return (0, "tunnel already exists...");
936 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700937
938 case VNET_API_ERROR_NO_SUCH_ENTRY:
Billy McFalla9a20e72017-02-15 11:39:12 -0500939 error = clib_error_return (0, "tunnel does not exist...");
940 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700941
942 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500943 error = clib_error_return
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700944 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500945 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700946 }
947
Billy McFalla9a20e72017-02-15 11:39:12 -0500948done:
949 unformat_free (line_input);
950
951 return error;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700952}
953
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800954/*?
955 * Add or delete a VXLAN-GPE Tunnel.
956 *
957 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
958 * to span multiple servers. This is done by building an L2 overlay on
959 * top of an L3 network underlay using VXLAN-GPE tunnels.
960 *
961 * This makes it possible for servers to be co-located in the same data
962 * center or be separated geographically as long as they are reachable
963 * through the underlay L3 network.
964 *
965 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE sengment.
966 *
967 * @cliexpar
968 * Example of how to create a VXLAN-GPE Tunnel:
969 * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 local 10.0.3.3 vni 13 encap-vrf-id 7}
970 * Example of how to delete a VXLAN Tunnel:
971 * @cliexcmd{create vxlan tunnel src 10.0.3.1 remote 10.0.3.3 vni 13 del}
972 ?*/
973/* *INDENT-OFF* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700974VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
975 .path = "create vxlan-gpe tunnel",
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800976 .short_help =
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800977 "create vxlan-gpe tunnel local <local-addr> "
978 " {remote <remote-addr>|group <mcast-addr> <intf-name>}"
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700979 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800980 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [del]\n",
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700981 .function = vxlan_gpe_add_del_tunnel_command_fn,
982};
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800983/* *INDENT-ON* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700984
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700985/**
986 * @brief CLI function for showing VXLAN GPE tunnels
987 *
988 * @param *vm
989 * @param *input
990 * @param *cmd
991 *
992 * @return error
993 *
994 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700995static clib_error_t *
996show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
997 unformat_input_t * input,
998 vlib_cli_command_t * cmd)
999{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001000 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001001 vxlan_gpe_tunnel_t * t;
Hongjun Nicccd1bf2016-06-07 18:43:05 +08001002
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001003 if (pool_elts (ngm->tunnels) == 0)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001004 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
1005
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001006 pool_foreach (t, ngm->tunnels,
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001007 ({
1008 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
1009 }));
Hongjun Nicccd1bf2016-06-07 18:43:05 +08001010
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001011 return 0;
1012}
1013
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001014/*?
1015 * Display all the VXLAN-GPE Tunnel entries.
1016 *
1017 * @cliexpar
1018 * Example of how to display the VXLAN-GPE Tunnel entries:
1019 * @cliexstart{show vxlan-gpe tunnel}
1020 * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
1021 * @cliexend
1022 ?*/
1023/* *INDENT-OFF* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001024VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
1025 .path = "show vxlan-gpe",
1026 .function = show_vxlan_gpe_tunnel_command_fn,
1027};
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001028/* *INDENT-ON* */
1029
1030void vnet_int_vxlan_gpe_bypass_mode (u32 sw_if_index,
1031 u8 is_ip6,
1032 u8 is_enable)
1033{
1034 if (is_ip6)
1035 vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-gpe-bypass",
1036 sw_if_index, is_enable, 0, 0);
1037 else
1038 vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-gpe-bypass",
1039 sw_if_index, is_enable, 0, 0);
1040}
1041
1042
1043static clib_error_t *
1044set_ip_vxlan_gpe_bypass (u32 is_ip6,
1045 unformat_input_t * input,
1046 vlib_cli_command_t * cmd)
1047{
1048 unformat_input_t _line_input, * line_input = &_line_input;
1049 vnet_main_t * vnm = vnet_get_main();
1050 clib_error_t * error = 0;
1051 u32 sw_if_index, is_enable;
1052
1053 sw_if_index = ~0;
1054 is_enable = 1;
1055
1056 if (! unformat_user (input, unformat_line_input, line_input))
1057 return 0;
1058
1059 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1060 {
1061 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1062 ;
1063 else if (unformat (line_input, "del"))
1064 is_enable = 0;
1065 else
1066 {
1067 error = unformat_parse_error (line_input);
1068 goto done;
1069 }
1070 }
1071
1072 if (~0 == sw_if_index)
1073 {
1074 error = clib_error_return (0, "unknown interface `%U'",
1075 format_unformat_error, line_input);
1076 goto done;
1077 }
1078
1079 vnet_int_vxlan_gpe_bypass_mode (sw_if_index, is_ip6, is_enable);
1080
1081 done:
1082 unformat_free (line_input);
1083
1084 return error;
1085}
1086
1087static clib_error_t *
1088set_ip4_vxlan_gpe_bypass (vlib_main_t * vm,
1089 unformat_input_t * input,
1090 vlib_cli_command_t * cmd)
1091{
1092 return set_ip_vxlan_gpe_bypass (0, input, cmd);
1093}
1094
1095/*?
1096 * This command adds the 'ip4-vxlan-gpe-bypass' graph node for a given interface.
1097 * By adding the IPv4 vxlan-gpe-bypass graph node to an interface, the node checks
1098 * for and validate input vxlan_gpe packet and bypass ip4-lookup, ip4-local,
1099 * ip4-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1100 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1101 *
1102 * @cliexpar
1103 * @parblock
1104 * Example of graph node before ip4-vxlan-gpe-bypass is enabled:
1105 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1106 * Name Next Previous
1107 * ip4-vxlan-gpe-bypass error-drop [0]
1108 * vxlan4-gpe-input [1]
1109 * ip4-lookup [2]
1110 * @cliexend
1111 *
1112 * Example of how to enable ip4-vxlan-gpe-bypass on an interface:
1113 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0}
1114 *
1115 * Example of graph node after ip4-vxlan-gpe-bypass is enabled:
1116 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1117 * Name Next Previous
1118 * ip4-vxlan-gpe-bypass error-drop [0] ip4-input
1119 * vxlan4-gpe-input [1] ip4-input-no-checksum
1120 * ip4-lookup [2]
1121 * @cliexend
1122 *
1123 * Example of how to display the feature enabed on an interface:
1124 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1125 * IP feature paths configured on GigabitEthernet2/0/0...
1126 * ...
1127 * ipv4 unicast:
1128 * ip4-vxlan-gpe-bypass
1129 * ip4-lookup
1130 * ...
1131 * @cliexend
1132 *
1133 * Example of how to disable ip4-vxlan-gpe-bypass on an interface:
1134 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1135 * @endparblock
1136?*/
1137/* *INDENT-OFF* */
1138VLIB_CLI_COMMAND (set_interface_ip_vxlan_gpe_bypass_command, static) = {
1139 .path = "set interface ip vxlan-gpe-bypass",
1140 .function = set_ip4_vxlan_gpe_bypass,
1141 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1142};
1143/* *INDENT-ON* */
1144
1145static clib_error_t *
1146set_ip6_vxlan_gpe_bypass (vlib_main_t * vm,
1147 unformat_input_t * input,
1148 vlib_cli_command_t * cmd)
1149{
1150 return set_ip_vxlan_gpe_bypass (1, input, cmd);
1151}
1152
1153/*?
1154 * This command adds the 'ip6-vxlan-gpe-bypass' graph node for a given interface.
1155 * By adding the IPv6 vxlan-gpe-bypass graph node to an interface, the node checks
1156 * for and validate input vxlan_gpe packet and bypass ip6-lookup, ip6-local,
1157 * ip6-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1158 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1159 *
1160 * @cliexpar
1161 * @parblock
1162 * Example of graph node before ip6-vxlan-gpe-bypass is enabled:
1163 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1164 * Name Next Previous
1165 * ip6-vxlan-gpe-bypass error-drop [0]
1166 * vxlan6-gpe-input [1]
1167 * ip6-lookup [2]
1168 * @cliexend
1169 *
1170 * Example of how to enable ip6-vxlan-gpe-bypass on an interface:
1171 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0}
1172 *
1173 * Example of graph node after ip6-vxlan-gpe-bypass is enabled:
1174 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1175 * Name Next Previous
1176 * ip6-vxlan-gpe-bypass error-drop [0] ip6-input
1177 * vxlan6-gpe-input [1] ip4-input-no-checksum
1178 * ip6-lookup [2]
1179 * @cliexend
1180 *
1181 * Example of how to display the feature enabed on an interface:
1182 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1183 * IP feature paths configured on GigabitEthernet2/0/0...
1184 * ...
1185 * ipv6 unicast:
1186 * ip6-vxlan-gpe-bypass
1187 * ip6-lookup
1188 * ...
1189 * @cliexend
1190 *
1191 * Example of how to disable ip6-vxlan-gpe-bypass on an interface:
1192 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1193 * @endparblock
1194?*/
1195/* *INDENT-OFF* */
1196VLIB_CLI_COMMAND (set_interface_ip6_vxlan_gpe_bypass_command, static) = {
1197 .path = "set interface ip6 vxlan-gpe-bypass",
1198 .function = set_ip6_vxlan_gpe_bypass,
1199 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1200};
1201/* *INDENT-ON* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001202
Hongjun Nie1bf5722017-08-03 20:34:45 +08001203/* *INDENT-OFF* */
1204VNET_FEATURE_INIT (ip4_vxlan_gpe_bypass, static) =
1205{
1206 .arc_name = "ip4-unicast",
1207 .node_name = "ip4-vxlan-gpe-bypass",
1208 .runs_before = VNET_FEATURES ("ip4-lookup"),
1209};
1210
1211VNET_FEATURE_INIT (ip6_vxlan_gpe_bypass, static) =
1212{
1213 .arc_name = "ip6-unicast",
1214 .node_name = "ip6-vxlan-gpe-bypass",
1215 .runs_before = VNET_FEATURES ("ip6-lookup"),
1216};
1217/* *INDENT-ON* */
1218
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -07001219/**
1220 * @brief Feature init function for VXLAN GPE
1221 *
1222 * @param *vm
1223 *
1224 * @return error
1225 *
1226 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001227clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
1228{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001229 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
Hongjun Nicccd1bf2016-06-07 18:43:05 +08001230
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001231 ngm->vnet_main = vnet_get_main();
1232 ngm->vlib_main = vm;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001233
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001234 ngm->vxlan4_gpe_tunnel_by_key
Hongjun Nidf921cc2016-05-25 01:16:19 +08001235 = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword));
1236
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001237 ngm->vxlan6_gpe_tunnel_by_key
Hongjun Nidf921cc2016-05-25 01:16:19 +08001238 = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword));
1239
1240
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001241 ngm->mcast_shared = hash_create_mem(0,
1242 sizeof(ip46_address_t),
1243 sizeof(mcast_shared_t));
1244
1245 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN_GPE,
Hongjun Nidf921cc2016-05-25 01:16:19 +08001246 vxlan4_gpe_input_node.index, 1 /* is_ip4 */);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001247 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN6_GPE,
Hongjun Nidf921cc2016-05-25 01:16:19 +08001248 vxlan6_gpe_input_node.index, 0 /* is_ip4 */);
Vengada Govindan6d403a02016-10-12 05:54:09 -07001249
1250 /* Register the list of standard decap protocols supported */
1251 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP4,
1252 VXLAN_GPE_INPUT_NEXT_IP4_INPUT);
1253 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP6,
1254 VXLAN_GPE_INPUT_NEXT_IP6_INPUT);
1255 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_ETHERNET,
1256 VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001257
1258 fib_node_register_type(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, &vxlan_gpe_vft);
1259
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001260 return 0;
1261}
1262
1263VLIB_INIT_FUNCTION(vxlan_gpe_init);
1264