@@ -75,6 +75,8 @@ controller_ovn_controller_SOURCES = \
if HAVE_NETLINK
controller_ovn_controller_SOURCES += \
+ controller/netlink-utils.h \
+ controller/netlink-utils.c \
controller/host-if-monitor.c \
controller/ovn-netlink-notifier.c \
controller/neighbor-exchange-netlink.h \
new file mode 100644
@@ -0,0 +1,108 @@
+/* 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 <errno.h>
+#include <linux/if_ether.h>
+#include <linux/if_link.h>
+#include <linux/rtnetlink.h>
+
+#include "lib/netlink.h"
+#include "lib/netlink-socket.h"
+#include "lib/packets.h"
+
+#include "openvswitch/ofpbuf.h"
+
+#include "netlink-utils.h"
+
+int32_t
+nl_ifindex_get(const char *ifname)
+{
+ return (int32_t) if_nametoindex(ifname);
+}
+
+int
+nl_create_device(const char *ifname, const char *kind)
+{
+ struct ifinfomsg *ifinfo;
+ struct ofpbuf request;
+ ofpbuf_init(&request, 0);
+
+ nl_msg_put_nlmsghdr(&request, 0, RTM_NEWLINK,
+ NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL);
+ ifinfo = ofpbuf_put_zeros(&request, sizeof *ifinfo);
+ nl_msg_put_string(&request, IFLA_IFNAME, ifname);
+ ifinfo->ifi_change = ifinfo->ifi_flags = IFF_UP;
+
+ size_t linkinfo_off = nl_msg_start_nested(&request, IFLA_LINKINFO);
+ nl_msg_put_string(&request, IFLA_INFO_KIND, kind);
+ nl_msg_end_nested(&request, linkinfo_off);
+
+ int err = nl_transact(NETLINK_ROUTE, &request, NULL);
+ ofpbuf_uninit(&request);
+ return err;
+}
+
+int
+nl_create_vrf(const char *ifname, uint32_t table_id)
+{
+ size_t linkinfo_off, infodata_off;
+ struct ifinfomsg *ifinfo;
+ int err;
+
+ struct ofpbuf request;
+ uint8_t request_stub[NETNL_REQ_BUFFER_SIZE];
+ ofpbuf_use_stub(&request, request_stub, sizeof(request_stub));
+
+ nl_msg_put_nlmsghdr(&request, 0, RTM_NEWLINK,
+ NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL);
+ ifinfo = ofpbuf_put_zeros(&request, sizeof *ifinfo);
+ nl_msg_put_string(&request, IFLA_IFNAME, ifname);
+
+ ifinfo->ifi_change = ifinfo->ifi_flags = IFF_UP;
+ linkinfo_off = nl_msg_start_nested(&request, IFLA_LINKINFO);
+ nl_msg_put_string(&request, IFLA_INFO_KIND, "vrf");
+ infodata_off = nl_msg_start_nested(&request, IFLA_INFO_DATA);
+ nl_msg_put_u32(&request, IFLA_VRF_TABLE, table_id);
+ nl_msg_end_nested(&request, infodata_off);
+ nl_msg_end_nested(&request, linkinfo_off);
+
+ err = nl_transact(NETLINK_ROUTE, &request, NULL);
+
+ ofpbuf_uninit(&request);
+ return err;
+}
+
+int
+nl_delete_device(const char *ifname)
+{
+ struct ifinfomsg *ifinfo;
+ int err;
+
+ struct ofpbuf request;
+ uint8_t request_stub[NETNL_REQ_BUFFER_SIZE];
+ ofpbuf_use_stub(&request, request_stub, sizeof(request_stub));
+
+ nl_msg_put_nlmsghdr(&request, 0, RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK);
+ ifinfo = ofpbuf_put_zeros(&request, sizeof *ifinfo);
+ nl_msg_put_string(&request, IFLA_IFNAME, ifname);
+ err = nl_transact(NETLINK_ROUTE, &request, NULL);
+
+ ofpbuf_uninit(&request);
+ return err;
+}
+
new file mode 100644
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2025 Canonical, Ltd.
+ * Copyright (c) 2025, STACKIT GmbH & Co. KG
+ *
+ * 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.
+ */
+
+#ifndef NETLINK_UTILS_H
+#define NETLINK_UTILS_H 1
+
+#include <stdint.h>
+#include <linux/rtnetlink.h>
+#include <netinet/in.h>
+#include <net/if.h>
+
+#define NETNL_REQ_BUFFER_SIZE 128
+
+#define TABLE_ID_VALID(table_id) (table_id != RT_TABLE_UNSPEC && \
+ table_id != RT_TABLE_COMPAT && \
+ table_id != RT_TABLE_LOCAL && \
+ table_id != RT_TABLE_MAX)
+
+int32_t nl_ifindex_get(const char *ifname);
+int nl_create_device(const char *ifname, const char *kind);
+int nl_create_vrf(const char *ifname, uint32_t table_id);
+int nl_delete_device(const char *ifname);
+
+#endif /* netlink-utils.h */
@@ -31,71 +31,25 @@
#include "route-table.h"
#include "route.h"
#include "vec.h"
+#include "netlink-utils.h"
#include "route-exchange-netlink.h"
VLOG_DEFINE_THIS_MODULE(route_exchange_netlink);
-#define NETNL_REQ_BUFFER_SIZE 128
-
static void re_nl_encode_nexthop(struct ofpbuf *, bool dst_is_ipv4,
const struct in6_addr *);
int
re_nl_create_vrf(const char *ifname, uint32_t table_id)
{
- if (!TABLE_ID_VALID(table_id)) {
- static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
- VLOG_WARN_RL(&rl,
- "attempt to create VRF using invalid table id %"PRIu32,
- table_id);
- return EINVAL;
- }
-
- size_t linkinfo_off, infodata_off;
- struct ifinfomsg *ifinfo;
- int err;
-
- struct ofpbuf request;
- uint8_t request_stub[NETNL_REQ_BUFFER_SIZE];
- ofpbuf_use_stub(&request, request_stub, sizeof(request_stub));
-
- nl_msg_put_nlmsghdr(&request, 0, RTM_NEWLINK,
- NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL);
- ifinfo = ofpbuf_put_zeros(&request, sizeof *ifinfo);
- nl_msg_put_string(&request, IFLA_IFNAME, ifname);
-
- ifinfo->ifi_change = ifinfo->ifi_flags = IFF_UP;
- linkinfo_off = nl_msg_start_nested(&request, IFLA_LINKINFO);
- nl_msg_put_string(&request, IFLA_INFO_KIND, "vrf");
- infodata_off = nl_msg_start_nested(&request, IFLA_INFO_DATA);
- nl_msg_put_u32(&request, IFLA_VRF_TABLE, table_id);
- nl_msg_end_nested(&request, infodata_off);
- nl_msg_end_nested(&request, linkinfo_off);
-
- err = nl_transact(NETLINK_ROUTE, &request, NULL);
-
- ofpbuf_uninit(&request);
- return err;
+ return nl_create_vrf(ifname, table_id);
}
int
re_nl_delete_vrf(const char *ifname)
{
- struct ifinfomsg *ifinfo;
- int err;
-
- struct ofpbuf request;
- uint8_t request_stub[NETNL_REQ_BUFFER_SIZE];
- ofpbuf_use_stub(&request, request_stub, sizeof(request_stub));
-
- nl_msg_put_nlmsghdr(&request, 0, RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK);
- ifinfo = ofpbuf_put_zeros(&request, sizeof *ifinfo);
- nl_msg_put_string(&request, IFLA_IFNAME, ifname);
- err = nl_transact(NETLINK_ROUTE, &request, NULL);
-
- ofpbuf_uninit(&request);
- return err;
+ return nl_delete_device(ifname);
}
void
@@ -29,11 +29,6 @@
#define RTPROT_OVN 84
#endif
-#define TABLE_ID_VALID(table_id) (table_id != RT_TABLE_UNSPEC && \
- table_id != RT_TABLE_COMPAT && \
- table_id != RT_TABLE_LOCAL && \
- table_id != RT_TABLE_MAX)
-
struct in6_addr;
struct hmap;
struct vector;
@@ -34,6 +34,7 @@
#include "route.h"
#include "route-exchange.h"
#include "route-exchange-netlink.h"
+#include "netlink-utils.h"
VLOG_DEFINE_THIS_MODULE(route_exchange);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
@@ -291,6 +291,8 @@ tests_ovstest_SOURCES = \
if HAVE_NETLINK
tests_ovstest_SOURCES += \
+ controller/netlink-utils.h \
+ controller/netlink-utils.c \
controller/host-if-monitor.c \
controller/host-if-monitor.h \
controller/neighbor-exchange-netlink.c \
Move the link creation/deletion and ifindex lookup helpers out of route-exchange-netlink into a new netlink-utils module, together with the TABLE_ID_VALID macro, so that they can be reused by other controller components. Signed-off-by: Alexandra Rukomoinikova <ARukomoinikova@k2.cloud> --- controller/automake.mk | 2 + controller/netlink-utils.c | 108 ++++++++++++++++++++++++++++ controller/netlink-utils.h | 38 ++++++++++ controller/route-exchange-netlink.c | 52 +------------- controller/route-exchange-netlink.h | 5 -- controller/route-exchange.c | 1 + tests/automake.mk | 2 + 7 files changed, 154 insertions(+), 54 deletions(-) create mode 100644 controller/netlink-utils.c create mode 100644 controller/netlink-utils.h