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/policer_classify.h>
18 vnet_policer_classify_feature_enable (vlib_main_t * vnm,
19 policer_classify_main_t * pcm,
21 policer_classify_table_id_t tid,
24 if (tid == POLICER_CLASSIFY_TABLE_L2)
26 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_POLICER_CLAS,
31 vnet_feature_config_main_t * fcm;
34 if (tid == POLICER_CLASSIFY_TABLE_IP4)
36 vnet_feature_enable_disable ("ip4-unicast", "ip4-policer-classify",
37 sw_if_index, feature_enable, 0, 0);
38 arc = vnet_get_feature_arc_index ("ip4-unicast");
43 vnet_feature_enable_disable ("ip6-unicast", "ip6-policer-classify",
44 sw_if_index, feature_enable, 0, 0);
45 arc = vnet_get_feature_arc_index ("ip6-unicast");
48 fcm = vnet_get_feature_arc_config_main (arc);
49 pcm->vnet_config_main[tid] = &fcm->config_main;
53 int vnet_set_policer_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
54 u32 ip4_table_index, u32 ip6_table_index,
55 u32 l2_table_index, u32 is_add)
57 policer_classify_main_t * pcm = &policer_classify_main;
58 vnet_classify_main_t * vcm = pcm->vnet_classify_main;
59 u32 pct[POLICER_CLASSIFY_N_TABLES] = {ip4_table_index, ip6_table_index,
63 /* Assume that we've validated sw_if_index in the API layer */
65 for (ti = 0; ti < POLICER_CLASSIFY_N_TABLES; ti++)
70 if (pool_is_free_index (vcm->tables, pct[ti]))
71 return VNET_API_ERROR_NO_SUCH_TABLE;
73 vec_validate_init_empty
74 (pcm->classify_table_index_by_sw_if_index[ti], sw_if_index, ~0);
76 /* Reject any DEL operation with wrong sw_if_index */
78 (pct[ti] != pcm->classify_table_index_by_sw_if_index[ti][sw_if_index]))
80 clib_warning ("Non-existent intf_idx=%d with table_index=%d for delete",
81 sw_if_index, pct[ti]);
82 return VNET_API_ERROR_NO_SUCH_TABLE;
85 /* Return ok on ADD operaton if feature is already enabled */
87 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] != ~0)
90 vnet_policer_classify_feature_enable (vm, pcm, sw_if_index, ti, is_add);
93 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = pct[ti];
95 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = ~0;
102 static clib_error_t *
103 set_policer_classify_command_fn (vlib_main_t * vm,
104 unformat_input_t * input,
105 vlib_cli_command_t * cmd)
107 vnet_main_t * vnm = vnet_get_main();
108 u32 sw_if_index = ~0;
109 u32 ip4_table_index = ~0;
110 u32 ip6_table_index = ~0;
111 u32 l2_table_index = ~0;
116 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
118 if (unformat (input, "interface %U", unformat_vnet_sw_interface,
121 else if (unformat (input, "ip4-table %d", &ip4_table_index))
123 else if (unformat (input, "ip6-table %d", &ip6_table_index))
125 else if (unformat (input, "l2-table %d", &l2_table_index))
127 else if (unformat (input, "del"))
133 if (sw_if_index == ~0)
134 return clib_error_return (0, "Interface must be specified.");
137 return clib_error_return (0, "Table index should be specified.");
140 return clib_error_return (0, "Only one table index per API is allowed.");
142 rv = vnet_set_policer_classify_intfc(vm, sw_if_index, ip4_table_index,
143 ip6_table_index, l2_table_index, is_add);
150 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
151 return clib_error_return (0, "No such interface");
153 case VNET_API_ERROR_NO_SUCH_ENTRY:
154 return clib_error_return (0, "No such classifier table");
159 VLIB_CLI_COMMAND (set_policer_classify_command, static) = {
160 .path = "set policer classify",
162 "set policer classify interface <int> [ip4-table <index>]\n"
163 " [ip6-table <index>] [l2-table <index>] [del]",
164 .function = set_policer_classify_command_fn,
168 unformat_table_type (unformat_input_t * input, va_list * va)
170 u32 * r = va_arg (*va, u32 *);
173 if (unformat (input, "ip4"))
174 tid = POLICER_CLASSIFY_TABLE_IP4;
175 else if (unformat (input, "ip6"))
176 tid = POLICER_CLASSIFY_TABLE_IP6;
177 else if (unformat (input, "l2"))
178 tid = POLICER_CLASSIFY_TABLE_L2;
185 static clib_error_t *
186 show_policer_classify_command_fn (vlib_main_t * vm,
187 unformat_input_t * input,
188 vlib_cli_command_t * cmd)
190 policer_classify_main_t * pcm = &policer_classify_main;
191 u32 type = POLICER_CLASSIFY_N_TABLES;
195 if (unformat (input, "type %U", unformat_table_type, &type))
198 return clib_error_return (0, "Type must be specified.");;
200 if (type == POLICER_CLASSIFY_N_TABLES)
201 return clib_error_return (0, "Invalid table type.");
203 vec_tbl = pcm->classify_table_index_by_sw_if_index[type];
205 if (vec_len(vec_tbl))
206 vlib_cli_output (vm, "%10s%20s\t\t%s", "Intfc idx", "Classify table",
209 vlib_cli_output (vm, "No tables configured.");
211 for (i = 0; i < vec_len (vec_tbl); i++)
213 if (vec_elt(vec_tbl, i) == ~0)
216 vlib_cli_output (vm, "%10d%20d\t\t%U", i, vec_elt(vec_tbl, i),
217 format_vnet_sw_if_index_name, pcm->vnet_main, i);
223 VLIB_CLI_COMMAND (show_policer_classify_command, static) = {
224 .path = "show classify policer",
225 .short_help = "show classify policer type [ip4|ip6|l2]",
226 .function = show_policer_classify_command_fn,