Skip to contentSkip to conversation

trace-cmd: Allow agent to use networking

PATCH Superseded Steven Rostedt 1 participant 9 messages Apr 17, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Allow agent to use networking

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

There's no reason for trace-cmd agent not to be able to do the communication over TCP instead of vsockets. Have trace-cmd record pass in a host name or IP address (IPv4 or IPv6) to the -A option and if the cid or guest name fails, then try host name or IP address.

This will fall back to the P2P time sync protocol and recordings from different hosts can be synchronized.

Also add more debug prints when the --debug option is used. This proved useful to debugging this code.

Steven Rostedt (Google) (8):
trace-cmd record: Move port_type into instance trace-cmd library: Add network roles for time sync trace-cmd record: Allow for ip connections to agents trace-cmd agent: Allow for ip connections from the agent trace-cmd library: Create tracecmd_debug() for debug printing
trace-cmd: Add debug prints for network connections
trace-cmd: Add print helpers to show connections
trace-cmd: Override tracecmd_debug() to show thread id

 .../include/private/trace-cmd-private.h       |   5 +
 lib/trace-cmd/trace-msg.c                     |  12 +-
 lib/trace-cmd/trace-timesync-ptp.c            |   4 +-
 lib/trace-cmd/trace-timesync.c                |   2 +
 lib/trace-cmd/trace-util.c                    |  12 +
 tracecmd/include/trace-local.h                |  16 ++
 tracecmd/trace-agent.c                        | 106 +++++--
 tracecmd/trace-cmd.c                          |  18 ++
 tracecmd/trace-listen.c                       |  84 ++++--
 tracecmd/trace-record.c                       | 258 ++++++++++++------
 tracecmd/trace-vsock.c                        |  20 ++
 11 files changed, 403 insertions(+), 134 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/8 trace-cmd record: Move port_type into instance

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

Instead of one global method of communication, have the "port_type" be part of the instance. This will allow for different instances to have different connection types.

---
 tracecmd/include/trace-local.h |  1 +
 tracecmd/trace-record.c        | 41 ++++++++++++++++++----------------
 2 files changed, 23 insertions(+), 19 deletions(-)
tracecmd/include/trace-local.h +1 -0
index eb894c4fed88..a253b4c6432a 100644
@@ -285,6 +285,7 @@ struct buffer_instance {
 	int			*fds;
 	bool			use_fifos;
 
+	enum port_type		port_type;
 	int			tsync_loop_interval;
 	struct tracecmd_time_sync *tsync;
 };
tracecmd/trace-record.c +22 -19

141 lines not on this page · raw message

Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/8 trace-cmd library: Add network roles for time sync

In reply to Steven Rostedt

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

Add TRACECMD_TIME_SYNC_ROLE_CLIENT and SERVER to distringuish from guest/host to client/server.

---
 lib/trace-cmd/include/private/trace-cmd-private.h | 2 ++
 lib/trace-cmd/trace-timesync-ptp.c                | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
