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/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
17 nsh_vxlan_gpe_main_t nsh_vxlan_gpe_main;
19 static u8 * format_decap_next (u8 * s, va_list * args)
21 u32 next_index = va_arg (*args, u32);
25 case NSH_VXLAN_GPE_INPUT_NEXT_DROP:
26 return format (s, "drop");
27 case NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT:
28 return format (s, "ip4");
29 case NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT:
30 return format (s, "ip6");
31 case NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP:
32 return format (s, "nsh-vxlan-gpe");
34 return format (s, "unknown %d", next_index);
39 u8 * format_nsh_vxlan_gpe_tunnel (u8 * s, va_list * args)
41 nsh_vxlan_gpe_tunnel_t * t = va_arg (*args, nsh_vxlan_gpe_tunnel_t *);
42 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
45 "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
47 format_ip4_address, &t->src,
48 format_ip4_address, &t->dst,
51 s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
52 s = format (s, " vxlan VNI %d ", t->vni);
53 s = format (s, "nsh ver %d ", (t->ver_o_c>>6));
54 if (t->ver_o_c & NSH_GRE_O_BIT)
55 s = format (s, "O-set ");
57 if (t->ver_o_c & NSH_GRE_C_BIT)
58 s = format (s, "C-set ");
60 s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
61 t->length, t->length * 4, t->md_type, t->next_protocol);
63 s = format (s, " service path %d service index %d\n",
64 (t->spi_si>>NSH_GRE_SPI_SHIFT) & NSH_GRE_SPI_MASK,
65 t->spi_si & NSH_GRE_SINDEX_MASK);
67 s = format (s, " c1 %d c2 %d c3 %d c4 %d\n",
68 t->c1, t->c2, t->c3, t->c4);
73 static u8 * format_nsh_vxlan_gpe_name (u8 * s, va_list * args)
75 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
76 u32 i = va_arg (*args, u32);
77 u32 show_dev_instance = ~0;
79 if (i < vec_len (ngm->dev_inst_by_real))
80 show_dev_instance = ngm->dev_inst_by_real[i];
82 if (show_dev_instance != ~0)
83 i = show_dev_instance;
85 return format (s, "nsh_vxlan_gpe_tunnel%d", i);
88 static int nsh_vxlan_gpe_name_renumber (vnet_hw_interface_t * hi,
91 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
93 vec_validate_init_empty (ngm->dev_inst_by_real, hi->dev_instance, ~0);
95 ngm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
100 static uword dummy_interface_tx (vlib_main_t * vm,
101 vlib_node_runtime_t * node,
102 vlib_frame_t * frame)
104 clib_warning ("you shouldn't be here, leaking buffers...");
105 return frame->n_vectors;
108 VNET_DEVICE_CLASS (nsh_vxlan_gpe_device_class,static) = {
109 .name = "NSH_VXLAN_GPE",
110 .format_device_name = format_nsh_vxlan_gpe_name,
111 .format_tx_trace = format_nsh_vxlan_gpe_encap_trace,
112 .tx_function = dummy_interface_tx,
113 .name_renumber = nsh_vxlan_gpe_name_renumber,
116 static uword dummy_set_rewrite (vnet_main_t * vnm,
121 uword max_rewrite_bytes)
126 static u8 * format_nsh_vxlan_gpe_header_with_length (u8 * s, va_list * args)
128 u32 dev_instance = va_arg (*args, u32);
129 s = format (s, "unimplemented dev %u", dev_instance);
133 VNET_HW_INTERFACE_CLASS (nsh_vxlan_gpe_hw_class) = {
134 .name = "NSH_VXLAN_GPE",
135 .format_header = format_nsh_vxlan_gpe_header_with_length,
136 .set_rewrite = dummy_set_rewrite,
139 #define foreach_copy_field \
145 _(decap_next_index) \
157 #define foreach_32bit_field \
164 static int nsh_vxlan_gpe_rewrite (nsh_vxlan_gpe_tunnel_t * t)
169 ip4_vxlan_gpe_and_nsh_header_t * h0;
172 len = sizeof (*h0) + vec_len(t->tlvs)*4;
174 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
176 h0 = (ip4_vxlan_gpe_and_nsh_header_t *) rw;
178 /* Fixed portion of the (outer) ip4 header */
180 ip0->ip_version_and_header_length = 0x45;
182 ip0->protocol = IP_PROTOCOL_UDP;
184 /* we fix up the ip4 header length and checksum after-the-fact */
185 ip0->src_address.as_u32 = t->src.as_u32;
186 ip0->dst_address.as_u32 = t->dst.as_u32;
187 ip0->checksum = ip4_header_checksum (ip0);
189 /* UDP header, randomize src port on something, maybe? */
190 h0->udp.src_port = clib_host_to_net_u16 (4790);
191 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
193 /* VXLAN header. Are we having fun yet? */
194 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
195 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
196 h0->vxlan.next_protocol = VXLAN_NEXT_PROTOCOL_NSH;
197 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
201 nsh0->ver_o_c = t->ver_o_c;
202 nsh0->md_type = t->md_type;
203 nsh0->next_protocol = t->next_protocol;
204 nsh0->spi_si = t->spi_si;
210 /* Endian swap 32-bit fields */
211 #define _(x) nsh0->x = clib_host_to_net_u32(nsh0->x);
215 /* fix nsh header length */
216 t->length = 6 + vec_len(t->tlvs);
217 nsh0->length = t->length;
220 if (vec_len(t->tlvs))
221 memcpy (nsh0->tlvs, t->tlvs, 4*vec_len(t->tlvs));
227 int vnet_nsh_vxlan_gpe_add_del_tunnel
228 (vnet_nsh_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
230 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
231 nsh_vxlan_gpe_tunnel_t *t = 0;
232 vnet_main_t * vnm = ngm->vnet_main;
233 vnet_hw_interface_t * hi;
235 u32 hw_if_index = ~0;
236 u32 sw_if_index = ~0;
238 nsh_vxlan_gpe_tunnel_key_t key, *key_copy;
241 key.src = a->dst.as_u32; /* decap src in key is encap dst in config */
242 key.vni = clib_host_to_net_u32 (a->vni << 8);
243 key.spi_si = clib_host_to_net_u32(a->spi_si);
245 p = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
249 /* adding a tunnel: tunnel must not already exist */
251 return VNET_API_ERROR_INVALID_VALUE;
253 if (a->decap_next_index >= NSH_VXLAN_GPE_INPUT_N_NEXT)
254 return VNET_API_ERROR_INVALID_DECAP_NEXT;
256 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
257 memset (t, 0, sizeof (*t));
259 /* copy from arg structure */
260 #define _(x) t->x = a->x;
264 rv = nsh_vxlan_gpe_rewrite (t);
268 pool_put (ngm->tunnels, t);
272 key_copy = clib_mem_alloc (sizeof (*key_copy));
273 memcpy (key_copy, &key, sizeof (*key_copy));
275 hash_set_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, key_copy,
278 if (vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) > 0)
280 hw_if_index = ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices
281 [vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices)-1];
282 _vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) -= 1;
284 hi = vnet_get_hw_interface (vnm, hw_if_index);
285 hi->dev_instance = t - ngm->tunnels;
286 hi->hw_instance = hi->dev_instance;
290 hw_if_index = vnet_register_interface
291 (vnm, nsh_vxlan_gpe_device_class.index, t - ngm->tunnels,
292 nsh_vxlan_gpe_hw_class.index, t - ngm->tunnels);
293 hi = vnet_get_hw_interface (vnm, hw_if_index);
294 hi->output_node_index = nsh_vxlan_gpe_encap_node.index;
297 t->hw_if_index = hw_if_index;
298 t->sw_if_index = sw_if_index = hi->sw_if_index;
300 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
301 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
305 /* deleting a tunnel: tunnel must exist */
307 return VNET_API_ERROR_NO_SUCH_ENTRY;
309 t = pool_elt_at_index (ngm->tunnels, p[0]);
311 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
312 vec_add1 (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
314 hp = hash_get_pair (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
315 key_copy = (void *)(hp->key);
316 hash_unset_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
317 clib_mem_free (key_copy);
319 vec_free (t->rewrite);
320 pool_put (ngm->tunnels, t);
324 *sw_if_indexp = sw_if_index;
329 static u32 fib_index_from_fib_id (u32 fib_id)
331 ip4_main_t * im = &ip4_main;
334 p = hash_get (im->fib_index_by_table_id, fib_id);
341 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
343 u32 * result = va_arg (*args, u32 *);
346 if (unformat (input, "drop"))
347 *result = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
348 else if (unformat (input, "ip4"))
349 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
350 else if (unformat (input, "ip6"))
351 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
352 else if (unformat (input, "ethernet"))
353 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
354 else if (unformat (input, "nsh-vxlan-gpe"))
355 *result = NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP;
356 else if (unformat (input, "%d", &tmp))
363 static clib_error_t *
364 nsh_vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
365 unformat_input_t * input,
366 vlib_cli_command_t * cmd)
368 unformat_input_t _line_input, * line_input = &_line_input;
369 ip4_address_t src, dst;
373 u32 encap_fib_index = 0;
374 u32 decap_fib_index = 0;
378 u8 next_protocol = 1; /* default: ip4 */
379 u32 decap_next_index = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
394 vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
396 /* Get a line of input. */
397 if (! unformat_user (input, unformat_line_input, line_input))
400 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
401 if (unformat (line_input, "del"))
403 else if (unformat (line_input, "src %U",
404 unformat_ip4_address, &src))
406 else if (unformat (line_input, "dst %U",
407 unformat_ip4_address, &dst))
409 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
411 encap_fib_index = fib_index_from_fib_id (tmp);
412 if (encap_fib_index == ~0)
413 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
415 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
417 decap_fib_index = fib_index_from_fib_id (tmp);
418 if (decap_fib_index == ~0)
419 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
421 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
424 else if (unformat (line_input, "vni %d", &vni))
426 else if (unformat (line_input, "version %d", &tmp))
427 ver_o_c |= (tmp & 3) << 6;
428 else if (unformat (line_input, "o-bit %d", &tmp))
429 ver_o_c |= (tmp & 1) << 5;
430 else if (unformat (line_input, "c-bit %d", &tmp))
431 ver_o_c |= (tmp & 1) << 4;
432 else if (unformat (line_input, "md-type %d", &tmp))
434 else if (unformat(line_input, "next-ip4"))
436 else if (unformat(line_input, "next-ip6"))
438 else if (unformat(line_input, "next-ethernet"))
440 else if (unformat(line_input, "next-nsh"))
442 else if (unformat (line_input, "c1 %d", &c1))
444 else if (unformat (line_input, "c2 %d", &c2))
446 else if (unformat (line_input, "c3 %d", &c3))
448 else if (unformat (line_input, "c4 %d", &c4))
450 else if (unformat (line_input, "spi %d", &spi))
452 else if (unformat (line_input, "si %d", &si))
454 else if (unformat (line_input, "tlv %x"))
455 vec_add1 (tlvs, tmp);
457 return clib_error_return (0, "parse error: '%U'",
458 format_unformat_error, line_input);
461 unformat_free (line_input);
464 return clib_error_return (0, "tunnel src address not specified");
467 return clib_error_return (0, "tunnel dst address not specified");
470 return clib_error_return (0, "vni not specified");
473 return clib_error_return (0, "spi not specified");
476 return clib_error_return (0, "si not specified");
478 spi_si = (spi<<8) | si;
480 memset (a, 0, sizeof (*a));
484 #define _(x) a->x = x;
488 rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
494 case VNET_API_ERROR_INVALID_DECAP_NEXT:
495 return clib_error_return (0, "invalid decap-next...");
497 case VNET_API_ERROR_TUNNEL_EXIST:
498 return clib_error_return (0, "tunnel already exists...");
500 case VNET_API_ERROR_NO_SUCH_ENTRY:
501 return clib_error_return (0, "tunnel does not exist...");
504 return clib_error_return
505 (0, "vnet_nsh_vxlan_gpe_add_del_tunnel returned %d", rv);
511 VLIB_CLI_COMMAND (create_nsh_vxlan_gpe_tunnel_command, static) = {
512 .path = "nsh vxlan tunnel",
514 "nsh vxlan tunnel src <ip4-addr> dst <ip4-addr>"
515 " c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn> vni <nn>\n"
516 " [encap-fib-id <nn>] [decap-fib-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
517 " [md-type <nn>][next-ip4][next-ip6][next-ethernet][next-nsh]\n"
518 " [tlv <xx>][decap-next [ip4|ip6|ethernet|nsh-encap]][del]\n",
519 .function = nsh_vxlan_gpe_add_del_tunnel_command_fn,
522 static clib_error_t *
523 show_nsh_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
524 unformat_input_t * input,
525 vlib_cli_command_t * cmd)
527 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
528 nsh_vxlan_gpe_tunnel_t * t;
530 if (pool_elts (ngm->tunnels) == 0)
531 vlib_cli_output (vm, "No nsh-vxlan-gpe tunnels configured...");
533 pool_foreach (t, ngm->tunnels,
535 vlib_cli_output (vm, "%U", format_nsh_vxlan_gpe_tunnel, t);
541 VLIB_CLI_COMMAND (show_nsh_vxlan_gpe_tunnel_command, static) = {
542 .path = "show nsh vxlan tunnel",
543 .function = show_nsh_vxlan_gpe_tunnel_command_fn,
546 clib_error_t *nsh_vxlan_gpe_init (vlib_main_t *vm)
548 nsh_vxlan_gpe_main_t *ngm = &nsh_vxlan_gpe_main;
550 ngm->vnet_main = vnet_get_main();
553 ngm->nsh_vxlan_gpe_tunnel_by_key
554 = hash_create_mem (0, sizeof(nsh_vxlan_gpe_tunnel_key_t), sizeof (uword));
556 udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
557 nsh_vxlan_gpe_input_node.index, 1 /* is_ip4 */);
561 VLIB_INIT_FUNCTION(nsh_vxlan_gpe_init);