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