FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
ip4_fib.h
Go to the documentation of this file.
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>
37 #include <vnet/fib/ip4_fib_8.h>
38 #include <vnet/fib/ip4_fib_16.h>
39 
40 // for the VPP_IP_FIB_MTRIE_16 definition
41 #include <vpp/vnet/config.h>
42 
43 /**
44  * the FIB module uses the 16-8-8 stride trie
45  */
46 #ifdef VPP_IP_FIB_MTRIE_16
47 typedef ip4_fib_16_t ip4_fib_t;
48 
49 #define ip4_fibs ip4_fib_16s
50 #define ip4_fib_table_lookup ip4_fib_16_table_lookup
51 #define ip4_fib_table_lookup_exact_match ip4_fib_16_table_lookup_exact_match
52 #define ip4_fib_table_entry_remove ip4_fib_16_table_entry_remove
53 #define ip4_fib_table_entry_insert ip4_fib_16_table_entry_insert
54 #define ip4_fib_table_fwding_dpo_update ip4_fib_16_table_fwding_dpo_update
55 #define ip4_fib_table_fwding_dpo_remove ip4_fib_16_table_fwding_dpo_remove
56 #define ip4_fib_table_lookup_lb ip4_fib_16_table_lookup_lb
57 #define ip4_fib_table_walk ip4_fib_16_table_walk
58 #define ip4_fib_table_sub_tree_walk ip4_fib_16_table_sub_tree_walk
59 #define ip4_fib_table_init ip4_fib_16_table_init
60 #define ip4_fib_table_free ip4_fib_16_table_free
61 #define ip4_mtrie_memory_usage ip4_mtrie_16_memory_usage
62 #define format_ip4_mtrie format_ip4_mtrie_16
63 
64 #else
66 
67 #define ip4_fibs ip4_fib_8s
68 #define ip4_fib_table_lookup ip4_fib_8_table_lookup
69 #define ip4_fib_table_lookup_exact_match ip4_fib_8_table_lookup_exact_match
70 #define ip4_fib_table_entry_remove ip4_fib_8_table_entry_remove
71 #define ip4_fib_table_entry_insert ip4_fib_8_table_entry_insert
72 #define ip4_fib_table_fwding_dpo_update ip4_fib_8_table_fwding_dpo_update
73 #define ip4_fib_table_fwding_dpo_remove ip4_fib_8_table_fwding_dpo_remove
74 #define ip4_fib_table_lookup_lb ip4_fib_8_table_lookup_lb
75 #define ip4_fib_table_walk ip4_fib_8_table_walk
76 #define ip4_fib_table_sub_tree_walk ip4_fib_8_table_sub_tree_walk
77 #define ip4_fib_table_init ip4_fib_8_table_init
78 #define ip4_fib_table_free ip4_fib_8_table_free
79 #define ip4_mtrie_memory_usage ip4_mtrie_8_memory_usage
80 #define format_ip4_mtrie format_ip4_mtrie_8
81 
82 #endif
83 
84 /**
85  * @brief Get the FIB at the given index
86  */
87 static inline ip4_fib_t *
89 {
90  return (pool_elt_at_index(ip4_fibs, index));
91 }
92 
95 {
97  ip4_fib_get(vec_elt (im->fib_index_by_sw_if_index, sw_if_index)),
98  dst));
99 }
100 
101 /**
102  * @brief Get or create an IPv4 fib.
103  *
104  * Get or create an IPv4 fib with the provided table ID.
105  *
106  * @param table_id
107  * When set to \c ~0, an arbitrary and unused fib ID is picked
108  * and can be retrieved with \c ret->table_id.
109  * Otherwise, the fib ID to be used to retrieve or create the desired fib.
110  * @returns A pointer to the retrieved or created fib.
111  *
112  */
114  fib_source_t src);
116 extern void ip4_fib_table_destroy(u32 fib_index);
117 
118 extern u8 *format_ip4_fib_table_memory(u8 * s, va_list * args);
119 
120 static inline
122 {
123  ip4_main_t * im = &ip4_main;
124  uword * p;
125 
126  p = hash_get (im->fib_index_by_table_id, table_id);
127  if (!p)
128  return ~0;
129 
130  return p[0];
131 }
132 
134 
135 #ifdef VPP_IP_FIB_MTRIE_16
137 ip4_fib_forwarding_lookup (u32 fib_index,
138  const ip4_address_t * addr)
139 {
140  ip4_mtrie_leaf_t leaf;
141  ip4_mtrie_16_t * mtrie;
142 
143  mtrie = &ip4_fib_get(fib_index)->mtrie;
144 
145  leaf = ip4_mtrie_16_lookup_step_one (mtrie, addr);
146  leaf = ip4_mtrie_16_lookup_step (leaf, addr, 2);
147  leaf = ip4_mtrie_16_lookup_step (leaf, addr, 3);
148 
149  return (ip4_mtrie_leaf_get_adj_index(leaf));
150 }
151 
154  u32 fib_index1,
155  const ip4_address_t * addr0,
156  const ip4_address_t * addr1,
157  index_t *lb0,
158  index_t *lb1)
159 {
160  ip4_mtrie_leaf_t leaf[2];
161  ip4_mtrie_16_t * mtrie[2];
162 
163  mtrie[0] = &ip4_fib_get(fib_index0)->mtrie;
164  mtrie[1] = &ip4_fib_get(fib_index1)->mtrie;
165 
166  leaf[0] = ip4_mtrie_16_lookup_step_one (mtrie[0], addr0);
167  leaf[1] = ip4_mtrie_16_lookup_step_one (mtrie[1], addr1);
168  leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 2);
169  leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 2);
170  leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 3);
171  leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 3);
172 
173  *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]);
174  *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]);
175 }
176 
179  u32 fib_index1,
180  u32 fib_index2,
181  u32 fib_index3,
182  const ip4_address_t * addr0,
183  const ip4_address_t * addr1,
184  const ip4_address_t * addr2,
185  const ip4_address_t * addr3,
186  index_t *lb0,
187  index_t *lb1,
188  index_t *lb2,
189  index_t *lb3)
190 {
191  ip4_mtrie_leaf_t leaf[4];
192  ip4_mtrie_16_t * mtrie[4];
193 
194  mtrie[0] = &ip4_fib_get(fib_index0)->mtrie;
195  mtrie[1] = &ip4_fib_get(fib_index1)->mtrie;
196  mtrie[2] = &ip4_fib_get(fib_index2)->mtrie;
197  mtrie[3] = &ip4_fib_get(fib_index3)->mtrie;
198 
199  leaf[0] = ip4_mtrie_16_lookup_step_one (mtrie[0], addr0);
200  leaf[1] = ip4_mtrie_16_lookup_step_one (mtrie[1], addr1);
201  leaf[2] = ip4_mtrie_16_lookup_step_one (mtrie[2], addr2);
202  leaf[3] = ip4_mtrie_16_lookup_step_one (mtrie[3], addr3);
203 
204  leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 2);
205  leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 2);
206  leaf[2] = ip4_mtrie_16_lookup_step (leaf[2], addr2, 2);
207  leaf[3] = ip4_mtrie_16_lookup_step (leaf[3], addr3, 2);
208 
209  leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 3);
210  leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 3);
211  leaf[2] = ip4_mtrie_16_lookup_step (leaf[2], addr2, 3);
212  leaf[3] = ip4_mtrie_16_lookup_step (leaf[3], addr3, 3);
213 
214  *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]);
215  *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]);
216  *lb2 = ip4_mtrie_leaf_get_adj_index(leaf[2]);
217  *lb3 = ip4_mtrie_leaf_get_adj_index(leaf[3]);
218 }
219 
220 #else
221 
224  const ip4_address_t * addr)
225 {
226  ip4_mtrie_leaf_t leaf;
227  ip4_mtrie_8_t * mtrie;
228 
229  mtrie = &ip4_fib_get(fib_index)->mtrie;
230 
231  leaf = ip4_mtrie_8_lookup_step_one (mtrie, addr);
232  leaf = ip4_mtrie_8_lookup_step (leaf, addr, 1);
233  leaf = ip4_mtrie_8_lookup_step (leaf, addr, 2);
234  leaf = ip4_mtrie_8_lookup_step (leaf, addr, 3);
235 
236  return (ip4_mtrie_leaf_get_adj_index(leaf));
237 }
238 
241  u32 fib_index1,
242  const ip4_address_t * addr0,
243  const ip4_address_t * addr1,
244  index_t *lb0,
245  index_t *lb1)
246 {
247  ip4_mtrie_leaf_t leaf[2];
248  ip4_mtrie_8_t * mtrie[2];
249 
250  mtrie[0] = &ip4_fib_get(fib_index0)->mtrie;
251  mtrie[1] = &ip4_fib_get(fib_index1)->mtrie;
252 
253  leaf[0] = ip4_mtrie_8_lookup_step_one (mtrie[0], addr0);
254  leaf[1] = ip4_mtrie_8_lookup_step_one (mtrie[1], addr1);
255  leaf[0] = ip4_mtrie_8_lookup_step (leaf[0], addr0, 1);
256  leaf[1] = ip4_mtrie_8_lookup_step (leaf[1], addr1, 1);
257  leaf[0] = ip4_mtrie_8_lookup_step (leaf[0], addr0, 2);
258  leaf[1] = ip4_mtrie_8_lookup_step (leaf[1], addr1, 2);
259  leaf[0] = ip4_mtrie_8_lookup_step (leaf[0], addr0, 3);
260  leaf[1] = ip4_mtrie_8_lookup_step (leaf[1], addr1, 3);
261 
262  *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]);
263  *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]);
264 }
265 
268  u32 fib_index1,
269  u32 fib_index2,
270  u32 fib_index3,
271  const ip4_address_t * addr0,
272  const ip4_address_t * addr1,
273  const ip4_address_t * addr2,
274  const ip4_address_t * addr3,
275  index_t *lb0,
276  index_t *lb1,
277  index_t *lb2,
278  index_t *lb3)
279 {
280  ip4_mtrie_leaf_t leaf[4];
281  ip4_mtrie_8_t * mtrie[4];
282 
283  mtrie[0] = &ip4_fib_get(fib_index0)->mtrie;
284  mtrie[1] = &ip4_fib_get(fib_index1)->mtrie;
285  mtrie[2] = &ip4_fib_get(fib_index2)->mtrie;
286  mtrie[3] = &ip4_fib_get(fib_index3)->mtrie;
287 
288  leaf[0] = ip4_mtrie_8_lookup_step_one (mtrie[0], addr0);
289  leaf[1] = ip4_mtrie_8_lookup_step_one (mtrie[1], addr1);
290  leaf[2] = ip4_mtrie_8_lookup_step_one (mtrie[2], addr2);
291  leaf[3] = ip4_mtrie_8_lookup_step_one (mtrie[3], addr3);
292 
293  leaf[0] = ip4_mtrie_8_lookup_step (leaf[0], addr0, 1);
294  leaf[1] = ip4_mtrie_8_lookup_step (leaf[1], addr1, 1);
295  leaf[2] = ip4_mtrie_8_lookup_step (leaf[2], addr2, 1);
296  leaf[3] = ip4_mtrie_8_lookup_step (leaf[3], addr3, 1);
297 
298  leaf[0] = ip4_mtrie_8_lookup_step (leaf[0], addr0, 2);
299  leaf[1] = ip4_mtrie_8_lookup_step (leaf[1], addr1, 2);
300  leaf[2] = ip4_mtrie_8_lookup_step (leaf[2], addr2, 2);
301  leaf[3] = ip4_mtrie_8_lookup_step (leaf[3], addr3, 2);
302 
303  leaf[0] = ip4_mtrie_8_lookup_step (leaf[0], addr0, 3);
304  leaf[1] = ip4_mtrie_8_lookup_step (leaf[1], addr1, 3);
305  leaf[2] = ip4_mtrie_8_lookup_step (leaf[2], addr2, 3);
306  leaf[3] = ip4_mtrie_8_lookup_step (leaf[3], addr3, 3);
307 
308  *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]);
309  *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]);
310  *lb2 = ip4_mtrie_leaf_get_adj_index(leaf[2]);
311  *lb3 = ip4_mtrie_leaf_get_adj_index(leaf[3]);
312 }
313 
314 #endif
315 
316 #endif
vlib.h
im
vnet_interface_main_t * im
Definition: interface_output.c:415
fib_entry.h
ip4_main
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1104
ip4_fib_table_get_index_for_sw_if_index
u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index)
Definition: pnat_test_stubs.h:21
ip4_mtrie_leaf_t
u32 ip4_mtrie_leaf_t
Definition: ip4_mtrie.h:52
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
ip4_fib_8_t_::mtrie
ip4_mtrie_8_t mtrie
Mtrie for fast lookups.
Definition: ip4_fib_8.h:45
ip4_fib_t
ip4_fib_8_t ip4_fib_t
The IPv4 FIB.
Definition: ip4_fib.h:65
ip4_mtrie_8_t
The mutiway-TRIE with a 8-8-8-8 stride.
Definition: ip4_mtrie.h:142
fib_table.h
ip4_fib_8_t_
The IPv4 FIB.
Definition: ip4_fib_8.h:36
ip4_fib_table_lookup_lb
#define ip4_fib_table_lookup_lb
Definition: ip4_fib.h:74
ip4_mtrie_leaf_get_adj_index
static u32 ip4_mtrie_leaf_get_adj_index(ip4_mtrie_leaf_t n)
From the stored slot value extract the LB index value.
Definition: ip4_mtrie.h:210
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
ip4_mtrie_16_lookup_step_one
static ip4_mtrie_leaf_t ip4_mtrie_16_lookup_step_one(const ip4_mtrie_16_t *m, const ip4_address_t *dst_address)
Lookup step number 1.
Definition: ip4_mtrie.h:241
vec_elt
#define vec_elt(v, i)
Get vector value at index i.
Definition: vec_bootstrap.h:210
hash_get
#define hash_get(h, key)
Definition: hash.h:249
ip4_fib_table_find_or_create_and_lock
u32 ip4_fib_table_find_or_create_and_lock(u32 table_id, fib_source_t src)
Get or create an IPv4 fib.
Definition: ip4_fib.c:228
index_t
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:43
static_always_inline
#define static_always_inline
Definition: clib.h:112
ip4_fib_table_create_and_lock
u32 ip4_fib_table_create_and_lock(fib_source_t src)
Definition: ip4_fib.c:243
uword
u64 uword
Definition: types.h:112
ip4_fib_forwarding_lookup_x4
static_always_inline void ip4_fib_forwarding_lookup_x4(u32 fib_index0, u32 fib_index1, u32 fib_index2, u32 fib_index3, const ip4_address_t *addr0, const ip4_address_t *addr1, const ip4_address_t *addr2, const ip4_address_t *addr3, index_t *lb0, index_t *lb1, index_t *lb2, index_t *lb3)
Definition: ip4_fib.h:267
ip4_fib_get
static ip4_fib_t * ip4_fib_get(u32 index)
Get the FIB at the given index.
Definition: ip4_fib.h:88
src
vl_api_address_t src
Definition: gre.api:54
ip4_fib_8.h
ip4_address_t
Definition: ip4_packet.h:50
ip4_fibs
#define ip4_fibs
Definition: ip4_fib.h:67
ip4_fib_forwarding_lookup_x2
static_always_inline void ip4_fib_forwarding_lookup_x2(u32 fib_index0, u32 fib_index1, const ip4_address_t *addr0, const ip4_address_t *addr1, index_t *lb0, index_t *lb1)
Definition: ip4_fib.h:240
ip4_mtrie_8_lookup_step
static ip4_mtrie_leaf_t ip4_mtrie_8_lookup_step(ip4_mtrie_leaf_t current_leaf, const ip4_address_t *dst_address, u32 dst_address_byte_index)
Definition: ip4_mtrie.h:252
ip4_fib_lookup
static u32 ip4_fib_lookup(ip4_main_t *im, u32 sw_if_index, ip4_address_t *dst)
Definition: ip4_fib.h:94
index
u32 index
Definition: flow_types.api:221
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
ip4_fib_16.h
ip.h
u32
unsigned int u32
Definition: types.h:88
table_id
u32 table_id
Definition: wireguard.api:102
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
ip4_mtrie_16_lookup_step
static ip4_mtrie_leaf_t ip4_mtrie_16_lookup_step(ip4_mtrie_leaf_t current_leaf, const ip4_address_t *dst_address, u32 dst_address_byte_index)
Lookup step.
Definition: ip4_mtrie.h:220
ip4_mtrie_8_lookup_step_one
static ip4_mtrie_leaf_t ip4_mtrie_8_lookup_step_one(const ip4_mtrie_8_t *m, const ip4_address_t *dst_address)
Definition: ip4_mtrie.h:270
ip4_fib_index_from_table_id
static u32 ip4_fib_index_from_table_id(u32 table_id)
Definition: ip4_fib.h:121
u8
unsigned char u8
Definition: types.h:56
ip4_fib_table_destroy
void ip4_fib_table_destroy(u32 fib_index)
Definition: ip4_fib.c:189
fib_source_t
enum fib_source_t_ fib_source_t
The different sources that can create a route.
format_ip4_fib_table_memory
u8 * format_ip4_fib_table_memory(u8 *s, va_list *args)
Definition: ip4_fib.c:322
ip4_fib_forwarding_lookup
static index_t ip4_fib_forwarding_lookup(u32 fib_index, const ip4_address_t *addr)
Definition: ip4_fib.h:223
ip4_mtrie_16_t
The mutiway-TRIE with a 16-8-8 stride.
Definition: ip4_mtrie.h:128
ip4_main_t
IPv4 main type.
Definition: ip4.h:107
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
ip4_fib_16_t_
The IPv4 FIB.
Definition: ip4_fib_16.h:36