Skip to contentSkip to conversation

genl: Update genl refcount when creating/freeing a family

PATCH In review Andrew Zaborowski 2 participants 5 messages Feb 14, 2020 – Feb 17, 2020 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 genl: Update genl refcount when creating/freeing a family

Since struct l_genl is already refcounted we can hold references to the l_genl object in an l_genl_family to allow the user to free them in arbitrary order.

---
 ell/genl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
ell/genl.c +2 -1
index 431ed05..ca4fa62 100644
@@ -1800,7 +1800,7 @@ LIB_EXPORT struct l_genl_family *l_genl_family_new(struct l_genl *genl,
 	if (!entry)
 		return NULL;
 
-	family = family_alloc(genl, info->id);
+	family = family_alloc(l_genl_ref(genl), info->id);
 
 	return family;
 }
@@ -2059,4 +2059,5 @@ LIB_EXPORT void l_genl_family_free(struct l_genl_family *family)
 	}
 
 	l_free(family);
+	l_genl_unref(genl);
 }
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/4 genl: Export genl-private.h functions as public

Rename and upgrade _genl_msg_create and _genl_msg_as_bytes from -private.h functions (for unit test use only) to public library functions, specifically to be usable in IWD for encoding and decoding raw genl messages.

---
 Makefile.am        |  1 -
 ell/ell.sym        |  2 ++
 ell/genl-private.h | 27 ----------------
 ell/genl.c         | 80 +++++++++++++++++++++++++++++-----------------
 ell/genl.h         |  5 +++
 5 files changed, 57 insertions(+), 58 deletions(-)
 delete mode 100644 ell/genl-private.h
Makefile.am +0 -1
index 3ead678..8e025cb 100644
@@ -83,7 +83,6 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/checksum.c \
 			ell/netlink-private.h \
 			ell/netlink.c \
-			ell/genl-private.h \
 			ell/genl.c \
 			ell/dbus-private.h \
 			ell/dbus.c \
ell/ell.sym +2 -0
index 46df1bd..0c83b87 100644
@@ -258,6 +258,8 @@ global:
 	l_genl_request_family;
 	l_genl_msg_new;
 	l_genl_msg_new_sized;
+	l_genl_msg_new_from_data;
+	l_genl_msg_to_data;
 	l_genl_msg_ref;
 	l_genl_msg_unref;
 	l_genl_msg_get_command;
ell/genl-private.h deleted +0 -27
deleted file mode 100644
index 3c3e525..0000000
@@ -1,27 +0,0 @@
-/*
- *
- *  Embedded Linux library
- *
- *  Copyright (C) 2011-2015  Intel Corporation. All rights reserved.
- *
- *  This library is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU Lesser General Public
- *  License as published by the Free Software Foundation; either
- *  version 2.1 of the License, or (at your option) any later version.
- *
- *  This library is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *  Lesser General Public License for more details.
- *
- *  You should have received a copy of the GNU Lesser General Public
- *  License along with this library; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- */
-
-struct l_genl_msg *_genl_msg_create(const struct nlmsghdr *nlmsg);
-const void *_genl_msg_as_bytes(struct l_genl_msg *msg, uint16_t type,
-					uint16_t flags, uint32_t seq,
-					uint32_t pid,
-					size_t *out_size);
ell/genl.c +50 -30

125 lines not on this page · raw message

ell/genl.h +5 -0
index c3f641f..312a6b2 100644
@@ -85,6 +85,11 @@ struct l_genl_attr {
 
 struct l_genl_msg* l_genl_msg_new(uint8_t cmd);
 struct l_genl_msg *l_genl_msg_new_sized(uint8_t cmd, uint32_t size);
+struct l_genl_msg *l_genl_msg_new_from_data(const void *data, size_t size);
+
+const void *l_genl_msg_to_data(struct l_genl_msg *msg, uint16_t type,
+				uint16_t flags, uint32_t seq, uint32_t pid,
+				size_t *out_size);
 
 struct l_genl_msg *l_genl_msg_ref(struct l_genl_msg *msg);
 void l_genl_msg_unref(struct l_genl_msg *msg);
Signature · 1 line
2.20.1
#2 lore raw

PATCH 3/4 genl: Take advantage of msg_as_bytes in can_write_data

In reply to Andrew Zaborowski
---
 ell/genl.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)
