Skip to contentSkip to conversation

trace-cmd: Have trace-cmd listen work with vsockets

PATCH New Steven Rostedt 1 participant 6 messages Apr 17, 2022 Documentation

lore
Share & tools
lore
#0 lore raw

v3 cover trace-cmd: Have trace-cmd listen work with vsockets

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

We have a use case where the host is very minimal (like an embedded device) and does not even have networking enabled. But it does have vsockets. We need to trace this host and have it send its data to the guest. By running trace-cmd listen on the guest and have the host use vsockets instead of networking, can allow this host to be traced.

Depends on: https://patchwork.kernel.org/project/linux-trace-devel/list/?series=632868
https://lore.kernel.org/r/[email protected]

Changes since v2: https://lore.kernel.org/all/[email protected]/

- Fixed freeing of host variable that was not allocated

Changes since v1: https://lore.kernel.org/all/[email protected]/

- Rebased on top of: https://lore.kernel.org/all/[email protected]/

- Added the documentation patch

Steven Rostedt (Google) (5):
trace-cmd listen: Remove UDP from function names trace-cmd listen: Replace bool use_tcp with enum type trace-cmd record: Replace bool use_tcp with enum type trace-cmd listen: Add vsocket usage trace-cmd listen: Add documentation on vsocket usage

 .../trace-cmd/trace-cmd-listen.1.txt          |   4 +
 .../trace-cmd/trace-cmd-record.1.txt          |   8 +
 .../include/private/trace-cmd-private.h       |   1 +
 lib/trace-cmd/trace-msg.c                     |  15 +-
 tracecmd/include/trace-local.h                |   8 +
 tracecmd/trace-listen.c                       | 196 +++++++++++++-----
 tracecmd/trace-record.c                       |  93 +++++++--
 tracecmd/trace-usage.c                        |   2 +
 8 files changed, 256 insertions(+), 71 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v3 1/5 trace-cmd listen: Remove UDP from function names

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

Several functions have "udp" in their name when they also handle TCP. As we are going to also add vsocket connections, it makes even less sense to have these names with "udp". Make the names of the functions (and some variables) more generic.

---
 tracecmd/trace-listen.c | 42 ++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
tracecmd/trace-listen.c +21 -21
index 874ecd61d98a..b2ab05bc3068 100644
@@ -137,8 +137,8 @@ static void remove_pid_file(void)
 	unlink(buf);
 }
 
