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