diff mbox series

[v2,RESEND] qga: Introduce disk smart

Message ID 20220307062041.1376177-1-pizhenwei@bytedance.com (mailing list archive)
State New, archived
Headers show
Series [v2,RESEND] qga: Introduce disk smart | expand

Commit Message

zhenwei pi March 7, 2022, 6:20 a.m. UTC
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.

CC: Keith Busch <kbusch@kernel.org>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
 qga/commands-posix.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
 qga/qapi-schema.json | 50 +++++++++++++++++++++++++++-
 2 files changed, 127 insertions(+), 1 deletion(-)

Comments

Marc-André Lureau March 7, 2022, 7:18 a.m. UTC | #1
Hi

On Mon, Mar 7, 2022 at 10:24 AM zhenwei pi <pizhenwei@bytedance.com> wrote:

> 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.
>
> CC: Keith Busch <kbusch@kernel.org>
> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
> ---
>  qga/commands-posix.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
>  qga/qapi-schema.json | 50 +++++++++++++++++++++++++++-
>  2 files changed, 127 insertions(+), 1 deletion(-)
>
> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> index 4ec83bbfbc..1fa089e9c8 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 <utmpx.h>
> @@ -49,6 +50,7 @@ extern char **environ;
>  #include <sys/socket.h>
>  #include <net/if.h>
>  #include <sys/statvfs.h>
> +#include <linux/nvme_ioctl.h>
>
>  #ifdef CONFIG_LIBUDEV
>  #include <libudev.h>
> @@ -1390,6 +1392,81 @@ static GuestDiskInfoList *get_disk_partitions(
>      return ret;
>  }
>
> +static void get_disk_smart(GuestDiskInfo *disk)
> +{
> +    if (disk->has_address
> +        && (disk->address->bus_type == GUEST_DISK_BUS_TYPE_NVME)) {
>

to avoid extra indentation, you could return early.


> +        int fd;
> +        GuestDiskSmart *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", disk->name);
>

g_strerror(errno) could help


> +            return;
> +        }
> +        if (ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd)) {
> +            g_debug("Failed to get smart: %s", disk->name);
>

same


> +            close(fd);
> +            return;
> +        }
> +
> +        smart = g_new0(GuestDiskSmart, 1);
> +        smart->type = GUEST_DISK_BUS_TYPE_NVME;
> +        smart->u.nvme.critical_warning = log.critical_warning;
> +        /* unaligned 'temperature' field */
> +        smart->u.nvme.temperature = lduw_le_p(&log.temperature);
> +        smart->u.nvme.available_spare = log.available_spare;
> +        smart->u.nvme.available_spare_threshold =
> log.available_spare_threshold;
> +        smart->u.nvme.percentage_used = log.percentage_used;
> +        smart->u.nvme.data_units_read_lo =
> le64_to_cpu(log.data_units_read[0]);
> +        smart->u.nvme.data_units_read_hi =
> le64_to_cpu(log.data_units_read[1]);
> +        smart->u.nvme.data_units_written_lo =
> +            le64_to_cpu(log.data_units_written[0]);
> +        smart->u.nvme.data_units_written_hi =
> +            le64_to_cpu(log.data_units_written[1]);
> +        smart->u.nvme.host_read_commands_lo =
> +            le64_to_cpu(log.host_read_commands[0]);
> +        smart->u.nvme.host_read_commands_hi =
> +            le64_to_cpu(log.host_read_commands[1]);
> +        smart->u.nvme.host_write_commands_lo =
> +            le64_to_cpu(log.host_write_commands[0]);
> +        smart->u.nvme.host_write_commands_hi =
> +            le64_to_cpu(log.host_write_commands[1]);
> +        smart->u.nvme.controller_busy_time_lo =
> +            le64_to_cpu(log.controller_busy_time[0]);
> +        smart->u.nvme.controller_busy_time_hi =
> +            le64_to_cpu(log.controller_busy_time[1]);
> +        smart->u.nvme.power_cycles_lo = le64_to_cpu(log.power_cycles[0]);
> +        smart->u.nvme.power_cycles_hi = le64_to_cpu(log.power_cycles[1]);
> +        smart->u.nvme.power_on_hours_lo =
> le64_to_cpu(log.power_on_hours[0]);
> +        smart->u.nvme.power_on_hours_hi =
> le64_to_cpu(log.power_on_hours[1]);
> +        smart->u.nvme.unsafe_shutdowns_lo =
> +            le64_to_cpu(log.unsafe_shutdowns[0]);
> +        smart->u.nvme.unsafe_shutdowns_hi =
> +            le64_to_cpu(log.unsafe_shutdowns[1]);
> +        smart->u.nvme.media_errors_lo = le64_to_cpu(log.media_errors[0]);
> +        smart->u.nvme.media_errors_hi = le64_to_cpu(log.media_errors[1]);
> +        smart->u.nvme.number_of_error_log_entries_lo =
> +            le64_to_cpu(log.number_of_error_log_entries[0]);
> +        smart->u.nvme.number_of_error_log_entries_hi =
> +            le64_to_cpu(log.number_of_error_log_entries[1]);
> +
> +        disk->has_smart = true;
> +        disk->smart = smart;
> +
> +        close(fd);
> +    }
> +}
> +
>  GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
>  {
>      GuestDiskInfoList *ret = NULL;
> @@ -1463,6 +1540,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 8f73770210..8bb8731ce4 100644
> --- a/qga/qapi-schema.json
> +++ b/qga/qapi-schema.json
> @@ -888,6 +888,53 @@
>             '*serial': 'str', '*dev': 'str',
>             '*ccw-address': 'GuestCCWAddress'} }
>
> +##
> +# @GuestNVMeSmart:
> +#
> +# NVMe smart informations, base on NVMe specification
>

based


> +#
> +# Since: 7.0
> +##
> +{ '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:
> +#
> +# Smart of disk
> +# - @nvme: NVMe disk smart
> +#
> +# Since: 7.0
> +##
> +{ 'union': 'GuestDiskSmart',
> +  'base': { 'type': 'GuestDiskBusType' },
> +  'discriminator': 'type',
> +  'data': { 'nvme': 'GuestNVMeSmart' } }
> +
>  ##
>  # @GuestDiskInfo:
>  #
>

You should update the doc about the new smart field


> @@ -904,7 +951,8 @@
>  ##
>  { 'struct': 'GuestDiskInfo',
>    'data': {'name': 'str', 'partition': 'bool', '*dependencies': ['str'],
> -           '*address': 'GuestDiskAddress', '*alias': 'str'} }
> +           '*address': 'GuestDiskAddress', '*alias': 'str',
> +           '*smart': 'GuestDiskSmart'} }
>
>  ##
>  # @guest-get-disks:
> --
> 2.20.1
>
>
>
diff mbox series

Patch

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 4ec83bbfbc..1fa089e9c8 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 <utmpx.h>
@@ -49,6 +50,7 @@  extern char **environ;
 #include <sys/socket.h>
 #include <net/if.h>
 #include <sys/statvfs.h>
+#include <linux/nvme_ioctl.h>
 
 #ifdef CONFIG_LIBUDEV
 #include <libudev.h>
@@ -1390,6 +1392,81 @@  static GuestDiskInfoList *get_disk_partitions(
     return ret;
 }
 
+static void get_disk_smart(GuestDiskInfo *disk)
+{
+    if (disk->has_address
+        && (disk->address->bus_type == GUEST_DISK_BUS_TYPE_NVME)) {
+        int fd;
+        GuestDiskSmart *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", disk->name);
+            return;
+        }
+        if (ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd)) {
+            g_debug("Failed to get smart: %s", disk->name);
+            close(fd);
+            return;
+        }
+
+        smart = g_new0(GuestDiskSmart, 1);
+        smart->type = GUEST_DISK_BUS_TYPE_NVME;
+        smart->u.nvme.critical_warning = log.critical_warning;
+        /* unaligned 'temperature' field */
+        smart->u.nvme.temperature = lduw_le_p(&log.temperature);
+        smart->u.nvme.available_spare = log.available_spare;
+        smart->u.nvme.available_spare_threshold = log.available_spare_threshold;
+        smart->u.nvme.percentage_used = log.percentage_used;
+        smart->u.nvme.data_units_read_lo = le64_to_cpu(log.data_units_read[0]);
+        smart->u.nvme.data_units_read_hi = le64_to_cpu(log.data_units_read[1]);
+        smart->u.nvme.data_units_written_lo =
+            le64_to_cpu(log.data_units_written[0]);
+        smart->u.nvme.data_units_written_hi =
+            le64_to_cpu(log.data_units_written[1]);
+        smart->u.nvme.host_read_commands_lo =
+            le64_to_cpu(log.host_read_commands[0]);
+        smart->u.nvme.host_read_commands_hi =
+            le64_to_cpu(log.host_read_commands[1]);
+        smart->u.nvme.host_write_commands_lo =
+            le64_to_cpu(log.host_write_commands[0]);
+        smart->u.nvme.host_write_commands_hi =
+            le64_to_cpu(log.host_write_commands[1]);
+        smart->u.nvme.controller_busy_time_lo =
+            le64_to_cpu(log.controller_busy_time[0]);
+        smart->u.nvme.controller_busy_time_hi =
+            le64_to_cpu(log.controller_busy_time[1]);
+        smart->u.nvme.power_cycles_lo = le64_to_cpu(log.power_cycles[0]);
+        smart->u.nvme.power_cycles_hi = le64_to_cpu(log.power_cycles[1]);
+        smart->u.nvme.power_on_hours_lo = le64_to_cpu(log.power_on_hours[0]);
+        smart->u.nvme.power_on_hours_hi = le64_to_cpu(log.power_on_hours[1]);
+        smart->u.nvme.unsafe_shutdowns_lo =
+            le64_to_cpu(log.unsafe_shutdowns[0]);
+        smart->u.nvme.unsafe_shutdowns_hi =
+            le64_to_cpu(log.unsafe_shutdowns[1]);
+        smart->u.nvme.media_errors_lo = le64_to_cpu(log.media_errors[0]);
+        smart->u.nvme.media_errors_hi = le64_to_cpu(log.media_errors[1]);
+        smart->u.nvme.number_of_error_log_entries_lo =
+            le64_to_cpu(log.number_of_error_log_entries[0]);
+        smart->u.nvme.number_of_error_log_entries_hi =
+            le64_to_cpu(log.number_of_error_log_entries[1]);
+
+        disk->has_smart = true;
+        disk->smart = smart;
+
+        close(fd);
+    }
+}
+
 GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
 {
     GuestDiskInfoList *ret = NULL;
@@ -1463,6 +1540,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 8f73770210..8bb8731ce4 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -888,6 +888,53 @@ 
            '*serial': 'str', '*dev': 'str',
            '*ccw-address': 'GuestCCWAddress'} }
 
+##
+# @GuestNVMeSmart:
+#
+# NVMe smart informations, base on NVMe specification
+#
+# Since: 7.0
+##
+{ '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:
+#
+# Smart of disk
+# - @nvme: NVMe disk smart
+#
+# Since: 7.0
+##
+{ 'union': 'GuestDiskSmart',
+  'base': { 'type': 'GuestDiskBusType' },
+  'discriminator': 'type',
+  'data': { 'nvme': 'GuestNVMeSmart' } }
+
 ##
 # @GuestDiskInfo:
 #
@@ -904,7 +951,8 @@ 
 ##
 { 'struct': 'GuestDiskInfo',
   'data': {'name': 'str', 'partition': 'bool', '*dependencies': ['str'],
-           '*address': 'GuestDiskAddress', '*alias': 'str'} }
+           '*address': 'GuestDiskAddress', '*alias': 'str',
+           '*smart': 'GuestDiskSmart'} }
 
 ##
 # @guest-get-disks: