From patchwork Tue Sep 17 13:15:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Slavomir Kaslev X-Patchwork-Id: 11148865 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7DA2A14F7 for ; Tue, 17 Sep 2019 13:15:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 667BE2171F for ; Tue, 17 Sep 2019 13:15:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727715AbfIQNPz (ORCPT ); Tue, 17 Sep 2019 09:15:55 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:39615 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727666AbfIQNPz (ORCPT ); Tue, 17 Sep 2019 09:15:55 -0400 Received: by mail-wm1-f66.google.com with SMTP id v17so3122190wml.4 for ; Tue, 17 Sep 2019 06:15:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eqL04MSk2EoGhJJyXr4kBNPm0I8sWENLjVypDjbX9MI=; b=MRH8nGqoSwZ9V10AkD1cAvafo2o7WA3GgmjR652O0np+IHg5ROJeIBw6zv31TikS// wFHMMvt4Kdmo0oe86j6X7vHlznpHPYQYxTDYTSYbdbFbMDdWYCluWzTn9FrKgaDiPw4n FLleeliz5bDXHMGWCSXh0YU5gThsa3l+pEnuWWMhJsWCvkr9FotnXVB/SaNHMuieeIM1 BpWgBWl1LmBbG/qm2Sv46YtJTXV2Jb7SZLvqsdOco/F+8ByMQX4qxMb4EKKcu5ALasvQ V9OVTgI5PHiofOUtRS4AGj998gBgq/GDlTGXtdLcDHrs3NH/4k7ZslRQLeLA0eiswTwU 94BA== X-Gm-Message-State: APjAAAWe+WSYykalofRzp5+lLEm0vd6QGP1vpI0YkJyoExvEpsCnk+GX eS+jreenepVemLbRWoWD6dCP3uP36w== X-Google-Smtp-Source: APXvYqyzzscUaaW6CkwK5uJrayuYQoP8iGlPk/7Nd7/387DmrbgdzaQ869v0/ot8E+7ACc+d+T/oaQ== X-Received: by 2002:a1c:a8d8:: with SMTP id r207mr3470736wme.135.1568726153178; Tue, 17 Sep 2019 06:15:53 -0700 (PDT) Received: from box.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id d22sm1667265wmb.44.2019.09.17.06.15.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2019 06:15:52 -0700 (PDT) From: Slavomir Kaslev To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v14 07/13] trace-cmd: Use splice(2) for vsockets if available Date: Tue, 17 Sep 2019 16:15:32 +0300 Message-Id: <20190917131538.31926-8-kaslevs@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190917131538.31926-1-kaslevs@vmware.com> References: <20190917131538.31926-1-kaslevs@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Detect if splice(2) reading is supported for vsockets (Linux 4.20 and later) and use it, or fallback to read/write-ing otherwise. Signed-off-by: Slavomir Kaslev --- tracecmd/trace-record.c | 56 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index c4f940e..94d01c5 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -2718,12 +2718,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 -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 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) @@ -2927,13 +2976,16 @@ create_recorder_instance(struct buffer_instance *instance, const char *file, int if (is_guest(instance)) { int fd; + unsigned int flags; fd = open_vsock(instance->cid, instance->client_ports[cpu]); if (fd < 0) die("Failed to connect to agent"); - return tracecmd_create_recorder_virt( - file, cpu, recorder_flags | TRACECMD_RECORD_NOSPLICE, fd); + flags = recorder_flags; + if (!can_splice_read_vsock()) + flags |= TRACECMD_RECORD_NOSPLICE; + return tracecmd_create_recorder_virt(file, cpu, flags, fd); } if (brass)