Retain the last complete observations for each monitored link, address
family, interface type and VNI while its neighbor dump fails. Continue
to update other interfaces. A complete dump replaces the retained
state, including when it returns no entries or a later write fails.

Invalidate the old link generation when the monitor detects deletion
or rename. This prevents retention after a replacement reuses the
ifindex or a link moves away from its watched name.

Assisted-by: GPT-6.1 Sol, Codex
Signed-off-by: Guillaume Boutry <[email protected]>
---
 controller/host-if-monitor-stub.c   |   6 +-
 controller/host-if-monitor.c        |  83 ++++++++-----
 controller/host-if-monitor.h        |   9 +-
 controller/neighbor-exchange-stub.c |   5 +
 controller/neighbor-exchange.c      |  89 ++++++++++++--
 controller/neighbor-exchange.h      |   3 +
 controller/ovn-controller.c         |   5 +
 tests/system-ovn-netlink.at         |  35 +++++-
 tests/test-ovn-netlink.c            | 176 ++++++++++++++++++++++++----
 9 files changed, 351 insertions(+), 60 deletions(-)

diff --git a/controller/host-if-monitor-stub.c 
b/controller/host-if-monitor-stub.c
index b174cf996..8dd93f9dd 100644
--- a/controller/host-if-monitor-stub.c
+++ b/controller/host-if-monitor-stub.c
@@ -36,8 +36,8 @@ host_if_monitor_update_watches(const struct sset *if_names 
OVS_UNUSED)
 {
 }
 
-int32_t
-host_if_monitor_ifname_toindex(const char *if_name OVS_UNUSED)
+struct host_if_monitor_link
+host_if_monitor_get_link(const char *if_name OVS_UNUSED)
 {
-    return 0;
+    return (struct host_if_monitor_link) { 0 };
 }
diff --git a/controller/host-if-monitor.c b/controller/host-if-monitor.c
index 65110d2a3..fa46c493c 100644
--- a/controller/host-if-monitor.c
+++ b/controller/host-if-monitor.c
@@ -19,7 +19,7 @@
 #include <net/if.h>
 
 #include "lib/rtnetlink.h"
-#include "lib/simap.h"
+#include "openvswitch/shash.h"
 #include "openvswitch/vlog.h"
 
 #include "host-if-monitor.h"
@@ -32,7 +32,9 @@ struct host_if_monitor {
     struct nln_notifier *link_notifier;
 
     struct sset watched_interfaces;
-    struct simap ifname_to_ifindex;
+    /* Maps interface names to struct host_if_monitor_link. */
+    struct shash links;
+    uint64_t last_link_generation;
 
     bool changes_detected;
 };
@@ -40,7 +42,7 @@ struct host_if_monitor {
 static struct host_if_monitor monitor = (struct host_if_monitor) {
     .link_notifier = NULL,
     .watched_interfaces = SSET_INITIALIZER(&monitor.watched_interfaces),
-    .ifname_to_ifindex = SIMAP_INITIALIZER(&monitor.ifname_to_ifindex),
+    .links = SHASH_INITIALIZER(&monitor.links),
     .changes_detected = false,
 };
 
@@ -65,6 +67,21 @@ host_if_monitor_run(void)
     return monitor.changes_detected;
 }
 
+static void
+host_if_monitor_set_link(const char *if_name, int32_t if_index)
+{
+    struct host_if_monitor_link *link = shash_find_data(&monitor.links,
+                                                       if_name);
+    if (!link) {
+        link = xzalloc(sizeof *link);
+        shash_add(&monitor.links, if_name, link);
+    }
+    if (link->if_index != if_index) {
+        link->if_index = if_index;
+        link->generation = ++monitor.last_link_generation;
+    }
+}
+
 void
 host_if_monitor_update_watches(const struct sset *if_names)
 {
@@ -85,10 +102,11 @@ host_if_monitor_update_watches(const struct sset *if_names)
         sset_clone(&monitor.watched_interfaces, if_names);
 
         /* Remove mappings for if_names that are not tracked anymore. */
-        struct simap_node *sn;
-        SIMAP_FOR_EACH_SAFE (sn, &monitor.ifname_to_ifindex) {
-            if (!sset_contains(&monitor.watched_interfaces, sn->name)) {
-                simap_delete(&monitor.ifname_to_ifindex, sn);
+        struct shash_node *node;
+        SHASH_FOR_EACH_SAFE (node, &monitor.links) {
+            if (!sset_contains(&monitor.watched_interfaces, node->name)) {
+                free(node->data);
+                shash_delete(&monitor.links, node);
             }
         }
     }
@@ -110,8 +128,10 @@ host_if_monitor_update_watches(const struct sset *if_names)
          *   callback is not called.
          */
         SSET_FOR_EACH (if_name, &new_if_names) {
-            simap_put(&monitor.ifname_to_ifindex, if_name,
-                      if_nametoindex(if_name));
+            int32_t if_index = if_nametoindex(if_name);
+            if (if_index) {
+                host_if_monitor_set_link(if_name, if_index);
+            }
         }
     } else {
         if (monitor.link_notifier) {
@@ -124,10 +144,12 @@ host_if_monitor_update_watches(const struct sset 
*if_names)
     sset_destroy(&new_if_names);
 }
 
-int32_t
-host_if_monitor_ifname_toindex(const char *if_name)
+struct host_if_monitor_link
+host_if_monitor_get_link(const char *if_name)
 {
-    return simap_get(&monitor.ifname_to_ifindex, if_name);
+    const struct host_if_monitor_link *link = shash_find_data(&monitor.links,
+                                                             if_name);
+    return link ? *link : (struct host_if_monitor_link) { 0 };
 }
 
 static void
@@ -137,22 +159,29 @@ if_notifier_cb(const struct rtnetlink_change *change, 
void *aux OVS_UNUSED)
         return;
     }
 
-    switch (change->nlmsg_type) {
-    case RTM_NEWLINK:
-        if ((change->ifi_flags & IFF_UP)
-            && sset_find(&monitor.watched_interfaces, change->ifname)) {
-            simap_put(&monitor.ifname_to_ifindex,
-                      change->ifname, change->if_index);
-            monitor.changes_detected = true;
-        }
-        break;
-    case RTM_DELLINK:
-        if (sset_find(&monitor.watched_interfaces, change->ifname)) {
-            simap_find_and_delete(&monitor.ifname_to_ifindex, change->ifname);
+    if (change->nlmsg_type != RTM_NEWLINK
+        && change->nlmsg_type != RTM_DELLINK) {
+        return;
+    }
+
+    /* A rename event names the new link, which may not be watched.
+     * Invalidate the old name by index so its generation cannot restore
+     * observations to an interface that no longer matches the watch. */
+    struct shash_node *node;
+    SHASH_FOR_EACH_SAFE (node, &monitor.links) {
+        const struct host_if_monitor_link *link = node->data;
+        if (link->if_index == change->if_index
+            && (change->nlmsg_type == RTM_DELLINK
+                || strcmp(node->name, change->ifname))) {
+            free(node->data);
+            shash_delete(&monitor.links, node);
             monitor.changes_detected = true;
         }
-        break;
-    default:
-        break;
+    }
+
+    if (change->nlmsg_type == RTM_NEWLINK && (change->ifi_flags & IFF_UP)
+        && sset_contains(&monitor.watched_interfaces, change->ifname)) {
+        host_if_monitor_set_link(change->ifname, change->if_index);
+        monitor.changes_detected = true;
     }
 }
diff --git a/controller/host-if-monitor.h b/controller/host-if-monitor.h
index a41c5869c..373946645 100644
--- a/controller/host-if-monitor.h
+++ b/controller/host-if-monitor.h
@@ -18,13 +18,18 @@
 
 #include <stdint.h>
 #include "lib/sset.h"
-#include "lib/smap.h"
 
 void host_if_monitor_wait(void);
 bool host_if_monitor_run(void);
 
 void host_if_monitor_update_watches(const struct sset *if_names);
 
-int32_t host_if_monitor_ifname_toindex(const char *if_name);
+struct host_if_monitor_link {
+    int32_t if_index;
+    /* Changes when a watched link is removed or replaced. */
+    uint64_t generation;
+};
+
+struct host_if_monitor_link host_if_monitor_get_link(const char *if_name);
 
 #endif /* HOST_IF_MONITOR_H */
diff --git a/controller/neighbor-exchange-stub.c 
b/controller/neighbor-exchange-stub.c
index a5420fcff..d2a80d580 100644
--- a/controller/neighbor-exchange-stub.c
+++ b/controller/neighbor-exchange-stub.c
@@ -29,6 +29,11 @@ neighbor_exchange_retry_due(void)
     return false;
 }
 
+void
+neighbor_exchange_snapshots_clear(struct hmap *snapshots OVS_UNUSED)
+{
+}
+
 void
 evpn_remote_vteps_clear(struct hmap *remote_vteps OVS_UNUSED)
 {
diff --git a/controller/neighbor-exchange.c b/controller/neighbor-exchange.c
index 52993170a..61092a579 100644
--- a/controller/neighbor-exchange.c
+++ b/controller/neighbor-exchange.c
@@ -52,6 +52,60 @@ static uint32_t evpn_static_entry_hash(const struct eth_addr 
*mac,
  * of other engine inputs. */
 static long long int neighbor_exchange_retry_at;
 
+struct neighbor_snapshot {
+    struct hmap_node node;
+    uint64_t link_generation;
+    enum neighbor_family family;
+    enum neighbor_interface_type type;
+    uint32_t vni;
+    struct vector learned;
+    bool seen;
+};
+
+static struct neighbor_snapshot *
+neighbor_snapshot_get(struct hmap *snapshots,
+                      const struct neighbor_interface_monitor *nim,
+                      uint64_t link_generation)
+{
+    uint32_t hash = hash_3words(nim->family, nim->type, nim->vni);
+    hash = hash_uint64_basis(link_generation, hash);
+    struct neighbor_snapshot *snapshot;
+    HMAP_FOR_EACH_WITH_HASH (snapshot, node, hash, snapshots) {
+        if (snapshot->link_generation == link_generation
+            && snapshot->family == nim->family && snapshot->type == nim->type
+            && snapshot->vni == nim->vni) {
+            return snapshot;
+        }
+    }
+
+    snapshot = xmalloc(sizeof *snapshot);
+    *snapshot = (struct neighbor_snapshot) {
+        .link_generation = link_generation,
+        .family = nim->family,
+        .type = nim->type,
+        .vni = nim->vni,
+        .learned = VECTOR_EMPTY_INITIALIZER(struct ne_nl_received_neigh),
+    };
+    hmap_insert(snapshots, &snapshot->node, hash);
+    return snapshot;
+}
+
+static void
+neighbor_snapshot_destroy(struct neighbor_snapshot *snapshot)
+{
+    vector_destroy(&snapshot->learned);
+    free(snapshot);
+}
+
+void
+neighbor_exchange_snapshots_clear(struct hmap *snapshots)
+{
+    struct neighbor_snapshot *snapshot;
+    HMAP_FOR_EACH_POP (snapshot, node, snapshots) {
+        neighbor_snapshot_destroy(snapshot);
+    }
+}
+
 void
 neighbor_exchange_run(const struct neighbor_exchange_ctx_in *n_ctx_in,
                       struct neighbor_exchange_ctx_out *n_ctx_out)
@@ -72,23 +126,35 @@ neighbor_exchange_run(const struct 
neighbor_exchange_ctx_in *n_ctx_in,
 
     bool retry = false;
     VECTOR_FOR_EACH (n_ctx_in->monitored_interfaces, nim) {
-        int32_t if_index = host_if_monitor_ifname_toindex(nim->if_name);
-        if (!if_index) {
+        struct host_if_monitor_link link =
+            host_if_monitor_get_link(nim->if_name);
+        if (!link.if_index) {
             continue;
         }
 
         struct vector received_neighbors =
             VECTOR_EMPTY_INITIALIZER(struct ne_nl_received_neigh);
         bool dump_failed;
-        int error = ne_nl_sync_neigh(nim->family, if_index, nim->type,
+        int error = ne_nl_sync_neigh(nim->family, link.if_index, nim->type,
                                     &nim->announced_neighbors,
                                     &received_neighbors, &dump_failed);
         retry |= error != 0;
 
+        struct neighbor_snapshot *snapshot =
+            neighbor_snapshot_get(n_ctx_out->snapshots, nim, link.generation);
+        snapshot->seen = true;
+        /* A successful empty dump supersedes the previous observations.
+         * Add/delete errors still leave us with a usable dump. */
+        if (!dump_failed) {
+            vector_destroy(&snapshot->learned);
+            snapshot->learned = vector_steal(&received_neighbors);
+        }
+        vector_destroy(&received_neighbors);
+
         struct ne_nl_received_neigh *ne;
         switch (nim->type) {
         case NEIGH_IFACE_BRIDGE:
-            VECTOR_FOR_EACH_PTR (&received_neighbors, ne) {
+            VECTOR_FOR_EACH_PTR (&snapshot->learned, ne) {
                 if (ne_is_valid_static_arp(ne)) {
                     if (!evpn_static_entry_find(n_ctx_out->static_arps,
                                                 ne->lladdr, ne->addr,
@@ -101,7 +167,7 @@ neighbor_exchange_run(const struct neighbor_exchange_ctx_in 
*n_ctx_in,
             }
             break;
         case NEIGH_IFACE_VXLAN:
-            VECTOR_FOR_EACH_PTR (&received_neighbors, ne) {
+            VECTOR_FOR_EACH_PTR (&snapshot->learned, ne) {
                 if (ne_is_valid_remote_vtep(ne)) {
                     uint16_t port = ne->port ? ne->port : DEFAULT_VXLAN_PORT;
                     if (!evpn_remote_vtep_find(n_ctx_out->remote_vteps,
@@ -129,8 +195,17 @@ neighbor_exchange_run(const struct 
neighbor_exchange_ctx_in *n_ctx_in,
             OVS_NOT_REACHED();
         }
 
-        vector_push(n_ctx_out->neighbor_table_watches, &if_index);
-        vector_destroy(&received_neighbors);
+        vector_push(n_ctx_out->neighbor_table_watches, &link.if_index);
+    }
+
+    struct neighbor_snapshot *snapshot;
+    HMAP_FOR_EACH_SAFE (snapshot, node, n_ctx_out->snapshots) {
+        if (!snapshot->seen) {
+            hmap_remove(n_ctx_out->snapshots, &snapshot->node);
+            neighbor_snapshot_destroy(snapshot);
+        } else {
+            snapshot->seen = false;
+        }
     }
 
     neighbor_exchange_retry_at = retry ? time_msec() + 1000 : 0;
diff --git a/controller/neighbor-exchange.h b/controller/neighbor-exchange.h
index 935477d7a..dcdb21fd9 100644
--- a/controller/neighbor-exchange.h
+++ b/controller/neighbor-exchange.h
@@ -32,6 +32,8 @@ struct neighbor_exchange_ctx_in {
 struct neighbor_exchange_ctx_out {
     /* Contains int32_t representing if_index. */
     struct vector *neighbor_table_watches;
+    /* Last usable observations for each monitored interface and family. */
+    struct hmap *snapshots;
     /* Contains 'struct evpn_remote_vtep'. */
     struct hmap *remote_vteps;
     /* Contains 'struct evpn_static_entry', remote FDB entries learned through
@@ -68,6 +70,7 @@ struct evpn_static_entry {
 void neighbor_exchange_run(const struct neighbor_exchange_ctx_in *,
                            struct neighbor_exchange_ctx_out *);
 bool neighbor_exchange_retry_due(void);
+void neighbor_exchange_snapshots_clear(struct hmap *);
 void evpn_remote_vteps_clear(struct hmap *remote_vteps);
 void evpn_remote_vtep_list(struct unixctl_conn *, int argc,
                            const char *argv[], void *data_);
diff --git a/controller/ovn-controller.c b/controller/ovn-controller.c
index 025ab98ad..bb28613bc 100644
--- a/controller/ovn-controller.c
+++ b/controller/ovn-controller.c
@@ -7059,6 +7059,7 @@ nexthop_exchange_update(struct ed_type_nexthop_exchange 
*nhe_data,
 }
 
 struct ed_type_neighbor_exchange {
+    struct hmap snapshots;
     /* Contains 'struct evpn_remote_vtep'. */
     struct hmap remote_vteps;
     /* Contains 'struct evpn_static_entry', remote FDB entries learned through
@@ -7075,6 +7076,7 @@ en_neighbor_exchange_init(struct engine_node *node 
OVS_UNUSED,
 {
     struct ed_type_neighbor_exchange *data = xmalloc(sizeof *data);
     *data = (struct ed_type_neighbor_exchange) {
+        .snapshots = HMAP_INITIALIZER(&data->snapshots),
         .remote_vteps = HMAP_INITIALIZER(&data->remote_vteps),
         .static_fdbs = HMAP_INITIALIZER(&data->static_fdbs),
         .static_arps = HMAP_INITIALIZER(&data->static_arps),
@@ -7087,6 +7089,8 @@ static void
 en_neighbor_exchange_cleanup(void *data_)
 {
     struct ed_type_neighbor_exchange *data = data_;
+    neighbor_exchange_snapshots_clear(&data->snapshots);
+    hmap_destroy(&data->snapshots);
     evpn_remote_vteps_clear(&data->remote_vteps);
     evpn_static_entries_clear(&data->static_fdbs);
     evpn_static_entries_clear(&data->static_arps);
@@ -7111,6 +7115,7 @@ en_neighbor_exchange_run(struct engine_node *node, void 
*data_)
     };
     struct neighbor_exchange_ctx_out n_ctx_out = {
         .neighbor_table_watches = &nt_notify->watches,
+        .snapshots = &data->snapshots,
         .remote_vteps = &data->remote_vteps,
         .static_fdbs = &data->static_fdbs,
         .static_arps = &data->static_arps,
diff --git a/tests/system-ovn-netlink.at b/tests/system-ovn-netlink.at
index 8a56ae83b..61eeb63a3 100644
--- a/tests/system-ovn-netlink.at
+++ b/tests/system-ovn-netlink.at
@@ -34,7 +34,40 @@ AT_CLEANUP
 
 AT_SETUP([sync netlink neighbors - retry failed dumps])
 AT_KEYWORDS([netlink-neighbors netlink-dump-error])
-check ovstest test-ovn-netlink neighbor-exchange-retry
+check ovstest test-ovn-netlink neighbor-exchange-snapshot retry
+AT_CLEANUP
+
+m4_define([NEIGHBOR_SNAPSHOT_SETUP], [
+check ip link add dump-br1 type bridge
+on_exit 'if ip link show dump-br1 >/dev/null 2>&1; then ip link del dump-br1; 
fi'
+check ip link add dump-br2 type bridge
+on_exit 'ip link del dump-br2'
+check ip link set dump-br2 up
+check ip neigh replace 192.0.2.2 lladdr 02:00:00:00:00:02 \
+    nud noarp extern_learn dev dump-br2
+])
+
+AT_SETUP([sync netlink neighbors - retain failed interface observations])
+AT_KEYWORDS([netlink-neighbors netlink-dump-error])
+NEIGHBOR_SNAPSHOT_SETUP
+check ovstest test-ovn-netlink neighbor-exchange-snapshot retention
+check ovstest test-ovn-netlink neighbor-exchange-snapshot write-error
+AT_CLEANUP
+
+AT_SETUP([sync netlink neighbors - discard observations on link replacement])
+AT_KEYWORDS([netlink-neighbors netlink-dump-error])
+NEIGHBOR_SNAPSHOT_SETUP
+if_index=$(netlink_if_index dump-br1)
+check ovstest test-ovn-netlink neighbor-exchange-snapshot replace \
+    "ip link del dump-br1 && ip link add dump-br1 index $if_index type bridge 
&& \
+    ip link set dump-br1 up"
+AT_CLEANUP
+
+AT_SETUP([sync netlink neighbors - discard observations on link rename])
+AT_KEYWORDS([netlink-neighbors netlink-dump-error])
+NEIGHBOR_SNAPSHOT_SETUP
+on_exit 'if ip link show dump-renamed >/dev/null 2>&1; then ip link del 
dump-renamed; fi'
+check ovstest test-ovn-netlink neighbor-exchange-snapshot rename
 AT_CLEANUP
 
 AT_SETUP([sync netlink neighbors - learn VXLAN VTEP neighbors])
diff --git a/tests/test-ovn-netlink.c b/tests/test-ovn-netlink.c
index 206eb1726..feecb4734 100644
--- a/tests/test-ovn-netlink.c
+++ b/tests/test-ovn-netlink.c
@@ -293,19 +293,60 @@ neighbor_script_reset(int32_t if_index, int error)
     nl_script.error = error;
 }
 
+static bool
+has_static_arp(const struct hmap *arps, uint32_t vni, uint8_t last)
+{
+    struct in6_addr ip = in6_addr_mapped_ipv4(htonl(0xc0000200 | last));
+    struct eth_addr mac = ETH_ADDR_C(02,00,00,00,00,00);
+    mac.ea[5] = last;
+    struct evpn_static_entry *entry;
+    HMAP_FOR_EACH (entry, hmap_node, arps) {
+        if (entry->vni == vni && ipv6_addr_equals(&entry->ip, &ip)
+            && eth_addr_equals(entry->mac, mac)) {
+            return true;
+        }
+    }
+    return false;
+}
+
 static void
-test_neighbor_exchange_retry(struct ovs_cmdl_context *ctx OVS_UNUSED)
+test_neighbor_exchange_snapshot(struct ovs_cmdl_context *ctx)
 {
+    bool check_retry = !strcmp(ctx->argv[1], "retry");
+    bool replace_link = !strcmp(ctx->argv[1], "replace");
+    bool rename_link = !strcmp(ctx->argv[1], "rename");
+    bool write_error = !strcmp(ctx->argv[1], "write-error");
+    ovs_assert(check_retry || replace_link || rename_link || write_error
+               || !strcmp(ctx->argv[1], "retention"));
+    ovs_assert(ctx->argc == (replace_link ? 3 : 2));
     struct neighbor_interface_monitor first = {
         .family = NEIGH_AF_INET,
-        .if_name = "lo",
-        .type = NEIGH_IFACE_LOOPBACK,
+        .if_name = "dump-br1",
+        .type = NEIGH_IFACE_BRIDGE,
+        .vni = 101,
         .announced_neighbors = HMAP_INITIALIZER(&first.announced_neighbors),
     };
+    struct neighbor_interface_monitor second = {
+        .family = NEIGH_AF_INET,
+        .if_name = "dump-br2",
+        .type = NEIGH_IFACE_BRIDGE,
+        .vni = 102,
+        .announced_neighbors = HMAP_INITIALIZER(&second.announced_neighbors),
+    };
     struct vector interfaces =
         VECTOR_EMPTY_INITIALIZER(struct neighbor_interface_monitor *);
     struct neighbor_interface_monitor *mon = &first;
     vector_push(&interfaces, &mon);
+    if (check_retry) {
+        /* The retry case only needs one dump target. */
+        ovs_strlcpy(first.if_name, "lo", sizeof first.if_name);
+        first.type = NEIGH_IFACE_LOOPBACK;
+    } else {
+        mon = &second;
+        vector_push(&interfaces, &mon);
+    }
+
+    struct hmap snapshots = HMAP_INITIALIZER(&snapshots);
     struct hmap arps = HMAP_INITIALIZER(&arps);
     struct hmap fdbs = HMAP_INITIALIZER(&fdbs);
     struct hmap vteps = HMAP_INITIALIZER(&vteps);
@@ -315,6 +356,7 @@ test_neighbor_exchange_retry(struct ovs_cmdl_context *ctx 
OVS_UNUSED)
     };
     struct neighbor_exchange_ctx_out output = {
         .neighbor_table_watches = &watches,
+        .snapshots = &snapshots,
         .static_arps = &arps,
         .static_fdbs = &fdbs,
         .remote_vteps = &vteps,
@@ -322,39 +364,133 @@ test_neighbor_exchange_retry(struct ovs_cmdl_context 
*ctx OVS_UNUSED)
 
     int32_t if_index = if_nametoindex(first.if_name);
     ovs_assert(if_index);
-    neighbor_script_reset(if_index, EIO);
+    if (check_retry) {
+        neighbor_script_reset(if_index, EIO);
+
+        long long int now = time_msec();
+        poll_wakeup_at = 0;
+        neighbor_exchange_run(&input, &output);
+        ovs_assert(nl_script.started && nl_script.finished);
+        long long int retry_at = poll_wakeup_at;
+        ovs_assert(retry_at >= now + 1000 && retry_at <= time_msec() + 1000);
+        poll_wakeup_at = 0;
+        ovs_assert(!neighbor_exchange_retry_due());
+        ovs_assert(poll_wakeup_at == retry_at);
+        long long int deadline = time_msec() + 3000;
+        while (!neighbor_exchange_retry_due() && time_msec() < deadline) {
+            poll_timer_wait_until(deadline);
+            poll_block();
+        }
+        ovs_assert(neighbor_exchange_retry_due());
 
-    long long int now = time_msec();
-    poll_wakeup_at = 0;
+        neighbor_script_reset(if_index, 0);
+        neighbor_exchange_run(&input, &output);
+        ovs_assert(nl_script.started && nl_script.finished);
+        ovs_assert(!neighbor_exchange_retry_due());
+        goto cleanup;
+    }
+
+    /* The second interface uses the real Netlink transport. */
+    neighbor_script_reset(if_index, 0);
+    neighbor_dump_add(AF_INET, if_index, NUD_NOARP, NTF_EXT_LEARNED, 0,
+                      ETH_ADDR_LEN, 1);
     neighbor_exchange_run(&input, &output);
     ovs_assert(nl_script.started && nl_script.finished);
-    long long int retry_at = poll_wakeup_at;
-    ovs_assert(retry_at >= now + 1000 && retry_at <= time_msec() + 1000);
-    poll_wakeup_at = 0;
-    ovs_assert(!neighbor_exchange_retry_due());
-    ovs_assert(poll_wakeup_at == retry_at);
-    long long int deadline = time_msec() + 3000;
-    while (!neighbor_exchange_retry_due() && time_msec() < deadline) {
-        poll_timer_wait_until(deadline);
-        poll_block();
+    ovs_assert(hmap_count(&arps) == 2
+               && has_static_arp(&arps, 101, 1)
+               && has_static_arp(&arps, 102, 2));
+
+    if (rename_link) {
+        /* Rename a down bridge to a name outside the watch set. */
+        ovs_assert(!system("ip link set dump-br1 name dump-renamed"));
+        ovs_assert(if_nametoindex("dump-renamed") == (uint32_t) if_index);
+        bool changed = host_if_monitor_run();
+        neighbor_script_reset(if_index, EIO);
+        neighbor_exchange_run(&input, &output);
+        ovs_assert(hmap_count(&arps) == 1
+                   && has_static_arp(&arps, 102, 2));
+        ovs_assert(changed && !nl_script.started
+                   && vector_len(&watches) == 1);
+        goto cleanup;
     }
-    ovs_assert(neighbor_exchange_retry_due());
 
+    if (replace_link) {
+        /* Consume the deletion and creation in a single monitor run. */
+        ovs_assert(!system(ctx->argv[2]));
+        ovs_assert(host_if_monitor_run());
+        ovs_assert(if_nametoindex(first.if_name) == (uint32_t) if_index);
+    }
+
+    if (write_error) {
+        /* A successful dump replaces the cache even when a delete fails. */
+        neighbor_script_reset(if_index, 0);
+        nl_script.transact_error = EIO;
+        neighbor_dump_add(AF_INET, if_index, NUD_NOARP, NTF_EXT_LEARNED, 0,
+                          ETH_ADDR_LEN, 3);
+        neighbor_dump_add(AF_INET, if_index, NUD_NOARP, 0, 0, ETH_ADDR_LEN, 4);
+        poll_wakeup_at = 0;
+        neighbor_exchange_run(&input, &output);
+        ovs_assert(nl_script.started && nl_script.finished);
+        ovs_assert(nl_script.deletes == 1 && !nl_script.adds);
+        ovs_assert(poll_wakeup_at > 0 && !neighbor_exchange_retry_due());
+        ovs_assert(hmap_count(&arps) == 2
+                   && has_static_arp(&arps, 101, 3)
+                   && has_static_arp(&arps, 102, 2));
+    }
+
+    /* The healthy interface still refreshes.  The partial dump must not
+     * publish its observation or write to the kernel. */
+    second.vni = 104;
+    neighbor_script_reset(if_index, EIO);
+    neighbor_dump_add(AF_INET, if_index, NUD_NOARP, NTF_EXT_LEARNED, 0,
+                      ETH_ADDR_LEN, 5);
+    neighbor_exchange_run(&input, &output);
+    ovs_assert(nl_script.started && nl_script.finished);
+    ovs_assert(!nl_script.deletes && !nl_script.adds);
+    ovs_assert(hmap_count(&arps) == (replace_link ? 1 : 2)
+               && has_static_arp(&arps, 104, 2));
+    if (!replace_link) {
+        ovs_assert(has_static_arp(&arps, 101, write_error ? 3 : 1));
+    }
+    ovs_assert(vector_len(&watches) == 2);
+
+    /* Scope changes discard the old snapshot even during failure. */
+    first.vni = 103;
+    neighbor_script_reset(if_index, EIO);
+    neighbor_exchange_run(&input, &output);
+    ovs_assert(nl_script.started && nl_script.finished);
+    ovs_assert(hmap_count(&arps) == 1
+               && has_static_arp(&arps, 104, 2));
+
+    /* Recovery publishes fresh data.  A complete empty dump withdraws it. */
+    first.vni = 101;
+    neighbor_script_reset(if_index, 0);
+    neighbor_dump_add(AF_INET, if_index, NUD_NOARP, NTF_EXT_LEARNED, 0,
+                      ETH_ADDR_LEN, 3);
+    neighbor_exchange_run(&input, &output);
+    ovs_assert(hmap_count(&arps) == 2
+               && has_static_arp(&arps, 101, 3)
+               && has_static_arp(&arps, 104, 2));
     neighbor_script_reset(if_index, 0);
     neighbor_exchange_run(&input, &output);
     ovs_assert(nl_script.started && nl_script.finished);
+    ovs_assert(hmap_count(&arps) == 1
+               && has_static_arp(&arps, 104, 2));
     ovs_assert(!neighbor_exchange_retry_due());
 
+cleanup:
     neighbor_script_reset(0, 0);
     vector_clear(&interfaces);
     neighbor_exchange_run(&input, &output);
-    ovs_assert(vector_is_empty(&watches));
+    ovs_assert(hmap_is_empty(&snapshots) && vector_is_empty(&watches));
+    hmap_destroy(&snapshots);
     hmap_destroy(&arps);
     hmap_destroy(&fdbs);
     hmap_destroy(&vteps);
     vector_destroy(&watches);
     vector_destroy(&interfaces);
     hmap_destroy(&first.announced_neighbors);
+    hmap_destroy(&second.announced_neighbors);
 }
 
 /* A failed dump must not be reported as successful synchronization. */
@@ -545,7 +681,7 @@ test_host_if_monitor(struct ovs_cmdl_context *ctx)
         exit(rc);
     }
     ovs_assert(host_if_monitor_run() == expect_notify);
-    printf("%"PRId32"\n", host_if_monitor_ifname_toindex(if_name));
+    printf("%"PRId32"\n", host_if_monitor_get_link(if_name).if_index);
     sset_destroy(&if_names);
 }
 
@@ -896,8 +1032,8 @@ test_ovn_netlink(int argc, char *argv[])
 {
     set_program_name(argv[0]);
     static const struct ovs_cmdl_command commands[] = {
-        {"neighbor-exchange-retry", NULL, 0, 0,
-         test_neighbor_exchange_retry, OVS_RO},
+        {"neighbor-exchange-snapshot", NULL, 1, 2,
+         test_neighbor_exchange_snapshot, OVS_RO},
         {"neighbor-dump-script", NULL, 1, 1,
          test_neighbor_dump_script, OVS_RO},
         {"neighbor-sync-error", NULL, 0, 0, test_neighbor_sync_error, OVS_RO},
_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to