2 * Copyright (c) 2016 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/cop/cop.h>
20 cop_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
22 cop_main_t * cm = &cop_main;
23 cop_config_data_t _data, *data = &_data;
24 vlib_main_t * vm = cm->vlib_main;
25 vnet_hw_interface_t * hi = vnet_get_sup_hw_interface (vnm, sw_if_index);;
26 cop_config_main_t * ccm;
30 clib_memset (data, 0, sizeof(*data));
33 * Ignore local interface, pg interfaces. $$$ need a #define for the
34 * first "real" interface. The answer is 5 at the moment.
36 if (hi->dev_class_index == vnet_local_interface_device_class.index)
39 for (address_family = VNET_COP_IP4; address_family < VNET_N_COPS;
42 ccm = &cm->cop_config_mains[address_family];
45 * Once-only code to initialize the per-address-family
46 * cop feature subgraphs.
47 * Since the (single) start-node, cop-input, must be able
48 * to push pkts into three separate subgraphs, we
49 * use a unified cop_feature_type_t enumeration.
52 if (!(ccm->config_main.node_index_by_feature_index))
54 switch (address_family)
58 static char * start_nodes[] = { "cop-input" };
59 static char * feature_nodes[] = {
60 [IP4_RX_COP_WHITELIST] = "ip4-cop-whitelist",
61 [IP4_RX_COP_INPUT] = "ip4-input",
64 vnet_config_init (vm, &ccm->config_main,
65 start_nodes, ARRAY_LEN(start_nodes),
66 feature_nodes, ARRAY_LEN(feature_nodes));
71 static char * start_nodes[] = { "cop-input" };
72 static char * feature_nodes[] = {
73 [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist",
74 [IP6_RX_COP_INPUT] = "ip6-input",
76 vnet_config_init (vm, &ccm->config_main,
77 start_nodes, ARRAY_LEN(start_nodes),
78 feature_nodes, ARRAY_LEN(feature_nodes));
82 case VNET_COP_DEFAULT:
84 static char * start_nodes[] = { "cop-input" };
85 static char * feature_nodes[] = {
86 [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist",
87 [DEFAULT_RX_COP_INPUT] = "ethernet-input",
89 vnet_config_init (vm, &ccm->config_main,
90 start_nodes, ARRAY_LEN(start_nodes),
91 feature_nodes, ARRAY_LEN(feature_nodes));
100 vec_validate_init_empty (ccm->config_index_by_sw_if_index, sw_if_index,
103 ci = ccm->config_index_by_sw_if_index[sw_if_index];
105 /* Create a sensible initial config: send pkts to xxx-input */
106 if (address_family == VNET_COP_IP4)
107 default_next = IP4_RX_COP_INPUT;
108 else if (address_family == VNET_COP_IP6)
109 default_next = IP6_RX_COP_INPUT;
111 default_next = DEFAULT_RX_COP_INPUT;
114 ci = vnet_config_add_feature (vm, &ccm->config_main,
117 data, sizeof(*data));
119 ci = vnet_config_del_feature (vm, &ccm->config_main,
122 data, sizeof(*data));
124 ccm->config_index_by_sw_if_index[sw_if_index] = ci;
129 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (cop_sw_interface_add_del);
131 static clib_error_t *
132 cop_init (vlib_main_t *vm)
134 cop_main_t * cm = &cop_main;
137 cm->vnet_main = vnet_get_main();
143 VLIB_INIT_FUNCTION (cop_init) =
145 .runs_after = VLIB_INITS ("ip4_whitelist_init", "ip6_whitelist_init"),
149 int cop_interface_enable_disable (u32 sw_if_index, int enable_disable)
151 cop_main_t * cm = &cop_main;
152 vnet_sw_interface_t * sw;
154 u32 node_index = enable_disable ? cop_input_node.index : ~0;
156 /* Not a physical port? */
157 sw = vnet_get_sw_interface (cm->vnet_main, sw_if_index);
158 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
159 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
162 * Redirect pkts from the driver to the cop node.
163 * Returns VNET_API_ERROR_UNIMPLEMENTED if the h/w driver
164 * doesn't implement the API.
166 * Node_index = ~0 => shut off redirection
168 rv = vnet_hw_interface_rx_redirect_to_node (cm->vnet_main, sw_if_index,
173 static clib_error_t *
174 cop_enable_disable_command_fn (vlib_main_t * vm,
175 unformat_input_t * input,
176 vlib_cli_command_t * cmd)
178 cop_main_t * cm = &cop_main;
179 u32 sw_if_index = ~0;
180 int enable_disable = 1;
184 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
185 if (unformat (input, "disable"))
187 else if (unformat (input, "%U", unformat_vnet_sw_interface,
188 cm->vnet_main, &sw_if_index))
194 if (sw_if_index == ~0)
195 return clib_error_return (0, "Please specify an interface...");
197 rv = cop_interface_enable_disable (sw_if_index, enable_disable);
203 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
204 return clib_error_return
205 (0, "Invalid interface, only works on physical ports");
208 case VNET_API_ERROR_UNIMPLEMENTED:
209 return clib_error_return (0, "Device driver doesn't support redirection");
213 return clib_error_return (0, "cop_interface_enable_disable returned %d",
219 VLIB_CLI_COMMAND (cop_interface_command, static) = {
220 .path = "cop interface",
222 "cop interface <interface-name> [disable]",
223 .function = cop_enable_disable_command_fn,
227 int cop_whitelist_enable_disable (cop_whitelist_enable_disable_args_t *a)
229 cop_main_t * cm = &cop_main;
230 vlib_main_t * vm = cm->vlib_main;
231 ip4_main_t * im4 = &ip4_main;
232 ip6_main_t * im6 = &ip6_main;
235 cop_config_main_t * ccm;
236 u32 next_to_add_del = 0;
240 cop_config_data_t _data, *data=&_data;
243 * Enable / disable whitelist processing on the specified interface
246 for (address_family = VNET_COP_IP4; address_family < VNET_N_COPS;
249 ccm = &cm->cop_config_mains[address_family];
251 switch(address_family)
254 is_add = (a->ip4 != 0);
255 next_to_add_del = IP4_RX_COP_WHITELIST;
256 /* configured opaque data must match, or no supper */
257 p = hash_get (im4->fib_index_by_table_id, a->fib_id);
263 return VNET_API_ERROR_NO_SUCH_FIB;
270 is_add = (a->ip6 != 0);
271 next_to_add_del = IP6_RX_COP_WHITELIST;
272 p = hash_get (im6->fib_index_by_table_id, a->fib_id);
278 return VNET_API_ERROR_NO_SUCH_FIB;
284 case VNET_COP_DEFAULT:
285 is_add = (a->default_cop != 0);
286 next_to_add_del = DEFAULT_RX_COP_WHITELIST;
290 clib_warning ("BUG");
293 ci = ccm->config_index_by_sw_if_index[a->sw_if_index];
294 data->fib_index = fib_index;
297 ci = vnet_config_add_feature (vm, &ccm->config_main,
300 data, sizeof (*data));
302 ci = vnet_config_del_feature (vm, &ccm->config_main,
305 data, sizeof (*data));
307 ccm->config_index_by_sw_if_index[a->sw_if_index] = ci;
312 static clib_error_t *
313 cop_whitelist_enable_disable_command_fn (vlib_main_t * vm,
314 unformat_input_t * input,
315 vlib_cli_command_t * cmd)
317 cop_main_t * cm = &cop_main;
318 u32 sw_if_index = ~0;
324 cop_whitelist_enable_disable_args_t _a, * a = &_a;
326 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
327 if (unformat (input, "ip4"))
329 else if (unformat (input, "ip6"))
331 else if (unformat (input, "default"))
333 else if (unformat (input, "%U", unformat_vnet_sw_interface,
334 cm->vnet_main, &sw_if_index))
336 else if (unformat (input, "fib-id %d", &fib_id))
342 if (sw_if_index == ~0)
343 return clib_error_return (0, "Please specify an interface...");
345 a->sw_if_index = sw_if_index;
348 a->default_cop = default_cop;
351 rv = cop_whitelist_enable_disable (a);
357 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
358 return clib_error_return
359 (0, "Invalid interface, only works on physical ports");
362 case VNET_API_ERROR_NO_SUCH_FIB:
363 return clib_error_return
367 case VNET_API_ERROR_UNIMPLEMENTED:
368 return clib_error_return (0, "Device driver doesn't support redirection");
372 return clib_error_return (0, "cop_whitelist_enable_disable returned %d",
379 VLIB_CLI_COMMAND (cop_whitelist_command, static) = {
380 .path = "cop whitelist",
382 "cop whitelist <interface-name> [ip4][ip6][default][fib-id <NN>][disable]",
383 .function = cop_whitelist_enable_disable_command_fn,