index f68d17bb8e1d..6e7b346cda07 100644
@@ -480,6 +480,8 @@ enum{
 enum tracecmd_time_sync_role {
 	TRACECMD_TIME_SYNC_ROLE_HOST	= (1 << 0),
 	TRACECMD_TIME_SYNC_ROLE_GUEST	= (1 << 1),
+	TRACECMD_TIME_SYNC_ROLE_CLIENT	= (1 << 2),
+	TRACECMD_TIME_SYNC_ROLE_SERVER	= (1 << 3),
 };
 
 /* Timestamp synchronization flags */
lib/trace-cmd/trace-timesync-ptp.c +3 -1
index 653d176e2e79..20e6e6f1ab59 100644
@@ -702,7 +702,9 @@ int ptp_clock_sync_register(void)
 {
 	return tracecmd_tsync_proto_register(PTP_NAME, PTP_ACCURACY,
 					     TRACECMD_TIME_SYNC_ROLE_GUEST |
-					     TRACECMD_TIME_SYNC_ROLE_HOST,
+					     TRACECMD_TIME_SYNC_ROLE_HOST |
+					     TRACECMD_TIME_SYNC_ROLE_CLIENT |
+					     TRACECMD_TIME_SYNC_ROLE_SERVER,
 					     0, TRACECMD_TSYNC_FLAG_INTERPOLATE,
 					     ptp_clock_sync_init,
 					     ptp_clock_sync_free,
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/8 trace-cmd record: Allow for ip connections to agents

In reply to Steven Rostedt

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

Have the -A option to trace-cmd record accept IP addresses (v4 or v6) or host names that can connect to agents that are not running on guests (or maybe they are!) but are running on other machines connecting to the network.

---
 tracecmd/include/trace-local.h |   3 +
 tracecmd/trace-record.c        | 216 +++++++++++++++++++++++----------
 2 files changed, 152 insertions(+), 67 deletions(-)
tracecmd/include/trace-local.h +3 -0
index a253b4c6432a..5c3fcfccec30 100644
@@ -189,6 +189,7 @@ enum buffer_instance_flags {
 	BUFFER_FL_AGENT		= 1 << 3,
 	BUFFER_FL_HAS_CLOCK	= 1 << 4,
 	BUFFER_FL_TSC2NSEC	= 1 << 5,
+	BUFFER_FL_NETWORK	= 1 << 6,
 };
 
 struct func_list {
@@ -280,6 +281,7 @@ struct buffer_instance {
 	int			argc;
 	char			**argv;
 
+	struct addrinfo		*result;
 	unsigned int		cid;
 	unsigned int		port;
 	int			*fds;
@@ -302,6 +304,7 @@ extern struct buffer_instance *first_instance;
 
 #define is_agent(instance)	((instance)->flags & BUFFER_FL_AGENT)
 #define is_guest(instance)	((instance)->flags & BUFFER_FL_GUEST)
+#define is_network(instance)	((instance)->flags & BUFFER_FL_NETWORK)
 
 struct buffer_instance *allocate_instance(const char *name);
 void add_instance(struct buffer_instance *instance, int cpu_count);
tracecmd/trace-record.c +149 -67

378 lines not on this page · raw message

Signature · 1 line
2.35.1
#4 lore raw

PATCH 4/8 trace-cmd agent: Allow for ip connections from the agent

In reply to Steven Rostedt

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

Add a -N option to trace-cmd agent to listen on a network socket that can be used by trace-cmd record -A to connect with.

---
 tracecmd/include/trace-local.h |  6 +++
 tracecmd/trace-agent.c         | 89 ++++++++++++++++++++++++++--------
 tracecmd/trace-listen.c        | 58 ++++++++++++++--------
 3 files changed, 113 insertions(+), 40 deletions(-)
tracecmd/include/trace-local.h +6 -0
index 5c3fcfccec30..bde3bcd8ab2c 100644
@@ -306,6 +306,12 @@ extern struct buffer_instance *first_instance;
 #define is_guest(instance)	((instance)->flags & BUFFER_FL_GUEST)
 #define is_network(instance)	((instance)->flags & BUFFER_FL_NETWORK)
 
+#define START_PORT_SEARCH 1500
+#define MAX_PORT_SEARCH 6000
+
+int trace_net_make(int port, enum port_type type);
+int trace_net_search(int start_port, int *sfd, enum port_type type);
+
 struct buffer_instance *allocate_instance(const char *name);
 void add_instance(struct buffer_instance *instance, int cpu_count);
 void update_first_instance(struct buffer_instance *instance, int topt);
tracecmd/trace-agent.c +70 -19

152 lines not on this page · raw message

tracecmd/trace-listen.c +37 -21

99 lines not on this page · raw message

Signature · 1 line
2.35.1
#5 lore raw

PATCH 5/8 trace-cmd library: Create tracecmd_debug() for debug printing

In reply to Steven Rostedt

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

Move the dprint() of trace-msg.c into trace-utils.c and export it such that other applications could use it as well. It is now renamed to tracecmd_debug().

---
 lib/trace-cmd/include/private/trace-cmd-private.h |  3 +++
 lib/trace-cmd/trace-msg.c                         | 12 +-----------
 lib/trace-cmd/trace-util.c                        | 12 ++++++++++++
 3 files changed, 16 insertions(+), 11 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +3 -0
index 6e7b346cda07..766e0a762c2b 100644
@@ -43,6 +43,9 @@ enum {
 	RINGBUF_TYPE_TIME_STAMP		= 31,
 };
 
+/* Can be overridden */
+void tracecmd_debug(const char *fmt, ...);
+
 void tracecmd_record_ref(struct tep_record *record);
 
 void tracecmd_set_debug(bool set_debug);
lib/trace-cmd/trace-msg.c +1 -11
index 6cf74f9b1c99..39465ade8ab3 100644
@@ -31,17 +31,7 @@
 typedef __u32 u32;
 typedef __be32 be32;
 
-static inline void dprint(const char *fmt, ...)
-{
-	va_list ap;
-
-	if (!tracecmd_get_debug())
-		return;
-
-	va_start(ap, fmt);
-	vprintf(fmt, ap);
-	va_end(ap);
-}
+#define dprint(fmt, ...)	tracecmd_debug(fmt, ##__VA_ARGS__)
 
 /* Two (4k) pages is the max transfer for now */
 #define MSG_MAX_LEN			8192
lib/trace-cmd/trace-util.c +12 -0
index b5a0a1a601d4..9564c81a5c99 100644
@@ -415,6 +415,18 @@ void __weak tracecmd_critical(const char *fmt, ...)
 	}
 }
 
+void __weak tracecmd_debug(const char *fmt, ...)
+{
+	va_list ap;
+
+	if (!tracecmd_get_debug())
+		return;
+
+	va_start(ap, fmt);
+	vprintf(fmt, ap);
+	va_end(ap);
+}
+
 #define LOG_BUF_SIZE 1024
 static void __plog(const char *prefix, const char *fmt, va_list ap, FILE *fp)
 {
Signature · 1 line
2.35.1
#6 lore raw

PATCH 6/8 trace-cmd: Add debug prints for network connections

In reply to Steven Rostedt

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

To help debug the network agent and listener, add some debug prints that are enabled when --debug option is used.

---
 lib/trace-cmd/trace-timesync.c | 2 ++
 tracecmd/trace-agent.c         | 3 +++
 tracecmd/trace-listen.c        | 4 ++++
 tracecmd/trace-record.c        | 9 +++++++++
 4 files changed, 18 insertions(+)
lib/trace-cmd/trace-timesync.c +2 -0
index 14cf20c870cb..cc44af38e8ad 100644
@@ -925,7 +925,9 @@ static void *tsync_agent_thread(void *data)
 	int sd;
 
 	while (true) {
+		tracecmd_debug("Listening on fd:%d\n", tsync->msg_handle->fd);
 		sd = accept(tsync->msg_handle->fd, NULL, NULL);
+		tracecmd_debug("Accepted fd:%d\n", sd);
 		if (sd < 0) {
 			if (errno == EINTR)
 				continue;
tracecmd/trace-agent.c +3 -0
index cd201ad948ad..819ac016612f 100644
@@ -22,6 +22,8 @@
 #include "trace-local.h"
 #include "trace-msg.h"
 
+#define dprint(fmt, ...)	tracecmd_debug(fmt, ##__VA_ARGS__)
+
 static void make_vsocks(int nr, int *fds, unsigned int *ports)
 {
 	unsigned int port;
@@ -55,6 +57,7 @@ static void make_net(int nr, int *fds, unsigned int *ports)
 			die("Failed to listen on port %d\n", port);
 		fds[i] = fd;
 		ports[i] = port;
+		dprint("CPU[%d]: fd:%d port:%d\n", i, fd, port);
 		start_port = port + 1;
 	}
 }
tracecmd/trace-listen.c +4 -0
index 8476fa51dadd..3b446acea972 100644
@@ -26,6 +26,8 @@
 #include "trace-local.h"
 #include "trace-msg.h"
 
+#define dprint(fmt, ...)	tracecmd_debug(fmt, ##__VA_ARGS__)
+
 #define MAX_OPTION_SIZE 4096
 
 #define _VAR_DIR_Q(dir)		#dir
@@ -279,6 +281,8 @@ int trace_net_make(int port, enum port_type type)
 	if (rp == NULL)
 		return -1;
 
+	dprint("Create listen port: %d fd:%d\n", port, sd);
+
 	return sd;
 }
 
tracecmd/trace-record.c +9 -0
index 085944296934..3470ff729606 100644
@@ -56,6 +56,8 @@
 #define FUNC_STACK_TRACE "func_stack_trace"
 #define TSC_CLOCK	"x86-tsc"
 
+#define dprint(fmt, ...)	tracecmd_debug(fmt, ##__VA_ARGS__)
+
 enum trace_type {
 	TRACE_TYPE_RECORD	= 1,
 	TRACE_TYPE_START	= (1 << 1),
@@ -3139,6 +3141,9 @@ static struct addrinfo *do_getaddrinfo(const char *host, unsigned int port,
 		return NULL;
 	}
 
+	dprint("Attached port %s: %d to results: %p\n",
+	       type == USE_TCP ? "TCP" : "UDP", port, results);
+
 	return results;
 }
 
@@ -3160,6 +3165,8 @@ static int connect_addr(struct addrinfo *results)
 	if (rp == NULL)
 		return -1;
 
+	dprint("connect results: %p with fd: %d\n", results, sfd);
+
 	return sfd;
 }
 
@@ -3193,7 +3200,9 @@ static int do_accept(int sd)
 	int cd;
 
 	for (;;) {
+		dprint("Wait on accept: %d\n", sd);
 		cd = accept(sd, NULL, NULL);
+		dprint("accepted: %d\n", cd);
 		if (cd < 0) {
 			if (errno == EINTR)
 				continue;
Signature · 1 line
2.35.1
#7 lore raw

PATCH 7/8 trace-cmd: Add print helpers to show connections

In reply to Steven Rostedt

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

Add helper functions trace_net_print_connection() and trace_vsock_print_connection() to print the host/port or cid/port that has connected.

---
 tracecmd/include/trace-local.h |  6 ++++++
 tracecmd/trace-agent.c         | 14 ++++++++++++++
 tracecmd/trace-listen.c        | 22 ++++++++++++++++++++++
 tracecmd/trace-vsock.c         | 20 ++++++++++++++++++++
 4 files changed, 62 insertions(+)
tracecmd/include/trace-local.h +6 -0
index bde3bcd8ab2c..02839e37eb2d 100644
@@ -311,6 +311,7 @@ extern struct buffer_instance *first_instance;
 
 int trace_net_make(int port, enum port_type type);
 int trace_net_search(int start_port, int *sfd, enum port_type type);
+int trace_net_print_connection(int fd);
 
 struct buffer_instance *allocate_instance(const char *name);
 void add_instance(struct buffer_instance *instance, int cpu_count);
@@ -366,6 +367,7 @@ int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid);
 int trace_vsock_get_port(int sd, unsigned int *port);
 bool trace_vsock_can_splice_read(void);
 int trace_vsock_local_cid(void);
+int trace_vsock_print_connection(int fd);
 #else
 static inline int trace_vsock_open(unsigned int cid, unsigned int port)
 {
@@ -404,6 +406,10 @@ static inline int trace_vsock_local_cid(void)
 {
 	return -ENOTSUP;
 }
+static inline int trace_vsock_print_connection(int fd)
+{
+	return -1;
+}
 #endif /* VSOCK */
 
 /* No longer in event-utils.h */
tracecmd/trace-agent.c +14 -0
index 819ac016612f..59cecae770a6 100644
@@ -109,6 +109,18 @@ static char *get_clock(int argc, char **argv)
 	return NULL;
 }
 
+static void trace_print_connection(int fd, bool network)
+{
+	int ret;
+
+	if (network)
+		ret = trace_net_print_connection(fd);
+	else
+		ret = trace_vsock_print_connection(fd);
+	if (ret < 0)
+		tracecmd_debug("Could not print connection fd:%d\n", fd);
+}
+
 static void agent_handle(int sd, int nr_cpus, int page_size, bool network)
 {
 	struct tracecmd_tsync_protos *tsync_protos = NULL;
@@ -273,6 +285,8 @@ static void agent_serve(unsigned int port, bool do_daemon, bool network)
 				continue;
 			die("accept");
 		}
+		if (tracecmd_get_debug())
+			trace_print_connection(cd, network);
 
 		if (handler_pid)
 			goto busy;
tracecmd/trace-listen.c +22 -0
index 3b446acea972..b7be761d032e 100644
@@ -756,6 +756,28 @@ static int do_fork(int cfd)
 	return 0;
 }
 
+int trace_net_print_connection(int fd)
+{
+	char host[NI_MAXHOST], service[NI_MAXSERV];
+	struct sockaddr_storage net_addr;
+	socklen_t addr_len;
+
+	addr_len = sizeof(net_addr);
+	if (getpeername(fd, (struct sockaddr *)&net_addr, &addr_len))
+		return -1;
+
+	if (getnameinfo((struct sockaddr *)&net_addr, addr_len,
+			host, NI_MAXHOST,
+			service, NI_MAXSERV, NI_NUMERICSERV))
+		return -1;
+
+	if (tracecmd_get_debug())
+		tracecmd_debug("Connected to %s:%s fd:%d\n", host, service, fd);
+	else
+		tracecmd_plog("Connected to %s:%s\n", host, service);
+	return 0;
+}
+
 static int do_connection(int cfd, struct sockaddr *addr,
 			  socklen_t addr_len)
 {
tracecmd/trace-vsock.c +20 -0
index d18ecb45004e..39294e7a2a3c 100644
@@ -94,6 +94,26 @@ int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid)
 	return 0;
 }
 
+int trace_vsock_print_connection(int fd)
+{
+	struct sockaddr_vm vm_addr;
+	socklen_t addr_len;
+	int cid, port;
+
+	addr_len = sizeof(vm_addr);
+	if (getpeername(fd, (struct sockaddr *)&vm_addr, &addr_len))
+		return -1;
+	if (vm_addr.svm_family != AF_VSOCK)
+		return -1;
+	cid = vm_addr.svm_cid;
+	port = vm_addr.svm_port;
+	if (tracecmd_get_debug())
+		tracecmd_debug("Connected to @%u:%u fd:%d\n", cid, port, fd);
+	else
+		tracecmd_plog("Connected to @%u:%u\n", cid, port);
+	return 0;
+}
+
 static int try_splice_read_vsock(void)
 {
 	int ret, sd, brass[2];
Signature · 1 line
2.35.1
#8 lore raw

PATCH 8/8 trace-cmd: Override tracecmd_debug() to show thread id

In reply to Steven Rostedt

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

To differentiate threads, have tracecmd_debug() show the thread id of the debug prints.

---
 tracecmd/trace-cmd.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tracecmd/trace-cmd.c +18 -0
index a83a8d0bb9e4..3f3e69a7f1e2 100644
@@ -10,12 +10,17 @@
 #include <dirent.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <sys/syscall.h>
 
 #include "trace-local.h"
 
 int silence_warnings;
 int show_status;
 
+#ifndef gettid
+#define gettid() syscall(__NR_gettid)
+#endif
+
 void warning(const char *fmt, ...)
 {
 	va_list ap;
@@ -45,6 +50,19 @@ void *malloc_or_die(unsigned int size)
 	return data;
 }
 
+void tracecmd_debug(const char *fmt, ...)
+{
+	va_list ap;
+
+	if (!tracecmd_get_debug())
+		return;
+
+	va_start(ap, fmt);
+	printf("[%d] ", (int)gettid());
+	vprintf(fmt, ap);
+	va_end(ap);
+}
+
 static struct trace_log_severity {
 	int		id;
 	const char	*name;
Signature · 1 line
2.35.1