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 static const char mount_tag
[] = "qtest";
31 static QVirtIO9P
*qvirtio_9p_start(const char *driver
)
33 const char *arch
= qtest_get_arch();
34 const char *cmd
= "-fsdev local,id=fsdev0,security_model=none,path=%s "
35 "-device %s,fsdev=fsdev0,mount_tag=%s";
36 QVirtIO9P
*v9p
= g_new0(QVirtIO9P
, 1);
38 v9p
->test_share
= g_strdup("/tmp/qtest.XXXXXX");
39 g_assert_nonnull(mkdtemp(v9p
->test_share
));
41 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
42 v9p
->qs
= qtest_pc_boot(cmd
, v9p
->test_share
, driver
, mount_tag
);
43 } else if (strcmp(arch
, "ppc64") == 0) {
44 v9p
->qs
= qtest_spapr_boot(cmd
, v9p
->test_share
, driver
, mount_tag
);
46 g_printerr("virtio-9p tests are only available on x86 or ppc64\n");
53 static void qvirtio_9p_stop(QVirtIO9P
*v9p
)
55 qtest_shutdown(v9p
->qs
);
56 rmdir(v9p
->test_share
);
57 g_free(v9p
->test_share
);
61 static QVirtIO9P
*qvirtio_9p_pci_start(void)
63 QVirtIO9P
*v9p
= qvirtio_9p_start("virtio-9p-pci");
64 QVirtioPCIDevice
*dev
= qvirtio_pci_device_find(v9p
->qs
->pcibus
,
66 g_assert_nonnull(dev
);
67 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_9P
);
68 v9p
->dev
= (QVirtioDevice
*) dev
;
70 qvirtio_pci_device_enable(dev
);
71 qvirtio_reset(v9p
->dev
);
72 qvirtio_set_acknowledge(v9p
->dev
);
73 qvirtio_set_driver(v9p
->dev
);
75 v9p
->vq
= qvirtqueue_setup(v9p
->dev
, v9p
->qs
->alloc
, 0);
79 static void qvirtio_9p_pci_stop(QVirtIO9P
*v9p
)
81 qvirtqueue_cleanup(v9p
->dev
->bus
, v9p
->vq
, v9p
->qs
->alloc
);
82 qvirtio_pci_device_disable(container_of(v9p
->dev
, QVirtioPCIDevice
, vdev
));
87 static void pci_config(QVirtIO9P
*v9p
)
89 size_t tag_len
= qvirtio_config_readw(v9p
->dev
, 0);
93 g_assert_cmpint(tag_len
, ==, strlen(mount_tag
));
95 tag
= g_malloc(tag_len
);
96 for (i
= 0; i
< tag_len
; i
++) {
97 tag
[i
] = qvirtio_config_readb(v9p
->dev
, i
+ 2);
99 g_assert_cmpmem(tag
, tag_len
, mount_tag
, tag_len
);
103 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
111 /* 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_memrewind(P9Req
*req
, size_t len
)
132 static void v9fs_memread(P9Req
*req
, void *addr
, size_t len
)
134 memread(req
->r_msg
+ req
->r_off
, addr
, len
);
138 static void v9fs_uint16_write(P9Req
*req
, uint16_t val
)
140 uint16_t le_val
= cpu_to_le16(val
);
142 v9fs_memwrite(req
, &le_val
, 2);
145 static void v9fs_uint16_read(P9Req
*req
, uint16_t *val
)
147 v9fs_memread(req
, val
, 2);
151 static void v9fs_uint32_write(P9Req
*req
, uint32_t val
)
153 uint32_t le_val
= cpu_to_le32(val
);
155 v9fs_memwrite(req
, &le_val
, 4);
158 static void v9fs_uint32_read(P9Req
*req
, uint32_t *val
)
160 v9fs_memread(req
, val
, 4);
164 /* len[2] string[len] */
165 static uint16_t v9fs_string_size(const char *string
)
167 size_t len
= strlen(string
);
169 g_assert_cmpint(len
, <=, UINT16_MAX
);
174 static void v9fs_string_write(P9Req
*req
, const char *string
)
176 int len
= strlen(string
);
178 g_assert_cmpint(len
, <=, UINT16_MAX
);
180 v9fs_uint16_write(req
, (uint16_t) len
);
181 v9fs_memwrite(req
, string
, len
);
184 static void v9fs_string_read(P9Req
*req
, uint16_t *len
, char **string
)
188 v9fs_uint16_read(req
, &local_len
);
193 *string
= g_malloc(local_len
);
194 v9fs_memread(req
, *string
, local_len
);
196 v9fs_memskip(req
, local_len
);
206 static P9Req
*v9fs_req_init(QVirtIO9P
*v9p
, uint32_t size
, uint8_t id
,
209 P9Req
*req
= g_new0(P9Req
, 1);
210 uint32_t t_size
= 7 + size
; /* 9P header has well-known size of 7 bytes */
212 .size
= cpu_to_le32(t_size
),
214 .tag
= cpu_to_le16(tag
)
217 g_assert_cmpint(t_size
, <=, P9_MAX_SIZE
);
220 req
->t_size
= t_size
;
221 req
->t_msg
= guest_alloc(v9p
->qs
->alloc
, req
->t_size
);
222 v9fs_memwrite(req
, &hdr
, 7);
227 static void v9fs_req_send(P9Req
*req
)
229 QVirtIO9P
*v9p
= req
->v9p
;
232 req
->r_msg
= guest_alloc(v9p
->qs
->alloc
, P9_MAX_SIZE
);
233 free_head
= qvirtqueue_add(v9p
->vq
, req
->t_msg
, req
->t_size
, false, true);
234 qvirtqueue_add(v9p
->vq
, req
->r_msg
, P9_MAX_SIZE
, true, false);
235 qvirtqueue_kick(v9p
->dev
, v9p
->vq
, free_head
);
239 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
241 QVirtIO9P
*v9p
= req
->v9p
;
245 for (i
= 0; i
< 10; i
++) {
246 qvirtio_wait_queue_isr(v9p
->dev
, v9p
->vq
, 1000 * 1000);
248 v9fs_memread(req
, &hdr
, 7);
249 le32_to_cpus(&hdr
.size
);
250 le16_to_cpus(&hdr
.tag
);
254 v9fs_memrewind(req
, 7);
257 g_assert_cmpint(hdr
.size
, >=, 7);
258 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
259 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
261 if (hdr
.id
!= id
&& hdr
.id
== P9_RLERROR
) {
263 v9fs_uint32_read(req
, &err
);
264 g_printerr("Received Rlerror (%d) instead of Response %d\n", err
, id
);
265 g_assert_not_reached();
267 g_assert_cmpint(hdr
.id
, ==, id
);
270 static void v9fs_req_free(P9Req
*req
)
272 QVirtIO9P
*v9p
= req
->v9p
;
274 guest_free(v9p
->qs
->alloc
, req
->t_msg
);
275 guest_free(v9p
->qs
->alloc
, req
->r_msg
);
279 /* size[4] Rlerror tag[2] ecode[4] */
280 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
282 v9fs_req_recv(req
, P9_RLERROR
);
283 v9fs_uint32_read(req
, err
);
287 /* size[4] Tversion tag[2] msize[4] version[s] */
288 static P9Req
*v9fs_tversion(QVirtIO9P
*v9p
, uint32_t msize
, const char *version
)
290 P9Req
*req
= v9fs_req_init(v9p
, 4 + v9fs_string_size(version
), P9_TVERSION
,
293 v9fs_uint32_write(req
, msize
);
294 v9fs_string_write(req
, version
);
299 /* size[4] Rversion tag[2] msize[4] version[s] */
300 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
304 v9fs_req_recv(req
, P9_RVERSION
);
305 v9fs_uint32_read(req
, &msize
);
307 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
309 if (len
|| version
) {
310 v9fs_string_read(req
, len
, version
);
316 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
317 static P9Req
*v9fs_tattach(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t n_uname
)
319 const char *uname
= ""; /* ignored by QEMU */
320 const char *aname
= ""; /* ignored by QEMU */
321 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
,
322 ++(v9p
->p9_req_tag
));
324 v9fs_uint32_write(req
, fid
);
325 v9fs_uint32_write(req
, P9_NOFID
);
326 v9fs_string_write(req
, uname
);
327 v9fs_string_write(req
, aname
);
328 v9fs_uint32_write(req
, n_uname
);
333 typedef char v9fs_qid
[13];
335 /* size[4] Rattach tag[2] qid[13] */
336 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
338 v9fs_req_recv(req
, P9_RATTACH
);
340 v9fs_memread(req
, qid
, 13);
345 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
346 static P9Req
*v9fs_twalk(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t newfid
,
347 uint16_t nwname
, char *const wnames
[])
351 uint32_t size
= 4 + 4 + 2;
353 for (i
= 0; i
< nwname
; i
++) {
354 size
+= v9fs_string_size(wnames
[i
]);
356 req
= v9fs_req_init(v9p
, size
, P9_TWALK
, ++(v9p
->p9_req_tag
));
357 v9fs_uint32_write(req
, fid
);
358 v9fs_uint32_write(req
, newfid
);
359 v9fs_uint16_write(req
, nwname
);
360 for (i
= 0; i
< nwname
; i
++) {
361 v9fs_string_write(req
, wnames
[i
]);
367 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
368 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
370 uint16_t local_nwqid
;
372 v9fs_req_recv(req
, P9_RWALK
);
373 v9fs_uint16_read(req
, &local_nwqid
);
375 *nwqid
= local_nwqid
;
378 *wqid
= g_malloc(local_nwqid
* 13);
379 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
384 static void fs_version(QVirtIO9P
*v9p
)
386 const char *version
= "9P2000.L";
388 char *server_version
;
391 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
);
392 v9fs_rversion(req
, &server_len
, &server_version
);
394 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
396 g_free(server_version
);
399 static void fs_attach(QVirtIO9P
*v9p
)
404 req
= v9fs_tattach(v9p
, 0, getuid());
405 v9fs_rattach(req
, NULL
);
408 static void fs_walk(QVirtIO9P
*v9p
)
410 char *wnames
[P9_MAXWELEM
], *paths
[P9_MAXWELEM
];
411 char *last_path
= v9p
->test_share
;
417 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
418 wnames
[i
] = g_strdup_printf("%s%d", __func__
, i
);
419 last_path
= paths
[i
] = g_strdup_printf("%s/%s", last_path
, wnames
[i
]);
420 g_assert(!mkdir(paths
[i
], 0700));
424 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
);
425 v9fs_rwalk(req
, &nwqid
, &wqid
);
427 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
429 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
430 rmdir(paths
[P9_MAXWELEM
- i
- 1]);
431 g_free(paths
[P9_MAXWELEM
- i
- 1]);
438 static void fs_walk_no_slash(QVirtIO9P
*v9p
)
440 char *const wnames
[] = { g_strdup(" /") };
445 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
);
446 v9fs_rlerror(req
, &err
);
448 g_assert_cmpint(err
, ==, ENOENT
);
453 static void fs_walk_dotdot(QVirtIO9P
*v9p
)
455 char *const wnames
[] = { g_strdup("..") };
456 v9fs_qid root_qid
, *wqid
;
460 req
= v9fs_tattach(v9p
, 0, getuid());
461 v9fs_rattach(req
, &root_qid
);
463 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
);
464 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
466 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
472 typedef void (*v9fs_test_fn
)(QVirtIO9P
*v9p
);
474 static void v9fs_run_pci_test(gconstpointer data
)
476 v9fs_test_fn fn
= data
;
477 QVirtIO9P
*v9p
= qvirtio_9p_pci_start();
482 qvirtio_9p_pci_stop(v9p
);
485 static void v9fs_qtest_pci_add(const char *path
, v9fs_test_fn fn
)
487 qtest_add_data_func(path
, fn
, v9fs_run_pci_test
);
490 int main(int argc
, char **argv
)
492 g_test_init(&argc
, &argv
, NULL
);
493 v9fs_qtest_pci_add("/virtio/9p/pci/nop", NULL
);
494 v9fs_qtest_pci_add("/virtio/9p/pci/config", pci_config
);
495 v9fs_qtest_pci_add("/virtio/9p/pci/fs/version/basic", fs_version
);
496 v9fs_qtest_pci_add("/virtio/9p/pci/fs/attach/basic", fs_attach
);
497 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/basic", fs_walk
);
498 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/no_slash", fs_walk_no_slash
);
499 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/dotdot_from_root",