Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | * @brief The IPv4 FIB |
| 17 | * |
| 18 | * FIBs are composed of two prefix data-bases (akak tables). The non-forwarding |
| 19 | * table contains all the routes that the control plane has programmed, the |
| 20 | * forwarding table contains the sub-set of those routes that can be used to |
| 21 | * forward packets. |
| 22 | * In the IPv4 FIB the non-forwarding table is an array of hash tables indexed |
| 23 | * by mask length, the forwarding table is an mtrie |
| 24 | * |
| 25 | * This IPv4 FIB is used by the protocol independent FIB. So directly using |
| 26 | * this APIs in client code is not encouraged. However, this IPv4 FIB can be |
| 27 | * used if all the client wants is an IPv4 prefix data-base |
| 28 | */ |
| 29 | |
| 30 | #ifndef __IP4_FIB_H__ |
| 31 | #define __IP4_FIB_H__ |
| 32 | |
| 33 | #include <vlib/vlib.h> |
| 34 | #include <vnet/ip/ip.h> |
| 35 | #include <vnet/fib/fib_entry.h> |
| 36 | #include <vnet/fib/fib_table.h> |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 37 | #include <vnet/ip/ip4_mtrie.h> |
| 38 | |
| 39 | typedef struct ip4_fib_t_ |
| 40 | { |
Neale Ranns | eb9df09 | 2018-07-03 23:29:49 -0700 | [diff] [blame] | 41 | /** Required for pool_get_aligned */ |
| 42 | CLIB_CACHE_LINE_ALIGN_MARK(cacheline0); |
| 43 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Mtrie for fast lookups. Hash is used to maintain overlapping prefixes. |
| 46 | * First member so it's in the first cacheline. |
| 47 | */ |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 48 | ip4_mtrie_16_t mtrie; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 49 | |
| 50 | /* Hash table for each prefix length mapping. */ |
| 51 | uword *fib_entry_by_dst_address[33]; |
| 52 | |
| 53 | /* Table ID (hash key) for this FIB. */ |
| 54 | u32 table_id; |
| 55 | |
| 56 | /* Index into FIB vector. */ |
| 57 | u32 index; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 58 | } ip4_fib_t; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 59 | |
| 60 | extern fib_node_index_t ip4_fib_table_lookup(const ip4_fib_t *fib, |
| 61 | const ip4_address_t *addr, |
| 62 | u32 len); |
| 63 | extern fib_node_index_t ip4_fib_table_lookup_exact_match(const ip4_fib_t *fib, |
| 64 | const ip4_address_t *addr, |
| 65 | u32 len); |
| 66 | |
| 67 | extern void ip4_fib_table_entry_remove(ip4_fib_t *fib, |
| 68 | const ip4_address_t *addr, |
| 69 | u32 len); |
| 70 | |
| 71 | extern void ip4_fib_table_entry_insert(ip4_fib_t *fib, |
| 72 | const ip4_address_t *addr, |
| 73 | u32 len, |
| 74 | fib_node_index_t fib_entry_index); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 75 | extern void ip4_fib_table_destroy(u32 fib_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 76 | |
| 77 | extern void ip4_fib_table_fwding_dpo_update(ip4_fib_t *fib, |
| 78 | const ip4_address_t *addr, |
| 79 | u32 len, |
| 80 | const dpo_id_t *dpo); |
| 81 | |
| 82 | extern void ip4_fib_table_fwding_dpo_remove(ip4_fib_t *fib, |
| 83 | const ip4_address_t *addr, |
| 84 | u32 len, |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 85 | const dpo_id_t *dpo, |
| 86 | fib_node_index_t cover_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 87 | extern u32 ip4_fib_table_lookup_lb (ip4_fib_t *fib, |
| 88 | const ip4_address_t * dst); |
| 89 | |
| 90 | /** |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 91 | * @brief Walk all entries in a FIB table |
| 92 | * N.B: This is NOT safe to deletes. If you need to delete walk the whole |
| 93 | * table and store elements in a vector, then delete the elements |
| 94 | */ |
| 95 | extern void ip4_fib_table_walk(ip4_fib_t *fib, |
| 96 | fib_table_walk_fn_t fn, |
| 97 | void *ctx); |
| 98 | |
| 99 | /** |
Neale Ranns | 8954199 | 2017-04-06 04:41:02 -0700 | [diff] [blame] | 100 | * @brief Walk all entries in a sub-tree of the FIB table |
| 101 | * N.B: This is NOT safe to deletes. If you need to delete walk the whole |
| 102 | * table and store elements in a vector, then delete the elements |
| 103 | */ |
| 104 | extern void ip4_fib_table_sub_tree_walk(ip4_fib_t *fib, |
| 105 | const fib_prefix_t *root, |
| 106 | fib_table_walk_fn_t fn, |
| 107 | void *ctx); |
| 108 | |
| 109 | /** |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 110 | * @brief Get the FIB at the given index |
| 111 | */ |
| 112 | static inline ip4_fib_t * |
| 113 | ip4_fib_get (u32 index) |
| 114 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 115 | return (pool_elt_at_index(ip4_main.v4_fibs, index)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | always_inline u32 |
| 119 | ip4_fib_lookup (ip4_main_t * im, u32 sw_if_index, ip4_address_t * dst) |
| 120 | { |
| 121 | return (ip4_fib_table_lookup_lb( |
| 122 | ip4_fib_get(vec_elt (im->fib_index_by_sw_if_index, sw_if_index)), |
| 123 | dst)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @brief Get or create an IPv4 fib. |
| 128 | * |
| 129 | * Get or create an IPv4 fib with the provided table ID. |
| 130 | * |
| 131 | * @param table_id |
| 132 | * When set to \c ~0, an arbitrary and unused fib ID is picked |
| 133 | * and can be retrieved with \c ret->table_id. |
| 134 | * Otherwise, the fib ID to be used to retrieve or create the desired fib. |
| 135 | * @returns A pointer to the retrieved or created fib. |
| 136 | * |
| 137 | */ |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 138 | extern u32 ip4_fib_table_find_or_create_and_lock(u32 table_id, |
| 139 | fib_source_t src); |
| 140 | extern u32 ip4_fib_table_create_and_lock(fib_source_t src); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 141 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 142 | extern u8 *format_ip4_fib_table_memory(u8 * s, va_list * args); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 143 | |
| 144 | static inline |
| 145 | u32 ip4_fib_index_from_table_id (u32 table_id) |
| 146 | { |
| 147 | ip4_main_t * im = &ip4_main; |
| 148 | uword * p; |
| 149 | |
| 150 | p = hash_get (im->fib_index_by_table_id, table_id); |
| 151 | if (!p) |
| 152 | return ~0; |
| 153 | |
| 154 | return p[0]; |
| 155 | } |
| 156 | |
| 157 | extern u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index); |
| 158 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 159 | always_inline index_t |
| 160 | ip4_fib_forwarding_lookup (u32 fib_index, |
| 161 | const ip4_address_t * addr) |
| 162 | { |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 163 | ip4_mtrie_leaf_t leaf; |
| 164 | ip4_mtrie_16_t * mtrie; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 165 | |
| 166 | mtrie = &ip4_fib_get(fib_index)->mtrie; |
| 167 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 168 | leaf = ip4_mtrie_16_lookup_step_one (mtrie, addr); |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame^] | 169 | leaf = ip4_mtrie_16_lookup_step (leaf, addr, 2); |
| 170 | leaf = ip4_mtrie_16_lookup_step (leaf, addr, 3); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 171 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 172 | return (ip4_mtrie_leaf_get_adj_index(leaf)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Neale Ranns | d724e4f | 2020-04-02 15:02:16 +0000 | [diff] [blame] | 175 | static_always_inline void |
| 176 | ip4_fib_forwarding_lookup_x2 (u32 fib_index0, |
| 177 | u32 fib_index1, |
| 178 | const ip4_address_t * addr0, |
| 179 | const ip4_address_t * addr1, |
| 180 | index_t *lb0, |
| 181 | index_t *lb1) |
| 182 | { |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 183 | ip4_mtrie_leaf_t leaf[2]; |
| 184 | ip4_mtrie_16_t * mtrie[2]; |
Neale Ranns | d724e4f | 2020-04-02 15:02:16 +0000 | [diff] [blame] | 185 | |
| 186 | mtrie[0] = &ip4_fib_get(fib_index0)->mtrie; |
| 187 | mtrie[1] = &ip4_fib_get(fib_index1)->mtrie; |
| 188 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 189 | leaf[0] = ip4_mtrie_16_lookup_step_one (mtrie[0], addr0); |
| 190 | leaf[1] = ip4_mtrie_16_lookup_step_one (mtrie[1], addr1); |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame^] | 191 | leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 2); |
| 192 | leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 2); |
| 193 | leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 3); |
| 194 | leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 3); |
Neale Ranns | d724e4f | 2020-04-02 15:02:16 +0000 | [diff] [blame] | 195 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 196 | *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]); |
| 197 | *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]); |
Neale Ranns | d724e4f | 2020-04-02 15:02:16 +0000 | [diff] [blame] | 198 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 199 | |
Neale Ranns | 31a4aa7 | 2021-08-10 12:35:57 +0000 | [diff] [blame] | 200 | static_always_inline void |
| 201 | ip4_fib_forwarding_lookup_x4 (u32 fib_index0, |
| 202 | u32 fib_index1, |
| 203 | u32 fib_index2, |
| 204 | u32 fib_index3, |
| 205 | const ip4_address_t * addr0, |
| 206 | const ip4_address_t * addr1, |
| 207 | const ip4_address_t * addr2, |
| 208 | const ip4_address_t * addr3, |
| 209 | index_t *lb0, |
| 210 | index_t *lb1, |
| 211 | index_t *lb2, |
| 212 | index_t *lb3) |
| 213 | { |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 214 | ip4_mtrie_leaf_t leaf[4]; |
| 215 | ip4_mtrie_16_t * mtrie[4]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 216 | |
Neale Ranns | 31a4aa7 | 2021-08-10 12:35:57 +0000 | [diff] [blame] | 217 | mtrie[0] = &ip4_fib_get(fib_index0)->mtrie; |
| 218 | mtrie[1] = &ip4_fib_get(fib_index1)->mtrie; |
| 219 | mtrie[2] = &ip4_fib_get(fib_index2)->mtrie; |
| 220 | mtrie[3] = &ip4_fib_get(fib_index3)->mtrie; |
| 221 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 222 | leaf[0] = ip4_mtrie_16_lookup_step_one (mtrie[0], addr0); |
| 223 | leaf[1] = ip4_mtrie_16_lookup_step_one (mtrie[1], addr1); |
| 224 | leaf[2] = ip4_mtrie_16_lookup_step_one (mtrie[2], addr2); |
| 225 | leaf[3] = ip4_mtrie_16_lookup_step_one (mtrie[3], addr3); |
Neale Ranns | 31a4aa7 | 2021-08-10 12:35:57 +0000 | [diff] [blame] | 226 | |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame^] | 227 | leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 2); |
| 228 | leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 2); |
| 229 | leaf[2] = ip4_mtrie_16_lookup_step (leaf[2], addr2, 2); |
| 230 | leaf[3] = ip4_mtrie_16_lookup_step (leaf[3], addr3, 2); |
Neale Ranns | 31a4aa7 | 2021-08-10 12:35:57 +0000 | [diff] [blame] | 231 | |
Neale Ranns | 7244a70 | 2021-08-06 13:12:00 +0000 | [diff] [blame^] | 232 | leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 3); |
| 233 | leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 3); |
| 234 | leaf[2] = ip4_mtrie_16_lookup_step (leaf[2], addr2, 3); |
| 235 | leaf[3] = ip4_mtrie_16_lookup_step (leaf[3], addr3, 3); |
Neale Ranns | 31a4aa7 | 2021-08-10 12:35:57 +0000 | [diff] [blame] | 236 | |
Neale Ranns | 6bb2db0 | 2021-08-06 12:24:14 +0000 | [diff] [blame] | 237 | *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]); |
| 238 | *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]); |
| 239 | *lb2 = ip4_mtrie_leaf_get_adj_index(leaf[2]); |
| 240 | *lb3 = ip4_mtrie_leaf_get_adj_index(leaf[3]); |
Neale Ranns | 31a4aa7 | 2021-08-10 12:35:57 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | #endif |