blob: 6d5197ed8ed206b19969bebfdbaafc6685179a51 [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 *
Brian Russellc5299ff2021-02-09 10:16:58 +000052policer_add_del (vlib_main_t *vm, u8 *name, qos_pol_cfg_params_st *cfg,
53 u32 *policer_index, u8 is_add)
Matus Fabian65fcd4d2016-05-13 05:44:48 -070054{
55 vnet_policer_main_t *pm = &vnet_policer_main;
56 policer_read_response_type_st test_policer;
Damjan Marion3891cd82016-10-27 10:27:00 +020057 policer_read_response_type_st *policer;
58 uword *p;
Matus Fabian70e6a8d2016-06-20 08:10:42 -070059 u32 pi;
Matus Fabian65fcd4d2016-05-13 05:44:48 -070060 int rv;
61
Matus Fabian70e6a8d2016-06-20 08:10:42 -070062 p = hash_get_mem (pm->policer_config_by_name, name);
63
Matus Fabian65fcd4d2016-05-13 05:44:48 -070064 if (is_add == 0)
65 {
Chaoyu Jin913b8732017-08-08 13:36:23 -070066 /* free policer config and template */
Matus Fabian65fcd4d2016-05-13 05:44:48 -070067 if (p == 0)
Damjan Marion3891cd82016-10-27 10:27:00 +020068 {
69 vec_free (name);
70 return clib_error_return (0, "No such policer configuration");
71 }
Chaoyu Jin913b8732017-08-08 13:36:23 -070072 pool_put_index (pm->configs, p[0]);
73 pool_put_index (pm->policer_templates, p[0]);
Matus Fabian65fcd4d2016-05-13 05:44:48 -070074 hash_unset_mem (pm->policer_config_by_name, name);
Chaoyu Jin913b8732017-08-08 13:36:23 -070075
76 /* free policer */
77 p = hash_get_mem (pm->policer_index_by_name, name);
78 if (p == 0)
79 {
80 vec_free (name);
81 return clib_error_return (0, "No such policer");
82 }
83 pool_put_index (pm->policers, p[0]);
Matus Fabian70e6a8d2016-06-20 08:10:42 -070084 hash_unset_mem (pm->policer_index_by_name, name);
Chaoyu Jin913b8732017-08-08 13:36:23 -070085
Damjan Marion3891cd82016-10-27 10:27:00 +020086 vec_free (name);
Matus Fabian65fcd4d2016-05-13 05:44:48 -070087 return 0;
88 }
89
Matus Fabian70e6a8d2016-06-20 08:10:42 -070090 if (p != 0)
91 {
Damjan Marion3891cd82016-10-27 10:27:00 +020092 vec_free (name);
Matus Fabian70e6a8d2016-06-20 08:10:42 -070093 return clib_error_return (0, "Policer already exists");
94 }
95
Matus Fabian65fcd4d2016-05-13 05:44:48 -070096 /* Vet the configuration before adding it to the table */
Brian Russellc5299ff2021-02-09 10:16:58 +000097 rv = pol_logical_2_physical (cfg, &test_policer);
Matus Fabian65fcd4d2016-05-13 05:44:48 -070098
99 if (rv == 0)
100 {
101 policer_read_response_type_st *pp;
Brian Russellc5299ff2021-02-09 10:16:58 +0000102 qos_pol_cfg_params_st *cp;
Brian Russella71ed782021-01-27 11:34:33 +0000103 int i;
Matus Fabian65fcd4d2016-05-13 05:44:48 -0700104
105 pool_get (pm->configs, cp);
106 pool_get (pm->policer_templates, pp);
107
108 ASSERT (cp - pm->configs == pp - pm->policer_templates);
109
110 clib_memcpy (cp, cfg, sizeof (*cp));
111 clib_memcpy (pp, &test_policer, sizeof (*pp));
112
113 hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700114 pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
115 policer[0] = pp[0];
116 pi = policer - pm->policers;
117 hash_set_mem (pm->policer_index_by_name, name, pi);
118 *policer_index = pi;
Brian Russell89cd7442021-01-19 16:46:08 +0000119 policer->thread_index = ~0;
Brian Russella71ed782021-01-27 11:34:33 +0000120
121 for (i = 0; i < NUM_POLICE_RESULTS; i++)
122 {
123 vlib_validate_combined_counter (&policer_counters[i], pi);
124 vlib_zero_combined_counter (&policer_counters[i], pi);
125 }
Matus Fabian65fcd4d2016-05-13 05:44:48 -0700126 }
127 else
128 {
129 vec_free (name);
130 return clib_error_return (0, "Config failed sanity check");
131 }
132
133 return 0;
134}
135
Damjan Marion3891cd82016-10-27 10:27:00 +0200136u8 *
137format_policer_instance (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138{
Damjan Marion3891cd82016-10-27 10:27:00 +0200139 policer_read_response_type_st *i
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 = va_arg (*va, policer_read_response_type_st *);
Brian Russella71ed782021-01-27 11:34:33 +0000141 uword pi = va_arg (*va, uword);
142 int result;
143 vlib_counter_t counts[NUM_POLICE_RESULTS];
144
145 for (result = 0; result < NUM_POLICE_RESULTS; result++)
146 {
147 vlib_get_combined_counter (&policer_counters[result], pi,
148 &counts[result]);
149 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Damjan Marion3891cd82016-10-27 10:27:00 +0200151 s = format (s, "policer at %llx: %s rate, %s color-aware\n",
152 i, i->single_rate ? "single" : "dual",
153 i->color_aware ? "is" : "not");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200155 i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200157 i->current_limit,
158 i->current_bucket, i->extended_limit, i->extended_bucket);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 s = format (s, "last update %llu\n", i->last_update_time);
Brian Russella71ed782021-01-27 11:34:33 +0000160 s = format (s, "conform %llu packets, %llu bytes\n",
161 counts[POLICE_CONFORM].packets, counts[POLICE_CONFORM].bytes);
162 s = format (s, "exceed %llu packets, %llu bytes\n",
163 counts[POLICE_EXCEED].packets, counts[POLICE_EXCEED].bytes);
164 s = format (s, "violate %llu packets, %llu bytes\n",
165 counts[POLICE_VIOLATE].packets, counts[POLICE_VIOLATE].bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 return s;
Damjan Marion3891cd82016-10-27 10:27:00 +0200167}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Damjan Marion3891cd82016-10-27 10:27:00 +0200169static u8 *
170format_policer_round_type (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171{
Brian Russellc5299ff2021-02-09 10:16:58 +0000172 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
Brian Russellc5299ff2021-02-09 10:16:58 +0000174 if (c->rnd_type == QOS_ROUND_TO_CLOSEST)
Damjan Marion3891cd82016-10-27 10:27:00 +0200175 s = format (s, "closest");
Brian Russellc5299ff2021-02-09 10:16:58 +0000176 else if (c->rnd_type == QOS_ROUND_TO_UP)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 s = format (s, "up");
Brian Russellc5299ff2021-02-09 10:16:58 +0000178 else if (c->rnd_type == QOS_ROUND_TO_DOWN)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 s = format (s, "down");
180 else
181 s = format (s, "ILLEGAL");
182 return s;
183}
184
185
Damjan Marion3891cd82016-10-27 10:27:00 +0200186static u8 *
187format_policer_rate_type (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188{
Brian Russellc5299ff2021-02-09 10:16:58 +0000189 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Brian Russellc5299ff2021-02-09 10:16:58 +0000191 if (c->rate_type == QOS_RATE_KBPS)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 s = format (s, "kbps");
Brian Russellc5299ff2021-02-09 10:16:58 +0000193 else if (c->rate_type == QOS_RATE_PPS)
Damjan Marion3891cd82016-10-27 10:27:00 +0200194 s = format (s, "pps");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 else
196 s = format (s, "ILLEGAL");
197 return s;
198}
199
Damjan Marion3891cd82016-10-27 10:27:00 +0200200static u8 *
201format_policer_type (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202{
Brian Russellc5299ff2021-02-09 10:16:58 +0000203 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Damjan Marion3891cd82016-10-27 10:27:00 +0200204
Brian Russellc5299ff2021-02-09 10:16:58 +0000205 if (c->rfc == QOS_POLICER_TYPE_1R2C)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 s = format (s, "1r2c");
Damjan Marion3891cd82016-10-27 10:27:00 +0200207
Brian Russellc5299ff2021-02-09 10:16:58 +0000208 else if (c->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209 s = format (s, "1r3c");
210
Brian Russellc5299ff2021-02-09 10:16:58 +0000211 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 s = format (s, "2r3c-2698");
213
Brian Russellc5299ff2021-02-09 10:16:58 +0000214 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 s = format (s, "2r3c-4115");
216
Brian Russellc5299ff2021-02-09 10:16:58 +0000217 else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218 s = format (s, "2r3c-mef5cf1");
219 else
220 s = format (s, "ILLEGAL");
221 return s;
222}
223
Damjan Marion3891cd82016-10-27 10:27:00 +0200224static u8 *
Damjan Marion3891cd82016-10-27 10:27:00 +0200225format_policer_action_type (u8 * s, va_list * va)
Matus Fabian4ac74c92016-05-31 07:33:29 -0700226{
Brian Russellc5299ff2021-02-09 10:16:58 +0000227 qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *);
Matus Fabian4ac74c92016-05-31 07:33:29 -0700228
Brian Russellc5299ff2021-02-09 10:16:58 +0000229 if (a->action_type == QOS_ACTION_DROP)
Matus Fabian4ac74c92016-05-31 07:33:29 -0700230 s = format (s, "drop");
Brian Russellc5299ff2021-02-09 10:16:58 +0000231 else if (a->action_type == QOS_ACTION_TRANSMIT)
Matus Fabian4ac74c92016-05-31 07:33:29 -0700232 s = format (s, "transmit");
Brian Russellc5299ff2021-02-09 10:16:58 +0000233 else if (a->action_type == QOS_ACTION_MARK_AND_TRANSMIT)
Brian Russelle3845d72021-02-08 15:33:18 +0000234 s = format (s, "mark-and-transmit %U", format_ip_dscp, a->dscp);
Matus Fabian4ac74c92016-05-31 07:33:29 -0700235 else
236 s = format (s, "ILLEGAL");
237 return s;
238}
239
Damjan Marion3891cd82016-10-27 10:27:00 +0200240u8 *
241format_policer_config (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242{
Brian Russellc5299ff2021-02-09 10:16:58 +0000243 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
245 s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200246 format_policer_type, c,
247 c->rb.kbps.cir_kbps,
248 c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 s = format (s, "rate type %U, round type %U\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200250 format_policer_rate_type, c, format_policer_round_type, c);
Matus Fabian4ac74c92016-05-31 07:33:29 -0700251 s = format (s, "conform action %U, exceed action %U, violate action %U\n",
Damjan Marion3891cd82016-10-27 10:27:00 +0200252 format_policer_action_type, &c->conform_action,
253 format_policer_action_type, &c->exceed_action,
254 format_policer_action_type, &c->violate_action);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 return s;
256}
257
258static uword
259unformat_policer_type (unformat_input_t * input, va_list * va)
260{
Brian Russellc5299ff2021-02-09 10:16:58 +0000261 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
263 if (!unformat (input, "type"))
264 return 0;
265
266 if (unformat (input, "1r2c"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000267 c->rfc = QOS_POLICER_TYPE_1R2C;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268 else if (unformat (input, "1r3c"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000269 c->rfc = QOS_POLICER_TYPE_1R3C_RFC_2697;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 else if (unformat (input, "2r3c-2698"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000271 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_2698;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 else if (unformat (input, "2r3c-4115"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000273 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_4115;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 else if (unformat (input, "2r3c-mef5cf1"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000275 c->rfc = QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 else
277 return 0;
278 return 1;
279}
280
281static uword
282unformat_policer_round_type (unformat_input_t * input, va_list * va)
283{
Brian Russellc5299ff2021-02-09 10:16:58 +0000284 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
Damjan Marion3891cd82016-10-27 10:27:00 +0200286 if (!unformat (input, "round"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 return 0;
288
Damjan Marion3891cd82016-10-27 10:27:00 +0200289 if (unformat (input, "closest"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000290 c->rnd_type = QOS_ROUND_TO_CLOSEST;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 else if (unformat (input, "up"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000292 c->rnd_type = QOS_ROUND_TO_UP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 else if (unformat (input, "down"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000294 c->rnd_type = QOS_ROUND_TO_DOWN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 else
296 return 0;
297 return 1;
298}
299
300static uword
301unformat_policer_rate_type (unformat_input_t * input, va_list * va)
302{
Brian Russellc5299ff2021-02-09 10:16:58 +0000303 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
Damjan Marion3891cd82016-10-27 10:27:00 +0200305 if (!unformat (input, "rate"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 return 0;
307
308 if (unformat (input, "kbps"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000309 c->rate_type = QOS_RATE_KBPS;
Damjan Marion3891cd82016-10-27 10:27:00 +0200310 else if (unformat (input, "pps"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000311 c->rate_type = QOS_RATE_PPS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 else
313 return 0;
314 return 1;
315}
316
317static uword
318unformat_policer_cir (unformat_input_t * input, va_list * va)
319{
Brian Russellc5299ff2021-02-09 10:16:58 +0000320 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
322 if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
323 return 1;
324 return 0;
325}
326
327static uword
328unformat_policer_eir (unformat_input_t * input, va_list * va)
329{
Brian Russellc5299ff2021-02-09 10:16:58 +0000330 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
332 if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
333 return 1;
334 return 0;
335}
336
337static uword
338unformat_policer_cb (unformat_input_t * input, va_list * va)
339{
Brian Russellc5299ff2021-02-09 10:16:58 +0000340 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341
342 if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
343 return 1;
344 return 0;
345}
346
347static uword
348unformat_policer_eb (unformat_input_t * input, va_list * va)
349{
Brian Russellc5299ff2021-02-09 10:16:58 +0000350 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351
352 if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
353 return 1;
354 return 0;
355}
356
Matus Fabian4ac74c92016-05-31 07:33:29 -0700357static uword
Matus Fabian4ac74c92016-05-31 07:33:29 -0700358unformat_policer_action_type (unformat_input_t * input, va_list * va)
359{
Brian Russellc5299ff2021-02-09 10:16:58 +0000360 qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *);
Matus Fabian4ac74c92016-05-31 07:33:29 -0700361
362 if (unformat (input, "drop"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000363 a->action_type = QOS_ACTION_DROP;
Matus Fabian4ac74c92016-05-31 07:33:29 -0700364 else if (unformat (input, "transmit"))
Brian Russellc5299ff2021-02-09 10:16:58 +0000365 a->action_type = QOS_ACTION_TRANSMIT;
Brian Russelle3845d72021-02-08 15:33:18 +0000366 else if (unformat (input, "mark-and-transmit %U", unformat_ip_dscp,
367 &a->dscp))
Brian Russellc5299ff2021-02-09 10:16:58 +0000368 a->action_type = QOS_ACTION_MARK_AND_TRANSMIT;
Matus Fabian4ac74c92016-05-31 07:33:29 -0700369 else
370 return 0;
371 return 1;
372}
373
374static uword
375unformat_policer_action (unformat_input_t * input, va_list * va)
376{
Brian Russellc5299ff2021-02-09 10:16:58 +0000377 qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *);
Matus Fabian4ac74c92016-05-31 07:33:29 -0700378
379 if (unformat (input, "conform-action %U", unformat_policer_action_type,
Damjan Marion3891cd82016-10-27 10:27:00 +0200380 &c->conform_action))
Matus Fabian4ac74c92016-05-31 07:33:29 -0700381 return 1;
382 else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
Damjan Marion3891cd82016-10-27 10:27:00 +0200383 &c->exceed_action))
Matus Fabian4ac74c92016-05-31 07:33:29 -0700384 return 1;
385 else if (unformat (input, "violate-action %U", unformat_policer_action_type,
Damjan Marion3891cd82016-10-27 10:27:00 +0200386 &c->violate_action))
Matus Fabian4ac74c92016-05-31 07:33:29 -0700387 return 1;
388 return 0;
389}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700391static uword
392unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
393{
Damjan Marion3891cd82016-10-27 10:27:00 +0200394 u32 *r = va_arg (*va, u32 *);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700395 vnet_policer_main_t *pm = &vnet_policer_main;
Damjan Marion3891cd82016-10-27 10:27:00 +0200396 uword *p;
397 u8 *match_name = 0;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700398
399 if (unformat (input, "%s", &match_name))
400 ;
401 else
402 return 0;
403
404 p = hash_get_mem (pm->policer_index_by_name, match_name);
405
406 if (p == 0)
407 return 0;
408
409 *r = p[0];
410
411 return 1;
412}
413
414static uword
415unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
416{
Damjan Marion3891cd82016-10-27 10:27:00 +0200417 u32 *r = va_arg (*va, u32 *);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700418
419 if (unformat (input, "conform-color"))
420 *r = POLICE_CONFORM;
421 else if (unformat (input, "exceed-color"))
422 *r = POLICE_EXCEED;
423 else
424 return 0;
425
426 return 1;
427}
428
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429#define foreach_config_param \
430_(eb) \
431_(cb) \
432_(eir) \
433_(cir) \
434_(rate_type) \
435_(round_type) \
Matus Fabian4ac74c92016-05-31 07:33:29 -0700436_(type) \
437_(action)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
439static clib_error_t *
440configure_policer_command_fn (vlib_main_t * vm,
Damjan Marion3891cd82016-10-27 10:27:00 +0200441 unformat_input_t * input,
442 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443{
Brian Russellc5299ff2021-02-09 10:16:58 +0000444 qos_pol_cfg_params_st c;
Damjan Marion3891cd82016-10-27 10:27:00 +0200445 unformat_input_t _line_input, *line_input = &_line_input;
Matus Fabian65fcd4d2016-05-13 05:44:48 -0700446 u8 is_add = 1;
Damjan Marion3891cd82016-10-27 10:27:00 +0200447 u8 *name = 0;
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700448 u32 pi;
Billy McFalla9a20e72017-02-15 11:39:12 -0500449 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
451 /* Get a line of input. */
Damjan Marion3891cd82016-10-27 10:27:00 +0200452 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 return 0;
454
Dave Barachb7b92992018-10-17 10:38:51 -0400455 clib_memset (&c, 0, sizeof (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
Damjan Marion3891cd82016-10-27 10:27:00 +0200457 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458 {
459 if (unformat (line_input, "del"))
Damjan Marion3891cd82016-10-27 10:27:00 +0200460 is_add = 0;
461 else if (unformat (line_input, "name %s", &name))
462 ;
463 else if (unformat (line_input, "color-aware"))
464 c.color_aware = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
466#define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
467 foreach_config_param
468#undef _
Damjan Marion3891cd82016-10-27 10:27:00 +0200469 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500470 {
471 error = clib_error_return (0, "unknown input `%U'",
472 format_unformat_error, line_input);
473 goto done;
474 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 }
476
Billy McFalla9a20e72017-02-15 11:39:12 -0500477 error = policer_add_del (vm, name, &c, &pi, is_add);
478
479done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 unformat_free (line_input);
481
Billy McFalla9a20e72017-02-15 11:39:12 -0500482 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483}
484
Damjan Marion3891cd82016-10-27 10:27:00 +0200485/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486VLIB_CLI_COMMAND (configure_policer_command, static) = {
487 .path = "configure policer",
488 .short_help = "configure policer name <name> <params> ",
489 .function = configure_policer_command_fn,
490};
Damjan Marion3891cd82016-10-27 10:27:00 +0200491/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492
493static clib_error_t *
494show_policer_command_fn (vlib_main_t * vm,
Damjan Marion3891cd82016-10-27 10:27:00 +0200495 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496{
497 vnet_policer_main_t *pm = &vnet_policer_main;
Damjan Marion3891cd82016-10-27 10:27:00 +0200498 hash_pair_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499 u32 pool_index;
Damjan Marion3891cd82016-10-27 10:27:00 +0200500 u8 *match_name = 0;
501 u8 *name;
Brian Russella71ed782021-01-27 11:34:33 +0000502 uword *pi;
Brian Russellc5299ff2021-02-09 10:16:58 +0000503 qos_pol_cfg_params_st *config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 policer_read_response_type_st *templ;
505
506 (void) unformat (input, "name %s", &match_name);
507
Damjan Marion3891cd82016-10-27 10:27:00 +0200508 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 hash_foreach_pair (p, pm->policer_config_by_name,
510 ({
511 name = (u8 *) p->key;
512 if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
513 {
Brian Russella71ed782021-01-27 11:34:33 +0000514 pi = hash_get_mem (pm->policer_index_by_name, name);
515
516 pool_index = p->value[0];
517 config = pool_elt_at_index (pm->configs, pool_index);
518 templ = pool_elt_at_index (pm->policer_templates, pool_index);
519 vlib_cli_output (vm, "Name \"%s\" %U ", name, format_policer_config,
520 config);
521 vlib_cli_output (vm, "Template %U", format_policer_instance, templ,
522 pi[0]);
523 vlib_cli_output (vm, "-----------");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 }
525 }));
Damjan Marion3891cd82016-10-27 10:27:00 +0200526 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 return 0;
528}
529
530
Damjan Marion3891cd82016-10-27 10:27:00 +0200531/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532VLIB_CLI_COMMAND (show_policer_command, static) = {
533 .path = "show policer",
534 .short_help = "show policer [name]",
535 .function = show_policer_command_fn,
536};
Damjan Marion3891cd82016-10-27 10:27:00 +0200537/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538
Chaoyu Jin913b8732017-08-08 13:36:23 -0700539static clib_error_t *
540show_policer_pools_command_fn (vlib_main_t * vm,
541 unformat_input_t * input,
542 vlib_cli_command_t * cmd)
543{
544 vnet_policer_main_t *pm = &vnet_policer_main;
545
546 vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
547 pool_elts (pm->configs),
548 pool_elts (pm->policer_templates),
549 pool_elts (pm->policers));
550 return 0;
551}
552/* *INDENT-OFF* */
553VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
554 .path = "show policer pools",
555 .short_help = "show policer pools",
556 .function = show_policer_pools_command_fn,
557};
558/* *INDENT-ON* */
559
Damjan Marion3891cd82016-10-27 10:27:00 +0200560clib_error_t *
561policer_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562{
Damjan Marion3891cd82016-10-27 10:27:00 +0200563 vnet_policer_main_t *pm = &vnet_policer_main;
564 void vnet_policer_node_funcs_reference (void);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Damjan Marion3891cd82016-10-27 10:27:00 +0200566 vnet_policer_node_funcs_reference ();
567
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568 pm->vlib_main = vm;
Damjan Marion3891cd82016-10-27 10:27:00 +0200569 pm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
571 pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700572 pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
573
574 vnet_classify_register_unformat_policer_next_index_fn
575 (unformat_policer_classify_next_index);
576 vnet_classify_register_unformat_opaque_index_fn
577 (unformat_policer_classify_precolor);
578
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579 return 0;
580}
581
Damjan Marion3891cd82016-10-27 10:27:00 +0200582VLIB_INIT_FUNCTION (policer_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
584
Damjan Marion3891cd82016-10-27 10:27:00 +0200585
586/*
587 * fd.io coding-style-patch-verification: ON
588 *
589 * Local Variables:
590 * eval: (c-set-style "gnu")
591 * End:
592 */