24f7662d94360109212011a1ad43a488abfb3377
[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_internal.h>
18 #include <vnet/adj/adj_glean.h>
19 #include <vnet/adj/adj_midchain.h>
20 #include <vnet/fib/fib_node_list.h>
21
22 /*
23  * Special Adj with index zero. we need to define this since the v4 mtrie
24  * assumes an index of 0 implies the ply is empty. therefore all 'real'
25  * adjs need a non-zero index.
26  */
27 static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
28
29 /* Adjacency packet/byte counters indexed by adjacency index. */
30 vlib_combined_counter_main_t adjacency_counters;
31
32 /*
33  * the single adj pool
34  */
35 ip_adjacency_t *adj_pool;
36
37 always_inline void
38 adj_poison (ip_adjacency_t * adj)
39 {
40     if (CLIB_DEBUG > 0)
41     {
42         memset (adj, 0xfe, sizeof (adj[0]));
43     }
44 }
45
46 ip_adjacency_t *
47 adj_alloc (fib_protocol_t proto)
48 {
49     ip_adjacency_t *adj;
50
51     pool_get(adj_pool, adj);
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_get_index(adj));
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     ip4_main.lookup_main.adjacency_heap = adj_pool;
70     ip6_main.lookup_main.adjacency_heap = adj_pool;
71
72     return (adj);
73 }
74
75 static int
76 adj_index_is_special (adj_index_t adj_index)
77 {
78     if (ADJ_INDEX_INVALID == adj_index)
79         return (!0);
80
81     return (0);
82 }
83
84 /**
85  * @brief Pretty print helper function for formatting specific adjacencies.
86  * @param s - input string to format
87  * @param args - other args passed to format function such as:
88  *                 - vnet_main_t
89  *                 - ip_lookup_main_t
90  *                 - adj_index
91  */
92 u8 *
93 format_ip_adjacency (u8 * s, va_list * args)
94 {
95     format_ip_adjacency_flags_t fiaf;
96     ip_adjacency_t * adj;
97     u32 adj_index;
98
99     adj_index = va_arg (*args, u32);
100     fiaf = va_arg (*args, format_ip_adjacency_flags_t);
101     adj = adj_get(adj_index);
102   
103     switch (adj->lookup_next_index)
104     {
105     case IP_LOOKUP_NEXT_REWRITE:
106         s = format (s, "%U", format_adj_nbr, adj_index, 0);
107         break;
108     case IP_LOOKUP_NEXT_ARP:
109         s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
110         break;
111     case IP_LOOKUP_NEXT_GLEAN:
112         s = format (s, "%U", format_adj_glean, adj_index, 0);
113         break;
114     case IP_LOOKUP_NEXT_MIDCHAIN:
115         s = format (s, "%U", format_adj_midchain, adj_index, 2);
116         break;
117     default:
118         break;
119     }
120
121     if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
122     {
123         s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
124         s = format (s, " node:[%d]:%U",
125                     adj->rewrite_header.node_index,
126                     format_vlib_node_name, vlib_get_main(),
127                     adj->rewrite_header.node_index);
128         s = format (s, " next:[%d]:%U",
129                     adj->rewrite_header.next_index,
130                     format_vlib_next_node_name,
131                     vlib_get_main(),
132                     adj->rewrite_header.node_index,
133                     adj->rewrite_header.next_index);
134         s = format(s, "\n children:\n  ");
135         s = fib_node_children_format(adj->ia_node.fn_children, s);
136     }
137
138     return s;
139 }
140
141 /*
142  * adj_last_lock_gone
143  *
144  * last lock/reference to the adj has gone, we no longer need it.
145  */
146 static void
147 adj_last_lock_gone (ip_adjacency_t *adj)
148 {
149     vlib_main_t * vm = vlib_get_main();
150
151     ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
152     ADJ_DBG(adj, "last-lock-gone");
153
154     vlib_worker_thread_barrier_sync (vm);
155
156     switch (adj->lookup_next_index)
157     {
158     case IP_LOOKUP_NEXT_MIDCHAIN:
159         dpo_reset(&adj->sub_type.midchain.next_dpo);
160         /* FALL THROUGH */
161     case IP_LOOKUP_NEXT_ARP:
162     case IP_LOOKUP_NEXT_REWRITE:
163         /*
164          * complete and incomplete nbr adjs
165          */
166         adj_nbr_remove(adj->ia_nh_proto,
167                        adj->ia_link,
168                        &adj->sub_type.nbr.next_hop,
169                        adj->rewrite_header.sw_if_index);
170         break;
171     case IP_LOOKUP_NEXT_GLEAN:
172         adj_glean_remove(adj->ia_nh_proto,
173                          adj->rewrite_header.sw_if_index);
174         break;
175     default:
176         /*
177          * type not stored in any DB from which we need to remove it
178          */
179         break;
180     }
181
182     vlib_worker_thread_barrier_release(vm);
183
184     fib_node_deinit(&adj->ia_node);
185     pool_put(adj_pool, adj);
186 }
187
188 void
189 adj_lock (adj_index_t adj_index)
190 {
191     ip_adjacency_t *adj;
192
193     if (adj_index_is_special(adj_index))
194     {
195         return;
196     }
197
198     adj = adj_get(adj_index);
199     ASSERT(adj);
200
201     ADJ_DBG(adj, "lock");
202     fib_node_lock(&adj->ia_node);
203 }
204
205 void
206 adj_unlock (adj_index_t adj_index)
207 {
208     ip_adjacency_t *adj;
209
210     if (adj_index_is_special(adj_index))
211     {
212         return;
213     }
214
215     adj = adj_get(adj_index);
216     ASSERT(adj);
217
218     ADJ_DBG(adj, "unlock");
219     ASSERT(adj);
220
221     fib_node_unlock(&adj->ia_node);
222 }
223
224 u32
225 adj_child_add (adj_index_t adj_index,
226                fib_node_type_t child_type,
227                fib_node_index_t child_index)
228 {
229     ASSERT(ADJ_INDEX_INVALID != adj_index);
230     if (adj_index_is_special(adj_index))
231     {
232         return (~0);
233     }
234
235     return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
236                                adj_index,
237                                child_type,
238                                child_index));
239 }
240
241 void
242 adj_child_remove (adj_index_t adj_index,
243                   u32 sibling_index)
244 {
245     if (adj_index_is_special(adj_index))
246     {
247         return;
248     }
249
250     fib_node_child_remove(FIB_NODE_TYPE_ADJ,
251                           adj_index,
252                           sibling_index);
253 }
254
255 /**
256  * @brief Return the link type of the adjacency
257  */
258 vnet_link_t
259 adj_get_link_type (adj_index_t ai)
260 {
261     const ip_adjacency_t *adj;
262
263     adj = adj_get(ai);
264
265     return (adj->ia_link); 
266 }
267
268 /**
269  * @brief Return the sw interface index of the adjacency.
270  */
271 u32
272 adj_get_sw_if_index (adj_index_t ai)
273 {
274     const ip_adjacency_t *adj;
275
276     adj = adj_get(ai);
277
278     return (adj->rewrite_header.sw_if_index);
279 }
280
281 /**
282  * @brief Return the link type of the adjacency
283  */
284 const u8*
285 adj_get_rewrite (adj_index_t ai)
286 {
287     vnet_rewrite_header_t *rw;
288     ip_adjacency_t *adj;
289
290     adj = adj_get(ai);
291     rw = &adj->rewrite_header;
292
293     ASSERT (rw->data_bytes != 0xfefe);
294
295     return (rw->data - rw->data_bytes);
296 }
297
298 static fib_node_t *
299 adj_get_node (fib_node_index_t index)
300 {
301     ip_adjacency_t *adj;
302
303     adj = adj_get(index);
304
305     return (&adj->ia_node);
306 }
307
308 #define ADJ_FROM_NODE(_node)                                            \
309     ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
310
311 static void
312 adj_node_last_lock_gone (fib_node_t *node)
313 {
314     adj_last_lock_gone(ADJ_FROM_NODE(node));
315 }
316
317 static fib_node_back_walk_rc_t
318 adj_back_walk_notify (fib_node_t *node,
319                       fib_node_back_walk_ctx_t *ctx)
320 {
321     /*
322      * Que pasa. yo soj en el final!
323      */
324     ASSERT(0);
325
326     return (FIB_NODE_BACK_WALK_CONTINUE);
327 }
328
329 /*
330  * Adjacency's graph node virtual function table
331  */
332 static const fib_node_vft_t adj_vft = {
333     .fnv_get = adj_get_node,
334     .fnv_last_lock = adj_node_last_lock_gone,
335     .fnv_back_walk = adj_back_walk_notify,
336 };
337
338 static clib_error_t *
339 adj_module_init (vlib_main_t * vm)
340 {
341     fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
342
343     adj_nbr_module_init();
344     adj_glean_module_init();
345     adj_midchain_module_init();
346
347     /*
348      * one special adj to reserve index 0
349      */
350     special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
351
352     return (NULL);
353 }
354
355 VLIB_INIT_FUNCTION (adj_module_init);
356
357 static clib_error_t *
358 adj_show (vlib_main_t * vm,
359           unformat_input_t * input,
360           vlib_cli_command_t * cmd)
361 {
362     adj_index_t ai = ADJ_INDEX_INVALID;
363     u32 sw_if_index = ~0;
364
365     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
366     {
367         if (unformat (input, "%d", &ai))
368             ;
369         else if (unformat (input, "%U",
370                            unformat_vnet_sw_interface, vnet_get_main(),
371                            &sw_if_index))
372             ;
373         else
374             break;
375     }
376
377     if (ADJ_INDEX_INVALID != ai)
378     {
379         vlib_cli_output (vm, "[@%d] %U",
380                          ai,
381                          format_ip_adjacency,  ai,
382                          FORMAT_IP_ADJACENCY_DETAIL);
383     }
384     else
385     {
386         /* *INDENT-OFF* */
387         pool_foreach_index(ai, adj_pool,
388         ({
389             if (~0 != sw_if_index &&
390                 sw_if_index != adj_get_sw_if_index(ai))
391             {
392             }
393             else
394             {
395                 vlib_cli_output (vm, "[@%d] %U",
396                                  ai,
397                                  format_ip_adjacency, ai,
398                                  FORMAT_IP_ADJACENCY_NONE);
399             }
400         }));
401         /* *INDENT-ON* */
402     }
403
404     return 0;
405 }
406
407 /*?
408  * Show all adjacencies.
409  * @cliexpar
410  * @cliexstart{sh adj}
411  * [@0]
412  * [@1]  glean: loop0
413  * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
414  * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
415  * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
416  * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
417  * @cliexend
418  ?*/
419 VLIB_CLI_COMMAND (adj_show_command, static) = {
420     .path = "show adj",
421     .short_help = "show adj [<adj_index>] [interface]",
422     .function = adj_show,
423 };
424
425 /* 
426  * DEPRECATED: DO NOT USE
427  */
428 ip_adjacency_t *
429 ip_add_adjacency (ip_lookup_main_t * lm,
430                   ip_adjacency_t * copy_adj,
431                   u32 n_adj,
432                   u32 * adj_index_return)
433 {
434   ip_adjacency_t * adj;
435
436   ASSERT(1==n_adj);
437
438   adj = adj_alloc(FIB_PROTOCOL_IP4);
439
440   if (copy_adj)
441       *adj = *copy_adj;
442
443   *adj_index_return = adj_get_index(adj);
444   return adj;
445 }