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/classify/vnet_classify.h>
19 vnet_policer_main_t vnet_policer_main;
22 policer_add_del (vlib_main_t * vm,
24 sse2_qos_pol_cfg_params_st * cfg,
25 u32 * policer_index, u8 is_add)
27 vnet_policer_main_t *pm = &vnet_policer_main;
28 policer_read_response_type_st test_policer;
29 policer_read_response_type_st *policer;
34 p = hash_get_mem (pm->policer_config_by_name, name);
38 /* free policer config and template */
42 return clib_error_return (0, "No such policer configuration");
44 pool_put_index (pm->configs, p[0]);
45 pool_put_index (pm->policer_templates, p[0]);
46 hash_unset_mem (pm->policer_config_by_name, name);
49 p = hash_get_mem (pm->policer_index_by_name, name);
53 return clib_error_return (0, "No such policer");
55 pool_put_index (pm->policers, p[0]);
56 hash_unset_mem (pm->policer_index_by_name, name);
65 return clib_error_return (0, "Policer already exists");
68 /* Vet the configuration before adding it to the table */
69 rv = sse2_pol_logical_2_physical (cfg, &test_policer);
73 policer_read_response_type_st *pp;
74 sse2_qos_pol_cfg_params_st *cp;
76 pool_get (pm->configs, cp);
77 pool_get (pm->policer_templates, pp);
79 ASSERT (cp - pm->configs == pp - pm->policer_templates);
81 clib_memcpy (cp, cfg, sizeof (*cp));
82 clib_memcpy (pp, &test_policer, sizeof (*pp));
84 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
85 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
87 pi = policer - pm->policers;
88 hash_set_mem (pm->policer_index_by_name, name, pi);
90 policer->thread_index = ~0;
95 return clib_error_return (0, "Config failed sanity check");
102 format_policer_instance (u8 * s, va_list * va)
104 policer_read_response_type_st *i
105 = va_arg (*va, policer_read_response_type_st *);
107 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
108 i, i->single_rate ? "single" : "dual",
109 i->color_aware ? "is" : "not");
110 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
111 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
112 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
114 i->current_bucket, i->extended_limit, i->extended_bucket);
115 s = format (s, "last update %llu\n", i->last_update_time);
120 format_policer_round_type (u8 * s, va_list * va)
122 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
124 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
125 s = format (s, "closest");
126 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
127 s = format (s, "up");
128 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
129 s = format (s, "down");
131 s = format (s, "ILLEGAL");
137 format_policer_rate_type (u8 * s, va_list * va)
139 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
141 if (c->rate_type == SSE2_QOS_RATE_KBPS)
142 s = format (s, "kbps");
143 else if (c->rate_type == SSE2_QOS_RATE_PPS)
144 s = format (s, "pps");
146 s = format (s, "ILLEGAL");
151 format_policer_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->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
156 s = format (s, "1r2c");
158 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
159 s = format (s, "1r3c");
161 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
162 s = format (s, "2r3c-2698");
164 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
165 s = format (s, "2r3c-4115");
167 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
168 s = format (s, "2r3c-mef5cf1");
170 s = format (s, "ILLEGAL");
175 format_dscp (u8 * s, va_list * va)
177 u32 i = va_arg (*va, u32);
182 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
186 return format (s, "ILLEGAL");
188 s = format (s, "%s", t);
193 format_policer_action_type (u8 * s, va_list * va)
195 sse2_qos_pol_action_params_st *a
196 = va_arg (*va, sse2_qos_pol_action_params_st *);
198 if (a->action_type == SSE2_QOS_ACTION_DROP)
199 s = format (s, "drop");
200 else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
201 s = format (s, "transmit");
202 else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
203 s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
205 s = format (s, "ILLEGAL");
210 format_policer_config (u8 * s, va_list * va)
212 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
214 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
215 format_policer_type, c,
217 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
218 s = format (s, "rate type %U, round type %U\n",
219 format_policer_rate_type, c, format_policer_round_type, c);
220 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
221 format_policer_action_type, &c->conform_action,
222 format_policer_action_type, &c->exceed_action,
223 format_policer_action_type, &c->violate_action);
228 unformat_policer_type (unformat_input_t * input, va_list * va)
230 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
232 if (!unformat (input, "type"))
235 if (unformat (input, "1r2c"))
236 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
237 else if (unformat (input, "1r3c"))
238 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
239 else if (unformat (input, "2r3c-2698"))
240 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
241 else if (unformat (input, "2r3c-4115"))
242 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
243 else if (unformat (input, "2r3c-mef5cf1"))
244 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
251 unformat_policer_round_type (unformat_input_t * input, va_list * va)
253 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
255 if (!unformat (input, "round"))
258 if (unformat (input, "closest"))
259 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
260 else if (unformat (input, "up"))
261 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
262 else if (unformat (input, "down"))
263 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
270 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
272 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
274 if (!unformat (input, "rate"))
277 if (unformat (input, "kbps"))
278 c->rate_type = SSE2_QOS_RATE_KBPS;
279 else if (unformat (input, "pps"))
280 c->rate_type = SSE2_QOS_RATE_PPS;
287 unformat_policer_cir (unformat_input_t * input, va_list * va)
289 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
291 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
297 unformat_policer_eir (unformat_input_t * input, va_list * va)
299 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
301 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
307 unformat_policer_cb (unformat_input_t * input, va_list * va)
309 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
311 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
317 unformat_policer_eb (unformat_input_t * input, va_list * va)
319 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
321 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
327 unformat_dscp (unformat_input_t * input, va_list * va)
329 u8 *r = va_arg (*va, u8 *);
332 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
341 unformat_policer_action_type (unformat_input_t * input, va_list * va)
343 sse2_qos_pol_action_params_st *a
344 = va_arg (*va, sse2_qos_pol_action_params_st *);
346 if (unformat (input, "drop"))
347 a->action_type = SSE2_QOS_ACTION_DROP;
348 else if (unformat (input, "transmit"))
349 a->action_type = SSE2_QOS_ACTION_TRANSMIT;
350 else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
351 a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
358 unformat_policer_action (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, "conform-action %U", unformat_policer_action_type,
365 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
368 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
375 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
377 u32 *r = va_arg (*va, u32 *);
378 vnet_policer_main_t *pm = &vnet_policer_main;
382 if (unformat (input, "%s", &match_name))
387 p = hash_get_mem (pm->policer_index_by_name, match_name);
398 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
400 u32 *r = va_arg (*va, u32 *);
402 if (unformat (input, "conform-color"))
404 else if (unformat (input, "exceed-color"))
412 #define foreach_config_param \
422 static clib_error_t *
423 configure_policer_command_fn (vlib_main_t * vm,
424 unformat_input_t * input,
425 vlib_cli_command_t * cmd)
427 sse2_qos_pol_cfg_params_st c;
428 unformat_input_t _line_input, *line_input = &_line_input;
432 clib_error_t *error = NULL;
434 /* Get a line of input. */
435 if (!unformat_user (input, unformat_line_input, line_input))
438 clib_memset (&c, 0, sizeof (c));
440 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
442 if (unformat (line_input, "del"))
444 else if (unformat (line_input, "name %s", &name))
446 else if (unformat (line_input, "color-aware"))
449 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
454 error = clib_error_return (0, "unknown input `%U'",
455 format_unformat_error, line_input);
460 error = policer_add_del (vm, name, &c, &pi, is_add);
463 unformat_free (line_input);
469 VLIB_CLI_COMMAND (configure_policer_command, static) = {
470 .path = "configure policer",
471 .short_help = "configure policer name <name> <params> ",
472 .function = configure_policer_command_fn,
476 static clib_error_t *
477 show_policer_command_fn (vlib_main_t * vm,
478 unformat_input_t * input, vlib_cli_command_t * cmd)
480 vnet_policer_main_t *pm = &vnet_policer_main;
485 sse2_qos_pol_cfg_params_st *config;
486 policer_read_response_type_st *templ;
488 (void) unformat (input, "name %s", &match_name);
491 hash_foreach_pair (p, pm->policer_config_by_name,
493 name = (u8 *) p->key;
494 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
496 pool_index = p->value[0];
497 config = pool_elt_at_index (pm->configs, pool_index);
498 templ = pool_elt_at_index (pm->policer_templates, pool_index);
499 vlib_cli_output (vm, "Name \"%s\" %U ",
500 name, format_policer_config, config);
501 vlib_cli_output (vm, "Template %U",
502 format_policer_instance, templ);
503 vlib_cli_output (vm, "-----------");
512 VLIB_CLI_COMMAND (show_policer_command, static) = {
513 .path = "show policer",
514 .short_help = "show policer [name]",
515 .function = show_policer_command_fn,
519 static clib_error_t *
520 show_policer_pools_command_fn (vlib_main_t * vm,
521 unformat_input_t * input,
522 vlib_cli_command_t * cmd)
524 vnet_policer_main_t *pm = &vnet_policer_main;
526 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
527 pool_elts (pm->configs),
528 pool_elts (pm->policer_templates),
529 pool_elts (pm->policers));
533 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
534 .path = "show policer pools",
535 .short_help = "show policer pools",
536 .function = show_policer_pools_command_fn,
541 policer_init (vlib_main_t * vm)
543 vnet_policer_main_t *pm = &vnet_policer_main;
544 void vnet_policer_node_funcs_reference (void);
546 vnet_policer_node_funcs_reference ();
549 pm->vnet_main = vnet_get_main ();
551 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
552 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
554 vnet_classify_register_unformat_policer_next_index_fn
555 (unformat_policer_classify_next_index);
556 vnet_classify_register_unformat_opaque_index_fn
557 (unformat_policer_classify_precolor);
562 VLIB_INIT_FUNCTION (policer_init);
567 * fd.io coding-style-patch-verification: ON
570 * eval: (c-set-style "gnu")