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_GRE_INPUT_NEXT_DROP:
32 return format (s, "drop");
33 case NSH_GRE_INPUT_NEXT_IP4_INPUT:
34 return format (s, "ip4");
35 case NSH_GRE_INPUT_NEXT_IP6_INPUT:
36 return format (s, "ip6");
37 case NSH_GRE_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->nsh_hdr.ver_o_c>>6));
61 if (t->nsh_hdr.ver_o_c & NSH_O_BIT)
62 s = format (s, "O-set ");
64 if (t->nsh_hdr.ver_o_c & NSH_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->nsh_hdr.length, t->nsh_hdr.length * 4, t->nsh_hdr.md_type, t->nsh_hdr.next_protocol);
70 s = format (s, " service path %d service index %d\n",
71 (t->nsh_hdr.spi_si>>NSH_SPI_SHIFT) & NSH_SPI_MASK,
72 t->nsh_hdr.spi_si & NSH_SINDEX_MASK);
74 s = format (s, " c1 %d c2 %d c3 %d c4 %d\n",
75 t->nsh_hdr.c1, t->nsh_hdr.c2, t->nsh_hdr.c3, t->nsh_hdr.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 \
154 #define foreach_copy_nshhdr_field \
166 #define foreach_32bit_field \
173 static int nsh_gre_rewrite (nsh_gre_tunnel_t * t)
178 ip4_gre_and_nsh_header_t * h0;
181 len = sizeof (*h0) + vec_len(t->nsh_hdr.tlvs)*4;
183 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
185 h0 = (ip4_gre_and_nsh_header_t *) rw;
187 /* Fixed portion of the (outer) ip4 header */
189 ip0->ip_version_and_header_length = 0x45;
191 ip0->protocol = IP_PROTOCOL_GRE;
192 /* we fix up the ip4 header length and checksum after-the-fact */
193 ip0->src_address.as_u32 = t->src.as_u32;
194 ip0->dst_address.as_u32 = t->dst.as_u32;
195 ip0->checksum = ip4_header_checksum (ip0);
197 /* GRE header, zero execpt for the NSH ethertype */
198 h0->gre.protocol = clib_host_to_net_u16(GRE_PROTOCOL_nsh);
202 nsh0->ver_o_c = t->nsh_hdr.ver_o_c;
203 nsh0->md_type = t->nsh_hdr.md_type;
204 nsh0->next_protocol = t->nsh_hdr.next_protocol;
205 nsh0->spi_si = t->nsh_hdr.spi_si;
206 nsh0->c1 = t->nsh_hdr.c1;
207 nsh0->c2 = t->nsh_hdr.c2;
208 nsh0->c3 = t->nsh_hdr.c3;
209 nsh0->c4 = t->nsh_hdr.c4;
211 /* Endian swap 32-bit fields */
212 #define _(x) nsh0->x = clib_host_to_net_u32(nsh0->x);
216 /* fix nsh header length */
217 t->nsh_hdr.length = 6 + vec_len(t->nsh_hdr.tlvs);
218 nsh0->length = t->nsh_hdr.length;
221 if (vec_len(t->nsh_hdr.tlvs))
222 clib_memcpy (nsh0->tlvs, t->nsh_hdr.tlvs, 4*vec_len(t->nsh_hdr.tlvs));
228 int vnet_nsh_gre_add_del_tunnel (vnet_nsh_gre_add_del_tunnel_args_t *a,
231 nsh_gre_main_t * ngm = &nsh_gre_main;
232 nsh_gre_tunnel_t *t = 0;
233 vnet_main_t * vnm = ngm->vnet_main;
234 vnet_hw_interface_t * hi;
236 u32 hw_if_index = ~0;
237 u32 sw_if_index = ~0;
240 u32 spi_si_net_byte_order;
242 spi_si_net_byte_order = clib_host_to_net_u32(a->nsh_hdr.spi_si);
244 key = (((u64)(a->src.as_u32))<<32) | spi_si_net_byte_order;
246 p = hash_get (ngm->nsh_gre_tunnel_by_src_address, key);
250 /* adding a tunnel: tunnel must not already exist */
252 return VNET_API_ERROR_INVALID_VALUE;
254 if (a->decap_next_index >= NSH_GRE_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 /* copy from arg structure */
266 #define _(x) t->nsh_hdr.x = a->nsh_hdr.x;
267 foreach_copy_nshhdr_field;
270 rv = nsh_gre_rewrite (t);
274 pool_put (ngm->tunnels, t);
278 hash_set (ngm->nsh_gre_tunnel_by_src_address, key, t - ngm->tunnels);
280 if (vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices) > 0)
282 hw_if_index = ngm->free_nsh_gre_tunnel_hw_if_indices
283 [vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices)-1];
284 _vec_len (ngm->free_nsh_gre_tunnel_hw_if_indices) -= 1;
286 hi = vnet_get_hw_interface (vnm, hw_if_index);
287 hi->dev_instance = t - ngm->tunnels;
288 hi->hw_instance = hi->dev_instance;
292 hw_if_index = vnet_register_interface
293 (vnm, nsh_gre_device_class.index, t - ngm->tunnels,
294 nsh_gre_hw_class.index, t - ngm->tunnels);
295 hi = vnet_get_hw_interface (vnm, hw_if_index);
296 hi->output_node_index = nsh_gre_encap_node.index;
299 t->hw_if_index = hw_if_index;
300 t->sw_if_index = sw_if_index = hi->sw_if_index;
302 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
303 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
307 /* deleting a tunnel: tunnel must exist */
309 return VNET_API_ERROR_NO_SUCH_ENTRY;
311 t = pool_elt_at_index (ngm->tunnels, p[0]);
313 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
314 vec_add1 (ngm->free_nsh_gre_tunnel_hw_if_indices, t->hw_if_index);
316 hash_unset (ngm->nsh_gre_tunnel_by_src_address, key);
317 vec_free (t->rewrite);
318 pool_put (ngm->tunnels, t);
322 *sw_if_indexp = sw_if_index;
327 static u32 fib_index_from_fib_id (u32 fib_id)
329 ip4_main_t * im = &ip4_main;
332 p = hash_get (im->fib_index_by_table_id, fib_id);
339 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
341 u32 * result = va_arg (*args, u32 *);
344 if (unformat (input, "drop"))
345 *result = NSH_GRE_INPUT_NEXT_DROP;
346 else if (unformat (input, "ip4"))
347 *result = NSH_GRE_INPUT_NEXT_IP4_INPUT;
348 else if (unformat (input, "ip6"))
349 *result = NSH_GRE_INPUT_NEXT_IP6_INPUT;
350 else if (unformat (input, "ethernet"))
351 *result = NSH_GRE_INPUT_NEXT_ETHERNET_INPUT;
352 else if (unformat (input, "%d", &tmp))
359 static clib_error_t *
360 nsh_gre_add_del_tunnel_command_fn (vlib_main_t * vm,
361 unformat_input_t * input,
362 vlib_cli_command_t * cmd)
364 unformat_input_t _line_input, * line_input = &_line_input;
365 ip4_address_t src, dst;
369 u32 encap_fib_index = 0;
370 u32 decap_fib_index = 0;
374 u8 next_protocol = 1; /* ip4 */
384 u32 decap_next_index = 1; /* ip4_input */
388 vnet_nsh_gre_add_del_tunnel_args_t _a, * a = &_a;
390 /* Get a line of input. */
391 if (! unformat_user (input, unformat_line_input, line_input))
394 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
395 if (unformat (line_input, "del"))
397 else if (unformat (line_input, "src %U",
398 unformat_ip4_address, &src))
400 else if (unformat (line_input, "dst %U",
401 unformat_ip4_address, &dst))
403 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
405 encap_fib_index = fib_index_from_fib_id (tmp);
406 if (encap_fib_index == ~0)
407 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
409 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
411 decap_fib_index = fib_index_from_fib_id (tmp);
412 if (decap_fib_index == ~0)
413 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
415 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
418 else if (unformat (line_input, "version %d", &tmp))
419 ver_o_c |= (tmp & 3) << 6;
420 else if (unformat (line_input, "o-bit %d", &tmp))
421 ver_o_c |= (tmp & 1) << 5;
422 else if (unformat (line_input, "c-bit %d", &tmp))
423 ver_o_c |= (tmp & 1) << 4;
424 else if (unformat (line_input, "md-type %d", &tmp))
426 else if (unformat(line_input, "next-ip4"))
428 else if (unformat(line_input, "next-ip6"))
430 else if (unformat(line_input, "next-ethernet"))
432 else if (unformat (line_input, "c1 %d", &c1))
434 else if (unformat (line_input, "c2 %d", &c2))
436 else if (unformat (line_input, "c3 %d", &c3))
438 else if (unformat (line_input, "c4 %d", &c4))
440 else if (unformat (line_input, "spi %d", &spi))
442 else if (unformat (line_input, "si %d", &si))
444 else if (unformat (line_input, "tlv %x"))
445 vec_add1 (tlvs, tmp);
447 return clib_error_return (0, "parse error: '%U'",
448 format_unformat_error, line_input);
451 unformat_free (line_input);
454 return clib_error_return (0, "tunnel src address not specified");
457 return clib_error_return (0, "tunnel dst address not specified");
460 return clib_error_return (0, "spi not specified");
463 return clib_error_return (0, "si not specified");
465 spi_si = (spi<<8) | si;
467 memset (a, 0, sizeof (*a));
471 #define _(x) a->x = x;
475 /* copy from arg structure */
476 #define _(x) a->nsh_hdr.x = x;
477 foreach_copy_nshhdr_field;
480 rv = vnet_nsh_gre_add_del_tunnel (a, 0 /* hw_if_indexp */);
486 case VNET_API_ERROR_INVALID_DECAP_NEXT:
487 return clib_error_return (0, "invalid decap-next...");
489 case VNET_API_ERROR_TUNNEL_EXIST:
490 return clib_error_return (0, "tunnel already exists...");
492 case VNET_API_ERROR_NO_SUCH_ENTRY:
493 return clib_error_return (0, "session does not exist...");
496 return clib_error_return
497 (0, "vnet_nsh_gre_add_del_tunnel returned %d", rv);
503 VLIB_CLI_COMMAND (create_nsh_gre_tunnel_command, static) = {
504 .path = "nsh gre tunnel",
506 "nsh gre tunnel src <ip4-addr> dst <ip4-addr>"
507 " c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn>\n"
508 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>] [c-bit <1|0>]\n"
509 " [md-type <nn>][next-ip4][next-ip6][next-ethernet]\n"
510 " [tlv <xx>][decap-next [ip4|ip6|ethernet]][del]\n",
511 .function = nsh_gre_add_del_tunnel_command_fn,
514 static clib_error_t *
515 show_nsh_gre_tunnel_command_fn (vlib_main_t * vm,
516 unformat_input_t * input,
517 vlib_cli_command_t * cmd)
519 nsh_gre_main_t * ngm = &nsh_gre_main;
520 nsh_gre_tunnel_t * t;
522 if (pool_elts (ngm->tunnels) == 0)
523 vlib_cli_output (vm, "No nsh-gre tunnels configured...");
525 pool_foreach (t, ngm->tunnels,
527 vlib_cli_output (vm, "%U", format_nsh_gre_tunnel, t);
533 VLIB_CLI_COMMAND (show_nsh_gre_tunnel_command, static) = {
534 .path = "show nsh gre tunnel",
535 .function = show_nsh_gre_tunnel_command_fn,
538 clib_error_t *nsh_gre_init (vlib_main_t *vm)
540 nsh_gre_main_t *ngm = &nsh_gre_main;
542 ngm->vnet_main = vnet_get_main();
545 ngm->nsh_gre_tunnel_by_src_address = hash_create (0, sizeof (uword));
546 gre_register_input_protocol (vm, GRE_PROTOCOL_nsh,
547 nsh_gre_input_node.index);
551 VLIB_INIT_FUNCTION(nsh_gre_init);