2 * nsh_cli.c - nsh cli functions
4 * Copyright (c) 2019 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
21 #include <vnet/adj/adj.h>
23 /* format from network order */
25 format_nsh_pop_header (u8 * s, va_list * args)
27 return format_nsh_header (s, args);
31 format_nsh_pop_node_map_trace (u8 * s, va_list * args)
33 return format_nsh_node_map_trace (s, args);
37 unformat_nsh_action (unformat_input_t * input, va_list * args)
39 u32 *result = va_arg (*args, u32 *);
42 if (unformat (input, "swap"))
43 *result = NSH_ACTION_SWAP;
44 else if (unformat (input, "push"))
45 *result = NSH_ACTION_PUSH;
46 else if (unformat (input, "pop"))
47 *result = NSH_ACTION_POP;
48 else if (unformat (input, "%d", &tmp))
57 format_nsh_action (u8 * s, va_list * args)
59 u32 nsh_action = va_arg (*args, u32);
64 return format (s, "swap");
66 return format (s, "push");
68 return format (s, "pop");
70 return format (s, "unknown %d", nsh_action);
76 format_nsh_map (u8 * s, va_list * args)
78 nsh_map_t *map = va_arg (*args, nsh_map_t *);
80 s = format (s, "nsh entry nsp: %d nsi: %d ",
81 (map->nsp_nsi >> NSH_NSP_SHIFT) & NSH_NSP_MASK,
82 map->nsp_nsi & NSH_NSI_MASK);
83 s = format (s, "maps to nsp: %d nsi: %d ",
84 (map->mapped_nsp_nsi >> NSH_NSP_SHIFT) & NSH_NSP_MASK,
85 map->mapped_nsp_nsi & NSH_NSI_MASK);
87 s = format (s, " nsh_action %U\n", format_nsh_action, map->nsh_action);
89 switch (map->next_node)
91 case NSH_NODE_NEXT_ENCAP_GRE4:
93 s = format (s, "encapped by GRE4 intf: %d", map->sw_if_index);
96 case NSH_NODE_NEXT_ENCAP_GRE6:
98 s = format (s, "encapped by GRE6 intf: %d", map->sw_if_index);
101 case NSH_NODE_NEXT_ENCAP_VXLANGPE:
103 s = format (s, "encapped by VXLAN GPE intf: %d", map->sw_if_index);
106 case NSH_NODE_NEXT_ENCAP_VXLAN4:
108 s = format (s, "encapped by VXLAN4 intf: %d", map->sw_if_index);
111 case NSH_NODE_NEXT_ENCAP_VXLAN6:
113 s = format (s, "encapped by VXLAN6 intf: %d", map->sw_if_index);
116 case NSH_NODE_NEXT_DECAP_ETH_INPUT:
118 s = format (s, "encap-none");
121 case NSH_NODE_NEXT_ENCAP_LISP_GPE:
123 s = format (s, "encapped by LISP GPE intf: %d", map->sw_if_index);
126 case NSH_NODE_NEXT_ENCAP_ETHERNET:
128 s = format (s, "encapped by Ethernet intf: %d", map->sw_if_index);
132 s = format (s, "only GRE and VXLANGPE support in this rev");
139 nsh_get_adj_by_sw_if_index (u32 sw_if_index)
144 pool_foreach_index (ai, adj_pool)
146 if (sw_if_index == adj_get_sw_if_index(ai))
158 * CLI command for NSH map
160 static clib_error_t *
161 nsh_add_del_map_command_fn (vlib_main_t * vm,
162 unformat_input_t * input,
163 vlib_cli_command_t * cmd)
165 unformat_input_t _line_input, *line_input = &_line_input;
167 u32 nsp, nsi, mapped_nsp, mapped_nsi, nsh_action;
168 int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
169 int nsh_action_set = 0;
172 u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
173 u32 rx_sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
174 nsh_add_del_map_args_t _a, *a = &_a;
178 /* Get a line of input. */
179 if (!unformat_user (input, unformat_line_input, line_input))
182 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
184 if (unformat (line_input, "del"))
186 else if (unformat (line_input, "nsp %d", &nsp))
188 else if (unformat (line_input, "nsi %d", &nsi))
190 else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
192 else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
194 else if (unformat (line_input, "nsh_action %U", unformat_nsh_action,
197 else if (unformat (line_input, "encap-gre4-intf %d", &sw_if_index))
198 next_node = NSH_NODE_NEXT_ENCAP_GRE4;
199 else if (unformat (line_input, "encap-gre6-intf %d", &sw_if_index))
200 next_node = NSH_NODE_NEXT_ENCAP_GRE6;
201 else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
202 next_node = NSH_NODE_NEXT_ENCAP_VXLANGPE;
203 else if (unformat (line_input, "encap-lisp-gpe-intf %d", &sw_if_index))
204 next_node = NSH_NODE_NEXT_ENCAP_LISP_GPE;
205 else if (unformat (line_input, "encap-vxlan4-intf %d", &sw_if_index))
206 next_node = NSH_NODE_NEXT_ENCAP_VXLAN4;
207 else if (unformat (line_input, "encap-vxlan6-intf %d", &sw_if_index))
208 next_node = NSH_NODE_NEXT_ENCAP_VXLAN6;
209 else if (unformat (line_input, "encap-eth-intf %d", &sw_if_index))
211 next_node = NSH_NODE_NEXT_ENCAP_ETHERNET;
212 adj_index = nsh_get_adj_by_sw_if_index (sw_if_index);
216 (line_input, "encap-none %d %d", &sw_if_index, &rx_sw_if_index))
217 next_node = NSH_NODE_NEXT_DECAP_ETH_INPUT;
219 return clib_error_return (0, "parse error: '%U'",
220 format_unformat_error, line_input);
223 unformat_free (line_input);
225 if (nsp_set == 0 || nsi_set == 0)
226 return clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
228 if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
229 return clib_error_return (0,
230 "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
232 if (nsh_action_set == 0)
233 return clib_error_return (0, "nsh_action required: swap|push|pop.");
236 return clib_error_return (0,
237 "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-lisp-gpe-intf <nn> | encap-none <tx_sw_if_index> <rx_sw_if_index>]");
239 clib_memset (a, 0, sizeof (*a));
241 /* set args structure */
243 a->map.nsp_nsi = (nsp << NSH_NSP_SHIFT) | nsi;
244 a->map.mapped_nsp_nsi = (mapped_nsp << NSH_NSP_SHIFT) | mapped_nsi;
245 a->map.nsh_action = nsh_action;
246 a->map.sw_if_index = sw_if_index;
247 a->map.rx_sw_if_index = rx_sw_if_index;
248 a->map.next_node = next_node;
249 a->map.adj_index = adj_index;
251 rv = nsh_add_del_map (a, &map_index);
257 case -1: //TODO API_ERROR_INVALID_VALUE:
258 return clib_error_return (0,
259 "mapping already exists. Remove it first.");
261 case -2: // TODO API_ERROR_NO_SUCH_ENTRY:
262 return clib_error_return (0, "mapping does not exist.");
265 return clib_error_return (0, "nsh_add_del_map returned %d", rv);
268 if ((a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN4)
269 | (a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN6))
271 rv = nsh_add_del_proxy_session (a);
277 case -1: //TODO API_ERROR_INVALID_VALUE:
278 return clib_error_return (0,
279 "nsh-proxy-session already exists. Remove it first.");
281 case -2: // TODO API_ERROR_NO_SUCH_ENTRY:
282 return clib_error_return (0, "nsh-proxy-session does not exist.");
285 return clib_error_return
286 (0, "nsh_add_del_proxy_session() returned %d", rv);
294 VLIB_CLI_COMMAND (create_nsh_map_command, static) = {
295 .path = "create nsh map",
297 "create nsh map nsp <nn> nsi <nn> [del] mapped-nsp <nn> mapped-nsi <nn> nsh_action [swap|push|pop] "
298 "[encap-gre4-intf <nn> | encap-gre4-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-lisp-gpe-intf <nn> "
299 " encap-vxlan4-intf <nn> | encap-vxlan6-intf <nn>| encap-eth-intf <nn> | encap-none]\n",
300 .function = nsh_add_del_map_command_fn,
305 * CLI command for showing the mapping between NSH entries
307 static clib_error_t *
308 show_nsh_map_command_fn (vlib_main_t * vm,
309 unformat_input_t * input, vlib_cli_command_t * cmd)
311 nsh_main_t *nm = &nsh_main;
314 if (pool_elts (nm->nsh_mappings) == 0)
315 vlib_cli_output (vm, "No nsh maps configured.");
317 pool_foreach (map, nm->nsh_mappings)
319 vlib_cli_output (vm, "%U", format_nsh_map, map);
326 VLIB_CLI_COMMAND (show_nsh_map_command, static) = {
327 .path = "show nsh map",
328 .function = show_nsh_map_command_fn,
333 * CLI command for adding NSH entry
335 static clib_error_t *
336 nsh_add_del_entry_command_fn (vlib_main_t * vm,
337 unformat_input_t * input,
338 vlib_cli_command_t * cmd)
340 unformat_input_t _line_input, *line_input = &_line_input;
346 u8 next_protocol = 1; /* default: ip4 */
357 nsh_tlv_header_t tlv_header;
358 u8 cur_len = 0, tlvs_len = 0;
360 nsh_main_t *nm = &nsh_main;
361 nsh_option_map_t _nsh_option, *nsh_option = &_nsh_option;
366 nsh_add_del_entry_args_t _a, *a = &_a;
367 u8 has_ioam_trace_option = 0;
369 /* Get a line of input. */
370 if (!unformat_user (input, unformat_line_input, line_input))
373 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
375 if (unformat (line_input, "del"))
377 else if (unformat (line_input, "version %d", &tmp))
378 ver_o_c |= (tmp & 3) << 6;
379 else if (unformat (line_input, "o-bit %d", &tmp))
380 ver_o_c |= (tmp & 1) << 5;
381 else if (unformat (line_input, "c-bit %d", &tmp))
382 ver_o_c |= (tmp & 1) << 4;
383 else if (unformat (line_input, "ttl %d", &ttl))
384 ver_o_c |= (ttl & NSH_LEN_MASK) >> 2;
385 else if (unformat (line_input, "md-type %d", &tmp))
387 else if (unformat (line_input, "next-ip4"))
389 else if (unformat (line_input, "next-ip6"))
391 else if (unformat (line_input, "next-ethernet"))
393 else if (unformat (line_input, "c1 %d", &c1))
395 else if (unformat (line_input, "c2 %d", &c2))
397 else if (unformat (line_input, "c3 %d", &c3))
399 else if (unformat (line_input, "c4 %d", &c4))
401 else if (unformat (line_input, "nsp %d", &nsp))
403 else if (unformat (line_input, "nsi %d", &nsi))
405 else if (unformat (line_input, "tlv-ioam-trace"))
406 has_ioam_trace_option = 1;
408 return clib_error_return (0, "parse error: '%U'",
409 format_unformat_error, line_input);
412 unformat_free (line_input);
415 return clib_error_return (0, "nsp not specified");
418 return clib_error_return (0, "nsi not specified");
420 if (md_type == 1 && has_ioam_trace_option == 1)
421 return clib_error_return (0, "Invalid MD Type");
423 nsp_nsi = (nsp << 8) | nsi;
425 clib_memset (a, 0, sizeof (*a));
430 a->nsh_entry.md.md1_data.c1 = c1;
431 a->nsh_entry.md.md1_data.c2 = c2;
432 a->nsh_entry.md.md1_data.c3 = c3;
433 a->nsh_entry.md.md1_data.c4 = c4;
434 length = (sizeof (nsh_base_header_t) + sizeof (nsh_md1_data_t)) >> 2;
436 else if (md_type == 2)
438 length = sizeof (nsh_base_header_t) >> 2;
440 vec_free (a->nsh_entry.tlvs_data);
441 tlvs_len = (MAX_METADATA_LEN << 2);
442 vec_validate_aligned (data, tlvs_len - 1, CLIB_CACHE_LINE_BYTES);
443 a->nsh_entry.tlvs_data = data;
446 if (has_ioam_trace_option)
448 tlv_header.class = clib_host_to_net_u16 (NSH_MD2_IOAM_CLASS);
449 tlv_header.type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
450 /* Uses network order's class and type to lookup */
452 nsh_md2_lookup_option (tlv_header.class, tlv_header.type);
453 if (nsh_option == NULL)
454 return clib_error_return (0, "iOAM Trace not registered");
456 if (nm->add_options[nsh_option->option_id] != NULL)
458 if (0 != nm->add_options[nsh_option->option_id] ((u8 *) current,
461 return clib_error_return (0, "Invalid MD Type");
465 nm->options_size[nsh_option->option_id] = option_size;
466 /* round to 4-byte */
467 option_size = (((option_size + 3) >> 2) << 2);
469 cur_len += option_size;
470 current = data + option_size;
473 /* Add more options' parsing */
475 a->nsh_entry.tlvs_len = cur_len;
476 length += (cur_len >> 2);
478 length = (length & NSH_LEN_MASK) | ((ttl & 0x3) << 6);
480 #define _(x) a->nsh_entry.nsh_base.x = x;
481 foreach_copy_nsh_base_hdr_field;
484 rv = nsh_add_del_entry (a, &entry_index);
491 return clib_error_return (0, "nsh_add_del_entry returned %d", rv);
498 VLIB_CLI_COMMAND (create_nsh_entry_command, static) = {
499 .path = "create nsh entry",
501 "create nsh entry {nsp <nn> nsi <nn>} [ttl <nn>] [md-type <nn>]"
502 " [c1 <nn> c2 <nn> c3 <nn> c4 <nn>] [tlv-ioam-trace] [del]\n",
503 .function = nsh_add_del_entry_command_fn,
507 /* format from network order */
509 format_nsh_header (u8 * s, va_list * args)
511 nsh_main_t *nm = &nsh_main;
512 nsh_md2_data_t *opt0;
513 nsh_md2_data_t *limit0;
514 nsh_option_map_t *nsh_option;
517 u8 *header = va_arg (*args, u8 *);
518 nsh_base_header_t *nsh_base = (nsh_base_header_t *) header;
519 nsh_md1_data_t *nsh_md1 = (nsh_md1_data_t *) (nsh_base + 1);
520 nsh_md2_data_t *nsh_md2 = (nsh_md2_data_t *) (nsh_base + 1);
521 opt0 = (nsh_md2_data_t *) nsh_md2;
522 limit0 = (nsh_md2_data_t *) ((u8 *) nsh_md2 +
523 ((nsh_base->length & NSH_LEN_MASK) * 4
524 - sizeof (nsh_base_header_t)));
526 s = format (s, "nsh ver %d ", (nsh_base->ver_o_c >> 6));
527 if (nsh_base->ver_o_c & NSH_O_BIT)
528 s = format (s, "O-set ");
530 if (nsh_base->ver_o_c & NSH_C_BIT)
531 s = format (s, "C-set ");
533 s = format (s, "ttl %d ", (nsh_base->ver_o_c & NSH_TTL_H4_MASK) << 2 |
534 (nsh_base->length & NSH_TTL_L2_MASK) >> 6);
536 s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
537 (nsh_base->length & NSH_LEN_MASK),
538 (nsh_base->length & NSH_LEN_MASK) * 4,
539 nsh_base->md_type, nsh_base->next_protocol);
541 s = format (s, " service path %d service index %d\n",
542 (clib_net_to_host_u32 (nsh_base->nsp_nsi) >> NSH_NSP_SHIFT) &
544 clib_net_to_host_u32 (nsh_base->nsp_nsi) & NSH_NSI_MASK);
546 if (nsh_base->md_type == 1)
548 s = format (s, " c1 %d c2 %d c3 %d c4 %d\n",
549 clib_net_to_host_u32 (nsh_md1->c1),
550 clib_net_to_host_u32 (nsh_md1->c2),
551 clib_net_to_host_u32 (nsh_md1->c3),
552 clib_net_to_host_u32 (nsh_md1->c4));
554 else if (nsh_base->md_type == 2)
556 s = format (s, " Supported TLVs: \n");
558 /* Scan the set of variable metadata, network order */
559 while (opt0 < limit0)
561 nsh_option = nsh_md2_lookup_option (opt0->class, opt0->type);
562 if (nsh_option != NULL)
564 if (nm->trace[nsh_option->option_id] != NULL)
566 s = (*nm->trace[nsh_option->option_id]) (s, opt0);
571 format (s, "\n untraced option %d length %d",
572 opt0->type, opt0->length);
578 format (s, "\n unrecognized option %d length %d",
579 opt0->type, opt0->length);
582 /* round to 4-byte */
583 option_len = ((opt0->length + 3) >> 2) << 2;
585 (nsh_md2_data_t *) (((u8 *) opt0) + sizeof (nsh_md2_data_t) +
594 format_nsh_node_map_trace (u8 * s, va_list * args)
596 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
597 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
598 nsh_input_trace_t *t = va_arg (*args, nsh_input_trace_t *);
600 s = format (s, "\n %U", format_nsh_header, &(t->trace_data));
605 static clib_error_t *
606 show_nsh_entry_command_fn (vlib_main_t * vm,
607 unformat_input_t * input, vlib_cli_command_t * cmd)
609 nsh_main_t *nm = &nsh_main;
610 nsh_entry_t *nsh_entry;
612 if (pool_elts (nm->nsh_entries) == 0)
613 vlib_cli_output (vm, "No nsh entries configured.");
615 pool_foreach (nsh_entry, nm->nsh_entries)
617 vlib_cli_output (vm, "%U", format_nsh_header, nsh_entry->rewrite);
618 vlib_cli_output (vm, " rewrite_size: %d bytes", nsh_entry->rewrite_size);
625 VLIB_CLI_COMMAND (show_nsh_entry_command, static) = {
626 .path = "show nsh entry",
627 .function = show_nsh_entry_command_fn,
632 * fd.io coding-style-patch-verification: ON
635 * eval: (c-set-style "gnu")