blob: 1382d17e2de75156fd59eff428450bf05b631335 [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 ();
38 vl_api_policer_add_del_reply_t *rmp;
39 int rv = 0;
40 u8 *name = NULL;
Brian Russellc5299ff2021-02-09 10:16:58 +000041 qos_pol_cfg_params_st cfg;
Pavel Kotucekd9aad292017-01-25 08:45:38 +010042 clib_error_t *error;
43 u32 policer_index;
44
45 name = format (0, "%s", mp->name);
Gabriel Ganne3491d7f2017-10-19 15:10:46 +020046 vec_terminate_c_string (name);
Pavel Kotucekd9aad292017-01-25 08:45:38 +010047
Dave Barachb7b92992018-10-17 10:38:51 -040048 clib_memset (&cfg, 0, sizeof (cfg));
Brian Russellb2aae752021-02-09 11:36:31 +000049 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;
Neale Rannsd91c1db2017-07-31 02:30:50 -070052 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);
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010056 cfg.conform_action.action_type =
Brian Russellc5299ff2021-02-09 10:16:58 +000057 (qos_action_type_en) mp->conform_action.type;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010058 cfg.conform_action.dscp = mp->conform_action.dscp;
Brian Russellc5299ff2021-02-09 10:16:58 +000059 cfg.exceed_action.action_type = (qos_action_type_en) mp->exceed_action.type;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010060 cfg.exceed_action.dscp = mp->exceed_action.dscp;
61 cfg.violate_action.action_type =
Brian Russellc5299ff2021-02-09 10:16:58 +000062 (qos_action_type_en) mp->violate_action.type;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010063 cfg.violate_action.dscp = mp->violate_action.dscp;
64
Pavel Kotucekd9aad292017-01-25 08:45:38 +010065 cfg.color_aware = mp->color_aware;
66
67 error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
68
69 if (error)
70 rv = VNET_API_ERROR_UNSPECIFIED;
71
72 /* *INDENT-OFF* */
73 REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
74 ({
75 if (rv == 0 && mp->is_add)
76 rmp->policer_index = ntohl(policer_index);
77 else
78 rmp->policer_index = ~0;
79 }));
80 /* *INDENT-ON* */
81}
82
83static void
Brian Russell48e26362021-02-10 13:53:42 +000084vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp)
85{
86 vl_api_policer_bind_reply_t *rmp;
87 u8 *name;
88 u32 worker_index;
89 u8 bind_enable;
90 int rv;
91
92 name = format (0, "%s", mp->name);
93 vec_terminate_c_string (name);
94
95 worker_index = ntohl (mp->worker_index);
96 bind_enable = mp->bind_enable;
97
98 rv = policer_bind_worker (name, worker_index, bind_enable);
99 vec_free (name);
100 REPLY_MACRO (VL_API_POLICER_BIND_REPLY);
101}
102
103static void
Brian Russellb0468302021-02-17 15:51:45 +0000104vl_api_policer_input_t_handler (vl_api_policer_input_t *mp)
105{
106 vl_api_policer_bind_reply_t *rmp;
107 u8 *name;
108 u32 sw_if_index;
109 u8 apply;
110 int rv;
111
112 VALIDATE_SW_IF_INDEX (mp);
113
114 name = format (0, "%s", mp->name);
115 vec_terminate_c_string (name);
116
117 sw_if_index = ntohl (mp->sw_if_index);
118 apply = mp->apply;
119
120 rv = policer_input (name, sw_if_index, apply);
121 vec_free (name);
122
123 BAD_SW_IF_INDEX_LABEL;
124 REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
125}
126
127static void
Brian Russellc5299ff2021-02-09 10:16:58 +0000128send_policer_details (u8 *name, qos_pol_cfg_params_st *config,
Brian Russell54be0cc2021-02-15 11:49:42 +0000129 policer_t *templ, vl_api_registration_t *reg,
130 u32 context)
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100131{
132 vl_api_policer_details_t *mp;
133
134 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400135 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar102e8a52021-06-22 08:37:26 +0000136 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_POLICER_DETAILS);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100137 mp->context = context;
138 mp->cir = htonl (config->rb.kbps.cir_kbps);
139 mp->eir = htonl (config->rb.kbps.eir_kbps);
Marek Gradzki59ed4902017-03-21 11:51:54 +0100140 mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
141 mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
Brian Russellb2aae752021-02-09 11:36:31 +0000142 mp->rate_type = (vl_api_sse2_qos_rate_type_t) config->rate_type;
143 mp->round_type = (vl_api_sse2_qos_round_type_t) config->rnd_type;
144 mp->type = (vl_api_sse2_qos_policer_type_t) config->rfc;
Jakub Grajciarcd01fb42020-03-02 13:16:53 +0100145 mp->conform_action.type =
146 (vl_api_sse2_qos_action_type_t) config->conform_action.action_type;
147 mp->conform_action.dscp = config->conform_action.dscp;
148 mp->exceed_action.type =
149 (vl_api_sse2_qos_action_type_t) config->exceed_action.action_type;
150 mp->exceed_action.dscp = config->exceed_action.dscp;
151 mp->violate_action.type =
152 (vl_api_sse2_qos_action_type_t) config->violate_action.action_type;
153 mp->violate_action.dscp = config->violate_action.dscp;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100154 mp->single_rate = templ->single_rate ? 1 : 0;
155 mp->color_aware = templ->color_aware ? 1 : 0;
156 mp->scale = htonl (templ->scale);
157 mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
158 mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
159 mp->current_limit = htonl (templ->current_limit);
160 mp->current_bucket = htonl (templ->current_bucket);
161 mp->extended_limit = htonl (templ->extended_limit);
162 mp->extended_bucket = htonl (templ->extended_bucket);
163 mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
164
165 strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
166
Florin Coras6c4dae22018-01-09 06:39:23 -0800167 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100168}
169
170static void
171vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
172{
Florin Coras6c4dae22018-01-09 06:39:23 -0800173 vl_api_registration_t *reg;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100174 vnet_policer_main_t *pm = &vnet_policer_main;
175 hash_pair_t *hp;
176 uword *p;
177 u32 pool_index;
178 u8 *match_name = 0;
179 u8 *name;
Brian Russellc5299ff2021-02-09 10:16:58 +0000180 qos_pol_cfg_params_st *config;
Brian Russell54be0cc2021-02-15 11:49:42 +0000181 policer_t *templ;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100182
Florin Coras6c4dae22018-01-09 06:39:23 -0800183 reg = vl_api_client_index_to_registration (mp->client_index);
184 if (!reg)
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100185 return;
186
187 if (mp->match_name_valid)
188 {
189 match_name = format (0, "%s%c", mp->match_name, 0);
Gabriel Ganne3491d7f2017-10-19 15:10:46 +0200190 vec_terminate_c_string (match_name);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100191 }
192
193 if (mp->match_name_valid)
194 {
195 p = hash_get_mem (pm->policer_config_by_name, match_name);
196 if (p)
197 {
198 pool_index = p[0];
199 config = pool_elt_at_index (pm->configs, pool_index);
200 templ = pool_elt_at_index (pm->policer_templates, pool_index);
Florin Coras6c4dae22018-01-09 06:39:23 -0800201 send_policer_details (match_name, config, templ, reg, mp->context);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100202 }
203 }
204 else
205 {
206 /* *INDENT-OFF* */
207 hash_foreach_pair (hp, pm->policer_config_by_name,
208 ({
209 name = (u8 *) hp->key;
210 pool_index = hp->value[0];
211 config = pool_elt_at_index (pm->configs, pool_index);
212 templ = pool_elt_at_index (pm->policer_templates, pool_index);
Florin Coras6c4dae22018-01-09 06:39:23 -0800213 send_policer_details(name, config, templ, reg, mp->context);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100214 }));
215 /* *INDENT-ON* */
216 }
217}
218
Filip Tehlar102e8a52021-06-22 08:37:26 +0000219#include <vnet/policer/policer.api.c>
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100220static clib_error_t *
221policer_api_hookup (vlib_main_t * vm)
222{
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100223 /*
224 * Set up the (msg_name, crc, message-id) table
225 */
Filip Tehlar102e8a52021-06-22 08:37:26 +0000226 REPLY_MSG_ID_BASE = setup_message_id_table ();
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100227
228 return 0;
229}
230
231VLIB_API_INIT_FUNCTION (policer_api_hookup);
232
233/*
234 * fd.io coding-style-patch-verification: ON
235 *
236 * Local Variables:
237 * eval: (c-set-style "gnu")
238 * End:
239 */