Skip to contentSkip to conversation

tests/qtest: add some tests for virtio-net failover

PATCH Superseded Laurent Vivier 1 participant 6 messages Nov 18, 2021 Arm/Amlogic Meson SoC support

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 cover tests/qtest: add some tests for virtio-net failover

This series adds a qtest entry to test virtio-net failover feature.

We check following error cases:

- check missing id on device with failover_pair_id triggers an error
- check a primary device plugged on a bus that doesn't support hotplug triggers an error

We check the status of the machine before and after hotplugging cards and feature negotiation:

- check we don't see the primary device at boot if failover is on
- check we see the primary device at boot if failover is off
- check we don't see the primary device if failover is on
but failover_pair_id is not the one with on (I think this should be changed)
- check the primary device is plugged after the feature negotiation
- check the result if the primary device is plugged before standby device and vice-versa
- check the if the primary device is coldplugged and the standy device hotplugged and vice-versa
- check the migration triggers the unplug and the hotplug

There is one preliminary patch in the series:

- PATCH 1 introduces a function to enable PCI bridge. Failover needs to be plugged on a pcie-root-port and while the root port is not configured the cards behind it are not available

v3:
- fix a bug with ACPI unplug and add the related test

v2:
- remove PATCH 1 that introduced a function that can be replaced by qobject_to_json_pretty() (Markus)
- Add migration to a file and from the file to check the card is correctly unplugged on the source, and hotplugged on the dest
- Add an ACPI call to eject the card as the kernel would do

Laurent Vivier (5):
qtest/libqos: add a function to initialize secondary PCI buses tests/qtest: add some tests for virtio-net failover
failover: fix unplug pending detection
libqtest: add a function to use a timeout when waiting for an event tests/libqtest: update virtio-net failover test

 hw/acpi/pcihp.c                   |  30 +-
 include/hw/pci/pci_bridge.h       |   8 +
 tests/qtest/libqos/libqtest.h     |  26 +-
 tests/qtest/libqos/pci.c          | 118 ++++++
 tests/qtest/libqos/pci.h          |   1 +
 tests/qtest/libqtest.c            |  37 +-
 tests/qtest/meson.build           |   3 +
 tests/qtest/virtio-net-failover.c | 644 ++++++++++++++++++++++++++++++
 tests/unit/test-qga.c             |   2 +-
 9 files changed, 859 insertions(+), 10 deletions(-)
 create mode 100644 tests/qtest/virtio-net-failover.c
Signature · 1 line
2.33.1
#1 lore raw

PATCH v3 1/5 qtest/libqos: add a function to initialize secondary PCI buses

Scan the PCI devices to find bridge and set PCI_SECONDARY_BUS and PCI_SUBORDINATE_BUS (algorithm from seabios)

---
 include/hw/pci/pci_bridge.h |   8 +++
 tests/qtest/libqos/pci.c    | 118 ++++++++++++++++++++++++++++++++++++
 tests/qtest/libqos/pci.h    |   1 +
 3 files changed, 127 insertions(+)
include/hw/pci/pci_bridge.h +8 -0
index a94d350034bf..30691a6e5728 100644
@@ -138,6 +138,7 @@ typedef struct PCIBridgeQemuCap {
     uint64_t mem_pref_64; /* Prefetchable memory to reserve (64-bit MMIO) */
 } PCIBridgeQemuCap;
 
