blob: 706bebbce09084e2e0b7031dd19a0f3aa99cbbab [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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 Ranns5a59b2b2020-10-19 14:47:20 +000025#include <vppinfra/bihash_24_8.h>
26#include <vppinfra/bihash_template.h>
27
28/*
29 * Default size of the ip6 fib hash table
30 */
31#define IP6_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
32#define IP6_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
33
34/**
35 * Enumeration of the FIB table instance types
36 */
37typedef enum ip6_fib_table_instance_type_t_
38{
39 /**
40 * This table stores the routes that are used to forward traffic.
41 * The key is the prefix, the result the adjacency to forward on.
42 */
43 IP6_FIB_TABLE_FWDING,
44 /**
45 * The table that stores ALL routes learned by the DP.
46 * Some of these routes may not be ready to install in forwarding
47 * at a given time.
48 * The key in this table is the prefix, the result is the fib_entry_t
49 */
50 IP6_FIB_TABLE_NON_FWDING,
51} ip6_fib_table_instance_type_t;
52
53#define IP6_FIB_NUM_TABLES (IP6_FIB_TABLE_NON_FWDING+1)
54
55/**
56 * A representation of a single IP6 table
57 */
58typedef struct ip6_fib_table_instance_t_
59{
60 /* The hash table */
61 clib_bihash_24_8_t ip6_hash;
62
63 /* bitmap / refcounts / vector of mask widths to search */
64 uword *non_empty_dst_address_length_bitmap;
65 u8 *prefix_lengths_in_search_order;
66 i32 dst_address_length_refcounts[129];
67} ip6_fib_table_instance_t;
68
69/**
70 * The two FIB tables; fwding and non-fwding
71 */
72extern ip6_fib_table_instance_t ip6_fib_table[IP6_FIB_NUM_TABLES];
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073
74extern fib_node_index_t ip6_fib_table_lookup(u32 fib_index,
75 const ip6_address_t *addr,
76 u32 len);
77extern fib_node_index_t ip6_fib_table_lookup_exact_match(u32 fib_index,
78 const ip6_address_t *addr,
79 u32 len);
80
81extern void ip6_fib_table_entry_remove(u32 fib_index,
82 const ip6_address_t *addr,
83 u32 len);
84
85extern void ip6_fib_table_entry_insert(u32 fib_index,
86 const ip6_address_t *addr,
87 u32 len,
88 fib_node_index_t fib_entry_index);
89extern void ip6_fib_table_destroy(u32 fib_index);
90
91extern void ip6_fib_table_fwding_dpo_update(u32 fib_index,
92 const ip6_address_t *addr,
93 u32 len,
94 const dpo_id_t *dpo);
95
96extern void ip6_fib_table_fwding_dpo_remove(u32 fib_index,
97 const ip6_address_t *addr,
98 u32 len,
99 const dpo_id_t *dpo);
100
101u32 ip6_fib_table_fwding_lookup_with_if_index(ip6_main_t * im,
102 u32 sw_if_index,
103 const ip6_address_t * dst);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100104
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100105/**
Neale Ranns32e1c012016-11-22 17:07:28 +0000106 * @brief Walk all entries in a FIB table
107 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
108 * table and store elements in a vector, then delete the elements
109 */
110extern void ip6_fib_table_walk(u32 fib_index,
111 fib_table_walk_fn_t fn,
112 void *ctx);
113
Neale Ranns630198f2017-05-22 09:20:20 -0400114always_inline u32
Simon Zhange7eba482019-08-25 15:30:45 +0800115ip6_fib_table_fwding_lookup (u32 fib_index,
Neale Ranns630198f2017-05-22 09:20:20 -0400116 const ip6_address_t * dst)
117{
Dave Barach908a5ea2017-07-14 12:42:21 -0400118 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -0800119 clib_bihash_kv_24_8_t kv, value;
Neale Ranns630198f2017-05-22 09:20:20 -0400120 int i, len;
121 int rv;
Neale Ranns630198f2017-05-22 09:20:20 -0400122 u64 fib;
123
Neale Ranns5a59b2b2020-10-19 14:47:20 +0000124 table = &ip6_fib_table[IP6_FIB_TABLE_FWDING];
Neale Ranns630198f2017-05-22 09:20:20 -0400125 len = vec_len (table->prefix_lengths_in_search_order);
126
127 kv.key[0] = dst->as_u64[0];
128 kv.key[1] = dst->as_u64[1];
129 fib = ((u64)((fib_index))<<32);
130
131 for (i = 0; i < len; i++)
132 {
133 int dst_address_length = table->prefix_lengths_in_search_order[i];
134 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
135
136 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
137 //As lengths are decreasing, masks are increasingly specific.
138 kv.key[0] &= mask->as_u64[0];
139 kv.key[1] &= mask->as_u64[1];
140 kv.key[2] = fib | dst_address_length;
141
Neale Rannsae809832018-11-23 09:00:27 -0800142 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
Neale Ranns630198f2017-05-22 09:20:20 -0400143 if (rv == 0)
144 return value.value;
145 }
146
147 /* default route is always present */
148 ASSERT(0);
149 return 0;
150}
151
Neale Ranns32e1c012016-11-22 17:07:28 +0000152/**
Neale Ranns89541992017-04-06 04:41:02 -0700153 * @brief Walk all entries in a sub-tree of the FIB table
154 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
155 * table and store elements in a vector, then delete the elements
156 */
157extern void ip6_fib_table_sub_tree_walk(u32 fib_index,
158 const fib_prefix_t *root,
159 fib_table_walk_fn_t fn,
160 void *ctx);
161
162/**
AkshayaNadahalli8ea6d712017-02-07 23:59:54 +0530163 * @brief return the DPO that the LB stacks on.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164 */
165always_inline u32
166ip6_src_lookup_for_packet (ip6_main_t * im,
167 vlib_buffer_t * b,
168 ip6_header_t * i)
169{
170 if (vnet_buffer (b)->ip.adj_index[VLIB_RX] == ~0)
171 {
172 const dpo_id_t *dpo;
173 index_t lbi;
174
175 lbi = ip6_fib_table_fwding_lookup_with_if_index(
176 im,
177 vnet_buffer (b)->sw_if_index[VLIB_RX],
178 &i->src_address);
179
180 dpo = load_balance_get_bucket_i(load_balance_get(lbi), 0);
181
182 if (dpo_is_adj(dpo))
183 {
184 vnet_buffer (b)->ip.adj_index[VLIB_RX] = dpo->dpoi_index;
185 }
186 }
187 return vnet_buffer (b)->ip.adj_index[VLIB_RX];
188}
189
190/**
191 * \brief Get or create an IPv6 fib.
192 *
193 * Get or create an IPv4 fib with the provided table ID.
194 *
195 * \param im
196 * ip4_main pointer.
197 * \param table_id
198 * When set to \c ~0, an arbitrary and unused fib ID is picked
199 * and can be retrieved with \c ret->table_id.
200 * Otherwise, the fib ID to be used to retrieve or create the desired fib.
201 * \returns A pointer to the retrieved or created fib.
202 *
203 */
Neale Ranns15002542017-09-10 04:39:11 -0700204extern u32 ip6_fib_table_find_or_create_and_lock(u32 table_id,
205 fib_source_t src);
Neale Ranns53da2212018-02-24 02:11:19 -0800206extern u32 ip6_fib_table_create_and_lock(fib_source_t src,
207 fib_table_flags_t flags,
208 u8* desc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100209
Neale Rannsc87aafa2017-11-29 00:59:31 -0800210extern u8 *format_ip6_fib_table_memory(u8 * s, va_list * args);
211
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100212static inline ip6_fib_t *
213ip6_fib_get (fib_node_index_t index)
214{
215 ASSERT(!pool_is_free_index(ip6_main.fibs, index));
Neale Rannsa3af3372017-03-28 03:49:52 -0700216 return (pool_elt_at_index (ip6_main.v6_fibs, index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100217}
218
219static inline
220u32 ip6_fib_index_from_table_id (u32 table_id)
221{
222 ip6_main_t * im = &ip6_main;
223 uword * p;
224
225 p = hash_get (im->fib_index_by_table_id, table_id);
226 if (!p)
227 return ~0;
228
229 return p[0];
230}
231
232extern u32 ip6_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
233
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100234#endif
235