FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
policer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <stdint.h>
16 #include <vnet/policer/policer.h>
18 
20 
23  u8 * name,
25  u32 * policer_index, u8 is_add)
26 {
28  policer_read_response_type_st test_policer;
30  uword *p;
31  u32 pi;
32  int rv;
33 
34  p = hash_get_mem (pm->policer_config_by_name, name);
35 
36  if (is_add == 0)
37  {
38  /* free policer config and template */
39  if (p == 0)
40  {
41  vec_free (name);
42  return clib_error_return (0, "No such policer configuration");
43  }
44  pool_put_index (pm->configs, p[0]);
47 
48  /* free policer */
49  p = hash_get_mem (pm->policer_index_by_name, name);
50  if (p == 0)
51  {
52  vec_free (name);
53  return clib_error_return (0, "No such policer");
54  }
55  pool_put_index (pm->policers, p[0]);
57 
58  vec_free (name);
59  return 0;
60  }
61 
62  if (p != 0)
63  {
64  vec_free (name);
65  return clib_error_return (0, "Policer already exists");
66  }
67 
68  /* Vet the configuration before adding it to the table */
69  rv = sse2_pol_logical_2_physical (cfg, &test_policer);
70 
71  if (rv == 0)
72  {
75 
76  pool_get (pm->configs, cp);
77  pool_get (pm->policer_templates, pp);
78 
79  ASSERT (cp - pm->configs == pp - pm->policer_templates);
80 
81  clib_memcpy (cp, cfg, sizeof (*cp));
82  clib_memcpy (pp, &test_policer, sizeof (*pp));
83 
84  hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
86  policer[0] = pp[0];
87  pi = policer - pm->policers;
88  hash_set_mem (pm->policer_index_by_name, name, pi);
89  *policer_index = pi;
90  }
91  else
92  {
93  vec_free (name);
94  return clib_error_return (0, "Config failed sanity check");
95  }
96 
97  return 0;
98 }
99 
100 u8 *
101 format_policer_instance (u8 * s, va_list * va)
102 {
104  = va_arg (*va, policer_read_response_type_st *);
105 
106  s = format (s, "policer at %llx: %s rate, %s color-aware\n",
107  i, i->single_rate ? "single" : "dual",
108  i->color_aware ? "is" : "not");
109  s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
111  s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
112  i->current_limit,
114  s = format (s, "last update %llu\n", i->last_update_time);
115  return s;
116 }
117 
118 static u8 *
119 format_policer_round_type (u8 * s, va_list * va)
120 {
122 
124  s = format (s, "closest");
125  else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
126  s = format (s, "up");
127  else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
128  s = format (s, "down");
129  else
130  s = format (s, "ILLEGAL");
131  return s;
132 }
133 
134 
135 static u8 *
136 format_policer_rate_type (u8 * s, va_list * va)
137 {
139 
140  if (c->rate_type == SSE2_QOS_RATE_KBPS)
141  s = format (s, "kbps");
142  else if (c->rate_type == SSE2_QOS_RATE_PPS)
143  s = format (s, "pps");
144  else
145  s = format (s, "ILLEGAL");
146  return s;
147 }
148 
149 static u8 *
150 format_policer_type (u8 * s, va_list * va)
151 {
153 
155  s = format (s, "1r2c");
156 
158  s = format (s, "1r3c");
159 
161  s = format (s, "2r3c-2698");
162 
164  s = format (s, "2r3c-4115");
165 
167  s = format (s, "2r3c-mef5cf1");
168  else
169  s = format (s, "ILLEGAL");
170  return s;
171 }
172 
173 static u8 *
174 format_dscp (u8 * s, va_list * va)
175 {
176  u32 i = va_arg (*va, u32);
177  char *t = 0;
178 
179  switch (i)
180  {
181 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
183 #undef _
184  default:
185  return format (s, "ILLEGAL");
186  }
187  s = format (s, "%s", t);
188  return s;
189 }
190 
191 static u8 *
192 format_policer_action_type (u8 * s, va_list * va)
193 {
195  = va_arg (*va, sse2_qos_pol_action_params_st *);
196 
198  s = format (s, "drop");
199  else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
200  s = format (s, "transmit");
202  s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
203  else
204  s = format (s, "ILLEGAL");
205  return s;
206 }
207 
208 u8 *
209 format_policer_config (u8 * s, va_list * va)
210 {
212 
213  s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
215  c->rb.kbps.cir_kbps,
216  c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
217  s = format (s, "rate type %U, round type %U\n",
219  s = format (s, "conform action %U, exceed action %U, violate action %U\n",
223  return s;
224 }
225 
226 static uword
228 {
230 
231  if (!unformat (input, "type"))
232  return 0;
233 
234  if (unformat (input, "1r2c"))
236  else if (unformat (input, "1r3c"))
238  else if (unformat (input, "2r3c-2698"))
240  else if (unformat (input, "2r3c-4115"))
242  else if (unformat (input, "2r3c-mef5cf1"))
244  else
245  return 0;
246  return 1;
247 }
248 
249 static uword
251 {
253 
254  if (!unformat (input, "round"))
255  return 0;
256 
257  if (unformat (input, "closest"))
259  else if (unformat (input, "up"))
261  else if (unformat (input, "down"))
263  else
264  return 0;
265  return 1;
266 }
267 
268 static uword
270 {
272 
273  if (!unformat (input, "rate"))
274  return 0;
275 
276  if (unformat (input, "kbps"))
278  else if (unformat (input, "pps"))
280  else
281  return 0;
282  return 1;
283 }
284 
285 static uword
286 unformat_policer_cir (unformat_input_t * input, va_list * va)
287 {
289 
290  if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
291  return 1;
292  return 0;
293 }
294 
295 static uword
296 unformat_policer_eir (unformat_input_t * input, va_list * va)
297 {
299 
300  if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
301  return 1;
302  return 0;
303 }
304 
305 static uword
306 unformat_policer_cb (unformat_input_t * input, va_list * va)
307 {
309 
310  if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
311  return 1;
312  return 0;
313 }
314 
315 static uword
316 unformat_policer_eb (unformat_input_t * input, va_list * va)
317 {
319 
320  if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
321  return 1;
322  return 0;
323 }
324 
325 static uword
326 unformat_dscp (unformat_input_t * input, va_list * va)
327 {
328  u8 *r = va_arg (*va, u8 *);
329 
330  if (0);
331 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
333 #undef _
334  else
335  return 0;
336  return 1;
337 }
338 
339 static uword
341 {
343  = va_arg (*va, sse2_qos_pol_action_params_st *);
344 
345  if (unformat (input, "drop"))
347  else if (unformat (input, "transmit"))
349  else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
351  else
352  return 0;
353  return 1;
354 }
355 
356 static uword
358 {
360 
361  if (unformat (input, "conform-action %U", unformat_policer_action_type,
362  &c->conform_action))
363  return 1;
364  else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
365  &c->exceed_action))
366  return 1;
367  else if (unformat (input, "violate-action %U", unformat_policer_action_type,
368  &c->violate_action))
369  return 1;
370  return 0;
371 }
372 
373 static uword
375 {
376  u32 *r = va_arg (*va, u32 *);
378  uword *p;
379  u8 *match_name = 0;
380 
381  if (unformat (input, "%s", &match_name))
382  ;
383  else
384  return 0;
385 
386  p = hash_get_mem (pm->policer_index_by_name, match_name);
387 
388  if (p == 0)
389  return 0;
390 
391  *r = p[0];
392 
393  return 1;
394 }
395 
396 static uword
398 {
399  u32 *r = va_arg (*va, u32 *);
400 
401  if (unformat (input, "conform-color"))
402  *r = POLICE_CONFORM;
403  else if (unformat (input, "exceed-color"))
404  *r = POLICE_EXCEED;
405  else
406  return 0;
407 
408  return 1;
409 }
410 
411 #define foreach_config_param \
412 _(eb) \
413 _(cb) \
414 _(eir) \
415 _(cir) \
416 _(rate_type) \
417 _(round_type) \
418 _(type) \
419 _(action)
420 
421 static clib_error_t *
423  unformat_input_t * input,
424  vlib_cli_command_t * cmd)
425 {
427  unformat_input_t _line_input, *line_input = &_line_input;
428  u8 is_add = 1;
429  u8 *name = 0;
430  u32 pi;
431  clib_error_t *error = NULL;
432 
433  /* Get a line of input. */
434  if (!unformat_user (input, unformat_line_input, line_input))
435  return 0;
436 
437  memset (&c, 0, sizeof (c));
438 
439  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
440  {
441  if (unformat (line_input, "del"))
442  is_add = 0;
443  else if (unformat (line_input, "name %s", &name))
444  ;
445  else if (unformat (line_input, "color-aware"))
446  c.color_aware = 1;
447 
448 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
450 #undef _
451  else
452  {
453  error = clib_error_return (0, "unknown input `%U'",
454  format_unformat_error, line_input);
455  goto done;
456  }
457  }
458 
459  error = policer_add_del (vm, name, &c, &pi, is_add);
460 
461 done:
462  unformat_free (line_input);
463 
464  return error;
465 }
466 
467 /* *INDENT-OFF* */
468 VLIB_CLI_COMMAND (configure_policer_command, static) = {
469  .path = "configure policer",
470  .short_help = "configure policer name <name> <params> ",
471  .function = configure_policer_command_fn,
472 };
473 /* *INDENT-ON* */
474 
475 static clib_error_t *
477  unformat_input_t * input, vlib_cli_command_t * cmd)
478 {
480  hash_pair_t *p;
481  u32 pool_index;
482  u8 *match_name = 0;
483  u8 *name;
486 
487  (void) unformat (input, "name %s", &match_name);
488 
489  /* *INDENT-OFF* */
491  ({
492  name = (u8 *) p->key;
493  if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
494  {
495  pool_index = p->value[0];
496  config = pool_elt_at_index (pm->configs, pool_index);
497  templ = pool_elt_at_index (pm->policer_templates, pool_index);
498  vlib_cli_output (vm, "Name \"%s\" %U ",
499  name, format_policer_config, config);
500  vlib_cli_output (vm, "Template %U",
501  format_policer_instance, templ);
502  vlib_cli_output (vm, "-----------");
503  }
504  }));
505  /* *INDENT-ON* */
506  return 0;
507 }
508 
509 
510 /* *INDENT-OFF* */
511 VLIB_CLI_COMMAND (show_policer_command, static) = {
512  .path = "show policer",
513  .short_help = "show policer [name]",
514  .function = show_policer_command_fn,
515 };
516 /* *INDENT-ON* */
517 
518 static clib_error_t *
520  unformat_input_t * input,
521  vlib_cli_command_t * cmd)
522 {
524 
525  vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
526  pool_elts (pm->configs),
528  pool_elts (pm->policers));
529  return 0;
530 }
531 /* *INDENT-OFF* */
532 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
533  .path = "show policer pools",
534  .short_help = "show policer pools",
535  .function = show_policer_pools_command_fn,
536 };
537 /* *INDENT-ON* */
538 
539 clib_error_t *
541 {
544 
546 
547  pm->vlib_main = vm;
548  pm->vnet_main = vnet_get_main ();
549 
550  pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
551  pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
552 
557 
558  return 0;
559 }
560 
562 
563 
564 
565 /*
566  * fd.io coding-style-patch-verification: ON
567  *
568  * Local Variables:
569  * eval: (c-set-style "gnu")
570  * End:
571  */
uword * policer_index_by_name
Definition: policer.h:37
static uword unformat_policer_eir(unformat_input_t *input, va_list *va)
Definition: policer.c:296
sse2_qos_pol_cfg_params_st * configs
Definition: policer.h:30
static uword unformat_policer_action(unformat_input_t *input, va_list *va)
Definition: policer.c:357
#define foreach_config_param
Definition: policer.c:411
a
Definition: bitmap.h:538
sse2_qos_pol_action_params_st conform_action
Definition: xlate.h:151
static u8 * format_dscp(u8 *s, va_list *va)
Definition: policer.c:174
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
uword * policer_config_by_name
Definition: policer.h:34
#define NULL
Definition: clib.h:57
int i
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
#define hash_set_mem(h, key, value)
Definition: hash.h:275
static uword unformat_policer_rate_type(unformat_input_t *input, va_list *va)
Definition: policer.c:269
vnet_main_t * vnet_main
Definition: policer.h:44
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static uword unformat_policer_type(unformat_input_t *input, va_list *va)
Definition: policer.c:227
u8 * format_policer_instance(u8 *s, va_list *va)
Definition: policer.c:101
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:228
static u8 * format_policer_round_type(u8 *s, va_list *va)
Definition: policer.c:119
unsigned char u8
Definition: types.h:56
static uword unformat_policer_action_type(unformat_input_t *input, va_list *va)
Definition: policer.c:340
vnet_policer_main_t vnet_policer_main
Definition: policer.c:19
memset(h->entries, 0, sizeof(h->entries[0])*entries)
static clib_error_t * show_policer_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: policer.c:476
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
struct sse2_qos_pol_cfg_params_st_::@291::@292 kbps
static u8 * format_policer_rate_type(u8 *s, va_list *va)
Definition: policer.c:136
#define clib_error_return(e, args...)
Definition: error.h:99
static uword unformat_policer_round_type(unformat_input_t *input, va_list *va)
Definition: policer.c:250
unsigned int u32
Definition: types.h:88
static uword unformat_policer_cir(unformat_input_t *input, va_list *va)
Definition: policer.c:286
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
unformat_function_t unformat_line_input
Definition: format.h:282
#define hash_unset_mem(h, key)
Definition: hash.h:291
clib_error_t * policer_init(vlib_main_t *vm)
Definition: policer.c:540
struct _unformat_input_t unformat_input_t
sse2_qos_pol_action_params_st violate_action
Definition: xlate.h:153
void vnet_classify_register_unformat_opaque_index_fn(unformat_function_t *fn)
policer_read_response_type_st * policers
Definition: policer.h:27
u8 name[64]
Definition: memclnt.api:151
void vnet_policer_node_funcs_reference(void)
Definition: node_funcs.c:298
sse2_qos_pol_action_params_st exceed_action
Definition: xlate.h:152
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:188
vlib_main_t * vlib_main
Definition: policer.h:43
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
svmdb_client_t * c
vlib_main_t * vm
Definition: buffer.c:294
static uword unformat_policer_classify_next_index(unformat_input_t *input, va_list *va)
Definition: policer.c:374
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
static clib_error_t * configure_policer_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: policer.c:422
#define clib_memcpy(a, b, c)
Definition: string.h:75
int sse2_pol_logical_2_physical(sse2_qos_pol_cfg_params_st *cfg, policer_read_response_type_st *phys)
Definition: xlate.c:1171
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:299
#define ASSERT(truth)
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 uword unformat_dscp(unformat_input_t *input, va_list *va)
Definition: policer.c:326
static u8 * format_policer_type(u8 *s, va_list *va)
Definition: policer.c:150
u8 * format_policer_config(u8 *s, va_list *va)
Definition: policer.c:209
static clib_error_t * show_policer_pools_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: policer.c:519
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:373
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
static u8 * format_policer_action_type(u8 *s, va_list *va)
Definition: policer.c:192
union sse2_qos_pol_cfg_params_st_::@291 rb
#define hash_get_mem(h, key)
Definition: hash.h:269
static uword unformat_policer_cb(unformat_input_t *input, va_list *va)
Definition: policer.c:306
static uword unformat_policer_classify_precolor(unformat_input_t *input, va_list *va)
Definition: policer.c:397
policer_read_response_type_st * policer_templates
Definition: policer.h:31
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
void vnet_classify_register_unformat_policer_next_index_fn(unformat_function_t *fn)
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:725
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_policer_eb(unformat_input_t *input, va_list *va)
Definition: policer.c:316
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128