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