From: Petr Oros <poros@redhat.com>
To: netdev@vger.kernel.org
Cc: David Ahern <dsahern@kernel.org>,
Stephen Hemminger <stephen@networkplumber.org>,
Ivan Vecera <ivecera@redhat.com>, Jiri Pirko <jiri@resnulli.us>,
Petr Oros <poros@redhat.com>
Subject: [PATCH iproute2-next 1/2] mnl_utils: add a dump helper for NLM_F_DUMP_INTR
Date: Tue, 10 Nov 2026 12:01:00 +0100 [thread overview]
Message-ID: <20261110110101.2155953-2-poros@redhat.com> (raw)
In-Reply-To: <20261110110101.2155953-1-poros@redhat.com>
The kernel sets NLM_F_DUMP_INTR on a dump message when the dumped
objects changed between two parts of the dump, so the result can miss an
object or list it twice. mnl_cb_run2() answers the flag with EINTR on
the first such message. mnlu_gen_socket_sndrcv() then prints "kernel
answers: Interrupted system call" and returns with the rest of the dump
still queued in the socket. The caller has passed a part of the objects
to its callback by then and cannot tell an interrupted dump from a
request that failed.
Add mnlu_gen_socket_sndrcv_dump(). It clears the flag in every received
message before the callbacks run, reads the dump to its end and tells
the caller through a bool that the dump was interrupted. The caller can
then repeat the request on the same socket or warn the user, as
rtnl_dump_filter() does for rtnetlink dumps.
The receive loop is shared with mnlu_socket_recv_run() and
mnlu_gen_socket_sndrcv(), which behave as before.
Reviewed-by: Ivan Vecera <ivecera@redhat.com>
Signed-off-by: Petr Oros <poros@redhat.com>
---
include/mnl_utils.h | 5 ++++
lib/mnl_utils.c | 58 +++++++++++++++++++++++++++++++++++++++------
2 files changed, 56 insertions(+), 7 deletions(-)
diff --git a/include/mnl_utils.h b/include/mnl_utils.h
index 4df9e398e59165..82c26f05742ce3 100644
--- a/include/mnl_utils.h
+++ b/include/mnl_utils.h
@@ -2,6 +2,8 @@
#ifndef __MNL_UTILS_H__
#define __MNL_UTILS_H__ 1
+#include <stdbool.h>
+
struct mnlu_gen_socket {
struct mnl_socket *nl;
char *buf;
@@ -22,6 +24,9 @@ struct nlmsghdr *mnlu_gen_socket_cmd_prepare(struct mnlu_gen_socket *nlg,
uint8_t cmd, uint16_t flags);
int mnlu_gen_socket_sndrcv(struct mnlu_gen_socket *nlg, const struct nlmsghdr *nlh,
mnl_cb_t data_cb, void *data);
+int mnlu_gen_socket_sndrcv_dump(struct mnlu_gen_socket *nlg,
+ const struct nlmsghdr *nlh,
+ mnl_cb_t data_cb, void *data, bool *intr);
struct mnl_socket *mnlu_socket_open(int bus);
int mnl_add_nl_group(struct mnl_socket *nl, unsigned int group);
diff --git a/lib/mnl_utils.c b/lib/mnl_utils.c
index 43b4cd1a9e40d4..4b971797777cb3 100644
--- a/lib/mnl_utils.c
+++ b/lib/mnl_utils.c
@@ -102,8 +102,29 @@ static mnl_cb_t mnlu_cb_array[NLMSG_MIN_TYPE] = {
[NLMSG_OVERRUN] = mnlu_cb_noop,
};
-int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, size_t buf_size,
- mnl_cb_t cb, void *data)
+/* mnl_cb_run2() fails with EINTR on the first message with NLM_F_DUMP_INTR
+ * and leaves the rest of the dump in the socket. Clear the flag, so that
+ * the dump is read to its end, and tell the caller that it was there.
+ */
+static bool mnlu_dump_intr_clear(void *buf, size_t len)
+{
+ struct nlmsghdr *nlh = buf;
+ bool intr = false;
+ int rem = len;
+
+ for (; mnl_nlmsg_ok(nlh, rem); nlh = mnl_nlmsg_next(nlh, &rem)) {
+ if (nlh->nlmsg_flags & NLM_F_DUMP_INTR) {
+ nlh->nlmsg_flags &= ~NLM_F_DUMP_INTR;
+ intr = true;
+ }
+ }
+
+ return intr;
+}
+
+static int __mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq,
+ void *buf, size_t buf_size,
+ mnl_cb_t cb, void *data, bool *intr)
{
unsigned int portid = mnl_socket_get_portid(nl);
int err;
@@ -112,6 +133,8 @@ int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, siz
err = mnl_socket_recvfrom(nl, buf, buf_size);
if (err <= 0)
break;
+ if (intr)
+ *intr |= mnlu_dump_intr_clear(buf, err);
err = mnl_cb_run2(buf, err, seq, portid,
cb, data, mnlu_cb_array,
ARRAY_SIZE(mnlu_cb_array));
@@ -120,6 +143,12 @@ int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, siz
return err;
}
+int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, size_t buf_size,
+ mnl_cb_t cb, void *data)
+{
+ return __mnlu_socket_recv_run(nl, seq, buf, buf_size, cb, data, NULL);
+}
+
static int ctrl_attrs_cb(const struct nlattr *attr, void *data)
{
int type = mnl_attr_get_type(attr);
@@ -242,20 +271,29 @@ struct nlmsghdr *mnlu_gen_socket_cmd_prepare(struct mnlu_gen_socket *nlg,
nlg->version);
}
-int mnlu_gen_socket_sndrcv(struct mnlu_gen_socket *nlg, const struct nlmsghdr *nlh,
- mnl_cb_t data_cb, void *data)
+/* Like mnlu_gen_socket_sndrcv(), for a dump request. A dump the kernel
+ * marked as interrupted is read to its end and reported in @intr. The
+ * objects passed to @data_cb can then be incomplete or duplicated and it
+ * is up to the caller to repeat the dump or to warn the user.
+ */
+int mnlu_gen_socket_sndrcv_dump(struct mnlu_gen_socket *nlg,
+ const struct nlmsghdr *nlh,
+ mnl_cb_t data_cb, void *data, bool *intr)
{
int err;
+ if (intr)
+ *intr = false;
+
err = mnl_socket_sendto(nlg->nl, nlh, nlh->nlmsg_len);
if (err < 0) {
perror("Failed to send data");
return -errno;
}
- err = mnlu_socket_recv_run(nlg->nl, nlh->nlmsg_seq, nlg->buf,
- MNL_SOCKET_BUFFER_SIZE,
- data_cb, data);
+ err = __mnlu_socket_recv_run(nlg->nl, nlh->nlmsg_seq, nlg->buf,
+ MNL_SOCKET_BUFFER_SIZE,
+ data_cb, data, intr);
if (err < 0) {
fprintf(stderr, "kernel answers: %s\n", strerror(errno));
return -errno;
@@ -263,6 +301,12 @@ int mnlu_gen_socket_sndrcv(struct mnlu_gen_socket *nlg, const struct nlmsghdr *n
return 0;
}
+int mnlu_gen_socket_sndrcv(struct mnlu_gen_socket *nlg, const struct nlmsghdr *nlh,
+ mnl_cb_t data_cb, void *data)
+{
+ return mnlu_gen_socket_sndrcv_dump(nlg, nlh, data_cb, data, NULL);
+}
+
int mnlu_gen_socket_recv_run(struct mnlu_gen_socket *nlg, mnl_cb_t cb,
void *data)
{
--
2.56.0
next prev parent reply other threads:[~2026-11-10 11:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-10 11:00 [PATCH iproute2-next 0/2] dpll: do not fail on interrupted dumps Petr Oros
2026-11-10 11:01 ` Petr Oros [this message]
2026-11-18 22:58 ` [PATCH iproute2-next 1/2] mnl_utils: add a dump helper for NLM_F_DUMP_INTR David Ahern
2026-11-10 11:01 ` [PATCH iproute2-next 2/2] dpll: warn about interrupted dumps Petr Oros
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261110110101.2155953-2-poros@redhat.com \
--to=poros@redhat.com \
--cc=dsahern@kernel.org \
--cc=ivecera@redhat.com \
--cc=jiri@resnulli.us \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox