Neighbor synchronization can reconcile kernel entries from an
incomplete Netlink dump. Stage observations and pending changes until
the dump finishes. Abort reconciliation on transport errors or dump
interruption and skip unsupported or malformed records with
rate-limited warnings.
Replace immediate self-retries with a one-second timer. Other
controller events can still trigger synchronization sooner.
Fixes: ad3a0a55b551 ("controller: Support monitoring/updating neighbor entries
through Netlink.")
Assisted-by: GPT-6.1 Sol, Codex
Signed-off-by: Guillaume Boutry <[email protected]>
---
controller/neighbor-exchange-netlink.c | 114 +++++---
controller/neighbor-exchange-netlink.h | 2 +-
controller/neighbor-exchange-stub.c | 6 +-
controller/neighbor-exchange.c | 56 ++--
controller/neighbor-exchange.h | 2 +-
controller/ovn-controller.c | 7 +-
tests/automake.mk | 10 +
tests/system-ovn-netlink.at | 37 +++
tests/test-ovn-netlink.c | 366 ++++++++++++++++++++++++-
9 files changed, 522 insertions(+), 78 deletions(-)
diff --git a/controller/neighbor-exchange-netlink.c
b/controller/neighbor-exchange-netlink.c
index 942dee147..fc33c68ff 100644
--- a/controller/neighbor-exchange-netlink.c
+++ b/controller/neighbor-exchange-netlink.c
@@ -14,6 +14,7 @@
*/
#include <config.h>
+#include <errno.h>
#include <stdbool.h>
#include <linux/if_ether.h>
#include <linux/rtnetlink.h>
@@ -44,14 +45,8 @@ VLOG_DEFINE_THIS_MODULE(neighbor_exchange_netlink);
#endif
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
+static struct vlog_rate_limit record_rl = VLOG_RATE_LIMIT_INIT(5, 20);
-/* Inspired from route_table_dump_one_table() in OVS. */
-typedef void ne_table_handle_msg_callback(const struct ne_table_msg *,
- void *aux);
-static void ne_table_dump_one_ifindex(unsigned char address_family,
- int32_t if_index,
- ne_table_handle_msg_callback *,
- void *aux);
struct ne_msg_handle_data {
/* Stores 'struct advertise_neighbor_entry'. */
struct hmapx *neighbors_to_advertise;
@@ -69,7 +64,10 @@ struct ne_msg_handle_data {
enum neighbor_interface_type type;
};
-static void handle_ne_msg(const struct ne_table_msg *, void *data);
+static int ne_table_dump_one_ifindex(uint8_t family, int32_t if_index,
+ struct ne_msg_handle_data *);
+static void handle_ne_msg(const struct ne_nl_received_neigh *,
+ struct ne_msg_handle_data *);
static int ne_table_parse__(struct ofpbuf *, size_t ofs,
const struct nlmsghdr *,
@@ -91,15 +89,20 @@ static int ne_nl_del_neigh(int32_t if_index, uint8_t family,
* (struct ne_nl_received_neigh) that exist in the table associated to
* 'if_index'.
*
+ * Sets 'dump_failed' when the dump is incomplete. In that case, leaves
+ * 'learned_neighbors' and the kernel table unchanged.
+ *
* Returns 0 on success, errno on failure. */
int
ne_nl_sync_neigh(uint8_t family, int32_t if_index,
enum neighbor_interface_type type,
const struct hmap *neighbors,
- struct vector *learned_neighbors)
+ struct vector *learned_neighbors, bool *dump_failed)
{
struct hmapx neighbors_to_advertise =
HMAPX_INITIALIZER(&neighbors_to_advertise);
+ struct vector learned =
+ VECTOR_EMPTY_INITIALIZER(struct ne_nl_received_neigh);
struct vector stale_neighbors =
VECTOR_EMPTY_INITIALIZER(struct ne_nl_received_neigh);
struct advertise_neighbor_entry *an;
@@ -111,12 +114,22 @@ ne_nl_sync_neigh(uint8_t family, int32_t if_index,
struct ne_msg_handle_data data = {
.neighbors_to_advertise = &neighbors_to_advertise,
- .learned_neighbors = learned_neighbors,
+ .learned_neighbors = learned_neighbors ? &learned : NULL,
.stale_neighbors = &stale_neighbors,
.neighbors = neighbors,
.type = type,
};
- ne_table_dump_one_ifindex(family, if_index, handle_ne_msg, &data);
+ ret = ne_table_dump_one_ifindex(family, if_index, &data);
+ *dump_failed = ret != 0;
+ if (ret) {
+ VLOG_WARN_RL(&rl, "Neighbor dump failed for ifindex=%"PRId32
+ " family=%u: %s", if_index, family, ovs_strerror(ret));
+ goto out;
+ }
+ if (!vector_is_empty(&learned)) {
+ vector_push_array(learned_neighbors, vector_get_array(&learned),
+ vector_len(&learned));
+ }
/* Add any remaining neighbors in the neighbors_to_advertise hmapx to the
* system table. */
@@ -168,6 +181,8 @@ ne_nl_sync_neigh(uint8_t family, int32_t if_index,
}
}
+out:
+ vector_destroy(&learned);
hmapx_destroy(&neighbors_to_advertise);
vector_destroy(&stale_neighbors);
return ret;
@@ -225,15 +240,15 @@ ne_is_valid_static_arp(struct ne_nl_received_neigh *ne)
ne->state & NUD_NOARP && ne->flags & NTF_EXT_LEARNED;
}
-static void
-ne_table_dump_one_ifindex(unsigned char address_family, int32_t if_index,
- ne_table_handle_msg_callback *handle_msg_cb,
- void *aux)
+static int
+ne_table_dump_one_ifindex(uint8_t address_family, int32_t if_index,
+ struct ne_msg_handle_data *data)
{
uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
struct ofpbuf request, reply, buf;
struct ndmsg *rq_msg;
struct nl_dump dump;
+ bool interrupted = false;
uint8_t request_stub[NETNL_REQ_BUFFER_SIZE];
ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
@@ -250,24 +265,45 @@ ne_table_dump_one_ifindex(unsigned char address_family,
int32_t if_index,
ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
while (nl_dump_next(&dump, &reply, &buf)) {
- struct ne_table_msg msg;
+ const struct nlmsghdr *nlmsghdr = ofpbuf_at(&reply, 0, NLMSG_HDRLEN);
+ if (nlmsghdr && (nlmsghdr->nlmsg_flags & NLM_F_DUMP_INTR)) {
+ interrupted = true;
+ }
+ /* Drain this buffer too. It may already contain NLMSG_DONE. */
+ if (interrupted) {
+ continue;
+ }
- if (ne_table_parse(&reply, &msg)) {
- struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(&reply);
+ const struct ndmsg *nd = ofpbuf_at(&reply, NLMSG_HDRLEN, sizeof *nd);
+ if (!nlmsghdr || !nd) {
+ VLOG_WARN_RL(&record_rl,
+ "Skipping truncated neighbor dump record");
+ continue;
+ }
- /* Older kernels do not support filtering. If so, we
- * filter ourselves. */
- if (!(nlmsghdr->nlmsg_flags & NLM_F_DUMP_FILTERED)) {
- if (msg.nd.family != address_family
- || (if_index && msg.nd.if_index != if_index)) {
- continue;
- }
- }
- handle_msg_cb(&msg, aux);
+ /* Older kernels may return an unfiltered dump. */
+ if (!(nlmsghdr->nlmsg_flags & NLM_F_DUMP_FILTERED)
+ && (nd->ndm_family != address_family
+ || (if_index && nd->ndm_ifindex != if_index))) {
+ continue;
+ }
+
+ struct ne_table_msg msg;
+ if (!ne_table_parse(&reply, &msg)) {
+ VLOG_WARN_RL(&record_rl,
+ "Skipping unsupported or malformed neighbor record "
+ "for ifindex=%"PRId32" family=%u",
+ nd->ndm_ifindex, nd->ndm_family);
+ continue;
}
+ handle_ne_msg(&msg.nd, data);
}
ofpbuf_uninit(&buf);
- nl_dump_done(&dump);
+ int error = nl_dump_done(&dump);
+ if (!error && (interrupted || (dump.nl_flags & NLM_F_DUMP_INTR))) {
+ error = EINTR;
+ }
+ return error;
}
static int
@@ -376,42 +412,40 @@ ne_table_parse__(struct ofpbuf *buf, size_t ofs, const
struct nlmsghdr *nlmsg,
}
static void
-handle_ne_msg(const struct ne_table_msg *msg, void *data)
+handle_ne_msg(const struct ne_nl_received_neigh *nd,
+ struct ne_msg_handle_data *data)
{
- struct ne_msg_handle_data *handle_data = data;
- const struct ne_nl_received_neigh *nd = &msg->nd;
-
/* OVN only manages VLAN 0 entries. */
if (nd->vlan) {
return;
}
- if (!ne_is_ovn_owned(nd, handle_data->type)) {
- if (!handle_data->learned_neighbors) {
+ if (!ne_is_ovn_owned(nd, data->type)) {
+ if (!data->learned_neighbors) {
return;
}
/* Learn the non-OVN entry. */
- vector_push(handle_data->learned_neighbors, nd);
+ vector_push(data->learned_neighbors, nd);
return;
}
/* This neighbor was presumably added by OVN, see if it's still valid.
* OVN only adds neighbors with port set to 0, all others can be
* removed. */
- if (!nd->port && handle_data->neighbors_to_advertise) {
+ if (!nd->port && data->neighbors_to_advertise) {
struct advertise_neighbor_entry *an =
- advertise_neigh_find(handle_data->neighbors, nd->lladdr,
+ advertise_neigh_find(data->neighbors, nd->lladdr,
&nd->addr);
if (an) {
- hmapx_find_and_delete(handle_data->neighbors_to_advertise, an);
+ hmapx_find_and_delete(data->neighbors_to_advertise, an);
return;
}
}
/* Store the entry for deletion. */
- if (handle_data->stale_neighbors) {
- vector_push(handle_data->stale_neighbors, nd);
+ if (data->stale_neighbors) {
+ vector_push(data->stale_neighbors, nd);
}
}
diff --git a/controller/neighbor-exchange-netlink.h
b/controller/neighbor-exchange-netlink.h
index 0b2dd4199..f25cf4096 100644
--- a/controller/neighbor-exchange-netlink.h
+++ b/controller/neighbor-exchange-netlink.h
@@ -54,7 +54,7 @@ struct ne_table_msg {
int ne_nl_sync_neigh(uint8_t family, int32_t if_index,
enum neighbor_interface_type type,
const struct hmap *neighbors,
- struct vector *learned_neighbors);
+ struct vector *learned_neighbors, bool *dump_failed);
bool ne_is_ovn_owned(const struct ne_nl_received_neigh *nd,
enum neighbor_interface_type type);
diff --git a/controller/neighbor-exchange-stub.c
b/controller/neighbor-exchange-stub.c
index a1c89ed2b..a5420fcff 100644
--- a/controller/neighbor-exchange-stub.c
+++ b/controller/neighbor-exchange-stub.c
@@ -23,10 +23,10 @@ neighbor_exchange_run(const struct neighbor_exchange_ctx_in
*ctx_in OVS_UNUSED,
{
}
-int
-neighbor_exchange_status_run(void)
+bool
+neighbor_exchange_retry_due(void)
{
- return 0;
+ return false;
}
void
diff --git a/controller/neighbor-exchange.c b/controller/neighbor-exchange.c
index df54969fb..52993170a 100644
--- a/controller/neighbor-exchange.c
+++ b/controller/neighbor-exchange.c
@@ -25,6 +25,7 @@
#include "openvswitch/vlog.h"
#include "ovn-util.h"
#include "packets.h"
+#include "timeval.h"
#include "vec.h"
#include "unixctl.h"
@@ -47,23 +48,9 @@ static uint32_t evpn_static_entry_hash(const struct eth_addr
*mac,
const struct in6_addr *ip,
uint32_t vni, uint32_t nh_id);
-/* Last neighbor_exchange netlink operation. */
-static int neighbor_exchange_nl_status;
-
-#define CLEAR_NEIGHBOR_EXCHANGE_NL_STATUS() \
- do { \
- neighbor_exchange_nl_status = 0; \
- } while (0)
-
-#define SET_NEIGHBOR_EXCHANGE_NL_STATUS(error) \
- do { \
- if (!neighbor_exchange_nl_status) { \
- neighbor_exchange_nl_status = (error); \
- if (error) { \
- poll_immediate_wake(); \
- } \
- } \
- } while (0)
+/* Retry failed Netlink operations at most once per second in the absence
+ * of other engine inputs. */
+static long long int neighbor_exchange_retry_at;
void
neighbor_exchange_run(const struct neighbor_exchange_ctx_in *n_ctx_in,
@@ -78,20 +65,25 @@ neighbor_exchange_run(const struct neighbor_exchange_ctx_in
*n_ctx_in,
host_if_monitor_update_watches(&if_names);
sset_destroy(&if_names);
- CLEAR_NEIGHBOR_EXCHANGE_NL_STATUS();
+ evpn_remote_vteps_clear(n_ctx_out->remote_vteps);
+ evpn_static_entries_clear(n_ctx_out->static_fdbs);
+ evpn_static_entries_clear(n_ctx_out->static_arps);
+ vector_clear(n_ctx_out->neighbor_table_watches);
+
+ 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) {
continue;
}
struct vector received_neighbors =
VECTOR_EMPTY_INITIALIZER(struct ne_nl_received_neigh);
- SET_NEIGHBOR_EXCHANGE_NL_STATUS(
- ne_nl_sync_neigh(nim->family, if_index, nim->type,
- &nim->announced_neighbors, &received_neighbors)
- );
+ bool dump_failed;
+ int error = ne_nl_sync_neigh(nim->family, if_index, nim->type,
+ &nim->announced_neighbors,
+ &received_neighbors, &dump_failed);
+ retry |= error != 0;
struct ne_nl_received_neigh *ne;
switch (nim->type) {
@@ -140,12 +132,24 @@ neighbor_exchange_run(const struct
neighbor_exchange_ctx_in *n_ctx_in,
vector_push(n_ctx_out->neighbor_table_watches, &if_index);
vector_destroy(&received_neighbors);
}
+
+ neighbor_exchange_retry_at = retry ? time_msec() + 1000 : 0;
+ if (retry) {
+ poll_timer_wait_until(neighbor_exchange_retry_at);
+ }
}
-int
-neighbor_exchange_status_run(void)
+bool
+neighbor_exchange_retry_due(void)
{
- return neighbor_exchange_nl_status;
+ if (!neighbor_exchange_retry_at) {
+ return false;
+ }
+ if (time_msec() >= neighbor_exchange_retry_at) {
+ return true;
+ }
+ poll_timer_wait_until(neighbor_exchange_retry_at);
+ return false;
}
void
diff --git a/controller/neighbor-exchange.h b/controller/neighbor-exchange.h
index 456ff23df..935477d7a 100644
--- a/controller/neighbor-exchange.h
+++ b/controller/neighbor-exchange.h
@@ -67,7 +67,7 @@ struct evpn_static_entry {
void neighbor_exchange_run(const struct neighbor_exchange_ctx_in *,
struct neighbor_exchange_ctx_out *);
-int neighbor_exchange_status_run(void);
+bool neighbor_exchange_retry_due(void);
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 13fc370f1..025ab98ad 100644
--- a/controller/ovn-controller.c
+++ b/controller/ovn-controller.c
@@ -7106,11 +7106,6 @@ en_neighbor_exchange_run(struct engine_node *node, void
*data_)
struct ed_type_nexthop_exchange *nhe_data =
engine_get_input_data("nexthop_exchange", node);
- evpn_remote_vteps_clear(&data->remote_vteps);
- evpn_static_entries_clear(&data->static_fdbs);
- evpn_static_entries_clear(&data->static_arps);
- vector_clear(&nt_notify->watches);
-
struct neighbor_exchange_ctx_in n_ctx_in = {
.monitored_interfaces = &neighbor_data->monitored_interfaces,
};
@@ -8925,7 +8920,7 @@ main(int argc, char *argv[])
struct ed_type_neighbor_exchange_status *neigh_res =
engine_get_internal_data(&en_neighbor_exchange_status);
neigh_res->netlink_trigger_run =
- !!neighbor_exchange_status_run();
+ neighbor_exchange_retry_due();
stopwatch_start(CONTROLLER_LOOP_STOPWATCH_NAME,
time_msec());
diff --git a/tests/automake.mk b/tests/automake.mk
index 8f7a8c4fa..8388b218b 100644
--- a/tests/automake.mk
+++ b/tests/automake.mk
@@ -291,9 +291,19 @@ tests_ovstest_SOURCES = \
northd/test-ipam.c
if HAVE_NETLINK
+# Deterministic dump failures exercise the production synchronization path.
+# Only ovstest uses these wrappers. They observe scheduling and inject faults.
+tests_ovstest_LDFLAGS = $(AM_LDFLAGS) \
+ -Wl,--wrap=nl_dump_start \
+ -Wl,--wrap=nl_dump_next \
+ -Wl,--wrap=nl_dump_done \
+ -Wl,--wrap=nl_transact \
+ -Wl,--wrap=poll_timer_wait_until_at
+
tests_ovstest_SOURCES += \
controller/host-if-monitor.c \
controller/host-if-monitor.h \
+ controller/neighbor-exchange.c \
controller/neighbor-exchange-netlink.c \
controller/neighbor-exchange-netlink.h \
controller/nexthop-exchange.c \
diff --git a/tests/system-ovn-netlink.at b/tests/system-ovn-netlink.at
index d47982b6e..8a56ae83b 100644
--- a/tests/system-ovn-netlink.at
+++ b/tests/system-ovn-netlink.at
@@ -1,5 +1,42 @@
AT_BANNER([system-ovn-netlink])
+AT_SETUP([sync netlink neighbors - failed dump])
+AT_KEYWORDS([netlink-neighbors netlink-dump-error])
+AT_CHECK([ovstest test-ovn-netlink neighbor-sync-error], [0], [], [ignore])
+AT_CLEANUP
+
+AT_SETUP([sync netlink neighbors - filter unrelated dump records])
+AT_KEYWORDS([netlink-neighbors netlink-dump-error])
+check ovstest test-ovn-netlink neighbor-dump-script other-interface
+check ovstest test-ovn-netlink neighbor-dump-script other-family
+AT_CLEANUP
+
+AT_SETUP([sync netlink neighbors - malformed dump records])
+AT_KEYWORDS([netlink-neighbors netlink-dump-error])
+check ovstest test-ovn-netlink neighbor-dump-script malformed
+check ovstest test-ovn-netlink neighbor-dump-script filtered-malformed
+check ovstest test-ovn-netlink neighbor-dump-script truncated
+AT_CLEANUP
+
+AT_SETUP([sync netlink neighbors - partial dump rollback])
+AT_KEYWORDS([netlink-neighbors netlink-dump-error])
+check ovstest test-ovn-netlink neighbor-dump-script late-error
+check ovstest test-ovn-netlink neighbor-dump-script interrupted
+check ovstest test-ovn-netlink neighbor-dump-script record-interrupted
+check ovstest test-ovn-netlink neighbor-dump-script parse-and-dump-error
+AT_CLEANUP
+
+AT_SETUP([sync netlink neighbors - complete dump publication])
+AT_KEYWORDS([netlink-neighbors netlink-dump-error])
+check ovstest test-ovn-netlink neighbor-dump-script complete
+check ovstest test-ovn-netlink neighbor-dump-script write-error
+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
+AT_CLEANUP
+
AT_SETUP([sync netlink neighbors - learn VXLAN VTEP neighbors])
AT_KEYWORDS([netlink-neighbors])
diff --git a/tests/test-ovn-netlink.c b/tests/test-ovn-netlink.c
index 4e5880b10..206eb1726 100644
--- a/tests/test-ovn-netlink.c
+++ b/tests/test-ovn-netlink.c
@@ -14,14 +14,23 @@
*/
#include <config.h>
+#include <errno.h>
+#include <linux/rtnetlink.h>
+#include <net/if.h>
+#include <sys/resource.h>
+#include "netlink.h"
+#include "netlink-socket.h"
#include "openvswitch/hmap.h"
+#include "openvswitch/poll-loop.h"
#include "route-table.h"
#include "packets.h"
#include "tests/ovstest.h"
#include "tests/test-utils.h"
+#include "timeval.h"
#include "controller/host-if-monitor.h"
+#include "controller/neighbor-exchange.h"
#include "controller/neighbor-exchange-netlink.h"
#include "controller/nexthop-exchange.h"
#include "controller/ovn-netlink-notifier.h"
@@ -29,6 +38,354 @@
#include "controller/route.h"
#include "controller/route-exchange-netlink.h"
+/* Script the dump transport and neighbor transactions. The normal
+ * ovstest commands delegate to the real Netlink functions. Production
+ * binaries do not use these linker wrappers. */
+static struct {
+ bool active;
+ bool started;
+ bool current;
+ int32_t if_index;
+ bool finished;
+ struct ofpbuf messages[4];
+ uint16_t request_type;
+ uint16_t done_flags;
+ size_t n_messages;
+ size_t next;
+ int error;
+ int transact_error;
+ unsigned int adds;
+ unsigned int deletes;
+} nl_script;
+
+static _Thread_local long long int poll_wakeup_at;
+
+void __real_poll_timer_wait_until_at(long long int, const char *);
+void __wrap_poll_timer_wait_until_at(long long int, const char *);
+
+void
+__wrap_poll_timer_wait_until_at(long long int when, const char *where)
+{
+ poll_wakeup_at = when;
+ __real_poll_timer_wait_until_at(when, where);
+}
+
+void __real_nl_dump_start(struct nl_dump *, int, const struct ofpbuf *);
+bool __real_nl_dump_next(struct nl_dump *, struct ofpbuf *, struct ofpbuf *);
+int __real_nl_dump_done(struct nl_dump *);
+int __real_nl_transact(int, const struct ofpbuf *, struct ofpbuf **);
+void __wrap_nl_dump_start(struct nl_dump *, int, const struct ofpbuf *);
+bool __wrap_nl_dump_next(struct nl_dump *, struct ofpbuf *, struct ofpbuf *);
+int __wrap_nl_dump_done(struct nl_dump *);
+int __wrap_nl_transact(int, const struct ofpbuf *, struct ofpbuf **);
+
+void
+__wrap_nl_dump_start(struct nl_dump *dump, int protocol,
+ const struct ofpbuf *request)
+{
+ nl_script.current = nl_script.active;
+ if (nl_script.current && nl_script.if_index) {
+ const struct nlattr *attr = nl_attr_find(
+ request, NLMSG_HDRLEN + sizeof(struct ndmsg), NDA_IFINDEX);
+ nl_script.current = attr && (nl_attr_get_u32(attr)
+ == (uint32_t) nl_script.if_index);
+ }
+ if (!nl_script.current) {
+ __real_nl_dump_start(dump, protocol, request);
+ return;
+ }
+ ovs_assert(protocol == NETLINK_ROUTE);
+ uint16_t type = nl_script.request_type ? nl_script.request_type
+ : RTM_GETNEIGH;
+ ovs_assert(nl_msg_nlmsghdr(request)->nlmsg_type == type);
+ dump->nl_flags = 0;
+ ovs_assert(!nl_script.started);
+ nl_script.started = true;
+}
+
+bool
+__wrap_nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply,
+ struct ofpbuf *buf)
+{
+ if (!nl_script.current) {
+ return __real_nl_dump_next(dump, reply, buf);
+ }
+ ovs_assert(nl_script.started && !nl_script.finished);
+ if (nl_script.next == nl_script.n_messages) {
+ return false;
+ }
+ *reply = nl_script.messages[nl_script.next++];
+ return true;
+}
+
+int
+__wrap_nl_dump_done(struct nl_dump *dump)
+{
+ if (!nl_script.current) {
+ return __real_nl_dump_done(dump);
+ }
+ ovs_assert(nl_script.started && !nl_script.finished);
+ nl_script.finished = true;
+ dump->nl_flags = nl_script.done_flags;
+ return nl_script.error;
+}
+
+int
+__wrap_nl_transact(int protocol, const struct ofpbuf *request,
+ struct ofpbuf **reply)
+{
+ if (!nl_script.current) {
+ return __real_nl_transact(protocol, request, reply);
+ }
+ ovs_assert(protocol == NETLINK_ROUTE && !reply);
+ const struct ndmsg *nd = ofpbuf_at(request, NLMSG_HDRLEN, sizeof *nd);
+ ovs_assert(nd && nd->ndm_family == AF_INET);
+ ovs_assert(nd->ndm_ifindex == (nl_script.if_index ? nl_script.if_index
+ : 42));
+ switch (nl_msg_nlmsghdr(request)->nlmsg_type) {
+ case RTM_NEWNEIGH:
+ nl_script.adds++;
+ break;
+ case RTM_DELNEIGH:
+ nl_script.deletes++;
+ break;
+ default:
+ OVS_NOT_REACHED();
+ }
+ return nl_script.transact_error;
+}
+
+static struct ofpbuf *
+neighbor_dump_message(void)
+{
+ ovs_assert(nl_script.n_messages < ARRAY_SIZE(nl_script.messages));
+ struct ofpbuf *message =
+ &nl_script.messages[nl_script.n_messages++];
+ ofpbuf_init(message, 128);
+ nl_msg_put_nlmsghdr(message, 0, RTM_NEWNEIGH, 0);
+ return message;
+}
+
+static void
+neighbor_dump_add(uint8_t family, int32_t if_index, uint16_t state,
+ uint8_t nd_flags, uint16_t nl_flags, size_t lladdr_len,
+ uint8_t last)
+{
+ struct ofpbuf *message = neighbor_dump_message();
+ struct ndmsg *nd = ofpbuf_put_zeros(message, sizeof *nd);
+ nd->ndm_family = family;
+ nd->ndm_ifindex = if_index;
+ nd->ndm_state = state;
+ nd->ndm_flags = nd_flags;
+ ovs_be32 ip = htonl(0xc0000200 | last);
+ if (family == AF_INET) {
+ nl_msg_put_be32(message, NDA_DST, ip);
+ } else {
+ struct in6_addr ip6 = in6_addr_mapped_ipv4(ip);
+ nl_msg_put_in6_addr(message, NDA_DST, &ip6);
+ }
+ uint8_t mac[20] = { 0x02, 0, 0, 0, 0, last };
+ ovs_assert(lladdr_len <= sizeof mac);
+ nl_msg_put_unspec(message, NDA_LLADDR, mac, lladdr_len);
+ struct nlmsghdr *header = nl_msg_nlmsghdr(message);
+ header->nlmsg_flags = nl_flags;
+ header->nlmsg_len = message->size;
+}
+
+static void
+test_neighbor_dump_script(struct ovs_cmdl_context *ctx)
+{
+ const char *scenario = ctx->argv[1];
+ bool complete = !strcmp(scenario, "complete");
+ bool write_error = !strcmp(scenario, "write-error");
+ bool other_interface = !strcmp(scenario, "other-interface");
+ bool other_family = !strcmp(scenario, "other-family");
+ bool malformed = !strcmp(scenario, "malformed");
+ bool filtered = !strcmp(scenario, "filtered-malformed");
+ bool truncated = !strcmp(scenario, "truncated");
+ bool late_error = !strcmp(scenario, "late-error");
+ bool both_errors = !strcmp(scenario, "parse-and-dump-error");
+ bool interrupted = !strcmp(scenario, "interrupted");
+ bool record_interrupted = !strcmp(scenario, "record-interrupted");
+ ovs_assert(complete || write_error || other_interface || other_family
+ || malformed || filtered || truncated || late_error
+ || interrupted || record_interrupted || both_errors);
+
+ struct hmap neighbors = HMAP_INITIALIZER(&neighbors);
+ struct advertise_neighbor_entry desired = {
+ .lladdr = ETH_ADDR_C(02,00,00,00,00,03),
+ .addr = in6_addr_mapped_ipv4(htonl(0xc0000203)),
+ };
+ hmap_insert(&neighbors, &desired.node,
+ advertise_neigh_hash(&desired.lladdr, &desired.addr));
+ struct vector learned =
+ VECTOR_EMPTY_INITIALIZER(struct ne_nl_received_neigh);
+ struct ne_nl_received_neigh sentinel = { .if_index = 17 };
+ vector_push(&learned, &sentinel);
+
+ memset(&nl_script, 0, sizeof nl_script);
+ nl_script.active = true;
+ /* A learned row and an obsolete OVN-owned row precede the fault. A
+ * successful sync must publish the former, add the desired row and
+ * delete the latter; a failed dump must do none of those operations. */
+ neighbor_dump_add(AF_INET, 42, NUD_REACHABLE, 0, 0, ETH_ADDR_LEN, 1);
+ neighbor_dump_add(AF_INET, 42, NUD_NOARP, 0, 0, ETH_ADDR_LEN, 2);
+ if (other_interface || other_family || malformed || filtered
+ || both_errors) {
+ neighbor_dump_add(other_family ? AF_INET6 : AF_INET,
+ other_interface ? 43 : 42, NUD_REACHABLE, 0,
+ filtered ? NLM_F_DUMP_FILTERED : 0, 20, 4);
+ } else if (truncated) {
+ struct ofpbuf *message = neighbor_dump_message();
+ ofpbuf_put_zeros(message, sizeof(struct ndmsg) - 1);
+ nl_msg_nlmsghdr(message)->nlmsg_len = message->size;
+ } else if (record_interrupted) {
+ /* Even an unrelated record marks the whole dump interrupted. */
+ neighbor_dump_add(AF_INET, 43, NUD_REACHABLE, 0, NLM_F_DUMP_INTR,
+ ETH_ADDR_LEN, 4);
+ neighbor_dump_add(AF_INET, 42, NUD_REACHABLE, 0, 0, ETH_ADDR_LEN, 5);
+ }
+ nl_script.error = late_error || both_errors ? EINTR : 0;
+ nl_script.done_flags = interrupted ? NLM_F_DUMP_INTR : 0;
+
+ nl_script.transact_error = write_error ? EIO : 0;
+
+ bool dump_failed;
+ int error = ne_nl_sync_neigh(AF_INET, 42, NEIGH_IFACE_UNSPECIFIED,
+ &neighbors, &learned, &dump_failed);
+ ovs_assert(nl_script.started && nl_script.finished);
+ ovs_assert(nl_script.next == nl_script.n_messages);
+ nl_script.active = false;
+ bool success = !(late_error || interrupted || record_interrupted
+ || both_errors);
+ ovs_assert(error == (success ? nl_script.transact_error : EINTR));
+ ovs_assert(dump_failed == !success);
+ ovs_assert(nl_script.adds == (success ? 1 : 0));
+ ovs_assert(nl_script.deletes == (success ? 1 : 0));
+ ovs_assert(hmap_count(&neighbors) == 1);
+ ovs_assert(learned.len == (success ? 2 : 1));
+ const struct ne_nl_received_neigh *rows = learned.buffer;
+ ovs_assert(!memcmp(&rows[0], &sentinel, sizeof sentinel));
+ if (success) {
+ struct eth_addr mac = ETH_ADDR_C(02,00,00,00,00,01);
+ struct in6_addr ip = in6_addr_mapped_ipv4(htonl(0xc0000201));
+ ovs_assert(rows[1].if_index == 42 && rows[1].family == AF_INET);
+ ovs_assert(rows[1].state == NUD_REACHABLE);
+ ovs_assert(eth_addr_equals(rows[1].lladdr, mac));
+ ovs_assert(ipv6_addr_equals(&rows[1].addr, &ip));
+ }
+ for (size_t i = 0; i < nl_script.n_messages; i++) {
+ ofpbuf_uninit(&nl_script.messages[i]);
+ }
+ vector_destroy(&learned);
+ hmap_destroy(&neighbors);
+}
+
+static void
+neighbor_script_reset(int32_t if_index, int error)
+{
+ for (size_t i = 0; i < nl_script.n_messages; i++) {
+ ofpbuf_uninit(&nl_script.messages[i]);
+ }
+ memset(&nl_script, 0, sizeof nl_script);
+ nl_script.active = if_index != 0;
+ nl_script.if_index = if_index;
+ nl_script.error = error;
+}
+
+static void
+test_neighbor_exchange_retry(struct ovs_cmdl_context *ctx OVS_UNUSED)
+{
+ struct neighbor_interface_monitor first = {
+ .family = NEIGH_AF_INET,
+ .if_name = "lo",
+ .type = NEIGH_IFACE_LOOPBACK,
+ .announced_neighbors = HMAP_INITIALIZER(&first.announced_neighbors),
+ };
+ struct vector interfaces =
+ VECTOR_EMPTY_INITIALIZER(struct neighbor_interface_monitor *);
+ struct neighbor_interface_monitor *mon = &first;
+ vector_push(&interfaces, &mon);
+ struct hmap arps = HMAP_INITIALIZER(&arps);
+ struct hmap fdbs = HMAP_INITIALIZER(&fdbs);
+ struct hmap vteps = HMAP_INITIALIZER(&vteps);
+ struct vector watches = VECTOR_EMPTY_INITIALIZER(int32_t);
+ const struct neighbor_exchange_ctx_in input = {
+ .monitored_interfaces = &interfaces,
+ };
+ struct neighbor_exchange_ctx_out output = {
+ .neighbor_table_watches = &watches,
+ .static_arps = &arps,
+ .static_fdbs = &fdbs,
+ .remote_vteps = &vteps,
+ };
+
+ int32_t if_index = if_nametoindex(first.if_name);
+ ovs_assert(if_index);
+ 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());
+
+ 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());
+
+ neighbor_script_reset(0, 0);
+ vector_clear(&interfaces);
+ neighbor_exchange_run(&input, &output);
+ ovs_assert(vector_is_empty(&watches));
+ hmap_destroy(&arps);
+ hmap_destroy(&fdbs);
+ hmap_destroy(&vteps);
+ vector_destroy(&watches);
+ vector_destroy(&interfaces);
+ hmap_destroy(&first.announced_neighbors);
+}
+
+/* A failed dump must not be reported as successful synchronization. */
+static void
+test_neighbor_sync_error(struct ovs_cmdl_context *ctx OVS_UNUSED)
+{
+ struct hmap neighbors = HMAP_INITIALIZER(&neighbors);
+ struct vector learned =
+ VECTOR_EMPTY_INITIALIZER(struct ne_nl_received_neigh);
+ struct ne_nl_received_neigh sentinel = { .if_index = 42 };
+ vector_push(&learned, &sentinel);
+
+ /* This command starts without pooled Netlink sockets. Exhaust its file
+ * descriptor allowance so the real dump fails before receiving data. */
+ struct rlimit original;
+ ovs_assert(!getrlimit(RLIMIT_NOFILE, &original));
+ struct rlimit limited = original;
+ limited.rlim_cur = 0;
+ ovs_assert(!setrlimit(RLIMIT_NOFILE, &limited));
+ bool dump_failed;
+ int error = ne_nl_sync_neigh(AF_INET, 0, NEIGH_IFACE_UNSPECIFIED,
+ &neighbors, &learned, &dump_failed);
+ ovs_assert(!setrlimit(RLIMIT_NOFILE, &original));
+
+ ovs_assert(error == EMFILE && dump_failed);
+ ovs_assert(learned.len == 1);
+ ovs_assert(!memcmp(learned.buffer, &sentinel, sizeof sentinel));
+ vector_destroy(&learned);
+ hmap_destroy(&neighbors);
+}
+
static void
test_neighbor_sync(struct ovs_cmdl_context *ctx)
{
@@ -88,8 +445,10 @@ test_neighbor_sync(struct ovs_cmdl_context *ctx)
advertise_neigh_hash(&ane->lladdr, &ane->addr));
}
+ bool dump_failed;
ovs_assert(ne_nl_sync_neigh(family, if_index, NEIGH_IFACE_UNSPECIFIED,
- &neighbors_to_add, &received_neighbors) == 0);
+ &neighbors_to_add, &received_neighbors,
+ &dump_failed) == 0);
struct ne_nl_received_neigh *ne;
VECTOR_FOR_EACH_PTR (&received_neighbors, ne) {
@@ -537,6 +896,11 @@ 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-dump-script", NULL, 1, 1,
+ test_neighbor_dump_script, OVS_RO},
+ {"neighbor-sync-error", NULL, 0, 0, test_neighbor_sync_error, OVS_RO},
{"neighbor-sync", NULL, 2, INT_MAX, test_neighbor_sync, OVS_RO},
{"neighbor-table-notify", NULL, 1, 1,
test_neighbor_table_notify, OVS_RO},
_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev