trace-cmd: Allow agent to use networking
v2 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.
Depends on: https://patchwork.kernel.org/project/linux-trace-devel/list/?series=632870
https://lore.kernel.org/r/[email protected]
Changes since v1: https://lore.kernel.org/r/[email protected]
- Rebased on the above dependency.
- Added patch to allow compiling of agent without vsocket support.
Steven Rostedt (Google) (9):
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 trace-cmd agent: Have agent work without vsockets available
lib/trace-cmd/Makefile | 2 +- .../include/private/trace-cmd-private.h | 5 + lib/trace-cmd/include/trace-tsync-local.h | 6 +- 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/Makefile | 5 +- tracecmd/include/trace-local.h | 16 ++ tracecmd/trace-agent.c | 106 ++++++-- tracecmd/trace-cmd.c | 20 +- tracecmd/trace-listen.c | 84 ++++-- tracecmd/trace-record.c | 256 ++++++++++++------ tracecmd/trace-usage.c | 2 - tracecmd/trace-vsock.c | 20 ++ 15 files changed, 406 insertions(+), 146 deletions(-)
Signature · 1 line
2.35.1
PATCH v2 1/9 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
@@ -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
Re: [PATCH v2 1/9] trace-cmd record: Move port_type into instance
Quoted 18 lines · Steven Rostedt
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.Signed-off-by Steven Rostedt (Google) <[email protected]>--- tracecmd/include/trace-local.h | 1 + tracecmd/trace-record.c | 41 ++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-)tracecmd/include/trace-local.h +0
-0
tracecmd/include/trace-local.h struct buffer_instance { · line 288
Quoted 4 lines
tracecmd/include/trace-local.h +1
-0@@ -285,6 +285,7 @@ struct buffer_instance {int *fds;bool use_fifos; +enum port_type port_type;
I would suggest setting the default value of port_type explicitly in allocate_instance(). Even though USE_UDP is 0, it is more clear. The first time when I went through these changes, I was wondering why UDP is not set at all. Then found that it was a static boolean in the previous implementation, defaulting to false.
Quoted 163 lines
tracecmd/include/trace-local.h +0
-0int tsync_loop_interval;struct tracecmd_time_sync *tsync; };tracecmd/trace-record.c +22
-20@@ -90,8 +90,6 @@ static bool fork_process;/* Max size to let a per cpu file get */ static int max_kb; -static enum port_type port_type; -static int do_ptrace;static int filter_task;@@ -3119,7 +3117,7 @@ static void finish(int sig) finished =1 ; } -static int connect_port (const char *host,unsigned int port) +static int connect_port (const char *host,unsigned int port,enum port_type type) {struct addrinfo hints;struct addrinfo *results, *rp;@@ -3128,17 +3126,17 @@ static int connect_port(const char *host, unsigned int port)snprintf (buf,BUFSIZ ,"%u" , port); -if (port_type ==USE_VSOCK ) +if (type ==USE_VSOCK )return trace_vsock_open (atoi (host), port);memset (&hints,0 ,sizeof (hints)); hints.ai_family =AF_UNSPEC ; - hints.ai_socktype = port_type ==USE_TCP ?SOCK_STREAM :SOCK_DGRAM ; + hints.ai_socktype = type ==USE_TCP ?SOCK_STREAM :SOCK_DGRAM ; s =getaddrinfo (host, buf, &hints, &results);if (s !=0 )die ("connecting to %s server %s:%s" , - port_type ==USE_TCP ?"TCP" :"UDP" , host, buf); + type ==USE_TCP ?"TCP" :"UDP" , host, buf);for (rp = results; rp !=NULL ; rp = rp->ai_next) { sfd = socket(rp->ai_family, rp->ai_socktype,@@ -3152,7 +3150,7 @@ static int connect_port(const char *host, unsigned int port)if (rp ==NULL )die ("Can not connect to %s server %s:%s" , - port_type ==USE_TCP ?"TCP" :"UDP" , host, buf); + type ==USE_TCP ?"TCP" :"UDP" , host, buf);freeaddrinfo (results);@@ -3359,7 +3357,8 @@ static int create_recorder(struct buffer_instance *instance, int cpu,else fd =do_accept (instance->fds[cpu]); }else { - fd =connect_port (host, instance->client_ports[cpu]); + fd =connect_port (host, instance->client_ports[cpu], + instance->port_type); }if (fd <0 )die ("Failed connecting to client" );@@ -3414,8 +3413,9 @@ static void check_first_msg_from_server(struct tracecmd_msg_handle *msg_handle) }static void communicate_with_listener_v1 (struct tracecmd_msg_handle *msg_handle, -unsigned int **client_ports) +struct buffer_instance *instance) { +unsigned int *client_ports;char buf[BUFSIZ ];ssize_t n;int cpu, i;@@ -3442,11 +3442,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" ); - port_type =USE_TCP ; + instance->port_type =USE_TCP ; msg_handle->flags |=TRACECMD_MSG_FL_USE_TCP ; } -if (port_type ==USE_TCP ) { +if (instance->port_type ==USE_TCP ) {/* Send one option */ write (msg_handle->fd,"1" ,2 );/* Size 4 */ @@ -3457,8 +3457,8 @@ static void communicate_with_listener_v1(struct tracecmd_msg_handle *msg_handle,/* No options */ write (msg_handle->fd,"0" ,2 ); - *client_ports =malloc (local_cpu_count *sizeof (*client_ports)); -if (!*client_ports) + client_ports =malloc (local_cpu_count *sizeof (*client_ports)); +if (!client_ports)die ("Failed to allocate client ports for %d cpus" , local_cpu_count);/* @@ -3476,8 +3476,10 @@ static void communicate_with_listener_v1(struct tracecmd_msg_handle *msg_handle,if (i ==BUFSIZ )die ("read bad port number" ); buf[i] =0 ; - (*client_ports)[cpu] =atoi (buf); + client_ports[cpu] =atoi (buf); } + + instance->client_ports = client_ports; } static void communicate_with_listener_v3(struct tracecmd_msg_handle *msg_handle,@@ -3609,10 +3611,11 @@ static int connect_ip(char *thost)static struct tracecmd_msg_handle *setup_network (struct buffer_instance *instance) {struct tracecmd_msg_handle *msg_handle =NULL ; +enum port_type type = instance->port_type;int sfd; again: -switch (port_type) { +switch (type) {case USE_VSOCK : sfd =connect_vsock (host);break ;@@ -3634,7 +3637,7 @@ again: msg_handle->version =V3_PROTOCOL ; } -switch (port_type) { +switch (type) {case USE_TCP : msg_handle->flags |=TRACECMD_MSG_FL_USE_TCP ;break ;@@ -3657,7 +3660,7 @@ again: }if (msg_handle->version ==V1_PROTOCOL ) -communicate_with_listener_v1 (msg_handle, &instance->client_ports); +communicate_with_listener_v1 (msg_handle, instance);return msg_handle; }@@ -6514,7 +6517,7 @@ static void parse_record_options(int argc,if (ctx->output)die ("-V incompatible with -o" ); host = optarg; - port_type =USE_VSOCK ; + ctx->instance->port_type =USE_VSOCK ;break ;case 'm' :if (max_kb)@@ -6532,7 +6535,7 @@ static void parse_record_options(int argc,if (IS_EXTRACT (ctx)) ctx->topt =1 ;/* Extract top instance also */ else - port_type =USE_TCP ; + ctx->instance->port_type =USE_TCP ;break ;case 'b' :check_instance_die (ctx->instance,"-b" ); --
Quoted 2 lines
2.35.1
--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
Re: [PATCH v2 1/9] trace-cmd record: Move port_type into instance
Quoted 5 lines · On Mon, 18 Apr 2022 13:22:21 +0300 Tzvetomir Stoyanov <[email protected]>…
Quoted 5 lines
+++
b/tracecmd/include/trace-local.htracecmd/include/trace-local.h +0
-0@@ -285,6 +285,7 @@ struct buffer_instance {int *fds;bool use_fifos;
Quoted 7 lines
tracecmd/include/trace-local.h struct buffer_instance { · line 288
Quoted 1 line
tracecmd/include/trace-local.h +1
-0+enum port_type port_type;I would suggest setting the default value of
port_typeexplicitly inallocate_instance(). Even thoughUSE_UDPis 0, it is more clear. The first time when I went through these changes, I was wondering why UDP is not set at all. Then found that it was a static boolean in the previous implementation, defaulting to false.
I could add a comment. I purposely had USE_UDP to zero for this purpose. This is a common trick to do in the kernel as well.
But it should have a comment stating that here. I'll update it in v3.
Thanks for the review.
-- Steve
PATCH v2 2/9 trace-cmd library: Add network roles for time sync
In reply to Steven RostedtFrom: "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
@@ -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
@@ -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
PATCH v2 3/9 trace-cmd record: Allow for ip connections to agents
In reply to Steven RostedtFrom: "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 | 214 +++++++++++++++++++++++---------- 2 files changed, 151 insertions(+), 66 deletions(-)
tracecmd/include/trace-local.h +3 -0
@@ -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 +148 -66
375 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v2 4/9 trace-cmd agent: Allow for ip connections from the agent
In reply to Steven RostedtFrom: "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
@@ -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
Re: [PATCH v2 4/9] trace-cmd agent: Allow for ip connections from the agent
Quoted 6 lines · 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.
I have concerns about exposing the agent over the network, without any client authentication. It runs with root privileges, the kernel tracing data will be exposed to anyone over the network. At least, this should be written clearly in the documentation and even should be printed by "trace-cmd agent -N ...". I saw your patch that adds a name or IP address of the client as parameter, it is some security, but the tracing data itself still flows unencrypted over the network and could be visible to anyone.
Quoted 296 lines
Signed-off-by Steven Rostedt (Google) <[email protected]>--- 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@@ -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@@ -41,6 +41,32 @@ static void make_vsocks(int nr, int *fds, unsigned int *ports) } } +static void make_net (int nr,int *fds,unsigned int *ports) +{ +int port; +int i, fd; +int start_port =START_PORT_SEARCH ; + +for (i =0 ; i < nr; i++) { + port =trace_net_search (start_port, &fd,USE_TCP ); +if (port <0 ) +die ("Failed to open socket" ); +if (listen (fd,5 ) <0 ) +die ("Failed to listen on port %d\n" , port); + fds[i] = fd; + ports[i] = port; + start_port = port +1 ; + } +} + +static void make_sockets (int nr,int *fds,unsigned int *ports,bool network) +{ +if (network) +return make_net (nr, fds, ports); +else +return make_vsocks (nr, fds, ports); +} +static int open_agent_fifos (int nr_cpus,int *fds) {char path[PATH_MAX ];@@ -80,7 +106,7 @@ static char *get_clock(int argc, char **argv)return NULL ; } -static void agent_handle (int sd,int nr_cpus,int page_size) +static void agent_handle (int sd,int nr_cpus,int page_size,bool network) {struct tracecmd_tsync_protos *tsync_protos =NULL ;struct tracecmd_time_sync *tsync =NULL ;@@ -117,17 +143,31 @@ static void agent_handle(int sd, int nr_cpus, int page_size) use_fifos = false;if (!use_fifos) -make_vsocks (nr_cpus, fds, ports); +make_sockets (nr_cpus, fds, ports, network);if (tsync_protos && tsync_protos->names) { -if (get_vsocket_params (msg_handle->fd, &local_id, - &remote_id)) { -warning ("Failed to get local and remote ids" ); -/* Just make something up */ - remote_id =-1 ; - local_id =-2 ; +if (network) { +/* For now just use something */ + remote_id =2 ; + local_id =1 ; + tsync_port =trace_net_search (START_PORT_SEARCH , &fd,USE_TCP ); +if (listen (fd,5 ) <0 ) +die ("Failed to listen on %d\n" , tsync_port); + }else { +if (get_vsocket_params (msg_handle->fd, &local_id, + &remote_id)) { +warning ("Failed to get local and remote ids" ); +/* Just make something up */ + remote_id =-1 ; + local_id =-2 ; + } + fd =trace_vsock_make_any (); +if (fd >=0 && +trace_vsock_get_port (fd, &tsync_port) <0 ) { +close (fd); + fd =-1 ; + } } - fd =trace_vsock_make_any (); -if (fd >=0 &&trace_vsock_get_port (fd, &tsync_port) >=0 ) { +if (fd >=0 ) { tsync =tracecmd_tsync_with_host (fd, tsync_protos,get_clock (argc, argv), remote_id, local_id);@@ -193,7 +233,7 @@ static pid_t do_fork()return fork (); } -static void agent_serve (unsigned int port,bool do_daemon) +static void agent_serve (unsigned int port,bool do_daemon,bool network) {int sd, cd, nr_cpus;unsigned int cid;@@ -204,14 +244,21 @@ static void agent_serve(unsigned int port, bool do_daemon) nr_cpus =tracecmd_count_cpus (); page_size =getpagesize (); - sd =trace_vsock_make (port); +if (network) { + sd =trace_net_make (port,USE_TCP ); +if (listen (sd,5 ) <0 ) +die ("Failed to listen on %d\n" , port); + }else + sd =trace_vsock_make (port);if (sd <0 ) -die ("Failed to open vsocket" ); +die ("Failed to open socket" );tracecmd_tsync_init (); - cid =trace_vsock_local_cid (); -if (cid >=0 ) -printf ("listening on @%u:%u\n" , cid, port); +if (!network) { + cid =trace_vsock_local_cid (); +if (cid >=0 ) +printf ("listening on @%u:%u\n" , cid, port); + }if (do_daemon &&daemon (1 ,0 ))die ("daemon" );@@ -231,7 +278,7 @@ static void agent_serve(unsigned int port, bool do_daemon)if (pid ==0 ) {close (sd);signal (SIGCHLD ,SIG_DFL ); -agent_handle (cd, nr_cpus, page_size); +agent_handle (cd, nr_cpus, page_size, network); }if (pid >0 ) handler_pid = pid;@@ -250,6 +297,7 @@ void trace_agent(int argc, char **argv) {bool do_daemon = false;unsigned int port =TRACE_AGENT_DEFAULT_PORT ; +bool network = false;if (argc <2 )usage (argv);@@ -267,7 +315,7 @@ void trace_agent(int argc, char **argv) {NULL, 0, NULL, 0} }; - c =getopt_long (argc-1 , argv+1 ,"+hp:D" , + c =getopt_long (argc-1 , argv+1 ,"+hp:DN" , long_options, &option_index);if (c ==-1 )break ;@@ -275,6 +323,9 @@ void trace_agent(int argc, char **argv)case 'h' :usage (argv);break ; +case 'N' : + network = true; +break ;case 'p' : port =atoi (optarg);break ;@@ -296,5 +347,5 @@ void trace_agent(int argc, char **argv)if (optind < argc-1 )usage (argv); -agent_serve (port, do_daemon); +agent_serve (port, do_daemon, network); }tracecmd/trace-listen.c +37
-22@@ -234,52 +234,68 @@ static int setup_vsock_port(int start_port, int *sfd)return start_port; } -#define START_PORT_SEARCH 1500 -#define MAX_PORT_SEARCH 6000 - -static int bind_a_port (int start_port,int *sfd,enum port_type type) +int trace_net_make (int port,enum port_type type) {struct addrinfo hints;struct addrinfo *result, *rp;char buf[BUFSIZ ]; +int sd;int s; -int num_port = start_port; -if (type ==USE_VSOCK ) -return setup_vsock_port (start_port, sfd); - again: -snprintf (buf,BUFSIZ ,"%d" , num_port); +snprintf (buf,BUFSIZ ,"%d" , port);memset (&hints,0 ,sizeof (hints)); hints.ai_family =AF_UNSPEC ; - hints.ai_socktype = type ==USE_TCP ?SOCK_STREAM :SOCK_DGRAM ; hints.ai_flags =AI_PASSIVE ; +switch (type) { +case USE_TCP : + hints.ai_socktype =SOCK_STREAM ; +break ; +case USE_UDP : + hints.ai_socktype =SOCK_DGRAM ; +break ; +default : +return -1 ; + } + s =getaddrinfo (NULL , buf, &hints, &result);if (s !=0 )pdie ("getaddrinfo: error opening socket" );for (rp = result; rp !=NULL ; rp = rp->ai_next) { - *sfd =socket (rp->ai_family, rp->ai_socktype, - rp->ai_protocol); -if (*sfd <0 ) + sd =socket (rp->ai_family, rp->ai_socktype, + rp->ai_protocol); +if (sd <0 )continue ; -if (bind (*sfd, rp->ai_addr, rp->ai_addrlen) ==0 ) +if (bind (sd, rp->ai_addr, rp->ai_addrlen) ==0 )break ; -close (*sfd); +close (sd); } +freeaddrinfo (result); + +if (rp ==NULL ) +return -1 ; + +return sd; +} + +int trace_net_search (int start_port,int *sfd,enum port_type type) +{ +int num_port = start_port; -if (rp ==NULL ) { -freeaddrinfo (result); +if (type ==USE_VSOCK ) +return setup_vsock_port (start_port, sfd); + again: + *sfd =trace_net_make (num_port, type); +if (*sfd <0 ) {if (++num_port >MAX_PORT_SEARCH )pdie ("No available ports to bind" ); goto again; } -freeaddrinfo (result); -return num_port; }@@ -309,10 +325,10 @@ static int open_port(const char *node, const char *port, int *pid,int num_port;/* -* bind_a_port() currently does not return an error, but if that +* trace_net_search() 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, type); + num_port =trace_net_search (start_port, &sfd, type);if (num_port <0 )return num_port; --
Quoted 2 lines
2.35.1
--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
Re: [PATCH v2 4/9] trace-cmd agent: Allow for ip connections from the agent
Quoted 8 lines · On Mon, 18 Apr 2022 13:22:51 +0300 Tzvetomir Stoyanov <[email protected]>…
Quoted 6 lines · 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.
Quoted 8 lines
I have concerns about exposing the agent over the network, without any client authentication. It runs with root privileges, the kernel tracing data will be exposed to anyone over the network. At least, this should be written clearly in the documentation and even should be printed by "trace-cmd agent -N ...". I saw your patch that adds a name or IP address of the client as parameter, it is some security, but the tracing data itself still flows unencrypted over the network and could be visible to anyone.
True. But it's similar to telnet. The use case for this option is for local networks where you expect to "trust" the network.
I'm fine with adding more warnings and stating that this is "NOT SECURE" in big letters to let people know the dangers of it. I'm hoping to have more authentication in the future.
I could even have it print a warning "UNSECURE CONNECTION" or something to enforce that this is not something you want to use in any scenario.
-- Steve
PATCH v2 5/9 trace-cmd library: Create tracecmd_debug() for debug printing
In reply to Steven RostedtFrom: "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
@@ -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
@@ -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
@@ -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 1024static void __plog (const char *prefix,const char *fmt,va_list ap,FILE *fp) {
Signature · 1 line
2.35.1
PATCH v2 6/9 trace-cmd: Add debug prints for network connections
In reply to Steven RostedtFrom: "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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH v2 7/9 trace-cmd: Add print helpers to show connections
In reply to Steven RostedtFrom: "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
@@ -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) {
@@ -403,6 +405,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
@@ -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
@@ -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
@@ -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
PATCH v2 8/9 trace-cmd: Override tracecmd_debug() to show thread id
In reply to Steven RostedtFrom: "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
@@ -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
PATCH v2 9/9 trace-cmd agent: Have agent work without vsockets available
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Compile agent code without vsockets available to allow it to work with networking. This includes allowing the ptp time synchronization protocol to be used.
--- lib/trace-cmd/Makefile | 2 +- lib/trace-cmd/include/trace-tsync-local.h | 6 +----- tracecmd/Makefile | 5 ++--- tracecmd/trace-cmd.c | 2 -- tracecmd/trace-usage.c | 2 -- 5 files changed, 4 insertions(+), 13 deletions(-)
lib/trace-cmd/Makefile +1 -1
@@ -21,8 +21,8 @@ ifeq ($(PERF_DEFINED), 1) OBJS += trace-perf.o endif OBJS += trace-timesync.o -ifeq ($(VSOCK_DEFINED), 1) OBJS += trace-timesync-ptp.o +ifeq ($(VSOCK_DEFINED), 1) OBJS += trace-timesync-kvm.o endif OBJS += trace-compress.o
lib/trace-cmd/include/trace-tsync-local.h +1 -5
@@ -64,15 +64,11 @@ int tracecmd_tsync_proto_register(const char *proto_name, int accuracy, int role long long *, long long *, long long*, long long *, unsigned int));int tracecmd_tsync_proto_unregister (char *proto_name); +int ptp_clock_sync_register (void );#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 ;
tracecmd/Makefile +2 -3
@@ -37,11 +37,10 @@ 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 TRACE_CMD_OBJS += trace-setup-guest.o +ifeq ($(VSOCK_DEFINED), 1) +TRACE_CMD_OBJS += trace-vsock.o endif ALL_OBJS := $(TRACE_CMD_OBJS:%.o=$(bdir)/%.o)
tracecmd/trace-cmd.c +0 -2
@@ -127,10 +127,8 @@ struct command commands[] = { {"hist", trace_hist}, {"mem", trace_mem}, {"listen", trace_listen}, -#ifdef VSOCK {"agent", trace_agent}, {"setup-guest", trace_setup_guest}, -#endif {"split", trace_split}, {"restore", trace_restore}, {"stack", trace_stack},
tracecmd/trace-usage.c +0 -2
@@ -325,7 +325,6 @@ static struct usage_help usage_help[] = {" -l logfile to write messages to.\n" " --verbose 'level' Set the desired log level\n" }, -#ifdef VSOCK {"agent" , "listen on a vsocket for trace clients",
@@ -344,7 +343,6 @@ static struct usage_help usage_help[] = {" -g FIFOs group owner\n" " -a Attach FIFOs to guest VM config\n" }, -#endif {"list" , "list the available events, plugins or options",
Signature · 1 line
2.35.1