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
));
83 qvirtio_pci_device_free((QVirtioPCIDevice
*)v9p
->dev
);
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 const char *rmessage_name(uint8_t id
)
242 id
== P9_RLERROR
? "RLERROR" :
243 id
== P9_RVERSION
? "RVERSION" :
244 id
== P9_RATTACH
? "RATTACH" :
245 id
== P9_RWALK
? "RWALK" :
249 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
251 QVirtIO9P
*v9p
= req
->v9p
;
255 for (i
= 0; i
< 10; i
++) {
256 qvirtio_wait_queue_isr(v9p
->dev
, v9p
->vq
, 1000 * 1000);
258 v9fs_memread(req
, &hdr
, 7);
259 le32_to_cpus(&hdr
.size
);
260 le16_to_cpus(&hdr
.tag
);
264 v9fs_memrewind(req
, 7);
267 g_assert_cmpint(hdr
.size
, >=, 7);
268 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
269 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
272 g_printerr("Received response %d (%s) instead of %d (%s)\n",
273 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
275 if (hdr
.id
== P9_RLERROR
) {
277 v9fs_uint32_read(req
, &err
);
278 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
281 g_assert_cmpint(hdr
.id
, ==, id
);
284 static void v9fs_req_free(P9Req
*req
)
286 QVirtIO9P
*v9p
= req
->v9p
;
288 guest_free(v9p
->qs
->alloc
, req
->t_msg
);
289 guest_free(v9p
->qs
->alloc
, req
->r_msg
);
293 /* size[4] Rlerror tag[2] ecode[4] */
294 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
296 v9fs_req_recv(req
, P9_RLERROR
);
297 v9fs_uint32_read(req
, err
);
301 /* size[4] Tversion tag[2] msize[4] version[s] */
302 static P9Req
*v9fs_tversion(QVirtIO9P
*v9p
, uint32_t msize
, const char *version
)
304 P9Req
*req
= v9fs_req_init(v9p
, 4 + v9fs_string_size(version
), P9_TVERSION
,
307 v9fs_uint32_write(req
, msize
);
308 v9fs_string_write(req
, version
);
313 /* size[4] Rversion tag[2] msize[4] version[s] */
314 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
318 v9fs_req_recv(req
, P9_RVERSION
);
319 v9fs_uint32_read(req
, &msize
);
321 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
323 if (len
|| version
) {
324 v9fs_string_read(req
, len
, version
);
330 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
331 static P9Req
*v9fs_tattach(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t n_uname
)
333 const char *uname
= ""; /* ignored by QEMU */
334 const char *aname
= ""; /* ignored by QEMU */
335 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
,
336 ++(v9p
->p9_req_tag
));
338 v9fs_uint32_write(req
, fid
);
339 v9fs_uint32_write(req
, P9_NOFID
);
340 v9fs_string_write(req
, uname
);
341 v9fs_string_write(req
, aname
);
342 v9fs_uint32_write(req
, n_uname
);
347 typedef char v9fs_qid
[13];
349 /* size[4] Rattach tag[2] qid[13] */
350 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
352 v9fs_req_recv(req
, P9_RATTACH
);
354 v9fs_memread(req
, qid
, 13);
359 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
360 static P9Req
*v9fs_twalk(QVirtIO9P
*v9p
, uint32_t fid
, uint32_t newfid
,
361 uint16_t nwname
, char *const wnames
[])
365 uint32_t size
= 4 + 4 + 2;
367 for (i
= 0; i
< nwname
; i
++) {
368 size
+= v9fs_string_size(wnames
[i
]);
370 req
= v9fs_req_init(v9p
, size
, P9_TWALK
, ++(v9p
->p9_req_tag
));
371 v9fs_uint32_write(req
, fid
);
372 v9fs_uint32_write(req
, newfid
);
373 v9fs_uint16_write(req
, nwname
);
374 for (i
= 0; i
< nwname
; i
++) {
375 v9fs_string_write(req
, wnames
[i
]);
381 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
382 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
384 uint16_t local_nwqid
;
386 v9fs_req_recv(req
, P9_RWALK
);
387 v9fs_uint16_read(req
, &local_nwqid
);
389 *nwqid
= local_nwqid
;
392 *wqid
= g_malloc(local_nwqid
* 13);
393 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
398 static void fs_version(QVirtIO9P
*v9p
)
400 const char *version
= "9P2000.L";
402 char *server_version
;
405 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
);
406 v9fs_rversion(req
, &server_len
, &server_version
);
408 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
410 g_free(server_version
);
413 static void fs_attach(QVirtIO9P
*v9p
)
418 req
= v9fs_tattach(v9p
, 0, getuid());
419 v9fs_rattach(req
, NULL
);
422 static void fs_walk(QVirtIO9P
*v9p
)
424 char *wnames
[P9_MAXWELEM
], *paths
[P9_MAXWELEM
];
425 char *last_path
= v9p
->test_share
;
431 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
432 wnames
[i
] = g_strdup_printf("%s%d", __func__
, i
);
433 last_path
= paths
[i
] = g_strdup_printf("%s/%s", last_path
, wnames
[i
]);
434 g_assert(!mkdir(paths
[i
], 0700));
438 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
);
439 v9fs_rwalk(req
, &nwqid
, &wqid
);
441 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
443 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
444 rmdir(paths
[P9_MAXWELEM
- i
- 1]);
445 g_free(paths
[P9_MAXWELEM
- i
- 1]);
452 static void fs_walk_no_slash(QVirtIO9P
*v9p
)
454 char *const wnames
[] = { g_strdup(" /") };
459 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
);
460 v9fs_rlerror(req
, &err
);
462 g_assert_cmpint(err
, ==, ENOENT
);
467 static void fs_walk_dotdot(QVirtIO9P
*v9p
)
469 char *const wnames
[] = { g_strdup("..") };
470 v9fs_qid root_qid
, *wqid
;
474 req
= v9fs_tattach(v9p
, 0, getuid());
475 v9fs_rattach(req
, &root_qid
);
477 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
);
478 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
480 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
486 typedef void (*v9fs_test_fn
)(QVirtIO9P
*v9p
);
488 static void v9fs_run_pci_test(gconstpointer data
)
490 v9fs_test_fn fn
= data
;
491 QVirtIO9P
*v9p
= qvirtio_9p_pci_start();
496 qvirtio_9p_pci_stop(v9p
);
499 static void v9fs_qtest_pci_add(const char *path
, v9fs_test_fn fn
)
501 qtest_add_data_func(path
, fn
, v9fs_run_pci_test
);
504 int main(int argc
, char **argv
)
506 g_test_init(&argc
, &argv
, NULL
);
507 v9fs_qtest_pci_add("/virtio/9p/pci/nop", NULL
);
508 v9fs_qtest_pci_add("/virtio/9p/pci/config", pci_config
);
509 v9fs_qtest_pci_add("/virtio/9p/pci/fs/version/basic", fs_version
);
510 v9fs_qtest_pci_add("/virtio/9p/pci/fs/attach/basic", fs_attach
);
511 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/basic", fs_walk
);
512 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/no_slash", fs_walk_no_slash
);
513 v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/dotdot_from_root",