Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 24 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | * The feature bitmap is a way of organizing input and output feature graph nodes. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 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 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | * corresponding bits. The bitmap is stored in packet context. Each feature clears |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 30 | * its bit and then calls feat_bitmap_get_next_node_index() to go to the next |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | * graph node. |
| 32 | */ |
| 33 | |
| 34 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 35 | /* 32 features in a u32 bitmap */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | #define FEAT_MAX 32 |
| 37 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 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 */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | { |
| 48 | u32 idx; |
| 49 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 50 | ASSERT (num_features <= FEAT_MAX); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 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 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 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 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 74 | /** |
| 75 | Return the graph node index for the feature corresponding to the |
| 76 | first set bit in the bitmap. |
| 77 | */ |
John Lo | beb0b2e | 2017-07-22 00:21:36 -0400 | [diff] [blame] | 78 | always_inline u32 |
| 79 | feat_bitmap_get_next_node_index (u32 * next_nodes, u32 bitmap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | { |
| 81 | u32 first_bit; |
| 82 | |
Damjan Marion | 1105600 | 2018-05-10 13:40:44 +0200 | [diff] [blame] | 83 | first_bit = count_leading_zeros (bitmap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | first_bit = uword_bits - 1 - first_bit; |
| 85 | return next_nodes[first_bit]; |
| 86 | } |
| 87 | |
John Lo | beb0b2e | 2017-07-22 00:21:36 -0400 | [diff] [blame] | 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 | */ |
| 93 | always_inline u32 |
| 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 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 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 | */ |