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_ETHERNET_INPUT:
32 return format (s, "ethernet");
33 case NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP:
34 return format (s, "nsh-vxlan-gpe");
36 return format (s, "unknown %d", next_index);
41 u8 * format_nsh_vxlan_gpe_tunnel (u8 * s, va_list * args)
43 nsh_vxlan_gpe_tunnel_t * t = va_arg (*args, nsh_vxlan_gpe_tunnel_t *);
44 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
47 "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
49 format_ip4_address, &t->src,
50 format_ip4_address, &t->dst,
53 s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
54 s = format (s, " vxlan VNI %d ", t->vni);
55 s = format (s, "nsh ver %d ", (t->nsh_hdr.ver_o_c>>6));
56 if (t->nsh_hdr.ver_o_c & NSH_O_BIT)
57 s = format (s, "O-set ");
59 if (t->nsh_hdr.ver_o_c & NSH_C_BIT)
60 s = format (s, "C-set ");
62 s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
63 t->nsh_hdr.length, t->nsh_hdr.length * 4, t->nsh_hdr.md_type, t->nsh_hdr.next_protocol);
65 s = format (s, " service path %d service index %d\n",
66 (t->nsh_hdr.spi_si>>NSH_SPI_SHIFT) & NSH_SPI_MASK,
67 t->nsh_hdr.spi_si & NSH_SINDEX_MASK);
69 s = format (s, " c1 %d c2 %d c3 %d c4 %d\n",
70 t->nsh_hdr.c1, t->nsh_hdr.c2, t->nsh_hdr.c3, t->nsh_hdr.c4);
75 static u8 * format_nsh_vxlan_gpe_name (u8 * s, va_list * args)
77 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
78 u32 i = va_arg (*args, u32);
79 u32 show_dev_instance = ~0;
81 if (i < vec_len (ngm->dev_inst_by_real))
82 show_dev_instance = ngm->dev_inst_by_real[i];
84 if (show_dev_instance != ~0)
85 i = show_dev_instance;
87 return format (s, "nsh_vxlan_gpe_tunnel%d", i);
90 static int nsh_vxlan_gpe_name_renumber (vnet_hw_interface_t * hi,
93 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
95 vec_validate_init_empty (ngm->dev_inst_by_real, hi->dev_instance, ~0);
97 ngm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
102 static uword dummy_interface_tx (vlib_main_t * vm,
103 vlib_node_runtime_t * node,
104 vlib_frame_t * frame)
106 clib_warning ("you shouldn't be here, leaking buffers...");
107 return frame->n_vectors;
110 VNET_DEVICE_CLASS (nsh_vxlan_gpe_device_class,static) = {
111 .name = "NSH_VXLAN_GPE",
112 .format_device_name = format_nsh_vxlan_gpe_name,
113 .format_tx_trace = format_nsh_vxlan_gpe_encap_trace,
114 .tx_function = dummy_interface_tx,
115 .name_renumber = nsh_vxlan_gpe_name_renumber,
118 static uword dummy_set_rewrite (vnet_main_t * vnm,
123 uword max_rewrite_bytes)
128 static u8 * format_nsh_vxlan_gpe_header_with_length (u8 * s, va_list * args)
130 u32 dev_instance = va_arg (*args, u32);
131 s = format (s, "unimplemented dev %u", dev_instance);
135 VNET_HW_INTERFACE_CLASS (nsh_vxlan_gpe_hw_class) = {
136 .name = "NSH_VXLAN_GPE",
137 .format_header = format_nsh_vxlan_gpe_header_with_length,
138 .set_rewrite = dummy_set_rewrite,
141 #define foreach_copy_field \
150 #define foreach_copy_nshhdr_field \
162 #define foreach_32bit_field \
169 static int nsh_vxlan_gpe_rewrite (nsh_vxlan_gpe_tunnel_t * t)
174 ip4_vxlan_gpe_and_nsh_header_t * h0;
177 len = sizeof (*h0) + vec_len(t->nsh_hdr.tlvs)*4;
179 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
181 h0 = (ip4_vxlan_gpe_and_nsh_header_t *) rw;
183 /* Fixed portion of the (outer) ip4 header */
185 ip0->ip_version_and_header_length = 0x45;
187 ip0->protocol = IP_PROTOCOL_UDP;
189 /* we fix up the ip4 header length and checksum after-the-fact */
190 ip0->src_address.as_u32 = t->src.as_u32;
191 ip0->dst_address.as_u32 = t->dst.as_u32;
192 ip0->checksum = ip4_header_checksum (ip0);
194 /* UDP header, randomize src port on something, maybe? */
195 h0->udp.src_port = clib_host_to_net_u16 (4790);
196 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
198 /* VXLAN header. Are we having fun yet? */
199 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
200 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
201 h0->vxlan.next_protocol = VXLAN_NEXT_PROTOCOL_NSH;
202 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
206 nsh0->ver_o_c = t->nsh_hdr.ver_o_c;
207 nsh0->md_type = t->nsh_hdr.md_type;
208 nsh0->next_protocol = t->nsh_hdr.next_protocol;
209 nsh0->spi_si = t->nsh_hdr.spi_si;
210 nsh0->c1 = t->nsh_hdr.c1;
211 nsh0->c2 = t->nsh_hdr.c2;
212 nsh0->c3 = t->nsh_hdr.c3;
213 nsh0->c4 = t->nsh_hdr.c4;
215 /* Endian swap 32-bit fields */
216 #define _(x) nsh0->x = clib_host_to_net_u32(nsh0->x);
220 /* fix nsh header length */
221 t->nsh_hdr.length = 6 + vec_len(t->nsh_hdr.tlvs);
222 nsh0->length = t->nsh_hdr.length;
225 if (vec_len(t->nsh_hdr.tlvs))
226 clib_memcpy (nsh0->tlvs, t->nsh_hdr.tlvs, 4*vec_len(t->nsh_hdr.tlvs));
232 int vnet_nsh_vxlan_gpe_add_del_tunnel
233 (vnet_nsh_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
235 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
236 nsh_vxlan_gpe_tunnel_t *t = 0;
237 vnet_main_t * vnm = ngm->vnet_main;
238 vnet_hw_interface_t * hi;
240 u32 hw_if_index = ~0;
241 u32 sw_if_index = ~0;
243 nsh_vxlan_gpe_tunnel_key_t key, *key_copy;
246 key.src = a->dst.as_u32; /* decap src in key is encap dst in config */
247 key.vni = clib_host_to_net_u32 (a->vni << 8);
248 key.spi_si = clib_host_to_net_u32(a->nsh_hdr.spi_si);
251 p = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
255 /* adding a tunnel: tunnel must not already exist */
257 return VNET_API_ERROR_INVALID_VALUE;
259 if (a->decap_next_index >= NSH_VXLAN_GPE_INPUT_N_NEXT)
260 return VNET_API_ERROR_INVALID_DECAP_NEXT;
262 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
263 memset (t, 0, sizeof (*t));
265 /* copy from arg structure */
266 #define _(x) t->x = a->x;
270 /* copy from arg structure */
271 #define _(x) t->nsh_hdr.x = a->nsh_hdr.x;
272 foreach_copy_nshhdr_field;
275 rv = nsh_vxlan_gpe_rewrite (t);
279 pool_put (ngm->tunnels, t);
283 key_copy = clib_mem_alloc (sizeof (*key_copy));
284 clib_memcpy (key_copy, &key, sizeof (*key_copy));
286 hash_set_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, key_copy,
289 if (vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) > 0)
291 hw_if_index = ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices
292 [vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices)-1];
293 _vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) -= 1;
295 hi = vnet_get_hw_interface (vnm, hw_if_index);
296 hi->dev_instance = t - ngm->tunnels;
297 hi->hw_instance = hi->dev_instance;
301 hw_if_index = vnet_register_interface
302 (vnm, nsh_vxlan_gpe_device_class.index, t - ngm->tunnels,
303 nsh_vxlan_gpe_hw_class.index, t - ngm->tunnels);
304 hi = vnet_get_hw_interface (vnm, hw_if_index);
305 hi->output_node_index = nsh_vxlan_gpe_encap_node.index;
308 t->hw_if_index = hw_if_index;
309 t->sw_if_index = sw_if_index = hi->sw_if_index;
311 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
312 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
316 /* deleting a tunnel: tunnel must exist */
318 return VNET_API_ERROR_NO_SUCH_ENTRY;
320 t = pool_elt_at_index (ngm->tunnels, p[0]);
322 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
323 vec_add1 (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
325 hp = hash_get_pair (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
326 key_copy = (void *)(hp->key);
327 hash_unset_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
328 clib_mem_free (key_copy);
330 vec_free (t->rewrite);
331 pool_put (ngm->tunnels, t);
335 *sw_if_indexp = sw_if_index;
340 static u32 fib_index_from_fib_id (u32 fib_id)
342 ip4_main_t * im = &ip4_main;
345 p = hash_get (im->fib_index_by_table_id, fib_id);
352 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
354 u32 * result = va_arg (*args, u32 *);
357 if (unformat (input, "drop"))
358 *result = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
359 else if (unformat (input, "ip4"))
360 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
361 else if (unformat (input, "ip6"))
362 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
363 else if (unformat (input, "ethernet"))
364 *result = NSH_VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
365 else if (unformat (input, "nsh-vxlan-gpe"))
366 *result = NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP;
367 else if (unformat (input, "%d", &tmp))
374 static clib_error_t *
375 nsh_vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
376 unformat_input_t * input,
377 vlib_cli_command_t * cmd)
379 unformat_input_t _line_input, * line_input = &_line_input;
380 ip4_address_t src, dst;
384 u32 encap_fib_index = 0;
385 u32 decap_fib_index = 0;
389 u8 next_protocol = 1; /* default: ip4 */
390 u32 decap_next_index = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
405 vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
407 /* Get a line of input. */
408 if (! unformat_user (input, unformat_line_input, line_input))
411 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
412 if (unformat (line_input, "del"))
414 else if (unformat (line_input, "src %U",
415 unformat_ip4_address, &src))
417 else if (unformat (line_input, "dst %U",
418 unformat_ip4_address, &dst))
420 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
422 encap_fib_index = fib_index_from_fib_id (tmp);
423 if (encap_fib_index == ~0)
424 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
426 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
428 decap_fib_index = fib_index_from_fib_id (tmp);
429 if (decap_fib_index == ~0)
430 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
432 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
435 else if (unformat (line_input, "vni %d", &vni))
437 else if (unformat (line_input, "version %d", &tmp))
438 ver_o_c |= (tmp & 3) << 6;
439 else if (unformat (line_input, "o-bit %d", &tmp))
440 ver_o_c |= (tmp & 1) << 5;
441 else if (unformat (line_input, "c-bit %d", &tmp))
442 ver_o_c |= (tmp & 1) << 4;
443 else if (unformat (line_input, "md-type %d", &tmp))
445 else if (unformat(line_input, "next-ip4"))
447 else if (unformat(line_input, "next-ip6"))
449 else if (unformat(line_input, "next-ethernet"))
451 else if (unformat(line_input, "next-nsh"))
453 else if (unformat (line_input, "c1 %d", &c1))
455 else if (unformat (line_input, "c2 %d", &c2))
457 else if (unformat (line_input, "c3 %d", &c3))
459 else if (unformat (line_input, "c4 %d", &c4))
461 else if (unformat (line_input, "spi %d", &spi))
463 else if (unformat (line_input, "si %d", &si))
465 else if (unformat (line_input, "tlv %x"))
466 vec_add1 (tlvs, tmp);
468 return clib_error_return (0, "parse error: '%U'",
469 format_unformat_error, line_input);
472 unformat_free (line_input);
475 return clib_error_return (0, "tunnel src address not specified");
478 return clib_error_return (0, "tunnel dst address not specified");
481 return clib_error_return (0, "vni not specified");
484 return clib_error_return (0, "spi not specified");
487 return clib_error_return (0, "si not specified");
489 spi_si = (spi<<8) | si;
491 memset (a, 0, sizeof (*a));
495 #define _(x) a->x = x;
499 #define _(x) a->nsh_hdr.x = x;
500 foreach_copy_nshhdr_field;
503 rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
509 case VNET_API_ERROR_INVALID_DECAP_NEXT:
510 return clib_error_return (0, "invalid decap-next...");
512 case VNET_API_ERROR_TUNNEL_EXIST:
513 return clib_error_return (0, "tunnel already exists...");
515 case VNET_API_ERROR_NO_SUCH_ENTRY:
516 return clib_error_return (0, "tunnel does not exist...");
519 return clib_error_return
520 (0, "vnet_nsh_vxlan_gpe_add_del_tunnel returned %d", rv);
526 VLIB_CLI_COMMAND (create_nsh_vxlan_gpe_tunnel_command, static) = {
527 .path = "nsh vxlan tunnel",
529 "nsh vxlan tunnel src <ip4-addr> dst <ip4-addr>"
530 " c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn> vni <nn>\n"
531 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
532 " [md-type <nn>][next-ip4][next-ip6][next-ethernet][next-nsh]\n"
533 " [tlv <xx>][decap-next [ip4|ip6|ethernet|nsh-vxlan-gpe]][del]\n",
534 .function = nsh_vxlan_gpe_add_del_tunnel_command_fn,
537 static clib_error_t *
538 show_nsh_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
539 unformat_input_t * input,
540 vlib_cli_command_t * cmd)
542 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
543 nsh_vxlan_gpe_tunnel_t * t;
545 if (pool_elts (ngm->tunnels) == 0)
546 vlib_cli_output (vm, "No nsh-vxlan-gpe tunnels configured...");
548 pool_foreach (t, ngm->tunnels,
550 vlib_cli_output (vm, "%U", format_nsh_vxlan_gpe_tunnel, t);
556 VLIB_CLI_COMMAND (show_nsh_vxlan_gpe_tunnel_command, static) = {
557 .path = "show nsh vxlan tunnel",
558 .function = show_nsh_vxlan_gpe_tunnel_command_fn,
561 clib_error_t *nsh_vxlan_gpe_init (vlib_main_t *vm)
563 nsh_vxlan_gpe_main_t *ngm = &nsh_vxlan_gpe_main;
565 ngm->vnet_main = vnet_get_main();
568 ngm->nsh_vxlan_gpe_tunnel_by_key
569 = hash_create_mem (0, sizeof(nsh_vxlan_gpe_tunnel_key_t), sizeof (uword));
571 udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
572 nsh_vxlan_gpe_input_node.index, 1 /* is_ip4 */);
576 VLIB_INIT_FUNCTION(nsh_vxlan_gpe_init);