+#define REDHAT_PCI_CAP_TYPE_OFFSET      3
 #define REDHAT_PCI_CAP_RESOURCE_RESERVE 1
 
 /*
@@ -152,6 +153,13 @@ typedef struct PCIResReserve {
     uint64_t mem_pref_64;
 } PCIResReserve;
 
+#define REDHAT_PCI_CAP_RES_RESERVE_BUS_RES     4
+#define REDHAT_PCI_CAP_RES_RESERVE_IO          8
+#define REDHAT_PCI_CAP_RES_RESERVE_MEM         16
+#define REDHAT_PCI_CAP_RES_RESERVE_PREF_MEM_32 20
+#define REDHAT_PCI_CAP_RES_RESERVE_PREF_MEM_64 24
+#define REDHAT_PCI_CAP_RES_RESERVE_CAP_SIZE    32
+
 int pci_bridge_qemu_reserve_cap_init(PCIDevice *dev, int cap_offset,
                                PCIResReserve res_reserve, Error **errp);
 
tests/qtest/libqos/pci.c +118 -0

130 lines not on this page · raw message

tests/qtest/libqos/pci.h +1 -0
index ee64fdecbda8..becb800f9e6a 100644
@@ -81,6 +81,7 @@ void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
                          void *data);
 QPCIDevice *qpci_device_find(QPCIBus *bus, int devfn);
 void qpci_device_init(QPCIDevice *dev, QPCIBus *bus, QPCIAddress *addr);
+int qpci_secondary_buses_init(QPCIBus *bus);
 
 bool qpci_has_buggy_msi(QPCIDevice *dev);
 bool qpci_check_buggy_msi(QPCIDevice *dev);
Signature · 1 line
2.33.1
#2 lore raw

PATCH v3 2/5 tests/qtest: add some tests for virtio-net failover

In reply to Laurent Vivier

Add test cases to test several error cases that must be generated by invalid failover configuration.

Add a combination of coldplug and hotplug test cases to be sure the primary is correctly managed according the presence or not of the STANDBY feature.

---
 tests/qtest/meson.build           |   3 +
 tests/qtest/virtio-net-failover.c | 635 ++++++++++++++++++++++++++++++
 2 files changed, 638 insertions(+)
 create mode 100644 tests/qtest/virtio-net-failover.c
tests/qtest/meson.build +3 -0
index c9d8458062ff..6d66bf522156 100644
@@ -22,6 +22,9 @@ qtests_generic = \
   (config_all_devices.has_key('CONFIG_VIRTIO_SCSI') ? ['fuzz-virtio-scsi-test'] : []) + \
   (config_all_devices.has_key('CONFIG_SB16') ? ['fuzz-sb16-test'] : []) + \
   (config_all_devices.has_key('CONFIG_SDHCI_PCI') ? ['fuzz-sdcard-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_VIRTIO_NET') and \
+   config_all_devices.has_key('CONFIG_Q35') and \
+   config_all_devices.has_key('CONFIG_VIRTIO_PCI') ? ['virtio-net-failover'] : []) + \
   [
   'cdrom-test',
   'device-introspect-test',
tests/qtest/virtio-net-failover.c new file +635 -0

635 lines not on this page · raw message

Signature · 1 line
2.33.1
#3 lore raw

PATCH v3 3/5 failover: fix unplug pending detection

In reply to Laurent Vivier

Failover needs to detect the end of the PCI unplug to start migration after the VFIO card has been unplugged.

To do that, a flag is set in pcie_cap_slot_unplug_request_cb() and reset in pcie_unplug_device().

But since

    17858a169508 ("hw/acpi/ich9: Set ACPI PCI hot-plug as default on Q35")

we have switched to ACPI unplug and these functions are not called anymore and the flag not set. So failover migration is not able to detect if card is really unplugged and acts as it's done as soon as it's started. So it doesn't wait the end of the unplug to start the migration. We don't see any problem when we test that because ACPI unplug is faster than PCIe native hotplug and when the migration really starts the unplug operation is already done.

See c000a9bd06ea ("pci: mark device having guest unplug request pending")
a99c4da9fc2a ("pci: mark devices partially unplugged")

---
 hw/acpi/pcihp.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
hw/acpi/pcihp.c +27 -3
index f610a25d2ef9..30405b5113d7 100644
@@ -222,9 +222,27 @@ static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slo
         PCIDevice *dev = PCI_DEVICE(qdev);
         if (PCI_SLOT(dev->devfn) == slot) {
             if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
-                hotplug_ctrl = qdev_get_hotplug_handler(qdev);
-                hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
-                object_unparent(OBJECT(qdev));
+                /*
+                 * partially_hotplugged is used by virtio-net failover:
+                 * failover has asked the guest OS to unplug the device
+                 * but we need to keep some references to the device
+                 * to be able to plug it back in case of failure so
+                 * we don't execute hotplug_handler_unplug().
+                 */
+                if (dev->partially_hotplugged) {
+                    /*
+                     * pending_deleted_event is set to true when
+                     * virtio-net failover asks to unplug the device,
+                     * and set to false here when the operation is done
+                     * This is used by the migration loop to detect the
+                     * end of the operation and really start the migration.
+                     */
+                    qdev->pending_deleted_event = false;
+                } else {
+                    hotplug_ctrl = qdev_get_hotplug_handler(qdev);
+                    hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
+                    object_unparent(OBJECT(qdev));
+                }
             }
         }
     }
