Skip to contentSkip to conversation

hmp: add 'info virtio-pci-status id' command

PATCH In review ZhiPeng Lu 5 participants 6 messages May 2, 2017

lore
Share & tools
lore

ZhiPeng Lu

author
#0 lore raw

PATCH 1/1 hmp: add 'info virtio-pci-status id' command

Add command to query a virtio pci device status. we can get id of the virtio pci device by 'info pci' command. HMP Test case:

    ==============
    virsh # qemu-monitor-command --hmp 3 info pci
      Bus  0, device   3, function 0:
        Ethernet controller: PCI device 1af4:1000
          IRQ 11.
          BAR0: I/O at 0xc000 [0xc03f].
          BAR1: 32 bit memory at 0xfebd1000 [0xfebd1fff].
          BAR4: 64 bit prefetchable memory at 0xfe000000 [0xfe003fff].
          BAR6: 32 bit memory at 0xffffffffffffffff [0x0003fffe].
          id "net0"
      Bus  0, device   4, function 0:
        USB controller: PCI device 8086:24cd
          IRQ 11.
          BAR0: 32 bit memory at 0xfebd2000 [0xfebd2fff].
          id "usb"
    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status net0"
    status=15

    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status usb"
    the 'info virtio_pci_status' command only supports virtio pci devices
---
 hmp-commands-info.hx    | 14 ++++++++++++++
 hw/pci/pci-stub.c       |  6 ++++++
 hw/virtio/virtio-pci.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/sysemu.h |  1 +
 4 files changed, 68 insertions(+)
hmp-commands-info.hx +14 -0
index a53f105..7b9c4db 100644
@@ -815,6 +815,20 @@ ETEXI
         .cmd = hmp_info_vm_generation_id,
     },
 
+    {
+        .name       = "virtio-pci-status",
+        .args_type  = "id:s",
+        .params     = "id",
+        .help       = "show virtio pci device  status",
+        .cmd        = hmp_info_virtio_pci_status,
+    },
+
+STEXI
+@item info virtio-pci-status  @var{id}
+@findex virtio-pci-status
+Show status about virtio pci device
+ETEXI
+
 STEXI
 @end table
 ETEXI
hw/pci/pci-stub.c +6 -0
index 36d2c43..3609a52 100644
@@ -35,3 +35,9 @@ void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
 {
     monitor_printf(mon, "PCI devices not supported\n");
 }
+
+void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict)
+{
+    monitor_printf(mon, "PCI devices not supported\n");
+}
+
hw/virtio/virtio-pci.c +47 -0

65 lines not on this page · raw message

include/sysemu/sysemu.h +1 -0
index 576c7ce..de66cc0 100644
@@ -238,4 +238,5 @@ extern QemuOptsList qemu_net_opts;
 extern QemuOptsList qemu_global_opts;
 extern QemuOptsList qemu_mon_opts;
 
+extern void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict);
 #endif
Signature · 1 line
1.8.3.1
#1 lore raw

* ZhiPeng Lu ([email protected]) wrote:

Hi,

Quoted 1 line

Add command to query a virtio pci device status.

Is this purely for debug? If it's not for debug then you'll need to structure it around a matching QMP command.

Quoted 11 lines

we can get id of the virtio pci device by 'info pci' command. HMP Test case:

    ==============
    virsh # qemu-monitor-command --hmp 3 info pci
      Bus  0, device   3, function 0:
        Ethernet controller: PCI device 1af4:1000
          IRQ 11.
          BAR0: I/O at 0xc000 [0xc03f].
          BAR1: 32 bit memory at 0xfebd1000 [0xfebd1fff].
          BAR4: 64 bit prefetchable memory at 0xfe000000 [0xfe003fff].
          BAR6: 32 bit memory at 0xffffffffffffffff [0x0003fffe].
Quoted 8 lines
          id "net0"
      Bus  0, device   4, function 0:
        USB controller: PCI device 8086:24cd
          IRQ 11.
          BAR0: 32 bit memory at 0xfebd2000 [0xfebd2fff].
          id "usb"
    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status net0"
    status=15

Would it be better to make this 'info virtio-pci' and perhaps then you might dump some more info in the future?

Quoted 79 lines

virsh # qemu-monitor-command 3 --hmp "info virtio-pci-status usb" the 'info virtio_pci_status' command only supports virtio pci devices

