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