Reorganize source tree to use single autotools instance
[vpp.git] / src / vnet / fib / fib_entry_src_rr.c
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 #include <vlib/vlib.h>
17 #include <vnet/ip/format.h>
18 #include <vnet/ip/lookup.h>
19 #include <vnet/adj/adj.h>
20 #include <vnet/dpo/drop_dpo.h>
21
22 #include "fib_entry_src.h"
23 #include "fib_entry_cover.h"
24 #include "fib_entry.h"
25 #include "fib_table.h"
26
27 /*
28  * fib_entry_src_rr_resolve_via_connected
29  *
30  * Resolve via a connected cover.
31  */
32 static void
33 fib_entry_src_rr_resolve_via_connected (fib_entry_src_t *src,
34                                         const fib_entry_t *fib_entry,
35                                         const fib_entry_t *cover)
36 {
37     const fib_route_path_t path = {
38         .frp_proto = fib_entry->fe_prefix.fp_proto,
39         .frp_addr = fib_entry->fe_prefix.fp_addr,
40         .frp_sw_if_index = fib_entry_get_resolving_interface(
41                                fib_entry_get_index(cover)),
42         .frp_fib_index = ~0,
43         .frp_weight = 1,
44     };
45     fib_route_path_t *paths = NULL;
46     vec_add1(paths, path);
47
48     /*
49      * since the cover is connected, the address this entry corresponds
50      * to is a peer (ARP-able for) on the interface to which the cover is
51      * connected. The fact we resolve via the cover, just means this RR
52      * source is the first SRC to use said peer. The ARP source will be along
53      * shortly to over-rule this RR source.
54      */
55     src->fes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NONE, paths);
56     src->fes_entry_flags = fib_entry_get_flags(fib_entry_get_index(cover));
57
58     vec_free(paths);
59 }
60
61
62 /**
63  * Source initialisation Function 
64  */
65 static void
66 fib_entry_src_rr_init (fib_entry_src_t *src)
67 {
68     src->rr.fesr_cover = FIB_NODE_INDEX_INVALID;
69     src->rr.fesr_sibling = FIB_NODE_INDEX_INVALID;
70 }
71
72 /*
73  * Source activation. Called when the source is the new best source on the entry
74  */
75 static int
76 fib_entry_src_rr_activate (fib_entry_src_t *src,
77                            const fib_entry_t *fib_entry)
78 {
79     fib_entry_t *cover;
80
81     /*
82      * find the covering prefix. become a dependent thereof.
83      * for IP there should always be a cover, though it may be the default route.
84      * For MPLS there is never a cover.
85      */
86     if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
87     {
88         src->fes_pl = fib_path_list_create_special(FIB_PROTOCOL_MPLS,
89                                                    FIB_PATH_LIST_FLAG_DROP,
90                                                    NULL);
91         fib_path_list_lock(src->fes_pl);
92         return (!0);
93     }
94
95     src->rr.fesr_cover = fib_table_get_less_specific(fib_entry->fe_fib_index,
96                                                      &fib_entry->fe_prefix);
97
98     ASSERT(FIB_NODE_INDEX_INVALID != src->rr.fesr_cover);
99
100     cover = fib_entry_get(src->rr.fesr_cover);
101
102     src->rr.fesr_sibling =
103         fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
104
105     /*
106      * if the ocver is attached then install an attached-host path
107      * (like an adj-fib). Otherwise inherit the forwarding from the cover
108      */
109     if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
110     {
111         fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
112     }
113     else
114     {
115         /*
116          * use the path-list of the cover, unless it would form a loop.
117          * that is unless the cover is via this entry.
118          * If a loop were to form it would be a 1 level loop (i.e. X via X),
119          * and there would be 2 locks on the path-list; one since its used
120          * by the cover, and 1 from here. The first lock will go when the
121          * cover is removed, the second, and last, when the covered walk
122          * occurs during the cover's removel - this is not a place where
123          * we can handle last lock gone.
124          * In short, don't let the loop form. The usual rules of 'we must
125          * let it form so we know when it breaks' don't apply here, since
126          * the loop will break when the cover changes, and this function
127          * will be called again when that happens.
128          */
129         fib_node_index_t *entries = NULL;
130         fib_protocol_t proto;
131
132         proto = fib_entry->fe_prefix.fp_proto;
133         vec_add1(entries, fib_entry_get_index(fib_entry));
134
135         if (fib_path_list_recursive_loop_detect(cover->fe_parent,
136                                                 &entries))
137         {
138             src->fes_pl = fib_path_list_create_special(
139                               proto,
140                               FIB_PATH_LIST_FLAG_DROP,
141                               drop_dpo_get(fib_proto_to_dpo(proto)));
142         }
143         else
144         {
145             src->fes_pl = cover->fe_parent;
146         }
147         vec_free(entries);
148
149     }
150     fib_path_list_lock(src->fes_pl);
151
152     /*
153      * return go for install
154      */
155     return (!0);
156 }
157
158 /**
159  * Source Deactivate. 
160  * Called when the source is no longer best source on the entry
161  */
162 static void
163 fib_entry_src_rr_deactivate (fib_entry_src_t *src,
164                              const fib_entry_t *fib_entry)
165 {
166     fib_entry_t *cover;
167
168     /*
169      * remove the depednecy on the covering entry
170      */
171     if (FIB_NODE_INDEX_INVALID != src->rr.fesr_cover)
172     {
173         cover = fib_entry_get(src->rr.fesr_cover);
174         fib_entry_cover_untrack(cover, src->rr.fesr_sibling);
175         src->rr.fesr_cover = FIB_NODE_INDEX_INVALID;
176     }
177
178     fib_path_list_unlock(src->fes_pl);
179     src->fes_pl = FIB_NODE_INDEX_INVALID;
180     src->fes_entry_flags = FIB_ENTRY_FLAG_NONE;
181 }
182
183 static fib_entry_src_cover_res_t
184 fib_entry_src_rr_cover_change (fib_entry_src_t *src,
185                                const fib_entry_t *fib_entry)
186 {
187     fib_entry_src_cover_res_t res = {
188         .install = !0,
189         .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
190     };
191
192     if (FIB_NODE_INDEX_INVALID == src->rr.fesr_cover)
193     {
194         /*
195          * the source may be added, but it is not active
196          * if it is not tracking the cover.
197          */
198         return (res);
199     }
200
201     /*
202      * this function is called when this entry's cover has a more specific
203      * entry inserted benaeth it. That does not necessarily mean that this
204      * entry is covered by the new prefix. check that
205      */
206     if (src->rr.fesr_cover != fib_table_get_less_specific(fib_entry->fe_fib_index,
207                                                           &fib_entry->fe_prefix))
208     {
209         fib_entry_src_rr_deactivate(src, fib_entry);
210         fib_entry_src_rr_activate(src, fib_entry);
211
212         /*
213          * dependent children need to re-resolve to the new forwarding info
214          */
215         res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
216     }
217     return (res);
218 }
219
220 /*
221  * fib_entry_src_rr_cover_update
222  *
223  * This entry's cover has updated its forwarding info. This entry
224  * will need to re-inheret.
225  */
226 static fib_entry_src_cover_res_t
227 fib_entry_src_rr_cover_update (fib_entry_src_t *src,
228                                const fib_entry_t *fib_entry)
229 {
230     fib_entry_src_cover_res_t res = {
231         .install = !0,
232         .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
233     };
234     fib_node_index_t old_path_list;
235     fib_entry_t *cover;
236
237     if (FIB_NODE_INDEX_INVALID == src->rr.fesr_cover)
238     {
239         /*
240          * the source may be added, but it is not active
241          * if it is not tracking the cover.
242          */
243         return (res);
244     }
245
246     cover = fib_entry_get(src->rr.fesr_cover);
247     old_path_list = src->fes_pl;
248
249     /*
250      * if the ocver is attached then install an attached-host path
251      * (like an adj-fib). Otherwise inherit the forwarding from the cover
252      */
253     if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
254     {
255         fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
256     }
257     else
258     {
259         src->fes_pl = cover->fe_parent;
260     }
261     fib_path_list_lock(src->fes_pl);
262     fib_path_list_unlock(old_path_list);
263
264     /*
265      * dependent children need to re-resolve to the new forwarding info
266      */
267     res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
268
269     return (res);
270 }
271
272 static u8*
273 fib_entry_src_rr_format (fib_entry_src_t *src,
274                          u8* s)
275 {
276     return (format(s, "cover:%d", src->rr.fesr_cover));
277 }
278
279 const static fib_entry_src_vft_t rr_src_vft = {
280     .fesv_init = fib_entry_src_rr_init,
281     .fesv_activate = fib_entry_src_rr_activate,
282     .fesv_deactivate = fib_entry_src_rr_deactivate,
283     .fesv_cover_change = fib_entry_src_rr_cover_change,
284     .fesv_cover_update = fib_entry_src_rr_cover_update,
285     .fesv_format = fib_entry_src_rr_format,
286 };
287
288 void
289 fib_entry_src_rr_register (void)
290 {
291     fib_entry_src_register(FIB_SOURCE_RR, &rr_src_vft);    
292     fib_entry_src_register(FIB_SOURCE_URPF_EXEMPT, &rr_src_vft);    
293 }