blob: b13a7343ddd0eefb04161cff14d82fa88c1a990b [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{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800186 ASSERT(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL == node->fn_type);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800187 return ((vxlan_gpe_tunnel_t*) (((char*)node) -
188 STRUCT_OFFSET_OF(vxlan_gpe_tunnel_t, node)));
189}
190
191/**
192 * Function definition to backwalk a FIB node -
193 * Here we will restack the new dpo of VXLAN_GPE DIP to encap node.
194 */
195static fib_node_back_walk_rc_t
196vxlan_gpe_tunnel_back_walk (fib_node_t *node,
197 fib_node_back_walk_ctx_t *ctx)
198{
199 vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_from_fib_node(node));
200 return (FIB_NODE_BACK_WALK_CONTINUE);
201}
202
203/**
204 * Function definition to get a FIB node from its index
205 */
206static fib_node_t*
207vxlan_gpe_tunnel_fib_node_get (fib_node_index_t index)
208{
209 vxlan_gpe_tunnel_t * t;
210 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
211
212 t = pool_elt_at_index(ngm->tunnels, index);
213
214 return (&t->node);
215}
216
217/**
218 * Function definition to inform the FIB node that its last lock has gone.
219 */
220static void
221vxlan_gpe_tunnel_last_lock_gone (fib_node_t *node)
222{
223 /*
224 * The VXLAN_GPE tunnel is a root of the graph. As such
225 * it never has children and thus is never locked.
226 */
227 ASSERT(0);
228}
229
230/*
231 * Virtual function table registered by VXLAN_GPE tunnels
232 * for participation in the FIB object graph.
233 */
234const static fib_node_vft_t vxlan_gpe_vft = {
235 .fnv_get = vxlan_gpe_tunnel_fib_node_get,
236 .fnv_last_lock = vxlan_gpe_tunnel_last_lock_gone,
237 .fnv_back_walk = vxlan_gpe_tunnel_back_walk,
238};
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700239
240#define foreach_gpe_copy_field \
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700241_(vni) \
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800242_(protocol) \
243_(mcast_sw_if_index) \
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700244_(encap_fib_index) \
Hongjun Ni7deb1392016-06-15 22:49:23 +0800245_(decap_fib_index)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700246
Hongjun Nidf921cc2016-05-25 01:16:19 +0800247#define foreach_copy_ipv4 { \
248 _(local.ip4.as_u32) \
249 _(remote.ip4.as_u32) \
250}
251
252#define foreach_copy_ipv6 { \
253 _(local.ip6.as_u64[0]) \
254 _(local.ip6.as_u64[1]) \
255 _(remote.ip6.as_u64[0]) \
256 _(remote.ip6.as_u64[1]) \
257}
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700258
259
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700260/**
261 * @brief Calculate IPv4 VXLAN GPE rewrite header
262 *
263 * @param *t
264 *
265 * @return rc
266 *
267 */
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800268int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
Vengada Govindan6d403a02016-10-12 05:54:09 -0700269 u8 protocol_override, uword encap_next_node)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700270{
271 u8 *rw = 0;
272 ip4_header_t * ip0;
273 ip4_vxlan_gpe_header_t * h0;
274 int len;
275
Vengada Govindan6d403a02016-10-12 05:54:09 -0700276 len = sizeof (*h0) + extension_size;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700277
Vengada Govindan6d403a02016-10-12 05:54:09 -0700278 vec_free(t->rewrite);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700279 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
280
281 h0 = (ip4_vxlan_gpe_header_t *) rw;
282
283 /* Fixed portion of the (outer) ip4 header */
284 ip0 = &h0->ip4;
285 ip0->ip_version_and_header_length = 0x45;
286 ip0->ttl = 254;
287 ip0->protocol = IP_PROTOCOL_UDP;
288
289 /* we fix up the ip4 header length and checksum after-the-fact */
Hongjun Nidf921cc2016-05-25 01:16:19 +0800290 ip0->src_address.as_u32 = t->local.ip4.as_u32;
291 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700292 ip0->checksum = ip4_header_checksum (ip0);
293
294 /* UDP header, randomize src port on something, maybe? */
295 h0->udp.src_port = clib_host_to_net_u16 (4790);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800296 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700297
298 /* VXLAN header. Are we having fun yet? */
299 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
300 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700301 if (protocol_override)
302 {
303 h0->vxlan.protocol = protocol_override;
304 }
305 else
306 {
307 h0->vxlan.protocol = t->protocol;
308 }
309 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700310 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
311
312 t->rewrite = rw;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700313 t->encap_next_node = encap_next_node;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700314 return (0);
315}
316
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700317/**
318 * @brief Calculate IPv6 VXLAN GPE rewrite header
319 *
320 * @param *t
321 *
322 * @return rc
323 *
324 */
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800325int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
Vengada Govindan6d403a02016-10-12 05:54:09 -0700326 u8 protocol_override, uword encap_next_node)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800327{
328 u8 *rw = 0;
329 ip6_header_t * ip0;
330 ip6_vxlan_gpe_header_t * h0;
331 int len;
332
Vengada Govindan6d403a02016-10-12 05:54:09 -0700333 len = sizeof (*h0) + extension_size;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800334
Vengada Govindan6d403a02016-10-12 05:54:09 -0700335 vec_free(t->rewrite);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800336 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
337
338 h0 = (ip6_vxlan_gpe_header_t *) rw;
339
340 /* Fixed portion of the (outer) ip4 header */
341 ip0 = &h0->ip6;
342 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
343 ip0->hop_limit = 255;
344 ip0->protocol = IP_PROTOCOL_UDP;
345
346 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
347 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
348 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
349 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
350
351 /* UDP header, randomize src port on something, maybe? */
352 h0->udp.src_port = clib_host_to_net_u16 (4790);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800353 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800354
355 /* VXLAN header. Are we having fun yet? */
356 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
357 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700358 if (protocol_override)
359 {
360 h0->vxlan.protocol = t->protocol;
361 }
362 else
363 {
364 h0->vxlan.protocol = protocol_override;
365 }
366 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800367 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
368
369 t->rewrite = rw;
Vengada Govindan6d403a02016-10-12 05:54:09 -0700370 t->encap_next_node = encap_next_node;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800371 return (0);
372}
373
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800374static void
375hash_set_key_copy (uword ** h, void * key, uword v) {
376 size_t ksz = hash_header(*h)->user;
377 void * copy = clib_mem_alloc (ksz);
378 clib_memcpy (copy, key, ksz);
379 hash_set_mem (*h, copy, v);
380}
381
382static void
383hash_unset_key_free (uword ** h, void * key) {
384 hash_pair_t * hp = hash_get_pair_mem (*h, key);
385 ASSERT (hp);
386 key = uword_to_pointer (hp->key, void *);
387 hash_unset_mem (*h, key);
388 clib_mem_free (key);
389}
390
391static uword
392vtep_addr_ref(ip46_address_t *ip)
393{
394 uword *vtep = ip46_address_is_ip4(ip) ?
395 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
396 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
397 if (vtep)
398 return ++(*vtep);
399 ip46_address_is_ip4(ip) ?
400 hash_set (vxlan_gpe_main.vtep4, ip->ip4.as_u32, 1) :
401 hash_set_key_copy (&vxlan_gpe_main.vtep6, &ip->ip6, 1);
402 return 1;
403}
404
405static uword
406vtep_addr_unref(ip46_address_t *ip)
407{
408 uword *vtep = ip46_address_is_ip4(ip) ?
409 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
410 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
411 ASSERT(vtep);
412 if (--(*vtep) != 0)
413 return *vtep;
414 ip46_address_is_ip4(ip) ?
415 hash_unset (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
416 hash_unset_key_free (&vxlan_gpe_main.vtep6, &ip->ip6);
417 return 0;
418}
419
420typedef CLIB_PACKED(union {
421 struct {
422 fib_node_index_t mfib_entry_index;
423 adj_index_t mcast_adj_index;
424 };
425 u64 as_u64;
426}) mcast_shared_t;
427
428static inline mcast_shared_t
429mcast_shared_get(ip46_address_t * ip)
430{
431 ASSERT(ip46_address_is_multicast(ip));
432 uword * p = hash_get_mem (vxlan_gpe_main.mcast_shared, ip);
433 ASSERT(p);
434 return (mcast_shared_t) { .as_u64 = *p };
435}
436
437static inline void
438mcast_shared_add(ip46_address_t *remote,
439 fib_node_index_t mfei,
440 adj_index_t ai)
441{
442 mcast_shared_t new_ep = {
443 .mcast_adj_index = ai,
444 .mfib_entry_index = mfei,
445 };
446
447 hash_set_key_copy (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
448}
449
450static inline void
451mcast_shared_remove(ip46_address_t *remote)
452{
453 mcast_shared_t ep = mcast_shared_get(remote);
454
455 adj_unlock(ep.mcast_adj_index);
456 mfib_table_entry_delete_index(ep.mfib_entry_index,
457 MFIB_SOURCE_VXLAN_GPE);
458
459 hash_unset_key_free (&vxlan_gpe_main.mcast_shared, remote);
460}
461
462static inline fib_protocol_t
463fib_ip_proto(bool is_ip6)
464{
465 return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
466}
467
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700468/**
469 * @brief Add or Del a VXLAN GPE tunnel
470 *
471 * @param *a
472 * @param *sw_if_index
473 *
474 * @return rc
475 *
476 */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800477int vnet_vxlan_gpe_add_del_tunnel
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700478(vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
479{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800480 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700481 vxlan_gpe_tunnel_t *t = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800482 vnet_main_t * vnm = ngm->vnet_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700483 vnet_hw_interface_t * hi;
484 uword * p;
485 u32 hw_if_index = ~0;
486 u32 sw_if_index = ~0;
487 int rv;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800488 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
489 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800490 u32 is_ip6 = a->is_ip6;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800491
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800492 if (!is_ip6)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800493 {
494 key4.local = a->local.ip4.as_u32;
495 key4.remote = a->remote.ip4.as_u32;
496 key4.vni = clib_host_to_net_u32 (a->vni << 8);
497 key4.pad = 0;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700498
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800499 p = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800500 }
501 else
502 {
503 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
504 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
505 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
506 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
507 key6.vni = clib_host_to_net_u32 (a->vni << 8);
508
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800509 p = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800510 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800511
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700512 if (a->is_add)
513 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800514 l2input_main_t * l2im = &l2input_main;
515
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700516 /* adding a tunnel: tunnel must not already exist */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800517 if (p)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800518 return VNET_API_ERROR_TUNNEL_EXIST;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800519
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800520 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700521 memset (t, 0, sizeof (*t));
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800522
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700523 /* copy from arg structure */
524#define _(x) t->x = a->x;
525 foreach_gpe_copy_field;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800526 if (!a->is_ip6) foreach_copy_ipv4
527 else foreach_copy_ipv6
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700528#undef _
529
Hongjun Nidf921cc2016-05-25 01:16:19 +0800530 if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
531
532 if (!a->is_ip6) {
Vengada Govindan6d403a02016-10-12 05:54:09 -0700533 rv = vxlan4_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800534 } else {
Vengada Govindan6d403a02016-10-12 05:54:09 -0700535 rv = vxlan6_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800536 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700537
538 if (rv)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800539 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800540 pool_put (ngm->tunnels, t);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700541 return rv;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800542 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700543
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800544 if (!is_ip6)
Hongjun Nidf921cc2016-05-25 01:16:19 +0800545 {
546 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
547 clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800548 hash_set_mem (ngm->vxlan4_gpe_tunnel_by_key, key4_copy,
549 t - ngm->tunnels);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800550 }
551 else
552 {
553 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
Florin Coras1d3b2ab2016-05-31 15:53:44 +0300554 clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800555 hash_set_mem (ngm->vxlan6_gpe_tunnel_by_key, key6_copy,
556 t - ngm->tunnels);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800557 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800558
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800559 if (vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700560 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800561 vnet_interface_main_t * im = &vnm->interface_main;
562 hw_if_index = ngm->free_vxlan_gpe_tunnel_hw_if_indices
563 [vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
564 _vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800565
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700566 hi = vnet_get_hw_interface (vnm, hw_if_index);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800567 hi->dev_instance = t - ngm->tunnels;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700568 hi->hw_instance = hi->dev_instance;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800569 /* clear old stats of freed tunnel before reuse */
570 sw_if_index = hi->sw_if_index;
571 vnet_interface_counter_lock(im);
572 vlib_zero_combined_counter
573 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
574 vlib_zero_combined_counter
575 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
576 vlib_zero_simple_counter
577 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
578 vnet_interface_counter_unlock(im);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700579 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800580 else
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700581 {
582 hw_if_index = vnet_register_interface
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800583 (vnm, vxlan_gpe_device_class.index, t - ngm->tunnels,
584 vxlan_gpe_hw_class.index, t - ngm->tunnels);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700585 hi = vnet_get_hw_interface (vnm, hw_if_index);
586 hi->output_node_index = vxlan_gpe_encap_node.index;
587 }
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800588
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700589 t->hw_if_index = hw_if_index;
590 t->sw_if_index = sw_if_index = hi->sw_if_index;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800591 vec_validate_init_empty (ngm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
592 ngm->tunnel_index_by_sw_if_index[sw_if_index] = t - ngm->tunnels;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800593
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800594 /* setup l2 input config with l2 feature and bd 0 to drop packet */
595 vec_validate (l2im->configs, sw_if_index);
596 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
597 l2im->configs[sw_if_index].bd_index = 0;
598
599 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
600 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800601 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700602 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800603 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL);
604 fib_prefix_t tun_remote_pfx;
605 u32 encap_index = vxlan_gpe_encap_node.index;
606 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
607
608 fib_prefix_from_ip46_addr(&t->remote, &tun_remote_pfx);
609 if (!ip46_address_is_multicast(&t->remote))
610 {
611 /* Unicast tunnel -
612 * source the FIB entry for the tunnel's destination
613 * and become a child thereof. The tunnel will then get poked
614 * when the forwarding for the entry updates, and the tunnel can
615 * re-stack accordingly
616 */
617 vtep_addr_ref(&t->local);
618 t->fib_entry_index = fib_table_entry_special_add
619 (t->encap_fib_index, &tun_remote_pfx, FIB_SOURCE_RR,
620 FIB_ENTRY_FLAG_NONE);
621 t->sibling_index = fib_entry_child_add
622 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, t - ngm->tunnels);
623 vxlan_gpe_tunnel_restack_dpo(t);
624 }
625 else
626 {
627 /* Multicast tunnel -
628 * as the same mcast group can be used for mutiple mcast tunnels
629 * with different VNIs, create the output fib adjecency only if
630 * it does not already exist
631 */
632 fib_protocol_t fp = fib_ip_proto(is_ip6);
633
634 if (vtep_addr_ref(&t->remote) == 1)
635 {
636 fib_node_index_t mfei;
637 adj_index_t ai;
638 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700639 .frp_proto = fib_proto_to_dpo(fp),
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800640 .frp_addr = zero_addr,
641 .frp_sw_if_index = 0xffffffff,
642 .frp_fib_index = ~0,
643 .frp_weight = 0,
644 .frp_flags = FIB_ROUTE_PATH_LOCAL,
645 };
646 const mfib_prefix_t mpfx = {
647 .fp_proto = fp,
648 .fp_len = (is_ip6 ? 128 : 32),
649 .fp_grp_addr = tun_remote_pfx.fp_addr,
650 };
651
652 /*
653 * Setup the (*,G) to receive traffic on the mcast group
654 * - the forwarding interface is for-us
655 * - the accepting interface is that from the API
656 */
657 mfib_table_entry_path_update(t->encap_fib_index,
658 &mpfx,
659 MFIB_SOURCE_VXLAN_GPE,
660 &path,
661 MFIB_ITF_FLAG_FORWARD);
662
663 path.frp_sw_if_index = a->mcast_sw_if_index;
664 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
665 mfei = mfib_table_entry_path_update(t->encap_fib_index,
666 &mpfx,
667 MFIB_SOURCE_VXLAN_GPE,
668 &path,
669 MFIB_ITF_FLAG_ACCEPT);
670
671 /*
672 * Create the mcast adjacency to send traffic to the group
673 */
674 ai = adj_mcast_add_or_lock(fp,
675 fib_proto_to_link(fp),
676 a->mcast_sw_if_index);
677
678 /*
679 * create a new end-point
680 */
681 mcast_shared_add(&t->remote, mfei, ai);
682 }
683
684 dpo_id_t dpo = DPO_INVALID;
685 mcast_shared_t ep = mcast_shared_get(&t->remote);
686
687 /* Stack shared mcast remote mac addr rewrite on encap */
688 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
689 fib_proto_to_dpo(fp),
690 ep.mcast_adj_index);
691
692 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
693 dpo_reset (&dpo);
694 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
695 }
696
697 /* Set vxlan tunnel output node */
698 hi->output_node_index = encap_index;
699
700 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700701 }
702 else
703 {
704 /* deleting a tunnel: tunnel must exist */
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800705 if (!p)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700706 return VNET_API_ERROR_NO_SUCH_ENTRY;
707
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800708 t = pool_elt_at_index (ngm->tunnels, p[0]);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700709
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800710 sw_if_index = t->sw_if_index;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700711 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800712 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, t->sw_if_index);
713 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
714 set_int_l2_mode(ngm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
715 vec_add1 (ngm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700716
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800717 ngm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
Hongjun Nic0959c92016-06-16 20:18:15 +0800718
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800719 if (!is_ip6)
720 hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800721 else
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800722 hash_unset_key_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700723
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800724 if (!ip46_address_is_multicast(&t->remote))
725 {
726 vtep_addr_unref(&t->local);
727 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
728 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
729 }
730 else if (vtep_addr_unref(&t->remote) == 0)
731 {
732 mcast_shared_remove(&t->remote);
733 }
734
735 fib_node_deinit(&t->node);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700736 vec_free (t->rewrite);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800737 pool_put (ngm->tunnels, t);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700738 }
739
740 if (sw_if_indexp)
741 *sw_if_indexp = sw_if_index;
742
743 return 0;
744}
745
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700746static clib_error_t *
747vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
748 unformat_input_t * input,
749 vlib_cli_command_t * cmd)
750{
751 unformat_input_t _line_input, * line_input = &_line_input;
752 u8 is_add = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800753 ip46_address_t local, remote;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700754 u8 local_set = 0;
755 u8 remote_set = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800756 u8 grp_set = 0;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800757 u8 ipv4_set = 0;
758 u8 ipv6_set = 0;
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800759 u32 mcast_sw_if_index = ~0;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700760 u32 encap_fib_index = 0;
761 u32 decap_fib_index = 0;
762 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700763 u32 vni;
764 u8 vni_set = 0;
765 int rv;
766 u32 tmp;
767 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100768 u32 sw_if_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500769 clib_error_t *error = NULL;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800770
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700771 /* Get a line of input. */
772 if (! unformat_user (input, unformat_line_input, line_input))
773 return 0;
774
775 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
776 if (unformat (line_input, "del"))
777 is_add = 0;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800778 else if (unformat (line_input, "local %U",
Hongjun Nidf921cc2016-05-25 01:16:19 +0800779 unformat_ip4_address, &local.ip4))
780 {
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700781 local_set = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800782 ipv4_set = 1;
783 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700784 else if (unformat (line_input, "remote %U",
Hongjun Nidf921cc2016-05-25 01:16:19 +0800785 unformat_ip4_address, &remote.ip4))
786 {
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700787 remote_set = 1;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800788 ipv4_set = 1;
789 }
790 else if (unformat (line_input, "local %U",
791 unformat_ip6_address, &local.ip6))
792 {
793 local_set = 1;
794 ipv6_set = 1;
795 }
796 else if (unformat (line_input, "remote %U",
797 unformat_ip6_address, &remote.ip6))
798 {
799 remote_set = 1;
800 ipv6_set = 1;
801 }
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800802 else if (unformat (line_input, "group %U %U",
803 unformat_ip4_address, &remote.ip4,
804 unformat_vnet_sw_interface,
805 vnet_get_main(), &mcast_sw_if_index))
806 {
807 grp_set = remote_set = 1;
808 ipv4_set = 1;
809 }
810 else if (unformat (line_input, "group %U %U",
811 unformat_ip6_address, &remote.ip6,
812 unformat_vnet_sw_interface,
813 vnet_get_main(), &mcast_sw_if_index))
814 {
815 grp_set = remote_set = 1;
816 ipv6_set = 1;
817 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700818 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
819 {
Hongjun Nidf921cc2016-05-25 01:16:19 +0800820 if (ipv6_set)
Neale Ranns630198f2017-05-22 09:20:20 -0400821 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800822 else
Neale Ranns630198f2017-05-22 09:20:20 -0400823 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800824
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700825 if (encap_fib_index == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500826 {
827 error = clib_error_return (0, "nonexistent encap fib id %d", tmp);
828 goto done;
829 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700830 }
831 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
832 {
Hongjun Nidf921cc2016-05-25 01:16:19 +0800833 if (ipv6_set)
Neale Ranns630198f2017-05-22 09:20:20 -0400834 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800835 else
Neale Ranns630198f2017-05-22 09:20:20 -0400836 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
Hongjun Nidf921cc2016-05-25 01:16:19 +0800837
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700838 if (decap_fib_index == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500839 {
840 error = clib_error_return (0, "nonexistent decap fib id %d", tmp);
841 goto done;
842 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700843 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700844 else if (unformat (line_input, "vni %d", &vni))
845 vni_set = 1;
846 else if (unformat(line_input, "next-ip4"))
847 protocol = VXLAN_GPE_PROTOCOL_IP4;
848 else if (unformat(line_input, "next-ip6"))
849 protocol = VXLAN_GPE_PROTOCOL_IP6;
850 else if (unformat(line_input, "next-ethernet"))
851 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
852 else if (unformat(line_input, "next-nsh"))
853 protocol = VXLAN_GPE_PROTOCOL_NSH;
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800854 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500855 {
856 error = clib_error_return (0, "parse error: '%U'",
857 format_unformat_error, line_input);
858 goto done;
859 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700860 }
861
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700862 if (local_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500863 {
864 error = clib_error_return (0, "tunnel local address not specified");
865 goto done;
866 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700867
868 if (remote_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500869 {
870 error = clib_error_return (0, "tunnel remote address not specified");
871 goto done;
872 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700873
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800874 if (grp_set && !ip46_address_is_multicast(&remote))
875 {
876 error = clib_error_return (0, "tunnel group address not multicast");
877 goto done;
878 }
879
880 if (grp_set == 0 && ip46_address_is_multicast(&remote))
881 {
882 error = clib_error_return (0, "remote address must be unicast");
883 goto done;
884 }
885
886 if (grp_set && mcast_sw_if_index == ~0)
887 {
888 error = clib_error_return (0, "tunnel nonexistent multicast device");
889 goto done;
890 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800891 if (ipv4_set && ipv6_set)
Billy McFalla9a20e72017-02-15 11:39:12 -0500892 {
893 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
894 goto done;
895 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800896
897 if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) ||
898 (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0))
Billy McFalla9a20e72017-02-15 11:39:12 -0500899 {
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800900 error = clib_error_return (0, "src and remote addresses are identical");
Billy McFalla9a20e72017-02-15 11:39:12 -0500901 goto done;
902 }
Hongjun Nidf921cc2016-05-25 01:16:19 +0800903
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700904 if (vni_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500905 {
906 error = clib_error_return (0, "vni not specified");
907 goto done;
908 }
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700909
910 memset (a, 0, sizeof (*a));
911
912 a->is_add = is_add;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800913 a->is_ip6 = ipv6_set;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700914
915#define _(x) a->x = x;
916 foreach_gpe_copy_field;
Hongjun Nidf921cc2016-05-25 01:16:19 +0800917 if (ipv4_set) foreach_copy_ipv4
918 else foreach_copy_ipv6
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700919#undef _
920
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100921 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700922
923 switch(rv)
924 {
925 case 0:
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100926 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 -0700927 break;
928 case VNET_API_ERROR_INVALID_DECAP_NEXT:
Billy McFalla9a20e72017-02-15 11:39:12 -0500929 error = clib_error_return (0, "invalid decap-next...");
930 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700931
932 case VNET_API_ERROR_TUNNEL_EXIST:
Billy McFalla9a20e72017-02-15 11:39:12 -0500933 error = clib_error_return (0, "tunnel already exists...");
934 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700935
936 case VNET_API_ERROR_NO_SUCH_ENTRY:
Billy McFalla9a20e72017-02-15 11:39:12 -0500937 error = clib_error_return (0, "tunnel does not exist...");
938 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700939
940 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500941 error = clib_error_return
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700942 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500943 goto done;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700944 }
945
Billy McFalla9a20e72017-02-15 11:39:12 -0500946done:
947 unformat_free (line_input);
948
949 return error;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700950}
951
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800952/*?
953 * Add or delete a VXLAN-GPE Tunnel.
954 *
955 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
956 * to span multiple servers. This is done by building an L2 overlay on
957 * top of an L3 network underlay using VXLAN-GPE tunnels.
958 *
959 * This makes it possible for servers to be co-located in the same data
960 * center or be separated geographically as long as they are reachable
961 * through the underlay L3 network.
962 *
963 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE sengment.
964 *
965 * @cliexpar
966 * Example of how to create a VXLAN-GPE Tunnel:
967 * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 local 10.0.3.3 vni 13 encap-vrf-id 7}
968 * Example of how to delete a VXLAN Tunnel:
969 * @cliexcmd{create vxlan tunnel src 10.0.3.1 remote 10.0.3.3 vni 13 del}
970 ?*/
971/* *INDENT-OFF* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700972VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
973 .path = "create vxlan-gpe tunnel",
Hongjun Nicccd1bf2016-06-07 18:43:05 +0800974 .short_help =
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800975 "create vxlan-gpe tunnel local <local-addr> "
976 " {remote <remote-addr>|group <mcast-addr> <intf-name>}"
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700977 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800978 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [del]\n",
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700979 .function = vxlan_gpe_add_del_tunnel_command_fn,
980};
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800981/* *INDENT-ON* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700982
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -0700983/**
984 * @brief CLI function for showing VXLAN GPE tunnels
985 *
986 * @param *vm
987 * @param *input
988 * @param *cmd
989 *
990 * @return error
991 *
992 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700993static clib_error_t *
994show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
995 unformat_input_t * input,
996 vlib_cli_command_t * cmd)
997{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800998 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -0700999 vxlan_gpe_tunnel_t * t;
Hongjun Nicccd1bf2016-06-07 18:43:05 +08001000
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001001 if (pool_elts (ngm->tunnels) == 0)
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001002 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
1003
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001004 pool_foreach (t, ngm->tunnels,
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001005 ({
1006 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
1007 }));
Hongjun Nicccd1bf2016-06-07 18:43:05 +08001008
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001009 return 0;
1010}
1011
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001012/*?
1013 * Display all the VXLAN-GPE Tunnel entries.
1014 *
1015 * @cliexpar
1016 * Example of how to display the VXLAN-GPE Tunnel entries:
1017 * @cliexstart{show vxlan-gpe tunnel}
1018 * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
1019 * @cliexend
1020 ?*/
1021/* *INDENT-OFF* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001022VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
1023 .path = "show vxlan-gpe",
1024 .function = show_vxlan_gpe_tunnel_command_fn,
1025};
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001026/* *INDENT-ON* */
1027
1028void vnet_int_vxlan_gpe_bypass_mode (u32 sw_if_index,
1029 u8 is_ip6,
1030 u8 is_enable)
1031{
1032 if (is_ip6)
1033 vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-gpe-bypass",
1034 sw_if_index, is_enable, 0, 0);
1035 else
1036 vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-gpe-bypass",
1037 sw_if_index, is_enable, 0, 0);
1038}
1039
1040
1041static clib_error_t *
1042set_ip_vxlan_gpe_bypass (u32 is_ip6,
1043 unformat_input_t * input,
1044 vlib_cli_command_t * cmd)
1045{
1046 unformat_input_t _line_input, * line_input = &_line_input;
1047 vnet_main_t * vnm = vnet_get_main();
1048 clib_error_t * error = 0;
1049 u32 sw_if_index, is_enable;
1050
1051 sw_if_index = ~0;
1052 is_enable = 1;
1053
1054 if (! unformat_user (input, unformat_line_input, line_input))
1055 return 0;
1056
1057 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1058 {
1059 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1060 ;
1061 else if (unformat (line_input, "del"))
1062 is_enable = 0;
1063 else
1064 {
1065 error = unformat_parse_error (line_input);
1066 goto done;
1067 }
1068 }
1069
1070 if (~0 == sw_if_index)
1071 {
1072 error = clib_error_return (0, "unknown interface `%U'",
1073 format_unformat_error, line_input);
1074 goto done;
1075 }
1076
1077 vnet_int_vxlan_gpe_bypass_mode (sw_if_index, is_ip6, is_enable);
1078
1079 done:
1080 unformat_free (line_input);
1081
1082 return error;
1083}
1084
1085static clib_error_t *
1086set_ip4_vxlan_gpe_bypass (vlib_main_t * vm,
1087 unformat_input_t * input,
1088 vlib_cli_command_t * cmd)
1089{
1090 return set_ip_vxlan_gpe_bypass (0, input, cmd);
1091}
1092
1093/*?
1094 * This command adds the 'ip4-vxlan-gpe-bypass' graph node for a given interface.
1095 * By adding the IPv4 vxlan-gpe-bypass graph node to an interface, the node checks
1096 * for and validate input vxlan_gpe packet and bypass ip4-lookup, ip4-local,
1097 * ip4-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1098 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1099 *
1100 * @cliexpar
1101 * @parblock
1102 * Example of graph node before ip4-vxlan-gpe-bypass is enabled:
1103 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1104 * Name Next Previous
1105 * ip4-vxlan-gpe-bypass error-drop [0]
1106 * vxlan4-gpe-input [1]
1107 * ip4-lookup [2]
1108 * @cliexend
1109 *
1110 * Example of how to enable ip4-vxlan-gpe-bypass on an interface:
1111 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0}
1112 *
1113 * Example of graph node after ip4-vxlan-gpe-bypass is enabled:
1114 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1115 * Name Next Previous
1116 * ip4-vxlan-gpe-bypass error-drop [0] ip4-input
1117 * vxlan4-gpe-input [1] ip4-input-no-checksum
1118 * ip4-lookup [2]
1119 * @cliexend
1120 *
1121 * Example of how to display the feature enabed on an interface:
1122 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1123 * IP feature paths configured on GigabitEthernet2/0/0...
1124 * ...
1125 * ipv4 unicast:
1126 * ip4-vxlan-gpe-bypass
1127 * ip4-lookup
1128 * ...
1129 * @cliexend
1130 *
1131 * Example of how to disable ip4-vxlan-gpe-bypass on an interface:
1132 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1133 * @endparblock
1134?*/
1135/* *INDENT-OFF* */
1136VLIB_CLI_COMMAND (set_interface_ip_vxlan_gpe_bypass_command, static) = {
1137 .path = "set interface ip vxlan-gpe-bypass",
1138 .function = set_ip4_vxlan_gpe_bypass,
1139 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1140};
1141/* *INDENT-ON* */
1142
1143static clib_error_t *
1144set_ip6_vxlan_gpe_bypass (vlib_main_t * vm,
1145 unformat_input_t * input,
1146 vlib_cli_command_t * cmd)
1147{
1148 return set_ip_vxlan_gpe_bypass (1, input, cmd);
1149}
1150
1151/*?
1152 * This command adds the 'ip6-vxlan-gpe-bypass' graph node for a given interface.
1153 * By adding the IPv6 vxlan-gpe-bypass graph node to an interface, the node checks
1154 * for and validate input vxlan_gpe packet and bypass ip6-lookup, ip6-local,
1155 * ip6-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1156 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1157 *
1158 * @cliexpar
1159 * @parblock
1160 * Example of graph node before ip6-vxlan-gpe-bypass is enabled:
1161 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1162 * Name Next Previous
1163 * ip6-vxlan-gpe-bypass error-drop [0]
1164 * vxlan6-gpe-input [1]
1165 * ip6-lookup [2]
1166 * @cliexend
1167 *
1168 * Example of how to enable ip6-vxlan-gpe-bypass on an interface:
1169 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0}
1170 *
1171 * Example of graph node after ip6-vxlan-gpe-bypass is enabled:
1172 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1173 * Name Next Previous
1174 * ip6-vxlan-gpe-bypass error-drop [0] ip6-input
1175 * vxlan6-gpe-input [1] ip4-input-no-checksum
1176 * ip6-lookup [2]
1177 * @cliexend
1178 *
1179 * Example of how to display the feature enabed on an interface:
1180 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1181 * IP feature paths configured on GigabitEthernet2/0/0...
1182 * ...
1183 * ipv6 unicast:
1184 * ip6-vxlan-gpe-bypass
1185 * ip6-lookup
1186 * ...
1187 * @cliexend
1188 *
1189 * Example of how to disable ip6-vxlan-gpe-bypass on an interface:
1190 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1191 * @endparblock
1192?*/
1193/* *INDENT-OFF* */
1194VLIB_CLI_COMMAND (set_interface_ip6_vxlan_gpe_bypass_command, static) = {
1195 .path = "set interface ip6 vxlan-gpe-bypass",
1196 .function = set_ip6_vxlan_gpe_bypass,
1197 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1198};
1199/* *INDENT-ON* */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001200
Hongjun Nie1bf5722017-08-03 20:34:45 +08001201/* *INDENT-OFF* */
1202VNET_FEATURE_INIT (ip4_vxlan_gpe_bypass, static) =
1203{
1204 .arc_name = "ip4-unicast",
1205 .node_name = "ip4-vxlan-gpe-bypass",
1206 .runs_before = VNET_FEATURES ("ip4-lookup"),
1207};
1208
1209VNET_FEATURE_INIT (ip6_vxlan_gpe_bypass, static) =
1210{
1211 .arc_name = "ip6-unicast",
1212 .node_name = "ip6-vxlan-gpe-bypass",
1213 .runs_before = VNET_FEATURES ("ip6-lookup"),
1214};
1215/* *INDENT-ON* */
1216
Keith Burns (alagalah)d46cca12016-08-25 11:21:39 -07001217/**
1218 * @brief Feature init function for VXLAN GPE
1219 *
1220 * @param *vm
1221 *
1222 * @return error
1223 *
1224 */
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001225clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
1226{
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001227 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
Hongjun Nicccd1bf2016-06-07 18:43:05 +08001228
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001229 ngm->vnet_main = vnet_get_main();
1230 ngm->vlib_main = vm;
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001231
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001232 ngm->vxlan4_gpe_tunnel_by_key
Hongjun Nidf921cc2016-05-25 01:16:19 +08001233 = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword));
1234
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001235 ngm->vxlan6_gpe_tunnel_by_key
Hongjun Nidf921cc2016-05-25 01:16:19 +08001236 = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword));
1237
1238
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001239 ngm->mcast_shared = hash_create_mem(0,
1240 sizeof(ip46_address_t),
1241 sizeof(mcast_shared_t));
1242
1243 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN_GPE,
Hongjun Nidf921cc2016-05-25 01:16:19 +08001244 vxlan4_gpe_input_node.index, 1 /* is_ip4 */);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001245 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN6_GPE,
Hongjun Nidf921cc2016-05-25 01:16:19 +08001246 vxlan6_gpe_input_node.index, 0 /* is_ip4 */);
Vengada Govindan6d403a02016-10-12 05:54:09 -07001247
1248 /* Register the list of standard decap protocols supported */
1249 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP4,
1250 VXLAN_GPE_INPUT_NEXT_IP4_INPUT);
1251 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP6,
1252 VXLAN_GPE_INPUT_NEXT_IP6_INPUT);
1253 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_ETHERNET,
1254 VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001255
1256 fib_node_register_type(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, &vxlan_gpe_vft);
1257
Keith Burns (alagalah)94b14422016-05-05 18:16:50 -07001258 return 0;
1259}
1260
1261VLIB_INIT_FUNCTION(vxlan_gpe_init);
1262