blob: 78bf6df5324402881e1af1c5410b0ea10cf2e9ec [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/adj/adj_nbr.h>
17#include <vnet/adj/adj_internal.h>
18#include <vnet/ethernet/arp_packet.h>
19#include <vnet/fib/fib_walk.h>
20
21/*
22 * Vector Hash tables of neighbour (traditional) adjacencies
23 * Key: interface(for the vector index), address (and its proto),
24 * link-type/ether-type.
25 */
Neale Ranns20aec3d2020-05-25 09:09:36 +000026static uword **adj_nbr_tables[FIB_PROTOCOL_IP_MAX];
Neale Ranns0bfe5d82016-08-25 15:29:12 +010027
Neale Ranns20aec3d2020-05-25 09:09:36 +000028typedef struct adj_nbr_key_t_
29{
30 ip46_address_t ank_ip;
31 u64 ank_linkt;
32} adj_nbr_key_t;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010033
34#define ADJ_NBR_SET_KEY(_key, _lt, _nh) \
35{ \
Neale Ranns20aec3d2020-05-25 09:09:36 +000036 ip46_address_copy(&(_key).ank_ip, (_nh)); \
37 _key.ank_linkt = (_lt); \
Neale Ranns0bfe5d82016-08-25 15:29:12 +010038}
39
40#define ADJ_NBR_ITF_OK(_proto, _itf) \
41 (((_itf) < vec_len(adj_nbr_tables[_proto])) && \
42 (NULL != adj_nbr_tables[_proto][sw_if_index]))
43
44static void
45adj_nbr_insert (fib_protocol_t nh_proto,
Neale Ranns924d03a2016-10-19 08:25:46 +010046 vnet_link_t link_type,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010047 const ip46_address_t *nh_addr,
48 u32 sw_if_index,
49 adj_index_t adj_index)
50{
Neale Ranns20aec3d2020-05-25 09:09:36 +000051 adj_nbr_key_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052
53 if (sw_if_index >= vec_len(adj_nbr_tables[nh_proto]))
54 {
55 vec_validate(adj_nbr_tables[nh_proto], sw_if_index);
56 }
57 if (NULL == adj_nbr_tables[nh_proto][sw_if_index])
58 {
59 adj_nbr_tables[nh_proto][sw_if_index] =
Neale Ranns20aec3d2020-05-25 09:09:36 +000060 hash_create_mem(0, sizeof(adj_nbr_key_t), sizeof(adj_index_t));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010061 }
62
63 ADJ_NBR_SET_KEY(kv, link_type, nh_addr);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010064
Neale Ranns20aec3d2020-05-25 09:09:36 +000065 hash_set_mem_alloc (&adj_nbr_tables[nh_proto][sw_if_index],
66 &kv, adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010067}
68
69void
Neale Ranns177bbdc2016-11-15 09:46:51 +000070adj_nbr_remove (adj_index_t ai,
71 fib_protocol_t nh_proto,
Neale Ranns924d03a2016-10-19 08:25:46 +010072 vnet_link_t link_type,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073 const ip46_address_t *nh_addr,
74 u32 sw_if_index)
75{
Neale Ranns20aec3d2020-05-25 09:09:36 +000076 adj_nbr_key_t kv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010077
78 if (!ADJ_NBR_ITF_OK(nh_proto, sw_if_index))
79 return;
80
81 ADJ_NBR_SET_KEY(kv, link_type, nh_addr);
82
Neale Ranns20aec3d2020-05-25 09:09:36 +000083 hash_unset_mem_free(&adj_nbr_tables[nh_proto][sw_if_index], &kv);
84
85 if (0 == hash_elts(adj_nbr_tables[nh_proto][sw_if_index]))
86 {
87 hash_free(adj_nbr_tables[nh_proto][sw_if_index]);
88 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010089}
90
Florin Corasf9d05682018-04-26 08:26:52 -070091adj_index_t
Neale Ranns0bfe5d82016-08-25 15:29:12 +010092adj_nbr_find (fib_protocol_t nh_proto,
Neale Ranns924d03a2016-10-19 08:25:46 +010093 vnet_link_t link_type,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010094 const ip46_address_t *nh_addr,
95 u32 sw_if_index)
96{
Neale Ranns20aec3d2020-05-25 09:09:36 +000097 adj_nbr_key_t kv;
98 uword *p;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010099
100 ADJ_NBR_SET_KEY(kv, link_type, nh_addr);
101
102 if (!ADJ_NBR_ITF_OK(nh_proto, sw_if_index))
103 return (ADJ_INDEX_INVALID);
104
Neale Ranns20aec3d2020-05-25 09:09:36 +0000105 p = hash_get_mem(adj_nbr_tables[nh_proto][sw_if_index], &kv);
106
107 if (p)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108 {
Neale Ranns20aec3d2020-05-25 09:09:36 +0000109 return (p[0]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100110 }
Neale Ranns20aec3d2020-05-25 09:09:36 +0000111 return (ADJ_INDEX_INVALID);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100112}
113
Neale Rannsb80c5362016-10-08 13:03:40 +0100114static inline u32
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100115adj_get_nd_node (fib_protocol_t proto)
116{
117 switch (proto) {
118 case FIB_PROTOCOL_IP4:
Neale Rannsb80c5362016-10-08 13:03:40 +0100119 return (ip4_arp_node.index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100120 case FIB_PROTOCOL_IP6:
Neale Rannsb80c5362016-10-08 13:03:40 +0100121 return (ip6_discover_neighbor_node.index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100122 case FIB_PROTOCOL_MPLS:
123 break;
124 }
125 ASSERT(0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100126 return (ip4_arp_node.index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100127}
128
AkshayaNadahalli17f5f602017-03-27 14:47:41 +0530129/**
130 * @brief Check and set feature flags if o/p interface has any o/p features.
131 */
132static void
133adj_nbr_evaluate_feature (adj_index_t ai)
134{
135 ip_adjacency_t *adj;
136 vnet_feature_main_t *fm = &feature_main;
137 i16 feature_count;
138 u8 arc_index;
139 u32 sw_if_index;
140
141 adj = adj_get(ai);
142
143 switch (adj->ia_link)
144 {
145 case VNET_LINK_IP4:
146 arc_index = ip4_main.lookup_main.output_feature_arc_index;
147 break;
148 case VNET_LINK_IP6:
149 arc_index = ip6_main.lookup_main.output_feature_arc_index;
150 break;
151 case VNET_LINK_MPLS:
152 arc_index = mpls_main.output_feature_arc_index;
153 break;
154 default:
155 return;
156 }
157
158 sw_if_index = adj->rewrite_header.sw_if_index;
AkshayaNadahalli98ab0912017-03-27 17:21:05 +0000159 if (vec_len(fm->feature_count_by_sw_if_index[arc_index]) > sw_if_index)
160 {
161 feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
162 if (feature_count > 0)
Neale Ranns4ec36c52020-03-31 09:21:29 -0400163 {
164 vnet_feature_config_main_t *cm;
AkshayaNadahalli17f5f602017-03-27 14:47:41 +0530165
Neale Ranns4ec36c52020-03-31 09:21:29 -0400166 adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES;
167 cm = &fm->feature_config_mains[arc_index];
168
169 adj->ia_cfg_index = vec_elt (cm->config_index_by_sw_if_index,
170 sw_if_index);
171 }
172 }
AkshayaNadahalli17f5f602017-03-27 14:47:41 +0530173 return;
174}
175
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100176static ip_adjacency_t*
177adj_nbr_alloc (fib_protocol_t nh_proto,
Neale Ranns924d03a2016-10-19 08:25:46 +0100178 vnet_link_t link_type,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179 const ip46_address_t *nh_addr,
180 u32 sw_if_index)
181{
182 ip_adjacency_t *adj;
183
184 adj = adj_alloc(nh_proto);
185
186 adj_nbr_insert(nh_proto, link_type, nh_addr,
187 sw_if_index,
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100188 adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100189
190 /*
191 * since we just added the ADJ we have no rewrite string for it,
192 * so its for ARP
193 */
194 adj->lookup_next_index = IP_LOOKUP_NEXT_ARP;
195 adj->sub_type.nbr.next_hop = *nh_addr;
196 adj->ia_link = link_type;
197 adj->ia_nh_proto = nh_proto;
Neale Rannsb80c5362016-10-08 13:03:40 +0100198 adj->rewrite_header.sw_if_index = sw_if_index;
Neale Ranns1bce5a92018-10-30 06:34:25 -0700199 vnet_rewrite_update_mtu(vnet_get_main(), adj->ia_link,
200 &adj->rewrite_header);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100201
AkshayaNadahalli17f5f602017-03-27 14:47:41 +0530202 adj_nbr_evaluate_feature (adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100203 return (adj);
204}
205
206/*
Neale Ranns32e1c012016-11-22 17:07:28 +0000207 * adj_nbr_add_or_lock
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100208 *
209 * Add an adjacency for the neighbour requested.
210 *
211 * The key for an adj is:
212 * - the Next-hops protocol (i.e. v4 or v6)
213 * - the address of the next-hop
214 * - the interface the next-hop is reachable through
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100215 */
216adj_index_t
217adj_nbr_add_or_lock (fib_protocol_t nh_proto,
Neale Ranns924d03a2016-10-19 08:25:46 +0100218 vnet_link_t link_type,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100219 const ip46_address_t *nh_addr,
220 u32 sw_if_index)
221{
222 adj_index_t adj_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100223
224 adj_index = adj_nbr_find(nh_proto, link_type, nh_addr, sw_if_index);
225
226 if (ADJ_INDEX_INVALID == adj_index)
227 {
Neale Ranns77cfc012019-12-15 22:26:37 +0000228 ip_adjacency_t *adj;
Neale Rannsb80c5362016-10-08 13:03:40 +0100229 vnet_main_t *vnm;
230
231 vnm = vnet_get_main();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100232 adj = adj_nbr_alloc(nh_proto, link_type, nh_addr, sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100233 adj_index = adj_get_index(adj);
234 adj_lock(adj_index);
235
Neale Ranns1855b8e2018-07-11 10:31:26 -0700236 if (ip46_address_is_equal(&ADJ_BCAST_ADDR, nh_addr))
237 {
238 adj->lookup_next_index = IP_LOOKUP_NEXT_BCAST;
239 }
240
Ole Troand7231612018-06-07 10:17:57 +0200241 vnet_rewrite_init(vnm, sw_if_index, link_type,
Neale Rannsb80c5362016-10-08 13:03:40 +0100242 adj_get_nd_node(nh_proto),
243 vnet_tx_node_index_for_sw_interface(vnm, sw_if_index),
244 &adj->rewrite_header);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100245
246 /*
Neale Rannsb80c5362016-10-08 13:03:40 +0100247 * we need a rewrite where the destination IP address is converted
248 * to the appropriate link-layer address. This is interface specific.
249 * So ask the interface to do it.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100250 */
Neale Rannsb80c5362016-10-08 13:03:40 +0100251 vnet_update_adjacency_for_sw_interface(vnm, sw_if_index, adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100252 }
253 else
254 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100255 adj_lock(adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100256 }
257
Neale Ranns77cfc012019-12-15 22:26:37 +0000258 adj_delegate_adj_created(adj_get(adj_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100259 return (adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100260}
261
262adj_index_t
263adj_nbr_add_or_lock_w_rewrite (fib_protocol_t nh_proto,
Neale Ranns924d03a2016-10-19 08:25:46 +0100264 vnet_link_t link_type,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100265 const ip46_address_t *nh_addr,
266 u32 sw_if_index,
267 u8 *rewrite)
268{
269 adj_index_t adj_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100270
271 adj_index = adj_nbr_find(nh_proto, link_type, nh_addr, sw_if_index);
272
273 if (ADJ_INDEX_INVALID == adj_index)
274 {
Neale Ranns13a08cc2018-11-07 09:25:54 -0800275 ip_adjacency_t *adj;
276
277 adj = adj_nbr_alloc(nh_proto, link_type, nh_addr, sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100278 adj->rewrite_header.sw_if_index = sw_if_index;
Neale Ranns13a08cc2018-11-07 09:25:54 -0800279 adj_index = adj_get_index(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100280 }
281
Neale Ranns13a08cc2018-11-07 09:25:54 -0800282 adj_lock(adj_index);
283 adj_nbr_update_rewrite(adj_index,
Neale Rannsb80c5362016-10-08 13:03:40 +0100284 ADJ_NBR_REWRITE_FLAG_COMPLETE,
285 rewrite);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100286
Neale Ranns77cfc012019-12-15 22:26:37 +0000287 adj_delegate_adj_created(adj_get(adj_index));
288
Neale Ranns13a08cc2018-11-07 09:25:54 -0800289 return (adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100290}
291
292/**
293 * adj_nbr_update_rewrite
294 *
295 * Update the adjacency's rewrite string. A NULL string implies the
Jim Thompsonf324dec2019-04-08 03:22:21 -0500296 * rewrite is reset (i.e. when ARP/ND entry is gone).
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100297 * NB: the adj being updated may be handling traffic in the DP.
298 */
299void
300adj_nbr_update_rewrite (adj_index_t adj_index,
Neale Rannsb80c5362016-10-08 13:03:40 +0100301 adj_nbr_rewrite_flag_t flags,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100302 u8 *rewrite)
303{
304 ip_adjacency_t *adj;
305
306 ASSERT(ADJ_INDEX_INVALID != adj_index);
307
308 adj = adj_get(adj_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100309
310 if (flags & ADJ_NBR_REWRITE_FLAG_COMPLETE)
311 {
312 /*
313 * update the adj's rewrite string and build the arc
314 * from the rewrite node to the interface's TX node
315 */
316 adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_REWRITE,
317 adj_get_rewrite_node(adj->ia_link),
318 vnet_tx_node_index_for_sw_interface(
319 vnet_get_main(),
320 adj->rewrite_header.sw_if_index),
321 rewrite);
322 }
323 else
324 {
325 adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_ARP,
326 adj_get_nd_node(adj->ia_nh_proto),
327 vnet_tx_node_index_for_sw_interface(
328 vnet_get_main(),
329 adj->rewrite_header.sw_if_index),
330 rewrite);
331 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100332}
333
334/**
335 * adj_nbr_update_rewrite_internal
336 *
337 * Update the adjacency's rewrite string. A NULL string implies the
Jim Thompsonf324dec2019-04-08 03:22:21 -0500338 * rewrite is reset (i.e. when ARP/ND entry is gone).
Neale Rannsb80c5362016-10-08 13:03:40 +0100339 * NB: the adj being updated may be handling traffic in the DP.
340 */
341void
342adj_nbr_update_rewrite_internal (ip_adjacency_t *adj,
Neale Rannsfa5d1982017-02-20 14:19:51 -0800343 ip_lookup_next_t adj_next_index,
Neale Rannsb80c5362016-10-08 13:03:40 +0100344 u32 this_node,
345 u32 next_node,
346 u8 *rewrite)
347{
Neale Ranns19c68d22016-12-07 15:38:14 +0000348 ip_adjacency_t *walk_adj;
Neale Ranns66300f62020-01-12 21:16:55 +0000349 adj_index_t walk_ai, ai;
Neale Rannsad95b5d2016-11-10 20:35:14 +0000350 vlib_main_t * vm;
351 u32 old_next;
Neale Ranns19c68d22016-12-07 15:38:14 +0000352 int do_walk;
Neale Rannsad95b5d2016-11-10 20:35:14 +0000353
354 vm = vlib_get_main();
355 old_next = adj->lookup_next_index;
356
Neale Ranns66300f62020-01-12 21:16:55 +0000357 ai = walk_ai = adj_get_index(adj);
Neale Rannsad95b5d2016-11-10 20:35:14 +0000358 if (VNET_LINK_MPLS == adj->ia_link)
359 {
360 /*
361 * The link type MPLS has no children in the control plane graph, it only
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700362 * has children in the data-plane graph. The backwalk is up the former.
Neale Rannsad95b5d2016-11-10 20:35:14 +0000363 * So we need to walk from its IP cousin.
364 */
365 walk_ai = adj_nbr_find(adj->ia_nh_proto,
366 fib_proto_to_link(adj->ia_nh_proto),
367 &adj->sub_type.nbr.next_hop,
368 adj->rewrite_header.sw_if_index);
369 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100370
371 /*
Neale Ranns19c68d22016-12-07 15:38:14 +0000372 * Don't call the walk re-entrantly
373 */
374 if (ADJ_INDEX_INVALID != walk_ai)
375 {
376 walk_adj = adj_get(walk_ai);
Neale Rannsfa5d1982017-02-20 14:19:51 -0800377 if (ADJ_FLAG_SYNC_WALK_ACTIVE & walk_adj->ia_flags)
Neale Ranns19c68d22016-12-07 15:38:14 +0000378 {
379 do_walk = 0;
380 }
381 else
382 {
383 /*
384 * Prevent re-entrant walk of the same adj
385 */
Neale Rannsfa5d1982017-02-20 14:19:51 -0800386 walk_adj->ia_flags |= ADJ_FLAG_SYNC_WALK_ACTIVE;
Neale Ranns19c68d22016-12-07 15:38:14 +0000387 do_walk = 1;
388 }
389 }
390 else
391 {
392 do_walk = 0;
393 }
394
395 /*
396 * lock the adjacencies that are affected by updates this walk will provoke.
397 * Since the aim of the walk is to update children to link to a different
398 * DPO, this adj will no longer be in use and its lock count will drop to 0.
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700399 * We don't want it to be deleted as part of this endeavour.
Neale Ranns19c68d22016-12-07 15:38:14 +0000400 */
Neale Ranns66300f62020-01-12 21:16:55 +0000401 adj_lock(ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000402 adj_lock(walk_ai);
403
404 /*
Neale Rannsb80c5362016-10-08 13:03:40 +0100405 * Updating a rewrite string is not atomic;
406 * - the rewrite string is too long to write in one instruction
407 * - when swapping from incomplete to complete, we also need to update
Neale Rannsad95b5d2016-11-10 20:35:14 +0000408 * the VLIB graph next-index of the adj.
Neale Rannsb80c5362016-10-08 13:03:40 +0100409 * ideally we would only want to suspend forwarding via this adj whilst we
410 * do this, but we do not have that level of granularity - it's suspend all
411 * worker threads or nothing.
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700412 * The other choices are:
Neale Rannsb80c5362016-10-08 13:03:40 +0100413 * - to mark the adj down and back walk so child load-balances drop this adj
414 * from the set.
415 * - update the next_node index of this adj to point to error-drop
416 * both of which will mean for MAC change we will drop for this adj
Neale Rannsad95b5d2016-11-10 20:35:14 +0000417 * which is not acceptable. However, when the adj changes type (from
418 * complete to incomplete and vice-versa) the child DPOs, which have the
419 * VLIB graph next node index, will be sending packets to the wrong graph
420 * node. So from the options above, updating the next_node of the adj to
421 * be drop will work, but it relies on each graph node v4/v6/mpls, rewrite/
422 * arp/midchain always be valid w.r.t. a mis-match of adj type and node type
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700423 * (i.e. a rewrite adj in the arp node). This is not enforceable. Getting it
Neale Rannsad95b5d2016-11-10 20:35:14 +0000424 * wrong will lead to hard to find bugs since its a race condition. So we
425 * choose the more reliable method of updating the children to use the drop,
426 * then switching adj's type, then updating the children again. Did I mention
427 * that this doesn't happen often...
428 * So we need to distinguish between the two cases:
429 * 1 - mac change
430 * 2 - adj type change
431 */
Neale Ranns19c68d22016-12-07 15:38:14 +0000432 if (do_walk &&
433 old_next != adj_next_index &&
Neale Rannsad95b5d2016-11-10 20:35:14 +0000434 ADJ_INDEX_INVALID != walk_ai)
435 {
436 /*
437 * the adj is changing type. we need to fix all children so that they
438 * stack momentarily on a drop, while the adj changes. If we don't do
439 * this the children will send packets to a VLIB graph node that does
440 * not correspond to the adj's type - and it goes downhill from there.
441 */
442 fib_node_back_walk_ctx_t bw_ctx = {
443 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_DOWN,
444 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700445 * force this walk to be synchronous. if we don't and a node in the graph
Neale Rannsad95b5d2016-11-10 20:35:14 +0000446 * (a heavily shared path-list) chooses to back-ground the walk (make it
447 * async) then it will pause and we will do the adj update below, before
448 * all the children are updated. not good.
449 */
450 .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC,
451 };
452
453 fib_walk_sync(FIB_NODE_TYPE_ADJ, walk_ai, &bw_ctx);
Steven Luong3d5f6222020-01-30 09:11:18 -0800454 /*
455 * fib_walk_sync may allocate a new adjacency and potentially cuase a
456 * realloc for adj_pool. When that happens, adj pointer is no longer
457 * valid here. We refresh the adj pointer accordingly.
458 */
459 adj = adj_get (ai);
Neale Rannsad95b5d2016-11-10 20:35:14 +0000460 }
461
462 /*
463 * If we are just updating the MAC string of the adj (which we also can't
464 * do atomically), then we need to stop packets switching through the adj.
465 * We can't do that on a per-adj basis, so it's all the packets.
466 * If we are updating the type, and we walked back to the children above,
467 * then this barrier serves to flush the queues/frames.
Neale Rannsb80c5362016-10-08 13:03:40 +0100468 */
469 vlib_worker_thread_barrier_sync(vm);
470
471 adj->lookup_next_index = adj_next_index;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000472 adj->ia_node_index = this_node;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100473
474 if (NULL != rewrite)
475 {
476 /*
477 * new rewrite provided.
Neale Rannsb80c5362016-10-08 13:03:40 +0100478 * fill in the adj's rewrite string, and build the VLIB graph arc.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100479 */
Neale Rannsb80c5362016-10-08 13:03:40 +0100480 vnet_rewrite_set_data_internal(&adj->rewrite_header,
481 sizeof(adj->rewrite_data),
482 rewrite,
483 vec_len(rewrite));
Neale Rannsb80c5362016-10-08 13:03:40 +0100484 vec_free(rewrite);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100485 }
486 else
487 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100488 vnet_rewrite_clear_data_internal(&adj->rewrite_header,
489 sizeof(adj->rewrite_data));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100490 }
Neale Rannsad95b5d2016-11-10 20:35:14 +0000491 adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
492 this_node,
493 next_node);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100494
495 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700496 * done with the rewrite update - let the workers loose.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100497 */
Neale Rannsb80c5362016-10-08 13:03:40 +0100498 vlib_worker_thread_barrier_release(vm);
Neale Rannsad95b5d2016-11-10 20:35:14 +0000499
Neale Ranns19c68d22016-12-07 15:38:14 +0000500 if (do_walk &&
501 (old_next != adj->lookup_next_index) &&
502 (ADJ_INDEX_INVALID != walk_ai))
Neale Rannsad95b5d2016-11-10 20:35:14 +0000503 {
504 /*
505 * backwalk to the children so they can stack on the now updated
506 * adjacency
507 */
508 fib_node_back_walk_ctx_t bw_ctx = {
509 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE,
510 };
511
512 fib_walk_sync(FIB_NODE_TYPE_ADJ, walk_ai, &bw_ctx);
513 }
Neale Ranns19c68d22016-12-07 15:38:14 +0000514 /*
515 * Prevent re-entrant walk of the same adj
516 */
517 if (do_walk)
518 {
Neale Ranns37157d52020-01-23 22:46:06 +0000519 walk_adj = adj_get(walk_ai);
Neale Rannsfa5d1982017-02-20 14:19:51 -0800520 walk_adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
Neale Ranns19c68d22016-12-07 15:38:14 +0000521 }
522
Neale Ranns4ec36c52020-03-31 09:21:29 -0400523 adj_delegate_adj_modified(adj);
Neale Ranns66300f62020-01-12 21:16:55 +0000524 adj_unlock(ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000525 adj_unlock(walk_ai);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100526}
527
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100528u32
529adj_nbr_db_size (void)
530{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100531 fib_protocol_t proto;
532 u32 sw_if_index = 0;
Neale Ranns20aec3d2020-05-25 09:09:36 +0000533 u64 count = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100534
535 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
536 {
537 vec_foreach_index(sw_if_index, adj_nbr_tables[proto])
538 {
539 if (NULL != adj_nbr_tables[proto][sw_if_index])
540 {
Neale Ranns20aec3d2020-05-25 09:09:36 +0000541 count += hash_elts(adj_nbr_tables[proto][sw_if_index]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100542 }
543 }
544 }
Neale Ranns20aec3d2020-05-25 09:09:36 +0000545 return (count);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100546}
547
548/**
Neale Ranns20aec3d2020-05-25 09:09:36 +0000549 * @brief Walk all adjacencies on a link for a given next-hop protocol
Neale Rannsb80c5362016-10-08 13:03:40 +0100550 */
Neale Rannsb80c5362016-10-08 13:03:40 +0100551void
552adj_nbr_walk (u32 sw_if_index,
553 fib_protocol_t adj_nh_proto,
554 adj_walk_cb_t cb,
555 void *ctx)
556{
Neale Ranns22391fa2020-05-29 10:19:41 -0400557 adj_index_t ai, *ais, *aip;
Neale Ranns20aec3d2020-05-25 09:09:36 +0000558 adj_nbr_key_t *key;
Neale Ranns20aec3d2020-05-25 09:09:36 +0000559
Neale Rannsb80c5362016-10-08 13:03:40 +0100560 if (!ADJ_NBR_ITF_OK(adj_nh_proto, sw_if_index))
561 return;
562
Neale Ranns22391fa2020-05-29 10:19:41 -0400563 ais = NULL;
564
565 /* elements may be removed from the table during the walk, so
566 * collect the set first then process them */
567 hash_foreach_mem (key, ai, adj_nbr_tables[adj_nh_proto][sw_if_index],
568 ({
569 vec_add1(ais, ai);
570 }));
571
572 vec_foreach(aip, ais)
Neale Ranns20aec3d2020-05-25 09:09:36 +0000573 {
Neale Ranns22391fa2020-05-29 10:19:41 -0400574 /* An adj may be deleted during the walk so check first */
575 if (!pool_is_free_index(adj_pool, *aip))
576 cb(*aip, ctx);
Neale Ranns20aec3d2020-05-25 09:09:36 +0000577 }
Neale Ranns22391fa2020-05-29 10:19:41 -0400578 vec_free(ais);
Neale Rannsb80c5362016-10-08 13:03:40 +0100579}
580
581/**
Neale Rannsb80c5362016-10-08 13:03:40 +0100582 * @brief Walk adjacencies on a link with a given v4 next-hop.
583 * that is visit the adjacencies with different link types.
584 */
585void
586adj_nbr_walk_nh4 (u32 sw_if_index,
587 const ip4_address_t *addr,
588 adj_walk_cb_t cb,
589 void *ctx)
590{
591 if (!ADJ_NBR_ITF_OK(FIB_PROTOCOL_IP4, sw_if_index))
592 return;
593
594 ip46_address_t nh = {
595 .ip4 = *addr,
596 };
Neale Ranns580bba72018-04-23 05:31:19 -0700597 vnet_link_t linkt;
598 adj_index_t ai;
Neale Rannsb80c5362016-10-08 13:03:40 +0100599
Neale Ranns580bba72018-04-23 05:31:19 -0700600 FOR_EACH_VNET_LINK(linkt)
601 {
602 ai = adj_nbr_find (FIB_PROTOCOL_IP4, linkt, &nh, sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100603
Neale Ranns580bba72018-04-23 05:31:19 -0700604 if (INDEX_INVALID != ai)
605 cb(ai, ctx);
606 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100607}
608
609/**
610 * @brief Walk adjacencies on a link with a given v6 next-hop.
611 * that is visit the adjacencies with different link types.
612 */
613void
614adj_nbr_walk_nh6 (u32 sw_if_index,
615 const ip6_address_t *addr,
616 adj_walk_cb_t cb,
617 void *ctx)
618{
619 if (!ADJ_NBR_ITF_OK(FIB_PROTOCOL_IP6, sw_if_index))
620 return;
621
622 ip46_address_t nh = {
623 .ip6 = *addr,
624 };
Neale Ranns580bba72018-04-23 05:31:19 -0700625 vnet_link_t linkt;
626 adj_index_t ai;
Neale Rannsb80c5362016-10-08 13:03:40 +0100627
Neale Ranns580bba72018-04-23 05:31:19 -0700628 FOR_EACH_VNET_LINK(linkt)
629 {
630 ai = adj_nbr_find (FIB_PROTOCOL_IP6, linkt, &nh, sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100631
Neale Ranns580bba72018-04-23 05:31:19 -0700632 if (INDEX_INVALID != ai)
633 cb(ai, ctx);
634 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100635}
636
637/**
638 * @brief Walk adjacencies on a link with a given next-hop.
639 * that is visit the adjacencies with different link types.
640 */
641void
642adj_nbr_walk_nh (u32 sw_if_index,
643 fib_protocol_t adj_nh_proto,
644 const ip46_address_t *nh,
645 adj_walk_cb_t cb,
646 void *ctx)
647{
648 if (!ADJ_NBR_ITF_OK(adj_nh_proto, sw_if_index))
649 return;
650
Neale Rannscbe25aa2019-09-30 10:53:31 +0000651 switch (adj_nh_proto)
Neale Ranns580bba72018-04-23 05:31:19 -0700652 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000653 case FIB_PROTOCOL_IP4:
654 adj_nbr_walk_nh4(sw_if_index, &nh->ip4, cb, ctx);
655 break;
656 case FIB_PROTOCOL_IP6:
657 adj_nbr_walk_nh6(sw_if_index, &nh->ip6, cb, ctx);
658 break;
659 case FIB_PROTOCOL_MPLS:
660 ASSERT(0);
661 break;
Neale Ranns580bba72018-04-23 05:31:19 -0700662 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100663}
664
665/**
Neale Ranns8b37b872016-11-21 12:25:22 +0000666 * Flags associated with the interface state walks
667 */
668typedef enum adj_nbr_interface_flags_t_
669{
670 ADJ_NBR_INTERFACE_UP = (1 << 0),
671} adj_nbr_interface_flags_t;
672
673/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100674 * Context for the state change walk of the DB
675 */
676typedef struct adj_nbr_interface_state_change_ctx_t_
677{
678 /**
Neale Ranns8b37b872016-11-21 12:25:22 +0000679 * Flags on the interface
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100680 */
Neale Ranns8b37b872016-11-21 12:25:22 +0000681 adj_nbr_interface_flags_t flags;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100682} adj_nbr_interface_state_change_ctx_t;
683
Neale Rannsb80c5362016-10-08 13:03:40 +0100684static adj_walk_rc_t
685adj_nbr_interface_state_change_one (adj_index_t ai,
Neale Ranns8b37b872016-11-21 12:25:22 +0000686 void *arg)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100687{
688 /*
689 * Back walk the graph to inform the forwarding entries
Neale Ranns8b37b872016-11-21 12:25:22 +0000690 * that this interface state has changed. Do this synchronously
691 * since this is the walk that provides convergence
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100692 */
693 adj_nbr_interface_state_change_ctx_t *ctx = arg;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100694 fib_node_back_walk_ctx_t bw_ctx = {
Neale Ranns8b37b872016-11-21 12:25:22 +0000695 .fnbw_reason = ((ctx->flags & ADJ_NBR_INTERFACE_UP) ?
696 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
697 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
698 /*
699 * the force sync applies only as far as the first fib_entry.
700 * And it's the fib_entry's we need to converge away from
701 * the adjacencies on the now down link
702 */
703 .fnbw_flags = (!(ctx->flags & ADJ_NBR_INTERFACE_UP) ?
704 FIB_NODE_BW_FLAG_FORCE_SYNC :
Neale Ranns30d53642018-08-27 07:29:15 -0700705 FIB_NODE_BW_FLAG_NONE),
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100706 };
Neale Ranns30d53642018-08-27 07:29:15 -0700707 ip_adjacency_t *adj;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100708
Neale Ranns30d53642018-08-27 07:29:15 -0700709 adj = adj_get(ai);
710
711 adj->ia_flags |= ADJ_FLAG_SYNC_WALK_ACTIVE;
Neale Rannsb80c5362016-10-08 13:03:40 +0100712 fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx);
Neale Ranns30d53642018-08-27 07:29:15 -0700713 adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
Neale Rannsb80c5362016-10-08 13:03:40 +0100714
715 return (ADJ_WALK_RC_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100716}
717
Neale Ranns8b37b872016-11-21 12:25:22 +0000718/**
719 * @brief Registered function for SW interface state changes
720 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100721static clib_error_t *
Neale Ranns8b37b872016-11-21 12:25:22 +0000722adj_nbr_sw_interface_state_change (vnet_main_t * vnm,
723 u32 sw_if_index,
724 u32 flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100725{
726 fib_protocol_t proto;
727
728 /*
729 * walk each adj on the interface and trigger a walk from that adj
730 */
731 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
732 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100733 adj_nbr_interface_state_change_ctx_t ctx = {
Neale Ranns8b37b872016-11-21 12:25:22 +0000734 .flags = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
735 ADJ_NBR_INTERFACE_UP :
736 0),
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100737 };
738
Neale Rannsb80c5362016-10-08 13:03:40 +0100739 adj_nbr_walk(sw_if_index, proto,
740 adj_nbr_interface_state_change_one,
741 &ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100742 }
743
744 return (NULL);
745}
746
Neale Ranns8b37b872016-11-21 12:25:22 +0000747VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION_PRIO(
748 adj_nbr_sw_interface_state_change,
749 VNET_ITF_FUNC_PRIORITY_HIGH);
750
751/**
752 * @brief Invoked on each SW interface of a HW interface when the
753 * HW interface state changes
754 */
Neale Ranns0053de62018-05-22 08:40:52 -0700755static walk_rc_t
Neale Ranns8b37b872016-11-21 12:25:22 +0000756adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm,
757 u32 sw_if_index,
758 void *arg)
759{
760 adj_nbr_interface_state_change_ctx_t *ctx = arg;
761 fib_protocol_t proto;
762
763 /*
764 * walk each adj on the interface and trigger a walk from that adj
765 */
766 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
767 {
768 adj_nbr_walk(sw_if_index, proto,
769 adj_nbr_interface_state_change_one,
770 ctx);
771 }
Neale Ranns0053de62018-05-22 08:40:52 -0700772 return (WALK_CONTINUE);
Neale Ranns8b37b872016-11-21 12:25:22 +0000773}
774
775/**
776 * @brief Registered callback for HW interface state changes
777 */
778static clib_error_t *
779adj_nbr_hw_interface_state_change (vnet_main_t * vnm,
780 u32 hw_if_index,
781 u32 flags)
782{
783 /*
784 * walk SW interface on the HW
785 */
786 adj_nbr_interface_state_change_ctx_t ctx = {
787 .flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
788 ADJ_NBR_INTERFACE_UP :
789 0),
790 };
791
792 vnet_hw_interface_walk_sw(vnm, hw_if_index,
793 adj_nbr_hw_sw_interface_state_change,
794 &ctx);
795
796 return (NULL);
797}
798
799VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION_PRIO(
800 adj_nbr_hw_interface_state_change,
801 VNET_ITF_FUNC_PRIORITY_HIGH);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100802
Neale Rannsb80c5362016-10-08 13:03:40 +0100803static adj_walk_rc_t
804adj_nbr_interface_delete_one (adj_index_t ai,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100805 void *arg)
806{
807 /*
808 * Back walk the graph to inform the forwarding entries
809 * that this interface has been deleted.
810 */
811 fib_node_back_walk_ctx_t bw_ctx = {
812 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
813 };
Neale Ranns30d53642018-08-27 07:29:15 -0700814 ip_adjacency_t *adj;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100815
Benoît Ganne73911562019-10-16 15:08:37 +0200816 adj_lock(ai);
817
Neale Ranns30d53642018-08-27 07:29:15 -0700818 adj = adj_get(ai);
819
820 adj->ia_flags |= ADJ_FLAG_SYNC_WALK_ACTIVE;
Neale Rannsb80c5362016-10-08 13:03:40 +0100821 fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx);
Neale Ranns30d53642018-08-27 07:29:15 -0700822 adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
Neale Rannsb80c5362016-10-08 13:03:40 +0100823
Benoît Ganne73911562019-10-16 15:08:37 +0200824 adj_unlock(ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100825 return (ADJ_WALK_RC_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100826}
827
828/**
829 * adj_nbr_interface_add_del
830 *
831 * Registered to receive interface Add and delete notifications
832 */
833static clib_error_t *
834adj_nbr_interface_add_del (vnet_main_t * vnm,
835 u32 sw_if_index,
836 u32 is_add)
837{
838 fib_protocol_t proto;
839
840 if (is_add)
841 {
842 /*
843 * not interested in interface additions. we will not back walk
844 * to resolve paths through newly added interfaces. Why? The control
845 * plane should have the brains to add interfaces first, then routes.
846 * So the case where there are paths with a interface that matches
847 * one just created is the case where the path resolved through an
848 * interface that was deleted, and still has not been removed. The
849 * new interface added, is NO GUARANTEE that the interface being
850 * added now, even though it may have the same sw_if_index, is the
851 * same interface that the path needs. So tough!
852 * If the control plane wants these routes to resolve it needs to
853 * remove and add them again.
854 */
855 return (NULL);
856 }
857
858 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
859 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100860 adj_nbr_walk(sw_if_index, proto,
861 adj_nbr_interface_delete_one,
862 NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100863 }
864
865 return (NULL);
866
867}
868
869VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_nbr_interface_add_del);
870
871
Neale Rannsb80c5362016-10-08 13:03:40 +0100872static adj_walk_rc_t
873adj_nbr_show_one (adj_index_t ai,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100874 void *arg)
875{
876 vlib_cli_output (arg, "[@%d] %U",
Neale Rannsb80c5362016-10-08 13:03:40 +0100877 ai,
878 format_ip_adjacency, ai,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100879 FORMAT_IP_ADJACENCY_NONE);
Neale Rannsb80c5362016-10-08 13:03:40 +0100880
881 return (ADJ_WALK_RC_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100882}
883
884static clib_error_t *
885adj_nbr_show (vlib_main_t * vm,
886 unformat_input_t * input,
887 vlib_cli_command_t * cmd)
888{
889 adj_index_t ai = ADJ_INDEX_INVALID;
Neale Ranns14053c92019-12-29 23:55:18 +0000890 ip46_address_t nh = ip46_address_initializer;
Neale Rannsb80c5362016-10-08 13:03:40 +0100891 u32 sw_if_index = ~0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100892
893 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
894 {
Neale Ranns14053c92019-12-29 23:55:18 +0000895 if (unformat (input, "%U",
896 unformat_vnet_sw_interface, vnet_get_main(),
897 &sw_if_index))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100898 ;
Neale Rannsb80c5362016-10-08 13:03:40 +0100899 else if (unformat (input, "%U",
Neale Ranns14053c92019-12-29 23:55:18 +0000900 unformat_ip46_address, &nh, IP46_TYPE_ANY))
901 ;
902 else if (unformat (input, "%d", &ai))
Neale Rannsb80c5362016-10-08 13:03:40 +0100903 ;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100904 else
905 break;
906 }
907
908 if (ADJ_INDEX_INVALID != ai)
909 {
910 vlib_cli_output (vm, "[@%d] %U",
911 ai,
Neale Rannsb80c5362016-10-08 13:03:40 +0100912 format_ip_adjacency, ai,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100913 FORMAT_IP_ADJACENCY_DETAIL);
914 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100915 else if (~0 != sw_if_index)
916 {
917 fib_protocol_t proto;
918
Neale Ranns14053c92019-12-29 23:55:18 +0000919 if (ip46_address_is_zero(&nh))
920 {
921 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
922 {
923 adj_nbr_walk(sw_if_index, proto,
924 adj_nbr_show_one,
925 vm);
926 }
927 }
928 else
929 {
930 proto = (ip46_address_is_ip4(&nh) ?
931 FIB_PROTOCOL_IP4 :
932 FIB_PROTOCOL_IP6);
933 adj_nbr_walk_nh(sw_if_index, proto, &nh,
934 adj_nbr_show_one,
935 vm);
936 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100937 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100938 else
939 {
940 fib_protocol_t proto;
941
942 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
943 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100944 vec_foreach_index(sw_if_index, adj_nbr_tables[proto])
945 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100946 adj_nbr_walk(sw_if_index, proto,
947 adj_nbr_show_one,
948 vm);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100949 }
950 }
951 }
952
953 return 0;
954}
955
Neale Rannsb80c5362016-10-08 13:03:40 +0100956/*?
957 * Show all neighbour adjacencies.
958 * @cliexpar
959 * @cliexstart{sh adj nbr}
960 * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
961 * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
962 * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
963 * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
964 * @cliexend
965 ?*/
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100966VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
967 .path = "show adj nbr",
Neale Rannsb80c5362016-10-08 13:03:40 +0100968 .short_help = "show adj nbr [<adj_index>] [interface]",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100969 .function = adj_nbr_show,
970};
971
972u8*
973format_adj_nbr_incomplete (u8* s, va_list *ap)
974{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400975 index_t index = va_arg(*ap, index_t);
976 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100977 vnet_main_t * vnm = vnet_get_main();
978 ip_adjacency_t * adj = adj_get(index);
979
Neale Ranns924d03a2016-10-19 08:25:46 +0100980 s = format (s, "arp-%U", format_vnet_link, adj->ia_link);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100981 s = format (s, ": via %U",
Neale Rannsb80c5362016-10-08 13:03:40 +0100982 format_ip46_address, &adj->sub_type.nbr.next_hop,
983 adj_proto_to_46(adj->ia_nh_proto));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100984 s = format (s, " %U",
Steven70488ab2018-03-28 17:59:00 -0700985 format_vnet_sw_if_index_name,
986 vnm, adj->rewrite_header.sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100987
988 return (s);
989}
990
991u8*
992format_adj_nbr (u8* s, va_list *ap)
993{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400994 index_t index = va_arg(*ap, index_t);
995 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100996 ip_adjacency_t * adj = adj_get(index);
997
Neale Ranns924d03a2016-10-19 08:25:46 +0100998 s = format (s, "%U", format_vnet_link, adj->ia_link);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100999 s = format (s, " via %U ",
Neale Rannsb80c5362016-10-08 13:03:40 +01001000 format_ip46_address, &adj->sub_type.nbr.next_hop,
1001 adj_proto_to_46(adj->ia_nh_proto));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001002 s = format (s, "%U",
1003 format_vnet_rewrite,
Neale Rannsb069a692017-03-15 12:34:25 -04001004 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001005
1006 return (s);
1007}
1008
1009static void
1010adj_dpo_lock (dpo_id_t *dpo)
1011{
1012 adj_lock(dpo->dpoi_index);
1013}
1014static void
1015adj_dpo_unlock (dpo_id_t *dpo)
1016{
1017 adj_unlock(dpo->dpoi_index);
1018}
1019
Neale Ranns6c3ebcc2016-10-02 21:20:15 +01001020static void
1021adj_mem_show (void)
1022{
1023 fib_show_memory_usage("Adjacency",
1024 pool_elts(adj_pool),
1025 pool_len(adj_pool),
1026 sizeof(ip_adjacency_t));
1027}
1028
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001029const static dpo_vft_t adj_nbr_dpo_vft = {
1030 .dv_lock = adj_dpo_lock,
1031 .dv_unlock = adj_dpo_unlock,
1032 .dv_format = format_adj_nbr,
Neale Ranns6c3ebcc2016-10-02 21:20:15 +01001033 .dv_mem_show = adj_mem_show,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001034 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001035};
1036const static dpo_vft_t adj_nbr_incompl_dpo_vft = {
1037 .dv_lock = adj_dpo_lock,
1038 .dv_unlock = adj_dpo_unlock,
1039 .dv_format = format_adj_nbr_incomplete,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001040 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001041};
1042
1043/**
1044 * @brief The per-protocol VLIB graph nodes that are assigned to an adjacency
1045 * object.
1046 *
1047 * this means that these graph nodes are ones from which a nbr is the
1048 * parent object in the DPO-graph.
1049 */
1050const static char* const nbr_ip4_nodes[] =
1051{
Neale Rannsf06aea52016-11-29 06:51:37 -08001052 "ip4-rewrite",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001053 NULL,
1054};
1055const static char* const nbr_ip6_nodes[] =
1056{
1057 "ip6-rewrite",
1058 NULL,
1059};
1060const static char* const nbr_mpls_nodes[] =
1061{
1062 "mpls-output",
1063 NULL,
1064};
Neale Ranns5e575b12016-10-03 09:40:25 +01001065const static char* const nbr_ethernet_nodes[] =
1066{
1067 "adj-l2-rewrite",
1068 NULL,
1069};
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001070const static char* const * const nbr_nodes[DPO_PROTO_NUM] =
1071{
1072 [DPO_PROTO_IP4] = nbr_ip4_nodes,
1073 [DPO_PROTO_IP6] = nbr_ip6_nodes,
1074 [DPO_PROTO_MPLS] = nbr_mpls_nodes,
Neale Ranns5e575b12016-10-03 09:40:25 +01001075 [DPO_PROTO_ETHERNET] = nbr_ethernet_nodes,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001076};
1077
1078const static char* const nbr_incomplete_ip4_nodes[] =
1079{
1080 "ip4-arp",
1081 NULL,
1082};
1083const static char* const nbr_incomplete_ip6_nodes[] =
1084{
1085 "ip6-discover-neighbor",
1086 NULL,
1087};
1088const static char* const nbr_incomplete_mpls_nodes[] =
1089{
1090 "mpls-adj-incomplete",
1091 NULL,
1092};
1093
1094const static char* const * const nbr_incomplete_nodes[DPO_PROTO_NUM] =
1095{
1096 [DPO_PROTO_IP4] = nbr_incomplete_ip4_nodes,
1097 [DPO_PROTO_IP6] = nbr_incomplete_ip6_nodes,
1098 [DPO_PROTO_MPLS] = nbr_incomplete_mpls_nodes,
1099};
1100
1101void
1102adj_nbr_module_init (void)
1103{
1104 dpo_register(DPO_ADJACENCY,
1105 &adj_nbr_dpo_vft,
1106 nbr_nodes);
1107 dpo_register(DPO_ADJACENCY_INCOMPLETE,
1108 &adj_nbr_incompl_dpo_vft,
1109 nbr_incomplete_nodes);
1110}