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 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
96 u32 adj_index = va_arg (*args, u32);
97 format_ip_adjacency_flags_t fiaf = va_arg (*args, format_ip_adjacency_flags_t);
98 ip_adjacency_t * adj = adj_get(adj_index);
100 switch (adj->lookup_next_index)
102 case IP_LOOKUP_NEXT_REWRITE:
103 s = format (s, "%U", format_adj_nbr, adj_index, 0);
105 case IP_LOOKUP_NEXT_ARP:
106 s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
108 case IP_LOOKUP_NEXT_GLEAN:
109 s = format (s, " %U",
110 format_vnet_sw_interface_name,
112 vnet_get_sw_interface(vnm,
113 adj->rewrite_header.sw_if_index));
116 case IP_LOOKUP_NEXT_MIDCHAIN:
117 s = format (s, "%U", format_adj_midchain, adj_index, 2);
122 s = format (s, " index:%d", adj_index);
124 if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
126 s = format (s, " locks:%d", adj->ia_node.fn_locks);
127 s = format(s, "\nchildren:\n ");
128 s = fib_node_children_format(adj->ia_node.fn_children, s);
137 * last lock/reference to the adj has gone, we no longer need it.
140 adj_last_lock_gone (ip_adjacency_t *adj)
142 ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
143 ADJ_DBG(adj, "last-lock-gone");
145 switch (adj->lookup_next_index)
147 case IP_LOOKUP_NEXT_MIDCHAIN:
148 dpo_reset(&adj->sub_type.midchain.next_dpo);
150 case IP_LOOKUP_NEXT_ARP:
151 case IP_LOOKUP_NEXT_REWRITE:
153 * complete and incomplete nbr adjs
155 adj_nbr_remove(adj->ia_nh_proto,
157 &adj->sub_type.nbr.next_hop,
158 adj->rewrite_header.sw_if_index);
160 case IP_LOOKUP_NEXT_GLEAN:
161 adj_glean_remove(adj->ia_nh_proto,
162 adj->rewrite_header.sw_if_index);
166 * type not stored in any DB from which we need to remove it
171 fib_node_deinit(&adj->ia_node);
172 pool_put(adj_pool, adj);
176 adj_lock (adj_index_t adj_index)
180 if (adj_index_is_special(adj_index))
185 adj = adj_get(adj_index);
188 ADJ_DBG(adj, "lock");
189 fib_node_lock(&adj->ia_node);
193 adj_unlock (adj_index_t adj_index)
197 if (adj_index_is_special(adj_index))
202 adj = adj_get(adj_index);
205 ADJ_DBG(adj, "unlock");
208 fib_node_unlock(&adj->ia_node);
212 adj_child_add (adj_index_t adj_index,
213 fib_node_type_t child_type,
214 fib_node_index_t child_index)
216 ASSERT(ADJ_INDEX_INVALID != adj_index);
217 if (adj_index_is_special(adj_index))
222 return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
229 adj_child_remove (adj_index_t adj_index,
232 if (adj_index_is_special(adj_index))
237 fib_node_child_remove(FIB_NODE_TYPE_ADJ,
243 adj_get_node (fib_node_index_t index)
247 adj = adj_get(index);
249 return (&adj->ia_node);
252 #define ADJ_FROM_NODE(_node) \
253 ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
256 adj_node_last_lock_gone (fib_node_t *node)
258 adj_last_lock_gone(ADJ_FROM_NODE(node));
261 static fib_node_back_walk_rc_t
262 adj_back_walk_notify (fib_node_t *node,
263 fib_node_back_walk_ctx_t *ctx)
266 * Que pasa. yo soj en el final!
270 return (FIB_NODE_BACK_WALK_CONTINUE);
274 * Adjacency's graph node virtual function table
276 static const fib_node_vft_t adj_vft = {
277 .fnv_get = adj_get_node,
278 .fnv_last_lock = adj_node_last_lock_gone,
279 .fnv_back_walk = adj_back_walk_notify,
282 static clib_error_t *
283 adj_module_init (vlib_main_t * vm)
285 fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
287 adj_nbr_module_init();
288 adj_glean_module_init();
289 adj_midchain_module_init();
292 * 4 special adjs for v4 and v6 resp.
294 special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
299 VLIB_INIT_FUNCTION (adj_module_init);
302 * DEPRECATED: DO NOT USE
304 * Create new block of given number of contiguous adjacencies.
307 ip_add_adjacency (ip_lookup_main_t * lm,
308 ip_adjacency_t * copy_adj,
310 u32 * adj_index_return)
312 ip_adjacency_t * adj;
316 adj = adj_alloc(FIB_PROTOCOL_IP4);
321 *adj_index_return = adj_get_index(adj);