fib: A 16-8-8 and a 8-8-8-8 versions of an ip4_fib_t
[vpp.git] / src / vnet / fib / ip4_fib.h
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 /**
41  * the FIB module uses the 16-8-8 stride trie
42  */
43 typedef ip4_fib_16_t ip4_fib_t;
44
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
59
60 /**
61  * @brief Get the FIB at the given index
62  */
63 static inline ip4_fib_t *
64 ip4_fib_get (u32 index)
65 {
66     return (pool_elt_at_index(ip4_fibs, index));
67 }
68
69 always_inline u32
70 ip4_fib_lookup (ip4_main_t * im, u32 sw_if_index, ip4_address_t * dst)
71 {
72     return (ip4_fib_table_lookup_lb(
73                 ip4_fib_get(vec_elt (im->fib_index_by_sw_if_index, sw_if_index)),
74                 dst));
75 }
76
77 /**
78  * @brief Get or create an IPv4 fib.
79  *
80  * Get or create an IPv4 fib with the provided table ID.
81  *
82  * @param 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.
87  *
88  */
89 extern u32 ip4_fib_table_find_or_create_and_lock(u32 table_id,
90                                                  fib_source_t src);
91 extern u32 ip4_fib_table_create_and_lock(fib_source_t src);
92 extern void ip4_fib_table_destroy(u32 fib_index);
93
94 extern u8 *format_ip4_fib_table_memory(u8 * s, va_list * args);
95
96 static inline 
97 u32 ip4_fib_index_from_table_id (u32 table_id)
98 {
99   ip4_main_t * im = &ip4_main;
100   uword * p;
101
102   p = hash_get (im->fib_index_by_table_id, table_id);
103   if (!p)
104     return ~0;
105
106   return p[0];
107 }
108
109 extern u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
110
111 always_inline index_t
112 ip4_fib_forwarding_lookup (u32 fib_index,
113                            const ip4_address_t * addr)
114 {
115     ip4_mtrie_leaf_t leaf;
116     ip4_mtrie_16_t * mtrie;
117
118     mtrie = &ip4_fib_get(fib_index)->mtrie;
119
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);
123
124     return (ip4_mtrie_leaf_get_adj_index(leaf));
125 }
126
127 static_always_inline void
128 ip4_fib_forwarding_lookup_x2 (u32 fib_index0,
129                               u32 fib_index1,
130                               const ip4_address_t * addr0,
131                               const ip4_address_t * addr1,
132                               index_t *lb0,
133                               index_t *lb1)
134 {
135     ip4_mtrie_leaf_t leaf[2];
136     ip4_mtrie_16_t * mtrie[2];
137
138     mtrie[0] = &ip4_fib_get(fib_index0)->mtrie;
139     mtrie[1] = &ip4_fib_get(fib_index1)->mtrie;
140
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);
147
148     *lb0 = ip4_mtrie_leaf_get_adj_index(leaf[0]);
149     *lb1 = ip4_mtrie_leaf_get_adj_index(leaf[1]);
150 }
151
152 static_always_inline void
153 ip4_fib_forwarding_lookup_x4 (u32 fib_index0,
154                               u32 fib_index1,
155                               u32 fib_index2,
156                               u32 fib_index3,
157                               const ip4_address_t * addr0,
158                               const ip4_address_t * addr1,
159                               const ip4_address_t * addr2,
160                               const ip4_address_t * addr3,
161                               index_t *lb0,
162                               index_t *lb1,
163                               index_t *lb2,
164                               index_t *lb3)
165 {
166     ip4_mtrie_leaf_t leaf[4];
167     ip4_mtrie_16_t * mtrie[4];
168
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;
173
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);
178
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);
183
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);
188
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]);
193 }
194
195 #endif