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>
19 policer_add_del (vlib_main_t *vm,
20 u8 * name, sse2_qos_pol_cfg_params_st * cfg,
23 vnet_policer_main_t *pm = &vnet_policer_main;
24 policer_read_response_type_st test_policer;
30 p = hash_get_mem (pm->policer_config_by_name, name);
34 return clib_error_return (0, "No such policer configuration");
36 hash_unset_mem (pm->policer_config_by_name, name);
41 /* Vet the configuration before adding it to the table */
42 rv = sse2_pol_logical_2_physical (cfg, &test_policer);
46 policer_read_response_type_st *pp;
47 sse2_qos_pol_cfg_params_st *cp;
49 pool_get (pm->configs, cp);
50 pool_get (pm->policer_templates, pp);
52 ASSERT (cp - pm->configs == pp - pm->policer_templates);
54 clib_memcpy (cp, cfg, sizeof (*cp));
55 clib_memcpy (pp, &test_policer, sizeof (*pp));
57 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
62 return clib_error_return (0, "Config failed sanity check");
68 u8 * format_policer_instance (u8 * s, va_list * va)
70 policer_read_response_type_st * i
71 = va_arg (*va, policer_read_response_type_st *);
73 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
74 i, i->single_rate ? "single" : "dual",
75 i->color_aware ? "is" : "not");
76 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
77 i->cir_tokens_per_period, i->pir_tokens_per_period,
79 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
84 s = format (s, "last update %llu\n", i->last_update_time);
88 static u8 * format_policer_round_type (u8 * s, va_list * va)
90 sse2_qos_pol_cfg_params_st * c
91 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
93 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
94 s = format(s, "closest");
95 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
97 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
98 s = format (s, "down");
100 s = format (s, "ILLEGAL");
105 static u8 * format_policer_rate_type (u8 * s, va_list * va)
107 sse2_qos_pol_cfg_params_st * c
108 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
110 if (c->rate_type == SSE2_QOS_RATE_KBPS)
111 s = format (s, "kbps");
112 else if (c->rate_type == SSE2_QOS_RATE_PPS)
113 s = format(s, "pps");
115 s = format (s, "ILLEGAL");
119 static u8 * format_policer_type (u8 * s, va_list * va)
121 sse2_qos_pol_cfg_params_st * c
122 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
124 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
125 s = format (s, "1r2c");
127 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
128 s = format (s, "1r3c");
130 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
131 s = format (s, "2r3c-2698");
133 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
134 s = format (s, "2r3c-4115");
136 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
137 s = format (s, "2r3c-mef5cf1");
139 s = format (s, "ILLEGAL");
143 u8 * format_policer_config (u8 * s, va_list * va)
145 sse2_qos_pol_cfg_params_st * c
146 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
148 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
149 format_policer_type, c,
153 c->rb.kbps.eb_bytes);
154 s = format (s, "rate type %U, round type %U\n",
155 format_policer_rate_type, c,
156 format_policer_round_type, c);
161 unformat_policer_type (unformat_input_t * input, va_list * va)
163 sse2_qos_pol_cfg_params_st * c
164 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
166 if (!unformat (input, "type"))
169 if (unformat (input, "1r2c"))
170 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
171 else if (unformat (input, "1r3c"))
172 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
173 else if (unformat (input, "2r3c-2698"))
174 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
175 else if (unformat (input, "2r3c-4115"))
176 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
177 else if (unformat (input, "2r3c-mef5cf1"))
178 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
185 unformat_policer_round_type (unformat_input_t * input, va_list * va)
187 sse2_qos_pol_cfg_params_st * c
188 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
190 if (!unformat(input, "round"))
193 if (unformat(input, "closest"))
194 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
195 else if (unformat (input, "up"))
196 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
197 else if (unformat (input, "down"))
198 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
205 unformat_policer_rate_type (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, "rate"))
213 if (unformat (input, "kbps"))
214 c->rate_type = SSE2_QOS_RATE_KBPS;
215 else if (unformat(input, "pps"))
216 c->rate_type = SSE2_QOS_RATE_PPS;
223 unformat_policer_cir (unformat_input_t * input, va_list * va)
225 sse2_qos_pol_cfg_params_st * c
226 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
228 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
234 unformat_policer_eir (unformat_input_t * input, va_list * va)
236 sse2_qos_pol_cfg_params_st * c
237 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
239 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
245 unformat_policer_cb (unformat_input_t * input, va_list * va)
247 sse2_qos_pol_cfg_params_st * c
248 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
250 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
256 unformat_policer_eb (unformat_input_t * input, va_list * va)
258 sse2_qos_pol_cfg_params_st * c
259 = va_arg (*va, sse2_qos_pol_cfg_params_st *);
261 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
267 #define foreach_config_param \
276 static clib_error_t *
277 configure_policer_command_fn (vlib_main_t * vm,
278 unformat_input_t * input,
279 vlib_cli_command_t * cmd)
281 sse2_qos_pol_cfg_params_st c;
282 unformat_input_t _line_input, * line_input = &_line_input;
286 /* Get a line of input. */
287 if (! unformat_user (input, unformat_line_input, line_input))
290 memset (&c, 0, sizeof (c));
292 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
294 if (unformat (line_input, "del"))
296 else if (unformat(line_input, "name %s", &name))
299 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
304 return clib_error_return (0, "unknown input `%U'",
305 format_unformat_error, line_input);
308 unformat_free (line_input);
310 return policer_add_del(vm, name, &c, is_add);
313 VLIB_CLI_COMMAND (configure_policer_command, static) = {
314 .path = "configure policer",
315 .short_help = "configure policer name <name> <params> ",
316 .function = configure_policer_command_fn,
320 static clib_error_t *
321 show_policer_command_fn (vlib_main_t * vm,
322 unformat_input_t * input,
323 vlib_cli_command_t * cmd)
325 vnet_policer_main_t *pm = &vnet_policer_main;
330 sse2_qos_pol_cfg_params_st *config;
331 policer_read_response_type_st *templ;
333 (void) unformat (input, "name %s", &match_name);
335 hash_foreach_pair (p, pm->policer_config_by_name,
337 name = (u8 *) p->key;
338 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
340 pool_index = p->value[0];
341 config = pool_elt_at_index (pm->configs, pool_index);
342 templ = pool_elt_at_index (pm->policer_templates, pool_index);
343 vlib_cli_output (vm, "Name \"%s\" %U ",
344 name, format_policer_config, config);
345 vlib_cli_output (vm, "Template %U",
346 format_policer_instance, templ);
347 vlib_cli_output (vm, "-----------");
354 VLIB_CLI_COMMAND (show_policer_command, static) = {
355 .path = "show policer",
356 .short_help = "show policer [name]",
357 .function = show_policer_command_fn,
360 clib_error_t *policer_init (vlib_main_t * vm)
362 vnet_policer_main_t * pm = &vnet_policer_main;
363 void vnet_policer_node_funcs_reference(void);
365 vnet_policer_node_funcs_reference();
368 pm->vnet_main = vnet_get_main();
370 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
374 VLIB_INIT_FUNCTION(policer_init);