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.
17 #include <vnet/policer/policer.h>
18 #include <vnet/policer/police_inlines.h>
19 #include <vnet/classify/vnet_classify.h>
20 #include <vnet/ip/ip_packet.h>
22 vnet_policer_main_t vnet_policer_main;
25 format_policer_handoff_trace (u8 *s, va_list *args)
27 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
28 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
29 policer_handoff_trace_t *t = va_arg (*args, policer_handoff_trace_t *);
31 s = format (s, "policer %d, handoff thread %d to %d", t->policer_index,
32 t->current_worker_index, t->next_worker_index);
37 vlib_combined_counter_main_t policer_counters[] = {
39 .name = "Policer-Conform",
40 .stat_segment_name = "/net/policer/conform",
43 .name = "Policer-Exceed",
44 .stat_segment_name = "/net/policer/exceed",
47 .name = "Policer-Violate",
48 .stat_segment_name = "/net/policer/violate",
53 policer_add_del (vlib_main_t *vm, u8 *name, qos_pol_cfg_params_st *cfg,
54 u32 *policer_index, u8 is_add)
56 vnet_policer_main_t *pm = &vnet_policer_main;
57 policer_t test_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 = pol_logical_2_physical (cfg, &test_policer);
103 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 policer_bind_worker (u8 *name, u32 worker, bool bind)
140 vnet_policer_main_t *pm = &vnet_policer_main;
144 p = hash_get_mem (pm->policer_index_by_name, name);
147 return VNET_API_ERROR_NO_SUCH_ENTRY;
150 policer = &pm->policers[p[0]];
154 if (worker >= vlib_num_workers ())
156 return VNET_API_ERROR_INVALID_WORKER;
159 policer->thread_index = vlib_get_worker_thread_index (worker);
163 policer->thread_index = ~0;
169 policer_input (u8 *name, u32 sw_if_index, vlib_dir_t dir, bool apply)
171 vnet_policer_main_t *pm = &vnet_policer_main;
176 p = hash_get_mem (pm->policer_index_by_name, name);
179 return VNET_API_ERROR_NO_SUCH_ENTRY;
182 policer = &pm->policers[p[0]];
183 policer_index = policer - pm->policers;
187 vec_validate (pm->policer_index_by_sw_if_index[dir], sw_if_index);
188 pm->policer_index_by_sw_if_index[dir][sw_if_index] = policer_index;
192 pm->policer_index_by_sw_if_index[dir][sw_if_index] = ~0;
197 vnet_feature_enable_disable ("device-input", "policer-input",
198 sw_if_index, apply, 0, 0);
202 vnet_feature_enable_disable ("ip4-output", "policer-output", sw_if_index,
204 vnet_feature_enable_disable ("ip6-output", "policer-output", sw_if_index,
211 format_policer_instance (u8 * s, va_list * va)
213 policer_t *i = va_arg (*va, policer_t *);
214 uword pi = va_arg (*va, uword);
216 vlib_counter_t counts[NUM_POLICE_RESULTS];
218 for (result = 0; result < NUM_POLICE_RESULTS; result++)
220 vlib_get_combined_counter (&policer_counters[result], pi,
224 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
225 i, i->single_rate ? "single" : "dual",
226 i->color_aware ? "is" : "not");
227 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
228 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
229 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
231 i->current_bucket, i->extended_limit, i->extended_bucket);
232 s = format (s, "last update %llu\n", i->last_update_time);
233 s = format (s, "conform %llu packets, %llu bytes\n",
234 counts[POLICE_CONFORM].packets, counts[POLICE_CONFORM].bytes);
235 s = format (s, "exceed %llu packets, %llu bytes\n",
236 counts[POLICE_EXCEED].packets, counts[POLICE_EXCEED].bytes);
237 s = format (s, "violate %llu packets, %llu bytes\n",
238 counts[POLICE_VIOLATE].packets, counts[POLICE_VIOLATE].bytes);
243 format_policer_round_type (u8 * s, va_list * va)
245 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
247 if (c->rnd_type == QOS_ROUND_TO_CLOSEST)
248 s = format (s, "closest");
249 else if (c->rnd_type == QOS_ROUND_TO_UP)
250 s = format (s, "up");
251 else if (c->rnd_type == QOS_ROUND_TO_DOWN)
252 s = format (s, "down");
254 s = format (s, "ILLEGAL");
260 format_policer_rate_type (u8 * s, va_list * va)
262 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
264 if (c->rate_type == QOS_RATE_KBPS)
265 s = format (s, "kbps");
266 else if (c->rate_type == QOS_RATE_PPS)
267 s = format (s, "pps");
269 s = format (s, "ILLEGAL");
274 format_policer_type (u8 * s, va_list * va)
276 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
278 if (c->rfc == QOS_POLICER_TYPE_1R2C)
279 s = format (s, "1r2c");
281 else if (c->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
282 s = format (s, "1r3c");
284 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698)
285 s = format (s, "2r3c-2698");
287 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
288 s = format (s, "2r3c-4115");
290 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
291 s = format (s, "2r3c-mef5cf1");
293 s = format (s, "ILLEGAL");
298 format_policer_action_type (u8 * s, va_list * va)
300 qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *);
302 if (a->action_type == QOS_ACTION_DROP)
303 s = format (s, "drop");
304 else if (a->action_type == QOS_ACTION_TRANSMIT)
305 s = format (s, "transmit");
306 else if (a->action_type == QOS_ACTION_MARK_AND_TRANSMIT)
307 s = format (s, "mark-and-transmit %U", format_ip_dscp, a->dscp);
309 s = format (s, "ILLEGAL");
314 format_policer_config (u8 * s, va_list * va)
316 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
318 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
319 format_policer_type, c,
321 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
322 s = format (s, "rate type %U, round type %U\n",
323 format_policer_rate_type, c, format_policer_round_type, c);
324 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
325 format_policer_action_type, &c->conform_action,
326 format_policer_action_type, &c->exceed_action,
327 format_policer_action_type, &c->violate_action);
332 unformat_policer_type (unformat_input_t * input, va_list * va)
334 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
336 if (!unformat (input, "type"))
339 if (unformat (input, "1r2c"))
340 c->rfc = QOS_POLICER_TYPE_1R2C;
341 else if (unformat (input, "1r3c"))
342 c->rfc = QOS_POLICER_TYPE_1R3C_RFC_2697;
343 else if (unformat (input, "2r3c-2698"))
344 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_2698;
345 else if (unformat (input, "2r3c-4115"))
346 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_4115;
347 else if (unformat (input, "2r3c-mef5cf1"))
348 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
355 unformat_policer_round_type (unformat_input_t * input, va_list * va)
357 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
359 if (!unformat (input, "round"))
362 if (unformat (input, "closest"))
363 c->rnd_type = QOS_ROUND_TO_CLOSEST;
364 else if (unformat (input, "up"))
365 c->rnd_type = QOS_ROUND_TO_UP;
366 else if (unformat (input, "down"))
367 c->rnd_type = QOS_ROUND_TO_DOWN;
374 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
376 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
378 if (!unformat (input, "rate"))
381 if (unformat (input, "kbps"))
382 c->rate_type = QOS_RATE_KBPS;
383 else if (unformat (input, "pps"))
384 c->rate_type = QOS_RATE_PPS;
391 unformat_policer_cir (unformat_input_t * input, va_list * va)
393 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
395 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
401 unformat_policer_eir (unformat_input_t * input, va_list * va)
403 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
405 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
411 unformat_policer_cb (unformat_input_t * input, va_list * va)
413 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
415 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
421 unformat_policer_eb (unformat_input_t * input, va_list * va)
423 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
425 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
431 unformat_policer_action_type (unformat_input_t * input, va_list * va)
433 qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *);
435 if (unformat (input, "drop"))
436 a->action_type = QOS_ACTION_DROP;
437 else if (unformat (input, "transmit"))
438 a->action_type = QOS_ACTION_TRANSMIT;
439 else if (unformat (input, "mark-and-transmit %U", unformat_ip_dscp,
441 a->action_type = QOS_ACTION_MARK_AND_TRANSMIT;
448 unformat_policer_action (unformat_input_t * input, va_list * va)
450 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
452 if (unformat (input, "conform-action %U", unformat_policer_action_type,
455 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
458 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
465 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
467 u32 *r = va_arg (*va, u32 *);
468 vnet_policer_main_t *pm = &vnet_policer_main;
472 if (unformat (input, "%s", &match_name))
477 p = hash_get_mem (pm->policer_index_by_name, match_name);
488 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
490 u32 *r = va_arg (*va, u32 *);
492 if (unformat (input, "conform-color"))
494 else if (unformat (input, "exceed-color"))
502 #define foreach_config_param \
512 static clib_error_t *
513 policer_add_command_fn (vlib_main_t *vm, unformat_input_t *input,
514 vlib_cli_command_t *cmd)
516 qos_pol_cfg_params_st c;
517 unformat_input_t _line_input, *line_input = &_line_input;
521 clib_error_t *error = NULL;
523 /* Get a line of input. */
524 if (!unformat_user (input, unformat_line_input, line_input))
527 clib_memset (&c, 0, sizeof (c));
529 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
531 if (unformat (line_input, "del"))
533 else if (unformat (line_input, "name %s", &name))
535 else if (unformat (line_input, "color-aware"))
538 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
543 error = clib_error_return (0, "unknown input `%U'",
544 format_unformat_error, line_input);
549 error = policer_add_del (vm, name, &c, &pi, is_add);
552 unformat_free (line_input);
557 static clib_error_t *
558 policer_del_command_fn (vlib_main_t *vm, unformat_input_t *input,
559 vlib_cli_command_t *cmd)
561 unformat_input_t _line_input, *line_input = &_line_input;
562 clib_error_t *error = NULL;
565 /* Get a line of input. */
566 if (!unformat_user (input, unformat_line_input, line_input))
569 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
571 if (unformat (line_input, "name %s", &name))
575 error = clib_error_return (0, "unknown input `%U'",
576 format_unformat_error, line_input);
581 error = policer_add_del (vm, name, NULL, NULL, 0);
584 unformat_free (line_input);
589 static clib_error_t *
590 policer_bind_command_fn (vlib_main_t *vm, unformat_input_t *input,
591 vlib_cli_command_t *cmd)
593 unformat_input_t _line_input, *line_input = &_line_input;
594 clib_error_t *error = NULL;
602 /* Get a line of input. */
603 if (!unformat_user (input, unformat_line_input, line_input))
606 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
608 if (unformat (line_input, "name %s", &name))
610 else if (unformat (line_input, "unbind"))
612 else if (unformat (line_input, "%d", &worker))
616 error = clib_error_return (0, "unknown input `%U'",
617 format_unformat_error, line_input);
622 if (bind && ~0 == worker)
624 error = clib_error_return (0, "specify worker to bind to: `%U'",
625 format_unformat_error, line_input);
629 rv = policer_bind_worker (name, worker, bind);
632 error = clib_error_return (0, "failed: `%d'", rv);
636 unformat_free (line_input);
641 static clib_error_t *
642 policer_input_command_fn (vlib_main_t *vm, unformat_input_t *input,
643 vlib_cli_command_t *cmd)
645 unformat_input_t _line_input, *line_input = &_line_input;
646 clib_error_t *error = NULL;
650 vlib_dir_t dir = cmd->function_arg;
655 /* Get a line of input. */
656 if (!unformat_user (input, unformat_line_input, line_input))
659 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
661 if (unformat (line_input, "name %s", &name))
663 else if (unformat (line_input, "unapply"))
665 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
666 vnet_get_main (), &sw_if_index))
670 error = clib_error_return (0, "unknown input `%U'",
671 format_unformat_error, line_input);
676 if (~0 == sw_if_index)
678 error = clib_error_return (0, "specify interface to apply to: `%U'",
679 format_unformat_error, line_input);
683 rv = policer_input (name, sw_if_index, dir, apply);
686 error = clib_error_return (0, "failed: `%d'", rv);
690 unformat_free (line_input);
695 VLIB_CLI_COMMAND (configure_policer_command, static) = {
696 .path = "configure policer",
697 .short_help = "configure policer name <name> <params> ",
698 .function = policer_add_command_fn,
701 VLIB_CLI_COMMAND (policer_add_command, static) = {
702 .path = "policer add",
703 .short_help = "policer name <name> <params> ",
704 .function = policer_add_command_fn,
707 VLIB_CLI_COMMAND (policer_del_command, static) = {
708 .path = "policer del",
709 .short_help = "policer del name <name> ",
710 .function = policer_del_command_fn,
713 VLIB_CLI_COMMAND (policer_bind_command, static) = {
714 .path = "policer bind",
715 .short_help = "policer bind [unbind] name <name> <worker>",
716 .function = policer_bind_command_fn,
719 VLIB_CLI_COMMAND (policer_input_command, static) = {
720 .path = "policer input",
721 .short_help = "policer input [unapply] name <name> <interfac>",
722 .function = policer_input_command_fn,
723 .function_arg = VLIB_RX,
726 VLIB_CLI_COMMAND (policer_output_command, static) = {
727 .path = "policer output",
728 .short_help = "policer output [unapply] name <name> <interfac>",
729 .function = policer_input_command_fn,
730 .function_arg = VLIB_TX,
733 static clib_error_t *
734 show_policer_command_fn (vlib_main_t * vm,
735 unformat_input_t * input, vlib_cli_command_t * cmd)
737 vnet_policer_main_t *pm = &vnet_policer_main;
743 qos_pol_cfg_params_st *config;
746 (void) unformat (input, "name %s", &match_name);
749 hash_foreach_pair (p, pm->policer_config_by_name,
751 name = (u8 *) p->key;
752 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
754 pi = hash_get_mem (pm->policer_index_by_name, name);
756 pool_index = p->value[0];
757 config = pool_elt_at_index (pm->configs, pool_index);
758 templ = pool_elt_at_index (pm->policer_templates, pool_index);
759 vlib_cli_output (vm, "Name \"%s\" %U ", name, format_policer_config,
763 vlib_cli_output (vm, "Template %U", format_policer_instance, templ,
769 vm, "Cannot print template - policer index hash lookup failed");
771 vlib_cli_output (vm, "-----------");
780 VLIB_CLI_COMMAND (show_policer_command, static) = {
781 .path = "show policer",
782 .short_help = "show policer [name]",
783 .function = show_policer_command_fn,
787 static clib_error_t *
788 show_policer_pools_command_fn (vlib_main_t * vm,
789 unformat_input_t * input,
790 vlib_cli_command_t * cmd)
792 vnet_policer_main_t *pm = &vnet_policer_main;
794 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
795 pool_elts (pm->configs),
796 pool_elts (pm->policer_templates),
797 pool_elts (pm->policers));
801 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
802 .path = "show policer pools",
803 .short_help = "show policer pools",
804 .function = show_policer_pools_command_fn,
809 policer_init (vlib_main_t * vm)
811 vnet_policer_main_t *pm = &vnet_policer_main;
814 pm->vnet_main = vnet_get_main ();
815 pm->log_class = vlib_log_register_class ("policer", 0);
816 pm->fq_index[VLIB_RX] =
817 vlib_frame_queue_main_init (policer_input_node.index, 0);
818 pm->fq_index[VLIB_TX] =
819 vlib_frame_queue_main_init (policer_output_node.index, 0);
821 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
822 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
824 vnet_classify_register_unformat_policer_next_index_fn
825 (unformat_policer_classify_next_index);
826 vnet_classify_register_unformat_opaque_index_fn
827 (unformat_policer_classify_precolor);
832 VLIB_INIT_FUNCTION (policer_init);
837 * fd.io coding-style-patch-verification: ON
840 * eval: (c-set-style "gnu")