blob: d6d6b07ac34d7e147ef4b3cc9df93f4887ea7bbc [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
45#define foreach_vpe_api_msg \
46_(POLICER_ADD_DEL, policer_add_del) \
47_(POLICER_DUMP, policer_dump)
48
49static void
50vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
51{
52 vlib_main_t *vm = vlib_get_main ();
53 vl_api_policer_add_del_reply_t *rmp;
54 int rv = 0;
55 u8 *name = NULL;
56 sse2_qos_pol_cfg_params_st cfg;
57 clib_error_t *error;
58 u32 policer_index;
59
60 name = format (0, "%s", mp->name);
Gabriel Ganne3491d7f2017-10-19 15:10:46 +020061 vec_terminate_c_string (name);
Pavel Kotucekd9aad292017-01-25 08:45:38 +010062
63 memset (&cfg, 0, sizeof (cfg));
64 cfg.rfc = mp->type;
65 cfg.rnd_type = mp->round_type;
66 cfg.rate_type = mp->rate_type;
Neale Rannsd91c1db2017-07-31 02:30:50 -070067 cfg.rb.kbps.cir_kbps = ntohl (mp->cir);
68 cfg.rb.kbps.eir_kbps = ntohl (mp->eir);
69 cfg.rb.kbps.cb_bytes = clib_net_to_host_u64 (mp->cb);
70 cfg.rb.kbps.eb_bytes = clib_net_to_host_u64 (mp->eb);
Pavel Kotucekd9aad292017-01-25 08:45:38 +010071 cfg.conform_action.action_type = mp->conform_action_type;
72 cfg.conform_action.dscp = mp->conform_dscp;
73 cfg.exceed_action.action_type = mp->exceed_action_type;
74 cfg.exceed_action.dscp = mp->exceed_dscp;
75 cfg.violate_action.action_type = mp->violate_action_type;
76 cfg.violate_action.dscp = mp->violate_dscp;
77 cfg.color_aware = mp->color_aware;
78
79 error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
80
81 if (error)
82 rv = VNET_API_ERROR_UNSPECIFIED;
83
84 /* *INDENT-OFF* */
85 REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
86 ({
87 if (rv == 0 && mp->is_add)
88 rmp->policer_index = ntohl(policer_index);
89 else
90 rmp->policer_index = ~0;
91 }));
92 /* *INDENT-ON* */
93}
94
95static void
96send_policer_details (u8 * name,
97 sse2_qos_pol_cfg_params_st * config,
98 policer_read_response_type_st * templ,
Florin Coras6c4dae22018-01-09 06:39:23 -080099 vl_api_registration_t * reg, u32 context)
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100100{
101 vl_api_policer_details_t *mp;
102
103 mp = vl_msg_api_alloc (sizeof (*mp));
104 memset (mp, 0, sizeof (*mp));
105 mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
106 mp->context = context;
107 mp->cir = htonl (config->rb.kbps.cir_kbps);
108 mp->eir = htonl (config->rb.kbps.eir_kbps);
Marek Gradzki59ed4902017-03-21 11:51:54 +0100109 mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
110 mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100111 mp->rate_type = config->rate_type;
112 mp->round_type = config->rnd_type;
113 mp->type = config->rfc;
114 mp->conform_action_type = config->conform_action.action_type;
115 mp->conform_dscp = config->conform_action.dscp;
116 mp->exceed_action_type = config->exceed_action.action_type;
117 mp->exceed_dscp = config->exceed_action.dscp;
118 mp->violate_action_type = config->violate_action.action_type;
119 mp->violate_dscp = config->violate_action.dscp;
120 mp->single_rate = templ->single_rate ? 1 : 0;
121 mp->color_aware = templ->color_aware ? 1 : 0;
122 mp->scale = htonl (templ->scale);
123 mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
124 mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
125 mp->current_limit = htonl (templ->current_limit);
126 mp->current_bucket = htonl (templ->current_bucket);
127 mp->extended_limit = htonl (templ->extended_limit);
128 mp->extended_bucket = htonl (templ->extended_bucket);
129 mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
130
131 strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
132
Florin Coras6c4dae22018-01-09 06:39:23 -0800133 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100134}
135
136static void
137vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
138{
Florin Coras6c4dae22018-01-09 06:39:23 -0800139 vl_api_registration_t *reg;
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100140 vnet_policer_main_t *pm = &vnet_policer_main;
141 hash_pair_t *hp;
142 uword *p;
143 u32 pool_index;
144 u8 *match_name = 0;
145 u8 *name;
146 sse2_qos_pol_cfg_params_st *config;
147 policer_read_response_type_st *templ;
148
Florin Coras6c4dae22018-01-09 06:39:23 -0800149 reg = vl_api_client_index_to_registration (mp->client_index);
150 if (!reg)
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100151 return;
152
153 if (mp->match_name_valid)
154 {
155 match_name = format (0, "%s%c", mp->match_name, 0);
Gabriel Ganne3491d7f2017-10-19 15:10:46 +0200156 vec_terminate_c_string (match_name);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100157 }
158
159 if (mp->match_name_valid)
160 {
161 p = hash_get_mem (pm->policer_config_by_name, match_name);
162 if (p)
163 {
164 pool_index = p[0];
165 config = pool_elt_at_index (pm->configs, pool_index);
166 templ = pool_elt_at_index (pm->policer_templates, pool_index);
Florin Coras6c4dae22018-01-09 06:39:23 -0800167 send_policer_details (match_name, config, templ, reg, mp->context);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100168 }
169 }
170 else
171 {
172 /* *INDENT-OFF* */
173 hash_foreach_pair (hp, pm->policer_config_by_name,
174 ({
175 name = (u8 *) hp->key;
176 pool_index = hp->value[0];
177 config = pool_elt_at_index (pm->configs, pool_index);
178 templ = pool_elt_at_index (pm->policer_templates, pool_index);
Florin Coras6c4dae22018-01-09 06:39:23 -0800179 send_policer_details(name, config, templ, reg, mp->context);
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100180 }));
181 /* *INDENT-ON* */
182 }
183}
184
185/*
186 * policer_api_hookup
187 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700188 * vlib has already mapped shared memory and
Pavel Kotucekd9aad292017-01-25 08:45:38 +0100189 * added the client registration handlers.
190 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
191 */
192#define vl_msg_name_crc_list
193#include <vnet/vnet_all_api_h.h>
194#undef vl_msg_name_crc_list
195
196static void
197setup_message_id_table (api_main_t * am)
198{
199#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
200 foreach_vl_msg_name_crc_policer;
201#undef _
202}
203
204static clib_error_t *
205policer_api_hookup (vlib_main_t * vm)
206{
207 api_main_t *am = &api_main;
208
209#define _(N,n) \
210 vl_msg_api_set_handlers(VL_API_##N, #n, \
211 vl_api_##n##_t_handler, \
212 vl_noop_handler, \
213 vl_api_##n##_t_endian, \
214 vl_api_##n##_t_print, \
215 sizeof(vl_api_##n##_t), 1);
216 foreach_vpe_api_msg;
217#undef _
218 /*
219 * Set up the (msg_name, crc, message-id) table
220 */
221 setup_message_id_table (am);
222
223 return 0;
224}
225
226VLIB_API_INIT_FUNCTION (policer_api_hookup);
227
228/*
229 * fd.io coding-style-patch-verification: ON
230 *
231 * Local Variables:
232 * eval: (c-set-style "gnu")
233 * End:
234 */