Skip to contentSkip to conversation

trace-cmd library: Remove dependency to vsockets

PATCH Superseded Steven Rostedt 1 participant 9 messages Apr 15, 2022 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover trace-cmd library: Remove dependency to vsockets

From: "Steven Rostedt (Google)" <[email protected]>

[ Sending again from my server in case my laptop had issues sending ]

In an effort to have trace-cmd agent and the time synchronization to work with networks, the dependency to vsockets needs to be removed. In fact, libtracecmd should not have any dependency to vsockets, as everything can be done externally. This simplifies the code, and allows the use of trace-msg to be used in more scenarios.

I tested host guest tracing to make sure that it did not break the existing use cases, although there may still be an outlier that I didn't cover.

Steven Rostedt (Google) (8):
trace-cmd: Add NO_VSOCK make option to force vsock code off trace-cmd library: Remove ports from clock context trace-cmd library: Remove vsocket dependency on P2P protocol trace-cmd library: Remove dependency on vsocks for sync identifiers trace-cmd library: Have tracecmd_tsync_with_guest() not depend on
cid/port
trace-cmd library: Remove dependency on vsocks from
tracecmd_tsync_get_session_params()
trace-cmd library: Remove vsock dependency from
tracecmd_tsync_with_host()
trace-cmd: Move vsocket code into its own file

 Makefile                                      |   4 +
 .../include/private/trace-cmd-private.h       |  12 +-
 lib/trace-cmd/include/trace-tsync-local.h     |  21 +-
 lib/trace-cmd/trace-msg.c                     |   3 +-
 lib/trace-cmd/trace-timesync-ptp.c            |  25 ++-
 lib/trace-cmd/trace-timesync.c                | 180 ++----------------
 tracecmd/Makefile                             |   1 +
 tracecmd/include/trace-local.h                |  49 +++++
 tracecmd/trace-agent.c                        |  96 +++-------
 tracecmd/trace-record.c                       |  94 +--------
 tracecmd/trace-vm.c                           |   2 +-
 tracecmd/trace-vsock.c                        | 156 +++++++++++++++
 12 files changed, 303 insertions(+), 340 deletions(-)
 create mode 100644 tracecmd/trace-vsock.c
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/8 trace-cmd: Add NO_VSOCK make option to force vsock code off

From: "Steven Rostedt (Google)" <[email protected]>

Add "make NO_VSOCK=1" to disable building with vsocket code. It is equivalent to not having vsocket support on the system.

---
 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +4 -0
index 9ccaaa61fecb..982514baad01 100644
@@ -287,7 +287,11 @@ CFLAGS ?= -g -Wall
 CPPFLAGS ?=
 LDFLAGS ?=
 
+ifndef NO_VSOCK
 VSOCK_DEFINED := $(shell if (echo "$(pound)include <linux/vm_sockets.h>" | $(CC) -E - >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
+else
+VSOCK_DEFINED := 0
+endif
 
 export VSOCK_DEFINED
 ifeq ($(VSOCK_DEFINED), 1)
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/8 trace-cmd library: Remove ports from clock context

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

The local and remote port fields are set but never used. Remove them.

---
 lib/trace-cmd/include/trace-tsync-local.h |  2 --
 lib/trace-cmd/trace-timesync.c            | 11 +++--------
 2 files changed, 3 insertions(+), 10 deletions(-)
lib/trace-cmd/include/trace-tsync-local.h +0 -2
index b357662c8d41..d0f82f5d0916 100644
@@ -51,9 +51,7 @@ struct clock_sync_context {
 
 	/* Identifiers of local and remote time sync peers: cid and port */
 	unsigned int			local_cid;
-	unsigned int			local_port;
 	unsigned int			remote_cid;
-	unsigned int			remote_port;
 };
 
 int tracecmd_tsync_proto_register(const char *proto_name, int accuracy, int roles,
lib/trace-cmd/trace-timesync.c +3 -8
index 9e82094eaa31..514d333e57a7 100644
@@ -405,8 +405,7 @@ int __hidden vsock_get_port(int sd, unsigned int *port)
 	return 0;
 }
 
-static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport,
-			      unsigned int *rcid, unsigned int *rport)
+static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid)
 {
 	struct sockaddr_vm addr;
 	socklen_t addr_len = sizeof(addr);
@@ -416,7 +415,6 @@ static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport,
 		return -1;
 	if (addr.svm_family != AF_VSOCK)
 		return -1;
-	*lport = addr.svm_port;
 	*lcid = addr.svm_cid;
 
 	memset(&addr, 0, sizeof(addr));
@@ -425,7 +423,6 @@ static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport,
 		return -1;
 	if (addr.svm_family != AF_VSOCK)
 		return -1;
-	*rport = addr.svm_port;
 	*rcid = addr.svm_cid;
 
 	return 0;
@@ -448,8 +445,7 @@ static int vsock_get_port(int sd, unsigned int *port)
 	return -ENOTSUP;
 }
 
