policer: add counters
[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/policer/police_inlines.h>
18 #include <vnet/classify/vnet_classify.h>
19
20 vnet_policer_main_t vnet_policer_main;
21
22 u8 *
23 format_policer_handoff_trace (u8 *s, va_list *args)
24 {
25   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
26   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
27   policer_handoff_trace_t *t = va_arg (*args, policer_handoff_trace_t *);
28
29   s = format (s, "policer %d, handoff thread %d to %d", t->policer_index,
30               t->current_worker_index, t->next_worker_index);
31
32   return s;
33 }
34
35 vlib_combined_counter_main_t policer_counters[] = {
36   {
37     .name = "Policer-Conform",
38     .stat_segment_name = "/net/policer/conform",
39   },
40   {
41     .name = "Policer-Exceed",
42     .stat_segment_name = "/net/policer/exceed",
43   },
44   {
45     .name = "Policer-Violate",
46     .stat_segment_name = "/net/policer/violate",
47   },
48 };
49
50 clib_error_t *
51 policer_add_del (vlib_main_t * vm,
52                  u8 * name,
53                  sse2_qos_pol_cfg_params_st * cfg,
54                  u32 * policer_index, u8 is_add)
55 {
56   vnet_policer_main_t *pm = &vnet_policer_main;
57   policer_read_response_type_st test_policer;
58   policer_read_response_type_st *policer;
59   uword *p;
60   u32 pi;
61   int rv;
62
63   p = hash_get_mem (pm->policer_config_by_name, name);
64
65   if (is_add == 0)
66     {
67       /* free policer config and template */
68       if (p == 0)
69         {
70           vec_free (name);
71           return clib_error_return (0, "No such policer configuration");
72         }
73       pool_put_index (pm->configs, p[0]);
74       pool_put_index (pm->policer_templates, p[0]);
75       hash_unset_mem (pm->policer_config_by_name, name);
76
77       /* free policer */
78       p = hash_get_mem (pm->policer_index_by_name, name);
79       if (p == 0)
80         {
81           vec_free (name);
82           return clib_error_return (0, "No such policer");
83         }
84       pool_put_index (pm->policers, p[0]);
85       hash_unset_mem (pm->policer_index_by_name, name);
86
87       vec_free (name);
88       return 0;
89     }
90
91   if (p != 0)
92     {
93       vec_free (name);
94       return clib_error_return (0, "Policer already exists");
95     }
96
97   /* Vet the configuration before adding it to the table */
98   rv = sse2_pol_logical_2_physical (cfg, &test_policer);
99
100   if (rv == 0)
101     {
102       policer_read_response_type_st *pp;
103       sse2_qos_pol_cfg_params_st *cp;
104       int i;
105
106       pool_get (pm->configs, cp);
107       pool_get (pm->policer_templates, pp);
108
109       ASSERT (cp - pm->configs == pp - pm->policer_templates);
110
111       clib_memcpy (cp, cfg, sizeof (*cp));
112       clib_memcpy (pp, &test_policer, sizeof (*pp));
113
114       hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs);
115       pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES);
116       policer[0] = pp[0];
117       pi = policer - pm->policers;
118       hash_set_mem (pm->policer_index_by_name, name, pi);
119       *policer_index = pi;
120       policer->thread_index = ~0;
121
122       for (i = 0; i < NUM_POLICE_RESULTS; i++)
123         {
124           vlib_validate_combined_counter (&policer_counters[i], pi);
125           vlib_zero_combined_counter (&policer_counters[i], pi);
126         }
127     }
128   else
129     {
130       vec_free (name);
131       return clib_error_return (0, "Config failed sanity check");
132     }
133
134   return 0;
135 }
136
137 u8 *
138 format_policer_instance (u8 * s, va_list * va)
139 {
140   policer_read_response_type_st *i
141     = va_arg (*va, policer_read_response_type_st *);
142   uword pi = va_arg (*va, uword);
143   int result;
144   vlib_counter_t counts[NUM_POLICE_RESULTS];
145
146   for (result = 0; result < NUM_POLICE_RESULTS; result++)
147     {
148       vlib_get_combined_counter (&policer_counters[result], pi,
149                                  &counts[result]);
150     }
151
152   s = format (s, "policer at %llx: %s rate, %s color-aware\n",
153               i, i->single_rate ? "single" : "dual",
154               i->color_aware ? "is" : "not");
155   s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n",
156               i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale);
157   s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n",
158               i->current_limit,
159               i->current_bucket, i->extended_limit, i->extended_bucket);
160   s = format (s, "last update %llu\n", i->last_update_time);
161   s = format (s, "conform %llu packets, %llu bytes\n",
162               counts[POLICE_CONFORM].packets, counts[POLICE_CONFORM].bytes);
163   s = format (s, "exceed %llu packets, %llu bytes\n",
164               counts[POLICE_EXCEED].packets, counts[POLICE_EXCEED].bytes);
165   s = format (s, "violate %llu packets, %llu bytes\n",
166               counts[POLICE_VIOLATE].packets, counts[POLICE_VIOLATE].bytes);
167   return s;
168 }
169
170 static u8 *
171 format_policer_round_type (u8 * s, va_list * va)
172 {
173   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
174
175   if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST)
176     s = format (s, "closest");
177   else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP)
178     s = format (s, "up");
179   else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN)
180     s = format (s, "down");
181   else
182     s = format (s, "ILLEGAL");
183   return s;
184 }
185
186
187 static u8 *
188 format_policer_rate_type (u8 * s, va_list * va)
189 {
190   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
191
192   if (c->rate_type == SSE2_QOS_RATE_KBPS)
193     s = format (s, "kbps");
194   else if (c->rate_type == SSE2_QOS_RATE_PPS)
195     s = format (s, "pps");
196   else
197     s = format (s, "ILLEGAL");
198   return s;
199 }
200
201 static u8 *
202 format_policer_type (u8 * s, va_list * va)
203 {
204   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
205
206   if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C)
207     s = format (s, "1r2c");
208
209   else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)
210     s = format (s, "1r3c");
211
212   else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)
213     s = format (s, "2r3c-2698");
214
215   else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)
216     s = format (s, "2r3c-4115");
217
218   else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
219     s = format (s, "2r3c-mef5cf1");
220   else
221     s = format (s, "ILLEGAL");
222   return s;
223 }
224
225 static u8 *
226 format_dscp (u8 * s, va_list * va)
227 {
228   u32 i = va_arg (*va, u32);
229   char *t = 0;
230
231   switch (i)
232     {
233 #define _(v,f,str) case VNET_DSCP_##f: t = str; break;
234       foreach_vnet_dscp
235 #undef _
236     default:
237       return format (s, "ILLEGAL");
238     }
239   s = format (s, "%s", t);
240   return s;
241 }
242
243 static u8 *
244 format_policer_action_type (u8 * s, va_list * va)
245 {
246   sse2_qos_pol_action_params_st *a
247     = va_arg (*va, sse2_qos_pol_action_params_st *);
248
249   if (a->action_type == SSE2_QOS_ACTION_DROP)
250     s = format (s, "drop");
251   else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
252     s = format (s, "transmit");
253   else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
254     s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
255   else
256     s = format (s, "ILLEGAL");
257   return s;
258 }
259
260 u8 *
261 format_policer_config (u8 * s, va_list * va)
262 {
263   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
264
265   s = format (s, "type %U cir %u eir %u cb %u eb %u\n",
266               format_policer_type, c,
267               c->rb.kbps.cir_kbps,
268               c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes);
269   s = format (s, "rate type %U, round type %U\n",
270               format_policer_rate_type, c, format_policer_round_type, c);
271   s = format (s, "conform action %U, exceed action %U, violate action %U\n",
272               format_policer_action_type, &c->conform_action,
273               format_policer_action_type, &c->exceed_action,
274               format_policer_action_type, &c->violate_action);
275   return s;
276 }
277
278 static uword
279 unformat_policer_type (unformat_input_t * input, va_list * va)
280 {
281   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
282
283   if (!unformat (input, "type"))
284     return 0;
285
286   if (unformat (input, "1r2c"))
287     c->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
288   else if (unformat (input, "1r3c"))
289     c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
290   else if (unformat (input, "2r3c-2698"))
291     c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
292   else if (unformat (input, "2r3c-4115"))
293     c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
294   else if (unformat (input, "2r3c-mef5cf1"))
295     c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
296   else
297     return 0;
298   return 1;
299 }
300
301 static uword
302 unformat_policer_round_type (unformat_input_t * input, va_list * va)
303 {
304   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
305
306   if (!unformat (input, "round"))
307     return 0;
308
309   if (unformat (input, "closest"))
310     c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST;
311   else if (unformat (input, "up"))
312     c->rnd_type = SSE2_QOS_ROUND_TO_UP;
313   else if (unformat (input, "down"))
314     c->rnd_type = SSE2_QOS_ROUND_TO_DOWN;
315   else
316     return 0;
317   return 1;
318 }
319
320 static uword
321 unformat_policer_rate_type (unformat_input_t * input, va_list * va)
322 {
323   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
324
325   if (!unformat (input, "rate"))
326     return 0;
327
328   if (unformat (input, "kbps"))
329     c->rate_type = SSE2_QOS_RATE_KBPS;
330   else if (unformat (input, "pps"))
331     c->rate_type = SSE2_QOS_RATE_PPS;
332   else
333     return 0;
334   return 1;
335 }
336
337 static uword
338 unformat_policer_cir (unformat_input_t * input, va_list * va)
339 {
340   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
341
342   if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps))
343     return 1;
344   return 0;
345 }
346
347 static uword
348 unformat_policer_eir (unformat_input_t * input, va_list * va)
349 {
350   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
351
352   if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps))
353     return 1;
354   return 0;
355 }
356
357 static uword
358 unformat_policer_cb (unformat_input_t * input, va_list * va)
359 {
360   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
361
362   if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes))
363     return 1;
364   return 0;
365 }
366
367 static uword
368 unformat_policer_eb (unformat_input_t * input, va_list * va)
369 {
370   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
371
372   if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes))
373     return 1;
374   return 0;
375 }
376
377 static uword
378 unformat_dscp (unformat_input_t * input, va_list * va)
379 {
380   u8 *r = va_arg (*va, u8 *);
381
382   if (0);
383 #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
384   foreach_vnet_dscp
385 #undef _
386     else
387     return 0;
388   return 1;
389 }
390
391 static uword
392 unformat_policer_action_type (unformat_input_t * input, va_list * va)
393 {
394   sse2_qos_pol_action_params_st *a
395     = va_arg (*va, sse2_qos_pol_action_params_st *);
396
397   if (unformat (input, "drop"))
398     a->action_type = SSE2_QOS_ACTION_DROP;
399   else if (unformat (input, "transmit"))
400     a->action_type = SSE2_QOS_ACTION_TRANSMIT;
401   else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
402     a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
403   else
404     return 0;
405   return 1;
406 }
407
408 static uword
409 unformat_policer_action (unformat_input_t * input, va_list * va)
410 {
411   sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *);
412
413   if (unformat (input, "conform-action %U", unformat_policer_action_type,
414                 &c->conform_action))
415     return 1;
416   else if (unformat (input, "exceed-action %U", unformat_policer_action_type,
417                      &c->exceed_action))
418     return 1;
419   else if (unformat (input, "violate-action %U", unformat_policer_action_type,
420                      &c->violate_action))
421     return 1;
422   return 0;
423 }
424
425 static uword
426 unformat_policer_classify_next_index (unformat_input_t * input, va_list * va)
427 {
428   u32 *r = va_arg (*va, u32 *);
429   vnet_policer_main_t *pm = &vnet_policer_main;
430   uword *p;
431   u8 *match_name = 0;
432
433   if (unformat (input, "%s", &match_name))
434     ;
435   else
436     return 0;
437
438   p = hash_get_mem (pm->policer_index_by_name, match_name);
439
440   if (p == 0)
441     return 0;
442
443   *r = p[0];
444
445   return 1;
446 }
447
448 static uword
449 unformat_policer_classify_precolor (unformat_input_t * input, va_list * va)
450 {
451   u32 *r = va_arg (*va, u32 *);
452
453   if (unformat (input, "conform-color"))
454     *r = POLICE_CONFORM;
455   else if (unformat (input, "exceed-color"))
456     *r = POLICE_EXCEED;
457   else
458     return 0;
459
460   return 1;
461 }
462
463 #define foreach_config_param                    \
464 _(eb)                                           \
465 _(cb)                                           \
466 _(eir)                                          \
467 _(cir)                                          \
468 _(rate_type)                                    \
469 _(round_type)                                   \
470 _(type)                                         \
471 _(action)
472
473 static clib_error_t *
474 configure_policer_command_fn (vlib_main_t * vm,
475                               unformat_input_t * input,
476                               vlib_cli_command_t * cmd)
477 {
478   sse2_qos_pol_cfg_params_st c;
479   unformat_input_t _line_input, *line_input = &_line_input;
480   u8 is_add = 1;
481   u8 *name = 0;
482   u32 pi;
483   clib_error_t *error = NULL;
484
485   /* Get a line of input. */
486   if (!unformat_user (input, unformat_line_input, line_input))
487     return 0;
488
489   clib_memset (&c, 0, sizeof (c));
490
491   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
492     {
493       if (unformat (line_input, "del"))
494         is_add = 0;
495       else if (unformat (line_input, "name %s", &name))
496         ;
497       else if (unformat (line_input, "color-aware"))
498         c.color_aware = 1;
499
500 #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ;
501       foreach_config_param
502 #undef _
503         else
504         {
505           error = clib_error_return (0, "unknown input `%U'",
506                                      format_unformat_error, line_input);
507           goto done;
508         }
509     }
510
511   error = policer_add_del (vm, name, &c, &pi, is_add);
512
513 done:
514   unformat_free (line_input);
515
516   return error;
517 }
518
519 /* *INDENT-OFF* */
520 VLIB_CLI_COMMAND (configure_policer_command, static) = {
521     .path = "configure policer",
522     .short_help = "configure policer name <name> <params> ",
523     .function = configure_policer_command_fn,
524 };
525 /* *INDENT-ON* */
526
527 static clib_error_t *
528 show_policer_command_fn (vlib_main_t * vm,
529                          unformat_input_t * input, vlib_cli_command_t * cmd)
530 {
531   vnet_policer_main_t *pm = &vnet_policer_main;
532   hash_pair_t *p;
533   u32 pool_index;
534   u8 *match_name = 0;
535   u8 *name;
536   uword *pi;
537   sse2_qos_pol_cfg_params_st *config;
538   policer_read_response_type_st *templ;
539
540   (void) unformat (input, "name %s", &match_name);
541
542   /* *INDENT-OFF* */
543   hash_foreach_pair (p, pm->policer_config_by_name,
544   ({
545     name = (u8 *) p->key;
546     if (match_name == 0 || !strcmp((char *) name, (char *) match_name))
547       {
548         pi = hash_get_mem (pm->policer_index_by_name, name);
549
550         pool_index = p->value[0];
551         config = pool_elt_at_index (pm->configs, pool_index);
552         templ = pool_elt_at_index (pm->policer_templates, pool_index);
553         vlib_cli_output (vm, "Name \"%s\" %U ", name, format_policer_config,
554                          config);
555         vlib_cli_output (vm, "Template %U", format_policer_instance, templ,
556                          pi[0]);
557         vlib_cli_output (vm, "-----------");
558       }
559   }));
560   /* *INDENT-ON* */
561   return 0;
562 }
563
564
565 /* *INDENT-OFF* */
566 VLIB_CLI_COMMAND (show_policer_command, static) = {
567     .path = "show policer",
568     .short_help = "show policer [name]",
569     .function = show_policer_command_fn,
570 };
571 /* *INDENT-ON* */
572
573 static clib_error_t *
574 show_policer_pools_command_fn (vlib_main_t * vm,
575                                unformat_input_t * input,
576                                vlib_cli_command_t * cmd)
577 {
578   vnet_policer_main_t *pm = &vnet_policer_main;
579
580   vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d",
581                    pool_elts (pm->configs),
582                    pool_elts (pm->policer_templates),
583                    pool_elts (pm->policers));
584   return 0;
585 }
586 /* *INDENT-OFF* */
587 VLIB_CLI_COMMAND (show_policer_pools_command, static) = {
588     .path = "show policer pools",
589     .short_help = "show policer pools",
590     .function = show_policer_pools_command_fn,
591 };
592 /* *INDENT-ON* */
593
594 clib_error_t *
595 policer_init (vlib_main_t * vm)
596 {
597   vnet_policer_main_t *pm = &vnet_policer_main;
598   void vnet_policer_node_funcs_reference (void);
599
600   vnet_policer_node_funcs_reference ();
601
602   pm->vlib_main = vm;
603   pm->vnet_main = vnet_get_main ();
604
605   pm->policer_config_by_name = hash_create_string (0, sizeof (uword));
606   pm->policer_index_by_name = hash_create_string (0, sizeof (uword));
607
608   vnet_classify_register_unformat_policer_next_index_fn
609     (unformat_policer_classify_next_index);
610   vnet_classify_register_unformat_opaque_index_fn
611     (unformat_policer_classify_precolor);
612
613   return 0;
614 }
615
616 VLIB_INIT_FUNCTION (policer_init);
617
618
619
620 /*
621  * fd.io coding-style-patch-verification: ON
622  *
623  * Local Variables:
624  * eval: (c-set-style "gnu")
625  * End:
626  */