/* * nsh_cli.c - nsh cli functions * * Copyright (c) 2019 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include /* format from network order */ u8 * format_nsh_pop_header (u8 * s, va_list * args) { return format_nsh_header (s, args); } u8 * format_nsh_pop_node_map_trace (u8 * s, va_list * args) { return format_nsh_node_map_trace (s, args); } static uword unformat_nsh_action (unformat_input_t * input, va_list * args) { u32 *result = va_arg (*args, u32 *); u32 tmp; if (unformat (input, "swap")) *result = NSH_ACTION_SWAP; else if (unformat (input, "push")) *result = NSH_ACTION_PUSH; else if (unformat (input, "pop")) *result = NSH_ACTION_POP; else if (unformat (input, "%d", &tmp)) *result = tmp; else return 0; return 1; } static u8 * format_nsh_action (u8 * s, va_list * args) { u32 nsh_action = va_arg (*args, u32); switch (nsh_action) { case NSH_ACTION_SWAP: return format (s, "swap"); case NSH_ACTION_PUSH: return format (s, "push"); case NSH_ACTION_POP: return format (s, "pop"); default: return format (s, "unknown %d", nsh_action); } return s; } u8 * format_nsh_map (u8 * s, va_list * args) { nsh_map_t *map = va_arg (*args, nsh_map_t *); s = format (s, "nsh entry nsp: %d nsi: %d ", (map->nsp_nsi >> NSH_NSP_SHIFT) & NSH_NSP_MASK, map->nsp_nsi & NSH_NSI_MASK); s = format (s, "maps to nsp: %d nsi: %d ", (map->mapped_nsp_nsi >> NSH_NSP_SHIFT) & NSH_NSP_MASK, map->mapped_nsp_nsi & NSH_NSI_MASK); s = format (s, " nsh_action %U\n", format_nsh_action, map->nsh_action); switch (map->next_node) { case NSH_NODE_NEXT_ENCAP_GRE4: { s = format (s, "encapped by GRE4 intf: %d", map->sw_if_index); break; } case NSH_NODE_NEXT_ENCAP_GRE6: { s = format (s, "encapped by GRE6 intf: %d", map->sw_if_index); break; } case NSH_NODE_NEXT_ENCAP_VXLANGPE: { s = format (s, "encapped by VXLAN GPE intf: %d", map->sw_if_index); break; } case NSH_NODE_NEXT_ENCAP_VXLAN4: { s = format (s, "encapped by VXLAN4 intf: %d", map->sw_if_index); break; } case NSH_NODE_NEXT_ENCAP_VXLAN6: { s = format (s, "encapped by VXLAN6 intf: %d", map->sw_if_index); break; } case NSH_NODE_NEXT_DECAP_ETH_INPUT: { s = format (s, "encap-none"); break; } case NSH_NODE_NEXT_ENCAP_LISP_GPE: { s = format (s, "encapped by LISP GPE intf: %d", map->sw_if_index); break; } case NSH_NODE_NEXT_ENCAP_ETHERNET: { s = format (s, "encapped by Ethernet intf: %d", map->sw_if_index); break; } default: s = format (s, "only GRE and VXLANGPE support in this rev"); } return s; } static adj_index_t nsh_get_adj_by_sw_if_index (u32 sw_if_index) { adj_index_t ai = ~0; /* *INDENT-OFF* */ pool_foreach_index(ai, adj_pool, ({ if (sw_if_index == adj_get_sw_if_index(ai)) { return ai; } })); /* *INDENT-ON* */ return ~0; } /** * CLI command for NSH map */ static clib_error_t * nsh_add_del_map_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; u8 is_add = 1; u32 nsp, nsi, mapped_nsp, mapped_nsi, nsh_action; int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0; int nsh_action_set = 0; u32 next_node = ~0; u32 adj_index = ~0; u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC u32 rx_sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC nsh_add_del_map_args_t _a, *a = &_a; u32 map_index; int rv; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "del")) is_add = 0; else if (unformat (line_input, "nsp %d", &nsp)) nsp_set = 1; else if (unformat (line_input, "nsi %d", &nsi)) nsi_set = 1; else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp)) mapped_nsp_set = 1; else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi)) mapped_nsi_set = 1; else if (unformat (line_input, "nsh_action %U", unformat_nsh_action, &nsh_action)) nsh_action_set = 1; else if (unformat (line_input, "encap-gre4-intf %d", &sw_if_index)) next_node = NSH_NODE_NEXT_ENCAP_GRE4; else if (unformat (line_input, "encap-gre6-intf %d", &sw_if_index)) next_node = NSH_NODE_NEXT_ENCAP_GRE6; else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index)) next_node = NSH_NODE_NEXT_ENCAP_VXLANGPE; else if (unformat (line_input, "encap-lisp-gpe-intf %d", &sw_if_index)) next_node = NSH_NODE_NEXT_ENCAP_LISP_GPE; else if (unformat (line_input, "encap-vxlan4-intf %d", &sw_if_index)) next_node = NSH_NODE_NEXT_ENCAP_VXLAN4; else if (unformat (line_input, "encap-vxlan6-intf %d", &sw_if_index)) next_node = NSH_NODE_NEXT_ENCAP_VXLAN6; else if (unformat (line_input, "encap-eth-intf %d", &sw_if_index)) { next_node = NSH_NODE_NEXT_ENCAP_ETHERNET; adj_index = nsh_get_adj_by_sw_if_index (sw_if_index); } else if (unformat (line_input, "encap-none %d %d", &sw_if_index, &rx_sw_if_index)) next_node = NSH_NODE_NEXT_DECAP_ETH_INPUT; else return clib_error_return (0, "parse error: '%U'", format_unformat_error, line_input); } unformat_free (line_input); if (nsp_set == 0 || nsi_set == 0) return clib_error_return (0, "nsp nsi pair required. Key: for NSH entry"); if (mapped_nsp_set == 0 || mapped_nsi_set == 0) return clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry"); if (nsh_action_set == 0) return clib_error_return (0, "nsh_action required: swap|push|pop."); if (next_node == ~0) return clib_error_return (0, "must specific action: [encap-gre-intf | encap-vxlan-gpe-intf | encap-lisp-gpe-intf | encap-none ]"); clib_memset (a, 0, sizeof (*a)); /* set args structure */ a->is_add = is_add; a->map.nsp_nsi = (nsp << NSH_NSP_SHIFT) | nsi; a->map.mapped_nsp_nsi = (mapped_nsp << NSH_NSP_SHIFT) | mapped_nsi; a->map.nsh_action = nsh_action; a->map.sw_if_index = sw_if_index; a->map.rx_sw_if_index = rx_sw_if_index; a->map.next_node = next_node; a->map.adj_index = adj_index; rv = nsh_add_del_map (a, &map_index); switch (rv) { case 0: break; case -1: //TODO API_ERROR_INVALID_VALUE: return clib_error_return (0, "mapping already exists. Remove it first."); case -2: // TODO API_ERROR_NO_SUCH_ENTRY: return clib_error_return (0, "mapping does not exist."); default: return clib_error_return (0, "nsh_add_del_map returned %d", rv); } if ((a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN4) | (a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN6)) { rv = nsh_add_del_proxy_session (a); switch (rv) { case 0: break; case -1: //TODO API_ERROR_INVALID_VALUE: return clib_error_return (0, "nsh-proxy-session already exists. Remove it first."); case -2: // TODO API_ERROR_NO_SUCH_ENTRY: return clib_error_return (0, "nsh-proxy-session does not exist."); default: return clib_error_return (0, "nsh_add_del_proxy_session() returned %d", rv); } } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (create_nsh_map_command, static) = { .path = "create nsh map", .short_help = "create nsh map nsp nsi [del] mapped-nsp mapped-nsi nsh_action [swap|push|pop] " "[encap-gre4-intf | encap-gre4-intf | encap-vxlan-gpe-intf | encap-lisp-gpe-intf " " encap-vxlan4-intf | encap-vxlan6-intf | encap-eth-intf | encap-none]\n", .function = nsh_add_del_map_command_fn, }; /* *INDENT-ON* */ /** * CLI command for showing the mapping between NSH entries */ static clib_error_t * show_nsh_map_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { nsh_main_t *nm = &nsh_main; nsh_map_t *map; if (pool_elts (nm->nsh_mappings) == 0) vlib_cli_output (vm, "No nsh maps configured."); pool_foreach (map, nm->nsh_mappings, ( { vlib_cli_output (vm, "%U", format_nsh_map, map); } )); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_nsh_map_command, static) = { .path = "show nsh map", .function = show_nsh_map_command_fn, }; /* *INDENT-ON* */ /** * CLI command for adding NSH entry */ static clib_error_t * nsh_add_del_entry_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; u8 is_add = 1; u8 ver_o_c = 0; u8 ttl = 63; u8 length = 0; u8 md_type = 0; u8 next_protocol = 1; /* default: ip4 */ u32 nsp; u8 nsp_set = 0; u32 nsi; u8 nsi_set = 0; u32 nsp_nsi; u32 c1 = 0; u32 c2 = 0; u32 c3 = 0; u32 c4 = 0; u8 *data = 0; nsh_tlv_header_t tlv_header; u8 cur_len = 0, tlvs_len = 0; u8 *current; nsh_main_t *nm = &nsh_main; nsh_option_map_t _nsh_option, *nsh_option = &_nsh_option; u8 o