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 <vppinfra/error.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/l2/l2_input.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/nsh-gre/nsh_gre.h>
23 nsh_gre_main_t nsh_gre_main;
25 static u8 * format_decap_next (u8 * s, va_list * args)
27 u32 next_index = va_arg (*args, u32);
31 case NSH_INPUT_NEXT_DROP:
32 return format (s, "drop");
33 case NSH_INPUT_NEXT_IP4_INPUT:
34 return format (s, "ip4");
35 case NSH_INPUT_NEXT_IP6_INPUT:
36 return format (s, "ip6");
37 case NSH_INPUT_NEXT_ETHERNET_INPUT:
38 return format (s, "ethernet");
40 return format (s, "index %d", next_index);
46 u8 * format_nsh_gre_tunnel (u8 * s, va_list * args)
48 nsh_gre_tunnel_t * t = va_arg (*args, nsh_gre_tunnel_t *);
49 nsh_gre_main_t * ngm = &nsh_gre_main;
51 s = format (s, "[%d] %U (src) %U (dst) fibs: (encap %d, decap %d)",
53 format_ip4_address, &t->src,
54 format_ip4_address, &t->dst,
58 s = format (s, " decap-next %U\n", format_decap_next, t->decap_next_index);
60 s = format (s, " ver %d ", (t->ver_o_c>>6));
61 if (t->ver_o_c & NSH_GRE_O_BIT)
62 s = format (s, "O-set ");
64 if (t->ver_o_c & NSH_GRE_C_BIT)
65 s = format (s, "C-set ");
67 s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
68 t->length, t->length * 4, t->md_type, t->next_protocol);
70 s = format (s, " service path %d service index %d\n",
71 (t->spi_si>>NSH_GRE_SPI_SHIFT) & NSH_GRE_SPI_MASK,
72 t->spi_si & NSH_GRE_SINDEX_MASK);
74 s = format (s, " c1 %d c2 %d c3 %d c4 %d\n",
75 t->c1, t->c2, t->c3, t->c4);
80 static u8 * format_nsh_gre_name (u8 * s, va_list * args)
82 nsh_gre_main_t * ngm = &nsh_gre_main;
83 u32 i = va_arg (*args, u32);
84 u32 show_dev_instance = ~0;
86 if (i < vec_len (ngm->dev_inst_by_real))
87 show_dev_instance = ngm->dev_inst_by_real[i];
89 if (show_dev_instance != ~0)
90 i = show_dev_instance;
92 return format (s, "nsh_gre_tunnel%d", i);
95 static int nsh_gre_name_renumber (vnet_hw_interface_t * hi,
98 nsh_gre_main_t * ngm = &nsh_gre_main;
100 vec_validate_init_empty (ngm->dev_inst_by_real, hi->dev_instance, ~0);
102 ngm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
107 static uword dummy_interface_tx (vlib_main_t * vm,
108 vlib_node_runtime_t * node,
109 vlib_frame_t * frame)
111 clib_warning ("you shouldn't be here, leaking buffers...");
112 return frame->n_vectors;
115 VNET_DEVICE_CLASS (nsh_gre_device_class,static) = {
117 .format_device_name = format_nsh_gre_name,
118 .format_tx_trace = format_nsh_gre_encap_trace,
119 .tx_function = dummy_interface_tx,
120 .name_renumber = nsh_gre_name_renumber,
123 static uword dummy_set_rewrite (vnet_main_t * vnm,
128 uword max_rewrite_bytes)
133 static u8 * format_nsh_gre_header_with_length (u8 * s, va_list * args)
135 u32 dev_instance = va_arg (*args, u32);
136 s = format (s, "unimplemented dev %u", dev_instance);
140 VNET_HW_INTERFACE_CLASS (nsh_gre_hw_class) = {
142 .format_header = format_nsh_gre_header_with_length,
143 .set_rewrite = dummy_set_rewrite,
146 #define foreach_copy_field \
151 _(decap_next_index) \
163 #define foreach_32bit_field \
170 static int nsh_gre_rewrite (nsh_gre_tunnel_t * t)
175 ip4_gre_and_nsh_header_t * h0;
178 len = sizeof (*h0) + vec_len(t->tlvs)*4;
180 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
182 h0 = (ip4_gre_and_nsh_header_t *) rw;
184 /* Fixed portion of the (outer) ip4 header */
186 ip0->ip_version_and_header_length = 0x45;
188 ip0->protocol = IP_PROTOCOL_GRE;
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 /* GRE header, zero execpt for the NSH ethertype */
195 h0->gre.protocol = clib_host_to_net_u16(GRE_PROTOCOL_nsh);
199 nsh0->ver_o_c = t->ver_o_c;
200 nsh0->md_type = t->md_type;
201 nsh0->next_protocol = t->next_protocol;
202 nsh0->spi_si = t->spi_si;
208 /* Endian swap 32-bit fields */
209 #define _(x) nsh0->x = clib_host_to_net_u32(nsh0->x);
213 /* fix nsh header length */
214 t->length = 6 + vec_len(t->tlvs);
215 nsh0->length = t->length;
218 if (vec_len(t->tlvs))
219 clib_memcpy (nsh0->tlvs, t->tlvs, 4*vec_len(t->tlvs));
225 int vnet_nsh_gre_add_del_tunnel (vnet_nsh_gre_add_del_tunnel_args_t *a,
228 nsh_gre_main_t * ngm = &nsh_gre_main;
229 nsh_gre_tunnel_t *t = 0;
230 vnet_main_t * vnm = ngm->vnet_main;
231 vnet_hw_interface_t * hi;
233 u32 hw_if_index = ~0;
234 u32 sw_if_index = ~0;
237 u32 spi_si_net_byte_order;
239 spi_si_net_byte_order = clib_host_to_net_u32(a->spi_si);
241 key = (((u64)(a->src.as_u32))<<32) | spi_si_net_byte_order;
243 p = hash_get (ngm->nsh_gre_tunnel_by_src_address, key);
247 /* adding a tunnel: tunnel must not already exist */
249 return VNET_API_ERROR_INVALID_VALUE;
251 if (a->decap_next_index >= NSH_INPUT_N_NEXT)
252 return VNET_API_ERROR_INVALID_DECAP_NEXT;
254 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
255 memset (t, 0, sizeof (*t));
257 /* copy from arg structure */
258 #define _(x) t->x = a->x;
262 rv = nsh_gre_rewrite (t);
266 pool_put (ngm->tunnels, t);
270 hash_set (ngm->nsh_gre_tunnel_by_src_address, key, t - ngm->tunnels);
272 if (vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices) > 0)
274 hw_if_index = ngm->free_nsh_gre_tunnel_hw_if_indices
275 [vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices)-1];
276 _vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices) -= 1;
278 hi = vnet_get_hw_interface (vnm, hw_if_index);
279 hi->dev_instance = t - ngm->tunnels;
280 hi->hw_instance = hi->dev_instance;
284 hw_if_index = vnet_register_interface
285 (vnm, nsh_gre_device_class.index, t - ngm->tunnels,
286 nsh_gre_hw_class.index, t - ngm->tunnels);
287 hi = vnet_get_hw_interface (vnm, hw_if_index);
288 hi->output_node_index = nsh_gre_encap_node.index;
291 t->hw_if_index = hw_if_index;
292 t->sw_if_index = sw_if_index = hi->sw_if_index;
294 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
295 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
299 /* deleting a tunnel: tunnel must exist */
301 return VNET_API_ERROR_NO_SUCH_ENTRY;
303 t = pool_elt_at_index (ngm->tunnels, p[0]);
305 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
306 vec_add1 (ngm->free_nsh_gre_tunnel_hw_if_indices, t->hw_if_index);
308 hash_unset (ngm->nsh_gre_tunnel_by_src_address, key);
309 vec_free (t->rewrite);
310 pool_put (ngm->tunnels, t);
314 *sw_if_indexp = sw_if_index;
319 static u32 fib_index_from_fib_id (u32 fib_id)
321 ip4_main_t * im = &ip4_main;
324 p = hash_get (im->fib_index_by_table_id, fib_id);
331 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
333 u32 * result = va_arg (*args, u32 *);
336 if (unformat (input, "drop"))
337 *result = NSH_INPUT_NEXT_DROP;
338 else if (unformat (input, "ip4"))
339 *result = NSH_INPUT_NEXT_IP4_INPUT;
340 else if (unformat (input, "ip6"))
341 *result = NSH_INPUT_NEXT_IP6_INPUT;
342 else if (unformat (input, "ethernet"))
343 *result = NSH_INPUT_NEXT_ETHERNET_INPUT;
344 else if (unformat (input, "%d", &tmp))
351 static clib_error_t *
352 nsh_gre_add_del_tunnel_command_fn (vlib_main_t * vm,
353 unformat_input_t * input,
354 vlib_cli_command_t * cmd)
356 unformat_input_t _line_input, * line_input = &_line_input;
357 ip4_address_t src, dst;
361 u32 encap_fib_index = 0;
362 u32 decap_fib_index = 0;
366 u8 next_protocol = 1; /* ip4 */
376 u32 decap_next_index = 1; /* ip4_input */
380 vnet_nsh_gre_add_del_tunnel_args_t _a, * a = &_a;
382 /* Get a line of input. */
383 if (! unformat_user (input, unformat_line_input, line_input))
386 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
387 if (unformat (line_input, "del"))
389 else if (unformat (line_input, "src %U",
390 unformat_ip4_address, &src))
392 else if (unformat (line_input, "dst %U",
393 unformat_ip4_address, &dst))
395 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
397 encap_fib_index = fib_index_from_fib_id (tmp);
398 if (encap_fib_index == ~0)
399 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
401 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
403 decap_fib_index = fib_index_from_fib_id (tmp);
404 if (decap_fib_index == ~0)
405 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
407 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
410 else if (unformat (line_input, "version %d", &tmp))
411 ver_o_c |= (tmp & 3) << 6;
412 else if (unformat (line_input, "o-bit %d", &tmp))
413 ver_o_c |= (tmp & 1) << 5;
414 else if (unformat (line_input, "c-bit %d", &tmp))
415 ver_o_c |= (tmp & 1) << 4;
416 else if (unformat (line_input, "md-type %d", &tmp))
418 else if (unformat(line_input, "next-ip4"))
420 else if (unformat(line_input, "next-ip6"))
422 else if (unformat(line_input, "next-ethernet"))
424 else if (unformat (line_input, "c1 %d", &c1))
426 else if (unformat (line_input, "c2 %d", &c2))
428 else if (unformat (line_input, "c3 %d", &c3))
430 else if (unformat (line_input, "c4 %d", &c4))
432 else if (unformat (line_input, "spi %d", &spi))
434 else if (unformat (line_input, "si %d", &si))
436 else if (unformat (line_input, "tlv %x"))
437 vec_add1 (tlvs, tmp);
439 return clib_error_return (0, "parse error: '%U'",
440 format_unformat_error, line_input);
443 unformat_free (line_input);
446 return clib_error_return (0, "tunnel src address not specified");
449 return clib_error_return (0, "tunnel dst address not specified");
452 return clib_error_return (0, "spi not specified");
455 return clib_error_return (0, "si not specified");
457 spi_si = (spi<<8) | si;
459 memset (a, 0, sizeof (*a));
463 #define _(x) a->x = x;
467 rv = vnet_nsh_gre_add_del_tunnel (a, 0 /* hw_if_indexp */);
473 case VNET_API_ERROR_INVALID_DECAP_NEXT:
474 return clib_error_return (0, "invalid decap-next...");
476 case VNET_API_ERROR_TUNNEL_EXIST:
477 return clib_error_return (0, "tunnel already exists...");
479 case VNET_API_ERROR_NO_SUCH_ENTRY:
480 return clib_error_return (0, "session does not exist...");
483 return clib_error_return
484 (0, "vnet_nsh_gre_add_del_tunnel returned %d", rv);
490 VLIB_CLI_COMMAND (create_nsh_gre_tunnel_command, static) = {
491 .path = "nsh gre tunnel",
493 "nsh gre tunnel src <ip4-addr> dst <ip4-addr>"
494 " c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn>\n"
495 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
496 " [md-type <nn>][next-ip4][next-ip6][next-ethernet]\n"
497 " [tlv <xx>][decap-next [ip4|ip6|ethernet]][del]\n",
498 .function = nsh_gre_add_del_tunnel_command_fn,
501 static clib_error_t *
502 show_nsh_gre_tunnel_command_fn (vlib_main_t * vm,
503 unformat_input_t * input,
504 vlib_cli_command_t * cmd)
506 nsh_gre_main_t * ngm = &nsh_gre_main;
507 nsh_gre_tunnel_t * t;
509 if (pool_elts (ngm->tunnels) == 0)
510 vlib_cli_output (vm, "No nsh-gre tunnels configured...");
512 pool_foreach (t, ngm->tunnels,
514 vlib_cli_output (vm, "%U", format_nsh_gre_tunnel, t);
520 VLIB_CLI_COMMAND (show_nsh_gre_tunnel_command, static) = {
521 .path = "show nsh gre tunnel",
522 .function = show_nsh_gre_tunnel_command_fn,
525 clib_error_t *nsh_gre_init (vlib_main_t *vm)
527 nsh_gre_main_t *ngm = &nsh_gre_main;
529 ngm->vnet_main = vnet_get_main();
532 ngm->nsh_gre_tunnel_by_src_address = hash_create (0, sizeof (uword));
533 gre_register_input_protocol (vm, GRE_PROTOCOL_nsh,
534 nsh_gre_input_node.index);
538 VLIB_INIT_FUNCTION(nsh_gre_init);