cpu_physical_memory_sync_dirty_bitmap: Fix alignment check
[qemu/ar7.git] / tests / virtio-9p-test.c
blobad33d963876fd32576062c01e2975909109f5a17
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 static const char mount_tag[] = "qtest";
23 typedef struct {
24 QVirtioDevice *dev;
25 QOSState *qs;
26 QVirtQueue *vq;
27 char *test_share;
28 uint16_t p9_req_tag;
29 } QVirtIO9P;
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);
45 } else {
46 g_printerr("virtio-9p tests are only available on x86 or ppc64\n");
47 exit(EXIT_FAILURE);
50 return v9p;
53 static void qvirtio_9p_stop(QVirtIO9P *v9p)
55 qtest_shutdown(v9p->qs);
56 rmdir(v9p->test_share);
57 g_free(v9p->test_share);
58 g_free(v9p);
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,
65 VIRTIO_ID_9P);
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);
76 return v9p;
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);
84 qvirtio_9p_stop(v9p);
87 static void pci_config(QVirtIO9P *v9p)
89 size_t tag_len = qvirtio_config_readw(v9p->dev, 0);
90 char *tag;
91 int i;
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);
100 g_free(tag);
103 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
105 typedef struct {
106 QVirtIO9P *v9p;
107 uint16_t tag;
108 uint64_t t_msg;
109 uint32_t t_size;
110 uint64_t r_msg;
111 /* No r_size, it is hardcoded to P9_MAX_SIZE */
112 size_t t_off;
113 size_t r_off;
114 } P9Req;
116 static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
118 memwrite(req->t_msg + req->t_off, addr, len);
119 req->t_off += len;
122 static void v9fs_memskip(P9Req *req, size_t len)
124 req->r_off += len;
127 static void v9fs_memrewind(P9Req *req, size_t len)
129 req->r_off -= len;
132 static void v9fs_memread(P9Req *req, void *addr, size_t len)
134 memread(req->r_msg + req->r_off, addr, len);
135 req->r_off += 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);
148 le16_to_cpus(val);
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);
161 le32_to_cpus(val);
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);
171 return 2 + len;
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)
186 uint16_t local_len;
188 v9fs_uint16_read(req, &local_len);
189 if (len) {
190 *len = local_len;
192 if (string) {
193 *string = g_malloc(local_len);
194 v9fs_memread(req, *string, local_len);
195 } else {
196 v9fs_memskip(req, local_len);
200 typedef struct {
201 uint32_t size;
202 uint8_t id;
203 uint16_t tag;
204 } QEMU_PACKED P9Hdr;
206 static P9Req *v9fs_req_init(QVirtIO9P *v9p, uint32_t size, uint8_t id,
207 uint16_t tag)
209 P9Req *req = g_new0(P9Req, 1);
210 uint32_t t_size = 7 + size; /* 9P header has well-known size of 7 bytes */
211 P9Hdr hdr = {
212 .size = cpu_to_le32(t_size),
213 .id = id,
214 .tag = cpu_to_le16(tag)
217 g_assert_cmpint(t_size, <=, P9_MAX_SIZE);
219 req->v9p = v9p;
220 req->t_size = t_size;
221 req->t_msg = guest_alloc(v9p->qs->alloc, req->t_size);
222 v9fs_memwrite(req, &hdr, 7);
223 req->tag = tag;
224 return req;
227 static void v9fs_req_send(P9Req *req)
229 QVirtIO9P *v9p = req->v9p;
230 uint32_t free_head;
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);
236 req->t_off = 0;
239 static const char *rmessage_name(uint8_t id)
241 return
242 id == P9_RLERROR ? "RLERROR" :
243 id == P9_RVERSION ? "RVERSION" :
244 id == P9_RATTACH ? "RATTACH" :
245 id == P9_RWALK ? "RWALK" :
246 "<unknown>";
249 static void v9fs_req_recv(P9Req *req, uint8_t id)
251 QVirtIO9P *v9p = req->v9p;
252 P9Hdr hdr;
253 int i;
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 hdr.size = ldl_le_p(&hdr.size);
260 hdr.tag = lduw_le_p(&hdr.tag);
261 if (hdr.size >= 7) {
262 break;
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);
271 if (hdr.id != id) {
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) {
276 uint32_t err;
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);
290 g_free(req);
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);
298 v9fs_req_free(req);
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,
305 P9_NOTAG);
307 v9fs_uint32_write(req, msize);
308 v9fs_string_write(req, version);
309 v9fs_req_send(req);
310 return req;
313 /* size[4] Rversion tag[2] msize[4] version[s] */
314 static void v9fs_rversion(P9Req *req, uint16_t *len, char **version)
316 uint32_t msize;
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);
327 v9fs_req_free(req);
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);
343 v9fs_req_send(req);
344 return req;
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);
353 if (qid) {
354 v9fs_memread(req, qid, 13);
356 v9fs_req_free(req);
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[])
363 P9Req *req;
364 int i;
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]);
377 v9fs_req_send(req);
378 return req;
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);
388 if (nwqid) {
389 *nwqid = local_nwqid;
391 if (wqid) {
392 *wqid = g_malloc(local_nwqid * 13);
393 v9fs_memread(req, *wqid, local_nwqid * 13);
395 v9fs_req_free(req);
398 static void fs_version(QVirtIO9P *v9p)
400 const char *version = "9P2000.L";
401 uint16_t server_len;
402 char *server_version;
403 P9Req *req;
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)
415 P9Req *req;
417 fs_version(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;
426 uint16_t nwqid;
427 v9fs_qid *wqid;
428 int i;
429 P9Req *req;
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));
437 fs_attach(v9p);
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]);
446 g_free(wnames[i]);
449 g_free(wqid);
452 static void fs_walk_no_slash(QVirtIO9P *v9p)
454 char *const wnames[] = { g_strdup(" /") };
455 P9Req *req;
456 uint32_t err;
458 fs_attach(v9p);
459 req = v9fs_twalk(v9p, 0, 1, 1, wnames);
460 v9fs_rlerror(req, &err);
462 g_assert_cmpint(err, ==, ENOENT);
464 g_free(wnames[0]);
467 static void fs_walk_dotdot(QVirtIO9P *v9p)
469 char *const wnames[] = { g_strdup("..") };
470 v9fs_qid root_qid, *wqid;
471 P9Req *req;
473 fs_version(v9p);
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);
482 g_free(wqid);
483 g_free(wnames[0]);
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();
493 if (fn) {
494 fn(v9p);
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",
514 fs_walk_dotdot);
516 return g_test_run();