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/classify/flow_classify.h>
18 vnet_flow_classify_feature_enable (vlib_main_t * vnm,
19 flow_classify_main_t * fcm,
21 flow_classify_table_id_t tid,
24 vnet_feature_config_main_t *vfcm;
27 if (tid == FLOW_CLASSIFY_TABLE_IP4)
29 vnet_feature_enable_disable ("ip4-unicast", "ip4-flow-classify",
30 sw_if_index, feature_enable, 0, 0);
31 arc = vnet_get_feature_arc_index ("ip4-unicast");
35 vnet_feature_enable_disable ("ip6-unicast", "ip6-flow-classify",
36 sw_if_index, feature_enable, 0, 0);
37 arc = vnet_get_feature_arc_index ("ip6-unicast");
40 vfcm = vnet_get_feature_arc_config_main (arc);
41 fcm->vnet_config_main[tid] = &vfcm->config_main;
44 int vnet_set_flow_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
45 u32 ip4_table_index, u32 ip6_table_index,
48 flow_classify_main_t * fcm = &flow_classify_main;
49 vnet_classify_main_t * vcm = fcm->vnet_classify_main;
50 u32 pct[FLOW_CLASSIFY_N_TABLES] = {ip4_table_index, ip6_table_index};
53 /* Assume that we've validated sw_if_index in the API layer */
55 for (ti = 0; ti < FLOW_CLASSIFY_N_TABLES; ti++)
60 if (pool_is_free_index (vcm->tables, pct[ti]))
61 return VNET_API_ERROR_NO_SUCH_TABLE;
63 vec_validate_init_empty
64 (fcm->classify_table_index_by_sw_if_index[ti], sw_if_index, ~0);
66 /* Reject any DEL operation with wrong sw_if_index */
68 (pct[ti] != fcm->classify_table_index_by_sw_if_index[ti][sw_if_index]))
70 clib_warning ("Non-existent intf_idx=%d with table_index=%d for delete",
71 sw_if_index, pct[ti]);
72 return VNET_API_ERROR_NO_SUCH_TABLE;
75 /* Return ok on ADD operaton if feature is already enabled */
77 fcm->classify_table_index_by_sw_if_index[ti][sw_if_index] != ~0)
80 vnet_flow_classify_feature_enable (vm, fcm, sw_if_index, ti, is_add);
83 fcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = pct[ti];
85 fcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = ~0;
93 set_flow_classify_command_fn (vlib_main_t * vm,
94 unformat_input_t * input,
95 vlib_cli_command_t * cmd)
97 vnet_main_t * vnm = vnet_get_main();
99 u32 ip4_table_index = ~0;
100 u32 ip6_table_index = ~0;
105 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
107 if (unformat (input, "interface %U", unformat_vnet_sw_interface,
110 else if (unformat (input, "ip4-table %d", &ip4_table_index))
112 else if (unformat (input, "ip6-table %d", &ip6_table_index))
114 else if (unformat (input, "del"))
120 if (sw_if_index == ~0)
121 return clib_error_return (0, "Interface must be specified.");
124 return clib_error_return (0, "Table index should be specified.");
127 return clib_error_return (0, "Only one table index per API is allowed.");
129 rv = vnet_set_flow_classify_intfc(vm, sw_if_index, ip4_table_index,
130 ip6_table_index, is_add);
137 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
138 return clib_error_return (0, "No such interface");
140 case VNET_API_ERROR_NO_SUCH_ENTRY:
141 return clib_error_return (0, "No such classifier table");
146 VLIB_CLI_COMMAND (set_input_acl_command, static) = {
147 .path = "set flow classify",
149 "set flow classify interface <int> [ip4-table <index>]\n"
150 " [ip6-table <index>] [del]",
151 .function = set_flow_classify_command_fn,
155 unformat_table_type (unformat_input_t * input, va_list * va)
157 u32 * r = va_arg (*va, u32 *);
160 if (unformat (input, "ip4"))
161 tid = FLOW_CLASSIFY_TABLE_IP4;
162 else if (unformat (input, "ip6"))
163 tid = FLOW_CLASSIFY_TABLE_IP6;
170 static clib_error_t *
171 show_flow_classify_command_fn (vlib_main_t * vm,
172 unformat_input_t * input,
173 vlib_cli_command_t * cmd)
175 flow_classify_main_t * fcm = &flow_classify_main;
176 u32 type = FLOW_CLASSIFY_N_TABLES;
180 if (unformat (input, "type %U", unformat_table_type, &type))
183 return clib_error_return (0, "Type must be specified.");;
185 if (type == FLOW_CLASSIFY_N_TABLES)
186 return clib_error_return (0, "Invalid table type.");
188 vec_tbl = fcm->classify_table_index_by_sw_if_index[type];
190 if (vec_len(vec_tbl))
191 vlib_cli_output (vm, "%10s%20s\t\t%s", "Intfc idx", "Classify table",
194 vlib_cli_output (vm, "No tables configured.");
196 for (i = 0; i < vec_len (vec_tbl); i++)
198 if (vec_elt(vec_tbl, i) == ~0)
201 vlib_cli_output (vm, "%10d%20d\t\t%U", i, vec_elt(vec_tbl, i),
202 format_vnet_sw_if_index_name, fcm->vnet_main, i);
208 VLIB_CLI_COMMAND (show_flow_classify_command, static) = {
209 .path = "show classify flow",
210 .short_help = "show classify flow type [ip4|ip6]",
211 .function = show_flow_classify_command_fn,