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 2/6] batctl: throughputmeter: Use global genl socket
Date: Mon, 10 May 2021 21:49:41 +0200	[thread overview]
Message-ID: <20210510194945.103735-3-sven@narfation.org> (raw)
In-Reply-To: <20210510194945.103735-1-sven@narfation.org>

The batctl framework can take care of creating the netlink socket and
gathering the data for a mesh interface. It is not necessary to duplicate
all the code to create, setup and destroy the batadv genl socket for a
standard socket.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 throughputmeter.c | 105 ++++++++++------------------------------------
 1 file changed, 21 insertions(+), 84 deletions(-)

diff --git a/throughputmeter.c b/throughputmeter.c
index f50f521..7f89651 100644
--- a/throughputmeter.c
+++ b/throughputmeter.c
@@ -34,7 +34,7 @@
 #include "netlink.h"
 
 static struct ether_addr *dst_mac;
-static char *tp_mesh_iface;
+static struct state *tp_state;
 
 struct tp_result {
 	int error;
@@ -151,62 +151,34 @@ static int tp_meter_cookie_callback(struct nl_msg *msg, void *arg)
 	return NL_OK;
 }
 
-static int tp_meter_start(char *mesh_iface, struct ether_addr *dst_mac,
+
+static int tp_meter_start(struct state *state, struct ether_addr *dst_mac,
 			  uint32_t time, struct tp_cookie *cookie)
 {
-	struct nl_sock *sock;
 	struct nl_msg *msg;
 	struct nl_cb *cb;
-	int ifindex;
-	int family;
-	int ret;
 	int err = 0;
 
-	sock = nl_socket_alloc();
-	if (!sock)
-		return -ENOMEM;
-
-	ret = genl_connect(sock);
-	if (ret < 0) {
-		err = -EOPNOTSUPP;
-		goto out;
-	}
-
-	family = genl_ctrl_resolve(sock, BATADV_NL_NAME);
-	if (family < 0) {
-		err = -EOPNOTSUPP;
-		goto out;
-	}
-
-	ifindex = if_nametoindex(mesh_iface);
-	if (!ifindex) {
-		fprintf(stderr, "Interface %s is unknown\n", mesh_iface);
-		err = -ENODEV;
-		goto out;
-	}
-
 	cb = nl_cb_alloc(NL_CB_DEFAULT);
 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, tp_meter_cookie_callback,
 		  cookie);
 	nl_cb_err(cb, NL_CB_CUSTOM, tpmeter_nl_print_error, cookie);
 
 	msg = nlmsg_alloc();
-	if (!msg) {
-		err = -ENOMEM;
-		goto out;
-	}
+	if (!msg)
+		return -ENOMEM;
 
-	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
+	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, state->batadv_family, 0,
 		    0, BATADV_CMD_TP_METER, 1);
 
-	nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, ifindex);
+	nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, state->mesh_ifindex);
 	nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst_mac);
 	nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, time);
 
-	nl_send_auto_complete(sock, msg);
+	nl_send_auto_complete(state->sock, msg);
 	nlmsg_free(msg);
 
-	nl_recvmsgs(sock, cb);
+	nl_recvmsgs(state->sock, cb);
 
 	nl_cb_put(cb);
 
@@ -215,9 +187,6 @@ static int tp_meter_start(char *mesh_iface, struct ether_addr *dst_mac,
 	else if (!cookie->found)
 		err= -EINVAL;
 
-out:
-	nl_socket_free(sock);
-
 	return err;
 }
 
@@ -251,57 +220,24 @@ static int tp_recv_result(struct nl_sock *sock, struct tp_result *result)
 	return err;
 }
 
-static int tp_meter_stop(char *mesh_iface, struct ether_addr *dst_mac)
+static int tp_meter_stop(struct state *state, struct ether_addr *dst_mac)
 {
-	struct nl_sock *sock;
 	struct nl_msg *msg;
-	int ifindex;
-	int family;
-	int ret;
-	int err = 0;
-
-	sock = nl_socket_alloc();
-	if (!sock)
-		return -ENOMEM;
-
-	ret = genl_connect(sock);
-	if (ret < 0) {
-		err = -EOPNOTSUPP;
-		goto out;
-	}
-
-	family = genl_ctrl_resolve(sock, BATADV_NL_NAME);
-	if (family < 0) {
-		err = -EOPNOTSUPP;
-		goto out;
-	}
-
-	ifindex = if_nametoindex(mesh_iface);
-	if (!ifindex) {
-		fprintf(stderr, "Interface %s is unknown\n", mesh_iface);
-		err = -ENODEV;
-		goto out;
-	}
 
 	msg = nlmsg_alloc();
-	if (!msg) {
-		err = -ENOMEM;
-		goto out;
-	}
+	if (!msg)
+		return -ENOMEM;
 
-	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
+	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, state->batadv_family, 0,
 		    0, BATADV_CMD_TP_METER_CANCEL, 1);
 
-	nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, ifindex);
+	nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, state->mesh_ifindex);
 	nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst_mac);
 
-	nl_send_auto_complete(sock, msg);
+	nl_send_auto_complete(state->sock, msg);
 	nlmsg_free(msg);
 
-out:
-	nl_socket_free(sock);
-
-	return err;
+	return 0;
 }
 
 static struct nl_sock *tp_prepare_listening_sock(void)
@@ -358,7 +294,7 @@ void tp_sig_handler(int sig)
 	case SIGINT:
 	case SIGTERM:
 		fflush(stdout);
-		tp_meter_stop(tp_mesh_iface, dst_mac);
+		tp_meter_stop(tp_state, dst_mac);
 		break;
 	default:
 		break;
@@ -443,7 +379,7 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 		dst_string = ether_ntoa_long(dst_mac);
 
 	/* for sighandler */
-	tp_mesh_iface = state->mesh_iface;
+	tp_state = state;
 	signal(SIGINT, tp_sig_handler);
 	signal(SIGTERM, tp_sig_handler);
 
@@ -451,7 +387,7 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 	if (!listen_sock)
 		goto out;
 
-	ret = tp_meter_start(state->mesh_iface, dst_mac, time, &cookie);
+	ret = tp_meter_start(state, dst_mac, time, &cookie);
 	if (ret < 0) {
 		printf("Failed to send tp_meter request to kernel: %d\n", ret);
 		goto out;
@@ -529,5 +465,6 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 	return ret;
 }
 
-COMMAND(SUBCOMMAND_MIF, throughputmeter, "tp", COMMAND_FLAG_MESH_IFACE, NULL,
+COMMAND(SUBCOMMAND_MIF, throughputmeter, "tp",
+	COMMAND_FLAG_MESH_IFACE | COMMAND_FLAG_NETLINK, NULL,
 	"<destination>     \tstart a throughput measurement");
-- 
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 ` Sven Eckelmann [this message]
2021-05-10 19:49 ` [PATCH 3/6] batctl: interface: List using shared genl socket Sven Eckelmann
2021-05-10 19:49 ` [PATCH 4/6] batctl: Get meshif info " Sven Eckelmann
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-3-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).