diff mbox series

[6/8] trace-cmd library: Remove dependency on vsocks from tracecmd_tsync_get_session_params()

Message ID 20220415010007.938408-7-rostedt@goodmis.org (mailing list archive)
State Superseded
Headers show
Series trace-cmd library: Remove dependency to vsockets | expand

Commit Message

Steven Rostedt April 15, 2022, 1 a.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

The only thing the application needs to get from the tsync is really
just the selected protocol. The port can be found elsewhere. Decouple
the two from tracecmd_tsync_get_session_params() and rename it to
tracecmd_tsync_get_selected_proto() just to return the selected
protocol.

This is needed to decouple vsockets from libtracecmd.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 .../include/private/trace-cmd-private.h       |  5 ++-
 lib/trace-cmd/trace-timesync.c                | 20 ++---------
 tracecmd/trace-agent.c                        | 34 ++++++++++++++++---
 3 files changed, 35 insertions(+), 24 deletions(-)
diff mbox series

Patch

diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h
index 06906b04fbd9..1839d82d4e11 100644
--- a/lib/trace-cmd/include/private/trace-cmd-private.h
+++ b/lib/trace-cmd/include/private/trace-cmd-private.h
@@ -499,9 +499,8 @@  int tracecmd_tsync_with_guest_stop(struct tracecmd_time_sync *tsync);
 int tracecmd_tsync_get_offsets(struct tracecmd_time_sync *tsync, int cpu,
 			       int *count, long long **ts,
 			       long long **offsets, long long **scalings, long long **frac);
-int tracecmd_tsync_get_session_params(struct tracecmd_time_sync *tsync,
-				      char **selected_proto,
-				      unsigned int *tsync_port);
+int tracecmd_tsync_get_selected_proto(struct tracecmd_time_sync *tsync,
+				      char **selected_proto);
 void tracecmd_tsync_free(struct tracecmd_time_sync *tsync);
 int tracecmd_write_guest_time_shift(struct tracecmd_output *handle,
 				    struct tracecmd_time_sync *tsync);
diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c
index 2f812ca5fc74..6f16090c2eb4 100644
--- a/lib/trace-cmd/trace-timesync.c
+++ b/lib/trace-cmd/trace-timesync.c
@@ -1098,37 +1098,23 @@  int tracecmd_tsync_with_host_stop(struct tracecmd_time_sync *tsync)
 }
 
 /**
- * tracecmd_tsync_get_session_params - Get parameters of established time sync session
- *
+ * tracecmd_tsync_get_selected_proto - Return the seleceted time sync protocol
  * @tsync: Time sync context, representing a running time sync session
  * @selected_proto: return, name of the selected time sync protocol for this session
- * @tsync_port: return, a VSOCK port on which new time sync requests are accepted.
  *
  * Returns 0 on success, or -1 in case of an error.
  *
  */
-int tracecmd_tsync_get_session_params(struct tracecmd_time_sync *tsync,
-				      char **selected_proto,
-				      unsigned int *tsync_port)
+int tracecmd_tsync_get_selected_proto(struct tracecmd_time_sync *tsync,
+				      char **selected_proto)
 {
-	int ret;
-
 	if (!tsync)
 		return -1;
 
-	if (tsync_port) {
-		if (!tsync->msg_handle)
-			return -1;
-		ret = vsock_get_port(tsync->msg_handle->fd, tsync_port);
-		if (ret < 0)
-			return ret;
-	}
 	if (selected_proto) {
 		if (!tsync->proto_name)
 			return -1;
 		(*selected_proto) = strdup(tsync->proto_name);
-
 	}
-
 	return 0;
 }
diff --git a/tracecmd/trace-agent.c b/tracecmd/trace-agent.c
index 151ca19c2270..1a9463fd4854 100644
--- a/tracecmd/trace-agent.c
+++ b/tracecmd/trace-agent.c
@@ -163,10 +163,32 @@  static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid)
 
 	return 0;
 }
+
+static int vsock_get_port(int sd, unsigned int *port)
+{
+	struct sockaddr_vm addr;
+	socklen_t addr_len = sizeof(addr);
+
+	if (getsockname(sd, (struct sockaddr *)&addr, &addr_len))
+		return -errno;
+
+	if (addr.svm_family != AF_VSOCK)
+		return -EINVAL;
+
+	if (port)
+		*port = addr.svm_port;
+
+	return 0;
+}
 #else
 static inline int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) {
 	return -1;
 }
+
+static int vsock_get_port(int sd, unsigned int *port)
+{
+	return -ENOTSUP;
+}
 #endif
 
 static void agent_handle(int sd, int nr_cpus, int page_size)
@@ -214,11 +236,15 @@  static void agent_handle(int sd, int nr_cpus, int page_size)
 			remote_id = -1;
 			local_id = -2;
 		}
-		tsync = tracecmd_tsync_with_host(tsync_protos,
-						 get_clock(argc, argv),
-						 remote_id, local_id);
+		if (vsock_get_port(msg_handle->fd, &tsync_port) >= 0) {
+			tsync = tracecmd_tsync_with_host(tsync_protos,
+							 get_clock(argc, argv),
+							 remote_id, local_id);
+		} else {
+			tsync = NULL;
+		}
 		if (tsync)
-			tracecmd_tsync_get_session_params(tsync, &tsync_proto, &tsync_port);
+			tracecmd_tsync_get_selected_proto(tsync, &tsync_proto);
 		else
 			warning("Failed to negotiate timestamps synchronization with the host");
 	}