A Protocol Independent Hierarchical FIB (VPP-352)
[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
21 #include "fib_entry_src.h"
22 #include "fib_entry_cover.h"
23 #include "fib_entry.h"
24 #include "fib_table.h"
25
26 /*
27  * fib_entry_src_rr_resolve_via_connected
28  *
29  * Resolve via a connected cover.
30  */
31 static void
32 fib_entry_src_rr_resolve_via_connected (fib_entry_src_t *src,
33                                         const fib_entry_t *fib_entry,
34                                         const fib_entry_t *cover)
35 {
36     const fib_route_path_t path = {
37         .frp_proto = fib_entry->fe_prefix.fp_proto,
38         .frp_addr = fib_entry->fe_prefix.fp_addr,
39         .frp_sw_if_index = fib_entry_get_resolving_interface(
40                                fib_entry_get_index(cover)),
41         .frp_fib_index = ~0,
42         .frp_weight = 1,
43     };
44     fib_route_path_t *paths = NULL;
45     vec_add1(paths, path);
46
47     /*
48      * since the cover is connected, the address this entry corresponds
49      * to is a peer (ARP-able for) on the interface to which the cover is
50      * connected. The fact we resolve via the cover, just means this RR
51      * source is the first SRC to use said peer. The ARP source will be along
52      * shortly to over-rule this RR source.
53      */
54     src->fes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NONE, paths);
55     src->fes_entry_flags = fib_entry_get_flags(fib_entry_get_index(cover));
56
57     vec_free(paths);
58 }
59
60 /**
61  * Source initialisation Function 
62  */
63 static void
64 fib_entry_src_rr_init (fib_entry_src_t *src)
65 {
66     src->rr.fesr_cover = FIB_NODE_INDEX_INVALID;
67     src->rr.fesr_sibling = FIB_NODE_INDEX_INVALID;
68 }
69
70 /*
71  * Source activation. Called when the source is the new best source on the entry
72  */
73 static int
74 fib_entry_src_rr_activate (fib_entry_src_t *src,
75                            const fib_entry_t *fib_entry)
76 {
77     fib_entry_t *cover;
78
79     /*
80      * find the covering prefix. become a dependent thereof.
81      * there should always be a cover, though it may be the default route.
82      */
83     src->rr.fesr_cover = fib_table_get_less_specific(fib_entry->fe_fib_index,
84                                                      &fib_entry->fe_prefix);
85
86     ASSERT(FIB_NODE_INDEX_INVALID != src->rr.fesr_cover);
87
88     cover = fib_entry_get(src->rr.fesr_cover);
89
90     src->rr.fesr_sibling =
91         fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
92
93     /*
94      * if the ocver is attached then install an attached-host path
95      * (like an adj-fib). Otherwise inherit the forwarding from the cover
96      */
97     if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
98     {
99         fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
100     }
101     else
102     {
103         src->fes_pl = cover->fe_parent;
104     }
105     fib_path_list_lock(src->fes_pl);
106
107     /*
108      * return go for install
109      */
110     return (!0);
111 }
112
113 /**
114  * Source Deactivate. 
115  * Called when the source is no longer best source on the entry
116  */
117 static void
118 fib_entry_src_rr_deactivate (fib_entry_src_t *src,
119                              const fib_entry_t *fib_entry)
120 {
121     fib_entry_t *cover;
122
123     /*
124      * remove the depednecy on the covering entry
125      */
126     ASSERT(FIB_NODE_INDEX_INVALID != src->rr.fesr_cover);
127     cover = fib_entry_get(src->rr.fesr_cover);
128
129     fib_entry_cover_untrack(cover, src->rr.fesr_sibling);
130
131     src->rr.fesr_cover = FIB_NODE_INDEX_INVALID;
132
133     fib_path_list_unlock(src->fes_pl);
134     src->fes_pl = FIB_NODE_INDEX_INVALID;
135     src->fes_entry_flags = FIB_ENTRY_FLAG_NONE;
136 }
137
138 static fib_entry_src_cover_res_t
139 fib_entry_src_rr_cover_change (fib_entry_src_t *src,
140                                const fib_entry_t *fib_entry)
141 {
142     fib_entry_src_cover_res_t res = {
143         .install = !0,
144         .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
145     };
146
147     if (FIB_NODE_INDEX_INVALID == src->rr.fesr_cover)
148     {
149         /*
150          * the source may be added, but it is not active
151          * if it is not tracking the cover.
152          */
153         return (res);
154     }
155
156     /*
157      * this function is called when this entry's cover has a more specific
158      * entry inserted benaeth it. That does not necessarily mean that this
159      * entry is covered by the new prefix. check that
160      */
161     if (src->rr.fesr_cover != fib_table_get_less_specific(fib_entry->fe_fib_index,
162                                                           &fib_entry->fe_prefix))
163     {
164         fib_entry_src_rr_deactivate(src, fib_entry);
165         fib_entry_src_rr_activate(src, fib_entry);
166
167         /*
168          * dependent children need to re-resolve to the new forwarding info
169          */
170         res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
171     }
172     return (res);
173 }
174
175 /*
176  * fib_entry_src_rr_cover_update
177  *
178  * This entry's cover has updated its forwarding info. This entry
179  * will need to re-inheret.
180  */
181 static fib_entry_src_cover_res_t
182 fib_entry_src_rr_cover_update (fib_entry_src_t *src,
183                                const fib_entry_t *fib_entry)
184 {
185     fib_entry_src_cover_res_t res = {
186         .install = !0,
187         .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
188     };
189     fib_node_index_t old_path_list;
190     fib_entry_t *cover;
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     cover = fib_entry_get(src->rr.fesr_cover);
202     old_path_list = src->fes_pl;
203
204     /*
205      * if the ocver is attached then install an attached-host path
206      * (like an adj-fib). Otherwise inherit the forwarding from the cover
207      */
208     if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
209     {
210         fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
211     }
212     else
213     {
214         src->fes_pl = cover->fe_parent;
215     }
216     fib_path_list_lock(src->fes_pl);
217     fib_path_list_unlock(old_path_list);
218
219     /*
220      * dependent children need to re-resolve to the new forwarding info
221      */
222     res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
223
224     return (res);
225 }
226
227 static u8*
228 fib_entry_src_rr_format (fib_entry_src_t *src,
229                          u8* s)
230 {
231     return (format(s, "cover:%d", src->rr.fesr_cover));
232 }
233
234 const static fib_entry_src_vft_t rr_src_vft = {
235     .fesv_init = fib_entry_src_rr_init,
236     .fesv_activate = fib_entry_src_rr_activate,
237     .fesv_deactivate = fib_entry_src_rr_deactivate,
238     .fesv_cover_change = fib_entry_src_rr_cover_change,
239     .fesv_cover_update = fib_entry_src_rr_cover_update,
240     .fesv_format = fib_entry_src_rr_format,
241 };
242
243 void
244 fib_entry_src_rr_register (void)
245 {
246     fib_entry_src_register(FIB_SOURCE_RR, &rr_src_vft);    
247 }