blob: df35b472a89c9a5493d209e93f7c9423a8a3c6d0 [file] [log] [blame]
Pavel Kotucekd9aad292017-01-25 08:45:38 +01001/*
2 *------------------------------------------------------------------
3 * policer_api.c - policer api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/policer/policer.h>
26
Filip Tehlar102e8a52021-06-22 08:37:26 +000027#include <vnet/format_fns.h>
28#include <vnet/policer/policer.api_enum.h>
29#include <vnet/policer/policer.api_types.h>
Pavel Kotucekd9aad292017-01-25 08:45:38 +010030
Filip Tehlar102e8a52021-06-22 08:37:26 +000031#define REPLY_MSG_ID_BASE vnet_policer_main.msg_id_base
Pavel Kotucekd9aad292017-01-25 08:45:38 +010032#include <vlibapi/api_helper_macros.h>
33
Pavel Kotucekd9aad292017-01-25 08:45:38 +010034static void
35vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
36{
37 vlib_main_t *vm = vlib_get_main ();
Maxime Peim2d1a62b2023-01-06 11:57:38 +000038 vnet_policer_main_t *pm = &vnet_policer_main;
Pavel Kotucekd9aad292017-01-25 08:45:38 +010039 vl_api_policer_add_del_reply_t *rmp;
40 int rv = 0;
Maxime Peim2d1a62b2023-01-06 11:57:38 +000041 uword *p;
42 char name[sizeof (mp->name) + 1];
Brian Russellc5299ff2021-02-09 10:16:58 +000043 qos_pol_cfg_params_st cfg;
Pavel Kotucekd9aad292017-01-25 08:45:38 +010044 u32 policer_index;
45
Maxime Peim2d1a62b2023-01-06 11:57:38 +000046 snprintf (name, sizeof (name), "%s", mp->name);
Pavel Kotucekd9aad292017-01-25 08:45:38 +010047
Maxime Peim2d1a62b2023-01-06 11:57:38 +000048 if (mp->is_add)
Leung Lai Yungafe31562021-12-27 04:14:20 +000049 {
Maxime Peim2d1a62b2023-01-06 11:57:38 +000050 clib_memset (&cfg, 0, sizeof (cfg));
51 cfg.rfc = (qos_policer_type_en) mp->type;
52 cfg.rnd_type = (qos_round_type_en) mp->round_type;
53 cfg.rate_type = (qos_rate_type_en) mp->rate_type;
54 cfg.rb.kbps.cir_kbps = ntohl (mp->cir);
55 cfg.rb.kbps.eir_kbps = ntohl (mp->eir);
56 cfg.rb.kbps.cb_bytes = clib_net_to_host_u64 (mp->cb);
57 cfg.rb.kbps.eb_bytes = clib_net_to_host_u64 (mp->eb);
58 cfg.conform_action.action_type =
59 (qos_action_type_en) mp->conform_action.type;
60 cfg.conform_action.dscp = mp->conform_action.dscp;
61 cfg.exceed_action.action_type =
62 (qos_action_type_en) mp->exceed_action.type;
63 cfg.exceed_action.dscp = mp->exceed_action.dscp;
64 cfg.violate_action.action_type =
65 (qos_action_type_en) mp->violate_action.type;
66 cfg.violate_action.dscp = mp->violate_action.dscp;
67 cfg.color_aware = mp->color_aware;
68
69 rv = policer_add (vm, (u8 *) name, &cfg, &policer_index);
70 }
71 else
72 {
73 p = hash_get_mem (pm->policer_index_by_name, name);
74
75 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
76 if (p != NULL)
77 rv = policer_del (vm, p[0]);
Leung Lai Yungafe31562021-12-27 04:14:20 +000078 }
Pavel Kotucekd9aad292017-01-25 08:45:38 +010079
Maxime Peim2d1a62b2023-01-06 11:57:38 +000080 REPLY_MACRO2 (VL_API_POLICER_ADD_DEL_REPLY, ({
81 if (rv == 0 && mp->is_add)
82 rmp->policer_index = htonl (policer_index);
83 else
84 rmp->policer_index = ~0;
85 }));
86}
87
88static_always_inline void
89policer_set_configuration (qos_pol_cfg_params_st *cfg,
90 vl_api_policer_config_t *infos)
91{
92 clib_memset (cfg, 0, sizeof (*cfg));
93 cfg->rfc = (qos_policer_type_en) infos->type;
94 cfg->rnd_type = (qos_round_type_en) infos->round_type;
95 cfg->rate_type = (qos_rate_type_en) infos->rate_type;
96 cfg->rb.kbps.cir_kbps = ntohl (infos->cir);
97 cfg->rb.kbps.eir_kbps = ntohl (infos->eir);
98 cfg->rb.kbps.cb_bytes = clib_net_to_host_u64 (infos->cb);
99 cfg->rb.kbps.eb_bytes = clib_net_to_host_u64 (infos->eb);
100 cfg->conform_action.action_type =
101 (qos_action_type_en) infos->conform_action.type;
102 cfg->conform_action.dscp = infos->conform_action.dscp;
103 cfg->exceed_action.action_type =
104 (qos_action_type_en) infos->exceed_action.type;
105 cfg->exceed_action.dscp = infos->exceed_action.dscp;
106 cfg->violate_action.action_type =
107 (qos_action_type_en) infos->violate_action.type;
108 cfg->violate_action.dscp = infos->violate_action.dscp;
109 cfg->color_aware = infos->color_aware;
110}
111
112static void
113vl_api_policer_add_t_handler (vl_api_policer_add_t *mp)
114{
115 vlib_main_t *vm = vlib_get_main ();
116 vl_api_policer_add_reply_t *rmp;
117 int rv = 0;
118 char name[sizeof (mp->name) + 1];
119 qos_pol_cfg_params_st cfg;
120 u32 policer_index;
121
122 snprintf (name, sizeof (name), "%s", mp->name);
123
124 policer_set_configuration (&cfg, &mp->infos);
125
126 rv = policer_add (vm, (u8 *) name, &cfg, &policer_index);
127
128 REPLY_MACRO2 (VL_API_POLICER_ADD_REPLY, ({
129 if (rv == 0)
130 rmp->policer_index = htonl (policer_index);
131 else
132 rmp->policer_index = ~0;
133 }));
134}
135
136static void
137vl_api_policer_del_t_handler (vl_api_policer_del_t *mp)
138{
139 vlib_main_t *vm = vlib_get_main ();
140 vl_api_policer_del_reply_t *rmp;
141 u32 policer_index;
142 int rv = 0;
143
144 policer_index = ntohl (mp->policer_index);
145 rv = policer_del (vm, policer_index);
146
147 REPLY_MACRO (VL_API_POLICER_DEL_REPLY);
148}
149
150static void
151vl_api_policer_update_t_handler (vl_api_policer_update_t *mp)
152{
153 vlib_main_t *vm = vlib_get_main ();
154 vl_api_policer_update_reply_t *rmp;
155 int rv = 0;
156 qos_pol_cfg_params_st cfg;
157 u32 policer_index;
158
159 policer_set_configuration (&cfg, &mp->infos);
160
161 policer_index = ntohl (mp->policer_index);
162 rv = policer_update (vm, policer_index, &cfg);
163
164 REPLY_MACRO (VL_API_POLICER_UPDATE_REPLY);
165}
166
167static void
168vl_api_policer_reset_t_handler (vl_api_policer_reset_t *mp)
169{
170 vlib_main_t *vm = vlib_get_main ();
171 vl_api_policer_reset_reply_t *rmp;
172 u32 policer_index;
173 int rv = 0;
174
175 policer_index = ntohl (mp->policer_index);
176 rv = policer_reset (vm, policer_index);
177
178 REPLY_MACRO (VL_API_POLICER_RESET_REPLY);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100179}
180
181static void
Brian Russell48e26362021-02-10 13:53:42 +0000182vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp)
183{
184 vl_api_policer_bind_reply_t *rmp;
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000185 vnet_policer_main_t *pm = &vnet_policer_main;
186 char name[sizeof (mp->name) + 1];
187 uword *p;
Brian Russell48e26362021-02-10 13:53:42 +0000188 u32 worker_index;
189 u8 bind_enable;
190 int rv;
191
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000192 snprintf (name, sizeof (name), "%s", mp->name);
Brian Russell48e26362021-02-10 13:53:42 +0000193
194 worker_index = ntohl (mp->worker_index);
195 bind_enable = mp->bind_enable;
196
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000197 p = hash_get_mem (pm->policer_index_by_name, name);
198
199 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
200 if (p != NULL)
201 rv = policer_bind_worker (p[0], worker_index, bind_enable);
202
Brian Russell48e26362021-02-10 13:53:42 +0000203 REPLY_MACRO (VL_API_POLICER_BIND_REPLY);
204}
205
206static void
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000207vl_api_policer_bind_v2_t_handler (vl_api_policer_bind_v2_t *mp)
208{
209 vl_api_policer_bind_v2_reply_t *rmp;
210 u32 policer_index;
211 u32 worker_index;
212 u8 bind_enable;
213 int rv;
214
215 policer_index = ntohl (mp->policer_index);
216 worker_index = ntohl (mp->worker_index);
217 bind_enable = mp->bind_enable;
218
219 rv = policer_bind_worker (policer_index, worker_index, bind_enable);
220
221 REPLY_MACRO (VL_API_POLICER_BIND_V2_REPLY);
222}
223
224static void
Brian Russellb0468302021-02-17 15:51:45 +0000225vl_api_policer_input_t_handler (vl_api_policer_input_t *mp)
226{
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000227 vl_api_policer_input_reply_t *rmp;
228 vnet_policer_main_t *pm = &vnet_policer_main;
229 char name[sizeof (mp->name) + 1];
230 uword *p;
Brian Russellb0468302021-02-17 15:51:45 +0000231 u32 sw_if_index;
232 u8 apply;
233 int rv;
234
235 VALIDATE_SW_IF_INDEX (mp);
236
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000237 snprintf (name, sizeof (name), "%s", mp->name);
Brian Russellb0468302021-02-17 15:51:45 +0000238
239 sw_if_index = ntohl (mp->sw_if_index);
240 apply = mp->apply;
241
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000242 p = hash_get_mem (pm->policer_index_by_name, name);
243
244 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
245 if (p != NULL)
246 rv = policer_input (p[0], sw_if_index, VLIB_RX, apply);
Brian Russellb0468302021-02-17 15:51:45 +0000247
248 BAD_SW_IF_INDEX_LABEL;
249 REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
250}
251
252static void
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000253vl_api_policer_input_v2_t_handler (vl_api_policer_input_v2_t *mp)
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +0200254{
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000255 vl_api_policer_input_v2_reply_t *rmp;
256 u32 policer_index;
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +0200257 u32 sw_if_index;
258 u8 apply;
259 int rv;
260
261 VALIDATE_SW_IF_INDEX (mp);
262
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000263 policer_index = ntohl (mp->policer_index);
264 sw_if_index = ntohl (mp->sw_if_index);
265 apply = mp->apply;
266
267 rv = policer_input (policer_index, sw_if_index, VLIB_RX, apply);
268
269 BAD_SW_IF_INDEX_LABEL;
270 REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
271}
272
273static void
274vl_api_policer_output_t_handler (vl_api_policer_output_t *mp)
275{
276 vl_api_policer_output_reply_t *rmp;
277 vnet_policer_main_t *pm = &vnet_policer_main;
278 char name[sizeof (mp->name) + 1];
279 uword *p;
280 u32 sw_if_index;
281 u8 apply;
282 int rv;
283
284 VALIDATE_SW_IF_INDEX (mp);
285
286 snprintf (name, sizeof (name), "%s", mp->name);
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +0200287
288 sw_if_index = ntohl (mp->sw_if_index);
289 apply = mp->apply;
290
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000291 p = hash_get_mem (pm->policer_index_by_name, name);
292
293 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
294 if (p != NULL)
295 rv = policer_input (p[0], sw_if_index, VLIB_TX, apply);
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +0200296
297 BAD_SW_IF_INDEX_LABEL;
298 REPLY_MACRO (VL_API_POLICER_OUTPUT_REPLY);
299}
300
301static void
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000302vl_api_policer_output_v2_t_handler (vl_api_policer_output_v2_t *mp)
303{
304 vl_api_policer_output_reply_t *rmp;
305 u32 policer_index;
306 u32 sw_if_index;
307 u8 apply;
308 int rv;
309
310 VALIDATE_SW_IF_INDEX (mp);
311
312 policer_index = ntohl (mp->policer_index);
313 sw_if_index = ntohl (mp->sw_if_index);
314 apply = mp->apply;
315
316 rv = policer_input (policer_index, sw_if_index, VLIB_TX, apply);
317
318 BAD_SW_IF_INDEX_LABEL;
319 REPLY_MACRO (VL_API_POLICER_OUTPUT_REPLY);
320}
321
322static void
323send_policer_details (qos_pol_cfg_params_st *config, policer_t *policer,
324 vl_api_registration_t *reg, u32 context)
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100325{
326 vl_api_policer_details_t *mp;
327
328 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400329 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar102e8a52021-06-22 08:37:26 +0000330 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_POLICER_DETAILS);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100331 mp->context = context;
332 mp->cir = htonl (config->rb.kbps.cir_kbps);
333 mp->eir = htonl (config->rb.kbps.eir_kbps);
Marek Gradzki59ed4902017-03-21 11:51:54 +0100334 mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
335 mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
Brian Russellb2aae752021-02-09 11:36:31 +0000336 mp->rate_type = (vl_api_sse2_qos_rate_type_t) config->rate_type;
337 mp->round_type = (vl_api_sse2_qos_round_type_t) config->rnd_type;
338 mp->type = (vl_api_sse2_qos_policer_type_t) config->rfc;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +0100339 mp->conform_action.type =
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000340 (vl_api_sse2_qos_action_type_t) policer->action[POLICE_CONFORM];
341 mp->conform_action.dscp = policer->mark_dscp[POLICE_CONFORM];
Jakub Grajciarcd01fb42020-03-02 13:16:53 +0100342 mp->exceed_action.type =
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000343 (vl_api_sse2_qos_action_type_t) policer->action[POLICE_EXCEED];
344 mp->exceed_action.dscp = policer->mark_dscp[POLICE_EXCEED];
Jakub Grajciarcd01fb42020-03-02 13:16:53 +0100345 mp->violate_action.type =
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000346 (vl_api_sse2_qos_action_type_t) policer->action[POLICE_VIOLATE];
347 mp->violate_action.dscp = policer->mark_dscp[POLICE_VIOLATE];
348 mp->single_rate = policer->single_rate ? 1 : 0;
349 mp->color_aware = policer->color_aware ? 1 : 0;
350 mp->scale = htonl (policer->scale);
351 mp->cir_tokens_per_period = htonl (policer->cir_tokens_per_period);
352 mp->pir_tokens_per_period = htonl (policer->pir_tokens_per_period);
353 mp->current_limit = htonl (policer->current_limit);
354 mp->current_bucket = htonl (policer->current_bucket);
355 mp->extended_limit = htonl (policer->extended_limit);
356 mp->extended_bucket = htonl (policer->extended_bucket);
357 mp->last_update_time = clib_host_to_net_u64 (policer->last_update_time);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100358
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000359 strncpy ((char *) mp->name, (char *) policer->name,
360 ARRAY_LEN (mp->name) - 1);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100361
Florin Coras6c4dae22018-01-09 06:39:23 -0800362 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100363}
364
365static void
366vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
367{
Florin Coras6c4dae22018-01-09 06:39:23 -0800368 vl_api_registration_t *reg;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100369 vnet_policer_main_t *pm = &vnet_policer_main;
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000370 uword *p, *pi;
371 u32 pool_index, policer_index;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100372 u8 *match_name = 0;
Brian Russellc5299ff2021-02-09 10:16:58 +0000373 qos_pol_cfg_params_st *config;
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000374 policer_t *policer;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100375
Florin Coras6c4dae22018-01-09 06:39:23 -0800376 reg = vl_api_client_index_to_registration (mp->client_index);
377 if (!reg)
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100378 return;
379
380 if (mp->match_name_valid)
381 {
382 match_name = format (0, "%s%c", mp->match_name, 0);
Gabriel Ganne3491d7f2017-10-19 15:10:46 +0200383 vec_terminate_c_string (match_name);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100384 }
385
386 if (mp->match_name_valid)
387 {
388 p = hash_get_mem (pm->policer_config_by_name, match_name);
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000389 pi = hash_get_mem (pm->policer_index_by_name, match_name);
390 if (0 == p || 0 == pi)
391 return;
392
393 pool_index = p[0];
394 policer_index = pi[0];
395 config = pool_elt_at_index (pm->configs, pool_index);
396 policer = pool_elt_at_index (pm->policers, policer_index);
397 send_policer_details (config, policer, reg, mp->context);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100398 }
399 else
400 {
Maxime Peim2d1a62b2023-01-06 11:57:38 +0000401 pool_foreach (policer, pm->policers)
402 {
403 p = hash_get_mem (pm->policer_config_by_name, policer->name);
404 if (0 == p)
405 continue;
406
407 pool_index = p[0];
408 config = pool_elt_at_index (pm->configs, pool_index);
409 send_policer_details (config, policer, reg, mp->context);
410 };
411 }
412}
413
414static void
415vl_api_policer_dump_v2_t_handler (vl_api_policer_dump_v2_t *mp)
416{
417 vl_api_registration_t *reg;
418 vnet_policer_main_t *pm = &vnet_policer_main;
419 qos_pol_cfg_params_st *config;
420 u32 policer_index, pool_index;
421 policer_t *policer;
422 uword *p;
423
424 reg = vl_api_client_index_to_registration (mp->client_index);
425 if (!reg)
426 return;
427
428 policer_index = ntohl (mp->policer_index);
429
430 if (~0 == policer_index)
431 {
432 pool_foreach (policer, pm->policers)
433 {
434 p = hash_get_mem (pm->policer_config_by_name, policer->name);
435 pool_index = p[0];
436 config = pool_elt_at_index (pm->configs, pool_index);
437 send_policer_details (config, policer, reg, mp->context);
438 };
439 }
440 else
441 {
442 if (pool_is_free_index (pm->policers, policer_index))
443 return;
444
445 policer = &pm->policers[policer_index];
446 p = hash_get_mem (pm->policer_config_by_name, policer->name);
447 pool_index = p[0];
448 config = pool_elt_at_index (pm->configs, pool_index);
449 send_policer_details (config, policer, reg, mp->context);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100450 }
451}
452
Filip Tehlar102e8a52021-06-22 08:37:26 +0000453#include <vnet/policer/policer.api.c>
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100454static clib_error_t *
455policer_api_hookup (vlib_main_t * vm)
456{
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100457 /*
458 * Set up the (msg_name, crc, message-id) table
459 */
Filip Tehlar102e8a52021-06-22 08:37:26 +0000460 REPLY_MSG_ID_BASE = setup_message_id_table ();
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100461
462 return 0;
463}
464
465VLIB_API_INIT_FUNCTION (policer_api_hookup);
466
467/*
468 * fd.io coding-style-patch-verification: ON
469 *
470 * Local Variables:
471 * eval: (c-set-style "gnu")
472 * End:
473 */