FD.io VPP  v21.01.1
Vector Packet Processing
policer_api.c
Go to the documentation of this file.
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 
44 
45 #define foreach_vpe_api_msg \
46 _(POLICER_ADD_DEL, policer_add_del) \
47 _(POLICER_DUMP, policer_dump)
48 
49 static void
51 {
54  int rv = 0;
55  u8 *name = NULL;
58  u32 policer_index;
59 
60  name = format (0, "%s", mp->name);
62 
63  clib_memset (&cfg, 0, sizeof (cfg));
64  cfg.rfc = mp->type;
65  cfg.rnd_type = mp->round_type;
66  cfg.rate_type = 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);
73  cfg.conform_action.dscp = mp->conform_action.dscp;
76  cfg.exceed_action.dscp = mp->exceed_action.dscp;
79  cfg.violate_action.dscp = mp->violate_action.dscp;
80 
81  cfg.color_aware = mp->color_aware;
82 
83  error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
84 
85  if (error)
86  rv = VNET_API_ERROR_UNSPECIFIED;
87 
88  /* *INDENT-OFF* */
89  REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
90  ({
91  if (rv == 0 && mp->is_add)
92  rmp->policer_index = ntohl(policer_index);
93  else
94  rmp->policer_index = ~0;
95  }));
96  /* *INDENT-ON* */
97 }
98 
99 static void
103  vl_api_registration_t * reg, u32 context)
104 {
106 
107  mp = vl_msg_api_alloc (sizeof (*mp));
108  clib_memset (mp, 0, sizeof (*mp));
109  mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
110  mp->context = context;
111  mp->cir = htonl (config->rb.kbps.cir_kbps);
112  mp->eir = htonl (config->rb.kbps.eir_kbps);
113  mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
114  mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
115  mp->rate_type = config->rate_type;
116  mp->round_type = config->rnd_type;
117  mp->type = config->rfc;
118  mp->conform_action.type =
119  (vl_api_sse2_qos_action_type_t) config->conform_action.action_type;
120  mp->conform_action.dscp = config->conform_action.dscp;
121  mp->exceed_action.type =
122  (vl_api_sse2_qos_action_type_t) config->exceed_action.action_type;
123  mp->exceed_action.dscp = config->exceed_action.dscp;
124  mp->violate_action.type =
125  (vl_api_sse2_qos_action_type_t) config->violate_action.action_type;
126  mp->violate_action.dscp = config->violate_action.dscp;
127  mp->single_rate = templ->single_rate ? 1 : 0;
128  mp->color_aware = templ->color_aware ? 1 : 0;
129  mp->scale = htonl (templ->scale);
130  mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
131  mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
132  mp->current_limit = htonl (templ->current_limit);
133  mp->current_bucket = htonl (templ->current_bucket);
134  mp->extended_limit = htonl (templ->extended_limit);
135  mp->extended_bucket = htonl (templ->extended_bucket);
136  mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
137 
138  strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
139 
140  vl_api_send_msg (reg, (u8 *) mp);
141 }
142 
143 static void
145 {
148  hash_pair_t *hp;
149  uword *p;
150  u32 pool_index;
151  u8 *match_name = 0;
152  u8 *name;
155 
157  if (!reg)
158  return;
159 
160  if (mp->match_name_valid)
161  {
162  match_name = format (0, "%s%c", mp->match_name, 0);
163  vec_terminate_c_string (match_name);
164  }
165 
166  if (mp->match_name_valid)
167  {
168  p = hash_get_mem (pm->policer_config_by_name, match_name);
169  if (p)
170  {
171  pool_index = p[0];
172  config = pool_elt_at_index (pm->configs, pool_index);
173  templ = pool_elt_at_index (pm->policer_templates, pool_index);
174  send_policer_details (match_name, config, templ, reg, mp->context);
175  }
176  }
177  else
178  {
179  /* *INDENT-OFF* */
181  ({
182  name = (u8 *) hp->key;
183  pool_index = hp->value[0];
184  config = pool_elt_at_index (pm->configs, pool_index);
185  templ = pool_elt_at_index (pm->policer_templates, pool_index);
186  send_policer_details(name, config, templ, reg, mp->context);
187  }));
188  /* *INDENT-ON* */
189  }
190 }
191 
192 /*
193  * policer_api_hookup
194  * Add vpe's API message handlers to the table.
195  * vlib has already mapped shared memory and
196  * added the client registration handlers.
197  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
198  */
199 #define vl_msg_name_crc_list
200 #include <vnet/vnet_all_api_h.h>
201 #undef vl_msg_name_crc_list
202 
203 static void
205 {
206 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
207  foreach_vl_msg_name_crc_policer;
208 #undef _
209 }
210 
211 static clib_error_t *
213 {
214  api_main_t *am = vlibapi_get_main ();
215 
216 #define _(N,n) \
217  vl_msg_api_set_handlers(VL_API_##N, #n, \
218  vl_api_##n##_t_handler, \
219  vl_noop_handler, \
220  vl_api_##n##_t_endian, \
221  vl_api_##n##_t_print, \
222  sizeof(vl_api_##n##_t), 1);
224 #undef _
225  /*
226  * Set up the (msg_name, crc, message-id) table
227  */
229 
230  return 0;
231 }
232 
234 
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "gnu")
240  * End:
241  */
Policer operational state response.
Definition: policer.api:108
Get list of policers.
Definition: policer.api:75
sse2_qos_pol_cfg_params_st * configs
Definition: policer.h:30
static void vl_api_policer_add_del_t_handler(vl_api_policer_add_del_t *mp)
Definition: policer_api.c:50
sse2_qos_pol_action_params_st conform_action
Definition: xlate.h:151
#define ntohs(x)
Definition: af_xdp.bpf.c:29
uword * policer_config_by_name
Definition: policer.h:34
vl_api_sse2_qos_policer_type_t type
Definition: policer.api:50
#define REPLY_MACRO2(t, body)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vl_api_sse2_qos_rate_type_t rate_type
Definition: policer.api:117
#define vec_terminate_c_string(V)
(If necessary) NULL terminate a vector containing a c-string.
Definition: vec.h:1090
struct sse2_qos_pol_cfg_params_st_::@498::@499 kbps
vlib_main_t * vm
Definition: in2out_ed.c:1580
#define foreach_vpe_api_msg
Definition: policer_api.c:45
void * vl_msg_api_alloc(int nbytes)
Add/del policer response.
Definition: policer.api:62
unsigned char u8
Definition: types.h:56
vnet_policer_main_t vnet_policer_main
Definition: policer.c:19
static void setup_message_id_table(api_main_t *am)
Definition: policer_api.c:204
vl_api_sse2_qos_action_t exceed_action
Definition: policer.api:53
description fragment has unexpected format
Definition: map.api:433
unsigned int u32
Definition: types.h:88
vl_api_sse2_qos_policer_type_t type
Definition: policer.api:119
Definition: cJSON.c:84
string match_name[64]
Definition: policer.api:81
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
sse2_qos_pol_action_params_st violate_action
Definition: xlate.h:153
static clib_error_t * policer_api_hookup(vlib_main_t *vm)
Definition: policer_api.c:212
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:227
sse2_qos_pol_action_params_st exceed_action
Definition: xlate.h:152
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
union sse2_qos_pol_cfg_params_st_::@498 rb
vl_api_sse2_qos_action_t violate_action
Definition: policer.api:54
vl_api_sse2_qos_action_t conform_action
Definition: policer.api:52
#define ARRAY_LEN(x)
Definition: clib.h:67
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
string name[64]
Definition: ip.api:44
static void vl_api_policer_dump_t_handler(vl_api_policer_dump_t *mp)
Definition: policer_api.c:144
clib_error_t * policer_add_del(vlib_main_t *vm, u8 *name, sse2_qos_pol_cfg_params_st *cfg, u32 *policer_index, u8 is_add)
Definition: policer.c:22
static void send_policer_details(u8 *name, sse2_qos_pol_cfg_params_st *config, policer_read_response_type_st *templ, vl_api_registration_t *reg, u32 context)
Definition: policer_api.c:100
VLIB_API_INIT_FUNCTION(policer_api_hookup)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vl_api_sse2_qos_round_type_t round_type
Definition: policer.api:49
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:373
u64 uword
Definition: types.h:112
vl_api_sse2_qos_rate_type_t rate_type
Definition: policer.api:48
#define hash_get_mem(h, key)
Definition: hash.h:269
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
policer_read_response_type_st * policer_templates
Definition: policer.h:31
vl_api_sse2_qos_action_t exceed_action
Definition: policer.api:121
Add/del policer.
Definition: policer.api:37
vl_api_sse2_qos_action_t violate_action
Definition: policer.api:122
vl_api_sse2_qos_action_t conform_action
Definition: policer.api:120
sse2_qos_action_type_en
Definition: xlate.h:72
vl_api_sse2_qos_round_type_t round_type
Definition: policer.api:118