blob: c3a9800e20199e1c8a7879a673f455e9129b7d29 [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
27#include <vnet/vnet_msg_enum.h>
28
29#define vl_typedefs /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_typedefs
32
33#define vl_endianfun /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39#define vl_printfun
40#include <vnet/vnet_all_api_h.h>
41#undef vl_printfun
42
43#include <vlibapi/api_helper_macros.h>
44
Brian Russell48e26362021-02-10 13:53:42 +000045#define foreach_vpe_api_msg \
46 _ (POLICER_ADD_DEL, policer_add_del) \
47 _ (POLICER_BIND, policer_bind) \
Brian Russellb0468302021-02-17 15:51:45 +000048 _ (POLICER_INPUT, policer_input) \
Brian Russell48e26362021-02-10 13:53:42 +000049 _ (POLICER_DUMP, policer_dump)
Pavel Kotucekd9aad292017-01-25 08:45:38 +010050
51static void
52vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
53{
54 vlib_main_t *vm = vlib_get_main ();
55 vl_api_policer_add_del_reply_t *rmp;
56 int rv = 0;
57 u8 *name = NULL;
Brian Russellc5299ff2021-02-09 10:16:58 +000058 qos_pol_cfg_params_st cfg;
Pavel Kotucekd9aad292017-01-25 08:45:38 +010059 clib_error_t *error;
60 u32 policer_index;
61
62 name = format (0, "%s", mp->name);
Gabriel Ganne3491d7f2017-10-19 15:10:46 +020063 vec_terminate_c_string (name);
Pavel Kotucekd9aad292017-01-25 08:45:38 +010064
Dave Barachb7b92992018-10-17 10:38:51 -040065 clib_memset (&cfg, 0, sizeof (cfg));
Brian Russellb2aae752021-02-09 11:36:31 +000066 cfg.rfc = (qos_policer_type_en) mp->type;
67 cfg.rnd_type = (qos_round_type_en) mp->round_type;
68 cfg.rate_type = (qos_rate_type_en) mp->rate_type;
Neale Rannsd91c1db2017-07-31 02:30:50 -070069 cfg.rb.kbps.cir_kbps = ntohl (mp->cir);
70 cfg.rb.kbps.eir_kbps = ntohl (mp->eir);
71 cfg.rb.kbps.cb_bytes = clib_net_to_host_u64 (mp->cb);
72 cfg.rb.kbps.eb_bytes = clib_net_to_host_u64 (mp->eb);
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010073 cfg.conform_action.action_type =
Brian Russellc5299ff2021-02-09 10:16:58 +000074 (qos_action_type_en) mp->conform_action.type;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010075 cfg.conform_action.dscp = mp->conform_action.dscp;
Brian Russellc5299ff2021-02-09 10:16:58 +000076 cfg.exceed_action.action_type = (qos_action_type_en) mp->exceed_action.type;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010077 cfg.exceed_action.dscp = mp->exceed_action.dscp;
78 cfg.violate_action.action_type =
Brian Russellc5299ff2021-02-09 10:16:58 +000079 (qos_action_type_en) mp->violate_action.type;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010080 cfg.violate_action.dscp = mp->violate_action.dscp;
81
Pavel Kotucekd9aad292017-01-25 08:45:38 +010082 cfg.color_aware = mp->color_aware;
83
84 error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
85
86 if (error)
87 rv = VNET_API_ERROR_UNSPECIFIED;
88
89 /* *INDENT-OFF* */
90 REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
91 ({
92 if (rv == 0 && mp->is_add)
93 rmp->policer_index = ntohl(policer_index);
94 else
95 rmp->policer_index = ~0;
96 }));
97 /* *INDENT-ON* */
98}
99
100static void
Brian Russell48e26362021-02-10 13:53:42 +0000101vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp)
102{
103 vl_api_policer_bind_reply_t *rmp;
104 u8 *name;
105 u32 worker_index;
106 u8 bind_enable;
107 int rv;
108
109 name = format (0, "%s", mp->name);
110 vec_terminate_c_string (name);
111
112 worker_index = ntohl (mp->worker_index);
113 bind_enable = mp->bind_enable;
114
115 rv = policer_bind_worker (name, worker_index, bind_enable);
116 vec_free (name);
117 REPLY_MACRO (VL_API_POLICER_BIND_REPLY);
118}
119
120static void
Brian Russellb0468302021-02-17 15:51:45 +0000121vl_api_policer_input_t_handler (vl_api_policer_input_t *mp)
122{
123 vl_api_policer_bind_reply_t *rmp;
124 u8 *name;
125 u32 sw_if_index;
126 u8 apply;
127 int rv;
128
129 VALIDATE_SW_IF_INDEX (mp);
130
131 name = format (0, "%s", mp->name);
132 vec_terminate_c_string (name);
133
134 sw_if_index = ntohl (mp->sw_if_index);
135 apply = mp->apply;
136
137 rv = policer_input (name, sw_if_index, apply);
138 vec_free (name);
139
140 BAD_SW_IF_INDEX_LABEL;
141 REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
142}
143
144static void
Brian Russellc5299ff2021-02-09 10:16:58 +0000145send_policer_details (u8 *name, qos_pol_cfg_params_st *config,
Brian Russell54be0cc2021-02-15 11:49:42 +0000146 policer_t *templ, vl_api_registration_t *reg,
147 u32 context)
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100148{
149 vl_api_policer_details_t *mp;
150
151 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400152 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100153 mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
154 mp->context = context;
155 mp->cir = htonl (config->rb.kbps.cir_kbps);
156 mp->eir = htonl (config->rb.kbps.eir_kbps);
Marek Gradzki59ed4902017-03-21 11:51:54 +0100157 mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
158 mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
Brian Russellb2aae752021-02-09 11:36:31 +0000159 mp->rate_type = (vl_api_sse2_qos_rate_type_t) config->rate_type;
160 mp->round_type = (vl_api_sse2_qos_round_type_t) config->rnd_type;
161 mp->type = (vl_api_sse2_qos_policer_type_t) config->rfc;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +0100162 mp->conform_action.type =
163 (vl_api_sse2_qos_action_type_t) config->conform_action.action_type;
164 mp->conform_action.dscp = config->conform_action.dscp;
165 mp->exceed_action.type =
166 (vl_api_sse2_qos_action_type_t) config->exceed_action.action_type;
167 mp->exceed_action.dscp = config->exceed_action.dscp;
168 mp->violate_action.type =
169 (vl_api_sse2_qos_action_type_t) config->violate_action.action_type;
170 mp->violate_action.dscp = config->violate_action.dscp;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100171 mp->single_rate = templ->single_rate ? 1 : 0;
172 mp->color_aware = templ->color_aware ? 1 : 0;
173 mp->scale = htonl (templ->scale);
174 mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
175 mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
176 mp->current_limit = htonl (templ->current_limit);
177 mp->current_bucket = htonl (templ->current_bucket);
178 mp->extended_limit = htonl (templ->extended_limit);
179 mp->extended_bucket = htonl (templ->extended_bucket);
180 mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
181
182 strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
183
Florin Coras6c4dae22018-01-09 06:39:23 -0800184 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100185}
186
187static void
188vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
189{
Florin Coras6c4dae22018-01-09 06:39:23 -0800190 vl_api_registration_t *reg;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100191 vnet_policer_main_t *pm = &vnet_policer_main;
192 hash_pair_t *hp;
193 uword *p;
194 u32 pool_index;
195 u8 *match_name = 0;
196 u8 *name;
Brian Russellc5299ff2021-02-09 10:16:58 +0000197 qos_pol_cfg_params_st *config;
Brian Russell54be0cc2021-02-15 11:49:42 +0000198 policer_t *templ;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100199
Florin Coras6c4dae22018-01-09 06:39:23 -0800200 reg = vl_api_client_index_to_registration (mp->client_index);
201 if (!reg)
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100202 return;
203
204 if (mp->match_name_valid)
205 {
206 match_name = format (0, "%s%c", mp->match_name, 0);
Gabriel Ganne3491d7f2017-10-19 15:10:46 +0200207 vec_terminate_c_string (match_name);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100208 }
209
210 if (mp->match_name_valid)
211 {
212 p = hash_get_mem (pm->policer_config_by_name, match_name);
213 if (p)
214 {
215 pool_index = p[0];
216 config = pool_elt_at_index (pm->configs, pool_index);
217 templ = pool_elt_at_index (pm->policer_templates, pool_index);
Florin Coras6c4dae22018-01-09 06:39:23 -0800218 send_policer_details (match_name, config, templ, reg, mp->context);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100219 }
220 }
221 else
222 {
223 /* *INDENT-OFF* */
224 hash_foreach_pair (hp, pm->policer_config_by_name,
225 ({
226 name = (u8 *) hp->key;
227 pool_index = hp->value[0];
228 config = pool_elt_at_index (pm->configs, pool_index);
229 templ = pool_elt_at_index (pm->policer_templates, pool_index);
Florin Coras6c4dae22018-01-09 06:39:23 -0800230 send_policer_details(name, config, templ, reg, mp->context);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100231 }));
232 /* *INDENT-ON* */
233 }
234}
235
236/*
237 * policer_api_hookup
238 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700239 * vlib has already mapped shared memory and
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100240 * added the client registration handlers.
241 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
242 */
243#define vl_msg_name_crc_list
244#include <vnet/vnet_all_api_h.h>
245#undef vl_msg_name_crc_list
246
247static void
248setup_message_id_table (api_main_t * am)
249{
250#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
251 foreach_vl_msg_name_crc_policer;
252#undef _
253}
254
255static clib_error_t *
256policer_api_hookup (vlib_main_t * vm)
257{
Dave Barach39d69112019-11-27 11:42:13 -0500258 api_main_t *am = vlibapi_get_main ();
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100259
260#define _(N,n) \
261 vl_msg_api_set_handlers(VL_API_##N, #n, \
262 vl_api_##n##_t_handler, \
263 vl_noop_handler, \
264 vl_api_##n##_t_endian, \
265 vl_api_##n##_t_print, \
266 sizeof(vl_api_##n##_t), 1);
267 foreach_vpe_api_msg;
268#undef _
269 /*
270 * Set up the (msg_name, crc, message-id) table
271 */
272 setup_message_id_table (am);
273
274 return 0;
275}
276
277VLIB_API_INIT_FUNCTION (policer_api_hookup);
278
279/*
280 * fd.io coding-style-patch-verification: ON
281 *
282 * Local Variables:
283 * eval: (c-set-style "gnu")
284 * End:
285 */