blob: 99c3ff1b4ba05efed0beed5d8b2ce7b1b9c4dd03 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
Damjan Marione936bbe2016-02-25 23:17:38 +010015#include <stdint.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070016#include <vnet/policer/policer.h>
Brian Russell97de8a22021-01-19 16:50:56 +000017#include <vnet/policer/police_inlines.h>
Matus Fabian70e6a8d2016-06-20 08:10:42 -070018#include <vnet/classify/vnet_classify.h>
Brian Russelle3845d72021-02-08 15:33:18 +000019#include <vnet/ip/ip_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020
Dave Wallace71612d62017-10-24 01:32:41 -040021vnet_policer_main_t vnet_policer_main;
22
Brian Russell97de8a22021-01-19 16:50:56 +000023u8 *
24format_policer_handoff_trace (u8 *s, va_list *args)
25{
26 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
27 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
28 policer_handoff_trace_t *t = va_arg (*args, policer_handoff_trace_t *);
29
30 s = format (s, "policer %d, handoff thread %d to %d", t->policer_index,
31 t->current_worker_index, t->next_worker_index);
32
33 return s;
34}
35
Brian Russella71ed782021-01-27 11:34:33 +000036vlib_combined_counter_main_t policer_counters[] = {
37 {
38 .name = "Policer-Conform",
39 .stat_segment_name = "/net/policer/conform",
40 },
41 {
42 .name = "Policer-Exceed",
43 .stat_segment_name = "/net/policer/exceed",
44 },
45 {
46 .name = "Policer-Violate",
47 .stat_segment_name = "/net/policer/violate",
48 },
49};
50
Matus Fabian65fcd4d2016-05-13 05:44:48 -070051clib_error_t *
Damjan Marion3891cd82016-10-27 10:27:00 +020052policer_add_del (vlib_main_t * vm,
53 u8 * name,
54 sse2_qos_pol_cfg_params_st * cfg,
55 u32 * policer_index, u8 is_add)
Matus Fabian65fcd4d2016-05-13 05:44:48 -070056{
57 vnet_policer_main_t *pm = &vnet_policer_main;
58 policer_read_response_type_st test_policer;
Damjan Marion3891cd82016-10-27 10:27:00 +020059 policer_read_response_type_st *policer;
60 uword *p;
Matus Fabian70e6a8d2016-06-20 08:10:42 -070061 u32 pi;
Matus Fabian65fcd4d2016-05-13 05:44:48 -070062 int rv;
63
Matus Fabian70e6a8d2016-06-20 08:10:42 -070064 p = hash_get_mem (pm->policer_config_by_name, name);
65
Matus Fabian65fcd4d2016-05-13 05:44:48 -070066 if (is_add == 0)
67 {
Chaoyu Jin913b8732017-08-08 13:36:23 -070068 /* free policer config and template */
Matus Fabian65fcd4d2016-05-13 05:44:48 -070069 if (p == 0)
Damjan Marion3891cd82016-10-27 10:27:00 +020070 {
71 vec_free (name);
72 return clib_error_return (0, "No such policer configuration");
73 }
Chaoyu Jin913b8732017-08-08 13:36:23 -070074 pool_put_index (pm->configs, p[0]);
75 pool_put_index (pm->policer_templates, p[0]);
Matus Fabian65fcd4d2016-05-13 05:44:48 -070076 hash_unset_mem (pm->policer_config_by_name, name);
Chaoyu Jin913b8732017-08-08 13:36:23 -070077
78 /* free policer */
79 p = hash_get_mem (pm->policer_index_by_name, name);
80 if (p == 0)
81 {
82 vec_free (name);
83 return clib_error_return (0, "No such policer");
84 }
85 pool_put_index (pm->policers, p[0]);
Matus Fabian70e6a8d2016-06-20 08:10:42 -070086 hash_unset_mem (pm->policer_index_by_name, name);
Chaoyu Jin913b8732017-08-08 13:36:23 -070087
Damjan Marion3891cd82016-10-27 10:27:00 +020088 vec_free (name);
Matus Fabian65fcd4d2016-05-13 05:44:48 -070089 return 0;
90 }
91
Matus Fabian70e6a8d2016-06-20 08:10:42 -070092 if (p != 0)
93 {
Damjan Marion3891cd82016-10-27 10:27:00 +020094 vec_free (name);
Matus Fabian70e6a8d2016-06-20 08:10:42 -070095 return clib_error_return (0, "Policer already exists");
96 }
97
Matus Fabian65fcd4d2016-05-13 05:44:48 -070098 /* Vet the configuration before adding it to the table */
99 rv = sse2_pol_logical_2_physical (cfg, &test_policer);
100
101 if (rv == 0)
102 {
103 policer_read_response_type_st *pp;
104 sse2_qos_pol_cfg_params_st *cp;
Brian Russella71ed782021-01-27 11:34:33 +0000105 int i;
Matus Fabian65fcd4d2016-05-13 05:44:48 -0700106
107 pool_get (pm->configs, cp);
108 pool_get (pm->policer_templates, pp);
109
110 ASSERT (cp - pm->configs == pp - pm->policer_templates);
111
112 clib_memcpy (cp, cfg, sizeof (*cp));
113 clib_memcpy (pp, &test_policer, sizeof (*pp));
114
115 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700116 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
117 policer[0] = pp[0];
118 pi = policer - pm->policers;
119 hash_set_mem (pm->policer_index_by_name, name, pi);
120 *policer_index = pi;
Brian Russell89cd7442021-01-19 16:46:08 +0000121 policer->thread_index = ~0;
Brian Russella71ed782021-01-27 11:34:33 +0000122
123 for (i = 0; i < NUM_POLICE_RESULTS; i++)
124 {
125 vlib_validate_combined_counter (&policer_counters[i], pi);
126 vlib_zero_combined_counter (&policer_counters[i], pi);
127 }
Matus Fabian65fcd4d2016-05-13 05:44:48 -0700128 }
129 else
130 {
131 vec_free (name);
132 return clib_error_return (0, "Config failed sanity check");
133 }
134
135 return 0;
136}
137
Damjan Marion3891cd82016-10-27 10:27:00 +0200138u8 *
139format_policer_instance (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140{
Damjan Marion3891cd82016-10-27 10:27:00 +0200141 policer_read_response_type_st *i
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 = va_arg (*va, policer_read_response_type_st *);
Brian Russella71ed782021-01-27 11:34:33 +0000143 uword pi = va_arg (*va, uword);
144 int result;
145 vlib_counter_t counts[NUM_POLICE_RESULTS];
146
147 for (result = 0; result < NUM_POLICE_RESULTS; result++)
148 {
149 vlib_get_combined_counter (&policer_counters[result], pi,
150 &counts[result]);
151 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Damjan Marion3891cd82016-10-27 10:27:00 +0200153 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
154 i, i->single_rate ? "single" : "dual",
155 i->color_aware ? "is" : "not");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200157 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200159 i->current_limit,
160 i->current_bucket, i->extended_limit, i->extended_bucket);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 s = format (s, "last update %llu\n", i->last_update_time);
Brian Russella71ed782021-01-27 11:34:33 +0000162 s = format (s, "conform %llu packets, %llu bytes\n",
163 counts[POLICE_CONFORM].packets, counts[POLICE_CONFORM].bytes);
164 s = format (s, "exceed %llu packets, %llu bytes\n",
165 counts[POLICE_EXCEED].packets, counts[POLICE_EXCEED].bytes);
166 s = format (s, "violate %llu packets, %llu bytes\n",
167 counts[POLICE_VIOLATE].packets, counts[POLICE_VIOLATE].bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 return s;
Damjan Marion3891cd82016-10-27 10:27:00 +0200169}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Damjan Marion3891cd82016-10-27 10:27:00 +0200171static u8 *
172format_policer_round_type (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173{
Damjan Marion3891cd82016-10-27 10:27:00 +0200174 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176 if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
Damjan Marion3891cd82016-10-27 10:27:00 +0200177 s = format (s, "closest");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
179 s = format (s, "up");
180 else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
181 s = format (s, "down");
182 else
183 s = format (s, "ILLEGAL");
184 return s;
185}
186
187
Damjan Marion3891cd82016-10-27 10:27:00 +0200188static u8 *
189format_policer_rate_type (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
Damjan Marion3891cd82016-10-27 10:27:00 +0200191 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
193 if (c->rate_type == SSE2_QOS_RATE_KBPS)
194 s = format (s, "kbps");
195 else if (c->rate_type == SSE2_QOS_RATE_PPS)
Damjan Marion3891cd82016-10-27 10:27:00 +0200196 s = format (s, "pps");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 else
198 s = format (s, "ILLEGAL");
199 return s;
200}
201
Damjan Marion3891cd82016-10-27 10:27:00 +0200202static u8 *
203format_policer_type (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204{
Damjan Marion3891cd82016-10-27 10:27:00 +0200205 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
206
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207 if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
208 s = format (s, "1r2c");
Damjan Marion3891cd82016-10-27 10:27:00 +0200209
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
211 s = format (s, "1r3c");
212
213 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
214 s = format (s, "2r3c-2698");
215
216 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
217 s = format (s, "2r3c-4115");
218
219 else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
220 s = format (s, "2r3c-mef5cf1");
221 else
222 s = format (s, "ILLEGAL");
223 return s;
224}
225
Damjan Marion3891cd82016-10-27 10:27:00 +0200226static u8 *
Damjan Marion3891cd82016-10-27 10:27:00 +0200227format_policer_action_type (u8 * s, va_list * va)
Matus Fabian4ac74c92016-05-31 07:33:29 -0700228{
Damjan Marion3891cd82016-10-27 10:27:00 +0200229 sse2_qos_pol_action_params_st *a
Matus Fabian4ac74c92016-05-31 07:33:29 -0700230 = va_arg (*va, sse2_qos_pol_action_params_st *);
231
232 if (a->action_type == SSE2_QOS_ACTION_DROP)
233 s = format (s, "drop");
234 else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
235 s = format (s, "transmit");
236 else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
Brian Russelle3845d72021-02-08 15:33:18 +0000237 s = format (s, "mark-and-transmit %U", format_ip_dscp, a->dscp);
Matus Fabian4ac74c92016-05-31 07:33:29 -0700238 else
239 s = format (s, "ILLEGAL");
240 return s;
241}
242
Damjan Marion3891cd82016-10-27 10:27:00 +0200243u8 *
244format_policer_config (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245{
Damjan Marion3891cd82016-10-27 10:27:00 +0200246 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
248 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200249 format_policer_type, c,
250 c->rb.kbps.cir_kbps,
251 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 s = format (s, "rate type %U, round type %U\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200253 format_policer_rate_type, c, format_policer_round_type, c);
Matus Fabian4ac74c92016-05-31 07:33:29 -0700254 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200255 format_policer_action_type, &c->conform_action,
256 format_policer_action_type, &c->exceed_action,
257 format_policer_action_type, &c->violate_action);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 return s;
259}
260
261static uword
262unformat_policer_type (unformat_input_t * input, va_list * va)
263{
Damjan Marion3891cd82016-10-27 10:27:00 +0200264 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
266 if (!unformat (input, "type"))
267 return 0;
268
269 if (unformat (input, "1r2c"))
270 c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
271 else if (unformat (input, "1r3c"))
272 c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
273 else if (unformat (input, "2r3c-2698"))
274 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
275 else if (unformat (input, "2r3c-4115"))
276 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
277 else if (unformat (input, "2r3c-mef5cf1"))
278 c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
279 else
280 return 0;
281 return 1;
282}
283
284static uword
285unformat_policer_round_type (unformat_input_t * input, va_list * va)
286{
Damjan Marion3891cd82016-10-27 10:27:00 +0200287 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Damjan Marion3891cd82016-10-27 10:27:00 +0200289 if (!unformat (input, "round"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 return 0;
291
Damjan Marion3891cd82016-10-27 10:27:00 +0200292 if (unformat (input, "closest"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
294 else if (unformat (input, "up"))
295 c->rnd_type = SSE2_QOS_ROUND_TO_UP;
296 else if (unformat (input, "down"))
297 c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
298 else
299 return 0;
300 return 1;
301}
302
303static uword
304unformat_policer_rate_type (unformat_input_t * input, va_list * va)
305{
Damjan Marion3891cd82016-10-27 10:27:00 +0200306 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
Damjan Marion3891cd82016-10-27 10:27:00 +0200308 if (!unformat (input, "rate"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 return 0;
310
311 if (unformat (input, "kbps"))
312 c->rate_type = SSE2_QOS_RATE_KBPS;
Damjan Marion3891cd82016-10-27 10:27:00 +0200313 else if (unformat (input, "pps"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 c->rate_type = SSE2_QOS_RATE_PPS;
315 else
316 return 0;
317 return 1;
318}
319
320static uword
321unformat_policer_cir (unformat_input_t * input, va_list * va)
322{
Damjan Marion3891cd82016-10-27 10:27:00 +0200323 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
325 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
326 return 1;
327 return 0;
328}
329
330static uword
331unformat_policer_eir (unformat_input_t * input, va_list * va)
332{
Damjan Marion3891cd82016-10-27 10:27:00 +0200333 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
335 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
336 return 1;
337 return 0;
338}
339
340static uword
341unformat_policer_cb (unformat_input_t * input, va_list * va)
342{
Damjan Marion3891cd82016-10-27 10:27:00 +0200343 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
345 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
346 return 1;
347 return 0;
348}
349
350static uword
351unformat_policer_eb (unformat_input_t * input, va_list * va)
352{
Damjan Marion3891cd82016-10-27 10:27:00 +0200353 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
355 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
356 return 1;
357 return 0;
358}
359
Matus Fabian4ac74c92016-05-31 07:33:29 -0700360static uword
Matus Fabian4ac74c92016-05-31 07:33:29 -0700361unformat_policer_action_type (unformat_input_t * input, va_list * va)
362{
Damjan Marion3891cd82016-10-27 10:27:00 +0200363 sse2_qos_pol_action_params_st *a
Matus Fabian4ac74c92016-05-31 07:33:29 -0700364 = va_arg (*va, sse2_qos_pol_action_params_st *);
365
366 if (unformat (input, "drop"))
367 a->action_type = SSE2_QOS_ACTION_DROP;
368 else if (unformat (input, "transmit"))
369 a->action_type = SSE2_QOS_ACTION_TRANSMIT;
Brian Russelle3845d72021-02-08 15:33:18 +0000370 else if (unformat (input, "mark-and-transmit %U", unformat_ip_dscp,
371 &a->dscp))
Matus Fabian4ac74c92016-05-31 07:33:29 -0700372 a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
373 else
374 return 0;
375 return 1;
376}
377
378static uword
379unformat_policer_action (unformat_input_t * input, va_list * va)
380{
Damjan Marion3891cd82016-10-27 10:27:00 +0200381 sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
Matus Fabian4ac74c92016-05-31 07:33:29 -0700382
383 if (unformat (input, "conform-action %U", unformat_policer_action_type,
Damjan Marion3891cd82016-10-27 10:27:00 +0200384 &c->conform_action))
Matus Fabian4ac74c92016-05-31 07:33:29 -0700385 return 1;
386 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
Damjan Marion3891cd82016-10-27 10:27:00 +0200387 &c->exceed_action))
Matus Fabian4ac74c92016-05-31 07:33:29 -0700388 return 1;
389 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
Damjan Marion3891cd82016-10-27 10:27:00 +0200390 &c->violate_action))
Matus Fabian4ac74c92016-05-31 07:33:29 -0700391 return 1;
392 return 0;
393}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700395static uword
396unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
397{
Damjan Marion3891cd82016-10-27 10:27:00 +0200398 u32 *r = va_arg (*va, u32 *);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700399 vnet_policer_main_t *pm = &vnet_policer_main;
Damjan Marion3891cd82016-10-27 10:27:00 +0200400 uword *p;
401 u8 *match_name = 0;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700402
403 if (unformat (input, "%s", &match_name))
404 ;
405 else
406 return 0;
407
408 p = hash_get_mem (pm->policer_index_by_name, match_name);
409
410 if (p == 0)
411 return 0;
412
413 *r = p[0];
414
415 return 1;
416}
417
418static uword
419unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
420{
Damjan Marion3891cd82016-10-27 10:27:00 +0200421 u32 *r = va_arg (*va, u32 *);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700422
423 if (unformat (input, "conform-color"))
424 *r = POLICE_CONFORM;
425 else if (unformat (input, "exceed-color"))
426 *r = POLICE_EXCEED;
427 else
428 return 0;
429
430 return 1;
431}
432
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433#define foreach_config_param \
434_(eb) \
435_(cb) \
436_(eir) \
437_(cir) \
438_(rate_type) \
439_(round_type) \
Matus Fabian4ac74c92016-05-31 07:33:29 -0700440_(type) \
441_(action)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
443static clib_error_t *
444configure_policer_command_fn (vlib_main_t * vm,
Damjan Marion3891cd82016-10-27 10:27:00 +0200445 unformat_input_t * input,
446 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 sse2_qos_pol_cfg_params_st c;
Damjan Marion3891cd82016-10-27 10:27:00 +0200449 unformat_input_t _line_input, *line_input = &_line_input;
Matus Fabian65fcd4d2016-05-13 05:44:48 -0700450 u8 is_add = 1;
Damjan Marion3891cd82016-10-27 10:27:00 +0200451 u8 *name = 0;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700452 u32 pi;
Billy McFalla9a20e72017-02-15 11:39:12 -0500453 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
455 /* Get a line of input. */
Damjan Marion3891cd82016-10-27 10:27:00 +0200456 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 return 0;
458
Dave Barachb7b92992018-10-17 10:38:51 -0400459 clib_memset (&c, 0, sizeof (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460
Damjan Marion3891cd82016-10-27 10:27:00 +0200461 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 {
463 if (unformat (line_input, "del"))
Damjan Marion3891cd82016-10-27 10:27:00 +0200464 is_add = 0;
465 else if (unformat (line_input, "name %s", &name))
466 ;
467 else if (unformat (line_input, "color-aware"))
468 c.color_aware = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
470#define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
471 foreach_config_param
472#undef _
Damjan Marion3891cd82016-10-27 10:27:00 +0200473 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500474 {
475 error = clib_error_return (0, "unknown input `%U'",
476 format_unformat_error, line_input);
477 goto done;
478 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 }
480
Billy McFalla9a20e72017-02-15 11:39:12 -0500481 error = policer_add_del (vm, name, &c, &pi, is_add);
482
483done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484 unformat_free (line_input);
485
Billy McFalla9a20e72017-02-15 11:39:12 -0500486 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487}
488
Damjan Marion3891cd82016-10-27 10:27:00 +0200489/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490VLIB_CLI_COMMAND (configure_policer_command, static) = {
491 .path = "configure policer",
492 .short_help = "configure policer name <name> <params> ",
493 .function = configure_policer_command_fn,
494};
Damjan Marion3891cd82016-10-27 10:27:00 +0200495/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496
497static clib_error_t *
498show_policer_command_fn (vlib_main_t * vm,
Damjan Marion3891cd82016-10-27 10:27:00 +0200499 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500{
501 vnet_policer_main_t *pm = &vnet_policer_main;
Damjan Marion3891cd82016-10-27 10:27:00 +0200502 hash_pair_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 u32 pool_index;
Damjan Marion3891cd82016-10-27 10:27:00 +0200504 u8 *match_name = 0;
505 u8 *name;
Brian Russella71ed782021-01-27 11:34:33 +0000506 uword *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 sse2_qos_pol_cfg_params_st *config;
508 policer_read_response_type_st *templ;
509
510 (void) unformat (input, "name %s", &match_name);
511
Damjan Marion3891cd82016-10-27 10:27:00 +0200512 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513 hash_foreach_pair (p, pm->policer_config_by_name,
514 ({
515 name = (u8 *) p->key;
516 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
517 {
Brian Russella71ed782021-01-27 11:34:33 +0000518 pi = hash_get_mem (pm->policer_index_by_name, name);
519
520 pool_index = p->value[0];
521 config = pool_elt_at_index (pm->configs, pool_index);
522 templ = pool_elt_at_index (pm->policer_templates, pool_index);
523 vlib_cli_output (vm, "Name \"%s\" %U ", name, format_policer_config,
524 config);
525 vlib_cli_output (vm, "Template %U", format_policer_instance, templ,
526 pi[0]);
527 vlib_cli_output (vm, "-----------");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 }
529 }));
Damjan Marion3891cd82016-10-27 10:27:00 +0200530 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 return 0;
532}
533
534
Damjan Marion3891cd82016-10-27 10:27:00 +0200535/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536VLIB_CLI_COMMAND (show_policer_command, static) = {
537 .path = "show policer",
538 .short_help = "show policer [name]",
539 .function = show_policer_command_fn,
540};
Damjan Marion3891cd82016-10-27 10:27:00 +0200541/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
Chaoyu Jin913b8732017-08-08 13:36:23 -0700543static clib_error_t *
544show_policer_pools_command_fn (vlib_main_t * vm,
545 unformat_input_t * input,
546 vlib_cli_command_t * cmd)
547{
548 vnet_policer_main_t *pm = &vnet_policer_main;
549
550 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
551 pool_elts (pm->configs),
552 pool_elts (pm->policer_templates),
553 pool_elts (pm->policers));
554 return 0;
555}
556/* *INDENT-OFF* */
557VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
558 .path = "show policer pools",
559 .short_help = "show policer pools",
560 .function = show_policer_pools_command_fn,
561};
562/* *INDENT-ON* */
563
Damjan Marion3891cd82016-10-27 10:27:00 +0200564clib_error_t *
565policer_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566{
Damjan Marion3891cd82016-10-27 10:27:00 +0200567 vnet_policer_main_t *pm = &vnet_policer_main;
568 void vnet_policer_node_funcs_reference (void);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569
Damjan Marion3891cd82016-10-27 10:27:00 +0200570 vnet_policer_node_funcs_reference ();
571
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 pm->vlib_main = vm;
Damjan Marion3891cd82016-10-27 10:27:00 +0200573 pm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574
575 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700576 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
577
578 vnet_classify_register_unformat_policer_next_index_fn
579 (unformat_policer_classify_next_index);
580 vnet_classify_register_unformat_opaque_index_fn
581 (unformat_policer_classify_precolor);
582
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583 return 0;
584}
585
Damjan Marion3891cd82016-10-27 10:27:00 +0200586VLIB_INIT_FUNCTION (policer_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
588
Damjan Marion3891cd82016-10-27 10:27:00 +0200589
590/*
591 * fd.io coding-style-patch-verification: ON
592 *
593 * Local Variables:
594 * eval: (c-set-style "gnu")
595 * End:
596 */