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/policer/police_inlines.h>
18 #include <vnet/classify/vnet_classify.h>
20 vnet_policer_main_t vnet_policer_main;
23 format_policer_handoff_trace (u8 *s, va_list *args)
25 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
26 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
27 policer_handoff_trace_t *t = va_arg (*args, policer_handoff_trace_t *);
29 s = format (s, "policer %d, handoff thread %d to %d", t->policer_index,
30 t->current_worker_index, t->next_worker_index);
36 policer_add_del (vlib_main_t * vm,
38 sse2_qos_pol_cfg_params_st * cfg,
39 u32 * policer_index, u8 is_add)
41 vnet_policer_main_t *pm = &vnet_policer_main;
42 policer_read_response_type_st test_policer;
43 policer_read_response_type_st *policer;
48 p = hash_get_mem (pm->policer_config_by_name, name);
52 /* free policer config and template */
56 return clib_error_return (0, "No such policer configuration");
58 pool_put_index (pm->configs, p[0]);
59 pool_put_index (pm->policer_templates, p[0]);
60 hash_unset_mem (pm->policer_config_by_name, name);
63 p = hash_get_mem (pm->policer_index_by_name, name);
67 return clib_error_return (0, "No such policer");
69 pool_put_index (pm->policers, p[0]);
70 hash_unset_mem (pm->policer_index_by_name, name);
79 return clib_error_return (0, "Policer already exists");
82 /* Vet the configuration before adding it to the table */
83 rv = sse2_pol_logical_2_physical (cfg, &test_policer);
87 policer_read_response_type_st *pp;
88 sse2_qos_pol_cfg_params_st *cp;
90 pool_get (pm->configs, cp);
91 pool_get (pm->policer_templates, pp);
93 ASSERT (cp - pm->configs == pp - pm->policer_templates);
95 clib_memcpy (cp, cfg, sizeof (*cp));
96 clib_memcpy (pp, &test_policer, sizeof (*pp));
98 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
99 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
101 pi = policer - pm->policers;
102 hash_set_mem (pm->policer_index_by_name, name, pi);
104 policer->thread_index = ~0;
109 return clib_error_return (0, "Config failed sanity check");
116 format_policer_instance (u8 * s, va_list * va)
118 policer_read_response_type_st *i
119 = va_arg (*va, policer_read_response_type_st *);
121 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
122 i, i->single_rate ? "single" : "dual",
123 i->color_aware ? "is" : "not");
124 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
125 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
126 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
128 i->current_bucket, i->extended_limit, i->extended_bucket);
129 s = format (s, "last update %llu\n", i->last_update_time);
134 format_policer_round_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->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
139 s = format (s, "closest");
140 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
141 s = format (s, "up");
142 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
143 s = format (s, "down");
145 s = format (s, "ILLEGAL");
151 format_policer_rate_type (u8 * s, va_list * va)
153 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
155 if (c->rate_type == SSE2_QOS_RATE_KBPS)
156 s = format (s, "kbps");
157 else if (c->rate_type == SSE2_QOS_RATE_PPS)
158 s = format (s, "pps");
160 s = format (s, "ILLEGAL");
165 format_policer_type (u8 * s, va_list * va)
167 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
169 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
170 s = format (s, "1r2c");
172 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
173 s = format (s, "1r3c");
175 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
176 s = format (s, "2r3c-2698");
178 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
179 s = format (s, "2r3c-4115");
181 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
182 s = format (s, "2r3c-mef5cf1");
184 s = format (s, "ILLEGAL");
189 format_dscp (u8 * s, va_list * va)
191 u32 i = va_arg (*va, u32);
196 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
200 return format (s, "ILLEGAL");
202 s = format (s, "%s", t);
207 format_policer_action_type (u8 * s, va_list * va)
209 sse2_qos_pol_action_params_st *a
210 = va_arg (*va, sse2_qos_pol_action_params_st *);
212 if (a->action_type == SSE2_QOS_ACTION_DROP)
213 s = format (s, "drop");
214 else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
215 s = format (s, "transmit");
216 else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
217 s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
219 s = format (s, "ILLEGAL");
224 format_policer_config (u8 * s, va_list * va)
226 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
228 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
229 format_policer_type, c,
231 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
232 s = format (s, "rate type %U, round type %U\n",
233 format_policer_rate_type, c, format_policer_round_type, c);
234 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
235 format_policer_action_type, &c->conform_action,
236 format_policer_action_type, &c->exceed_action,
237 format_policer_action_type, &c->violate_action);
242 unformat_policer_type (unformat_input_t * input, va_list * va)
244 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
246 if (!unformat (input, "type"))
249 if (unformat (input, "1r2c"))
250 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
251 else if (unformat (input, "1r3c"))
252 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
253 else if (unformat (input, "2r3c-2698"))
254 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
255 else if (unformat (input, "2r3c-4115"))
256 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
257 else if (unformat (input, "2r3c-mef5cf1"))
258 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
265 unformat_policer_round_type (unformat_input_t * input, va_list * va)
267 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
269 if (!unformat (input, "round"))
272 if (unformat (input, "closest"))
273 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
274 else if (unformat (input, "up"))
275 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
276 else if (unformat (input, "down"))
277 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
284 unformat_policer_rate_type (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, "rate"))
291 if (unformat (input, "kbps"))
292 c->rate_type = SSE2_QOS_RATE_KBPS;
293 else if (unformat (input, "pps"))
294 c->rate_type = SSE2_QOS_RATE_PPS;
301 unformat_policer_cir (unformat_input_t * input, va_list * va)
303 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
305 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
311 unformat_policer_eir (unformat_input_t * input, va_list * va)
313 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
315 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
321 unformat_policer_cb (unformat_input_t * input, va_list * va)
323 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
325 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
331 unformat_policer_eb (unformat_input_t * input, va_list * va)
333 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
335 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
341 unformat_dscp (unformat_input_t * input, va_list * va)
343 u8 *r = va_arg (*va, u8 *);
346 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
355 unformat_policer_action_type (unformat_input_t * input, va_list * va)
357 sse2_qos_pol_action_params_st *a
358 = va_arg (*va, sse2_qos_pol_action_params_st *);
360 if (unformat (input, "drop"))
361 a->action_type = SSE2_QOS_ACTION_DROP;
362 else if (unformat (input, "transmit"))
363 a->action_type = SSE2_QOS_ACTION_TRANSMIT;
364 else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
365 a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
372 unformat_policer_action (unformat_input_t * input, va_list * va)
374 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
376 if (unformat (input, "conform-action %U", unformat_policer_action_type,
379 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
382 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
389 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
391 u32 *r = va_arg (*va, u32 *);
392 vnet_policer_main_t *pm = &vnet_policer_main;
396 if (unformat (input, "%s", &match_name))
401 p = hash_get_mem (pm->policer_index_by_name, match_name);
412 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
414 u32 *r = va_arg (*va, u32 *);
416 if (unformat (input, "conform-color"))
418 else if (unformat (input, "exceed-color"))
426 #define foreach_config_param \
436 static clib_error_t *
437 configure_policer_command_fn (vlib_main_t * vm,
438 unformat_input_t * input,
439 vlib_cli_command_t * cmd)
441 sse2_qos_pol_cfg_params_st c;
442 unformat_input_t _line_input, *line_input = &_line_input;
446 clib_error_t *error = NULL;
448 /* Get a line of input. */
449 if (!unformat_user (input, unformat_line_input, line_input))
452 clib_memset (&c, 0, sizeof (c));
454 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
456 if (unformat (line_input, "del"))
458 else if (unformat (line_input, "name %s", &name))
460 else if (unformat (line_input, "color-aware"))
463 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
468 error = clib_error_return (0, "unknown input `%U'",
469 format_unformat_error, line_input);
474 error = policer_add_del (vm, name, &c, &pi, is_add);
477 unformat_free (line_input);
483 VLIB_CLI_COMMAND (configure_policer_command, static) = {
484 .path = "configure policer",
485 .short_help = "configure policer name <name> <params> ",
486 .function = configure_policer_command_fn,
490 static clib_error_t *
491 show_policer_command_fn (vlib_main_t * vm,
492 unformat_input_t * input, vlib_cli_command_t * cmd)
494 vnet_policer_main_t *pm = &vnet_policer_main;
499 sse2_qos_pol_cfg_params_st *config;
500 policer_read_response_type_st *templ;
502 (void) unformat (input, "name %s", &match_name);
505 hash_foreach_pair (p, pm->policer_config_by_name,
507 name = (u8 *) p->key;
508 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
510 pool_index = p->value[0];
511 config = pool_elt_at_index (pm->configs, pool_index);
512 templ = pool_elt_at_index (pm->policer_templates, pool_index);
513 vlib_cli_output (vm, "Name \"%s\" %U ",
514 name, format_policer_config, config);
515 vlib_cli_output (vm, "Template %U",
516 format_policer_instance, templ);
517 vlib_cli_output (vm, "-----------");
526 VLIB_CLI_COMMAND (show_policer_command, static) = {
527 .path = "show policer",
528 .short_help = "show policer [name]",
529 .function = show_policer_command_fn,
533 static clib_error_t *
534 show_policer_pools_command_fn (vlib_main_t * vm,
535 unformat_input_t * input,
536 vlib_cli_command_t * cmd)
538 vnet_policer_main_t *pm = &vnet_policer_main;
540 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
541 pool_elts (pm->configs),
542 pool_elts (pm->policer_templates),
543 pool_elts (pm->policers));
547 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
548 .path = "show policer pools",
549 .short_help = "show policer pools",
550 .function = show_policer_pools_command_fn,
555 policer_init (vlib_main_t * vm)
557 vnet_policer_main_t *pm = &vnet_policer_main;
558 void vnet_policer_node_funcs_reference (void);
560 vnet_policer_node_funcs_reference ();
563 pm->vnet_main = vnet_get_main ();
565 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
566 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
568 vnet_classify_register_unformat_policer_next_index_fn
569 (unformat_policer_classify_next_index);
570 vnet_classify_register_unformat_opaque_index_fn
571 (unformat_policer_classify_precolor);
576 VLIB_INIT_FUNCTION (policer_init);
581 * fd.io coding-style-patch-verification: ON
584 * eval: (c-set-style "gnu")