From patchwork Fri Apr 15 01:00:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12814182 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B96CEC433EF for ; Fri, 15 Apr 2022 01:00:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244661AbiDOBC6 (ORCPT ); Thu, 14 Apr 2022 21:02:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348323AbiDOBCi (ORCPT ); Thu, 14 Apr 2022 21:02:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A50F2ED75 for ; Thu, 14 Apr 2022 18:00:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0AEAB621FD for ; Fri, 15 Apr 2022 01:00:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6E06C385AB; Fri, 15 Apr 2022 01:00:09 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nfAJk-003w8U-Mk; Thu, 14 Apr 2022 21:00:08 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 1/8] trace-cmd: Add NO_VSOCK make option to force vsock code off Date: Thu, 14 Apr 2022 21:00:00 -0400 Message-Id: <20220415010007.938408-2-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415010007.938408-1-rostedt@goodmis.org> References: <20220415010007.938408-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" Add "make NO_VSOCK=1" to disable building with vsocket code. It is equivalent to not having vsocket support on the system. Signed-off-by: Steven Rostedt (Google) --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 9ccaaa61fecb..982514baad01 100644 --- a/Makefile +++ b/Makefile @@ -287,7 +287,11 @@ CFLAGS ?= -g -Wall CPPFLAGS ?= LDFLAGS ?= +ifndef NO_VSOCK VSOCK_DEFINED := $(shell if (echo "$(pound)include " | $(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) From patchwork Fri Apr 15 01:00:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12814179 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 867ABC433EF for ; Fri, 15 Apr 2022 01:00:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348396AbiDOBCz (ORCPT ); Thu, 14 Apr 2022 21:02:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348318AbiDOBCh (ORCPT ); Thu, 14 Apr 2022 21:02:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B1A427FC0 for ; Thu, 14 Apr 2022 18:00:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 81553621F8 for ; Fri, 15 Apr 2022 01:00:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D315BC385A5; Fri, 15 Apr 2022 01:00:09 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nfAJk-003w8X-NY; Thu, 14 Apr 2022 21:00:08 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 2/8] trace-cmd library: Remove ports from clock context Date: Thu, 14 Apr 2022 21:00:01 -0400 Message-Id: <20220415010007.938408-3-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415010007.938408-1-rostedt@goodmis.org> References: <20220415010007.938408-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" The local and remote port fields are set but never used. Remove them. Signed-off-by: Steven Rostedt (Google) --- lib/trace-cmd/include/trace-tsync-local.h | 2 -- lib/trace-cmd/trace-timesync.c | 11 +++-------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/trace-cmd/include/trace-tsync-local.h b/lib/trace-cmd/include/trace-tsync-local.h index b357662c8d41..d0f82f5d0916 100644 --- a/lib/trace-cmd/include/trace-tsync-local.h +++ b/lib/trace-cmd/include/trace-tsync-local.h @@ -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, diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c index 9e82094eaa31..514d333e57a7 100644 --- a/lib/trace-cmd/trace-timesync.c +++ b/lib/trace-cmd/trace-timesync.c @@ -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, From patchwork Fri Apr 15 01:00:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12814181 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57DA9C433FE for ; Fri, 15 Apr 2022 01:00:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243703AbiDOBC5 (ORCPT ); Thu, 14 Apr 2022 21:02:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348326AbiDOBCi (ORCPT ); Thu, 14 Apr 2022 21:02:38 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E3DC27CCC for ; Thu, 14 Apr 2022 18:00:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 25FF5B82BEF for ; Fri, 15 Apr 2022 01:00:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA437C385A1; Fri, 15 Apr 2022 01:00:09 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nfAJk-003w8a-OO; Thu, 14 Apr 2022 21:00:08 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 3/8] trace-cmd library: Remove vsocket dependency on P2P protocol Date: Thu, 14 Apr 2022 21:00:02 -0400 Message-Id: <20220415010007.938408-4-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415010007.938408-1-rostedt@goodmis.org> References: <20220415010007.938408-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- 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(-) diff --git a/lib/trace-cmd/include/trace-tsync-local.h b/lib/trace-cmd/include/trace-tsync-local.h index d0f82f5d0916..4340dfaf80ca 100644 --- a/lib/trace-cmd/include/trace-tsync-local.h +++ b/lib/trace-cmd/include/trace-tsync-local.h @@ -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, diff --git a/lib/trace-cmd/trace-timesync-ptp.c b/lib/trace-cmd/trace-timesync-ptp.c index 4f7805627259..0e23ff83869a 100644 --- a/lib/trace-cmd/trace-timesync-ptp.c +++ b/lib/trace-cmd/trace-timesync-ptp.c @@ -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 diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c index 514d333e57a7..594f660e2a40 100644 --- a/lib/trace-cmd/trace-timesync.c +++ b/lib/trace-cmd/trace-timesync.c @@ -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; From patchwork Fri Apr 15 01:00:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12814180 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D646BC433F5 for ; Fri, 15 Apr 2022 01:00:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239380AbiDOBCz (ORCPT ); Thu, 14 Apr 2022 21:02:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348319AbiDOBCh (ORCPT ); Thu, 14 Apr 2022 21:02:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A69B27CCC for ; Thu, 14 Apr 2022 18:00:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 719A4621F7 for ; Fri, 15 Apr 2022 01:00:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDF1DC385A7; Fri, 15 Apr 2022 01:00:09 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nfAJk-003w8d-PC; Thu, 14 Apr 2022 21:00:08 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 4/8] trace-cmd library: Remove dependency on vsocks for sync identifiers Date: Thu, 14 Apr 2022 21:00:03 -0400 Message-Id: <20220415010007.938408-5-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415010007.938408-1-rostedt@goodmis.org> References: <20220415010007.938408-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- .../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(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 69343765c5ff..24295e4e09d3 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -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, diff --git a/lib/trace-cmd/include/trace-tsync-local.h b/lib/trace-cmd/include/trace-tsync-local.h index 4340dfaf80ca..697c076c43a6 100644 --- a/lib/trace-cmd/include/trace-tsync-local.h +++ b/lib/trace-cmd/include/trace-tsync-local.h @@ -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 { diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c index 594f660e2a40..b41aece82082 100644 --- a/lib/trace-cmd/trace-timesync.c +++ b/lib/trace-cmd/trace-timesync.c @@ -405,29 +405,6 @@ int __hidden vsock_get_port(int sd, unsigned int *port) return 0; } -static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) -{ - struct sockaddr_vm addr; - socklen_t addr_len = sizeof(addr); - - memset(&addr, 0, sizeof(addr)); - if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) - return -1; - if (addr.svm_family != AF_VSOCK) - return -1; - *lcid = addr.svm_cid; - - memset(&addr, 0, sizeof(addr)); - addr_len = sizeof(addr); - if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) - return -1; - if (addr.svm_family != AF_VSOCK) - return -1; - *rcid = addr.svm_cid; - - return 0; -} - #else static int vsock_open(unsigned int cid, unsigned int port) { @@ -444,12 +421,6 @@ static int vsock_get_port(int sd, unsigned int *port) { return -ENOTSUP; } - -static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) -{ - return -ENOTSUP; -} - #endif /* VSOCK */ static struct tracefs_instance * @@ -498,12 +469,8 @@ 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_id, - &clock->remote_id)) - goto error; - clock->instance = clock_synch_create_instance(tsync->clock_str, - clock->remote_id); + tsync->remote_id); if (!clock->instance) goto error; @@ -1071,6 +1038,8 @@ out: * @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 + * @remote_id: Identifier to uniquely identify the remote host + * @local_id: Identifier to uniquely identify the local machine * * On success, a pointer to time sync context is returned, or NULL in * case of an error. The context must be freed with tracecmd_tsync_free() @@ -1080,7 +1049,7 @@ out: */ 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) { struct tracecmd_time_sync *tsync; cpu_set_t *pin_mask = NULL; @@ -1110,6 +1079,9 @@ tracecmd_tsync_with_host(const struct tracecmd_tsync_protos *tsync_protos, if (clock) tsync->clock_str = strdup(clock); + tsync->remote_id = remote_id; + tsync->local_id = local_id; + pthread_attr_init(&attrib); tsync->vcpu_count = tracecmd_count_cpus(); pthread_attr_setdetachstate(&attrib, PTHREAD_CREATE_JOINABLE); diff --git a/tracecmd/trace-agent.c b/tracecmd/trace-agent.c index a46feea3d3c7..151ca19c2270 100644 --- a/tracecmd/trace-agent.c +++ b/tracecmd/trace-agent.c @@ -140,6 +140,35 @@ static char *get_clock(int argc, char **argv) return NULL; } +#ifdef VSOCK +static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) +{ + struct sockaddr_vm addr; + socklen_t addr_len = sizeof(addr); + + memset(&addr, 0, sizeof(addr)); + if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) + return -1; + if (addr.svm_family != AF_VSOCK) + return -1; + *lcid = addr.svm_cid; + + memset(&addr, 0, sizeof(addr)); + addr_len = sizeof(addr); + if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) + return -1; + if (addr.svm_family != AF_VSOCK) + return -1; + *rcid = addr.svm_cid; + + return 0; +} +#else +static inline int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) { + return -1; +} +#endif + static void agent_handle(int sd, int nr_cpus, int page_size) { struct tracecmd_tsync_protos *tsync_protos = NULL; @@ -147,6 +176,8 @@ static void agent_handle(int sd, int nr_cpus, int page_size) struct tracecmd_msg_handle *msg_handle; char *tsync_proto = NULL; unsigned long long trace_id; + unsigned int remote_id; + unsigned int local_id; unsigned int tsync_port = 0; unsigned int *ports; char **argv = NULL; @@ -176,8 +207,16 @@ static void agent_handle(int sd, int nr_cpus, int page_size) if (!use_fifos) make_vsocks(nr_cpus, fds, ports); 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; + } tsync = tracecmd_tsync_with_host(tsync_protos, - get_clock(argc, argv)); + get_clock(argc, argv), + remote_id, local_id); if (tsync) tracecmd_tsync_get_session_params(tsync, &tsync_proto, &tsync_port); else From patchwork Fri Apr 15 01:00:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12814186 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 516FCC43219 for ; Fri, 15 Apr 2022 01:00:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344291AbiDOBDA (ORCPT ); Thu, 14 Apr 2022 21:03:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348334AbiDOBCj (ORCPT ); Thu, 14 Apr 2022 21:02:39 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33E9A2F019 for ; Thu, 14 Apr 2022 18:00:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id BD6DBB82BF6 for ; Fri, 15 Apr 2022 01:00:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D955BC385AD; Fri, 15 Apr 2022 01:00:09 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nfAJk-003w8g-Q0; Thu, 14 Apr 2022 21:00:08 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 5/8] trace-cmd library: Have tracecmd_tsync_with_guest() not depend on cid/port Date: Thu, 14 Apr 2022 21:00:04 -0400 Message-Id: <20220415010007.938408-6-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415010007.938408-1-rostedt@goodmis.org> References: <20220415010007.938408-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- .../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(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 24295e4e09d3..06906b04fbd9 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -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, diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c index b41aece82082..2f812ca5fc74 100644 --- a/lib/trace-cmd/trace-timesync.c +++ b/lib/trace-cmd/trace-timesync.c @@ -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 diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index c3e52a48fa07..56fa5a798dcc 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -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); From patchwork Fri Apr 15 01:00:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12814183 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2895AC4332F for ; Fri, 15 Apr 2022 01:00:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230247AbiDOBC4 (ORCPT ); Thu, 14 Apr 2022 21:02:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348330AbiDOBCj (ORCPT ); Thu, 14 Apr 2022 21:02:39 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC50827FC0 for ; Thu, 14 Apr 2022 18:00:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 57ADDB80DBB for ; Fri, 15 Apr 2022 01:00:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9C24C385AE; Fri, 15 Apr 2022 01:00:09 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nfAJk-003w8j-Qr; Thu, 14 Apr 2022 21:00:08 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 6/8] trace-cmd library: Remove dependency on vsocks from tracecmd_tsync_get_session_params() Date: Thu, 14 Apr 2022 21:00:05 -0400 Message-Id: <20220415010007.938408-7-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415010007.938408-1-rostedt@goodmis.org> References: <20220415010007.938408-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- .../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(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 06906b04fbd9..1839d82d4e11 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -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); diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c index 2f812ca5fc74..6f16090c2eb4 100644 --- a/lib/trace-cmd/trace-timesync.c +++ b/lib/trace-cmd/trace-timesync.c @@ -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; } diff --git a/tracecmd/trace-agent.c b/tracecmd/trace-agent.c index 151ca19c2270..1a9463fd4854 100644 --- a/tracecmd/trace-agent.c +++ b/tracecmd/trace-agent.c @@ -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"); } From patchwork Fri Apr 15 01:00:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12814184 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E02BC433F5 for ; Fri, 15 Apr 2022 01:00:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244781AbiDOBC6 (ORCPT ); Thu, 14 Apr 2022 21:02:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348325AbiDOBCi (ORCPT ); Thu, 14 Apr 2022 21:02:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A20129800 for ; Thu, 14 Apr 2022 18:00:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1EEB7621FA for ; Fri, 15 Apr 2022 01:00:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE02BC385AF; Fri, 15 Apr 2022 01:00:09 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nfAJk-003w8s-Rd; Thu, 14 Apr 2022 21:00:08 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 7/8] trace-cmd library: Remove vsock dependency from tracecmd_tsync_with_host() Date: Thu, 14 Apr 2022 21:00:06 -0400 Message-Id: <20220415010007.938408-8-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415010007.938408-1-rostedt@goodmis.org> References: <20220415010007.938408-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- .../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(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 1839d82d4e11..45ae1dded66d 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -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 * diff --git a/lib/trace-cmd/trace-msg.c b/lib/trace-cmd/trace-msg.c index 03b853e4368b..726e9424c8fd 100644 --- a/lib/trace-cmd/trace-msg.c +++ b/lib/trace-cmd/trace-msg.c @@ -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); diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c index 6f16090c2eb4..823dcf21ba26 100644 --- a/lib/trace-cmd/trace-timesync.c +++ b/lib/trace-cmd/trace-timesync.c @@ -344,61 +344,6 @@ error: return -1; } -#ifdef VSOCK -static int vsock_make(void) -{ - struct sockaddr_vm addr = { - .svm_family = AF_VSOCK, - .svm_cid = VMADDR_CID_ANY, - .svm_port = VMADDR_PORT_ANY, - }; - int sd; - - sd = socket(AF_VSOCK, SOCK_STREAM, 0); - if (sd < 0) - return -errno; - - setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); - - if (bind(sd, (struct sockaddr *)&addr, sizeof(addr))) - return -errno; - - if (listen(sd, SOMAXCONN)) - return -errno; - - return sd; -} - -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 int vsock_make(void) -{ - return -ENOTSUP; - -} - -static int vsock_get_port(int sd, unsigned int *port) -{ - return -ENOTSUP; -} -#endif /* VSOCK */ - static struct tracefs_instance * clock_synch_create_instance(const char *clock, unsigned int cid) { @@ -1005,6 +950,7 @@ out: /** * tracecmd_tsync_with_host - Synchronize timestamps with host + * @fd: File descriptor connecting with the 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 @@ -1018,17 +964,16 @@ out: * until tracecmd_tsync_with_host_stop() is called. */ 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) { struct tracecmd_time_sync *tsync; cpu_set_t *pin_mask = NULL; pthread_attr_t attrib; size_t mask_size = 0; - unsigned int port; const char *proto; int ret; - int fd; tsync = calloc(1, sizeof(struct tracecmd_time_sync)); if (!tsync) @@ -1039,12 +984,6 @@ tracecmd_tsync_with_host(const struct tracecmd_tsync_protos *tsync_protos, if (!proto) goto error; tsync->proto_name = strdup(proto); - fd = vsock_make(); - if (fd < 0) - goto error; - - if (vsock_get_port(fd, &port) < 0) - goto error; tsync->msg_handle = tracecmd_msg_handle_alloc(fd, 0); if (clock) tsync->clock_str = strdup(clock); @@ -1072,10 +1011,11 @@ tracecmd_tsync_with_host(const struct tracecmd_tsync_protos *tsync_protos, error: if (tsync) { - if (tsync->msg_handle) + if (tsync->msg_handle) { + /* Do not close the fd that was passed it */ + tsync->msg_handle->fd = -1; tracecmd_msg_handle_close(tsync->msg_handle); - else if (fd >= 0) - close(fd); + } free(tsync->clock_str); free(tsync); } diff --git a/tracecmd/trace-agent.c b/tracecmd/trace-agent.c index 1a9463fd4854..ff4a0884f619 100644 --- a/tracecmd/trace-agent.c +++ b/tracecmd/trace-agent.c @@ -141,6 +141,31 @@ static char *get_clock(int argc, char **argv) } #ifdef VSOCK + +static int vsock_make(void) +{ + struct sockaddr_vm addr = { + .svm_family = AF_VSOCK, + .svm_cid = VMADDR_CID_ANY, + .svm_port = VMADDR_PORT_ANY, + }; + int sd; + + sd = socket(AF_VSOCK, SOCK_STREAM, 0); + if (sd < 0) + return -errno; + + setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); + + if (bind(sd, (struct sockaddr *)&addr, sizeof(addr))) + return -errno; + + if (listen(sd, SOMAXCONN)) + return -errno; + + return sd; +} + static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) { struct sockaddr_vm addr; @@ -181,7 +206,22 @@ static int vsock_get_port(int sd, unsigned int *port) return 0; } #else -static inline int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) { +static inline bool can_splice_read_vsock(void) +{ + return false; +} + +static inline int vsock_make(void) +{ + return -ENOTSUP; + +} + +static inline int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) { + return -1; +} +static inline int vsock_get_port(int sd, unsigned int *port) +{ return -1; } @@ -207,6 +247,7 @@ static void agent_handle(int sd, int nr_cpus, int page_size) bool use_fifos; int *fds; int ret; + int fd; fds = calloc(nr_cpus, sizeof(*fds)); ports = calloc(nr_cpus, sizeof(*ports)); @@ -236,17 +277,19 @@ static void agent_handle(int sd, int nr_cpus, int page_size) remote_id = -1; local_id = -2; } - if (vsock_get_port(msg_handle->fd, &tsync_port) >= 0) { - tsync = tracecmd_tsync_with_host(tsync_protos, + fd = vsock_make(); + if (fd >= 0 && vsock_get_port(fd, &tsync_port) >= 0) { + tsync = tracecmd_tsync_with_host(fd, tsync_protos, get_clock(argc, argv), remote_id, local_id); - } else { - tsync = NULL; } - if (tsync) + if (tsync) { tracecmd_tsync_get_selected_proto(tsync, &tsync_proto); - else + } else { warning("Failed to negotiate timestamps synchronization with the host"); + if (fd >= 0) + close(fd); + } } trace_id = tracecmd_generate_traceid(); ret = tracecmd_msg_send_trace_resp(msg_handle, nr_cpus, page_size, diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 56fa5a798dcc..8e89aa94977c 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -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); From patchwork Fri Apr 15 01:00:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 12814187 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F41E7C433FE for ; Fri, 15 Apr 2022 01:00:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348285AbiDOBDA (ORCPT ); Thu, 14 Apr 2022 21:03:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348341AbiDOBCl (ORCPT ); Thu, 14 Apr 2022 21:02:41 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB1B5286FE for ; Thu, 14 Apr 2022 18:00:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 20550CE2C14 for ; Fri, 15 Apr 2022 01:00:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F108DC385AC; Fri, 15 Apr 2022 01:00:09 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.95) (envelope-from ) id 1nfAJk-003w8w-SP; Thu, 14 Apr 2022 21:00:08 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 8/8] trace-cmd: Move vsocket code into its own file Date: Thu, 14 Apr 2022 21:00:07 -0400 Message-Id: <20220415010007.938408-9-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415010007.938408-1-rostedt@goodmis.org> References: <20220415010007.938408-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- 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 diff --git a/lib/trace-cmd/include/trace-tsync-local.h b/lib/trace-cmd/include/trace-tsync-local.h index 697c076c43a6..885c9f51d891 100644 --- a/lib/trace-cmd/include/trace-tsync-local.h +++ b/lib/trace-cmd/include/trace-tsync-local.h @@ -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 */ diff --git a/lib/trace-cmd/trace-timesync-ptp.c b/lib/trace-cmd/trace-timesync-ptp.c index 0e23ff83869a..653d176e2e79 100644 --- a/lib/trace-cmd/trace-timesync-ptp.c +++ b/lib/trace-cmd/trace-timesync-ptp.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c index 823dcf21ba26..14cf20c870cb 100644 --- a/lib/trace-cmd/trace-timesync.c +++ b/lib/trace-cmd/trace-timesync.c @@ -8,9 +8,6 @@ #include #include #include -#ifdef VSOCK -#include -#endif #include #include #include @@ -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, diff --git a/tracecmd/Makefile b/tracecmd/Makefile index 355f04723ad7..13f7776e8e45 100644 --- a/tracecmd/Makefile +++ b/tracecmd/Makefile @@ -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 diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h index bb33de06bb58..ac864c70be11 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -9,6 +9,7 @@ #include #include /* for DIR */ #include /* for isdigit() */ +#include #include #include "trace-cmd-private.h" @@ -339,6 +340,54 @@ int trace_open_vsock(unsigned int cid, unsigned int port); char *trace_get_guest_file(const char *file, const char *guest); +#ifdef VSOCK +int trace_vsock_open(unsigned int cid, unsigned int port); +int trace_vsock_make(unsigned int port); +int trace_vsock_make_any(void); +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); +#else +static inline int trace_vsock_open(unsigned int cid, unsigned int port) +{ + return -ENOTSUP; +} + +static inline int trace_vsock_make(unsigned int port) +{ + return -ENOTSUP; + +} + +static inline int trace_vsock_make_any(void) +{ + return -ENOTSUP; + +} + +static inline int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport, + unsigned int *rcid, unsigned int *rport) +{ + return -ENOTSUP; +} + +static inline int trace_vsock_get_port(int sd, unsigned int *port) +{ + return -ENOTSUP; +} + +static inline bool trace_vsock_can_splice_read(void) +{ + return false; +} + +static inline int trace_vsock_local_cid(void) +{ + return -ENOTSUP; +} +#endif /* VSOCK */ + /* No longer in event-utils.h */ __printf(1,2) void __noreturn die(const char *fmt, ...); /* Can be overriden */ diff --git a/tracecmd/trace-agent.c b/tracecmd/trace-agent.c index ff4a0884f619..719d7126ca67 100644 --- a/tracecmd/trace-agent.c +++ b/tracecmd/trace-agent.c @@ -14,85 +14,25 @@ #include #include #include -#include #include #include #include -#include #include #include "trace-local.h" #include "trace-msg.h" -#define GET_LOCAL_CID 0x7b9 - -static int get_local_cid(unsigned int *cid) -{ - int fd, ret = 0; - - fd = open("/dev/vsock", O_RDONLY); - if (fd < 0) - return -errno; - - if (ioctl(fd, GET_LOCAL_CID, cid)) - ret = -errno; - - close(fd); - return ret; -} - -int trace_make_vsock(unsigned int port) -{ - struct sockaddr_vm addr = { - .svm_family = AF_VSOCK, - .svm_cid = VMADDR_CID_ANY, - .svm_port = port, - }; - int sd; - - sd = socket(AF_VSOCK, SOCK_STREAM, 0); - if (sd < 0) - return -errno; - - setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); - - if (bind(sd, (struct sockaddr *)&addr, sizeof(addr))) - return -errno; - - if (listen(sd, SOMAXCONN)) - return -errno; - - return sd; -} - -int trace_get_vsock_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; -} - static void make_vsocks(int nr, int *fds, unsigned int *ports) { unsigned int port; int i, fd, ret; for (i = 0; i < nr; i++) { - fd = trace_make_vsock(VMADDR_PORT_ANY); + fd = trace_vsock_make_any(); if (fd < 0) die("Failed to open vsocket"); - ret = trace_get_vsock_port(fd, &port); + ret = trace_vsock_get_port(fd, &port); if (ret < 0) die("Failed to get vsocket address"); @@ -140,97 +80,6 @@ static char *get_clock(int argc, char **argv) return NULL; } -#ifdef VSOCK - -static int vsock_make(void) -{ - struct sockaddr_vm addr = { - .svm_family = AF_VSOCK, - .svm_cid = VMADDR_CID_ANY, - .svm_port = VMADDR_PORT_ANY, - }; - int sd; - - sd = socket(AF_VSOCK, SOCK_STREAM, 0); - if (sd < 0) - return -errno; - - setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); - - if (bind(sd, (struct sockaddr *)&addr, sizeof(addr))) - return -errno; - - if (listen(sd, SOMAXCONN)) - return -errno; - - return sd; -} - -static int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) -{ - struct sockaddr_vm addr; - socklen_t addr_len = sizeof(addr); - - memset(&addr, 0, sizeof(addr)); - if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) - return -1; - if (addr.svm_family != AF_VSOCK) - return -1; - *lcid = addr.svm_cid; - - memset(&addr, 0, sizeof(addr)); - addr_len = sizeof(addr); - if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) - return -1; - if (addr.svm_family != AF_VSOCK) - return -1; - *rcid = addr.svm_cid; - - 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 bool can_splice_read_vsock(void) -{ - return false; -} - -static inline int vsock_make(void) -{ - return -ENOTSUP; - -} - -static inline int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) { - return -1; -} -static inline int vsock_get_port(int sd, unsigned int *port) -{ - 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) { struct tracecmd_tsync_protos *tsync_protos = NULL; @@ -277,8 +126,8 @@ static void agent_handle(int sd, int nr_cpus, int page_size) remote_id = -1; local_id = -2; } - fd = vsock_make(); - if (fd >= 0 && vsock_get_port(fd, &tsync_port) >= 0) { + fd = trace_vsock_make_any(); + if (fd >= 0 && trace_vsock_get_port(fd, &tsync_port) >= 0) { tsync = tracecmd_tsync_with_host(fd, tsync_protos, get_clock(argc, argv), remote_id, local_id); @@ -355,12 +204,13 @@ static void agent_serve(unsigned int port, bool do_daemon) nr_cpus = tracecmd_count_cpus(); page_size = getpagesize(); - sd = trace_make_vsock(port); + sd = trace_vsock_make(port); if (sd < 0) die("Failed to open vsocket"); tracecmd_tsync_init(); - if (!get_local_cid(&cid)) + cid = trace_vsock_local_cid(); + if (cid >= 0) printf("listening on @%u:%u\n", cid, port); if (do_daemon && daemon(1, 0)) diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 8e89aa94977c..ac6fb7e98892 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -37,9 +37,6 @@ #include #include #include -#ifdef VSOCK -#include -#endif #include "tracefs.h" #include "version.h" @@ -3159,81 +3156,6 @@ static int connect_port(const char *host, unsigned int port) return sfd; } -#ifdef VSOCK -int trace_open_vsock(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 try_splice_read_vsock(void) -{ - int ret, sd, brass[2]; - - sd = socket(AF_VSOCK, SOCK_STREAM, 0); - if (sd < 0) - return -errno; - - ret = pipe(brass); - if (ret < 0) - goto out_close_sd; - - /* - * On kernels that don't support splice reading from vsockets - * 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 -int trace_open_vsock(unsigned int cid, unsigned int port) -{ - die("vsock is not supported"); - return -1; -} -static inline bool can_splice_read_vsock(void) -{ - return false; -} -#endif - static int do_accept(int sd) { int cd; @@ -3366,14 +3288,14 @@ create_recorder_instance(struct buffer_instance *instance, const char *file, int if (instance->use_fifos) fd = instance->fds[cpu]; else - fd = trace_open_vsock(instance->cid, instance->client_ports[cpu]); + fd = trace_vsock_open(instance->cid, instance->client_ports[cpu]); if (fd < 0) die("Failed to connect to agent"); flags = recorder_flags; if (instance->use_fifos) flags |= TRACECMD_RECORD_NOBRASS; - else if (!can_splice_read_vsock()) + else if (!trace_vsock_can_splice_read()) flags |= TRACECMD_RECORD_NOSPLICE; return tracecmd_create_recorder_virt(file, cpu, flags, fd); } @@ -3988,7 +3910,7 @@ static int host_tsync(struct common_record_context *ctx, guest_pid = guest->pid; start_mapping_vcpus(guest); - fd = trace_open_vsock(instance->cid, tsync_port); + fd = trace_vsock_open(instance->cid, tsync_port); instance->tsync = tracecmd_tsync_with_guest(top_instance.trace_id, instance->tsync_loop_interval, fd, guest_pid, @@ -4019,7 +3941,7 @@ static void connect_to_agent(struct common_record_context *ctx, use_fifos = nr_fifos > 0; } - sd = trace_open_vsock(instance->cid, instance->port); + sd = trace_vsock_open(instance->cid, instance->port); if (sd < 0) die("Failed to connect to vsocket @%u:%u", instance->cid, instance->port); diff --git a/tracecmd/trace-vm.c b/tracecmd/trace-vm.c index 02979ba59a57..57dbef8d42e4 100644 --- a/tracecmd/trace-vm.c +++ b/tracecmd/trace-vm.c @@ -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) diff --git a/tracecmd/trace-vsock.c b/tracecmd/trace-vsock.c new file mode 100644 index 000000000000..d18ecb45004e --- /dev/null +++ b/tracecmd/trace-vsock.c @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include + +#include "trace-cmd-private.h" + +int __hidden trace_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; +} + +int __hidden trace_vsock_make(unsigned int port) +{ + struct sockaddr_vm addr = { + .svm_family = AF_VSOCK, + .svm_cid = VMADDR_CID_ANY, + .svm_port = port, + }; + int sd; + + sd = socket(AF_VSOCK, SOCK_STREAM, 0); + if (sd < 0) + return -errno; + + setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); + + if (bind(sd, (struct sockaddr *)&addr, sizeof(addr))) + return -errno; + + if (listen(sd, SOMAXCONN)) + return -errno; + + return sd; +} + +int __hidden trace_vsock_make_any(void) +{ + return trace_vsock_make(VMADDR_PORT_ANY); +} + +int __hidden trace_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; +} + +int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid) +{ + struct sockaddr_vm addr; + socklen_t addr_len = sizeof(addr); + + memset(&addr, 0, sizeof(addr)); + if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) + return -1; + if (addr.svm_family != AF_VSOCK) + return -1; + *lcid = addr.svm_cid; + + memset(&addr, 0, sizeof(addr)); + addr_len = sizeof(addr); + if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) + return -1; + if (addr.svm_family != AF_VSOCK) + return -1; + *rcid = addr.svm_cid; + + return 0; +} + +static int try_splice_read_vsock(void) +{ + int ret, sd, brass[2]; + + sd = socket(AF_VSOCK, SOCK_STREAM, 0); + if (sd < 0) + return -errno; + + ret = pipe(brass); + if (ret < 0) + goto out_close_sd; + + /* + * On kernels that don't support splice reading from vsockets + * 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; +} + +bool __hidden trace_vsock_can_splice_read(void) +{ + static bool initialized, res; + + if (initialized) + return res; + + res = try_splice_read_vsock() > 0; + initialized = true; + return res; +} + +#define GET_LOCAL_CID 0x7b9 + +int __hidden trace_vsock_local_cid(void) +{ + int cid; + int fd; + + fd = open("/dev/vsock", O_RDONLY); + if (fd < 0) + return -errno; + + if (ioctl(fd, GET_LOCAL_CID, &cid)) + cid = -errno; + + close(fd); + return cid; +}