Adjacency layout change and move to vnet/adj
[vpp.git] / src / 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/adj/adj_mcast.h>
21 #include <vnet/fib/fib_node_list.h>
22
23 /* Adjacency packet/byte counters indexed by adjacency index. */
24 vlib_combined_counter_main_t adjacency_counters;
25
26 /*
27  * the single adj pool
28  */
29 ip_adjacency_t *adj_pool;
30
31 /**
32  * @brief Global Config for enabling per-adjacency counters.
33  * By default these are disabled.
34  */
35 int adj_per_adj_counters;
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_aligned(adj_pool, adj, CLIB_CACHE_LINE_BYTES);
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->lookup_next_index = 0;
62
63     fib_node_init(&adj->ia_node,
64                   FIB_NODE_TYPE_ADJ);
65     adj->ia_nh_proto = proto;
66     adj->ia_flags = 0;
67
68     ip4_main.lookup_main.adjacency_heap = adj_pool;
69     ip6_main.lookup_main.adjacency_heap = adj_pool;
70
71     return (adj);
72 }
73
74 static int
75 adj_index_is_special (adj_index_t adj_index)
76 {
77     if (ADJ_INDEX_INVALID == adj_index)
78         return (!0);
79
80     return (0);
81 }
82
83 /**
84  * @brief Pretty print helper function for formatting specific adjacencies.
85  * @param s - input string to format
86  * @param args - other args passed to format function such as:
87  *                 - vnet_main_t
88  *                 - ip_lookup_main_t
89  *                 - adj_index
90  */
91 u8 *
92 format_ip_adjacency (u8 * s, va_list * args)
93 {
94     format_ip_adjacency_flags_t fiaf;
95     ip_adjacency_t * adj;
96     u32 adj_index;
97
98     adj_index = va_arg (*args, u32);
99     fiaf = va_arg (*args, format_ip_adjacency_flags_t);
100     adj = adj_get(adj_index);
101   
102     switch (adj->lookup_next_index)
103     {
104     case IP_LOOKUP_NEXT_REWRITE:
105         s = format (s, "%U", format_adj_nbr, adj_index, 0);
106         break;
107     case IP_LOOKUP_NEXT_ARP:
108         s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
109         break;
110     case IP_LOOKUP_NEXT_GLEAN:
111         s = format (s, "%U", format_adj_glean, adj_index, 0);
112         break;
113     case IP_LOOKUP_NEXT_MIDCHAIN:
114         s = format (s, "%U", format_adj_midchain, adj_index, 2);
115         break;
116     case IP_LOOKUP_NEXT_MCAST:
117         s = format (s, "%U", format_adj_mcast, adj_index, 0);
118         break;
119     default:
120         break;
121     }
122
123     if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
124     {
125         vlib_counter_t counts;
126
127         vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
128         s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
129         s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
130         s = format(s, "\n children:\n  ");
131         s = fib_node_children_format(adj->ia_node.fn_children, s);
132     }
133
134     return s;
135 }
136
137 /*
138  * adj_last_lock_gone
139  *
140  * last lock/reference to the adj has gone, we no longer need it.
141  */
142 static void
143 adj_last_lock_gone (ip_adjacency_t *adj)
144 {
145     vlib_main_t * vm = vlib_get_main();
146
147     ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
148     ADJ_DBG(adj, "last-lock-gone");
149
150     vlib_worker_thread_barrier_sync (vm);
151
152     switch (adj->lookup_next_index)
153     {
154     case IP_LOOKUP_NEXT_MIDCHAIN:
155         dpo_reset(&adj->sub_type.midchain.next_dpo);
156         /* FALL THROUGH */
157     case IP_LOOKUP_NEXT_ARP:
158     case IP_LOOKUP_NEXT_REWRITE:
159         /*
160          * complete and incomplete nbr adjs
161          */
162         adj_nbr_remove(adj_get_index(adj),
163                        adj->ia_nh_proto,
164                        adj->ia_link,
165                        &adj->sub_type.nbr.next_hop,
166                        adj->rewrite_header.sw_if_index);
167         break;
168     case IP_LOOKUP_NEXT_GLEAN:
169         adj_glean_remove(adj->ia_nh_proto,
170                          adj->rewrite_header.sw_if_index);
171         break;
172     case IP_LOOKUP_NEXT_MCAST:
173         adj_mcast_remove(adj->ia_nh_proto,
174                          adj->rewrite_header.sw_if_index);
175         break;
176     default:
177         /*
178          * type not stored in any DB from which we need to remove it
179          */
180         break;
181     }
182
183     vlib_worker_thread_barrier_release(vm);
184
185     fib_node_deinit(&adj->ia_node);
186     pool_put(adj_pool, adj);
187 }
188
189 void
190 adj_lock (adj_index_t adj_index)
191 {
192     ip_adjacency_t *adj;
193
194     if (adj_index_is_special(adj_index))
195     {
196         return;
197     }
198
199     adj = adj_get(adj_index);
200     ASSERT(adj);
201
202     ADJ_DBG(adj, "lock");
203     fib_node_lock(&adj->ia_node);
204 }
205
206 void
207 adj_unlock (adj_index_t adj_index)
208 {
209     ip_adjacency_t *adj;
210
211     if (adj_index_is_special(adj_index))
212     {
213         return;
214     }
215
216     adj = adj_get(adj_index);
217     ASSERT(adj);
218
219     ADJ_DBG(adj, "unlock");
220     ASSERT(adj);
221
222     fib_node_unlock(&adj->ia_node);
223 }
224
225 u32
226 adj_child_add (adj_index_t adj_index,
227                fib_node_type_t child_type,
228                fib_node_index_t child_index)
229 {
230     ASSERT(ADJ_INDEX_INVALID != adj_index);
231     if (adj_index_is_special(adj_index))
232     {
233         return (~0);
234     }
235
236     return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
237                                adj_index,
238                                child_type,
239                                child_index));
240 }
241
242 void
243 adj_child_remove (adj_index_t adj_index,
244                   u32 sibling_index)
245 {
246     if (adj_index_is_special(adj_index))
247     {
248         return;
249     }
250
251     fib_node_child_remove(FIB_NODE_TYPE_ADJ,
252                           adj_index,
253                           sibling_index);
254 }
255
256 /*
257  * Context for the walk to update the cached feture flags.
258  */
259 typedef struct adj_feature_update_t_
260 {
261     u8 arc;
262     u8 enable;
263 } adj_feature_update_ctx_t;
264
265 static adj_walk_rc_t
266 adj_feature_update_walk_cb (adj_index_t ai,
267                             void *arg)
268 {
269     adj_feature_update_ctx_t *ctx = arg;
270     ip_adjacency_t *adj;
271
272     adj = adj_get(ai);
273
274     /*
275      * this ugly mess matches the feature arc that is changing with affected
276      * adjacencies
277      */
278     if (((ctx->arc == ip6_main.lookup_main.output_feature_arc_index) &&
279          (VNET_LINK_IP6 == adj->ia_link)) ||
280         ((ctx->arc == ip4_main.lookup_main.output_feature_arc_index) &&
281          (VNET_LINK_IP4 == adj->ia_link)) ||
282         ((ctx->arc == mpls_main.output_feature_arc_index) &&
283          (VNET_LINK_MPLS == adj->ia_link)))
284     {
285         if (ctx->enable)
286             adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES;
287         else
288             adj->rewrite_header.flags &= ~VNET_REWRITE_HAS_FEATURES;
289     }
290     return (ADJ_WALK_RC_CONTINUE);
291 }
292
293 void
294 adj_feature_update (u32 sw_if_index,
295                     u8 arc_index,
296                     u8 is_enable)
297 {
298     /*
299      * Walk all the adjacencies on the interface to update the cached
300      * 'has-features' flag
301      */
302     adj_feature_update_ctx_t ctx = {
303         .arc = arc_index,
304         .enable = is_enable,
305     };
306     adj_walk (sw_if_index, adj_feature_update_walk_cb, &ctx);
307 }
308
309 /**
310  * @brief Walk the Adjacencies on a given interface
311  */
312 void
313 adj_walk (u32 sw_if_index,
314           adj_walk_cb_t cb,
315           void *ctx)
316 {
317     /*
318      * walk all the neighbor adjacencies
319      */
320     fib_protocol_t proto;
321
322     FOR_EACH_FIB_IP_PROTOCOL(proto)
323     {
324         adj_nbr_walk(sw_if_index, proto, cb, ctx);
325     }
326 }
327
328 /**
329  * @brief Return the link type of the adjacency
330  */
331 vnet_link_t
332 adj_get_link_type (adj_index_t ai)
333 {
334     const ip_adjacency_t *adj;
335
336     adj = adj_get(ai);
337
338     return (adj->ia_link); 
339 }
340
341 /**
342  * @brief Return the sw interface index of the adjacency.
343  */
344 u32
345 adj_get_sw_if_index (adj_index_t ai)
346 {
347     const ip_adjacency_t *adj;
348
349     adj = adj_get(ai);
350
351     return (adj->rewrite_header.sw_if_index);
352 }
353
354 /**
355  * @brief Return the rewrite string of the adjacency
356  */
357 const u8*
358 adj_get_rewrite (adj_index_t ai)
359 {
360     vnet_rewrite_header_t *rw;
361     ip_adjacency_t *adj;
362
363     adj = adj_get(ai);
364     rw = &adj->rewrite_header;
365
366     ASSERT (rw->data_bytes != 0xfefe);
367
368     return (rw->data - rw->data_bytes);
369 }
370
371 static fib_node_t *
372 adj_get_node (fib_node_index_t index)
373 {
374     ip_adjacency_t *adj;
375
376     adj = adj_get(index);
377
378     return (&adj->ia_node);
379 }
380
381 #define ADJ_FROM_NODE(_node)                                            \
382     ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
383
384 static void
385 adj_node_last_lock_gone (fib_node_t *node)
386 {
387     adj_last_lock_gone(ADJ_FROM_NODE(node));
388 }
389
390 static fib_node_back_walk_rc_t
391 adj_back_walk_notify (fib_node_t *node,
392                       fib_node_back_walk_ctx_t *ctx)
393 {
394     /*
395      * Que pasa. yo soj en el final!
396      */
397     ASSERT(0);
398
399     return (FIB_NODE_BACK_WALK_CONTINUE);
400 }
401
402 /*
403  * Adjacency's graph node virtual function table
404  */
405 static const fib_node_vft_t adj_vft = {
406     .fnv_get = adj_get_node,
407     .fnv_last_lock = adj_node_last_lock_gone,
408     .fnv_back_walk = adj_back_walk_notify,
409 };
410
411 static clib_error_t *
412 adj_module_init (vlib_main_t * vm)
413 {
414     fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
415
416     adj_nbr_module_init();
417     adj_glean_module_init();
418     adj_midchain_module_init();
419     adj_mcast_module_init();
420
421     return (NULL);
422 }
423
424 VLIB_INIT_FUNCTION (adj_module_init);
425
426 static clib_error_t *
427 adj_show (vlib_main_t * vm,
428           unformat_input_t * input,
429           vlib_cli_command_t * cmd)
430 {
431     adj_index_t ai = ADJ_INDEX_INVALID;
432     u32 sw_if_index = ~0;
433     int summary = 0;
434
435     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
436     {
437         if (unformat (input, "%d", &ai))
438             ;
439         else if (unformat (input, "sum"))
440             summary = 1;
441         else if (unformat (input, "summary"))
442             summary = 1;
443         else if (unformat (input, "%U",
444                            unformat_vnet_sw_interface, vnet_get_main(),
445                            &sw_if_index))
446             ;
447         else
448             break;
449     }
450
451     if (summary)
452     {
453         vlib_cli_output (vm, "Number of adjacenies: %d", pool_elts(adj_pool));
454         vlib_cli_output (vm, "Per-adjacency counters: %s",
455                          (adj_are_counters_enabled() ?
456                           "enabled":
457                           "disabled"));
458     }
459     else
460     {
461         if (ADJ_INDEX_INVALID != ai)
462         {
463             if (pool_is_free_index(adj_pool, ai))
464             {
465                 vlib_cli_output (vm, "adjacency %d invalid", ai);
466                 return 0;
467             }
468
469             vlib_cli_output (vm, "[@%d] %U",
470                              ai,
471                              format_ip_adjacency,  ai,
472                              FORMAT_IP_ADJACENCY_DETAIL);
473         }
474         else
475         {
476             /* *INDENT-OFF* */
477             pool_foreach_index(ai, adj_pool,
478             ({
479                 if (~0 != sw_if_index &&
480                     sw_if_index != adj_get_sw_if_index(ai))
481                 {
482                 }
483                 else
484                 {
485                     vlib_cli_output (vm, "[@%d] %U",
486                                      ai,
487                                      format_ip_adjacency, ai,
488                                      FORMAT_IP_ADJACENCY_NONE);
489                 }
490             }));
491             /* *INDENT-ON* */
492         }
493     }
494     return 0;
495 }
496
497 /*?
498  * Show all adjacencies.
499  * @cliexpar
500  * @cliexstart{sh adj}
501  * [@0]
502  * [@1]  glean: loop0
503  * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
504  * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
505  * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
506  * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
507  * @cliexend
508  ?*/
509 VLIB_CLI_COMMAND (adj_show_command, static) = {
510     .path = "show adj",
511     .short_help = "show adj [<adj_index>] [interface] [summary]",
512     .function = adj_show,
513 };
514
515 /**
516  * @brief CLI invoked function to enable/disable per-adj counters
517  */
518 static clib_error_t *
519 adj_cli_counters_set (vlib_main_t * vm,
520                       unformat_input_t * input,
521                       vlib_cli_command_t * cmd)
522 {
523     clib_error_t *error = NULL;
524     int enable = ~0;
525
526     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
527     {
528         if (unformat (input, "enable"))
529             enable = 1;
530         else if (unformat (input, "disable"))
531             enable = 0;
532         else
533             break;
534     }
535
536     if (enable != ~0)
537     {
538         /* user requested something sensible */
539         adj_per_adj_counters = enable;
540     }
541     else
542     {
543         error = clib_error_return (0, "specify 'enable' or 'disable'");
544     }
545
546     return (error);
547 }
548
549 /*?
550  * Enabe/disble per-adjacency counters. This is optional because it comes with
551  * a non-negligible performance cost.
552  ?*/
553 VLIB_CLI_COMMAND (adj_cli_counters_set_command, static) = {
554     .path = "adjacency counters",
555     .short_help = "adjacency counters [enable|disable]",
556     .function = adj_cli_counters_set,
557 };