-static int process_udp_child(int sfd, const char *host, const char *port,
-			     int cpu, int page_size, int use_tcp)
+static int process_child(int sfd, const char *host, const char *port,
+			 int cpu, int page_size, int use_tcp)
 {
 	struct sockaddr_storage peer_addr;
 	socklen_t peer_addr_len;
@@ -205,7 +205,7 @@ static int process_udp_child(int sfd, const char *host, const char *port,
 #define START_PORT_SEARCH 1500
 #define MAX_PORT_SEARCH 6000
 
-static int udp_bind_a_port(int start_port, int *sfd, int use_tcp)
+static int bind_a_port(int start_port, int *sfd, int use_tcp)
 {
 	struct addrinfo hints;
 	struct addrinfo *result, *rp;
@@ -223,7 +223,7 @@ static int udp_bind_a_port(int start_port, int *sfd, int use_tcp)
 
 	s = getaddrinfo(NULL, buf, &hints, &result);
 	if (s != 0)
-		pdie("getaddrinfo: error opening udp socket");
+		pdie("getaddrinfo: error opening socket");
 
 	for (rp = result; rp != NULL; rp = rp->ai_next) {
 		*sfd = socket(rp->ai_family, rp->ai_socktype,
@@ -249,40 +249,40 @@ static int udp_bind_a_port(int start_port, int *sfd, int use_tcp)
 	return num_port;
 }
 
-static void fork_udp_reader(int sfd, const char *node, const char *port,
-			    int *pid, int cpu, int pagesize, int use_tcp)
+static void fork_reader(int sfd, const char *node, const char *port,
+			int *pid, int cpu, int pagesize, int use_tcp)
 {
 	int ret;
 
 	*pid = fork();
 
 	if (*pid < 0)
-		pdie("creating udp reader");
+		pdie("creating reader");
 
 	if (!*pid) {
-		ret = process_udp_child(sfd, node, port, cpu, pagesize, use_tcp);
+		ret = process_child(sfd, node, port, cpu, pagesize, use_tcp);
 		if (ret < 0)
-			pdie("Problem with udp reader %d", ret);
+			pdie("Problem with reader %d", ret);
 	}
 
 	close(sfd);
 }
 
-static int open_udp(const char *node, const char *port, int *pid,
-		    int cpu, int pagesize, int start_port, int use_tcp)
+static int open_port(const char *node, const char *port, int *pid,
+		     int cpu, int pagesize, int start_port, int use_tcp)
 {
 	int sfd;
 	int num_port;
 
 	/*
-	 * udp_bind_a_port() currently does not return an error, but if that
+	 * bind_a_port() currently does not return an error, but if that
 	 * changes in the future, we have a check for it now.
 	 */
-	num_port = udp_bind_a_port(start_port, &sfd, use_tcp);
+	num_port = bind_a_port(start_port, &sfd, use_tcp);
 	if (num_port < 0)
 		return num_port;
 
-	fork_udp_reader(sfd, node, port, pid, cpu, pagesize, use_tcp);
+	fork_reader(sfd, node, port, pid, cpu, pagesize, use_tcp);
 
 	return num_port;
 }
@@ -468,7 +468,7 @@ static int *create_all_readers(const char *node, const char *port,
 	unsigned int *port_array;
 	int *pid_array;
 	unsigned int start_port;
-	unsigned int udp_port;
+	unsigned int connect_port;
 	int cpus = msg_handle->cpu_count;
 	int cpu;
 	int pid;
@@ -490,19 +490,19 @@ static int *create_all_readers(const char *node, const char *port,
 
 	start_port = START_PORT_SEARCH;
 
-	/* Now create a UDP port for each CPU */
+	/* Now create a port for each CPU */
 	for (cpu = 0; cpu < cpus; cpu++) {
-		udp_port = open_udp(node, port, &pid, cpu,
-				    pagesize, start_port, use_tcp);
-		if (udp_port < 0)
+		connect_port = open_port(node, port, &pid, cpu,
+					 pagesize, start_port, use_tcp);
+		if (connect_port < 0)
 			goto out_free;
-		port_array[cpu] = udp_port;
+		port_array[cpu] = connect_port;
 		pid_array[cpu] = pid;
 		/*
 		 * Due to some bugging finding ports,
 		 * force search after last port
 		 */
-		start_port = udp_port + 1;
+		start_port = connect_port + 1;
 	}
 
 	if (msg_handle->version == V3_PROTOCOL) {
Signature · 1 line
2.35.1
#2 lore raw

PATCH v3 2/5 trace-cmd listen: Replace bool use_tcp with enum type

In reply to Steven Rostedt

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

To allow listen to use vsockets as a connection interface, using a boolean "use_tcp" is not flexible enough, as now there are three kinds of connections. In preparation for adding vsockets have trace-cmd listen use an enum instead of a boolean.

---
 tracecmd/include/trace-local.h |  5 +++++
 tracecmd/trace-listen.c        | 27 +++++++++++++++------------
 2 files changed, 20 insertions(+), 12 deletions(-)
tracecmd/include/trace-local.h +5 -0
index 863635886769..d23cbf24cc06 100644
@@ -42,6 +42,11 @@ extern int show_status;
 
 int trace_set_verbose(char *level);
 
+enum port_type {
+	USE_UDP,
+	USE_TCP,
+};
+
 struct pid_record_data {
 	int			pid;
 	int			brass[2];
tracecmd/trace-listen.c +15 -12
index b2ab05bc3068..a5d4ec64f77c 100644
@@ -138,7 +138,7 @@ static void remove_pid_file(void)
 }
 
 static int process_child(int sfd, const char *host, const char *port,
-			 int cpu, int page_size, int use_tcp)
+			 int cpu, int page_size, enum port_type type)
 {
 	struct sockaddr_storage peer_addr;
 	socklen_t peer_addr_len;
@@ -160,7 +160,7 @@ static int process_child(int sfd, const char *host, const char *port,
 	if (fd < 0)
 		pdie("creating %s", tempfile);
 
-	if (use_tcp) {
+	if (type == USE_TCP) {
 		if (listen(sfd, backlog) < 0)
 			pdie("listen");
 		peer_addr_len = sizeof(peer_addr);
@@ -184,7 +184,7 @@ static int process_child(int sfd, const char *host, const char *port,
 		if (!r)
 			break;
 		/* UDP requires that we get the full size in one go */
-		if (!use_tcp && r < page_size && !once) {
+		if (type == USE_UDP && r < page_size && !once) {
 			once = 1;
 			warning("read %d bytes, expected %d", r, page_size);
 		}
@@ -205,7 +205,7 @@ static int process_child(int sfd, const char *host, const char *port,
 #define START_PORT_SEARCH 1500
 #define MAX_PORT_SEARCH 6000
 
-static int bind_a_port(int start_port, int *sfd, int use_tcp)
+static int bind_a_port(int start_port, int *sfd, enum port_type type)
 {
 	struct addrinfo hints;
 	struct addrinfo *result, *rp;
@@ -218,7 +218,7 @@ static int bind_a_port(int start_port, int *sfd, int use_tcp)
 
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_UNSPEC;
-	hints.ai_socktype = use_tcp ? SOCK_STREAM : SOCK_DGRAM;
+	hints.ai_socktype = type == USE_TCP ? SOCK_STREAM : SOCK_DGRAM;
 	hints.ai_flags = AI_PASSIVE;
 
 	s = getaddrinfo(NULL, buf, &hints, &result);
@@ -250,7 +250,7 @@ static int bind_a_port(int start_port, int *sfd, int use_tcp)
 }
 
 static void fork_reader(int sfd, const char *node, const char *port,
-			int *pid, int cpu, int pagesize, int use_tcp)
+			int *pid, int cpu, int pagesize, enum port_type type)
 {
 	int ret;
 
@@ -260,7 +260,7 @@ static void fork_reader(int sfd, const char *node, const char *port,
 		pdie("creating reader");
 
 	if (!*pid) {
-		ret = process_child(sfd, node, port, cpu, pagesize, use_tcp);
+		ret = process_child(sfd, node, port, cpu, pagesize, type);
 		if (ret < 0)
 			pdie("Problem with reader %d", ret);
 	}
@@ -269,7 +269,7 @@ static void fork_reader(int sfd, const char *node, const char *port,
 }
 
 static int open_port(const char *node, const char *port, int *pid,
-		     int cpu, int pagesize, int start_port, int use_tcp)
+		     int cpu, int pagesize, int start_port, enum port_type type)
 {
 	int sfd;
 	int num_port;
@@ -278,11 +278,11 @@ static int open_port(const char *node, const char *port, int *pid,
 	 * bind_a_port() currently does not return an error, but if that
 	 * changes in the future, we have a check for it now.
 	 */
-	num_port = bind_a_port(start_port, &sfd, use_tcp);
+	num_port = bind_a_port(start_port, &sfd, type);
 	if (num_port < 0)
 		return num_port;
 
-	fork_reader(sfd, node, port, pid, cpu, pagesize, use_tcp);
+	fork_reader(sfd, node, port, pid, cpu, pagesize, type);
 
 	return num_port;
 }
@@ -463,7 +463,7 @@ static void destroy_all_readers(int cpus, int *pid_array, const char *node,
 static int *create_all_readers(const char *node, const char *port,
 			       int pagesize, struct tracecmd_msg_handle *msg_handle)
 {
-	int use_tcp = msg_handle->flags & TRACECMD_MSG_FL_USE_TCP;
+	enum port_type port_type = USE_UDP;
 	char buf[BUFSIZ];
 	unsigned int *port_array;
 	int *pid_array;
@@ -476,6 +476,9 @@ static int *create_all_readers(const char *node, const char *port,
 	if (!pagesize)
 		return NULL;
 
+	if (msg_handle->flags & TRACECMD_MSG_FL_USE_TCP)
+		port_type = USE_TCP;
+
 	port_array = malloc(sizeof(*port_array) * cpus);
 	if (!port_array)
 		return NULL;
@@ -493,7 +496,7 @@ static int *create_all_readers(const char *node, const char *port,
 	/* Now create a port for each CPU */
 	for (cpu = 0; cpu < cpus; cpu++) {
 		connect_port = open_port(node, port, &pid, cpu,
-					 pagesize, start_port, use_tcp);
+					 pagesize, start_port, port_type);
 		if (connect_port < 0)
 			goto out_free;
 		port_array[cpu] = connect_port;
Signature · 1 line
2.35.1
#3 lore raw

PATCH v3 3/5 trace-cmd record: Replace bool use_tcp with enum type

In reply to Steven Rostedt

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

To allow trace-cmd record to communicate with trace-cmd listen with vsockets as a connection interface, using a boolean "use_tcp" is not flexible enough, as now there are three kinds of connections. In preparation for adding vsockets have trace-cmd record/listen use an enum instead of a boolean.

---
 tracecmd/trace-record.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tracecmd/trace-record.c +8 -8
index ac6fb7e98892..022a024c665b 100644
@@ -90,7 +90,7 @@ static bool fork_process;
 /* Max size to let a per cpu file get */
 static int max_kb;
 
-static bool use_tcp;
+static enum port_type port_type;
 
 static int do_ptrace;
 
@@ -3130,12 +3130,12 @@ static int connect_port(const char *host, unsigned int port)
 
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_UNSPEC;
-	hints.ai_socktype = use_tcp ? SOCK_STREAM : SOCK_DGRAM;
+	hints.ai_socktype = port_type == USE_TCP ? SOCK_STREAM : SOCK_DGRAM;
 
 	s = getaddrinfo(host, buf, &hints, &results);
 	if (s != 0)
 		die("connecting to %s server %s:%s",
-		    use_tcp ? "TCP" : "UDP", host, buf);
+		    port_type == USE_TCP ? "TCP" : "UDP", host, buf);
 
 	for (rp = results; rp != NULL; rp = rp->ai_next) {
 		sfd = socket(rp->ai_family, rp->ai_socktype,
@@ -3149,7 +3149,7 @@ static int connect_port(const char *host, unsigned int port)
 
 	if (rp == NULL)
 		die("Can not connect to %s server %s:%s",
-		    use_tcp ? "TCP" : "UDP", host, buf);
+		    port_type == USE_TCP ? "TCP" : "UDP", host, buf);
 
 	freeaddrinfo(results);
 
@@ -3439,11 +3439,11 @@ static void communicate_with_listener_v1(struct tracecmd_msg_handle *msg_handle,
 	/* TODO, test for ipv4 */
 	if (page_size >= UDP_MAX_PACKET) {
 		warning("page size too big for UDP using TCP in live read");
-		use_tcp = 1;
+		port_type = USE_TCP;
 		msg_handle->flags |= TRACECMD_MSG_FL_USE_TCP;
 	}
 
-	if (use_tcp) {
+	if (port_type == USE_TCP) {
 		/* Send one option */
 		write(msg_handle->fd, "1", 2);
 		/* Size 4 */
@@ -3591,7 +3591,7 @@ again:
 		msg_handle->version = V3_PROTOCOL;
 	}
 
-	if (use_tcp)
+	if (port_type == USE_TCP)
 		msg_handle->flags |= TRACECMD_MSG_FL_USE_TCP;
 
 	if (msg_handle->version == V3_PROTOCOL) {
@@ -6467,7 +6467,7 @@ static void parse_record_options(int argc,
 			if (IS_EXTRACT(ctx))
 				ctx->topt = 1; /* Extract top instance also */
 			else
-				use_tcp = 1;
+				port_type = USE_TCP;
 			break;
 		case 'b':
 			check_instance_die(ctx->instance, "-b");
Signature · 1 line
2.35.1
#4 lore raw

PATCH v3 4/5 trace-cmd listen: Add vsocket usage

In reply to Steven Rostedt

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

As vsockets are slightly faster than TCP connections, and act the same, there's no reason to not use them for the listen command.

---
 .../include/private/trace-cmd-private.h       |   1 +
 lib/trace-cmd/trace-msg.c                     |  15 +-
 tracecmd/include/trace-local.h                |   3 +
 tracecmd/trace-listen.c                       | 143 ++++++++++++++----
 tracecmd/trace-record.c                       |  79 +++++++++-
 5 files changed, 202 insertions(+), 39 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -0
index 45ae1dded66d..f68d17bb8e1d 100644
@@ -374,6 +374,7 @@ long tracecmd_flush_recording(struct tracecmd_recorder *recorder);
 
 enum tracecmd_msg_flags {
 	TRACECMD_MSG_FL_USE_TCP		= 1 << 0,
+	TRACECMD_MSG_FL_USE_VSOCK	= 1 << 1,
 };
 
 /* for both client and server */
lib/trace-cmd/trace-msg.c +11 -4
index 726e9424c8fd..6cf74f9b1c99 100644
@@ -214,10 +214,14 @@ static int make_tinit(struct tracecmd_msg_handle *msg_handle,
 	int opt_num = 0;
 	int data_size = 0;
 
-	if (msg_handle->flags & TRACECMD_MSG_FL_USE_TCP) {
+	if (msg_handle->flags & (TRACECMD_MSG_FL_USE_TCP |
+				 TRACECMD_MSG_FL_USE_VSOCK)) {
+		msg->buf = msg_handle->flags & TRACECMD_MSG_FL_USE_TCP ?
+			strdup("tcp") : strdup("vsock");
+		if (!msg->buf)
+			return -1;
 		opt_num++;
-		msg->buf = strdup("tcp");
-		data_size += 4;
+		data_size += strlen(msg->buf) + 1;
 	}
 
 	msg->tinit.cpus = htonl(cpu_count);
@@ -566,11 +570,14 @@ out:
 static bool process_option(struct tracecmd_msg_handle *msg_handle,
 			   const char *opt)
 {
-	/* currently the only option we have is to use TCP */
 	if (strcmp(opt, "tcp") == 0) {
 		msg_handle->flags |= TRACECMD_MSG_FL_USE_TCP;
 		return true;
 	}
+	if (strcmp(opt, "vsock") == 0) {
+		msg_handle->flags |= TRACECMD_MSG_FL_USE_VSOCK;
+		return true;
+	}
 	return false;
 }
 
tracecmd/include/trace-local.h +3 -0
index d23cbf24cc06..aa1cb8d939bd 100644
@@ -45,6 +45,7 @@ int trace_set_verbose(char *level);
 enum port_type {
 	USE_UDP,
 	USE_TCP,
+	USE_VSOCK
 };
 
 struct pid_record_data {
@@ -343,6 +344,8 @@ int trace_make_vsock(unsigned int port);
 int trace_get_vsock_port(int sd, unsigned int *port);
 int trace_open_vsock(unsigned int cid, unsigned int port);
 
+int get_local_cid(unsigned int *cid);
+
 char *trace_get_guest_file(const char *file, const char *guest);
 
 #ifdef VSOCK
tracecmd/trace-listen.c +115 -28

244 lines not on this page · raw message

tracecmd/trace-record.c +72 -7

143 lines not on this page · raw message

Signature · 1 line
2.35.1
#5 lore raw

PATCH v3 5/5 trace-cmd listen: Add documentation on vsocket usage

In reply to Steven Rostedt

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

Document the -V option for trace-cmd listen/record in both the usage and the man pages.

---
 Documentation/trace-cmd/trace-cmd-listen.1.txt | 4 ++++
 Documentation/trace-cmd/trace-cmd-record.1.txt | 8 ++++++++
 tracecmd/trace-usage.c                         | 2 ++
 3 files changed, 14 insertions(+)
Documentation/trace-cmd/trace-cmd-listen.1.txt +4 -0
index aa6a307ff3b7..7c6093ba5354 100644
@@ -26,6 +26,10 @@ OPTIONS
     This options causes trace-cmd listen to go into a daemon mode and run in
     the background.
 
+*-V*::
+    Listen on a vsocket instead. This is useful for tracing between host and
+    guest VMs.
+
 *-d* 'dir'::
     This option specifies a directory to write the data files into.
 
Documentation/trace-cmd/trace-cmd-record.1.txt +8 -0
index 426e856bf74a..6b8e3b4ad364 100644
@@ -259,6 +259,14 @@ OPTIONS
     Note: This option is not supported with latency tracer plugins:
       wakeup, wakeup_rt, irqsoff, preemptoff and preemptirqsoff
 
+*-V* 'cid:port'::
+    If recording on a guest VM and the host is running *trace-cmd listen* with
+    the *-V* option as well, or if this is recording on the host, and a guest
+    in running *trace-cmd listen* with the *-V* option, then connect to the
+    listener (the same as connecting with the *-N* option via the network).
+    This has the same limitations as the *-N* option above with respect to
+    latency tracer plugins.
+
 *-t*::
     This option is used with *-N*, when there's a need to send the live data
     with TCP packets instead of UDP. Although TCP is not nearly as fast as
tracecmd/trace-usage.c +2 -0
index 20995fabed73..ec6376557394 100644
@@ -44,6 +44,7 @@ static struct usage_help usage_help[] = {
 		"          -s sleep interval between recording (in usecs) [default: 1000]\n"
 		"          -S used with --profile, to enable only events in command line\n"
 		"          -N host:port to connect to (see listen)\n"
+		"          -V cid:port to connect to via vsocket (see listen)\n"
 		"          -t used with -N, forces use of tcp in live trace\n"
 		"          -b change kernel buffersize (in kilobytes per CPU)\n"
 		"          -B create sub buffer and following events will be enabled here\n"
@@ -318,6 +319,7 @@ static struct usage_help usage_help[] = {
 		"          Creates a socket to listen for clients.\n"
 		"          -p port number to listen on.\n"
 		"          -D run in daemon mode.\n"
+		"          -V listen on a vsocket instead.\n"
 		"          -o file name to use for clients.\n"
 		"          -d directory to store client files.\n"
 		"          -l logfile to write messages to.\n"
Signature · 1 line
2.35.1