diff mbox series

[07/12] netlink: support appending using scatter / gather

Message ID 20240722190443.43196-7-denkenz@gmail.com (mailing list archive)
State New
Headers show
Series [01/12] useful: Add utility to find the next power of two | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success

Commit Message

Denis Kenzior July 22, 2024, 7:04 p.m. UTC
This version builds the attribute using scatter / gather semantics
(iovs) in order to minimize the number of copies required to build the
message.
---
 ell/ell.sym   |  1 +
 ell/netlink.c | 34 ++++++++++++++++++++++++++++++++++
 ell/netlink.h |  3 +++
 3 files changed, 38 insertions(+)
diff mbox series

Patch

diff --git a/ell/ell.sym b/ell/ell.sym
index 247786d5ee98..b99b6e552eaa 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -439,6 +439,7 @@  global:
 	l_netlink_message_ref;
 	l_netlink_message_unref;
 	l_netlink_message_append;
+	l_netlink_message_appendv;
 	l_netlink_message_add_header;
 	/* path */
 	l_basename;
diff --git a/ell/netlink.c b/ell/netlink.c
index 39c5bc335e19..d7294a95f20d 100644
--- a/ell/netlink.c
+++ b/ell/netlink.c
@@ -821,6 +821,40 @@  LIB_EXPORT int l_netlink_message_append(struct l_netlink_message *message,
 	return 0;
 }
 
+LIB_EXPORT int l_netlink_message_appendv(struct l_netlink_message *message,
+					uint16_t type,
+					const struct iovec *iov, size_t iov_len)
+{
+	size_t len = 0;
+	void *dest;
+	size_t i;
+	int r;
+
+	if (unlikely(!message))
+		return -EINVAL;
+
+	for (i = 0; i < iov_len; i++)
+		len += iov[i].iov_len;
+
+	if (len > USHRT_MAX - NLA_HDRLEN)
+		return -ERANGE;
+
+	r = message_grow(message, NLA_HDRLEN + NLA_ALIGN(len));
+	if (r < 0)
+		return r;
+
+	r = add_attribute(message, type, len, &dest);
+	if (r < 0)
+		return r;
+
+	for (i = 0, len = 0; i < iov_len; i++, iov++) {
+		memcpy(dest + len, iov->iov_base, iov->iov_len);
+		len += iov->iov_len;
+	}
+
+	return 0;
+}
+
 int netlink_message_reserve_header(struct l_netlink_message *message,
 					size_t len, void **out_header)
 {
diff --git a/ell/netlink.h b/ell/netlink.h
index 70a55d652560..4d1c4761b3bb 100644
--- a/ell/netlink.h
+++ b/ell/netlink.h
@@ -53,6 +53,9 @@  struct l_netlink_message *l_netlink_message_ref(
 void l_netlink_message_unref(struct l_netlink_message *message);
 int l_netlink_message_append(struct l_netlink_message *message, uint16_t type,
 					const void *data, size_t len);
+int l_netlink_message_appendv(struct l_netlink_message *message,
+					uint16_t type,
+					const struct iovec *iov, size_t iov_len);
 int l_netlink_message_add_header(struct l_netlink_message *message,
 					const void *header, size_t len);