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 cop_config_main_t * ccm;
29 memset (data, 0, sizeof(*data));
32 * Ignore local interface, pg interfaces. $$$ need a #define for the
33 * first "real" interface. The answer is 5 at the moment.
38 for (address_family = VNET_COP_IP4; address_family < VNET_N_COPS;
41 ccm = &cm->cop_config_mains[address_family];
44 * Once-only code to initialize the per-address-family
45 * cop feature subgraphs.
46 * Since the (single) start-node, cop-input, must be able
47 * to push pkts into three separate subgraphs, we
48 * use a unified cop_feature_type_t enumeration.
51 if (!(ccm->config_main.node_index_by_feature_index))
53 switch (address_family)
57 static char * start_nodes[] = { "cop-input" };
58 static char * feature_nodes[] = {
59 [IP4_RX_COP_WHITELIST] = "ip4-cop-whitelist",
60 [IP4_RX_COP_INPUT] = "ip4-input",
63 vnet_config_init (vm, &ccm->config_main,
64 start_nodes, ARRAY_LEN(start_nodes),
65 feature_nodes, ARRAY_LEN(feature_nodes));
70 static char * start_nodes[] = { "cop-input" };
71 static char * feature_nodes[] = {
72 [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist",
73 [IP6_RX_COP_INPUT] = "ip6-input",
75 vnet_config_init (vm, &ccm->config_main,
76 start_nodes, ARRAY_LEN(start_nodes),
77 feature_nodes, ARRAY_LEN(feature_nodes));
81 case VNET_COP_DEFAULT:
83 static char * start_nodes[] = { "cop-input" };
84 static char * feature_nodes[] = {
85 [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist",
86 [DEFAULT_RX_COP_INPUT] = "ethernet-input",
88 vnet_config_init (vm, &ccm->config_main,
89 start_nodes, ARRAY_LEN(start_nodes),
90 feature_nodes, ARRAY_LEN(feature_nodes));
99 vec_validate_init_empty (ccm->config_index_by_sw_if_index, sw_if_index,
102 ci = ccm->config_index_by_sw_if_index[sw_if_index];
104 /* Create a sensible initial config: send pkts to xxx-input */
105 if (address_family == VNET_COP_IP4)
106 default_next = IP4_RX_COP_INPUT;
107 else if (address_family == VNET_COP_IP6)
108 default_next = IP6_RX_COP_INPUT;
110 default_next = DEFAULT_RX_COP_INPUT;
113 ci = vnet_config_add_feature (vm, &ccm->config_main,
116 data, sizeof(*data));
118 ci = vnet_config_del_feature (vm, &ccm->config_main,
121 data, sizeof(*data));
123 ccm->config_index_by_sw_if_index[sw_if_index] = ci;
128 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (cop_sw_interface_add_del);
130 static clib_error_t *
131 cop_init (vlib_main_t *vm)
133 cop_main_t * cm = &cop_main;
134 clib_error_t * error;
136 if ((error = vlib_call_init_function (vm, ip4_whitelist_init)))
139 if ((error = vlib_call_init_function (vm, ip6_whitelist_init)))
143 cm->vnet_main = vnet_get_main();
148 VLIB_INIT_FUNCTION (cop_init);
150 int cop_interface_enable_disable (u32 sw_if_index, int enable_disable)
152 cop_main_t * cm = &cop_main;
153 vnet_sw_interface_t * sw;
155 u32 node_index = enable_disable ? cop_input_node.index : ~0;
157 /* Not a physical port? */
158 sw = vnet_get_sw_interface (cm->vnet_main, sw_if_index);
159 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
160 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
163 * Redirect pkts from the driver to the cop node.
164 * Returns VNET_API_ERROR_UNIMPLEMENTED if the h/w driver
165 * doesn't implement the API.
167 * Node_index = ~0 => shut off redirection
169 rv = vnet_hw_interface_rx_redirect_to_node (cm->vnet_main, sw_if_index,
174 static clib_error_t *
175 cop_enable_disable_command_fn (vlib_main_t * vm,
176 unformat_input_t * input,
177 vlib_cli_command_t * cmd)
179 cop_main_t * cm = &cop_main;
180 u32 sw_if_index = ~0;
181 int enable_disable = 1;
185 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
186 if (unformat (input, "disable"))
188 else if (unformat (input, "%U", unformat_vnet_sw_interface,
189 cm->vnet_main, &sw_if_index))
195 if (sw_if_index == ~0)
196 return clib_error_return (0, "Please specify an interface...");
198 rv = cop_interface_enable_disable (sw_if_index, enable_disable);
204 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
205 return clib_error_return
206 (0, "Invalid interface, only works on physical ports");
209 case VNET_API_ERROR_UNIMPLEMENTED:
210 return clib_error_return (0, "Device driver doesn't support redirection");
214 return clib_error_return (0, "cop_interface_enable_disable returned %d",
220 VLIB_CLI_COMMAND (cop_interface_command, static) = {
221 .path = "cop interface",
223 "cop interface <interface-name> [disable]",
224 .function = cop_enable_disable_command_fn,
228 int cop_whitelist_enable_disable (cop_whitelist_enable_disable_args_t *a)
230 cop_main_t * cm = &cop_main;
231 vlib_main_t * vm = cm->vlib_main;
232 ip4_main_t * im4 = &ip4_main;
233 ip6_main_t * im6 = &ip6_main;
236 cop_config_main_t * ccm;
237 u32 next_to_add_del = 0;
241 cop_config_data_t _data, *data=&_data;
244 * Enable / disable whitelist processing on the specified interface
247 for (address_family = VNET_COP_IP4; address_family < VNET_N_COPS;
250 ccm = &cm->cop_config_mains[address_family];
252 switch(address_family)
255 is_add = (a->ip4 != 0);
256 next_to_add_del = IP4_RX_COP_WHITELIST;
257 /* configured opaque data must match, or no supper */
258 p = hash_get (im4->fib_index_by_table_id, a->fib_id);
264 return VNET_API_ERROR_NO_SUCH_FIB;
271 is_add = (a->ip6 != 0);
272 next_to_add_del = IP6_RX_COP_WHITELIST;
273 p = hash_get (im6->fib_index_by_table_id, a->fib_id);
279 return VNET_API_ERROR_NO_SUCH_FIB;
285 case VNET_COP_DEFAULT:
286 is_add = (a->default_cop != 0);
287 next_to_add_del = DEFAULT_RX_COP_WHITELIST;
291 clib_warning ("BUG");
294 ci = ccm->config_index_by_sw_if_index[a->sw_if_index];
295 data->fib_index = fib_index;
298 ci = vnet_config_add_feature (vm, &ccm->config_main,
301 data, sizeof (*data));
303 ci = vnet_config_del_feature (vm, &ccm->config_main,
306 data, sizeof (*data));
308 ccm->config_index_by_sw_if_index[a->sw_if_index] = ci;
313 static clib_error_t *
314 cop_whitelist_enable_disable_command_fn (vlib_main_t * vm,
315 unformat_input_t * input,
316 vlib_cli_command_t * cmd)
318 cop_main_t * cm = &cop_main;
319 u32 sw_if_index = ~0;
325 cop_whitelist_enable_disable_args_t _a, * a = &_a;
327 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
328 if (unformat (input, "ip4"))
330 else if (unformat (input, "ip6"))
332 else if (unformat (input, "default"))
334 else if (unformat (input, "%U", unformat_vnet_sw_interface,
335 cm->vnet_main, &sw_if_index))
337 else if (unformat (input, "fib-id %d", &fib_id))
343 if (sw_if_index == ~0)
344 return clib_error_return (0, "Please specify an interface...");
346 a->sw_if_index = sw_if_index;
349 a->default_cop = default_cop;
352 rv = cop_whitelist_enable_disable (a);
358 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
359 return clib_error_return
360 (0, "Invalid interface, only works on physical ports");
363 case VNET_API_ERROR_NO_SUCH_FIB:
364 return clib_error_return
368 case VNET_API_ERROR_UNIMPLEMENTED:
369 return clib_error_return (0, "Device driver doesn't support redirection");
373 return clib_error_return (0, "cop_whitelist_enable_disable returned %d",
380 VLIB_CLI_COMMAND (cop_whitelist_command, static) = {
381 .path = "cop whitelist",
383 "cop whitelist <interface-name> [ip4][ip6][default][fib-id <NN>][disable]",
384 .function = cop_whitelist_enable_disable_command_fn,