FD.io VPP  v21.10.1-2-g0a485f517
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/format_fns.h>
28 #include <vnet/policer/policer.api_enum.h>
29 #include <vnet/policer/policer.api_types.h>
30 
31 #define REPLY_MSG_ID_BASE vnet_policer_main.msg_id_base
33 
34 static void
36 {
39  int rv = 0;
40  u8 *name = NULL;
43  u32 policer_index;
44 
45  name = format (0, "%s", mp->name);
47 
48  clib_memset (&cfg, 0, sizeof (cfg));
49  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;
52  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);
57  (qos_action_type_en) mp->conform_action.type;
58  cfg.conform_action.dscp = mp->conform_action.dscp;
59  cfg.exceed_action.action_type = (qos_action_type_en) mp->exceed_action.type;
60  cfg.exceed_action.dscp = mp->exceed_action.dscp;
62  (qos_action_type_en) mp->violate_action.type;
63  cfg.violate_action.dscp = mp->violate_action.dscp;
64 
65  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 
83 static void
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);
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 
103 static void
105 {
106  vl_api_policer_bind_reply_t *rmp;
107  u8 *name;
109  u8 apply;
110  int rv;
111 
113 
114  name = format (0, "%s", mp->name);
116 
117  sw_if_index = ntohl (mp->sw_if_index);
118  apply = mp->apply;
119 
121  vec_free (name);
122 
124  REPLY_MACRO (VL_API_POLICER_INPUT_REPLY);
125 }
126 
127 static void
129  policer_t *templ, vl_api_registration_t *reg,
130  u32 context)
131 {
133 
134  mp = vl_msg_api_alloc (sizeof (*mp));
135  clib_memset (mp, 0, sizeof (*mp));
136  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_POLICER_DETAILS);
137  mp->context = context;
138  mp->cir = htonl (config->rb.kbps.cir_kbps);
139  mp->eir = htonl (config->rb.kbps.eir_kbps);
140  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);
142  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;
145  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;
154  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 
167  vl_api_send_msg (reg, (u8 *) mp);
168 }
169 
170 static void
172 {
175  hash_pair_t *hp;
176  uword *p;
177  u32 pool_index;
178  u8 *match_name = 0;
179  u8 *name;
180  qos_pol_cfg_params_st *config;
181  policer_t *templ;
182 
184  if (!reg)
185  return;
186 
187  if (mp->match_name_valid)
188  {
189  match_name = format (0, "%s%c", mp->match_name, 0);
190  vec_terminate_c_string (match_name);
191  }
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);
201  send_policer_details (match_name, config, templ, reg, mp->context);
202  }
203  }
204  else
205  {
206  /* *INDENT-OFF* */
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);
213  send_policer_details(name, config, templ, reg, mp->context);
214  }));
215  /* *INDENT-ON* */
216  }
217 }
218 
219 #include <vnet/policer/policer.api.c>
220 static clib_error_t *
222 {
223  /*
224  * Set up the (msg_name, crc, message-id) table
225  */
227 
228  return 0;
229 }
230 
232 
233 /*
234  * fd.io coding-style-patch-verification: ON
235  *
236  * Local Variables:
237  * eval: (c-set-style "gnu")
238  * End:
239  */
vl_api_policer_add_del_reply_t
Add/del policer response.
Definition: policer.api:97
vnet_policer_main_t::configs
qos_pol_cfg_params_st * configs
Definition: policer.h:32
policer_t::color_aware
u32 color_aware
Definition: police.h:79
vl_api_policer_details_t::rate_type
vl_api_sse2_qos_rate_type_t rate_type
Definition: policer.api:152
vnet_policer_main_t
Definition: policer.h:26
VALIDATE_SW_IF_INDEX
#define VALIDATE_SW_IF_INDEX(mp)
Definition: api_helper_macros.h:281
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
vnet_policer_main_t::policer_config_by_name
uword * policer_config_by_name
Definition: policer.h:36
api.h
vl_api_policer_add_del_reply_t::policer_index
u32 policer_index
Definition: policer.api:101
policer_input
int policer_input(u8 *name, u32 sw_if_index, bool apply)
Definition: policer.c:169
vl_api_policer_details_t::conform_action
vl_api_sse2_qos_action_t conform_action
Definition: policer.api:155
ntohs
#define ntohs(x)
Definition: af_xdp.bpf.c:29
policer_t::extended_bucket
u32 extended_bucket
Definition: police.h:95
vl_api_policer_details_t::violate_action
vl_api_sse2_qos_action_t violate_action
Definition: policer.api:157
qos_pol_cfg_params_st_::color_aware
u8 color_aware
Definition: xlate.h:137
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
vl_api_policer_details_t::eir
u32 eir
Definition: policer.api:149
vnet_policer_main
vnet_policer_main_t vnet_policer_main
Definition: policer.c:22
vl_api_policer_dump_t::match_name
string match_name[64]
Definition: policer.api:116
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
hash_foreach_pair
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:373
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
vl_api_policer_details_t::round_type
vl_api_sse2_qos_round_type_t round_type
Definition: policer.api:153
name
string name[64]
Definition: fib.api:25
policer_t::current_bucket
u32 current_bucket
Definition: police.h:93
vl_api_policer_details_t::exceed_action
vl_api_sse2_qos_action_t exceed_action
Definition: policer.api:156
vl_api_policer_details_t::current_bucket
u32 current_bucket
Definition: policer.api:164
vl_api_policer_dump_t::match_name_valid
bool match_name_valid
Definition: policer.api:115
vl_api_policer_add_del_t::violate_action
vl_api_sse2_qos_action_t violate_action
Definition: policer.api:89
vl_api_policer_dump_t
Get list of policers.
Definition: policer.api:110
policer_t
Definition: police.h:73
policer_t::current_limit
u32 current_limit
Definition: police.h:92
vl_api_policer_add_del_t::type
vl_api_sse2_qos_policer_type_t type
Definition: policer.api:85
qos_pol_cfg_params_st_::rfc
qos_policer_type_en rfc
Definition: xlate.h:136
vl_api_policer_add_del_t::cb
u64 cb
Definition: policer.api:81
qos_pol_cfg_params_st_
Definition: xlate.h:115
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vl_api_policer_add_del_t::eir
u32 eir
Definition: policer.api:80
policer.h
vl_api_policer_details_t::type
vl_api_sse2_qos_policer_type_t type
Definition: policer.api:154
vl_api_policer_add_del_t::is_add
bool is_add
Definition: policer.api:77
qos_pol_cfg_params_st_::conform_action
qos_pol_action_params_st conform_action
Definition: xlate.h:141
vl_api_policer_input_t::apply
bool apply
Definition: policer.api:52
send_policer_details
static void send_policer_details(u8 *name, qos_pol_cfg_params_st *config, policer_t *templ, vl_api_registration_t *reg, u32 context)
Definition: policer_api.c:128
error
Definition: cJSON.c:88
vl_api_policer_add_del_t_handler
static void vl_api_policer_add_del_t_handler(vl_api_policer_add_del_t *mp)
Definition: policer_api.c:35
policer_t::cir_tokens_per_period
u32 cir_tokens_per_period
Definition: police.h:89
vl_api_policer_add_del_t::name
string name[64]
Definition: policer.api:78
vl_api_policer_bind_t::bind_enable
bool bind_enable
Definition: policer.api:35
vl_api_policer_add_del_t::conform_action
vl_api_sse2_qos_action_t conform_action
Definition: policer.api:87
vl_api_policer_details_t::pir_tokens_per_period
u32 pir_tokens_per_period
Definition: policer.api:162
policer_t::pir_tokens_per_period
u32 pir_tokens_per_period
Definition: police.h:90
policer_t::scale
u32 scale
Definition: police.h:80
policer_add_del
clib_error_t * policer_add_del(vlib_main_t *vm, u8 *name, qos_pol_cfg_params_st *cfg, u32 *policer_index, u8 is_add)
Definition: policer.c:53
vl_api_policer_input_t_handler
static void vl_api_policer_input_t_handler(vl_api_policer_input_t *mp)
Definition: policer_api.c:104
policer_t::single_rate
u32 single_rate
Definition: police.h:78
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vl_api_policer_details_t::scale
u32 scale
Definition: policer.api:160
vl_api_policer_dump_t::client_index
u32 client_index
Definition: policer.api:112
uword
u64 uword
Definition: types.h:112
vl_api_policer_details_t::single_rate
bool single_rate
Definition: policer.api:158
vl_api_policer_details_t::cir
u32 cir
Definition: policer.api:148
vl_api_policer_dump_t_handler
static void vl_api_policer_dump_t_handler(vl_api_policer_dump_t *mp)
Definition: policer_api.c:171
interface.h
REPLY_MSG_ID_BASE
#define REPLY_MSG_ID_BASE
Definition: policer_api.c:31
vl_api_policer_add_del_t::rate_type
vl_api_sse2_qos_rate_type_t rate_type
Definition: policer.api:83
qos_pol_action_params_st_::dscp
ip_dscp_t dscp
Definition: xlate.h:81
vl_api_policer_bind_t
policer bind: Associate/disassociate a policer with a worker thread.
Definition: policer.api:28
vl_api_policer_details_t::color_aware
bool color_aware
Definition: policer.api:159
apply
static int apply(phash_main_t *pm, u32 tail, u32 rollback)
Definition: phash.c:408
qos_pol_cfg_params_st_::exceed_action
qos_pol_action_params_st exceed_action
Definition: xlate.h:142
vl_api_policer_input_t
policer input: Apply policer as an input feature.
Definition: policer.api:45
qos_policer_type_en
enum qos_policer_type_en_ qos_policer_type_en
vl_api_policer_details_t::context
u32 context
Definition: policer.api:145
BAD_SW_IF_INDEX_LABEL
#define BAD_SW_IF_INDEX_LABEL
Definition: api_helper_macros.h:289
vl_api_policer_details_t::extended_limit
u32 extended_limit
Definition: policer.api:165
vnet_policer_main_t::policer_templates
policer_t * policer_templates
Definition: policer.h:33
vl_api_policer_bind_t::worker_index
u32 worker_index
Definition: policer.api:34
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
qos_pol_cfg_params_st_::rnd_type
qos_round_type_en rnd_type
Definition: xlate.h:135
policer_t::extended_limit
u32 extended_limit
Definition: police.h:94
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vl_api_policer_details_t
Policer operational state response.
Definition: policer.api:143
qos_pol_cfg_params_st_::violate_action
qos_pol_action_params_st violate_action
Definition: xlate.h:143
vl_api_policer_bind_t::name
string name[64]
Definition: policer.api:33
vl_api_policer_add_del_t::round_type
vl_api_sse2_qos_round_type_t round_type
Definition: policer.api:84
format_fns.h
policer_api_hookup
static clib_error_t * policer_api_hookup(vlib_main_t *vm)
Definition: policer_api.c:221
format
description fragment has unexpected format
Definition: map.api:433
vl_api_policer_dump_t::context
u32 context
Definition: policer.api:113
u32
unsigned int u32
Definition: types.h:88
qos_pol_cfg_params_st_::rate_type
qos_rate_type_en rate_type
Definition: xlate.h:134
vl_api_policer_add_del_t::exceed_action
vl_api_sse2_qos_action_t exceed_action
Definition: policer.api:88
vl_api_policer_add_del_t::cir
u32 cir
Definition: policer.api:79
vl_api_policer_add_del_t::eb
u64 eb
Definition: policer.api:82
api_helper_macros.h
vl_api_policer_details_t::current_limit
u32 current_limit
Definition: policer.api:163
vl_api_policer_add_del_t
Add/del policer.
Definition: policer.api:72
policer_bind_worker
int policer_bind_worker(u8 *name, u32 worker, bool bind)
Definition: policer.c:138
vl_api_policer_details_t::eb
u64 eb
Definition: policer.api:151
qos_pol_cfg_params_st_::rb
union qos_pol_cfg_params_st_::@496 rb
vl_api_policer_details_t::name
string name[64]
Definition: policer.api:147
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
vl_api_policer_details_t::last_update_time
u64 last_update_time
Definition: policer.api:167
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vl_api_policer_details_t::cb
u64 cb
Definition: policer.api:150
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(policer_api_hookup)
vec_terminate_c_string
#define vec_terminate_c_string(V)
(If necessary) NULL terminate a vector containing a c-string.
Definition: vec.h:1132
vl_api_policer_input_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: policer.api:51
context
u32 context
Definition: ip.api:852
rv
int __clib_unused rv
Definition: application.c:491
vnet.h
api_errno.h
qos_pol_action_params_st_::action_type
qos_action_type_en action_type
Definition: xlate.h:80
policer_t::last_update_time
u64 last_update_time
Definition: police.h:97
qos_pol_cfg_params_st_::kbps
struct qos_pol_cfg_params_st_::@496::@497 kbps
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
vl_api_policer_details_t::extended_bucket
u32 extended_bucket
Definition: policer.api:166
vl_api_policer_bind_t_handler
static void vl_api_policer_bind_t_handler(vl_api_policer_bind_t *mp)
Definition: policer_api.c:84
vl_api_policer_add_del_t::color_aware
bool color_aware
Definition: policer.api:86
hash_pair_t
Definition: hash.h:159
vl_api_policer_details_t::cir_tokens_per_period
u32 cir_tokens_per_period
Definition: policer.api:161
vl_api_policer_input_t::name
string name[64]
Definition: policer.api:50
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199