diff options
author | Dave Barach <dave@barachs.net> | 2020-07-14 18:30:05 -0400 |
---|---|---|
committer | Damjan Marion <dmarion@me.com> | 2020-07-16 21:39:23 +0000 |
commit | ac0326fc5ae7ac4a8126bbc2f496a92fcfe4755e (patch) | |
tree | 3bb8d7fac43b165ccd6dd106b597fd733e5b28b8 /src/plugins/adl | |
parent | 9a0f2a5e7f678de58807fd49d47deec367656715 (diff) |
adl: move allow/deny list function to plugin
Provide binary API compatibility support for the "cop" APIs until vpp
21.01.
Change the deprecation date in map.api to vpp 21.01.
Type: refactor
Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: I0e60d96de4ae9ae4448f134cf257934126f3b760
Diffstat (limited to 'src/plugins/adl')
-rw-r--r-- | src/plugins/adl/CMakeLists.txt | 31 | ||||
-rw-r--r-- | src/plugins/adl/FEATURE.yaml | 11 | ||||
-rw-r--r-- | src/plugins/adl/adl.api | 63 | ||||
-rw-r--r-- | src/plugins/adl/adl.c | 417 | ||||
-rw-r--r-- | src/plugins/adl/adl.h | 114 | ||||
-rw-r--r-- | src/plugins/adl/adl_api.c | 136 | ||||
-rw-r--r-- | src/plugins/adl/adl_test.c | 153 | ||||
-rw-r--r-- | src/plugins/adl/ip4_allowlist.c | 343 | ||||
-rw-r--r-- | src/plugins/adl/ip6_allowlist.c | 300 | ||||
-rw-r--r-- | src/plugins/adl/node.c | 309 | ||||
-rw-r--r-- | src/plugins/adl/setup.pg | 62 | ||||
-rw-r--r-- | src/plugins/adl/test/test_adl.py | 103 |
12 files changed, 2042 insertions, 0 deletions
diff --git a/src/plugins/adl/CMakeLists.txt b/src/plugins/adl/CMakeLists.txt new file mode 100644 index 00000000000..a1a61be4a5c --- /dev/null +++ b/src/plugins/adl/CMakeLists.txt @@ -0,0 +1,31 @@ +# Copyright (c) 2020 Cisco Systems 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. + +add_vpp_plugin(adl + SOURCES + adl_api.c + adl.c + adl.h + ip4_allowlist.c + ip6_allowlist.c + node.c + + MULTIARCH_SOURCES + node.c + + API_FILES + adl.api + + API_TEST_SOURCES + adl_test.c +) diff --git a/src/plugins/adl/FEATURE.yaml b/src/plugins/adl/FEATURE.yaml new file mode 100644 index 00000000000..bd8de2a409f --- /dev/null +++ b/src/plugins/adl/FEATURE.yaml @@ -0,0 +1,11 @@ +--- +name: ADL +maintainer: Dave Barach <dave@barachs.net> +features: + - v4, v6 non-default FIB src-address lookup + - Drop packets which don't hit a receive adjacency + - Not widely used + +description: "A very simple / fast source-address allow/deny list feature" +state: experimental +properties: [API, CLI, MULTITHREAD] diff --git a/src/plugins/adl/adl.api b/src/plugins/adl/adl.api new file mode 100644 index 00000000000..cbbb026a77c --- /dev/null +++ b/src/plugins/adl/adl.api @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2020 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. + */ + +option version = "0.0.1"; + +import "vnet/interface_types.api"; + + /** \brief adl: enable/disable filtration features on an interface + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_inded - desired interface + @param enable_disable - 1 => enable, 0 => disable +*/ + +autoreply define adl_interface_enable_disable +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + bool enable_disable; +}; + +/** \brief adl: enable/disable allow list filtration features on an interface + Note: the supplied fib_id must match in order to remove the feature! + + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - interface handle, physical interfaces only + @param fib_id - fib identifier for the allow/deny fib + @param ip4 - 1 => enable ip4 filtration, 0=> disable ip4 filtration + @param ip6 - 1 => enable ip6 filtration, 0=> disable ip6 filtration + @param default_adl - 1 => enable non-ip4, non-ip6 filtration + 0 => disable it +*/ + +autoreply define adl_allowlist_enable_disable +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + u32 fib_id; + bool ip4; + bool ip6; + bool default_adl; +}; + + /* + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/adl/adl.c b/src/plugins/adl/adl.c new file mode 100644 index 00000000000..832bfd4a982 --- /dev/null +++ b/src/plugins/adl/adl.c @@ -0,0 +1,417 @@ +/* + * Copyright (c) 2016,2020 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/ethernet/ethernet.h> +#include <vnet/plugin/plugin.h> +#include <vpp/app/version.h> +#include <plugins/adl/adl.h> + +adl_main_t adl_main; + +static clib_error_t * +adl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add) +{ + adl_main_t *am = &adl_main; + adl_config_data_t _data, *data = &_data; + vlib_main_t *vm = am->vlib_main; + vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);; + adl_config_main_t *acm; + int address_family; + u32 ci, default_next; + + clib_memset (data, 0, sizeof (*data)); + + /* + * Ignore local interface, pg interfaces. $$$ need a #define for the + * first "real" interface. The answer is 5 at the moment. + */ + if (hi->dev_class_index == vnet_local_interface_device_class.index) + return 0; + + for (address_family = VNET_ADL_IP4; address_family < VNET_N_ADLS; + address_family++) + { + acm = &am->adl_config_mains[address_family]; + + /* + * Once-only code to initialize the per-address-family + * adl feature subgraphs. + * Since the (single) start-node, adl-input, must be able + * to push pkts into three separate subgraphs, we + * use a unified adl_feature_type_t enumeration. + */ + + if (!(acm->config_main.node_index_by_feature_index)) + { + switch (address_family) + { + case VNET_ADL_IP4: + { + static char *start_nodes[] = { "adl-input" }; + static char *feature_nodes[] = { + [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist", + [IP4_RX_ADL_INPUT] = "ip4-input", + }; + + vnet_config_init (vm, &acm->config_main, + start_nodes, ARRAY_LEN (start_nodes), + feature_nodes, ARRAY_LEN (feature_nodes)); + } + break; + case VNET_ADL_IP6: + { + static char *start_nodes[] = { "adl-input" }; + static char *feature_nodes[] = { + [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist", + [IP6_RX_ADL_INPUT] = "ip6-input", + }; + vnet_config_init (vm, &acm->config_main, + start_nodes, ARRAY_LEN (start_nodes), + feature_nodes, ARRAY_LEN (feature_nodes)); + } + break; + + case VNET_ADL_DEFAULT: + { + static char *start_nodes[] = { "adl-input" }; + static char *feature_nodes[] = { + [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist", + [DEFAULT_RX_ADL_INPUT] = "ethernet-input", + }; + vnet_config_init (vm, &acm->config_main, + start_nodes, ARRAY_LEN (start_nodes), + feature_nodes, ARRAY_LEN (feature_nodes)); + } + break; + + default: + clib_warning ("bug"); + break; + } + } + vec_validate_init_empty (acm->config_index_by_sw_if_index, sw_if_index, + ~0); + + ci = acm->config_index_by_sw_if_index[sw_if_index]; + + /* Create a sensible initial config: send pkts to xxx-input */ + if (address_family == VNET_ADL_IP4) + default_next = IP4_RX_ADL_INPUT; + else if (address_family == VNET_ADL_IP6) + default_next = IP6_RX_ADL_INPUT; + else + default_next = DEFAULT_RX_ADL_INPUT; + + if (is_add) + ci = vnet_config_add_feature (vm, &acm->config_main, + ci, default_next, data, sizeof (*data)); + else + { + /* If the feature was actually configured */ + if (ci != ~0) + { + ci = vnet_config_del_feature (vm, &acm->config_main, + ci, default_next, data, + sizeof (*data)); + } + } + + acm->config_index_by_sw_if_index[sw_if_index] = ci; + } + return 0; +} + +VNET_SW_INTERFACE_ADD_DEL_FUNCTION (adl_sw_interface_add_del); + +static clib_error_t * +adl_init (vlib_main_t * vm) +{ + adl_main_t *cm = &adl_main; + + cm->vlib_main = vm; + cm->vnet_main = vnet_get_main (); + + /* + * Setup the packet generator so we can inject ethernet + * frames into this node + */ + ethernet_setup_node (vm, adl_input_node.index); + return 0; +} + +/* *INDENT-OFF* */ +VLIB_INIT_FUNCTION (adl_init) = +{ + .runs_after = VLIB_INITS ("ip4_allowlist_init", "ip6_allowlist_init"), +}; +/* *INDENT-ON* */ + +/* *INDENT-OFF* */ +VNET_FEATURE_INIT (adl, static) = +{ + .arc_name = "device-input", + .node_name = "adl-input", + .runs_before = VNET_FEATURES ("ethernet-input"), +}; +/* *INDENT-ON */ + +int adl_interface_enable_disable (u32 sw_if_index, int enable_disable) +{ + /* + * Redirect pkts from the driver to the adl node. + */ + vnet_feature_enable_disable ("device-input", "adl-input", + sw_if_index, enable_disable, 0, 0); + return 0; +} + +static clib_error_t * +adl_enable_disable_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + adl_main_t * cm = &adl_main; + u32 sw_if_index = ~0; + int enable_disable = 1; + + int rv; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { + if (unformat (input, "disable")) + enable_disable = 0; + else if (unformat (input, "%U", unformat_vnet_sw_interface, + cm->vnet_main, &sw_if_index)) + ; + else + break; + } + + if (sw_if_index == ~0) + return clib_error_return (0, "Please specify an interface..."); + + rv = adl_interface_enable_disable (sw_if_index, enable_disable); + + switch(rv) { + case 0: + break; + + case VNET_API_ERROR_INVALID_SW_IF_INDEX: + return clib_error_return + (0, "Invalid interface, only works on physical ports"); + break; + + case VNET_API_ERROR_UNIMPLEMENTED: + return clib_error_return (0, "Device driver doesn't support redirection"); + break; + + default: + return clib_error_return (0, "adl_interface_enable_disable returned %d", + rv); + } + return 0; +} + +VLIB_CLI_COMMAND (adl_interface_command, static) = { + .path = "adl interface", + .short_help = + "adl interface <interface-name> [disable]", + .function = adl_enable_disable_command_fn, +}; + + +int adl_allowlist_enable_disable (adl_allowlist_enable_disable_args_t *a) +{ + adl_main_t * cm = &adl_main; + vlib_main_t * vm = cm->vlib_main; + ip4_main_t * im4 = &ip4_main; + ip6_main_t * im6 = &ip6_main; + int address_family; + int is_add; + adl_config_main_t * acm; + u32 next_to_add_del = 0; + uword * p; + u32 fib_index = 0; + u32 ci; + adl_config_data_t _data, *data=&_data; + + /* + * Enable / disable allowlist processing on the specified interface + */ + + for (address_family = VNET_ADL_IP4; address_family < VNET_N_ADLS; + address_family++) + { + acm = &cm->adl_config_mains[address_family]; + + switch(address_family) + { + case VNET_ADL_IP4: + is_add = (a->ip4 != 0); + next_to_add_del = IP4_RX_ADL_ALLOWLIST; + /* configured opaque data must match, or no supper */ + p = hash_get (im4->fib_index_by_table_id, a->fib_id); + if (p) + fib_index = p[0]; + else + { + if (is_add) + return VNET_API_ERROR_NO_SUCH_FIB; + else + continue; + } + break; + + case VNET_ADL_IP6: + is_add = (a->ip6 != 0); + next_to_add_del = IP6_RX_ADL_ALLOWLIST; + p = hash_get (im6->fib_index_by_table_id, a->fib_id); + if (p) + fib_index = p[0]; + else + { + if (is_add) + return VNET_API_ERROR_NO_SUCH_FIB; + else + continue; + } + break; + + case VNET_ADL_DEFAULT: + is_add = (a->default_adl != 0); + next_to_add_del = DEFAULT_RX_ADL_ALLOWLIST; + break; + + default: + clib_warning ("BUG"); + } + + ci = acm->config_index_by_sw_if_index[a->sw_if_index]; + data->fib_index = fib_index; + + if (is_add) + ci = vnet_config_add_feature (vm, &acm->config_main, + ci, + next_to_add_del, + data, sizeof (*data)); + else + { + /* If the feature was actually configured... */ + if (ci != ~0) + { + /* delete it */ + ci = vnet_config_del_feature (vm, &acm->config_main, + ci, + next_to_add_del, + data, sizeof (*data)); + } + } + + acm->config_index_by_sw_if_index[a->sw_if_index] = ci; + } + return 0; +} + +static clib_error_t * +adl_allowlist_enable_disable_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + adl_main_t * cm = &adl_main; + u32 sw_if_index = ~0; + u8 ip4 = 0; + u8 ip6 = 0; + u8 default_adl = 0; + u32 fib_id = 0; + int rv; + adl_allowlist_enable_disable_args_t _a, * a = &_a; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { + if (unformat (input, "ip4")) + ip4 = 1; + else if (unformat (input, "ip6")) + ip6 = 1; + else if (unformat (input, "default")) + default_adl = 1; + else if (unformat (input, "%U", unformat_vnet_sw_interface, + cm->vnet_main, &sw_if_index)) + ; + else if (unformat (input, "fib-id %d", &fib_id)) + ; + else + break; + } + + if (sw_if_index == ~0) + return clib_error_return (0, "Please specify an interface..."); + + a->sw_if_index = sw_if_index; + a->ip4 = ip4; + a->ip6 = ip6; + a->default_adl = default_adl; + a->fib_id = fib_id; + + rv = adl_allowlist_enable_disable (a); + + switch(rv) { + case 0: + break; + + case VNET_API_ERROR_INVALID_SW_IF_INDEX: + return clib_error_return + (0, "Invalid interface, only works on physical ports"); + break; + + case VNET_API_ERROR_NO_SUCH_FIB: + return clib_error_return + (0, "Invalid fib"); + break; + + case VNET_API_ERROR_UNIMPLEMENTED: + return clib_error_return (0, "Device driver doesn't support redirection"); + break; + + default: + return clib_error_return (0, "adl_allowlist_enable_disable returned %d", + rv); + } + + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (adl_allowlist_command, static) = +{ + .path = "adl allowlist", + .short_help = + "adl allowlist <interface-name> [ip4][ip6][default][fib-id <NN>][disable]", + .function = adl_allowlist_enable_disable_command_fn, +}; +/* *INDENT-ON* */ + +/* *INDENT-OFF* */ +VLIB_PLUGIN_REGISTER () = +{ + .version = VPP_BUILD_VER, + .description = "Allow/deny list plugin", +}; +/* *INDENT-ON* */ + + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/adl/adl.h b/src/plugins/adl/adl.h new file mode 100644 index 00000000000..e88b9f15c97 --- /dev/null +++ b/src/plugins/adl/adl.h @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2016,2020 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 __vnet_adl_h__ +#define __vnet_adl_h__ + +#include <vlib/vlib.h> +#include <vnet/vnet.h> +#include <vnet/pg/pg.h> + +#include <vppinfra/error.h> +#include <vppinfra/hash.h> +#include <vnet/vnet.h> +#include <vnet/ip/ip.h> +#include <vnet/l2/l2_input.h> +#include <vnet/ethernet/ethernet.h> +#include <vnet/ip/ip4_packet.h> +#include <vnet/ip/ip6_packet.h> + +typedef enum { + VNET_ADL_IP4, + VNET_ADL_IP6, + VNET_ADL_DEFAULT, + VNET_N_ADLS, +} vnet_adl_t; + +typedef enum { + /* First check src address against allowlist */ + IP4_RX_ADL_ALLOWLIST, + IP6_RX_ADL_ALLOWLIST, + DEFAULT_RX_ADL_ALLOWLIST, + + /* Pkts not otherwise dropped go to xxx-input */ + IP4_RX_ADL_INPUT, + IP6_RX_ADL_INPUT, + DEFAULT_RX_ADL_INPUT, + + /* Going, going, gone... */ + RX_ADL_DROP, + + ADL_RX_N_FEATURES, +} adl_feature_type_t; + +typedef struct { + vnet_config_main_t config_main; + u32 * config_index_by_sw_if_index; +} adl_config_main_t; + +typedef struct { + u32 fib_index; +} adl_config_data_t; + +typedef struct { + adl_config_main_t adl_config_mains[VNET_N_ADLS]; + + u16 msg_id_base; + + /* convenience */ + vlib_main_t * vlib_main; + vnet_main_t * vnet_main; +} adl_main_t; + +extern adl_main_t adl_main; + +extern vlib_node_registration_t adl_input_node; + +int adl_interface_enable_disable (u32 sw_if_index, int enable_disable); + +typedef struct { + u32 sw_if_index; + u8 ip4; + u8 ip6; + u8 default_adl; + u32 fib_id; +} adl_allowlist_enable_disable_args_t; + +int adl_allowlist_enable_disable (adl_allowlist_enable_disable_args_t *a); + +/* Plugin private opaque union type */ +typedef struct { + /* MUST be in sync with .../src/vnet/buffer.h */ + u32 sw_if_index[VLIB_N_RX_TX]; + i16 l2_hdr_offset; + i16 l3_hdr_offset; + i16 l4_hdr_offset; + u8 feature_arc_index; + u8 dont_waste_me; + /* end of must be in sync with .../src/vnet/buffer.h */ + union + { + /* COP - configurable junk filter(s) */ + struct + { + /* Current configuration index. */ + u32 current_config_index; + } adl; + }; +} adl_buffer_opaque_t; + +#define adl_buffer(b) ((adl_buffer_opaque_t *) (b)->opaque) + +#endif /* __vnet_adl_h__ */ diff --git a/src/plugins/adl/adl_api.c b/src/plugins/adl/adl_api.c new file mode 100644 index 00000000000..37c96400b70 --- /dev/null +++ b/src/plugins/adl/adl_api.c @@ -0,0 +1,136 @@ +/* + *------------------------------------------------------------------ + * adl_api.c - adl api + * + * Copyright (c) 2016,2020 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 <adl/adl.h> + +#include <vlibapi/api.h> +#include <vlibmemory/api.h> +#include <vpp/app/version.h> + +/* define message IDs */ +#include <vnet/format_fns.h> +#include <adl/adl.api_enum.h> +#include <adl/adl.api_types.h> + +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) + +#define REPLY_MSG_ID_BASE am->msg_id_base +#include <vlibapi/api_helper_macros.h> + +#define foreach_vpe_api_msg \ +_(ADL_INTERFACE_ENABLE_DISABLE, adl_interface_enable_disable) \ +_(ADL_LIST_ENABLE_DISABLE, adl_allowlist_enable_disable) + +/* + * Compatibility shim for the core engine cop_interface_enable_disable API, + * which will be deprecated in vpp 20.12. + */ +int vl_api_cop_interface_enable_disable_callback + (u32 sw_if_index, int enable_disable) +{ + return adl_interface_enable_disable (sw_if_index, enable_disable); +} + +static void vl_api_adl_interface_enable_disable_t_handler + (vl_api_adl_interface_enable_disable_t * mp) +{ + adl_main_t *am = &adl_main; + vl_api_adl_interface_enable_disable_reply_t *rmp; + int rv; + u32 sw_if_index = ntohl (mp->sw_if_index); + int enable_disable; + + VALIDATE_SW_IF_INDEX (mp); + + enable_disable = (int) mp->enable_disable; + + rv = adl_interface_enable_disable (sw_if_index, enable_disable); + + BAD_SW_IF_INDEX_LABEL; + + REPLY_MACRO (VL_API_ADL_INTERFACE_ENABLE_DISABLE_REPLY); +} + +/* + * Compatibility shim for the core engine cop_whitelist_enable_disable API, + * which will be deprecated in vpp 20.12. + */ +int vl_api_cop_whitelist_enable_disable_callback + (adl_allowlist_enable_disable_args_t * a) +{ + return adl_allowlist_enable_disable (a); +} + +static void vl_api_adl_allowlist_enable_disable_t_handler + (vl_api_adl_allowlist_enable_disable_t * mp) +{ + adl_main_t *am = &adl_main; + vl_api_adl_allowlist_enable_disable_reply_t *rmp; + adl_allowlist_enable_disable_args_t _a, *a = &_a; + u32 sw_if_index = ntohl (mp->sw_if_index); + int rv; + + VALIDATE_SW_IF_INDEX (mp); + + a->sw_if_index = sw_if_index; + a->ip4 = mp->ip4; + a->ip6 = mp->ip6; + a->default_adl = mp->default_adl; + a->fib_id = ntohl (mp->fib_id); + + rv = adl_allowlist_enable_disable (a); + + BAD_SW_IF_INDEX_LABEL; + + REPLY_MACRO (VL_API_ADL_ALLOWLIST_ENABLE_DISABLE_REPLY); +} + +#include <adl/adl.api.c> +static clib_error_t * +adl_api_init (vlib_main_t * vm) +{ + adl_main_t *am = &adl_main; + void register_vl_api_cop_interface_enable_disable_callback (void *); + void register_vl_api_cop_whitelist_enable_disable_callback (void *); + + am->vlib_main = vm; + + /* Ask for a correctly-sized block of API message decode slots */ + am->msg_id_base = setup_message_id_table (); + + /* Set up transitional API callbacks */ + register_vl_api_cop_interface_enable_disable_callback + (vl_api_cop_interface_enable_disable_callback); + register_vl_api_cop_whitelist_enable_disable_callback + (vl_api_cop_whitelist_enable_disable_callback); + + return 0; +} + +VLIB_INIT_FUNCTION (adl_api_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/adl/adl_test.c b/src/plugins/adl/adl_test.c new file mode 100644 index 00000000000..9a32f96da99 --- /dev/null +++ b/src/plugins/adl/adl_test.c @@ -0,0 +1,153 @@ +/* + * adl.c - adl vpp-api-test plug-in + * + * Copyright (c) 2020 Cisco Systems and/or 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 <vat/vat.h> +#include <vlibapi/api.h> +#include <vlibmemory/api.h> +#include <vppinfra/error.h> +#include <stdbool.h> + +#define __plugin_msg_base adl_test_main.msg_id_base +#include <vlibapi/vat_helper_macros.h> + +uword unformat_sw_if_index (unformat_input_t * input, va_list * args); + +/* Declare message IDs */ +#include <adl/adl.api_enum.h> +#include <adl/adl.api_types.h> + +typedef struct +{ + /* API message ID base */ + u16 msg_id_base; + vat_main_t *vat_main; +} adl_test_main_t; + +adl_test_main_t adl_test_main; + +static int +api_adl_interface_enable_disable (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + int enable_disable = 1; + u32 sw_if_index = ~0; + vl_api_adl_interface_enable_disable_t *mp; + int ret; + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index)) + ; + else if (unformat (i, "sw_if_index %d", &sw_if_index)) + ; + else if (unformat (i, "disable")) + enable_disable = 0; + else + break; + } + + if (sw_if_index == ~0) + { + errmsg ("missing interface name / explicit sw_if_index number \n"); + return -99; + } + + /* Construct the API message */ + M (ADL_INTERFACE_ENABLE_DISABLE, mp); + mp->sw_if_index = ntohl (sw_if_index); + mp->enable_disable = enable_disable; + + /* send it... */ + S (mp); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +static int +api_adl_allowlist_enable_disable (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + u32 sw_if_index = ~0; + vl_api_adl_allowlist_enable_disable_t *mp; + u32 fib_id = ~0; + int ip4 = 0; + int ip6 = 0; + int default_adl = 0; + int ret; + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index)) + ; + else if (unformat (i, "sw_if_index %d", &sw_if_index)) + ; + else if (unformat (i, "fib-id %d", &fib_id)) + ; + else if (unformat (i, "ip4")) + ip4 = 1; + else if (unformat (i, "ip6")) + ip6 = 1; + else if (unformat (i, "default")) + default_adl = 1; + else + break; + } + + if (sw_if_index == ~0) + { + errmsg ("missing interface name / explicit sw_if_index number \n"); + return -99; + } + + if (fib_id == ~0) + { + errmsg ("FIB id must be specified...\n"); + return -99; + } + + /* Construct the API message */ + M (ADL_ALLOWLIST_ENABLE_DISABLE, mp); + mp->sw_if_index = ntohl (sw_if_index); + mp->fib_id = ntohl (fib_id); + mp->ip4 = ip4; + mp->ip6 = ip6; + mp->default_adl = default_adl; + + /* send it... */ + S (mp); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +/* + * List of messages that the adl test plugin sends, + * and that the data plane plugin processes + */ +#include <adl/adl.api_test.c> + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/adl/ip4_allowlist.c b/src/plugins/adl/ip4_allowlist.c new file mode 100644 index 00000000000..ca7b4c115f0 --- /dev/null +++ b/src/plugins/adl/ip4_allowlist.c @@ -0,0 +1,343 @@ +/* + * Copyright (c) 2016,2020 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 <plugins/adl/adl.h> +#include <vnet/fib/ip4_fib.h> +#include <vnet/dpo/load_balance.h> + +typedef struct { + u32 next_index; + u32 sw_if_index; +} ip4_adl_allowlist_trace_t; + +/* packet trace format function */ +static u8 * format_ip4_adl_allowlist_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 *); + ip4_adl_allowlist_trace_t * t = va_arg (*args, ip4_adl_allowlist_trace_t *); + + s = format (s, "IP4_ADL_ALLOWLIST: sw_if_index %d, next index %d", + t->sw_if_index, t->next_index); + return s; +} + +#define foreach_ip4_adl_allowlist_error \ +_(ALLOWED, "ip4 allowlist allowed") \ +_(DROPPED, "ip4 allowlist dropped") + +typedef enum { +#define _(sym,str) IP4_ADL_ALLOWLIST_ERROR_##sym, + foreach_ip4_adl_allowlist_error +#undef _ + IP4_ADL_ALLOWLIST_N_ERROR, +} ip4_adl_allowlist_error_t; + +static char * ip4_adl_allowlist_error_strings[] = { +#define _(sym,string) string, + foreach_ip4_adl_allowlist_error +#undef _ +}; + +VLIB_NODE_FN (ip4_adl_allowlist_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + u32 n_left_from, * from, * to_next; + adl_feature_type_t next_index; + adl_main_t *cm = &adl_main; + vlib_combined_counter_main_t * vcm = &load_balance_main.lbm_via_counters; + u32 thread_index = vm->thread_index; + u32 allowed_packets; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + allowed_packets = n_left_from; + 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, next1; + u32 sw_if_index0, sw_if_index1; + ip4_header_t * ip0, * ip1; + adl_config_main_t * ccm0, * ccm1; + adl_config_data_t * c0, * c1; + ip4_fib_mtrie_t * mtrie0, * mtrie1; + ip4_fib_mtrie_leaf_t leaf0, leaf1; + u32 lb_index0, lb_index1; + const load_balance_t * lb0, *lb1; + const dpo_id_t *dpo0, *dpo1; + + /* 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, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); + } + + /* speculatively enqueue b0 and b1 to the current next frame */ + to_next[0] = bi0 = from[0]; + to_next[1] = bi1 = from[1]; + from += 2; + to_next += 2; + n_left_from -= 2; + n_left_to_next -= 2; + + b0 = vlib_get_buffer (vm, bi0); + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + + ip0 = vlib_buffer_get_current (b0); + + ccm0 = cm->adl_config_mains + VNET_ADL_IP4; + + c0 = vnet_get_config_data + (&ccm0->config_main, + &adl_buffer (b0)->adl.current_config_index, + &next0, + sizeof (c0[0])); + + mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie; + + leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address); + + leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, + &ip0->src_address, 2); + + leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, + &ip0->src_address, 3); + + lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); + + ASSERT (lb_index0 + == ip4_fib_table_lookup_lb (ip4_fib_get(c0->fib_index), + &ip0->src_address)); + lb0 = load_balance_get (lb_index0); + dpo0 = load_balance_get_bucket_i(lb0, 0); + + if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE)) + { + b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED]; + allowed_packets--; + next0 = RX_ADL_DROP; + } + + b1 = vlib_get_buffer (vm, bi1); + sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX]; + + ip1 = vlib_buffer_get_current (b1); + + ccm1 = cm->adl_config_mains + VNET_ADL_IP4; + + c1 = vnet_get_config_data + (&ccm1->config_main, + &adl_buffer (b1)->adl.current_config_index, + &next1, + sizeof (c1[0])); + mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie; + + leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address); + + leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, + &ip1->src_address, 2); + + leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, + &ip1->src_address, 3); + + lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1); + ASSERT (lb_index1 + == ip4_fib_table_lookup_lb (ip4_fib_get(c1->fib_index), + &ip1->src_address)); + lb1 = load_balance_get (lb_index1); + dpo1 = load_balance_get_bucket_i(lb1, 0); + + vlib_increment_combined_counter + (vcm, thread_index, lb_index0, 1, + vlib_buffer_length_in_chain (vm, b0) + + sizeof(ethernet_header_t)); + + vlib_increment_combined_counter + (vcm, thread_index, lb_index1, 1, + vlib_buffer_length_in_chain (vm, b1) + + sizeof(ethernet_header_t)); + + + if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE)) + { + b1->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED]; + allowed_packets--; + next1 = RX_ADL_DROP; + } + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) + { + ip4_adl_allowlist_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b1->flags & VLIB_BUFFER_IS_TRACED))) + { + ip4_adl_allowlist_trace_t *t = + vlib_add_trace (vm, node, b1, sizeof (*t)); + t->sw_if_index = sw_if_index1; + t->next_index = next1; + } + + /* verify speculative enqueues, maybe switch current next frame */ + 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; + u32 sw_if_index0; + ip4_header_t * ip0; + adl_config_main_t *ccm0; + adl_config_data_t *c0; + ip4_fib_mtrie_t * mtrie0; + ip4_fib_mtrie_leaf_t leaf0; + u32 lb_index0; + const load_balance_t * lb0; + const dpo_id_t *dpo0; + + /* speculatively enqueue b0 to the current next frame */ + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + + ip0 = vlib_buffer_get_current (b0); + + ccm0 = cm->adl_config_mains + VNET_ADL_IP4; + + c0 = vnet_get_config_data + (&ccm0->config_main, + &adl_buffer (b0)->adl.current_config_index, + &next0, + sizeof (c0[0])); + + mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie; + + leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address); + + leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, + &ip0->src_address, 2); + + leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, + &ip0->src_address, 3); + + lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); + + ASSERT (lb_index0 + == ip4_fib_table_lookup_lb (ip4_fib_get(c0->fib_index), + &ip0->src_address)); + + lb0 = load_balance_get (lb_index0); + dpo0 = load_balance_get_bucket_i(lb0, 0); + + vlib_increment_combined_counter + (vcm, thread_index, lb_index0, 1, + vlib_buffer_length_in_chain (vm, b0) + + sizeof(ethernet_header_t)); + + if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE)) + { + b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED]; + allowed_packets--; + next0 = RX_ADL_DROP; + } + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) + { + ip4_adl_allowlist_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + + /* verify speculative enqueue, maybe switch current next frame */ + 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); + } + + vlib_node_increment_counter (vm, node->node_index, + IP4_ADL_ALLOWLIST_ERROR_ALLOWED, + allowed_packets); + + return frame->n_vectors; +} + +VLIB_REGISTER_NODE (ip4_adl_allowlist_node) = { + .name = "ip4-adl-allowlist", + .vector_size = sizeof (u32), + .format_trace = format_ip4_adl_allowlist_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(ip4_adl_allowlist_error_strings), + .error_strings = ip4_adl_allowlist_error_strings, + + .n_next_nodes = ADL_RX_N_FEATURES, + + /* edit / add dispositions here */ + .next_nodes = { + [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist", + [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist", + [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist", + [IP4_RX_ADL_INPUT] = "ip4-input", + [IP6_RX_ADL_INPUT] = "ip6-input", + [DEFAULT_RX_ADL_INPUT] = "ethernet-input", + [RX_ADL_DROP] = "error-drop", + }, +}; + +static clib_error_t * +ip4_allowlist_init (vlib_main_t * vm) +{ + return 0; +} + +VLIB_INIT_FUNCTION (ip4_allowlist_init); diff --git a/src/plugins/adl/ip6_allowlist.c b/src/plugins/adl/ip6_allowlist.c new file mode 100644 index 00000000000..d272f101b43 --- /dev/null +++ b/src/plugins/adl/ip6_allowlist.c @@ -0,0 +1,300 @@ +/* + * Copyright (c) 2016,2020 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 <plugins/adl/adl.h> +#include <vnet/fib/ip6_fib.h> +#include <vnet/dpo/load_balance.h> + +typedef struct { + u32 next_index; + u32 sw_if_index; +} ip6_adl_allowlist_trace_t; + +/* packet trace format function */ +static u8 * format_ip6_adl_allowlist_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 *); + ip6_adl_allowlist_trace_t * t = va_arg (*args, ip6_adl_allowlist_trace_t *); + + s = format (s, "IP6_ADL_ALLOWLIST: sw_if_index %d, next index %d", + t->sw_if_index, t->next_index); + return s; +} + +#define foreach_ip6_adl_allowlist_error \ +_(ALLOWED, "ip6 allowlist allowed") \ +_(DROPPED, "ip6 allowlist dropped") + +typedef enum { +#define _(sym,str) IP6_ADL_ALLOWLIST_ERROR_##sym, + foreach_ip6_adl_allowlist_error +#undef _ + IP6_ADL_ALLOWLIST_N_ERROR, +} ip6_adl_allowlist_error_t; + +static char * ip6_adl_allowlist_error_strings[] = { +#define _(sym,string) string, + foreach_ip6_adl_allowlist_error +#undef _ +}; + +VLIB_NODE_FN (ip6_adl_allowlist_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + u32 n_left_from, * from, * to_next; + adl_feature_type_t next_index; + adl_main_t *cm = &adl_main; + vlib_combined_counter_main_t * vcm = &load_balance_main.lbm_via_counters; + u32 thread_index = vm->thread_index; + u32 allowed_packets; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + allowed_packets = n_left_from; + 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, next1; + u32 sw_if_index0, sw_if_index1; + ip6_header_t * ip0, * ip1; + adl_config_main_t * ccm0, * ccm1; + adl_config_data_t * c0, * c1; + u32 lb_index0, lb_index1; + const load_balance_t * lb0, *lb1; + const dpo_id_t *dpo0, *dpo1; + + /* 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, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); + } + + /* speculatively enqueue b0 and b1 to the current next frame */ + to_next[0] = bi0 = from[0]; + to_next[1] = bi1 = from[1]; + from += 2; + to_next += 2; + n_left_from -= 2; + n_left_to_next -= 2; + + b0 = vlib_get_buffer (vm, bi0); + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + + ip0 = vlib_buffer_get_current (b0); + + ccm0 = cm->adl_config_mains + VNET_ADL_IP6; + + c0 = vnet_get_config_data + (&ccm0->config_main, + &adl_buffer (b0)->adl.current_config_index, + &next0, + sizeof (c0[0])); + + lb_index0 = ip6_fib_table_fwding_lookup (c0->fib_index, + &ip0->src_address); + lb0 = load_balance_get (lb_index0); + dpo0 = load_balance_get_bucket_i(lb0, 0); + + if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE)) + { + b0->error = node->errors[IP6_ADL_ALLOWLIST_ERROR_DROPPED]; + allowed_packets--; + next0 = RX_ADL_DROP; + } + + b1 = vlib_get_buffer (vm, bi1); + sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX]; + + ip1 = vlib_buffer_get_current (b1); + + ccm1 = cm->adl_config_mains + VNET_ADL_IP6; + + c1 = vnet_get_config_data + (&ccm1->config_main, + &adl_buffer (b1)->adl.current_config_index, + &next1, + sizeof (c1[0])); + + lb_index1 = ip6_fib_table_fwding_lookup (c1->fib_index, + &ip1->src_address); + + lb1 = load_balance_get (lb_index1); + dpo1 = load_balance_get_bucket_i(lb1, 0); + + vlib_increment_combined_counter + (vcm, thread_index, lb_index0, 1, + vlib_buffer_length_in_chain (vm, b0) + + sizeof(ethernet_header_t)); + + vlib_increment_combined_counter + (vcm, thread_index, lb_index1, 1, + vlib_buffer_length_in_chain (vm, b1) + + sizeof(ethernet_header_t)); + + if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE)) + { + b1->error = node->errors[IP6_ADL_ALLOWLIST_ERROR_DROPPED]; + allowed_packets--; + next1 = RX_ADL_DROP; + } + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) + { + ip6_adl_allowlist_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b1->flags & VLIB_BUFFER_IS_TRACED))) + { + ip6_adl_allowlist_trace_t *t = + vlib_add_trace (vm, node, b1, sizeof (*t)); + t->sw_if_index = sw_if_index1; + t->next_index = next1; + } + + /* verify speculative enqueues, maybe switch current next frame */ + 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; + u32 sw_if_index0; + ip6_header_t * ip0; + adl_config_main_t *ccm0; + adl_config_data_t *c0; + u32 lb_index0; + const load_balance_t * lb0; + const dpo_id_t *dpo0; + + /* speculatively enqueue b0 to the current next frame */ + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX]; + + ip0 = vlib_buffer_get_current (b0); + + ccm0 = cm->adl_config_mains + VNET_ADL_IP6; + + c0 = vnet_get_config_data + (&ccm0->config_main, + &adl_buffer (b0)->adl.current_config_index, + &next0, + sizeof (c0[0])); + + lb_index0 = ip6_fib_table_fwding_lookup (c0->fib_index, + &ip0->src_address); + + lb0 = load_balance_get (lb_index0); + dpo0 = load_balance_get_bucket_i(lb0, 0); + + vlib_increment_combined_counter + (vcm, thread_index, lb_index0, 1, + vlib_buffer_length_in_chain (vm, b0) + + sizeof(ethernet_header_t)); + + if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE)) + { + b0->error = node->errors[IP6_ADL_ALLOWLIST_ERROR_DROPPED]; + allowed_packets--; + next0 = RX_ADL_DROP; + } + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) + { + ip6_adl_allowlist_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + + /* verify speculative enqueue, maybe switch current next frame */ + 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); + } + vlib_node_increment_counter (vm, node->node_index, + IP6_ADL_ALLOWLIST_ERROR_ALLOWED, + allowed_packets); + return frame->n_vectors; +} + +VLIB_REGISTER_NODE (ip6_adl_allowlist_node) = { + .name = "ip6-adl-allowlist", + .vector_size = sizeof (u32), + .format_trace = format_ip6_adl_allowlist_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(ip6_adl_allowlist_error_strings), + .error_strings = ip6_adl_allowlist_error_strings, + + .n_next_nodes = ADL_RX_N_FEATURES, + + /* edit / add dispositions here */ + .next_nodes = { + [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist", + [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist", + [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist", + [IP4_RX_ADL_INPUT] = "ip4-input", + [IP6_RX_ADL_INPUT] = "ip6-input", + [DEFAULT_RX_ADL_INPUT] = "ethernet-input", + [RX_ADL_DROP] = "error-drop", + }, +}; + +static clib_error_t * +ip6_allowlist_init (vlib_main_t * vm) +{ + return 0; +} + +VLIB_INIT_FUNCTION (ip6_allowlist_init); diff --git a/src/plugins/adl/node.c b/src/plugins/adl/node.c new file mode 100644 index 00000000000..9ee991c4c6a --- /dev/null +++ b/src/plugins/adl/node.c @@ -0,0 +1,309 @@ +/* + * Copyright (c) 2016,2020 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 <plugins/adl/adl.h> +#include <vnet/pg/pg.h> + +typedef struct { + u32 next_index; + u32 sw_if_index; +} adl_input_trace_t; + +/* packet trace format function */ +static u8 * format_adl_input_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 *); + adl_input_trace_t * t = va_arg (*args, adl_input_trace_t *); + + s = format (s, "ADL_INPUT: sw_if_index %d, next index %d", + t->sw_if_index, t->next_index); + return s; +} + +#define foreach_adl_input_error \ +_(PROCESSED, "Allow/Deny packets processed") + +typedef enum { +#define _(sym,str) ADL_INPUT_ERROR_##sym, + foreach_adl_input_error +#undef _ + ADL_INPUT_N_ERROR, +} adl_input_error_t; + +static char * adl_input_error_strings[] = { +#define _(sym,string) string, + foreach_adl_input_error +#undef _ +}; + +VLIB_NODE_FN (adl_input_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + u32 n_left_from, * from, * to_next; + adl_feature_type_t next_index; + adl_main_t *am = &adl_main; + + from = vlib_frame_vector_args (frame); + n_left_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, next1; + u32 sw_if_index0, sw_if_index1; + ethernet_header_t * en0, * en1; + adl_config_main_t * ccm0, * ccm1; + u32 advance0, advance1; + int proto0, proto1; + + /* 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, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); + } + + /* speculatively enqueue b0 and b1 to the current next frame */ + to_next[0] = bi0 = from[0]; + to_next[1] = bi1 = from[1]; + from += 2; + to_next += 2; + n_left_from -= 2; + n_left_to_next -= 2; + + b0 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + en0 = vlib_buffer_get_current (b0); + en1 = vlib_buffer_get_current (b1); + + sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX]; + sw_if_index1 = adl_buffer(b1)->sw_if_index[VLIB_RX]; + + proto0 = VNET_ADL_DEFAULT; + proto1 = VNET_ADL_DEFAULT; + advance0 = 0; + advance1 = 0; + + if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4)) + { + proto0 = VNET_ADL_IP4; + advance0 = sizeof(ethernet_header_t); + } + else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6)) + { + proto0 = VNET_ADL_IP6; + advance0 = sizeof(ethernet_header_t); + } + + if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4)) + { + proto1 = VNET_ADL_IP4; + advance1 = sizeof(ethernet_header_t); + } + else if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6)) + { + proto1 = VNET_ADL_IP6; + advance1 = sizeof(ethernet_header_t); + } + + ccm0 = am->adl_config_mains + proto0; + ccm1 = am->adl_config_mains + proto1; + adl_buffer(b0)->adl.current_config_index = + ccm0->config_index_by_sw_if_index [sw_if_index0]; + + adl_buffer(b1)->adl.current_config_index = + ccm1->config_index_by_sw_if_index [sw_if_index1]; + + vlib_buffer_advance (b0, advance0); + vlib_buffer_advance (b1, advance1); + + vnet_get_config_data (&ccm0->config_main, + &adl_buffer(b0)->adl.current_config_index, + &next0, 0 /* bytes of config data */); + + vnet_get_config_data (&ccm1->config_main, + &adl_buffer(b1)->adl.current_config_index, + &next1, 0 /* bytes of config data */); + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) + { + adl_input_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b1->flags & VLIB_BUFFER_IS_TRACED))) + { + adl_input_trace_t *t = + vlib_add_trace (vm, node, b1, sizeof (*t)); + t->sw_if_index = sw_if_index1; + t->next_index = next1; + } + /* verify speculative enqueues, maybe switch current next frame */ + 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; + u32 sw_if_index0; + ethernet_header_t *en0; + adl_config_main_t *ccm0; + u32 advance0; + int proto0; + + /* speculatively enqueue b0 to the current next frame */ + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + + /* + * Direct from the driver, we should be at offset 0 + * aka at &b0->data[0] + */ + ASSERT (b0->current_data == 0); + + en0 = vlib_buffer_get_current (b0); + + sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX]; + + proto0 = VNET_ADL_DEFAULT; + advance0 = 0; + + if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4)) + { + proto0 = VNET_ADL_IP4; + advance0 = sizeof(ethernet_header_t); + } + else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6)) + { + proto0 = VNET_ADL_IP6; + advance0 = sizeof(ethernet_header_t); + } + + ccm0 = am->adl_config_mains + proto0; + adl_buffer(b0)->adl.current_config_index = + ccm0->config_index_by_sw_if_index [sw_if_index0]; + + vlib_buffer_advance (b0, advance0); + + vnet_get_config_data (&ccm0->config_main, + &adl_buffer(b0)->adl.current_config_index, + &next0, 0 /* bytes of config data */); + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) + { + adl_input_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + + /* verify speculative enqueue, maybe switch current next frame */ + 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); + } + vlib_node_increment_counter (vm, adl_input_node.index, + ADL_INPUT_ERROR_PROCESSED, frame->n_vectors); + return frame->n_vectors; +} + +VLIB_REGISTER_NODE (adl_input_node) = { + .name = "adl-input", + .vector_size = sizeof (u32), + .format_trace = format_adl_input_trace, + .unformat_buffer = unformat_ethernet_header, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(adl_input_error_strings), + .error_strings = adl_input_error_strings, + + .n_next_nodes = ADL_RX_N_FEATURES, + + /* edit / add dispositions here */ + .next_nodes = { + [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist", + [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist", + [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist", + [IP4_RX_ADL_INPUT] = "ip4-input", + [IP6_RX_ADL_INPUT] = "ip6-input", + [DEFAULT_RX_ADL_INPUT] = "ethernet-input", + [RX_ADL_DROP] = "error-drop", + }, +}; + +#define foreach_adl_stub \ +_(default-adl-allowlist, default_adl_allowlist) + +#define _(n,f) \ + \ +static uword \ +f##_node_fn (vlib_main_t * vm, \ + vlib_node_runtime_t * node, \ + vlib_frame_t * frame) \ +{ \ + clib_warning ("BUG: stub function called"); \ + return 0; \ +} \ + \ +VLIB_REGISTER_NODE (f##_input_node) = { \ + .function = f##_node_fn, \ + .name = #n, \ + .vector_size = sizeof (u32), \ + .type = VLIB_NODE_TYPE_INTERNAL, \ + \ + .n_errors = 0, \ + .error_strings = 0, \ + \ + .n_next_nodes = 0, \ +}; + +foreach_adl_stub; diff --git a/src/plugins/adl/setup.pg b/src/plugins/adl/setup.pg new file mode 100644 index 00000000000..7f816bc0893 --- /dev/null +++ b/src/plugins/adl/setup.pg @@ -0,0 +1,62 @@ +set term pag off + +loop create +set int ip address loop0 192.168.1.1/24 +set int ip6 table loop0 0 +set int ip address loop0 2001:db01::1/64 +set int state loop0 up + +packet-generator new { + name ip4 + limit 100 + rate 0 + size 128-128 + interface loop0 + node adl-input + data { IP4: 1.2.40 -> 3cfd.fed0.b6c8 + UDP: 192.168.1.2-192.168.1.10 -> 192.168.2.1 + UDP: 1234 -> 2345 + incrementing 114 + } +} + +packet-generator new { + name ip6-allow + limit 50 + rate 0 + size 128-128 + interface loop0 + node adl-input + data { IP6: 1.2.40 -> 3cfd.fed0.b6c8 + UDP: 2001:db01::2 -> 2001:db01::1 + UDP: 1234 -> 2345 + incrementing 80 + } +} + +packet-generator new { + name ip6-drop + limit 50 + rate 0 + size 128-128 + interface loop0 + node adl-input + data { IP6: 1.2.40 -> 3cfd.fed0.b6c8 + UDP: 2001:db01::3 -> 2001:db01::1 + UDP: 1234 -> 2345 + incrementing 80 + } +} + +ip table 1 +ip route add 192.168.2.1/32 via drop +ip route add table 1 192.168.1.2/32 via local + +ip6 table 1 +ip route add 2001:db01::1/128 via drop +ip route add table 1 2001:db01::2/128 via local + +comment { bin adl_interface_enable_disable loop0 } +comment { bin adl_allowlist_enable_disable loop0 fib-id 1 ip4 ip6 } +uncomment {bin cop_interface_enable_disable loop0 } +uncomment {bin cop_whitelist_enable_disable loop0 fib-id 1 ip4 ip6 } diff --git a/src/plugins/adl/test/test_adl.py b/src/plugins/adl/test/test_adl.py new file mode 100644 index 00000000000..4a996fc5c90 --- /dev/null +++ b/src/plugins/adl/test/test_adl.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 + +import unittest + +from framework import VppTestCase, VppTestRunner, running_gcov_tests +from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath + + +class TestAdl(VppTestCase): + """ Allow/Deny Plugin Unit Test Cases """ + + @classmethod + def setUpClass(cls): + super(TestAdl, cls).setUpClass() + + @classmethod + def tearDownClass(cls): + super(TestAdl, cls).tearDownClass() + + def setUp(self): + super(TestAdl, self).setUp() + + def tearDown(self): + super(TestAdl, self).tearDown() + + def test_adl1_unittest(self): + """ Plugin API Test """ + cmds = ["loop create\n", + "set int ip address loop0 192.168.1.1/24\n", + "set int ip6 table loop0 0\n", + "set int ip address loop0 2001:db01::1/64\n", + "set int state loop0 up\n", + "packet-generator new {\n" + " name ip4\n" + " limit 100\n" + " rate 0\n" + " size 128-128\n" + " interface loop0\n" + " node adl-input\n" + " data { IP4: 1.2.40 -> 3cfd.fed0.b6c8\n" + " UDP: 192.168.1.2-192.168.1.10 -> 192.168.2.1\n" + " UDP: 1234 -> 2345\n" + " incrementing 114\n" + " }\n" + " }\n", + "packet-generator new {\n" + " name ip6-allow\n" + " limit 50\n" + " rate 0\n" + " size 128-128\n" + " interface loop0\n" + " node adl-input\n" + " data { IP6: 1.2.40 -> 3cfd.fed0.b6c8\n" + " UDP: 2001:db01::2 -> 2001:db01::1\n" + " UDP: 1234 -> 2345\n" + " incrementing 80\n" + " }\n" + " }\n", + "packet-generator new {\n" + " name ip6-drop\n" + " limit 50\n" + " rate 0\n" + " size 128-128\n" + " interface loop0\n" + " node adl-input\n" + " data { IP6: 1.2.40 -> 3cfd.fed0.b6c8\n" + " UDP: 2001:db01::3 -> 2001:db01::1\n" + " UDP: 1234 -> 2345\n" + " incrementing 80\n" + " }\n" + " }\n", + "ip table 1\n", + "ip route add 192.168.2.1/32 via drop\n", + "ip route add table 1 192.168.1.2/32 via local\n", + "ip6 table 1\n", + "ip route add 2001:db01::1/128 via drop\n", + "ip route add table 1 2001:db01::2/128 via local\n", + "bin adl_interface_enable_disable loop0\n", + "bin adl_allowlist_enable_disable loop0 fib-id 1 ip4 ip6\n", + "pa en\n"] + + for cmd in cmds: + r = self.vapi.cli_return_response(cmd) + if r.retval != 0: + if hasattr(r, 'reply'): + self.logger.info(cmd + " FAIL reply " + r.reply) + else: + self.logger.info(cmd + " FAIL retval " + str(r.retval)) + + total_pkts = self.statistics.get_err_counter( + "/err/adl-input/Allow/Deny packets processed") + + self.assertEqual(total_pkts, 200) + + ip4_allow = self.statistics.get_err_counter( + "/err/ip4-adl-allowlist/ip4 allowlist allowed") + self.assertEqual(ip4_allow, 12) + ip6_allow = self.statistics.get_err_counter( + "/err/ip6-adl-allowlist/ip6 allowlist allowed") + self.assertEqual(ip6_allow, 50) + +if __name__ == '__main__': + unittest.main(testRunner=VppTestRunner) |