blob: c6e02ecc7c90cf4866ee86cd9590129a614ed4c2 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Dave Barach97d8dc22016-08-15 15:31:15 -040024/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070025 * The feature bitmap is a way of organizing input and output feature graph nodes.
Dave Barach97d8dc22016-08-15 15:31:15 -040026 * 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
Ed Warnickecb9cada2015-12-08 15:45:58 -070029 * corresponding bits. The bitmap is stored in packet context. Each feature clears
Dave Barach97d8dc22016-08-15 15:31:15 -040030 * its bit and then calls feat_bitmap_get_next_node_index() to go to the next
Ed Warnickecb9cada2015-12-08 15:45:58 -070031 * graph node.
32 */
33
34
Dave Barach97d8dc22016-08-15 15:31:15 -040035/* 32 features in a u32 bitmap */
Ed Warnickecb9cada2015-12-08 15:45:58 -070036#define FEAT_MAX 32
37
Dave Barach97d8dc22016-08-15 15:31:15 -040038/**
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*/
42always_inline void
43feat_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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070047{
48 u32 idx;
49
Dave Barach97d8dc22016-08-15 15:31:15 -040050 ASSERT (num_features <= FEAT_MAX);
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Dave Barach97d8dc22016-08-15 15:31:15 -040052 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 }
Dave Barach97d8dc22016-08-15 15:31:15 -040065
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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070072}
73
Dave Barach97d8dc22016-08-15 15:31:15 -040074/**
75 Return the graph node index for the feature corresponding to the
76 first set bit in the bitmap.
77*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070078always_inline
Dave Barach97d8dc22016-08-15 15:31:15 -040079 u32 feat_bitmap_get_next_node_index (u32 * next_nodes, u32 bitmap)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
81 u32 first_bit;
82
Dave Barach97d8dc22016-08-15 15:31:15 -040083 count_leading_zeros (first_bit, bitmap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 first_bit = uword_bits - 1 - first_bit;
85 return next_nodes[first_bit];
86}
87
Dave Barach97d8dc22016-08-15 15:31:15 -040088#endif /* included_vnet_l2_feat_bitmap_h */
89
90/*
91 * fd.io coding-style-patch-verification: ON
92 *
93 * Local Variables:
94 * eval: (c-set-style "gnu")
95 * End:
96 */