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);
35 vlib_combined_counter_main_t policer_counters[] = {
37 .name = "Policer-Conform",
38 .stat_segment_name = "/net/policer/conform",
41 .name = "Policer-Exceed",
42 .stat_segment_name = "/net/policer/exceed",
45 .name = "Policer-Violate",
46 .stat_segment_name = "/net/policer/violate",
51 policer_add_del (vlib_main_t * vm,
53 sse2_qos_pol_cfg_params_st * cfg,
54 u32 * policer_index, u8 is_add)
56 vnet_policer_main_t *pm = &vnet_policer_main;
57 policer_read_response_type_st test_policer;
58 policer_read_response_type_st *policer;
63 p = hash_get_mem (pm->policer_config_by_name, name);
67 /* free policer config and template */
71 return clib_error_return (0, "No such policer configuration");
73 pool_put_index (pm->configs, p[0]);
74 pool_put_index (pm->policer_templates, p[0]);
75 hash_unset_mem (pm->policer_config_by_name, name);
78 p = hash_get_mem (pm->policer_index_by_name, name);
82 return clib_error_return (0, "No such policer");
84 pool_put_index (pm->policers, p[0]);
85 hash_unset_mem (pm->policer_index_by_name, name);
94 return clib_error_return (0, "Policer already exists");
97 /* Vet the configuration before adding it to the table */
98 rv = sse2_pol_logical_2_physical (cfg, &test_policer);
102 policer_read_response_type_st *pp;
103 sse2_qos_pol_cfg_params_st *cp;
106 pool_get (pm->configs, cp);
107 pool_get (pm->policer_templates, pp);
109 ASSERT (cp - pm->configs == pp - pm->policer_templates);
111 clib_memcpy (cp, cfg, sizeof (*cp));
112 clib_memcpy (pp, &test_policer, sizeof (*pp));
114 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
115 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
117 pi = policer - pm->policers;
118 hash_set_mem (pm->policer_index_by_name, name, pi);
120 policer->thread_index = ~0;
122 for (i = 0; i < NUM_POLICE_RESULTS; i++)
124 vlib_validate_combined_counter (&policer_counters[i], pi);
125 vlib_zero_combined_counter (&policer_counters[i], pi);
131 return clib_error_return (0, "Config failed sanity check");
138 format_policer_instance (u8 * s, va_list * va)
140 policer_read_response_type_st *i
141 = va_arg (*va, policer_read_response_type_st *);
142 uword pi = va_arg (*va, uword);
144 vlib_counter_t counts[NUM_POLICE_RESULTS];
146 for (result = 0; result < NUM_POLICE_RESULTS; result++)
148 vlib_get_combined_counter (&policer_counters[result], pi,
152 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
153 i, i->single_rate ? "single" : "dual",
154 i->color_aware ? "is" : "not");
155 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
156 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
157 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
159 i->current_bucket, i->extended_limit, i->extended_bucket);
160 s = format (s, "last update %llu\n", i->last_update_time);
161 s = format (s, "conform %llu packets, %llu bytes\n",
162 counts[POLICE_CONFORM].packets, counts[POLICE_CONFORM].bytes);
163 s = format (s, "exceed %llu packets, %llu bytes\n",
164 counts[POLICE_EXCEED].packets, counts[POLICE_EXCEED].bytes);
165 s = format (s, "violate %llu packets, %llu bytes\n",
166 counts[POLICE_VIOLATE].packets, counts[POLICE_VIOLATE].bytes);
171 format_policer_round_type (u8 * s, va_list * va)
173 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
175 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
176 s = format (s, "closest");
177 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
178 s = format (s, "up");
179 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
180 s = format (s, "down");
182 s = format (s, "ILLEGAL");
188 format_policer_rate_type (u8 * s, va_list * va)
190 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
192 if (c->rate_type == SSE2_QOS_RATE_KBPS)
193 s = format (s, "kbps");
194 else if (c->rate_type == SSE2_QOS_RATE_PPS)
195 s = format (s, "pps");
197 s = format (s, "ILLEGAL");
202 format_policer_type (u8 * s, va_list * va)
204 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
206 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
207 s = format (s, "1r2c");
209 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
210 s = format (s, "1r3c");
212 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
213 s = format (s, "2r3c-2698");
215 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
216 s = format (s, "2r3c-4115");
218 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
219 s = format (s, "2r3c-mef5cf1");
221 s = format (s, "ILLEGAL");
226 format_dscp (u8 * s, va_list * va)
228 u32 i = va_arg (*va, u32);
233 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
237 return format (s, "ILLEGAL");
239 s = format (s, "%s", t);
244 format_policer_action_type (u8 * s, va_list * va)
246 sse2_qos_pol_action_params_st *a
247 = va_arg (*va, sse2_qos_pol_action_params_st *);
249 if (a->action_type == SSE2_QOS_ACTION_DROP)
250 s = format (s, "drop");
251 else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
252 s = format (s, "transmit");
253 else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
254 s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
256 s = format (s, "ILLEGAL");
261 format_policer_config (u8 * s, va_list * va)
263 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
265 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
266 format_policer_type, c,
268 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
269 s = format (s, "rate type %U, round type %U\n",
270 format_policer_rate_type, c, format_policer_round_type, c);
271 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
272 format_policer_action_type, &c->conform_action,
273 format_policer_action_type, &c->exceed_action,
274 format_policer_action_type, &c->violate_action);
279 unformat_policer_type (unformat_input_t * input, va_list * va)
281 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
283 if (!unformat (input, "type"))
286 if (unformat (input, "1r2c"))
287 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
288 else if (unformat (input, "1r3c"))
289 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
290 else if (unformat (input, "2r3c-2698"))
291 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
292 else if (unformat (input, "2r3c-4115"))
293 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
294 else if (unformat (input, "2r3c-mef5cf1"))
295 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
302 unformat_policer_round_type (unformat_input_t * input, va_list * va)
304 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
306 if (!unformat (input, "round"))
309 if (unformat (input, "closest"))
310 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
311 else if (unformat (input, "up"))
312 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
313 else if (unformat (input, "down"))
314 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
321 unformat_policer_rate_type (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, "rate"))
328 if (unformat (input, "kbps"))
329 c->rate_type = SSE2_QOS_RATE_KBPS;
330 else if (unformat (input, "pps"))
331 c->rate_type = SSE2_QOS_RATE_PPS;
338 unformat_policer_cir (unformat_input_t * input, va_list * va)
340 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
342 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
348 unformat_policer_eir (unformat_input_t * input, va_list * va)
350 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
352 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
358 unformat_policer_cb (unformat_input_t * input, va_list * va)
360 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
362 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
368 unformat_policer_eb (unformat_input_t * input, va_list * va)
370 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
372 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
378 unformat_dscp (unformat_input_t * input, va_list * va)
380 u8 *r = va_arg (*va, u8 *);
383 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
392 unformat_policer_action_type (unformat_input_t * input, va_list * va)
394 sse2_qos_pol_action_params_st *a
395 = va_arg (*va, sse2_qos_pol_action_params_st *);
397 if (unformat (input, "drop"))
398 a->action_type = SSE2_QOS_ACTION_DROP;
399 else if (unformat (input, "transmit"))
400 a->action_type = SSE2_QOS_ACTION_TRANSMIT;
401 else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
402 a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
409 unformat_policer_action (unformat_input_t * input, va_list * va)
411 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
413 if (unformat (input, "conform-action %U", unformat_policer_action_type,
416 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
419 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
426 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
428 u32 *r = va_arg (*va, u32 *);
429 vnet_policer_main_t *pm = &vnet_policer_main;
433 if (unformat (input, "%s", &match_name))
438 p = hash_get_mem (pm->policer_index_by_name, match_name);
449 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
451 u32 *r = va_arg (*va, u32 *);
453 if (unformat (input, "conform-color"))
455 else if (unformat (input, "exceed-color"))
463 #define foreach_config_param \
473 static clib_error_t *
474 configure_policer_command_fn (vlib_main_t * vm,
475 unformat_input_t * input,
476 vlib_cli_command_t * cmd)
478 sse2_qos_pol_cfg_params_st c;
479 unformat_input_t _line_input, *line_input = &_line_input;
483 clib_error_t *error = NULL;
485 /* Get a line of input. */
486 if (!unformat_user (input, unformat_line_input, line_input))
489 clib_memset (&c, 0, sizeof (c));
491 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
493 if (unformat (line_input, "del"))
495 else if (unformat (line_input, "name %s", &name))
497 else if (unformat (line_input, "color-aware"))
500 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
505 error = clib_error_return (0, "unknown input `%U'",
506 format_unformat_error, line_input);
511 error = policer_add_del (vm, name, &c, &pi, is_add);
514 unformat_free (line_input);
520 VLIB_CLI_COMMAND (configure_policer_command, static) = {
521 .path = "configure policer",
522 .short_help = "configure policer name <name> <params> ",
523 .function = configure_policer_command_fn,
527 static clib_error_t *
528 show_policer_command_fn (vlib_main_t * vm,
529 unformat_input_t * input, vlib_cli_command_t * cmd)
531 vnet_policer_main_t *pm = &vnet_policer_main;
537 sse2_qos_pol_cfg_params_st *config;
538 policer_read_response_type_st *templ;
540 (void) unformat (input, "name %s", &match_name);
543 hash_foreach_pair (p, pm->policer_config_by_name,
545 name = (u8 *) p->key;
546 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
548 pi = hash_get_mem (pm->policer_index_by_name, name);
550 pool_index = p->value[0];
551 config = pool_elt_at_index (pm->configs, pool_index);
552 templ = pool_elt_at_index (pm->policer_templates, pool_index);
553 vlib_cli_output (vm, "Name \"%s\" %U ", name, format_policer_config,
555 vlib_cli_output (vm, "Template %U", format_policer_instance, templ,
557 vlib_cli_output (vm, "-----------");
566 VLIB_CLI_COMMAND (show_policer_command, static) = {
567 .path = "show policer",
568 .short_help = "show policer [name]",
569 .function = show_policer_command_fn,
573 static clib_error_t *
574 show_policer_pools_command_fn (vlib_main_t * vm,
575 unformat_input_t * input,
576 vlib_cli_command_t * cmd)
578 vnet_policer_main_t *pm = &vnet_policer_main;
580 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
581 pool_elts (pm->configs),
582 pool_elts (pm->policer_templates),
583 pool_elts (pm->policers));
587 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
588 .path = "show policer pools",
589 .short_help = "show policer pools",
590 .function = show_policer_pools_command_fn,
595 policer_init (vlib_main_t * vm)
597 vnet_policer_main_t *pm = &vnet_policer_main;
598 void vnet_policer_node_funcs_reference (void);
600 vnet_policer_node_funcs_reference ();
603 pm->vnet_main = vnet_get_main ();
605 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
606 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
608 vnet_classify_register_unformat_policer_next_index_fn
609 (unformat_policer_classify_next_index);
610 vnet_classify_register_unformat_opaque_index_fn
611 (unformat_policer_classify_precolor);
616 VLIB_INIT_FUNCTION (policer_init);
621 * fd.io coding-style-patch-verification: ON
624 * eval: (c-set-style "gnu")