qemu/tests/virtio-scsi-test.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for VirtIO SCSI
   3 *
   4 * Copyright (c) 2014 SUSE LINUX Products GmbH
   5 * Copyright (c) 2015 Red Hat Inc.
   6 *
   7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
   8 * See the COPYING file in the top-level directory.
   9 */
  10
  11#include "qemu/osdep.h"
  12#include "libqtest.h"
  13#include "qemu/module.h"
  14#include "scsi/constants.h"
  15#include "libqos/libqos-pc.h"
  16#include "libqos/libqos-spapr.h"
  17#include "libqos/virtio.h"
  18#include "libqos/virtio-pci.h"
  19#include "standard-headers/linux/virtio_ids.h"
  20#include "standard-headers/linux/virtio_pci.h"
  21#include "standard-headers/linux/virtio_scsi.h"
  22#include "libqos/virtio-scsi.h"
  23#include "libqos/qgraph.h"
  24
  25#define PCI_SLOT                0x02
  26#define PCI_FN                  0x00
  27#define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
  28
  29#define MAX_NUM_QUEUES 64
  30
  31typedef struct {
  32    QVirtioDevice *dev;
  33    int num_queues;
  34    QVirtQueue *vq[MAX_NUM_QUEUES + 2];
  35} QVirtioSCSIQueues;
  36
  37static QGuestAllocator *alloc;
  38
  39static void qvirtio_scsi_pci_free(QVirtioSCSIQueues *vs)
  40{
  41    int i;
  42
  43    for (i = 0; i < vs->num_queues + 2; i++) {
  44        qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], alloc);
  45    }
  46    g_free(vs);
  47}
  48
  49static uint64_t qvirtio_scsi_alloc(QVirtioSCSIQueues *vs, size_t alloc_size,
  50                                   const void *data)
  51{
  52    uint64_t addr;
  53
  54    addr = guest_alloc(alloc, alloc_size);
  55    if (data) {
  56        memwrite(addr, data, alloc_size);
  57    }
  58
  59    return addr;
  60}
  61
  62static uint8_t virtio_scsi_do_command(QVirtioSCSIQueues *vs,
  63                                      const uint8_t *cdb,
  64                                      const uint8_t *data_in,
  65                                      size_t data_in_len,
  66                                      uint8_t *data_out, size_t data_out_len,
  67                                      struct virtio_scsi_cmd_resp *resp_out)
  68{
  69    QVirtQueue *vq;
  70    struct virtio_scsi_cmd_req req = { { 0 } };
  71    struct virtio_scsi_cmd_resp resp = { .response = 0xff, .status = 0xff };
  72    uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
  73    uint8_t response;
  74    uint32_t free_head;
  75
  76    vq = vs->vq[2];
  77
  78    req.lun[0] = 1; /* Select LUN */
  79    req.lun[1] = 1; /* Select target 1 */
  80    memcpy(req.cdb, cdb, VIRTIO_SCSI_CDB_SIZE);
  81
  82    /* XXX: Fix endian if any multi-byte field in req/resp is used */
  83
  84    /* Add request header */
  85    req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
  86    free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true);
  87
  88    if (data_out_len) {
  89        data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
  90        qvirtqueue_add(vq, data_out_addr, data_out_len, false, true);
  91    }
  92
  93    /* Add response header */
  94    resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
  95    qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len);
  96
  97    if (data_in_len) {
  98        data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
  99        qvirtqueue_add(vq, data_in_addr, data_in_len, true, false);
 100    }
 101
 102    qvirtqueue_kick(vs->dev, vq, free_head);
 103    qvirtio_wait_used_elem(vs->dev, vq, free_head, NULL,
 104                           QVIRTIO_SCSI_TIMEOUT_US);
 105
 106    response = readb(resp_addr +
 107                     offsetof(struct virtio_scsi_cmd_resp, response));
 108
 109    if (resp_out) {
 110        memread(resp_addr, resp_out, sizeof(*resp_out));
 111    }
 112
 113    guest_free(alloc, req_addr);
 114    guest_free(alloc, resp_addr);
 115    guest_free(alloc, data_in_addr);
 116    guest_free(alloc, data_out_addr);
 117    return response;
 118}
 119
 120static QVirtioSCSIQueues *qvirtio_scsi_init(QVirtioDevice *dev)
 121{
 122    QVirtioSCSIQueues *vs;
 123    const uint8_t test_unit_ready_cdb[VIRTIO_SCSI_CDB_SIZE] = {};
 124    struct virtio_scsi_cmd_resp resp;
 125    int i;
 126
 127    vs = g_new0(QVirtioSCSIQueues, 1);
 128    vs->dev = dev;
 129    vs->num_queues = qvirtio_config_readl(dev, 0);
 130
 131    g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
 132
 133    for (i = 0; i < vs->num_queues + 2; i++) {
 134        vs->vq[i] = qvirtqueue_setup(dev, alloc, i);
 135    }
 136
 137    /* Clear the POWER ON OCCURRED unit attention */
 138    g_assert_cmpint(virtio_scsi_do_command(vs, test_unit_ready_cdb,
 139                                           NULL, 0, NULL, 0, &resp),
 140                    ==, 0);
 141    g_assert_cmpint(resp.status, ==, CHECK_CONDITION);
 142    g_assert_cmpint(resp.sense[0], ==, 0x70); /* Fixed format sense buffer */
 143    g_assert_cmpint(resp.sense[2], ==, UNIT_ATTENTION);
 144    g_assert_cmpint(resp.sense[12], ==, 0x29); /* POWER ON */
 145    g_assert_cmpint(resp.sense[13], ==, 0x00);
 146
 147    return vs;
 148}
 149
 150static void hotplug(void *obj, void *data, QGuestAllocator *alloc)
 151{
 152    qtest_qmp_device_add("scsi-hd", "scsihd", "{'drive': 'drv1'}");
 153    qtest_qmp_device_del("scsihd");
 154}
 155
 156/* Test WRITE SAME with the lba not aligned */
 157static void test_unaligned_write_same(void *obj, void *data,
 158                                      QGuestAllocator *t_alloc)
 159{
 160    QVirtioSCSI *scsi = obj;
 161    QVirtioSCSIQueues *vs;
 162    uint8_t buf1[512] = { 0 };
 163    uint8_t buf2[512] = { 1 };
 164    const uint8_t write_same_cdb_1[VIRTIO_SCSI_CDB_SIZE] = {
 165        0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00
 166    };
 167    const uint8_t write_same_cdb_2[VIRTIO_SCSI_CDB_SIZE] = {
 168        0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
 169    };
 170    const uint8_t write_same_cdb_ndob[VIRTIO_SCSI_CDB_SIZE] = {
 171        0x41, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
 172    };
 173
 174    alloc = t_alloc;
 175    vs = qvirtio_scsi_init(scsi->vdev);
 176
 177    g_assert_cmphex(0, ==,
 178        virtio_scsi_do_command(vs, write_same_cdb_1, NULL, 0, buf1, 512,
 179                               NULL));
 180
 181    g_assert_cmphex(0, ==,
 182        virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512,
 183                               NULL));
 184
 185    g_assert_cmphex(0, ==,
 186        virtio_scsi_do_command(vs, write_same_cdb_ndob, NULL, 0, NULL, 0,
 187                               NULL));
 188
 189    qvirtio_scsi_pci_free(vs);
 190}
 191
 192static void test_iothread_attach_node(void *obj, void *data,
 193                                      QGuestAllocator *t_alloc)
 194{
 195    QVirtioSCSIPCI *scsi_pci = obj;
 196    QVirtioSCSI *scsi = &scsi_pci->scsi;
 197    QVirtioSCSIQueues *vs;
 198    char tmp_path[] = "/tmp/qtest.XXXXXX";
 199    int fd;
 200    int ret;
 201
 202    uint8_t buf[512] = { 0 };
 203    const uint8_t write_cdb[VIRTIO_SCSI_CDB_SIZE] = {
 204        /* WRITE(10) to LBA 0, transfer length 1 */
 205        0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
 206    };
 207
 208    alloc = t_alloc;
 209    vs = qvirtio_scsi_init(scsi->vdev);
 210
 211    /* Create a temporary qcow2 overlay*/
 212    fd = mkstemp(tmp_path);
 213    g_assert(fd >= 0);
 214    close(fd);
 215
 216    if (!have_qemu_img()) {
 217        g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
 218                       "skipping snapshot test");
 219        goto fail;
 220    }
 221
 222    mkqcow2(tmp_path, 64);
 223
 224    /* Attach the overlay to the null0 node */
 225    qmp_assert_success("{'execute': 'blockdev-add', 'arguments': {"
 226                       "   'driver': 'qcow2', 'node-name': 'overlay',"
 227                       "   'backing': 'null0', 'file': {"
 228                       "     'driver': 'file', 'filename': %s}}}", tmp_path);
 229
 230    /* Send a request to see if the AioContext is still right */
 231    ret = virtio_scsi_do_command(vs, write_cdb, NULL, 0, buf, 512, NULL);
 232    g_assert_cmphex(ret, ==, 0);
 233
 234fail:
 235    qvirtio_scsi_pci_free(vs);
 236    unlink(tmp_path);
 237}
 238
 239static void *virtio_scsi_hotplug_setup(GString *cmd_line, void *arg)
 240{
 241    g_string_append(cmd_line,
 242                    " -drive id=drv1,if=none,file=null-co://,format=raw");
 243    return arg;
 244}
 245
 246static void *virtio_scsi_setup(GString *cmd_line, void *arg)
 247{
 248    g_string_append(cmd_line,
 249                    " -drive file=blkdebug::null-co://,"
 250                    "if=none,id=dr1,format=raw,file.align=4k "
 251                    "-device scsi-hd,drive=dr1,lun=0,scsi-id=1");
 252    return arg;
 253}
 254
 255static void *virtio_scsi_setup_iothread(GString *cmd_line, void *arg)
 256{
 257    g_string_append(cmd_line,
 258                    " -object iothread,id=thread0"
 259                    " -blockdev driver=null-co,node-name=null0"
 260                    " -device scsi-hd,drive=null0");
 261    return arg;
 262}
 263
 264static void register_virtio_scsi_test(void)
 265{
 266    QOSGraphTestOptions opts = { };
 267
 268    opts.before = virtio_scsi_hotplug_setup;
 269    qos_add_test("hotplug", "virtio-scsi", hotplug, &opts);
 270
 271    opts.before = virtio_scsi_setup;
 272    qos_add_test("unaligned-write-same", "virtio-scsi",
 273                 test_unaligned_write_same, &opts);
 274
 275    opts.before = virtio_scsi_setup_iothread;
 276    opts.edge = (QOSGraphEdgeOptions) {
 277        .extra_device_opts = "iothread=thread0",
 278    };
 279    qos_add_test("iothread-attach-node", "virtio-scsi-pci",
 280                 test_iothread_attach_node, &opts);
 281}
 282
 283libqos_init(register_virtio_scsi_test);
 284