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/ip/ip4_mtrie.h>
39 typedef struct ip4_fib_t_
42 * Mtrie for fast lookups. Hash is used to maintain overlapping prefixes.
43 * First member so it's in the first cacheline.
45 ip4_fib_mtrie_t mtrie;
47 /* Hash table for each prefix length mapping. */
48 uword *fib_entry_by_dst_address[33];
50 /* Table ID (hash key) for this FIB. */
53 /* Index into FIB vector. */
56 /* flow hash configuration */
57 flow_hash_config_t flow_hash_config;
59 /* N-tuple classifier indices */
60 u32 fwd_classify_table_index;
61 u32 rev_classify_table_index;
65 extern fib_node_index_t ip4_fib_table_lookup(const ip4_fib_t *fib,
66 const ip4_address_t *addr,
68 extern fib_node_index_t ip4_fib_table_lookup_exact_match(const ip4_fib_t *fib,
69 const ip4_address_t *addr,
72 extern void ip4_fib_table_entry_remove(ip4_fib_t *fib,
73 const ip4_address_t *addr,
76 extern void ip4_fib_table_entry_insert(ip4_fib_t *fib,
77 const ip4_address_t *addr,
79 fib_node_index_t fib_entry_index);
80 extern void ip4_fib_table_destroy(u32 fib_index);
82 extern void ip4_fib_table_fwding_dpo_update(ip4_fib_t *fib,
83 const ip4_address_t *addr,
87 extern void ip4_fib_table_fwding_dpo_remove(ip4_fib_t *fib,
88 const ip4_address_t *addr,
91 fib_node_index_t cover_index);
92 extern u32 ip4_fib_table_lookup_lb (ip4_fib_t *fib,
93 const ip4_address_t * dst);
96 * @brief Walk all entries in a FIB table
97 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
98 * table and store elements in a vector, then delete the elements
100 extern void ip4_fib_table_walk(ip4_fib_t *fib,
101 fib_table_walk_fn_t fn,
105 * @brief Get the FIB at the given index
107 static inline ip4_fib_t *
108 ip4_fib_get (u32 index)
110 return (pool_elt_at_index(ip4_main.v4_fibs, index));
114 ip4_fib_lookup (ip4_main_t * im, u32 sw_if_index, ip4_address_t * dst)
116 return (ip4_fib_table_lookup_lb(
117 ip4_fib_get(vec_elt (im->fib_index_by_sw_if_index, sw_if_index)),
122 * @brief Get or create an IPv4 fib.
124 * Get or create an IPv4 fib with the provided table ID.
127 * When set to \c ~0, an arbitrary and unused fib ID is picked
128 * and can be retrieved with \c ret->table_id.
129 * Otherwise, the fib ID to be used to retrieve or create the desired fib.
130 * @returns A pointer to the retrieved or created fib.
133 extern u32 ip4_fib_table_find_or_create_and_lock(u32 table_id);
134 extern u32 ip4_fib_table_create_and_lock(void);
138 u32 ip4_fib_index_from_table_id (u32 table_id)
140 ip4_main_t * im = &ip4_main;
143 p = hash_get (im->fib_index_by_table_id, table_id);
150 extern u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
152 extern flow_hash_config_t ip4_fib_table_get_flow_hash_config(u32 fib_index);
155 always_inline index_t
156 ip4_fib_forwarding_lookup (u32 fib_index,
157 const ip4_address_t * addr)
159 ip4_fib_mtrie_leaf_t leaf;
160 ip4_fib_mtrie_t * mtrie;
162 mtrie = &ip4_fib_get(fib_index)->mtrie;
164 leaf = ip4_fib_mtrie_lookup_step_one (mtrie, addr);
165 leaf = ip4_fib_mtrie_lookup_step (mtrie, leaf, addr, 2);
166 leaf = ip4_fib_mtrie_lookup_step (mtrie, leaf, addr, 3);
168 return (ip4_fib_mtrie_leaf_get_adj_index(leaf));