migration: print features as on off
[qemu/kevin.git] / tests / virtio-9p-test.c
blob00f00f7246e926ffe8df740d3f2099b38c301cba
1 /*
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.
8 */
10 #include "qemu/osdep.h"
11 #include "libqtest.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";
25 typedef struct {
26 QVirtioDevice *dev;
27 QOSState *qs;
28 QVirtQueue *vq;
29 char *test_share;
30 uint16_t p9_req_tag;
31 } QVirtIO9P;
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);
47 } else {
48 g_printerr("virtio-9p tests are only available on x86 or ppc64\n");
49 exit(EXIT_FAILURE);
52 return v9p;
55 static void qvirtio_9p_stop(QVirtIO9P *v9p)
57 qtest_shutdown(v9p->qs);
58 rmdir(v9p->test_share);
59 g_free(v9p->test_share);
60 g_free(v9p);
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,
67 VIRTIO_ID_9P);
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);
81 return v9p;
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);
89 qvirtio_9p_stop(v9p);
92 static void pci_config(QVirtIO9P *v9p)
94 size_t tag_len = qvirtio_config_readw(v9p->dev, 0);
95 char *tag;
96 int i;
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);
105 g_free(tag);
108 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
110 typedef struct {
111 QVirtIO9P *v9p;
112 uint16_t tag;
113 uint64_t t_msg;
114 uint32_t t_size;
115 uint64_t r_msg;
116 /* No r_size, it is hardcoded to P9_MAX_SIZE */
117 size_t t_off;
118 size_t r_off;
119 uint32_t free_head;
120 } P9Req;
122 static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
124 memwrite(req->t_msg + req->t_off, addr, len);
125 req->t_off += len;
128 static void v9fs_memskip(P9Req *req, size_t len)
130 req->r_off += len;
133 static void v9fs_memread(P9Req *req, void *addr, size_t len)
135 memread(req->r_msg + req->r_off, addr, len);
136 req->r_off += 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);
149 le16_to_cpus(val);
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);
162 le32_to_cpus(val);
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);
172 return 2 + len;
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)
187 uint16_t local_len;
189 v9fs_uint16_read(req, &local_len);
190 if (len) {
191 *len = local_len;
193 if (string) {
194 *string = g_malloc(local_len);
195 v9fs_memread(req, *string, local_len);
196 } else {
197 v9fs_memskip(req, local_len);
201 typedef struct {
202 uint32_t size;
203 uint8_t id;
204 uint16_t tag;
205 } QEMU_PACKED P9Hdr;
207 static P9Req *v9fs_req_init(QVirtIO9P *v9p, uint32_t size, uint8_t id,
208 uint16_t tag)
210 P9Req *req = g_new0(P9Req, 1);
211 uint32_t t_size = 7 + size; /* 9P header has well-known size of 7 bytes */
212 P9Hdr hdr = {
213 .size = cpu_to_le32(t_size),
214 .id = id,
215 .tag = cpu_to_le16(tag)
218 g_assert_cmpint(t_size, <=, P9_MAX_SIZE);
220 req->v9p = v9p;
221 req->t_size = t_size;
222 req->t_msg = guest_alloc(v9p->qs->alloc, req->t_size);
223 v9fs_memwrite(req, &hdr, 7);
224 req->tag = tag;
225 return req;
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,
234 true);
235 qvirtqueue_add(v9p->vq, req->r_msg, P9_MAX_SIZE, true, false);
236 qvirtqueue_kick(v9p->dev, v9p->vq, req->free_head);
237 req->t_off = 0;
240 static const char *rmessage_name(uint8_t id)
242 return
243 id == P9_RLERROR ? "RLERROR" :
244 id == P9_RVERSION ? "RVERSION" :
245 id == P9_RATTACH ? "RATTACH" :
246 id == P9_RWALK ? "RWALK" :
247 "<unknown>";
250 static void v9fs_req_recv(P9Req *req, uint8_t id)
252 QVirtIO9P *v9p = req->v9p;
253 P9Hdr hdr;
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);
266 if (hdr.id != id) {
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) {
271 uint32_t err;
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);
285 g_free(req);
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);
293 v9fs_req_free(req);
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,
300 P9_NOTAG);
302 v9fs_uint32_write(req, msize);
303 v9fs_string_write(req, version);
304 v9fs_req_send(req);
305 return req;
308 /* size[4] Rversion tag[2] msize[4] version[s] */
309 static void v9fs_rversion(P9Req *req, uint16_t *len, char **version)
311 uint32_t msize;
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);
322 v9fs_req_free(req);
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);
338 v9fs_req_send(req);
339 return req;
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);
348 if (qid) {
349 v9fs_memread(req, qid, 13);
351 v9fs_req_free(req);
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[])
358 P9Req *req;
359 int i;
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]);
372 v9fs_req_send(req);
373 return req;
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);
383 if (nwqid) {
384 *nwqid = local_nwqid;
386 if (wqid) {
387 *wqid = g_malloc(local_nwqid * 13);
388 v9fs_memread(req, *wqid, local_nwqid * 13);
390 v9fs_req_free(req);
393 static void fs_version(QVirtIO9P *v9p)
395 const char *version = "9P2000.L";
396 uint16_t server_len;
397 char *server_version;
398 P9Req *req;
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)
410 P9Req *req;
412 fs_version(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;
421 uint16_t nwqid;
422 v9fs_qid *wqid;
423 int i;
424 P9Req *req;
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));
432 fs_attach(v9p);
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]);
441 g_free(wnames[i]);
444 g_free(wqid);
447 static void fs_walk_no_slash(QVirtIO9P *v9p)
449 char *const wnames[] = { g_strdup(" /") };
450 P9Req *req;
451 uint32_t err;
453 fs_attach(v9p);
454 req = v9fs_twalk(v9p, 0, 1, 1, wnames);
455 v9fs_rlerror(req, &err);
457 g_assert_cmpint(err, ==, ENOENT);
459 g_free(wnames[0]);
462 static void fs_walk_dotdot(QVirtIO9P *v9p)
464 char *const wnames[] = { g_strdup("..") };
465 v9fs_qid root_qid, *wqid;
466 P9Req *req;
468 fs_version(v9p);
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);
477 g_free(wqid);
478 g_free(wnames[0]);
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();
488 if (fn) {
489 fn(v9p);
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",
509 fs_walk_dotdot);
511 return g_test_run();