]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: Extract from devlink/mnlg a helper, mnlu_socket_open()
authorPetr Machata <me@pmachata.org>
Thu, 12 Nov 2020 22:24:41 +0000 (23:24 +0100)
committerDavid Ahern <dsahern@gmail.com>
Sat, 14 Nov 2020 02:43:15 +0000 (19:43 -0700)
This little dance of mnl_socket_open(), option setting, and bind, is the
same regardless of tool. Extract into a new module that should hold helpers
for working with libmnl, mnl_util.c.

Signed-off-by: Petr Machata <me@pmachata.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
devlink/Makefile
devlink/mnlg.c
include/mnl_utils.h [new file with mode: 0644]
lib/Makefile
lib/mnl_utils.c [new file with mode: 0644]

index 7da7d1fa18d5068e36ba0f842805e800d26539fa..d540feb3c0121c50573705e902ba3bacc5e758ff 100644 (file)
@@ -12,7 +12,7 @@ endif
 
 all: $(TARGETS) $(LIBS)
 
-devlink: $(DEVLINKOBJ)
+devlink: $(DEVLINKOBJ) $(LIBNETLINK)
        $(QUIET_LINK)$(CC) $^ $(LDFLAGS) $(LDLIBS) -o $@
 
 install: all
index c7d25e8713a12dc4ce0caa5541ae61f57c394414..9817bbad5e7d8b4ca613d8261c79ad4a18619e37 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/genetlink.h>
 
 #include "libnetlink.h"
+#include "mnl_utils.h"
 #include "utils.h"
 #include "mnlg.h"
 
@@ -263,7 +264,6 @@ struct mnlg_socket *mnlg_socket_open(const char *family_name, uint8_t version)
 {
        struct mnlg_socket *nlg;
        struct nlmsghdr *nlh;
-       int one = 1;
        int err;
 
        nlg = malloc(sizeof(*nlg));
@@ -274,19 +274,9 @@ struct mnlg_socket *mnlg_socket_open(const char *family_name, uint8_t version)
        if (!nlg->buf)
                goto err_buf_alloc;
 
-       nlg->nl = mnl_socket_open(NETLINK_GENERIC);
+       nlg->nl = mnlu_socket_open(NETLINK_GENERIC);
        if (!nlg->nl)
-               goto err_mnl_socket_open;
-
-       /* Older kernels may no support capped/extended ACK reporting */
-       mnl_socket_setsockopt(nlg->nl, NETLINK_CAP_ACK, &one, sizeof(one));
-       mnl_socket_setsockopt(nlg->nl, NETLINK_EXT_ACK, &one, sizeof(one));
-
-       err = mnl_socket_bind(nlg->nl, 0, MNL_SOCKET_AUTOPID);
-       if (err < 0)
-               goto err_mnl_socket_bind;
-
-       nlg->portid = mnl_socket_get_portid(nlg->nl);
+               goto err_socket_open;
 
        nlh = __mnlg_msg_prepare(nlg, CTRL_CMD_GETFAMILY,
                                 NLM_F_REQUEST | NLM_F_ACK, GENL_ID_CTRL, 1);
@@ -305,9 +295,8 @@ struct mnlg_socket *mnlg_socket_open(const char *family_name, uint8_t version)
 
 err_mnlg_socket_recv_run:
 err_mnlg_socket_send:
-err_mnl_socket_bind:
        mnl_socket_close(nlg->nl);
-err_mnl_socket_open:
+err_socket_open:
        free(nlg->buf);
 err_buf_alloc:
        free(nlg);
diff --git a/include/mnl_utils.h b/include/mnl_utils.h
new file mode 100644 (file)
index 0000000..10a064a
--- /dev/null
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __MNL_UTILS_H__
+#define __MNL_UTILS_H__ 1
+
+struct mnl_socket *mnlu_socket_open(int bus);
+
+#endif /* __MNL_UTILS_H__ */
index 7cba1857b7fac517ccd04003547b01c9c20f15b5..13f4ee15373b78fbbe9fd387b36c301c2e92dfe0 100644 (file)
@@ -7,7 +7,7 @@ UTILOBJ = utils.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \
        inet_proto.o namespace.o json_writer.o json_print.o \
        names.o color.o bpf.o exec.o fs.o cg_map.o
 
-NLOBJ=libgenl.o libnetlink.o
+NLOBJ=libgenl.o libnetlink.o mnl_utils.o
 
 all: libnetlink.a libutil.a
 
diff --git a/lib/mnl_utils.c b/lib/mnl_utils.c
new file mode 100644 (file)
index 0000000..2426912
--- /dev/null
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * mnl_utils.c Helpers for working with libmnl.
+ */
+
+#include <libmnl/libmnl.h>
+
+#include "mnl_utils.h"
+
+struct mnl_socket *mnlu_socket_open(int bus)
+{
+       struct mnl_socket *nl;
+       int one = 1;
+
+       nl = mnl_socket_open(bus);
+       if (nl == NULL)
+               return NULL;
+
+       mnl_socket_setsockopt(nl, NETLINK_CAP_ACK, &one, sizeof(one));
+       mnl_socket_setsockopt(nl, NETLINK_EXT_ACK, &one, sizeof(one));
+
+       if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0)
+               goto err_bind;
+
+       return nl;
+
+err_bind:
+       mnl_socket_close(nl);
+       return NULL;
+}