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>
17 vxlan_gpe_main_t vxlan_gpe_main;
19 u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
21 vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
22 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
24 s = format (s, "[%d] local: %U remote: %U ",
26 format_ip4_address, &t->local,
27 format_ip4_address, &t->remote);
31 case VXLAN_GPE_PROTOCOL_IP4:
32 s = format (s, "next-protocol ip4");
34 case VXLAN_GPE_PROTOCOL_IP6:
35 s = format (s, "next-protocol ip6");
37 case VXLAN_GPE_PROTOCOL_ETHERNET:
38 s = format (s, "next-protocol ethernet");
40 case VXLAN_GPE_PROTOCOL_NSH:
41 s = format (s, "next-protocol nsh");
44 s = format (s, "next-protocol unknown %d", t->protocol);
47 s = format (s, " fibs: (encap %d, decap %d)",
51 s = format (s, " vxlan VNI %d ", t->vni);
56 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
58 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
59 u32 i = va_arg (*args, u32);
60 u32 show_dev_instance = ~0;
62 if (i < vec_len (gm->dev_inst_by_real))
63 show_dev_instance = gm->dev_inst_by_real[i];
65 if (show_dev_instance != ~0)
66 i = show_dev_instance;
68 return format (s, "vxlan_gpe_tunnel%d", i);
71 static int vxlan_gpe_name_renumber (vnet_hw_interface_t * hi,
74 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
76 vec_validate_init_empty (gm->dev_inst_by_real, hi->dev_instance, ~0);
78 gm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
83 static uword dummy_interface_tx (vlib_main_t * vm,
84 vlib_node_runtime_t * node,
87 clib_warning ("you shouldn't be here, leaking buffers...");
88 return frame->n_vectors;
91 static uword dummy_set_rewrite (vnet_main_t * vnm,
96 uword max_rewrite_bytes)
101 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
103 .format_device_name = format_vxlan_gpe_name,
104 .format_tx_trace = format_vxlan_gpe_encap_trace,
105 .tx_function = dummy_interface_tx,
106 .name_renumber = vxlan_gpe_name_renumber,
109 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
111 u32 dev_instance = va_arg (*args, u32);
112 s = format (s, "unimplemented dev %u", dev_instance);
116 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
118 .format_header = format_vxlan_gpe_header_with_length,
119 .set_rewrite = dummy_set_rewrite,
123 #define foreach_gpe_copy_field \
131 #define foreach_copy_field \
141 static int vxlan_gpe_rewrite (vxlan_gpe_tunnel_t * t)
145 ip4_vxlan_gpe_header_t * h0;
150 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
152 h0 = (ip4_vxlan_gpe_header_t *) rw;
154 /* Fixed portion of the (outer) ip4 header */
156 ip0->ip_version_and_header_length = 0x45;
158 ip0->protocol = IP_PROTOCOL_UDP;
160 /* we fix up the ip4 header length and checksum after-the-fact */
161 ip0->src_address.as_u32 = t->local.as_u32;
162 ip0->dst_address.as_u32 = t->remote.as_u32;
163 ip0->checksum = ip4_header_checksum (ip0);
165 /* UDP header, randomize src port on something, maybe? */
166 h0->udp.src_port = clib_host_to_net_u16 (4790);
167 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
169 /* VXLAN header. Are we having fun yet? */
170 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
171 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
172 h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
173 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
179 int vnet_vxlan_gpe_add_del_tunnel
180 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
182 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
183 vxlan_gpe_tunnel_t *t = 0;
184 vnet_main_t * vnm = gm->vnet_main;
185 vnet_hw_interface_t * hi;
187 u32 hw_if_index = ~0;
188 u32 sw_if_index = ~0;
190 vxlan_gpe_tunnel_key_t key, *key_copy;
193 key.local = a->local.as_u32;
194 key.remote = a->remote.as_u32;
195 key.vni = clib_host_to_net_u32 (a->vni << 8);
198 p = hash_get_mem (gm->vxlan_gpe_tunnel_by_key, &key);
202 /* adding a tunnel: tunnel must not already exist */
204 return VNET_API_ERROR_INVALID_VALUE;
206 if (a->decap_next_index >= VXLAN_GPE_INPUT_N_NEXT)
207 return VNET_API_ERROR_INVALID_DECAP_NEXT;
209 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
210 memset (t, 0, sizeof (*t));
212 /* copy from arg structure */
213 #define _(x) t->x = a->x;
214 foreach_gpe_copy_field;
217 rv = vxlan_gpe_rewrite (t);
221 pool_put (gm->tunnels, t);
225 key_copy = clib_mem_alloc (sizeof (*key_copy));
226 clib_memcpy (key_copy, &key, sizeof (*key_copy));
228 hash_set_mem (gm->vxlan_gpe_tunnel_by_key, key_copy,
231 if (vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
233 hw_if_index = gm->free_vxlan_gpe_tunnel_hw_if_indices
234 [vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
235 _vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
237 hi = vnet_get_hw_interface (vnm, hw_if_index);
238 hi->dev_instance = t - gm->tunnels;
239 hi->hw_instance = hi->dev_instance;
243 hw_if_index = vnet_register_interface
244 (vnm, vxlan_gpe_device_class.index, t - gm->tunnels,
245 vxlan_gpe_hw_class.index, t - gm->tunnels);
246 hi = vnet_get_hw_interface (vnm, hw_if_index);
247 hi->output_node_index = vxlan_gpe_encap_node.index;
250 t->hw_if_index = hw_if_index;
251 t->sw_if_index = sw_if_index = hi->sw_if_index;
253 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
254 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
258 /* deleting a tunnel: tunnel must exist */
260 return VNET_API_ERROR_NO_SUCH_ENTRY;
262 t = pool_elt_at_index (gm->tunnels, p[0]);
264 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
265 vec_add1 (gm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
267 hp = hash_get_pair (gm->vxlan_gpe_tunnel_by_key, &key);
268 key_copy = (void *)(hp->key);
269 hash_unset_mem (gm->vxlan_gpe_tunnel_by_key, &key);
270 clib_mem_free (key_copy);
272 vec_free (t->rewrite);
273 pool_put (gm->tunnels, t);
277 *sw_if_indexp = sw_if_index;
282 static u32 fib_index_from_fib_id (u32 fib_id)
284 ip4_main_t * im = &ip4_main;
287 p = hash_get (im->fib_index_by_table_id, fib_id);
294 static uword unformat_gpe_decap_next (unformat_input_t * input, va_list * args)
296 u32 * result = va_arg (*args, u32 *);
299 if (unformat (input, "drop"))
300 *result = VXLAN_GPE_INPUT_NEXT_DROP;
301 else if (unformat (input, "ip4"))
302 *result = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
303 else if (unformat (input, "ip6"))
304 *result = VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
305 else if (unformat (input, "ethernet"))
306 *result = VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
307 else if (unformat (input, "%d", &tmp))
314 static clib_error_t *
315 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
316 unformat_input_t * input,
317 vlib_cli_command_t * cmd)
319 unformat_input_t _line_input, * line_input = &_line_input;
321 ip4_address_t local, remote;
324 u32 encap_fib_index = 0;
325 u32 decap_fib_index = 0;
326 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
327 u32 decap_next_index = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
332 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
335 /* Get a line of input. */
336 if (! unformat_user (input, unformat_line_input, line_input))
339 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
340 if (unformat (line_input, "del"))
342 else if (unformat (line_input, "local %U",
343 unformat_ip4_address, &local))
345 else if (unformat (line_input, "remote %U",
346 unformat_ip4_address, &remote))
348 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
350 encap_fib_index = fib_index_from_fib_id (tmp);
351 if (encap_fib_index == ~0)
352 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
354 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
356 decap_fib_index = fib_index_from_fib_id (tmp);
357 if (decap_fib_index == ~0)
358 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
360 else if (unformat (line_input, "decap-next %U", unformat_gpe_decap_next,
363 else if (unformat (line_input, "vni %d", &vni))
365 else if (unformat(line_input, "next-ip4"))
366 protocol = VXLAN_GPE_PROTOCOL_IP4;
367 else if (unformat(line_input, "next-ip6"))
368 protocol = VXLAN_GPE_PROTOCOL_IP6;
369 else if (unformat(line_input, "next-ethernet"))
370 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
371 else if (unformat(line_input, "next-nsh"))
372 protocol = VXLAN_GPE_PROTOCOL_NSH;
374 return clib_error_return (0, "parse error: '%U'",
375 format_unformat_error, line_input);
378 unformat_free (line_input);
381 return clib_error_return (0, "tunnel local address not specified");
384 return clib_error_return (0, "tunnel remote address not specified");
387 return clib_error_return (0, "vni not specified");
389 memset (a, 0, sizeof (*a));
393 #define _(x) a->x = x;
394 foreach_gpe_copy_field;
397 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
402 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
404 case VNET_API_ERROR_INVALID_DECAP_NEXT:
405 return clib_error_return (0, "invalid decap-next...");
407 case VNET_API_ERROR_TUNNEL_EXIST:
408 return clib_error_return (0, "tunnel already exists...");
410 case VNET_API_ERROR_NO_SUCH_ENTRY:
411 return clib_error_return (0, "tunnel does not exist...");
414 return clib_error_return
415 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
421 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
422 .path = "create vxlan-gpe tunnel",
424 "create vxlan-gpe tunnel local <ip4-addr> remote <ip4-addr>"
425 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
426 " [encap-vrf-id <nn>] [decap-vrf-id <nn>]"
428 .function = vxlan_gpe_add_del_tunnel_command_fn,
431 static clib_error_t *
432 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
433 unformat_input_t * input,
434 vlib_cli_command_t * cmd)
436 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
437 vxlan_gpe_tunnel_t * t;
439 if (pool_elts (gm->tunnels) == 0)
440 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
442 pool_foreach (t, gm->tunnels,
444 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
450 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
451 .path = "show vxlan-gpe",
452 .function = show_vxlan_gpe_tunnel_command_fn,
455 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
457 vxlan_gpe_main_t *gm = &vxlan_gpe_main;
459 gm->vnet_main = vnet_get_main();
462 gm->vxlan_gpe_tunnel_by_key
463 = hash_create_mem (0, sizeof(vxlan_gpe_tunnel_key_t), sizeof (uword));
465 udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
466 vxlan_gpe_input_node.index, 1 /* is_ip4 */);
470 VLIB_INIT_FUNCTION(vxlan_gpe_init);