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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
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
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>
41 * the FIB module uses the 16-8-8 stride trie
43 typedef ip4_fib_16_t ip4_fib_t;
45 #define ip4_fibs ip4_fib_16s
46 #define ip4_fib_table_lookup ip4_fib_16_table_lookup
47 #define ip4_fib_table_lookup_exact_match ip4_fib_16_table_lookup_exact_match
48 #define ip4_fib_table_entry_remove ip4_fib_16_table_entry_remove
49 #define ip4_fib_table_entry_insert ip4_fib_16_table_entry_insert
50 #define ip4_fib_table_fwding_dpo_update ip4_fib_16_table_fwding_dpo_update
51 #define ip4_fib_table_fwding_dpo_remove ip4_fib_16_table_fwding_dpo_remove
52 #define ip4_fib_table_lookup_lb ip4_fib_16_table_lookup_lb
53 #define ip4_fib_table_walk ip4_fib_16_table_walk
54 #define ip4_fib_table_sub_tree_walk ip4_fib_16_table_sub_tree_walk
55 #define ip4_fib_table_init ip4_fib_16_table_init
56 #define ip4_fib_table_free ip4_fib_16_table_free
57 #define ip4_mtrie_memory_usage ip4_mtrie_16_memory_usage
58 #define format_ip4_mtrie format_ip4_mtrie_16
61 * @brief Get the FIB at the given index
63 static inline ip4_fib_t *
64 ip4_fib_get (u32 index)
66 return (pool_elt_at_index(ip4_fibs, index));
70 ip4_fib_lookup (ip4_main_t * im, u32 sw_if_index, ip4_address_t * dst)
72 return (ip4_fib_table_lookup_lb(
73 ip4_fib_get(vec_elt (im->fib_index_by_sw_if_index, sw_if_index)),
78 * @brief Get or create an IPv4 fib.
80 * Get or create an IPv4 fib with the provided table ID.
83 * When set to \c ~0, an arbitrary and unused fib ID is picked
84 * and can be retrieved with \c ret->table_id.
85 * Otherwise, the fib ID to be used to retrieve or create the desired fib.
86 * @returns A pointer to the retrieved or created fib.
89 extern u32 ip4_fib_table_find_or_create_and_lock(u32 table_id,
91 extern u32 ip4_fib_table_create_and_lock(fib_source_t src);
92 extern void ip4_fib_table_destroy(u32 fib_index);
94 extern u8 *format_ip4_fib_table_memory(u8 * s, va_list * args);
97 u32 ip4_fib_index_from_table_id (u32 table_id)
99 ip4_main_t * im = &ip4_main;
102 p = hash_get (im->fib_index_by_table_id, table_id);
109 extern u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
111 always_inline index_t
112 ip4_fib_forwarding_lookup (u32 fib_index,
113 const ip4_address_t * addr)
115 ip4_mtrie_leaf_t leaf;
116 ip4_mtrie_16_t * mtrie;
118 mtrie = &ip4_fib_get(fib_index)->mtrie;
120 leaf = ip4_mtrie_16_lookup_step_one (mtrie, addr);
121 leaf = ip4_mtrie_16_lookup_step (leaf, addr, 2);
122 leaf = ip4_mtrie_16_lookup_step (leaf, addr, 3);
124 return (ip4_mtrie_leaf_get_adj_index(leaf));
127 static_always_inline void
128 ip4_fib_forwarding_lookup_x2 (u32 fib_index0,
130 const ip4_address_t * addr0,
131 const ip4_address_t * addr1,
135 ip4_mtrie_leaf_t leaf[2];
136 ip4_mtrie_16_t * mtrie[2];
138 mtrie[0] = &ip4_fib_get(fib_index0)->mtrie;
139 mtrie[1] = &ip4_fib_get(fib_index1)->mtrie;
141 leaf[0] = ip4_mtrie_16_lookup_step_one (mtrie[0], addr0);
142 leaf[1] = ip4_mtrie_16_lookup_step_one (mtrie[1], addr1);
143 leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 2);
144 leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 2);
145 leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 3);
146 leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 3);
148 *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]);
149 *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]);
152 static_always_inline void
153 ip4_fib_forwarding_lookup_x4 (u32 fib_index0,
157 const ip4_address_t * addr0,
158 const ip4_address_t * addr1,
159 const ip4_address_t * addr2,
160 const ip4_address_t * addr3,
166 ip4_mtrie_leaf_t leaf[4];
167 ip4_mtrie_16_t * mtrie[4];
169 mtrie[0] = &ip4_fib_get(fib_index0)->mtrie;
170 mtrie[1] = &ip4_fib_get(fib_index1)->mtrie;
171 mtrie[2] = &ip4_fib_get(fib_index2)->mtrie;
172 mtrie[3] = &ip4_fib_get(fib_index3)->mtrie;
174 leaf[0] = ip4_mtrie_16_lookup_step_one (mtrie[0], addr0);
175 leaf[1] = ip4_mtrie_16_lookup_step_one (mtrie[1], addr1);
176 leaf[2] = ip4_mtrie_16_lookup_step_one (mtrie[2], addr2);
177 leaf[3] = ip4_mtrie_16_lookup_step_one (mtrie[3], addr3);
179 leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 2);
180 leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 2);
181 leaf[2] = ip4_mtrie_16_lookup_step (leaf[2], addr2, 2);
182 leaf[3] = ip4_mtrie_16_lookup_step (leaf[3], addr3, 2);
184 leaf[0] = ip4_mtrie_16_lookup_step (leaf[0], addr0, 3);
185 leaf[1] = ip4_mtrie_16_lookup_step (leaf[1], addr1, 3);
186 leaf[2] = ip4_mtrie_16_lookup_step (leaf[2], addr2, 3);
187 leaf[3] = ip4_mtrie_16_lookup_step (leaf[3], addr3, 3);
189 *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]);
190 *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]);
191 *lb2 = ip4_mtrie_leaf_get_adj_index(leaf[2]);
192 *lb3 = ip4_mtrie_leaf_get_adj_index(leaf[3]);