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_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 = pol_logical_2_physical (cfg, &test_policer);
102 policer_read_response_type_st *pp;
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;
141 policer_read_response_type_st *policer;
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 format_policer_instance (u8 * s, va_list * va)
171 policer_read_response_type_st *i
172 = va_arg (*va, policer_read_response_type_st *);
173 uword pi = va_arg (*va, uword);
175 vlib_counter_t counts[NUM_POLICE_RESULTS];
177 for (result = 0; result < NUM_POLICE_RESULTS; result++)
179 vlib_get_combined_counter (&policer_counters[result], pi,
183 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
184 i, i->single_rate ? "single" : "dual",
185 i->color_aware ? "is" : "not");
186 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
187 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
188 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
190 i->current_bucket, i->extended_limit, i->extended_bucket);
191 s = format (s, "last update %llu\n", i->last_update_time);
192 s = format (s, "conform %llu packets, %llu bytes\n",
193 counts[POLICE_CONFORM].packets, counts[POLICE_CONFORM].bytes);
194 s = format (s, "exceed %llu packets, %llu bytes\n",
195 counts[POLICE_EXCEED].packets, counts[POLICE_EXCEED].bytes);
196 s = format (s, "violate %llu packets, %llu bytes\n",
197 counts[POLICE_VIOLATE].packets, counts[POLICE_VIOLATE].bytes);
202 format_policer_round_type (u8 * s, va_list * va)
204 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
206 if (c->rnd_type == QOS_ROUND_TO_CLOSEST)
207 s = format (s, "closest");
208 else if (c->rnd_type == QOS_ROUND_TO_UP)
209 s = format (s, "up");
210 else if (c->rnd_type == QOS_ROUND_TO_DOWN)
211 s = format (s, "down");
213 s = format (s, "ILLEGAL");
219 format_policer_rate_type (u8 * s, va_list * va)
221 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
223 if (c->rate_type == QOS_RATE_KBPS)
224 s = format (s, "kbps");
225 else if (c->rate_type == QOS_RATE_PPS)
226 s = format (s, "pps");
228 s = format (s, "ILLEGAL");
233 format_policer_type (u8 * s, va_list * va)
235 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
237 if (c->rfc == QOS_POLICER_TYPE_1R2C)
238 s = format (s, "1r2c");
240 else if (c->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
241 s = format (s, "1r3c");
243 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698)
244 s = format (s, "2r3c-2698");
246 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
247 s = format (s, "2r3c-4115");
249 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
250 s = format (s, "2r3c-mef5cf1");
252 s = format (s, "ILLEGAL");
257 format_policer_action_type (u8 * s, va_list * va)
259 qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *);
261 if (a->action_type == QOS_ACTION_DROP)
262 s = format (s, "drop");
263 else if (a->action_type == QOS_ACTION_TRANSMIT)
264 s = format (s, "transmit");
265 else if (a->action_type == QOS_ACTION_MARK_AND_TRANSMIT)
266 s = format (s, "mark-and-transmit %U", format_ip_dscp, a->dscp);
268 s = format (s, "ILLEGAL");
273 format_policer_config (u8 * s, va_list * va)
275 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
277 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
278 format_policer_type, c,
280 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
281 s = format (s, "rate type %U, round type %U\n",
282 format_policer_rate_type, c, format_policer_round_type, c);
283 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
284 format_policer_action_type, &c->conform_action,
285 format_policer_action_type, &c->exceed_action,
286 format_policer_action_type, &c->violate_action);
291 unformat_policer_type (unformat_input_t * input, va_list * va)
293 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
295 if (!unformat (input, "type"))
298 if (unformat (input, "1r2c"))
299 c->rfc = QOS_POLICER_TYPE_1R2C;
300 else if (unformat (input, "1r3c"))
301 c->rfc = QOS_POLICER_TYPE_1R3C_RFC_2697;
302 else if (unformat (input, "2r3c-2698"))
303 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_2698;
304 else if (unformat (input, "2r3c-4115"))
305 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_4115;
306 else if (unformat (input, "2r3c-mef5cf1"))
307 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
314 unformat_policer_round_type (unformat_input_t * input, va_list * va)
316 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
318 if (!unformat (input, "round"))
321 if (unformat (input, "closest"))
322 c->rnd_type = QOS_ROUND_TO_CLOSEST;
323 else if (unformat (input, "up"))
324 c->rnd_type = QOS_ROUND_TO_UP;
325 else if (unformat (input, "down"))
326 c->rnd_type = QOS_ROUND_TO_DOWN;
333 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
335 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
337 if (!unformat (input, "rate"))
340 if (unformat (input, "kbps"))
341 c->rate_type = QOS_RATE_KBPS;
342 else if (unformat (input, "pps"))
343 c->rate_type = QOS_RATE_PPS;
350 unformat_policer_cir (unformat_input_t * input, va_list * va)
352 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
354 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
360 unformat_policer_eir (unformat_input_t * input, va_list * va)
362 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
364 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
370 unformat_policer_cb (unformat_input_t * input, va_list * va)
372 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
374 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
380 unformat_policer_eb (unformat_input_t * input, va_list * va)
382 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
384 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
390 unformat_policer_action_type (unformat_input_t * input, va_list * va)
392 qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *);
394 if (unformat (input, "drop"))
395 a->action_type = QOS_ACTION_DROP;
396 else if (unformat (input, "transmit"))
397 a->action_type = QOS_ACTION_TRANSMIT;
398 else if (unformat (input, "mark-and-transmit %U", unformat_ip_dscp,
400 a->action_type = QOS_ACTION_MARK_AND_TRANSMIT;
407 unformat_policer_action (unformat_input_t * input, va_list * va)
409 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
411 if (unformat (input, "conform-action %U", unformat_policer_action_type,
414 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
417 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
424 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
426 u32 *r = va_arg (*va, u32 *);
427 vnet_policer_main_t *pm = &vnet_policer_main;
431 if (unformat (input, "%s", &match_name))
436 p = hash_get_mem (pm->policer_index_by_name, match_name);
447 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
449 u32 *r = va_arg (*va, u32 *);
451 if (unformat (input, "conform-color"))
453 else if (unformat (input, "exceed-color"))
461 #define foreach_config_param \
471 static clib_error_t *
472 configure_policer_command_fn (vlib_main_t * vm,
473 unformat_input_t * input,
474 vlib_cli_command_t * cmd)
476 qos_pol_cfg_params_st c;
477 unformat_input_t _line_input, *line_input = &_line_input;
481 clib_error_t *error = NULL;
483 /* Get a line of input. */
484 if (!unformat_user (input, unformat_line_input, line_input))
487 clib_memset (&c, 0, sizeof (c));
489 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
491 if (unformat (line_input, "del"))
493 else if (unformat (line_input, "name %s", &name))
495 else if (unformat (line_input, "color-aware"))
498 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
503 error = clib_error_return (0, "unknown input `%U'",
504 format_unformat_error, line_input);
509 error = policer_add_del (vm, name, &c, &pi, is_add);
512 unformat_free (line_input);
518 VLIB_CLI_COMMAND (configure_policer_command, static) = {
519 .path = "configure policer",
520 .short_help = "configure policer name <name> <params> ",
521 .function = configure_policer_command_fn,
525 static clib_error_t *
526 show_policer_command_fn (vlib_main_t * vm,
527 unformat_input_t * input, vlib_cli_command_t * cmd)
529 vnet_policer_main_t *pm = &vnet_policer_main;
535 qos_pol_cfg_params_st *config;
536 policer_read_response_type_st *templ;
538 (void) unformat (input, "name %s", &match_name);
541 hash_foreach_pair (p, pm->policer_config_by_name,
543 name = (u8 *) p->key;
544 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
546 pi = hash_get_mem (pm->policer_index_by_name, name);
548 pool_index = p->value[0];
549 config = pool_elt_at_index (pm->configs, pool_index);
550 templ = pool_elt_at_index (pm->policer_templates, pool_index);
551 vlib_cli_output (vm, "Name \"%s\" %U ", name, format_policer_config,
553 vlib_cli_output (vm, "Template %U", format_policer_instance, templ,
555 vlib_cli_output (vm, "-----------");
564 VLIB_CLI_COMMAND (show_policer_command, static) = {
565 .path = "show policer",
566 .short_help = "show policer [name]",
567 .function = show_policer_command_fn,
571 static clib_error_t *
572 show_policer_pools_command_fn (vlib_main_t * vm,
573 unformat_input_t * input,
574 vlib_cli_command_t * cmd)
576 vnet_policer_main_t *pm = &vnet_policer_main;
578 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
579 pool_elts (pm->configs),
580 pool_elts (pm->policer_templates),
581 pool_elts (pm->policers));
585 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
586 .path = "show policer pools",
587 .short_help = "show policer pools",
588 .function = show_policer_pools_command_fn,
593 policer_init (vlib_main_t * vm)
595 vnet_policer_main_t *pm = &vnet_policer_main;
596 void vnet_policer_node_funcs_reference (void);
598 vnet_policer_node_funcs_reference ();
601 pm->vnet_main = vnet_get_main ();
602 pm->log_class = vlib_log_register_class ("policer", 0);
604 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
605 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
607 vnet_classify_register_unformat_policer_next_index_fn
608 (unformat_policer_classify_next_index);
609 vnet_classify_register_unformat_opaque_index_fn
610 (unformat_policer_classify_precolor);
615 VLIB_INIT_FUNCTION (policer_init);
620 * fd.io coding-style-patch-verification: ON
623 * eval: (c-set-style "gnu")