b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Sven Eckelmann <sven@narfation.org>
Subject: [PATCH 4/6] batctl: Get meshif info using shared genl socket
Date: Mon, 10 May 2021 21:49:43 +0200	[thread overview]
Message-ID: <20210510194945.103735-5-sven@narfation.org> (raw)
In-Reply-To: <20210510194945.103735-1-sven@narfation.org>

All subcommands which are using the netlink_get_info helper are already
creating state object with batadv genl socket. It is not necessary to
create a new one just to request data from the kernel.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 gateways.c    |  3 +--
 mcast_flags.c |  3 +--
 netlink.c     | 40 +++++++++++++---------------------------
 netlink.h     |  2 +-
 originators.c |  3 +--
 5 files changed, 17 insertions(+), 34 deletions(-)

diff --git a/gateways.c b/gateways.c
index 7625bd8..bdd6795 100644
--- a/gateways.c
+++ b/gateways.c
@@ -122,8 +122,7 @@ static int netlink_print_gateways(struct state *state, char *orig_iface,
 
 	/* only parse routing algorithm name */
 	last_err = -EINVAL;
-	info_header = netlink_get_info(state->mesh_ifindex,
-				       BATADV_CMD_GET_ORIGINATORS, NULL);
+	info_header = netlink_get_info(state, BATADV_CMD_GET_ORIGINATORS, NULL);
 	free(info_header);
 
 	if (strlen(algo_name_buf) == 0)
diff --git a/mcast_flags.c b/mcast_flags.c
index 721f549..44344e0 100644
--- a/mcast_flags.c
+++ b/mcast_flags.c
@@ -104,8 +104,7 @@ static int netlink_print_mcast_flags(struct state *state, char *orig_iface,
 	int ret;
 
 	/* only parse own multicast flags */
-	info_header = netlink_get_info(state->mesh_ifindex,
-				       BATADV_CMD_GET_MCAST_FLAGS, NULL);
+	info_header = netlink_get_info(state, BATADV_CMD_GET_MCAST_FLAGS, NULL);
 	free(info_header);
 
 	if (mcast_flags == -EOPNOTSUPP || mcast_flags_priv == -EOPNOTSUPP)
diff --git a/netlink.c b/netlink.c
index 31c9b01..e3a7b7c 100644
--- a/netlink.c
+++ b/netlink.c
@@ -339,60 +339,46 @@ static int info_callback(struct nl_msg *msg, void *arg)
 			opts->remaining_header = NULL;
 	}
 
-	return NL_STOP;
+	return NL_OK;
 }
 
-char *netlink_get_info(int ifindex, uint8_t nl_cmd, const char *header)
+char *netlink_get_info(struct state *state, uint8_t nl_cmd, const char *header)
 {
-	struct nl_sock *sock;
 	struct nl_msg *msg;
 	struct nl_cb *cb;
-	int family;
 	struct print_opts opts = {
 		.read_opt = 0,
 		.nl_cmd = nl_cmd,
 		.remaining_header = NULL,
 		.static_header = header,
 	};
-
-	sock = nl_socket_alloc();
-	if (!sock)
-		return NULL;
-
-	genl_connect(sock);
-
-	family = genl_ctrl_resolve(sock, BATADV_NL_NAME);
-	if (family < 0) {
-		nl_socket_free(sock);
-		return NULL;
-	}
+	int ret;
 
 	msg = nlmsg_alloc();
-	if (!msg) {
-		nl_socket_free(sock);
+	if (!msg)
 		return NULL;
-	}
 
-	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, 0,
+	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, state->batadv_family, 0, 0,
 		    BATADV_CMD_GET_MESH_INFO, 1);
 
-	nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, ifindex);
+	nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, state->mesh_ifindex);
 
-	nl_send_auto_complete(sock, msg);
+	nl_send_auto_complete(state->sock, msg);
 
 	nlmsg_free(msg);
 
 	cb = nl_cb_alloc(NL_CB_DEFAULT);
 	if (!cb)
-		goto err_free_sock;
+		return NULL;
 
 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, info_callback, &opts);
 	nl_cb_err(cb, NL_CB_CUSTOM, netlink_print_error, NULL);
 
-	nl_recvmsgs(sock, cb);
+	ret = nl_recvmsgs(state->sock, cb);
+	if (ret < 0)
+		return opts.remaining_header;
 
-err_free_sock:
-	nl_socket_free(sock);
+	nl_wait_for_ack(state->sock);
 
 	return opts.remaining_header;
 }
@@ -458,7 +444,7 @@ int netlink_print_common(struct state *state, char *orig_iface, int read_opt,
 			printf("\033[2J\033[0;0f");
 
 		if (!(read_opt & SKIP_HEADER))
-			opts.remaining_header = netlink_get_info(state->mesh_ifindex,
+			opts.remaining_header = netlink_get_info(state,
 								 nl_cmd,
 								 header);
 
diff --git a/netlink.h b/netlink.h
index 4ee2f39..48a2a23 100644
--- a/netlink.h
+++ b/netlink.h
@@ -30,7 +30,7 @@ struct ether_addr;
 int netlink_create(struct state *state);
 void netlink_destroy(struct state *state);
 
-char *netlink_get_info(int ifindex, uint8_t nl_cmd, const char *header);
+char *netlink_get_info(struct state *state, uint8_t nl_cmd, const char *header);
 int translate_mac_netlink(const char *mesh_iface, const struct ether_addr *mac,
 			  struct ether_addr *mac_out);
 int get_nexthop_netlink(const char *mesh_iface, const struct ether_addr *mac,
diff --git a/originators.c b/originators.c
index 8a29dd7..a8b313e 100644
--- a/originators.c
+++ b/originators.c
@@ -174,8 +174,7 @@ static int netlink_print_originators(struct state *state, char *orig_iface,
 
 	/* only parse routing algorithm name */
 	last_err = -EINVAL;
-	info_header = netlink_get_info(state->mesh_ifindex,
-				       BATADV_CMD_GET_ORIGINATORS, NULL);
+	info_header = netlink_get_info(state, BATADV_CMD_GET_ORIGINATORS, NULL);
 	free(info_header);
 
 	if (strlen(algo_name_buf) == 0)
-- 
2.30.2


  parent reply	other threads:[~2021-05-10 19:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-10 19:49 [PATCH 0/6] batctl: Use shared batadv genl socket for requests Sven Eckelmann
2021-05-10 19:49 ` [PATCH 1/6] batctl: Consume genl ACKs after setting reads Sven Eckelmann
2021-05-10 19:49 ` [PATCH 2/6] batctl: throughputmeter: Use global genl socket Sven Eckelmann
2021-05-10 19:49 ` [PATCH 3/6] batctl: interface: List using shared " Sven Eckelmann
2021-05-10 19:49 ` Sven Eckelmann [this message]
2021-05-10 19:49 ` [PATCH 5/6] batctl: Use common genl socket for netlink_query_common Sven Eckelmann
2021-05-10 19:49 ` [PATCH 6/6] batctl: routing_algo: List using shared genl socket Sven Eckelmann

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=20210510194945.103735-5-sven@narfation.org \
    --to=sven@narfation.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.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;
as well as URLs for NNTP newsgroup(s).