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 | |
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')
-rw-r--r-- | src/plugins/adl/CMakeLists.txt | 31 | ||||
-rw-r--r-- | src/plugins/adl/FEATURE.yaml (renamed from src/vnet/cop/FEATURE.yaml) | 6 | ||||
-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 (renamed from src/vnet/cop/ip4_whitelist.c) | 139 | ||||
-rw-r--r-- | src/plugins/adl/ip6_allowlist.c (renamed from src/vnet/cop/ip6_whitelist.c) | 159 | ||||
-rw-r--r-- | src/plugins/adl/node.c (renamed from src/vnet/cop/node1.c) | 156 | ||||
-rw-r--r-- | src/plugins/adl/setup.pg | 62 | ||||
-rw-r--r-- | src/plugins/adl/test/test_adl.py | 103 | ||||
-rw-r--r-- | src/plugins/map/map.api | 4 | ||||
-rw-r--r-- | src/vat/api_format.c | 3 | ||||
-rw-r--r-- | src/vnet/CMakeLists.txt | 19 | ||||
-rw-r--r-- | src/vnet/cop/cop.api | 4 | ||||
-rw-r--r-- | src/vnet/cop/cop.c | 384 | ||||
-rw-r--r-- | src/vnet/cop/cop.h | 89 | ||||
-rw-r--r-- | src/vnet/cop/cop_api.c | 60 |
19 files changed, 1382 insertions, 720 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/vnet/cop/FEATURE.yaml b/src/plugins/adl/FEATURE.yaml index c6fbdb86f05..bd8de2a409f 100644 --- a/src/vnet/cop/FEATURE.yaml +++ b/src/plugins/adl/FEATURE.yaml @@ -1,11 +1,11 @@ --- -name: COP -maintainer: Dave Barach <dbarach@cisco.com> +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 whitelist feature" +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/vnet/cop/ip4_whitelist.c b/src/plugins/adl/ip4_allowlist.c index 484ad3637af..ca7b4c115f0 100644 --- a/src/vnet/cop/ip4_whitelist.c +++ b/src/plugins/adl/ip4_allowlist.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Cisco and/or its affiliates. + * 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: @@ -12,55 +12,58 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include <vnet/cop/cop.h> +#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_cop_whitelist_trace_t; +} ip4_adl_allowlist_trace_t; /* packet trace format function */ -static u8 * format_ip4_cop_whitelist_trace (u8 * s, va_list * args) +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_cop_whitelist_trace_t * t = va_arg (*args, ip4_cop_whitelist_trace_t *); - - s = format (s, "IP4_COP_WHITELIST: sw_if_index %d, next index %d", + 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_cop_whitelist_error \ -_(DROPPED, "ip4 cop whitelist packets dropped") +#define foreach_ip4_adl_allowlist_error \ +_(ALLOWED, "ip4 allowlist allowed") \ +_(DROPPED, "ip4 allowlist dropped") typedef enum { -#define _(sym,str) IP4_COP_WHITELIST_ERROR_##sym, - foreach_ip4_cop_whitelist_error +#define _(sym,str) IP4_ADL_ALLOWLIST_ERROR_##sym, + foreach_ip4_adl_allowlist_error #undef _ - IP4_COP_WHITELIST_N_ERROR, -} ip4_cop_whitelist_error_t; + IP4_ADL_ALLOWLIST_N_ERROR, +} ip4_adl_allowlist_error_t; -static char * ip4_cop_whitelist_error_strings[] = { +static char * ip4_adl_allowlist_error_strings[] = { #define _(sym,string) string, - foreach_ip4_cop_whitelist_error + foreach_ip4_adl_allowlist_error #undef _ }; -VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, +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; - cop_feature_type_t next_index; - cop_main_t *cm = &cop_main; + 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) @@ -77,8 +80,8 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, u32 next0, next1; u32 sw_if_index0, sw_if_index1; ip4_header_t * ip0, * ip1; - cop_config_main_t * ccm0, * ccm1; - cop_config_data_t * c0, * c1; + 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; @@ -88,10 +91,10 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, /* 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); @@ -112,11 +115,11 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, ip0 = vlib_buffer_get_current (b0); - ccm0 = cm->cop_config_mains + VNET_COP_IP4; + ccm0 = cm->adl_config_mains + VNET_ADL_IP4; c0 = vnet_get_config_data (&ccm0->config_main, - &vnet_buffer (b0)->cop.current_config_index, + &adl_buffer (b0)->adl.current_config_index, &next0, sizeof (c0[0])); @@ -140,8 +143,9 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE)) { - b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED]; - next0 = RX_COP_DROP; + b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED]; + allowed_packets--; + next0 = RX_ADL_DROP; } b1 = vlib_get_buffer (vm, bi1); @@ -149,11 +153,11 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, ip1 = vlib_buffer_get_current (b1); - ccm1 = cm->cop_config_mains + VNET_COP_IP4; + ccm1 = cm->adl_config_mains + VNET_ADL_IP4; c1 = vnet_get_config_data (&ccm1->config_main, - &vnet_buffer (b1)->cop.current_config_index, + &adl_buffer (b1)->adl.current_config_index, &next1, sizeof (c1[0])); mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie; @@ -186,14 +190,15 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE)) { - b1->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED]; - next1 = RX_COP_DROP; + 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_cop_whitelist_trace_t *t = + 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; @@ -202,7 +207,7 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) && (b1->flags & VLIB_BUFFER_IS_TRACED))) { - ip4_cop_whitelist_trace_t *t = + 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; @@ -221,8 +226,8 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, u32 next0; u32 sw_if_index0; ip4_header_t * ip0; - cop_config_main_t *ccm0; - cop_config_data_t *c0; + adl_config_main_t *ccm0; + adl_config_data_t *c0; ip4_fib_mtrie_t * mtrie0; ip4_fib_mtrie_leaf_t leaf0; u32 lb_index0; @@ -242,11 +247,11 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, ip0 = vlib_buffer_get_current (b0); - ccm0 = cm->cop_config_mains + VNET_COP_IP4; + ccm0 = cm->adl_config_mains + VNET_ADL_IP4; - c0 = vnet_get_config_data + c0 = vnet_get_config_data (&ccm0->config_main, - &vnet_buffer (b0)->cop.current_config_index, + &adl_buffer (b0)->adl.current_config_index, &next0, sizeof (c0[0])); @@ -254,41 +259,42 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address); - leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, + leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2); - leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, + 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 + 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 + vlib_increment_combined_counter (vcm, thread_index, lb_index0, 1, - vlib_buffer_length_in_chain (vm, b0) + vlib_buffer_length_in_chain (vm, b0) + sizeof(ethernet_header_t)); if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE)) { - b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED]; - next0 = RX_COP_DROP; + 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))) + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) { - ip4_cop_whitelist_trace_t *t = + 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, @@ -297,36 +303,41 @@ VLIB_NODE_FN (ip4_cop_whitelist_node) (vlib_main_t * vm, 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_cop_whitelist_node) = { - .name = "ip4-cop-whitelist", +VLIB_REGISTER_NODE (ip4_adl_allowlist_node) = { + .name = "ip4-adl-allowlist", .vector_size = sizeof (u32), - .format_trace = format_ip4_cop_whitelist_trace, + .format_trace = format_ip4_adl_allowlist_trace, .type = VLIB_NODE_TYPE_INTERNAL, - - .n_errors = ARRAY_LEN(ip4_cop_whitelist_error_strings), - .error_strings = ip4_cop_whitelist_error_strings, - .n_next_nodes = COP_RX_N_FEATURES, + .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_COP_WHITELIST] = "ip4-cop-whitelist", - [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist", - [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist", - [IP4_RX_COP_INPUT] = "ip4-input", - [IP6_RX_COP_INPUT] = "ip6-input", - [DEFAULT_RX_COP_INPUT] = "ethernet-input", - [RX_COP_DROP] = "error-drop", + [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_whitelist_init (vlib_main_t * vm) +ip4_allowlist_init (vlib_main_t * vm) { return 0; } -VLIB_INIT_FUNCTION (ip4_whitelist_init); +VLIB_INIT_FUNCTION (ip4_allowlist_init); diff --git a/src/vnet/cop/ip6_whitelist.c b/src/plugins/adl/ip6_allowlist.c index 3342ed64056..d272f101b43 100644 --- a/src/vnet/cop/ip6_whitelist.c +++ b/src/plugins/adl/ip6_allowlist.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Cisco and/or its affiliates. + * 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: @@ -12,55 +12,58 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include <vnet/cop/cop.h> +#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_cop_whitelist_trace_t; +} ip6_adl_allowlist_trace_t; /* packet trace format function */ -static u8 * format_ip6_cop_whitelist_trace (u8 * s, va_list * args) +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_cop_whitelist_trace_t * t = va_arg (*args, ip6_cop_whitelist_trace_t *); - - s = format (s, "IP6_COP_WHITELIST: sw_if_index %d, next index %d", + 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_cop_whitelist_error \ -_(DROPPED, "ip6 cop whitelist packets dropped") +#define foreach_ip6_adl_allowlist_error \ +_(ALLOWED, "ip6 allowlist allowed") \ +_(DROPPED, "ip6 allowlist dropped") typedef enum { -#define _(sym,str) IP6_COP_WHITELIST_ERROR_##sym, - foreach_ip6_cop_whitelist_error +#define _(sym,str) IP6_ADL_ALLOWLIST_ERROR_##sym, + foreach_ip6_adl_allowlist_error #undef _ - IP6_COP_WHITELIST_N_ERROR, -} ip6_cop_whitelist_error_t; + IP6_ADL_ALLOWLIST_N_ERROR, +} ip6_adl_allowlist_error_t; -static char * ip6_cop_whitelist_error_strings[] = { +static char * ip6_adl_allowlist_error_strings[] = { #define _(sym,string) string, - foreach_ip6_cop_whitelist_error + foreach_ip6_adl_allowlist_error #undef _ }; -VLIB_NODE_FN (ip6_cop_whitelist_node) (vlib_main_t * vm, +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; - cop_feature_type_t next_index; - cop_main_t *cm = &cop_main; + 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) @@ -77,19 +80,19 @@ VLIB_NODE_FN (ip6_cop_whitelist_node) (vlib_main_t * vm, u32 next0, next1; u32 sw_if_index0, sw_if_index1; ip6_header_t * ip0, * ip1; - cop_config_main_t * ccm0, * ccm1; - cop_config_data_t * c0, * c1; + 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); @@ -110,23 +113,24 @@ VLIB_NODE_FN (ip6_cop_whitelist_node) (vlib_main_t * vm, ip0 = vlib_buffer_get_current (b0); - ccm0 = cm->cop_config_mains + VNET_COP_IP6; + ccm0 = cm->adl_config_mains + VNET_ADL_IP6; - c0 = vnet_get_config_data + c0 = vnet_get_config_data (&ccm0->config_main, - &vnet_buffer (b0)->cop.current_config_index, + &adl_buffer (b0)->adl.current_config_index, &next0, sizeof (c0[0])); - lb_index0 = ip6_fib_table_fwding_lookup (c0->fib_index, + 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_COP_WHITELIST_ERROR_DROPPED]; - next0 = RX_COP_DROP; + b0->error = node->errors[IP6_ADL_ALLOWLIST_ERROR_DROPPED]; + allowed_packets--; + next0 = RX_ADL_DROP; } b1 = vlib_get_buffer (vm, bi1); @@ -134,49 +138,50 @@ VLIB_NODE_FN (ip6_cop_whitelist_node) (vlib_main_t * vm, ip1 = vlib_buffer_get_current (b1); - ccm1 = cm->cop_config_mains + VNET_COP_IP6; + ccm1 = cm->adl_config_mains + VNET_ADL_IP6; - c1 = vnet_get_config_data + c1 = vnet_get_config_data (&ccm1->config_main, - &vnet_buffer (b1)->cop.current_config_index, + &adl_buffer (b1)->adl.current_config_index, &next1, sizeof (c1[0])); - lb_index1 = ip6_fib_table_fwding_lookup (c1->fib_index, + 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 + vlib_increment_combined_counter (vcm, thread_index, lb_index0, 1, - vlib_buffer_length_in_chain (vm, b0) + vlib_buffer_length_in_chain (vm, b0) + sizeof(ethernet_header_t)); - vlib_increment_combined_counter + 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_COP_WHITELIST_ERROR_DROPPED]; - next1 = RX_COP_DROP; + 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))) + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) { - ip6_cop_whitelist_trace_t *t = + 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))) + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b1->flags & VLIB_BUFFER_IS_TRACED))) { - ip6_cop_whitelist_trace_t *t = + 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; @@ -195,8 +200,8 @@ VLIB_NODE_FN (ip6_cop_whitelist_node) (vlib_main_t * vm, u32 next0; u32 sw_if_index0; ip6_header_t * ip0; - cop_config_main_t *ccm0; - cop_config_data_t *c0; + adl_config_main_t *ccm0; + adl_config_data_t *c0; u32 lb_index0; const load_balance_t * lb0; const dpo_id_t *dpo0; @@ -210,44 +215,45 @@ VLIB_NODE_FN (ip6_cop_whitelist_node) (vlib_main_t * vm, n_left_to_next -= 1; b0 = vlib_get_buffer (vm, bi0); - sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX]; ip0 = vlib_buffer_get_current (b0); - ccm0 = cm->cop_config_mains + VNET_COP_IP6; + ccm0 = cm->adl_config_mains + VNET_ADL_IP6; - c0 = vnet_get_config_data + c0 = vnet_get_config_data (&ccm0->config_main, - &vnet_buffer (b0)->cop.current_config_index, + &adl_buffer (b0)->adl.current_config_index, &next0, sizeof (c0[0])); - lb_index0 = ip6_fib_table_fwding_lookup (c0->fib_index, + 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 + vlib_increment_combined_counter (vcm, thread_index, lb_index0, 1, - vlib_buffer_length_in_chain (vm, b0) + vlib_buffer_length_in_chain (vm, b0) + sizeof(ethernet_header_t)); if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE)) { - b0->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED]; - next0 = RX_COP_DROP; + 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))) + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) { - ip6_cop_whitelist_trace_t *t = + 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, @@ -256,36 +262,39 @@ VLIB_NODE_FN (ip6_cop_whitelist_node) (vlib_main_t * vm, 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_cop_whitelist_node) = { - .name = "ip6-cop-whitelist", +VLIB_REGISTER_NODE (ip6_adl_allowlist_node) = { + .name = "ip6-adl-allowlist", .vector_size = sizeof (u32), - .format_trace = format_ip6_cop_whitelist_trace, + .format_trace = format_ip6_adl_allowlist_trace, .type = VLIB_NODE_TYPE_INTERNAL, - - .n_errors = ARRAY_LEN(ip6_cop_whitelist_error_strings), - .error_strings = ip6_cop_whitelist_error_strings, - .n_next_nodes = COP_RX_N_FEATURES, + .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_COP_WHITELIST] = "ip4-cop-whitelist", - [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist", - [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist", - [IP4_RX_COP_INPUT] = "ip4-input", - [IP6_RX_COP_INPUT] = "ip6-input", - [DEFAULT_RX_COP_INPUT] = "ethernet-input", - [RX_COP_DROP] = "error-drop", + [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_whitelist_init (vlib_main_t * vm) +ip6_allowlist_init (vlib_main_t * vm) { return 0; } -VLIB_INIT_FUNCTION (ip6_whitelist_init); +VLIB_INIT_FUNCTION (ip6_allowlist_init); diff --git a/src/vnet/cop/node1.c b/src/plugins/adl/node.c index 172c13d312e..9ee991c4c6a 100644 --- a/src/vnet/cop/node1.c +++ b/src/plugins/adl/node.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Cisco and/or its affiliates. + * 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: @@ -13,48 +13,49 @@ * limitations under the License. */ -#include <vnet/cop/cop.h> +#include <plugins/adl/adl.h> +#include <vnet/pg/pg.h> typedef struct { u32 next_index; u32 sw_if_index; -} cop_input_trace_t; +} adl_input_trace_t; /* packet trace format function */ -static u8 * format_cop_input_trace (u8 * s, va_list * args) +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 *); - cop_input_trace_t * t = va_arg (*args, cop_input_trace_t *); - - s = format (s, "COP_INPUT: sw_if_index %d, next index %d", + 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_cop_input_error \ -_(PROCESSED, "COP input packets processed") +#define foreach_adl_input_error \ +_(PROCESSED, "Allow/Deny packets processed") typedef enum { -#define _(sym,str) COP_INPUT_ERROR_##sym, - foreach_cop_input_error +#define _(sym,str) ADL_INPUT_ERROR_##sym, + foreach_adl_input_error #undef _ - COP_INPUT_N_ERROR, -} cop_input_error_t; + ADL_INPUT_N_ERROR, +} adl_input_error_t; -static char * cop_input_error_strings[] = { +static char * adl_input_error_strings[] = { #define _(sym,string) string, - foreach_cop_input_error + foreach_adl_input_error #undef _ }; -VLIB_NODE_FN (cop_input_node) (vlib_main_t * vm, +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; - cop_feature_type_t next_index; - cop_main_t *cm = &cop_main; + adl_feature_type_t next_index; + adl_main_t *am = &adl_main; from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; @@ -74,17 +75,17 @@ VLIB_NODE_FN (cop_input_node) (vlib_main_t * vm, u32 next0, next1; u32 sw_if_index0, sw_if_index1; ethernet_header_t * en0, * en1; - cop_config_main_t * ccm0, * ccm1; + 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); @@ -106,68 +107,68 @@ VLIB_NODE_FN (cop_input_node) (vlib_main_t * vm, en0 = vlib_buffer_get_current (b0); en1 = vlib_buffer_get_current (b1); - sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; - sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX]; + sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX]; + sw_if_index1 = adl_buffer(b1)->sw_if_index[VLIB_RX]; - proto0 = VNET_COP_DEFAULT; - proto1 = VNET_COP_DEFAULT; + 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_COP_IP4; + proto0 = VNET_ADL_IP4; advance0 = sizeof(ethernet_header_t); } else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6)) { - proto0 = VNET_COP_IP6; + proto0 = VNET_ADL_IP6; advance0 = sizeof(ethernet_header_t); } if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4)) { - proto1 = VNET_COP_IP4; + proto1 = VNET_ADL_IP4; advance1 = sizeof(ethernet_header_t); } else if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6)) { - proto1 = VNET_COP_IP6; + proto1 = VNET_ADL_IP6; advance1 = sizeof(ethernet_header_t); } - ccm0 = cm->cop_config_mains + proto0; - ccm1 = cm->cop_config_mains + proto1; - vnet_buffer(b0)->cop.current_config_index = + 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]; - vnet_buffer(b1)->cop.current_config_index = + 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, - &vnet_buffer(b0)->cop.current_config_index, + &adl_buffer(b0)->adl.current_config_index, &next0, 0 /* bytes of config data */); vnet_get_config_data (&ccm1->config_main, - &vnet_buffer(b1)->cop.current_config_index, + &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))) + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) { - cop_input_trace_t *t = + 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))) + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b1->flags & VLIB_BUFFER_IS_TRACED))) { - cop_input_trace_t *t = + adl_input_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t)); t->sw_if_index = sw_if_index1; t->next_index = next1; @@ -185,7 +186,7 @@ VLIB_NODE_FN (cop_input_node) (vlib_main_t * vm, u32 next0; u32 sw_if_index0; ethernet_header_t *en0; - cop_config_main_t *ccm0; + adl_config_main_t *ccm0; u32 advance0; int proto0; @@ -199,7 +200,7 @@ VLIB_NODE_FN (cop_input_node) (vlib_main_t * vm, b0 = vlib_get_buffer (vm, bi0); - /* + /* * Direct from the driver, we should be at offset 0 * aka at &b0->data[0] */ @@ -207,41 +208,41 @@ VLIB_NODE_FN (cop_input_node) (vlib_main_t * vm, en0 = vlib_buffer_get_current (b0); - sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX]; - proto0 = VNET_COP_DEFAULT; + proto0 = VNET_ADL_DEFAULT; advance0 = 0; if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4)) { - proto0 = VNET_COP_IP4; + proto0 = VNET_ADL_IP4; advance0 = sizeof(ethernet_header_t); } else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6)) { - proto0 = VNET_COP_IP6; + proto0 = VNET_ADL_IP6; advance0 = sizeof(ethernet_header_t); } - ccm0 = cm->cop_config_mains + proto0; - vnet_buffer(b0)->cop.current_config_index = + 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, - &vnet_buffer(b0)->cop.current_config_index, + &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))) + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) { - cop_input_trace_t *t = + 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, @@ -250,36 +251,37 @@ VLIB_NODE_FN (cop_input_node) (vlib_main_t * vm, vlib_put_next_frame (vm, node, next_index, n_left_to_next); } - vlib_node_increment_counter (vm, cop_input_node.index, - COP_INPUT_ERROR_PROCESSED, frame->n_vectors); + vlib_node_increment_counter (vm, adl_input_node.index, + ADL_INPUT_ERROR_PROCESSED, frame->n_vectors); return frame->n_vectors; } -VLIB_REGISTER_NODE (cop_input_node) = { - .name = "cop-input", +VLIB_REGISTER_NODE (adl_input_node) = { + .name = "adl-input", .vector_size = sizeof (u32), - .format_trace = format_cop_input_trace, + .format_trace = format_adl_input_trace, + .unformat_buffer = unformat_ethernet_header, .type = VLIB_NODE_TYPE_INTERNAL, - - .n_errors = ARRAY_LEN(cop_input_error_strings), - .error_strings = cop_input_error_strings, - .n_next_nodes = COP_RX_N_FEATURES, + .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_COP_WHITELIST] = "ip4-cop-whitelist", - [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist", - [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist", - [IP4_RX_COP_INPUT] = "ip4-input", - [IP6_RX_COP_INPUT] = "ip6-input", - [DEFAULT_RX_COP_INPUT] = "ethernet-input", - [RX_COP_DROP] = "error-drop", + [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_cop_stub \ -_(default-cop-whitelist, default_cop_whitelist) +#define foreach_adl_stub \ +_(default-adl-allowlist, default_adl_allowlist) #define _(n,f) \ \ @@ -304,10 +306,4 @@ VLIB_REGISTER_NODE (f##_input_node) = { \ .n_next_nodes = 0, \ }; -foreach_cop_stub; - - - - - - +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) diff --git a/src/plugins/map/map.api b/src/plugins/map/map.api index 0ae1901e07a..adf4d0a5bbd 100644 --- a/src/plugins/map/map.api +++ b/src/plugins/map/map.api @@ -112,7 +112,7 @@ define map_domains_get_reply define map_domain_dump { - option deprecated="v20.12"; + option deprecated="v21.01"; u32 client_index; u32 context; }; @@ -162,7 +162,7 @@ define map_rule_details /** \brief Enable or disable a MAP interface @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request - @param sw_if_index - + @param sw_if_index - @param is_enable - 0=disable, 1=enable interface @param is_translation - 0=encapsulation, 1=translation */ diff --git a/src/vat/api_format.c b/src/vat/api_format.c index a3a67c1eb57..d95fb8e9ee0 100644 --- a/src/vat/api_format.c +++ b/src/vat/api_format.c @@ -2,7 +2,7 @@ *------------------------------------------------------------------ * api_format.c * - * Copyright (c) 2014-2016 Cisco and/or its affiliates. + * Copyright (c) 2014-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: @@ -43,7 +43,6 @@ #include <vnet/mpls/mpls.h> #include <vnet/ipsec/ipsec.h> #include <inttypes.h> -#include <vnet/cop/cop.h> #include <vnet/ip/ip6_hop_by_hop.h> #include <vnet/ip/ip_source_and_port_range_check.h> #include <vnet/policer/xlate.h> diff --git a/src/vnet/CMakeLists.txt b/src/vnet/CMakeLists.txt index bd1b75b54a1..9ee0eeba070 100644 --- a/src/vnet/CMakeLists.txt +++ b/src/vnet/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2018 Cisco and/or its affiliates. +# Copyright (c) 2018-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: @@ -105,26 +105,13 @@ list(APPEND VNET_API_FILES ) ############################################################################## -# Cop - junk filter +# Binary API compatibility until vpp 20.12, see the ADL plugin... ############################################################################## + list(APPEND VNET_SOURCES - cop/cop.c - cop/node1.c - cop/ip4_whitelist.c - cop/ip6_whitelist.c cop/cop_api.c ) -list(APPEND VNET_MULTIARCH_SOURCES - cop/node1.c - cop/ip4_whitelist.c - cop/ip6_whitelist.c -) - -list(APPEND VNET_HEADERS - cop/cop.h -) - list(APPEND VNET_API_FILES cop/cop.api) ############################################################################## diff --git a/src/vnet/cop/cop.api b/src/vnet/cop/cop.api index f0f267c4a87..39677a26673 100644 --- a/src/vnet/cop/cop.api +++ b/src/vnet/cop/cop.api @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015-2016 Cisco and/or its affiliates. + * Copyright (c) 2015-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: @@ -26,6 +26,7 @@ import "vnet/interface_types.api"; autoreply define cop_interface_enable_disable { + option deprecated="v21.01"; u32 client_index; u32 context; vl_api_interface_index_t sw_if_index; @@ -46,6 +47,7 @@ autoreply define cop_interface_enable_disable autoreply define cop_whitelist_enable_disable { + option deprecated="v21.01"; u32 client_index; u32 context; vl_api_interface_index_t sw_if_index; diff --git a/src/vnet/cop/cop.c b/src/vnet/cop/cop.c deleted file mode 100644 index edd09856092..00000000000 --- a/src/vnet/cop/cop.c +++ /dev/null @@ -1,384 +0,0 @@ -/* - * Copyright (c) 2016 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/cop/cop.h> - -cop_main_t cop_main; - -static clib_error_t * -cop_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add) -{ - cop_main_t * cm = &cop_main; - cop_config_data_t _data, *data = &_data; - vlib_main_t * vm = cm->vlib_main; - vnet_hw_interface_t * hi = vnet_get_sup_hw_interface (vnm, sw_if_index);; - cop_config_main_t * ccm; - 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_COP_IP4; address_family < VNET_N_COPS; - address_family++) - { - ccm = &cm->cop_config_mains[address_family]; - - /* - * Once-only code to initialize the per-address-family - * cop feature subgraphs. - * Since the (single) start-node, cop-input, must be able - * to push pkts into three separate subgraphs, we - * use a unified cop_feature_type_t enumeration. - */ - - if (!(ccm->config_main.node_index_by_feature_index)) - { - switch (address_family) - { - case VNET_COP_IP4: - { - static char * start_nodes[] = { "cop-input" }; - static char * feature_nodes[] = { - [IP4_RX_COP_WHITELIST] = "ip4-cop-whitelist", - [IP4_RX_COP_INPUT] = "ip4-input", - }; - - vnet_config_init (vm, &ccm->config_main, - start_nodes, ARRAY_LEN(start_nodes), - feature_nodes, ARRAY_LEN(feature_nodes)); - } - break; - case VNET_COP_IP6: - { - static char * start_nodes[] = { "cop-input" }; - static char * feature_nodes[] = { - [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist", - [IP6_RX_COP_INPUT] = "ip6-input", - }; - vnet_config_init (vm, &ccm->config_main, - start_nodes, ARRAY_LEN(start_nodes), - feature_nodes, ARRAY_LEN(feature_nodes)); - } - break; - - case VNET_COP_DEFAULT: - { - static char * start_nodes[] = { "cop-input" }; - static char * feature_nodes[] = { - [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist", - [DEFAULT_RX_COP_INPUT] = "ethernet-input", - }; - vnet_config_init (vm, &ccm->config_main, - start_nodes, ARRAY_LEN(start_nodes), - feature_nodes, ARRAY_LEN(feature_nodes)); - } - break; - - default: - clib_warning ("bug"); - break; - } - } - vec_validate_init_empty (ccm->config_index_by_sw_if_index, sw_if_index, - ~0); - - ci = ccm->config_index_by_sw_if_index[sw_if_index]; - - /* Create a sensible initial config: send pkts to xxx-input */ - if (address_family == VNET_COP_IP4) - default_next = IP4_RX_COP_INPUT; - else if (address_family == VNET_COP_IP6) - default_next = IP6_RX_COP_INPUT; - else - default_next = DEFAULT_RX_COP_INPUT; - - if (is_add) - ci = vnet_config_add_feature (vm, &ccm->config_main, - ci, - default_next, - data, sizeof(*data)); - else - ci = vnet_config_del_feature (vm, &ccm->config_main, - ci, - default_next, - data, sizeof(*data)); - - ccm->config_index_by_sw_if_index[sw_if_index] = ci; - } - return 0; -} - -VNET_SW_INTERFACE_ADD_DEL_FUNCTION (cop_sw_interface_add_del); - -static clib_error_t * -cop_init (vlib_main_t *vm) -{ - cop_main_t * cm = &cop_main; - - cm->vlib_main = vm; - cm->vnet_main = vnet_get_main(); - - return 0; -} - -/* *INDENT-OFF* */ -VLIB_INIT_FUNCTION (cop_init) = -{ - .runs_after = VLIB_INITS ("ip4_whitelist_init", "ip6_whitelist_init"), -}; -/* *INDENT-ON* */ - -int cop_interface_enable_disable (u32 sw_if_index, int enable_disable) -{ - cop_main_t * cm = &cop_main; - vnet_sw_interface_t * sw; - int rv; - u32 node_index = enable_disable ? cop_input_node.index : ~0; - - /* Not a physical port? */ - sw = vnet_get_sw_interface (cm->vnet_main, sw_if_index); - if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE) - return VNET_API_ERROR_INVALID_SW_IF_INDEX; - - /* - * Redirect pkts from the driver to the cop node. - * Returns VNET_API_ERROR_UNIMPLEMENTED if the h/w driver - * doesn't implement the API. - * - * Node_index = ~0 => shut off redirection - */ - rv = vnet_hw_interface_rx_redirect_to_node (cm->vnet_main, sw_if_index, - node_index); - return rv; -} - -static clib_error_t * -cop_enable_disable_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) -{ - cop_main_t * cm = &cop_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 = cop_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, "cop_interface_enable_disable returned %d", - rv); - } - return 0; -} - -VLIB_CLI_COMMAND (cop_interface_command, static) = { - .path = "cop interface", - .short_help = - "cop interface <interface-name> [disable]", - .function = cop_enable_disable_command_fn, -}; - - -int cop_whitelist_enable_disable (cop_whitelist_enable_disable_args_t *a) -{ - cop_main_t * cm = &cop_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; - cop_config_main_t * ccm; - u32 next_to_add_del = 0; - uword * p; - u32 fib_index = 0; - u32 ci; - cop_config_data_t _data, *data=&_data; - - /* - * Enable / disable whitelist processing on the specified interface - */ - - for (address_family = VNET_COP_IP4; address_family < VNET_N_COPS; - address_family++) - { - ccm = &cm->cop_config_mains[address_family]; - - switch(address_family) - { - case VNET_COP_IP4: - is_add = (a->ip4 != 0); - next_to_add_del = IP4_RX_COP_WHITELIST; - /* 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_COP_IP6: - is_add = (a->ip6 != 0); - next_to_add_del = IP6_RX_COP_WHITELIST; - 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_COP_DEFAULT: - is_add = (a->default_cop != 0); - next_to_add_del = DEFAULT_RX_COP_WHITELIST; - break; - - default: - clib_warning ("BUG"); - } - - ci = ccm->config_index_by_sw_if_index[a->sw_if_index]; - data->fib_index = fib_index; - - if (is_add) - ci = vnet_config_add_feature (vm, &ccm->config_main, - ci, - next_to_add_del, - data, sizeof (*data)); - else - ci = vnet_config_del_feature (vm, &ccm->config_main, - ci, - next_to_add_del, - data, sizeof (*data)); - - ccm->config_index_by_sw_if_index[a->sw_if_index] = ci; - } - return 0; -} - -static clib_error_t * -cop_whitelist_enable_disable_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) -{ - cop_main_t * cm = &cop_main; - u32 sw_if_index = ~0; - u8 ip4 = 0; - u8 ip6 = 0; - u8 default_cop = 0; - u32 fib_id = 0; - int rv; - cop_whitelist_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_cop = 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_cop = default_cop; - a->fib_id = fib_id; - - rv = cop_whitelist_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, "cop_whitelist_enable_disable returned %d", - rv); - } - - return 0; -} - -VLIB_CLI_COMMAND (cop_whitelist_command, static) = { - .path = "cop whitelist", - .short_help = - "cop whitelist <interface-name> [ip4][ip6][default][fib-id <NN>][disable]", - .function = cop_whitelist_enable_disable_command_fn, -}; diff --git a/src/vnet/cop/cop.h b/src/vnet/cop/cop.h deleted file mode 100644 index ee848ce872e..00000000000 --- a/src/vnet/cop/cop.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2016 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_cop_h__ -#define __vnet_cop_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_COP_IP4, - VNET_COP_IP6, - VNET_COP_DEFAULT, - VNET_N_COPS, -} vnet_cop_t; - -typedef enum { - /* First check src address against whitelist */ - IP4_RX_COP_WHITELIST, - IP6_RX_COP_WHITELIST, - DEFAULT_RX_COP_WHITELIST, - - /* Pkts not otherwise dropped go to xxx-input */ - IP4_RX_COP_INPUT, - IP6_RX_COP_INPUT, - DEFAULT_RX_COP_INPUT, - - /* Going, going, gone... */ - RX_COP_DROP, - - COP_RX_N_FEATURES, -} cop_feature_type_t; - -typedef struct { - vnet_config_main_t config_main; - u32 * config_index_by_sw_if_index; -} cop_config_main_t; - -typedef struct { - u32 fib_index; -} cop_config_data_t; - -typedef struct { - cop_config_main_t cop_config_mains[VNET_N_COPS]; - - /* convenience */ - vlib_main_t * vlib_main; - vnet_main_t * vnet_main; -} cop_main_t; - -extern cop_main_t cop_main; - -extern vlib_node_registration_t cop_input_node; - -int cop_interface_enable_disable (u32 sw_if_index, int enable_disable); - -typedef struct { - u32 sw_if_index; - u8 ip4; - u8 ip6; - u8 default_cop; - u32 fib_id; -} cop_whitelist_enable_disable_args_t; - -int cop_whitelist_enable_disable (cop_whitelist_enable_disable_args_t *a); - -#endif /* __vnet_cop_h__ */ diff --git a/src/vnet/cop/cop_api.c b/src/vnet/cop/cop_api.c index 1415bf03312..6ebcebcbf86 100644 --- a/src/vnet/cop/cop_api.c +++ b/src/vnet/cop/cop_api.c @@ -2,7 +2,7 @@ *------------------------------------------------------------------ * cop_api.c - cop api * - * Copyright (c) 2016 Cisco and/or its affiliates. + * 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: @@ -22,7 +22,6 @@ #include <vnet/interface.h> #include <vnet/api_errno.h> -#include <vnet/cop/cop.h> #include <vnet/vnet_msg_enum.h> @@ -46,6 +45,36 @@ _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable) \ _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable) +typedef struct +{ + u32 sw_if_index; + u8 ip4; + u8 ip6; + u8 default_cop; + u32 fib_id; +} cop_whitelist_enable_disable_args_t; + +/* + * This API will be deprecated in vpp 20.12. + * + * Continue to support it for the moment if the "adl" plugin + * is loaded... + */ +static int default_interface_enable_disable_callback + (u32 sw_if_index, int enable_disable) +{ + return VNET_API_ERROR_UNIMPLEMENTED; +} + +static int (*interface_enable_disable_callback) (u32, int) = + default_interface_enable_disable_callback; + +void +register_vl_api_cop_interface_enable_disable_callback (void *cb) +{ + interface_enable_disable_callback = cb; +} + static void vl_api_cop_interface_enable_disable_t_handler (vl_api_cop_interface_enable_disable_t * mp) { @@ -58,13 +87,36 @@ static void vl_api_cop_interface_enable_disable_t_handler enable_disable = (int) mp->enable_disable; - rv = cop_interface_enable_disable (sw_if_index, enable_disable); + rv = (*interface_enable_disable_callback) (sw_if_index, enable_disable); BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY); } +/* + * This API will be deprecated in vpp 20.12. + * + * Continue to support it for the moment if the "adl" plugin + * is loaded... + */ + +static int default_whitelist_enable_disable_callback + (cop_whitelist_enable_disable_args_t * a) +{ + return VNET_API_ERROR_UNIMPLEMENTED; +} + +static int (*whitelist_enable_disable_callback) + (cop_whitelist_enable_disable_args_t * a) = + default_whitelist_enable_disable_callback; + +void +register_vl_api_cop_whitelist_enable_disable_callback (void *cb) +{ + whitelist_enable_disable_callback = cb; +} + static void vl_api_cop_whitelist_enable_disable_t_handler (vl_api_cop_whitelist_enable_disable_t * mp) { @@ -81,7 +133,7 @@ static void vl_api_cop_whitelist_enable_disable_t_handler a->default_cop = mp->default_cop; a->fib_id = ntohl (mp->fib_id); - rv = cop_whitelist_enable_disable (a); + rv = (*whitelist_enable_disable_callback) (a); BAD_SW_IF_INDEX_LABEL; |