@@ -396,6 +414,12 @@ void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
         return;
     }
 
+    /*
+     * pending_deleted_event is used by virtio-net failover to detect the
+     * end of the unplug operation, the flag is set to false in
+     * acpi_pcihp_eject_slot() when the operation is completed.
+     */
+    pdev->qdev.pending_deleted_event = true;
     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
 }
Signature · 1 line
2.33.1
#4 lore raw

PATCH v3 4/5 libqtest: add a function to use a timeout when waiting for an event

In reply to Laurent Vivier

To be able to check we _don't_ receive a given event, we need to be able to stop to wait for it after a given amount of time.

To do that, introduce a timeout value in qtest_qmp_eventwait(). The new version of the function is qtest_qmp_eventwait_timeout(), that uses the new function qtest_qmp_receive_dict_timeout().

---
 tests/qtest/libqos/libqtest.h | 26 +++++++++++++++++++++++-
 tests/qtest/libqtest.c        | 37 ++++++++++++++++++++++++++++++-----
 tests/unit/test-qga.c         |  2 +-
 3 files changed, 58 insertions(+), 7 deletions(-)
tests/qtest/libqos/libqtest.h +25 -1
index 59e927119563..3f96afa5f431 100644
@@ -209,6 +209,17 @@ void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
     GCC_FMT_ATTR(2, 0);
 
+/**
+ * qtest_qmp_receive_dict_timeout:
+ * @s: #QTestState instance to operate on.
+ * @timeout: time to wait before aborting read
+ *
+ * Reads a QMP message from QEMU and returns the response.
+ * If a timeout is provided, return NULL if message is
+ * not received during the given amount of time
+ */
+QDict *qtest_qmp_receive_dict_timeout(QTestState *s, struct timeval *timeout);
+
 /**
  * qtest_qmp_receive_dict:
  * @s: #QTestState instance to operate on.
@@ -236,6 +247,19 @@ QDict *qtest_qmp_receive(QTestState *s);
  */
 void qtest_qmp_eventwait(QTestState *s, const char *event);
 
+/**
+ * qtest_qmp_eventwait_timeout:
+ * @s: #QTestState instance to operate on.
+ * @timeout: time to wait before aborting wait
+ * @event: event to wait for.
+ *
+ * Continuously polls for QMP responses until it receives the desired event
+ * or the timeout exausts.
+ * Returns a copy of the event for further investigation or NULL on timeout
+ */
+QDict *qtest_qmp_eventwait_timeout(QTestState *s, struct timeval *timeout,
+                                   const char *event);
+
 /**
  * qtest_qmp_eventwait_ref:
  * @s: #QTestState instance to operate on.
@@ -690,7 +714,7 @@ void qtest_remove_abrt_handler(void *data);
 void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
     GCC_FMT_ATTR(2, 3);
 
-QDict *qmp_fd_receive(int fd);
+QDict *qmp_fd_receive(int fd, struct timeval *timeout);
 void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num,
                       const char *fmt, va_list ap) GCC_FMT_ATTR(4, 0);
 void qmp_fd_vsend(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
tests/qtest/libqtest.c +32 -5
index 25aeea385bfa..10e05d0c7aa8 100644
@@ -599,7 +599,7 @@ static void qmp_response(void *opaque, QObject *obj, Error *err)
     g_assert(qmp->response);
 }
 
-QDict *qmp_fd_receive(int fd)
+QDict *qmp_fd_receive(int fd, struct timeval *timeout)
 {
     QMPResponseParser qmp;
     bool log = getenv("QTEST_LOG") != NULL;
@@ -610,6 +610,18 @@ QDict *qmp_fd_receive(int fd)
         ssize_t len;
         char c;
 
+        if (timeout) {
+            fd_set set;
+            int ret;
+
+            FD_ZERO(&set);
+            FD_SET(fd, &set);
+            ret = select(fd + 1, &set, NULL, NULL, timeout);
+            if (ret == 0) {
+                /* timeout */
+                return NULL;
+            }
+        }
         len = read(fd, &c, 1);
         if (len == -1 && errno == EINTR) {
             continue;
@@ -643,9 +655,14 @@ QDict *qtest_qmp_receive(QTestState *s)
     }
 }
 
