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,
26 vnet_policer_main_t *pm = &vnet_policer_main;
27 policer_read_response_type_st test_policer;
28 policer_read_response_type_st * policer;
33 p = hash_get_mem (pm->policer_config_by_name, name);
40 return clib_error_return (0, "No such policer configuration");
42 hash_unset_mem (pm->policer_config_by_name, name);
43 hash_unset_mem (pm->policer_index_by_name, name);
51 return clib_error_return (0, "Policer already exists");
54 /* Vet the configuration before adding it to the table */
55 rv = sse2_pol_logical_2_physical (cfg, &test_policer);
59 policer_read_response_type_st *pp;
60 sse2_qos_pol_cfg_params_st *cp;
62 pool_get (pm->configs, cp);
63 pool_get (pm->policer_templates, pp);
65 ASSERT (cp - pm->configs == pp - pm->policer_templates);
67 clib_memcpy (cp, cfg, sizeof (*cp));
68 clib_memcpy (pp, &test_policer, sizeof (*pp));
70 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
71 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
73 pi = policer - pm->policers;
74 hash_set_mem (pm->policer_index_by_name, name, pi);
80 return clib_error_return (0, "Config failed sanity check");
86 u8 * 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,
97 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
102 s = format (s, "last update %llu\n", i->last_update_time);
106 static u8 * format_policer_round_type (u8 * s, va_list * va)
108 sse2_qos_pol_cfg_params_st * c
109 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
111 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
112 s = format(s, "closest");
113 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
114 s = format (s, "up");
115 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
116 s = format (s, "down");
118 s = format (s, "ILLEGAL");
123 static u8 * format_policer_rate_type (u8 * s, va_list * va)
125 sse2_qos_pol_cfg_params_st * c
126 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
128 if (c->rate_type == SSE2_QOS_RATE_KBPS)
129 s = format (s, "kbps");
130 else if (c->rate_type == SSE2_QOS_RATE_PPS)
131 s = format(s, "pps");
133 s = format (s, "ILLEGAL");
137 static u8 * format_policer_type (u8 * s, va_list * va)
139 sse2_qos_pol_cfg_params_st * c
140 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
142 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
143 s = format (s, "1r2c");
145 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
146 s = format (s, "1r3c");
148 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
149 s = format (s, "2r3c-2698");
151 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
152 s = format (s, "2r3c-4115");
154 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
155 s = format (s, "2r3c-mef5cf1");
157 s = format (s, "ILLEGAL");
161 static u8 * format_dscp (u8 * s, va_list * va)
163 u32 i = va_arg (*va, u32);
167 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
171 return format (s, "ILLEGAL");
173 s = format (s, "%s", t);
177 static u8 * 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");
193 u8 * format_policer_config (u8 * s, va_list * va)
195 sse2_qos_pol_cfg_params_st * c
196 = 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,
203 c->rb.kbps.eb_bytes);
204 s = format (s, "rate type %U, round type %U\n",
205 format_policer_rate_type, c,
206 format_policer_round_type, c);
207 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
208 format_policer_action_type, &c->conform_action,
209 format_policer_action_type, &c->exceed_action,
210 format_policer_action_type, &c->violate_action);
215 unformat_policer_type (unformat_input_t * input, va_list * va)
217 sse2_qos_pol_cfg_params_st * c
218 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
220 if (!unformat (input, "type"))
223 if (unformat (input, "1r2c"))
224 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
225 else if (unformat (input, "1r3c"))
226 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
227 else if (unformat (input, "2r3c-2698"))
228 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
229 else if (unformat (input, "2r3c-4115"))
230 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
231 else if (unformat (input, "2r3c-mef5cf1"))
232 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
239 unformat_policer_round_type (unformat_input_t * input, va_list * va)
241 sse2_qos_pol_cfg_params_st * c
242 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
244 if (!unformat(input, "round"))
247 if (unformat(input, "closest"))
248 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
249 else if (unformat (input, "up"))
250 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
251 else if (unformat (input, "down"))
252 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
259 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
261 sse2_qos_pol_cfg_params_st * c
262 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
264 if (!unformat(input, "rate"))
267 if (unformat (input, "kbps"))
268 c->rate_type = SSE2_QOS_RATE_KBPS;
269 else if (unformat(input, "pps"))
270 c->rate_type = SSE2_QOS_RATE_PPS;
277 unformat_policer_cir (unformat_input_t * input, va_list * va)
279 sse2_qos_pol_cfg_params_st * c
280 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
282 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
288 unformat_policer_eir (unformat_input_t * input, va_list * va)
290 sse2_qos_pol_cfg_params_st * c
291 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
293 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
299 unformat_policer_cb (unformat_input_t * input, va_list * va)
301 sse2_qos_pol_cfg_params_st * c
302 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
304 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
310 unformat_policer_eb (unformat_input_t * input, va_list * va)
312 sse2_qos_pol_cfg_params_st * c
313 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
315 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
321 unformat_dscp (unformat_input_t * input, va_list * va)
323 u8 * r = va_arg (*va, u8 *);
326 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
335 unformat_policer_action_type (unformat_input_t * input, va_list * va)
337 sse2_qos_pol_action_params_st * a
338 = va_arg (*va, sse2_qos_pol_action_params_st *);
340 if (unformat (input, "drop"))
341 a->action_type = SSE2_QOS_ACTION_DROP;
342 else if (unformat (input, "transmit"))
343 a->action_type = SSE2_QOS_ACTION_TRANSMIT;
344 else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
345 a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
352 unformat_policer_action (unformat_input_t * input, va_list * va)
354 sse2_qos_pol_cfg_params_st * c
355 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
357 if (unformat (input, "conform-action %U", unformat_policer_action_type,
360 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
363 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
370 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
372 u32 * r = va_arg (*va, u32 *);
373 vnet_policer_main_t *pm = &vnet_policer_main;
377 if (unformat (input, "%s", &match_name))
382 p = hash_get_mem (pm->policer_index_by_name, match_name);
393 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
395 u32 * r = va_arg (*va, u32 *);
397 if (unformat (input, "conform-color"))
399 else if (unformat (input, "exceed-color"))
407 #define foreach_config_param \
417 static clib_error_t *
418 configure_policer_command_fn (vlib_main_t * vm,
419 unformat_input_t * input,
420 vlib_cli_command_t * cmd)
422 sse2_qos_pol_cfg_params_st c;
423 unformat_input_t _line_input, * line_input = &_line_input;
428 /* Get a line of input. */
429 if (! unformat_user (input, unformat_line_input, line_input))
432 memset (&c, 0, sizeof (c));
434 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
436 if (unformat (line_input, "del"))
438 else if (unformat(line_input, "name %s", &name))
440 else if (unformat(line_input, "color-aware"))
443 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
448 return clib_error_return (0, "unknown input `%U'",
449 format_unformat_error, line_input);
452 unformat_free (line_input);
454 return policer_add_del(vm, name, &c, &pi, is_add);
457 VLIB_CLI_COMMAND (configure_policer_command, static) = {
458 .path = "configure policer",
459 .short_help = "configure policer name <name> <params> ",
460 .function = configure_policer_command_fn,
464 static clib_error_t *
465 show_policer_command_fn (vlib_main_t * vm,
466 unformat_input_t * input,
467 vlib_cli_command_t * cmd)
469 vnet_policer_main_t *pm = &vnet_policer_main;
474 sse2_qos_pol_cfg_params_st *config;
475 policer_read_response_type_st *templ;
477 (void) unformat (input, "name %s", &match_name);
479 hash_foreach_pair (p, pm->policer_config_by_name,
481 name = (u8 *) p->key;
482 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
484 pool_index = p->value[0];
485 config = pool_elt_at_index (pm->configs, pool_index);
486 templ = pool_elt_at_index (pm->policer_templates, pool_index);
487 vlib_cli_output (vm, "Name \"%s\" %U ",
488 name, format_policer_config, config);
489 vlib_cli_output (vm, "Template %U",
490 format_policer_instance, templ);
491 vlib_cli_output (vm, "-----------");
498 VLIB_CLI_COMMAND (show_policer_command, static) = {
499 .path = "show policer",
500 .short_help = "show policer [name]",
501 .function = show_policer_command_fn,
504 clib_error_t *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);