]> git.proxmox.com Git - mirror_qemu.git/blob - tests/virtio-blk-test.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / tests / virtio-blk-test.c
1 /*
2 * QTest testcase for VirtIO Block Device
3 *
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 * Copyright (c) 2014 Marc MarĂ­
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/bswap.h"
14 #include "qemu/module.h"
15 #include "standard-headers/linux/virtio_blk.h"
16 #include "standard-headers/linux/virtio_pci.h"
17 #include "libqos/qgraph.h"
18 #include "libqos/virtio-blk.h"
19
20 /* TODO actually test the results and get rid of this */
21 #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
22
23 #define TEST_IMAGE_SIZE (64 * 1024 * 1024)
24 #define QVIRTIO_BLK_TIMEOUT_US (30 * 1000 * 1000)
25 #define PCI_SLOT_HP 0x06
26
27 typedef struct QVirtioBlkReq {
28 uint32_t type;
29 uint32_t ioprio;
30 uint64_t sector;
31 char *data;
32 uint8_t status;
33 } QVirtioBlkReq;
34
35
36 #ifdef HOST_WORDS_BIGENDIAN
37 const bool host_is_big_endian = true;
38 #else
39 const bool host_is_big_endian; /* false */
40 #endif
41
42 static void drive_destroy(void *path)
43 {
44 unlink(path);
45 g_free(path);
46 qos_invalidate_command_line();
47 }
48
49 static char *drive_create(void)
50 {
51 int fd, ret;
52 char *t_path = g_strdup("/tmp/qtest.XXXXXX");
53
54 /* Create a temporary raw image */
55 fd = mkstemp(t_path);
56 g_assert_cmpint(fd, >=, 0);
57 ret = ftruncate(fd, TEST_IMAGE_SIZE);
58 g_assert_cmpint(ret, ==, 0);
59 close(fd);
60
61 g_test_queue_destroy(drive_destroy, t_path);
62 return t_path;
63 }
64
65 static inline void virtio_blk_fix_request(QVirtioDevice *d, QVirtioBlkReq *req)
66 {
67 if (qvirtio_is_big_endian(d) != host_is_big_endian) {
68 req->type = bswap32(req->type);
69 req->ioprio = bswap32(req->ioprio);
70 req->sector = bswap64(req->sector);
71 }
72 }
73
74
75 static inline void virtio_blk_fix_dwz_hdr(QVirtioDevice *d,
76 struct virtio_blk_discard_write_zeroes *dwz_hdr)
77 {
78 if (qvirtio_is_big_endian(d) != host_is_big_endian) {
79 dwz_hdr->sector = bswap64(dwz_hdr->sector);
80 dwz_hdr->num_sectors = bswap32(dwz_hdr->num_sectors);
81 dwz_hdr->flags = bswap32(dwz_hdr->flags);
82 }
83 }
84
85 static uint64_t virtio_blk_request(QGuestAllocator *alloc, QVirtioDevice *d,
86 QVirtioBlkReq *req, uint64_t data_size)
87 {
88 uint64_t addr;
89 uint8_t status = 0xFF;
90
91 switch (req->type) {
92 case VIRTIO_BLK_T_IN:
93 case VIRTIO_BLK_T_OUT:
94 g_assert_cmpuint(data_size % 512, ==, 0);
95 break;
96 case VIRTIO_BLK_T_DISCARD:
97 case VIRTIO_BLK_T_WRITE_ZEROES:
98 g_assert_cmpuint(data_size %
99 sizeof(struct virtio_blk_discard_write_zeroes), ==, 0);
100 break;
101 default:
102 g_assert_cmpuint(data_size, ==, 0);
103 }
104
105 addr = guest_alloc(alloc, sizeof(*req) + data_size);
106
107 virtio_blk_fix_request(d, req);
108
109 memwrite(addr, req, 16);
110 memwrite(addr + 16, req->data, data_size);
111 memwrite(addr + 16 + data_size, &status, sizeof(status));
112
113 return addr;
114 }
115
116 static void test_basic(QVirtioDevice *dev, QGuestAllocator *alloc,
117 QVirtQueue *vq)
118 {
119 QVirtioBlkReq req;
120 uint64_t req_addr;
121 uint64_t capacity;
122 uint32_t features;
123 uint32_t free_head;
124 uint8_t status;
125 char *data;
126 QTestState *qts = global_qtest;
127
128 capacity = qvirtio_config_readq(dev, 0);
129
130 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
131
132 features = qvirtio_get_features(dev);
133 features = features & ~(QVIRTIO_F_BAD_FEATURE |
134 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
135 (1u << VIRTIO_RING_F_EVENT_IDX) |
136 (1u << VIRTIO_BLK_F_SCSI));
137 qvirtio_set_features(dev, features);
138
139 qvirtio_set_driver_ok(dev);
140
141 /* Write and read with 3 descriptor layout */
142 /* Write request */
143 req.type = VIRTIO_BLK_T_OUT;
144 req.ioprio = 1;
145 req.sector = 0;
146 req.data = g_malloc0(512);
147 strcpy(req.data, "TEST");
148
149 req_addr = virtio_blk_request(alloc, dev, &req, 512);
150
151 g_free(req.data);
152
153 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
154 qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
155 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
156
157 qvirtqueue_kick(qts, dev, vq, free_head);
158
159 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
160 QVIRTIO_BLK_TIMEOUT_US);
161 status = readb(req_addr + 528);
162 g_assert_cmpint(status, ==, 0);
163
164 guest_free(alloc, req_addr);
165
166 /* Read request */
167 req.type = VIRTIO_BLK_T_IN;
168 req.ioprio = 1;
169 req.sector = 0;
170 req.data = g_malloc0(512);
171
172 req_addr = virtio_blk_request(alloc, dev, &req, 512);
173
174 g_free(req.data);
175
176 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
177 qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
178 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
179
180 qvirtqueue_kick(qts, dev, vq, free_head);
181
182 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
183 QVIRTIO_BLK_TIMEOUT_US);
184 status = readb(req_addr + 528);
185 g_assert_cmpint(status, ==, 0);
186
187 data = g_malloc0(512);
188 memread(req_addr + 16, data, 512);
189 g_assert_cmpstr(data, ==, "TEST");
190 g_free(data);
191
192 guest_free(alloc, req_addr);
193
194 if (features & (1u << VIRTIO_BLK_F_WRITE_ZEROES)) {
195 struct virtio_blk_discard_write_zeroes dwz_hdr;
196 void *expected;
197
198 /*
199 * WRITE_ZEROES request on the same sector of previous test where
200 * we wrote "TEST".
201 */
202 req.type = VIRTIO_BLK_T_WRITE_ZEROES;
203 req.data = (char *) &dwz_hdr;
204 dwz_hdr.sector = 0;
205 dwz_hdr.num_sectors = 1;
206 dwz_hdr.flags = 0;
207
208 virtio_blk_fix_dwz_hdr(dev, &dwz_hdr);
209
210 req_addr = virtio_blk_request(alloc, dev, &req, sizeof(dwz_hdr));
211
212 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
213 qvirtqueue_add(qts, vq, req_addr + 16, sizeof(dwz_hdr), false, true);
214 qvirtqueue_add(qts, vq, req_addr + 16 + sizeof(dwz_hdr), 1, true,
215 false);
216
217 qvirtqueue_kick(qts, dev, vq, free_head);
218
219 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
220 QVIRTIO_BLK_TIMEOUT_US);
221 status = readb(req_addr + 16 + sizeof(dwz_hdr));
222 g_assert_cmpint(status, ==, 0);
223
224 guest_free(alloc, req_addr);
225
226 /* Read request to check if the sector contains all zeroes */
227 req.type = VIRTIO_BLK_T_IN;
228 req.ioprio = 1;
229 req.sector = 0;
230 req.data = g_malloc0(512);
231
232 req_addr = virtio_blk_request(alloc, dev, &req, 512);
233
234 g_free(req.data);
235
236 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
237 qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
238 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
239
240 qvirtqueue_kick(qts, dev, vq, free_head);
241
242 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
243 QVIRTIO_BLK_TIMEOUT_US);
244 status = readb(req_addr + 528);
245 g_assert_cmpint(status, ==, 0);
246
247 data = g_malloc(512);
248 expected = g_malloc0(512);
249 memread(req_addr + 16, data, 512);
250 g_assert_cmpmem(data, 512, expected, 512);
251 g_free(expected);
252 g_free(data);
253
254 guest_free(alloc, req_addr);
255 }
256
257 if (features & (1u << VIRTIO_BLK_F_DISCARD)) {
258 struct virtio_blk_discard_write_zeroes dwz_hdr;
259
260 req.type = VIRTIO_BLK_T_DISCARD;
261 req.data = (char *) &dwz_hdr;
262 dwz_hdr.sector = 0;
263 dwz_hdr.num_sectors = 1;
264 dwz_hdr.flags = 0;
265
266 virtio_blk_fix_dwz_hdr(dev, &dwz_hdr);
267
268 req_addr = virtio_blk_request(alloc, dev, &req, sizeof(dwz_hdr));
269
270 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
271 qvirtqueue_add(qts, vq, req_addr + 16, sizeof(dwz_hdr), false, true);
272 qvirtqueue_add(qts, vq, req_addr + 16 + sizeof(dwz_hdr), 1, true, false);
273
274 qvirtqueue_kick(qts, dev, vq, free_head);
275
276 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
277 QVIRTIO_BLK_TIMEOUT_US);
278 status = readb(req_addr + 16 + sizeof(dwz_hdr));
279 g_assert_cmpint(status, ==, 0);
280
281 guest_free(alloc, req_addr);
282 }
283
284 if (features & (1u << VIRTIO_F_ANY_LAYOUT)) {
285 /* Write and read with 2 descriptor layout */
286 /* Write request */
287 req.type = VIRTIO_BLK_T_OUT;
288 req.ioprio = 1;
289 req.sector = 1;
290 req.data = g_malloc0(512);
291 strcpy(req.data, "TEST");
292
293 req_addr = virtio_blk_request(alloc, dev, &req, 512);
294
295 g_free(req.data);
296
297 free_head = qvirtqueue_add(qts, vq, req_addr, 528, false, true);
298 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
299 qvirtqueue_kick(qts, dev, vq, free_head);
300
301 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
302 QVIRTIO_BLK_TIMEOUT_US);
303 status = readb(req_addr + 528);
304 g_assert_cmpint(status, ==, 0);
305
306 guest_free(alloc, req_addr);
307
308 /* Read request */
309 req.type = VIRTIO_BLK_T_IN;
310 req.ioprio = 1;
311 req.sector = 1;
312 req.data = g_malloc0(512);
313
314 req_addr = virtio_blk_request(alloc, dev, &req, 512);
315
316 g_free(req.data);
317
318 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
319 qvirtqueue_add(qts, vq, req_addr + 16, 513, true, false);
320
321 qvirtqueue_kick(qts, dev, vq, free_head);
322
323 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
324 QVIRTIO_BLK_TIMEOUT_US);
325 status = readb(req_addr + 528);
326 g_assert_cmpint(status, ==, 0);
327
328 data = g_malloc0(512);
329 memread(req_addr + 16, data, 512);
330 g_assert_cmpstr(data, ==, "TEST");
331 g_free(data);
332
333 guest_free(alloc, req_addr);
334 }
335 }
336
337 static void basic(void *obj, void *data, QGuestAllocator *t_alloc)
338 {
339 QVirtioBlk *blk_if = obj;
340 QVirtQueue *vq;
341 vq = qvirtqueue_setup(blk_if->vdev, t_alloc, 0);
342 test_basic(blk_if->vdev, t_alloc, vq);
343 qvirtqueue_cleanup(blk_if->vdev->bus, vq, t_alloc);
344
345 }
346
347 static void indirect(void *obj, void *u_data, QGuestAllocator *t_alloc)
348 {
349 QVirtQueue *vq;
350 QVirtioBlk *blk_if = obj;
351 QVirtioDevice *dev = blk_if->vdev;
352 QVirtioBlkReq req;
353 QVRingIndirectDesc *indirect;
354 uint64_t req_addr;
355 uint64_t capacity;
356 uint32_t features;
357 uint32_t free_head;
358 uint8_t status;
359 char *data;
360 QTestState *qts = global_qtest;
361
362 capacity = qvirtio_config_readq(dev, 0);
363 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
364
365 features = qvirtio_get_features(dev);
366 g_assert_cmphex(features & (1u << VIRTIO_RING_F_INDIRECT_DESC), !=, 0);
367 features = features & ~(QVIRTIO_F_BAD_FEATURE |
368 (1u << VIRTIO_RING_F_EVENT_IDX) |
369 (1u << VIRTIO_BLK_F_SCSI));
370 qvirtio_set_features(dev, features);
371
372 vq = qvirtqueue_setup(dev, t_alloc, 0);
373 qvirtio_set_driver_ok(dev);
374
375 /* Write request */
376 req.type = VIRTIO_BLK_T_OUT;
377 req.ioprio = 1;
378 req.sector = 0;
379 req.data = g_malloc0(512);
380 strcpy(req.data, "TEST");
381
382 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
383
384 g_free(req.data);
385
386 indirect = qvring_indirect_desc_setup(qts, dev, t_alloc, 2);
387 qvring_indirect_desc_add(qts, indirect, req_addr, 528, false);
388 qvring_indirect_desc_add(qts, indirect, req_addr + 528, 1, true);
389 free_head = qvirtqueue_add_indirect(qts, vq, indirect);
390 qvirtqueue_kick(qts, dev, vq, free_head);
391
392 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
393 QVIRTIO_BLK_TIMEOUT_US);
394 status = readb(req_addr + 528);
395 g_assert_cmpint(status, ==, 0);
396
397 g_free(indirect);
398 guest_free(t_alloc, req_addr);
399
400 /* Read request */
401 req.type = VIRTIO_BLK_T_IN;
402 req.ioprio = 1;
403 req.sector = 0;
404 req.data = g_malloc0(512);
405 strcpy(req.data, "TEST");
406
407 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
408
409 g_free(req.data);
410
411 indirect = qvring_indirect_desc_setup(qts, dev, t_alloc, 2);
412 qvring_indirect_desc_add(qts, indirect, req_addr, 16, false);
413 qvring_indirect_desc_add(qts, indirect, req_addr + 16, 513, true);
414 free_head = qvirtqueue_add_indirect(qts, vq, indirect);
415 qvirtqueue_kick(qts, dev, vq, free_head);
416
417 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
418 QVIRTIO_BLK_TIMEOUT_US);
419 status = readb(req_addr + 528);
420 g_assert_cmpint(status, ==, 0);
421
422 data = g_malloc0(512);
423 memread(req_addr + 16, data, 512);
424 g_assert_cmpstr(data, ==, "TEST");
425 g_free(data);
426
427 g_free(indirect);
428 guest_free(t_alloc, req_addr);
429 qvirtqueue_cleanup(dev->bus, vq, t_alloc);
430 }
431
432 static void config(void *obj, void *data, QGuestAllocator *t_alloc)
433 {
434 QVirtioBlk *blk_if = obj;
435 QVirtioDevice *dev = blk_if->vdev;
436 int n_size = TEST_IMAGE_SIZE / 2;
437 uint64_t capacity;
438
439 capacity = qvirtio_config_readq(dev, 0);
440 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
441
442 qvirtio_set_driver_ok(dev);
443
444 qmp_discard_response("{ 'execute': 'block_resize', "
445 " 'arguments': { 'device': 'drive0', "
446 " 'size': %d } }", n_size);
447 qvirtio_wait_config_isr(dev, QVIRTIO_BLK_TIMEOUT_US);
448
449 capacity = qvirtio_config_readq(dev, 0);
450 g_assert_cmpint(capacity, ==, n_size / 512);
451 }
452
453 static void msix(void *obj, void *u_data, QGuestAllocator *t_alloc)
454 {
455 QVirtQueue *vq;
456 QVirtioBlkPCI *blk = obj;
457 QVirtioPCIDevice *pdev = &blk->pci_vdev;
458 QVirtioDevice *dev = &pdev->vdev;
459 QVirtioBlkReq req;
460 int n_size = TEST_IMAGE_SIZE / 2;
461 uint64_t req_addr;
462 uint64_t capacity;
463 uint32_t features;
464 uint32_t free_head;
465 uint8_t status;
466 char *data;
467 QOSGraphObject *blk_object = obj;
468 QPCIDevice *pci_dev = blk_object->get_driver(blk_object, "pci-device");
469 QTestState *qts = global_qtest;
470
471 if (qpci_check_buggy_msi(pci_dev)) {
472 return;
473 }
474
475 qpci_msix_enable(pdev->pdev);
476 qvirtio_pci_set_msix_configuration_vector(pdev, t_alloc, 0);
477
478 capacity = qvirtio_config_readq(dev, 0);
479 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
480
481 features = qvirtio_get_features(dev);
482 features = features & ~(QVIRTIO_F_BAD_FEATURE |
483 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
484 (1u << VIRTIO_RING_F_EVENT_IDX) |
485 (1u << VIRTIO_BLK_F_SCSI));
486 qvirtio_set_features(dev, features);
487
488 vq = qvirtqueue_setup(dev, t_alloc, 0);
489 qvirtqueue_pci_msix_setup(pdev, (QVirtQueuePCI *)vq, t_alloc, 1);
490
491 qvirtio_set_driver_ok(dev);
492
493 qmp_discard_response("{ 'execute': 'block_resize', "
494 " 'arguments': { 'device': 'drive0', "
495 " 'size': %d } }", n_size);
496
497 qvirtio_wait_config_isr(dev, QVIRTIO_BLK_TIMEOUT_US);
498
499 capacity = qvirtio_config_readq(dev, 0);
500 g_assert_cmpint(capacity, ==, n_size / 512);
501
502 /* Write request */
503 req.type = VIRTIO_BLK_T_OUT;
504 req.ioprio = 1;
505 req.sector = 0;
506 req.data = g_malloc0(512);
507 strcpy(req.data, "TEST");
508
509 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
510
511 g_free(req.data);
512
513 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
514 qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
515 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
516 qvirtqueue_kick(qts, dev, vq, free_head);
517
518 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
519 QVIRTIO_BLK_TIMEOUT_US);
520
521 status = readb(req_addr + 528);
522 g_assert_cmpint(status, ==, 0);
523
524 guest_free(t_alloc, req_addr);
525
526 /* Read request */
527 req.type = VIRTIO_BLK_T_IN;
528 req.ioprio = 1;
529 req.sector = 0;
530 req.data = g_malloc0(512);
531
532 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
533
534 g_free(req.data);
535
536 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
537 qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
538 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
539
540 qvirtqueue_kick(qts, dev, vq, free_head);
541
542
543 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
544 QVIRTIO_BLK_TIMEOUT_US);
545
546 status = readb(req_addr + 528);
547 g_assert_cmpint(status, ==, 0);
548
549 data = g_malloc0(512);
550 memread(req_addr + 16, data, 512);
551 g_assert_cmpstr(data, ==, "TEST");
552 g_free(data);
553
554 guest_free(t_alloc, req_addr);
555
556 /* End test */
557 qpci_msix_disable(pdev->pdev);
558 qvirtqueue_cleanup(dev->bus, vq, t_alloc);
559 }
560
561 static void idx(void *obj, void *u_data, QGuestAllocator *t_alloc)
562 {
563 QVirtQueue *vq;
564 QVirtioBlkPCI *blk = obj;
565 QVirtioPCIDevice *pdev = &blk->pci_vdev;
566 QVirtioDevice *dev = &pdev->vdev;
567 QVirtioBlkReq req;
568 uint64_t req_addr;
569 uint64_t capacity;
570 uint32_t features;
571 uint32_t free_head;
572 uint32_t write_head;
573 uint32_t desc_idx;
574 uint8_t status;
575 char *data;
576 QOSGraphObject *blk_object = obj;
577 QPCIDevice *pci_dev = blk_object->get_driver(blk_object, "pci-device");
578 QTestState *qts = global_qtest;
579
580 if (qpci_check_buggy_msi(pci_dev)) {
581 return;
582 }
583
584 qpci_msix_enable(pdev->pdev);
585 qvirtio_pci_set_msix_configuration_vector(pdev, t_alloc, 0);
586
587 capacity = qvirtio_config_readq(dev, 0);
588 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
589
590 features = qvirtio_get_features(dev);
591 features = features & ~(QVIRTIO_F_BAD_FEATURE |
592 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
593 (1u << VIRTIO_F_NOTIFY_ON_EMPTY) |
594 (1u << VIRTIO_BLK_F_SCSI));
595 qvirtio_set_features(dev, features);
596
597 vq = qvirtqueue_setup(dev, t_alloc, 0);
598 qvirtqueue_pci_msix_setup(pdev, (QVirtQueuePCI *)vq, t_alloc, 1);
599
600 qvirtio_set_driver_ok(dev);
601
602 /* Write request */
603 req.type = VIRTIO_BLK_T_OUT;
604 req.ioprio = 1;
605 req.sector = 0;
606 req.data = g_malloc0(512);
607 strcpy(req.data, "TEST");
608
609 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
610
611 g_free(req.data);
612
613 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
614 qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
615 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
616 qvirtqueue_kick(qts, dev, vq, free_head);
617
618 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
619 QVIRTIO_BLK_TIMEOUT_US);
620
621 /* Write request */
622 req.type = VIRTIO_BLK_T_OUT;
623 req.ioprio = 1;
624 req.sector = 1;
625 req.data = g_malloc0(512);
626 strcpy(req.data, "TEST");
627
628 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
629
630 g_free(req.data);
631
632 /* Notify after processing the third request */
633 qvirtqueue_set_used_event(qts, vq, 2);
634 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
635 qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
636 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
637 qvirtqueue_kick(qts, dev, vq, free_head);
638 write_head = free_head;
639
640 /* No notification expected */
641 status = qvirtio_wait_status_byte_no_isr(qts, dev,
642 vq, req_addr + 528,
643 QVIRTIO_BLK_TIMEOUT_US);
644 g_assert_cmpint(status, ==, 0);
645
646 guest_free(t_alloc, req_addr);
647
648 /* Read request */
649 req.type = VIRTIO_BLK_T_IN;
650 req.ioprio = 1;
651 req.sector = 1;
652 req.data = g_malloc0(512);
653
654 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
655
656 g_free(req.data);
657
658 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
659 qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
660 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
661
662 qvirtqueue_kick(qts, dev, vq, free_head);
663
664 /* We get just one notification for both requests */
665 qvirtio_wait_used_elem(qts, dev, vq, write_head, NULL,
666 QVIRTIO_BLK_TIMEOUT_US);
667 g_assert(qvirtqueue_get_buf(qts, vq, &desc_idx, NULL));
668 g_assert_cmpint(desc_idx, ==, free_head);
669
670 status = readb(req_addr + 528);
671 g_assert_cmpint(status, ==, 0);
672
673 data = g_malloc0(512);
674 memread(req_addr + 16, data, 512);
675 g_assert_cmpstr(data, ==, "TEST");
676 g_free(data);
677
678 guest_free(t_alloc, req_addr);
679
680 /* End test */
681 qpci_msix_disable(pdev->pdev);
682
683 qvirtqueue_cleanup(dev->bus, vq, t_alloc);
684 }
685
686 static void pci_hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
687 {
688 QVirtioPCIDevice *dev1 = obj;
689 QVirtioPCIDevice *dev;
690 QTestState *qts = dev1->pdev->bus->qts;
691
692 /* plug secondary disk */
693 qtest_qmp_device_add(qts, "virtio-blk-pci", "drv1",
694 "{'addr': %s, 'drive': 'drive1'}",
695 stringify(PCI_SLOT_HP) ".0");
696
697 dev = virtio_pci_new(dev1->pdev->bus,
698 &(QPCIAddress) { .devfn = QPCI_DEVFN(PCI_SLOT_HP, 0) });
699 g_assert_nonnull(dev);
700 g_assert_cmpint(dev->vdev.device_type, ==, VIRTIO_ID_BLOCK);
701 qvirtio_pci_device_disable(dev);
702 qos_object_destroy((QOSGraphObject *)dev);
703
704 /* unplug secondary disk */
705 qpci_unplug_acpi_device_test(qts, "drv1", PCI_SLOT_HP);
706 }
707
708 /*
709 * Check that setting the vring addr on a non-existent virtqueue does
710 * not crash.
711 */
712 static void test_nonexistent_virtqueue(void *obj, void *data,
713 QGuestAllocator *t_alloc)
714 {
715 QVirtioBlkPCI *blk = obj;
716 QVirtioPCIDevice *pdev = &blk->pci_vdev;
717 QPCIBar bar0;
718 QPCIDevice *dev;
719
720 dev = qpci_device_find(pdev->pdev->bus, QPCI_DEVFN(4, 0));
721 g_assert(dev != NULL);
722 qpci_device_enable(dev);
723
724 bar0 = qpci_iomap(dev, 0, NULL);
725
726 qpci_io_writeb(dev, bar0, VIRTIO_PCI_QUEUE_SEL, 2);
727 qpci_io_writel(dev, bar0, VIRTIO_PCI_QUEUE_PFN, 1);
728
729
730 g_free(dev);
731 }
732
733 static void resize(void *obj, void *data, QGuestAllocator *t_alloc)
734 {
735 QVirtioBlk *blk_if = obj;
736 QVirtioDevice *dev = blk_if->vdev;
737 int n_size = TEST_IMAGE_SIZE / 2;
738 uint64_t capacity;
739 QVirtQueue *vq;
740
741 vq = qvirtqueue_setup(dev, t_alloc, 0);
742
743 test_basic(dev, t_alloc, vq);
744
745 qmp_discard_response("{ 'execute': 'block_resize', "
746 " 'arguments': { 'device': 'drive0', "
747 " 'size': %d } }", n_size);
748
749 qvirtio_wait_queue_isr(dev, vq, QVIRTIO_BLK_TIMEOUT_US);
750
751 capacity = qvirtio_config_readq(dev, 0);
752 g_assert_cmpint(capacity, ==, n_size / 512);
753
754 qvirtqueue_cleanup(dev->bus, vq, t_alloc);
755
756 }
757
758 static void *virtio_blk_test_setup(GString *cmd_line, void *arg)
759 {
760 char *tmp_path = drive_create();
761
762 g_string_append_printf(cmd_line,
763 " -drive if=none,id=drive0,file=%s,"
764 "format=raw,auto-read-only=off "
765 "-drive if=none,id=drive1,file=null-co://,"
766 "file.read-zeroes=on,format=raw ",
767 tmp_path);
768
769 return arg;
770 }
771
772 static void register_virtio_blk_test(void)
773 {
774 QOSGraphTestOptions opts = {
775 .before = virtio_blk_test_setup,
776 };
777
778 qos_add_test("indirect", "virtio-blk", indirect, &opts);
779 qos_add_test("config", "virtio-blk", config, &opts);
780 qos_add_test("basic", "virtio-blk", basic, &opts);
781 qos_add_test("resize", "virtio-blk", resize, &opts);
782
783 /* tests just for virtio-blk-pci */
784 qos_add_test("msix", "virtio-blk-pci", msix, &opts);
785 qos_add_test("idx", "virtio-blk-pci", idx, &opts);
786 qos_add_test("nxvirtq", "virtio-blk-pci",
787 test_nonexistent_virtqueue, &opts);
788 qos_add_test("hotplug", "virtio-blk-pci", pci_hotplug, &opts);
789 }
790
791 libqos_init(register_virtio_blk_test);