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/module.h"
13 #include "hw/9pfs/9p.h"
14 #include "hw/9pfs/9p-synth.h"
15 #include "libqos/virtio-9p.h"
16 #include "libqos/qgraph.h"
18 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
19 static QGuestAllocator
*alloc
;
21 static void pci_config(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
25 size_t tag_len
= qvirtio_config_readw(v9p
->vdev
, 0);
29 g_assert_cmpint(tag_len
, ==, strlen(MOUNT_TAG
));
31 tag
= g_malloc(tag_len
);
32 for (i
= 0; i
< tag_len
; i
++) {
33 tag
[i
] = qvirtio_config_readb(v9p
->vdev
, i
+ 2);
35 g_assert_cmpmem(tag
, tag_len
, MOUNT_TAG
, tag_len
);
39 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
47 /* No r_size, it is hardcoded to P9_MAX_SIZE */
53 static void v9fs_memwrite(P9Req
*req
, const void *addr
, size_t len
)
55 memwrite(req
->t_msg
+ req
->t_off
, addr
, len
);
59 static void v9fs_memskip(P9Req
*req
, size_t len
)
64 static void v9fs_memread(P9Req
*req
, void *addr
, size_t len
)
66 memread(req
->r_msg
+ req
->r_off
, addr
, len
);
70 static void v9fs_uint16_write(P9Req
*req
, uint16_t val
)
72 uint16_t le_val
= cpu_to_le16(val
);
74 v9fs_memwrite(req
, &le_val
, 2);
77 static void v9fs_uint16_read(P9Req
*req
, uint16_t *val
)
79 v9fs_memread(req
, val
, 2);
83 static void v9fs_uint32_write(P9Req
*req
, uint32_t val
)
85 uint32_t le_val
= cpu_to_le32(val
);
87 v9fs_memwrite(req
, &le_val
, 4);
90 static void v9fs_uint64_write(P9Req
*req
, uint64_t val
)
92 uint64_t le_val
= cpu_to_le64(val
);
94 v9fs_memwrite(req
, &le_val
, 8);
97 static void v9fs_uint32_read(P9Req
*req
, uint32_t *val
)
99 v9fs_memread(req
, val
, 4);
103 /* len[2] string[len] */
104 static uint16_t v9fs_string_size(const char *string
)
106 size_t len
= strlen(string
);
108 g_assert_cmpint(len
, <=, UINT16_MAX
- 2);
113 static void v9fs_string_write(P9Req
*req
, const char *string
)
115 int len
= strlen(string
);
117 g_assert_cmpint(len
, <=, UINT16_MAX
);
119 v9fs_uint16_write(req
, (uint16_t) len
);
120 v9fs_memwrite(req
, string
, len
);
123 static void v9fs_string_read(P9Req
*req
, uint16_t *len
, char **string
)
127 v9fs_uint16_read(req
, &local_len
);
132 *string
= g_malloc(local_len
);
133 v9fs_memread(req
, *string
, local_len
);
135 v9fs_memskip(req
, local_len
);
145 static P9Req
*v9fs_req_init(QVirtio9P
*v9p
, uint32_t size
, uint8_t id
,
148 P9Req
*req
= g_new0(P9Req
, 1);
149 uint32_t total_size
= 7; /* 9P header has well-known size of 7 bytes */
152 .tag
= cpu_to_le16(tag
)
155 g_assert_cmpint(total_size
, <=, UINT32_MAX
- size
);
157 hdr
.size
= cpu_to_le32(total_size
);
159 g_assert_cmpint(total_size
, <=, P9_MAX_SIZE
);
162 req
->t_size
= total_size
;
163 req
->t_msg
= guest_alloc(alloc
, req
->t_size
);
164 v9fs_memwrite(req
, &hdr
, 7);
169 static void v9fs_req_send(P9Req
*req
)
171 QVirtio9P
*v9p
= req
->v9p
;
173 req
->r_msg
= guest_alloc(alloc
, P9_MAX_SIZE
);
174 req
->free_head
= qvirtqueue_add(v9p
->vq
, req
->t_msg
, req
->t_size
, false,
176 qvirtqueue_add(v9p
->vq
, req
->r_msg
, P9_MAX_SIZE
, true, false);
177 qvirtqueue_kick(v9p
->vdev
, v9p
->vq
, req
->free_head
);
181 static const char *rmessage_name(uint8_t id
)
184 id
== P9_RLERROR
? "RLERROR" :
185 id
== P9_RVERSION
? "RVERSION" :
186 id
== P9_RATTACH
? "RATTACH" :
187 id
== P9_RWALK
? "RWALK" :
188 id
== P9_RLOPEN
? "RLOPEN" :
189 id
== P9_RWRITE
? "RWRITE" :
190 id
== P9_RFLUSH
? "RFLUSH" :
194 static void v9fs_req_wait_for_reply(P9Req
*req
, uint32_t *len
)
196 QVirtio9P
*v9p
= req
->v9p
;
198 qvirtio_wait_used_elem(v9p
->vdev
, v9p
->vq
, req
->free_head
, len
,
199 QVIRTIO_9P_TIMEOUT_US
);
202 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
206 v9fs_memread(req
, &hdr
, 7);
207 hdr
.size
= ldl_le_p(&hdr
.size
);
208 hdr
.tag
= lduw_le_p(&hdr
.tag
);
210 g_assert_cmpint(hdr
.size
, >=, 7);
211 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
212 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
215 g_printerr("Received response %d (%s) instead of %d (%s)\n",
216 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
218 if (hdr
.id
== P9_RLERROR
) {
220 v9fs_uint32_read(req
, &err
);
221 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
224 g_assert_cmpint(hdr
.id
, ==, id
);
227 static void v9fs_req_free(P9Req
*req
)
229 guest_free(alloc
, req
->t_msg
);
230 guest_free(alloc
, req
->r_msg
);
234 /* size[4] Rlerror tag[2] ecode[4] */
235 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
237 v9fs_req_recv(req
, P9_RLERROR
);
238 v9fs_uint32_read(req
, err
);
242 /* size[4] Tversion tag[2] msize[4] version[s] */
243 static P9Req
*v9fs_tversion(QVirtio9P
*v9p
, uint32_t msize
, const char *version
,
247 uint32_t body_size
= 4;
248 uint16_t string_size
= v9fs_string_size(version
);
250 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
251 body_size
+= string_size
;
252 req
= v9fs_req_init(v9p
, body_size
, P9_TVERSION
, tag
);
254 v9fs_uint32_write(req
, msize
);
255 v9fs_string_write(req
, version
);
260 /* size[4] Rversion tag[2] msize[4] version[s] */
261 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
265 v9fs_req_recv(req
, P9_RVERSION
);
266 v9fs_uint32_read(req
, &msize
);
268 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
270 if (len
|| version
) {
271 v9fs_string_read(req
, len
, version
);
277 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
278 static P9Req
*v9fs_tattach(QVirtio9P
*v9p
, uint32_t fid
, uint32_t n_uname
,
281 const char *uname
= ""; /* ignored by QEMU */
282 const char *aname
= ""; /* ignored by QEMU */
283 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
, tag
);
285 v9fs_uint32_write(req
, fid
);
286 v9fs_uint32_write(req
, P9_NOFID
);
287 v9fs_string_write(req
, uname
);
288 v9fs_string_write(req
, aname
);
289 v9fs_uint32_write(req
, n_uname
);
294 typedef char v9fs_qid
[13];
296 /* size[4] Rattach tag[2] qid[13] */
297 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
299 v9fs_req_recv(req
, P9_RATTACH
);
301 v9fs_memread(req
, qid
, 13);
306 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
307 static P9Req
*v9fs_twalk(QVirtio9P
*v9p
, uint32_t fid
, uint32_t newfid
,
308 uint16_t nwname
, char *const wnames
[], uint16_t tag
)
312 uint32_t body_size
= 4 + 4 + 2;
314 for (i
= 0; i
< nwname
; i
++) {
315 uint16_t wname_size
= v9fs_string_size(wnames
[i
]);
317 g_assert_cmpint(body_size
, <=, UINT32_MAX
- wname_size
);
318 body_size
+= wname_size
;
320 req
= v9fs_req_init(v9p
, body_size
, P9_TWALK
, tag
);
321 v9fs_uint32_write(req
, fid
);
322 v9fs_uint32_write(req
, newfid
);
323 v9fs_uint16_write(req
, nwname
);
324 for (i
= 0; i
< nwname
; i
++) {
325 v9fs_string_write(req
, wnames
[i
]);
331 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
332 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
334 uint16_t local_nwqid
;
336 v9fs_req_recv(req
, P9_RWALK
);
337 v9fs_uint16_read(req
, &local_nwqid
);
339 *nwqid
= local_nwqid
;
342 *wqid
= g_malloc(local_nwqid
* 13);
343 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
348 /* size[4] Tlopen tag[2] fid[4] flags[4] */
349 static P9Req
*v9fs_tlopen(QVirtio9P
*v9p
, uint32_t fid
, uint32_t flags
,
354 req
= v9fs_req_init(v9p
, 4 + 4, P9_TLOPEN
, tag
);
355 v9fs_uint32_write(req
, fid
);
356 v9fs_uint32_write(req
, flags
);
361 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
362 static void v9fs_rlopen(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
364 v9fs_req_recv(req
, P9_RLOPEN
);
366 v9fs_memread(req
, qid
, 13);
368 v9fs_memskip(req
, 13);
371 v9fs_uint32_read(req
, iounit
);
376 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
377 static P9Req
*v9fs_twrite(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
378 uint32_t count
, const void *data
, uint16_t tag
)
381 uint32_t body_size
= 4 + 8 + 4;
383 g_assert_cmpint(body_size
, <=, UINT32_MAX
- count
);
385 req
= v9fs_req_init(v9p
, body_size
, P9_TWRITE
, tag
);
386 v9fs_uint32_write(req
, fid
);
387 v9fs_uint64_write(req
, offset
);
388 v9fs_uint32_write(req
, count
);
389 v9fs_memwrite(req
, data
, count
);
394 /* size[4] Rwrite tag[2] count[4] */
395 static void v9fs_rwrite(P9Req
*req
, uint32_t *count
)
397 v9fs_req_recv(req
, P9_RWRITE
);
399 v9fs_uint32_read(req
, count
);
404 /* size[4] Tflush tag[2] oldtag[2] */
405 static P9Req
*v9fs_tflush(QVirtio9P
*v9p
, uint16_t oldtag
, uint16_t tag
)
409 req
= v9fs_req_init(v9p
, 2, P9_TFLUSH
, tag
);
410 v9fs_uint32_write(req
, oldtag
);
415 /* size[4] Rflush tag[2] */
416 static void v9fs_rflush(P9Req
*req
)
418 v9fs_req_recv(req
, P9_RFLUSH
);
422 static void fs_version(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
424 QVirtio9P
*v9p
= obj
;
426 const char *version
= "9P2000.L";
428 char *server_version
;
431 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
, P9_NOTAG
);
432 v9fs_req_wait_for_reply(req
, NULL
);
433 v9fs_rversion(req
, &server_len
, &server_version
);
435 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
437 g_free(server_version
);
440 static void fs_attach(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
442 QVirtio9P
*v9p
= obj
;
446 fs_version(v9p
, NULL
, t_alloc
);
447 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
448 v9fs_req_wait_for_reply(req
, NULL
);
449 v9fs_rattach(req
, NULL
);
452 static void fs_walk(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
454 QVirtio9P
*v9p
= obj
;
456 char *wnames
[P9_MAXWELEM
];
462 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
463 wnames
[i
] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
466 fs_attach(v9p
, NULL
, t_alloc
);
467 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
, 0);
468 v9fs_req_wait_for_reply(req
, NULL
);
469 v9fs_rwalk(req
, &nwqid
, &wqid
);
471 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
473 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
480 static void fs_walk_no_slash(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
482 QVirtio9P
*v9p
= obj
;
484 char *const wnames
[] = { g_strdup(" /") };
488 fs_attach(v9p
, NULL
, t_alloc
);
489 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
490 v9fs_req_wait_for_reply(req
, NULL
);
491 v9fs_rlerror(req
, &err
);
493 g_assert_cmpint(err
, ==, ENOENT
);
498 static void fs_walk_dotdot(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
500 QVirtio9P
*v9p
= obj
;
502 char *const wnames
[] = { g_strdup("..") };
503 v9fs_qid root_qid
, *wqid
;
506 fs_version(v9p
, NULL
, t_alloc
);
507 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
508 v9fs_req_wait_for_reply(req
, NULL
);
509 v9fs_rattach(req
, &root_qid
);
511 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
512 v9fs_req_wait_for_reply(req
, NULL
);
513 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
515 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
521 static void fs_lopen(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
523 QVirtio9P
*v9p
= obj
;
525 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE
) };
528 fs_attach(v9p
, NULL
, t_alloc
);
529 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
530 v9fs_req_wait_for_reply(req
, NULL
);
531 v9fs_rwalk(req
, NULL
, NULL
);
533 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
534 v9fs_req_wait_for_reply(req
, NULL
);
535 v9fs_rlopen(req
, NULL
, NULL
);
540 static void fs_write(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
542 QVirtio9P
*v9p
= obj
;
544 static const uint32_t write_count
= P9_MAX_SIZE
/ 2;
545 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE
) };
546 char *buf
= g_malloc0(write_count
);
550 fs_attach(v9p
, NULL
, t_alloc
);
551 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
552 v9fs_req_wait_for_reply(req
, NULL
);
553 v9fs_rwalk(req
, NULL
, NULL
);
555 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
556 v9fs_req_wait_for_reply(req
, NULL
);
557 v9fs_rlopen(req
, NULL
, NULL
);
559 req
= v9fs_twrite(v9p
, 1, 0, write_count
, buf
, 0);
560 v9fs_req_wait_for_reply(req
, NULL
);
561 v9fs_rwrite(req
, &count
);
562 g_assert_cmpint(count
, ==, write_count
);
568 static void fs_flush_success(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
570 QVirtio9P
*v9p
= obj
;
572 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
573 P9Req
*req
, *flush_req
;
575 uint8_t should_block
;
577 fs_attach(v9p
, NULL
, t_alloc
);
578 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
579 v9fs_req_wait_for_reply(req
, NULL
);
580 v9fs_rwalk(req
, NULL
, NULL
);
582 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
583 v9fs_req_wait_for_reply(req
, NULL
);
584 v9fs_rlopen(req
, NULL
, NULL
);
586 /* This will cause the 9p server to try to write data to the backend,
587 * until the write request gets cancelled.
590 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
592 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
594 /* The write request is supposed to be flushed: the server should just
595 * mark the write request as used and reply to the flush request.
597 v9fs_req_wait_for_reply(req
, &reply_len
);
598 g_assert_cmpint(reply_len
, ==, 0);
600 v9fs_rflush(flush_req
);
605 static void fs_flush_ignored(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
607 QVirtio9P
*v9p
= obj
;
609 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
610 P9Req
*req
, *flush_req
;
612 uint8_t should_block
;
614 fs_attach(v9p
, NULL
, t_alloc
);
615 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
616 v9fs_req_wait_for_reply(req
, NULL
);
617 v9fs_rwalk(req
, NULL
, NULL
);
619 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
620 v9fs_req_wait_for_reply(req
, NULL
);
621 v9fs_rlopen(req
, NULL
, NULL
);
623 /* This will cause the write request to complete right away, before it
624 * could be actually cancelled.
627 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
629 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
631 /* The write request is supposed to complete. The server should
632 * reply to the write request and the flush request.
634 v9fs_req_wait_for_reply(req
, NULL
);
635 v9fs_rwrite(req
, &count
);
636 g_assert_cmpint(count
, ==, sizeof(should_block
));
637 v9fs_rflush(flush_req
);
642 static void register_virtio_9p_test(void)
644 qos_add_test("config", "virtio-9p", pci_config
, NULL
);
645 qos_add_test("fs/version/basic", "virtio-9p", fs_version
, NULL
);
646 qos_add_test("fs/attach/basic", "virtio-9p", fs_attach
, NULL
);
647 qos_add_test("fs/walk/basic", "virtio-9p", fs_walk
, NULL
);
648 qos_add_test("fs/walk/no_slash", "virtio-9p", fs_walk_no_slash
,
650 qos_add_test("fs/walk/dotdot_from_root", "virtio-9p",
651 fs_walk_dotdot
, NULL
);
652 qos_add_test("fs/lopen/basic", "virtio-9p", fs_lopen
, NULL
);
653 qos_add_test("fs/write/basic", "virtio-9p", fs_write
, NULL
);
654 qos_add_test("fs/flush/success", "virtio-9p", fs_flush_success
,
656 qos_add_test("fs/flush/ignored", "virtio-9p", fs_flush_ignored
,
660 libqos_init(register_virtio_9p_test
);