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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/policer/policer.h>
17 #include <vnet/classify/vnet_classify.h>
20 policer_add_del (vlib_main_t * vm,
22 sse2_qos_pol_cfg_params_st * cfg,
23 u32 * policer_index, u8 is_add)
25 vnet_policer_main_t *pm = &vnet_policer_main;
26 policer_read_response_type_st test_policer;
27 policer_read_response_type_st *policer;
32 p = hash_get_mem (pm->policer_config_by_name, name);
36 /* free policer config and template */
40 return clib_error_return (0, "No such policer configuration");
42 pool_put_index (pm->configs, p[0]);
43 pool_put_index (pm->policer_templates, p[0]);
44 hash_unset_mem (pm->policer_config_by_name, name);
47 p = hash_get_mem (pm->policer_index_by_name, name);
51 return clib_error_return (0, "No such policer");
53 pool_put_index (pm->policers, p[0]);
54 hash_unset_mem (pm->policer_index_by_name, name);
63 return clib_error_return (0, "Policer already exists");
66 /* Vet the configuration before adding it to the table */
67 rv = sse2_pol_logical_2_physical (cfg, &test_policer);
71 policer_read_response_type_st *pp;
72 sse2_qos_pol_cfg_params_st *cp;
74 pool_get (pm->configs, cp);
75 pool_get (pm->policer_templates, pp);
77 ASSERT (cp - pm->configs == pp - pm->policer_templates);
79 clib_memcpy (cp, cfg, sizeof (*cp));
80 clib_memcpy (pp, &test_policer, sizeof (*pp));
82 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
83 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
85 pi = policer - pm->policers;
86 hash_set_mem (pm->policer_index_by_name, name, pi);
92 return clib_error_return (0, "Config failed sanity check");
99 format_policer_instance (u8 * s, va_list * va)
101 policer_read_response_type_st *i
102 = va_arg (*va, policer_read_response_type_st *);
104 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
105 i, i->single_rate ? "single" : "dual",
106 i->color_aware ? "is" : "not");
107 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
108 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
109 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
111 i->current_bucket, i->extended_limit, i->extended_bucket);
112 s = format (s, "last update %llu\n", i->last_update_time);
117 format_policer_round_type (u8 * s, va_list * va)
119 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
121 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
122 s = format (s, "closest");
123 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
124 s = format (s, "up");
125 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
126 s = format (s, "down");
128 s = format (s, "ILLEGAL");
134 format_policer_rate_type (u8 * s, va_list * va)
136 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
138 if (c->rate_type == SSE2_QOS_RATE_KBPS)
139 s = format (s, "kbps");
140 else if (c->rate_type == SSE2_QOS_RATE_PPS)
141 s = format (s, "pps");
143 s = format (s, "ILLEGAL");
148 format_policer_type (u8 * s, va_list * va)
150 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
152 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
153 s = format (s, "1r2c");
155 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
156 s = format (s, "1r3c");
158 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
159 s = format (s, "2r3c-2698");
161 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
162 s = format (s, "2r3c-4115");
164 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
165 s = format (s, "2r3c-mef5cf1");
167 s = format (s, "ILLEGAL");
172 format_dscp (u8 * s, va_list * va)
174 u32 i = va_arg (*va, u32);
179 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
183 return format (s, "ILLEGAL");
185 s = format (s, "%s", t);
190 format_policer_action_type (u8 * s, va_list * va)
192 sse2_qos_pol_action_params_st *a
193 = va_arg (*va, sse2_qos_pol_action_params_st *);
195 if (a->action_type == SSE2_QOS_ACTION_DROP)
196 s = format (s, "drop");
197 else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
198 s = format (s, "transmit");
199 else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
200 s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
202 s = format (s, "ILLEGAL");
207 format_policer_config (u8 * s, va_list * va)
209 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
211 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
212 format_policer_type, c,
214 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
215 s = format (s, "rate type %U, round type %U\n",
216 format_policer_rate_type, c, format_policer_round_type, c);
217 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
218 format_policer_action_type, &c->conform_action,
219 format_policer_action_type, &c->exceed_action,
220 format_policer_action_type, &c->violate_action);
225 unformat_policer_type (unformat_input_t * input, va_list * va)
227 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
229 if (!unformat (input, "type"))
232 if (unformat (input, "1r2c"))
233 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
234 else if (unformat (input, "1r3c"))
235 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
236 else if (unformat (input, "2r3c-2698"))
237 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
238 else if (unformat (input, "2r3c-4115"))
239 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
240 else if (unformat (input, "2r3c-mef5cf1"))
241 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
248 unformat_policer_round_type (unformat_input_t * input, va_list * va)
250 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
252 if (!unformat (input, "round"))
255 if (unformat (input, "closest"))
256 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
257 else if (unformat (input, "up"))
258 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
259 else if (unformat (input, "down"))
260 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
267 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
269 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
271 if (!unformat (input, "rate"))
274 if (unformat (input, "kbps"))
275 c->rate_type = SSE2_QOS_RATE_KBPS;
276 else if (unformat (input, "pps"))
277 c->rate_type = SSE2_QOS_RATE_PPS;
284 unformat_policer_cir (unformat_input_t * input, va_list * va)
286 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
288 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
294 unformat_policer_eir (unformat_input_t * input, va_list * va)
296 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
298 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
304 unformat_policer_cb (unformat_input_t * input, va_list * va)
306 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
308 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
314 unformat_policer_eb (unformat_input_t * input, va_list * va)
316 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
318 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
324 unformat_dscp (unformat_input_t * input, va_list * va)
326 u8 *r = va_arg (*va, u8 *);
329 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
338 unformat_policer_action_type (unformat_input_t * input, va_list * va)
340 sse2_qos_pol_action_params_st *a
341 = va_arg (*va, sse2_qos_pol_action_params_st *);
343 if (unformat (input, "drop"))
344 a->action_type = SSE2_QOS_ACTION_DROP;
345 else if (unformat (input, "transmit"))
346 a->action_type = SSE2_QOS_ACTION_TRANSMIT;
347 else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
348 a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
355 unformat_policer_action (unformat_input_t * input, va_list * va)
357 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
359 if (unformat (input, "conform-action %U", unformat_policer_action_type,
362 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
365 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
372 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
374 u32 *r = va_arg (*va, u32 *);
375 vnet_policer_main_t *pm = &vnet_policer_main;
379 if (unformat (input, "%s", &match_name))
384 p = hash_get_mem (pm->policer_index_by_name, match_name);
395 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
397 u32 *r = va_arg (*va, u32 *);
399 if (unformat (input, "conform-color"))
401 else if (unformat (input, "exceed-color"))
409 #define foreach_config_param \
419 static clib_error_t *
420 configure_policer_command_fn (vlib_main_t * vm,
421 unformat_input_t * input,
422 vlib_cli_command_t * cmd)
424 sse2_qos_pol_cfg_params_st c;
425 unformat_input_t _line_input, *line_input = &_line_input;
429 clib_error_t *error = NULL;
431 /* Get a line of input. */
432 if (!unformat_user (input, unformat_line_input, line_input))
435 memset (&c, 0, sizeof (c));
437 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
439 if (unformat (line_input, "del"))
441 else if (unformat (line_input, "name %s", &name))
443 else if (unformat (line_input, "color-aware"))
446 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
451 error = clib_error_return (0, "unknown input `%U'",
452 format_unformat_error, line_input);
457 error = policer_add_del (vm, name, &c, &pi, is_add);
460 unformat_free (line_input);
466 VLIB_CLI_COMMAND (configure_policer_command, static) = {
467 .path = "configure policer",
468 .short_help = "configure policer name <name> <params> ",
469 .function = configure_policer_command_fn,
473 static clib_error_t *
474 show_policer_command_fn (vlib_main_t * vm,
475 unformat_input_t * input, vlib_cli_command_t * cmd)
477 vnet_policer_main_t *pm = &vnet_policer_main;
482 sse2_qos_pol_cfg_params_st *config;
483 policer_read_response_type_st *templ;
485 (void) unformat (input, "name %s", &match_name);
488 hash_foreach_pair (p, pm->policer_config_by_name,
490 name = (u8 *) p->key;
491 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
493 pool_index = p->value[0];
494 config = pool_elt_at_index (pm->configs, pool_index);
495 templ = pool_elt_at_index (pm->policer_templates, pool_index);
496 vlib_cli_output (vm, "Name \"%s\" %U ",
497 name, format_policer_config, config);
498 vlib_cli_output (vm, "Template %U",
499 format_policer_instance, templ);
500 vlib_cli_output (vm, "-----------");
509 VLIB_CLI_COMMAND (show_policer_command, static) = {
510 .path = "show policer",
511 .short_help = "show policer [name]",
512 .function = show_policer_command_fn,
516 static clib_error_t *
517 show_policer_pools_command_fn (vlib_main_t * vm,
518 unformat_input_t * input,
519 vlib_cli_command_t * cmd)
521 vnet_policer_main_t *pm = &vnet_policer_main;
523 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
524 pool_elts (pm->configs),
525 pool_elts (pm->policer_templates),
526 pool_elts (pm->policers));
530 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
531 .path = "show policer pools",
532 .short_help = "show policer pools",
533 .function = show_policer_pools_command_fn,
538 policer_init (vlib_main_t * vm)
540 vnet_policer_main_t *pm = &vnet_policer_main;
541 void vnet_policer_node_funcs_reference (void);
543 vnet_policer_node_funcs_reference ();
546 pm->vnet_main = vnet_get_main ();
548 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
549 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
551 vnet_classify_register_unformat_policer_next_index_fn
552 (unformat_policer_classify_next_index);
553 vnet_classify_register_unformat_opaque_index_fn
554 (unformat_policer_classify_precolor);
559 VLIB_INIT_FUNCTION (policer_init);
564 * fd.io coding-style-patch-verification: ON
567 * eval: (c-set-style "gnu")