ell/genl.c +5 -17
index 815af90..f65263d 100644
@@ -832,8 +832,8 @@ static bool can_write_data(struct l_io *io, void *user_data)
 {
 	struct l_genl *genl = user_data;
 	struct genl_request *request;
-	struct nlmsghdr *nlmsg;
-	struct genlmsghdr *genlmsg;
+	const void *data;
+	size_t size;
 	ssize_t bytes_written;
 
 	request = l_queue_pop_head(genl->request_queue);
@@ -841,22 +841,10 @@ static bool can_write_data(struct l_io *io, void *user_data)
 		return false;
 
 	request->seq = get_next_id(&genl->next_seq);
+	data = msg_as_bytes(request->msg, request->type, request->flags,
+				request->seq, genl->pid, &size);
 
-	nlmsg = request->msg->data;
-
-	nlmsg->nlmsg_len = request->msg->len;
-	nlmsg->nlmsg_type = request->type;
-	nlmsg->nlmsg_flags = request->flags;
-	nlmsg->nlmsg_seq = request->seq;
-	nlmsg->nlmsg_pid = genl->pid;
-
-	genlmsg = request->msg->data + NLMSG_HDRLEN;
-
-	genlmsg->cmd = request->msg->cmd;
-	genlmsg->version = request->msg->version;
-
-	bytes_written = send(genl->fd, request->msg->data,
-						request->msg->len, 0);
+	bytes_written = send(genl->fd, data, size, 0);
 	if (bytes_written < 0) {
 		l_queue_push_head(genl->request_queue, request);
 		return false;
Signature · 1 line
2.20.1
#3 lore raw

PATCH 4/4 unit: Update calls to functions from genl-private.h

In reply to Andrew Zaborowski
---
 unit/test-genl-msg.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
unit/test-genl-msg.c +7 -8
index b1c5275..119d01e 100644
@@ -29,8 +29,6 @@
 #include <linux/genetlink.h>
 #include <ell/ell.h>
 
-#include "ell/genl-private.h"
-
 static bool do_print = false;
 
 static void do_debug(const char *str, void *user_data)
@@ -61,7 +59,7 @@ static void parse_set_station(const void *data)
 	const void *payload;
 
 	nlmsg = (struct nlmsghdr *) set_station_request;
-	msg = _genl_msg_create(nlmsg);
+	msg = l_genl_msg_new_from_data(nlmsg, sizeof(set_station_request));
 	assert(msg);
 
 	assert(l_genl_msg_get_command(msg) == 18);
@@ -113,7 +111,7 @@ static void build_set_station(const void *data)
 	assert(l_genl_msg_append_attr(msg, 6, 6, mac));
 	assert(l_genl_msg_append_attr(msg, 67, 8, flags));
 
-	raw = _genl_msg_as_bytes(msg, 0x17, 0x05, 0x550d538b, 3604, &size);
+	raw = l_genl_msg_to_data(msg, 0x17, 0x05, 0x550d538b, 3604, &size);
 
 	if (do_print) {
 		l_util_hexdump(false, raw, size, do_debug, "[MSG] ");
@@ -157,7 +155,8 @@ static void parse_set_rekey_offload(const void *data)
 	const void *payload;
 
 	nlmsg = (struct nlmsghdr *) set_rekey_offload_request;
-	msg = _genl_msg_create(nlmsg);
+	msg = l_genl_msg_new_from_data(nlmsg,
+					sizeof(set_rekey_offload_request));
 	assert(msg);
 
 	assert(l_genl_msg_get_command(msg) == 79);
@@ -230,7 +229,7 @@ static void build_set_rekey_offload(const void *data)
 	assert(l_genl_msg_append_attr(msg, 3, 8, replay_counter));
 	assert(l_genl_msg_leave_nested(msg));
 
-	raw = _genl_msg_as_bytes(msg, 0x1b, 0x05, 0x53e1a359, 0xe74002ba,
+	raw = l_genl_msg_to_data(msg, 0x1b, 0x05, 0x53e1a359, 0xe74002ba,
 					&size);
 	if (do_print) {
 		l_util_hexdump(false, raw, size, do_debug, "[MSG] ");
@@ -285,7 +284,7 @@ static void parse_libnl_nested(const void *data)
 	const void *payload;
 
 	nlmsg = (struct nlmsghdr *) libnl_nested;
-	msg = _genl_msg_create(nlmsg);
+	msg = l_genl_msg_new_from_data(nlmsg, sizeof(libnl_nested));
 	assert(msg);
 
 	assert(l_genl_msg_get_command(msg) == 1);
@@ -353,7 +352,7 @@ static void build_libnl_nested(const void *data)
 	assert(l_genl_msg_leave_nested(msg));
 	assert(l_genl_msg_leave_nested(msg));
 
-	raw = _genl_msg_as_bytes(msg, 0x15, 0x05, 0x55130572, 0x0c406877,
+	raw = l_genl_msg_to_data(msg, 0x15, 0x05, 0x55130572, 0x0c406877,
 					&size);
 	if (do_print) {
 		l_util_hexdump(false, raw, size, do_debug, "[MSG] ");
Signature · 1 line
2.20.1
#4 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 7 lines · Andrew Zaborowski

Since struct l_genl is already refcounted we can hold references to the l_genl object in an l_genl_family to allow the user to free them in arbitrary order.

---
  ell/genl.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

All applied, thanks.

Regards,
-Denis