Detect if splice(2) reading is supported for vsock sockets (Linux 4.20 and later) and use it, or fallback to read/write-ing otherwise. Signed-off-by: Slavomir Kaslev <kaslevs@xxxxxxxxxx> --- tracecmd/trace-record.c | 59 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 1622b5e..cd80462 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -2658,12 +2658,61 @@ static int open_vsock(unsigned int cid, unsigned int port) return sd; } + +static int try_splice_read_vsock(void) +{ + int ret, sd, brass[2]; + + sd = socket(AF_VSOCK, SOCK_STREAM, 0); + if (sd < 0) + return sd; + + ret = pipe(brass); + if (ret < 0) + goto out_close_sd; + + /* + * On kernels that don't support splice reading from vsock sockets + * this will fail with EINVAL, or ENOTCONN otherwise. + * Technically, it should never succeed but if it does, claim splice + * reading is supported. + */ + ret = splice(sd, NULL, brass[1], NULL, 10, 0); + if (ret < 0) + ret = errno != EINVAL; + else + ret = 1; + + close(brass[0]); + close(brass[1]); +out_close_sd: + close(sd); + return ret; +} + +static bool can_splice_read_vsock(void) +{ + static bool initialized, res; + + if (initialized) + return res; + + res = try_splice_read_vsock() > 0; + initialized = true; + return res; +} + #else static inline int open_vsock(unsigned int cid, unsigned int port) { die("vsock is not supported"); return -1; } + +static bool can_splice_read_vsock(void) +{ + return false; +} #endif static int do_accept(int sd) @@ -2868,13 +2917,16 @@ create_recorder_instance(struct buffer_instance *instance, const char *file, int if (instance->flags & BUFFER_FL_GUEST) { int sd; + unsigned int flags; sd = open_vsock(instance->cid, instance->client_ports[cpu]); if (sd < 0) die("Failed to connect to agent"); - return tracecmd_create_recorder_virt( - file, cpu, recorder_flags | TRACECMD_RECORD_NOSPLICE, sd); + flags = recorder_flags; + if (!can_splice_read_vsock()) + flags |= TRACECMD_RECORD_NOSPLICE; + return tracecmd_create_recorder_virt(file, cpu, flags, sd); } if (brass) @@ -2928,7 +2980,8 @@ static int create_recorder(struct buffer_instance *instance, int cpu, if (instance->flags & BUFFER_FL_AGENT) { fd = do_accept(instance->fds[cpu]); - flags |= TRACECMD_RECORD_NOSPLICE; + if (!can_splice_read_vsock()) + flags |= TRACECMD_RECORD_NOSPLICE; } else { fd = connect_port(host, instance->client_ports[cpu]); } -- 2.19.1
![]() |