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 vl_api_policer_add_del_reply_t *rmp;
41 qos_pol_cfg_params_st cfg;
45 name = format (0, "%s", mp->name);
46 vec_terminate_c_string (name);
48 clib_memset (&cfg, 0, sizeof (cfg));
49 cfg.rfc = (qos_policer_type_en) mp->type;
50 cfg.rnd_type = (qos_round_type_en) mp->round_type;
51 cfg.rate_type = (qos_rate_type_en) mp->rate_type;
52 cfg.rb.kbps.cir_kbps = ntohl (mp->cir);
53 cfg.rb.kbps.eir_kbps = ntohl (mp->eir);
54 cfg.rb.kbps.cb_bytes = clib_net_to_host_u64 (mp->cb);
55 cfg.rb.kbps.eb_bytes = clib_net_to_host_u64 (mp->eb);
56 cfg.conform_action.action_type =
57 (qos_action_type_en) mp->conform_action.type;
58 cfg.conform_action.dscp = mp->conform_action.dscp;
59 cfg.exceed_action.action_type = (qos_action_type_en) mp->exceed_action.type;
60 cfg.exceed_action.dscp = mp->exceed_action.dscp;
61 cfg.violate_action.action_type =
62 (qos_action_type_en) mp->violate_action.type;
63 cfg.violate_action.dscp = mp->violate_action.dscp;
65 cfg.color_aware = mp->color_aware;
67 error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
71 rv = VNET_API_ERROR_UNSPECIFIED;
72 clib_error_free (error);
76 REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
78 if (rv == 0 && mp->is_add)
79 rmp->policer_index = ntohl(policer_index);
81 rmp->policer_index = ~0;
87 vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp)
89 vl_api_policer_bind_reply_t *rmp;
95 name = format (0, "%s", mp->name);
96 vec_terminate_c_string (name);
98 worker_index = ntohl (mp->worker_index);
99 bind_enable = mp->bind_enable;
101 rv = policer_bind_worker (name, worker_index, bind_enable);
103 REPLY_MACRO (VL_API_POLICER_BIND_REPLY);
107 vl_api_policer_input_t_handler (vl_api_policer_input_t *mp)
109 vl_api_policer_bind_reply_t *rmp;
115 VALIDATE_SW_IF_INDEX (mp);
117 name = format (0, "%s", mp->name);
118 vec_terminate_c_string (name);
120 sw_if_index = ntohl (mp->sw_if_index);
123 rv = policer_input (name, sw_if_index, apply);
126 BAD_SW_IF_INDEX_LABEL;
127 REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
131 send_policer_details (u8 *name, qos_pol_cfg_params_st *config,
132 policer_t *templ, vl_api_registration_t *reg,
135 vl_api_policer_details_t *mp;
137 mp = vl_msg_api_alloc (sizeof (*mp));
138 clib_memset (mp, 0, sizeof (*mp));
139 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_POLICER_DETAILS);
140 mp->context = context;
141 mp->cir = htonl (config->rb.kbps.cir_kbps);
142 mp->eir = htonl (config->rb.kbps.eir_kbps);
143 mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
144 mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
145 mp->rate_type = (vl_api_sse2_qos_rate_type_t) config->rate_type;
146 mp->round_type = (vl_api_sse2_qos_round_type_t) config->rnd_type;
147 mp->type = (vl_api_sse2_qos_policer_type_t) config->rfc;
148 mp->conform_action.type =
149 (vl_api_sse2_qos_action_type_t) config->conform_action.action_type;
150 mp->conform_action.dscp = config->conform_action.dscp;
151 mp->exceed_action.type =
152 (vl_api_sse2_qos_action_type_t) config->exceed_action.action_type;
153 mp->exceed_action.dscp = config->exceed_action.dscp;
154 mp->violate_action.type =
155 (vl_api_sse2_qos_action_type_t) config->violate_action.action_type;
156 mp->violate_action.dscp = config->violate_action.dscp;
157 mp->single_rate = templ->single_rate ? 1 : 0;
158 mp->color_aware = templ->color_aware ? 1 : 0;
159 mp->scale = htonl (templ->scale);
160 mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
161 mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
162 mp->current_limit = htonl (templ->current_limit);
163 mp->current_bucket = htonl (templ->current_bucket);
164 mp->extended_limit = htonl (templ->extended_limit);
165 mp->extended_bucket = htonl (templ->extended_bucket);
166 mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
168 strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
170 vl_api_send_msg (reg, (u8 *) mp);
174 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
176 vl_api_registration_t *reg;
177 vnet_policer_main_t *pm = &vnet_policer_main;
183 qos_pol_cfg_params_st *config;
186 reg = vl_api_client_index_to_registration (mp->client_index);
190 if (mp->match_name_valid)
192 match_name = format (0, "%s%c", mp->match_name, 0);
193 vec_terminate_c_string (match_name);
196 if (mp->match_name_valid)
198 p = hash_get_mem (pm->policer_config_by_name, match_name);
202 config = pool_elt_at_index (pm->configs, pool_index);
203 templ = pool_elt_at_index (pm->policer_templates, pool_index);
204 send_policer_details (match_name, config, templ, reg, mp->context);
210 hash_foreach_pair (hp, pm->policer_config_by_name,
212 name = (u8 *) hp->key;
213 pool_index = hp->value[0];
214 config = pool_elt_at_index (pm->configs, pool_index);
215 templ = pool_elt_at_index (pm->policer_templates, pool_index);
216 send_policer_details(name, config, templ, reg, mp->context);
222 #include <vnet/policer/policer.api.c>
223 static clib_error_t *
224 policer_api_hookup (vlib_main_t * vm)
227 * Set up the (msg_name, crc, message-id) table
229 REPLY_MSG_ID_BASE = setup_message_id_table ();
234 VLIB_API_INIT_FUNCTION (policer_api_hookup);
237 * fd.io coding-style-patch-verification: ON
240 * eval: (c-set-style "gnu")