blob: 80d56b1a7ba18febbabaa9c767137f78a86c911d [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/*
Benoît Ganne23c48962024-04-05 09:45:29 +020029 * Default size of the ip6 fib forwarding hash table
Neale Ranns5a59b2b2020-10-19 14:47:20 +000030 */
31#define IP6_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
32#define IP6_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
33
34/**
Benoît Ganne23c48962024-04-05 09:45:29 +020035 * A representation the forwarding IP6 table
Neale Ranns5a59b2b2020-10-19 14:47:20 +000036 */
Benoît Ganne23c48962024-04-05 09:45:29 +020037typedef struct ip6_fib_fwding_table_instance_t_
Neale Ranns5a59b2b2020-10-19 14:47:20 +000038{
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 Ganne23c48962024-04-05 09:45:29 +020046} ip6_fib_fwding_table_instance_t;
Neale Ranns5a59b2b2020-10-19 14:47:20 +000047
48/**
49 * The two FIB tables; fwding and non-fwding
50 */
Benoît Ganne23c48962024-04-05 09:45:29 +020051extern ip6_fib_fwding_table_instance_t ip6_fib_fwding_table;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052
53extern fib_node_index_t ip6_fib_table_lookup(u32 fib_index,
54 const ip6_address_t *addr,
55 u32 len);
56extern fib_node_index_t ip6_fib_table_lookup_exact_match(u32 fib_index,
57 const ip6_address_t *addr,
58 u32 len);
59
60extern void ip6_fib_table_entry_remove(u32 fib_index,
61 const ip6_address_t *addr,
62 u32 len);
63
64extern 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);
68extern void ip6_fib_table_destroy(u32 fib_index);
69
70extern 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
75extern 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
80u32 ip6_fib_table_fwding_lookup_with_if_index(ip6_main_t * im,
81 u32 sw_if_index,
82 const ip6_address_t * dst);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083
Neale Ranns0bfe5d82016-08-25 15:29:12 +010084/**
Neale Ranns32e1c012016-11-22 17:07:28 +000085 * @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 */
89extern void ip6_fib_table_walk(u32 fib_index,
90 fib_table_walk_fn_t fn,
91 void *ctx);
92
Neale Ranns630198f2017-05-22 09:20:20 -040093always_inline u32
Simon Zhange7eba482019-08-25 15:30:45 +080094ip6_fib_table_fwding_lookup (u32 fib_index,
Neale Ranns630198f2017-05-22 09:20:20 -040095 const ip6_address_t * dst)
96{
Benoît Ganne23c48962024-04-05 09:45:29 +020097 ip6_fib_fwding_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -080098 clib_bihash_kv_24_8_t kv, value;
Neale Ranns630198f2017-05-22 09:20:20 -040099 int i, len;
100 int rv;
Neale Ranns630198f2017-05-22 09:20:20 -0400101 u64 fib;
102
Benoît Ganne23c48962024-04-05 09:45:29 +0200103 table = &ip6_fib_fwding_table;
Neale Ranns630198f2017-05-22 09:20:20 -0400104 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 Rannsae809832018-11-23 09:00:27 -0800121 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
Neale Ranns630198f2017-05-22 09:20:20 -0400122 if (rv == 0)
123 return value.value;
124 }
125
126 /* default route is always present */
127 ASSERT(0);
128 return 0;
129}
130
Neale Ranns32e1c012016-11-22 17:07:28 +0000131/**
Neale Ranns89541992017-04-06 04:41:02 -0700132 * @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 */
136extern 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/**
AkshayaNadahalli8ea6d712017-02-07 23:59:54 +0530142 * @brief return the DPO that the LB stacks on.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100143 */
144always_inline u32
145ip6_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 Ranns15002542017-09-10 04:39:11 -0700183extern u32 ip6_fib_table_find_or_create_and_lock(u32 table_id,
184 fib_source_t src);
Neale Ranns53da2212018-02-24 02:11:19 -0800185extern u32 ip6_fib_table_create_and_lock(fib_source_t src,
186 fib_table_flags_t flags,
187 u8* desc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100188
Neale Rannsc87aafa2017-11-29 00:59:31 -0800189extern u8 *format_ip6_fib_table_memory(u8 * s, va_list * args);
190
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191static inline ip6_fib_t *
192ip6_fib_get (fib_node_index_t index)
193{
194 ASSERT(!pool_is_free_index(ip6_main.fibs, index));
Neale Rannsa3af3372017-03-28 03:49:52 -0700195 return (pool_elt_at_index (ip6_main.v6_fibs, index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100196}
197
198static inline
199u32 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
211extern u32 ip6_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
Benoît Ganne23c48962024-04-05 09:45:29 +0200212extern void ip6_fib_table_show (vlib_main_t *vm, fib_table_t *fib_table, int summary);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100213
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100214#endif
215