37380034fd2f5b7f4dbf464f378c7eea6e6cfb70
[vpp.git] / src / vnet / policer / policer.c
1 /*
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  */
15 #include <stdint.h>
16 #include <vnet/policer/policer.h>
17 #include <vnet/classify/vnet_classify.h>
18
19 vnet_policer_main_t vnet_policer_main;
20
21 clib_error_t *
22 policer_add_del (vlib_main_t * vm,
23                  u8 * name,
24                  sse2_qos_pol_cfg_params_st * cfg,
25                  u32 * policer_index, u8 is_add)
26 {
27   vnet_policer_main_t *pm = &vnet_policer_main;
28   policer_read_response_type_st test_policer;
29   policer_read_response_type_st *policer;
30   uword *p;
31   u32 pi;
32   int rv;
33
34   p = hash_get_mem (pm->policer_config_by_name, name);
35
36   if (is_add == 0)
37     {
38       /* free policer config and template */
39       if (p == 0)
40         {
41           vec_free (name);
42           return clib_error_return (0, "No such policer configuration");
43         }
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);
47
48       /* free policer */
49       p = hash_get_mem (pm->policer_index_by_name, name);
50       if (p == 0)
51         {
52           vec_free (name);
53           return clib_error_return (0, "No such policer");
54         }
55       pool_put_index (pm->policers, p[0]);
56       hash_unset_mem (pm->policer_index_by_name, name);
57
58       vec_free (name);
59       return 0;
60     }
61
62   if (p != 0)
63     {
64       vec_free (name);
65       return clib_error_return (0, "Policer already exists");
66     }
67
68   /* Vet the configuration before adding it to the table */
69   rv = sse2_pol_logical_2_physical (cfg, &test_policer);
70
71   if (rv == 0)
72     {
73       policer_read_response_type_st *pp;
74       sse2_qos_pol_cfg_params_st *cp;
75
76       pool_get (pm->configs, cp);
77       pool_get (pm->policer_templates, pp);
78
79       ASSERT (cp - pm->configs == pp - pm->policer_templates);
80
81       clib_memcpy (cp, cfg, sizeof (*cp));
82       clib_memcpy (pp, &test_policer, sizeof (*pp));
83
84       hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
85       pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
86       policer[0] = pp[0];
87       pi = policer - pm->policers;
88       hash_set_mem (pm->policer_index_by_name, name, pi);
89       *policer_index = pi;
90     }
91   else
92     {
93       vec_free (name);
94       return clib_error_return (0, "Config failed sanity check");
95     }
96
97   return 0;
98 }
99
100 u8 *
101 format_policer_instance (u8 * s, va_list * va)
102 {
103   policer_read_response_type_st *i
104     = va_arg (*va, policer_read_response_type_st *);
105
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",
112               i->current_limit,
113               i->current_bucket, i->extended_limit, i->extended_bucket);
114   s = format (s, "last update %llu\n", i->last_update_time);
115   return s;
116 }
117
118 static u8 *
119 format_policer_round_type (u8 * s, va_list * va)
120 {
121   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
122
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");
129   else
130     s = format (s, "ILLEGAL");
131   return s;
132 }
133
134
135 static u8 *
136 format_policer_rate_type (u8 * s, va_list * va)
137 {
138   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
139
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");
144   else
145     s = format (s, "ILLEGAL");
146   return s;
147 }
148
149 static u8 *
150 format_policer_type (u8 * s, va_list * va)
151 {
152   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
153
154   if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
155     s = format (s, "1r2c");
156
157   else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
158     s = format (s, "1r3c");
159
160   else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
161     s = format (s, "2r3c-2698");
162
163   else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
164     s = format (s, "2r3c-4115");
165
166   else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
167     s = format (s, "2r3c-mef5cf1");
168   else
169     s = format (s, "ILLEGAL");
170   return s;
171 }
172
173 static u8 *
174 format_dscp (u8 * s, va_list * va)
175 {
176   u32 i = va_arg (*va, u32);
177   char *t = 0;
178
179   switch (i)
180     {
181 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
182       foreach_vnet_dscp
183 #undef _
184     default:
185       return format (s, "ILLEGAL");
186     }
187   s = format (s, "%s", t);
188   return s;
189 }
190
191 static u8 *
192 format_policer_action_type (u8 * s, va_list * va)
193 {
194   sse2_qos_pol_action_params_st *a
195     = va_arg (*va, sse2_qos_pol_action_params_st *);
196
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);
203   else
204     s = format (s, "ILLEGAL");
205   return s;
206 }
207
208 u8 *
209 format_policer_config (u8 * s, va_list * va)
210 {
211   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
212
213   s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
214               format_policer_type, c,
215               c->rb.kbps.cir_kbps,
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);
223   return s;
224 }
225
226 static uword
227 unformat_policer_type (unformat_input_t * input, va_list * va)
228 {
229   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
230
231   if (!unformat (input, "type"))
232     return 0;
233
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;
244   else
245     return 0;
246   return 1;
247 }
248
249 static uword
250 unformat_policer_round_type (unformat_input_t * input, va_list * va)
251 {
252   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
253
254   if (!unformat (input, "round"))
255     return 0;
256
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;
263   else
264     return 0;
265   return 1;
266 }
267
268 static uword
269 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
270 {
271   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
272
273   if (!unformat (input, "rate"))
274     return 0;
275
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;
280   else
281     return 0;
282   return 1;
283 }
284
285 static uword
286 unformat_policer_cir (unformat_input_t * input, va_list * va)
287 {
288   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
289
290   if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
291     return 1;
292   return 0;
293 }
294
295 static uword
296 unformat_policer_eir (unformat_input_t * input, va_list * va)
297 {
298   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
299
300   if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
301     return 1;
302   return 0;
303 }
304
305 static uword
306 unformat_policer_cb (unformat_input_t * input, va_list * va)
307 {
308   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
309
310   if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
311     return 1;
312   return 0;
313 }
314
315 static uword
316 unformat_policer_eb (unformat_input_t * input, va_list * va)
317 {
318   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
319
320   if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
321     return 1;
322   return 0;
323 }
324
325 static uword
326 unformat_dscp (unformat_input_t * input, va_list * va)
327 {
328   u8 *r = va_arg (*va, u8 *);
329
330   if (0);
331 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
332   foreach_vnet_dscp
333 #undef _
334     else
335     return 0;
336   return 1;
337 }
338
339 static uword
340 unformat_policer_action_type (unformat_input_t * input, va_list * va)
341 {
342   sse2_qos_pol_action_params_st *a
343     = va_arg (*va, sse2_qos_pol_action_params_st *);
344
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;
351   else
352     return 0;
353   return 1;
354 }
355
356 static uword
357 unformat_policer_action (unformat_input_t * input, va_list * va)
358 {
359   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
360
361   if (unformat (input, "conform-action %U", unformat_policer_action_type,
362                 &c->conform_action))
363     return 1;
364   else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
365                      &c->exceed_action))
366     return 1;
367   else if (unformat (input, "violate-action %U", unformat_policer_action_type,
368                      &c->violate_action))
369     return 1;
370   return 0;
371 }
372
373 static uword
374 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
375 {
376   u32 *r = va_arg (*va, u32 *);
377   vnet_policer_main_t *pm = &vnet_policer_main;
378   uword *p;
379   u8 *match_name = 0;
380
381   if (unformat (input, "%s", &match_name))
382     ;
383   else
384     return 0;
385
386   p = hash_get_mem (pm->policer_index_by_name, match_name);
387
388   if (p == 0)
389     return 0;
390
391   *r = p[0];
392
393   return 1;
394 }
395
396 static uword
397 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
398 {
399   u32 *r = va_arg (*va, u32 *);
400
401   if (unformat (input, "conform-color"))
402     *r = POLICE_CONFORM;
403   else if (unformat (input, "exceed-color"))
404     *r = POLICE_EXCEED;
405   else
406     return 0;
407
408   return 1;
409 }
410
411 #define foreach_config_param                    \
412 _(eb)                                           \
413 _(cb)                                           \
414 _(eir)                                          \
415 _(cir)                                          \
416 _(rate_type)                                    \
417 _(round_type)                                   \
418 _(type)                                         \
419 _(action)
420
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)
425 {
426   sse2_qos_pol_cfg_params_st c;
427   unformat_input_t _line_input, *line_input = &_line_input;
428   u8 is_add = 1;
429   u8 *name = 0;
430   u32 pi;
431   clib_error_t *error = NULL;
432
433   /* Get a line of input. */
434   if (!unformat_user (input, unformat_line_input, line_input))
435     return 0;
436
437   memset (&c, 0, sizeof (c));
438
439   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
440     {
441       if (unformat (line_input, "del"))
442         is_add = 0;
443       else if (unformat (line_input, "name %s", &name))
444         ;
445       else if (unformat (line_input, "color-aware"))
446         c.color_aware = 1;
447
448 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
449       foreach_config_param
450 #undef _
451         else
452         {
453           error = clib_error_return (0, "unknown input `%U'",
454                                      format_unformat_error, line_input);
455           goto done;
456         }
457     }
458
459   error = policer_add_del (vm, name, &c, &pi, is_add);
460
461 done:
462   unformat_free (line_input);
463
464   return error;
465 }
466
467 /* *INDENT-OFF* */
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,
472 };
473 /* *INDENT-ON* */
474
475 static clib_error_t *
476 show_policer_command_fn (vlib_main_t * vm,
477                          unformat_input_t * input, vlib_cli_command_t * cmd)
478 {
479   vnet_policer_main_t *pm = &vnet_policer_main;
480   hash_pair_t *p;
481   u32 pool_index;
482   u8 *match_name = 0;
483   u8 *name;
484   sse2_qos_pol_cfg_params_st *config;
485   policer_read_response_type_st *templ;
486
487   (void) unformat (input, "name %s", &match_name);
488
489   /* *INDENT-OFF* */
490   hash_foreach_pair (p, pm->policer_config_by_name,
491   ({
492     name = (u8 *) p->key;
493     if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
494       {
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, "-----------");
503       }
504   }));
505   /* *INDENT-ON* */
506   return 0;
507 }
508
509
510 /* *INDENT-OFF* */
511 VLIB_CLI_COMMAND (show_policer_command, static) = {
512     .path = "show policer",
513     .short_help = "show policer [name]",
514     .function = show_policer_command_fn,
515 };
516 /* *INDENT-ON* */
517
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)
522 {
523   vnet_policer_main_t *pm = &vnet_policer_main;
524
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));
529   return 0;
530 }
531 /* *INDENT-OFF* */
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,
536 };
537 /* *INDENT-ON* */
538
539 clib_error_t *
540 policer_init (vlib_main_t * vm)
541 {
542   vnet_policer_main_t *pm = &vnet_policer_main;
543   void vnet_policer_node_funcs_reference (void);
544
545   vnet_policer_node_funcs_reference ();
546
547   pm->vlib_main = vm;
548   pm->vnet_main = vnet_get_main ();
549
550   pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
551   pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
552
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);
557
558   return 0;
559 }
560
561 VLIB_INIT_FUNCTION (policer_init);
562
563
564
565 /*
566  * fd.io coding-style-patch-verification: ON
567  *
568  * Local Variables:
569  * eval: (c-set-style "gnu")
570  * End:
571  */