---
 hmp-commands-info.hx    | 14 ++++++++++++++
 hw/pci/pci-stub.c       |  6 ++++++
 hw/virtio/virtio-pci.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/sysemu.h |  1 +
 4 files changed, 68 insertions(+)
hmp-commands-info.hx +14 -0
index a53f105..7b9c4db 100644
@@ -815,6 +815,20 @@ ETEXI
         .cmd = hmp_info_vm_generation_id,
     },
 
+    {
+        .name       = "virtio-pci-status",
+        .args_type  = "id:s",
+        .params     = "id",
+        .help       = "show virtio pci device  status",
+        .cmd        = hmp_info_virtio_pci_status,
+    },
+
+STEXI
+@item info virtio-pci-status  @var{id}
+@findex virtio-pci-status
+Show status about virtio pci device
+ETEXI
+
 STEXI
 @end table
 ETEXI
hw/pci/pci-stub.c +6 -0
index 36d2c43..3609a52 100644
@@ -35,3 +35,9 @@ void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
 {
     monitor_printf(mon, "PCI devices not supported\n");
 }
+
+void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict)
+{
+    monitor_printf(mon, "PCI devices not supported\n");
+}
+
hw/virtio/virtio-pci.c +7 -0
index f9b7244..5fa862b 100644
@@ -37,6 +37,7 @@
 #include "qemu/range.h"
 #include "hw/virtio/virtio-bus.h"
 #include "qapi/visitor.h"
+#include "monitor/monitor.h"
 
 #define VIRTIO_PCI_REGION_SIZE(dev)     VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
 
@@ -50,6 +51,7 @@ static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 static void virtio_pci_reset(DeviceState *qdev);
 
+static void query_virtio_pci_status(Monitor *mon, const char *id);
 /* virtio device */
 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
@@ -2556,6 +2558,51 @@ static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                         virtio_bus_name);
 }
 
+static void query_virtio_pci_status(Monitor *mon, const char *id)
+{
+    int ret = 0, i = 0;
+    PCIDevice *dev = NULL;
+    hwaddr addr = 0;

hw/virtio/virtio-pci.c static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, · line 2575

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+    uint8_t val = 0;
+    const char *devtype = NULL;
+
+    ret = pci_qdev_find_device(id, &dev);
+    if (ret) {
+        monitor_printf(mon, "Can not find device %s\n", id);
+        return;
+    }
+    devtype = object_get_typename(OBJECT(dev));
+    if (strncmp("virtio-", devtype, 7) == 0) {

I wonder if there's a better way to do that - something like
if (object_dynamic_cast(OBJECT(dev), "virtio-pci-device"))

although my knowledge of qom isn't that great.

Quoted 4 lines
hw/virtio/virtio-pci.c +4 -0
+        for (i = 0; i < PCI_NUM_REGIONS; i++) {
+            if (dev->io_regions[i].type == PCI_BASE_ADDRESS_SPACE_IO) {
+                addr = dev->io_regions[i].addr;
+                break;

hw/virtio/virtio-pci.c static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, · line 2589

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+            }
+        }
+        if (addr != -1 && addr != 0) {
+            address_space_rw(&address_space_io, addr + VIRTIO_PCI_STATUS,
+            MEMTXATTRS_UNSPECIFIED, &val, 1, 0);
+            if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
+                fprintf(stderr, "driver is ok\n");
+            } else {
+                fprintf(stderr, "driver is not ok\n");
+            }

Decode the value into flags that you add to the monitor_printf, not a separate fprintf.

Dave

Quoted 33 lines
hw/virtio/virtio-pci.c +16 -0
+            monitor_printf(mon, "status=%d", val);
+        } else {
+            monitor_printf(mon, "status=%d", val);
+        }
+    } else {
+        monitor_printf(mon, "the 'info virtio_pci_status' command "
+           "only supports virtio pci devices");
+    }
+}
+
+void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict)
+{
+    const char *id = qdict_get_try_str(qdict, "id");
+    query_virtio_pci_status(mon, id);
+}
+
 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
 {
     BusClass *bus_class = BUS_CLASS(klass);
include/sysemu/sysemu.h +1 -0
index 576c7ce..de66cc0 100644
@@ -238,4 +238,5 @@ extern QemuOptsList qemu_net_opts;
 extern QemuOptsList qemu_global_opts;
 extern QemuOptsList qemu_mon_opts;
 
+extern void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict);
 #endif
Signature · 1 line
1.8.3.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#2 lore raw
In reply to ZhiPeng Lu
Quoted 51 lines · On Tue, 2 May 2017 10:38:11 +0800 ZhiPeng Lu <[email protected]> wrote:

Add command to query a virtio pci device status. we can get id of the virtio pci device by 'info pci' command. HMP Test case:

    ==============
    virsh # qemu-monitor-command --hmp 3 info pci
      Bus  0, device   3, function 0:
        Ethernet controller: PCI device 1af4:1000
          IRQ 11.
          BAR0: I/O at 0xc000 [0xc03f].
          BAR1: 32 bit memory at 0xfebd1000 [0xfebd1fff].
          BAR4: 64 bit prefetchable memory at 0xfe000000 [0xfe003fff].
          BAR6: 32 bit memory at 0xffffffffffffffff [0x0003fffe].
          id "net0"
      Bus  0, device   4, function 0:
        USB controller: PCI device 8086:24cd
          IRQ 11.
          BAR0: 32 bit memory at 0xfebd2000 [0xfebd2fff].
          id "usb"
    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status net0"
    status=15

    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status usb"
    the 'info virtio_pci_status' command only supports virtio pci devices
---
 hmp-commands-info.hx    | 14 ++++++++++++++
 hw/pci/pci-stub.c       |  6 ++++++
 hw/virtio/virtio-pci.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/sysemu.h |  1 +
 4 files changed, 68 insertions(+)
hw/virtio/virtio-pci.c +19 -0
+static void query_virtio_pci_status(Monitor *mon, const char *id)
+{
+    int ret = 0, i = 0;
+    PCIDevice *dev = NULL;
+    hwaddr addr = 0;
+    uint8_t val = 0;
+    const char *devtype = NULL;
+
+    ret = pci_qdev_find_device(id, &dev);
+    if (ret) {
+        monitor_printf(mon, "Can not find device %s\n", id);
+        return;
+    }
+    devtype = object_get_typename(OBJECT(dev));
+    if (strncmp("virtio-", devtype, 7) == 0) {
+        for (i = 0; i < PCI_NUM_REGIONS; i++) {
+            if (dev->io_regions[i].type == PCI_BASE_ADDRESS_SPACE_IO) {
+                addr = dev->io_regions[i].addr;
+                break;

hw/virtio/virtio-pci.c static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, · line 2589

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+            }
+        }
+        if (addr != -1 && addr != 0) {
+            address_space_rw(&address_space_io, addr + VIRTIO_PCI_STATUS,
+            MEMTXATTRS_UNSPECIFIED, &val, 1, 0);
+            if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
+                fprintf(stderr, "driver is ok\n");
+            } else {
+                fprintf(stderr, "driver is not ok\n");
+            }

Why are you only printing verbose information for DRIVER_OK? Either dump the status only, or decode everything.

(Or is that left over from debugging?)

hw/virtio/virtio-pci.c static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, · line 2598

Quoted 9 lines
hw/virtio/virtio-pci.c +9 -0
+            monitor_printf(mon, "status=%d", val);
+        } else {
+            monitor_printf(mon, "status=%d", val);
+        }
+    } else {
+        monitor_printf(mon, "the 'info virtio_pci_status' command "
+           "only supports virtio pci devices");
+    }
+}

Instead of introducing a virtio-pci only command, I think it would be better to introduce a virtio info command that can be used for any transport. There is already a transport callback to get the status byte. (When you have that mechanism in place, you could also easily use it to get information like feature bits.)

#3 lore raw
In reply to ZhiPeng Lu
Quoted 15 lines · ZhiPeng Lu

Add command to query a virtio pci device status. we can get id of the virtio pci device by 'info pci' command. HMP Test case:

    ==============
    virsh # qemu-monitor-command --hmp 3 info pci
      Bus  0, device   3, function 0:
        Ethernet controller: PCI device 1af4:1000
          IRQ 11.
          BAR0: I/O at 0xc000 [0xc03f].
          BAR1: 32 bit memory at 0xfebd1000 [0xfebd1fff].
          BAR4: 64 bit prefetchable memory at 0xfe000000 [0xfe003fff].
          BAR6: 32 bit memory at 0xffffffffffffffff [0x0003fffe].
          id "net0"
      Bus  0, device   4, function 0:
        USB controller: PCI device 8086:24cd
Quoted 8 lines
          IRQ 11.
          BAR0: 32 bit memory at 0xfebd2000 [0xfebd2fff].
          id "usb"
    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status net0"
    status=15

    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status usb"
    the 'info virtio_pci_status' command only supports virtio pci devices

This information can be useful for troubleshooting virtio devices, but I'm concerned about adding new ad-hoc HMP commands.

Adding new commands for one specific field of device state lead to inconsistent user interfaces. It adds a maintenance burden because all future versions of QEMU must continue to support this command for compatibility.

QMP should also be supported by new commands unless there is a reason to make the command HMP-only.

There is already a trace event called "virtio_set_status" so you can observe status changes from the host without adding a new monitor command. Unfortunately tracing does not allow you to query the current value so it might not be possible to trigger the trace event at the appropriate time.

One option is to make the virtio status a QOM property so the existing qom-get command can be used to fetch the value without adding a new monitor command.

Does anyone have other ideas?

Quoted 134 lines
---
 hmp-commands-info.hx    | 14 ++++++++++++++
 hw/pci/pci-stub.c       |  6 ++++++
 hw/virtio/virtio-pci.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/sysemu.h |  1 +
 4 files changed, 68 insertions(+)
hmp-commands-info.hx +14 -0
index a53f105..7b9c4db 100644
@@ -815,6 +815,20 @@ ETEXI
         .cmd = hmp_info_vm_generation_id,
     },
 
+    {
+        .name       = "virtio-pci-status",
+        .args_type  = "id:s",
+        .params     = "id",
+        .help       = "show virtio pci device  status",
+        .cmd        = hmp_info_virtio_pci_status,
+    },
+
+STEXI
+@item info virtio-pci-status  @var{id}
+@findex virtio-pci-status
+Show status about virtio pci device
+ETEXI
+
 STEXI
 @end table
 ETEXI
hw/pci/pci-stub.c +6 -0
index 36d2c43..3609a52 100644
@@ -35,3 +35,9 @@ void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
 {
     monitor_printf(mon, "PCI devices not supported\n");
 }
+
+void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict)
+{
+    monitor_printf(mon, "PCI devices not supported\n");
+}
+
hw/virtio/virtio-pci.c +47 -0
index f9b7244..5fa862b 100644
@@ -37,6 +37,7 @@
 #include "qemu/range.h"
 #include "hw/virtio/virtio-bus.h"
 #include "qapi/visitor.h"
+#include "monitor/monitor.h"
 
 #define VIRTIO_PCI_REGION_SIZE(dev)     VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
 
@@ -50,6 +51,7 @@ static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 static void virtio_pci_reset(DeviceState *qdev);
 
+static void query_virtio_pci_status(Monitor *mon, const char *id);
 /* virtio device */
 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
@@ -2556,6 +2558,51 @@ static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                         virtio_bus_name);
 }
 
+static void query_virtio_pci_status(Monitor *mon, const char *id)
+{
+    int ret = 0, i = 0;
+    PCIDevice *dev = NULL;
+    hwaddr addr = 0;
+    uint8_t val = 0;
+    const char *devtype = NULL;
+
+    ret = pci_qdev_find_device(id, &dev);
+    if (ret) {
+        monitor_printf(mon, "Can not find device %s\n", id);
+        return;
+    }
+    devtype = object_get_typename(OBJECT(dev));
+    if (strncmp("virtio-", devtype, 7) == 0) {
+        for (i = 0; i < PCI_NUM_REGIONS; i++) {
+            if (dev->io_regions[i].type == PCI_BASE_ADDRESS_SPACE_IO) {
+                addr = dev->io_regions[i].addr;
+                break;
+            }
+        }
+        if (addr != -1 && addr != 0) {
+            address_space_rw(&address_space_io, addr + VIRTIO_PCI_STATUS,
+            MEMTXATTRS_UNSPECIFIED, &val, 1, 0);
+            if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
+                fprintf(stderr, "driver is ok\n");
+            } else {
+                fprintf(stderr, "driver is not ok\n");
+            }
+            monitor_printf(mon, "status=%d", val);
+        } else {
+            monitor_printf(mon, "status=%d", val);
+        }
+    } else {
+        monitor_printf(mon, "the 'info virtio_pci_status' command "
+           "only supports virtio pci devices");
+    }
+}
+
+void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict)
+{
+    const char *id = qdict_get_try_str(qdict, "id");
+    query_virtio_pci_status(mon, id);
+}
+
 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
 {
     BusClass *bus_class = BUS_CLASS(klass);
include/sysemu/sysemu.h +1 -0
index 576c7ce..de66cc0 100644
@@ -238,4 +238,5 @@ extern QemuOptsList qemu_net_opts;
 extern QemuOptsList qemu_global_opts;
 extern QemuOptsList qemu_mon_opts;
 
+extern void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict);
 #endif
Signature · 1 line
1.8.3.1
#4 lore raw
Quoted 42 lines · * Stefan Hajnoczi ([email protected]) wrote:
Quoted 15 lines · ZhiPeng Lu

Add command to query a virtio pci device status. we can get id of the virtio pci device by 'info pci' command. HMP Test case:

    ==============
    virsh # qemu-monitor-command --hmp 3 info pci
      Bus  0, device   3, function 0:
        Ethernet controller: PCI device 1af4:1000
          IRQ 11.
          BAR0: I/O at 0xc000 [0xc03f].
          BAR1: 32 bit memory at 0xfebd1000 [0xfebd1fff].
          BAR4: 64 bit prefetchable memory at 0xfe000000 [0xfe003fff].
          BAR6: 32 bit memory at 0xffffffffffffffff [0x0003fffe].
          id "net0"
      Bus  0, device   4, function 0:
        USB controller: PCI device 8086:24cd
Quoted 8 lines
          IRQ 11.
          BAR0: 32 bit memory at 0xfebd2000 [0xfebd2fff].
          id "usb"
    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status net0"
    status=15

    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status usb"
    the 'info virtio_pci_status' command only supports virtio pci devices

This information can be useful for troubleshooting virtio devices, but I'm concerned about adding new ad-hoc HMP commands.

Adding new commands for one specific field of device state lead to inconsistent user interfaces. It adds a maintenance burden because all future versions of QEMU must continue to support this command for compatibility.

QMP should also be supported by new commands unless there is a reason to make the command HMP-only.

There is already a trace event called "virtio_set_status" so you can observe status changes from the host without adding a new monitor command. Unfortunately tracing does not allow you to query the current value so it might not be possible to trigger the trace event at the appropriate time.

Quoted 3 lines

One option is to make the virtio status a QOM property so the existing qom-get command can be used to fetch the value without adding a new monitor command.

Unfortunately there is no HMP equivalent of qom-get, my previous attempts at it didn't get anywhere.

Quoted 1 line

Does anyone have other ideas?

I think it should be at least at the level of 'info virtio-pci'.

Dave

Quoted 127 lines
Quoted 127 lines
---
 hmp-commands-info.hx    | 14 ++++++++++++++
 hw/pci/pci-stub.c       |  6 ++++++
 hw/virtio/virtio-pci.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/sysemu.h |  1 +
 4 files changed, 68 insertions(+)
hmp-commands-info.hx +14 -0
index a53f105..7b9c4db 100644
@@ -815,6 +815,20 @@ ETEXI
         .cmd = hmp_info_vm_generation_id,
     },
 
+    {
+        .name       = "virtio-pci-status",
+        .args_type  = "id:s",
+        .params     = "id",
+        .help       = "show virtio pci device  status",
+        .cmd        = hmp_info_virtio_pci_status,
+    },
+
+STEXI
+@item info virtio-pci-status  @var{id}
+@findex virtio-pci-status
+Show status about virtio pci device
+ETEXI
+
 STEXI
 @end table
 ETEXI
hw/pci/pci-stub.c +6 -0
index 36d2c43..3609a52 100644
@@ -35,3 +35,9 @@ void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
 {
     monitor_printf(mon, "PCI devices not supported\n");
 }
+
+void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict)
+{
+    monitor_printf(mon, "PCI devices not supported\n");
+}
+
hw/virtio/virtio-pci.c +47 -0
index f9b7244..5fa862b 100644
@@ -37,6 +37,7 @@
 #include "qemu/range.h"
 #include "hw/virtio/virtio-bus.h"
 #include "qapi/visitor.h"
+#include "monitor/monitor.h"
 
 #define VIRTIO_PCI_REGION_SIZE(dev)     VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
 
@@ -50,6 +51,7 @@ static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                                VirtIOPCIProxy *dev);
 static void virtio_pci_reset(DeviceState *qdev);
 
+static void query_virtio_pci_status(Monitor *mon, const char *id);
 /* virtio device */
 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
@@ -2556,6 +2558,51 @@ static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                         virtio_bus_name);
 }
 
+static void query_virtio_pci_status(Monitor *mon, const char *id)
+{
+    int ret = 0, i = 0;
+    PCIDevice *dev = NULL;
+    hwaddr addr = 0;
+    uint8_t val = 0;
+    const char *devtype = NULL;
+
+    ret = pci_qdev_find_device(id, &dev);
+    if (ret) {
+        monitor_printf(mon, "Can not find device %s\n", id);
+        return;
+    }
+    devtype = object_get_typename(OBJECT(dev));
+    if (strncmp("virtio-", devtype, 7) == 0) {
+        for (i = 0; i < PCI_NUM_REGIONS; i++) {
+            if (dev->io_regions[i].type == PCI_BASE_ADDRESS_SPACE_IO) {
+                addr = dev->io_regions[i].addr;
+                break;
+            }
+        }
+        if (addr != -1 && addr != 0) {
+            address_space_rw(&address_space_io, addr + VIRTIO_PCI_STATUS,
+            MEMTXATTRS_UNSPECIFIED, &val, 1, 0);
+            if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
+                fprintf(stderr, "driver is ok\n");
+            } else {
+                fprintf(stderr, "driver is not ok\n");
+            }
+            monitor_printf(mon, "status=%d", val);
+        } else {
+            monitor_printf(mon, "status=%d", val);
+        }
+    } else {
+        monitor_printf(mon, "the 'info virtio_pci_status' command "
+           "only supports virtio pci devices");
+    }
+}
+
+void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict)
+{
+    const char *id = qdict_get_try_str(qdict, "id");
+    query_virtio_pci_status(mon, id);
+}
+
 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
 {
     BusClass *bus_class = BUS_CLASS(klass);
include/sysemu/sysemu.h +0 -0
index 576c7ce..de66cc0 100644
@@ -238,4 +238,5 @@ extern QemuOptsList qemu_net_opts;
 extern QemuOptsList qemu_global_opts;
 extern QemuOptsList qemu_mon_opts;
 
Quoted 7 lines
Quoted 7 lines
include/sysemu/sysemu.h +1 -0
+extern void hmp_info_virtio_pci_status(Monitor *mon, const QDict *qdict);
 #endif
Signature · 1 line
1.8.3.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw
In reply to ZhiPeng Lu
Quoted 24 lines · ZhiPeng Lu

Add command to query a virtio pci device status. we can get id of the virtio pci device by 'info pci' command. HMP Test case:

    ==============
    virsh # qemu-monitor-command --hmp 3 info pci
      Bus  0, device   3, function 0:
        Ethernet controller: PCI device 1af4:1000
          IRQ 11.
          BAR0: I/O at 0xc000 [0xc03f].
          BAR1: 32 bit memory at 0xfebd1000 [0xfebd1fff].
          BAR4: 64 bit prefetchable memory at 0xfe000000 [0xfe003fff].
          BAR6: 32 bit memory at 0xffffffffffffffff [0x0003fffe].
          id "net0"
      Bus  0, device   4, function 0:
        USB controller: PCI device 8086:24cd
          IRQ 11.
          BAR0: 32 bit memory at 0xfebd2000 [0xfebd2fff].
          id "usb"
    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status net0"
    status=15

    virsh # qemu-monitor-command 3  --hmp "info virtio-pci-status usb"
    the 'info virtio_pci_status' command only supports virtio pci devices
Quoted 7 lines
---
 hmp-commands-info.hx    | 14 ++++++++++++++
 hw/pci/pci-stub.c       |  6 ++++++
 hw/virtio/virtio-pci.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/sysemu.h |  1 +
 4 files changed, 68 insertions(+)

You implemented this just for HMP.

In general, functionality available in HMP should also available in QMP. Exceptions include functionality that makes no sense in QMP, or is of use only for human users. If you think your command is an exception, please explain why in the commit message.

If it isn't, you need to implement it for QMP (including suitable test cases), then rewrite the HMP version to reuse either the QMP command or a common core.

Example for "makes no sense in QMP": setting the current CPU[1], because a QMP monitor doesn't have a current CPU.

Examples for "is of use only for human users": HMP command "help", the integrated pocket calculator[2].

[1] For reasons lost in time, we added QMP command "cpu" anyway, it just does nothing.

[2] I doubt that one is of much use to humans, either.