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 <[email protected]>
---
 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

diff --git a/controller/automake.mk b/controller/automake.mk
index bf126d257..19bfe6569 100644
--- a/controller/automake.mk
+++ b/controller/automake.mk
@@ -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 \
diff --git a/controller/netlink-utils.c b/controller/netlink-utils.c
new file mode 100644
index 000000000..953e8b013
--- /dev/null
+++ b/controller/netlink-utils.c
@@ -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;
+}
+
diff --git a/controller/netlink-utils.h b/controller/netlink-utils.h
new file mode 100644
index 000000000..51e4ebb08
--- /dev/null
+++ b/controller/netlink-utils.h
@@ -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 */
diff --git a/controller/route-exchange-netlink.c 
b/controller/route-exchange-netlink.c
index 8f1615c4e..a70eaccd9 100644
--- a/controller/route-exchange-netlink.c
+++ b/controller/route-exchange-netlink.c
@@ -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
diff --git a/controller/route-exchange-netlink.h 
b/controller/route-exchange-netlink.h
index c137b5119..b6718f3c7 100644
--- a/controller/route-exchange-netlink.h
+++ b/controller/route-exchange-netlink.h
@@ -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;
diff --git a/controller/route-exchange.c b/controller/route-exchange.c
index 027375071..d2d611311 100644
--- a/controller/route-exchange.c
+++ b/controller/route-exchange.c
@@ -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);
diff --git a/tests/automake.mk b/tests/automake.mk
index d4c1d3710..b0effd8ea 100644
--- a/tests/automake.mk
+++ b/tests/automake.mk
@@ -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 \
-- 
2.48.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to