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");
47 global_qtest
= v9p
->qs
->qts
;
52 static void qvirtio_9p_stop(QVirtIO9P
*v9p
)
54 qtest_shutdown(v9p
->qs
);
58 static QVirtIO9P
*qvirtio_9p_pci_start(void)
60 QVirtIO9P
*v9p
= qvirtio_9p_start("virtio-9p-pci");
61 QVirtioPCIDevice
*dev
= qvirtio_pci_device_find(v9p
->qs
->pcibus
,
63 g_assert_nonnull(dev
);
64 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_9P
);
65 v9p
->dev
= (QVirtioDevice
*) dev
;
67 qvirtio_pci_device_enable(dev
);
68 qvirtio_start_device(v9p
->dev
);
70 v9p
->vq
= qvirtqueue_setup(v9p
->dev
, v9p
->qs
->alloc
, 0);
72 qvirtio_set_driver_ok(v9p
->dev
);
77 static void qvirtio_9p_pci_stop(QVirtIO9P
*v9p
)
79 qvirtqueue_cleanup(v9p
->dev
->bus
, v9p
->vq
, v9p
->qs
->alloc
);
80 qvirtio_pci_device_disable(container_of(v9p
->dev
, QVirtioPCIDevice
, vdev
));
81 qvirtio_pci_device_free((QVirtioPCIDevice
*)v9p
->dev
);
85 static void pci_config(QVirtIO9P
*v9p
)
87 size_t tag_len
= qvirtio_config_readw(v9p
->dev
, 0);
91 g_assert_cmpint(tag_len
, ==, strlen(mount_tag
));
93 tag
= g_malloc(tag_len
);
94 for (i
= 0; i
< tag_len
; i
++) {
95 tag
[i
] = qvirtio_config_readb(v9p
->dev
, i
+ 2);
97 g_assert_cmpmem(tag
, tag_len
, mount_tag
, tag_len
);
101 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
109 /* No r_size, it is hardcoded to P9_MAX_SIZE */
115 static void v9fs_memwrite(P9Req
*req
, const void *addr
, size_t len
)
117 memwrite(req
->t_msg
+ req
->t_off
, addr
, len
);
121 static void v9fs_memskip(P9Req
*req
, size_t len
)
126 static void v9fs_memread(P9Req
*req
, void *addr
, size_t len
)
128 memread(req
->r_msg
+ req
->r_off
, addr
, len
);
132 static void v9fs_uint16_write(P9Req
*req
, uint16_t val
)
134 uint16_t le_val
= cpu_to_le16(val
);
136 v9fs_memwrite(req
, &le_val
, 2);
139 static void v9fs_uint16_read(P9Req
*req
, uint16_t *val
)
141 v9fs_memread(req
, val
, 2);
145 static void v9fs_uint32_write(P9Req
*req
, uint32_t val
)
147 uint32_t le_val
= cpu_to_le32(val
);
149 v9fs_memwrite(req
, &le_val
, 4);
152 static void v9fs_uint64_write(P9Req
*req
, uint64_t val
)
154 uint64_t le_val
= cpu_to_le64(val
);
156 v9fs_memwrite(req
, &le_val
, 8);
159 static void v9fs_uint32_read(P9Req
*req
, uint32_t *val
)
161 v9fs_memread(req
, val
, 4);
165 /* len[2] string[len] */
166 static uint16_t v9fs_string_size(const char *string
)
168 size_t len
= strlen(string
);
170 g_assert_cmpint(len
, <=, UINT16_MAX
- 2);
175 static void v9fs_string_write(P9Req
*req
, const char *string
)
177 int len
= strlen(string
);
179 g_assert_cmpint(len
, <=, UINT16_MAX
);
181 v9fs_uint16_write(req
, (uint16_t) len
);
182 v9fs_memwrite(req
, string
, len
);
185 static void v9fs_string_read(P9Req
*req
, uint16_t *len
, char **string
)
189 v9fs_uint16_read(req
, &local_len
);
194 *string
= g_malloc(local_len
);
195 v9fs_memread(req
, *string
, local_len
);
197 v9fs_memskip(req
, local_len
);
207 static P9Req
*v9fs_req_init(QVirtIO9P
*v9p
, uint32_t size
, uint8_t id
,
210 P9Req
*req
= g_new0(P9Req
, 1);
211 uint32_t total_size
= 7; /* 9P header has well-known size of 7 bytes */
214 .tag
= cpu_to_le16(tag
)
217 g_assert_cmpint(total_size
, <=, UINT32_MAX
- size
);
219 hdr
.size
= cpu_to_le32(total_size
);
221 g_assert_cmpint(total_size
, <=, P9_MAX_SIZE
);
224 req
->t_size
= total_size
;
225 req
->t_msg
= guest_alloc(v9p
->qs
->alloc
, req
->t_size
);
226 v9fs_memwrite(req
, &hdr
, 7);
231 static void v9fs_req_send(P9Req
*req
)
233 QVirtIO9P
*v9p
= req
->v9p
;
235 req
->r_msg
= guest_alloc(v9p
->qs
->alloc
, P9_MAX_SIZE
);
236 req
->free_head
= qvirtqueue_add(v9p
->vq
, req
->t_msg
, req
->t_size
, false,
238 qvirtqueue_add(v9p
->vq
, req
->r_msg
, P9_MAX_SIZE
, true, false);
239 qvirtqueue_kick(v9p
->dev
, v9p
->vq
, req
->free_head
);
243 static const char *rmessage_name(uint8_t id
)
246 id
== P9_RLERROR
? "RLERROR" :
247 id
== P9_RVERSION
? "RVERSION" :
248 id
== P9_RATTACH
? "RATTACH" :
249 id
== P9_RWALK
? "RWALK" :
250 id
== P9_RLOPEN
? "RLOPEN" :
251 id
== P9_RWRITE
? "RWRITE" :
252 id
== P9_RFLUSH
? "RFLUSH" :
256 static void v9fs_req_wait_for_reply(P9Req
*req
, uint32_t *len
)
258 QVirtIO9P
*v9p
= req
->v9p
;
260 qvirtio_wait_used_elem(v9p
->dev
, v9p
->vq
, req
->free_head
, len
,
261 QVIRTIO_9P_TIMEOUT_US
);
264 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
268 v9fs_memread(req
, &hdr
, 7);
269 hdr
.size
= ldl_le_p(&hdr
.size
);
270 hdr
.tag
= lduw_le_p(&hdr
.tag
);
272 g_assert_cmpint(hdr
.size
, >=, 7);
273 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
274 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
277 g_printerr("Received response %d (%s) instead of %d (%s)\n",
278 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
280 if (hdr
.id
== P9_RLERROR
) {
282 v9fs_uint32_read(req
, &err
);
283 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
286 g_assert_cmpint(hdr
.id
, ==, id
);
289 static void v9fs_req_free(P9Req
*req
)
291 QVirtIO9P
*v9p
= req
->v9p
;
293 guest_free(v9p
->qs
->alloc
, req
->t_msg
);
294 guest_free(v9p
->qs
->alloc
, req
->r_msg
);
298 /* size[4] Rlerror tag[2] ecode[4] */
299 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
301 v9fs_req_recv(req
, P9_RLERROR
);
302 v9fs_uint32_read(req
, err
);
306 /* size[4] Tversion tag[2] msize[4] version[s] */
307 static P9Req
*v9fs_tversion(QVirtIO9P
*v9p
, uint32_t msize
, const char *version
,
311 uint32_t body_size
= 4;
312 uint16_t string_size
= v9fs_string_size(version
);
314 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
315 body_size
+= string_size
;
316 req
= v9fs_req_init(v9p
, body_size
, P9_TVERSION
, tag
);
318 v9fs_uint32_write(req
, msize
);
319 v9fs_string_write(req
, version
);
324 /* size[4] Rversion tag[2] msize[4] version[s] */
325 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
329 v9fs_req_recv(req
, P9_RVERSION
);
330 v9fs_uint32_read(req
, &msize
);
332 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
334 if (len
|| version
) {
335 v9fs_string_read(req
, len
, version
);
341 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
342 static P9Req
*v9fs_tattach(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t n_uname
,
345 const char *uname
= ""; /* ignored by QEMU */
346 const char *aname
= ""; /* ignored by QEMU */
347 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
, tag
);
349 v9fs_uint32_write(req
, fid
);
350 v9fs_uint32_write(req
, P9_NOFID
);
351 v9fs_string_write(req
, uname
);
352 v9fs_string_write(req
, aname
);
353 v9fs_uint32_write(req
, n_uname
);
358 typedef char v9fs_qid
[13];
360 /* size[4] Rattach tag[2] qid[13] */
361 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
363 v9fs_req_recv(req
, P9_RATTACH
);
365 v9fs_memread(req
, qid
, 13);
370 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
371 static P9Req
*v9fs_twalk(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t newfid
,
372 uint16_t nwname
, char *const wnames
[], uint16_t tag
)
376 uint32_t body_size
= 4 + 4 + 2;
378 for (i
= 0; i
< nwname
; i
++) {
379 uint16_t wname_size
= v9fs_string_size(wnames
[i
]);
381 g_assert_cmpint(body_size
, <=, UINT32_MAX
- wname_size
);
382 body_size
+= wname_size
;
384 req
= v9fs_req_init(v9p
, body_size
, P9_TWALK
, tag
);
385 v9fs_uint32_write(req
, fid
);
386 v9fs_uint32_write(req
, newfid
);
387 v9fs_uint16_write(req
, nwname
);
388 for (i
= 0; i
< nwname
; i
++) {
389 v9fs_string_write(req
, wnames
[i
]);
395 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
396 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
398 uint16_t local_nwqid
;
400 v9fs_req_recv(req
, P9_RWALK
);
401 v9fs_uint16_read(req
, &local_nwqid
);
403 *nwqid
= local_nwqid
;
406 *wqid
= g_malloc(local_nwqid
* 13);
407 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
412 /* size[4] Tlopen tag[2] fid[4] flags[4] */
413 static P9Req
*v9fs_tlopen(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t flags
,
418 req
= v9fs_req_init(v9p
, 4 + 4, P9_TLOPEN
, tag
);
419 v9fs_uint32_write(req
, fid
);
420 v9fs_uint32_write(req
, flags
);
425 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
426 static void v9fs_rlopen(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
428 v9fs_req_recv(req
, P9_RLOPEN
);
430 v9fs_memread(req
, qid
, 13);
432 v9fs_memskip(req
, 13);
435 v9fs_uint32_read(req
, iounit
);
440 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
441 static P9Req
*v9fs_twrite(QVirtIO9P
*v9p
, uint32_t fid
, uint64_t offset
,
442 uint32_t count
, const void *data
, uint16_t tag
)
445 uint32_t body_size
= 4 + 8 + 4;
447 g_assert_cmpint(body_size
, <=, UINT32_MAX
- count
);
449 req
= v9fs_req_init(v9p
, body_size
, P9_TWRITE
, tag
);
450 v9fs_uint32_write(req
, fid
);
451 v9fs_uint64_write(req
, offset
);
452 v9fs_uint32_write(req
, count
);
453 v9fs_memwrite(req
, data
, count
);
458 /* size[4] Rwrite tag[2] count[4] */
459 static void v9fs_rwrite(P9Req
*req
, uint32_t *count
)
461 v9fs_req_recv(req
, P9_RWRITE
);
463 v9fs_uint32_read(req
, count
);
468 /* size[4] Tflush tag[2] oldtag[2] */
469 static P9Req
*v9fs_tflush(QVirtIO9P
*v9p
, uint16_t oldtag
, uint16_t tag
)
473 req
= v9fs_req_init(v9p
, 2, P9_TFLUSH
, tag
);
474 v9fs_uint32_write(req
, oldtag
);
479 /* size[4] Rflush tag[2] */
480 static void v9fs_rflush(P9Req
*req
)
482 v9fs_req_recv(req
, P9_RFLUSH
);
486 static void fs_version(QVirtIO9P
*v9p
)
488 const char *version
= "9P2000.L";
490 char *server_version
;
493 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
, P9_NOTAG
);
494 v9fs_req_wait_for_reply(req
, NULL
);
495 v9fs_rversion(req
, &server_len
, &server_version
);
497 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
499 g_free(server_version
);
502 static void fs_attach(QVirtIO9P
*v9p
)
507 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
508 v9fs_req_wait_for_reply(req
, NULL
);
509 v9fs_rattach(req
, NULL
);
512 static void fs_walk(QVirtIO9P
*v9p
)
514 char *wnames
[P9_MAXWELEM
];
520 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
521 wnames
[i
] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
525 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
, 0);
526 v9fs_req_wait_for_reply(req
, NULL
);
527 v9fs_rwalk(req
, &nwqid
, &wqid
);
529 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
531 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
538 static void fs_walk_no_slash(QVirtIO9P
*v9p
)
540 char *const wnames
[] = { g_strdup(" /") };
545 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
546 v9fs_req_wait_for_reply(req
, NULL
);
547 v9fs_rlerror(req
, &err
);
549 g_assert_cmpint(err
, ==, ENOENT
);
554 static void fs_walk_dotdot(QVirtIO9P
*v9p
)
556 char *const wnames
[] = { g_strdup("..") };
557 v9fs_qid root_qid
, *wqid
;
561 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
562 v9fs_req_wait_for_reply(req
, NULL
);
563 v9fs_rattach(req
, &root_qid
);
565 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
566 v9fs_req_wait_for_reply(req
, NULL
);
567 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
569 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
575 static void fs_lopen(QVirtIO9P
*v9p
)
577 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE
) };
581 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
582 v9fs_req_wait_for_reply(req
, NULL
);
583 v9fs_rwalk(req
, NULL
, NULL
);
585 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
586 v9fs_req_wait_for_reply(req
, NULL
);
587 v9fs_rlopen(req
, NULL
, NULL
);
592 static void fs_write(QVirtIO9P
*v9p
)
594 static const uint32_t write_count
= P9_MAX_SIZE
/ 2;
595 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE
) };
596 char *buf
= g_malloc0(write_count
);
601 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
602 v9fs_req_wait_for_reply(req
, NULL
);
603 v9fs_rwalk(req
, NULL
, NULL
);
605 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
606 v9fs_req_wait_for_reply(req
, NULL
);
607 v9fs_rlopen(req
, NULL
, NULL
);
609 req
= v9fs_twrite(v9p
, 1, 0, write_count
, buf
, 0);
610 v9fs_req_wait_for_reply(req
, NULL
);
611 v9fs_rwrite(req
, &count
);
612 g_assert_cmpint(count
, ==, write_count
);
618 static void fs_flush_success(QVirtIO9P
*v9p
)
620 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
621 P9Req
*req
, *flush_req
;
623 uint8_t should_block
;
626 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
627 v9fs_req_wait_for_reply(req
, NULL
);
628 v9fs_rwalk(req
, NULL
, NULL
);
630 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
631 v9fs_req_wait_for_reply(req
, NULL
);
632 v9fs_rlopen(req
, NULL
, NULL
);
634 /* This will cause the 9p server to try to write data to the backend,
635 * until the write request gets cancelled.
638 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
640 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
642 /* The write request is supposed to be flushed: the server should just
643 * mark the write request as used and reply to the flush request.
645 v9fs_req_wait_for_reply(req
, &reply_len
);
646 g_assert_cmpint(reply_len
, ==, 0);
648 v9fs_rflush(flush_req
);
653 static void fs_flush_ignored(QVirtIO9P
*v9p
)
655 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
656 P9Req
*req
, *flush_req
;
658 uint8_t should_block
;
661 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
662 v9fs_req_wait_for_reply(req
, NULL
);
663 v9fs_rwalk(req
, NULL
, NULL
);
665 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
666 v9fs_req_wait_for_reply(req
, NULL
);
667 v9fs_rlopen(req
, NULL
, NULL
);
669 /* This will cause the write request to complete right away, before it
670 * could be actually cancelled.
673 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
675 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
677 /* The write request is supposed to complete. The server should
678 * reply to the write request and the flush request.
680 v9fs_req_wait_for_reply(req
, NULL
);
681 v9fs_rwrite(req
, &count
);
682 g_assert_cmpint(count
, ==, sizeof(should_block
));
683 v9fs_rflush(flush_req
);
688 typedef void (*v9fs_test_fn
)(QVirtIO9P
*v9p
);
690 static void v9fs_run_pci_test(gconstpointer data
)
692 v9fs_test_fn fn
= data
;
693 QVirtIO9P
*v9p
= qvirtio_9p_pci_start();
698 qvirtio_9p_pci_stop(v9p
);
701 static void v9fs_qtest_pci_add(const char *path
, v9fs_test_fn fn
)
703 qtest_add_data_func(path
, fn
, v9fs_run_pci_test
);
706 int main(int argc
, char **argv
)
708 g_test_init(&argc
, &argv
, NULL
);
709 v9fs_qtest_pci_add("/virtio/9p/pci/nop", NULL
);
710 v9fs_qtest_pci_add("/virtio/9p/pci/config", pci_config
);
711 v9fs_qtest_pci_add("/virtio/9p/pci/fs/version/basic", fs_version
);
712 v9fs_qtest_pci_add("/virtio/9p/pci/fs/attach/basic", fs_attach
);
713 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/basic", fs_walk
);
714 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/no_slash", fs_walk_no_slash
);
715 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/dotdot_from_root",
717 v9fs_qtest_pci_add("/virtio/9p/pci/fs/lopen/basic", fs_lopen
);
718 v9fs_qtest_pci_add("/virtio/9p/pci/fs/write/basic", fs_write
);
719 v9fs_qtest_pci_add("/virtio/9p/pci/fs/flush/success", fs_flush_success
);
720 v9fs_qtest_pci_add("/virtio/9p/pci/fs/flush/ignored", fs_flush_ignored
);