ip: Move the IP6 fib into ip6_[m]fib.c
[vpp.git] / src / vnet / fib / ip6_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 #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>
25 #include <vppinfra/bihash_24_8.h>
26 #include <vppinfra/bihash_template.h>
27
28 /*
29  * Default size of the ip6 fib hash table
30  */
31 #define IP6_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
32 #define IP6_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
33
34 /**
35  * Enumeration of the FIB table instance types
36  */
37 typedef enum ip6_fib_table_instance_type_t_
38 {
39     /**
40      * This table stores the routes that are used to forward traffic.
41      * The key is the prefix, the result the adjacency to forward on.
42      */
43   IP6_FIB_TABLE_FWDING,
44     /**
45      * The table that stores ALL routes learned by the DP.
46      * Some of these routes may not be ready to install in forwarding
47      * at a given time.
48      * The key in this table is the prefix, the result is the fib_entry_t
49      */
50   IP6_FIB_TABLE_NON_FWDING,
51 } ip6_fib_table_instance_type_t;
52
53 #define IP6_FIB_NUM_TABLES (IP6_FIB_TABLE_NON_FWDING+1)
54
55 /**
56  * A representation of a single IP6 table
57  */
58 typedef struct ip6_fib_table_instance_t_
59 {
60   /* The hash table */
61   clib_bihash_24_8_t ip6_hash;
62
63   /* bitmap / refcounts / vector of mask widths to search */
64   uword *non_empty_dst_address_length_bitmap;
65   u8 *prefix_lengths_in_search_order;
66   i32 dst_address_length_refcounts[129];
67 } ip6_fib_table_instance_t;
68
69 /**
70  * The two FIB tables; fwding and non-fwding
71  */
72 extern ip6_fib_table_instance_t ip6_fib_table[IP6_FIB_NUM_TABLES];
73
74 extern fib_node_index_t ip6_fib_table_lookup(u32 fib_index,
75                                              const ip6_address_t *addr,
76                                              u32 len);
77 extern fib_node_index_t ip6_fib_table_lookup_exact_match(u32 fib_index,
78                                                          const ip6_address_t *addr,
79                                                          u32 len);
80
81 extern void ip6_fib_table_entry_remove(u32 fib_index,
82                                        const ip6_address_t *addr,
83                                        u32 len);
84
85 extern void ip6_fib_table_entry_insert(u32 fib_index,
86                                        const ip6_address_t *addr,
87                                        u32 len,
88                                        fib_node_index_t fib_entry_index);
89 extern void ip6_fib_table_destroy(u32 fib_index);
90
91 extern void ip6_fib_table_fwding_dpo_update(u32 fib_index,
92                                             const ip6_address_t *addr,
93                                             u32 len,
94                                             const dpo_id_t *dpo);
95
96 extern void ip6_fib_table_fwding_dpo_remove(u32 fib_index,
97                                             const ip6_address_t *addr,
98                                             u32 len,
99                                             const dpo_id_t *dpo);
100
101 u32 ip6_fib_table_fwding_lookup_with_if_index(ip6_main_t * im,
102                                               u32 sw_if_index,
103                                               const ip6_address_t * dst);
104
105 /**
106  * @brief Walk all entries in a FIB table
107  * N.B: This is NOT safe to deletes. If you need to delete walk the whole
108  * table and store elements in a vector, then delete the elements
109  */
110 extern void ip6_fib_table_walk(u32 fib_index,
111                                fib_table_walk_fn_t fn,
112                                void *ctx);
113
114 always_inline u32
115 ip6_fib_table_fwding_lookup (u32 fib_index,
116                              const ip6_address_t * dst)
117 {
118     ip6_fib_table_instance_t *table;
119     clib_bihash_kv_24_8_t kv, value;
120     int i, len;
121     int rv;
122     u64 fib;
123
124     table = &ip6_fib_table[IP6_FIB_TABLE_FWDING];
125     len = vec_len (table->prefix_lengths_in_search_order);
126
127     kv.key[0] = dst->as_u64[0];
128     kv.key[1] = dst->as_u64[1];
129     fib = ((u64)((fib_index))<<32);
130
131     for (i = 0; i < len; i++)
132     {
133         int dst_address_length = table->prefix_lengths_in_search_order[i];
134         ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
135
136         ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
137         //As lengths are decreasing, masks are increasingly specific.
138         kv.key[0] &= mask->as_u64[0];
139         kv.key[1] &= mask->as_u64[1];
140         kv.key[2] = fib | dst_address_length;
141
142         rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
143         if (rv == 0)
144             return value.value;
145     }
146
147     /* default route is always present */
148     ASSERT(0);
149     return 0;
150 }
151
152 /**
153  * @brief Walk all entries in a sub-tree of the FIB table
154  * N.B: This is NOT safe to deletes. If you need to delete walk the whole
155  * table and store elements in a vector, then delete the elements
156  */
157 extern void ip6_fib_table_sub_tree_walk(u32 fib_index,
158                                         const fib_prefix_t *root,
159                                         fib_table_walk_fn_t fn,
160                                         void *ctx);
161
162 /**
163  * @brief return the DPO that the LB stacks on.
164  */
165 always_inline u32
166 ip6_src_lookup_for_packet (ip6_main_t * im,
167                            vlib_buffer_t * b,
168                            ip6_header_t * i)
169 {
170     if (vnet_buffer (b)->ip.adj_index[VLIB_RX] == ~0)
171     {
172         const dpo_id_t *dpo;
173         index_t lbi;
174
175         lbi = ip6_fib_table_fwding_lookup_with_if_index(
176                   im,
177                   vnet_buffer (b)->sw_if_index[VLIB_RX],
178                   &i->src_address);
179
180         dpo = load_balance_get_bucket_i(load_balance_get(lbi), 0);
181
182         if (dpo_is_adj(dpo))
183         {
184             vnet_buffer (b)->ip.adj_index[VLIB_RX] = dpo->dpoi_index;
185         }
186     }
187     return vnet_buffer (b)->ip.adj_index[VLIB_RX];
188 }
189
190 /**
191  * \brief Get or create an IPv6 fib.
192  *
193  * Get or create an IPv4 fib with the provided table ID.
194  *
195  * \param im
196  *      ip4_main pointer.
197  * \param table_id
198  *      When set to \c ~0, an arbitrary and unused fib ID is picked
199  *      and can be retrieved with \c ret->table_id.
200  *      Otherwise, the fib ID to be used to retrieve or create the desired fib.
201  * \returns A pointer to the retrieved or created fib.
202  *
203  */
204 extern u32 ip6_fib_table_find_or_create_and_lock(u32 table_id,
205                                                  fib_source_t src);
206 extern u32 ip6_fib_table_create_and_lock(fib_source_t src,
207                                          fib_table_flags_t flags,
208                                          u8* desc);
209
210 extern u8 *format_ip6_fib_table_memory(u8 * s, va_list * args);
211
212 static inline ip6_fib_t *
213 ip6_fib_get (fib_node_index_t index)
214 {
215     ASSERT(!pool_is_free_index(ip6_main.fibs, index));
216     return (pool_elt_at_index (ip6_main.v6_fibs, index));
217 }
218
219 static inline 
220 u32 ip6_fib_index_from_table_id (u32 table_id)
221 {
222   ip6_main_t * im = &ip6_main;
223   uword * p;
224
225   p = hash_get (im->fib_index_by_table_id, table_id);
226   if (!p)
227     return ~0;
228
229   return p[0];
230 }
231
232 extern u32 ip6_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
233
234 #endif
235