2 * src/vnet/ip/ip_neighboor.c: ip neighbor generic handling
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vppinfra/llist.h>
20 #include <vnet/ip-neighbor/ip_neighbor.h>
21 #include <vnet/ip-neighbor/ip4_neighbor.h>
22 #include <vnet/ip-neighbor/ip6_neighbor.h>
23 #include <vnet/ip-neighbor/ip_neighbor_watch.h>
25 #include <vnet/ip/ip6_ll_table.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/adj/adj_mcast.h>
29 /** Pool for All IP neighbors */
30 static ip_neighbor_t *ip_neighbor_pool;
32 /** protocol specific lists of time sorted neighbors */
33 index_t ip_neighbor_list_head[IP46_N_TYPES];
35 typedef struct ip_neighbor_elt_t_
37 clib_llist_anchor_t ipne_anchor;
41 /** Pool of linked list elemeents */
42 ip_neighbor_elt_t *ip_neighbor_elt_pool;
44 typedef struct ip_neighbor_db_t_
46 /** per interface hash */
48 /** per-protocol limit - max number of neighbors*/
50 /** max age of a neighbor before it's forcibly evicted */
52 /** when the limit is reached and new neighbors are created, should
53 * we recycle an old one */
55 /** per-protocol number of elements */
57 /** per-protocol number of elements per-fib-index*/
58 u32 *ipndb_n_elts_per_fib;
61 static vlib_log_class_t ipn_logger;
63 /* DBs of neighbours one per AF */
65 static ip_neighbor_db_t ip_neighbor_db[IP46_N_TYPES] = {
68 /* Default to not aging and not recycling */
70 .ipndb_recycle = false,
74 /* Default to not aging and not recycling */
76 .ipndb_recycle = false,
81 #define IP_NEIGHBOR_DBG(...) \
82 vlib_log_debug (ipn_logger, __VA_ARGS__);
84 #define IP_NEIGHBOR_INFO(...) \
85 vlib_log_notice (ipn_logger, __VA_ARGS__);
88 ip_neighbor_get (index_t ipni)
90 if (pool_is_free_index (ip_neighbor_pool, ipni))
93 return (pool_elt_at_index (ip_neighbor_pool, ipni));
97 ip_neighbor_get_index (const ip_neighbor_t * ipn)
99 return (ipn - ip_neighbor_pool);
103 ip_neighbor_touch (ip_neighbor_t * ipn)
105 ipn->ipn_flags &= ~IP_NEIGHBOR_FLAG_STALE;
109 ip_neighbor_is_dynamic (const ip_neighbor_t * ipn)
111 return (ipn->ipn_flags & IP_NEIGHBOR_FLAG_DYNAMIC);
114 const ip46_address_t *
115 ip_neighbor_get_ip (const ip_neighbor_t * ipn)
117 return (&ipn->ipn_key->ipnk_ip);
120 const mac_address_t *
121 ip_neighbor_get_mac (const ip_neighbor_t * ipn)
123 return (&ipn->ipn_mac);
127 ip_neighbor_get_sw_if_index (const ip_neighbor_t * ipn)
129 return (ipn->ipn_key->ipnk_sw_if_index);
133 ip_neighbor_list_remove (ip_neighbor_t * ipn)
135 /* new neighbours, are added to the head of the list, since the
136 * list is time sorted, newest first */
137 ip_neighbor_elt_t *elt;
139 if (~0 != ipn->ipn_elt)
141 elt = pool_elt_at_index (ip_neighbor_elt_pool, ipn->ipn_elt);
143 clib_llist_remove (ip_neighbor_elt_pool, ipne_anchor, elt);
148 ip_neighbor_refresh (ip_neighbor_t * ipn)
150 /* new neighbours, are added to the head of the list, since the
151 * list is time sorted, newest first */
152 ip_neighbor_elt_t *elt, *head;
154 ip_neighbor_touch (ipn);
155 ipn->ipn_time_last_updated = vlib_time_now (vlib_get_main ());
156 ipn->ipn_n_probes = 0;
158 if (ip_neighbor_is_dynamic (ipn))
160 if (~0 == ipn->ipn_elt)
161 /* first time insertion */
162 pool_get_zero (ip_neighbor_elt_pool, elt);
165 /* already inserted - extract first */
166 elt = pool_elt_at_index (ip_neighbor_elt_pool, ipn->ipn_elt);
168 clib_llist_remove (ip_neighbor_elt_pool, ipne_anchor, elt);
170 head = pool_elt_at_index (ip_neighbor_elt_pool,
171 ip_neighbor_list_head[ipn->
172 ipn_key->ipnk_type]);
174 elt->ipne_index = ip_neighbor_get_index (ipn);
175 clib_llist_add (ip_neighbor_elt_pool, ipne_anchor, elt, head);
176 ipn->ipn_elt = elt - ip_neighbor_elt_pool;
181 ip_neighbor_db_add (const ip_neighbor_t * ipn)
183 vec_validate (ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_hash,
184 ipn->ipn_key->ipnk_sw_if_index);
186 if (!ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_hash
187 [ipn->ipn_key->ipnk_sw_if_index])
188 ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_hash[ipn->
189 ipn_key->ipnk_sw_if_index]
190 = hash_create_mem (0, sizeof (ip_neighbor_key_t), sizeof (index_t));
192 hash_set_mem (ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_hash
193 [ipn->ipn_key->ipnk_sw_if_index], ipn->ipn_key,
194 ip_neighbor_get_index (ipn));
196 ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_n_elts++;
200 ip_neighbor_db_remove (const ip_neighbor_key_t * key)
202 vec_validate (ip_neighbor_db[key->ipnk_type].ipndb_hash,
203 key->ipnk_sw_if_index);
205 hash_unset_mem (ip_neighbor_db[key->ipnk_type].ipndb_hash
206 [key->ipnk_sw_if_index], key);
208 ip_neighbor_db[key->ipnk_type].ipndb_n_elts--;
211 static ip_neighbor_t *
212 ip_neighbor_db_find (const ip_neighbor_key_t * key)
216 if (key->ipnk_sw_if_index >=
217 vec_len (ip_neighbor_db[key->ipnk_type].ipndb_hash))
221 hash_get_mem (ip_neighbor_db[key->ipnk_type].ipndb_hash
222 [key->ipnk_sw_if_index], key);
225 return ip_neighbor_get (p[0]);
231 ip46_type_pfx_len (ip46_type_t type)
233 return (type == IP46_TYPE_IP4 ? 32 : 128);
237 ip_neighbor_adj_fib_add (ip_neighbor_t * ipn, u32 fib_index)
239 if (ipn->ipn_key->ipnk_type == IP46_TYPE_IP6 &&
240 ip6_address_is_link_local_unicast (&ipn->ipn_key->ipnk_ip.ip6))
242 ip6_ll_prefix_t pfx = {
243 .ilp_addr = ipn->ipn_key->ipnk_ip.ip6,
244 .ilp_sw_if_index = ipn->ipn_key->ipnk_sw_if_index,
246 ipn->ipn_fib_entry_index =
247 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
251 fib_protocol_t fproto;
253 fproto = fib_proto_from_ip46 (ipn->ipn_key->ipnk_type);
256 .fp_len = ip46_type_pfx_len (ipn->ipn_key->ipnk_type),
258 .fp_addr = ipn->ipn_key->ipnk_ip,
261 ipn->ipn_fib_entry_index =
262 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
263 FIB_ENTRY_FLAG_ATTACHED,
264 fib_proto_to_dpo (fproto),
266 ipn->ipn_key->ipnk_sw_if_index,
267 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
269 vec_validate (ip_neighbor_db
270 [ipn->ipn_key->ipnk_type].ipndb_n_elts_per_fib,
273 ip_neighbor_db[ipn->ipn_key->
274 ipnk_type].ipndb_n_elts_per_fib[fib_index]++;
277 ip_neighbor_db[ipn->ipn_key->
278 ipnk_type].ipndb_n_elts_per_fib[fib_index])
279 fib_table_lock (fib_index, fproto, FIB_SOURCE_ADJ);
284 ip_neighbor_adj_fib_remove (ip_neighbor_t * ipn, u32 fib_index)
286 if (FIB_NODE_INDEX_INVALID != ipn->ipn_fib_entry_index)
288 if (ipn->ipn_key->ipnk_type == IP46_TYPE_IP6 &&
289 ip6_address_is_link_local_unicast (&ipn->ipn_key->ipnk_ip.ip6))
291 ip6_ll_prefix_t pfx = {
292 .ilp_addr = ipn->ipn_key->ipnk_ip.ip6,
293 .ilp_sw_if_index = ipn->ipn_key->ipnk_sw_if_index,
295 ip6_ll_table_entry_delete (&pfx);
299 fib_protocol_t fproto;
301 fproto = fib_proto_from_ip46 (ipn->ipn_key->ipnk_type);
304 .fp_len = ip46_type_pfx_len (ipn->ipn_key->ipnk_type),
306 .fp_addr = ipn->ipn_key->ipnk_ip,
309 fib_table_entry_path_remove (fib_index,
312 fib_proto_to_dpo (fproto),
314 ipn->ipn_key->ipnk_sw_if_index,
315 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
317 ip_neighbor_db[ipn->ipn_key->
318 ipnk_type].ipndb_n_elts_per_fib[fib_index]--;
321 ip_neighbor_db[ipn->ipn_key->
322 ipnk_type].ipndb_n_elts_per_fib[fib_index])
323 fib_table_unlock (fib_index, fproto, FIB_SOURCE_ADJ);
329 ip_neighbor_mk_complete (adj_index_t ai, ip_neighbor_t * ipn)
331 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
332 ethernet_build_rewrite (vnet_get_main (),
334 ipn_key->ipnk_sw_if_index,
335 adj_get_link_type (ai),
336 ipn->ipn_mac.bytes));
340 ip_neighbor_mk_incomplete (adj_index_t ai)
342 ip_adjacency_t *adj = adj_get (ai);
344 adj_nbr_update_rewrite (ai,
345 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
346 ethernet_build_rewrite (vnet_get_main (),
348 rewrite_header.sw_if_index,
350 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
354 ip_neighbor_mk_complete_walk (adj_index_t ai, void *ctx)
356 ip_neighbor_t *ipn = ctx;
358 ip_neighbor_mk_complete (ai, ipn);
360 return (ADJ_WALK_RC_CONTINUE);
364 ip_neighbor_mk_incomplete_walk (adj_index_t ai, void *ctx)
366 ip_neighbor_mk_incomplete (ai);
368 return (ADJ_WALK_RC_CONTINUE);
372 ip_neighbor_free (ip_neighbor_t * ipn)
374 IP_NEIGHBOR_DBG ("free: %U", format_ip_neighbor,
375 ip_neighbor_get_index (ipn));
377 adj_nbr_walk_nh (ipn->ipn_key->ipnk_sw_if_index,
378 fib_proto_from_ip46 (ipn->ipn_key->ipnk_type),
379 &ipn->ipn_key->ipnk_ip,
380 ip_neighbor_mk_incomplete_walk, ipn);
381 ip_neighbor_adj_fib_remove
383 fib_table_get_index_for_sw_if_index
384 (fib_proto_from_ip46 (ipn->ipn_key->ipnk_type),
385 ipn->ipn_key->ipnk_sw_if_index));
387 ip_neighbor_list_remove (ipn);
388 ip_neighbor_db_remove (ipn->ipn_key);
389 clib_mem_free (ipn->ipn_key);
391 pool_put (ip_neighbor_pool, ipn);
395 ip_neighbor_force_reuse (ip46_type_t type)
397 if (!ip_neighbor_db[type].ipndb_recycle)
400 /* pluck the oldest entry, which is the one from the end of the list */
401 ip_neighbor_elt_t *elt, *head;
404 pool_elt_at_index (ip_neighbor_elt_pool, ip_neighbor_list_head[type]);
406 if (clib_llist_is_empty (ip_neighbor_elt_pool, ipne_anchor, head))
409 elt = clib_llist_prev (ip_neighbor_elt_pool, ipne_anchor, head);
410 ip_neighbor_free (ip_neighbor_get (elt->ipne_index));
415 static ip_neighbor_t *
416 ip_neighbor_alloc (const ip_neighbor_key_t * key,
417 const mac_address_t * mac, ip_neighbor_flags_t flags)
421 if (ip_neighbor_db[key->ipnk_type].ipndb_limit &&
422 (ip_neighbor_db[key->ipnk_type].ipndb_n_elts >=
423 ip_neighbor_db[key->ipnk_type].ipndb_limit))
425 if (!ip_neighbor_force_reuse (key->ipnk_type))
429 pool_get_zero (ip_neighbor_pool, ipn);
431 ipn->ipn_key = clib_mem_alloc (sizeof (*ipn->ipn_key));
432 clib_memcpy (ipn->ipn_key, key, sizeof (*ipn->ipn_key));
434 ipn->ipn_fib_entry_index = FIB_NODE_INDEX_INVALID;
435 ipn->ipn_flags = flags;
438 mac_address_copy (&ipn->ipn_mac, mac);
440 ip_neighbor_db_add (ipn);
442 /* create the adj-fib. the entry in the FIB table for the peer's interface */
443 if (!(ipn->ipn_flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY))
444 ip_neighbor_adj_fib_add
445 (ipn, fib_table_get_index_for_sw_if_index
446 (fib_proto_from_ip46 (ipn->ipn_key->ipnk_type),
447 ipn->ipn_key->ipnk_sw_if_index));
453 ip_neighbor_add (const ip46_address_t * ip,
455 const mac_address_t * mac,
457 ip_neighbor_flags_t flags, u32 * stats_index)
459 fib_protocol_t fproto;
462 /* main thread only */
463 ASSERT (0 == vlib_get_thread_index ());
465 fproto = fib_proto_from_ip46 (type);
467 const ip_neighbor_key_t key = {
469 .ipnk_sw_if_index = sw_if_index,
473 ipn = ip_neighbor_db_find (&key);
477 IP_NEIGHBOR_DBG ("update: %U, %U",
478 format_vnet_sw_if_index_name, vnet_get_main (),
479 sw_if_index, format_ip46_address, ip, type,
480 format_ip_neighbor_flags, flags, format_mac_address_t,
483 ip_neighbor_touch (ipn);
485 /* Refuse to over-write static neighbor entry. */
486 if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
487 (ipn->ipn_flags & IP_NEIGHBOR_FLAG_STATIC))
489 /* if MAC address match, still check to send event */
490 if (0 == mac_address_cmp (&ipn->ipn_mac, mac))
491 goto check_customers;
496 * prevent a DoS attack from the data-plane that
497 * spams us with no-op updates to the MAC address
499 if (0 == mac_address_cmp (&ipn->ipn_mac, mac))
501 ip_neighbor_refresh (ipn);
502 goto check_customers;
505 mac_address_copy (&ipn->ipn_mac, mac);
507 /* A dynamic entry can become static, but not vice-versa.
508 * i.e. since if it was programmed by the CP then it must
509 * be removed by the CP */
510 if ((flags & IP_NEIGHBOR_FLAG_STATIC) &&
511 !(ipn->ipn_flags & IP_NEIGHBOR_FLAG_STATIC))
513 ip_neighbor_list_remove (ipn);
514 ipn->ipn_flags |= IP_NEIGHBOR_FLAG_STATIC;
515 ipn->ipn_flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC;
520 IP_NEIGHBOR_INFO ("add: %U, %U",
521 format_vnet_sw_if_index_name, vnet_get_main (),
522 sw_if_index, format_ip46_address, ip, type,
523 format_ip_neighbor_flags, flags, format_mac_address_t,
526 ipn = ip_neighbor_alloc (&key, mac, flags);
529 return VNET_API_ERROR_LIMIT_EXCEEDED;
532 /* Update time stamp and flags. */
533 ip_neighbor_refresh (ipn);
535 adj_nbr_walk_nh (ipn->ipn_key->ipnk_sw_if_index,
536 fproto, &ipn->ipn_key->ipnk_ip,
537 ip_neighbor_mk_complete_walk, ipn);
540 /* Customer(s) requesting event for this address? */
541 ip_neighbor_publish (ip_neighbor_get_index (ipn));
544 *stats_index = adj_nbr_find (fproto,
545 fib_proto_to_link (fproto),
546 &ipn->ipn_key->ipnk_ip,
547 ipn->ipn_key->ipnk_sw_if_index);
552 ip_neighbor_del (const ip46_address_t * ip, ip46_type_t type, u32 sw_if_index)
556 /* main thread only */
557 ASSERT (0 == vlib_get_thread_index ());
559 IP_NEIGHBOR_INFO ("delete: %U, %U",
560 format_vnet_sw_if_index_name, vnet_get_main (),
561 sw_if_index, format_ip46_address, ip, type);
563 const ip_neighbor_key_t key = {
565 .ipnk_sw_if_index = sw_if_index,
569 ipn = ip_neighbor_db_find (&key);
572 return (VNET_API_ERROR_NO_SUCH_ENTRY);
574 ip_neighbor_free (ipn);
579 typedef struct ip_neighbor_del_all_ctx_t_
582 } ip_neighbor_del_all_ctx_t;
585 ip_neighbor_del_all_walk_cb (index_t ipni, void *arg)
587 ip_neighbor_del_all_ctx_t *ctx = arg;
589 vec_add1 (ctx->ipn_del, ipni);
591 return (WALK_CONTINUE);
595 ip_neighbor_del_all (ip46_type_t type, u32 sw_if_index)
597 IP_NEIGHBOR_INFO ("delete-all: %U, %U",
598 format_ip46_type, type,
599 format_vnet_sw_if_index_name, vnet_get_main (),
602 ip_neighbor_del_all_ctx_t ctx = {
607 ip_neighbor_walk (type, sw_if_index, ip_neighbor_del_all_walk_cb, &ctx);
609 vec_foreach (ipni, ctx.ipn_del) ip_neighbor_free (ip_neighbor_get (*ipni));
610 vec_free (ctx.ipn_del);
614 ip_neighbor_update (vnet_main_t * vnm, adj_index_t ai)
621 ip_neighbor_key_t key = {
622 .ipnk_ip = adj->sub_type.nbr.next_hop,
623 .ipnk_type = fib_proto_to_ip46 (adj->ia_nh_proto),
624 .ipnk_sw_if_index = adj->rewrite_header.sw_if_index,
626 ipn = ip_neighbor_db_find (&key);
628 switch (adj->lookup_next_index)
630 case IP_LOOKUP_NEXT_ARP:
633 adj_nbr_walk_nh (adj->rewrite_header.sw_if_index,
635 &ipn->ipn_key->ipnk_ip,
636 ip_neighbor_mk_complete_walk, ipn);
641 * no matching ARP entry.
642 * construct the rewrite required to for an ARP packet, and stick
643 * that in the adj's pipe to smoke.
645 adj_nbr_update_rewrite
647 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
648 ethernet_build_rewrite
650 adj->rewrite_header.sw_if_index,
652 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
655 * since the FIB has added this adj for a route, it makes sense it
656 * may want to forward traffic sometime soon. Let's send a
657 * speculative ARP. just one. If we were to do periodically that
658 * wouldn't be bad either, but that's more code than i'm prepared to
659 * write at this time for relatively little reward.
662 * adj_nbr_update_rewrite may actually call fib_walk_sync.
663 * fib_walk_sync may allocate a new adjacency and potentially cause
664 * a realloc for adj_pool. When that happens, adj pointer is no
665 * longer valid here.x We refresh adj pointer accordingly.
668 ip_neighbor_probe (adj);
671 case IP_LOOKUP_NEXT_GLEAN:
672 case IP_LOOKUP_NEXT_BCAST:
673 case IP_LOOKUP_NEXT_MCAST:
674 case IP_LOOKUP_NEXT_DROP:
675 case IP_LOOKUP_NEXT_PUNT:
676 case IP_LOOKUP_NEXT_LOCAL:
677 case IP_LOOKUP_NEXT_REWRITE:
678 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
679 case IP_LOOKUP_NEXT_MIDCHAIN:
680 case IP_LOOKUP_NEXT_ICMP_ERROR:
681 case IP_LOOKUP_N_NEXT:
688 ip_neighbor_learn (const ip_neighbor_learn_t * l)
690 ip_neighbor_add (&l->ip, l->type, &l->mac, l->sw_if_index,
691 IP_NEIGHBOR_FLAG_DYNAMIC, NULL);
694 static clib_error_t *
695 ip_neighbor_cmd (vlib_main_t * vm,
696 unformat_input_t * input, vlib_cli_command_t * cmd)
698 ip46_address_t ip = ip46_address_initializer;
699 mac_address_t mac = ZERO_MAC_ADDRESS;
700 vnet_main_t *vnm = vnet_get_main ();
701 ip_neighbor_flags_t flags;
702 u32 sw_if_index = ~0;
706 flags = IP_NEIGHBOR_FLAG_DYNAMIC;
708 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
710 /* set ip arp TenGigE1/1/0/1 1.2.3.4 aa:bb:... or aabb.ccdd... */
711 if (unformat (input, "%U %U %U",
712 unformat_vnet_sw_interface, vnm, &sw_if_index,
713 unformat_ip46_address, &ip, IP46_TYPE_ANY,
714 unformat_mac_address_t, &mac))
716 else if (unformat (input, "delete") || unformat (input, "del"))
718 else if (unformat (input, "static"))
720 flags |= IP_NEIGHBOR_FLAG_STATIC;
721 flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC;
723 else if (unformat (input, "no-fib-entry"))
724 flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
725 else if (unformat (input, "count %d", &count))
731 if (sw_if_index == ~0 ||
732 ip46_address_is_zero (&ip) || mac_address_is_zero (&mac))
733 return clib_error_return (0,
734 "specify interface, IP address and MAC: `%U'",
735 format_unformat_error, input);
740 ip_neighbor_add (&ip, ip46_address_get_type (&ip), &mac, sw_if_index,
743 ip_neighbor_del (&ip, ip46_address_get_type (&ip), sw_if_index);
745 ip46_address_increment (ip46_address_get_type (&ip), &ip);
746 mac_address_increment (&mac);
756 * Add or delete IPv4 ARP cache entries.
758 * @note 'set ip neighbor' options (e.g. delete, static, 'fib-id <id>',
759 * 'count <number>', 'interface ip4_addr mac_addr') can be added in
760 * any order and combination.
764 * Add or delete IPv4 ARP cache entries as follows. MAC Address can be in
765 * either aa:bb:cc:dd:ee:ff format or aabb.ccdd.eeff format.
766 * @cliexcmd{set ip neighbor GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe}
767 * @cliexcmd{set ip neighbor delete GigabitEthernet2/0/0 6.0.0.3 de:ad:be:ef:ba:be}
769 * To add or delete an IPv4 ARP cache entry to or from a specific fib
771 * @cliexcmd{set ip neighbor fib-id 1 GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe}
772 * @cliexcmd{set ip neighbor fib-id 1 delete GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe}
774 * Add or delete IPv4 static ARP cache entries as follows:
775 * @cliexcmd{set ip neighbor static GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe}
776 * @cliexcmd{set ip neighbor static delete GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe}
778 * For testing / debugging purposes, the 'set ip neighbor' command can add or
779 * delete multiple entries. Supply the 'count N' parameter:
780 * @cliexcmd{set ip neighbor count 10 GigabitEthernet2/0/0 6.0.0.3 dead.beef.babe}
783 VLIB_CLI_COMMAND (ip_neighbor_command, static) = {
784 .path = "set ip neighbor",
786 "set ip neighbor [del] <intfc> <ip-address> <mac-address> [static] [no-fib-entry] [count <count>] [fib-id <fib-id>] [proxy <lo-addr> - <hi-addr>]",
787 .function = ip_neighbor_cmd,
789 VLIB_CLI_COMMAND (ip_neighbor_command2, static) = {
790 .path = "ip neighbor",
792 "ip neighbor [del] <intfc> <ip-address> <mac-address> [static] [no-fib-entry] [count <count>] [fib-id <fib-id>] [proxy <lo-addr> - <hi-addr>]",
793 .function = ip_neighbor_cmd,
798 ip_neighbor_sort (void *a1, void *a2)
800 index_t *ipni1 = a1, *ipni2 = a2;
801 ip_neighbor_t *ipn1, *ipn2;
804 ipn1 = ip_neighbor_get (*ipni1);
805 ipn2 = ip_neighbor_get (*ipni2);
807 cmp = vnet_sw_interface_compare (vnet_get_main (),
808 ipn1->ipn_key->ipnk_sw_if_index,
809 ipn2->ipn_key->ipnk_sw_if_index);
811 cmp = ip46_address_cmp (&ipn1->ipn_key->ipnk_ip, &ipn2->ipn_key->ipnk_ip);
816 ip_neighbor_entries (u32 sw_if_index, ip46_type_t type)
818 index_t *ipnis = NULL;
822 pool_foreach (ipn, ip_neighbor_pool,
824 if (sw_if_index != ~0 &&
825 ipn->ipn_key->ipnk_sw_if_index != sw_if_index &&
826 (IP46_TYPE_ANY == type ||
827 (ipn->ipn_key->ipnk_type == type)))
829 vec_add1 (ipnis, ip_neighbor_get_index(ipn));
835 vec_sort_with_function (ipnis, ip_neighbor_sort);
839 static clib_error_t *
840 ip_neighbor_show_sorted_i (vlib_main_t * vm,
841 unformat_input_t * input,
842 vlib_cli_command_t * cmd, ip46_type_t type)
844 ip_neighbor_elt_t *elt, *head;
846 head = pool_elt_at_index (ip_neighbor_elt_pool,
847 ip_neighbor_list_head[type]);
850 vlib_cli_output (vm, "%=12s%=40s%=6s%=20s%=24s", "Time", "IP",
851 "Flags", "Ethernet", "Interface");
854 /* the list is time sorted, newest first, so start from the back
855 * and work forwards. Stop when we get to one that is alive */
856 clib_llist_foreach_reverse(ip_neighbor_elt_pool,
857 ipne_anchor, head, elt,
859 vlib_cli_output (vm, "%U", format_ip_neighbor, elt->ipne_index);
866 static clib_error_t *
867 ip_neighbor_show_i (vlib_main_t * vm,
868 unformat_input_t * input,
869 vlib_cli_command_t * cmd, ip46_type_t type)
871 index_t *ipni, *ipnis = NULL;
874 /* Filter entries by interface if given. */
876 (void) unformat_user (input, unformat_vnet_sw_interface, vnet_get_main (),
879 ipnis = ip_neighbor_entries (sw_if_index, type);
882 vlib_cli_output (vm, "%=12s%=40s%=6s%=20s%=24s", "Time", "IP",
883 "Flags", "Ethernet", "Interface");
885 vec_foreach (ipni, ipnis)
887 vlib_cli_output (vm, "%U", format_ip_neighbor, *ipni);
894 static clib_error_t *
895 ip_neighbor_show (vlib_main_t * vm,
896 unformat_input_t * input, vlib_cli_command_t * cmd)
898 return (ip_neighbor_show_i (vm, input, cmd, IP46_TYPE_ANY));
901 static clib_error_t *
902 ip6_neighbor_show (vlib_main_t * vm,
903 unformat_input_t * input, vlib_cli_command_t * cmd)
905 return (ip_neighbor_show_i (vm, input, cmd, IP46_TYPE_IP6));
908 static clib_error_t *
909 ip4_neighbor_show (vlib_main_t * vm,
910 unformat_input_t * input, vlib_cli_command_t * cmd)
912 return (ip_neighbor_show_i (vm, input, cmd, IP46_TYPE_IP4));
915 static clib_error_t *
916 ip6_neighbor_show_sorted (vlib_main_t * vm,
917 unformat_input_t * input, vlib_cli_command_t * cmd)
919 return (ip_neighbor_show_sorted_i (vm, input, cmd, IP46_TYPE_IP6));
922 static clib_error_t *
923 ip4_neighbor_show_sorted (vlib_main_t * vm,
924 unformat_input_t * input, vlib_cli_command_t * cmd)
926 return (ip_neighbor_show_sorted_i (vm, input, cmd, IP46_TYPE_IP4));
930 * Display all the IP neighbor entries.
933 * Example of how to display the IPv4 ARP table:
934 * @cliexstart{show ip neighbor}
935 * Time FIB IP4 Flags Ethernet Interface
936 * 346.3028 0 6.1.1.3 de:ad:be:ef:ba:be GigabitEthernet2/0/0
937 * 3077.4271 0 6.1.1.4 S de:ad:be:ef:ff:ff GigabitEthernet2/0/0
938 * 2998.6409 1 6.2.2.3 de:ad:be:ef:00:01 GigabitEthernet2/0/0
939 * Proxy arps enabled for:
940 * Fib_index 0 6.0.0.1 - 6.0.0.11
944 VLIB_CLI_COMMAND (show_ip_neighbors_cmd_node, static) = {
945 .path = "show ip neighbors",
946 .function = ip_neighbor_show,
947 .short_help = "show ip neighbors [interface]",
949 VLIB_CLI_COMMAND (show_ip4_neighbors_cmd_node, static) = {
950 .path = "show ip4 neighbors",
951 .function = ip4_neighbor_show,
952 .short_help = "show ip4 neighbors [interface]",
954 VLIB_CLI_COMMAND (show_ip6_neighbors_cmd_node, static) = {
955 .path = "show ip6 neighbors",
956 .function = ip6_neighbor_show,
957 .short_help = "show ip6 neighbors [interface]",
959 VLIB_CLI_COMMAND (show_ip_neighbor_cmd_node, static) = {
960 .path = "show ip neighbor",
961 .function = ip_neighbor_show,
962 .short_help = "show ip neighbor [interface]",
964 VLIB_CLI_COMMAND (show_ip4_neighbor_cmd_node, static) = {
965 .path = "show ip4 neighbor",
966 .function = ip4_neighbor_show,
967 .short_help = "show ip4 neighbor [interface]",
969 VLIB_CLI_COMMAND (show_ip6_neighbor_cmd_node, static) = {
970 .path = "show ip6 neighbor",
971 .function = ip6_neighbor_show,
972 .short_help = "show ip6 neighbor [interface]",
974 VLIB_CLI_COMMAND (show_ip4_neighbor_sorted_cmd_node, static) = {
975 .path = "show ip4 neighbor-sorted",
976 .function = ip4_neighbor_show_sorted,
977 .short_help = "show ip4 neighbor-sorted",
979 VLIB_CLI_COMMAND (show_ip6_neighbor_sorted_cmd_node, static) = {
980 .path = "show ip6 neighbor-sorted",
981 .function = ip6_neighbor_show_sorted,
982 .short_help = "show ip6 neighbor-sorted",
986 static ip_neighbor_vft_t ip_nbr_vfts[IP46_N_TYPES];
989 ip_neighbor_register (ip46_type_t type, const ip_neighbor_vft_t * vft)
991 ip_nbr_vfts[type] = *vft;
995 ip_neighbor_probe_dst (const ip_adjacency_t * adj, const ip46_address_t * dst)
997 if (!vnet_sw_interface_is_admin_up (vnet_get_main (),
998 adj->rewrite_header.sw_if_index))
1001 switch (adj->ia_nh_proto)
1003 case FIB_PROTOCOL_IP6:
1004 ip6_neighbor_probe_dst (adj, &dst->ip6);
1006 case FIB_PROTOCOL_IP4:
1007 ip4_neighbor_probe_dst (adj, &dst->ip4);
1009 case FIB_PROTOCOL_MPLS:
1016 ip_neighbor_probe (const ip_adjacency_t * adj)
1018 ip_neighbor_probe_dst (adj, &adj->sub_type.nbr.next_hop);
1022 ip_neighbor_advertise (vlib_main_t * vm,
1024 const ip46_address_t * addr, u32 sw_if_index)
1026 vnet_main_t *vnm = vnet_get_main ();
1028 if (type == IP46_TYPE_IP4 || type == IP46_TYPE_BOTH)
1029 ip4_neighbor_advertise (vm, vnm, sw_if_index, (addr) ? &addr->ip4 : NULL);
1030 if (type == IP46_TYPE_IP6 || type == IP46_TYPE_BOTH)
1031 ip6_neighbor_advertise (vm, vnm, sw_if_index, (addr) ? &addr->ip6 : NULL);
1035 ip_neighbor_walk (ip46_type_t type,
1036 u32 sw_if_index, ip_neighbor_walk_cb_t cb, void *ctx)
1038 ip_neighbor_key_t *key;
1041 if (~0 == sw_if_index)
1045 vec_foreach (hash, ip_neighbor_db[type].ipndb_hash)
1048 hash_foreach (key, ipni, *hash,
1059 if (vec_len (ip_neighbor_db[type].ipndb_hash) <= sw_if_index)
1061 hash = ip_neighbor_db[type].ipndb_hash[sw_if_index];
1064 hash_foreach (key, ipni, hash,
1073 ip4_neighbor_proxy_add (u32 fib_index,
1074 const ip4_address_t * start,
1075 const ip4_address_t * end)
1077 if (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_add)
1079 return (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_add
1080 (fib_index, start, end));
1087 ip4_neighbor_proxy_delete (u32 fib_index,
1088 const ip4_address_t * start,
1089 const ip4_address_t * end)
1091 if (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_del)
1093 return (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_del
1094 (fib_index, start, end));
1100 ip4_neighbor_proxy_enable (u32 sw_if_index)
1102 if (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_enable)
1104 return (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_enable (sw_if_index));
1110 ip4_neighbor_proxy_disable (u32 sw_if_index)
1112 if (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_disable)
1114 return (ip_nbr_vfts[IP46_TYPE_IP4].inv_proxy4_disable (sw_if_index));
1120 ip6_neighbor_proxy_add (u32 sw_if_index, const ip6_address_t * addr)
1122 if (ip_nbr_vfts[IP46_TYPE_IP6].inv_proxy6_add)
1124 return (ip_nbr_vfts[IP46_TYPE_IP6].inv_proxy6_add (sw_if_index, addr));
1130 ip6_neighbor_proxy_del (u32 sw_if_index, const ip6_address_t * addr)
1132 if (ip_nbr_vfts[IP46_TYPE_IP6].inv_proxy6_del)
1134 return (ip_nbr_vfts[IP46_TYPE_IP6].inv_proxy6_del (sw_if_index, addr));
1140 ip_neighbor_ethernet_change_mac (ethernet_main_t * em,
1141 u32 sw_if_index, uword opaque)
1146 IP_NEIGHBOR_DBG ("mac-change: %U",
1147 format_vnet_sw_if_index_name, vnet_get_main (),
1151 pool_foreach (ipn, ip_neighbor_pool,
1153 if (ipn->ipn_key->ipnk_sw_if_index == sw_if_index)
1154 adj_nbr_walk_nh (ipn->ipn_key->ipnk_sw_if_index,
1155 fib_proto_from_ip46(ipn->ipn_key->ipnk_type),
1156 &ipn->ipn_key->ipnk_ip,
1157 ip_neighbor_mk_complete_walk,
1162 ai = adj_glean_get (FIB_PROTOCOL_IP4, sw_if_index);
1164 if (ADJ_INDEX_INVALID != ai)
1165 adj_glean_update_rewrite (ai);
1169 ip_neighbor_populate (ip46_type_t type, u32 sw_if_index)
1171 index_t *ipnis = NULL, *ipni;
1174 IP_NEIGHBOR_DBG ("populate: %U %U",
1175 format_vnet_sw_if_index_name, vnet_get_main (),
1176 sw_if_index, format_ip46_type, type);
1179 pool_foreach (ipn, ip_neighbor_pool,
1181 if (ipn->ipn_key->ipnk_type == type &&
1182 ipn->ipn_key->ipnk_sw_if_index == sw_if_index)
1183 vec_add1 (ipnis, ipn - ip_neighbor_pool);
1187 vec_foreach (ipni, ipnis)
1189 ipn = ip_neighbor_get (*ipni);
1191 adj_nbr_walk_nh (ipn->ipn_key->ipnk_sw_if_index,
1192 fib_proto_from_ip46 (ipn->ipn_key->ipnk_type),
1193 &ipn->ipn_key->ipnk_ip,
1194 ip_neighbor_mk_complete_walk, ipn);
1200 ip_neighbor_flush (ip46_type_t type, u32 sw_if_index)
1202 index_t *ipnis = NULL, *ipni;
1205 IP_NEIGHBOR_DBG ("flush: %U %U",
1206 format_vnet_sw_if_index_name, vnet_get_main (),
1207 sw_if_index, format_ip46_type, type);
1210 pool_foreach (ipn, ip_neighbor_pool,
1212 if (ipn->ipn_key->ipnk_type == type &&
1213 ipn->ipn_key->ipnk_sw_if_index == sw_if_index &&
1214 ip_neighbor_is_dynamic (ipn))
1215 vec_add1 (ipnis, ipn - ip_neighbor_pool);
1219 vec_foreach (ipni, ipnis) ip_neighbor_free (ip_neighbor_get (*ipni));
1224 ip_neighbor_mark_one (index_t ipni, void *ctx)
1228 ipn = ip_neighbor_get (ipni);
1230 ipn->ipn_flags |= IP_NEIGHBOR_FLAG_STALE;
1232 return (WALK_CONTINUE);
1236 ip_neighbor_mark (ip46_type_t type)
1238 ip_neighbor_walk (type, ~0, ip_neighbor_mark_one, NULL);
1241 typedef struct ip_neighbor_sweep_ctx_t_
1243 index_t *ipnsc_stale;
1244 } ip_neighbor_sweep_ctx_t;
1247 ip_neighbor_sweep_one (index_t ipni, void *arg)
1249 ip_neighbor_sweep_ctx_t *ctx = arg;
1252 ipn = ip_neighbor_get (ipni);
1254 if (ipn->ipn_flags & IP_NEIGHBOR_FLAG_STALE)
1256 vec_add1 (ctx->ipnsc_stale, ipni);
1259 return (WALK_CONTINUE);
1263 ip_neighbor_sweep (ip46_type_t type)
1265 ip_neighbor_sweep_ctx_t ctx = { };
1268 ip_neighbor_walk (type, ~0, ip_neighbor_sweep_one, &ctx);
1270 vec_foreach (ipni, ctx.ipnsc_stale)
1272 ip_neighbor_free (ip_neighbor_get (*ipni));
1274 vec_free (ctx.ipnsc_stale);
1278 * Remove any arp entries associated with the specified interface
1280 static clib_error_t *
1281 ip_neighbor_interface_admin_change (vnet_main_t * vnm,
1282 u32 sw_if_index, u32 flags)
1286 IP_NEIGHBOR_DBG ("interface-admin: %U %s",
1287 format_vnet_sw_if_index_name, vnet_get_main (),
1289 (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ? "up" : "down"));
1291 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
1293 FOREACH_IP46_TYPE (type) ip_neighbor_populate (type, sw_if_index);
1297 /* admin down, flush all neighbours */
1298 FOREACH_IP46_TYPE (type) ip_neighbor_flush (type, sw_if_index);
1304 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip_neighbor_interface_admin_change);
1307 * Remove any arp entries associated with the specified interface
1309 static clib_error_t *
1310 ip_neighbor_delete_sw_interface (vnet_main_t * vnm,
1311 u32 sw_if_index, u32 is_add)
1313 IP_NEIGHBOR_DBG ("interface-change: %U %s",
1314 format_vnet_sw_if_index_name, vnet_get_main (),
1315 sw_if_index, (is_add ? "add" : "del"));
1317 if (!is_add && sw_if_index != ~0)
1321 FOREACH_IP46_TYPE (type) ip_neighbor_flush (type, sw_if_index);
1327 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip_neighbor_delete_sw_interface);
1329 typedef struct ip_neighbor_walk_covered_ctx_t_
1332 ip46_address_t addr;
1335 } ip_neighbor_walk_covered_ctx_t;
1338 ip_neighbor_walk_covered (index_t ipni, void *arg)
1340 ip_neighbor_walk_covered_ctx_t *ctx = arg;
1343 ipn = ip_neighbor_get (ipni);
1345 ASSERT (ipn->ipn_key->ipnk_type == ctx->type);
1347 if (IP46_TYPE_IP4 == ctx->type)
1349 if (ip4_destination_matches_route (&ip4_main,
1350 &ipn->ipn_key->ipnk_ip.ip4,
1353 ip_neighbor_is_dynamic (ipn))
1355 vec_add1 (ctx->ipnis, ip_neighbor_get_index (ipn));
1358 return (WALK_CONTINUE);
1363 * callback when an interface address is added or deleted
1366 ip_neighbor_add_del_interface_address_v4 (ip4_main_t * im,
1369 ip4_address_t * address,
1371 u32 if_address_index, u32 is_del)
1374 * Flush the ARP cache of all entries covered by the address
1375 * that is being removed.
1377 IP_NEIGHBOR_DBG ("addr-%d: %U, %U/%d",
1378 (is_del ? "del" : "add"),
1379 format_vnet_sw_if_index_name, vnet_get_main (),
1380 sw_if_index, format_ip4_address, address, address_length);
1384 ip_neighbor_walk_covered_ctx_t ctx = {
1385 .addr.ip4 = *address,
1386 .type = IP46_TYPE_IP4,
1387 .length = address_length,
1391 ip_neighbor_walk (IP46_TYPE_IP4, sw_if_index,
1392 ip_neighbor_walk_covered, &ctx);
1394 vec_foreach (ipni, ctx.ipnis)
1395 ip_neighbor_free (ip_neighbor_get (*ipni));
1397 vec_free (ctx.ipnis);
1402 * callback when an interface address is added or deleted
1405 ip_neighbor_add_del_interface_address_v6 (ip6_main_t * im,
1408 ip6_address_t * address,
1410 u32 if_address_index, u32 is_del)
1413 * Flush the ARP cache of all entries covered by the address
1414 * that is being removed.
1416 IP_NEIGHBOR_DBG ("addr-change: %U, %U/%d %s",
1417 format_vnet_sw_if_index_name, vnet_get_main (),
1418 sw_if_index, format_ip6_address, address, address_length,
1419 (is_del ? "del" : "add"));
1423 ip_neighbor_walk_covered_ctx_t ctx = {
1424 .addr.ip6 = *address,
1425 .type = IP46_TYPE_IP6,
1426 .length = address_length,
1430 ip_neighbor_walk (IP46_TYPE_IP6, sw_if_index,
1431 ip_neighbor_walk_covered, &ctx);
1433 vec_foreach (ipni, ctx.ipnis)
1434 ip_neighbor_free (ip_neighbor_get (*ipni));
1436 vec_free (ctx.ipnis);
1440 typedef struct ip_neighbor_table_bind_ctx_t_
1444 } ip_neighbor_table_bind_ctx_t;
1447 ip_neighbor_walk_table_bind (index_t ipni, void *arg)
1449 ip_neighbor_table_bind_ctx_t *ctx = arg;
1452 ipn = ip_neighbor_get (ipni);
1453 ip_neighbor_adj_fib_remove (ipn, ctx->old_fib_index);
1454 ip_neighbor_adj_fib_add (ipn, ctx->new_fib_index);
1456 return (WALK_CONTINUE);
1460 ip_neighbor_table_bind_v4 (ip4_main_t * im,
1463 u32 new_fib_index, u32 old_fib_index)
1465 ip_neighbor_table_bind_ctx_t ctx = {
1466 .old_fib_index = old_fib_index,
1467 .new_fib_index = new_fib_index,
1470 ip_neighbor_walk (IP46_TYPE_IP4, sw_if_index,
1471 ip_neighbor_walk_table_bind, &ctx);
1475 ip_neighbor_table_bind_v6 (ip6_main_t * im,
1478 u32 new_fib_index, u32 old_fib_index)
1480 ip_neighbor_table_bind_ctx_t ctx = {
1481 .old_fib_index = old_fib_index,
1482 .new_fib_index = new_fib_index,
1485 ip_neighbor_walk (IP46_TYPE_IP6, sw_if_index,
1486 ip_neighbor_walk_table_bind, &ctx);
1489 typedef enum ip_neighbor_age_state_t_
1491 IP_NEIGHBOR_AGE_ALIVE,
1492 IP_NEIGHBOR_AGE_PROBE,
1493 IP_NEIGHBOR_AGE_DEAD,
1494 } ip_neighbor_age_state_t;
1496 #define IP_NEIGHBOR_PROCESS_SLEEP_LONG (0)
1498 static ip_neighbor_age_state_t
1499 ip_neighbour_age_out (index_t ipni, f64 now, f64 * wait)
1505 ipn = ip_neighbor_get (ipni);
1506 ipndb_age = ip_neighbor_db[ipn->ipn_key->ipnk_type].ipndb_age;
1507 ttl = now - ipn->ipn_time_last_updated;
1510 if (ttl > ipndb_age)
1512 IP_NEIGHBOR_DBG ("aged: %U @%f - %f > %d",
1513 format_ip_neighbor, ipni, now,
1514 ipn->ipn_time_last_updated, ipndb_age);
1515 if (ipn->ipn_n_probes > 2)
1517 /* 3 strikes and yea-re out */
1518 IP_NEIGHBOR_DBG ("dead: %U", format_ip_neighbor, ipni);
1520 return (IP_NEIGHBOR_AGE_DEAD);
1526 ai = adj_glean_get (fib_proto_from_ip46 (ipn->ipn_key->ipnk_type),
1527 ip_neighbor_get_sw_if_index (ipn));
1529 if (ADJ_INDEX_INVALID != ai)
1530 ip_neighbor_probe_dst (adj_get (ai), ip_neighbor_get_ip (ipn));
1532 ipn->ipn_n_probes++;
1538 /* here we are sure that ttl <= ipndb_age */
1539 *wait = ipndb_age - ttl + 1;
1540 return (IP_NEIGHBOR_AGE_ALIVE);
1543 return (IP_NEIGHBOR_AGE_PROBE);
1546 typedef enum ip_neighbor_process_event_t_
1548 IP_NEIGHBOR_AGE_PROCESS_WAKEUP,
1549 } ip_neighbor_process_event_t;
1552 ip_neighbor_age_loop (vlib_main_t * vm,
1553 vlib_node_runtime_t * rt,
1554 vlib_frame_t * f, ip46_type_t type)
1556 uword event_type, *event_data = NULL;
1559 /* Set the timeout to an effectively infinite value when the process starts */
1560 timeout = IP_NEIGHBOR_PROCESS_SLEEP_LONG;
1567 vlib_process_wait_for_event (vm);
1569 vlib_process_wait_for_event_or_clock (vm, timeout);
1571 event_type = vlib_process_get_events (vm, &event_data);
1572 vec_reset_length (event_data);
1574 now = vlib_time_now (vm);
1581 ip_neighbor_elt_t *elt, *head;
1584 timeout = ip_neighbor_db[type].ipndb_age;
1585 head = pool_elt_at_index (ip_neighbor_elt_pool,
1586 ip_neighbor_list_head[type]);
1589 /* the list is time sorted, newest first, so start from the back
1590 * and work forwards. Stop when we get to one that is alive */
1592 clib_llist_foreach_reverse(ip_neighbor_elt_pool,
1593 ipne_anchor, head, elt,
1595 ip_neighbor_age_state_t res;
1597 res = ip_neighbour_age_out(elt->ipne_index, now, &wait);
1599 if (IP_NEIGHBOR_AGE_ALIVE == res) {
1600 /* the oldest neighbor has not yet expired, go back to sleep */
1601 timeout = clib_min (wait, timeout);
1604 else if (IP_NEIGHBOR_AGE_DEAD == res) {
1605 /* the oldest neighbor is dead, pop it, then restart the walk
1606 * again from the back */
1607 ip_neighbor_free (ip_neighbor_get(elt->ipne_index));
1611 timeout = clib_min (wait, timeout);
1616 case IP_NEIGHBOR_AGE_PROCESS_WAKEUP:
1619 if (!ip_neighbor_db[type].ipndb_age)
1621 /* aging has been disabled */
1625 ip_neighbor_elt_t *elt, *head;
1627 head = pool_elt_at_index (ip_neighbor_elt_pool,
1628 ip_neighbor_list_head[type]);
1629 /* no neighbors yet */
1630 if (clib_llist_is_empty (ip_neighbor_elt_pool, ipne_anchor, head))
1632 timeout = ip_neighbor_db[type].ipndb_age;
1636 /* poke the oldset neighbour for aging, which returns how long we sleep for */
1637 elt = clib_llist_prev (ip_neighbor_elt_pool, ipne_anchor, head);
1638 ip_neighbour_age_out (elt->ipne_index, now, &timeout);
1647 ip4_neighbor_age_process (vlib_main_t * vm,
1648 vlib_node_runtime_t * rt, vlib_frame_t * f)
1650 return (ip_neighbor_age_loop (vm, rt, f, IP46_TYPE_IP4));
1654 ip6_neighbor_age_process (vlib_main_t * vm,
1655 vlib_node_runtime_t * rt, vlib_frame_t * f)
1657 return (ip_neighbor_age_loop (vm, rt, f, IP46_TYPE_IP6));
1661 VLIB_REGISTER_NODE (ip4_neighbor_age_process_node,static) = {
1662 .function = ip4_neighbor_age_process,
1663 .type = VLIB_NODE_TYPE_PROCESS,
1664 .name = "ip4-neighbor-age-process",
1666 VLIB_REGISTER_NODE (ip6_neighbor_age_process_node,static) = {
1667 .function = ip6_neighbor_age_process,
1668 .type = VLIB_NODE_TYPE_PROCESS,
1669 .name = "ip6-neighbor-age-process",
1674 ip_neighbor_config (ip46_type_t type, u32 limit, u32 age, bool recycle)
1676 ip_neighbor_db[type].ipndb_limit = limit;
1677 ip_neighbor_db[type].ipndb_recycle = recycle;
1678 ip_neighbor_db[type].ipndb_age = age;
1680 vlib_process_signal_event (vlib_get_main (),
1681 (IP46_TYPE_IP4 == type ?
1682 ip4_neighbor_age_process_node.index :
1683 ip6_neighbor_age_process_node.index),
1684 IP_NEIGHBOR_AGE_PROCESS_WAKEUP, 0);
1689 static clib_error_t *
1690 ip_neighbor_config_show (vlib_main_t * vm,
1691 unformat_input_t * input, vlib_cli_command_t * cmd)
1696 FOREACH_IP46_TYPE(type) {
1697 vlib_cli_output (vm, "%U:", format_ip46_type, type);
1698 vlib_cli_output (vm, " limit:%d, age:%d, recycle:%d",
1699 ip_neighbor_db[type].ipndb_limit,
1700 ip_neighbor_db[type].ipndb_age,
1701 ip_neighbor_db[type].ipndb_recycle);
1709 VLIB_CLI_COMMAND (show_ip_neighbor_cfg_cmd_node, static) = {
1710 .path = "show ip neighbor-config",
1711 .function = ip_neighbor_config_show,
1712 .short_help = "show ip neighbor-config",
1716 static clib_error_t *
1717 ip_neighbor_init (vlib_main_t * vm)
1720 ip4_add_del_interface_address_callback_t cb = {
1721 .function = ip_neighbor_add_del_interface_address_v4,
1723 vec_add1 (ip4_main.add_del_interface_address_callbacks, cb);
1726 ip6_add_del_interface_address_callback_t cb = {
1727 .function = ip_neighbor_add_del_interface_address_v6,
1729 vec_add1 (ip6_main.add_del_interface_address_callbacks, cb);
1732 ip4_table_bind_callback_t cb = {
1733 .function = ip_neighbor_table_bind_v4,
1735 vec_add1 (ip4_main.table_bind_callbacks, cb);
1738 ip6_table_bind_callback_t cb = {
1739 .function = ip_neighbor_table_bind_v6,
1741 vec_add1 (ip6_main.table_bind_callbacks, cb);
1744 ethernet_address_change_ctx_t ctx = {
1745 .function = ip_neighbor_ethernet_change_mac,
1746 .function_opaque = 0,
1748 vec_add1 (ethernet_main.address_change_callbacks, ctx);
1751 ipn_logger = vlib_log_register_class ("ip", "neighbor");
1755 FOREACH_IP46_TYPE (type)
1756 ip_neighbor_list_head[type] =
1757 clib_llist_make_head (ip_neighbor_elt_pool, ipne_anchor);
1763 VLIB_INIT_FUNCTION (ip_neighbor_init) =
1765 .runs_after = VLIB_INITS("ip_main_init"),
1770 * fd.io coding-style-patch-verification: ON
1773 * eval: (c-set-style "gnu")