Skip to contentSkip to conversation

useful: Add utility to find the next power of two

PATCH Applied Denis Kenzior 1 participant 13 messages Jul 22, 2024 – Jul 23, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/12 useful: Add utility to find the next power of two

---
 ell/useful.h | 6 ++++++
 1 file changed, 6 insertions(+)
ell/useful.h +6 -0
index 7bb924bcab8c..504ea089cb21 100644
@@ -52,6 +52,12 @@ static inline unsigned char bit_field(const unsigned char oct,
 	return (oct >> start) & mask;
 }
 
+/* Must be called with n >= 2 and n <= ULONG_MAX / 2 + 1 */
+static inline unsigned long roundup_pow_of_two(unsigned long n)
+{
+	return 1UL << (sizeof(unsigned long) * 8 - __builtin_clzl(n - 1));
+}
+
 #define DIV_ROUND_CLOSEST(x, divisor)			\
 ({							\
 	typeof(divisor) _d = (divisor);			\
Signature · 1 line
2.45.2
#1 lore raw

PATCH 2/12 unit: Add unit test for roundup_pow_of_two

---
 unit/test-util.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
unit/test-util.c +20 -0
index b178b86cf489..341c790c3731 100644
@@ -15,6 +15,7 @@
 #include <errno.h>
 
 #include <ell/ell.h>
+#include <ell/useful.h>
 
 static void test_hexstring(const void *test_data)
 {
@@ -270,6 +271,23 @@ static void test_clear_bits(const void *test_data)
 	assert(l_memeqzero(bitmap, sizeof(bitmap)));
 }
 
+static void test_roundup_pow_of_two(const void *test_data)
+{
+	assert(roundup_pow_of_two(2UL) == 2UL);
+	assert(roundup_pow_of_two(3UL) == 4UL);
+	assert(roundup_pow_of_two(5UL) == 8UL);
+	assert(roundup_pow_of_two(15UL) == 16UL);
+	assert(roundup_pow_of_two(25UL) == 32UL);
+	assert(roundup_pow_of_two(1025) == 2048UL);
+
+	if (sizeof(unsigned long) > 4) {
+		unsigned long l = UINT_MAX;
+
+		l += 1;
+		assert(roundup_pow_of_two(UINT_MAX) == l);
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -293,5 +311,7 @@ int main(int argc, char *argv[])
 	l_test_add("L_BITS_SET", test_set_bits, NULL);
 	l_test_add("L_BITS_CLEAR", test_clear_bits, NULL);
 
+	l_test_add("roundup_pow_of_two", test_roundup_pow_of_two, NULL);
+
 	return l_test_run();
 }
Signature · 1 line
2.45.2
#2 lore raw

PATCH 3/12 util: Introduce l_util_pagesize

In reply to Denis Kenzior
---
 ell/ell.sym |  1 +
 ell/util.c  | 12 ++++++++++++
 ell/util.h  |  2 ++
 3 files changed, 15 insertions(+)
ell/ell.sym +1 -0
index 9fcc3aad9a2f..bc0c6e10ebca 100644
@@ -23,6 +23,7 @@ global:
 	l_util_hexdumpv;
 	l_util_debug;
 	l_util_get_debugfs_path;
+	l_util_pagesize;
 	l_memeq;
 	l_secure_memeq;
 	l_safe_atox8;
ell/util.c +12 -0
index 549e253eefc0..45ec6c8110bc 100644
@@ -15,6 +15,7 @@
 #include <limits.h>
 #include <stdint.h>
 #include <errno.h>
+#include <unistd.h>
 
 #include "utf8.h"
 #include "util.h"
@@ -812,3 +813,14 @@ LIB_EXPORT int l_safe_atox32(const char *s, uint32_t *out_x)
 
 	return safe_atou(s, 16, out_x);
 }
+
+LIB_EXPORT size_t l_util_pagesize(void)
+{
+	static size_t page_size = 0;
+
+	if (likely(page_size > 0))
+		return page_size;
+
+	page_size = sysconf(_SC_PAGESIZE);
+	return page_size;
+}
ell/util.h +2 -0
index f4fc4d1143e3..c56f182292fa 100644
@@ -515,6 +515,8 @@ int l_safe_atox32(const char *s, uint32_t *out_u);
 int l_safe_atox16(const char *s, uint16_t *out_u);
 int l_safe_atox8(const char *s, uint8_t *out_u);
 
+size_t l_util_pagesize(void);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.45.2
#3 lore raw

PATCH 4/12 string: Use conservative allocation growth strategy

In reply to Denis Kenzior

Instead of always growing exponentially, only request enough pages to hold the new string. It is unlikely that ell based applications will be processing large strings, so be more conservative with memory use.

---
 ell/string.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
ell/string.c +7 -14
index a6d359d20cc9..e8e45beacb92 100644
@@ -34,25 +34,18 @@ struct l_string {
 	char *str;
 };
 
-static inline size_t next_power(size_t len)
-{
-	size_t n = 1;
-
-	if (len > SIZE_MAX / 2)
-		return SIZE_MAX;
-
-	while (n < len)
-		n = n << 1;
-
-	return n;
-}
-
 static void grow_string(struct l_string *str, size_t extra)
 {
 	if (str->len + extra < str->max)
 		return;
 
-	str->max = next_power(str->len + extra + 1);
+	str->max = str->len + extra + 1;
+
+	if (str->max < l_util_pagesize())
+		str->max = roundup_pow_of_two(str->max);
+	else
+		str->max = align_len(str->max, l_util_pagesize());
+
 	str->str = l_realloc(str->str, str->max);
 }
 
Signature · 1 line
2.45.2
#4 lore raw

PATCH 5/12 unit: Refactor genl-msg tests

In reply to Denis Kenzior

Both parse|build_set_station and parse|build_set_rekey_offload tests declare precanned information as static const variables in the individual test functions. Declare this precanned information inside a test structure for easier maintenance and future reusability.

---
 unit/test-genl-msg.c | 124 ++++++++++++++++++++++++++-----------------
 1 file changed, 74 insertions(+), 50 deletions(-)
unit/test-genl-msg.c +74 -50
index 0ae92aefebc9..e6432f4362ef 100644
@@ -31,10 +31,25 @@ static unsigned char set_station_request[] = {
 	0x02, 0x00, 0x00, 0x00,
 };
 
+struct set_station_test {
+	uint8_t mac[6];
+	uint32_t ifindex;
+	uint32_t flags[2];
+	uint32_t seq;
+	uint32_t pid;
+};
+
+static const struct set_station_test set_station = {
+	.mac = { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 },
+	.ifindex = 3,
+	.flags = { 2, 2 },
+	.seq = 0x550d538b,
+	.pid = 3604,
+};
+
 static void parse_set_station(const void *data)
 {
-	static const unsigned char mac[6] =
-		{ 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 };
+	const struct set_station_test *test = data;
 	struct nlmsghdr *nlmsg;
 	struct l_genl_msg *msg;
 	struct l_genl_attr attr;
@@ -56,13 +71,13 @@ static void parse_set_station(const void *data)
 	assert(l_genl_attr_next(&attr, &type, &len, &payload));
 	assert(type == 3);
 	assert(len == 4);
-	assert(*((unsigned int *) payload) == 3);
+	assert(*((unsigned int *) payload) == test->ifindex);
 
 	/* MAC Address 24:A2:E1:EC:17:04 */
 	assert(l_genl_attr_next(&attr, &type, &len, &payload));
 	assert(type == 6);
-	assert(len == 6);
-	assert(!memcmp(payload, mac, 6));
+	assert(len == sizeof(test->mac));
+	assert(!memcmp(payload, test->mac, sizeof(test->mac)));
 
 	/* Station Flags 2: len 8
 	 *     Mask: 0x00000002
@@ -72,19 +87,16 @@ static void parse_set_station(const void *data)
 	 */
 	assert(l_genl_attr_next(&attr, &type, &len, &payload));
 	assert(type == 67);
-	assert(len == 8);
-	assert(((unsigned int *) payload)[0] == 2);
-	assert(((unsigned int *) payload)[1] == 2);
+	assert(len == sizeof(test->flags));
+	assert(((unsigned int *) payload)[0] == test->flags[0]);
+	assert(((unsigned int *) payload)[1] == test->flags[1]);
 
 	l_genl_msg_unref(msg);
 }
 
 static void build_set_station(const void *data)
 {
-	static uint32_t index = 3;
-	static const unsigned char mac[6] =
-		{ 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 };
-	static uint32_t flags[] = { 2, 2 };
+	const struct set_station_test *test = data;
 	struct l_genl_msg *msg;
 	const void *raw;
 	size_t size;
@@ -92,15 +104,19 @@ static void build_set_station(const void *data)
 	msg = l_genl_msg_new_sized(18, 512);
 	assert(msg);
 
-	assert(l_genl_msg_append_attr(msg, 3, 4, &index));
-	assert(l_genl_msg_append_attr(msg, 6, 6, mac));
-	assert(l_genl_msg_append_attr(msg, 67, 8, flags));
+	assert(l_genl_msg_append_attr(msg, 3,
+					sizeof(test->ifindex), &test->ifindex));
+	assert(l_genl_msg_append_attr(msg, 6, sizeof(test->mac), test->mac));
+	assert(l_genl_msg_append_attr(msg, 67,
+					sizeof(test->flags), test->flags));
 
-	raw = l_genl_msg_to_data(msg, 0x17, 0x05, 0x550d538b, 3604, &size);
+	raw = l_genl_msg_to_data(msg, 0x17, NLM_F_REQUEST | NLM_F_ACK,
+					test->seq, test->pid, &size);
 
 	if (do_print) {
 		l_util_hexdump(false, raw, size, do_debug, "[MSG] ");
-		l_util_hexdump(true, set_station_request, size,
+		l_util_hexdump(true, set_station_request,
+					sizeof(set_station_request),
 					do_debug, "[MSG] ");
 	}
 
@@ -120,16 +136,29 @@ static const unsigned char set_rekey_offload_request[] = {
 	0x0c, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
 };
 
+struct set_rekey_offload_test {
+	uint32_t seq;
+	uint32_t pid;
+	uint32_t ifindex;
+	uint8_t kek[16];
+	uint8_t kck[16];
+	uint8_t replay_counter[8];
+};
+
+static const struct set_rekey_offload_test rekey_offload = {
+	.seq = 0x53e1a359,
+	.pid = 0xe74002ba,
+	.kek = {	0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b,
+			0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b },
+	.kck = {	0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d,
+			0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 },
+	.replay_counter = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
+	.ifindex = 3,
+};
+
 static void parse_set_rekey_offload(const void *data)
 {
-	static const unsigned char kek[] = {
-		0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b,
-		0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b };
-	static const unsigned char kck[] = {
-		0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d,
-		0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 };
-	static const unsigned char replay_counter[] = {
-		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
+	const struct set_rekey_offload_test *test = data;
 	struct nlmsghdr *nlmsg;
 	struct l_genl_msg *msg;
 	struct l_genl_attr attr;
@@ -172,33 +201,25 @@ static void parse_set_rekey_offload(const void *data)
 
 	assert(l_genl_attr_next(&nested, &type, &len, &payload));
 	assert(type == 1);
-	assert(len == 16);
-	assert(!memcmp(payload, kek, 16));
+	assert(len == sizeof(test->kek));
+	assert(!memcmp(payload, test->kek, sizeof(test->kek)));
 
 	assert(l_genl_attr_next(&nested, &type, &len, &payload));
 	assert(type == 2);
-	assert(len == 16);
-	assert(!memcmp(payload, kck, 16));
+	assert(len == sizeof(test->kck));
+	assert(!memcmp(payload, test->kck, sizeof(test->kck)));
 
 	assert(l_genl_attr_next(&nested, &type, &len, &payload));
 	assert(type == 3);
-	assert(len == 8);
-	assert(!memcmp(payload, replay_counter, 8));
+	assert(len == sizeof(test->replay_counter));
+	assert(!memcmp(payload, test->replay_counter, len));
 
 	l_genl_msg_unref(msg);
 }
 
 static void build_set_rekey_offload(const void *data)
 {
-	static uint32_t index = 3;
-	static const unsigned char kek[] = {
-		0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b,
-		0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b };
-	static const unsigned char kck[] = {
-		0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d,
-		0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 };
-	static const unsigned char replay_counter[] = {
-		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
+	const struct set_rekey_offload_test *test = data;
 	struct l_genl_msg *msg;
 	const void *raw;
 	size_t size;
@@ -206,16 +227,19 @@ static void build_set_rekey_offload(const void *data)
 	msg = l_genl_msg_new_sized(79, 0);
 	assert(msg);
 
-	assert(l_genl_msg_append_attr(msg, 3, 4, &index));
+	assert(l_genl_msg_append_attr(msg, 3,
+					sizeof(test->ifindex), &test->ifindex));
 
 	assert(l_genl_msg_enter_nested(msg, 122));
-	assert(l_genl_msg_append_attr(msg, 1, 16, kek));
-	assert(l_genl_msg_append_attr(msg, 2, 16, kck));
-	assert(l_genl_msg_append_attr(msg, 3, 8, replay_counter));
+	assert(l_genl_msg_append_attr(msg, 1, sizeof(test->kek), test->kek));
+	assert(l_genl_msg_append_attr(msg, 2, sizeof(test->kck), test->kck));
+	assert(l_genl_msg_append_attr(msg, 3, sizeof(test->replay_counter),
+						test->replay_counter));
 	assert(l_genl_msg_leave_nested(msg));
 
-	raw = l_genl_msg_to_data(msg, 0x1b, 0x05, 0x53e1a359, 0xe74002ba,
-					&size);
+	raw = l_genl_msg_to_data(msg, 0x1b, NLM_F_REQUEST | NLM_F_ACK,
+					test->seq, test->pid, &size);
+
 	if (do_print) {
 		l_util_hexdump(false, raw, size, do_debug, "[MSG] ");
 		l_util_hexdump(true, set_rekey_offload_request, size,
@@ -412,13 +436,13 @@ int main(int argc, char *argv[])
 	if (!little_endian)
 		goto done;
 
-	l_test_add("Parse Set Station Request", parse_set_station, NULL);
+	l_test_add("Parse Set Station Request", parse_set_station, &set_station);
 	l_test_add("Parse Set Rekey Offload Request",
-				parse_set_rekey_offload, NULL);
+				parse_set_rekey_offload, &rekey_offload);
 
-	l_test_add("Build Set Station Request", build_set_station, NULL);
+	l_test_add("Build Set Station Request", build_set_station, &set_station);
 	l_test_add("Build Set Rekey Offload Request",
-				build_set_rekey_offload, NULL);
+				build_set_rekey_offload, &rekey_offload);
 
 	l_test_add("libnl-generated Example with Nesting",
 				parse_libnl_nested, NULL);
Signature · 1 line
2.45.2
#5 lore raw

PATCH 6/12 netlink: Add l_netlink_message class

In reply to Denis Kenzior

Initial implementation of the l_netlink_message class. For now, only basic message building is supported. Both RTNL (rtnetlink) and Generic Netlink (genetlink) messages can be created by including the appropriate header using l_netlink_message_add_header.

---
 ell/ell.sym           |   6 ++
 ell/netlink-private.h |  11 +++
 ell/netlink.c         | 174 ++++++++++++++++++++++++++++++++++++++++++
 ell/netlink.h         |  12 +++
 4 files changed, 203 insertions(+)
ell/ell.sym +6 -0
index bc0c6e10ebca..247786d5ee98 100644
@@ -434,6 +434,12 @@ global:
 	l_netlink_register;
 	l_netlink_unregister;
 	l_netlink_set_debug;
+	l_netlink_message_new;
+	l_netlink_message_new_sized;
+	l_netlink_message_ref;
+	l_netlink_message_unref;
+	l_netlink_message_append;
+	l_netlink_message_add_header;
 	/* path */
 	l_basename;
 	l_path_find;
ell/netlink-private.h +11 -0
index fdb584e62652..d04af749b9fb 100644
@@ -31,6 +31,17 @@ enum nlmsgerr_attrs {
 #define NLM_F_ACK_TLVS 0x200
 #endif
 
+struct l_netlink_message {
+	int ref_count;
+	uint32_t size;
+	union { /* The actual data */
+		struct nlmsghdr *hdr;
+		void *data;
+	};
+};
+
 bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg,
 					const char **out_error_msg,
 					uint32_t *out_error_offset);
+int netlink_message_reserve_header(struct l_netlink_message *message,
+					size_t header_len, void **out_header);
ell/netlink.c +174 -0

183 lines not on this page · raw message

ell/netlink.h +12 -0
index 3f3dc50e14be..70a55d652560 100644
@@ -44,6 +44,18 @@ bool l_netlink_set_debug(struct l_netlink *netlink,
 			l_netlink_debug_func_t function,
 			void *user_data, l_netlink_destroy_func_t destroy);
 
+struct l_netlink_message *l_netlink_message_new(uint16_t type, uint16_t flags);
+struct l_netlink_message *l_netlink_message_new_sized(uint16_t type,
+							uint16_t flags,
+							size_t initial_size);
+struct l_netlink_message *l_netlink_message_ref(
+					struct l_netlink_message *message);
+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_add_header(struct l_netlink_message *message,
+					const void *header, size_t len);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.45.2
#6 lore raw

PATCH 7/12 netlink: support appending using scatter / gather

In reply to Denis Kenzior

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(+)
ell/ell.sym +1 -0
index 247786d5ee98..b99b6e552eaa 100644
@@ -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;
ell/netlink.c +34 -0
index 39c5bc335e19..d7294a95f20d 100644
@@ -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)
 {
ell/netlink.h +3 -0
index 70a55d652560..4d1c4761b3bb 100644
@@ -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);
 
Signature · 1 line
2.45.2
#7 lore raw

PATCH 8/12 netlink: Support adding nested attributes to messages

In reply to Denis Kenzior
---
 ell/ell.sym           |  2 ++
 ell/netlink-private.h |  2 ++
 ell/netlink.c         | 54 +++++++++++++++++++++++++++++++++++++++++++
 ell/netlink.h         |  3 +++
 4 files changed, 61 insertions(+)
ell/ell.sym +2 -0
index b99b6e552eaa..0dad73f5546e 100644
@@ -441,6 +441,8 @@ global:
 	l_netlink_message_append;
 	l_netlink_message_appendv;
 	l_netlink_message_add_header;
+	l_netlink_message_enter_nested;
+	l_netlink_message_leave_nested;
 	/* path */
 	l_basename;
 	l_path_find;
ell/netlink-private.h +2 -0
index d04af749b9fb..db4317786a8f 100644
@@ -38,6 +38,8 @@ struct l_netlink_message {
 		struct nlmsghdr *hdr;
 		void *data;
 	};
+	uint32_t nest_offset[4];
+	uint8_t nest_level;
 };
 
 bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg,
ell/netlink.c +54 -0

63 lines not on this page · raw message

ell/netlink.h +3 -0
index 4d1c4761b3bb..593cddf1aee8 100644
@@ -58,6 +58,9 @@ int l_netlink_message_appendv(struct l_netlink_message *message,
 					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);
+int l_netlink_message_enter_nested(struct l_netlink_message *message,
+					uint16_t type);
+int l_netlink_message_leave_nested(struct l_netlink_message *message);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.45.2
#8 lore raw

PATCH 9/12 netlink: Add additional helpers

In reply to Denis Kenzior

Helpers around l_netlink_message_append for use with common data types.

---
 ell/netlink.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
ell/netlink.h +55 -0
index 593cddf1aee8..e7962b9932dc 100644
@@ -62,6 +62,61 @@ int l_netlink_message_enter_nested(struct l_netlink_message *message,
 					uint16_t type);
 int l_netlink_message_leave_nested(struct l_netlink_message *message);
 
+static inline int l_netlink_message_append_u8(struct l_netlink_message *message,
+						uint16_t type, uint8_t u8)
+{
+	return l_netlink_message_append(message, type, &u8, sizeof(uint8_t));
+}
+
+static inline int l_netlink_message_append_u16(struct l_netlink_message *message,
+						uint16_t type, uint16_t u16)
+{
+	return l_netlink_message_append(message, type, &u16, sizeof(uint16_t));
+}
+
+static inline int l_netlink_message_append_u32(struct l_netlink_message *message,
+						uint16_t type, uint32_t u32)
+{
+	return l_netlink_message_append(message, type, &u32, sizeof(uint32_t));
+}
+
+static inline int l_netlink_message_append_u64(struct l_netlink_message *message,
+						uint16_t type, uint64_t u64)
+{
+	return l_netlink_message_append(message, type, &u64, sizeof(uint64_t));
+}
+
+static inline int l_netlink_message_append_s8(struct l_netlink_message *message,
+						uint16_t type, int8_t s8)
+{
+	return l_netlink_message_append(message, type, &s8, sizeof(int8_t));
+}
+
+static inline int l_netlink_message_append_s16(struct l_netlink_message *message,
+						uint16_t type, int16_t s16)
+{
+	return l_netlink_message_append(message, type, &s16, sizeof(int16_t));
+}
+
+static inline int l_netlink_message_append_s32(struct l_netlink_message *message,
+						uint16_t type, int32_t s32)
+{
+	return l_netlink_message_append(message, type, &s32, sizeof(int32_t));
+}
+
+static inline int l_netlink_message_append_s64(struct l_netlink_message *message,
+						uint16_t type, int64_t s64)
+{
+	return l_netlink_message_append(message, type, &s64, sizeof(int64_t));
+}
+
+static inline int l_netlink_message_append_mac(struct l_netlink_message *message,
+						uint16_t type,
+						const uint8_t mac[static 6])
+{
+	return l_netlink_message_append(message, type, mac, 6);
+}
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.45.2
#9 lore raw

PATCH 10/12 unit: Add netlink message builder / parser tests

In reply to Denis Kenzior

These tests are similar to l_genl_msg based tests, but use l_netlink_message APIs directly.

---
 unit/test-genl-msg.c | 74 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
unit/test-genl-msg.c +74 -0
index e6432f4362ef..3d996f4e242a 100644
@@ -14,6 +14,8 @@
 #include <linux/genetlink.h>
 #include <ell/ell.h>
 
+#include "ell/netlink-private.h"
+
 static bool do_print = false;
 
 static void do_debug(const char *str, void *user_data)
@@ -126,6 +128,38 @@ static void build_set_station(const void *data)
 	l_genl_msg_unref(msg);
 }
 
+static void build_set_station_netlink(const void *data)
+{
+	const struct set_station_test *test = data;
+	struct l_netlink_message *m;
+	struct genlmsghdr genlhdr = { .cmd = 18, .version = 0, .reserved = 0 };
+
+	m = l_netlink_message_new(0x17, NLM_F_REQUEST | NLM_F_ACK);
+	assert(m);
+
+	assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr)));
+	assert(!l_netlink_message_append_u32(m, 3, test->ifindex));
+	assert(!l_netlink_message_append_mac(m, 6, test->mac));
+	assert(!l_netlink_message_append(m, 67,
+					test->flags, sizeof(test->flags)));
+
+	m->hdr->nlmsg_seq = test->seq;
+	m->hdr->nlmsg_pid = test->pid;
+
+	if (do_print) {
+		l_util_hexdump(false, m->data, m->hdr->nlmsg_len,
+					do_debug, "[MSG] ");
+		l_util_hexdump(true, set_station_request,
+				sizeof(set_station_request), do_debug, "[MSG] ");
+	}
+
+	assert(m->hdr->nlmsg_len == sizeof(set_station_request));
+	assert(!memcmp(m->data, set_station_request,
+					sizeof(set_station_request)));
+
+	l_netlink_message_unref(m);
+}
+
 static const unsigned char set_rekey_offload_request[] = {
 	0x54, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x05, 0x00, 0x59, 0xa3, 0xe1, 0x53,
 	0xba, 0x02, 0x40, 0xe7, 0x4f, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
@@ -252,6 +286,42 @@ static void build_set_rekey_offload(const void *data)
 	l_genl_msg_unref(msg);
 }
 
+static void build_set_rekey_offload_netlink(const void *data)
+{
+	const struct set_rekey_offload_test *test = data;
+	struct l_netlink_message *m;
+	struct genlmsghdr genlhdr = { .cmd = 79, .version = 0, .reserved = 0 };
+
+	m = l_netlink_message_new(0x1b, NLM_F_REQUEST | NLM_F_ACK);
+	assert(m);
+
+	assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr)));
+	assert(!l_netlink_message_append_u32(m, 3, test->ifindex));
+	assert(!l_netlink_message_enter_nested(m, 122));
+	assert(!l_netlink_message_append(m, 1, test->kek, sizeof(test->kek)));
+	assert(!l_netlink_message_append(m, 2, test->kck, sizeof(test->kck)));
+	assert(!l_netlink_message_append(m, 3, test->replay_counter,
+						sizeof(test->replay_counter)));
+	assert(!l_netlink_message_leave_nested(m));
+
+	m->hdr->nlmsg_seq = test->seq;
+	m->hdr->nlmsg_pid = test->pid;
+
+	if (do_print) {
+		l_util_hexdump(false, m->data, m->hdr->nlmsg_len,
+					do_debug, "[MSG] ");
+		l_util_hexdump(true, set_rekey_offload_request,
+				sizeof(set_rekey_offload_request),
+				do_debug, "[MSG] ");
+	}
+
+	assert(m->hdr->nlmsg_len == sizeof(set_rekey_offload_request));
+	assert(!memcmp(m->data, set_rekey_offload_request,
+					sizeof(set_rekey_offload_request)));
+
+	l_netlink_message_unref(m);
+}
+
 /*
  * This example is generated by libnl:
 	msg = nlmsg_alloc();
@@ -441,8 +511,12 @@ int main(int argc, char *argv[])
 				parse_set_rekey_offload, &rekey_offload);
 
 	l_test_add("Build Set Station Request", build_set_station, &set_station);
+	l_test_add("Build Set Station Request (Netlink)",
+			build_set_station_netlink, &set_station);
 	l_test_add("Build Set Rekey Offload Request",
 				build_set_rekey_offload, &rekey_offload);
+	l_test_add("Build Set Rekey Offload Request (Netlink)",
+			build_set_rekey_offload_netlink, &rekey_offload);
 
 	l_test_add("libnl-generated Example with Nesting",
 				parse_libnl_nested, NULL);
Signature · 1 line
2.45.2
#10 lore raw

PATCH 11/12 netlink: add netlink_message_from_nlmsg

In reply to Denis Kenzior

This is a low level utility to create a l_netlink_message from raw data represented by struct nlmsghdr. Typically used on the RX path when reading from the socket, or by unit tests.

---
 ell/netlink-private.h |  2 ++
 ell/netlink.c         | 11 +++++++++++
 2 files changed, 13 insertions(+)
ell/netlink-private.h +2 -0
index db4317786a8f..b114f62f6bf3 100644
@@ -47,3 +47,5 @@ bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg,
 					uint32_t *out_error_offset);
 int netlink_message_reserve_header(struct l_netlink_message *message,
 					size_t header_len, void **out_header);
+struct l_netlink_message *netlink_message_from_nlmsg(
+						const struct nlmsghdr *nlmsg);
ell/netlink.c +11 -0
index f73b876b4ef0..024740b62fc3 100644
@@ -781,6 +781,17 @@ LIB_EXPORT struct l_netlink_message *l_netlink_message_new(uint16_t type,
 	return l_netlink_message_new_sized(type, flags, 256 - NLMSG_HDRLEN);
 }
 
+struct l_netlink_message *netlink_message_from_nlmsg(
+						const struct nlmsghdr *nlmsg)
+{
+	struct l_netlink_message *message = l_new(struct l_netlink_message, 1);
+
+	message->hdr = l_memdup(nlmsg, nlmsg->nlmsg_len);
+	message->size = nlmsg->nlmsg_len;
+
+	return l_netlink_message_ref(message);
+}
+
 LIB_EXPORT struct l_netlink_message *l_netlink_message_ref(
 					struct l_netlink_message *message)
 {
Signature · 1 line
2.45.2
#11 lore raw

PATCH 12/12 genl: Use l_netlink_message as implementation for l_genl_msg

In reply to Denis Kenzior

This switches the underlying l_genl_msg implementation to l_netlink_message.

---
 ell/genl.c | 146 ++++++++++-------------------------------------------
 1 file changed, 28 insertions(+), 118 deletions(-)
ell/genl.c +28 -118
index d2c6ea15dfa9..5a8dd371a970 100644
@@ -19,20 +19,15 @@
 #include "queue.h"
 #include "io.h"
 #include "private.h"
+#include "netlink.h"
 #include "netlink-private.h"
 #include "notifylist.h"
 #include "genl.h"
 
-#define MAX_NESTING_LEVEL 4
 #define GENL_DEBUG(fmt, args...)	\
 	l_util_debug(genl->debug_callback, genl->debug_data, "%s:%i " fmt, \
 			__func__, __LINE__, ## args)
 
-struct nest_info {
-	uint16_t type;
-	uint16_t offset;
-};
-
 struct unicast_watch {
 	struct l_notifylist_entry super;
 	char name[GENL_NAMSIZ];
@@ -89,11 +84,7 @@ struct l_genl_msg {
 	char *error_msg;
 	uint8_t cmd;
 	uint8_t version;
-	void *data;
-	uint32_t size;
-	uint32_t len;
-	struct nest_info nests[MAX_NESTING_LEVEL];
-	uint8_t nesting_level;
+	struct l_netlink_message *nlm;
 };
 
 struct genl_request {
@@ -714,44 +705,6 @@ static bool match_request_hid(const void *a, const void *b)
 	return request->handle_id == id;
 }
 
-static struct l_genl_msg *msg_alloc(uint8_t cmd, uint8_t version, uint32_t size)
-{
-	struct l_genl_msg *msg;
-
-	msg = l_new(struct l_genl_msg, 1);
-
-	msg->cmd = cmd;
-	msg->version = version;
-
-	msg->len = NLMSG_HDRLEN + GENL_HDRLEN;
-	msg->size = msg->len + NLMSG_ALIGN(size);
-
-	msg->data = l_realloc(NULL, msg->size);
-	memset(msg->data, 0, msg->size);
-	msg->nesting_level = 0;
-
-	return l_genl_msg_ref(msg);
-}
-
-static bool msg_grow(struct l_genl_msg *msg, uint32_t needed)
-{
-	uint32_t grow_by;
-
-	if (msg->size >= msg->len + needed)
-		return true;
-
-	grow_by = msg->len + needed - msg->size;
-
-	if (grow_by < 32)
-		grow_by = 128;
-
-	msg->data = l_realloc(msg->data, msg->size + grow_by);
-	memset(msg->data + msg->size, 0, grow_by);
-	msg->size += grow_by;
-
-	return true;
-}
-
 static struct l_genl_msg *msg_create(const struct nlmsghdr *nlmsg)
 {
 	struct l_genl_msg *msg;
@@ -771,13 +724,10 @@ static struct l_genl_msg *msg_create(const struct nlmsghdr *nlmsg)
 		goto done;
 	}
 
-	msg->data = l_memdup(nlmsg, nlmsg->nlmsg_len);
+	msg->nlm = netlink_message_from_nlmsg(nlmsg);
 
-	msg->len = nlmsg->nlmsg_len;
-	msg->size = nlmsg->nlmsg_len;
-
-	if (msg->len >= GENL_HDRLEN) {
-		struct genlmsghdr *genlmsg = msg->data + NLMSG_HDRLEN;
+	if (nlmsg->nlmsg_len >= NLMSG_LENGTH(GENL_HDRLEN)) {
+		struct genlmsghdr *genlmsg = msg->nlm->data + NLMSG_HDRLEN;
 
 		msg->cmd = genlmsg->cmd;
 		msg->version = genlmsg->version;
@@ -794,23 +744,23 @@ static const void *msg_as_bytes(struct l_genl_msg *msg, uint16_t type,
 	struct nlmsghdr *nlmsg;
 	struct genlmsghdr *genlmsg;
 
-	nlmsg = msg->data;
+	nlmsg = msg->nlm->data;
 
-	nlmsg->nlmsg_len = msg->len;
 	nlmsg->nlmsg_type = type;
 	nlmsg->nlmsg_flags = flags;
 	nlmsg->nlmsg_seq = seq;
 	nlmsg->nlmsg_pid = pid;
 
-	genlmsg = msg->data + NLMSG_HDRLEN;
+	genlmsg = msg->nlm->data + NLMSG_HDRLEN;
 
 	genlmsg->cmd = msg->cmd;
 	genlmsg->version = msg->version;
+	genlmsg->reserved = 0;
 
 	if (out_size)
-		*out_size = msg->len;
+		*out_size = nlmsg->nlmsg_len;
 
-	return msg->data;
+	return msg->nlm->data;
 }
 
 static void write_watch_destroy(void *user_data)
@@ -842,7 +792,7 @@ static bool can_write_data(struct l_io *io, void *user_data)
 		return false;
 	}
 
-	l_util_hexdump(false, request->msg->data, bytes_written,
+	l_util_hexdump(false, data, bytes_written,
 				genl->debug_callback, genl->debug_data);
 
 	l_queue_push_tail(genl->pending_list, request);
@@ -1472,7 +1422,14 @@ LIB_EXPORT struct l_genl_msg *l_genl_msg_new(uint8_t cmd)
 
 LIB_EXPORT struct l_genl_msg *l_genl_msg_new_sized(uint8_t cmd, uint32_t size)
 {
-	return msg_alloc(cmd, 0x00, size);
+	struct l_genl_msg *msg = l_new(struct l_genl_msg, 1);
+
+	msg->cmd = cmd;
+	msg->nlm = l_netlink_message_new_sized(0, 0, size + GENL_HDRLEN);
+	netlink_message_reserve_header(msg->nlm,
+					sizeof(struct genlmsghdr), NULL);
+
+	return l_genl_msg_ref(msg);
 }
 
 LIB_EXPORT struct l_genl_msg *l_genl_msg_new_from_data(const void *data,
@@ -1516,7 +1473,7 @@ LIB_EXPORT void l_genl_msg_unref(struct l_genl_msg *msg)
 		return;
 
 	l_free(msg->error_msg);
-	l_free(msg->data);
+	l_netlink_message_unref(msg->nlm);
 	l_free(msg);
 }
 
@@ -1555,23 +1512,12 @@ LIB_EXPORT const char *l_genl_msg_get_extended_error(struct l_genl_msg *msg)
 LIB_EXPORT bool l_genl_msg_append_attr(struct l_genl_msg *msg, uint16_t type,
 						uint16_t len, const void *data)
 {
-	struct nlattr *nla;
-
 	if (unlikely(!msg))
 		return false;
 
-	if (!msg_grow(msg, NLA_HDRLEN + NLA_ALIGN(len)))
+	if (l_netlink_message_append(msg->nlm, type, data, len) < 0)
 		return false;
 
-	nla = msg->data + msg->len;
-	nla->nla_len = NLA_HDRLEN + len;
-	nla->nla_type = type;
-
-	if (len)
-		memcpy(msg->data + msg->len + NLA_HDRLEN, data, len);
-
-	msg->len += NLA_HDRLEN + NLA_ALIGN(len);
-
 	return true;
 }
 
@@ -1579,32 +1525,12 @@ LIB_EXPORT bool l_genl_msg_append_attrv(struct l_genl_msg *msg, uint16_t type,
 					const struct iovec *iov,
 					size_t iov_len)
 {
-	struct nlattr *nla;
-	size_t len = 0;
-	unsigned int i;
-
 	if (unlikely(!msg))
 		return false;
 
-	for (i = 0; i < iov_len; i++)
-		len += iov[i].iov_len;
-
-	if (!msg_grow(msg, NLA_HDRLEN + NLA_ALIGN(len)))
+	if (l_netlink_message_appendv(msg->nlm, type, iov, iov_len) < 0)
 		return false;
 
-	nla = msg->data + msg->len;
-	nla->nla_len = NLA_HDRLEN + len;
-	nla->nla_type = type;
-
-	msg->len += NLA_HDRLEN;
-
-	for (i = 0; i < iov_len; i++, iov++) {
-		memcpy(msg->data + msg->len, iov->iov_base, iov->iov_len);
-		msg->len += iov->iov_len;
-	}
-
-	msg->len += NLA_ALIGN(len) - len;
-
 	return true;
 }
 
@@ -1613,37 +1539,20 @@ LIB_EXPORT bool l_genl_msg_enter_nested(struct l_genl_msg *msg, uint16_t type)
 	if (unlikely(!msg))
 		return false;
 
-	if (unlikely(msg->nesting_level == MAX_NESTING_LEVEL))
-		return false;
-
-	if (!msg_grow(msg, NLA_HDRLEN))
+	if (l_netlink_message_enter_nested(msg->nlm, type) < 0)
 		return false;
 
-	msg->nests[msg->nesting_level].type = type | NLA_F_NESTED;
-	msg->nests[msg->nesting_level].offset = msg->len;
-	msg->nesting_level += 1;
-
-	msg->len += NLA_HDRLEN;
-
 	return true;
 }
 
 LIB_EXPORT bool l_genl_msg_leave_nested(struct l_genl_msg *msg)
 {
-	struct nlattr *nla;
-
 	if (unlikely(!msg))
 		return false;
 
-	if (unlikely(msg->nesting_level == 0))
+	if (l_netlink_message_leave_nested(msg->nlm) < 0)
 		return false;
 
-	nla = msg->data + msg->nests[msg->nesting_level - 1].offset;
-	nla->nla_type = msg->nests[msg->nesting_level - 1].type;
-	nla->nla_len = msg->len - msg->nests[msg->nesting_level - 1].offset;
-
-	msg->nesting_level -= 1;
-
 	return true;
 }
 
@@ -1656,11 +1565,12 @@ LIB_EXPORT bool l_genl_attr_init(struct l_genl_attr *attr,
 	if (unlikely(!attr) || unlikely(!msg))
 		return false;
 
-	if (!msg->data || msg->len < NLMSG_HDRLEN + GENL_HDRLEN)
+	if (!msg->nlm ||
+			msg->nlm->hdr->nlmsg_len < NLMSG_HDRLEN + GENL_HDRLEN)
 		return false;
 
-	nla = msg->data + NLMSG_HDRLEN + GENL_HDRLEN;
-	len = msg->len - NLMSG_HDRLEN - GENL_HDRLEN;
+	nla = msg->nlm->data + NLMSG_HDRLEN + GENL_HDRLEN;
+	len = msg->nlm->hdr->nlmsg_len - NLMSG_HDRLEN - GENL_HDRLEN;
 
 	if (!NLA_OK(nla, len))
 		return false;
Signature · 1 line
2.45.2