From patchwork Tue Jun 18 14:39:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Slavomir Kaslev X-Patchwork-Id: 11001875 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BFB2A14BB for ; Tue, 18 Jun 2019 14:40:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AE9F828470 for ; Tue, 18 Jun 2019 14:40:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A2C7628825; Tue, 18 Jun 2019 14:40:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D117428470 for ; Tue, 18 Jun 2019 14:40:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729220AbfFROkO (ORCPT ); Tue, 18 Jun 2019 10:40:14 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:55532 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725919AbfFROkO (ORCPT ); Tue, 18 Jun 2019 10:40:14 -0400 Received: by mail-wm1-f67.google.com with SMTP id a15so3581503wmj.5 for ; Tue, 18 Jun 2019 07:40:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zMoolKfl078lBlX2PU/tiitUWSDsEGlNd9hgMjhjn1U=; b=J2ge9WNU9fjW94NtTpyj5sH/Lo41zwemHP69GtN9aIOkBxuZvX28j8HgCciEGhuoph EoK/yLklKIa0qasQtXSsh3u2AL+3yEPPVpaebIeDyLYBGHanIH69iS7MLXTe5GLWHcGJ n/bOvi03c+Pm3Kz0fT4fEkxD51NIgJDMXO//YRPOugVmanj60cI5o+iXt3x+RtchzB1R iqbAU2RfrNS9T1mzu4rSiCYPSzOtnho93ZqjgO31U/+EeQAR14HYMe2Xp+qgzORJcCez NOIKjoVuDLUUugR++LtC0v4b3xHav/RO4wjnE1hMlxIXNMGc9Gc+0aJtN6oCXLBNlbIA SR4w== 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=zMoolKfl078lBlX2PU/tiitUWSDsEGlNd9hgMjhjn1U=; b=W6LwetgNpaPi2PbXWW61XxlzmEkMT3a9gbAS8hzjRDJY6wzTmYnReRpjmDiPFN9qE9 uZ7WOFvqGzeZleVhAbtQL356ElTj6bwk2yPpgnJO7exi+Pue0rsYq5VfUdULox8xMBZo InI7cTUySHuCLpolRnSnDye660pMj3KSEsK/qDsY1cZpO8GDcgiOtA+uqW0M7TAFSX6F +oezJCmmcWGMYSa1bO39Nq27rNJjCmYyRqiNBci3rcBwT05d6NVLycvhC2gAz5k0uRjM /ua7Xas0Y+yQAeJO6kYoOrSUOmmi6GsNBLfTrhg5phLWhOXc+xKRhEtLsdIyQtg+jAT3 VkbQ== X-Gm-Message-State: APjAAAX4IZE3ueWG++BOaSVcArxRMzEwfl8fABolyiDO3r9qxbbmiyVh ANVxRbseni0ieJK6Gv5Sm1o65eE= X-Google-Smtp-Source: APXvYqzd9DpGdpZS/ySWfwHYmqBqmYRMm7TCsBj6my2HuMbXvFcW4+M86Yi8gYNiq1w8rCQXNZ40lg== X-Received: by 2002:a1c:df46:: with SMTP id w67mr3847035wmg.69.1560868810950; Tue, 18 Jun 2019 07:40:10 -0700 (PDT) Received: from box.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id y1sm2411808wma.32.2019.06.18.07.40.10 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 18 Jun 2019 07:40:10 -0700 (PDT) From: "Slavomir Kaslev (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v11 04/13] trace-cmd: Add VM tracing protocol messages Date: Tue, 18 Jun 2019 17:39:49 +0300 Message-Id: <20190618143958.2865-5-slavomir.kaslev@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190618143958.2865-1-slavomir.kaslev@gmail.com> References: <20190618143958.2865-1-slavomir.kaslev@gmail.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 X-Virus-Scanned: ClamAV using ClamSMTP From: Slavomir Kaslev Add TRACE_REQ and TRACE_RESP messages used for initiating guest VM tracing and CLOSE_RESP message used for acknowledging stop recording request. Signed-off-by: Slavomir Kaslev Signed-off-by: Slavomir Kaslev (VMware) --- include/trace-cmd/trace-cmd.h | 14 ++ tracecmd/trace-msg.c | 234 +++++++++++++++++++++++++++++++++- 2 files changed, 245 insertions(+), 3 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index 28b5b38..db5baa8 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -324,7 +324,9 @@ int tracecmd_msg_data_send(struct tracecmd_msg_handle *msg_handle, const char *buf, int size); int tracecmd_msg_finish_sending_data(struct tracecmd_msg_handle *msg_handle); int tracecmd_msg_send_close_msg(struct tracecmd_msg_handle *msg_handle); +int tracecmd_msg_send_close_resp_msg(struct tracecmd_msg_handle *msg_handle); int tracecmd_msg_wait_close(struct tracecmd_msg_handle *msg_handle); +int tracecmd_msg_wait_close_resp(struct tracecmd_msg_handle *msg_handle); /* for server */ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle); @@ -335,6 +337,18 @@ int tracecmd_msg_collect_data(struct tracecmd_msg_handle *msg_handle, int ofd); bool tracecmd_msg_done(struct tracecmd_msg_handle *msg_handle); void tracecmd_msg_set_done(struct tracecmd_msg_handle *msg_handle); +int tracecmd_msg_send_trace_req(struct tracecmd_msg_handle *msg_handle, + int argc, char **argv); +int tracecmd_msg_recv_trace_req(struct tracecmd_msg_handle *msg_handle, + int *argc, char ***argv); + +int tracecmd_msg_send_trace_resp(struct tracecmd_msg_handle *msg_handle, + int nr_cpus, int page_size, + unsigned int *ports); +int tracecmd_msg_recv_trace_resp(struct tracecmd_msg_handle *msg_handle, + int *nr_cpus, int *page_size, + unsigned int **ports); + /* --- Plugin handling --- */ extern struct tep_plugin_option trace_ftrace_options[]; diff --git a/tracecmd/trace-msg.c b/tracecmd/trace-msg.c index 74c0bc1..69c7c5f 100644 --- a/tracecmd/trace-msg.c +++ b/tracecmd/trace-msg.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,17 @@ struct tracecmd_msg_rinit { be32 cpus; } __attribute__((packed)); +struct tracecmd_msg_trace_req { + be32 flags; + be32 argc; +} __attribute__((packed)); + +struct tracecmd_msg_trace_resp { + be32 flags; + be32 cpus; + be32 page_size; +} __attribute__((packed)); + struct tracecmd_msg_header { be32 size; be32 cmd; @@ -71,7 +83,10 @@ struct tracecmd_msg_header { C(RINIT, 2, sizeof(struct tracecmd_msg_rinit)), \ C(SEND_DATA, 3, 0), \ C(FIN_DATA, 4, 0), \ - C(NOT_SUPP, 5, 0), + C(NOT_SUPP, 5, 0), \ + C(TRACE_REQ, 6, sizeof(struct tracecmd_msg_trace_req)), \ + C(TRACE_RESP, 7, sizeof(struct tracecmd_msg_trace_resp)),\ + C(CLOSE_RESP, 8, 0), #undef C #define C(a,b,c) MSG_##a = b @@ -103,6 +118,8 @@ struct tracecmd_msg { union { struct tracecmd_msg_tinit tinit; struct tracecmd_msg_rinit rinit; + struct tracecmd_msg_trace_req trace_req; + struct tracecmd_msg_trace_resp trace_resp; }; char *buf; } __attribute__((packed)); @@ -625,6 +642,14 @@ int tracecmd_msg_send_close_msg(struct tracecmd_msg_handle *msg_handle) return tracecmd_msg_send(msg_handle->fd, &msg); } +int tracecmd_msg_send_close_resp_msg(struct tracecmd_msg_handle *msg_handle) +{ + struct tracecmd_msg msg; + + tracecmd_msg_init(MSG_CLOSE_RESP, &msg); + return tracecmd_msg_send(msg_handle->fd, &msg); +} + int tracecmd_msg_data_send(struct tracecmd_msg_handle *msg_handle, const char *buf, int size) { @@ -745,7 +770,7 @@ int tracecmd_msg_collect_data(struct tracecmd_msg_handle *msg_handle, int ofd) return tracecmd_msg_wait_close(msg_handle); } -int tracecmd_msg_wait_close(struct tracecmd_msg_handle *msg_handle) +static int tracecmd_msg_wait_for_cmd(struct tracecmd_msg_handle *msg_handle, enum tracecmd_msg_cmd cmd) { struct tracecmd_msg msg; int ret = -1; @@ -756,7 +781,7 @@ int tracecmd_msg_wait_close(struct tracecmd_msg_handle *msg_handle) if (ret < 0) goto error; - if (ntohl(msg.hdr.cmd) == MSG_CLOSE) + if (ntohl(msg.hdr.cmd) == cmd) return 0; error_operation(&msg); @@ -771,3 +796,206 @@ error: msg_free(&msg); return ret; } + +int tracecmd_msg_wait_close(struct tracecmd_msg_handle *msg_handle) +{ + return tracecmd_msg_wait_for_cmd(msg_handle, MSG_CLOSE); +} + +int tracecmd_msg_wait_close_resp(struct tracecmd_msg_handle *msg_handle) +{ + return tracecmd_msg_wait_for_cmd(msg_handle, MSG_CLOSE_RESP); +} + +static int make_trace_req(struct tracecmd_msg *msg, int argc, char **argv) +{ + size_t args_size = 0; + char *p; + int i; + + for (i = 0; i < argc; i++) + args_size += strlen(argv[i]) + 1; + + msg->hdr.size = htonl(ntohl(msg->hdr.size) + args_size); + msg->trace_req.argc = htonl(argc); + msg->buf = calloc(args_size, 1); + if (!msg->buf) + return -ENOMEM; + + p = msg->buf; + for (i = 0; i < argc; i++) + p = stpcpy(p, argv[i]) + 1; + + return 0; +} + +int tracecmd_msg_send_trace_req(struct tracecmd_msg_handle *msg_handle, + int argc, char **argv) +{ + struct tracecmd_msg msg; + int ret; + + tracecmd_msg_init(MSG_TRACE_REQ, &msg); + ret = make_trace_req(&msg, argc, argv); + if (ret < 0) + return ret; + + return tracecmd_msg_send(msg_handle->fd, &msg); +} + + /* + * NOTE: On success, the returned `argv` should be freed with: + * free(argv[0]); + * free(argv); + */ +int tracecmd_msg_recv_trace_req(struct tracecmd_msg_handle *msg_handle, + int *argc, char ***argv) +{ + struct tracecmd_msg msg; + char *p, *buf_end, **args; + int i, ret, nr_args; + ssize_t buf_len; + + ret = tracecmd_msg_recv(msg_handle->fd, &msg); + if (ret < 0) + return ret; + + if (ntohl(msg.hdr.cmd) != MSG_TRACE_REQ) { + ret = -ENOTSUP; + goto out; + } + + nr_args = ntohl(msg.trace_req.argc); + if (nr_args <= 0) { + ret = -EINVAL; + goto out; + } + + buf_len = ntohl(msg.hdr.size) - MSG_HDR_LEN - ntohl(msg.hdr.cmd_size); + buf_end = (char *)msg.buf + buf_len; + if (buf_len <= 0 && ((char *)msg.buf)[buf_len-1] != '\0') { + ret = -EINVAL; + goto out; + } + + args = calloc(nr_args, sizeof(*args)); + if (!args) { + ret = -ENOMEM; + goto out; + } + + for (i = 0, p = msg.buf; i < nr_args; i++, p++) { + if (p >= buf_end) { + ret = -EINVAL; + goto out_args; + } + args[i] = p; + p = strchr(p, '\0'); + } + + *argc = nr_args; + *argv = args; + + /* + * On success we're passing msg.buf to the caller through argv[0] so we + * reset it here before calling msg_free(). + */ + msg.buf = NULL; + msg_free(&msg); + return 0; + +out_args: + free(args); +out: + error_operation(&msg); + if (ret == -EOPNOTSUPP) + handle_unexpected_msg(msg_handle, &msg); + msg_free(&msg); + return ret; +} + +static int make_trace_resp(struct tracecmd_msg *msg, + int page_size, int nr_cpus, unsigned int *ports) +{ + int data_size; + + data_size = write_uints(NULL, 0, ports, nr_cpus); + msg->buf = malloc(data_size); + if (!msg->buf) + return -ENOMEM; + write_uints(msg->buf, data_size, ports, nr_cpus); + + msg->hdr.size = htonl(ntohl(msg->hdr.size) + data_size); + msg->trace_resp.cpus = htonl(nr_cpus); + msg->trace_resp.page_size = htonl(page_size); + + return 0; +} + +int tracecmd_msg_send_trace_resp(struct tracecmd_msg_handle *msg_handle, + int nr_cpus, int page_size, + unsigned int *ports) +{ + struct tracecmd_msg msg; + int ret; + + tracecmd_msg_init(MSG_TRACE_RESP, &msg); + ret = make_trace_resp(&msg, page_size, nr_cpus, ports); + if (ret < 0) + return ret; + + return tracecmd_msg_send(msg_handle->fd, &msg); +} + +int tracecmd_msg_recv_trace_resp(struct tracecmd_msg_handle *msg_handle, + int *nr_cpus, int *page_size, + unsigned int **ports) +{ + struct tracecmd_msg msg; + char *p, *buf_end; + ssize_t buf_len; + int i, ret; + + ret = tracecmd_msg_recv(msg_handle->fd, &msg); + if (ret < 0) + return ret; + + if (ntohl(msg.hdr.cmd) != MSG_TRACE_RESP) { + ret = -ENOTSUP; + goto out; + } + + buf_len = msg_buf_len(&msg); + if (buf_len <= 0) { + ret = -EINVAL; + goto out; + } + + *nr_cpus = ntohl(msg.trace_resp.cpus); + *page_size = ntohl(msg.trace_resp.page_size); + *ports = calloc(*nr_cpus, sizeof(**ports)); + if (!*ports) { + ret = -ENOMEM; + goto out; + } + + buf_end = msg.buf + buf_len; + for (i = 0, p = msg.buf; i < *nr_cpus; i++, p++) { + if (p >= buf_end || tatou(p, &(*ports)[i])) { + free(*ports); + ret = -EINVAL; + goto out; + } + p = strchr(p, '\0'); + } + + msg_free(&msg); + return 0; + +out: + error_operation(&msg); + if (ret == -EOPNOTSUPP) + handle_unexpected_msg(msg_handle, &msg); + msg_free(&msg); + return ret; +}