2 * QTest testcase for VirtIO 9P
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
12 #include "qemu-common.h"
13 #include "libqos/libqos-pc.h"
14 #include "libqos/libqos-spapr.h"
15 #include "libqos/virtio.h"
16 #include "libqos/virtio-pci.h"
17 #include "standard-headers/linux/virtio_ids.h"
18 #include "standard-headers/linux/virtio_pci.h"
19 #include "hw/9pfs/9p.h"
20 #include "hw/9pfs/9p-synth.h"
22 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
24 static const char mount_tag
[] = "qtest";
32 static QVirtIO9P
*qvirtio_9p_start(const char *driver
)
34 const char *arch
= qtest_get_arch();
35 const char *cmd
= "-fsdev synth,id=fsdev0 "
36 "-device %s,fsdev=fsdev0,mount_tag=%s";
37 QVirtIO9P
*v9p
= g_new0(QVirtIO9P
, 1);
39 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
40 v9p
->qs
= qtest_pc_boot(cmd
, driver
, mount_tag
);
41 } else if (strcmp(arch
, "ppc64") == 0) {
42 v9p
->qs
= qtest_spapr_boot(cmd
, driver
, mount_tag
);
44 g_printerr("virtio-9p tests are only available on x86 or ppc64\n");
51 static void qvirtio_9p_stop(QVirtIO9P
*v9p
)
53 qtest_shutdown(v9p
->qs
);
57 static QVirtIO9P
*qvirtio_9p_pci_start(void)
59 QVirtIO9P
*v9p
= qvirtio_9p_start("virtio-9p-pci");
60 QVirtioPCIDevice
*dev
= qvirtio_pci_device_find(v9p
->qs
->pcibus
,
62 g_assert_nonnull(dev
);
63 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_9P
);
64 v9p
->dev
= (QVirtioDevice
*) dev
;
66 qvirtio_pci_device_enable(dev
);
67 qvirtio_reset(v9p
->dev
);
68 qvirtio_set_acknowledge(v9p
->dev
);
69 qvirtio_set_driver(v9p
->dev
);
71 v9p
->vq
= qvirtqueue_setup(v9p
->dev
, v9p
->qs
->alloc
, 0);
73 qvirtio_set_driver_ok(v9p
->dev
);
78 static void qvirtio_9p_pci_stop(QVirtIO9P
*v9p
)
80 qvirtqueue_cleanup(v9p
->dev
->bus
, v9p
->vq
, v9p
->qs
->alloc
);
81 qvirtio_pci_device_disable(container_of(v9p
->dev
, QVirtioPCIDevice
, vdev
));
82 qvirtio_pci_device_free((QVirtioPCIDevice
*)v9p
->dev
);
86 static void pci_config(QVirtIO9P
*v9p
)
88 size_t tag_len
= qvirtio_config_readw(v9p
->dev
, 0);
92 g_assert_cmpint(tag_len
, ==, strlen(mount_tag
));
94 tag
= g_malloc(tag_len
);
95 for (i
= 0; i
< tag_len
; i
++) {
96 tag
[i
] = qvirtio_config_readb(v9p
->dev
, i
+ 2);
98 g_assert_cmpmem(tag
, tag_len
, mount_tag
, tag_len
);
102 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
110 /* No r_size, it is hardcoded to P9_MAX_SIZE */
116 static void v9fs_memwrite(P9Req
*req
, const void *addr
, size_t len
)
118 memwrite(req
->t_msg
+ req
->t_off
, addr
, len
);
122 static void v9fs_memskip(P9Req
*req
, size_t len
)
127 static void v9fs_memread(P9Req
*req
, void *addr
, size_t len
)
129 memread(req
->r_msg
+ req
->r_off
, addr
, len
);
133 static void v9fs_uint16_write(P9Req
*req
, uint16_t val
)
135 uint16_t le_val
= cpu_to_le16(val
);
137 v9fs_memwrite(req
, &le_val
, 2);
140 static void v9fs_uint16_read(P9Req
*req
, uint16_t *val
)
142 v9fs_memread(req
, val
, 2);
146 static void v9fs_uint32_write(P9Req
*req
, uint32_t val
)
148 uint32_t le_val
= cpu_to_le32(val
);
150 v9fs_memwrite(req
, &le_val
, 4);
153 static void v9fs_uint64_write(P9Req
*req
, uint64_t val
)
155 uint64_t le_val
= cpu_to_le64(val
);
157 v9fs_memwrite(req
, &le_val
, 8);
160 static void v9fs_uint32_read(P9Req
*req
, uint32_t *val
)
162 v9fs_memread(req
, val
, 4);
166 /* len[2] string[len] */
167 static uint16_t v9fs_string_size(const char *string
)
169 size_t len
= strlen(string
);
171 g_assert_cmpint(len
, <=, UINT16_MAX
- 2);
176 static void v9fs_string_write(P9Req
*req
, const char *string
)
178 int len
= strlen(string
);
180 g_assert_cmpint(len
, <=, UINT16_MAX
);
182 v9fs_uint16_write(req
, (uint16_t) len
);
183 v9fs_memwrite(req
, string
, len
);
186 static void v9fs_string_read(P9Req
*req
, uint16_t *len
, char **string
)
190 v9fs_uint16_read(req
, &local_len
);
195 *string
= g_malloc(local_len
);
196 v9fs_memread(req
, *string
, local_len
);
198 v9fs_memskip(req
, local_len
);
208 static P9Req
*v9fs_req_init(QVirtIO9P
*v9p
, uint32_t size
, uint8_t id
,
211 P9Req
*req
= g_new0(P9Req
, 1);
212 uint32_t total_size
= 7; /* 9P header has well-known size of 7 bytes */
215 .tag
= cpu_to_le16(tag
)
218 g_assert_cmpint(total_size
, <=, UINT32_MAX
- size
);
220 hdr
.size
= cpu_to_le32(total_size
);
222 g_assert_cmpint(total_size
, <=, P9_MAX_SIZE
);
225 req
->t_size
= total_size
;
226 req
->t_msg
= guest_alloc(v9p
->qs
->alloc
, req
->t_size
);
227 v9fs_memwrite(req
, &hdr
, 7);
232 static void v9fs_req_send(P9Req
*req
)
234 QVirtIO9P
*v9p
= req
->v9p
;
236 req
->r_msg
= guest_alloc(v9p
->qs
->alloc
, P9_MAX_SIZE
);
237 req
->free_head
= qvirtqueue_add(v9p
->vq
, req
->t_msg
, req
->t_size
, false,
239 qvirtqueue_add(v9p
->vq
, req
->r_msg
, P9_MAX_SIZE
, true, false);
240 qvirtqueue_kick(v9p
->dev
, v9p
->vq
, req
->free_head
);
244 static const char *rmessage_name(uint8_t id
)
247 id
== P9_RLERROR
? "RLERROR" :
248 id
== P9_RVERSION
? "RVERSION" :
249 id
== P9_RATTACH
? "RATTACH" :
250 id
== P9_RWALK
? "RWALK" :
251 id
== P9_RLOPEN
? "RLOPEN" :
252 id
== P9_RWRITE
? "RWRITE" :
253 id
== P9_RFLUSH
? "RFLUSH" :
257 static void v9fs_req_wait_for_reply(P9Req
*req
, uint32_t *len
)
259 QVirtIO9P
*v9p
= req
->v9p
;
261 qvirtio_wait_used_elem(v9p
->dev
, v9p
->vq
, req
->free_head
, len
,
262 QVIRTIO_9P_TIMEOUT_US
);
265 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
269 v9fs_memread(req
, &hdr
, 7);
270 hdr
.size
= ldl_le_p(&hdr
.size
);
271 hdr
.tag
= lduw_le_p(&hdr
.tag
);
273 g_assert_cmpint(hdr
.size
, >=, 7);
274 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
275 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
278 g_printerr("Received response %d (%s) instead of %d (%s)\n",
279 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
281 if (hdr
.id
== P9_RLERROR
) {
283 v9fs_uint32_read(req
, &err
);
284 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
287 g_assert_cmpint(hdr
.id
, ==, id
);
290 static void v9fs_req_free(P9Req
*req
)
292 QVirtIO9P
*v9p
= req
->v9p
;
294 guest_free(v9p
->qs
->alloc
, req
->t_msg
);
295 guest_free(v9p
->qs
->alloc
, req
->r_msg
);
299 /* size[4] Rlerror tag[2] ecode[4] */
300 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
302 v9fs_req_recv(req
, P9_RLERROR
);
303 v9fs_uint32_read(req
, err
);
307 /* size[4] Tversion tag[2] msize[4] version[s] */
308 static P9Req
*v9fs_tversion(QVirtIO9P
*v9p
, uint32_t msize
, const char *version
,
312 uint32_t body_size
= 4;
313 uint16_t string_size
= v9fs_string_size(version
);
315 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
316 body_size
+= string_size
;
317 req
= v9fs_req_init(v9p
, body_size
, P9_TVERSION
, tag
);
319 v9fs_uint32_write(req
, msize
);
320 v9fs_string_write(req
, version
);
325 /* size[4] Rversion tag[2] msize[4] version[s] */
326 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
330 v9fs_req_recv(req
, P9_RVERSION
);
331 v9fs_uint32_read(req
, &msize
);
333 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
335 if (len
|| version
) {
336 v9fs_string_read(req
, len
, version
);
342 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
343 static P9Req
*v9fs_tattach(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t n_uname
,
346 const char *uname
= ""; /* ignored by QEMU */
347 const char *aname
= ""; /* ignored by QEMU */
348 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
, tag
);
350 v9fs_uint32_write(req
, fid
);
351 v9fs_uint32_write(req
, P9_NOFID
);
352 v9fs_string_write(req
, uname
);
353 v9fs_string_write(req
, aname
);
354 v9fs_uint32_write(req
, n_uname
);
359 typedef char v9fs_qid
[13];
361 /* size[4] Rattach tag[2] qid[13] */
362 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
364 v9fs_req_recv(req
, P9_RATTACH
);
366 v9fs_memread(req
, qid
, 13);
371 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
372 static P9Req
*v9fs_twalk(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t newfid
,
373 uint16_t nwname
, char *const wnames
[], uint16_t tag
)
377 uint32_t body_size
= 4 + 4 + 2;
379 for (i
= 0; i
< nwname
; i
++) {
380 uint16_t wname_size
= v9fs_string_size(wnames
[i
]);
382 g_assert_cmpint(body_size
, <=, UINT32_MAX
- wname_size
);
383 body_size
+= wname_size
;
385 req
= v9fs_req_init(v9p
, body_size
, P9_TWALK
, tag
);
386 v9fs_uint32_write(req
, fid
);
387 v9fs_uint32_write(req
, newfid
);
388 v9fs_uint16_write(req
, nwname
);
389 for (i
= 0; i
< nwname
; i
++) {
390 v9fs_string_write(req
, wnames
[i
]);
396 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
397 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
399 uint16_t local_nwqid
;
401 v9fs_req_recv(req
, P9_RWALK
);
402 v9fs_uint16_read(req
, &local_nwqid
);
404 *nwqid
= local_nwqid
;
407 *wqid
= g_malloc(local_nwqid
* 13);
408 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
413 /* size[4] Tlopen tag[2] fid[4] flags[4] */
414 static P9Req
*v9fs_tlopen(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t flags
,
419 req
= v9fs_req_init(v9p
, 4 + 4, P9_TLOPEN
, tag
);
420 v9fs_uint32_write(req
, fid
);
421 v9fs_uint32_write(req
, flags
);
426 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
427 static void v9fs_rlopen(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
429 v9fs_req_recv(req
, P9_RLOPEN
);
431 v9fs_memread(req
, qid
, 13);
433 v9fs_memskip(req
, 13);
436 v9fs_uint32_read(req
, iounit
);
441 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
442 static P9Req
*v9fs_twrite(QVirtIO9P
*v9p
, uint32_t fid
, uint64_t offset
,
443 uint32_t count
, const void *data
, uint16_t tag
)
446 uint32_t body_size
= 4 + 8 + 4;
448 g_assert_cmpint(body_size
, <=, UINT32_MAX
- count
);
450 req
= v9fs_req_init(v9p
, body_size
, P9_TWRITE
, tag
);
451 v9fs_uint32_write(req
, fid
);
452 v9fs_uint64_write(req
, offset
);
453 v9fs_uint32_write(req
, count
);
454 v9fs_memwrite(req
, data
, count
);
459 /* size[4] Rwrite tag[2] count[4] */
460 static void v9fs_rwrite(P9Req
*req
, uint32_t *count
)
462 v9fs_req_recv(req
, P9_RWRITE
);
464 v9fs_uint32_read(req
, count
);
469 /* size[4] Tflush tag[2] oldtag[2] */
470 static P9Req
*v9fs_tflush(QVirtIO9P
*v9p
, uint16_t oldtag
, uint16_t tag
)
474 req
= v9fs_req_init(v9p
, 2, P9_TFLUSH
, tag
);
475 v9fs_uint32_write(req
, oldtag
);
480 /* size[4] Rflush tag[2] */
481 static void v9fs_rflush(P9Req
*req
)
483 v9fs_req_recv(req
, P9_RFLUSH
);
487 static void fs_version(QVirtIO9P
*v9p
)
489 const char *version
= "9P2000.L";
491 char *server_version
;
494 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
, P9_NOTAG
);
495 v9fs_req_wait_for_reply(req
, NULL
);
496 v9fs_rversion(req
, &server_len
, &server_version
);
498 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
500 g_free(server_version
);
503 static void fs_attach(QVirtIO9P
*v9p
)
508 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
509 v9fs_req_wait_for_reply(req
, NULL
);
510 v9fs_rattach(req
, NULL
);
513 static void fs_walk(QVirtIO9P
*v9p
)
515 char *wnames
[P9_MAXWELEM
];
521 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
522 wnames
[i
] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
526 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
, 0);
527 v9fs_req_wait_for_reply(req
, NULL
);
528 v9fs_rwalk(req
, &nwqid
, &wqid
);
530 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
532 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
539 static void fs_walk_no_slash(QVirtIO9P
*v9p
)
541 char *const wnames
[] = { g_strdup(" /") };
546 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
547 v9fs_req_wait_for_reply(req
, NULL
);
548 v9fs_rlerror(req
, &err
);
550 g_assert_cmpint(err
, ==, ENOENT
);
555 static void fs_walk_dotdot(QVirtIO9P
*v9p
)
557 char *const wnames
[] = { g_strdup("..") };
558 v9fs_qid root_qid
, *wqid
;
562 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
563 v9fs_req_wait_for_reply(req
, NULL
);
564 v9fs_rattach(req
, &root_qid
);
566 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
567 v9fs_req_wait_for_reply(req
, NULL
);
568 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
570 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
576 static void fs_lopen(QVirtIO9P
*v9p
)
578 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE
) };
582 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
583 v9fs_req_wait_for_reply(req
, NULL
);
584 v9fs_rwalk(req
, NULL
, NULL
);
586 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
587 v9fs_req_wait_for_reply(req
, NULL
);
588 v9fs_rlopen(req
, NULL
, NULL
);
593 static void fs_write(QVirtIO9P
*v9p
)
595 static const uint32_t write_count
= P9_MAX_SIZE
/ 2;
596 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE
) };
597 char *buf
= g_malloc0(write_count
);
602 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
603 v9fs_req_wait_for_reply(req
, NULL
);
604 v9fs_rwalk(req
, NULL
, NULL
);
606 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
607 v9fs_req_wait_for_reply(req
, NULL
);
608 v9fs_rlopen(req
, NULL
, NULL
);
610 req
= v9fs_twrite(v9p
, 1, 0, write_count
, buf
, 0);
611 v9fs_req_wait_for_reply(req
, NULL
);
612 v9fs_rwrite(req
, &count
);
613 g_assert_cmpint(count
, ==, write_count
);
619 static void fs_flush_success(QVirtIO9P
*v9p
)
621 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
622 P9Req
*req
, *flush_req
;
624 uint8_t should_block
;
627 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
628 v9fs_req_wait_for_reply(req
, NULL
);
629 v9fs_rwalk(req
, NULL
, NULL
);
631 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
632 v9fs_req_wait_for_reply(req
, NULL
);
633 v9fs_rlopen(req
, NULL
, NULL
);
635 /* This will cause the 9p server to try to write data to the backend,
636 * until the write request gets cancelled.
639 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
641 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
643 /* The write request is supposed to be flushed: the server should just
644 * mark the write request as used and reply to the flush request.
646 v9fs_req_wait_for_reply(req
, &reply_len
);
647 g_assert_cmpint(reply_len
, ==, 0);
649 v9fs_rflush(flush_req
);
654 static void fs_flush_ignored(QVirtIO9P
*v9p
)
656 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
657 P9Req
*req
, *flush_req
;
659 uint8_t should_block
;
662 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
663 v9fs_req_wait_for_reply(req
, NULL
);
664 v9fs_rwalk(req
, NULL
, NULL
);
666 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
667 v9fs_req_wait_for_reply(req
, NULL
);
668 v9fs_rlopen(req
, NULL
, NULL
);
670 /* This will cause the write request to complete right away, before it
671 * could be actually cancelled.
674 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
676 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
678 /* The write request is supposed to complete. The server should
679 * reply to the write request and the flush request.
681 v9fs_req_wait_for_reply(req
, NULL
);
682 v9fs_rwrite(req
, &count
);
683 g_assert_cmpint(count
, ==, sizeof(should_block
));
684 v9fs_rflush(flush_req
);
689 typedef void (*v9fs_test_fn
)(QVirtIO9P
*v9p
);
691 static void v9fs_run_pci_test(gconstpointer data
)
693 v9fs_test_fn fn
= data
;
694 QVirtIO9P
*v9p
= qvirtio_9p_pci_start();
699 qvirtio_9p_pci_stop(v9p
);
702 static void v9fs_qtest_pci_add(const char *path
, v9fs_test_fn fn
)
704 qtest_add_data_func(path
, fn
, v9fs_run_pci_test
);
707 int main(int argc
, char **argv
)
709 g_test_init(&argc
, &argv
, NULL
);
710 v9fs_qtest_pci_add("/virtio/9p/pci/nop", NULL
);
711 v9fs_qtest_pci_add("/virtio/9p/pci/config", pci_config
);
712 v9fs_qtest_pci_add("/virtio/9p/pci/fs/version/basic", fs_version
);
713 v9fs_qtest_pci_add("/virtio/9p/pci/fs/attach/basic", fs_attach
);
714 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/basic", fs_walk
);
715 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/no_slash", fs_walk_no_slash
);
716 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/dotdot_from_root",
718 v9fs_qtest_pci_add("/virtio/9p/pci/fs/lopen/basic", fs_lopen
);
719 v9fs_qtest_pci_add("/virtio/9p/pci/fs/write/basic", fs_write
);
720 v9fs_qtest_pci_add("/virtio/9p/pci/fs/flush/success", fs_flush_success
);
721 v9fs_qtest_pci_add("/virtio/9p/pci/fs/flush/ignored", fs_flush_ignored
);