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 | #include <vnet/ip/ip.h> |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 41 | #include <vnet/ip/ip4_mtrie.h> |
| 42 | #include <vnet/fib/ip4_fib.h> |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Global pool of IPv4 8bit PLYs |
| 47 | */ |
| 48 | ip4_fib_mtrie_8_ply_t *ip4_ply_pool; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 50 | always_inline u32 |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 51 | ip4_fib_mtrie_leaf_is_non_empty (ip4_fib_mtrie_8_ply_t * p, u8 dst_byte) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | { |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 53 | /* |
| 54 | * It's 'non-empty' if the length of the leaf stored is greater than the |
| 55 | * length of a leaf in the covering ply. i.e. the leaf is more specific |
| 56 | * than it's would be cover in the covering ply |
| 57 | */ |
| 58 | if (p->dst_address_bits_of_leaves[dst_byte] > p->dst_address_bits_base) |
| 59 | return (1); |
| 60 | return (0); |
| 61 | } |
| 62 | |
| 63 | always_inline ip4_fib_mtrie_leaf_t |
| 64 | ip4_fib_mtrie_leaf_set_adj_index (u32 adj_index) |
| 65 | { |
| 66 | ip4_fib_mtrie_leaf_t l; |
| 67 | l = 1 + 2 * adj_index; |
| 68 | ASSERT (ip4_fib_mtrie_leaf_get_adj_index (l) == adj_index); |
| 69 | return l; |
| 70 | } |
| 71 | |
| 72 | always_inline u32 |
| 73 | ip4_fib_mtrie_leaf_is_next_ply (ip4_fib_mtrie_leaf_t n) |
| 74 | { |
| 75 | return (n & 1) == 0; |
| 76 | } |
| 77 | |
| 78 | always_inline u32 |
| 79 | ip4_fib_mtrie_leaf_get_next_ply_index (ip4_fib_mtrie_leaf_t n) |
| 80 | { |
| 81 | ASSERT (ip4_fib_mtrie_leaf_is_next_ply (n)); |
| 82 | return n >> 1; |
| 83 | } |
| 84 | |
| 85 | always_inline ip4_fib_mtrie_leaf_t |
| 86 | ip4_fib_mtrie_leaf_set_next_ply_index (u32 i) |
| 87 | { |
| 88 | ip4_fib_mtrie_leaf_t l; |
| 89 | l = 0 + 2 * i; |
| 90 | ASSERT (ip4_fib_mtrie_leaf_get_next_ply_index (l) == i); |
| 91 | return l; |
| 92 | } |
| 93 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 94 | #ifndef __ALTIVEC__ |
| 95 | #define PLY_X4_SPLAT_INIT(init_x4, init) \ |
| 96 | init_x4 = u32x4_splat (init); |
| 97 | #else |
| 98 | #define PLY_X4_SPLAT_INIT(init_x4, init) \ |
| 99 | { \ |
| 100 | u32x4_union_t y; \ |
| 101 | y.as_u32[0] = init; \ |
| 102 | y.as_u32[1] = init; \ |
| 103 | y.as_u32[2] = init; \ |
| 104 | y.as_u32[3] = init; \ |
| 105 | init_x4 = y.as_u32x4; \ |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | #ifdef CLIB_HAVE_VEC128 |
| 110 | #define PLY_INIT_LEAVES(p) \ |
| 111 | { \ |
| 112 | u32x4 *l, init_x4; \ |
| 113 | \ |
| 114 | PLY_X4_SPLAT_INIT(init_x4, init); \ |
| 115 | for (l = p->leaves_as_u32x4; \ |
| 116 | l < p->leaves_as_u32x4 + ARRAY_LEN (p->leaves_as_u32x4); \ |
| 117 | l += 4) \ |
| 118 | { \ |
| 119 | l[0] = init_x4; \ |
| 120 | l[1] = init_x4; \ |
| 121 | l[2] = init_x4; \ |
| 122 | l[3] = init_x4; \ |
| 123 | } \ |
| 124 | } |
| 125 | #else |
| 126 | #define PLY_INIT_LEAVES(p) \ |
| 127 | { \ |
| 128 | u32 *l; \ |
| 129 | \ |
| 130 | for (l = p->leaves; l < p->leaves + ARRAY_LEN (p->leaves); l += 4) \ |
| 131 | { \ |
| 132 | l[0] = init; \ |
| 133 | l[1] = init; \ |
| 134 | l[2] = init; \ |
| 135 | l[3] = init; \ |
| 136 | } \ |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #define PLY_INIT(p, init, prefix_len, ply_base_len) \ |
| 141 | { \ |
| 142 | /* \ |
| 143 | * A leaf is 'empty' if it represents a leaf from the covering PLY \ |
| 144 | * i.e. if the prefix length of the leaf is less than or equal to \ |
| 145 | * the prefix length of the PLY \ |
| 146 | */ \ |
| 147 | p->n_non_empty_leafs = (prefix_len > ply_base_len ? \ |
| 148 | ARRAY_LEN (p->leaves) : 0); \ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 149 | clib_memset (p->dst_address_bits_of_leaves, prefix_len, \ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 150 | sizeof (p->dst_address_bits_of_leaves)); \ |
| 151 | p->dst_address_bits_base = ply_base_len; \ |
| 152 | \ |
| 153 | /* Initialize leaves. */ \ |
| 154 | PLY_INIT_LEAVES(p); \ |
| 155 | } |
| 156 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 157 | static void |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 158 | ply_8_init (ip4_fib_mtrie_8_ply_t * p, |
| 159 | ip4_fib_mtrie_leaf_t init, uword prefix_len, u32 ply_base_len) |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 160 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 161 | PLY_INIT (p, init, prefix_len, ply_base_len); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | ply_16_init (ip4_fib_mtrie_16_ply_t * p, |
| 166 | ip4_fib_mtrie_leaf_t init, uword prefix_len) |
| 167 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 168 | clib_memset (p->dst_address_bits_of_leaves, prefix_len, |
| 169 | sizeof (p->dst_address_bits_of_leaves)); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 170 | PLY_INIT_LEAVES (p); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static ip4_fib_mtrie_leaf_t |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 174 | ply_create (ip4_fib_mtrie_t * m, |
| 175 | ip4_fib_mtrie_leaf_t init_leaf, |
| 176 | u32 leaf_prefix_len, u32 ply_base_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 178 | ip4_fib_mtrie_8_ply_t *p; |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 179 | void *old_heap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | /* Get cache aligned ply. */ |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 181 | |
| 182 | old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 183 | pool_get_aligned (ip4_ply_pool, p, CLIB_CACHE_LINE_BYTES); |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 184 | clib_mem_set_heap (old_heap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 186 | ply_8_init (p, init_leaf, leaf_prefix_len, ply_base_len); |
| 187 | return ip4_fib_mtrie_leaf_set_next_ply_index (p - ip4_ply_pool); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 190 | always_inline ip4_fib_mtrie_8_ply_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | get_next_ply_for_leaf (ip4_fib_mtrie_t * m, ip4_fib_mtrie_leaf_t l) |
| 192 | { |
| 193 | uword n = ip4_fib_mtrie_leaf_get_next_ply_index (l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 195 | return pool_elt_at_index (ip4_ply_pool, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 198 | void |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 199 | ip4_mtrie_free (ip4_fib_mtrie_t * m) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | { |
Lijian.Zhang | 33af8c1 | 2019-09-16 16:22:36 +0800 | [diff] [blame] | 201 | /* the root ply is embedded so there is nothing to do, |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 202 | * the assumption being that the IP4 FIB table has emptied the trie |
| 203 | * before deletion. |
| 204 | */ |
| 205 | #if CLIB_DEBUG > 0 |
| 206 | int i; |
| 207 | for (i = 0; i < ARRAY_LEN (m->root_ply.leaves); i++) |
| 208 | { |
| 209 | ASSERT (!ip4_fib_mtrie_leaf_is_next_ply (m->root_ply.leaves[i])); |
| 210 | } |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | ip4_mtrie_init (ip4_fib_mtrie_t * m) |
| 216 | { |
| 217 | ply_16_init (&m->root_ply, IP4_FIB_MTRIE_LEAF_EMPTY, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 220 | typedef struct |
| 221 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | ip4_address_t dst_address; |
| 223 | u32 dst_address_length; |
| 224 | u32 adj_index; |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 225 | u32 cover_address_length; |
| 226 | u32 cover_adj_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | } ip4_fib_mtrie_set_unset_leaf_args_t; |
| 228 | |
| 229 | static void |
| 230 | set_ply_with_more_specific_leaf (ip4_fib_mtrie_t * m, |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 231 | ip4_fib_mtrie_8_ply_t * ply, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | ip4_fib_mtrie_leaf_t new_leaf, |
| 233 | uword new_leaf_dst_address_bits) |
| 234 | { |
| 235 | ip4_fib_mtrie_leaf_t old_leaf; |
| 236 | uword i; |
| 237 | |
| 238 | ASSERT (ip4_fib_mtrie_leaf_is_terminal (new_leaf)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | |
| 240 | for (i = 0; i < ARRAY_LEN (ply->leaves); i++) |
| 241 | { |
| 242 | old_leaf = ply->leaves[i]; |
| 243 | |
| 244 | /* Recurse into sub plies. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 245 | if (!ip4_fib_mtrie_leaf_is_terminal (old_leaf)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 247 | ip4_fib_mtrie_8_ply_t *sub_ply = |
| 248 | get_next_ply_for_leaf (m, old_leaf); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 249 | set_ply_with_more_specific_leaf (m, sub_ply, new_leaf, |
| 250 | new_leaf_dst_address_bits); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* Replace less specific terminal leaves with new leaf. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 254 | else if (new_leaf_dst_address_bits >= |
| 255 | ply->dst_address_bits_of_leaves[i]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 257 | clib_atomic_cmp_and_swap (&ply->leaves[i], old_leaf, new_leaf); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 258 | ASSERT (ply->leaves[i] == new_leaf); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | ply->dst_address_bits_of_leaves[i] = new_leaf_dst_address_bits; |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 260 | ply->n_non_empty_leafs += ip4_fib_mtrie_leaf_is_non_empty (ply, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | static void |
| 266 | set_leaf (ip4_fib_mtrie_t * m, |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 267 | const ip4_fib_mtrie_set_unset_leaf_args_t * a, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 268 | u32 old_ply_index, u32 dst_address_byte_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | { |
| 270 | ip4_fib_mtrie_leaf_t old_leaf, new_leaf; |
| 271 | i32 n_dst_bits_next_plies; |
| 272 | u8 dst_byte; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 273 | ip4_fib_mtrie_8_ply_t *old_ply; |
| 274 | |
| 275 | old_ply = pool_elt_at_index (ip4_ply_pool, old_ply_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | |
Neale Ranns | f060930 | 2017-04-11 09:13:39 -0700 | [diff] [blame] | 277 | ASSERT (a->dst_address_length <= 32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | ASSERT (dst_address_byte_index < ARRAY_LEN (a->dst_address.as_u8)); |
| 279 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 280 | /* how many bits of the destination address are in the next PLY */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 281 | n_dst_bits_next_plies = |
| 282 | a->dst_address_length - BITS (u8) * (dst_address_byte_index + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | |
| 284 | dst_byte = a->dst_address.as_u8[dst_address_byte_index]; |
| 285 | |
| 286 | /* Number of bits next plies <= 0 => insert leaves this ply. */ |
| 287 | if (n_dst_bits_next_plies <= 0) |
| 288 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 289 | /* The mask length of the address to insert maps to this ply */ |
Neale Ranns | 6ff0549 | 2017-06-06 06:52:14 -0700 | [diff] [blame] | 290 | uword old_leaf_is_terminal; |
| 291 | u32 i, n_dst_bits_this_ply; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 293 | /* The number of bits, and hence slots/buckets, we will fill */ |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 294 | n_dst_bits_this_ply = clib_min (8, -n_dst_bits_next_plies); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 295 | ASSERT ((a->dst_address.as_u8[dst_address_byte_index] & |
| 296 | pow2_mask (n_dst_bits_this_ply)) == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 297 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 298 | /* Starting at the value of the byte at this section of the v4 address |
| 299 | * fill the buckets/slots of the ply */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 300 | for (i = dst_byte; i < dst_byte + (1 << n_dst_bits_this_ply); i++) |
| 301 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 302 | ip4_fib_mtrie_8_ply_t *new_ply; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | |
| 304 | old_leaf = old_ply->leaves[i]; |
| 305 | old_leaf_is_terminal = ip4_fib_mtrie_leaf_is_terminal (old_leaf); |
| 306 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | if (a->dst_address_length >= old_ply->dst_address_bits_of_leaves[i]) |
| 308 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 309 | /* The new leaf is more or equally specific than the one currently |
| 310 | * occupying the slot */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | new_leaf = ip4_fib_mtrie_leaf_set_adj_index (a->adj_index); |
| 312 | |
| 313 | if (old_leaf_is_terminal) |
| 314 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 315 | /* The current leaf is terminal, we can replace it with |
| 316 | * the new one */ |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 317 | old_ply->n_non_empty_leafs -= |
| 318 | ip4_fib_mtrie_leaf_is_non_empty (old_ply, i); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 319 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 320 | old_ply->dst_address_bits_of_leaves[i] = |
| 321 | a->dst_address_length; |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 322 | clib_atomic_cmp_and_swap (&old_ply->leaves[i], old_leaf, |
| 323 | new_leaf); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 324 | ASSERT (old_ply->leaves[i] == new_leaf); |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 325 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 326 | old_ply->n_non_empty_leafs += |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 327 | ip4_fib_mtrie_leaf_is_non_empty (old_ply, i); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 328 | ASSERT (old_ply->n_non_empty_leafs <= |
| 329 | ARRAY_LEN (old_ply->leaves)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 330 | } |
| 331 | else |
| 332 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 333 | /* Existing leaf points to another ply. We need to place |
| 334 | * new_leaf into all more specific slots. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | new_ply = get_next_ply_for_leaf (m, old_leaf); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 336 | set_ply_with_more_specific_leaf (m, new_ply, new_leaf, |
| 337 | a->dst_address_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 340 | else if (!old_leaf_is_terminal) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 342 | /* The current leaf is less specific and not termial (i.e. a ply), |
| 343 | * recurse on down the trie */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | new_ply = get_next_ply_for_leaf (m, old_leaf); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 345 | set_leaf (m, a, new_ply - ip4_ply_pool, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 346 | dst_address_byte_index + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | } |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 348 | /* |
| 349 | * else |
| 350 | * the route we are adding is less specific than the leaf currently |
| 351 | * occupying this slot. leave it there |
| 352 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | else |
| 356 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 357 | /* The address to insert requires us to move down at a lower level of |
| 358 | * the trie - recurse on down */ |
| 359 | ip4_fib_mtrie_8_ply_t *new_ply; |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 360 | u8 ply_base_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 362 | ply_base_len = 8 * (dst_address_byte_index + 1); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 363 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | old_leaf = old_ply->leaves[dst_byte]; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 365 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | if (ip4_fib_mtrie_leaf_is_terminal (old_leaf)) |
| 367 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 368 | /* There is a leaf occupying the slot. Replace it with a new ply */ |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 369 | old_ply->n_non_empty_leafs -= |
| 370 | ip4_fib_mtrie_leaf_is_non_empty (old_ply, dst_byte); |
| 371 | |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 372 | new_leaf = |
| 373 | ply_create (m, old_leaf, |
| 374 | old_ply->dst_address_bits_of_leaves[dst_byte], |
| 375 | ply_base_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | new_ply = get_next_ply_for_leaf (m, new_leaf); |
| 377 | |
| 378 | /* Refetch since ply_create may move pool. */ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 379 | old_ply = pool_elt_at_index (ip4_ply_pool, old_ply_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 381 | clib_atomic_cmp_and_swap (&old_ply->leaves[dst_byte], old_leaf, |
| 382 | new_leaf); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 383 | ASSERT (old_ply->leaves[dst_byte] == new_leaf); |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 384 | old_ply->dst_address_bits_of_leaves[dst_byte] = ply_base_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 386 | old_ply->n_non_empty_leafs += |
| 387 | ip4_fib_mtrie_leaf_is_non_empty (old_ply, dst_byte); |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 388 | ASSERT (old_ply->n_non_empty_leafs >= 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | } |
| 390 | else |
| 391 | new_ply = get_next_ply_for_leaf (m, old_leaf); |
| 392 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 393 | set_leaf (m, a, new_ply - ip4_ply_pool, dst_address_byte_index + 1); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | static void |
| 398 | set_root_leaf (ip4_fib_mtrie_t * m, |
| 399 | const ip4_fib_mtrie_set_unset_leaf_args_t * a) |
| 400 | { |
| 401 | ip4_fib_mtrie_leaf_t old_leaf, new_leaf; |
| 402 | ip4_fib_mtrie_16_ply_t *old_ply; |
| 403 | i32 n_dst_bits_next_plies; |
| 404 | u16 dst_byte; |
| 405 | |
| 406 | old_ply = &m->root_ply; |
| 407 | |
Neale Ranns | f060930 | 2017-04-11 09:13:39 -0700 | [diff] [blame] | 408 | ASSERT (a->dst_address_length <= 32); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 409 | |
| 410 | /* how many bits of the destination address are in the next PLY */ |
| 411 | n_dst_bits_next_plies = a->dst_address_length - BITS (u16); |
| 412 | |
| 413 | dst_byte = a->dst_address.as_u16[0]; |
| 414 | |
| 415 | /* Number of bits next plies <= 0 => insert leaves this ply. */ |
| 416 | if (n_dst_bits_next_plies <= 0) |
| 417 | { |
| 418 | /* The mask length of the address to insert maps to this ply */ |
Neale Ranns | 6ff0549 | 2017-06-06 06:52:14 -0700 | [diff] [blame] | 419 | uword old_leaf_is_terminal; |
| 420 | u32 i, n_dst_bits_this_ply; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 421 | |
| 422 | /* The number of bits, and hence slots/buckets, we will fill */ |
| 423 | n_dst_bits_this_ply = 16 - a->dst_address_length; |
| 424 | ASSERT ((clib_host_to_net_u16 (a->dst_address.as_u16[0]) & |
| 425 | pow2_mask (n_dst_bits_this_ply)) == 0); |
| 426 | |
| 427 | /* Starting at the value of the byte at this section of the v4 address |
| 428 | * fill the buckets/slots of the ply */ |
| 429 | for (i = 0; i < (1 << n_dst_bits_this_ply); i++) |
| 430 | { |
| 431 | ip4_fib_mtrie_8_ply_t *new_ply; |
| 432 | u16 slot; |
| 433 | |
| 434 | slot = clib_net_to_host_u16 (dst_byte); |
| 435 | slot += i; |
| 436 | slot = clib_host_to_net_u16 (slot); |
| 437 | |
| 438 | old_leaf = old_ply->leaves[slot]; |
| 439 | old_leaf_is_terminal = ip4_fib_mtrie_leaf_is_terminal (old_leaf); |
| 440 | |
| 441 | if (a->dst_address_length >= |
| 442 | old_ply->dst_address_bits_of_leaves[slot]) |
| 443 | { |
| 444 | /* The new leaf is more or equally specific than the one currently |
| 445 | * occupying the slot */ |
| 446 | new_leaf = ip4_fib_mtrie_leaf_set_adj_index (a->adj_index); |
| 447 | |
| 448 | if (old_leaf_is_terminal) |
| 449 | { |
| 450 | /* The current leaf is terminal, we can replace it with |
| 451 | * the new one */ |
| 452 | old_ply->dst_address_bits_of_leaves[slot] = |
| 453 | a->dst_address_length; |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 454 | clib_atomic_cmp_and_swap (&old_ply->leaves[slot], |
| 455 | old_leaf, new_leaf); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 456 | ASSERT (old_ply->leaves[slot] == new_leaf); |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | /* Existing leaf points to another ply. We need to place |
| 461 | * new_leaf into all more specific slots. */ |
| 462 | new_ply = get_next_ply_for_leaf (m, old_leaf); |
| 463 | set_ply_with_more_specific_leaf (m, new_ply, new_leaf, |
| 464 | a->dst_address_length); |
| 465 | } |
| 466 | } |
| 467 | else if (!old_leaf_is_terminal) |
| 468 | { |
| 469 | /* The current leaf is less specific and not termial (i.e. a ply), |
| 470 | * recurse on down the trie */ |
| 471 | new_ply = get_next_ply_for_leaf (m, old_leaf); |
| 472 | set_leaf (m, a, new_ply - ip4_ply_pool, 2); |
| 473 | } |
| 474 | /* |
| 475 | * else |
| 476 | * the route we are adding is less specific than the leaf currently |
| 477 | * occupying this slot. leave it there |
| 478 | */ |
| 479 | } |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | /* The address to insert requires us to move down at a lower level of |
| 484 | * the trie - recurse on down */ |
| 485 | ip4_fib_mtrie_8_ply_t *new_ply; |
| 486 | u8 ply_base_len; |
| 487 | |
| 488 | ply_base_len = 16; |
| 489 | |
| 490 | old_leaf = old_ply->leaves[dst_byte]; |
| 491 | |
| 492 | if (ip4_fib_mtrie_leaf_is_terminal (old_leaf)) |
| 493 | { |
| 494 | /* There is a leaf occupying the slot. Replace it with a new ply */ |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 495 | new_leaf = |
| 496 | ply_create (m, old_leaf, |
| 497 | old_ply->dst_address_bits_of_leaves[dst_byte], |
| 498 | ply_base_len); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 499 | new_ply = get_next_ply_for_leaf (m, new_leaf); |
| 500 | |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 501 | clib_atomic_cmp_and_swap (&old_ply->leaves[dst_byte], old_leaf, |
| 502 | new_leaf); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 503 | ASSERT (old_ply->leaves[dst_byte] == new_leaf); |
| 504 | old_ply->dst_address_bits_of_leaves[dst_byte] = ply_base_len; |
| 505 | } |
| 506 | else |
| 507 | new_ply = get_next_ply_for_leaf (m, old_leaf); |
| 508 | |
| 509 | set_leaf (m, a, new_ply - ip4_ply_pool, 2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | static uword |
| 514 | unset_leaf (ip4_fib_mtrie_t * m, |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 515 | const ip4_fib_mtrie_set_unset_leaf_args_t * a, |
| 516 | ip4_fib_mtrie_8_ply_t * old_ply, u32 dst_address_byte_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | { |
| 518 | ip4_fib_mtrie_leaf_t old_leaf, del_leaf; |
| 519 | i32 n_dst_bits_next_plies; |
Dave Barach | 6f6f34f | 2016-08-08 13:05:31 -0400 | [diff] [blame] | 520 | i32 i, n_dst_bits_this_ply, old_leaf_is_terminal; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | u8 dst_byte; |
| 522 | |
Neale Ranns | f060930 | 2017-04-11 09:13:39 -0700 | [diff] [blame] | 523 | ASSERT (a->dst_address_length <= 32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 524 | ASSERT (dst_address_byte_index < ARRAY_LEN (a->dst_address.as_u8)); |
| 525 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 526 | n_dst_bits_next_plies = |
| 527 | a->dst_address_length - BITS (u8) * (dst_address_byte_index + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 528 | |
| 529 | dst_byte = a->dst_address.as_u8[dst_address_byte_index]; |
| 530 | if (n_dst_bits_next_plies < 0) |
| 531 | dst_byte &= ~pow2_mask (-n_dst_bits_next_plies); |
| 532 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 533 | n_dst_bits_this_ply = |
| 534 | n_dst_bits_next_plies <= 0 ? -n_dst_bits_next_plies : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | n_dst_bits_this_ply = clib_min (8, n_dst_bits_this_ply); |
| 536 | |
| 537 | del_leaf = ip4_fib_mtrie_leaf_set_adj_index (a->adj_index); |
| 538 | |
| 539 | for (i = dst_byte; i < dst_byte + (1 << n_dst_bits_this_ply); i++) |
| 540 | { |
| 541 | old_leaf = old_ply->leaves[i]; |
| 542 | old_leaf_is_terminal = ip4_fib_mtrie_leaf_is_terminal (old_leaf); |
| 543 | |
| 544 | if (old_leaf == del_leaf |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 545 | || (!old_leaf_is_terminal |
| 546 | && unset_leaf (m, a, get_next_ply_for_leaf (m, old_leaf), |
| 547 | dst_address_byte_index + 1))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | { |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 549 | old_ply->n_non_empty_leafs -= |
| 550 | ip4_fib_mtrie_leaf_is_non_empty (old_ply, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 551 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 552 | old_ply->leaves[i] = |
| 553 | ip4_fib_mtrie_leaf_set_adj_index (a->cover_adj_index); |
mu.duojiao | 9744e6d | 2018-10-17 10:59:09 +0800 | [diff] [blame] | 554 | old_ply->dst_address_bits_of_leaves[i] = a->cover_address_length; |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 555 | |
| 556 | old_ply->n_non_empty_leafs += |
| 557 | ip4_fib_mtrie_leaf_is_non_empty (old_ply, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | |
| 559 | ASSERT (old_ply->n_non_empty_leafs >= 0); |
| 560 | if (old_ply->n_non_empty_leafs == 0 && dst_address_byte_index > 0) |
| 561 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 562 | pool_put (ip4_ply_pool, old_ply); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | /* Old ply was deleted. */ |
| 564 | return 1; |
| 565 | } |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 566 | #if CLIB_DEBUG > 0 |
| 567 | else if (dst_address_byte_index) |
| 568 | { |
| 569 | int ii, count = 0; |
| 570 | for (ii = 0; ii < ARRAY_LEN (old_ply->leaves); ii++) |
| 571 | { |
| 572 | count += ip4_fib_mtrie_leaf_is_non_empty (old_ply, ii); |
| 573 | } |
| 574 | ASSERT (count); |
| 575 | } |
| 576 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | /* Old ply was not deleted. */ |
| 581 | return 0; |
| 582 | } |
| 583 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 584 | static void |
| 585 | unset_root_leaf (ip4_fib_mtrie_t * m, |
| 586 | const ip4_fib_mtrie_set_unset_leaf_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 587 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 588 | ip4_fib_mtrie_leaf_t old_leaf, del_leaf; |
| 589 | i32 n_dst_bits_next_plies; |
| 590 | i32 i, n_dst_bits_this_ply, old_leaf_is_terminal; |
| 591 | u16 dst_byte; |
| 592 | ip4_fib_mtrie_16_ply_t *old_ply; |
| 593 | |
Neale Ranns | f060930 | 2017-04-11 09:13:39 -0700 | [diff] [blame] | 594 | ASSERT (a->dst_address_length <= 32); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 595 | |
| 596 | old_ply = &m->root_ply; |
| 597 | n_dst_bits_next_plies = a->dst_address_length - BITS (u16); |
| 598 | |
| 599 | dst_byte = a->dst_address.as_u16[0]; |
| 600 | |
| 601 | n_dst_bits_this_ply = (n_dst_bits_next_plies <= 0 ? |
| 602 | (16 - a->dst_address_length) : 0); |
| 603 | |
| 604 | del_leaf = ip4_fib_mtrie_leaf_set_adj_index (a->adj_index); |
| 605 | |
| 606 | /* Starting at the value of the byte at this section of the v4 address |
| 607 | * fill the buckets/slots of the ply */ |
| 608 | for (i = 0; i < (1 << n_dst_bits_this_ply); i++) |
| 609 | { |
| 610 | u16 slot; |
| 611 | |
| 612 | slot = clib_net_to_host_u16 (dst_byte); |
| 613 | slot += i; |
| 614 | slot = clib_host_to_net_u16 (slot); |
| 615 | |
| 616 | old_leaf = old_ply->leaves[slot]; |
| 617 | old_leaf_is_terminal = ip4_fib_mtrie_leaf_is_terminal (old_leaf); |
| 618 | |
| 619 | if (old_leaf == del_leaf |
| 620 | || (!old_leaf_is_terminal |
| 621 | && unset_leaf (m, a, get_next_ply_for_leaf (m, old_leaf), 2))) |
| 622 | { |
| 623 | old_ply->leaves[slot] = |
| 624 | ip4_fib_mtrie_leaf_set_adj_index (a->cover_adj_index); |
| 625 | old_ply->dst_address_bits_of_leaves[slot] = a->cover_address_length; |
| 626 | } |
| 627 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 631 | ip4_fib_mtrie_route_add (ip4_fib_mtrie_t * m, |
| 632 | const ip4_address_t * dst_address, |
| 633 | u32 dst_address_length, u32 adj_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 634 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | ip4_fib_mtrie_set_unset_leaf_args_t a; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 636 | ip4_main_t *im = &ip4_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 637 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | /* Honor dst_address_length. Fib masks are in network byte order */ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 639 | a.dst_address.as_u32 = (dst_address->as_u32 & |
| 640 | im->fib_masks[dst_address_length]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 641 | a.dst_address_length = dst_address_length; |
| 642 | a.adj_index = adj_index; |
| 643 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 644 | set_root_leaf (m, &a); |
| 645 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 646 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 647 | void |
| 648 | ip4_fib_mtrie_route_del (ip4_fib_mtrie_t * m, |
| 649 | const ip4_address_t * dst_address, |
| 650 | u32 dst_address_length, |
| 651 | u32 adj_index, |
| 652 | u32 cover_address_length, u32 cover_adj_index) |
| 653 | { |
| 654 | ip4_fib_mtrie_set_unset_leaf_args_t a; |
| 655 | ip4_main_t *im = &ip4_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 657 | /* Honor dst_address_length. Fib masks are in network byte order */ |
| 658 | a.dst_address.as_u32 = (dst_address->as_u32 & |
| 659 | im->fib_masks[dst_address_length]); |
| 660 | a.dst_address_length = dst_address_length; |
| 661 | a.adj_index = adj_index; |
| 662 | a.cover_adj_index = cover_adj_index; |
| 663 | a.cover_address_length = cover_address_length; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 664 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 665 | /* the top level ply is never removed */ |
| 666 | unset_root_leaf (m, &a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 669 | /* Returns number of bytes of memory used by mtrie. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 670 | static uword |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 671 | mtrie_ply_memory_usage (ip4_fib_mtrie_t * m, ip4_fib_mtrie_8_ply_t * p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 672 | { |
| 673 | uword bytes, i; |
| 674 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 675 | bytes = sizeof (p[0]); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 676 | for (i = 0; i < ARRAY_LEN (p->leaves); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | { |
| 678 | ip4_fib_mtrie_leaf_t l = p->leaves[i]; |
| 679 | if (ip4_fib_mtrie_leaf_is_next_ply (l)) |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 680 | bytes += mtrie_ply_memory_usage (m, get_next_ply_for_leaf (m, l)); |
| 681 | } |
| 682 | |
| 683 | return bytes; |
| 684 | } |
| 685 | |
| 686 | /* Returns number of bytes of memory used by mtrie. */ |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 687 | uword |
| 688 | ip4_fib_mtrie_memory_usage (ip4_fib_mtrie_t * m) |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 689 | { |
| 690 | uword bytes, i; |
| 691 | |
| 692 | bytes = sizeof (*m); |
| 693 | for (i = 0; i < ARRAY_LEN (m->root_ply.leaves); i++) |
| 694 | { |
| 695 | ip4_fib_mtrie_leaf_t l = m->root_ply.leaves[i]; |
| 696 | if (ip4_fib_mtrie_leaf_is_next_ply (l)) |
| 697 | bytes += mtrie_ply_memory_usage (m, get_next_ply_for_leaf (m, l)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | return bytes; |
| 701 | } |
| 702 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 703 | static u8 * |
| 704 | format_ip4_fib_mtrie_leaf (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 705 | { |
| 706 | ip4_fib_mtrie_leaf_t l = va_arg (*va, ip4_fib_mtrie_leaf_t); |
| 707 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 708 | if (ip4_fib_mtrie_leaf_is_terminal (l)) |
| 709 | s = format (s, "lb-index %d", ip4_fib_mtrie_leaf_get_adj_index (l)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | else |
| 711 | s = format (s, "next ply %d", ip4_fib_mtrie_leaf_get_next_ply_index (l)); |
| 712 | return s; |
| 713 | } |
| 714 | |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 715 | #define FORMAT_PLY(s, _p, _a, _i, _base_address, _ply_max_len, _indent) \ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 716 | ({ \ |
| 717 | u32 a, ia_length; \ |
| 718 | ip4_address_t ia; \ |
| 719 | ip4_fib_mtrie_leaf_t _l = p->leaves[(_i)]; \ |
| 720 | \ |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 721 | a = (_base_address) + ((_a) << (32 - (_ply_max_len))); \ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 722 | ia.as_u32 = clib_host_to_net_u32 (a); \ |
| 723 | ia_length = (_p)->dst_address_bits_of_leaves[(_i)]; \ |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 724 | s = format (s, "\n%U%U %U", \ |
| 725 | format_white_space, (_indent) + 4, \ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 726 | format_ip4_address_and_length, &ia, ia_length, \ |
| 727 | format_ip4_fib_mtrie_leaf, _l); \ |
| 728 | \ |
| 729 | if (ip4_fib_mtrie_leaf_is_next_ply (_l)) \ |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 730 | s = format (s, "\n%U", \ |
| 731 | format_ip4_fib_mtrie_ply, m, a, (_indent) + 8, \ |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 732 | ip4_fib_mtrie_leaf_get_next_ply_index (_l)); \ |
| 733 | s; \ |
| 734 | }) |
| 735 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 736 | static u8 * |
| 737 | format_ip4_fib_mtrie_ply (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 738 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 739 | ip4_fib_mtrie_t *m = va_arg (*va, ip4_fib_mtrie_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | u32 base_address = va_arg (*va, u32); |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 741 | u32 indent = va_arg (*va, u32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 742 | u32 ply_index = va_arg (*va, u32); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 743 | ip4_fib_mtrie_8_ply_t *p; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 744 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 745 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 746 | p = pool_elt_at_index (ip4_ply_pool, ply_index); |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 747 | s = format (s, "%Uply index %d, %d non-empty leaves", |
| 748 | format_white_space, indent, ply_index, p->n_non_empty_leafs); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 749 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 750 | for (i = 0; i < ARRAY_LEN (p->leaves); i++) |
| 751 | { |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 752 | if (ip4_fib_mtrie_leaf_is_non_empty (p, i)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 753 | { |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 754 | s = FORMAT_PLY (s, p, i, i, base_address, |
Neale Ranns | 756cd94 | 2018-04-06 09:18:11 -0700 | [diff] [blame] | 755 | p->dst_address_bits_base + 8, indent); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | return s; |
| 760 | } |
| 761 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 762 | u8 * |
| 763 | format_ip4_fib_mtrie (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 764 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 765 | ip4_fib_mtrie_t *m = va_arg (*va, ip4_fib_mtrie_t *); |
Neale Ranns | 3919425 | 2017-11-27 01:03:25 -0800 | [diff] [blame] | 766 | int verbose = va_arg (*va, int); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 767 | ip4_fib_mtrie_16_ply_t *p; |
| 768 | u32 base_address = 0; |
| 769 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 770 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 771 | s = format (s, "%d plies, memory usage %U\n", |
| 772 | pool_elts (ip4_ply_pool), |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 773 | format_memory_size, ip4_fib_mtrie_memory_usage (m)); |
| 774 | s = format (s, "root-ply"); |
| 775 | p = &m->root_ply; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 776 | |
Neale Ranns | 3919425 | 2017-11-27 01:03:25 -0800 | [diff] [blame] | 777 | if (verbose) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | { |
Neale Ranns | 3919425 | 2017-11-27 01:03:25 -0800 | [diff] [blame] | 779 | s = format (s, "root-ply"); |
| 780 | p = &m->root_ply; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 781 | |
Neale Ranns | 3919425 | 2017-11-27 01:03:25 -0800 | [diff] [blame] | 782 | for (i = 0; i < ARRAY_LEN (p->leaves); i++) |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 783 | { |
Neale Ranns | 3919425 | 2017-11-27 01:03:25 -0800 | [diff] [blame] | 784 | u16 slot; |
| 785 | |
| 786 | slot = clib_host_to_net_u16 (i); |
| 787 | |
| 788 | if (p->dst_address_bits_of_leaves[slot] > 0) |
| 789 | { |
mu.duojiao | 59a8295 | 2018-10-11 14:27:30 +0800 | [diff] [blame] | 790 | s = FORMAT_PLY (s, p, i, slot, base_address, 16, 0); |
Neale Ranns | 3919425 | 2017-11-27 01:03:25 -0800 | [diff] [blame] | 791 | } |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 792 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return s; |
| 796 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 797 | |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 798 | /** Default heap size for the IPv4 mtries */ |
| 799 | #define IP4_FIB_DEFAULT_MTRIE_HEAP_SIZE (32<<20) |
| 800 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 801 | static clib_error_t * |
| 802 | ip4_mtrie_module_init (vlib_main_t * vm) |
| 803 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 804 | CLIB_UNUSED (ip4_fib_mtrie_8_ply_t * p); |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 805 | ip4_main_t *im = &ip4_main; |
| 806 | clib_error_t *error = NULL; |
| 807 | uword *old_heap; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 808 | |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 809 | if (0 == im->mtrie_heap_size) |
| 810 | im->mtrie_heap_size = IP4_FIB_DEFAULT_MTRIE_HEAP_SIZE; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 811 | #if USE_DLMALLOC == 0 |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 812 | im->mtrie_mheap = mheap_alloc (0, im->mtrie_heap_size); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 813 | #else |
| 814 | im->mtrie_mheap = create_mspace (im->mtrie_heap_size, 1 /* locked */ ); |
| 815 | #endif |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 816 | |
| 817 | /* Burn one ply so index 0 is taken */ |
| 818 | old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 819 | pool_get (ip4_ply_pool, p); |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 820 | clib_mem_set_heap (old_heap); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 821 | |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 822 | return (error); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | VLIB_INIT_FUNCTION (ip4_mtrie_module_init); |
| 826 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 827 | /* |
| 828 | * fd.io coding-style-patch-verification: ON |
| 829 | * |
| 830 | * Local Variables: |
| 831 | * eval: (c-set-style "gnu") |
| 832 | * End: |
| 833 | */ |