+QDict *qtest_qmp_receive_dict_timeout(QTestState *s, struct timeval *timeout)
+{
+    return qmp_fd_receive(s->qmp_fd, timeout);
+}
+
 QDict *qtest_qmp_receive_dict(QTestState *s)
 {
-    return qmp_fd_receive(s->qmp_fd);
+    return qtest_qmp_receive_dict_timeout(s, NULL);
 }
 
 int qtest_socket_server(const char *socket_path)
@@ -729,7 +746,7 @@ QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
 {
     qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap);
 
-    return qmp_fd_receive(fd);
+    return qmp_fd_receive(fd, NULL);
 }
 
 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
@@ -848,7 +865,8 @@ QDict *qtest_qmp_event_ref(QTestState *s, const char *event)
     return NULL;
 }
 
-QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
+QDict *qtest_qmp_eventwait_timeout(QTestState *s, struct timeval *timeout,
+                                   const char *event)
 {
     QDict *response = qtest_qmp_event_ref(s, event);
 
@@ -857,7 +875,11 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
     }
 
     for (;;) {
-        response = qtest_qmp_receive_dict(s);
+        response = qtest_qmp_receive_dict_timeout(s, timeout);
+        if (timeout != NULL && response == NULL) {
+            /* exit on timeout */
+            return NULL;
+        }
         if ((qdict_haskey(response, "event")) &&
             (strcmp(qdict_get_str(response, "event"), event) == 0)) {
             return response;
@@ -866,6 +888,11 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
     }
 }
 
+QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
+{
+    return qtest_qmp_eventwait_timeout(s, NULL, event);
+}
+
 void qtest_qmp_eventwait(QTestState *s, const char *event)
 {
     QDict *response;
tests/unit/test-qga.c +1 -1
index 5cb140d1b53d..22aa6405aaec 100644
@@ -174,7 +174,7 @@ static void test_qga_sync_delimited(gconstpointer fix)
         g_assert_cmpint(v, ==, 1);
     } while (c != 0xff);
 
-    ret = qmp_fd_receive(fixture->fd);
+    ret = qmp_fd_receive(fixture->fd, NULL);
     g_assert_nonnull(ret);
     qmp_assert_no_error(ret);
 
Signature · 1 line
2.33.1
#5 lore raw

PATCH v3 5/5 tests/libqtest: update virtio-net failover test

In reply to Laurent Vivier

Update the migration test to check we correctly wait the end of the card unplug before doing the migration.

---
 tests/qtest/virtio-net-failover.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
tests/qtest/virtio-net-failover.c +12 -3
index 576d4454c3c3..7cc276e6ffc1 100644
@@ -478,6 +478,7 @@ static void test_outmigrate(gconstpointer opaque)
     QTestState *qts;
     QDict *resp, *args, *data;
     g_autofree gchar *uri = g_strdup_printf("exec: cat > %s", (gchar *)opaque);
+    struct timeval timeout;
 
     qts = machine_start(BASE_MACHINE
                      "-netdev user,id=hs0 "
@@ -525,11 +526,19 @@ static void test_outmigrate(gconstpointer opaque)
 
     qobject_unref(resp);
 
-    qtest_outl(qts, ACPI_PCIHP_ADDR_ICH9 + PCI_EJ_BASE, 1);
+    /*
+     * The migration cannot start if the card is not ejected,
+     * so we check it cannot end ("STOP") before the card is ejected
+     */
+    /* 10s is enough for ACPI, PCIe native would need at least 30s */
+    timeout.tv_sec = 10;
+    timeout.tv_usec = 0;
+    resp = qtest_qmp_eventwait_timeout(qts, &timeout, "STOP");
+    g_assert_null(resp);
 
-    resp = qtest_qmp_eventwait_ref(qts, "STOP");
-    qobject_unref(resp);
+    qtest_outl(qts, ACPI_PCIHP_ADDR_ICH9 + PCI_EJ_BASE, 1);
 
+    qtest_qmp_eventwait(qts, "STOP");
     /*
      * in fact, the card is ejected from the point of view of kernel
      * but not really from QEMU to be able to hotplug it back if
Signature · 1 line
2.33.1