FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
node_funcs.c
Go to the documentation of this file.
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 
16 #include <stdint.h>
17 
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/policer/policer.h>
21 #include <vnet/ip/ip.h>
24 
25 #define IP4_NON_DSCP_BITS 0x03
26 #define IP4_DSCP_SHIFT 2
27 #define IP6_NON_DSCP_BITS 0xf03fffff
28 #define IP6_DSCP_SHIFT 22
29 
30 /* Dispatch functions meant to be instantiated elsewhere */
31 
32 typedef struct
33 {
38 
39 /* packet trace format function */
40 static u8 *
41 format_policer_trace (u8 * s, va_list * args)
42 {
43  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
44  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
45  vnet_policer_trace_t *t = va_arg (*args, vnet_policer_trace_t *);
46 
47  s = format (s, "VNET_POLICER: sw_if_index %d policer_index %d next %d",
49  return s;
50 }
51 
52 #define foreach_vnet_policer_error \
53 _(TRANSMIT, "Packets Transmitted") \
54 _(DROP, "Packets Dropped")
55 
56 typedef enum
57 {
58 #define _(sym,str) VNET_POLICER_ERROR_##sym,
60 #undef _
63 
64 static char *vnet_policer_error_strings[] = {
65 #define _(sym,string) string,
67 #undef _
68 };
69 
72 {
74  ip4_header_t *ip4h;
75  ip6_header_t *ip6h;
76  u16 type;
77 
78  eh = (ethernet_header_t *) b->data;
79  type = clib_net_to_host_u16 (eh->type);
80 
81  if (PREDICT_TRUE (type == ETHERNET_TYPE_IP4))
82  {
83  ip4h = (ip4_header_t *) & (b->data[sizeof (ethernet_header_t)]);;
84  ip4h->tos &= IP4_NON_DSCP_BITS;
85  ip4h->tos |= dscp << IP4_DSCP_SHIFT;
86  ip4h->checksum = ip4_header_checksum (ip4h);
87  }
88  else
89  {
90  if (PREDICT_TRUE (type == ETHERNET_TYPE_IP6))
91  {
92  ip6h = (ip6_header_t *) & (b->data[sizeof (ethernet_header_t)]);
94  clib_host_to_net_u32 (IP6_NON_DSCP_BITS);
96  clib_host_to_net_u32 (dscp << IP6_DSCP_SHIFT);
97  }
98  }
99 }
100 
103  vlib_buffer_t * b,
104  u32 policer_index,
105  u64 time_in_policer_periods,
106  policer_result_e packet_color)
107 {
108  u8 act;
109  u32 len;
110  u32 col;
113 
114  len = vlib_buffer_length_in_chain (vm, b);
115  pol = &pm->policers[policer_index];
116  col = vnet_police_packet (pol, len, packet_color, time_in_policer_periods);
117  act = pol->action[col];
119  vnet_policer_mark (b, pol->mark_dscp[col]);
120 
121  return act;
122 }
123 
124 static inline uword
126  vlib_node_runtime_t * node,
127  vlib_frame_t * frame, vnet_policer_index_t which)
128 {
129  u32 n_left_from, *from, *to_next;
130  vnet_policer_next_t next_index;
132  u64 time_in_policer_periods;
133  u32 transmitted = 0;
134 
135  time_in_policer_periods =
137 
138  from = vlib_frame_vector_args (frame);
139  n_left_from = frame->n_vectors;
140  next_index = node->cached_next_index;
141 
142  while (n_left_from > 0)
143  {
144  u32 n_left_to_next;
145 
146  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
147 
148  while (n_left_from >= 4 && n_left_to_next >= 2)
149  {
150  u32 bi0, bi1;
151  vlib_buffer_t *b0, *b1;
152  u32 next0, next1;
153  u32 sw_if_index0, sw_if_index1;
154  u32 pi0 = 0, pi1 = 0;
155  u8 act0, act1;
156 
157  /* Prefetch next iteration. */
158  {
159  vlib_buffer_t *b2, *b3;
160 
161  b2 = vlib_get_buffer (vm, from[2]);
162  b3 = vlib_get_buffer (vm, from[3]);
163 
164  vlib_prefetch_buffer_header (b2, LOAD);
165  vlib_prefetch_buffer_header (b3, LOAD);
166  }
167 
168  /* speculatively enqueue b0 and b1 to the current next frame */
169  to_next[0] = bi0 = from[0];
170  to_next[1] = bi1 = from[1];
171  from += 2;
172  to_next += 2;
173  n_left_from -= 2;
174  n_left_to_next -= 2;
175 
176  b0 = vlib_get_buffer (vm, bi0);
177  b1 = vlib_get_buffer (vm, bi1);
178 
179  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
181 
182  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
184 
185 
187  {
188  pi0 = pm->policer_index_by_sw_if_index[sw_if_index0];
189  pi1 = pm->policer_index_by_sw_if_index[sw_if_index1];
190  }
191 
192  if (which == VNET_POLICER_INDEX_BY_OPAQUE)
193  {
194  pi0 = vnet_buffer (b0)->policer.index;
195  pi1 = vnet_buffer (b1)->policer.index;
196  }
197 
198  if (which == VNET_POLICER_INDEX_BY_EITHER)
199  {
200  pi0 = vnet_buffer (b0)->policer.index;
201  pi0 = (pi0 != ~0) ? pi0 :
202  pm->policer_index_by_sw_if_index[sw_if_index0];
203  pi1 = vnet_buffer (b1)->policer.index;
204  pi1 = (pi1 != ~0) ? pi1 :
205  pm->policer_index_by_sw_if_index[sw_if_index1];
206  }
207 
208  act0 = vnet_policer_police (vm, b0, pi0, time_in_policer_periods,
209  POLICE_CONFORM /* no chaining */ );
210 
211  act1 = vnet_policer_police (vm, b1, pi1, time_in_policer_periods,
212  POLICE_CONFORM /* no chaining */ );
213 
214  if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP)) /* drop action */
215  {
216  next0 = VNET_POLICER_NEXT_DROP;
217  b0->error = node->errors[VNET_POLICER_ERROR_DROP];
218  }
219  else /* transmit or mark-and-transmit action */
220  {
221  transmitted++;
222  }
223 
224  if (PREDICT_FALSE (act1 == SSE2_QOS_ACTION_DROP)) /* drop action */
225  {
226  next1 = VNET_POLICER_NEXT_DROP;
227  b1->error = node->errors[VNET_POLICER_ERROR_DROP];
228  }
229  else /* transmit or mark-and-transmit action */
230  {
231  transmitted++;
232  }
233 
234 
235  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
236  {
237  if (b0->flags & VLIB_BUFFER_IS_TRACED)
238  {
240  vlib_add_trace (vm, node, b0, sizeof (*t));
241  t->sw_if_index = sw_if_index0;
242  t->next_index = next0;
243  }
244  if (b1->flags & VLIB_BUFFER_IS_TRACED)
245  {
247  vlib_add_trace (vm, node, b1, sizeof (*t));
248  t->sw_if_index = sw_if_index1;
249  t->next_index = next1;
250  }
251  }
252 
253  /* verify speculative enqueues, maybe switch current next frame */
254  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
255  to_next, n_left_to_next,
256  bi0, bi1, next0, next1);
257  }
258 
259  while (n_left_from > 0 && n_left_to_next > 0)
260  {
261  u32 bi0;
262  vlib_buffer_t *b0;
263  u32 next0;
264  u32 sw_if_index0;
265  u32 pi0 = 0;
266  u8 act0;
267 
268  bi0 = from[0];
269  to_next[0] = bi0;
270  from += 1;
271  to_next += 1;
272  n_left_from -= 1;
273  n_left_to_next -= 1;
274 
275  b0 = vlib_get_buffer (vm, bi0);
276 
277  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
279 
281  pi0 = pm->policer_index_by_sw_if_index[sw_if_index0];
282 
283  if (which == VNET_POLICER_INDEX_BY_OPAQUE)
284  pi0 = vnet_buffer (b0)->policer.index;
285 
286  if (which == VNET_POLICER_INDEX_BY_EITHER)
287  {
288  pi0 = vnet_buffer (b0)->policer.index;
289  pi0 = (pi0 != ~0) ? pi0 :
290  pm->policer_index_by_sw_if_index[sw_if_index0];
291  }
292 
293  act0 = vnet_policer_police (vm, b0, pi0, time_in_policer_periods,
294  POLICE_CONFORM /* no chaining */ );
295 
296  if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP)) /* drop action */
297  {
298  next0 = VNET_POLICER_NEXT_DROP;
299  b0->error = node->errors[VNET_POLICER_ERROR_DROP];
300  }
301  else /* transmit or mark-and-transmit action */
302  {
303  transmitted++;
304  }
305 
307  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
308  {
310  vlib_add_trace (vm, node, b0, sizeof (*t));
311  t->sw_if_index = sw_if_index0;
312  t->next_index = next0;
313  t->policer_index = pi0;
314  }
315 
316  /* verify speculative enqueue, maybe switch current next frame */
317  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
318  to_next, n_left_to_next,
319  bi0, next0);
320  }
321 
322  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
323  }
324 
326  VNET_POLICER_ERROR_TRANSMIT, transmitted);
327  return frame->n_vectors;
328 }
329 
330 uword
332  vlib_node_runtime_t * node, vlib_frame_t * frame)
333 {
334  return vnet_policer_inline (vm, node, frame,
336 }
337 
338 uword
340  vlib_node_runtime_t * node, vlib_frame_t * frame)
341 {
342  return vnet_policer_inline (vm, node, frame, VNET_POLICER_INDEX_BY_OPAQUE);
343 }
344 
345 uword
347  vlib_node_runtime_t * node, vlib_frame_t * frame)
348 {
349  return vnet_policer_inline (vm, node, frame, VNET_POLICER_INDEX_BY_EITHER);
350 }
351 
352 void
354 {
355 }
356 
357 
358 #define TEST_CODE 1
359 
360 #ifdef TEST_CODE
361 
362 /* *INDENT-OFF* */
364  .function = vnet_policer_by_sw_if_index,
365  .name = "policer-by-sw-if-index",
366  .vector_size = sizeof (u32),
367  .format_trace = format_policer_trace,
368  .type = VLIB_NODE_TYPE_INTERNAL,
369 
371  .error_strings = vnet_policer_error_strings,
372 
373  .n_next_nodes = VNET_POLICER_N_NEXT,
374 
375  /* edit / add dispositions here */
376  .next_nodes = {
377  [VNET_POLICER_NEXT_TRANSMIT] = "ethernet-input",
378  [VNET_POLICER_NEXT_DROP] = "error-drop",
379  },
380 };
381 
384 /* *INDENT-ON* */
385 
386 
387 int
388 test_policer_add_del (u32 rx_sw_if_index, u8 * config_name, int is_add)
389 {
393  vnet_hw_interface_t *rxhi;
394  uword *p;
395 
396  rxhi = vnet_get_sup_hw_interface (pm->vnet_main, rx_sw_if_index);
397 
398  /* Make sure caller didn't pass a vlan subif, etc. */
399  if (rxhi->sw_if_index != rx_sw_if_index)
400  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
401 
402  if (is_add)
403  {
404 
405  p = hash_get_mem (pm->policer_config_by_name, config_name);
406 
407  if (p == 0)
408  return -2;
409 
410  template = pool_elt_at_index (pm->policer_templates, p[0]);
411 
414 
416 
417  policer[0] = template[0];
418 
419  vec_validate (pm->policer_index_by_sw_if_index, rx_sw_if_index);
420  pm->policer_index_by_sw_if_index[rx_sw_if_index]
421  = policer - pm->policers;
422  }
423  else
424  {
425  u32 pi;
427  rxhi->hw_if_index,
428  ~0 /* disable */ );
429 
430  pi = pm->policer_index_by_sw_if_index[rx_sw_if_index];
431  pm->policer_index_by_sw_if_index[rx_sw_if_index] = ~0;
432  pool_put_index (pm->policers, pi);
433  }
434 
435  return 0;
436 }
437 
438 static clib_error_t *
440  unformat_input_t * input, vlib_cli_command_t * cmd)
441 {
443  unformat_input_t _line_input, *line_input = &_line_input;
444  u32 rx_sw_if_index;
445  int rv;
446  u8 *config_name = 0;
447  int rx_set = 0;
448  int is_add = 1;
449  int is_show = 0;
450  clib_error_t *error = NULL;
451 
452  /* Get a line of input. */
453  if (!unformat_user (input, unformat_line_input, line_input))
454  return 0;
455 
456  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
457  {
458  if (unformat (line_input, "intfc %U", unformat_vnet_sw_interface,
459  pm->vnet_main, &rx_sw_if_index))
460  rx_set = 1;
461  else if (unformat (line_input, "show"))
462  is_show = 1;
463  else if (unformat (line_input, "policer %s", &config_name))
464  ;
465  else if (unformat (line_input, "del"))
466  is_add = 0;
467  else
468  break;
469  }
470 
471  if (rx_set == 0)
472  {
473  error = clib_error_return (0, "interface not set");
474  goto done;
475  }
476 
477  if (is_show)
478  {
479  u32 pi = pm->policer_index_by_sw_if_index[rx_sw_if_index];
481  policer = pool_elt_at_index (pm->policers, pi);
482 
483  vlib_cli_output (vm, "%U", format_policer_instance, policer);
484  goto done;
485  }
486 
487  if (is_add && config_name == 0)
488  {
489  error = clib_error_return (0, "policer config name required");
490  goto done;
491  }
492 
493  rv = test_policer_add_del (rx_sw_if_index, config_name, is_add);
494 
495  switch (rv)
496  {
497  case 0:
498  break;
499 
500  default:
501  error = clib_error_return
502  (0, "WARNING: vnet_vnet_policer_add_del returned %d", rv);
503  goto done;
504  }
505 
506 done:
507  unformat_free (line_input);
508 
509  return error;
510 }
511 
512 /* *INDENT-OFF* */
513 VLIB_CLI_COMMAND (test_patch_command, static) = {
514  .path = "test policer",
515  .short_help =
516  "intfc <intfc> policer <policer-config-name> [del]",
517  .function = test_policer_command_fn,
518 };
519 /* *INDENT-ON* */
520 
521 #endif /* TEST_CODE */
522 
523 
524 typedef struct
525 {
532 
533 static u8 *
534 format_policer_classify_trace (u8 * s, va_list * args)
535 {
536  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
537  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
538  policer_classify_trace_t *t = va_arg (*args, policer_classify_trace_t *);
539 
540  s = format (s, "POLICER_CLASSIFY: sw_if_index %d next %d table %d offset %d"
541  " policer_index %d",
542  t->sw_if_index, t->next_index, t->table_index, t->offset,
543  t->policer_index);
544  return s;
545 }
546 
547 #define foreach_policer_classify_error \
548 _(MISS, "Policer classify misses") \
549 _(HIT, "Policer classify hits") \
550 _(CHAIN_HIT, "Polcier classify hits after chain walk") \
551 _(DROP, "Policer classify action drop")
552 
553 typedef enum
554 {
555 #define _(sym,str) POLICER_CLASSIFY_ERROR_##sym,
557 #undef _
560 
562 #define _(sym,string) string,
564 #undef _
565 };
566 
567 static inline uword
569  vlib_node_runtime_t * node,
570  vlib_frame_t * frame,
572 {
573  u32 n_left_from, *from, *to_next;
577  f64 now = vlib_time_now (vm);
578  u32 hits = 0;
579  u32 misses = 0;
580  u32 chain_hits = 0;
581  u32 drop = 0;
582  u32 n_next_nodes;
583  u64 time_in_policer_periods;
584 
585  time_in_policer_periods =
587 
588  n_next_nodes = node->n_next_nodes;
589 
590  from = vlib_frame_vector_args (frame);
591  n_left_from = frame->n_vectors;
592 
593  /* First pass: compute hashes */
594  while (n_left_from > 2)
595  {
596  vlib_buffer_t *b0, *b1;
597  u32 bi0, bi1;
598  u8 *h0, *h1;
599  u32 sw_if_index0, sw_if_index1;
600  u32 table_index0, table_index1;
601  vnet_classify_table_t *t0, *t1;
602 
603  /* Prefetch next iteration */
604  {
605  vlib_buffer_t *p1, *p2;
606 
607  p1 = vlib_get_buffer (vm, from[1]);
608  p2 = vlib_get_buffer (vm, from[2]);
609 
610  vlib_prefetch_buffer_header (p1, STORE);
612  vlib_prefetch_buffer_header (p2, STORE);
614  }
615 
616  bi0 = from[0];
617  b0 = vlib_get_buffer (vm, bi0);
618  h0 = b0->data;
619 
620  bi1 = from[1];
621  b1 = vlib_get_buffer (vm, bi1);
622  h1 = b1->data;
623 
624  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
625  table_index0 =
626  pcm->classify_table_index_by_sw_if_index[tid][sw_if_index0];
627 
628  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
629  table_index1 =
630  pcm->classify_table_index_by_sw_if_index[tid][sw_if_index1];
631 
632  t0 = pool_elt_at_index (vcm->tables, table_index0);
633 
634  t1 = pool_elt_at_index (vcm->tables, table_index1);
635 
636  vnet_buffer (b0)->l2_classify.hash =
637  vnet_classify_hash_packet (t0, (u8 *) h0);
638 
639  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
640 
641  vnet_buffer (b1)->l2_classify.hash =
642  vnet_classify_hash_packet (t1, (u8 *) h1);
643 
644  vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
645 
646  vnet_buffer (b0)->l2_classify.table_index = table_index0;
647 
648  vnet_buffer (b1)->l2_classify.table_index = table_index1;
649 
650  from += 2;
651  n_left_from -= 2;
652  }
653 
654  while (n_left_from > 0)
655  {
656  vlib_buffer_t *b0;
657  u32 bi0;
658  u8 *h0;
659  u32 sw_if_index0;
660  u32 table_index0;
662 
663  bi0 = from[0];
664  b0 = vlib_get_buffer (vm, bi0);
665  h0 = b0->data;
666 
667  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
668  table_index0 =
669  pcm->classify_table_index_by_sw_if_index[tid][sw_if_index0];
670 
671  t0 = pool_elt_at_index (vcm->tables, table_index0);
672  vnet_buffer (b0)->l2_classify.hash =
673  vnet_classify_hash_packet (t0, (u8 *) h0);
674 
675  vnet_buffer (b0)->l2_classify.table_index = table_index0;
676  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
677 
678  from++;
679  n_left_from--;
680  }
681 
682  next_index = node->cached_next_index;
683  from = vlib_frame_vector_args (frame);
684  n_left_from = frame->n_vectors;
685 
686  while (n_left_from > 0)
687  {
688  u32 n_left_to_next;
689 
690  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
691 
692  /* Not enough load/store slots to dual loop... */
693  while (n_left_from > 0 && n_left_to_next > 0)
694  {
695  u32 bi0;
696  vlib_buffer_t *b0;
698  u32 table_index0;
700  vnet_classify_entry_t *e0;
701  u64 hash0;
702  u8 *h0;
703  u8 act0;
704 
705  /* Stride 3 seems to work best */
706  if (PREDICT_TRUE (n_left_from > 3))
707  {
708  vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
710  u32 table_index1;
711  u64 phash1;
712 
713  table_index1 = vnet_buffer (p1)->l2_classify.table_index;
714 
715  if (PREDICT_TRUE (table_index1 != ~0))
716  {
717  tp1 = pool_elt_at_index (vcm->tables, table_index1);
718  phash1 = vnet_buffer (p1)->l2_classify.hash;
719  vnet_classify_prefetch_entry (tp1, phash1);
720  }
721  }
722 
723  /* Speculatively enqueue b0 to the current next frame */
724  bi0 = from[0];
725  to_next[0] = bi0;
726  from += 1;
727  to_next += 1;
728  n_left_from -= 1;
729  n_left_to_next -= 1;
730 
731  b0 = vlib_get_buffer (vm, bi0);
732  h0 = b0->data;
733  table_index0 = vnet_buffer (b0)->l2_classify.table_index;
734  e0 = 0;
735  t0 = 0;
736 
737  if (tid == POLICER_CLASSIFY_TABLE_L2)
738  {
739  /* Feature bitmap update */
740  vnet_buffer (b0)->l2.feature_bitmap &=
741  ~L2INPUT_FEAT_POLICER_CLAS;
742  /* Determine the next node */
743  next0 =
745  vnet_buffer (b0)->
746  l2.feature_bitmap);
747  }
748  else
750  &b0->current_config_index, &next0,
751  /* # bytes of config data */ 0);
752 
753  vnet_buffer (b0)->l2_classify.opaque_index = ~0;
754 
755  if (PREDICT_TRUE (table_index0 != ~0))
756  {
757  hash0 = vnet_buffer (b0)->l2_classify.hash;
758  t0 = pool_elt_at_index (vcm->tables, table_index0);
759  e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
760 
761  if (e0)
762  {
763  act0 = vnet_policer_police (vm,
764  b0,
765  e0->next_index,
766  time_in_policer_periods,
767  e0->opaque_index);
768  if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP))
769  {
771  b0->error = node->errors[POLICER_CLASSIFY_ERROR_DROP];
772  drop++;
773  }
774  hits++;
775  }
776  else
777  {
778  while (1)
779  {
780  if (PREDICT_TRUE (t0->next_table_index != ~0))
781  {
782  t0 = pool_elt_at_index (vcm->tables,
783  t0->next_table_index);
784  }
785  else
786  {
787  next0 = (t0->miss_next_index < n_next_nodes) ?
788  t0->miss_next_index : next0;
789  misses++;
790  break;
791  }
792 
793  hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
794  e0 =
795  vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
796  if (e0)
797  {
798  act0 = vnet_policer_police (vm,
799  b0,
800  e0->next_index,
801  time_in_policer_periods,
802  e0->opaque_index);
803  if (PREDICT_FALSE (act0 == SSE2_QOS_ACTION_DROP))
804  {
806  b0->error =
807  node->errors[POLICER_CLASSIFY_ERROR_DROP];
808  drop++;
809  }
810  hits++;
811  chain_hits++;
812  break;
813  }
814  }
815  }
816  }
818  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
819  {
821  vlib_add_trace (vm, node, b0, sizeof (*t));
822  t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
823  t->next_index = next0;
824  t->table_index = t0 ? t0 - vcm->tables : ~0;
825  t->offset = (e0 && t0) ? vnet_classify_get_offset (t0, e0) : ~0;
826  t->policer_index = e0 ? e0->next_index : ~0;
827  }
828 
829  /* Verify speculative enqueue, maybe switch current next frame */
830  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
831  n_left_to_next, bi0, next0);
832  }
833 
834  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
835  }
836 
838  POLICER_CLASSIFY_ERROR_MISS, misses);
840  POLICER_CLASSIFY_ERROR_HIT, hits);
842  POLICER_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
844  POLICER_CLASSIFY_ERROR_DROP, drop);
845 
846  return frame->n_vectors;
847 }
848 
849 static uword
851  vlib_node_runtime_t * node, vlib_frame_t * frame)
852 {
853  return policer_classify_inline (vm, node, frame,
855 }
856 
857 /* *INDENT-OFF* */
859  .function = ip4_policer_classify,
860  .name = "ip4-policer-classify",
861  .vector_size = sizeof (u32),
862  .format_trace = format_policer_classify_trace,
864  .error_strings = policer_classify_error_strings,
865  .n_next_nodes = POLICER_CLASSIFY_NEXT_INDEX_N_NEXT,
866  .next_nodes = {
867  [POLICER_CLASSIFY_NEXT_INDEX_DROP] = "error-drop",
868  },
869 };
870 
872 /* *INDENT-ON* */
873 
874 static uword
876  vlib_node_runtime_t * node, vlib_frame_t * frame)
877 {
878  return policer_classify_inline (vm, node, frame,
880 }
881 
882 /* *INDENT-OFF* */
884  .function = ip6_policer_classify,
885  .name = "ip6-policer-classify",
886  .vector_size = sizeof (u32),
887  .format_trace = format_policer_classify_trace,
889  .error_strings = policer_classify_error_strings,
890  .n_next_nodes = POLICER_CLASSIFY_NEXT_INDEX_N_NEXT,
891  .next_nodes = {
892  [POLICER_CLASSIFY_NEXT_INDEX_DROP] = "error-drop",
893  },
894 };
895 
897 /* *INDENT-ON* */
898 
899 static uword
901  vlib_node_runtime_t * node, vlib_frame_t * frame)
902 {
903  return policer_classify_inline (vm, node, frame, POLICER_CLASSIFY_TABLE_L2);
904 }
905 
907 {
908  .function = l2_policer_classify,.name = "l2-policer-classify",.vector_size =
909  sizeof (u32),.format_trace = format_policer_classify_trace,.n_errors =
910  ARRAY_LEN (policer_classify_error_strings),.error_strings =
911  policer_classify_error_strings,.n_next_nodes =
913  {
914  [POLICER_CLASSIFY_NEXT_INDEX_DROP] = "error-drop",}
915 ,};
916 
918 
919 
920 static clib_error_t *
922 {
924 
925  pcm->vlib_main = vm;
926  pcm->vnet_main = vnet_get_main ();
928 
929  /* Initialize L2 feature next-node indexes */
934  pcm->feat_next_node_index);
935 
936  return 0;
937 }
938 
940 
941 /*
942  * fd.io coding-style-patch-verification: ON
943  *
944  * Local Variables:
945  * eval: (c-set-style "gnu")
946  * End:
947  */
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
#define foreach_vnet_policer_error
Definition: node_funcs.c:52
#define CLIB_UNUSED(x)
Definition: clib.h:79
#define POLICER_TICKS_PER_PERIOD_SHIFT
Definition: police.h:57
VLIB_NODE_FUNCTION_MULTIARCH(policer_by_sw_if_index_node, vnet_policer_by_sw_if_index)
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:116
vnet_policer_error_t
Definition: node_funcs.c:56
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
vlib_node_registration_t ip6_policer_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_policer_classify_node)
Definition: node_funcs.c:883
uword * policer_config_by_name
Definition: policer.h:34
static_always_inline void vnet_policer_mark(vlib_buffer_t *b, u8 dscp)
Definition: node_funcs.c:71
#define PREDICT_TRUE(x)
Definition: clib.h:98
int vnet_hw_interface_rx_redirect_to_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: interface.c:1103
#define NULL
Definition: clib.h:55
vnet_policer_next_t
Definition: policer.h:56
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:185
static uword l2_policer_classify(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:900
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:459
static_always_inline u8 vnet_policer_police(vlib_main_t *vm, vlib_buffer_t *b, u32 policer_index, u64 time_in_policer_periods, policer_result_e packet_color)
Definition: node_funcs.c:102
static uword vnet_policer_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, vnet_policer_index_t which)
Definition: node_funcs.c:125
int test_policer_add_del(u32 rx_sw_if_index, u8 *config_name, int is_add)
Definition: node_funcs.c:388
static u64 clib_cpu_time_now(void)
Definition: time.h:73
policer_classify_error_t
Definition: node_funcs.c:553
#define IP4_DSCP_SHIFT
Definition: node_funcs.c:26
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:982
vnet_main_t * vnet_main
Definition: policer.h:44
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
unformat_function_t unformat_vnet_sw_interface
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:418
u8 * format_policer_instance(u8 *s, va_list *va)
Definition: policer.c:86
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:100
policer_classify_table_id_t
static vlib_node_registration_t policer_by_sw_if_index_node
(constructor) VLIB_REGISTER_NODE (policer_by_sw_if_index_node)
Definition: node_funcs.c:363
#define static_always_inline
Definition: clib.h:85
uword vnet_policer_by_opaque(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:339
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static char * vnet_policer_error_strings[]
Definition: node_funcs.c:64
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:164
#define clib_error_return(e, args...)
Definition: error.h:111
unsigned long u64
Definition: types.h:89
static void vnet_classify_prefetch_bucket(vnet_classify_table_t *t, u64 hash)
u32 * classify_table_index_by_sw_if_index[POLICER_CLASSIFY_N_TABLES]
static clib_error_t * policer_classify_init(vlib_main_t *vm)
Definition: node_funcs.c:921
static void vnet_classify_prefetch_entry(vnet_classify_table_t *t, u64 hash)
unformat_function_t unformat_line_input
Definition: format.h:281
#define IP6_DSCP_SHIFT
Definition: node_funcs.c:28
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
static policer_result_e vnet_police_packet(policer_read_response_type_st *policer, u32 packet_length, policer_result_e packet_color, u64 time)
Definition: police.h:90
struct _unformat_input_t unformat_input_t
vnet_config_main_t * vnet_config_main[POLICER_CLASSIFY_N_TABLES]
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:122
#define PREDICT_FALSE(x)
Definition: clib.h:97
static uword policer_classify_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, policer_classify_table_id_t tid)
Definition: node_funcs.c:568
u32 node_index
Node index.
Definition: node.h:436
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
static u32 feat_bitmap_get_next_node_index(u32 *next_nodes, u32 bitmap)
Return the graph node index for the feature corresponding to the first set bit in the bitmap...
Definition: feat_bitmap.h:79
policer_read_response_type_st * policers
Definition: policer.h:27
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:216
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:350
void vnet_policer_node_funcs_reference(void)
Definition: node_funcs.c:353
uword vnet_policer_by_sw_if_index(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:331
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:113
uword vnet_policer_by_either(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:346
u32 * policer_index_by_sw_if_index
Definition: policer.h:40
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1115
static char * policer_classify_error_strings[]
Definition: node_funcs.c:561
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
static uword vnet_classify_get_offset(vnet_classify_table_t *t, vnet_classify_entry_t *v)
static uword ip4_policer_classify(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:850
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
vlib_main_t * vm
Definition: buffer.c:276
static void feat_bitmap_init_next_nodes(vlib_main_t *vm, u32 node_index, u32 num_features, char **feat_names, u32 *next_nodes)
Initialize the feature next-node indexes of a graph node.
Definition: feat_bitmap.h:43
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:85
#define IP4_NON_DSCP_BITS
Definition: node_funcs.c:25
#define ARRAY_LEN(x)
Definition: clib.h:59
char ** l2input_get_feat_names(void)
Return an array of strings containing graph node names of each feature.
Definition: l2_input.c:58
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:69
#define foreach_policer_classify_error
Definition: node_funcs.c:547
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:455
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:255
unsigned int u32
Definition: types.h:88
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
vnet_classify_main_t vnet_classify_main
Definition: vnet_classify.c:22
policer_classify_next_index_t
#define IP6_NON_DSCP_BITS
Definition: node_funcs.c:27
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:328
vnet_classify_main_t * vnet_classify_main
unsigned short u16
Definition: types.h:57
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
vlib_node_registration_t ip4_policer_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_policer_classify_node)
Definition: node_funcs.c:858
static clib_error_t * test_policer_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: node_funcs.c:439
policer_result_e
Definition: police.h:18
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
#define hash_get_mem(h, key)
Definition: hash.h:268
static uword ip6_policer_classify(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:875
policer_read_response_type_st * policer_templates
Definition: policer.h:31
#define vnet_buffer(b)
Definition: buffer.h:294
policer_classify_main_t policer_classify_main
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:152
vlib_node_registration_t l2_policer_classify_node
(constructor) VLIB_REGISTER_NODE (l2_policer_classify_node)
Definition: node_funcs.c:906
u16 flags
Copy of main node flags.
Definition: node.h:449
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:74
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:577
vnet_policer_main_t vnet_policer_main
Definition: policer.h:47
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
vnet_classify_entry_t * vnet_classify_find_entry(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:238
static u8 * format_policer_trace(u8 *s, va_list *args)
Definition: node_funcs.c:41
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971
vnet_policer_index_t
Definition: policer.h:49
Definition: defs.h:46
static u8 * format_policer_classify_trace(u8 *s, va_list *args)
Definition: node_funcs.c:534
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169