2 * Copyright (c) 2016,2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vnet/ethernet/ethernet.h>
16 #include <vnet/plugin/plugin.h>
17 #include <vpp/app/version.h>
18 #include <plugins/adl/adl.h>
23 adl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
25 adl_main_t *am = &adl_main;
26 adl_config_data_t _data, *data = &_data;
27 vlib_main_t *vm = am->vlib_main;
28 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);;
29 adl_config_main_t *acm;
33 clib_memset (data, 0, sizeof (*data));
36 * Ignore local interface, pg interfaces. $$$ need a #define for the
37 * first "real" interface. The answer is 5 at the moment.
39 if (hi->dev_class_index == vnet_local_interface_device_class.index)
42 for (address_family = VNET_ADL_IP4; address_family < VNET_N_ADLS;
45 acm = &am->adl_config_mains[address_family];
48 * Once-only code to initialize the per-address-family
49 * adl feature subgraphs.
50 * Since the (single) start-node, adl-input, must be able
51 * to push pkts into three separate subgraphs, we
52 * use a unified adl_feature_type_t enumeration.
55 if (!(acm->config_main.node_index_by_feature_index))
57 switch (address_family)
61 static char *start_nodes[] = { "adl-input" };
62 static char *feature_nodes[] = {
63 [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist",
64 [IP4_RX_ADL_INPUT] = "ip4-input",
67 vnet_config_init (vm, &acm->config_main,
68 start_nodes, ARRAY_LEN (start_nodes),
69 feature_nodes, ARRAY_LEN (feature_nodes));
74 static char *start_nodes[] = { "adl-input" };
75 static char *feature_nodes[] = {
76 [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist",
77 [IP6_RX_ADL_INPUT] = "ip6-input",
79 vnet_config_init (vm, &acm->config_main,
80 start_nodes, ARRAY_LEN (start_nodes),
81 feature_nodes, ARRAY_LEN (feature_nodes));
85 case VNET_ADL_DEFAULT:
87 static char *start_nodes[] = { "adl-input" };
88 static char *feature_nodes[] = {
89 [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist",
90 [DEFAULT_RX_ADL_INPUT] = "ethernet-input",
92 vnet_config_init (vm, &acm->config_main,
93 start_nodes, ARRAY_LEN (start_nodes),
94 feature_nodes, ARRAY_LEN (feature_nodes));
103 vec_validate_init_empty (acm->config_index_by_sw_if_index, sw_if_index,
106 ci = acm->config_index_by_sw_if_index[sw_if_index];
108 /* Create a sensible initial config: send pkts to xxx-input */
109 if (address_family == VNET_ADL_IP4)
110 default_next = IP4_RX_ADL_INPUT;
111 else if (address_family == VNET_ADL_IP6)
112 default_next = IP6_RX_ADL_INPUT;
114 default_next = DEFAULT_RX_ADL_INPUT;
117 ci = vnet_config_add_feature (vm, &acm->config_main,
118 ci, default_next, data, sizeof (*data));
121 /* If the feature was actually configured */
124 ci = vnet_config_del_feature (vm, &acm->config_main,
125 ci, default_next, data,
130 acm->config_index_by_sw_if_index[sw_if_index] = ci;
135 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (adl_sw_interface_add_del);
137 static clib_error_t *
138 adl_init (vlib_main_t * vm)
140 adl_main_t *cm = &adl_main;
143 cm->vnet_main = vnet_get_main ();
146 * Setup the packet generator so we can inject ethernet
147 * frames into this node
149 ethernet_setup_node (vm, adl_input_node.index);
154 VLIB_INIT_FUNCTION (adl_init) =
156 .runs_after = VLIB_INITS ("ip4_allowlist_init", "ip6_allowlist_init"),
161 VNET_FEATURE_INIT (adl, static) =
163 .arc_name = "device-input",
164 .node_name = "adl-input",
165 .runs_before = VNET_FEATURES ("ethernet-input"),
169 int adl_interface_enable_disable (u32 sw_if_index, int enable_disable)
172 * Redirect pkts from the driver to the adl node.
174 vnet_feature_enable_disable ("device-input", "adl-input",
175 sw_if_index, enable_disable, 0, 0);
179 static clib_error_t *
180 adl_enable_disable_command_fn (vlib_main_t * vm,
181 unformat_input_t * input,
182 vlib_cli_command_t * cmd)
184 adl_main_t * cm = &adl_main;
185 u32 sw_if_index = ~0;
186 int enable_disable = 1;
190 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
191 if (unformat (input, "disable"))
193 else if (unformat (input, "%U", unformat_vnet_sw_interface,
194 cm->vnet_main, &sw_if_index))
200 if (sw_if_index == ~0)
201 return clib_error_return (0, "Please specify an interface...");
203 rv = adl_interface_enable_disable (sw_if_index, enable_disable);
209 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
210 return clib_error_return
211 (0, "Invalid interface, only works on physical ports");
214 case VNET_API_ERROR_UNIMPLEMENTED:
215 return clib_error_return (0, "Device driver doesn't support redirection");
219 return clib_error_return (0, "adl_interface_enable_disable returned %d",
225 VLIB_CLI_COMMAND (adl_interface_command, static) = {
226 .path = "adl interface",
228 "adl interface <interface-name> [disable]",
229 .function = adl_enable_disable_command_fn,
233 int adl_allowlist_enable_disable (adl_allowlist_enable_disable_args_t *a)
235 adl_main_t * cm = &adl_main;
236 vlib_main_t * vm = cm->vlib_main;
237 ip4_main_t * im4 = &ip4_main;
238 ip6_main_t * im6 = &ip6_main;
241 adl_config_main_t * acm;
242 u32 next_to_add_del = 0;
246 adl_config_data_t _data, *data=&_data;
249 * Enable / disable allowlist processing on the specified interface
252 for (address_family = VNET_ADL_IP4; address_family < VNET_N_ADLS;
255 acm = &cm->adl_config_mains[address_family];
257 switch(address_family)
260 is_add = (a->ip4 != 0);
261 next_to_add_del = IP4_RX_ADL_ALLOWLIST;
262 /* configured opaque data must match, or no supper */
263 p = hash_get (im4->fib_index_by_table_id, a->fib_id);
269 return VNET_API_ERROR_NO_SUCH_FIB;
276 is_add = (a->ip6 != 0);
277 next_to_add_del = IP6_RX_ADL_ALLOWLIST;
278 p = hash_get (im6->fib_index_by_table_id, a->fib_id);
284 return VNET_API_ERROR_NO_SUCH_FIB;
290 case VNET_ADL_DEFAULT:
291 is_add = (a->default_adl != 0);
292 next_to_add_del = DEFAULT_RX_ADL_ALLOWLIST;
296 clib_warning ("BUG");
299 ci = acm->config_index_by_sw_if_index[a->sw_if_index];
300 data->fib_index = fib_index;
303 ci = vnet_config_add_feature (vm, &acm->config_main,
306 data, sizeof (*data));
309 /* If the feature was actually configured... */
313 ci = vnet_config_del_feature (vm, &acm->config_main,
316 data, sizeof (*data));
320 acm->config_index_by_sw_if_index[a->sw_if_index] = ci;
325 static clib_error_t *
326 adl_allowlist_enable_disable_command_fn (vlib_main_t * vm,
327 unformat_input_t * input,
328 vlib_cli_command_t * cmd)
330 adl_main_t * cm = &adl_main;
331 u32 sw_if_index = ~0;
337 adl_allowlist_enable_disable_args_t _a, * a = &_a;
339 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
340 if (unformat (input, "ip4"))
342 else if (unformat (input, "ip6"))
344 else if (unformat (input, "default"))
346 else if (unformat (input, "%U", unformat_vnet_sw_interface,
347 cm->vnet_main, &sw_if_index))
349 else if (unformat (input, "fib-id %d", &fib_id))
355 if (sw_if_index == ~0)
356 return clib_error_return (0, "Please specify an interface...");
358 a->sw_if_index = sw_if_index;
361 a->default_adl = default_adl;
364 rv = adl_allowlist_enable_disable (a);
370 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
371 return clib_error_return
372 (0, "Invalid interface, only works on physical ports");
375 case VNET_API_ERROR_NO_SUCH_FIB:
376 return clib_error_return
380 case VNET_API_ERROR_UNIMPLEMENTED:
381 return clib_error_return (0, "Device driver doesn't support redirection");
385 return clib_error_return (0, "adl_allowlist_enable_disable returned %d",
393 VLIB_CLI_COMMAND (adl_allowlist_command, static) =
395 .path = "adl allowlist",
397 "adl allowlist <interface-name> [ip4][ip6][default][fib-id <NN>][disable]",
398 .function = adl_allowlist_enable_disable_command_fn,
403 VLIB_PLUGIN_REGISTER () =
405 .version = VPP_BUILD_VER,
406 .description = "Allow/deny list plugin",
412 * fd.io coding-style-patch-verification: ON
415 * eval: (c-set-style "gnu")