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.
15 #include <vnet/policer/policer.h>
17 u8 * format_policer_instance (u8 * s, va_list * va)
19 policer_read_response_type_st * i
20 = va_arg (*va, policer_read_response_type_st *);
22 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
23 i, i->single_rate ? "single" : "dual",
24 i->color_aware ? "is" : "not");
25 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
26 i->cir_tokens_per_period, i->pir_tokens_per_period,
28 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
33 s = format (s, "last update %llu\n", i->last_update_time);
37 static u8 * format_policer_round_type (u8 * s, va_list * va)
39 sse2_qos_pol_cfg_params_st * c
40 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
42 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
43 s = format(s, "closest");
44 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
46 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
47 s = format (s, "down");
49 s = format (s, "ILLEGAL");
54 static u8 * format_policer_rate_type (u8 * s, va_list * va)
56 sse2_qos_pol_cfg_params_st * c
57 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
59 if (c->rate_type == SSE2_QOS_RATE_KBPS)
60 s = format (s, "kbps");
61 else if (c->rate_type == SSE2_QOS_RATE_PPS)
64 s = format (s, "ILLEGAL");
68 static u8 * format_policer_type (u8 * s, va_list * va)
70 sse2_qos_pol_cfg_params_st * c
71 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
73 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
74 s = format (s, "1r2c");
76 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
77 s = format (s, "1r3c");
79 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
80 s = format (s, "2r3c-2698");
82 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
83 s = format (s, "2r3c-4115");
85 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
86 s = format (s, "2r3c-mef5cf1");
88 s = format (s, "ILLEGAL");
92 u8 * format_policer_config (u8 * s, va_list * va)
94 sse2_qos_pol_cfg_params_st * c
95 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
97 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
98 format_policer_type, c,
102 c->rb.kbps.eb_bytes);
103 s = format (s, "rate type %U, round type %U\n",
104 format_policer_rate_type, c,
105 format_policer_round_type, c);
110 unformat_policer_type (unformat_input_t * input, va_list * va)
112 sse2_qos_pol_cfg_params_st * c
113 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
115 if (!unformat (input, "type"))
118 if (unformat (input, "1r2c"))
119 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
120 else if (unformat (input, "1r3c"))
121 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
122 else if (unformat (input, "2r3c-2698"))
123 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
124 else if (unformat (input, "2r3c-4115"))
125 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
126 else if (unformat (input, "2r3c-mef5cf1"))
127 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
134 unformat_policer_round_type (unformat_input_t * input, va_list * va)
136 sse2_qos_pol_cfg_params_st * c
137 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
139 if (!unformat(input, "round"))
142 if (unformat(input, "closest"))
143 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
144 else if (unformat (input, "up"))
145 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
146 else if (unformat (input, "down"))
147 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
154 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
156 sse2_qos_pol_cfg_params_st * c
157 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
159 if (!unformat(input, "rate"))
162 if (unformat (input, "kbps"))
163 c->rate_type = SSE2_QOS_RATE_KBPS;
164 else if (unformat(input, "pps"))
165 c->rate_type = SSE2_QOS_RATE_PPS;
172 unformat_policer_cir (unformat_input_t * input, va_list * va)
174 sse2_qos_pol_cfg_params_st * c
175 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
177 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
183 unformat_policer_eir (unformat_input_t * input, va_list * va)
185 sse2_qos_pol_cfg_params_st * c
186 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
188 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
194 unformat_policer_cb (unformat_input_t * input, va_list * va)
196 sse2_qos_pol_cfg_params_st * c
197 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
199 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
205 unformat_policer_eb (unformat_input_t * input, va_list * va)
207 sse2_qos_pol_cfg_params_st * c
208 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
210 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
216 #define foreach_config_param \
225 static clib_error_t *
226 configure_policer_command_fn (vlib_main_t * vm,
227 unformat_input_t * input,
228 vlib_cli_command_t * cmd)
230 vnet_policer_main_t *pm = &vnet_policer_main;
231 sse2_qos_pol_cfg_params_st c;
232 policer_read_response_type_st test_policer;
233 unformat_input_t _line_input, * line_input = &_line_input;
239 /* Get a line of input. */
240 if (! unformat_user (input, unformat_line_input, line_input))
243 memset (&c, 0, sizeof (c));
245 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
247 if (unformat (line_input, "del"))
249 else if (unformat(line_input, "name %s", &name))
252 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
257 return clib_error_return (0, "unknown input `%U'",
258 format_unformat_error, line_input);
261 unformat_free (line_input);
265 p = hash_get_mem (pm->policer_config_by_name, name);
269 return clib_error_return (0, "No such policer configuration");
271 hash_unset_mem (pm->policer_config_by_name, name);
276 /* Vet the configuration before adding it to the table */
277 rv = sse2_pol_logical_2_physical (&c, &test_policer);
281 policer_read_response_type_st *pp;
282 sse2_qos_pol_cfg_params_st *cp;
284 pool_get (pm->configs, cp);
285 pool_get (pm->policer_templates, pp);
287 ASSERT (cp - pm->configs == pp - pm->policer_templates);
289 memcpy (cp, &c, sizeof (*cp));
290 memcpy (pp, &test_policer, sizeof (*pp));
292 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
297 return clib_error_return (0, "Config failed sanity check");
303 VLIB_CLI_COMMAND (configure_policer_command, static) = {
304 .path = "configure policer",
305 .short_help = "configure policer name <name> <params> ",
306 .function = configure_policer_command_fn,
310 static clib_error_t *
311 show_policer_command_fn (vlib_main_t * vm,
312 unformat_input_t * input,
313 vlib_cli_command_t * cmd)
315 vnet_policer_main_t *pm = &vnet_policer_main;
320 sse2_qos_pol_cfg_params_st *config;
321 policer_read_response_type_st *templ;
323 (void) unformat (input, "name %s", &match_name);
325 hash_foreach_pair (p, pm->policer_config_by_name,
327 name = (u8 *) p->key;
328 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
330 pool_index = p->value[0];
331 config = pool_elt_at_index (pm->configs, pool_index);
332 templ = pool_elt_at_index (pm->policer_templates, pool_index);
333 vlib_cli_output (vm, "Name \"%s\" %U ",
334 name, format_policer_config, config);
335 vlib_cli_output (vm, "Template %U",
336 format_policer_instance, templ);
337 vlib_cli_output (vm, "-----------");
344 VLIB_CLI_COMMAND (show_policer_command, static) = {
345 .path = "show policer",
346 .short_help = "show policer [name]",
347 .function = show_policer_command_fn,
350 clib_error_t *policer_init (vlib_main_t * vm)
352 vnet_policer_main_t * pm = &vnet_policer_main;
353 void vnet_policer_node_funcs_reference(void);
355 vnet_policer_node_funcs_reference();
358 pm->vnet_main = vnet_get_main();
360 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
364 VLIB_INIT_FUNCTION(policer_init);