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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/vxlan-gpe/vxlan_gpe.h>
16 #include <vnet/ip/format.h>
18 vxlan_gpe_main_t vxlan_gpe_main;
20 u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
22 vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
23 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
25 s = format (s, "[%d] local: %U remote: %U ",
27 format_ip46_address, &t->local,
28 format_ip46_address, &t->remote);
30 s = format (s, " vxlan VNI %d ", t->vni);
34 case VXLAN_GPE_PROTOCOL_IP4:
35 s = format (s, "next-protocol ip4");
37 case VXLAN_GPE_PROTOCOL_IP6:
38 s = format (s, "next-protocol ip6");
40 case VXLAN_GPE_PROTOCOL_ETHERNET:
41 s = format (s, "next-protocol ethernet");
43 case VXLAN_GPE_PROTOCOL_NSH:
44 s = format (s, "next-protocol nsh");
47 s = format (s, "next-protocol unknown %d", t->protocol);
50 s = format (s, " fibs: (encap %d, decap %d)",
57 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
59 u32 dev_instance = va_arg (*args, u32);
60 return format (s, "vxlan_gpe_tunnel%d", dev_instance);
64 static uword dummy_interface_tx (vlib_main_t * vm,
65 vlib_node_runtime_t * node,
68 clib_warning ("you shouldn't be here, leaking buffers...");
69 return frame->n_vectors;
71 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
73 .format_device_name = format_vxlan_gpe_name,
74 .format_tx_trace = format_vxlan_gpe_encap_trace,
75 .tx_function = dummy_interface_tx,
78 static uword dummy_set_rewrite (vnet_main_t * vnm,
83 uword max_rewrite_bytes)
89 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
91 u32 dev_instance = va_arg (*args, u32);
92 s = format (s, "unimplemented dev %u", dev_instance);
96 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
98 .format_header = format_vxlan_gpe_header_with_length,
99 .set_rewrite = dummy_set_rewrite,
103 #define foreach_gpe_copy_field \
110 #define foreach_copy_ipv4 { \
111 _(local.ip4.as_u32) \
112 _(remote.ip4.as_u32) \
115 #define foreach_copy_ipv6 { \
116 _(local.ip6.as_u64[0]) \
117 _(local.ip6.as_u64[1]) \
118 _(remote.ip6.as_u64[0]) \
119 _(remote.ip6.as_u64[1]) \
123 static int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t)
127 ip4_vxlan_gpe_header_t * h0;
132 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
134 h0 = (ip4_vxlan_gpe_header_t *) rw;
136 /* Fixed portion of the (outer) ip4 header */
138 ip0->ip_version_and_header_length = 0x45;
140 ip0->protocol = IP_PROTOCOL_UDP;
142 /* we fix up the ip4 header length and checksum after-the-fact */
143 ip0->src_address.as_u32 = t->local.ip4.as_u32;
144 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
145 ip0->checksum = ip4_header_checksum (ip0);
147 /* UDP header, randomize src port on something, maybe? */
148 h0->udp.src_port = clib_host_to_net_u16 (4790);
149 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
151 /* VXLAN header. Are we having fun yet? */
152 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
153 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
154 h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
155 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
161 static int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t)
165 ip6_vxlan_gpe_header_t * h0;
170 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
172 h0 = (ip6_vxlan_gpe_header_t *) rw;
174 /* Fixed portion of the (outer) ip4 header */
176 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
177 ip0->hop_limit = 255;
178 ip0->protocol = IP_PROTOCOL_UDP;
180 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
181 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
182 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
183 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
185 /* UDP header, randomize src port on something, maybe? */
186 h0->udp.src_port = clib_host_to_net_u16 (4790);
187 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
189 /* VXLAN header. Are we having fun yet? */
190 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
191 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
192 h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
193 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
199 int vnet_vxlan_gpe_add_del_tunnel
200 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
202 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
203 vxlan_gpe_tunnel_t *t = 0;
204 vnet_main_t * vnm = gm->vnet_main;
205 vnet_hw_interface_t * hi;
207 u32 hw_if_index = ~0;
208 u32 sw_if_index = ~0;
210 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
211 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
216 key4.local = a->local.ip4.as_u32;
217 key4.remote = a->remote.ip4.as_u32;
218 key4.vni = clib_host_to_net_u32 (a->vni << 8);
221 p = hash_get_mem(gm->vxlan4_gpe_tunnel_by_key, &key4);
225 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
226 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
227 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
228 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
229 key6.vni = clib_host_to_net_u32 (a->vni << 8);
231 p = hash_get_mem(gm->vxlan6_gpe_tunnel_by_key, &key6);
236 /* adding a tunnel: tunnel must not already exist */
238 return VNET_API_ERROR_INVALID_VALUE;
240 if (a->decap_next_index >= VXLAN_GPE_INPUT_N_NEXT)
241 return VNET_API_ERROR_INVALID_DECAP_NEXT;
243 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
244 memset (t, 0, sizeof (*t));
246 /* copy from arg structure */
247 #define _(x) t->x = a->x;
248 foreach_gpe_copy_field;
249 if (!a->is_ip6) foreach_copy_ipv4
250 else foreach_copy_ipv6
253 if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
256 rv = vxlan4_gpe_rewrite (t);
258 rv = vxlan6_gpe_rewrite (t);
263 pool_put (gm->tunnels, t);
269 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
270 clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
271 hash_set_mem (gm->vxlan4_gpe_tunnel_by_key, key4_copy,
276 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
277 clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
278 hash_set_mem (gm->vxlan6_gpe_tunnel_by_key, key6_copy,
282 if (vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
284 hw_if_index = gm->free_vxlan_gpe_tunnel_hw_if_indices
285 [vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
286 _vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
288 hi = vnet_get_hw_interface (vnm, hw_if_index);
289 hi->dev_instance = t - gm->tunnels;
290 hi->hw_instance = hi->dev_instance;
294 hw_if_index = vnet_register_interface
295 (vnm, vxlan_gpe_device_class.index, t - gm->tunnels,
296 vxlan_gpe_hw_class.index, t - gm->tunnels);
297 hi = vnet_get_hw_interface (vnm, hw_if_index);
298 hi->output_node_index = vxlan_gpe_encap_node.index;
301 t->hw_if_index = hw_if_index;
302 t->sw_if_index = sw_if_index = hi->sw_if_index;
304 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
305 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
309 /* deleting a tunnel: tunnel must exist */
311 return VNET_API_ERROR_NO_SUCH_ENTRY;
313 t = pool_elt_at_index (gm->tunnels, p[0]);
315 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
316 vec_add1 (gm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
320 hp = hash_get_pair (gm->vxlan4_gpe_tunnel_by_key, &key4);
321 key4_copy = (void *)(hp->key);
322 hash_unset_mem (gm->vxlan4_gpe_tunnel_by_key, &key4);
323 clib_mem_free (key4_copy);
327 hp = hash_get_pair (gm->vxlan6_gpe_tunnel_by_key, &key6);
328 key6_copy = (void *)(hp->key);
329 hash_unset_mem (gm->vxlan4_gpe_tunnel_by_key, &key6);
330 clib_mem_free (key6_copy);
333 vec_free (t->rewrite);
334 pool_put (gm->tunnels, t);
338 *sw_if_indexp = sw_if_index;
343 static u32 fib4_index_from_fib_id (u32 fib_id)
345 ip4_main_t * im = &ip4_main;
348 p = hash_get (im->fib_index_by_table_id, fib_id);
355 static u32 fib6_index_from_fib_id (u32 fib_id)
357 ip6_main_t * im = &ip6_main;
360 p = hash_get (im->fib_index_by_table_id, fib_id);
367 static uword unformat_gpe_decap_next (unformat_input_t * input, va_list * args)
369 u32 * result = va_arg (*args, u32 *);
372 if (unformat (input, "drop"))
373 *result = VXLAN_GPE_INPUT_NEXT_DROP;
374 else if (unformat (input, "ip4"))
375 *result = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
376 else if (unformat (input, "ip6"))
377 *result = VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
378 else if (unformat (input, "ethernet"))
379 *result = VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
380 else if (unformat (input, "%d", &tmp))
387 static clib_error_t *
388 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
389 unformat_input_t * input,
390 vlib_cli_command_t * cmd)
392 unformat_input_t _line_input, * line_input = &_line_input;
394 ip46_address_t local, remote;
399 u32 encap_fib_index = 0;
400 u32 decap_fib_index = 0;
401 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
402 u32 decap_next_index = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
407 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
410 /* Get a line of input. */
411 if (! unformat_user (input, unformat_line_input, line_input))
414 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
415 if (unformat (line_input, "del"))
417 else if (unformat (line_input, "local %U",
418 unformat_ip4_address, &local.ip4))
423 else if (unformat (line_input, "remote %U",
424 unformat_ip4_address, &remote.ip4))
429 else if (unformat (line_input, "local %U",
430 unformat_ip6_address, &local.ip6))
435 else if (unformat (line_input, "remote %U",
436 unformat_ip6_address, &remote.ip6))
441 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
444 encap_fib_index = fib6_index_from_fib_id (tmp);
446 encap_fib_index = fib4_index_from_fib_id (tmp);
448 if (encap_fib_index == ~0)
449 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
451 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
455 decap_fib_index = fib6_index_from_fib_id (tmp);
457 decap_fib_index = fib4_index_from_fib_id (tmp);
459 if (decap_fib_index == ~0)
460 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
462 else if (unformat (line_input, "decap-next %U", unformat_gpe_decap_next,
465 else if (unformat (line_input, "vni %d", &vni))
467 else if (unformat(line_input, "next-ip4"))
468 protocol = VXLAN_GPE_PROTOCOL_IP4;
469 else if (unformat(line_input, "next-ip6"))
470 protocol = VXLAN_GPE_PROTOCOL_IP6;
471 else if (unformat(line_input, "next-ethernet"))
472 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
473 else if (unformat(line_input, "next-nsh"))
474 protocol = VXLAN_GPE_PROTOCOL_NSH;
476 return clib_error_return (0, "parse error: '%U'",
477 format_unformat_error, line_input);
480 unformat_free (line_input);
483 return clib_error_return (0, "tunnel local address not specified");
486 return clib_error_return (0, "tunnel remote address not specified");
488 if (ipv4_set && ipv6_set)
489 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
491 if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) ||
492 (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0))
493 return clib_error_return (0, "src and dst addresses are identical");
496 return clib_error_return (0, "vni not specified");
498 memset (a, 0, sizeof (*a));
501 a->is_ip6 = ipv6_set;
503 #define _(x) a->x = x;
504 foreach_gpe_copy_field;
505 if (ipv4_set) foreach_copy_ipv4
506 else foreach_copy_ipv6
509 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
514 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
516 case VNET_API_ERROR_INVALID_DECAP_NEXT:
517 return clib_error_return (0, "invalid decap-next...");
519 case VNET_API_ERROR_TUNNEL_EXIST:
520 return clib_error_return (0, "tunnel already exists...");
522 case VNET_API_ERROR_NO_SUCH_ENTRY:
523 return clib_error_return (0, "tunnel does not exist...");
526 return clib_error_return
527 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
533 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
534 .path = "create vxlan-gpe tunnel",
536 "create vxlan-gpe tunnel local <local-addr> remote <remote-addr>"
537 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
538 " [encap-vrf-id <nn>] [decap-vrf-id <nn>]"
540 .function = vxlan_gpe_add_del_tunnel_command_fn,
543 static clib_error_t *
544 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
545 unformat_input_t * input,
546 vlib_cli_command_t * cmd)
548 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
549 vxlan_gpe_tunnel_t * t;
551 if (pool_elts (gm->tunnels) == 0)
552 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
554 pool_foreach (t, gm->tunnels,
556 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
562 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
563 .path = "show vxlan-gpe",
564 .function = show_vxlan_gpe_tunnel_command_fn,
567 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
569 vxlan_gpe_main_t *gm = &vxlan_gpe_main;
571 gm->vnet_main = vnet_get_main();
574 gm->vxlan4_gpe_tunnel_by_key
575 = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword));
577 gm->vxlan6_gpe_tunnel_by_key
578 = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword));
581 udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
582 vxlan4_gpe_input_node.index, 1 /* is_ip4 */);
583 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6_gpe,
584 vxlan6_gpe_input_node.index, 0 /* is_ip4 */);
588 VLIB_INIT_FUNCTION(vxlan_gpe_init);