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->ver_o_c>>6));
56 if (t->ver_o_c & NSH_GRE_O_BIT)
57 s = format (s, "O-set ");
59 if (t->ver_o_c & NSH_GRE_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->length, t->length * 4, t->md_type, t->next_protocol);
65 s = format (s, " service path %d service index %d\n",
66 (t->spi_si>>NSH_GRE_SPI_SHIFT) & NSH_GRE_SPI_MASK,
67 t->spi_si & NSH_GRE_SINDEX_MASK);
69 s = format (s, " c1 %d c2 %d c3 %d c4 %d\n",
70 t->c1, t->c2, t->c3, t->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 \
147 _(decap_next_index) \
159 #define foreach_32bit_field \
166 static int nsh_vxlan_gpe_rewrite (nsh_vxlan_gpe_tunnel_t * t)
171 ip4_vxlan_gpe_and_nsh_header_t * h0;
174 len = sizeof (*h0) + vec_len(t->tlvs)*4;
176 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
178 h0 = (ip4_vxlan_gpe_and_nsh_header_t *) rw;
180 /* Fixed portion of the (outer) ip4 header */
182 ip0->ip_version_and_header_length = 0x45;
184 ip0->protocol = IP_PROTOCOL_UDP;
186 /* we fix up the ip4 header length and checksum after-the-fact */
187 ip0->src_address.as_u32 = t->src.as_u32;
188 ip0->dst_address.as_u32 = t->dst.as_u32;
189 ip0->checksum = ip4_header_checksum (ip0);
191 /* UDP header, randomize src port on something, maybe? */
192 h0->udp.src_port = clib_host_to_net_u16 (4790);
193 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
195 /* VXLAN header. Are we having fun yet? */
196 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
197 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
198 h0->vxlan.next_protocol = VXLAN_NEXT_PROTOCOL_NSH;
199 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
203 nsh0->ver_o_c = t->ver_o_c;
204 nsh0->md_type = t->md_type;
205 nsh0->next_protocol = t->next_protocol;
206 nsh0->spi_si = t->spi_si;
212 /* Endian swap 32-bit fields */
213 #define _(x) nsh0->x = clib_host_to_net_u32(nsh0->x);
217 /* fix nsh header length */
218 t->length = 6 + vec_len(t->tlvs);
219 nsh0->length = t->length;
222 if (vec_len(t->tlvs))
223 clib_memcpy (nsh0->tlvs, t->tlvs, 4*vec_len(t->tlvs));
229 int vnet_nsh_vxlan_gpe_add_del_tunnel
230 (vnet_nsh_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
232 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
233 nsh_vxlan_gpe_tunnel_t *t = 0;
234 vnet_main_t * vnm = ngm->vnet_main;
235 vnet_hw_interface_t * hi;
237 u32 hw_if_index = ~0;
238 u32 sw_if_index = ~0;
240 nsh_vxlan_gpe_tunnel_key_t key, *key_copy;
243 key.src = a->dst.as_u32; /* decap src in key is encap dst in config */
244 key.vni = clib_host_to_net_u32 (a->vni << 8);
245 key.spi_si = clib_host_to_net_u32(a->spi_si);
248 p = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
252 /* adding a tunnel: tunnel must not already exist */
254 return VNET_API_ERROR_INVALID_VALUE;
256 if (a->decap_next_index >= NSH_VXLAN_GPE_INPUT_N_NEXT)
257 return VNET_API_ERROR_INVALID_DECAP_NEXT;
259 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
260 memset (t, 0, sizeof (*t));
262 /* copy from arg structure */
263 #define _(x) t->x = a->x;
267 rv = nsh_vxlan_gpe_rewrite (t);
271 pool_put (ngm->tunnels, t);
275 key_copy = clib_mem_alloc (sizeof (*key_copy));
276 clib_memcpy (key_copy, &key, sizeof (*key_copy));
278 hash_set_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, key_copy,
281 if (vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) > 0)
283 hw_if_index = ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices
284 [vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices)-1];
285 _vec_len (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices) -= 1;
287 hi = vnet_get_hw_interface (vnm, hw_if_index);
288 hi->dev_instance = t - ngm->tunnels;
289 hi->hw_instance = hi->dev_instance;
293 hw_if_index = vnet_register_interface
294 (vnm, nsh_vxlan_gpe_device_class.index, t - ngm->tunnels,
295 nsh_vxlan_gpe_hw_class.index, t - ngm->tunnels);
296 hi = vnet_get_hw_interface (vnm, hw_if_index);
297 hi->output_node_index = nsh_vxlan_gpe_encap_node.index;
300 t->hw_if_index = hw_if_index;
301 t->sw_if_index = sw_if_index = hi->sw_if_index;
303 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
304 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
308 /* deleting a tunnel: tunnel must exist */
310 return VNET_API_ERROR_NO_SUCH_ENTRY;
312 t = pool_elt_at_index (ngm->tunnels, p[0]);
314 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
315 vec_add1 (ngm->free_nsh_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
317 hp = hash_get_pair (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
318 key_copy = (void *)(hp->key);
319 hash_unset_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key);
320 clib_mem_free (key_copy);
322 vec_free (t->rewrite);
323 pool_put (ngm->tunnels, t);
327 *sw_if_indexp = sw_if_index;
332 static u32 fib_index_from_fib_id (u32 fib_id)
334 ip4_main_t * im = &ip4_main;
337 p = hash_get (im->fib_index_by_table_id, fib_id);
344 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
346 u32 * result = va_arg (*args, u32 *);
349 if (unformat (input, "drop"))
350 *result = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
351 else if (unformat (input, "ip4"))
352 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
353 else if (unformat (input, "ip6"))
354 *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
355 else if (unformat (input, "ethernet"))
356 *result = NSH_VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
357 else if (unformat (input, "nsh-vxlan-gpe"))
358 *result = NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP;
359 else if (unformat (input, "%d", &tmp))
366 static clib_error_t *
367 nsh_vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
368 unformat_input_t * input,
369 vlib_cli_command_t * cmd)
371 unformat_input_t _line_input, * line_input = &_line_input;
372 ip4_address_t src, dst;
376 u32 encap_fib_index = 0;
377 u32 decap_fib_index = 0;
381 u8 next_protocol = 1; /* default: ip4 */
382 u32 decap_next_index = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
397 vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
399 /* Get a line of input. */
400 if (! unformat_user (input, unformat_line_input, line_input))
403 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
404 if (unformat (line_input, "del"))
406 else if (unformat (line_input, "src %U",
407 unformat_ip4_address, &src))
409 else if (unformat (line_input, "dst %U",
410 unformat_ip4_address, &dst))
412 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
414 encap_fib_index = fib_index_from_fib_id (tmp);
415 if (encap_fib_index == ~0)
416 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
418 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
420 decap_fib_index = fib_index_from_fib_id (tmp);
421 if (decap_fib_index == ~0)
422 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
424 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
427 else if (unformat (line_input, "vni %d", &vni))
429 else if (unformat (line_input, "version %d", &tmp))
430 ver_o_c |= (tmp & 3) << 6;
431 else if (unformat (line_input, "o-bit %d", &tmp))
432 ver_o_c |= (tmp & 1) << 5;
433 else if (unformat (line_input, "c-bit %d", &tmp))
434 ver_o_c |= (tmp & 1) << 4;
435 else if (unformat (line_input, "md-type %d", &tmp))
437 else if (unformat(line_input, "next-ip4"))
439 else if (unformat(line_input, "next-ip6"))
441 else if (unformat(line_input, "next-ethernet"))
443 else if (unformat(line_input, "next-nsh"))
445 else if (unformat (line_input, "c1 %d", &c1))
447 else if (unformat (line_input, "c2 %d", &c2))
449 else if (unformat (line_input, "c3 %d", &c3))
451 else if (unformat (line_input, "c4 %d", &c4))
453 else if (unformat (line_input, "spi %d", &spi))
455 else if (unformat (line_input, "si %d", &si))
457 else if (unformat (line_input, "tlv %x"))
458 vec_add1 (tlvs, tmp);
460 return clib_error_return (0, "parse error: '%U'",
461 format_unformat_error, line_input);
464 unformat_free (line_input);
467 return clib_error_return (0, "tunnel src address not specified");
470 return clib_error_return (0, "tunnel dst address not specified");
473 return clib_error_return (0, "vni not specified");
476 return clib_error_return (0, "spi not specified");
479 return clib_error_return (0, "si not specified");
481 spi_si = (spi<<8) | si;
483 memset (a, 0, sizeof (*a));
487 #define _(x) a->x = x;
491 rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
497 case VNET_API_ERROR_INVALID_DECAP_NEXT:
498 return clib_error_return (0, "invalid decap-next...");
500 case VNET_API_ERROR_TUNNEL_EXIST:
501 return clib_error_return (0, "tunnel already exists...");
503 case VNET_API_ERROR_NO_SUCH_ENTRY:
504 return clib_error_return (0, "tunnel does not exist...");
507 return clib_error_return
508 (0, "vnet_nsh_vxlan_gpe_add_del_tunnel returned %d", rv);
514 VLIB_CLI_COMMAND (create_nsh_vxlan_gpe_tunnel_command, static) = {
515 .path = "nsh vxlan tunnel",
517 "nsh vxlan tunnel src <ip4-addr> dst <ip4-addr>"
518 " c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn> vni <nn>\n"
519 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
520 " [md-type <nn>][next-ip4][next-ip6][next-ethernet][next-nsh]\n"
521 " [tlv <xx>][decap-next [ip4|ip6|ethernet|nsh-vxlan-gpe]][del]\n",
522 .function = nsh_vxlan_gpe_add_del_tunnel_command_fn,
525 static clib_error_t *
526 show_nsh_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
527 unformat_input_t * input,
528 vlib_cli_command_t * cmd)
530 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
531 nsh_vxlan_gpe_tunnel_t * t;
533 if (pool_elts (ngm->tunnels) == 0)
534 vlib_cli_output (vm, "No nsh-vxlan-gpe tunnels configured...");
536 pool_foreach (t, ngm->tunnels,
538 vlib_cli_output (vm, "%U", format_nsh_vxlan_gpe_tunnel, t);
544 VLIB_CLI_COMMAND (show_nsh_vxlan_gpe_tunnel_command, static) = {
545 .path = "show nsh vxlan tunnel",
546 .function = show_nsh_vxlan_gpe_tunnel_command_fn,
549 clib_error_t *nsh_vxlan_gpe_init (vlib_main_t *vm)
551 nsh_vxlan_gpe_main_t *ngm = &nsh_vxlan_gpe_main;
553 ngm->vnet_main = vnet_get_main();
556 ngm->nsh_vxlan_gpe_tunnel_by_key
557 = hash_create_mem (0, sizeof(nsh_vxlan_gpe_tunnel_key_t), sizeof (uword));
559 udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
560 nsh_vxlan_gpe_input_node.index, 1 /* is_ip4 */);
564 VLIB_INIT_FUNCTION(nsh_vxlan_gpe_init);