2 *------------------------------------------------------------------
3 * policer_api.c - policer api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/policer/policer.h>
27 #include <vnet/format_fns.h>
28 #include <vnet/policer/policer.api_enum.h>
29 #include <vnet/policer/policer.api_types.h>
31 #define REPLY_MSG_ID_BASE vnet_policer_main.msg_id_base
32 #include <vlibapi/api_helper_macros.h>
35 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
37 vlib_main_t *vm = vlib_get_main ();
38 vnet_policer_main_t *pm = &vnet_policer_main;
39 vl_api_policer_add_del_reply_t *rmp;
42 char name[sizeof (mp->name) + 1];
43 qos_pol_cfg_params_st cfg;
46 snprintf (name, sizeof (name), "%s", mp->name);
50 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;
69 rv = policer_add (vm, (u8 *) name, &cfg, &policer_index);
73 p = hash_get_mem (pm->policer_index_by_name, name);
75 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
77 rv = policer_del (vm, p[0]);
80 REPLY_MACRO2 (VL_API_POLICER_ADD_DEL_REPLY, ({
81 if (rv == 0 && mp->is_add)
82 rmp->policer_index = htonl (policer_index);
84 rmp->policer_index = ~0;
88 static_always_inline void
89 policer_set_configuration (qos_pol_cfg_params_st *cfg,
90 vl_api_policer_config_t *infos)
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;
113 vl_api_policer_add_t_handler (vl_api_policer_add_t *mp)
115 vlib_main_t *vm = vlib_get_main ();
116 vl_api_policer_add_reply_t *rmp;
118 char name[sizeof (mp->name) + 1];
119 qos_pol_cfg_params_st cfg;
122 snprintf (name, sizeof (name), "%s", mp->name);
124 policer_set_configuration (&cfg, &mp->infos);
126 rv = policer_add (vm, (u8 *) name, &cfg, &policer_index);
128 REPLY_MACRO2 (VL_API_POLICER_ADD_REPLY, ({
130 rmp->policer_index = htonl (policer_index);
132 rmp->policer_index = ~0;
137 vl_api_policer_del_t_handler (vl_api_policer_del_t *mp)
139 vlib_main_t *vm = vlib_get_main ();
140 vl_api_policer_del_reply_t *rmp;
144 policer_index = ntohl (mp->policer_index);
145 rv = policer_del (vm, policer_index);
147 REPLY_MACRO (VL_API_POLICER_DEL_REPLY);
151 vl_api_policer_update_t_handler (vl_api_policer_update_t *mp)
153 vlib_main_t *vm = vlib_get_main ();
154 vl_api_policer_update_reply_t *rmp;
156 qos_pol_cfg_params_st cfg;
159 policer_set_configuration (&cfg, &mp->infos);
161 policer_index = ntohl (mp->policer_index);
162 rv = policer_update (vm, policer_index, &cfg);
164 REPLY_MACRO (VL_API_POLICER_UPDATE_REPLY);
168 vl_api_policer_reset_t_handler (vl_api_policer_reset_t *mp)
170 vlib_main_t *vm = vlib_get_main ();
171 vl_api_policer_reset_reply_t *rmp;
175 policer_index = ntohl (mp->policer_index);
176 rv = policer_reset (vm, policer_index);
178 REPLY_MACRO (VL_API_POLICER_RESET_REPLY);
182 vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp)
184 vl_api_policer_bind_reply_t *rmp;
185 vnet_policer_main_t *pm = &vnet_policer_main;
186 char name[sizeof (mp->name) + 1];
192 snprintf (name, sizeof (name), "%s", mp->name);
194 worker_index = ntohl (mp->worker_index);
195 bind_enable = mp->bind_enable;
197 p = hash_get_mem (pm->policer_index_by_name, name);
199 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
201 rv = policer_bind_worker (p[0], worker_index, bind_enable);
203 REPLY_MACRO (VL_API_POLICER_BIND_REPLY);
207 vl_api_policer_bind_v2_t_handler (vl_api_policer_bind_v2_t *mp)
209 vl_api_policer_bind_v2_reply_t *rmp;
215 policer_index = ntohl (mp->policer_index);
216 worker_index = ntohl (mp->worker_index);
217 bind_enable = mp->bind_enable;
219 rv = policer_bind_worker (policer_index, worker_index, bind_enable);
221 REPLY_MACRO (VL_API_POLICER_BIND_V2_REPLY);
225 vl_api_policer_input_t_handler (vl_api_policer_input_t *mp)
227 vl_api_policer_input_reply_t *rmp;
228 vnet_policer_main_t *pm = &vnet_policer_main;
229 char name[sizeof (mp->name) + 1];
235 VALIDATE_SW_IF_INDEX (mp);
237 snprintf (name, sizeof (name), "%s", mp->name);
239 sw_if_index = ntohl (mp->sw_if_index);
242 p = hash_get_mem (pm->policer_index_by_name, name);
244 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
246 rv = policer_input (p[0], sw_if_index, VLIB_RX, apply);
248 BAD_SW_IF_INDEX_LABEL;
249 REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
253 vl_api_policer_input_v2_t_handler (vl_api_policer_input_v2_t *mp)
255 vl_api_policer_input_v2_reply_t *rmp;
261 VALIDATE_SW_IF_INDEX (mp);
263 policer_index = ntohl (mp->policer_index);
264 sw_if_index = ntohl (mp->sw_if_index);
267 rv = policer_input (policer_index, sw_if_index, VLIB_RX, apply);
269 BAD_SW_IF_INDEX_LABEL;
270 REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
274 vl_api_policer_output_t_handler (vl_api_policer_output_t *mp)
276 vl_api_policer_output_reply_t *rmp;
277 vnet_policer_main_t *pm = &vnet_policer_main;
278 char name[sizeof (mp->name) + 1];
284 VALIDATE_SW_IF_INDEX (mp);
286 snprintf (name, sizeof (name), "%s", mp->name);
288 sw_if_index = ntohl (mp->sw_if_index);
291 p = hash_get_mem (pm->policer_index_by_name, name);
293 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
295 rv = policer_input (p[0], sw_if_index, VLIB_TX, apply);
297 BAD_SW_IF_INDEX_LABEL;
298 REPLY_MACRO (VL_API_POLICER_OUTPUT_REPLY);
302 vl_api_policer_output_v2_t_handler (vl_api_policer_output_v2_t *mp)
304 vl_api_policer_output_reply_t *rmp;
310 VALIDATE_SW_IF_INDEX (mp);
312 policer_index = ntohl (mp->policer_index);
313 sw_if_index = ntohl (mp->sw_if_index);
316 rv = policer_input (policer_index, sw_if_index, VLIB_TX, apply);
318 BAD_SW_IF_INDEX_LABEL;
319 REPLY_MACRO (VL_API_POLICER_OUTPUT_REPLY);
323 send_policer_details (qos_pol_cfg_params_st *config, policer_t *policer,
324 vl_api_registration_t *reg, u32 context)
326 vl_api_policer_details_t *mp;
328 mp = vl_msg_api_alloc (sizeof (*mp));
329 clib_memset (mp, 0, sizeof (*mp));
330 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_POLICER_DETAILS);
331 mp->context = context;
332 mp->cir = htonl (config->rb.kbps.cir_kbps);
333 mp->eir = htonl (config->rb.kbps.eir_kbps);
334 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);
336 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;
339 mp->conform_action.type =
340 (vl_api_sse2_qos_action_type_t) policer->action[POLICE_CONFORM];
341 mp->conform_action.dscp = policer->mark_dscp[POLICE_CONFORM];
342 mp->exceed_action.type =
343 (vl_api_sse2_qos_action_type_t) policer->action[POLICE_EXCEED];
344 mp->exceed_action.dscp = policer->mark_dscp[POLICE_EXCEED];
345 mp->violate_action.type =
346 (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);
359 strncpy ((char *) mp->name, (char *) policer->name,
360 ARRAY_LEN (mp->name) - 1);
362 vl_api_send_msg (reg, (u8 *) mp);
366 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
368 vl_api_registration_t *reg;
369 vnet_policer_main_t *pm = &vnet_policer_main;
371 u32 pool_index, policer_index;
373 qos_pol_cfg_params_st *config;
376 reg = vl_api_client_index_to_registration (mp->client_index);
380 if (mp->match_name_valid)
382 match_name = format (0, "%s%c", mp->match_name, 0);
383 vec_terminate_c_string (match_name);
386 if (mp->match_name_valid)
388 p = hash_get_mem (pm->policer_config_by_name, match_name);
389 pi = hash_get_mem (pm->policer_index_by_name, match_name);
390 if (0 == p || 0 == pi)
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);
401 pool_foreach (policer, pm->policers)
403 p = hash_get_mem (pm->policer_config_by_name, policer->name);
408 config = pool_elt_at_index (pm->configs, pool_index);
409 send_policer_details (config, policer, reg, mp->context);
415 vl_api_policer_dump_v2_t_handler (vl_api_policer_dump_v2_t *mp)
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;
424 reg = vl_api_client_index_to_registration (mp->client_index);
428 policer_index = ntohl (mp->policer_index);
430 if (~0 == policer_index)
432 pool_foreach (policer, pm->policers)
434 p = hash_get_mem (pm->policer_config_by_name, policer->name);
436 config = pool_elt_at_index (pm->configs, pool_index);
437 send_policer_details (config, policer, reg, mp->context);
442 if (pool_is_free_index (pm->policers, policer_index))
445 policer = &pm->policers[policer_index];
446 p = hash_get_mem (pm->policer_config_by_name, policer->name);
448 config = pool_elt_at_index (pm->configs, pool_index);
449 send_policer_details (config, policer, reg, mp->context);
453 #include <vnet/policer/policer.api.c>
454 static clib_error_t *
455 policer_api_hookup (vlib_main_t * vm)
458 * Set up the (msg_name, crc, message-id) table
460 REPLY_MSG_ID_BASE = setup_message_id_table ();
465 VLIB_API_INIT_FUNCTION (policer_api_hookup);
468 * fd.io coding-style-patch-verification: ON
471 * eval: (c-set-style "gnu")