policer: use enum types
[vpp.git] / src / vnet / policer / policer_api.c
1 /*
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
49 static void
50 vl_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   qos_pol_cfg_params_st cfg;
57   clib_error_t *error;
58   u32 policer_index;
59
60   name = format (0, "%s", mp->name);
61   vec_terminate_c_string (name);
62
63   clib_memset (&cfg, 0, sizeof (cfg));
64   cfg.rfc = (qos_policer_type_en) mp->type;
65   cfg.rnd_type = (qos_round_type_en) mp->round_type;
66   cfg.rate_type = (qos_rate_type_en) mp->rate_type;
67   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);
71   cfg.conform_action.action_type =
72     (qos_action_type_en) mp->conform_action.type;
73   cfg.conform_action.dscp = mp->conform_action.dscp;
74   cfg.exceed_action.action_type = (qos_action_type_en) mp->exceed_action.type;
75   cfg.exceed_action.dscp = mp->exceed_action.dscp;
76   cfg.violate_action.action_type =
77     (qos_action_type_en) mp->violate_action.type;
78   cfg.violate_action.dscp = mp->violate_action.dscp;
79
80   cfg.color_aware = mp->color_aware;
81
82   error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
83
84   if (error)
85     rv = VNET_API_ERROR_UNSPECIFIED;
86
87   /* *INDENT-OFF* */
88   REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
89   ({
90     if (rv == 0 &&  mp->is_add)
91       rmp->policer_index = ntohl(policer_index);
92     else
93       rmp->policer_index = ~0;
94   }));
95   /* *INDENT-ON* */
96 }
97
98 static void
99 send_policer_details (u8 *name, qos_pol_cfg_params_st *config,
100                       policer_read_response_type_st *templ,
101                       vl_api_registration_t *reg, u32 context)
102 {
103   vl_api_policer_details_t *mp;
104
105   mp = vl_msg_api_alloc (sizeof (*mp));
106   clib_memset (mp, 0, sizeof (*mp));
107   mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
108   mp->context = context;
109   mp->cir = htonl (config->rb.kbps.cir_kbps);
110   mp->eir = htonl (config->rb.kbps.eir_kbps);
111   mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
112   mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
113   mp->rate_type = (vl_api_sse2_qos_rate_type_t) config->rate_type;
114   mp->round_type = (vl_api_sse2_qos_round_type_t) config->rnd_type;
115   mp->type = (vl_api_sse2_qos_policer_type_t) config->rfc;
116   mp->conform_action.type =
117     (vl_api_sse2_qos_action_type_t) config->conform_action.action_type;
118   mp->conform_action.dscp = config->conform_action.dscp;
119   mp->exceed_action.type =
120     (vl_api_sse2_qos_action_type_t) config->exceed_action.action_type;
121   mp->exceed_action.dscp = config->exceed_action.dscp;
122   mp->violate_action.type =
123     (vl_api_sse2_qos_action_type_t) config->violate_action.action_type;
124   mp->violate_action.dscp = config->violate_action.dscp;
125   mp->single_rate = templ->single_rate ? 1 : 0;
126   mp->color_aware = templ->color_aware ? 1 : 0;
127   mp->scale = htonl (templ->scale);
128   mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
129   mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
130   mp->current_limit = htonl (templ->current_limit);
131   mp->current_bucket = htonl (templ->current_bucket);
132   mp->extended_limit = htonl (templ->extended_limit);
133   mp->extended_bucket = htonl (templ->extended_bucket);
134   mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
135
136   strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
137
138   vl_api_send_msg (reg, (u8 *) mp);
139 }
140
141 static void
142 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
143 {
144   vl_api_registration_t *reg;
145   vnet_policer_main_t *pm = &vnet_policer_main;
146   hash_pair_t *hp;
147   uword *p;
148   u32 pool_index;
149   u8 *match_name = 0;
150   u8 *name;
151   qos_pol_cfg_params_st *config;
152   policer_read_response_type_st *templ;
153
154   reg = vl_api_client_index_to_registration (mp->client_index);
155   if (!reg)
156     return;
157
158   if (mp->match_name_valid)
159     {
160       match_name = format (0, "%s%c", mp->match_name, 0);
161       vec_terminate_c_string (match_name);
162     }
163
164   if (mp->match_name_valid)
165     {
166       p = hash_get_mem (pm->policer_config_by_name, match_name);
167       if (p)
168         {
169           pool_index = p[0];
170           config = pool_elt_at_index (pm->configs, pool_index);
171           templ = pool_elt_at_index (pm->policer_templates, pool_index);
172           send_policer_details (match_name, config, templ, reg, mp->context);
173         }
174     }
175   else
176     {
177       /* *INDENT-OFF* */
178       hash_foreach_pair (hp, pm->policer_config_by_name,
179       ({
180         name = (u8 *) hp->key;
181         pool_index = hp->value[0];
182         config = pool_elt_at_index (pm->configs, pool_index);
183         templ = pool_elt_at_index (pm->policer_templates, pool_index);
184         send_policer_details(name, config, templ, reg, mp->context);
185       }));
186       /* *INDENT-ON* */
187     }
188 }
189
190 /*
191  * policer_api_hookup
192  * Add vpe's API message handlers to the table.
193  * vlib has already mapped shared memory and
194  * added the client registration handlers.
195  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
196  */
197 #define vl_msg_name_crc_list
198 #include <vnet/vnet_all_api_h.h>
199 #undef vl_msg_name_crc_list
200
201 static void
202 setup_message_id_table (api_main_t * am)
203 {
204 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
205   foreach_vl_msg_name_crc_policer;
206 #undef _
207 }
208
209 static clib_error_t *
210 policer_api_hookup (vlib_main_t * vm)
211 {
212   api_main_t *am = vlibapi_get_main ();
213
214 #define _(N,n)                                                  \
215     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
216                            vl_api_##n##_t_handler,              \
217                            vl_noop_handler,                     \
218                            vl_api_##n##_t_endian,               \
219                            vl_api_##n##_t_print,                \
220                            sizeof(vl_api_##n##_t), 1);
221   foreach_vpe_api_msg;
222 #undef _
223   /*
224    * Set up the (msg_name, crc, message-id) table
225    */
226   setup_message_id_table (am);
227
228   return 0;
229 }
230
231 VLIB_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  */