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 ip_lookup_main_t * lm;
32 ip_config_main_t * ipcm;
36 if (tid == POLICER_CLASSIFY_TABLE_IP4)
38 lm = &ip4_main.lookup_main;
39 ftype = ip4_main.ip4_unicast_rx_feature_policer_classify;
43 lm = &ip6_main.lookup_main;
44 ftype = ip6_main.ip6_unicast_rx_feature_policer_classify;
47 ipcm = &lm->rx_config_mains[VNET_UNICAST];
49 ci = ipcm->config_index_by_sw_if_index[sw_if_index];
50 ci = (feature_enable ? vnet_config_add_feature : vnet_config_del_feature)
51 (vnm, &ipcm->config_main, ci, ftype, 0, 0);
53 ipcm->config_index_by_sw_if_index[sw_if_index] = ci;
54 pcm->vnet_config_main[tid] = &ipcm->config_main;
58 int vnet_set_policer_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
59 u32 ip4_table_index, u32 ip6_table_index,
60 u32 l2_table_index, u32 is_add)
62 policer_classify_main_t * pcm = &policer_classify_main;
63 vnet_classify_main_t * vcm = pcm->vnet_classify_main;
64 u32 pct[POLICER_CLASSIFY_N_TABLES] = {ip4_table_index, ip6_table_index,
68 /* Assume that we've validated sw_if_index in the API layer */
70 for (ti = 0; ti < POLICER_CLASSIFY_N_TABLES; ti++)
75 if (pool_is_free_index (vcm->tables, pct[ti]))
76 return VNET_API_ERROR_NO_SUCH_TABLE;
78 vec_validate_init_empty
79 (pcm->classify_table_index_by_sw_if_index[ti], sw_if_index, ~0);
81 /* Reject any DEL operation with wrong sw_if_index */
83 (pct[ti] != pcm->classify_table_index_by_sw_if_index[ti][sw_if_index]))
85 clib_warning ("Non-existent intf_idx=%d with table_index=%d for delete",
86 sw_if_index, pct[ti]);
87 return VNET_API_ERROR_NO_SUCH_TABLE;
90 /* Return ok on ADD operaton if feature is already enabled */
92 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] != ~0)
95 vnet_policer_classify_feature_enable (vm, pcm, sw_if_index, ti, is_add);
98 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = pct[ti];
100 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = ~0;
107 static clib_error_t *
108 set_policer_classify_command_fn (vlib_main_t * vm,
109 unformat_input_t * input,
110 vlib_cli_command_t * cmd)
112 vnet_main_t * vnm = vnet_get_main();
113 u32 sw_if_index = ~0;
114 u32 ip4_table_index = ~0;
115 u32 ip6_table_index = ~0;
116 u32 l2_table_index = ~0;
121 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
123 if (unformat (input, "interface %U", unformat_vnet_sw_interface,
126 else if (unformat (input, "ip4-table %d", &ip4_table_index))
128 else if (unformat (input, "ip6-table %d", &ip6_table_index))
130 else if (unformat (input, "l2-table %d", &l2_table_index))
132 else if (unformat (input, "del"))
138 if (sw_if_index == ~0)
139 return clib_error_return (0, "Interface must be specified.");
142 return clib_error_return (0, "Table index should be specified.");
145 return clib_error_return (0, "Only one table index per API is allowed.");
147 rv = vnet_set_policer_classify_intfc(vm, sw_if_index, ip4_table_index,
148 ip6_table_index, l2_table_index, is_add);
155 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
156 return clib_error_return (0, "No such interface");
158 case VNET_API_ERROR_NO_SUCH_ENTRY:
159 return clib_error_return (0, "No such classifier table");
164 VLIB_CLI_COMMAND (set_input_acl_command, static) = {
165 .path = "set policer classify",
167 "set policer classify interface <int> [ip4-table <index>]\n"
168 " [ip6-table <index>] [l2-table <index>] [del]",
169 .function = set_policer_classify_command_fn,
173 unformat_table_type (unformat_input_t * input, va_list * va)
175 u32 * r = va_arg (*va, u32 *);
178 if (unformat (input, "ip4"))
179 tid = POLICER_CLASSIFY_TABLE_IP4;
180 else if (unformat (input, "ip6"))
181 tid = POLICER_CLASSIFY_TABLE_IP6;
182 else if (unformat (input, "l2"))
183 tid = POLICER_CLASSIFY_TABLE_L2;
190 static clib_error_t *
191 show_policer_classify_command_fn (vlib_main_t * vm,
192 unformat_input_t * input,
193 vlib_cli_command_t * cmd)
195 policer_classify_main_t * pcm = &policer_classify_main;
196 u32 type = POLICER_CLASSIFY_N_TABLES;
200 if (unformat (input, "type %U", unformat_table_type, &type))
203 return clib_error_return (0, "Type must be specified.");;
205 if (type == POLICER_CLASSIFY_N_TABLES)
206 return clib_error_return (0, "Invalid table type.");
208 vec_tbl = pcm->classify_table_index_by_sw_if_index[type];
210 if (vec_len(vec_tbl))
211 vlib_cli_output (vm, "%10s%20s\t\t%s", "Intfc idx", "Classify table",
214 vlib_cli_output (vm, "No tables configured.");
216 for (i = 0; i < vec_len (vec_tbl); i++)
218 if (vec_elt(vec_tbl, i) == ~0)
221 vlib_cli_output (vm, "%10d%20d\t\t%U", i, vec_elt(vec_tbl, i),
222 format_vnet_sw_if_index_name, pcm->vnet_main, i);
228 VLIB_CLI_COMMAND (show_policer_classify_command, static) = {
229 .path = "show classify policer",
230 .short_help = "show classify policer type [ip4|ip6|l2]",
231 .function = show_policer_classify_command_fn,