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/adj/adj_delegate.h>
22 #include <vnet/fib/fib_node_list.h>
23 #include <vnet/fib/fib_walk.h>
25 /* Adjacency packet/byte counters indexed by adjacency index. */
26 vlib_combined_counter_main_t adjacency_counters = {
28 .stat_segment_name = "/net/adjacency",
34 ip_adjacency_t *adj_pool;
37 * The adjacency logger
39 vlib_log_class_t adj_logger;
42 * @brief Global Config for enabling per-adjacency counters.
43 * By default these are disabled.
45 int adj_per_adj_counters;
47 const ip46_address_t ADJ_BCAST_ADDR = {
49 .as_u64[0] = 0xffffffffffffffff,
50 .as_u64[1] = 0xffffffffffffffff,
57 static const char *adj_attr_names[] = ADJ_ATTR_NAMES;
60 adj_poison (ip_adjacency_t * adj)
64 clib_memset (adj, 0xfe, sizeof (adj[0]));
69 adj_alloc (fib_protocol_t proto)
72 u8 need_barrier_sync = 0;
76 ASSERT (vm->thread_index == 0);
78 pool_get_aligned_will_expand (adj_pool, need_barrier_sync,
79 CLIB_CACHE_LINE_BYTES);
80 /* If the adj_pool will expand, stop the parade. */
81 if (need_barrier_sync)
82 vlib_worker_thread_barrier_sync (vm);
84 pool_get_aligned(adj_pool, adj, CLIB_CACHE_LINE_BYTES);
88 /* Validate adjacency counters. */
89 if (need_barrier_sync == 0)
91 /* If the adj counter pool will expand, stop the parade */
92 need_barrier_sync = vlib_validate_combined_counter_will_expand
93 (&adjacency_counters, adj_get_index (adj));
94 if (need_barrier_sync)
95 vlib_worker_thread_barrier_sync (vm);
97 vlib_validate_combined_counter(&adjacency_counters,
100 /* Make sure certain fields are always initialized. */
101 vlib_zero_combined_counter(&adjacency_counters,
103 fib_node_init(&adj->ia_node,
106 adj->ia_nh_proto = proto;
108 adj->ia_cfg_index = 0;
109 adj->rewrite_header.sw_if_index = ~0;
110 adj->rewrite_header.flags = 0;
111 adj->lookup_next_index = 0;
112 adj->ia_delegates = NULL;
114 /* lest it become a midchain in the future */
115 clib_memset(&adj->sub_type.midchain.next_dpo, 0,
116 sizeof(adj->sub_type.midchain.next_dpo));
118 if (need_barrier_sync)
119 vlib_worker_thread_barrier_release (vm);
125 adj_index_is_special (adj_index_t adj_index)
127 if (ADJ_INDEX_INVALID == adj_index)
134 format_adj_flags (u8 * s, va_list * args)
139 af = va_arg (*args, int);
141 if (ADJ_FLAG_NONE == af)
143 return (format(s, "None"));
145 FOR_EACH_ADJ_ATTR(at)
149 s = format(s, "%s ", adj_attr_names[at]);
156 * @brief Pretty print helper function for formatting specific adjacencies.
157 * @param s - input string to format
158 * @param args - other args passed to format function such as:
164 format_ip_adjacency (u8 * s, va_list * args)
166 format_ip_adjacency_flags_t fiaf;
167 ip_adjacency_t * adj;
170 adj_index = va_arg (*args, u32);
171 fiaf = va_arg (*args, format_ip_adjacency_flags_t);
173 if (!adj_is_valid(adj_index))
174 return format(s, "<invalid adjacency>");
176 adj = adj_get(adj_index);
178 switch (adj->lookup_next_index)
180 case IP_LOOKUP_NEXT_REWRITE:
181 case IP_LOOKUP_NEXT_BCAST:
182 s = format (s, "%U", format_adj_nbr, adj_index, 0);
184 case IP_LOOKUP_NEXT_ARP:
185 s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
187 case IP_LOOKUP_NEXT_GLEAN:
188 s = format (s, "%U", format_adj_glean, adj_index, 0);
190 case IP_LOOKUP_NEXT_MIDCHAIN:
191 s = format (s, "%U", format_adj_midchain, adj_index, 2);
193 case IP_LOOKUP_NEXT_MCAST:
194 s = format (s, "%U", format_adj_mcast, adj_index, 0);
196 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
197 s = format (s, "%U", format_adj_mcast_midchain, adj_index, 0);
199 case IP_LOOKUP_NEXT_DROP:
200 case IP_LOOKUP_NEXT_PUNT:
201 case IP_LOOKUP_NEXT_LOCAL:
202 case IP_LOOKUP_NEXT_ICMP_ERROR:
203 case IP_LOOKUP_N_NEXT:
207 if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
209 vlib_counter_t counts;
211 vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
212 s = format (s, "\n flags:%U", format_adj_flags, adj->ia_flags);
213 s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
214 s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
215 s = format(s, "\n delegates:");
216 s = adj_delegate_format(s, adj);
218 s = format(s, "\n children:");
219 if (fib_node_list_get_size(adj->ia_node.fn_children))
221 s = format(s, "\n ");
222 s = fib_node_children_format(adj->ia_node.fn_children, s);
230 adj_recursive_loop_detect (adj_index_t ai,
231 fib_node_index_t **entry_indicies)
233 ip_adjacency_t * adj;
237 switch (adj->lookup_next_index)
239 case IP_LOOKUP_NEXT_REWRITE:
240 case IP_LOOKUP_NEXT_ARP:
241 case IP_LOOKUP_NEXT_GLEAN:
242 case IP_LOOKUP_NEXT_MCAST:
243 case IP_LOOKUP_NEXT_BCAST:
244 case IP_LOOKUP_NEXT_DROP:
245 case IP_LOOKUP_NEXT_PUNT:
246 case IP_LOOKUP_NEXT_LOCAL:
247 case IP_LOOKUP_NEXT_ICMP_ERROR:
248 case IP_LOOKUP_N_NEXT:
250 * these adjacency types are terminal graph nodes, so there's no
251 * possibility of a loop down here.
254 case IP_LOOKUP_NEXT_MIDCHAIN:
255 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
256 return (adj_ndr_midchain_recursive_loop_detect(ai, entry_indicies));
265 * last lock/reference to the adj has gone, we no longer need it.
268 adj_last_lock_gone (ip_adjacency_t *adj)
270 vlib_main_t * vm = vlib_get_main();
272 ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
273 ADJ_DBG(adj, "last-lock-gone");
275 adj_delegate_adj_deleted(adj);
277 vlib_worker_thread_barrier_sync (vm);
279 switch (adj->lookup_next_index)
281 case IP_LOOKUP_NEXT_MIDCHAIN:
282 adj_midchain_teardown(adj);
284 case IP_LOOKUP_NEXT_ARP:
285 case IP_LOOKUP_NEXT_REWRITE:
286 case IP_LOOKUP_NEXT_BCAST:
288 * complete and incomplete nbr adjs
290 adj_nbr_remove(adj_get_index(adj),
293 &adj->sub_type.nbr.next_hop,
294 adj->rewrite_header.sw_if_index);
296 case IP_LOOKUP_NEXT_GLEAN:
297 adj_glean_remove(adj);
299 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
300 adj_midchain_teardown(adj);
302 case IP_LOOKUP_NEXT_MCAST:
303 adj_mcast_remove(adj->ia_nh_proto,
304 adj->rewrite_header.sw_if_index);
306 case IP_LOOKUP_NEXT_DROP:
307 case IP_LOOKUP_NEXT_PUNT:
308 case IP_LOOKUP_NEXT_LOCAL:
309 case IP_LOOKUP_NEXT_ICMP_ERROR:
310 case IP_LOOKUP_N_NEXT:
312 * type not stored in any DB from which we need to remove it
317 vlib_worker_thread_barrier_release(vm);
319 fib_node_deinit(&adj->ia_node);
320 ASSERT(0 == vec_len(adj->ia_delegates));
321 vec_free(adj->ia_delegates);
322 pool_put(adj_pool, adj);
326 adj_dpo_get_urpf (const dpo_id_t *dpo)
330 adj = adj_get(dpo->dpoi_index);
332 return (adj->rewrite_header.sw_if_index);
336 adj_dpo_get_mtu (const dpo_id_t *dpo)
340 adj = adj_get(dpo->dpoi_index);
342 return (adj->rewrite_header.max_l3_packet_bytes);
346 adj_lock (adj_index_t adj_index)
350 if (adj_index_is_special(adj_index))
355 adj = adj_get(adj_index);
358 fib_node_lock(&adj->ia_node);
362 adj_unlock (adj_index_t adj_index)
366 if (adj_index_is_special(adj_index))
371 adj = adj_get(adj_index);
374 fib_node_unlock(&adj->ia_node);
378 adj_child_add (adj_index_t adj_index,
379 fib_node_type_t child_type,
380 fib_node_index_t child_index)
382 ASSERT(ADJ_INDEX_INVALID != adj_index);
383 if (adj_index_is_special(adj_index))
388 return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
395 adj_child_remove (adj_index_t adj_index,
398 if (adj_index_is_special(adj_index))
403 fib_node_child_remove(FIB_NODE_TYPE_ADJ,
409 * Context for the walk to update the cached feature flags.
411 typedef struct adj_feature_update_t_
415 } adj_feature_update_ctx_t;
418 adj_feature_update_walk_cb (adj_index_t ai,
421 adj_feature_update_ctx_t *ctx = arg;
427 * this ugly mess matches the feature arc that is changing with affected
430 if (((ctx->arc == ip6_main.lookup_main.output_feature_arc_index) &&
431 (VNET_LINK_IP6 == adj->ia_link)) ||
432 ((ctx->arc == ip4_main.lookup_main.output_feature_arc_index) &&
433 (VNET_LINK_IP4 == adj->ia_link)) ||
434 ((ctx->arc == mpls_main.output_feature_arc_index) &&
435 (VNET_LINK_MPLS == adj->ia_link)))
437 vnet_feature_main_t *fm = &feature_main;
438 vnet_feature_config_main_t *cm;
440 cm = &fm->feature_config_mains[ctx->arc];
443 adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES;
445 adj->rewrite_header.flags &= ~VNET_REWRITE_HAS_FEATURES;
447 adj->ia_cfg_index = vec_elt (cm->config_index_by_sw_if_index,
448 adj->rewrite_header.sw_if_index);
450 return (ADJ_WALK_RC_CONTINUE);
454 adj_feature_update (u32 sw_if_index,
460 * Walk all the adjacencies on the interface to update the cached
461 * 'has-features' flag
463 adj_feature_update_ctx_t ctx = {
467 adj_walk (sw_if_index, adj_feature_update_walk_cb, &ctx);
471 adj_mtu_update_walk_cb (adj_index_t ai,
478 vnet_rewrite_update_mtu (vnet_get_main(), adj->ia_link,
479 &adj->rewrite_header);
480 adj_delegate_adj_modified(adj);
483 * Backwalk to all Path MTU trackers, casual like ..
486 fib_node_back_walk_ctx_t bw_ctx = {
487 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_MTU,
490 fib_walk_async(FIB_NODE_TYPE_ADJ, ai,
491 FIB_WALK_PRIORITY_LOW, &bw_ctx);
494 return (ADJ_WALK_RC_CONTINUE);
497 static clib_error_t *
498 adj_mtu_update (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
500 adj_walk (sw_if_index, adj_mtu_update_walk_cb, NULL);
505 VNET_SW_INTERFACE_MTU_CHANGE_FUNCTION(adj_mtu_update);
508 * @brief Walk the Adjacencies on a given interface
511 adj_walk (u32 sw_if_index,
516 * walk all the neighbor adjacencies
518 fib_protocol_t proto;
520 FOR_EACH_FIB_IP_PROTOCOL(proto)
522 adj_nbr_walk(sw_if_index, proto, cb, ctx);
523 adj_mcast_walk(sw_if_index, proto, cb, ctx);
528 * @brief Return the link type of the adjacency
531 adj_get_link_type (adj_index_t ai)
533 const ip_adjacency_t *adj;
537 return (adj->ia_link);
541 * @brief Return the sw interface index of the adjacency.
544 adj_get_sw_if_index (adj_index_t ai)
546 const ip_adjacency_t *adj;
550 return (adj->rewrite_header.sw_if_index);
554 * @brief Return true if the adjacency is 'UP', i.e. can be used for forwarding
555 * 0 is down, !0 is up.
558 adj_is_up (adj_index_t ai)
560 return (adj_bfd_is_up(ai));
564 * @brief Return the rewrite string of the adjacency
567 adj_get_rewrite (adj_index_t ai)
569 vnet_rewrite_header_t *rw;
573 rw = &adj->rewrite_header;
575 ASSERT (rw->data_bytes != 0xfefe);
577 return (rw->data - rw->data_bytes);
581 adj_get_node (fib_node_index_t index)
585 adj = adj_get(index);
587 return (&adj->ia_node);
590 #define ADJ_FROM_NODE(_node) \
591 ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
594 adj_node_last_lock_gone (fib_node_t *node)
596 adj_last_lock_gone(ADJ_FROM_NODE(node));
599 static fib_node_back_walk_rc_t
600 adj_back_walk_notify (fib_node_t *node,
601 fib_node_back_walk_ctx_t *ctx)
605 adj = ADJ_FROM_NODE(node);
607 switch (adj->lookup_next_index)
609 case IP_LOOKUP_NEXT_MIDCHAIN:
610 adj_midchain_delegate_restack(adj_get_index(adj));
612 case IP_LOOKUP_NEXT_ARP:
613 case IP_LOOKUP_NEXT_REWRITE:
614 case IP_LOOKUP_NEXT_BCAST:
615 case IP_LOOKUP_NEXT_GLEAN:
616 case IP_LOOKUP_NEXT_MCAST:
617 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
618 case IP_LOOKUP_NEXT_DROP:
619 case IP_LOOKUP_NEXT_PUNT:
620 case IP_LOOKUP_NEXT_LOCAL:
621 case IP_LOOKUP_NEXT_ICMP_ERROR:
622 case IP_LOOKUP_N_NEXT:
624 * Que pasa. yo soj en el final!
630 return (FIB_NODE_BACK_WALK_CONTINUE);
634 * Adjacency's graph node virtual function table
636 static const fib_node_vft_t adj_vft = {
637 .fnv_get = adj_get_node,
638 .fnv_last_lock = adj_node_last_lock_gone,
639 .fnv_back_walk = adj_back_walk_notify,
642 static clib_error_t *
643 adj_module_init (vlib_main_t * vm)
645 fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
647 adj_nbr_module_init();
648 adj_glean_module_init();
649 adj_midchain_module_init();
650 adj_mcast_module_init();
652 vnet_feature_register(adj_feature_update, NULL);
654 adj_logger = vlib_log_register_class("adj", "adj");
659 VLIB_INIT_FUNCTION (adj_module_init);
661 static clib_error_t *
662 adj_show (vlib_main_t * vm,
663 unformat_input_t * input,
664 vlib_cli_command_t * cmd)
666 adj_index_t ai = ADJ_INDEX_INVALID;
667 u32 sw_if_index = ~0;
670 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
672 if (unformat (input, "%d", &ai))
674 else if (unformat (input, "summary") || unformat (input, "sum"))
676 else if (unformat (input, "%U",
677 unformat_vnet_sw_interface, vnet_get_main(),
686 vlib_cli_output (vm, "Number of adjacencies: %d", pool_elts(adj_pool));
687 vlib_cli_output (vm, "Per-adjacency counters: %s",
688 (adj_are_counters_enabled() ?
694 if (ADJ_INDEX_INVALID != ai)
696 if (pool_is_free_index(adj_pool, ai))
698 vlib_cli_output (vm, "adjacency %d invalid", ai);
702 vlib_cli_output (vm, "[@%d] %U",
704 format_ip_adjacency, ai,
705 FORMAT_IP_ADJACENCY_DETAIL);
710 pool_foreach_index (ai, adj_pool)
712 if (~0 != sw_if_index &&
713 sw_if_index != adj_get_sw_if_index(ai))
718 vlib_cli_output (vm, "[@%d] %U",
720 format_ip_adjacency, ai,
721 FORMAT_IP_ADJACENCY_NONE);
731 * Show all adjacencies.
733 * @cliexstart{sh adj}
736 * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
737 * [@3] mpls via 1.0.0.2 loop0: MPLS: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
738 * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
739 * [@5] mpls via 1.0.0.3 loop0: MPLS: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
742 VLIB_CLI_COMMAND (adj_show_command, static) = {
744 .short_help = "show adj [<adj_index>] [interface] [summary]",
745 .function = adj_show,
749 * @brief CLI invoked function to enable/disable per-adj counters
751 static clib_error_t *
752 adj_cli_counters_set (vlib_main_t * vm,
753 unformat_input_t * input,
754 vlib_cli_command_t * cmd)
756 clib_error_t *error = NULL;
759 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
761 if (unformat (input, "enable"))
763 else if (unformat (input, "disable"))
771 /* user requested something sensible */
772 adj_per_adj_counters = enable;
776 error = clib_error_return (0, "specify 'enable' or 'disable'");
783 * Enable/disable per-adjacency counters. This is optional because it comes
784 * with a non-negligible performance cost.
786 VLIB_CLI_COMMAND (adj_cli_counters_set_command, static) = {
787 .path = "adjacency counters",
788 .short_help = "adjacency counters [enable|disable]",
789 .function = adj_cli_counters_set,