blob: 4cf12a24e9e44d86726f293938766446448383c2 [file] [log] [blame]
Matus Fabian70e6a8d2016-06-20 08:10:42 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include <vnet/classify/policer_classify.h>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070016#include <vnet/l2/l2_input.h>
Matus Fabian70e6a8d2016-06-20 08:10:42 -070017
Dave Wallace71612d62017-10-24 01:32:41 -040018policer_classify_main_t policer_classify_main;
19
Matus Fabian70e6a8d2016-06-20 08:10:42 -070020static void
21vnet_policer_classify_feature_enable (vlib_main_t * vnm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +053022 policer_classify_main_t * pcm,
23 u32 sw_if_index,
24 policer_classify_table_id_t tid,
25 int feature_enable)
Matus Fabian70e6a8d2016-06-20 08:10:42 -070026{
27 if (tid == POLICER_CLASSIFY_TABLE_L2)
28 {
29 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_POLICER_CLAS,
khemendra kumard7bfa0e2017-11-27 15:15:53 +053030 feature_enable);
Matus Fabian70e6a8d2016-06-20 08:10:42 -070031 }
32 else
33 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +053034 vnet_feature_config_main_t *fcm;
Damjan Marion8b3191e2016-11-09 19:54:20 +010035 u8 arc;
Matus Fabian70e6a8d2016-06-20 08:10:42 -070036
37 if (tid == POLICER_CLASSIFY_TABLE_IP4)
Damjan Marion8b3191e2016-11-09 19:54:20 +010038 {
39 vnet_feature_enable_disable ("ip4-unicast", "ip4-policer-classify",
40 sw_if_index, feature_enable, 0, 0);
41 arc = vnet_get_feature_arc_index ("ip4-unicast");
42 }
khemendra kumard7bfa0e2017-11-27 15:15:53 +053043
Matus Fabian70e6a8d2016-06-20 08:10:42 -070044 else
Damjan Marion8b3191e2016-11-09 19:54:20 +010045 {
46 vnet_feature_enable_disable ("ip6-unicast", "ip6-policer-classify",
47 sw_if_index, feature_enable, 0, 0);
48 arc = vnet_get_feature_arc_index ("ip6-unicast");
49 }
Matus Fabian70e6a8d2016-06-20 08:10:42 -070050
Damjan Marion8b3191e2016-11-09 19:54:20 +010051 fcm = vnet_get_feature_arc_config_main (arc);
52 pcm->vnet_config_main[tid] = &fcm->config_main;
Matus Fabian70e6a8d2016-06-20 08:10:42 -070053 }
54}
55
khemendra kumard7bfa0e2017-11-27 15:15:53 +053056int
57vnet_set_policer_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
58 u32 ip4_table_index, u32 ip6_table_index,
59 u32 l2_table_index, u32 is_add)
Matus Fabian70e6a8d2016-06-20 08:10:42 -070060{
khemendra kumard7bfa0e2017-11-27 15:15:53 +053061 policer_classify_main_t *pcm = &policer_classify_main;
62 vnet_classify_main_t *vcm = pcm->vnet_classify_main;
63 u32 pct[POLICER_CLASSIFY_N_TABLES] = { ip4_table_index, ip6_table_index,
64 l2_table_index
65 };
Matus Fabian70e6a8d2016-06-20 08:10:42 -070066 u32 ti;
67
68 /* Assume that we've validated sw_if_index in the API layer */
69
70 for (ti = 0; ti < POLICER_CLASSIFY_N_TABLES; ti++)
71 {
72 if (pct[ti] == ~0)
khemendra kumard7bfa0e2017-11-27 15:15:53 +053073 continue;
Matus Fabian70e6a8d2016-06-20 08:10:42 -070074
75 if (pool_is_free_index (vcm->tables, pct[ti]))
khemendra kumard7bfa0e2017-11-27 15:15:53 +053076 return VNET_API_ERROR_NO_SUCH_TABLE;
Matus Fabian70e6a8d2016-06-20 08:10:42 -070077
78 vec_validate_init_empty
khemendra kumard7bfa0e2017-11-27 15:15:53 +053079 (pcm->classify_table_index_by_sw_if_index[ti], sw_if_index, ~0);
Matus Fabian70e6a8d2016-06-20 08:10:42 -070080
81 /* Reject any DEL operation with wrong sw_if_index */
82 if (!is_add &&
khemendra kumard7bfa0e2017-11-27 15:15:53 +053083 (pct[ti] !=
84 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index]))
85 {
86 clib_warning
87 ("Non-existent intf_idx=%d with table_index=%d for delete",
88 sw_if_index, pct[ti]);
89 return VNET_API_ERROR_NO_SUCH_TABLE;
90 }
Matus Fabian70e6a8d2016-06-20 08:10:42 -070091
92 /* Return ok on ADD operaton if feature is already enabled */
93 if (is_add &&
khemendra kumard7bfa0e2017-11-27 15:15:53 +053094 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] != ~0)
95 return 0;
Matus Fabian70e6a8d2016-06-20 08:10:42 -070096
97 vnet_policer_classify_feature_enable (vm, pcm, sw_if_index, ti, is_add);
98
99 if (is_add)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530100 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = pct[ti];
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700101 else
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530102 pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = ~0;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700103 }
104
105
106 return 0;
107}
108
109static clib_error_t *
110set_policer_classify_command_fn (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530111 unformat_input_t * input,
112 vlib_cli_command_t * cmd)
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700113{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530114 vnet_main_t *vnm = vnet_get_main ();
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700115 u32 sw_if_index = ~0;
116 u32 ip4_table_index = ~0;
117 u32 ip6_table_index = ~0;
118 u32 l2_table_index = ~0;
119 u32 is_add = 1;
120 u32 idx_cnt = 0;
121 int rv;
122
123 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
124 {
125 if (unformat (input, "interface %U", unformat_vnet_sw_interface,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530126 vnm, &sw_if_index))
127 ;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700128 else if (unformat (input, "ip4-table %d", &ip4_table_index))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530129 idx_cnt++;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700130 else if (unformat (input, "ip6-table %d", &ip6_table_index))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530131 idx_cnt++;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700132 else if (unformat (input, "l2-table %d", &l2_table_index))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530133 idx_cnt++;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700134 else if (unformat (input, "del"))
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530135 is_add = 0;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700136 else
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530137 break;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700138 }
139
140 if (sw_if_index == ~0)
141 return clib_error_return (0, "Interface must be specified.");
142
143 if (!idx_cnt)
144 return clib_error_return (0, "Table index should be specified.");
145
146 if (idx_cnt > 1)
147 return clib_error_return (0, "Only one table index per API is allowed.");
148
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530149 rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
150 ip6_table_index, l2_table_index,
151 is_add);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700152
153 switch (rv)
154 {
155 case 0:
156 break;
157
158 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
159 return clib_error_return (0, "No such interface");
160
161 case VNET_API_ERROR_NO_SUCH_ENTRY:
162 return clib_error_return (0, "No such classifier table");
163 }
164 return 0;
165}
166
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530167/* *INDENT-OFF* */
Chris Luked4024f52016-09-06 09:32:36 -0400168VLIB_CLI_COMMAND (set_policer_classify_command, static) = {
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700169 .path = "set policer classify",
170 .short_help =
171 "set policer classify interface <int> [ip4-table <index>]\n"
172 " [ip6-table <index>] [l2-table <index>] [del]",
173 .function = set_policer_classify_command_fn,
174};
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530175/* *INDENT-ON* */
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700176
177static uword
178unformat_table_type (unformat_input_t * input, va_list * va)
179{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530180 u32 *r = va_arg (*va, u32 *);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700181 u32 tid;
182
183 if (unformat (input, "ip4"))
184 tid = POLICER_CLASSIFY_TABLE_IP4;
185 else if (unformat (input, "ip6"))
186 tid = POLICER_CLASSIFY_TABLE_IP6;
187 else if (unformat (input, "l2"))
188 tid = POLICER_CLASSIFY_TABLE_L2;
189 else
190 return 0;
191
192 *r = tid;
193 return 1;
194}
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530195
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700196static clib_error_t *
197show_policer_classify_command_fn (vlib_main_t * vm,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530198 unformat_input_t * input,
199 vlib_cli_command_t * cmd)
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700200{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530201 policer_classify_main_t *pcm = &policer_classify_main;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700202 u32 type = POLICER_CLASSIFY_N_TABLES;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530203 u32 *vec_tbl;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700204 int i;
205
206 if (unformat (input, "type %U", unformat_table_type, &type))
207 ;
208 else
209 return clib_error_return (0, "Type must be specified.");;
210
211 if (type == POLICER_CLASSIFY_N_TABLES)
212 return clib_error_return (0, "Invalid table type.");
213
214 vec_tbl = pcm->classify_table_index_by_sw_if_index[type];
215
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530216 if (vec_len (vec_tbl))
217 vlib_cli_output (vm, "%10s%20s\t\t%s", "Intfc idx", "Classify table",
218 "Interface name");
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700219 else
220 vlib_cli_output (vm, "No tables configured.");
221
222 for (i = 0; i < vec_len (vec_tbl); i++)
223 {
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530224 if (vec_elt (vec_tbl, i) == ~0)
225 continue;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700226
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530227 vlib_cli_output (vm, "%10d%20d\t\t%U", i, vec_elt (vec_tbl, i),
228 format_vnet_sw_if_index_name, pcm->vnet_main, i);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700229 }
230
231 return 0;
232}
233
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530234/* *INDENT-OFF* */
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700235VLIB_CLI_COMMAND (show_policer_classify_command, static) = {
236 .path = "show classify policer",
237 .short_help = "show classify policer type [ip4|ip6|l2]",
238 .function = show_policer_classify_command_fn,
239};
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530240/* *INDENT-ON* */
241
242/*
243 * fd.io coding-style-patch-verification: ON
244 *
245 * Local Variables:
246 * eval: (c-set-style "gnu")
247 * End:
248 */