A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / adj / adj_glean.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 <vnet/adj/adj.h>
17 #include <vnet/adj/adj_alloc.h>
18 #include <vnet/adj/adj_internal.h>
19 #include <vnet/fib/fib_walk.h>
20
21 /*
22  * The 'DB' of all glean adjs.
23  * There is only one glean per-interface per-protocol, so this is a per-interface
24  * vector
25  */
26 static adj_index_t *adj_gleans[FIB_PROTOCOL_MAX];
27
28 static inline vlib_node_registration_t*
29 adj_get_glean_node (fib_protocol_t proto)
30 {
31     switch (proto) {
32     case FIB_PROTOCOL_IP4:
33         return (&ip4_glean_node);
34     case FIB_PROTOCOL_IP6:
35         return (&ip6_glean_node);
36     case FIB_PROTOCOL_MPLS:
37         break;
38     }
39     ASSERT(0);
40     return (NULL);
41 }
42
43 /*
44  * adj_glean_add_or_lock
45  *
46  * The next_hop address here is used for source address selection in the DP.
47  * The glean adj is added to an interface's connected prefix, the next-hop
48  * passed here is the local prefix on the same interface.
49  */
50 adj_index_t
51 adj_glean_add_or_lock (fib_protocol_t proto,
52                        u32 sw_if_index,
53                        const ip46_address_t *nh_addr)
54 {
55     ip_adjacency_t * adj;
56
57     vec_validate_init_empty(adj_gleans[proto], sw_if_index, ADJ_INDEX_INVALID);
58
59     if (ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
60     {
61         adj = adj_alloc(proto);
62
63         adj->lookup_next_index = IP_LOOKUP_NEXT_GLEAN;
64         adj->ia_nh_proto = proto;
65         adj_gleans[proto][sw_if_index] = adj->heap_handle;
66
67         if (NULL != nh_addr)
68         {
69             adj->sub_type.glean.receive_addr = *nh_addr;
70         }
71
72         adj->rewrite_header.data_bytes = 0;
73
74         vnet_rewrite_for_sw_interface(vnet_get_main(),
75                                       adj_fib_proto_2_nd(proto),
76                                       sw_if_index,
77                                       adj_get_glean_node(proto)->index,
78                                       VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
79                                       &adj->rewrite_header,
80                                       sizeof (adj->rewrite_data));
81     }
82     else
83     {
84         adj = adj_get(adj_gleans[proto][sw_if_index]);
85     }
86
87     adj_lock(adj->heap_handle);
88
89     return (adj->heap_handle);
90 }
91
92 void
93 adj_glean_remove (fib_protocol_t proto,
94                   u32 sw_if_index)
95 {
96     ASSERT(sw_if_index < vec_len(adj_gleans[proto]));
97
98     adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID;
99 }
100
101 static clib_error_t *
102 adj_glean_interface_state_change (vnet_main_t * vnm,
103                                   u32 sw_if_index,
104                                   u32 flags)
105 {
106     /*
107      * for each glean on the interface trigger a walk back to the children
108      */
109     fib_protocol_t proto;
110     ip_adjacency_t *adj;
111
112
113     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
114     {
115         if (sw_if_index >= vec_len(adj_gleans[proto]) ||
116             ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
117             continue;
118
119         adj = adj_get(adj_gleans[proto][sw_if_index]);
120
121         fib_node_back_walk_ctx_t bw_ctx = {
122             .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
123                             FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
124                             FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
125         };
126
127         fib_walk_sync(FIB_NODE_TYPE_ADJ, adj->heap_handle, &bw_ctx);
128     }
129
130     return (NULL);
131 }
132
133 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change);
134
135 static clib_error_t *
136 adj_glean_interface_delete (vnet_main_t * vnm,
137                             u32 sw_if_index,
138                             u32 is_add)
139 {
140     /*
141      * for each glean on the interface trigger a walk back to the children
142      */
143     fib_protocol_t proto;
144     ip_adjacency_t *adj;
145
146     if (is_add)
147     {
148         /*
149          * not interested in interface additions. we will not back walk
150          * to resolve paths through newly added interfaces. Why? The control
151          * plane should have the brains to add interfaces first, then routes.
152          * So the case where there are paths with a interface that matches
153          * one just created is the case where the path resolved through an
154          * interface that was deleted, and still has not been removed. The
155          * new interface added, is NO GUARANTEE that the interface being
156          * added now, even though it may have the same sw_if_index, is the
157          * same interface that the path needs. So tough!
158          * If the control plane wants these routes to resolve it needs to
159          * remove and add them again.
160          */
161         return (NULL);
162     }
163
164     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
165     {
166         if (sw_if_index >= vec_len(adj_gleans[proto]) ||
167             ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
168             continue;
169
170         adj = adj_get(adj_gleans[proto][sw_if_index]);
171
172         fib_node_back_walk_ctx_t bw_ctx = {
173             .fnbw_reason =  FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
174         };
175
176         fib_walk_sync(FIB_NODE_TYPE_ADJ, adj->heap_handle, &bw_ctx);
177     }
178
179     return (NULL);
180 }
181
182 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete);
183
184 u8*
185 format_adj_glean (u8* s, va_list *ap)
186 {
187     index_t index = va_arg(ap, index_t);
188     CLIB_UNUSED(u32 indent) = va_arg(ap, u32);
189     vnet_main_t * vnm = vnet_get_main();
190     ip_adjacency_t * adj = adj_get(index);
191
192     return (format(s, " glean: %U",
193                    format_vnet_sw_interface_name,
194                    vnm,
195                    vnet_get_sw_interface(vnm,
196                                          adj->rewrite_header.sw_if_index)));
197 }
198
199
200 static void
201 adj_dpo_lock (dpo_id_t *dpo)
202 {
203     adj_lock(dpo->dpoi_index);
204 }
205 static void
206 adj_dpo_unlock (dpo_id_t *dpo)
207 {
208     adj_unlock(dpo->dpoi_index);
209 }
210
211 const static dpo_vft_t adj_glean_dpo_vft = {
212     .dv_lock = adj_dpo_lock,
213     .dv_unlock = adj_dpo_unlock,
214     .dv_format = format_adj_glean,
215 };
216
217 /**
218  * @brief The per-protocol VLIB graph nodes that are assigned to a glean
219  *        object.
220  *
221  * this means that these graph nodes are ones from which a glean is the
222  * parent object in the DPO-graph.
223  */
224 const static char* const glean_ip4_nodes[] =
225 {
226     "ip4-glean",
227     NULL,
228 };
229 const static char* const glean_ip6_nodes[] =
230 {
231     "ip6-glean",
232     NULL,
233 };
234
235 const static char* const * const glean_nodes[DPO_PROTO_NUM] =
236 {
237     [DPO_PROTO_IP4]  = glean_ip4_nodes,
238     [DPO_PROTO_IP6]  = glean_ip6_nodes,
239     [DPO_PROTO_MPLS] = NULL,
240 };
241
242 void
243 adj_glean_module_init (void)
244 {
245     dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);
246 }