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>
20 policer_add_del (vlib_main_t * vm,
22 sse2_qos_pol_cfg_params_st * cfg,
23 u32 * policer_index, u8 is_add)
25 vnet_policer_main_t *pm = &vnet_policer_main;
26 policer_read_response_type_st test_policer;
27 policer_read_response_type_st *policer;
32 p = hash_get_mem (pm->policer_config_by_name, name);
39 return clib_error_return (0, "No such policer configuration");
41 hash_unset_mem (pm->policer_config_by_name, name);
42 hash_unset_mem (pm->policer_index_by_name, name);
50 return clib_error_return (0, "Policer already exists");
53 /* Vet the configuration before adding it to the table */
54 rv = sse2_pol_logical_2_physical (cfg, &test_policer);
58 policer_read_response_type_st *pp;
59 sse2_qos_pol_cfg_params_st *cp;
61 pool_get (pm->configs, cp);
62 pool_get (pm->policer_templates, pp);
64 ASSERT (cp - pm->configs == pp - pm->policer_templates);
66 clib_memcpy (cp, cfg, sizeof (*cp));
67 clib_memcpy (pp, &test_policer, sizeof (*pp));
69 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
70 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
72 pi = policer - pm->policers;
73 hash_set_mem (pm->policer_index_by_name, name, pi);
79 return clib_error_return (0, "Config failed sanity check");
86 format_policer_instance (u8 * s, va_list * va)
88 policer_read_response_type_st *i
89 = va_arg (*va, policer_read_response_type_st *);
91 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
92 i, i->single_rate ? "single" : "dual",
93 i->color_aware ? "is" : "not");
94 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
95 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
96 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
98 i->current_bucket, i->extended_limit, i->extended_bucket);
99 s = format (s, "last update %llu\n", i->last_update_time);
104 format_policer_round_type (u8 * s, va_list * va)
106 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
108 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
109 s = format (s, "closest");
110 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
111 s = format (s, "up");
112 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
113 s = format (s, "down");
115 s = format (s, "ILLEGAL");
121 format_policer_rate_type (u8 * s, va_list * va)
123 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
125 if (c->rate_type == SSE2_QOS_RATE_KBPS)
126 s = format (s, "kbps");
127 else if (c->rate_type == SSE2_QOS_RATE_PPS)
128 s = format (s, "pps");
130 s = format (s, "ILLEGAL");
135 format_policer_type (u8 * s, va_list * va)
137 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
139 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
140 s = format (s, "1r2c");
142 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
143 s = format (s, "1r3c");
145 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
146 s = format (s, "2r3c-2698");
148 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
149 s = format (s, "2r3c-4115");
151 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
152 s = format (s, "2r3c-mef5cf1");
154 s = format (s, "ILLEGAL");
159 format_dscp (u8 * s, va_list * va)
161 u32 i = va_arg (*va, u32);
166 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
170 return format (s, "ILLEGAL");
172 s = format (s, "%s", t);
177 format_policer_action_type (u8 * s, va_list * va)
179 sse2_qos_pol_action_params_st *a
180 = va_arg (*va, sse2_qos_pol_action_params_st *);
182 if (a->action_type == SSE2_QOS_ACTION_DROP)
183 s = format (s, "drop");
184 else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
185 s = format (s, "transmit");
186 else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
187 s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
189 s = format (s, "ILLEGAL");
194 format_policer_config (u8 * s, va_list * va)
196 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
198 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
199 format_policer_type, c,
201 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
202 s = format (s, "rate type %U, round type %U\n",
203 format_policer_rate_type, c, format_policer_round_type, c);
204 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
205 format_policer_action_type, &c->conform_action,
206 format_policer_action_type, &c->exceed_action,
207 format_policer_action_type, &c->violate_action);
212 unformat_policer_type (unformat_input_t * input, va_list * va)
214 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
216 if (!unformat (input, "type"))
219 if (unformat (input, "1r2c"))
220 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
221 else if (unformat (input, "1r3c"))
222 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
223 else if (unformat (input, "2r3c-2698"))
224 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
225 else if (unformat (input, "2r3c-4115"))
226 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
227 else if (unformat (input, "2r3c-mef5cf1"))
228 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
235 unformat_policer_round_type (unformat_input_t * input, va_list * va)
237 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
239 if (!unformat (input, "round"))
242 if (unformat (input, "closest"))
243 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
244 else if (unformat (input, "up"))
245 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
246 else if (unformat (input, "down"))
247 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
254 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
256 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
258 if (!unformat (input, "rate"))
261 if (unformat (input, "kbps"))
262 c->rate_type = SSE2_QOS_RATE_KBPS;
263 else if (unformat (input, "pps"))
264 c->rate_type = SSE2_QOS_RATE_PPS;
271 unformat_policer_cir (unformat_input_t * input, va_list * va)
273 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
275 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
281 unformat_policer_eir (unformat_input_t * input, va_list * va)
283 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
285 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
291 unformat_policer_cb (unformat_input_t * input, va_list * va)
293 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
295 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
301 unformat_policer_eb (unformat_input_t * input, va_list * va)
303 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
305 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
311 unformat_dscp (unformat_input_t * input, va_list * va)
313 u8 *r = va_arg (*va, u8 *);
316 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
325 unformat_policer_action_type (unformat_input_t * input, va_list * va)
327 sse2_qos_pol_action_params_st *a
328 = va_arg (*va, sse2_qos_pol_action_params_st *);
330 if (unformat (input, "drop"))
331 a->action_type = SSE2_QOS_ACTION_DROP;
332 else if (unformat (input, "transmit"))
333 a->action_type = SSE2_QOS_ACTION_TRANSMIT;
334 else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
335 a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
342 unformat_policer_action (unformat_input_t * input, va_list * va)
344 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
346 if (unformat (input, "conform-action %U", unformat_policer_action_type,
349 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
352 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
359 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
361 u32 *r = va_arg (*va, u32 *);
362 vnet_policer_main_t *pm = &vnet_policer_main;
366 if (unformat (input, "%s", &match_name))
371 p = hash_get_mem (pm->policer_index_by_name, match_name);
382 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
384 u32 *r = va_arg (*va, u32 *);
386 if (unformat (input, "conform-color"))
388 else if (unformat (input, "exceed-color"))
396 #define foreach_config_param \
406 static clib_error_t *
407 configure_policer_command_fn (vlib_main_t * vm,
408 unformat_input_t * input,
409 vlib_cli_command_t * cmd)
411 sse2_qos_pol_cfg_params_st c;
412 unformat_input_t _line_input, *line_input = &_line_input;
416 clib_error_t *error = NULL;
418 /* Get a line of input. */
419 if (!unformat_user (input, unformat_line_input, line_input))
422 memset (&c, 0, sizeof (c));
424 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
426 if (unformat (line_input, "del"))
428 else if (unformat (line_input, "name %s", &name))
430 else if (unformat (line_input, "color-aware"))
433 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
438 error = clib_error_return (0, "unknown input `%U'",
439 format_unformat_error, line_input);
444 error = policer_add_del (vm, name, &c, &pi, is_add);
447 unformat_free (line_input);
453 VLIB_CLI_COMMAND (configure_policer_command, static) = {
454 .path = "configure policer",
455 .short_help = "configure policer name <name> <params> ",
456 .function = configure_policer_command_fn,
460 static clib_error_t *
461 show_policer_command_fn (vlib_main_t * vm,
462 unformat_input_t * input, vlib_cli_command_t * cmd)
464 vnet_policer_main_t *pm = &vnet_policer_main;
469 sse2_qos_pol_cfg_params_st *config;
470 policer_read_response_type_st *templ;
472 (void) unformat (input, "name %s", &match_name);
475 hash_foreach_pair (p, pm->policer_config_by_name,
477 name = (u8 *) p->key;
478 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
480 pool_index = p->value[0];
481 config = pool_elt_at_index (pm->configs, pool_index);
482 templ = pool_elt_at_index (pm->policer_templates, pool_index);
483 vlib_cli_output (vm, "Name \"%s\" %U ",
484 name, format_policer_config, config);
485 vlib_cli_output (vm, "Template %U",
486 format_policer_instance, templ);
487 vlib_cli_output (vm, "-----------");
496 VLIB_CLI_COMMAND (show_policer_command, static) = {
497 .path = "show policer",
498 .short_help = "show policer [name]",
499 .function = show_policer_command_fn,
504 policer_init (vlib_main_t * vm)
506 vnet_policer_main_t *pm = &vnet_policer_main;
507 void vnet_policer_node_funcs_reference (void);
509 vnet_policer_node_funcs_reference ();
512 pm->vnet_main = vnet_get_main ();
514 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
515 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
517 vnet_classify_register_unformat_policer_next_index_fn
518 (unformat_policer_classify_next_index);
519 vnet_classify_register_unformat_opaque_index_fn
520 (unformat_policer_classify_precolor);
525 VLIB_INIT_FUNCTION (policer_init);
530 * fd.io coding-style-patch-verification: ON
533 * eval: (c-set-style "gnu")