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);
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 t_size = 7 + size; /* 9P header has well-known size of 7 bytes */
214 .size = cpu_to_le32(t_size),
216 .tag = cpu_to_le16(tag)
219 g_assert_cmpint(t_size, <=, P9_MAX_SIZE);
222 req->t_size = t_size;
223 req->t_msg = guest_alloc(v9p->qs->alloc, req->t_size);
224 v9fs_memwrite(req, &hdr, 7);
229 static void v9fs_req_send(P9Req *req)
231 QVirtIO9P *v9p = req->v9p;
233 req->r_msg = guest_alloc(v9p->qs->alloc, P9_MAX_SIZE);
234 req->free_head = qvirtqueue_add(v9p->vq, req->t_msg, req->t_size, false,
236 qvirtqueue_add(v9p->vq, req->r_msg, P9_MAX_SIZE, true, false);
237 qvirtqueue_kick(v9p->dev, v9p->vq, req->free_head);
241 static const char *rmessage_name(uint8_t id)
244 id == P9_RLERROR ? "RLERROR" :
245 id == P9_RVERSION ? "RVERSION" :
246 id == P9_RATTACH ? "RATTACH" :
247 id == P9_RWALK ? "RWALK" :
248 id == P9_RLOPEN ? "RLOPEN" :
249 id == P9_RWRITE ? "RWRITE" :
250 id == P9_RFLUSH ? "RFLUSH" :
254 static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len)
256 QVirtIO9P *v9p = req->v9p;
258 qvirtio_wait_used_elem(v9p->dev, v9p->vq, req->free_head, len,
259 QVIRTIO_9P_TIMEOUT_US);
262 static void v9fs_req_recv(P9Req *req, uint8_t id)
266 v9fs_memread(req, &hdr, 7);
267 hdr.size = ldl_le_p(&hdr.size);
268 hdr.tag = lduw_le_p(&hdr.tag);
270 g_assert_cmpint(hdr.size, >=, 7);
271 g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE);
272 g_assert_cmpint(hdr.tag, ==, req->tag);
275 g_printerr("Received response %d (%s) instead of %d (%s)\n",
276 hdr.id, rmessage_name(hdr.id), id, rmessage_name(id));
278 if (hdr.id == P9_RLERROR) {
280 v9fs_uint32_read(req, &err);
281 g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err));
284 g_assert_cmpint(hdr.id, ==, id);
287 static void v9fs_req_free(P9Req *req)
289 QVirtIO9P *v9p = req->v9p;
291 guest_free(v9p->qs->alloc, req->t_msg);
292 guest_free(v9p->qs->alloc, req->r_msg);
296 /* size[4] Rlerror tag[2] ecode[4] */
297 static void v9fs_rlerror(P9Req *req, uint32_t *err)
299 v9fs_req_recv(req, P9_RLERROR);
300 v9fs_uint32_read(req, err);
304 /* size[4] Tversion tag[2] msize[4] version[s] */
305 static P9Req *v9fs_tversion(QVirtIO9P *v9p, uint32_t msize, const char *version,
308 P9Req *req = v9fs_req_init(v9p, 4 + v9fs_string_size(version), P9_TVERSION,
311 v9fs_uint32_write(req, msize);
312 v9fs_string_write(req, version);
317 /* size[4] Rversion tag[2] msize[4] version[s] */
318 static void v9fs_rversion(P9Req *req, uint16_t *len, char **version)
322 v9fs_req_recv(req, P9_RVERSION);
323 v9fs_uint32_read(req, &msize);
325 g_assert_cmpint(msize, ==, P9_MAX_SIZE);
327 if (len || version) {
328 v9fs_string_read(req, len, version);
334 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
335 static P9Req *v9fs_tattach(QVirtIO9P *v9p, uint32_t fid, uint32_t n_uname,
338 const char *uname = ""; /* ignored by QEMU */
339 const char *aname = ""; /* ignored by QEMU */
340 P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag);
342 v9fs_uint32_write(req, fid);
343 v9fs_uint32_write(req, P9_NOFID);
344 v9fs_string_write(req, uname);
345 v9fs_string_write(req, aname);
346 v9fs_uint32_write(req, n_uname);
351 typedef char v9fs_qid[13];
353 /* size[4] Rattach tag[2] qid[13] */
354 static void v9fs_rattach(P9Req *req, v9fs_qid *qid)
356 v9fs_req_recv(req, P9_RATTACH);
358 v9fs_memread(req, qid, 13);
363 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
364 static P9Req *v9fs_twalk(QVirtIO9P *v9p, uint32_t fid, uint32_t newfid,
365 uint16_t nwname, char *const wnames[], uint16_t tag)
369 uint32_t size = 4 + 4 + 2;
371 for (i = 0; i < nwname; i++) {
372 size += v9fs_string_size(wnames[i]);
374 req = v9fs_req_init(v9p, size, P9_TWALK, tag);
375 v9fs_uint32_write(req, fid);
376 v9fs_uint32_write(req, newfid);
377 v9fs_uint16_write(req, nwname);
378 for (i = 0; i < nwname; i++) {
379 v9fs_string_write(req, wnames[i]);
385 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
386 static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid)
388 uint16_t local_nwqid;
390 v9fs_req_recv(req, P9_RWALK);
391 v9fs_uint16_read(req, &local_nwqid);
393 *nwqid = local_nwqid;
396 *wqid = g_malloc(local_nwqid * 13);
397 v9fs_memread(req, *wqid, local_nwqid * 13);
402 /* size[4] Tlopen tag[2] fid[4] flags[4] */
403 static P9Req *v9fs_tlopen(QVirtIO9P *v9p, uint32_t fid, uint32_t flags,
408 req = v9fs_req_init(v9p, 4 + 4, P9_TLOPEN, tag);
409 v9fs_uint32_write(req, fid);
410 v9fs_uint32_write(req, flags);
415 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
416 static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
418 v9fs_req_recv(req, P9_RLOPEN);
420 v9fs_memread(req, qid, 13);
422 v9fs_memskip(req, 13);
425 v9fs_uint32_read(req, iounit);
430 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
431 static P9Req *v9fs_twrite(QVirtIO9P *v9p, uint32_t fid, uint64_t offset,
432 uint32_t count, const void *data, uint16_t tag)
435 uint32_t body_size = 4 + 8 + 4;
437 g_assert_cmpint(body_size, <=, UINT32_MAX - count);
439 req = v9fs_req_init(v9p, body_size, P9_TWRITE, tag);
440 v9fs_uint32_write(req, fid);
441 v9fs_uint64_write(req, offset);
442 v9fs_uint32_write(req, count);
443 v9fs_memwrite(req, data, count);
448 /* size[4] Rwrite tag[2] count[4] */
449 static void v9fs_rwrite(P9Req *req, uint32_t *count)
451 v9fs_req_recv(req, P9_RWRITE);
453 v9fs_uint32_read(req, count);
458 /* size[4] Tflush tag[2] oldtag[2] */
459 static P9Req *v9fs_tflush(QVirtIO9P *v9p, uint16_t oldtag, uint16_t tag)
463 req = v9fs_req_init(v9p, 2, P9_TFLUSH, tag);
464 v9fs_uint32_write(req, oldtag);
469 /* size[4] Rflush tag[2] */
470 static void v9fs_rflush(P9Req *req)
472 v9fs_req_recv(req, P9_RFLUSH);
476 static void fs_version(QVirtIO9P *v9p)
478 const char *version = "9P2000.L";
480 char *server_version;
483 req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG);
484 v9fs_req_wait_for_reply(req, NULL);
485 v9fs_rversion(req, &server_len, &server_version);
487 g_assert_cmpmem(server_version, server_len, version, strlen(version));
489 g_free(server_version);
492 static void fs_attach(QVirtIO9P *v9p)
497 req = v9fs_tattach(v9p, 0, getuid(), 0);
498 v9fs_req_wait_for_reply(req, NULL);
499 v9fs_rattach(req, NULL);
502 static void fs_walk(QVirtIO9P *v9p)
504 char *wnames[P9_MAXWELEM];
510 for (i = 0; i < P9_MAXWELEM; i++) {
511 wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
515 req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0);
516 v9fs_req_wait_for_reply(req, NULL);
517 v9fs_rwalk(req, &nwqid, &wqid);
519 g_assert_cmpint(nwqid, ==, P9_MAXWELEM);
521 for (i = 0; i < P9_MAXWELEM; i++) {
528 static void fs_walk_no_slash(QVirtIO9P *v9p)
530 char *const wnames[] = { g_strdup(" /") };
535 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
536 v9fs_req_wait_for_reply(req, NULL);
537 v9fs_rlerror(req, &err);
539 g_assert_cmpint(err, ==, ENOENT);
544 static void fs_walk_dotdot(QVirtIO9P *v9p)
546 char *const wnames[] = { g_strdup("..") };
547 v9fs_qid root_qid, *wqid;
551 req = v9fs_tattach(v9p, 0, getuid(), 0);
552 v9fs_req_wait_for_reply(req, NULL);
553 v9fs_rattach(req, &root_qid);
555 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
556 v9fs_req_wait_for_reply(req, NULL);
557 v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */
559 g_assert_cmpmem(&root_qid, 13, wqid[0], 13);
565 static void fs_lopen(QVirtIO9P *v9p)
567 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) };
571 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
572 v9fs_req_wait_for_reply(req, NULL);
573 v9fs_rwalk(req, NULL, NULL);
575 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
576 v9fs_req_wait_for_reply(req, NULL);
577 v9fs_rlopen(req, NULL, NULL);
582 static void fs_write(QVirtIO9P *v9p)
584 static const uint32_t write_count = P9_MAX_SIZE / 2;
585 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) };
586 char *buf = g_malloc0(write_count);
591 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
592 v9fs_req_wait_for_reply(req, NULL);
593 v9fs_rwalk(req, NULL, NULL);
595 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
596 v9fs_req_wait_for_reply(req, NULL);
597 v9fs_rlopen(req, NULL, NULL);
599 req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0);
600 v9fs_req_wait_for_reply(req, NULL);
601 v9fs_rwrite(req, &count);
602 g_assert_cmpint(count, ==, write_count);
608 static void fs_flush_success(QVirtIO9P *v9p)
610 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
611 P9Req *req, *flush_req;
613 uint8_t should_block;
616 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
617 v9fs_req_wait_for_reply(req, NULL);
618 v9fs_rwalk(req, NULL, NULL);
620 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
621 v9fs_req_wait_for_reply(req, NULL);
622 v9fs_rlopen(req, NULL, NULL);
624 /* This will cause the 9p server to try to write data to the backend,
625 * until the write request gets cancelled.
628 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
630 flush_req = v9fs_tflush(v9p, req->tag, 1);
632 /* The write request is supposed to be flushed: the server should just
633 * mark the write request as used and reply to the flush request.
635 v9fs_req_wait_for_reply(req, &reply_len);
636 g_assert_cmpint(reply_len, ==, 0);
638 v9fs_rflush(flush_req);
643 static void fs_flush_ignored(QVirtIO9P *v9p)
645 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
646 P9Req *req, *flush_req;
648 uint8_t should_block;
651 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
652 v9fs_req_wait_for_reply(req, NULL);
653 v9fs_rwalk(req, NULL, NULL);
655 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
656 v9fs_req_wait_for_reply(req, NULL);
657 v9fs_rlopen(req, NULL, NULL);
659 /* This will cause the write request to complete right away, before it
660 * could be actually cancelled.
663 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
665 flush_req = v9fs_tflush(v9p, req->tag, 1);
667 /* The write request is supposed to complete. The server should
668 * reply to the write request and the flush request.
670 v9fs_req_wait_for_reply(req, NULL);
671 v9fs_rwrite(req, &count);
672 g_assert_cmpint(count, ==, sizeof(should_block));
673 v9fs_rflush(flush_req);
678 typedef void (*v9fs_test_fn)(QVirtIO9P *v9p);
680 static void v9fs_run_pci_test(gconstpointer data)
682 v9fs_test_fn fn = data;
683 QVirtIO9P *v9p = qvirtio_9p_pci_start();
688 qvirtio_9p_pci_stop(v9p);
691 static void v9fs_qtest_pci_add(const char *path, v9fs_test_fn fn)
693 qtest_add_data_func(path, fn, v9fs_run_pci_test);
696 int main(int argc, char **argv)
698 g_test_init(&argc, &argv, NULL);
699 v9fs_qtest_pci_add("/virtio/9p/pci/nop", NULL);
700 v9fs_qtest_pci_add("/virtio/9p/pci/config", pci_config);
701 v9fs_qtest_pci_add("/virtio/9p/pci/fs/version/basic", fs_version);
702 v9fs_qtest_pci_add("/virtio/9p/pci/fs/attach/basic", fs_attach);
703 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/basic", fs_walk);
704 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/no_slash", fs_walk_no_slash);
705 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/dotdot_from_root",
707 v9fs_qtest_pci_add("/virtio/9p/pci/fs/lopen/basic", fs_lopen);
708 v9fs_qtest_pci_add("/virtio/9p/pci/fs/write/basic", fs_write);
709 v9fs_qtest_pci_add("/virtio/9p/pci/fs/flush/success", fs_flush_success);
710 v9fs_qtest_pci_add("/virtio/9p/pci/fs/flush/ignored", fs_flush_ignored);