On 8/14/25 7:36 PM, Ales Musil via dev wrote:
> From: Dumitru Ceara <[email protected]>
> 
> This implements an interface for maintaining (Linux) neighbor entries
> through Netlink.  Netlink was chosen for the same reason it was selected
> for maintaining (Linux) route tables for control planes managed by OVN
> in 0ed52a4d5aaf ("controller: Introduce route-exchange-netlink.").
> 
> Acked-by: Xavier Simonart <[email protected]>
> Co-Authored-by: Ales Musil <[email protected]>
> Signed-off-by: Ales Musil <[email protected]>
> Signed-off-by: Dumitru Ceara <[email protected]>
> ---
> V4:
> - Added Xavier's ack.
> - Addresed Ilya's comments:
>   - nits
>   - netlink dump filtering
>   - delay netlink delete
>   - moved test file
> - Also added tests for deletion of stale OVN entries.
> 
> V3:
> - Addressed Xavier's comment.
> - Moved code that sets NLM_F_REPLACE in patch 1.
> - move change to ignore VLAN 0 and dst port 0 to this patch
> - fixed bug in ne_table_parse__() - missing ntohs() for port
> - added tests for the neighbor-exchange-netlink module
> 
> Changes in V2:
> - removed the ne_nl_neigh_filter
> - fixed memory leak of announced_neighbors hmap.
> ---
>  controller/automake.mk                 |   4 +
>  controller/neighbor-exchange-netlink.c | 490 +++++++++++++++++++++++++
>  controller/neighbor-exchange-netlink.h |  60 +++
>  controller/neighbor.c                  |  77 ++++
>  controller/neighbor.h                  |  73 ++++
>  tests/automake.mk                      |  17 +-
>  tests/ovn-macros.at                    |   4 +
>  tests/system-common-macros.at          |  16 +
>  tests/system-dpdk-testsuite.at         |   1 +
>  tests/system-kmod-testsuite.at         |   1 +
>  tests/system-ovn-netlink.at            | 222 +++++++++++
>  tests/system-userspace-testsuite.at    |   1 +
>  tests/test-ovn-netlink.c               | 120 ++++++
>  tests/test-utils.c                     |  36 ++
>  tests/test-utils.h                     |  10 +-
>  15 files changed, 1125 insertions(+), 7 deletions(-)
>  create mode 100644 controller/neighbor-exchange-netlink.c
>  create mode 100644 controller/neighbor-exchange-netlink.h
>  create mode 100644 controller/neighbor.c
>  create mode 100644 controller/neighbor.h
>  create mode 100644 tests/system-ovn-netlink.at
>  create mode 100644 tests/test-ovn-netlink.c
> 
> diff --git a/controller/automake.mk b/controller/automake.mk
> index f0638ea97..3eb45475c 100644
> --- a/controller/automake.mk
> +++ b/controller/automake.mk
> @@ -30,6 +30,8 @@ controller_ovn_controller_SOURCES = \
>       controller/ofctrl.h \
>       controller/ofctrl-seqno.c \
>       controller/ofctrl-seqno.h \
> +     controller/neighbor.c \
> +     controller/neighbor.h \
>       controller/pinctrl.c \
>       controller/pinctrl.h \
>       controller/patch.c \
> @@ -65,6 +67,8 @@ controller_ovn_controller_SOURCES = \
>  
>  if HAVE_NETLINK
>  controller_ovn_controller_SOURCES += \
> +     controller/neighbor-exchange-netlink.h \
> +     controller/neighbor-exchange-netlink.c \
>       controller/route-exchange-netlink.h \
>       controller/route-exchange-netlink.c \
>       controller/route-exchange.c \
> diff --git a/controller/neighbor-exchange-netlink.c 
> b/controller/neighbor-exchange-netlink.c
> new file mode 100644
> index 000000000..305afda41
> --- /dev/null
> +++ b/controller/neighbor-exchange-netlink.c
> @@ -0,0 +1,490 @@
> +/* Copyright (c) 2025, Red Hat, Inc.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at:
> + *
> + *     http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +#include <config.h>
> +#include <stdbool.h>
> +#include <linux/if_ether.h>
> +#include <linux/rtnetlink.h>
> +
> +#include "hmapx.h"
> +#include "lib/netlink.h"
> +#include "lib/netlink-socket.h"
> +#include "lib/packets.h"
> +#include "openvswitch/vlog.h"
> +
> +#include "neighbor-exchange-netlink.h"
> +#include "neighbor.h"
> +
> +VLOG_DEFINE_THIS_MODULE(neighbor_exchange_netlink);
> +
> +#define NETNL_REQ_BUFFER_SIZE 128
> +
> +/* NTF_EXT_LEARNED was introduced in Linux v3.19, define it if
> + * not available. */
> +#ifndef NTF_EXT_LEARNED
> +#define NTF_EXT_LEARNED (1 << 4)
> +#endif
> +
> +static struct vlog_rate_limit 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;
> +
> +    /* Stores 'struct ne_nl_received_neigh'. */
> +    struct vector *learned_neighbors;
> +
> +    /* Stores 'struct ne_nl_received_neigh'. */
> +    struct vector *stale_neighbors;
> +
> +    /* Stores 'struct advertise_neighbor_entry'. */
> +    const struct hmap *neighbors;
> +
> +    /* Non-zero error code if any netlink operation failed. */
> +    int ret;
> +};
> +
> +static void handle_ne_msg(const struct ne_table_msg *, void *data);
> +
> +static int ne_table_parse__(struct ofpbuf *, size_t ofs,
> +                            const struct nlmsghdr *,
> +                            const struct ndmsg *,
> +                            struct ne_table_msg *);
> +static int ne_nl_add_neigh(int32_t if_index, uint8_t family,
> +                           uint16_t state, uint8_t flags,
> +                           const struct eth_addr *,
> +                           const struct in6_addr *,
> +                           uint16_t port, uint16_t vlan);
> +static int ne_nl_del_neigh(int32_t if_index, uint8_t family,
> +                           const struct eth_addr *,
> +                           const struct in6_addr *,
> +                           uint16_t port, uint16_t vlan);
> +
> +/* Inserts all neigh entries listed in 'neighbors' (of type
> + * 'struct advertise_neighbor_entry') in the table associated to
> + * 'if_index'.  Populates 'learned_neighbors' with all neigh entries
> + * (struct ne_nl_received_neigh) that exist in the table associated to
> + * 'if_index'.
> + *
> + * Returns 0 on success, errno on failure. */
> +int
> +ne_nl_sync_neigh(uint8_t family, int32_t if_index,
> +                 const struct hmap *neighbors,
> +                 struct vector *learned_neighbors)
> +{
> +    struct hmapx neighbors_to_advertise =
> +        HMAPX_INITIALIZER(&neighbors_to_advertise);
> +    struct vector stale_neighbors =
> +        VECTOR_EMPTY_INITIALIZER(struct ne_nl_received_neigh);
> +    struct advertise_neighbor_entry *an;
> +    int ret;
> +
> +    HMAP_FOR_EACH (an, node, neighbors) {
> +        hmapx_add(&neighbors_to_advertise, an);
> +    }
> +
> +    struct ne_msg_handle_data data = {
> +        .neighbors_to_advertise = &neighbors_to_advertise,
> +        .learned_neighbors = learned_neighbors,
> +        .stale_neighbors = &stale_neighbors,
> +        .neighbors = neighbors,
> +    };
> +    ne_table_dump_one_ifindex(family, if_index, handle_ne_msg, &data);
> +    ret = data.ret;
> +
> +    /* Add any remaining neighbors in the neighbors_to_advertise hmapx to the
> +     * system table. */
> +    struct hmapx_node *hn;
> +    HMAPX_FOR_EACH (hn, &neighbors_to_advertise) {
> +        an = hn->data;
> +        int err = ne_nl_add_neigh(if_index, family,
> +                                  NUD_NOARP,        /* state = static */
> +                                  0,                /* flags */
> +                                  &an->lladdr, &an->addr,
> +                                  0,                /* port */
> +                                  0);               /* vlan */
> +        if (err) {
> +            char addr_s[INET6_ADDRSTRLEN + 1];
> +            VLOG_WARN_RL(&rl, "Add neigh ifindex=%"PRId32
> +                              " eth=" ETH_ADDR_FMT " dst=%s"
> +                              " failed: %s",
> +                         if_index, ETH_ADDR_ARGS(an->lladdr),
> +                         ipv6_string_mapped(
> +                             addr_s, &an->addr) ? addr_s : "(invalid)",
> +                         ovs_strerror(err));
> +            if (!ret) {
> +                /* Report the first error value to the caller. */
> +                ret = err;
> +            }
> +        }
> +    }
> +
> +    /* Remove any stale neighbors from the system table. */
> +    struct ne_nl_received_neigh *ne;
> +    VECTOR_FOR_EACH_PTR (&stale_neighbors, ne) {
> +        int err = ne_nl_del_neigh(ne->if_index, ne->family,
> +                                  &ne->lladdr, &ne->addr,
> +                                  ne->port, ne->vlan);
> +        if (err) {
> +            char addr_s[INET6_ADDRSTRLEN + 1];
> +            VLOG_WARN_RL(&rl, "Delete neigh ifindex=%"PRId32" vlan=%"PRIu16
> +                              " eth=" ETH_ADDR_FMT " dst=%s port=%"PRIu16
> +                              " failed: %s",
> +                         ne->if_index, ne->vlan, ETH_ADDR_ARGS(ne->lladdr),
> +                         ipv6_string_mapped(addr_s, &ne->addr)
> +                         ? addr_s : "(invalid)",
> +                         ne->port,
> +                         ovs_strerror(err));
> +            if (!ret) {
> +                /* Report the first error value to the caller. */
> +                ret = err;
> +            }
> +        }
> +    }
> +
> +    hmapx_destroy(&neighbors_to_advertise);
> +    vector_destroy(&stale_neighbors);
> +    return ret;
> +}
> +
> +/* OVN expects all static entries added on this ifindex to be OVN-owned.
> + * Everything else must be learnt. */
> +bool
> +ne_is_ovn_owned(const struct ne_nl_received_neigh *nd)
> +{
> +    return !(nd->state & NUD_PERMANENT) && (nd->state & NUD_NOARP)
> +           && !(nd->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)
> +{
> +    uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
> +    struct ofpbuf request, reply, buf;
> +    struct ndmsg *rq_msg;
> +    struct nl_dump dump;
> +
> +    uint8_t request_stub[NETNL_REQ_BUFFER_SIZE];
> +    ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
> +
> +    nl_msg_put_nlmsghdr(&request, sizeof *rq_msg, RTM_GETNEIGH, 
> NLM_F_REQUEST);
> +    rq_msg = ofpbuf_put_zeros(&request, sizeof *rq_msg);
> +    rq_msg->ndm_family = address_family;
> +    if (if_index) {
> +        nl_msg_put_u32(&request, NDA_IFINDEX, if_index);
> +    }
> +
> +    nl_dump_start(&dump, NETLINK_ROUTE, &request);
> +    ofpbuf_uninit(&request);
> +
> +    ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
> +    while (nl_dump_next(&dump, &reply, &buf)) {
> +        struct ne_table_msg msg;
> +
> +        if (ne_table_parse(&reply, &msg)) {
> +            struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(&reply);
> +
> +            /* 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);
> +        }
> +    }
> +    ofpbuf_uninit(&buf);
> +    nl_dump_done(&dump);
> +}
> +
> +static int
> +ne_table_parse__(struct ofpbuf *buf, size_t ofs, const struct nlmsghdr 
> *nlmsg,
> +                 const struct ndmsg *nd, struct ne_table_msg *change)
> +{
> +    bool parsed;
> +
> +    static const struct nl_policy policy[] = {
> +        [NDA_DST] = { .type = NL_A_U32, .optional = true },
> +        [NDA_LLADDR] = { .type = NL_A_LL_ADDR, .optional = true },
> +        [NDA_PORT] = { .type = NL_A_U16, .optional = true },
> +    };
> +
> +    static const struct nl_policy policy6[] = {
> +        [NDA_DST] = { .type = NL_A_IPV6, .optional = true },
> +        [NDA_LLADDR] = { .type = NL_A_LL_ADDR, .optional = true },
> +        [NDA_PORT] = { .type = NL_A_U16, .optional = true },
> +    };
> +
> +    static const struct nl_policy policy_bridge[] = {
> +        [NDA_DST] = { .type = NL_A_UNSPEC, .optional = true,
> +                      .min_len = sizeof(struct in_addr),
> +                      .max_len = sizeof(struct in6_addr)},
> +        [NDA_LLADDR] = { .type = NL_A_LL_ADDR, .optional = true },
> +        [NDA_PORT] = { .type = NL_A_U16, .optional = true },
> +        [NDA_VLAN] = { .type = NL_A_U16, .optional = true },
> +    };
> +
> +    BUILD_ASSERT(ARRAY_SIZE(policy) == ARRAY_SIZE(policy6));
> +    BUILD_ASSERT(ARRAY_SIZE(policy) == ARRAY_SIZE(policy_bridge));
> +    struct nlattr *attrs[ARRAY_SIZE(policy)];
> +
> +    if (nd->ndm_family == AF_INET) {
> +        parsed = nl_policy_parse(buf, ofs, policy, attrs,
> +                                 ARRAY_SIZE(policy));
> +    } else if (nd->ndm_family == AF_INET6) {
> +        parsed = nl_policy_parse(buf, ofs, policy6, attrs,
> +                                 ARRAY_SIZE(policy6));
> +    } else if (nd->ndm_family == AF_BRIDGE) {
> +        parsed = nl_policy_parse(buf, ofs, policy_bridge, attrs,
> +                                 ARRAY_SIZE(policy_bridge));
> +    } else {
> +        VLOG_WARN_RL(&rl, "received non AF_INET/AF_INET6/AF_BRIDGE rtnetlink 
> "
> +                          "neigh message");
> +        return 0;
> +    }
> +
> +    if (parsed) {
> +        *change = (struct ne_table_msg) {
> +            .nlmsg_type = nlmsg->nlmsg_type,
> +            .nd.if_index = nd->ndm_ifindex,
> +            .nd.family = nd->ndm_family,
> +            .nd.state = nd->ndm_state,
> +            .nd.flags = nd->ndm_flags,
> +            .nd.type = nd->ndm_type,
> +        };
> +
> +        if (attrs[NDA_DST]) {
> +            size_t nda_dst_size = nl_attr_get_size(attrs[NDA_DST]);
> +
> +            switch (nda_dst_size) {
> +            case sizeof(uint32_t):
> +                in6_addr_set_mapped_ipv4(&change->nd.addr,
> +                                         nl_attr_get_be32(attrs[NDA_DST]));
> +                break;
> +            case sizeof(struct in6_addr):
> +                change->nd.addr = nl_attr_get_in6_addr(attrs[NDA_DST]);
> +                break;
> +            default:
> +                VLOG_DBG_RL(&rl,
> +                            "neigh message contains non-IPv4/IPv6 NDA_DST");
> +                return 0;
> +            }
> +        }
> +
> +        if (attrs[NDA_LLADDR]) {
> +            if (nl_attr_get_size(attrs[NDA_LLADDR]) != ETH_ALEN) {
> +                VLOG_DBG_RL(&rl, "neigh message contains non-ETH 
> NDA_LLADDR");
> +                return 0;
> +            }
> +            change->nd.lladdr = nl_attr_get_eth_addr(attrs[NDA_LLADDR]);
> +        }
> +
> +        if (attrs[NDA_PORT]) {
> +            change->nd.port = ntohs(nl_attr_get_be16(attrs[NDA_PORT]));
> +        }
> +
> +        if (attrs[NDA_VLAN]) {
> +            change->nd.vlan = nl_attr_get_u16(attrs[NDA_VLAN]);
> +        }
> +    } else {
> +        VLOG_DBG_RL(&rl, "received unparseable rtnetlink neigh message");
> +        return 0;
> +    }
> +
> +    /* Success. */
> +    return RTNLGRP_NEIGH;
> +}
> +
> +static void
> +handle_ne_msg(const struct ne_table_msg *msg, void *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)) {
> +        if (!handle_data->learned_neighbors) {
> +            return;
> +        }
> +
> +        /* Learn the non-OVN entry. */
> +        vector_push(handle_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) {
> +        struct advertise_neighbor_entry *an =
> +            advertise_neigh_find(handle_data->neighbors, nd->lladdr,
> +                                 &nd->addr);
> +        if (an) {
> +            hmapx_find_and_delete(handle_data->neighbors_to_advertise, an);
> +            return;
> +        }
> +    }
> +
> +    /* Store the entry for deletion. */
> +    vector_push(handle_data->stale_neighbors, nd);

This function assumes that learned_neighbors and neighbors_to_advertise
can be NULL, we should be consistent and check the stale_neighbors as well.

Best regards, Ilya Maximets.
_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to