From patchwork Wed Apr 20 02:26:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: zhenwei pi X-Patchwork-Id: 12819624 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 699EAC433F5 for ; Wed, 20 Apr 2022 02:32:23 +0000 (UTC) Received: from localhost ([::1]:52530 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nh08k-0000y9-EA for qemu-devel@archiver.kernel.org; Tue, 19 Apr 2022 22:32:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34934) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nh06T-0007J1-FP for qemu-devel@nongnu.org; Tue, 19 Apr 2022 22:30:01 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]:54975) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nh06R-0000bR-Vb for qemu-devel@nongnu.org; Tue, 19 Apr 2022 22:30:01 -0400 Received: by mail-pj1-x1031.google.com with SMTP id md4so571589pjb.4 for ; Tue, 19 Apr 2022 19:29:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IZq204ud6fBz5lkG0Ev/lsOEGRkv42FQMj7Tl2ZsDWY=; b=Q6D4nhcq+rSi6LRl93ZU/0QdQGInTEdPHKn2tr1Ex4+2BaZIoDdcpqnELMqP1wzmo7 Z9h1LQyCBy+p8eJq6jq7E5fYbtdz1RMJnQj2xOMvVmsv6EwIIeMARUJcG8Zw3Ez/wd4f VmefRHhd1rzLGvtaITIM8AYGm8j56vIOCobm14YOSY1zdzya1nab67rlcIbTvFFCMLRu wpdFbB49qMFHdwe+uHZBn4//iwK0RtVArfoo9at6PoVBhHyx13GoQuGWND237iOuZwQ1 MuWWw+INIbtpitAQF1ut3tOTur/rpoqECLbHjIGWniThc18+yMUgvhXLEjPByQFZqCVA 3UEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IZq204ud6fBz5lkG0Ev/lsOEGRkv42FQMj7Tl2ZsDWY=; b=1XeDFijGcq5uAn2RIv9NaNbK3M6GNjdjIPOl2gGUTf+RrWDJCnBLjsDae8tiGIeEck EqLt4T9OFMWq9/jqF201ZyKs+qfe7UHhsQwS++4tXS7y5wiZQDCOcy8pxF6zBlJM8CNU GVnnU3IbXAIiYOP5N8a4BnEDpkAGdRyhlAyy0bsB9CbXrtuvNKpKZ1KPhDDitpG6Ix3W aY2BFXCwGnWTzfbkAZMG3kpYewIF+M4IWJ8b9LWLqW2ehcwTgY5R82dQMTmc7vWbGewq 0EnheeBXLq2r1LgyINM2RyGzthL4dBJ/3CF4K0c+ZT+GKQvjUcMhvTYmdpkn2E25+VUd mRVw== X-Gm-Message-State: AOAM533DXrbu0IHC1TFxaLIm2Rb0rxpxBDXHJs6yfK9qCD4RooM9dBWS BWUeCrcOExWrRDmvw6J8WZrr5zTdJrjhBg== X-Google-Smtp-Source: ABdhPJyldmYL59CuYo+b9BHexAap2m14JjBvatpFhz7St7EfqsxUE++Tc/CsjMKPUXrNWMNqiEfbXw== X-Received: by 2002:a17:90a:d0c5:b0:1c9:ec78:18e5 with SMTP id y5-20020a17090ad0c500b001c9ec7818e5mr1807066pjw.53.1650421798717; Tue, 19 Apr 2022 19:29:58 -0700 (PDT) Received: from always-x1.bytedance.net ([61.120.150.70]) by smtp.gmail.com with ESMTPSA id c64-20020a624e43000000b005081ec7d679sm17629625pfb.1.2022.04.19.19.29.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Apr 2022 19:29:58 -0700 (PDT) From: zhenwei pi To: michael.roth@amd.com, armbru@redhat.com Subject: [PATCH v2 1/2] qga: Introduce NVMe disk bus type Date: Wed, 20 Apr 2022 10:26:09 +0800 Message-Id: <20220420022610.418052-2-pizhenwei@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220420022610.418052-1-pizhenwei@bytedance.com> References: <20220420022610.418052-1-pizhenwei@bytedance.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=pizhenwei@bytedance.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marcandre.lureau@redhat.com, qemu-devel@nongnu.org, zhenwei pi , f4bug@amsat.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Assigning a NVMe disk by VFIO or emulating a NVMe controller by QEMU, a NVMe disk get exposed in guest side. Support NVMe disk bus type and implement posix version. Test PCI passthrough case: ~#virsh qemu-agent-command buster '{"execute":"guest-get-disks"}' | jq ... { "name": "/dev/nvme0n1", "dependencies": [], "partition": false, "address": { "serial": "SAMSUNG MZQL23T8HCLS-00A07_S64HNE0N500076", "bus-type": "nvme", "bus": 0, "unit": 0, "pci-controller": { "bus": 0, "slot": 22, "domain": 0, "function": 0 }, "dev": "/dev/nvme0n1", "target": 0 } ... Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: zhenwei pi --- qga/commands-posix.c | 5 ++++- qga/qapi-schema.json | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 75dbaab68e..4ec83bbfbc 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -889,7 +889,8 @@ static bool build_guest_fsinfo_for_pci_dev(char const *syspath, if (driver && (g_str_equal(driver, "ata_piix") || g_str_equal(driver, "sym53c8xx") || g_str_equal(driver, "virtio-pci") || - g_str_equal(driver, "ahci"))) { + g_str_equal(driver, "ahci") || + g_str_equal(driver, "nvme"))) { break; } @@ -984,6 +985,8 @@ static bool build_guest_fsinfo_for_pci_dev(char const *syspath, g_debug("no host for '%s' (driver '%s')", syspath, driver); goto cleanup; } + } else if (strcmp(driver, "nvme") == 0) { + disk->bus_type = GUEST_DISK_BUS_TYPE_NVME; } else { g_debug("unknown driver '%s' (sysfs path '%s')", driver, syspath); goto cleanup; diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index 94e4aacdcc..5ea76cfd1d 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -827,13 +827,14 @@ # @mmc: Win multimedia card (MMC) bus type # @virtual: Win virtual bus type # @file-backed-virtual: Win file-backed bus type +# @nvme: NVMe disks (since 7.1) # # Since: 2.2; 'Unknown' and all entries below since 2.4 ## { 'enum': 'GuestDiskBusType', 'data': [ 'ide', 'fdc', 'scsi', 'virtio', 'xen', 'usb', 'uml', 'sata', 'sd', 'unknown', 'ieee1394', 'ssa', 'fibre', 'raid', 'iscsi', - 'sas', 'mmc', 'virtual', 'file-backed-virtual' ] } + 'sas', 'mmc', 'virtual', 'file-backed-virtual', 'nvme' ] } ## From patchwork Wed Apr 20 02:26:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: zhenwei pi X-Patchwork-Id: 12819625 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7F637C433EF for ; Wed, 20 Apr 2022 02:32:54 +0000 (UTC) Received: from localhost ([::1]:53026 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nh09F-0001Ir-B9 for qemu-devel@archiver.kernel.org; Tue, 19 Apr 2022 22:32:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34996) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nh06c-0007U6-Vs for qemu-devel@nongnu.org; Tue, 19 Apr 2022 22:30:11 -0400 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]:33306) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nh06Y-0000oh-WB for qemu-devel@nongnu.org; Tue, 19 Apr 2022 22:30:08 -0400 Received: by mail-pf1-x433.google.com with SMTP id n22so646160pfa.0 for ; Tue, 19 Apr 2022 19:30:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jUctx0Qvv4r3a5AvkhZ9HX88SyBEmkboL1xu34kADiE=; b=XbDCXMiW16tRkA3jW8NtQo26OqmJh9nE3i5UvFjFSn28E+ROOAgs4zU1bIzBeLT59e TV4EdP+9p+kwgE6daPxBGhutfxJNTXMT8ZftNSxBA4kQuVdZAQ/krdkbvFCwTjxk2SGG S8vp9tRItxUQ8hHwoEqUr0XvzxomSMtl2wABPddGJ9LCc+0p+UM84VFg2z1aE/vMn8Q1 6eTIRS/WuXYFHSabCYN/VQDf8mFR5UaTR5bJpVLyR7iUROUEze1KEd81TxJZoiD8+2Xv xQrA08NUfcRbV5axUJycviDfA3TXbKYg1ozDU+QcXKKfvgL71MmK3DQSlLJCT/zO9iTI gJWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jUctx0Qvv4r3a5AvkhZ9HX88SyBEmkboL1xu34kADiE=; b=5YXFzxk7OyRN3yVFJeHxugj4/FCaKRMRi3cV43Dm/vIOZnSkSGvxyKe+lydljp14nJ 2C2z5g14kDRiwMh/G6+macgIiHNTV25+IyrH2VWH7fOnEWNPvZyTVXzRIjzgFIasMT0a sJtDew/0DzGYeKY21fe4hZ9fQPHLBnAhjCeeTt+pOPHZW9xHToX7CIc4aI79TwQZOXeE A6BOR2TtERgNpD++pRaJGnxGBxl5GLU0NHLbXL/5ZCIOZI4lDxwFyeoiV5tJ+iu1aXSW 5LmqNYvQJt+LTsDPAKa8wUpNomFXjoM+fvKdnZLSrjl9CP0eMsKzXd952PuDViPXn/1e pFdw== X-Gm-Message-State: AOAM530XVS6rz8UNWgvAeM3O7ZPHuMkMllgnnF4YSorHEbjlci1pcZBC ywCnu4MhJOmyHmagjazF5O5p7g== X-Google-Smtp-Source: ABdhPJxkfbhUolBi25YeZxBsBDYdI3AeaU9qdfc09l6vl/jXCllcspylqiD3m48fyr5BDOsjwtlzow== X-Received: by 2002:a05:6a00:338a:b0:50a:b6b4:8a21 with SMTP id cm10-20020a056a00338a00b0050ab6b48a21mr3545216pfb.66.1650421805436; Tue, 19 Apr 2022 19:30:05 -0700 (PDT) Received: from always-x1.bytedance.net ([61.120.150.70]) by smtp.gmail.com with ESMTPSA id c64-20020a624e43000000b005081ec7d679sm17629625pfb.1.2022.04.19.19.29.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Apr 2022 19:30:04 -0700 (PDT) From: zhenwei pi To: michael.roth@amd.com, armbru@redhat.com Subject: [PATCH v2 2/2] qga: Introduce disk smart Date: Wed, 20 Apr 2022 10:26:10 +0800 Message-Id: <20220420022610.418052-3-pizhenwei@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220420022610.418052-1-pizhenwei@bytedance.com> References: <20220420022610.418052-1-pizhenwei@bytedance.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::433; envelope-from=pizhenwei@bytedance.com; helo=mail-pf1-x433.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Keith Busch , marcandre.lureau@redhat.com, qemu-devel@nongnu.org, zhenwei pi , f4bug@amsat.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" After assigning a NVMe/SCSI controller to guest by VFIO, we lose everything on the host side. A guest uses these devices exclusively, we usually don't care the actions on these devices. But there is a low probability that hitting physical hardware warning, we need a chance to get the basic smart log info. Introduce disk smart, and implement NVMe smart on linux. Thanks to Keith and Marc-André. CC: Keith Busch Reviewed-by: Marc-André Lureau Signed-off-by: zhenwei pi --- qga/commands-posix.c | 73 ++++++++++++++++++++++++++++++++++++++++++++ qga/qapi-schema.json | 53 +++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 4ec83bbfbc..383bbebe42 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -27,6 +27,7 @@ #include "qemu/base64.h" #include "qemu/cutils.h" #include "commands-common.h" +#include "block/nvme.h" #ifdef HAVE_UTMPX #include @@ -49,6 +50,7 @@ extern char **environ; #include #include #include +#include #ifdef CONFIG_LIBUDEV #include @@ -1390,6 +1392,76 @@ static GuestDiskInfoList *get_disk_partitions( return ret; } +static void get_nvme_smart(GuestDiskInfo *disk) +{ + int fd; + GuestNVMeSmart *smart; + NvmeSmartLog log = {0}; + struct nvme_admin_cmd cmd = { + .opcode = NVME_ADM_CMD_GET_LOG_PAGE, + .nsid = NVME_NSID_BROADCAST, + .addr = (uint64_t)&log, + .data_len = sizeof(log), + .cdw10 = NVME_LOG_SMART_INFO | (1 << 15) /* RAE bit */ + | (((sizeof(log) >> 2) - 1) << 16) + }; + + fd = qemu_open_old(disk->name, O_RDONLY); + if (fd == -1) { + g_debug("Failed to open device: %s: %s", disk->name, g_strerror(errno)); + return; + } + + if (ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd)) { + g_debug("Failed to get smart: %s: %s", disk->name, g_strerror(errno)); + close(fd); + return; + } + + disk->has_smart = true; + disk->smart = g_new0(GuestDiskSmart, 1); + disk->smart->type = GUEST_DISK_BUS_TYPE_NVME; + + smart = &disk->smart->u.nvme; + smart->critical_warning = log.critical_warning; + smart->temperature = lduw_le_p(&log.temperature); /* unaligned field */ + smart->available_spare = log.available_spare; + smart->available_spare_threshold = log.available_spare_threshold; + smart->percentage_used = log.percentage_used; + smart->data_units_read_lo = le64_to_cpu(log.data_units_read[0]); + smart->data_units_read_hi = le64_to_cpu(log.data_units_read[1]); + smart->data_units_written_lo = le64_to_cpu(log.data_units_written[0]); + smart->data_units_written_hi = le64_to_cpu(log.data_units_written[1]); + smart->host_read_commands_lo = le64_to_cpu(log.host_read_commands[0]); + smart->host_read_commands_hi = le64_to_cpu(log.host_read_commands[1]); + smart->host_write_commands_lo = le64_to_cpu(log.host_write_commands[0]); + smart->host_write_commands_hi = le64_to_cpu(log.host_write_commands[1]); + smart->controller_busy_time_lo = le64_to_cpu(log.controller_busy_time[0]); + smart->controller_busy_time_hi = le64_to_cpu(log.controller_busy_time[1]); + smart->power_cycles_lo = le64_to_cpu(log.power_cycles[0]); + smart->power_cycles_hi = le64_to_cpu(log.power_cycles[1]); + smart->power_on_hours_lo = le64_to_cpu(log.power_on_hours[0]); + smart->power_on_hours_hi = le64_to_cpu(log.power_on_hours[1]); + smart->unsafe_shutdowns_lo = le64_to_cpu(log.unsafe_shutdowns[0]); + smart->unsafe_shutdowns_hi = le64_to_cpu(log.unsafe_shutdowns[1]); + smart->media_errors_lo = le64_to_cpu(log.media_errors[0]); + smart->media_errors_hi = le64_to_cpu(log.media_errors[1]); + smart->number_of_error_log_entries_lo = + le64_to_cpu(log.number_of_error_log_entries[0]); + smart->number_of_error_log_entries_hi = + le64_to_cpu(log.number_of_error_log_entries[1]); + + close(fd); +} + +static void get_disk_smart(GuestDiskInfo *disk) +{ + if (disk->has_address + && (disk->address->bus_type == GUEST_DISK_BUS_TYPE_NVME)) { + get_nvme_smart(disk); + } +} + GuestDiskInfoList *qmp_guest_get_disks(Error **errp) { GuestDiskInfoList *ret = NULL; @@ -1463,6 +1535,7 @@ GuestDiskInfoList *qmp_guest_get_disks(Error **errp) } get_disk_deps(disk_dir, disk); + get_disk_smart(disk); ret = get_disk_partitions(ret, de->d_name, disk_dir, dev_name); } diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index 5ea76cfd1d..4d8e506c9e 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -888,6 +888,55 @@ '*serial': 'str', '*dev': 'str', '*ccw-address': 'GuestCCWAddress'} } +## +# @GuestNVMeSmart: +# +# NVMe smart informations, based on NVMe specification, +# section +# +# Since: 7.1 +## +{ 'struct': 'GuestNVMeSmart', + 'data': {'critical-warning': 'int', + 'temperature': 'int', + 'available-spare': 'int', + 'available-spare-threshold': 'int', + 'percentage-used': 'int', + 'data-units-read-lo': 'uint64', + 'data-units-read-hi': 'uint64', + 'data-units-written-lo': 'uint64', + 'data-units-written-hi': 'uint64', + 'host-read-commands-lo': 'uint64', + 'host-read-commands-hi': 'uint64', + 'host-write-commands-lo': 'uint64', + 'host-write-commands-hi': 'uint64', + 'controller-busy-time-lo': 'uint64', + 'controller-busy-time-hi': 'uint64', + 'power-cycles-lo': 'uint64', + 'power-cycles-hi': 'uint64', + 'power-on-hours-lo': 'uint64', + 'power-on-hours-hi': 'uint64', + 'unsafe-shutdowns-lo': 'uint64', + 'unsafe-shutdowns-hi': 'uint64', + 'media-errors-lo': 'uint64', + 'media-errors-hi': 'uint64', + 'number-of-error-log-entries-lo': 'uint64', + 'number-of-error-log-entries-hi': 'uint64' } } + +## +# @GuestDiskSmart: +# +# Disk type related smart information. +# +# - @nvme: NVMe disk smart +# +# Since: 7.1 +## +{ 'union': 'GuestDiskSmart', + 'base': { 'type': 'GuestDiskBusType' }, + 'discriminator': 'type', + 'data': { 'nvme': 'GuestNVMeSmart' } } + ## # @GuestDiskInfo: # @@ -899,12 +948,14 @@ # @address: disk address information (only for non-virtual devices) # @alias: optional alias assigned to the disk, on Linux this is a name assigned # by device mapper +# @smart: disk smart information (Since 7.1) # # Since 5.2 ## { 'struct': 'GuestDiskInfo', 'data': {'name': 'str', 'partition': 'bool', '*dependencies': ['str'], - '*address': 'GuestDiskAddress', '*alias': 'str'} } + '*address': 'GuestDiskAddress', '*alias': 'str', + '*smart': 'GuestDiskSmart'} } ## # @guest-get-disks: