diff mbox series

[v2,5/7] trace-cmd library: Have tracecmd_tsync_with_guest() not depend on cid/port

Message ID 20220417182154.1041513-6-rostedt@goodmis.org (mailing list archive)
State Accepted
Commit 84e434958ea800f02eef543ec845a5fc8cabb252
Headers show
Series trace-cmd library: Remove dependency to vsockets | expand

Commit Message

Steven Rostedt April 17, 2022, 6:21 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Have tracecmd_tsync_with_guest() get passed a file descriptor for
connecting with the guest instead of passing in the cid and port, as
this will allow it to be used for network connections as well.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 .../include/private/trace-cmd-private.h       |  2 +-
 lib/trace-cmd/trace-timesync.c                | 36 ++-----------------
 tracecmd/trace-record.c                       |  7 ++--
 3 files changed, 9 insertions(+), 36 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 24295e4e09d3..06906b04fbd9 100644
--- a/lib/trace-cmd/include/private/trace-cmd-private.h
+++ b/lib/trace-cmd/include/private/trace-cmd-private.h
@@ -493,7 +493,7 @@  tracecmd_tsync_with_host(const struct tracecmd_tsync_protos *tsync_protos,
 int tracecmd_tsync_with_host_stop(struct tracecmd_time_sync *tsync);
 struct tracecmd_time_sync *
 tracecmd_tsync_with_guest(unsigned long long trace_id, int loop_interval,
-			  unsigned int cid, unsigned int port, int guest_pid,
+			  unsigned int fd, int guest_pid,
 			  int guest_cpus, const char *proto_name, const char *clock);
 int tracecmd_tsync_with_guest_stop(struct tracecmd_time_sync *tsync);
 int tracecmd_tsync_get_offsets(struct tracecmd_time_sync *tsync, int cpu,
diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c
index b41aece82082..2f812ca5fc74 100644
--- a/lib/trace-cmd/trace-timesync.c
+++ b/lib/trace-cmd/trace-timesync.c
@@ -345,25 +345,6 @@  error:
 }
 
 #ifdef VSOCK
-static int vsock_open(unsigned int cid, unsigned int port)
-{
-	struct sockaddr_vm addr = {
-		.svm_family = AF_VSOCK,
-		.svm_cid = cid,
-		.svm_port = port,
-	};
-	int sd;
-
-	sd = socket(AF_VSOCK, SOCK_STREAM, 0);
-	if (sd < 0)
-		return -errno;
-
-	if (connect(sd, (struct sockaddr *)&addr, sizeof(addr)))
-		return -errno;
-
-	return sd;
-}
-
 static int vsock_make(void)
 {
 	struct sockaddr_vm addr = {
@@ -388,7 +369,7 @@  static int vsock_make(void)
 	return sd;
 }
 
-int __hidden vsock_get_port(int sd, unsigned int *port)
+static int vsock_get_port(int sd, unsigned int *port)
 {
 	struct sockaddr_vm addr;
 	socklen_t addr_len = sizeof(addr);
@@ -406,11 +387,6 @@  int __hidden vsock_get_port(int sd, unsigned int *port)
 }
 
 #else
-static int vsock_open(unsigned int cid, unsigned int port)
-{
-	return -ENOTSUP;
-}
-
 static int vsock_make(void)
 {
 	return -ENOTSUP;
@@ -811,8 +787,7 @@  static void *tsync_host_thread(void *data)
  * tracecmd_tsync_with_guest - Synchronize timestamps with guest
  *
  * @trace_id: Local ID for the current trace session
- * @cid: CID of the guest
- * @port: VSOCKET port, on which the guest listens for tsync requests
+ * @fd: file descriptor of guest
  * @guest_pid: PID of the host OS process, running the guest
  * @guest_cpus: Number of the guest VCPUs
  * @proto_name: Name of the negotiated time synchronization protocol
@@ -826,14 +801,13 @@  static void *tsync_host_thread(void *data)
  */
 struct tracecmd_time_sync *
 tracecmd_tsync_with_guest(unsigned long long trace_id, int loop_interval,
-			  unsigned int cid, unsigned int port, int guest_pid,
+			  unsigned int fd, int guest_pid,
 			  int guest_cpus, const char *proto_name, const char *clock)
 {
 	struct tracecmd_time_sync *tsync;
 	cpu_set_t *pin_mask = NULL;
 	pthread_attr_t attrib;
 	size_t mask_size = 0;
-	int fd = -1;
 	int ret;
 
 	if (!proto_name)
@@ -846,9 +820,6 @@  tracecmd_tsync_with_guest(unsigned long long trace_id, int loop_interval,
 	tsync->trace_id = trace_id;
 	tsync->loop_interval = loop_interval;
 	tsync->proto_name = strdup(proto_name);
-	fd = vsock_open(cid, port);
-	if (fd < 0)
-		goto error;
 
 	tsync->msg_handle = tracecmd_msg_handle_alloc(fd, 0);
 	if (!tsync->msg_handle) {
@@ -1034,7 +1005,6 @@  out:
 
 /**
  * tracecmd_tsync_with_host - Synchronize timestamps with host
- *
  * @tsync_protos: List of tsync protocols, supported by the host
  * @clock: Trace clock, used for that session
  * @port: returned, VSOCKET port, on which the guest listens for tsync requests
diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index c3e52a48fa07..56fa5a798dcc 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -3976,19 +3976,22 @@  static int host_tsync(struct common_record_context *ctx,
 		      unsigned int tsync_port, char *proto)
 {
 	struct trace_guest *guest;
+	int fd;
 
 	if (!proto)
 		return -1;
+
 	guest = trace_get_guest(instance->cid, NULL);
 	if (guest == NULL)
 		return -1;
 
 	start_mapping_vcpus(guest);
 
+	fd = trace_open_vsock(instance->cid, tsync_port);
 	instance->tsync = tracecmd_tsync_with_guest(top_instance.trace_id,
 						    instance->tsync_loop_interval,
-						    instance->cid, tsync_port,
-						    guest->pid, instance->cpu_count,
+						    fd, guest->pid,
+						    instance->cpu_count,
 						    proto, ctx->clock);
 	stop_mapping_vcpus(instance, guest);