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/fib/fib_node_list.h>
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.
27 static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
29 /* Adjacency packet/byte counters indexed by adjacency index. */
30 vlib_combined_counter_main_t adjacency_counters;
35 ip_adjacency_t *adj_pool;
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;
61 adj->mcast_group_index = ~0;
62 adj->saved_lookup_next_index = 0;
65 fib_node_init(&adj->ia_node,
67 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);
121 if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
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,
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);
144 * last lock/reference to the adj has gone, we no longer need it.
147 adj_last_lock_gone (ip_adjacency_t *adj)
149 vlib_main_t * vm = vlib_get_main();
151 ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
152 ADJ_DBG(adj, "last-lock-gone");
154 vlib_worker_thread_barrier_sync (vm);
156 switch (adj->lookup_next_index)
158 case IP_LOOKUP_NEXT_MIDCHAIN:
159 dpo_reset(&adj->sub_type.midchain.next_dpo);
161 case IP_LOOKUP_NEXT_ARP:
162 case IP_LOOKUP_NEXT_REWRITE:
164 * complete and incomplete nbr adjs
166 adj_nbr_remove(adj->ia_nh_proto,
168 &adj->sub_type.nbr.next_hop,
169 adj->rewrite_header.sw_if_index);
171 case IP_LOOKUP_NEXT_GLEAN:
172 adj_glean_remove(adj->ia_nh_proto,
173 adj->rewrite_header.sw_if_index);
177 * type not stored in any DB from which we need to remove it
182 vlib_worker_thread_barrier_release(vm);
184 fib_node_deinit(&adj->ia_node);
185 pool_put(adj_pool, adj);
189 adj_lock (adj_index_t adj_index)
193 if (adj_index_is_special(adj_index))
198 adj = adj_get(adj_index);
201 ADJ_DBG(adj, "lock");
202 fib_node_lock(&adj->ia_node);
206 adj_unlock (adj_index_t adj_index)
210 if (adj_index_is_special(adj_index))
215 adj = adj_get(adj_index);
218 ADJ_DBG(adj, "unlock");
221 fib_node_unlock(&adj->ia_node);
225 adj_child_add (adj_index_t adj_index,
226 fib_node_type_t child_type,
227 fib_node_index_t child_index)
229 ASSERT(ADJ_INDEX_INVALID != adj_index);
230 if (adj_index_is_special(adj_index))
235 return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
242 adj_child_remove (adj_index_t adj_index,
245 if (adj_index_is_special(adj_index))
250 fib_node_child_remove(FIB_NODE_TYPE_ADJ,
256 * @brief Return the link type of the adjacency
259 adj_get_link_type (adj_index_t ai)
261 const ip_adjacency_t *adj;
265 return (adj->ia_link);
269 * @brief Return the sw interface index of the adjacency.
272 adj_get_sw_if_index (adj_index_t ai)
274 const ip_adjacency_t *adj;
278 return (adj->rewrite_header.sw_if_index);
282 * @brief Return the link type of the adjacency
285 adj_get_rewrite (adj_index_t ai)
287 vnet_rewrite_header_t *rw;
291 rw = &adj->rewrite_header;
293 ASSERT (rw->data_bytes != 0xfefe);
295 return (rw->data - rw->data_bytes);
299 adj_get_node (fib_node_index_t index)
303 adj = adj_get(index);
305 return (&adj->ia_node);
308 #define ADJ_FROM_NODE(_node) \
309 ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
312 adj_node_last_lock_gone (fib_node_t *node)
314 adj_last_lock_gone(ADJ_FROM_NODE(node));
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)
322 * Que pasa. yo soj en el final!
326 return (FIB_NODE_BACK_WALK_CONTINUE);
330 * Adjacency's graph node virtual function table
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,
338 static clib_error_t *
339 adj_module_init (vlib_main_t * vm)
341 fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
343 adj_nbr_module_init();
344 adj_glean_module_init();
345 adj_midchain_module_init();
348 * one special adj to reserve index 0
350 special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
355 VLIB_INIT_FUNCTION (adj_module_init);
357 static clib_error_t *
358 adj_show (vlib_main_t * vm,
359 unformat_input_t * input,
360 vlib_cli_command_t * cmd)
362 adj_index_t ai = ADJ_INDEX_INVALID;
363 u32 sw_if_index = ~0;
365 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
367 if (unformat (input, "%d", &ai))
369 else if (unformat (input, "%U",
370 unformat_vnet_sw_interface, vnet_get_main(),
377 if (ADJ_INDEX_INVALID != ai)
379 vlib_cli_output (vm, "[@%d] %U",
381 format_ip_adjacency, ai,
382 FORMAT_IP_ADJACENCY_DETAIL);
387 pool_foreach_index(ai, adj_pool,
389 if (~0 != sw_if_index &&
390 sw_if_index != adj_get_sw_if_index(ai))
395 vlib_cli_output (vm, "[@%d] %U",
397 format_ip_adjacency, ai,
398 FORMAT_IP_ADJACENCY_NONE);
408 * Show all adjacencies.
410 * @cliexstart{sh adj}
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
419 VLIB_CLI_COMMAND (adj_show_command, static) = {
421 .short_help = "show adj [<adj_index>] [interface]",
422 .function = adj_show,
426 * DEPRECATED: DO NOT USE
429 ip_add_adjacency (ip_lookup_main_t * lm,
430 ip_adjacency_t * copy_adj,
432 u32 * adj_index_return)
434 ip_adjacency_t * adj;
438 adj = adj_alloc(FIB_PROTOCOL_IP4);
443 *adj_index_return = adj_get_index(adj);