Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | * config.h: feature configuration |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #ifndef included_vnet_config_h |
| 41 | #define included_vnet_config_h |
| 42 | |
| 43 | #include <vlib/vlib.h> |
| 44 | #include <vppinfra/heap.h> |
| 45 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 46 | typedef struct |
| 47 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | /* Features are prioritized by index. Smaller indices get |
| 49 | performed first. */ |
| 50 | u32 feature_index; |
| 51 | |
| 52 | /* VLIB node which performs feature. */ |
| 53 | u32 node_index; |
| 54 | |
| 55 | /* Next index relative to previous node or main node. */ |
| 56 | u32 next_index; |
| 57 | |
| 58 | /* Opaque per feature configuration data. */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 59 | u32 *feature_config; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | } vnet_config_feature_t; |
| 61 | |
| 62 | always_inline void |
| 63 | vnet_config_feature_free (vnet_config_feature_t * f) |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 64 | { |
| 65 | vec_free (f->feature_config); |
| 66 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 68 | typedef struct |
| 69 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | /* Sorted vector of features for this configuration. */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 71 | vnet_config_feature_t *features; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | /* Config string as vector for hashing. */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 74 | u32 *config_string_vector; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | |
| 76 | /* Config string including all next indices and feature data as a vector. */ |
| 77 | u32 config_string_heap_index, config_string_heap_handle; |
| 78 | |
| 79 | /* Index in main pool. */ |
| 80 | u32 index; |
| 81 | |
| 82 | /* Number of interfaces/traffic classes that reference this config. */ |
| 83 | u32 reference_count; |
| 84 | } vnet_config_t; |
| 85 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 86 | typedef struct |
| 87 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | /* Pool of configs. Index 0 is always null config and is never deleted. */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 89 | vnet_config_t *config_pool; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | |
| 91 | /* Hash table mapping vector config string to config pool index. */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 92 | uword *config_string_hash; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 94 | /* Global heap of configuration data. */ |
| 95 | u32 *config_string_heap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
| 97 | /* Node index which starts/ends feature processing. */ |
Neale Ranns | 5d0136f | 2020-05-12 08:51:02 +0000 | [diff] [blame] | 98 | u32 *start_node_indices, *end_node_indices_by_user_index, |
| 99 | default_end_node_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
| 101 | /* Interior feature processing nodes (not including start and end nodes). */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 102 | u32 *node_index_by_feature_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | |
Dave Barach | de393bb | 2016-06-23 11:27:51 -0400 | [diff] [blame] | 104 | /* vnet_config pool index by user index */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 105 | u32 *config_pool_index_by_user_index; |
Dave Barach | de393bb | 2016-06-23 11:27:51 -0400 | [diff] [blame] | 106 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | /* Temporary vector for holding config strings. Used to avoid continually |
| 108 | allocating vectors. */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 109 | u32 *config_string_temp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | } vnet_config_main_t; |
| 111 | |
| 112 | always_inline void |
| 113 | vnet_config_free (vnet_config_main_t * cm, vnet_config_t * c) |
| 114 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 115 | vnet_config_feature_t *f; |
| 116 | vec_foreach (f, c->features) vnet_config_feature_free (f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | vec_free (c->features); |
| 118 | heap_dealloc (cm->config_string_heap, c->config_string_heap_handle); |
| 119 | vec_free (c->config_string_vector); |
| 120 | } |
| 121 | |
| 122 | always_inline void * |
| 123 | vnet_get_config_data (vnet_config_main_t * cm, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 124 | u32 * config_index, u32 * next_index, u32 n_data_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 126 | u32 i, n, *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
| 128 | i = *config_index; |
| 129 | |
| 130 | d = heap_elt_at_index (cm->config_string_heap, i); |
| 131 | |
| 132 | n = round_pow2 (n_data_bytes, sizeof (d[0])) / sizeof (d[0]); |
| 133 | |
| 134 | /* Last 32 bits are next index. */ |
| 135 | *next_index = d[n]; |
| 136 | |
| 137 | /* Advance config index to next config. */ |
| 138 | *config_index = (i + n + 1); |
| 139 | |
| 140 | /* Return config data to user for this feature. */ |
| 141 | return (void *) d; |
| 142 | } |
| 143 | |
| 144 | void vnet_config_init (vlib_main_t * vm, |
| 145 | vnet_config_main_t * cm, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 146 | char *start_node_names[], |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | int n_start_node_names, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 148 | char *feature_node_names[], int n_feature_node_names); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
BenoƮt Ganne | 6178bda | 2020-11-04 10:02:03 +0100 | [diff] [blame] | 150 | void vnet_config_del (vnet_config_main_t * cm, u32 config_id); |
| 151 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | /* Calls to add/delete features from configurations. */ |
| 153 | u32 vnet_config_add_feature (vlib_main_t * vm, |
| 154 | vnet_config_main_t * cm, |
| 155 | u32 config_id, |
| 156 | u32 feature_index, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 157 | void *feature_config, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | u32 n_feature_config_bytes); |
| 159 | |
| 160 | u32 vnet_config_del_feature (vlib_main_t * vm, |
| 161 | vnet_config_main_t * cm, |
| 162 | u32 config_id, |
| 163 | u32 feature_index, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 164 | void *feature_config, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | u32 n_feature_config_bytes); |
| 166 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 167 | u32 vnet_config_modify_end_node (vlib_main_t * vm, |
| 168 | vnet_config_main_t * cm, |
| 169 | u32 config_string_heap_index, |
| 170 | u32 end_node_index); |
| 171 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame^] | 172 | u32 vnet_config_reset_end_node (vlib_main_t *vm, vnet_config_main_t *cm, |
| 173 | u32 config_string_heap_index); |
| 174 | |
| 175 | u32 vnet_config_get_end_node (vlib_main_t *vm, vnet_config_main_t *cm, |
| 176 | u32 config_string_heap_index); |
| 177 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 178 | u8 *vnet_config_format_features (vlib_main_t * vm, |
| 179 | vnet_config_main_t * cm, |
| 180 | u32 config_index, u8 * s); |
| 181 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | #endif /* included_vnet_config_h */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * fd.io coding-style-patch-verification: ON |
| 186 | * |
| 187 | * Local Variables: |
| 188 | * eval: (c-set-style "gnu") |
| 189 | * End: |
| 190 | */ |