-static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport,
-			      unsigned int *rcid, unsigned int *rport)
+static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid)
 {
 	return -ENOTSUP;
 }
@@ -503,8 +499,7 @@ static int clock_context_init(struct tracecmd_time_sync *tsync,
 	clock->is_server = clock->is_guest;
 
 	if (get_vsocket_params(tsync->msg_handle->fd, &clock->local_cid,
-			       &clock->local_port, &clock->remote_cid,
-			       &clock->remote_port))
+			       &clock->remote_cid))
 		goto error;
 
 	clock->instance = clock_synch_create_instance(tsync->clock_str,
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/8 trace-cmd library: Remove vsocket dependency on P2P protocol

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

The P2P protocol can work with network connections as well. To allow network agents, remove the dependency on cid ids, and just call them local and remote ids.

---
 lib/trace-cmd/include/trace-tsync-local.h |  6 +++---
 lib/trace-cmd/trace-timesync-ptp.c        | 24 +++++++++++------------
 lib/trace-cmd/trace-timesync.c            |  6 +++---
 3 files changed, 18 insertions(+), 18 deletions(-)
lib/trace-cmd/include/trace-tsync-local.h +3 -3
index d0f82f5d0916..4340dfaf80ca 100644
@@ -49,9 +49,9 @@ struct clock_sync_context {
 							 * calculated offsets per CPU
 							 */
 
-	/* Identifiers of local and remote time sync peers: cid and port */
-	unsigned int			local_cid;
-	unsigned int			remote_cid;
+	/* Identifiers of local and remote time sync peers */
+	unsigned int			local_id;
+	unsigned int			remote_id;
 };
 
 int tracecmd_tsync_proto_register(const char *proto_name, int accuracy, int roles,
lib/trace-cmd/trace-timesync-ptp.c +12 -12
index 4f7805627259..0e23ff83869a 100644
@@ -100,8 +100,8 @@ struct ptp_markers_context {
 };
 
 struct ptp_marker_buf {
-	int local_cid;
-	int remote_cid;
+	int local_id;
+	int remote_id;
 	int count;
 	int packet_id;
 } __packed;
@@ -163,7 +163,7 @@ static int ptp_clock_sync_init(struct tracecmd_time_sync *tsync)
 		char buff[256];
 		int res_fd;
 
-		sprintf(buff, "res-cid%d.txt", clock_context->remote_cid);
+		sprintf(buff, "res-id%d.txt", clock_context->remote_id);
 
 		res_fd = open(buff, O_CREAT|O_WRONLY|O_TRUNC, 0644);
 		if (res_fd > 0)
@@ -247,8 +247,8 @@ static int ptp_marker_find(struct tep_event *event, struct tep_record *record,
 		return 0;
 	if (record->size >= (ctx->ptp->id->offset + sizeof(struct ptp_marker))) {
 		marker = (struct ptp_marker *)(record->data + ctx->ptp->id->offset);
-		if (marker->data.local_cid == ctx->clock->local_cid &&
-		    marker->data.remote_cid == ctx->clock->remote_cid &&
+		if (marker->data.local_id == ctx->clock->local_id &&
+		    marker->data.remote_id == ctx->clock->remote_id &&
 		    marker->series_id == ctx->ptp->series_id &&
 		    marker->data.count)
 			ptp_probe_store(ctx, marker, record->ts);
@@ -460,8 +460,8 @@ static int ptp_clock_client(struct tracecmd_time_sync *tsync,
 	ret = tracecmd_msg_send_time_sync(tsync->msg_handle, PTP_NAME,
 					  PTP_SYNC_PKT_START, sizeof(start),
 					  (char *)&start);
-	marker.data.local_cid = clock_context->local_cid;
-	marker.data.remote_cid = clock_context->remote_cid;
+	marker.data.local_id = clock_context->local_id;
+	marker.data.remote_id = clock_context->remote_id;
 	marker.series_id = ntohl(start.series_id);
 	marker.data.packet_id = 'r';
 	ptp->series_id = marker.series_id;
@@ -566,8 +566,8 @@ static int ptp_clock_server(struct tracecmd_time_sync *tsync,
 	tracefs_instance_file_write(clock_context->instance, "trace", "\0");
 
 	ptp->series_id++;
-	marker.data.local_cid = clock_context->local_cid;
-	marker.data.remote_cid = clock_context->remote_cid;
+	marker.data.local_id = clock_context->local_id;
+	marker.data.remote_id = clock_context->remote_id;
 	marker.series_id = ptp->series_id;
 	msg = (char *)&msg_ret;
 	size = sizeof(msg_ret);
@@ -627,7 +627,7 @@ static int ptp_clock_server(struct tracecmd_time_sync *tsync,
 		char buff[256];
 		int res_fd;
 
-		sprintf(buff, "res-cid%d.txt", clock_context->remote_cid);
+		sprintf(buff, "res-id%d.txt", clock_context->remote_id);
 
 		res_fd = open(buff, O_WRONLY|O_APPEND, 0644);
 		if (res_fd > 0) {
@@ -681,8 +681,8 @@ static int ptp_clock_sync_calc(struct tracecmd_time_sync *tsync,
 		ptp = (struct ptp_clock_sync *)clock_context->proto_data;
 		if (ptp->debug_fd > 0)
 			close(ptp->debug_fd);
-		sprintf(buff, "s-cid%d_%d.txt",
-				clock_context->remote_cid, ptp->series_id+1);
+		sprintf(buff, "s-id%d_%d.txt",
+				clock_context->remote_id, ptp->series_id+1);
 		ptp->debug_fd = open(buff, O_CREAT|O_WRONLY|O_TRUNC, 0644);
 	}
 #endif
lib/trace-cmd/trace-timesync.c +3 -3
index 514d333e57a7..594f660e2a40 100644
@@ -498,12 +498,12 @@ static int clock_context_init(struct tracecmd_time_sync *tsync,
 	clock->is_guest = guest;
 	clock->is_server = clock->is_guest;
 
-	if (get_vsocket_params(tsync->msg_handle->fd, &clock->local_cid,
-			       &clock->remote_cid))
+	if (get_vsocket_params(tsync->msg_handle->fd, &clock->local_id,
+			       &clock->remote_id))
 		goto error;
 
 	clock->instance = clock_synch_create_instance(tsync->clock_str,
-						      clock->remote_cid);
+						      clock->remote_id);
 	if (!clock->instance)
 		goto error;
 
Signature · 1 line
2.35.1
#4 lore raw

PATCH 4/8 trace-cmd library: Remove dependency on vsocks for sync identifiers

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

In an effort to remove the dependency on vsockets from libtracecmd and synchronization, move the creation of remote and local ids out of the library and change tracecmd_tsync_with_host() to pass in those identifiers.

---
 .../include/private/trace-cmd-private.h       |  2 +-
 lib/trace-cmd/include/trace-tsync-local.h     |  2 +
 lib/trace-cmd/trace-timesync.c                | 42 ++++---------------
 tracecmd/trace-agent.c                        | 41 +++++++++++++++++-
 4 files changed, 50 insertions(+), 37 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -1
index 69343765c5ff..24295e4e09d3 100644
@@ -489,7 +489,7 @@ int tracecmd_tsync_proto_getall(struct tracecmd_tsync_protos **protos, const cha
 bool tsync_proto_is_supported(const char *proto_name);
 struct tracecmd_time_sync *
 tracecmd_tsync_with_host(const struct tracecmd_tsync_protos *tsync_protos,
-			 const char *clock);
+			 const char *clock, int remote_id, int local_id);
 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,
lib/trace-cmd/include/trace-tsync-local.h +2 -0
index 4340dfaf80ca..697c076c43a6 100644
@@ -22,6 +22,8 @@ struct tracecmd_time_sync {
 	void				*context;
 	int				guest_pid;
 	int				vcpu_count;
+	int				remote_id;
+	int				local_id;
 };
 
 struct clock_sync_offsets {
lib/trace-cmd/trace-timesync.c +7 -35

79 lines not on this page · raw message

tracecmd/trace-agent.c +40 -1

60 lines not on this page · raw message

Signature · 1 line
2.35.1
#5 lore raw

PATCH 5/8 trace-cmd library: Have tracecmd_tsync_with_guest() not depend on cid/port

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

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.

---
 .../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(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -1
index 24295e4e09d3..06906b04fbd9 100644
@@ -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,
lib/trace-cmd/trace-timesync.c +3 -33
index b41aece82082..2f812ca5fc74 100644
@@ -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
tracecmd/trace-record.c +5 -2
index c3e52a48fa07..56fa5a798dcc 100644
@@ -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);
 
Signature · 1 line
2.35.1
#6 lore raw

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

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

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.

---
 .../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(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -3
index 06906b04fbd9..1839d82d4e11 100644
@@ -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);
lib/trace-cmd/trace-timesync.c +3 -17
index 2f812ca5fc74..6f16090c2eb4 100644
@@ -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;
 }
tracecmd/trace-agent.c +30 -4
index 151ca19c2270..1a9463fd4854 100644
@@ -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");
 	}
Signature · 1 line
2.35.1
#7 lore raw

PATCH 7/8 trace-cmd library: Remove vsock dependency from tracecmd_tsync_with_host()

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

The time synchronization works for networks too. In order to remove the dependency of vsockets, have tracecmd_tsync_with_host() get the file descriptor from the caller instead of creating one. This allows the caller to either create a vsocket or a network interface and it will still all work.

---
 .../include/private/trace-cmd-private.h       |  3 +-
 lib/trace-cmd/trace-msg.c                     |  3 +-
 lib/trace-cmd/trace-timesync.c                | 74 ++-----------------
 tracecmd/trace-agent.c                        | 57 ++++++++++++--
 tracecmd/trace-record.c                       |  7 +-
 5 files changed, 65 insertions(+), 79 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -1
index 1839d82d4e11..45ae1dded66d 100644
@@ -488,7 +488,8 @@ void tracecmd_tsync_init(void);
 int tracecmd_tsync_proto_getall(struct tracecmd_tsync_protos **protos, const char *clock, int role);
 bool tsync_proto_is_supported(const char *proto_name);
 struct tracecmd_time_sync *
-tracecmd_tsync_with_host(const struct tracecmd_tsync_protos *tsync_protos,
+tracecmd_tsync_with_host(int fd,
+			 const struct tracecmd_tsync_protos *tsync_protos,
 			 const char *clock, int remote_id, int local_id);
 int tracecmd_tsync_with_host_stop(struct tracecmd_time_sync *tsync);
 struct tracecmd_time_sync *
lib/trace-cmd/trace-msg.c +2 -1
index 03b853e4368b..726e9424c8fd 100644
@@ -629,7 +629,8 @@ static int flush_cache(struct tracecmd_msg_handle *msg_handle)
 
 void tracecmd_msg_handle_close(struct tracecmd_msg_handle *msg_handle)
 {
-	close(msg_handle->fd);
+	if (msg_handle->fd >= 0)
+		close(msg_handle->fd);
 	if (msg_handle->cfd >= 0)
 		close(msg_handle->cfd);
 	free(msg_handle);
lib/trace-cmd/trace-timesync.c +7 -67

113 lines not on this page · raw message

tracecmd/trace-agent.c +50 -7

86 lines not on this page · raw message

tracecmd/trace-record.c +4 -3
index 56fa5a798dcc..8e89aa94977c 100644
@@ -3228,8 +3228,7 @@ int trace_open_vsock(unsigned int cid, unsigned int port)
 	die("vsock is not supported");
 	return -1;
 }
-
-static bool can_splice_read_vsock(void)
+static inline bool can_splice_read_vsock(void)
 {
 	return false;
 }
@@ -3976,6 +3975,7 @@ static int host_tsync(struct common_record_context *ctx,
 		      unsigned int tsync_port, char *proto)
 {
 	struct trace_guest *guest;
+	int guest_pid = -1;
 	int fd;
 
 	if (!proto)
@@ -3985,12 +3985,13 @@ static int host_tsync(struct common_record_context *ctx,
 	if (guest == NULL)
 		return -1;
 
+	guest_pid = guest->pid;
 	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,
-						    fd, guest->pid,
+						    fd, guest_pid,
 						    instance->cpu_count,
 						    proto, ctx->clock);
 	stop_mapping_vcpus(instance, guest);
Signature · 1 line
2.35.1
#8 lore raw

PATCH 8/8 trace-cmd: Move vsocket code into its own file

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Create trace-vsock.c to hold vsock specific operations. Several files duplicate the implementation of vsocket connections. Consolidate all of them into a single file to be used by all, and also can be disabled in a single place, getting rid of a lot of the #ifdef VSOCKs that are poluting the code.

---
 lib/trace-cmd/include/trace-tsync-local.h |  11 ++
 lib/trace-cmd/trace-timesync-ptp.c        |   1 -
 lib/trace-cmd/trace-timesync.c            |   5 -
 tracecmd/Makefile                         |   1 +
 tracecmd/include/trace-local.h            |  49 +++++++
 tracecmd/trace-agent.c                    | 164 +---------------------
 tracecmd/trace-record.c                   |  86 +-----------
 tracecmd/trace-vm.c                       |   2 +-
 tracecmd/trace-vsock.c                    | 156 ++++++++++++++++++++
 9 files changed, 229 insertions(+), 246 deletions(-)
 create mode 100644 tracecmd/trace-vsock.c
lib/trace-cmd/include/trace-tsync-local.h +11 -0
index 697c076c43a6..885c9f51d891 100644
@@ -65,7 +65,18 @@ int tracecmd_tsync_proto_register(const char *proto_name, int accuracy, int role
 					      long long *, unsigned int));
 int tracecmd_tsync_proto_unregister(char *proto_name);
 
+#ifdef VSOCK
 int ptp_clock_sync_register(void);
 int kvm_clock_sync_register(void);
+#else
+static inline int ptp_clock_sync_register(void)
+{
+	return 0;
+}
+static inline int kvm_clock_sync_register(void)
+{
+	return 0;
+}
+#endif
 
 #endif /* _TRACE_TSYNC_LOCAL_H */
lib/trace-cmd/trace-timesync-ptp.c +0 -1
index 0e23ff83869a..653d176e2e79 100644
@@ -8,7 +8,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <arpa/inet.h>
-#include <linux/vm_sockets.h>
 #include <sys/types.h>
 #include <linux/types.h>
 #include <time.h>
lib/trace-cmd/trace-timesync.c +0 -5
index 823dcf21ba26..14cf20c870cb 100644
@@ -8,9 +8,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <arpa/inet.h>
-#ifdef VSOCK
-#include <linux/vm_sockets.h>
-#endif
 #include <linux/limits.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -65,10 +62,8 @@ static struct tsync_proto *tsync_proto_find(const char *proto_name)
  */
 void tracecmd_tsync_init(void)
 {
-#ifdef VSOCK
 	ptp_clock_sync_register();
 	kvm_clock_sync_register();
-#endif
 }
 
 int tracecmd_tsync_proto_register(const char *proto_name, int accuracy, int roles,
tracecmd/Makefile +1 -0
index 355f04723ad7..13f7776e8e45 100644
@@ -37,6 +37,7 @@ TRACE_CMD_OBJS += trace-dump.o
 TRACE_CMD_OBJS += trace-clear.o
 TRACE_CMD_OBJS += trace-vm.o
 TRACE_CMD_OBJS += trace-convert.o
+TRACE_CMD_OBJS += trace-vsock.o
 
 ifeq ($(VSOCK_DEFINED), 1)
 TRACE_CMD_OBJS += trace-agent.o
tracecmd/include/trace-local.h +49 -0

61 lines not on this page · raw message

tracecmd/trace-agent.c +7 -157

209 lines not on this page · raw message

tracecmd/trace-record.c +4 -82

122 lines not on this page · raw message

tracecmd/trace-vm.c +1 -1
index 02979ba59a57..57dbef8d42e4 100644
@@ -284,7 +284,7 @@ static void find_pid_by_cid(struct trace_guest *guest)
 	int fd;
 
 	instance = start_trace_connect();
-	fd = trace_open_vsock(guest->cid, -1);
+	fd = trace_vsock_open(guest->cid, -1);
 	guest->pid = stop_trace_connect(instance);
 	/* Just in case! */
 	if (fd >= 0)
tracecmd/trace-vsock.c new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
2.35.1