A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / adj / adj.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/adj/adj_glean.h>
20 #include <vnet/adj/adj_midchain.h>
21 #include <vnet/fib/fib_node_list.h>
22
23 /*
24  * Special Adj with index zero. we need to define this since the v4 mtrie
25  * assumes an index of 0 implies the ply is empty. therefore all 'real'
26  * adjs need a non-zero index.
27  */
28 static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
29
30 /* Adjacency packet/byte counters indexed by adjacency index. */
31 vlib_combined_counter_main_t adjacency_counters;
32
33 always_inline void
34 adj_poison (ip_adjacency_t * adj)
35 {
36     if (CLIB_DEBUG > 0)
37     {
38         u32 save_handle = adj->heap_handle;;
39
40         memset (adj, 0xfe, sizeof (adj[0]));
41
42         adj->heap_handle = save_handle;
43     }
44 }
45
46 ip_adjacency_t *
47 adj_alloc (fib_protocol_t proto)
48 {
49     ip_adjacency_t *adj;
50
51     adj = aa_alloc();
52
53     adj_poison(adj);
54
55     /* Make sure certain fields are always initialized. */
56     /* Validate adjacency counters. */
57     vlib_validate_combined_counter(&adjacency_counters,
58                                    adj->heap_handle);
59
60     adj->rewrite_header.sw_if_index = ~0;
61     adj->mcast_group_index = ~0;
62     adj->saved_lookup_next_index = 0;
63     adj->n_adj = 1;
64
65     fib_node_init(&adj->ia_node,
66                   FIB_NODE_TYPE_ADJ);
67     adj->ia_nh_proto = proto;
68
69     return (adj);
70 }
71
72 static int
73 adj_index_is_special (adj_index_t adj_index)
74 {
75     if (ADJ_INDEX_INVALID == adj_index)
76         return (!0);
77
78     return (0);
79 }
80
81 /**
82  * @brief Pretty print helper function for formatting specific adjacencies.
83  * @param s - input string to format
84  * @param args - other args passed to format function such as:
85  *                 - vnet_main_t
86  *                 - ip_lookup_main_t
87  *                 - adj_index
88  */
89 u8 *
90 format_ip_adjacency (u8 * s, va_list * args)
91 {
92   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
93   u32 adj_index = va_arg (*args, u32);
94   format_ip_adjacency_flags_t fiaf = va_arg (*args, format_ip_adjacency_flags_t);
95   ip_adjacency_t * adj = adj_get(adj_index);
96   
97   switch (adj->lookup_next_index)
98   {
99   case IP_LOOKUP_NEXT_REWRITE:
100       s = format (s, "%U", format_adj_nbr, adj_index, 0);
101       break;
102   case IP_LOOKUP_NEXT_ARP:
103       s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
104       break;
105   case IP_LOOKUP_NEXT_GLEAN:
106       s = format (s, " %U",
107                   format_vnet_sw_interface_name,
108                   vnm,
109                   vnet_get_sw_interface(vnm,
110                                         adj->rewrite_header.sw_if_index));
111       break;
112
113   case IP_LOOKUP_NEXT_MIDCHAIN:
114       s = format (s, "%U", format_adj_midchain, adj_index, 2);
115       break;
116   default:
117       break;
118   }
119   s = format (s, " index:%d", adj_index);
120
121   if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
122   {
123       s = format (s, " locks:%d", adj->ia_node.fn_locks);
124       s = format(s, "\nchildren:\n ");
125       s = fib_node_children_format(adj->ia_node.fn_children, s);
126   }
127
128   return s;
129 }
130
131 /*
132  * adj_last_lock_gone
133  *
134  * last lock/reference to the adj has gone, we no longer need it.
135  */
136 static void
137 adj_last_lock_gone (ip_adjacency_t *adj)
138 {
139     ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
140     ADJ_DBG(adj, "last-lock-gone");
141
142     switch (adj->lookup_next_index)
143     {
144     case IP_LOOKUP_NEXT_MIDCHAIN:
145         dpo_reset(&adj->sub_type.midchain.next_dpo);
146         /* FALL THROUGH */
147     case IP_LOOKUP_NEXT_ARP:
148     case IP_LOOKUP_NEXT_REWRITE:
149         /*
150          * complete and incomplete nbr adjs
151          */
152         adj_nbr_remove(adj->ia_nh_proto,
153                        adj->ia_link,
154                        &adj->sub_type.nbr.next_hop,
155                        adj->rewrite_header.sw_if_index);
156         break;
157     case IP_LOOKUP_NEXT_GLEAN:
158         adj_glean_remove(adj->ia_nh_proto,
159                          adj->rewrite_header.sw_if_index);
160         break;
161     default:
162         /*
163          * type not stored in any DB from which we need to remove it
164          */
165         break;
166     }
167
168     fib_node_deinit(&adj->ia_node);
169     aa_free(adj);
170 }
171
172 void
173 adj_lock (adj_index_t adj_index)
174 {
175     ip_adjacency_t *adj;
176
177     if (adj_index_is_special(adj_index))
178     {
179         return;
180     }
181
182     adj = adj_get(adj_index);
183     ASSERT(adj);
184     ASSERT(adj->heap_handle!=0);
185
186     ADJ_DBG(adj, "lock");
187     fib_node_lock(&adj->ia_node);
188 }
189
190 void
191 adj_unlock (adj_index_t adj_index)
192 {
193     ip_adjacency_t *adj;
194
195     if (adj_index_is_special(adj_index))
196     {
197         return;
198     }
199
200     adj = adj_get(adj_index);
201     ASSERT(adj);
202     ASSERT(adj->heap_handle!=0);
203
204     ADJ_DBG(adj, "unlock");
205     ASSERT(adj);
206     ASSERT(adj->heap_handle!=0);
207
208     fib_node_unlock(&adj->ia_node);
209 }
210
211 u32
212 adj_child_add (adj_index_t adj_index,
213                fib_node_type_t child_type,
214                fib_node_index_t child_index)
215 {
216     ASSERT(ADJ_INDEX_INVALID != adj_index);
217     if (adj_index_is_special(adj_index))
218     {
219         return (~0);
220     }
221
222     return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
223                                adj_index,
224                                child_type,
225                                child_index));
226 }
227
228 void
229 adj_child_remove (adj_index_t adj_index,
230                   u32 sibling_index)
231 {
232     if (adj_index_is_special(adj_index))
233     {
234         return;
235     }
236
237     fib_node_child_remove(FIB_NODE_TYPE_ADJ,
238                           adj_index,
239                           sibling_index);
240 }
241
242 static fib_node_t *
243 adj_get_node (fib_node_index_t index)
244 {
245     ip_adjacency_t *adj;
246
247     adj = adj_get(index);
248
249     return (&adj->ia_node);
250 }
251
252 #define ADJ_FROM_NODE(_node)                                            \
253     ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
254
255 static void
256 adj_node_last_lock_gone (fib_node_t *node)
257 {
258     adj_last_lock_gone(ADJ_FROM_NODE(node));
259 }
260
261 static fib_node_back_walk_rc_t
262 adj_back_walk_notify (fib_node_t *node,
263                       fib_node_back_walk_ctx_t *ctx)
264 {
265     /*
266      * Que pasa. yo soj en el final!
267      */
268     ASSERT(0);
269
270     return (FIB_NODE_BACK_WALK_CONTINUE);
271 }
272
273 /*
274  * Adjacency's graph node virtual function table
275  */
276 static const fib_node_vft_t adj_vft = {
277     .fnv_get = adj_get_node,
278     .fnv_last_lock = adj_node_last_lock_gone,
279     .fnv_back_walk = adj_back_walk_notify,
280 };
281
282 static clib_error_t *
283 adj_module_init (vlib_main_t * vm)
284 {
285     fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
286
287     adj_nbr_module_init();
288     adj_glean_module_init();
289     adj_midchain_module_init();
290
291     /*
292      * 4 special adjs for v4 and v6 resp.
293      */
294     aa_bootstrap(8);
295     special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
296
297     return (NULL);
298 }
299
300 VLIB_INIT_FUNCTION (adj_module_init);
301
302 /* 
303  * DEPRECATED: DO NOT USE
304  *
305  * Create new block of given number of contiguous adjacencies.
306  */
307 ip_adjacency_t *
308 ip_add_adjacency (ip_lookup_main_t * lm,
309                   ip_adjacency_t * copy_adj,
310                   u32 n_adj,
311                   u32 * adj_index_return)
312 {
313   ip_adjacency_t * adj;
314   u32 ai, i, handle;
315
316   ASSERT(1==n_adj);
317
318   adj = aa_alloc ();
319   handle = ai = adj->heap_handle;
320
321   /* Validate adjacency counters. */
322   vlib_validate_combined_counter (&adjacency_counters, ai + n_adj - 1);
323
324   for (i = 0; i < n_adj; i++)
325     {
326       /* Make sure certain fields are always initialized. */
327       adj[i].rewrite_header.sw_if_index = ~0;
328       adj[i].mcast_group_index = ~0;
329       adj[i].saved_lookup_next_index = 0;
330
331       if (copy_adj)
332         adj[i] = copy_adj[i];
333
334       adj[i].heap_handle = handle;
335       adj[i].n_adj = n_adj;
336
337       /* Zero possibly stale counters for re-used adjacencies. */
338       vlib_zero_combined_counter (&adjacency_counters, ai + i);
339     }
340
341   *adj_index_return = ai;
342   return adj;
343 }