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>
19 #include <vnet/ip/ip_packet.h>
21 vnet_policer_main_t vnet_policer_main;
24 format_policer_handoff_trace (u8 *s, va_list *args)
26 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
27 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
28 policer_handoff_trace_t *t = va_arg (*args, policer_handoff_trace_t *);
30 s = format (s, "policer %d, handoff thread %d to %d", t->policer_index,
31 t->current_worker_index, t->next_worker_index);
36 vlib_combined_counter_main_t policer_counters[] = {
38 .name = "Policer-Conform",
39 .stat_segment_name = "/net/policer/conform",
42 .name = "Policer-Exceed",
43 .stat_segment_name = "/net/policer/exceed",
46 .name = "Policer-Violate",
47 .stat_segment_name = "/net/policer/violate",
52 policer_add_del (vlib_main_t *vm, u8 *name, qos_pol_cfg_params_st *cfg,
53 u32 *policer_index, u8 is_add)
55 vnet_policer_main_t *pm = &vnet_policer_main;
56 policer_read_response_type_st test_policer;
57 policer_read_response_type_st *policer;
62 p = hash_get_mem (pm->policer_config_by_name, name);
66 /* free policer config and template */
70 return clib_error_return (0, "No such policer configuration");
72 pool_put_index (pm->configs, p[0]);
73 pool_put_index (pm->policer_templates, p[0]);
74 hash_unset_mem (pm->policer_config_by_name, name);
77 p = hash_get_mem (pm->policer_index_by_name, name);
81 return clib_error_return (0, "No such policer");
83 pool_put_index (pm->policers, p[0]);
84 hash_unset_mem (pm->policer_index_by_name, name);
93 return clib_error_return (0, "Policer already exists");
96 /* Vet the configuration before adding it to the table */
97 rv = pol_logical_2_physical (cfg, &test_policer);
101 policer_read_response_type_st *pp;
102 qos_pol_cfg_params_st *cp;
105 pool_get (pm->configs, cp);
106 pool_get (pm->policer_templates, pp);
108 ASSERT (cp - pm->configs == pp - pm->policer_templates);
110 clib_memcpy (cp, cfg, sizeof (*cp));
111 clib_memcpy (pp, &test_policer, sizeof (*pp));
113 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
114 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
116 pi = policer - pm->policers;
117 hash_set_mem (pm->policer_index_by_name, name, pi);
119 policer->thread_index = ~0;
121 for (i = 0; i < NUM_POLICE_RESULTS; i++)
123 vlib_validate_combined_counter (&policer_counters[i], pi);
124 vlib_zero_combined_counter (&policer_counters[i], pi);
130 return clib_error_return (0, "Config failed sanity check");
137 format_policer_instance (u8 * s, va_list * va)
139 policer_read_response_type_st *i
140 = va_arg (*va, policer_read_response_type_st *);
141 uword pi = va_arg (*va, uword);
143 vlib_counter_t counts[NUM_POLICE_RESULTS];
145 for (result = 0; result < NUM_POLICE_RESULTS; result++)
147 vlib_get_combined_counter (&policer_counters[result], pi,
151 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
152 i, i->single_rate ? "single" : "dual",
153 i->color_aware ? "is" : "not");
154 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
155 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
156 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
158 i->current_bucket, i->extended_limit, i->extended_bucket);
159 s = format (s, "last update %llu\n", i->last_update_time);
160 s = format (s, "conform %llu packets, %llu bytes\n",
161 counts[POLICE_CONFORM].packets, counts[POLICE_CONFORM].bytes);
162 s = format (s, "exceed %llu packets, %llu bytes\n",
163 counts[POLICE_EXCEED].packets, counts[POLICE_EXCEED].bytes);
164 s = format (s, "violate %llu packets, %llu bytes\n",
165 counts[POLICE_VIOLATE].packets, counts[POLICE_VIOLATE].bytes);
170 format_policer_round_type (u8 * s, va_list * va)
172 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
174 if (c->rnd_type == QOS_ROUND_TO_CLOSEST)
175 s = format (s, "closest");
176 else if (c->rnd_type == QOS_ROUND_TO_UP)
177 s = format (s, "up");
178 else if (c->rnd_type == QOS_ROUND_TO_DOWN)
179 s = format (s, "down");
181 s = format (s, "ILLEGAL");
187 format_policer_rate_type (u8 * s, va_list * va)
189 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
191 if (c->rate_type == QOS_RATE_KBPS)
192 s = format (s, "kbps");
193 else if (c->rate_type == QOS_RATE_PPS)
194 s = format (s, "pps");
196 s = format (s, "ILLEGAL");
201 format_policer_type (u8 * s, va_list * va)
203 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
205 if (c->rfc == QOS_POLICER_TYPE_1R2C)
206 s = format (s, "1r2c");
208 else if (c->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
209 s = format (s, "1r3c");
211 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698)
212 s = format (s, "2r3c-2698");
214 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
215 s = format (s, "2r3c-4115");
217 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
218 s = format (s, "2r3c-mef5cf1");
220 s = format (s, "ILLEGAL");
225 format_policer_action_type (u8 * s, va_list * va)
227 qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *);
229 if (a->action_type == QOS_ACTION_DROP)
230 s = format (s, "drop");
231 else if (a->action_type == QOS_ACTION_TRANSMIT)
232 s = format (s, "transmit");
233 else if (a->action_type == QOS_ACTION_MARK_AND_TRANSMIT)
234 s = format (s, "mark-and-transmit %U", format_ip_dscp, a->dscp);
236 s = format (s, "ILLEGAL");
241 format_policer_config (u8 * s, va_list * va)
243 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
245 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
246 format_policer_type, c,
248 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
249 s = format (s, "rate type %U, round type %U\n",
250 format_policer_rate_type, c, format_policer_round_type, c);
251 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
252 format_policer_action_type, &c->conform_action,
253 format_policer_action_type, &c->exceed_action,
254 format_policer_action_type, &c->violate_action);
259 unformat_policer_type (unformat_input_t * input, va_list * va)
261 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
263 if (!unformat (input, "type"))
266 if (unformat (input, "1r2c"))
267 c->rfc = QOS_POLICER_TYPE_1R2C;
268 else if (unformat (input, "1r3c"))
269 c->rfc = QOS_POLICER_TYPE_1R3C_RFC_2697;
270 else if (unformat (input, "2r3c-2698"))
271 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_2698;
272 else if (unformat (input, "2r3c-4115"))
273 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_4115;
274 else if (unformat (input, "2r3c-mef5cf1"))
275 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
282 unformat_policer_round_type (unformat_input_t * input, va_list * va)
284 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
286 if (!unformat (input, "round"))
289 if (unformat (input, "closest"))
290 c->rnd_type = QOS_ROUND_TO_CLOSEST;
291 else if (unformat (input, "up"))
292 c->rnd_type = QOS_ROUND_TO_UP;
293 else if (unformat (input, "down"))
294 c->rnd_type = QOS_ROUND_TO_DOWN;
301 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
303 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
305 if (!unformat (input, "rate"))
308 if (unformat (input, "kbps"))
309 c->rate_type = QOS_RATE_KBPS;
310 else if (unformat (input, "pps"))
311 c->rate_type = QOS_RATE_PPS;
318 unformat_policer_cir (unformat_input_t * input, va_list * va)
320 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
322 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
328 unformat_policer_eir (unformat_input_t * input, va_list * va)
330 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
332 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
338 unformat_policer_cb (unformat_input_t * input, va_list * va)
340 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
342 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
348 unformat_policer_eb (unformat_input_t * input, va_list * va)
350 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
352 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
358 unformat_policer_action_type (unformat_input_t * input, va_list * va)
360 qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *);
362 if (unformat (input, "drop"))
363 a->action_type = QOS_ACTION_DROP;
364 else if (unformat (input, "transmit"))
365 a->action_type = QOS_ACTION_TRANSMIT;
366 else if (unformat (input, "mark-and-transmit %U", unformat_ip_dscp,
368 a->action_type = QOS_ACTION_MARK_AND_TRANSMIT;
375 unformat_policer_action (unformat_input_t * input, va_list * va)
377 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
379 if (unformat (input, "conform-action %U", unformat_policer_action_type,
382 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
385 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
392 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
394 u32 *r = va_arg (*va, u32 *);
395 vnet_policer_main_t *pm = &vnet_policer_main;
399 if (unformat (input, "%s", &match_name))
404 p = hash_get_mem (pm->policer_index_by_name, match_name);
415 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
417 u32 *r = va_arg (*va, u32 *);
419 if (unformat (input, "conform-color"))
421 else if (unformat (input, "exceed-color"))
429 #define foreach_config_param \
439 static clib_error_t *
440 configure_policer_command_fn (vlib_main_t * vm,
441 unformat_input_t * input,
442 vlib_cli_command_t * cmd)
444 qos_pol_cfg_params_st c;
445 unformat_input_t _line_input, *line_input = &_line_input;
449 clib_error_t *error = NULL;
451 /* Get a line of input. */
452 if (!unformat_user (input, unformat_line_input, line_input))
455 clib_memset (&c, 0, sizeof (c));
457 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
459 if (unformat (line_input, "del"))
461 else if (unformat (line_input, "name %s", &name))
463 else if (unformat (line_input, "color-aware"))
466 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
471 error = clib_error_return (0, "unknown input `%U'",
472 format_unformat_error, line_input);
477 error = policer_add_del (vm, name, &c, &pi, is_add);
480 unformat_free (line_input);
486 VLIB_CLI_COMMAND (configure_policer_command, static) = {
487 .path = "configure policer",
488 .short_help = "configure policer name <name> <params> ",
489 .function = configure_policer_command_fn,
493 static clib_error_t *
494 show_policer_command_fn (vlib_main_t * vm,
495 unformat_input_t * input, vlib_cli_command_t * cmd)
497 vnet_policer_main_t *pm = &vnet_policer_main;
503 qos_pol_cfg_params_st *config;
504 policer_read_response_type_st *templ;
506 (void) unformat (input, "name %s", &match_name);
509 hash_foreach_pair (p, pm->policer_config_by_name,
511 name = (u8 *) p->key;
512 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
514 pi = hash_get_mem (pm->policer_index_by_name, name);
516 pool_index = p->value[0];
517 config = pool_elt_at_index (pm->configs, pool_index);
518 templ = pool_elt_at_index (pm->policer_templates, pool_index);
519 vlib_cli_output (vm, "Name \"%s\" %U ", name, format_policer_config,
521 vlib_cli_output (vm, "Template %U", format_policer_instance, templ,
523 vlib_cli_output (vm, "-----------");
532 VLIB_CLI_COMMAND (show_policer_command, static) = {
533 .path = "show policer",
534 .short_help = "show policer [name]",
535 .function = show_policer_command_fn,
539 static clib_error_t *
540 show_policer_pools_command_fn (vlib_main_t * vm,
541 unformat_input_t * input,
542 vlib_cli_command_t * cmd)
544 vnet_policer_main_t *pm = &vnet_policer_main;
546 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
547 pool_elts (pm->configs),
548 pool_elts (pm->policer_templates),
549 pool_elts (pm->policers));
553 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
554 .path = "show policer pools",
555 .short_help = "show policer pools",
556 .function = show_policer_pools_command_fn,
561 policer_init (vlib_main_t * vm)
563 vnet_policer_main_t *pm = &vnet_policer_main;
564 void vnet_policer_node_funcs_reference (void);
566 vnet_policer_node_funcs_reference ();
569 pm->vnet_main = vnet_get_main ();
570 pm->log_class = vlib_log_register_class ("policer", 0);
572 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
573 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
575 vnet_classify_register_unformat_policer_next_index_fn
576 (unformat_policer_classify_next_index);
577 vnet_classify_register_unformat_opaque_index_fn
578 (unformat_policer_classify_precolor);
583 VLIB_INIT_FUNCTION (policer_init);
588 * fd.io coding-style-patch-verification: ON
591 * eval: (c-set-style "gnu")