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 | #ifndef __IP6_FIB_H__ |
| 17 | #define __IP6_FIB_H__ |
| 18 | |
| 19 | #include <vlib/vlib.h> |
| 20 | #include <vnet/ip/format.h> |
| 21 | #include <vnet/fib/fib_entry.h> |
| 22 | #include <vnet/fib/fib_table.h> |
| 23 | #include <vnet/ip/lookup.h> |
| 24 | #include <vnet/dpo/load_balance.h> |
Neale Ranns | 5a59b2b | 2020-10-19 14:47:20 +0000 | [diff] [blame] | 25 | #include <vppinfra/bihash_24_8.h> |
| 26 | #include <vppinfra/bihash_template.h> |
| 27 | |
| 28 | /* |
Benoît Ganne | 23c4896 | 2024-04-05 09:45:29 +0200 | [diff] [blame] | 29 | * Default size of the ip6 fib forwarding hash table |
Neale Ranns | 5a59b2b | 2020-10-19 14:47:20 +0000 | [diff] [blame] | 30 | */ |
| 31 | #define IP6_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024) |
| 32 | #define IP6_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20) |
| 33 | |
| 34 | /** |
Benoît Ganne | 23c4896 | 2024-04-05 09:45:29 +0200 | [diff] [blame] | 35 | * A representation the forwarding IP6 table |
Neale Ranns | 5a59b2b | 2020-10-19 14:47:20 +0000 | [diff] [blame] | 36 | */ |
Benoît Ganne | 23c4896 | 2024-04-05 09:45:29 +0200 | [diff] [blame] | 37 | typedef struct ip6_fib_fwding_table_instance_t_ |
Neale Ranns | 5a59b2b | 2020-10-19 14:47:20 +0000 | [diff] [blame] | 38 | { |
| 39 | /* The hash table */ |
| 40 | clib_bihash_24_8_t ip6_hash; |
| 41 | |
| 42 | /* bitmap / refcounts / vector of mask widths to search */ |
| 43 | uword *non_empty_dst_address_length_bitmap; |
| 44 | u8 *prefix_lengths_in_search_order; |
| 45 | i32 dst_address_length_refcounts[129]; |
Benoît Ganne | 23c4896 | 2024-04-05 09:45:29 +0200 | [diff] [blame] | 46 | } ip6_fib_fwding_table_instance_t; |
Neale Ranns | 5a59b2b | 2020-10-19 14:47:20 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * The two FIB tables; fwding and non-fwding |
| 50 | */ |
Benoît Ganne | 23c4896 | 2024-04-05 09:45:29 +0200 | [diff] [blame] | 51 | extern ip6_fib_fwding_table_instance_t ip6_fib_fwding_table; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 52 | |
| 53 | extern fib_node_index_t ip6_fib_table_lookup(u32 fib_index, |
| 54 | const ip6_address_t *addr, |
| 55 | u32 len); |
| 56 | extern fib_node_index_t ip6_fib_table_lookup_exact_match(u32 fib_index, |
| 57 | const ip6_address_t *addr, |
| 58 | u32 len); |
| 59 | |
| 60 | extern void ip6_fib_table_entry_remove(u32 fib_index, |
| 61 | const ip6_address_t *addr, |
| 62 | u32 len); |
| 63 | |
| 64 | extern void ip6_fib_table_entry_insert(u32 fib_index, |
| 65 | const ip6_address_t *addr, |
| 66 | u32 len, |
| 67 | fib_node_index_t fib_entry_index); |
| 68 | extern void ip6_fib_table_destroy(u32 fib_index); |
| 69 | |
| 70 | extern void ip6_fib_table_fwding_dpo_update(u32 fib_index, |
| 71 | const ip6_address_t *addr, |
| 72 | u32 len, |
| 73 | const dpo_id_t *dpo); |
| 74 | |
| 75 | extern void ip6_fib_table_fwding_dpo_remove(u32 fib_index, |
| 76 | const ip6_address_t *addr, |
| 77 | u32 len, |
| 78 | const dpo_id_t *dpo); |
| 79 | |
| 80 | u32 ip6_fib_table_fwding_lookup_with_if_index(ip6_main_t * im, |
| 81 | u32 sw_if_index, |
| 82 | const ip6_address_t * dst); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 83 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 84 | /** |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 85 | * @brief Walk all entries in a FIB table |
| 86 | * N.B: This is NOT safe to deletes. If you need to delete walk the whole |
| 87 | * table and store elements in a vector, then delete the elements |
| 88 | */ |
| 89 | extern void ip6_fib_table_walk(u32 fib_index, |
| 90 | fib_table_walk_fn_t fn, |
| 91 | void *ctx); |
| 92 | |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 93 | always_inline u32 |
Simon Zhang | e7eba48 | 2019-08-25 15:30:45 +0800 | [diff] [blame] | 94 | ip6_fib_table_fwding_lookup (u32 fib_index, |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 95 | const ip6_address_t * dst) |
| 96 | { |
Benoît Ganne | 23c4896 | 2024-04-05 09:45:29 +0200 | [diff] [blame] | 97 | ip6_fib_fwding_table_instance_t *table; |
Neale Ranns | ae80983 | 2018-11-23 09:00:27 -0800 | [diff] [blame] | 98 | clib_bihash_kv_24_8_t kv, value; |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 99 | int i, len; |
| 100 | int rv; |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 101 | u64 fib; |
| 102 | |
Benoît Ganne | 23c4896 | 2024-04-05 09:45:29 +0200 | [diff] [blame] | 103 | table = &ip6_fib_fwding_table; |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 104 | len = vec_len (table->prefix_lengths_in_search_order); |
| 105 | |
| 106 | kv.key[0] = dst->as_u64[0]; |
| 107 | kv.key[1] = dst->as_u64[1]; |
| 108 | fib = ((u64)((fib_index))<<32); |
| 109 | |
| 110 | for (i = 0; i < len; i++) |
| 111 | { |
| 112 | int dst_address_length = table->prefix_lengths_in_search_order[i]; |
| 113 | ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length]; |
| 114 | |
| 115 | ASSERT(dst_address_length >= 0 && dst_address_length <= 128); |
| 116 | //As lengths are decreasing, masks are increasingly specific. |
| 117 | kv.key[0] &= mask->as_u64[0]; |
| 118 | kv.key[1] &= mask->as_u64[1]; |
| 119 | kv.key[2] = fib | dst_address_length; |
| 120 | |
Neale Ranns | ae80983 | 2018-11-23 09:00:27 -0800 | [diff] [blame] | 121 | rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value); |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 122 | if (rv == 0) |
| 123 | return value.value; |
| 124 | } |
| 125 | |
| 126 | /* default route is always present */ |
| 127 | ASSERT(0); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 131 | /** |
Neale Ranns | 8954199 | 2017-04-06 04:41:02 -0700 | [diff] [blame] | 132 | * @brief Walk all entries in a sub-tree of the FIB table |
| 133 | * N.B: This is NOT safe to deletes. If you need to delete walk the whole |
| 134 | * table and store elements in a vector, then delete the elements |
| 135 | */ |
| 136 | extern void ip6_fib_table_sub_tree_walk(u32 fib_index, |
| 137 | const fib_prefix_t *root, |
| 138 | fib_table_walk_fn_t fn, |
| 139 | void *ctx); |
| 140 | |
| 141 | /** |
AkshayaNadahalli | 8ea6d71 | 2017-02-07 23:59:54 +0530 | [diff] [blame] | 142 | * @brief return the DPO that the LB stacks on. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 143 | */ |
| 144 | always_inline u32 |
| 145 | ip6_src_lookup_for_packet (ip6_main_t * im, |
| 146 | vlib_buffer_t * b, |
| 147 | ip6_header_t * i) |
| 148 | { |
| 149 | if (vnet_buffer (b)->ip.adj_index[VLIB_RX] == ~0) |
| 150 | { |
| 151 | const dpo_id_t *dpo; |
| 152 | index_t lbi; |
| 153 | |
| 154 | lbi = ip6_fib_table_fwding_lookup_with_if_index( |
| 155 | im, |
| 156 | vnet_buffer (b)->sw_if_index[VLIB_RX], |
| 157 | &i->src_address); |
| 158 | |
| 159 | dpo = load_balance_get_bucket_i(load_balance_get(lbi), 0); |
| 160 | |
| 161 | if (dpo_is_adj(dpo)) |
| 162 | { |
| 163 | vnet_buffer (b)->ip.adj_index[VLIB_RX] = dpo->dpoi_index; |
| 164 | } |
| 165 | } |
| 166 | return vnet_buffer (b)->ip.adj_index[VLIB_RX]; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * \brief Get or create an IPv6 fib. |
| 171 | * |
| 172 | * Get or create an IPv4 fib with the provided table ID. |
| 173 | * |
| 174 | * \param im |
| 175 | * ip4_main pointer. |
| 176 | * \param table_id |
| 177 | * When set to \c ~0, an arbitrary and unused fib ID is picked |
| 178 | * and can be retrieved with \c ret->table_id. |
| 179 | * Otherwise, the fib ID to be used to retrieve or create the desired fib. |
| 180 | * \returns A pointer to the retrieved or created fib. |
| 181 | * |
| 182 | */ |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 183 | extern u32 ip6_fib_table_find_or_create_and_lock(u32 table_id, |
| 184 | fib_source_t src); |
Neale Ranns | 53da221 | 2018-02-24 02:11:19 -0800 | [diff] [blame] | 185 | extern u32 ip6_fib_table_create_and_lock(fib_source_t src, |
| 186 | fib_table_flags_t flags, |
| 187 | u8* desc); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 188 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 189 | extern u8 *format_ip6_fib_table_memory(u8 * s, va_list * args); |
| 190 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 191 | static inline ip6_fib_t * |
| 192 | ip6_fib_get (fib_node_index_t index) |
| 193 | { |
| 194 | ASSERT(!pool_is_free_index(ip6_main.fibs, index)); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 195 | return (pool_elt_at_index (ip6_main.v6_fibs, index)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static inline |
| 199 | u32 ip6_fib_index_from_table_id (u32 table_id) |
| 200 | { |
| 201 | ip6_main_t * im = &ip6_main; |
| 202 | uword * p; |
| 203 | |
| 204 | p = hash_get (im->fib_index_by_table_id, table_id); |
| 205 | if (!p) |
| 206 | return ~0; |
| 207 | |
| 208 | return p[0]; |
| 209 | } |
| 210 | |
| 211 | extern u32 ip6_fib_table_get_index_for_sw_if_index(u32 sw_if_index); |
Benoît Ganne | 23c4896 | 2024-04-05 09:45:29 +0200 | [diff] [blame] | 212 | extern void ip6_fib_table_show (vlib_main_t *vm, fib_table_t *fib_table, int summary); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 213 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 214 | #endif |
| 215 | |