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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
23 /* Adjacency packet/byte counters indexed by adjacency index. */
24 vlib_combined_counter_main_t adjacency_counters;
29 ip_adjacency_t *adj_pool;
32 * @brief Global Config for enabling per-adjacency counters.
33 * By default these are disabled.
35 int adj_per_adj_counters;
38 adj_poison (ip_adjacency_t * adj)
42 memset (adj, 0xfe, sizeof (adj[0]));
47 adj_alloc (fib_protocol_t proto)
51 pool_get(adj_pool, adj);
55 /* Make sure certain fields are always initialized. */
56 /* Validate adjacency counters. */
57 vlib_validate_combined_counter(&adjacency_counters,
60 adj->rewrite_header.sw_if_index = ~0;
62 adj->lookup_next_index = 0;
64 fib_node_init(&adj->ia_node,
66 adj->ia_nh_proto = proto;
69 ip4_main.lookup_main.adjacency_heap = adj_pool;
70 ip6_main.lookup_main.adjacency_heap = adj_pool;
76 adj_index_is_special (adj_index_t adj_index)
78 if (ADJ_INDEX_INVALID == adj_index)
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:
93 format_ip_adjacency (u8 * s, va_list * args)
95 format_ip_adjacency_flags_t fiaf;
99 adj_index = va_arg (*args, u32);
100 fiaf = va_arg (*args, format_ip_adjacency_flags_t);
101 adj = adj_get(adj_index);
103 switch (adj->lookup_next_index)
105 case IP_LOOKUP_NEXT_REWRITE:
106 s = format (s, "%U", format_adj_nbr, adj_index, 0);
108 case IP_LOOKUP_NEXT_ARP:
109 s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
111 case IP_LOOKUP_NEXT_GLEAN:
112 s = format (s, "%U", format_adj_glean, adj_index, 0);
114 case IP_LOOKUP_NEXT_MIDCHAIN:
115 s = format (s, "%U", format_adj_midchain, adj_index, 2);
117 case IP_LOOKUP_NEXT_MCAST:
118 s = format (s, "%U", format_adj_mcast, adj_index, 0);
124 if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
126 vlib_counter_t counts;
128 vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
129 s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
130 s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
131 s = format(s, "\n children:\n ");
132 s = fib_node_children_format(adj->ia_node.fn_children, s);
141 * last lock/reference to the adj has gone, we no longer need it.
144 adj_last_lock_gone (ip_adjacency_t *adj)
146 vlib_main_t * vm = vlib_get_main();
148 ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
149 ADJ_DBG(adj, "last-lock-gone");
151 vlib_worker_thread_barrier_sync (vm);
153 switch (adj->lookup_next_index)
155 case IP_LOOKUP_NEXT_MIDCHAIN:
156 dpo_reset(&adj->sub_type.midchain.next_dpo);
158 case IP_LOOKUP_NEXT_ARP:
159 case IP_LOOKUP_NEXT_REWRITE:
161 * complete and incomplete nbr adjs
163 adj_nbr_remove(adj_get_index(adj),
166 &adj->sub_type.nbr.next_hop,
167 adj->rewrite_header.sw_if_index);
169 case IP_LOOKUP_NEXT_GLEAN:
170 adj_glean_remove(adj->ia_nh_proto,
171 adj->rewrite_header.sw_if_index);
173 case IP_LOOKUP_NEXT_MCAST:
174 adj_mcast_remove(adj->ia_nh_proto,
175 adj->rewrite_header.sw_if_index);
179 * type not stored in any DB from which we need to remove it
184 vlib_worker_thread_barrier_release(vm);
186 fib_node_deinit(&adj->ia_node);
187 pool_put(adj_pool, adj);
191 adj_lock (adj_index_t adj_index)
195 if (adj_index_is_special(adj_index))
200 adj = adj_get(adj_index);
203 ADJ_DBG(adj, "lock");
204 fib_node_lock(&adj->ia_node);
208 adj_unlock (adj_index_t adj_index)
212 if (adj_index_is_special(adj_index))
217 adj = adj_get(adj_index);
220 ADJ_DBG(adj, "unlock");
223 fib_node_unlock(&adj->ia_node);
227 adj_child_add (adj_index_t adj_index,
228 fib_node_type_t child_type,
229 fib_node_index_t child_index)
231 ASSERT(ADJ_INDEX_INVALID != adj_index);
232 if (adj_index_is_special(adj_index))
237 return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
244 adj_child_remove (adj_index_t adj_index,
247 if (adj_index_is_special(adj_index))
252 fib_node_child_remove(FIB_NODE_TYPE_ADJ,
258 * Context for the walk to update the cached feture flags.
260 typedef struct adj_feature_update_t_
264 } adj_feature_update_ctx_t;
267 adj_feature_update_walk_cb (adj_index_t ai,
270 adj_feature_update_ctx_t *ctx = arg;
276 * this ugly mess matches the feature arc that is changing with affected
279 if (((ctx->arc == ip6_main.lookup_main.output_feature_arc_index) &&
280 (VNET_LINK_IP6 == adj->ia_link)) ||
281 ((ctx->arc == ip4_main.lookup_main.output_feature_arc_index) &&
282 (VNET_LINK_IP4 == adj->ia_link)) ||
283 ((ctx->arc == mpls_main.output_feature_arc_index) &&
284 (VNET_LINK_MPLS == adj->ia_link)))
287 adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES;
289 adj->rewrite_header.flags &= ~VNET_REWRITE_HAS_FEATURES;
291 return (ADJ_WALK_RC_CONTINUE);
295 adj_feature_update (u32 sw_if_index,
300 * Walk all the adjacencies on the interface to update the cached
301 * 'has-features' flag
303 adj_feature_update_ctx_t ctx = {
307 adj_walk (sw_if_index, adj_feature_update_walk_cb, &ctx);
311 * @brief Walk the Adjacencies on a given interface
314 adj_walk (u32 sw_if_index,
319 * walk all the neighbor adjacencies
321 fib_protocol_t proto;
323 FOR_EACH_FIB_IP_PROTOCOL(proto)
325 adj_nbr_walk(sw_if_index, proto, cb, ctx);
330 * @brief Return the link type of the adjacency
333 adj_get_link_type (adj_index_t ai)
335 const ip_adjacency_t *adj;
339 return (adj->ia_link);
343 * @brief Return the sw interface index of the adjacency.
346 adj_get_sw_if_index (adj_index_t ai)
348 const ip_adjacency_t *adj;
352 return (adj->rewrite_header.sw_if_index);
356 * @brief Return the rewrite string of the adjacency
359 adj_get_rewrite (adj_index_t ai)
361 vnet_rewrite_header_t *rw;
365 rw = &adj->rewrite_header;
367 ASSERT (rw->data_bytes != 0xfefe);
369 return (rw->data - rw->data_bytes);
373 adj_get_node (fib_node_index_t index)
377 adj = adj_get(index);
379 return (&adj->ia_node);
382 #define ADJ_FROM_NODE(_node) \
383 ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
386 adj_node_last_lock_gone (fib_node_t *node)
388 adj_last_lock_gone(ADJ_FROM_NODE(node));
391 static fib_node_back_walk_rc_t
392 adj_back_walk_notify (fib_node_t *node,
393 fib_node_back_walk_ctx_t *ctx)
396 * Que pasa. yo soj en el final!
400 return (FIB_NODE_BACK_WALK_CONTINUE);
404 * Adjacency's graph node virtual function table
406 static const fib_node_vft_t adj_vft = {
407 .fnv_get = adj_get_node,
408 .fnv_last_lock = adj_node_last_lock_gone,
409 .fnv_back_walk = adj_back_walk_notify,
412 static clib_error_t *
413 adj_module_init (vlib_main_t * vm)
415 fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
417 adj_nbr_module_init();
418 adj_glean_module_init();
419 adj_midchain_module_init();
420 adj_mcast_module_init();
425 VLIB_INIT_FUNCTION (adj_module_init);
427 static clib_error_t *
428 adj_show (vlib_main_t * vm,
429 unformat_input_t * input,
430 vlib_cli_command_t * cmd)
432 adj_index_t ai = ADJ_INDEX_INVALID;
433 u32 sw_if_index = ~0;
436 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
438 if (unformat (input, "%d", &ai))
440 else if (unformat (input, "sum"))
442 else if (unformat (input, "summary"))
444 else if (unformat (input, "%U",
445 unformat_vnet_sw_interface, vnet_get_main(),
454 vlib_cli_output (vm, "Number of adjacenies: %d", pool_elts(adj_pool));
455 vlib_cli_output (vm, "Per-adjacency counters: %s",
456 (adj_are_counters_enabled() ?
462 if (ADJ_INDEX_INVALID != ai)
464 if (pool_is_free_index(adj_pool, ai))
466 vlib_cli_output (vm, "adjacency %d invalid", ai);
470 vlib_cli_output (vm, "[@%d] %U",
472 format_ip_adjacency, ai,
473 FORMAT_IP_ADJACENCY_DETAIL);
478 pool_foreach_index(ai, adj_pool,
480 if (~0 != sw_if_index &&
481 sw_if_index != adj_get_sw_if_index(ai))
486 vlib_cli_output (vm, "[@%d] %U",
488 format_ip_adjacency, ai,
489 FORMAT_IP_ADJACENCY_NONE);
499 * Show all adjacencies.
501 * @cliexstart{sh adj}
504 * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
505 * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
506 * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
507 * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
510 VLIB_CLI_COMMAND (adj_show_command, static) = {
512 .short_help = "show adj [<adj_index>] [interface] [summary]",
513 .function = adj_show,
517 * @brief CLI invoked function to enable/disable per-adj counters
519 static clib_error_t *
520 adj_cli_counters_set (vlib_main_t * vm,
521 unformat_input_t * input,
522 vlib_cli_command_t * cmd)
524 clib_error_t *error = NULL;
527 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
529 if (unformat (input, "enable"))
531 else if (unformat (input, "disable"))
539 /* user requested something sensible */
540 adj_per_adj_counters = enable;
544 error = clib_error_return (0, "specify 'enable' or 'disable'");
551 * Enabe/disble per-adjacency counters. This is optional because it comes with
552 * a non-negligible performance cost.
554 VLIB_CLI_COMMAND (adj_cli_counters_set_command, static) = {
555 .path = "adjacency counters",
556 .short_help = "adjacency counters [enable|disable]",
557 .function = adj_cli_counters_set,