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 | * ip/ip4_fib.h: ip4 mtrie fib |
| 17 | * |
| 18 | * Copyright (c) 2012 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_ip_ip4_fib_h |
| 41 | #define included_ip_ip4_fib_h |
| 42 | |
| 43 | #include <vppinfra/cache.h> |
| 44 | #include <vppinfra/vector.h> |
| 45 | #include <vnet/ip/lookup.h> |
| 46 | #include <vnet/ip/ip4_packet.h> /* for ip4_address_t */ |
| 47 | |
| 48 | /* ip4 fib leafs: 4 ply 8-8-8-8 mtrie. |
| 49 | 1 + 2*adj_index for terminal leaves. |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 50 | 0 + 2*next_ply_index for non-terminals, i.e. PLYs |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | 1 => empty (adjacency index of zero is special miss adjacency). */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 52 | typedef u32 ip4_mtrie_leaf_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 54 | #define IP4_MTRIE_LEAF_EMPTY (1 + 2 * 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 56 | /** |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 57 | * @brief the 16 way stride that is the top PLY of the mtrie |
| 58 | * We do not maintain the count of 'real' leaves in this PLY, since |
| 59 | * it is never removed. The FIB will destroy the mtrie and the ply once |
| 60 | * the FIB is destroyed. |
| 61 | */ |
| 62 | #define PLY_16_SIZE (1<<16) |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 63 | typedef struct ip4_mtrie_16_ply_t_ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 64 | { |
| 65 | /** |
| 66 | * The leaves/slots/buckets to be filed with leafs |
| 67 | */ |
Damjan Marion | 9ff617c | 2021-12-23 13:19:15 +0100 | [diff] [blame] | 68 | ip4_mtrie_leaf_t leaves[PLY_16_SIZE]; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Prefix length for terminal leaves. |
| 72 | */ |
| 73 | u8 dst_address_bits_of_leaves[PLY_16_SIZE]; |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 74 | } ip4_mtrie_16_ply_t; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 77 | * @brief One ply of the 4 ply mtrie fib. |
| 78 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 79 | typedef struct ip4_mtrie_8_ply_t_ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 80 | { |
Damjan Marion | 9ff617c | 2021-12-23 13:19:15 +0100 | [diff] [blame] | 81 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 82 | /** |
| 83 | * The leaves/slots/buckets to be filed with leafs |
| 84 | */ |
Damjan Marion | 9ff617c | 2021-12-23 13:19:15 +0100 | [diff] [blame] | 85 | ip4_mtrie_leaf_t leaves[256]; |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Prefix length for leaves/ply. |
| 89 | */ |
| 90 | u8 dst_address_bits_of_leaves[256]; |
| 91 | |
| 92 | /** |
| 93 | * Number of non-empty leafs (whether terminal or not). |
| 94 | */ |
| 95 | i32 n_non_empty_leafs; |
| 96 | |
| 97 | /** |
Lijian.Zhang | 33af8c1 | 2019-09-16 16:22:36 +0800 | [diff] [blame] | 98 | * The length of the ply's covering prefix. Also a measure of its depth |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 99 | * If a leaf in a slot has a mask length longer than this then it is |
| 100 | * 'non-empty'. Otherwise it is the value of the cover. |
| 101 | */ |
| 102 | i32 dst_address_bits_base; |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 103 | } ip4_mtrie_8_ply_t; |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 104 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 105 | STATIC_ASSERT (0 == sizeof (ip4_mtrie_8_ply_t) % CLIB_CACHE_LINE_BYTES, |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 106 | "IP4 Mtrie ply cache line"); |
| 107 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 108 | /** |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 109 | * @brief The mutiway-TRIE with a 16-8-8 stride. |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 110 | * There is no data associated with the mtrie apart from the top PLY |
| 111 | */ |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 112 | typedef struct |
| 113 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Embed the PLY with the mtrie struct. This means that the Data-plane |
| 116 | * 'get me the mtrie' returns the first ply, and not an indirect 'pointer' |
Lijian.Zhang | 33af8c1 | 2019-09-16 16:22:36 +0800 | [diff] [blame] | 117 | * to it. therefore no cacheline misses in the data-path. |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 118 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 119 | ip4_mtrie_16_ply_t root_ply; |
| 120 | } ip4_mtrie_16_t; |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 121 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 122 | /** |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 123 | * @brief The mutiway-TRIE with a 8-8-8-8 stride. |
| 124 | * There is no data associated with the mtrie apart from the top PLY |
| 125 | */ |
| 126 | typedef struct |
| 127 | { |
| 128 | /* pool index of the root ply */ |
| 129 | u32 root_ply; |
| 130 | } ip4_mtrie_8_t; |
| 131 | |
| 132 | /** |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 133 | * @brief Initialise an mtrie |
| 134 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 135 | void ip4_mtrie_16_init (ip4_mtrie_16_t *m); |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 136 | void ip4_mtrie_8_init (ip4_mtrie_8_t *m); |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 137 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 138 | /** |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 139 | * @brief Free an mtrie, It must be empty when free'd |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 140 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 141 | void ip4_mtrie_16_free (ip4_mtrie_16_t *m); |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 142 | void ip4_mtrie_8_free (ip4_mtrie_8_t *m); |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 143 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 144 | /** |
Lijian.Zhang | 33af8c1 | 2019-09-16 16:22:36 +0800 | [diff] [blame] | 145 | * @brief Add a route/entry to the mtrie |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 146 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 147 | void ip4_mtrie_16_route_add (ip4_mtrie_16_t *m, |
| 148 | const ip4_address_t *dst_address, |
| 149 | u32 dst_address_length, u32 adj_index); |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 150 | void ip4_mtrie_8_route_add (ip4_mtrie_8_t *m, const ip4_address_t *dst_address, |
| 151 | u32 dst_address_length, u32 adj_index); |
| 152 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 153 | /** |
Lijian.Zhang | 33af8c1 | 2019-09-16 16:22:36 +0800 | [diff] [blame] | 154 | * @brief remove a route/entry to the mtrie |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 155 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 156 | void ip4_mtrie_16_route_del (ip4_mtrie_16_t *m, |
| 157 | const ip4_address_t *dst_address, |
| 158 | u32 dst_address_length, u32 adj_index, |
| 159 | u32 cover_address_length, u32 cover_adj_index); |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 160 | void ip4_mtrie_8_route_del (ip4_mtrie_8_t *m, const ip4_address_t *dst_address, |
| 161 | u32 dst_address_length, u32 adj_index, |
| 162 | u32 cover_address_length, u32 cover_adj_index); |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 163 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 164 | /** |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 165 | * @brief return the memory used by the table |
| 166 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 167 | uword ip4_mtrie_16_memory_usage (ip4_mtrie_16_t *m); |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 168 | uword ip4_mtrie_8_memory_usage (ip4_mtrie_8_t *m); |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 169 | |
| 170 | /** |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 171 | * @brief Format/display the contents of the mtrie |
| 172 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 173 | format_function_t format_ip4_mtrie_16; |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 174 | format_function_t format_ip4_mtrie_8; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 176 | /** |
| 177 | * @brief A global pool of 8bit stride plys |
| 178 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 179 | extern ip4_mtrie_8_ply_t *ip4_ply_pool; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
Lijian.Zhang | 33af8c1 | 2019-09-16 16:22:36 +0800 | [diff] [blame] | 182 | * Is the leaf terminal (i.e. an LB index) or non-terminal (i.e. a PLY index) |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 183 | */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 184 | always_inline u32 |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 185 | ip4_mtrie_leaf_is_terminal (ip4_mtrie_leaf_t n) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 186 | { |
| 187 | return n & 1; |
| 188 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 190 | /** |
| 191 | * From the stored slot value extract the LB index value |
| 192 | */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 193 | always_inline u32 |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 194 | ip4_mtrie_leaf_get_adj_index (ip4_mtrie_leaf_t n) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | { |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 196 | ASSERT (ip4_mtrie_leaf_is_terminal (n)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | return n >> 1; |
| 198 | } |
| 199 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 200 | /** |
| 201 | * @brief Lookup step. Processes 1 byte of 4 byte ip4 address. |
| 202 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 203 | always_inline ip4_mtrie_leaf_t |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 204 | ip4_mtrie_16_lookup_step (ip4_mtrie_leaf_t current_leaf, |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 205 | const ip4_address_t *dst_address, |
| 206 | u32 dst_address_byte_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | { |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 208 | ip4_mtrie_8_ply_t *ply; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 209 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 210 | uword current_is_terminal = ip4_mtrie_leaf_is_terminal (current_leaf); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 212 | if (!current_is_terminal) |
| 213 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 214 | ply = ip4_ply_pool + (current_leaf >> 1); |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 215 | return (ply->leaves[dst_address->as_u8[dst_address_byte_index]]); |
| 216 | } |
| 217 | |
| 218 | return current_leaf; |
| 219 | } |
| 220 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 221 | /** |
| 222 | * @brief Lookup step number 1. Processes 2 bytes of 4 byte ip4 address. |
| 223 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 224 | always_inline ip4_mtrie_leaf_t |
| 225 | ip4_mtrie_16_lookup_step_one (const ip4_mtrie_16_t *m, |
| 226 | const ip4_address_t *dst_address) |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 227 | { |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 228 | ip4_mtrie_leaf_t next_leaf; |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 229 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 230 | next_leaf = m->root_ply.leaves[dst_address->as_u16[0]]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
| 232 | return next_leaf; |
| 233 | } |
| 234 | |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame] | 235 | always_inline ip4_mtrie_leaf_t |
| 236 | ip4_mtrie_8_lookup_step (ip4_mtrie_leaf_t current_leaf, |
| 237 | const ip4_address_t *dst_address, |
| 238 | u32 dst_address_byte_index) |
| 239 | { |
| 240 | ip4_mtrie_8_ply_t *ply; |
| 241 | |
| 242 | uword current_is_terminal = ip4_mtrie_leaf_is_terminal (current_leaf); |
| 243 | |
| 244 | if (!current_is_terminal) |
| 245 | { |
| 246 | ply = ip4_ply_pool + (current_leaf >> 1); |
| 247 | return (ply->leaves[dst_address->as_u8[dst_address_byte_index]]); |
| 248 | } |
| 249 | |
| 250 | return current_leaf; |
| 251 | } |
| 252 | |
| 253 | always_inline ip4_mtrie_leaf_t |
| 254 | ip4_mtrie_8_lookup_step_one (const ip4_mtrie_8_t *m, |
| 255 | const ip4_address_t *dst_address) |
| 256 | { |
| 257 | ip4_mtrie_leaf_t next_leaf; |
| 258 | ip4_mtrie_8_ply_t *ply; |
| 259 | |
| 260 | ply = pool_elt_at_index (ip4_ply_pool, m->root_ply); |
| 261 | next_leaf = ply->leaves[dst_address->as_u8[0]]; |
| 262 | |
| 263 | return next_leaf; |
| 264 | } |
| 265 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | #endif /* included_ip_ip4_fib_h */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * fd.io coding-style-patch-verification: ON |
| 270 | * |
| 271 | * Local Variables: |
| 272 | * eval: (c-set-style "gnu") |
| 273 | * End: |
| 274 | */ |