FD.io VPP  v18.07-34-g55fbdb9
Vector Packet Processing
feat_bitmap.h
Go to the documentation of this file.
1 /*
2  * feat_bitmap.h: bitmap for managing feature invocation
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef included_vnet_l2_feat_bitmap_h
19 #define included_vnet_l2_feat_bitmap_h
20 
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 
24 /*
25  * The feature bitmap is a way of organizing input and output feature graph nodes.
26  * The set of features to be executed are arranged in a bitmap with one bit per
27  * feature and each bit positioned in the same order that the features should be
28  * executed. Features can be dynamically removed from the set by masking off their
29  * corresponding bits. The bitmap is stored in packet context. Each feature clears
30  * its bit and then calls feat_bitmap_get_next_node_index() to go to the next
31  * graph node.
32  */
33 
34 
35 /* 32 features in a u32 bitmap */
36 #define FEAT_MAX 32
37 
38 /**
39  Initialize the feature next-node indexes of a graph node.
40  Should be called by the init function of each feature graph node.
41 */
42 always_inline void
43 feat_bitmap_init_next_nodes (vlib_main_t * vm, u32 node_index, /* the current graph node index */
44  u32 num_features, /* number of entries in feat_names */
45  char **feat_names, /* array of feature graph node names */
46  u32 * next_nodes) /* array of 32 next indexes to init */
47 {
48  u32 idx;
49 
50  ASSERT (num_features <= FEAT_MAX);
51 
52  for (idx = 0; idx < num_features; idx++)
53  {
54  if (vlib_get_node_by_name (vm, (u8 *) feat_names[idx]))
55  {
56  next_nodes[idx] =
57  vlib_node_add_named_next (vm, node_index, feat_names[idx]);
58  }
59  else
60  { // Node may be in plugin which is not installed, use drop node
61  next_nodes[idx] =
62  vlib_node_add_named_next (vm, node_index, "feature-bitmap-drop");
63  }
64  }
65 
66  /* All unassigned bits go to the drop node */
67  for (; idx < FEAT_MAX; idx++)
68  {
69  next_nodes[idx] = vlib_node_add_named_next (vm, node_index,
70  "feature-bitmap-drop");
71  }
72 }
73 
74 /**
75  Return the graph node index for the feature corresponding to the
76  first set bit in the bitmap.
77 */
79 feat_bitmap_get_next_node_index (u32 * next_nodes, u32 bitmap)
80 {
81  u32 first_bit;
82 
83  first_bit = count_leading_zeros (bitmap);
84  first_bit = uword_bits - 1 - first_bit;
85  return next_nodes[first_bit];
86 }
87 
88 /**
89  Return the graph node index for the feature corresponding to the next
90  set bit after clearing the current feature bit in the feature_bitmap
91  of the current packet.
92 */
94 vnet_l2_feature_next (vlib_buffer_t * b, u32 * next_nodes, u32 feat_bit)
95 {
96  vnet_buffer (b)->l2.feature_bitmap &= ~feat_bit;
97  u32 fb = vnet_buffer (b)->l2.feature_bitmap;
98  ASSERT (fb != 0);
99  return feat_bitmap_get_next_node_index (next_nodes, fb);
100 }
101 
102 #endif /* included_vnet_l2_feat_bitmap_h */
103 
104 /*
105  * fd.io coding-style-patch-verification: ON
106  *
107  * Local Variables:
108  * eval: (c-set-style "gnu")
109  * End:
110  */
#define count_leading_zeros(x)
Definition: clib.h:132
static uword vlib_node_add_named_next(vlib_main_t *vm, uword node, char *name)
Definition: node_funcs.h:1122
unsigned char u8
Definition: types.h:56
static u32 vnet_l2_feature_next(vlib_buffer_t *b, u32 *next_nodes, u32 feat_bit)
Return the graph node index for the feature corresponding to the next set bit after clearing the curr...
Definition: feat_bitmap.h:94
#define always_inline
Definition: clib.h:92
unsigned int u32
Definition: types.h:88
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
#define FEAT_MAX
Definition: feat_bitmap.h:36
vlib_main_t * vm
Definition: buffer.c:294
#define uword_bits
Definition: types.h:102
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
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
#define ASSERT(truth)
#define vnet_buffer(b)
Definition: buffer.h:360