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 clib_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);
246 p = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
250 /* adding a tunnel: tunnel must not already exist */
252 return VNET_API_ERROR_INVALID_VALUE;
254 if (a->decap_next_index >= NSH_VXLAN_GPE_INPUT_N_NEXT)
255 return VNET_API_ERROR_INVALID_DECAP_NEXT;
257 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
258 memset (t, 0, sizeof (*t));
260 /* copy from arg structure */
261 #define _(x) t->x = a->x;
265 rv = nsh_vxlan_gpe_rewrite (t);
269 pool_put (ngm->tunnels, t);
273 key_copy = clib_mem_alloc (sizeof (*key_copy));
274 clib_memcpy (key_copy, &key, sizeof (*key_copy));
276 hash_set_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, key_copy,
279 if (vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) > 0)
281 hw_if_index = ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices
282 [vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices)-1];
283 _vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) -= 1;
285 hi = vnet_get_hw_interface (vnm, hw_if_index);
286 hi->dev_instance = t - ngm->tunnels;
287 hi->hw_instance = hi->dev_instance;
291 hw_if_index = vnet_register_interface
292 (vnm, nsh_vxlan_gpe_device_class.index, t - ngm->tunnels,
293 nsh_vxlan_gpe_hw_class.index, t - ngm->tunnels);
294 hi = vnet_get_hw_interface (vnm, hw_if_index);
295 hi->output_node_index = nsh_vxlan_gpe_encap_node.index;
298 t->hw_if_index = hw_if_index;
299 t->sw_if_index = sw_if_index = hi->sw_if_index;
301 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
302 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
306 /* deleting a tunnel: tunnel must exist */
308 return VNET_API_ERROR_NO_SUCH_ENTRY;
310 t = pool_elt_at_index (ngm->tunnels, p[0]);
312 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
313 vec_add1 (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
315 hp = hash_get_pair (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
316 key_copy = (void *)(hp->key);
317 hash_unset_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
318 clib_mem_free (key_copy);
320 vec_free (t->rewrite);
321 pool_put (ngm->tunnels, t);
325 *sw_if_indexp = sw_if_index;
330 static u32 fib_index_from_fib_id (u32 fib_id)
332 ip4_main_t * im = &ip4_main;
335 p = hash_get (im->fib_index_by_table_id, fib_id);
342 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
344 u32 * result = va_arg (*args, u32 *);
347 if (unformat (input, "drop"))
348 *result = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
349 else if (unformat (input, "ip4"))
350 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
351 else if (unformat (input, "ip6"))
352 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
353 else if (unformat (input, "ethernet"))
354 *result = NSH_VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
355 else if (unformat (input, "nsh-vxlan-gpe"))
356 *result = NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP;
357 else if (unformat (input, "%d", &tmp))
364 static clib_error_t *
365 nsh_vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
366 unformat_input_t * input,
367 vlib_cli_command_t * cmd)
369 unformat_input_t _line_input, * line_input = &_line_input;
370 ip4_address_t src, dst;
374 u32 encap_fib_index = 0;
375 u32 decap_fib_index = 0;
379 u8 next_protocol = 1; /* default: ip4 */
380 u32 decap_next_index = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
395 vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
397 /* Get a line of input. */
398 if (! unformat_user (input, unformat_line_input, line_input))
401 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
402 if (unformat (line_input, "del"))
404 else if (unformat (line_input, "src %U",
405 unformat_ip4_address, &src))
407 else if (unformat (line_input, "dst %U",
408 unformat_ip4_address, &dst))
410 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
412 encap_fib_index = fib_index_from_fib_id (tmp);
413 if (encap_fib_index == ~0)
414 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
416 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
418 decap_fib_index = fib_index_from_fib_id (tmp);
419 if (decap_fib_index == ~0)
420 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
422 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
425 else if (unformat (line_input, "vni %d", &vni))
427 else if (unformat (line_input, "version %d", &tmp))
428 ver_o_c |= (tmp & 3) << 6;
429 else if (unformat (line_input, "o-bit %d", &tmp))
430 ver_o_c |= (tmp & 1) << 5;
431 else if (unformat (line_input, "c-bit %d", &tmp))
432 ver_o_c |= (tmp & 1) << 4;
433 else if (unformat (line_input, "md-type %d", &tmp))
435 else if (unformat(line_input, "next-ip4"))
437 else if (unformat(line_input, "next-ip6"))
439 else if (unformat(line_input, "next-ethernet"))
441 else if (unformat(line_input, "next-nsh"))
443 else if (unformat (line_input, "c1 %d", &c1))
445 else if (unformat (line_input, "c2 %d", &c2))
447 else if (unformat (line_input, "c3 %d", &c3))
449 else if (unformat (line_input, "c4 %d", &c4))
451 else if (unformat (line_input, "spi %d", &spi))
453 else if (unformat (line_input, "si %d", &si))
455 else if (unformat (line_input, "tlv %x"))
456 vec_add1 (tlvs, tmp);
458 return clib_error_return (0, "parse error: '%U'",
459 format_unformat_error, line_input);
462 unformat_free (line_input);
465 return clib_error_return (0, "tunnel src address not specified");
468 return clib_error_return (0, "tunnel dst address not specified");
471 return clib_error_return (0, "vni not specified");
474 return clib_error_return (0, "spi not specified");
477 return clib_error_return (0, "si not specified");
479 spi_si = (spi<<8) | si;
481 memset (a, 0, sizeof (*a));
485 #define _(x) a->x = x;
489 rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
495 case VNET_API_ERROR_INVALID_DECAP_NEXT:
496 return clib_error_return (0, "invalid decap-next...");
498 case VNET_API_ERROR_TUNNEL_EXIST:
499 return clib_error_return (0, "tunnel already exists...");
501 case VNET_API_ERROR_NO_SUCH_ENTRY:
502 return clib_error_return (0, "tunnel does not exist...");
505 return clib_error_return
506 (0, "vnet_nsh_vxlan_gpe_add_del_tunnel returned %d", rv);
512 VLIB_CLI_COMMAND (create_nsh_vxlan_gpe_tunnel_command, static) = {
513 .path = "nsh vxlan tunnel",
515 "nsh vxlan tunnel src <ip4-addr> dst <ip4-addr>"
516 " c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn> vni <nn>\n"
517 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
518 " [md-type <nn>][next-ip4][next-ip6][next-ethernet][next-nsh]\n"
519 " [tlv <xx>][decap-next [ip4|ip6|ethernet|nsh-vxlan-gpe]][del]\n",
520 .function = nsh_vxlan_gpe_add_del_tunnel_command_fn,
523 static clib_error_t *
524 show_nsh_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
525 unformat_input_t * input,
526 vlib_cli_command_t * cmd)
528 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
529 nsh_vxlan_gpe_tunnel_t * t;
531 if (pool_elts (ngm->tunnels) == 0)
532 vlib_cli_output (vm, "No nsh-vxlan-gpe tunnels configured...");
534 pool_foreach (t, ngm->tunnels,
536 vlib_cli_output (vm, "%U", format_nsh_vxlan_gpe_tunnel, t);
542 VLIB_CLI_COMMAND (show_nsh_vxlan_gpe_tunnel_command, static) = {
543 .path = "show nsh vxlan tunnel",
544 .function = show_nsh_vxlan_gpe_tunnel_command_fn,
547 clib_error_t *nsh_vxlan_gpe_init (vlib_main_t *vm)
549 nsh_vxlan_gpe_main_t *ngm = &nsh_vxlan_gpe_main;
551 ngm->vnet_main = vnet_get_main();
554 ngm->nsh_vxlan_gpe_tunnel_by_key
555 = hash_create_mem (0, sizeof(nsh_vxlan_gpe_tunnel_key_t), sizeof (uword));
557 udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
558 nsh_vxlan_gpe_input_node.index, 1 /* is_ip4 */);
562 VLIB_INIT_FUNCTION(nsh_vxlan_gpe_init);