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);
94 return clib_error_return (0, "Config failed sanity check");
101 format_policer_instance (u8 * s, va_list * va)
103 policer_read_response_type_st *i
104 = va_arg (*va, policer_read_response_type_st *);
106 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
107 i, i->single_rate ? "single" : "dual",
108 i->color_aware ? "is" : "not");
109 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
110 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
111 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
113 i->current_bucket, i->extended_limit, i->extended_bucket);
114 s = format (s, "last update %llu\n", i->last_update_time);
119 format_policer_round_type (u8 * s, va_list * va)
121 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
123 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
124 s = format (s, "closest");
125 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
126 s = format (s, "up");
127 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
128 s = format (s, "down");
130 s = format (s, "ILLEGAL");
136 format_policer_rate_type (u8 * s, va_list * va)
138 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
140 if (c->rate_type == SSE2_QOS_RATE_KBPS)
141 s = format (s, "kbps");
142 else if (c->rate_type == SSE2_QOS_RATE_PPS)
143 s = format (s, "pps");
145 s = format (s, "ILLEGAL");
150 format_policer_type (u8 * s, va_list * va)
152 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
154 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
155 s = format (s, "1r2c");
157 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
158 s = format (s, "1r3c");
160 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
161 s = format (s, "2r3c-2698");
163 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
164 s = format (s, "2r3c-4115");
166 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
167 s = format (s, "2r3c-mef5cf1");
169 s = format (s, "ILLEGAL");
174 format_dscp (u8 * s, va_list * va)
176 u32 i = va_arg (*va, u32);
181 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
185 return format (s, "ILLEGAL");
187 s = format (s, "%s", t);
192 format_policer_action_type (u8 * s, va_list * va)
194 sse2_qos_pol_action_params_st *a
195 = va_arg (*va, sse2_qos_pol_action_params_st *);
197 if (a->action_type == SSE2_QOS_ACTION_DROP)
198 s = format (s, "drop");
199 else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
200 s = format (s, "transmit");
201 else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
202 s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
204 s = format (s, "ILLEGAL");
209 format_policer_config (u8 * s, va_list * va)
211 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
213 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
214 format_policer_type, c,
216 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
217 s = format (s, "rate type %U, round type %U\n",
218 format_policer_rate_type, c, format_policer_round_type, c);
219 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
220 format_policer_action_type, &c->conform_action,
221 format_policer_action_type, &c->exceed_action,
222 format_policer_action_type, &c->violate_action);
227 unformat_policer_type (unformat_input_t * input, va_list * va)
229 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
231 if (!unformat (input, "type"))
234 if (unformat (input, "1r2c"))
235 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
236 else if (unformat (input, "1r3c"))
237 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
238 else if (unformat (input, "2r3c-2698"))
239 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
240 else if (unformat (input, "2r3c-4115"))
241 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
242 else if (unformat (input, "2r3c-mef5cf1"))
243 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
250 unformat_policer_round_type (unformat_input_t * input, va_list * va)
252 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
254 if (!unformat (input, "round"))
257 if (unformat (input, "closest"))
258 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
259 else if (unformat (input, "up"))
260 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
261 else if (unformat (input, "down"))
262 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
269 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
271 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
273 if (!unformat (input, "rate"))
276 if (unformat (input, "kbps"))
277 c->rate_type = SSE2_QOS_RATE_KBPS;
278 else if (unformat (input, "pps"))
279 c->rate_type = SSE2_QOS_RATE_PPS;
286 unformat_policer_cir (unformat_input_t * input, va_list * va)
288 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
290 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
296 unformat_policer_eir (unformat_input_t * input, va_list * va)
298 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
300 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
306 unformat_policer_cb (unformat_input_t * input, va_list * va)
308 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
310 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
316 unformat_policer_eb (unformat_input_t * input, va_list * va)
318 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
320 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
326 unformat_dscp (unformat_input_t * input, va_list * va)
328 u8 *r = va_arg (*va, u8 *);
331 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
340 unformat_policer_action_type (unformat_input_t * input, va_list * va)
342 sse2_qos_pol_action_params_st *a
343 = va_arg (*va, sse2_qos_pol_action_params_st *);
345 if (unformat (input, "drop"))
346 a->action_type = SSE2_QOS_ACTION_DROP;
347 else if (unformat (input, "transmit"))
348 a->action_type = SSE2_QOS_ACTION_TRANSMIT;
349 else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
350 a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
357 unformat_policer_action (unformat_input_t * input, va_list * va)
359 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
361 if (unformat (input, "conform-action %U", unformat_policer_action_type,
364 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
367 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
374 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
376 u32 *r = va_arg (*va, u32 *);
377 vnet_policer_main_t *pm = &vnet_policer_main;
381 if (unformat (input, "%s", &match_name))
386 p = hash_get_mem (pm->policer_index_by_name, match_name);
397 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
399 u32 *r = va_arg (*va, u32 *);
401 if (unformat (input, "conform-color"))
403 else if (unformat (input, "exceed-color"))
411 #define foreach_config_param \
421 static clib_error_t *
422 configure_policer_command_fn (vlib_main_t * vm,
423 unformat_input_t * input,
424 vlib_cli_command_t * cmd)
426 sse2_qos_pol_cfg_params_st c;
427 unformat_input_t _line_input, *line_input = &_line_input;
431 clib_error_t *error = NULL;
433 /* Get a line of input. */
434 if (!unformat_user (input, unformat_line_input, line_input))
437 memset (&c, 0, sizeof (c));
439 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
441 if (unformat (line_input, "del"))
443 else if (unformat (line_input, "name %s", &name))
445 else if (unformat (line_input, "color-aware"))
448 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
453 error = clib_error_return (0, "unknown input `%U'",
454 format_unformat_error, line_input);
459 error = policer_add_del (vm, name, &c, &pi, is_add);
462 unformat_free (line_input);
468 VLIB_CLI_COMMAND (configure_policer_command, static) = {
469 .path = "configure policer",
470 .short_help = "configure policer name <name> <params> ",
471 .function = configure_policer_command_fn,
475 static clib_error_t *
476 show_policer_command_fn (vlib_main_t * vm,
477 unformat_input_t * input, vlib_cli_command_t * cmd)
479 vnet_policer_main_t *pm = &vnet_policer_main;
484 sse2_qos_pol_cfg_params_st *config;
485 policer_read_response_type_st *templ;
487 (void) unformat (input, "name %s", &match_name);
490 hash_foreach_pair (p, pm->policer_config_by_name,
492 name = (u8 *) p->key;
493 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
495 pool_index = p->value[0];
496 config = pool_elt_at_index (pm->configs, pool_index);
497 templ = pool_elt_at_index (pm->policer_templates, pool_index);
498 vlib_cli_output (vm, "Name \"%s\" %U ",
499 name, format_policer_config, config);
500 vlib_cli_output (vm, "Template %U",
501 format_policer_instance, templ);
502 vlib_cli_output (vm, "-----------");
511 VLIB_CLI_COMMAND (show_policer_command, static) = {
512 .path = "show policer",
513 .short_help = "show policer [name]",
514 .function = show_policer_command_fn,
518 static clib_error_t *
519 show_policer_pools_command_fn (vlib_main_t * vm,
520 unformat_input_t * input,
521 vlib_cli_command_t * cmd)
523 vnet_policer_main_t *pm = &vnet_policer_main;
525 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
526 pool_elts (pm->configs),
527 pool_elts (pm->policer_templates),
528 pool_elts (pm->policers));
532 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
533 .path = "show policer pools",
534 .short_help = "show policer pools",
535 .function = show_policer_pools_command_fn,
540 policer_init (vlib_main_t * vm)
542 vnet_policer_main_t *pm = &vnet_policer_main;
543 void vnet_policer_node_funcs_reference (void);
545 vnet_policer_node_funcs_reference ();
548 pm->vnet_main = vnet_get_main ();
550 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
551 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
553 vnet_classify_register_unformat_policer_next_index_fn
554 (unformat_policer_classify_next_index);
555 vnet_classify_register_unformat_opaque_index_fn
556 (unformat_policer_classify_precolor);
561 VLIB_INIT_FUNCTION (policer_init);
566 * fd.io coding-style-patch-verification: ON
569 * eval: (c-set-style "gnu")