FD.io VPP  v17.07-30-g839fa73
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 {
52  vlib_main_t *vm = vlib_get_main ();
54  int rv = 0;
55  u8 *name = NULL;
57  clib_error_t *error;
58  u32 policer_index;
59 
60  name = format (0, "%s", mp->name);
61 
62  memset (&cfg, 0, sizeof (cfg));
63  cfg.rfc = mp->type;
64  cfg.rnd_type = mp->round_type;
65  cfg.rate_type = mp->rate_type;
66  cfg.rb.kbps.cir_kbps = mp->cir;
67  cfg.rb.kbps.eir_kbps = mp->eir;
68  cfg.rb.kbps.cb_bytes = mp->cb;
69  cfg.rb.kbps.eb_bytes = mp->eb;
73  cfg.exceed_action.dscp = mp->exceed_dscp;
76  cfg.color_aware = mp->color_aware;
77 
78  error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
79 
80  if (error)
81  rv = VNET_API_ERROR_UNSPECIFIED;
82 
83  /* *INDENT-OFF* */
84  REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
85  ({
86  if (rv == 0 && mp->is_add)
87  rmp->policer_index = ntohl(policer_index);
88  else
89  rmp->policer_index = ~0;
90  }));
91  /* *INDENT-ON* */
92 }
93 
94 static void
98  unix_shared_memory_queue_t * q, u32 context)
99 {
101 
102  mp = vl_msg_api_alloc (sizeof (*mp));
103  memset (mp, 0, sizeof (*mp));
104  mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
105  mp->context = context;
106  mp->cir = htonl (config->rb.kbps.cir_kbps);
107  mp->eir = htonl (config->rb.kbps.eir_kbps);
108  mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes);
109  mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes);
110  mp->rate_type = config->rate_type;
111  mp->round_type = config->rnd_type;
112  mp->type = config->rfc;
114  mp->conform_dscp = config->conform_action.dscp;
116  mp->exceed_dscp = config->exceed_action.dscp;
118  mp->violate_dscp = config->violate_action.dscp;
119  mp->single_rate = templ->single_rate ? 1 : 0;
120  mp->color_aware = templ->color_aware ? 1 : 0;
121  mp->scale = htonl (templ->scale);
122  mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
123  mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
124  mp->current_limit = htonl (templ->current_limit);
125  mp->current_bucket = htonl (templ->current_bucket);
126  mp->extended_limit = htonl (templ->extended_limit);
127  mp->extended_bucket = htonl (templ->extended_bucket);
128  mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
129 
130  strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
131 
132  vl_msg_api_send_shmem (q, (u8 *) & mp);
133 }
134 
135 static void
137 {
140  hash_pair_t *hp;
141  uword *p;
142  u32 pool_index;
143  u8 *match_name = 0;
144  u8 *name;
147 
149  if (q == 0)
150  return;
151 
152  if (mp->match_name_valid)
153  {
154  match_name = format (0, "%s%c", mp->match_name, 0);
155  }
156 
157  if (mp->match_name_valid)
158  {
159  p = hash_get_mem (pm->policer_config_by_name, match_name);
160  if (p)
161  {
162  pool_index = p[0];
163  config = pool_elt_at_index (pm->configs, pool_index);
164  templ = pool_elt_at_index (pm->policer_templates, pool_index);
165  send_policer_details (match_name, config, templ, q, mp->context);
166  }
167  }
168  else
169  {
170  /* *INDENT-OFF* */
172  ({
173  name = (u8 *) hp->key;
174  pool_index = hp->value[0];
175  config = pool_elt_at_index (pm->configs, pool_index);
176  templ = pool_elt_at_index (pm->policer_templates, pool_index);
177  send_policer_details(name, config, templ, q, mp->context);
178  }));
179  /* *INDENT-ON* */
180  }
181 }
182 
183 /*
184  * policer_api_hookup
185  * Add vpe's API message handlers to the table.
186  * vlib has alread mapped shared memory and
187  * added the client registration handlers.
188  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
189  */
190 #define vl_msg_name_crc_list
191 #include <vnet/vnet_all_api_h.h>
192 #undef vl_msg_name_crc_list
193 
194 static void
196 {
197 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
198  foreach_vl_msg_name_crc_policer;
199 #undef _
200 }
201 
202 static clib_error_t *
204 {
205  api_main_t *am = &api_main;
206 
207 #define _(N,n) \
208  vl_msg_api_set_handlers(VL_API_##N, #n, \
209  vl_api_##n##_t_handler, \
210  vl_noop_handler, \
211  vl_api_##n##_t_endian, \
212  vl_api_##n##_t_print, \
213  sizeof(vl_api_##n##_t), 1);
215 #undef _
216  /*
217  * Set up the (msg_name, crc, message-id) table
218  */
220 
221  return 0;
222 }
223 
225 
226 /*
227  * fd.io coding-style-patch-verification: ON
228  *
229  * Local Variables:
230  * eval: (c-set-style "gnu")
231  * End:
232  */
Policer operational state response.
Definition: policer.api:113
Get list of policers.
Definition: policer.api:77
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
uword * policer_config_by_name
Definition: policer.h:34
#define REPLY_MACRO2(t, body)
#define NULL
Definition: clib.h:55
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define foreach_vpe_api_msg
Definition: policer_api.c:45
Add/del policer response.
Definition: policer.api:64
static void setup_message_id_table(api_main_t *am)
Definition: policer_api.c:195
void * vl_msg_api_alloc(int nbytes)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
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:203
sse2_qos_pol_action_params_st exceed_action
Definition: xlate.h:152
api_main_t api_main
Definition: api_shared.c:35
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
#define ARRAY_LEN(x)
Definition: clib.h:59
union sse2_qos_pol_cfg_params_st_::@232 rb
static void vl_api_policer_dump_t_handler(vl_api_policer_dump_t *mp)
Definition: policer_api.c:136
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
static void send_policer_details(u8 *name, sse2_qos_pol_cfg_params_st *config, policer_read_response_type_st *templ, unix_shared_memory_queue_t *q, u32 context)
Definition: policer_api.c:95
unsigned int u32
Definition: types.h:88
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:20
VLIB_API_INIT_FUNCTION(policer_api_hookup)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:349
struct sse2_qos_pol_cfg_params_st_::@232::@233 kbps
#define hash_get_mem(h, key)
Definition: hash.h:268
policer_read_response_type_st * policer_templates
Definition: policer.h:31
Add/del policer.
Definition: policer.api:36
vnet_policer_main_t vnet_policer_main
Definition: policer.h:47
struct _unix_shared_memory_queue unix_shared_memory_queue_t