diff options
author | Keith Burns (alagalah) <alagalah@gmail.com> | 2016-05-13 09:31:35 -0700 |
---|---|---|
committer | HÃ¥kan Jonsson <hakan.jonsson@ericsson.com> | 2016-06-15 06:18:31 +0000 |
commit | 1a8e2a697ff5513907a767a327d1e363b789f73a (patch) | |
tree | decb5c2b57eb9618a99ee6926d423f3ef4565784 /nsh-plugin | |
parent | 08157b83782b7bedfd238c580cde5347ab5408a8 (diff) |
NSHSFC-1 Initial move from VPP to NSH_SFC
- API is giving me some negative energy currently
-- I'll work it out but wanted to publish both VPP-61 and this simultaneously
Change-Id: I4a04b56dd470655114605621d0c44ea3c26bc069
Signed-off-by: Keith Burns (alagalah) <alagalah@gmail.com>
Signed-off-by: Ed Warnicke <eaw@cisco.com>
Diffstat (limited to 'nsh-plugin')
-rw-r--r-- | nsh-plugin/.gitignore | 16 | ||||
-rw-r--r-- | nsh-plugin/Makefile.am | 50 | ||||
-rw-r--r-- | nsh-plugin/README-build | 6 | ||||
-rwxr-xr-x | nsh-plugin/build.sh | 7 | ||||
-rw-r--r-- | nsh-plugin/configure.ac | 18 | ||||
-rw-r--r-- | nsh-plugin/nsh/nsh.api | 75 | ||||
-rw-r--r-- | nsh-plugin/nsh/nsh.c | 895 | ||||
-rw-r--r-- | nsh-plugin/nsh/nsh.h | 130 | ||||
-rw-r--r-- | nsh-plugin/nsh/nsh_all_api_h.h | 19 | ||||
-rw-r--r-- | nsh-plugin/nsh/nsh_error.def | 17 | ||||
-rw-r--r-- | nsh-plugin/nsh/nsh_msg_enum.h | 31 | ||||
-rw-r--r-- | nsh-plugin/nsh/nsh_packet.h | 93 | ||||
-rw-r--r-- | nsh-plugin/nsh/nsh_test.c | 330 |
13 files changed, 1687 insertions, 0 deletions
diff --git a/nsh-plugin/.gitignore b/nsh-plugin/.gitignore new file mode 100644 index 0000000..7fc8961 --- /dev/null +++ b/nsh-plugin/.gitignore @@ -0,0 +1,16 @@ +# Build artifacts +aclocal.m4 +autom4te.cache/ +build/ +compile +config.guess +config.sub +configure +depcomp +install-sh +ltmain.sh +Makefile.in +missing + +*~ +cscope* diff --git a/nsh-plugin/Makefile.am b/nsh-plugin/Makefile.am new file mode 100644 index 0000000..99c607e --- /dev/null +++ b/nsh-plugin/Makefile.am @@ -0,0 +1,50 @@ + +# Copyright (c) <current-year> <your-organization> +# 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. + +AUTOMAKE_OPTIONS = foreign subdir-objects + +AM_CFLAGS = -Wall -I@TOOLKIT_INCLUDE@ + +lib_LTLIBRARIES = nsh_plugin.la nsh_test_plugin.la +nsh_plugin_la_SOURCES = nsh/nsh.c \ + nsh/nsh_plugin.api.h +nsh_plugin_la_LDFLAGS = -module + +BUILT_SOURCES = nsh/nsh.api.h + +SUFFIXES = .api.h .api + +%.api.h: %.api + mkdir -p `dirname $@` ; \ + $(CC) $(CPPFLAGS) -E -P -C -x c $^ \ + | vppapigen --input - --output $@ --show-name $@ + +nobase_include_HEADERS = \ + nsh/nsh_all_api_h.h \ + nsh/nsh_msg_enum.h \ + nsh/nsh.api.h + +nsh_test_plugin_la_SOURCES = \ + nsh/nsh_test.c nsh/nsh_plugin.api.h +nsh_test_plugin_la_LDFLAGS = -module + +install-data-hook: + mkdir $(prefix)/lib/vpp_plugins || true + mkdir $(prefix)/lib/vpp_api_test_plugins || true + cp $(prefix)/lib/nsh_plugin.so.*.*.* $(prefix)/lib/vpp_plugins + cp $(prefix)/lib/nsh_test_plugin.so.*.*.* \ + $(prefix)/lib/vpp_api_test_plugins + rm -f $(prefix)/lib/nsh_plugin.* + rm -f $(prefix)/lib/nsh_test_plugin.* + diff --git a/nsh-plugin/README-build b/nsh-plugin/README-build new file mode 100644 index 0000000..f67cb90 --- /dev/null +++ b/nsh-plugin/README-build @@ -0,0 +1,6 @@ +autoreconf -i -f +mkdir build +cd build +../configure --with-plugin-toolkit +make +sudo make install diff --git a/nsh-plugin/build.sh b/nsh-plugin/build.sh new file mode 100755 index 0000000..3aa0a39 --- /dev/null +++ b/nsh-plugin/build.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +autoreconf -i -f +[ -d build ] || mkdir build +cd build +../configure --with-plugin-toolkit +make +sudo make install diff --git a/nsh-plugin/configure.ac b/nsh-plugin/configure.ac new file mode 100644 index 0000000..46e8faf --- /dev/null +++ b/nsh-plugin/configure.ac @@ -0,0 +1,18 @@ + +AC_INIT(nsh_plugin, 1.0) +AM_INIT_AUTOMAKE + +AC_PROG_LIBTOOL +AM_PROG_AS +AC_PROG_CC +AM_PROG_CC_C_O + +AC_ARG_WITH(plugin-toolkit, + AC_HELP_STRING([--with-plugin-toolkit], + [build using the vpp toolkit]), + [with_plugin_toolkit=${prefix}/include], + [with_plugin_toolkit=.]) + +AC_SUBST(TOOLKIT_INCLUDE,[${with_plugin_toolkit}]) +AM_CONDITIONAL(WITH_PLUGIN_TOOLKIT, test "$with_plugin_toolkit" != ".") +AC_OUTPUT([Makefile]) diff --git a/nsh-plugin/nsh/nsh.api b/nsh-plugin/nsh/nsh.api new file mode 100644 index 0000000..67d04e2 --- /dev/null +++ b/nsh-plugin/nsh/nsh.api @@ -0,0 +1,75 @@ + + +/** \brief /** \brief Set or delete an NSH header entry keyed by NSP/NSI + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param is_add - add address if non-zero, else delete + @param nsp_nsi - Key for nsh_header_t entry to map to. : 24bit NSP 8bit NSI + @param md_type - metadata type [1|2] - only MDType1 supported today + @param ver_o_c - version, O-bit and C-bit (see nsh_packet.h) + @param length - header length in n x 32bits, should be 6 for MDtype1 + @param next_protocol - next protocol encapsulated behind NSH header: 1=Ethernet, 2=IP4, 3=IP6 + @param c1 - 32bit Metadata type1 field (context1) + @param c2 - 32bit Metadata type1 field (context2) + @param c3 - 32bit Metadata type1 field (context3) + @param c4 - 32bit Metadata type1 field (context4) + @param tlvs - Metadata Type 2 only, Type Length Value metadata. Not currently supported +*/ +define nsh_add_del_entry { + u32 client_index; + u32 context; + u8 is_add; + u32 nsp_nsi; + u8 md_type; + u8 ver_o_c; + u8 length; + u8 next_protocol; + u32 c1; + u32 c2; + u32 c3; + u32 c4; + u32 tlvs[0]; +}; + +/** \brief Reply from adding NSH entry (nsh_add_del_entry) + @param context - sender context, to match reply w/ request + @param retval - 0 means all ok +*/ +define nsh_add_del_entry_reply { + u32 context; + i32 retval; +}; + +/** \brief Set or delete a mapping from one NSH header to another and its egress (decap to inner packet, encap NSH with outer header) + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param is_add - add address if non-zero, else delete + @param nsh_nsi - Key for nsh_header_t entry to map to. : 24bit NSP 8bit NSI + @param mapped_nsp_nsi - Key for nsh_header_t entry to map to. : 24bit NSP 8bit NSI this may be ~0 + if next action is to decap to NSH next protocol + Note the following heuristic: + - if nsp_nsi == mapped_nsp_nsi then use-case is like SFC SFF + - if nsp_nsi != mapped_nsp_nsi then use-case is like SFC SF + Note: these are heuristics. Rules about NSI decrement are out of scope + @param sw_if_index - index number of outer encap for NSH egress + @param next_node - explicitly which node to send to + Note the above parameters are instantiated by "encap-gre-intf <x>" means sw_if_index x, next_node gre-input +*/ +define nsh_add_del_map { + u32 client_index; + u32 context; + u8 is_add; + u32 nsp_nsi; + u32 mapped_nsp_nsi; + u32 sw_if_index; + u32 next_node; +}; + +/** \brief Reply from adding NSH map (nsh_add_del_map) + @param context - sender context, to match reply w/ request + @param retval - 0 means all ok +*/ +define nsh_add_del_map_reply { + u32 context; + i32 retval; +};
\ No newline at end of file diff --git a/nsh-plugin/nsh/nsh.c b/nsh-plugin/nsh/nsh.c new file mode 100644 index 0000000..e6bc761 --- /dev/null +++ b/nsh-plugin/nsh/nsh.c @@ -0,0 +1,895 @@ +/* + * nsh.c - nsh mapping + * + * Copyright (c) 2013 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 <vnet/vnet.h> +#include <vnet/plugin/plugin.h> +#include <nsh/nsh.h> + +#include <vlibapi/api.h> +#include <vlibmemory/api.h> +#include <vlibsocket/api.h> + +/* define message IDs */ +#include <nsh/nsh_msg_enum.h> + +/* define message structures */ +#define vl_typedefs +#include <nsh/nsh_all_api_h.h> +#undef vl_typedefs + +/* define generated endian-swappers */ +#define vl_endianfun +#include <nsh/nsh_all_api_h.h> +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define vl_printfun +#include <nsh/nsh_all_api_h.h> +#undef vl_printfun + +/* Get the API version number */ +#define vl_api_version(n,v) static u32 api_version=(v); +#include <nsh/nsh_all_api_h.h> +#undef vl_api_version + +/* + * A handy macro to set up a message reply. + * Assumes that the following variables are available: + * mp - pointer to request message + * rmp - pointer to reply message type + * rv - return value + */ + +#define REPLY_MACRO(t) \ +do { \ + unix_shared_memory_queue_t * q = \ + vl_api_client_index_to_input_queue (mp->client_index); \ + if (!q) \ + return; \ + \ + rmp = vl_msg_api_alloc (sizeof (*rmp)); \ + rmp->_vl_msg_id = ntohs((t)+nm->msg_id_base); \ + rmp->context = mp->context; \ + rmp->retval = ntohl(rv); \ + \ + vl_msg_api_send_shmem (q, (u8 *)&rmp); \ +} while(0); + +/* List of message types that this plugin understands */ + +#define foreach_nsh_plugin_api_msg \ +_(NSH_ADD_DEL_ENTRY, nsh_add_del_entry) \ +_(NSH_ADD_DEL_MAP, nsh_add_del_map) + +clib_error_t * +vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h, + int from_early_init) +{ + nsh_main_t * nm = &nsh_main; + clib_error_t * error = 0; + + nm->vlib_main = vm; + nm->vnet_main = h->vnet_main; + + return error; +} + +typedef struct { + nsh_header_t nsh_header; +} nsh_input_trace_t; + +u8 * format_nsh_header (u8 * s, va_list * args) +{ + nsh_header_t * nsh = va_arg (*args, nsh_header_t *); + + s = format (s, "nsh ver %d ", (nsh->ver_o_c>>6)); + if (nsh->ver_o_c & NSH_O_BIT) + s = format (s, "O-set "); + + if (nsh->ver_o_c & NSH_C_BIT) + s = format (s, "C-set "); + + s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n", + nsh->length, nsh->length * 4, nsh->md_type, nsh->next_protocol); + + s = format (s, " service path %d service index %d\n", + (nsh->nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK, + nsh->nsp_nsi & NSH_NSI_MASK); + + s = format (s, " c1 %d c2 %d c3 %d c4 %d\n", + nsh->c1, nsh->c2, nsh->c3, nsh->c4); + + 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); + + switch (map->next_node) + { + case NSH_INPUT_NEXT_ENCAP_GRE: + { + s = format (s, "encapped by GRE intf: %d", map->sw_if_index); + break; + } + case NSH_INPUT_NEXT_ENCAP_VXLANGPE: + { + s = format (s, "encapped by VXLAN GPE intf: %d", map->sw_if_index); + break; + } + default: + s = format (s, "only GRE and VXLANGPE support in this rev"); + } + + return s; +} + +u8 * format_nsh_header_with_length (u8 * s, va_list * args) +{ + nsh_header_t * h = va_arg (*args, nsh_header_t *); + u32 max_header_bytes = va_arg (*args, u32); + u32 tmp, header_bytes; + + header_bytes = sizeof (h[0]); + if (max_header_bytes != 0 && header_bytes > max_header_bytes) + return format (s, "nsh header truncated"); + + tmp = clib_net_to_host_u32 (h->nsp_nsi); + s = format (s, " nsp %d nsi %d ", + (tmp>>NSH_NSP_SHIFT) & NSH_NSP_MASK, + tmp & NSH_NSI_MASK); + + s = format (s, "c1 %u c2 %u c3 %u c4 %u", + clib_net_to_host_u32 (h->c1), + clib_net_to_host_u32 (h->c2), + clib_net_to_host_u32 (h->c3), + clib_net_to_host_u32 (h->c4)); + + s = format (s, "ver %d ", h->ver_o_c>>6); + + if (h->ver_o_c & NSH_O_BIT) + s = format (s, "O-set "); + + if (h->ver_o_c & NSH_C_BIT) + s = format (s, "C-set "); + + s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n", + h->length, h->length * 4, h->md_type, h->next_protocol); + return s; +} + +u8 * format_nsh_input_map_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + nsh_input_trace_t * t + = va_arg (*args, nsh_input_trace_t *); + + s = format (s, "\n %U", format_nsh_header, &t->nsh_header, + (u32) sizeof (t->nsh_header) ); + + return s; +} + +/** + * Action function to add or del an nsh map. + * Shared by both CLI and binary API + **/ + +int nsh_add_del_map (nsh_add_del_map_args_t *a) +{ + nsh_main_t * nm = &nsh_main; + nsh_map_t *map = 0; + u32 key, *key_copy; + uword * entry; + hash_pair_t *hp; + + key = a->map.nsp_nsi; + + entry = hash_get_mem (nm->nsh_mapping_by_key, &key); + + if (a->is_add) + { + /* adding an entry, must not already exist */ + if (entry) + return -1; //TODO API_ERROR_INVALID_VALUE; + + pool_get_aligned (nm->nsh_mappings, map, CLIB_CACHE_LINE_BYTES); + memset (map, 0, sizeof (*map)); + + /* copy from arg structure */ + map->nsp_nsi = a->map.nsp_nsi; + map->mapped_nsp_nsi = a->map.mapped_nsp_nsi; + map->sw_if_index = a->map.sw_if_index; + map->next_node = a->map.next_node; + + + key_copy = clib_mem_alloc (sizeof (*key_copy)); + clib_memcpy (key_copy, &key, sizeof (*key_copy)); + + hash_set_mem (nm->nsh_mapping_by_key, key_copy, + map - nm->nsh_mappings); + } + else + { + if (!entry) + return -2 ; //TODO API_ERROR_NO_SUCH_ENTRY; + + map = pool_elt_at_index (nm->nsh_mappings, entry[0]); + hp = hash_get_pair (nm->nsh_mapping_by_key, &key); + key_copy = (void *)(hp->key); + hash_unset_mem (nm->nsh_mapping_by_key, &key); + clib_mem_free (key_copy); + + pool_put (nm->nsh_mappings, map); + } + + 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; + int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0; + u32 next_node = ~0; + u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC + nsh_add_del_map_args_t _a, * a = &_a; + 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, "encap-gre-intf %d", &sw_if_index)) + next_node = NSH_INPUT_NEXT_ENCAP_GRE; + else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index)) + next_node = NSH_INPUT_NEXT_ENCAP_VXLANGPE; + else if (unformat (line_input, "encap-none")) + next_node = NSH_INPUT_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case + 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 (next_node == ~0) + return clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]"); + + 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.sw_if_index = sw_if_index; + a->map.next_node = next_node; + + + rv = nsh_add_del_map (a); + + 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); + } + return 0; +} + +VLIB_CLI_COMMAND (create_nsh_map_command, static) = { + .path = "create nsh map", + .short_help = + "create nsh map nsp <nn> nsi <nn> [del] map-nsp <nn> map-nsi <nn> [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]\n", + .function = nsh_add_del_map_command_fn, +}; + +/** API message handler */ +static void vl_api_nsh_add_del_map_t_handler +(vl_api_nsh_add_del_map_t * mp) +{ + vl_api_nsh_add_del_map_reply_t * rmp; + nsh_main_t * nm = &nsh_main; + int rv; + nsh_add_del_map_args_t *a = 0; + + rv = nsh_add_del_map (a); + + REPLY_MACRO(VL_API_NSH_ADD_DEL_MAP_REPLY); +} + +/** + * 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; +} + +VLIB_CLI_COMMAND (show_nsh_map_command, static) = { + .path = "show nsh map", + .function = show_nsh_map_command_fn, +}; + +/** + * Action function for adding an NSH entry + */ + +int nsh_add_del_entry (nsh_add_del_entry_args_t *a) +{ + nsh_main_t * nm = &nsh_main; + nsh_header_t *hdr = 0; + u32 key, *key_copy; + uword * entry; + hash_pair_t *hp; + + key = a->nsh.nsp_nsi; + + entry = hash_get_mem (nm->nsh_entry_by_key, &key); + + if (a->is_add) + { + /* adding an entry, must not already exist */ + if (entry) + return -1; // TODO VNET_API_ERROR_INVALID_VALUE; + + pool_get_aligned (nm->nsh_entries, hdr, CLIB_CACHE_LINE_BYTES); + memset (hdr, 0, sizeof (*hdr)); + + /* copy from arg structure */ +#define _(x) hdr->x = a->nsh.x; + foreach_copy_nshhdr_field; +#undef _ + + key_copy = clib_mem_alloc (sizeof (*key_copy)); + clib_memcpy (key_copy, &key, sizeof (*key_copy)); + + hash_set_mem (nm->nsh_entry_by_key, key_copy, + hdr - nm->nsh_entries); + } + else + { + if (!entry) + return -2; //TODO API_ERROR_NO_SUCH_ENTRY; + + hdr = pool_elt_at_index (nm->nsh_entries, entry[0]); + hp = hash_get_pair (nm->nsh_entry_by_key, &key); + key_copy = (void *)(hp->key); + hash_unset_mem (nm->nsh_entry_by_key, &key); + clib_mem_free (key_copy); + + pool_put (nm->nsh_entries, hdr); + } + + return 0; +} + + +/** + * 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 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; + u32 *tlvs = 0; + u32 tmp; + int rv; + nsh_add_del_entry_args_t _a, * a = &_a; + + /* 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, "version %d", &tmp)) + ver_o_c |= (tmp & 3) << 6; + else if (unformat (line_input, "o-bit %d", &tmp)) + ver_o_c |= (tmp & 1) << 5; + else if (unformat (line_input, "c-bit %d", &tmp)) + ver_o_c |= (tmp & 1) << 4; + else if (unformat (line_input, "md-type %d", &tmp)) + md_type = tmp; + else if (unformat(line_input, "next-ip4")) + next_protocol = 1; + else if (unformat(line_input, "next-ip6")) + next_protocol = 2; + else if (unformat(line_input, "next-ethernet")) + next_protocol = 3; + else if (unformat (line_input, "c1 %d", &c1)) + ; + else if (unformat (line_input, "c2 %d", &c2)) + ; + else if (unformat (line_input, "c3 %d", &c3)) + ; + else if (unformat (line_input, "c4 %d", &c4)) + ; + 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, "tlv %x")) + vec_add1 (tlvs, tmp); + else + return clib_error_return (0, "parse error: '%U'", + format_unformat_error, line_input); + } + + unformat_free (line_input); + + if (nsp_set == 0) + return clib_error_return (0, "nsp not specified"); + + if (nsi_set == 0) + return clib_error_return (0, "nsi not specified"); + + if (md_type != 1) + return clib_error_return (0, "md-type 1 only supported at this time"); + + md_type = 1; + length = 6; + + nsp_nsi = (nsp<<8) | nsi; + + memset (a, 0, sizeof (*a)); + + a->is_add = is_add; + +#define _(x) a->nsh.x = x; + foreach_copy_nshhdr_field; +#undef _ + + a->nsh.tlvs[0] = 0 ; // TODO FIXME this shouldn't be set 0 - in NSH_SFC project + + rv = nsh_add_del_entry (a); + + switch(rv) + { + case 0: + break; + default: + return clib_error_return + (0, "nsh_add_del_entry returned %d", rv); + } + + return 0; +} + +VLIB_CLI_COMMAND (create_nsh_entry_command, static) = { + .path = "create nsh entry", + .short_help = + "create nsh entry {nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn>" + " [md-type <nn>] [tlv <xx>] [del]\n", + .function = nsh_add_del_entry_command_fn, +}; + +/** API message handler */ +static void vl_api_nsh_add_del_entry_t_handler +(vl_api_nsh_add_del_entry_t * mp) +{ + vl_api_nsh_add_del_entry_reply_t * rmp; + nsh_main_t * nm = &nsh_main; + int rv; + nsh_add_del_entry_args_t *a = 0; + + rv = nsh_add_del_entry (a); + + REPLY_MACRO(VL_API_NSH_ADD_DEL_ENTRY_REPLY); +} + +static clib_error_t * +show_nsh_entry_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + nsh_main_t * nm = &nsh_main; + nsh_header_t * hdr; + + if (pool_elts (nm->nsh_entries) == 0) + vlib_cli_output (vm, "No nsh entries configured."); + + pool_foreach (hdr, nm->nsh_entries, + ({ + vlib_cli_output (vm, "%U", format_nsh_header, hdr); + })); + + return 0; +} + +VLIB_CLI_COMMAND (show_nsh_entry_command, static) = { + .path = "show nsh entry", + .function = show_nsh_entry_command_fn, +}; + + +/* Set up the API message handling tables */ +static clib_error_t * +nsh_plugin_api_hookup (vlib_main_t *vm) +{ + nsh_main_t * nm = &nsh_main; +#define _(N,n) \ + vl_msg_api_set_handlers((VL_API_##N + nm->msg_id_base), \ + #n, \ + vl_api_##n##_t_handler, \ + vl_noop_handler, \ + vl_api_##n##_t_endian, \ + vl_api_##n##_t_print, \ + sizeof(vl_api_##n##_t), 1); + foreach_nsh_plugin_api_msg; +#undef _ + + return 0; +} + +clib_error_t *nsh_init (vlib_main_t *vm) +{ + nsh_main_t *nm = &nsh_main; + clib_error_t * error = 0; + u8 * name; + + nm->nsh_mapping_by_key + = hash_create_mem (0, sizeof(u32), sizeof (uword)); + + nm->nsh_mapping_by_mapped_key + = hash_create_mem (0, sizeof(u32), sizeof (uword)); + + nm->nsh_entry_by_key + = hash_create_mem (0, sizeof(u32), sizeof (uword)); + + name = format (0, "nsh_%08x%c", api_version, 0); + + + nm->msg_id_base = vl_msg_api_get_msg_ids + ((char *) name, VL_MSG_FIRST_AVAILABLE); + + error = nsh_plugin_api_hookup (vm); + + vec_free(name); + + return error; +} + +VLIB_INIT_FUNCTION(nsh_init); + + + +static uword +nsh_input_map (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ + u32 n_left_from, next_index, * from, * to_next; + nsh_main_t * nm = &nsh_main; + + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, + to_next, n_left_to_next); + + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 bi0, bi1; + vlib_buffer_t * b0, * b1; + u32 next0 = NSH_INPUT_NEXT_DROP, next1 = NSH_INPUT_NEXT_DROP; + uword * entry0, * entry1; + nsh_header_t * hdr0 = 0, * hdr1 = 0; + u32 nsp_nsi0, nsp_nsi1; + u32 error0, error1; + nsh_map_t * map0 = 0, * map1 = 0; + + /* Prefetch next iteration. */ + { + vlib_buffer_t * p2, * p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); + } + + bi0 = from[0]; + bi1 = from[1]; + to_next[0] = bi0; + to_next[1] = bi1; + from += 2; + to_next += 2; + n_left_from -= 2; + n_left_to_next -= 2; + + error0 = 0; + error1 = 0; + + b0 = vlib_get_buffer (vm, bi0); + hdr0 = vlib_buffer_get_current (b0); + nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi); + entry0 = hash_get_mem (nm->nsh_mapping_by_key, &nsp_nsi0); + + b1 = vlib_get_buffer (vm, bi1); + hdr1 = vlib_buffer_get_current (b1); + nsp_nsi1 = clib_net_to_host_u32(hdr1->nsp_nsi); + entry1 = hash_get_mem (nm->nsh_mapping_by_key, &nsp_nsi1); + + if (PREDICT_FALSE(entry0 == 0)) + { + error0 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace0; + } + + if (PREDICT_FALSE(entry1 == 0)) + { + error1 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace1; + } + + /* Entry should point to a mapping ...*/ + map0 = pool_elt_at_index (nm->nsh_mappings, entry0[0]); + map1 = pool_elt_at_index (nm->nsh_mappings, entry1[0]); + + if (PREDICT_FALSE(map0 == 0)) + { + error0 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace0; + } + + if (PREDICT_FALSE(map1 == 0)) + { + error1 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace1; + } + + entry0 = hash_get_mem (nm->nsh_entry_by_key, &map0->mapped_nsp_nsi); + entry1 = hash_get_mem (nm->nsh_entry_by_key, &map1->mapped_nsp_nsi); + + if (PREDICT_FALSE(entry0 == 0)) + { + error0 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace0; + } + if (PREDICT_FALSE(entry1 == 0)) + { + error1 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace1; + } + + hdr0 = pool_elt_at_index (nm->nsh_entries, entry0[0]); + hdr1 = pool_elt_at_index (nm->nsh_entries, entry1[0]); + + /* set up things for next node to transmit ie which node to handle it and where */ + next0 = map0->next_node; + next1 = map1->next_node; + vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index; + vnet_buffer(b1)->sw_if_index[VLIB_TX] = map1->sw_if_index; + + trace0: + b0->error = error0 ? node->errors[error0] : 0; + + if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) + { + nsh_input_trace_t *tr = + vlib_add_trace (vm, node, b0, sizeof (*tr)); + tr->nsh_header = *hdr0; + } + + trace1: + b1->error = error1 ? node->errors[error1] : 0; + + if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) + { + nsh_input_trace_t *tr = + vlib_add_trace (vm, node, b1, sizeof (*tr)); + tr->nsh_header = *hdr1; + } + + + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + + } + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t * b0; + u32 next0 = NSH_INPUT_NEXT_DROP; + uword * entry0; + nsh_header_t * hdr0 = 0; + u32 nsp_nsi0; + u32 error0; + nsh_map_t * map0 = 0; + + next_index = next0; + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + error0 = 0; + + b0 = vlib_get_buffer (vm, bi0); + hdr0 = vlib_buffer_get_current (b0); + nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi); + entry0 = hash_get_mem (nm->nsh_mapping_by_key, &nsp_nsi0); + + if (PREDICT_FALSE(entry0 == 0)) + { + error0 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace00; + } + + /* Entry should point to a mapping ...*/ + map0 = pool_elt_at_index (nm->nsh_mappings, entry0[0]); + + if (PREDICT_FALSE(map0 == 0)) + { + error0 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace00; + } + + entry0 = hash_get_mem (nm->nsh_entry_by_key, &map0->mapped_nsp_nsi); + + if (PREDICT_FALSE(entry0 == 0)) + { + error0 = NSH_INPUT_ERROR_NO_MAPPING; + goto trace00; + } + + hdr0 = pool_elt_at_index (nm->nsh_entries, entry0[0]); + + /* set up things for next node to transmit ie which node to handle it and where */ + next0 = map0->next_node; + vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index; + + trace00: + b0->error = error0 ? node->errors[error0] : 0; + + if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) + { + nsh_input_trace_t *tr = + vlib_add_trace (vm, node, b0, sizeof (*tr)); + tr->nsh_header = *hdr0; + } + + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + + } + + + return from_frame->n_vectors; +} + + +static char * nsh_input_error_strings[] = { +#define _(sym,string) string, + foreach_nsh_input_error +#undef _ +}; + +VLIB_REGISTER_NODE (nsh_input_node) = { + .function = nsh_input_map, + .name = "nsh-input", + .vector_size = sizeof (u32), + .format_trace = format_nsh_input_map_trace, + .format_buffer = format_nsh_header_with_length, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(nsh_input_error_strings), + .error_strings = nsh_input_error_strings, + + .n_next_nodes = NSH_INPUT_N_NEXT, + + .next_nodes = { +#define _(s,n) [NSH_INPUT_NEXT_##s] = n, + foreach_nsh_input_next +#undef _ + }, +}; diff --git a/nsh-plugin/nsh/nsh.h b/nsh-plugin/nsh/nsh.h new file mode 100644 index 0000000..c574148 --- /dev/null +++ b/nsh-plugin/nsh/nsh.h @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2015 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. + */ +#ifndef included_nsh_h +#define included_nsh_h + +#include <vnet/vnet.h> +#include <nsh/nsh_packet.h> +#include <vnet/ip/ip4_packet.h> + +typedef struct { + + /** Key for nsh_header_t entry: 24bit NSP 8bit NSI */ + u32 nsp_nsi; + + /** Key for nsh_header_t entry to map to. : 24bit NSP 8bit NSI + * This may be ~0 if next action is to decap to NSH next protocol + * Note the following heuristic: + * if nsp_nsi == mapped_nsp_nsi then use-case is like SFC SFF + * if nsp_nsi != mapped_nsp_nsi then use-case is like SFC SF + * Note: these are heuristics. Rules about NSI decrement are out of scope + */ + u32 mapped_nsp_nsi; + + /* vnet intfc sw_if_index */ + u32 sw_if_index; + + u32 next_node; + +} nsh_map_t; + +typedef struct { + nsh_map_t map; + u32 is_add; +} nsh_add_del_map_args_t; + +typedef struct { + u8 is_add; + nsh_header_t nsh; +} nsh_add_del_entry_args_t; + +typedef struct { + /* API message ID base */ + u16 msg_id_base; + + /* vector of nsh_header entry instances */ + nsh_header_t *nsh_entries; + + /* hash lookup nsh header by key: {u32: nsp_nsi} */ + uword * nsh_entry_by_key; + + /* vector of nsh_mappings */ + nsh_map_t *nsh_mappings; + + /* hash lookup nsh mapping by key: {u32: nsp_nsi} */ + uword * nsh_mapping_by_key; + uword * nsh_mapping_by_mapped_key; // for use in NSHSFC + + /* convenience */ + vlib_main_t * vlib_main; + vnet_main_t * vnet_main; +} nsh_main_t; + +nsh_main_t nsh_main; + +u8 * format_nsh_input_map_trace (u8 * s, va_list * args); +u8 * format_nsh_header_with_length (u8 * s, va_list * args); + +/* Helper macros used in nsh.c and nsh_test.c */ +#define foreach_copy_nshhdr_field \ +_(ver_o_c) \ +_(length) \ +_(md_type) \ +_(next_protocol) \ +_(nsp_nsi) \ +_(c1) \ +_(c2) \ +_(c3) \ +_(c4) +/* TODO Temp killing tlvs as its causing pain - fix in NSH_SFC */ +#define foreach_32bit_field \ +_(nsp_nsi) \ +_(c1) \ +_(c2) \ +_(c3) \ +_(c4) + +/* Statistics (not really errors) */ +#define foreach_nsh_input_error \ +_(MAPPED, "NSH header found and mapped") \ +_(NO_MAPPING, "no mapping for nsh key") \ +_(INVALID_NEXT_PROTOCOL, "invalid next protocol") \ + +typedef enum { +#define _(sym,str) NSH_INPUT_ERROR_##sym, + foreach_nsh_input_error +#undef _ + NSH_INPUT_N_ERROR, + +} nsh_input_error_t; + +#define foreach_nsh_input_next \ + _(DROP, "error-drop") \ + _(ENCAP_GRE, "gre-input" ) \ + _(ENCAP_VXLANGPE, "vxlan-gpe-encap" ) \ +/* /\* TODO once moved to Project:NSH_SFC *\/ */ + /* _(ENCAP_ETHERNET, "*** TX TO ETHERNET ***") \ */ +/* _(DECAP_ETHERNET_LOOKUP, "ethernet-input" ) \ */ +/* _(DECAP_IP4_INPUT, "ip4-input") \ */ +/* _(DECAP_IP6_INPUT, "ip6-input" ) \ */ + +typedef enum { +#define _(s,n) NSH_INPUT_NEXT_##s, + foreach_nsh_input_next +#undef _ + NSH_INPUT_N_NEXT, +} nsh_input_next_t; + +#endif /* included_nsh_h */ diff --git a/nsh-plugin/nsh/nsh_all_api_h.h b/nsh-plugin/nsh/nsh_all_api_h.h new file mode 100644 index 0000000..83a30e1 --- /dev/null +++ b/nsh-plugin/nsh/nsh_all_api_h.h @@ -0,0 +1,19 @@ + +/* + * nsh_all_api_h.h - skeleton vpp engine plug-in api #include file + * + * Copyright (c) <current-year> <your-organization> + * 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 the generated file, see BUILT_SOURCES in Makefile.am */ +#include <nsh/nsh.api.h> diff --git a/nsh-plugin/nsh/nsh_error.def b/nsh-plugin/nsh/nsh_error.def new file mode 100644 index 0000000..c54e3b8 --- /dev/null +++ b/nsh-plugin/nsh/nsh_error.def @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2015 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. + */ +nsh_input_error (DECAPSULATED, "good packets decapsulated") +nsh_input_error (NO_MAPPING, "no mapping for nsh key") +nsh_input_error (INVALID_NEXT_PROTOCOL, "invalid next protocol")
\ No newline at end of file diff --git a/nsh-plugin/nsh/nsh_msg_enum.h b/nsh-plugin/nsh/nsh_msg_enum.h new file mode 100644 index 0000000..2caf9a1 --- /dev/null +++ b/nsh-plugin/nsh/nsh_msg_enum.h @@ -0,0 +1,31 @@ + +/* + * nsh_msg_enum.h - skeleton vpp engine plug-in message enumeration + * + * Copyright (c) <current-year> <your-organization> + * 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. + */ +#ifndef included_nsh_msg_enum_h +#define included_nsh_msg_enum_h + +#include <vppinfra/byte_order.h> + +#define vl_msg_id(n,h) n, +typedef enum { +#include <nsh/nsh_all_api_h.h> + /* We'll want to know how many messages IDs we need... */ + VL_MSG_FIRST_AVAILABLE, +} vl_msg_id_t; +#undef vl_msg_id + +#endif /* included_nsh_msg_enum_h */ diff --git a/nsh-plugin/nsh/nsh_packet.h b/nsh-plugin/nsh/nsh_packet.h new file mode 100644 index 0000000..553fc0b --- /dev/null +++ b/nsh-plugin/nsh/nsh_packet.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2015 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. + */ +#ifndef included_nsh_packet_h +#define included_nsh_packet_h + +/* + * NSH packet format from draft-quinn-sfc-nsh-03.txt + * + * NSH Base Header + * 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |Ver|O|C|R|R|R|R|R|R| Length | MD Type | Next Protocol | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * + * Base Header Field Descriptions: + * + * Version: The version field is used to ensure backward compatibility + * going forward with future NSH updates. + * + * O bit: Indicates that this packet is an operations and management + * (OAM) packet. SFF and SFs nodes MUST examine the payload and take + * appropriate action (e.g. return status information). + * + * OAM message specifics and handling details are outside the scope of + * this document. + * + * C bit: Indicates that a critical metadata TLV is present (see section + * 7). This bit acts as an indication for hardware implementers to + * decide how to handle the presence of a critical TLV without + * necessarily needing to parse all TLVs present. The C bit MUST be set + * to 1 if one or more critical TLVs are present. + * + * All other flag fields are reserved. + * + * Length: total length, in 4 byte words, of the NSH header, including + * optional variable TLVs. Length must be equal or greater than 6. + * + * MD Type: indicates the format of NSH beyond the base header and the + * type of metadata being carried. This typing is used to describe the + * use for the metadata. A new registry will be requested from IANA for + * the MD Type. NSH defines one type, type = 0x1 which indicates that + * the format of the header is as per this draft. + * + * The format of the base header is invariant, and not described by MD + * Type. + * + * Next Protocol: indicates the protocol type of the original packet. A + * new IANA registry will be created for protocol type. + * + * This draft defines the following Next Protocol values: + * + * 0x1 : IPv4 + * 0x2 : IPv6 + * 0x3 : Ethernet + */ + +typedef CLIB_PACKED(struct { + u8 ver_o_c; + u8 length; + u8 md_type; + u8 next_protocol; + u32 nsp_nsi; // nsp 24 bits, nsi 8 bits + /* Context headers, always present */ + u32 c1; u32 c2; u32 c3; u32 c4; + + /* Optional variable length metadata */ + u32 tlvs[0]; +}) nsh_header_t; + +#define NSH_VERSION (0<<6) +#define NSH_O_BIT (1<<5) +#define NSH_C_BIT (1<<4) + +/* Network byte order shift / mask */ +#define NSH_NSI_MASK 0xFF +#define NSH_NSP_MASK (0x00FFFFFF) +#define NSH_NSP_SHIFT 8 + +#endif /* included_nsh_packet_h */ diff --git a/nsh-plugin/nsh/nsh_test.c b/nsh-plugin/nsh/nsh_test.c new file mode 100644 index 0000000..812b4ab --- /dev/null +++ b/nsh-plugin/nsh/nsh_test.c @@ -0,0 +1,330 @@ +/* + * nsh.c - skeleton vpp-api-test plug-in + * + * Copyright (c) <current-year> <your-organization> + * 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 <vat/vat.h> +#include <vlibapi/api.h> +#include <vlibmemory/api.h> +#include <vlibsocket/api.h> +#include <vppinfra/error.h> +#include <nsh/nsh.h> + +uword unformat_sw_if_index (unformat_input_t * input, va_list * args); + +/* Declare message IDs */ +#include <nsh/nsh_msg_enum.h> + +/* define message structures */ +#define vl_typedefs +#include <nsh/nsh_all_api_h.h> +#undef vl_typedefs + +/* declare message handlers for each api */ + +#define vl_endianfun /* define message structures */ +#include <nsh/nsh_all_api_h.h> +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) +#define vl_printfun +#include <nsh/nsh_all_api_h.h> +#undef vl_printfun + +/* Get the API version number. */ +#define vl_api_version(n,v) static u32 api_version=(v); +#include <nsh/nsh_all_api_h.h> +#undef vl_api_version + + +typedef struct { + /* API message ID base */ + u16 msg_id_base; + vat_main_t *vat_main; +} nsh_test_main_t; + +nsh_test_main_t nsh_test_main; + +#define foreach_standard_reply_retval_handler \ +_(nsh_add_del_entry_reply) \ +_(nsh_add_del_map_reply) \ + +#define _(n) \ + static void vl_api_##n##_t_handler \ + (vl_api_##n##_t * mp) \ + { \ + vat_main_t * vam = nsh_test_main.vat_main; \ + i32 retval = ntohl(mp->retval); \ + if (vam->async_mode) { \ + vam->async_errors += (retval < 0); \ + } else { \ + vam->retval = retval; \ + vam->result_ready = 1; \ + } \ + } +foreach_standard_reply_retval_handler; +#undef _ + +/* + * Table of message reply handlers, must include boilerplate handlers + * we just generated + */ +#define foreach_vpe_api_reply_msg \ +_(NSH_ADD_DEL_ENTRY_REPLY, nsh_add_del_entry_reply) \ +_(NSH_ADD_DEL_MAP_REPLY, nsh_add_del_map_reply) + + +/* M: construct, but don't yet send a message */ + +#define M(T,t) \ +do { \ + vam->result_ready = 0; \ + mp = vl_msg_api_alloc(sizeof(*mp)); \ + memset (mp, 0, sizeof (*mp)); \ + mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \ + mp->client_index = vam->my_client_index; \ +} while(0); + +#define M2(T,t,n) \ +do { \ + vam->result_ready = 0; \ + mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \ + memset (mp, 0, sizeof (*mp)); \ + mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \ + mp->client_index = vam->my_client_index; \ +} while(0); + +/* S: send a message */ +#define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp)) + +/* W: wait for results, with timeout */ +#define W \ +do { \ + timeout = vat_time_now (vam) + 1.0; \ + \ + while (vat_time_now (vam) < timeout) { \ + if (vam->result_ready == 1) { \ + return (vam->retval); \ + } \ + } \ + return -99; \ +} while(0); + +static int api_nsh_add_del_entry (vat_main_t * vam) +{ + nsh_test_main_t * sm = &nsh_test_main; + unformat_input_t * line_input = vam->input; + f64 timeout; + u8 is_add = 1; + u8 ver_o_c = 0; + 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; + u32 *tlvs = 0; + u32 tmp; + vl_api_nsh_add_del_entry_t * mp; + + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { + if (unformat (line_input, "del")) + is_add = 0; + else if (unformat (line_input, "version %d", &tmp)) + ver_o_c |= (tmp & 3) << 6; + else if (unformat (line_input, "o-bit %d", &tmp)) + ver_o_c |= (tmp & 1) << 5; + else if (unformat (line_input, "c-bit %d", &tmp)) + ver_o_c |= (tmp & 1) << 4; + else if (unformat (line_input, "md-type %d", &tmp)) + md_type = tmp; + else if (unformat(line_input, "next-ip4")) + next_protocol = 1; + else if (unformat(line_input, "next-ip6")) + next_protocol = 2; + else if (unformat(line_input, "next-ethernet")) + next_protocol = 3; + else if (unformat (line_input, "c1 %d", &c1)) + ; + else if (unformat (line_input, "c2 %d", &c2)) + ; + else if (unformat (line_input, "c3 %d", &c3)) + ; + else if (unformat (line_input, "c4 %d", &c4)) + ; + 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, "tlv %x")) + vec_add1 (tlvs, tmp); + else + return -99; // PARSE ERROR; + } + + unformat_free (line_input); + + if (nsp_set == 0) + return -1; //TODO Error type for this cond: clib_error_return (0, "nsp not specified"); + + if (nsi_set == 0) + return -2; //TODO Error type for this cond:clib_error_return (0, "nsi not specified"); + + if (md_type != 1) + return -3; //TODO Error type for this cond: clib_error_return (0, "md-type 1 only supported at this time"); + + //TODO sort out TLVS and MD Type2 support + md_type = 1; + length = 6; + + nsp_nsi = (nsp<<8) | nsi; + + /* Construct the API message */ + M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry); + mp->is_add = is_add; + +#define _(x) mp->x = x; + foreach_copy_nshhdr_field; +#undef _ + + mp->tlvs[0] = 0 ; // TODO FIXME this shouldn't be set 0 - in NSH_SFC project + + /* send it... */ + S; + + /* Wait for a reply... */ + W; +} + +static int api_nsh_add_del_map (vat_main_t * vam) +{ + nsh_test_main_t * sm = &nsh_test_main; + unformat_input_t * line_input = vam->input; + f64 timeout; + u8 is_add = 1; + u32 nsp, nsi, mapped_nsp, mapped_nsi; + int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0; + u32 next_node = ~0; + u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC + vl_api_nsh_add_del_map_t * mp; + + + 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, "encap-gre-intf %d", &sw_if_index)) + next_node = NSH_INPUT_NEXT_ENCAP_GRE; + else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index)) + next_node = NSH_INPUT_NEXT_ENCAP_VXLANGPE; + else if (unformat (line_input, "encap-none")) + next_node = NSH_INPUT_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case + else + return -99; //TODO clib_error_return (0, "parse error: '%U'", + } + + unformat_free (line_input); + + if (nsp_set == 0 || nsi_set == 0) + return -1; // TODO create return value: clib_error_return (0, "nsp nsi pair required. Key: for NSH entry"); + + if (mapped_nsp_set == 0 || mapped_nsi_set == 0) + return -2; // TODO create return valuee clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry"); + + if (next_node == ~0) + return -3; //TODO clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]"); + + + M(NSH_ADD_DEL_ENTRY, nsh_add_del_entry); + /* set args structure */ + mp->is_add = is_add; + mp->nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi; + mp->mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi; + mp->sw_if_index = sw_if_index; + mp->next_node = next_node; + + /* send it... */ + S; + + /* Wait for a reply... */ + W; + + +} +/* + * List of messages that the api test plugin sends, + * and that the data plane plugin processes + */ +#define foreach_vpe_api_msg \ +_(nsh_add_del_entry, "{nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn> [md-type <nn>] [tlv <xx>] [del]") \ +_(nsh_add_del_map, "nsp <nn> nsi <nn> [del] map-nsp <nn> map-nsi <nn> [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]") + +void vat_api_hookup (vat_main_t *vam) +{ + nsh_test_main_t * sm = &nsh_test_main; + /* Hook up handlers for replies from the data plane plug-in */ +#define _(N,n) \ + vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \ + #n, \ + vl_api_##n##_t_handler, \ + vl_noop_handler, \ + vl_api_##n##_t_endian, \ + vl_api_##n##_t_print, \ + sizeof(vl_api_##n##_t), 1); + foreach_vpe_api_reply_msg; +#undef _ + + /* API messages we can send */ +#define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n); + foreach_vpe_api_msg; +#undef _ + + /* Help strings */ +#define _(n,h) hash_set_mem (vam->help_by_name, #n, h); + foreach_vpe_api_msg; +#undef _ +} + +clib_error_t * vat_plugin_register (vat_main_t *vam) +{ + nsh_test_main_t * sm = &nsh_test_main; + u8 * name; + + sm->vat_main = vam; + + /* Ask the vpp engine for the first assigned message-id */ + name = format (0, "nsh_%08x%c", api_version, 0); + sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name); + + if (sm->msg_id_base != (u16) ~0) + vat_api_hookup (vam); + + vec_free(name); + + return 0; +} |