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>
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.
28 static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
30 /* Adjacency packet/byte counters indexed by adjacency index. */
31 vlib_combined_counter_main_t adjacency_counters;
36 ip_adjacency_t *adj_pool;
39 adj_poison (ip_adjacency_t * adj)
43 memset (adj, 0xfe, sizeof (adj[0]));
48 adj_alloc (fib_protocol_t proto)
52 pool_get(adj_pool, adj);
56 /* Make sure certain fields are always initialized. */
57 /* Validate adjacency counters. */
58 vlib_validate_combined_counter(&adjacency_counters,
61 adj->rewrite_header.sw_if_index = ~0;
63 adj->lookup_next_index = 0;
65 fib_node_init(&adj->ia_node,
67 adj->ia_nh_proto = proto;
70 ip4_main.lookup_main.adjacency_heap = adj_pool;
71 ip6_main.lookup_main.adjacency_heap = adj_pool;
77 adj_index_is_special (adj_index_t adj_index)
79 if (ADJ_INDEX_INVALID == adj_index)
86 * @brief Pretty print helper function for formatting specific adjacencies.
87 * @param s - input string to format
88 * @param args - other args passed to format function such as:
94 format_ip_adjacency (u8 * s, va_list * args)
96 format_ip_adjacency_flags_t fiaf;
100 adj_index = va_arg (*args, u32);
101 fiaf = va_arg (*args, format_ip_adjacency_flags_t);
102 adj = adj_get(adj_index);
104 switch (adj->lookup_next_index)
106 case IP_LOOKUP_NEXT_REWRITE:
107 s = format (s, "%U", format_adj_nbr, adj_index, 0);
109 case IP_LOOKUP_NEXT_ARP:
110 s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
112 case IP_LOOKUP_NEXT_GLEAN:
113 s = format (s, "%U", format_adj_glean, adj_index, 0);
115 case IP_LOOKUP_NEXT_MIDCHAIN:
116 s = format (s, "%U", format_adj_midchain, adj_index, 2);
118 case IP_LOOKUP_NEXT_MCAST:
119 s = format (s, "%U", format_adj_mcast, adj_index, 0);
125 if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
127 vlib_counter_t counts;
129 vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
130 s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
131 s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
132 s = format (s, " node:[%d]:%U",
133 adj->rewrite_header.node_index,
134 format_vlib_node_name, vlib_get_main(),
135 adj->rewrite_header.node_index);
136 s = format (s, " next:[%d]:%U",
137 adj->rewrite_header.next_index,
138 format_vlib_next_node_name,
140 adj->rewrite_header.node_index,
141 adj->rewrite_header.next_index);
142 s = format(s, "\n children:\n ");
143 s = fib_node_children_format(adj->ia_node.fn_children, s);
152 * last lock/reference to the adj has gone, we no longer need it.
155 adj_last_lock_gone (ip_adjacency_t *adj)
157 vlib_main_t * vm = vlib_get_main();
159 ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
160 ADJ_DBG(adj, "last-lock-gone");
162 vlib_worker_thread_barrier_sync (vm);
164 switch (adj->lookup_next_index)
166 case IP_LOOKUP_NEXT_MIDCHAIN:
167 dpo_reset(&adj->sub_type.midchain.next_dpo);
169 case IP_LOOKUP_NEXT_ARP:
170 case IP_LOOKUP_NEXT_REWRITE:
172 * complete and incomplete nbr adjs
174 adj_nbr_remove(adj_get_index(adj),
177 &adj->sub_type.nbr.next_hop,
178 adj->rewrite_header.sw_if_index);
180 case IP_LOOKUP_NEXT_GLEAN:
181 adj_glean_remove(adj->ia_nh_proto,
182 adj->rewrite_header.sw_if_index);
184 case IP_LOOKUP_NEXT_MCAST:
185 adj_mcast_remove(adj->ia_nh_proto,
186 adj->rewrite_header.sw_if_index);
190 * type not stored in any DB from which we need to remove it
195 vlib_worker_thread_barrier_release(vm);
197 fib_node_deinit(&adj->ia_node);
198 pool_put(adj_pool, adj);
202 adj_lock (adj_index_t adj_index)
206 if (adj_index_is_special(adj_index))
211 adj = adj_get(adj_index);
214 ADJ_DBG(adj, "lock");
215 fib_node_lock(&adj->ia_node);
219 adj_unlock (adj_index_t adj_index)
223 if (adj_index_is_special(adj_index))
228 adj = adj_get(adj_index);
231 ADJ_DBG(adj, "unlock");
234 fib_node_unlock(&adj->ia_node);
238 adj_child_add (adj_index_t adj_index,
239 fib_node_type_t child_type,
240 fib_node_index_t child_index)
242 ASSERT(ADJ_INDEX_INVALID != adj_index);
243 if (adj_index_is_special(adj_index))
248 return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
255 adj_child_remove (adj_index_t adj_index,
258 if (adj_index_is_special(adj_index))
263 fib_node_child_remove(FIB_NODE_TYPE_ADJ,
269 * @brief Return the link type of the adjacency
272 adj_get_link_type (adj_index_t ai)
274 const ip_adjacency_t *adj;
278 return (adj->ia_link);
282 * @brief Return the sw interface index of the adjacency.
285 adj_get_sw_if_index (adj_index_t ai)
287 const ip_adjacency_t *adj;
291 return (adj->rewrite_header.sw_if_index);
295 * @brief Return the link type of the adjacency
298 adj_get_rewrite (adj_index_t ai)
300 vnet_rewrite_header_t *rw;
304 rw = &adj->rewrite_header;
306 ASSERT (rw->data_bytes != 0xfefe);
308 return (rw->data - rw->data_bytes);
312 adj_get_node (fib_node_index_t index)
316 adj = adj_get(index);
318 return (&adj->ia_node);
321 #define ADJ_FROM_NODE(_node) \
322 ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
325 adj_node_last_lock_gone (fib_node_t *node)
327 adj_last_lock_gone(ADJ_FROM_NODE(node));
330 static fib_node_back_walk_rc_t
331 adj_back_walk_notify (fib_node_t *node,
332 fib_node_back_walk_ctx_t *ctx)
335 * Que pasa. yo soj en el final!
339 return (FIB_NODE_BACK_WALK_CONTINUE);
343 * Adjacency's graph node virtual function table
345 static const fib_node_vft_t adj_vft = {
346 .fnv_get = adj_get_node,
347 .fnv_last_lock = adj_node_last_lock_gone,
348 .fnv_back_walk = adj_back_walk_notify,
351 static clib_error_t *
352 adj_module_init (vlib_main_t * vm)
354 fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
356 adj_nbr_module_init();
357 adj_glean_module_init();
358 adj_midchain_module_init();
359 adj_mcast_module_init();
362 * one special adj to reserve index 0
364 special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
369 VLIB_INIT_FUNCTION (adj_module_init);
371 static clib_error_t *
372 adj_show (vlib_main_t * vm,
373 unformat_input_t * input,
374 vlib_cli_command_t * cmd)
376 adj_index_t ai = ADJ_INDEX_INVALID;
377 u32 sw_if_index = ~0;
379 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
381 if (unformat (input, "%d", &ai))
383 else if (unformat (input, "%U",
384 unformat_vnet_sw_interface, vnet_get_main(),
391 if (ADJ_INDEX_INVALID != ai)
393 if (pool_is_free_index(adj_pool, ai))
395 vlib_cli_output (vm, "adjacency %d invalid", ai);
399 vlib_cli_output (vm, "[@%d] %U",
401 format_ip_adjacency, ai,
402 FORMAT_IP_ADJACENCY_DETAIL);
407 pool_foreach_index(ai, adj_pool,
409 if (~0 != sw_if_index &&
410 sw_if_index != adj_get_sw_if_index(ai))
415 vlib_cli_output (vm, "[@%d] %U",
417 format_ip_adjacency, ai,
418 FORMAT_IP_ADJACENCY_NONE);
428 * Show all adjacencies.
430 * @cliexstart{sh adj}
433 * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
434 * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
435 * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
436 * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
439 VLIB_CLI_COMMAND (adj_show_command, static) = {
441 .short_help = "show adj [<adj_index>] [interface]",
442 .function = adj_show,
446 * DEPRECATED: DO NOT USE
449 ip_add_adjacency (ip_lookup_main_t * lm,
450 ip_adjacency_t * copy_adj,
452 u32 * adj_index_return)
454 ip_adjacency_t * adj;
458 adj = adj_alloc(FIB_PROTOCOL_IP4);
463 *adj_index_return = adj_get_index(adj);