e460e2ffd09c61a89c3fc686ce759909cd036d83
[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/fib/fib_urpf_list.h>
24 #include <vnet/ip/lookup.h>
25 #include <vnet/dpo/load_balance.h>
26
27 extern fib_node_index_t ip6_fib_table_lookup(u32 fib_index,
28                                              const ip6_address_t *addr,
29                                              u32 len);
30 extern fib_node_index_t ip6_fib_table_lookup_exact_match(u32 fib_index,
31                                                          const ip6_address_t *addr,
32                                                          u32 len);
33
34 extern void ip6_fib_table_entry_remove(u32 fib_index,
35                                        const ip6_address_t *addr,
36                                        u32 len);
37
38 extern void ip6_fib_table_entry_insert(u32 fib_index,
39                                        const ip6_address_t *addr,
40                                        u32 len,
41                                        fib_node_index_t fib_entry_index);
42 extern void ip6_fib_table_destroy(u32 fib_index);
43
44 extern void ip6_fib_table_fwding_dpo_update(u32 fib_index,
45                                             const ip6_address_t *addr,
46                                             u32 len,
47                                             const dpo_id_t *dpo);
48
49 extern void ip6_fib_table_fwding_dpo_remove(u32 fib_index,
50                                             const ip6_address_t *addr,
51                                             u32 len,
52                                             const dpo_id_t *dpo);
53
54 u32 ip6_fib_table_fwding_lookup_with_if_index(ip6_main_t * im,
55                                               u32 sw_if_index,
56                                               const ip6_address_t * dst);
57 u32 ip6_fib_table_fwding_lookup(ip6_main_t * im,
58                                 u32 fib_index,
59                                 const ip6_address_t * dst);
60
61 /**
62  * @brief Walk all entries in a FIB table
63  * N.B: This is NOT safe to deletes. If you need to delete walk the whole
64  * table and store elements in a vector, then delete the elements
65  */
66 extern void ip6_fib_table_walk(u32 fib_index,
67                                fib_table_walk_fn_t fn,
68                                void *ctx);
69
70 /**
71  * @brief returns number of links on which src is reachable.
72  */
73 always_inline int
74 ip6_urpf_loose_check (ip6_main_t * im,
75                       vlib_buffer_t * b,
76                       ip6_header_t * i)
77 {
78     const load_balance_t *lb0;
79     index_t lbi;
80
81     lbi = ip6_fib_table_fwding_lookup_with_if_index(
82               im,
83               vnet_buffer (b)->sw_if_index[VLIB_RX],
84               &i->src_address);
85
86     lb0 = load_balance_get(lbi);
87
88     return (fib_urpf_check_size (lb0->lb_urpf));
89 }
90
91 /**
92  * @brief return the DPO that the LB stacks on.
93  */
94 always_inline u32
95 ip6_src_lookup_for_packet (ip6_main_t * im,
96                            vlib_buffer_t * b,
97                            ip6_header_t * i)
98 {
99     if (vnet_buffer (b)->ip.adj_index[VLIB_RX] == ~0)
100     {
101         const dpo_id_t *dpo;
102         index_t lbi;
103
104         lbi = ip6_fib_table_fwding_lookup_with_if_index(
105                   im,
106                   vnet_buffer (b)->sw_if_index[VLIB_RX],
107                   &i->src_address);
108
109         dpo = load_balance_get_bucket_i(load_balance_get(lbi), 0);
110
111         if (dpo_is_adj(dpo))
112         {
113             vnet_buffer (b)->ip.adj_index[VLIB_RX] = dpo->dpoi_index;
114         }
115     }
116     return vnet_buffer (b)->ip.adj_index[VLIB_RX];
117 }
118
119 /**
120  * \brief Get or create an IPv6 fib.
121  *
122  * Get or create an IPv4 fib with the provided table ID.
123  *
124  * \param im
125  *      ip4_main pointer.
126  * \param 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.
131  *
132  */
133 extern u32 ip6_fib_table_find_or_create_and_lock(u32 table_id);
134 extern u32 ip6_fib_table_create_and_lock(void);
135
136 static inline ip6_fib_t *
137 ip6_fib_get (fib_node_index_t index)
138 {
139     ASSERT(!pool_is_free_index(ip6_main.fibs, index));
140     return (&pool_elt_at_index (ip6_main.fibs, index)->v6);
141 }
142
143 static inline 
144 u32 ip6_fib_index_from_table_id (u32 table_id)
145 {
146   ip6_main_t * im = &ip6_main;
147   uword * p;
148
149   p = hash_get (im->fib_index_by_table_id, table_id);
150   if (!p)
151     return ~0;
152
153   return p[0];
154 }
155
156 extern u32 ip6_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
157
158 extern flow_hash_config_t ip6_fib_table_get_flow_hash_config(u32 fib_index);
159
160 #endif
161