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