Initial commit of vpp code.
[vpp.git] / vnet / vnet / l2 / feat_bitmap.h
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 // Initialize the feature next-node indexes of a graph node.
39 // Should be called by the init function of each feature graph node.
40 always_inline
41 void feat_bitmap_init_next_nodes (
42         vlib_main_t  * vm,
43         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     if (vlib_get_node_by_name(vm, (u8 *) feat_names[idx])) {
54       next_nodes[idx] = 
55           vlib_node_add_named_next(vm, node_index, feat_names[idx]);
56     } else { // Node may be in plugin which is not installed, use drop node
57       next_nodes[idx] = 
58           vlib_node_add_named_next(vm, node_index, "feature-bitmap-drop");
59     }
60   }
61  
62   // All unassigned bits go to the drop node
63   for (; idx<FEAT_MAX; idx++) {
64     next_nodes[idx] = vlib_node_add_named_next(vm, node_index, "feature-bitmap-drop");
65   }
66 }
67
68 // Return the graph node index for the feature corresponding to the 
69 // first set bit in the bitmap.
70 always_inline
71 u32 feat_bitmap_get_next_node_index (u32 * next_nodes, u32 bitmap)
72 {
73   u32 first_bit;
74
75   count_leading_zeros(first_bit, bitmap);
76   first_bit = uword_bits - 1 - first_bit;
77   return next_nodes[first_bit];
78 }
79
80 #endif // included_vnet_l2_feat_bitmap_h