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"
21 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
23 static const char mount_tag
[] = "qtest";
33 static QVirtIO9P
*qvirtio_9p_start(const char *driver
)
35 const char *arch
= qtest_get_arch();
36 const char *cmd
= "-fsdev local,id=fsdev0,security_model=none,path=%s "
37 "-device %s,fsdev=fsdev0,mount_tag=%s";
38 QVirtIO9P
*v9p
= g_new0(QVirtIO9P
, 1);
40 v9p
->test_share
= g_strdup("/tmp/qtest.XXXXXX");
41 g_assert_nonnull(mkdtemp(v9p
->test_share
));
43 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
44 v9p
->qs
= qtest_pc_boot(cmd
, v9p
->test_share
, driver
, mount_tag
);
45 } else if (strcmp(arch
, "ppc64") == 0) {
46 v9p
->qs
= qtest_spapr_boot(cmd
, v9p
->test_share
, driver
, mount_tag
);
48 g_printerr("virtio-9p tests are only available on x86 or ppc64\n");
55 static void qvirtio_9p_stop(QVirtIO9P
*v9p
)
57 qtest_shutdown(v9p
->qs
);
58 rmdir(v9p
->test_share
);
59 g_free(v9p
->test_share
);
63 static QVirtIO9P
*qvirtio_9p_pci_start(void)
65 QVirtIO9P
*v9p
= qvirtio_9p_start("virtio-9p-pci");
66 QVirtioPCIDevice
*dev
= qvirtio_pci_device_find(v9p
->qs
->pcibus
,
68 g_assert_nonnull(dev
);
69 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_9P
);
70 v9p
->dev
= (QVirtioDevice
*) dev
;
72 qvirtio_pci_device_enable(dev
);
73 qvirtio_reset(v9p
->dev
);
74 qvirtio_set_acknowledge(v9p
->dev
);
75 qvirtio_set_driver(v9p
->dev
);
77 v9p
->vq
= qvirtqueue_setup(v9p
->dev
, v9p
->qs
->alloc
, 0);
79 qvirtio_set_driver_ok(v9p
->dev
);
84 static void qvirtio_9p_pci_stop(QVirtIO9P
*v9p
)
86 qvirtqueue_cleanup(v9p
->dev
->bus
, v9p
->vq
, v9p
->qs
->alloc
);
87 qvirtio_pci_device_disable(container_of(v9p
->dev
, QVirtioPCIDevice
, vdev
));
88 qvirtio_pci_device_free((QVirtioPCIDevice
*)v9p
->dev
);
92 static void pci_config(QVirtIO9P
*v9p
)
94 size_t tag_len
= qvirtio_config_readw(v9p
->dev
, 0);
98 g_assert_cmpint(tag_len
, ==, strlen(mount_tag
));
100 tag
= g_malloc(tag_len
);
101 for (i
= 0; i
< tag_len
; i
++) {
102 tag
[i
] = qvirtio_config_readb(v9p
->dev
, i
+ 2);
104 g_assert_cmpmem(tag
, tag_len
, mount_tag
, tag_len
);
108 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
116 /* No r_size, it is hardcoded to P9_MAX_SIZE */
122 static void v9fs_memwrite(P9Req
*req
, const void *addr
, size_t len
)
124 memwrite(req
->t_msg
+ req
->t_off
, addr
, len
);
128 static void v9fs_memskip(P9Req
*req
, size_t len
)
133 static void v9fs_memread(P9Req
*req
, void *addr
, size_t len
)
135 memread(req
->r_msg
+ req
->r_off
, addr
, len
);
139 static void v9fs_uint16_write(P9Req
*req
, uint16_t val
)
141 uint16_t le_val
= cpu_to_le16(val
);
143 v9fs_memwrite(req
, &le_val
, 2);
146 static void v9fs_uint16_read(P9Req
*req
, uint16_t *val
)
148 v9fs_memread(req
, val
, 2);
152 static void v9fs_uint32_write(P9Req
*req
, uint32_t val
)
154 uint32_t le_val
= cpu_to_le32(val
);
156 v9fs_memwrite(req
, &le_val
, 4);
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
);
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 t_size
= 7 + size
; /* 9P header has well-known size of 7 bytes */
213 .size
= cpu_to_le32(t_size
),
215 .tag
= cpu_to_le16(tag
)
218 g_assert_cmpint(t_size
, <=, P9_MAX_SIZE
);
221 req
->t_size
= t_size
;
222 req
->t_msg
= guest_alloc(v9p
->qs
->alloc
, req
->t_size
);
223 v9fs_memwrite(req
, &hdr
, 7);
228 static void v9fs_req_send(P9Req
*req
)
230 QVirtIO9P
*v9p
= req
->v9p
;
232 req
->r_msg
= guest_alloc(v9p
->qs
->alloc
, P9_MAX_SIZE
);
233 req
->free_head
= qvirtqueue_add(v9p
->vq
, req
->t_msg
, req
->t_size
, false,
235 qvirtqueue_add(v9p
->vq
, req
->r_msg
, P9_MAX_SIZE
, true, false);
236 qvirtqueue_kick(v9p
->dev
, v9p
->vq
, req
->free_head
);
240 static const char *rmessage_name(uint8_t id
)
243 id
== P9_RLERROR
? "RLERROR" :
244 id
== P9_RVERSION
? "RVERSION" :
245 id
== P9_RATTACH
? "RATTACH" :
246 id
== P9_RWALK
? "RWALK" :
250 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
252 QVirtIO9P
*v9p
= req
->v9p
;
255 qvirtio_wait_used_elem(v9p
->dev
, v9p
->vq
, req
->free_head
,
256 QVIRTIO_9P_TIMEOUT_US
);
258 v9fs_memread(req
, &hdr
, 7);
259 hdr
.size
= ldl_le_p(&hdr
.size
);
260 hdr
.tag
= lduw_le_p(&hdr
.tag
);
262 g_assert_cmpint(hdr
.size
, >=, 7);
263 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
264 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
267 g_printerr("Received response %d (%s) instead of %d (%s)\n",
268 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
270 if (hdr
.id
== P9_RLERROR
) {
272 v9fs_uint32_read(req
, &err
);
273 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
276 g_assert_cmpint(hdr
.id
, ==, id
);
279 static void v9fs_req_free(P9Req
*req
)
281 QVirtIO9P
*v9p
= req
->v9p
;
283 guest_free(v9p
->qs
->alloc
, req
->t_msg
);
284 guest_free(v9p
->qs
->alloc
, req
->r_msg
);
288 /* size[4] Rlerror tag[2] ecode[4] */
289 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
291 v9fs_req_recv(req
, P9_RLERROR
);
292 v9fs_uint32_read(req
, err
);
296 /* size[4] Tversion tag[2] msize[4] version[s] */
297 static P9Req
*v9fs_tversion(QVirtIO9P
*v9p
, uint32_t msize
, const char *version
)
299 P9Req
*req
= v9fs_req_init(v9p
, 4 + v9fs_string_size(version
), P9_TVERSION
,
302 v9fs_uint32_write(req
, msize
);
303 v9fs_string_write(req
, version
);
308 /* size[4] Rversion tag[2] msize[4] version[s] */
309 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
313 v9fs_req_recv(req
, P9_RVERSION
);
314 v9fs_uint32_read(req
, &msize
);
316 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
318 if (len
|| version
) {
319 v9fs_string_read(req
, len
, version
);
325 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
326 static P9Req
*v9fs_tattach(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t n_uname
)
328 const char *uname
= ""; /* ignored by QEMU */
329 const char *aname
= ""; /* ignored by QEMU */
330 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
,
331 ++(v9p
->p9_req_tag
));
333 v9fs_uint32_write(req
, fid
);
334 v9fs_uint32_write(req
, P9_NOFID
);
335 v9fs_string_write(req
, uname
);
336 v9fs_string_write(req
, aname
);
337 v9fs_uint32_write(req
, n_uname
);
342 typedef char v9fs_qid
[13];
344 /* size[4] Rattach tag[2] qid[13] */
345 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
347 v9fs_req_recv(req
, P9_RATTACH
);
349 v9fs_memread(req
, qid
, 13);
354 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
355 static P9Req
*v9fs_twalk(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t newfid
,
356 uint16_t nwname
, char *const wnames
[])
360 uint32_t size
= 4 + 4 + 2;
362 for (i
= 0; i
< nwname
; i
++) {
363 size
+= v9fs_string_size(wnames
[i
]);
365 req
= v9fs_req_init(v9p
, size
, P9_TWALK
, ++(v9p
->p9_req_tag
));
366 v9fs_uint32_write(req
, fid
);
367 v9fs_uint32_write(req
, newfid
);
368 v9fs_uint16_write(req
, nwname
);
369 for (i
= 0; i
< nwname
; i
++) {
370 v9fs_string_write(req
, wnames
[i
]);
376 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
377 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
379 uint16_t local_nwqid
;
381 v9fs_req_recv(req
, P9_RWALK
);
382 v9fs_uint16_read(req
, &local_nwqid
);
384 *nwqid
= local_nwqid
;
387 *wqid
= g_malloc(local_nwqid
* 13);
388 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
393 static void fs_version(QVirtIO9P
*v9p
)
395 const char *version
= "9P2000.L";
397 char *server_version
;
400 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
);
401 v9fs_rversion(req
, &server_len
, &server_version
);
403 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
405 g_free(server_version
);
408 static void fs_attach(QVirtIO9P
*v9p
)
413 req
= v9fs_tattach(v9p
, 0, getuid());
414 v9fs_rattach(req
, NULL
);
417 static void fs_walk(QVirtIO9P
*v9p
)
419 char *wnames
[P9_MAXWELEM
], *paths
[P9_MAXWELEM
];
420 char *last_path
= v9p
->test_share
;
426 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
427 wnames
[i
] = g_strdup_printf("%s%d", __func__
, i
);
428 last_path
= paths
[i
] = g_strdup_printf("%s/%s", last_path
, wnames
[i
]);
429 g_assert(!mkdir(paths
[i
], 0700));
433 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
);
434 v9fs_rwalk(req
, &nwqid
, &wqid
);
436 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
438 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
439 rmdir(paths
[P9_MAXWELEM
- i
- 1]);
440 g_free(paths
[P9_MAXWELEM
- i
- 1]);
447 static void fs_walk_no_slash(QVirtIO9P
*v9p
)
449 char *const wnames
[] = { g_strdup(" /") };
454 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
);
455 v9fs_rlerror(req
, &err
);
457 g_assert_cmpint(err
, ==, ENOENT
);
462 static void fs_walk_dotdot(QVirtIO9P
*v9p
)
464 char *const wnames
[] = { g_strdup("..") };
465 v9fs_qid root_qid
, *wqid
;
469 req
= v9fs_tattach(v9p
, 0, getuid());
470 v9fs_rattach(req
, &root_qid
);
472 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
);
473 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
475 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
481 typedef void (*v9fs_test_fn
)(QVirtIO9P
*v9p
);
483 static void v9fs_run_pci_test(gconstpointer data
)
485 v9fs_test_fn fn
= data
;
486 QVirtIO9P
*v9p
= qvirtio_9p_pci_start();
491 qvirtio_9p_pci_stop(v9p
);
494 static void v9fs_qtest_pci_add(const char *path
, v9fs_test_fn fn
)
496 qtest_add_data_func(path
, fn
, v9fs_run_pci_test
);
499 int main(int argc
, char **argv
)
501 g_test_init(&argc
, &argv
, NULL
);
502 v9fs_qtest_pci_add("/virtio/9p/pci/nop", NULL
);
503 v9fs_qtest_pci_add("/virtio/9p/pci/config", pci_config
);
504 v9fs_qtest_pci_add("/virtio/9p/pci/fs/version/basic", fs_version
);
505 v9fs_qtest_pci_add("/virtio/9p/pci/fs/attach/basic", fs_attach
);
506 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/basic", fs_walk
);
507 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/no_slash", fs_walk_no_slash
);
508 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/dotdot_from_root",