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");
33 case VXLAN_GPE_PROTOCOL_IP6:
34 s = format (s, "next-protocol ip6");
35 case VXLAN_GPE_PROTOCOL_ETHERNET:
36 s = format (s, "next-protocol ethernet");
37 case VXLAN_GPE_PROTOCOL_NSH:
38 s = format (s, "next-protocol nsh");
40 s = format (s, "next-protocol unknown %d", t->protocol);
43 s = format (s, " fibs: (encap %d, decap %d)",
47 s = format (s, " vxlan VNI %d ", t->vni);
52 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
54 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
55 u32 i = va_arg (*args, u32);
56 u32 show_dev_instance = ~0;
58 if (i < vec_len (gm->dev_inst_by_real))
59 show_dev_instance = gm->dev_inst_by_real[i];
61 if (show_dev_instance != ~0)
62 i = show_dev_instance;
64 return format (s, "vxlan_gpe_tunnel%d", i);
67 static int vxlan_gpe_name_renumber (vnet_hw_interface_t * hi,
70 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
72 vec_validate_init_empty (gm->dev_inst_by_real, hi->dev_instance, ~0);
74 gm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
79 static uword dummy_interface_tx (vlib_main_t * vm,
80 vlib_node_runtime_t * node,
83 clib_warning ("you shouldn't be here, leaking buffers...");
84 return frame->n_vectors;
87 static uword dummy_set_rewrite (vnet_main_t * vnm,
92 uword max_rewrite_bytes)
97 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
99 .format_device_name = format_vxlan_gpe_name,
100 .format_tx_trace = format_vxlan_gpe_encap_trace,
101 .tx_function = dummy_interface_tx,
102 .name_renumber = vxlan_gpe_name_renumber,
105 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
107 u32 dev_instance = va_arg (*args, u32);
108 s = format (s, "unimplemented dev %u", dev_instance);
112 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
114 .format_header = format_vxlan_gpe_header_with_length,
115 .set_rewrite = dummy_set_rewrite,
119 #define foreach_gpe_copy_field \
127 #define foreach_copy_field \
137 static int vxlan_gpe_rewrite (vxlan_gpe_tunnel_t * t)
141 ip4_vxlan_gpe_header_t * h0;
146 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
148 h0 = (ip4_vxlan_gpe_header_t *) rw;
150 /* Fixed portion of the (outer) ip4 header */
152 ip0->ip_version_and_header_length = 0x45;
154 ip0->protocol = IP_PROTOCOL_UDP;
156 /* we fix up the ip4 header length and checksum after-the-fact */
157 ip0->src_address.as_u32 = t->local.as_u32;
158 ip0->dst_address.as_u32 = t->remote.as_u32;
159 ip0->checksum = ip4_header_checksum (ip0);
161 /* UDP header, randomize src port on something, maybe? */
162 h0->udp.src_port = clib_host_to_net_u16 (4790);
163 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
165 /* VXLAN header. Are we having fun yet? */
166 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
167 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
168 h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
169 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
175 int vnet_vxlan_gpe_add_del_tunnel
176 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
178 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
179 vxlan_gpe_tunnel_t *t = 0;
180 vnet_main_t * vnm = gm->vnet_main;
181 vnet_hw_interface_t * hi;
183 u32 hw_if_index = ~0;
184 u32 sw_if_index = ~0;
186 vxlan_gpe_tunnel_key_t key, *key_copy;
189 key.local = a->local.as_u32;
190 key.remote = a->remote.as_u32;
191 key.vni = clib_host_to_net_u32 (a->vni << 8);
194 p = hash_get_mem (gm->vxlan_gpe_tunnel_by_key, &key);
198 /* adding a tunnel: tunnel must not already exist */
200 return VNET_API_ERROR_INVALID_VALUE;
202 if (a->decap_next_index >= VXLAN_GPE_INPUT_N_NEXT)
203 return VNET_API_ERROR_INVALID_DECAP_NEXT;
205 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
206 memset (t, 0, sizeof (*t));
208 /* copy from arg structure */
209 #define _(x) t->x = a->x;
210 foreach_gpe_copy_field;
213 rv = vxlan_gpe_rewrite (t);
217 pool_put (gm->tunnels, t);
221 key_copy = clib_mem_alloc (sizeof (*key_copy));
222 clib_memcpy (key_copy, &key, sizeof (*key_copy));
224 hash_set_mem (gm->vxlan_gpe_tunnel_by_key, key_copy,
227 if (vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
229 hw_if_index = gm->free_vxlan_gpe_tunnel_hw_if_indices
230 [vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
231 _vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
233 hi = vnet_get_hw_interface (vnm, hw_if_index);
234 hi->dev_instance = t - gm->tunnels;
235 hi->hw_instance = hi->dev_instance;
239 hw_if_index = vnet_register_interface
240 (vnm, vxlan_gpe_device_class.index, t - gm->tunnels,
241 vxlan_gpe_hw_class.index, t - gm->tunnels);
242 hi = vnet_get_hw_interface (vnm, hw_if_index);
243 hi->output_node_index = vxlan_gpe_encap_node.index;
246 t->hw_if_index = hw_if_index;
247 t->sw_if_index = sw_if_index = hi->sw_if_index;
249 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
250 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
254 /* deleting a tunnel: tunnel must exist */
256 return VNET_API_ERROR_NO_SUCH_ENTRY;
258 t = pool_elt_at_index (gm->tunnels, p[0]);
260 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
261 vec_add1 (gm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
263 hp = hash_get_pair (gm->vxlan_gpe_tunnel_by_key, &key);
264 key_copy = (void *)(hp->key);
265 hash_unset_mem (gm->vxlan_gpe_tunnel_by_key, &key);
266 clib_mem_free (key_copy);
268 vec_free (t->rewrite);
269 pool_put (gm->tunnels, t);
273 *sw_if_indexp = sw_if_index;
278 static u32 fib_index_from_fib_id (u32 fib_id)
280 ip4_main_t * im = &ip4_main;
283 p = hash_get (im->fib_index_by_table_id, fib_id);
290 static uword unformat_gpe_decap_next (unformat_input_t * input, va_list * args)
292 u32 * result = va_arg (*args, u32 *);
295 if (unformat (input, "drop"))
296 *result = VXLAN_GPE_INPUT_NEXT_DROP;
297 else if (unformat (input, "ip4"))
298 *result = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
299 else if (unformat (input, "ip6"))
300 *result = VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
301 else if (unformat (input, "ethernet"))
302 *result = VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
303 else if (unformat (input, "%d", &tmp))
310 static clib_error_t *
311 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
312 unformat_input_t * input,
313 vlib_cli_command_t * cmd)
315 unformat_input_t _line_input, * line_input = &_line_input;
317 ip4_address_t local, remote;
320 u32 encap_fib_index = 0;
321 u32 decap_fib_index = 0;
322 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
323 u32 decap_next_index = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
328 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
330 /* Get a line of input. */
331 if (! unformat_user (input, unformat_line_input, line_input))
334 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
335 if (unformat (line_input, "del"))
337 else if (unformat (line_input, "local %U",
338 unformat_ip4_address, &local))
340 else if (unformat (line_input, "remote %U",
341 unformat_ip4_address, &remote))
343 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
345 encap_fib_index = fib_index_from_fib_id (tmp);
346 if (encap_fib_index == ~0)
347 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
349 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
351 decap_fib_index = fib_index_from_fib_id (tmp);
352 if (decap_fib_index == ~0)
353 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
355 else if (unformat (line_input, "decap-next %U", unformat_gpe_decap_next,
358 else if (unformat (line_input, "vni %d", &vni))
360 else if (unformat(line_input, "next-ip4"))
361 protocol = VXLAN_GPE_PROTOCOL_IP4;
362 else if (unformat(line_input, "next-ip6"))
363 protocol = VXLAN_GPE_PROTOCOL_IP6;
364 else if (unformat(line_input, "next-ethernet"))
365 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
366 else if (unformat(line_input, "next-nsh"))
367 protocol = VXLAN_GPE_PROTOCOL_NSH;
369 return clib_error_return (0, "parse error: '%U'",
370 format_unformat_error, line_input);
373 unformat_free (line_input);
376 return clib_error_return (0, "tunnel local address not specified");
379 return clib_error_return (0, "tunnel remote address not specified");
382 return clib_error_return (0, "vni not specified");
384 memset (a, 0, sizeof (*a));
388 #define _(x) a->x = x;
389 foreach_gpe_copy_field;
392 rv = vnet_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
398 case VNET_API_ERROR_INVALID_DECAP_NEXT:
399 return clib_error_return (0, "invalid decap-next...");
401 case VNET_API_ERROR_TUNNEL_EXIST:
402 return clib_error_return (0, "tunnel already exists...");
404 case VNET_API_ERROR_NO_SUCH_ENTRY:
405 return clib_error_return (0, "tunnel does not exist...");
408 return clib_error_return
409 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
415 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
416 .path = "create vxlan-gpe tunnel",
418 "create vxlan-gpe tunnel local <ip4-addr> remote <ip4-addr>"
419 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
420 " [encap-vrf-id <nn>] [decap-vrf-id <nn>]"
422 .function = vxlan_gpe_add_del_tunnel_command_fn,
425 static clib_error_t *
426 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
427 unformat_input_t * input,
428 vlib_cli_command_t * cmd)
430 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
431 vxlan_gpe_tunnel_t * t;
433 if (pool_elts (gm->tunnels) == 0)
434 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
436 pool_foreach (t, gm->tunnels,
438 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
444 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
445 .path = "show vxlan-gpe",
446 .function = show_vxlan_gpe_tunnel_command_fn,
449 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
451 vxlan_gpe_main_t *gm = &vxlan_gpe_main;
453 gm->vnet_main = vnet_get_main();
456 gm->vxlan_gpe_tunnel_by_key
457 = hash_create_mem (0, sizeof(vxlan_gpe_tunnel_key_t), sizeof (uword));
459 udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
460 vxlan_gpe_input_node.index, 1 /* is_ip4 */);
464 VLIB_INIT_FUNCTION(vxlan_gpe_init);