cirrus: fix off-by-one in cirrus_bitblt_rop_bkwd_transp_*_16
[qemu.git] / tests / virtio-9p-test.c
1 /*
2 * QTest testcase for VirtIO 9P
3 *
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 *
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 */
9
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"
20
21 static const char mount_tag[] = "qtest";
22
23 typedef struct {
24 QVirtioDevice *dev;
25 QOSState *qs;
26 QVirtQueue *vq;
27 char *test_share;
28 uint16_t p9_req_tag;
29 } QVirtIO9P;
30
31 static QVirtIO9P *qvirtio_9p_start(const char *driver)
32 {
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);
37
38 v9p->test_share = g_strdup("/tmp/qtest.XXXXXX");
39 g_assert_nonnull(mkdtemp(v9p->test_share));
40
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);
48 }
49
50 return v9p;
51 }
52
53 static void qvirtio_9p_stop(QVirtIO9P *v9p)
54 {
55 qtest_shutdown(v9p->qs);
56 rmdir(v9p->test_share);
57 g_free(v9p->test_share);
58 g_free(v9p);
59 }
60
61 static QVirtIO9P *qvirtio_9p_pci_start(void)
62 {
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;
69
70 qvirtio_pci_device_enable(dev);
71 qvirtio_reset(v9p->dev);
72 qvirtio_set_acknowledge(v9p->dev);
73 qvirtio_set_driver(v9p->dev);
74
75 v9p->vq = qvirtqueue_setup(v9p->dev, v9p->qs->alloc, 0);
76 return v9p;
77 }
78
79 static void qvirtio_9p_pci_stop(QVirtIO9P *v9p)
80 {
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);
85 }
86
87 static void pci_config(QVirtIO9P *v9p)
88 {
89 size_t tag_len = qvirtio_config_readw(v9p->dev, 0);
90 char *tag;
91 int i;
92
93 g_assert_cmpint(tag_len, ==, strlen(mount_tag));
94
95 tag = g_malloc(tag_len);
96 for (i = 0; i < tag_len; i++) {
97 tag[i] = qvirtio_config_readb(v9p->dev, i + 2);
98 }
99 g_assert_cmpmem(tag, tag_len, mount_tag, tag_len);
100 g_free(tag);
101 }
102
103 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
104
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;
115
116 static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
117 {
118 memwrite(req->t_msg + req->t_off, addr, len);
119 req->t_off += len;
120 }
121
122 static void v9fs_memskip(P9Req *req, size_t len)
123 {
124 req->r_off += len;
125 }
126
127 static void v9fs_memrewind(P9Req *req, size_t len)
128 {
129 req->r_off -= len;
130 }
131
132 static void v9fs_memread(P9Req *req, void *addr, size_t len)
133 {
134 memread(req->r_msg + req->r_off, addr, len);
135 req->r_off += len;
136 }
137
138 static void v9fs_uint16_write(P9Req *req, uint16_t val)
139 {
140 uint16_t le_val = cpu_to_le16(val);
141
142 v9fs_memwrite(req, &le_val, 2);
143 }
144
145 static void v9fs_uint16_read(P9Req *req, uint16_t *val)
146 {
147 v9fs_memread(req, val, 2);
148 le16_to_cpus(val);
149 }
150
151 static void v9fs_uint32_write(P9Req *req, uint32_t val)
152 {
153 uint32_t le_val = cpu_to_le32(val);
154
155 v9fs_memwrite(req, &le_val, 4);
156 }
157
158 static void v9fs_uint32_read(P9Req *req, uint32_t *val)
159 {
160 v9fs_memread(req, val, 4);
161 le32_to_cpus(val);
162 }
163
164 /* len[2] string[len] */
165 static uint16_t v9fs_string_size(const char *string)
166 {
167 size_t len = strlen(string);
168
169 g_assert_cmpint(len, <=, UINT16_MAX);
170
171 return 2 + len;
172 }
173
174 static void v9fs_string_write(P9Req *req, const char *string)
175 {
176 int len = strlen(string);
177
178 g_assert_cmpint(len, <=, UINT16_MAX);
179
180 v9fs_uint16_write(req, (uint16_t) len);
181 v9fs_memwrite(req, string, len);
182 }
183
184 static void v9fs_string_read(P9Req *req, uint16_t *len, char **string)
185 {
186 uint16_t local_len;
187
188 v9fs_uint16_read(req, &local_len);
189 if (len) {
190 *len = local_len;
191 }
192 if (string) {
193 *string = g_malloc(local_len);
194 v9fs_memread(req, *string, local_len);
195 } else {
196 v9fs_memskip(req, local_len);
197 }
198 }
199
200 typedef struct {
201 uint32_t size;
202 uint8_t id;
203 uint16_t tag;
204 } QEMU_PACKED P9Hdr;
205
206 static P9Req *v9fs_req_init(QVirtIO9P *v9p, uint32_t size, uint8_t id,
207 uint16_t tag)
208 {
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)
215 };
216
217 g_assert_cmpint(t_size, <=, P9_MAX_SIZE);
218
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;
225 }
226
227 static void v9fs_req_send(P9Req *req)
228 {
229 QVirtIO9P *v9p = req->v9p;
230 uint32_t free_head;
231
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;
237 }
238
239 static const char *rmessage_name(uint8_t id)
240 {
241 return
242 id == P9_RLERROR ? "RLERROR" :
243 id == P9_RVERSION ? "RVERSION" :
244 id == P9_RATTACH ? "RATTACH" :
245 id == P9_RWALK ? "RWALK" :
246 "<unknown>";
247 }
248
249 static void v9fs_req_recv(P9Req *req, uint8_t id)
250 {
251 QVirtIO9P *v9p = req->v9p;
252 P9Hdr hdr;
253 int i;
254
255 for (i = 0; i < 10; i++) {
256 qvirtio_wait_queue_isr(v9p->dev, v9p->vq, 1000 * 1000);
257
258 v9fs_memread(req, &hdr, 7);
259 le32_to_cpus(&hdr.size);
260 le16_to_cpus(&hdr.tag);
261 if (hdr.size >= 7) {
262 break;
263 }
264 v9fs_memrewind(req, 7);
265 }
266
267 g_assert_cmpint(hdr.size, >=, 7);
268 g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE);
269 g_assert_cmpint(hdr.tag, ==, req->tag);
270
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));
274
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));
279 }
280 }
281 g_assert_cmpint(hdr.id, ==, id);
282 }
283
284 static void v9fs_req_free(P9Req *req)
285 {
286 QVirtIO9P *v9p = req->v9p;
287
288 guest_free(v9p->qs->alloc, req->t_msg);
289 guest_free(v9p->qs->alloc, req->r_msg);
290 g_free(req);
291 }
292
293 /* size[4] Rlerror tag[2] ecode[4] */
294 static void v9fs_rlerror(P9Req *req, uint32_t *err)
295 {
296 v9fs_req_recv(req, P9_RLERROR);
297 v9fs_uint32_read(req, err);
298 v9fs_req_free(req);
299 }
300
301 /* size[4] Tversion tag[2] msize[4] version[s] */
302 static P9Req *v9fs_tversion(QVirtIO9P *v9p, uint32_t msize, const char *version)
303 {
304 P9Req *req = v9fs_req_init(v9p, 4 + v9fs_string_size(version), P9_TVERSION,
305 P9_NOTAG);
306
307 v9fs_uint32_write(req, msize);
308 v9fs_string_write(req, version);
309 v9fs_req_send(req);
310 return req;
311 }
312
313 /* size[4] Rversion tag[2] msize[4] version[s] */
314 static void v9fs_rversion(P9Req *req, uint16_t *len, char **version)
315 {
316 uint32_t msize;
317
318 v9fs_req_recv(req, P9_RVERSION);
319 v9fs_uint32_read(req, &msize);
320
321 g_assert_cmpint(msize, ==, P9_MAX_SIZE);
322
323 if (len || version) {
324 v9fs_string_read(req, len, version);
325 }
326
327 v9fs_req_free(req);
328 }
329
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)
332 {
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));
337
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;
345 }
346
347 typedef char v9fs_qid[13];
348
349 /* size[4] Rattach tag[2] qid[13] */
350 static void v9fs_rattach(P9Req *req, v9fs_qid *qid)
351 {
352 v9fs_req_recv(req, P9_RATTACH);
353 if (qid) {
354 v9fs_memread(req, qid, 13);
355 }
356 v9fs_req_free(req);
357 }
358
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[])
362 {
363 P9Req *req;
364 int i;
365 uint32_t size = 4 + 4 + 2;
366
367 for (i = 0; i < nwname; i++) {
368 size += v9fs_string_size(wnames[i]);
369 }
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]);
376 }
377 v9fs_req_send(req);
378 return req;
379 }
380
381 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
382 static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid)
383 {
384 uint16_t local_nwqid;
385
386 v9fs_req_recv(req, P9_RWALK);
387 v9fs_uint16_read(req, &local_nwqid);
388 if (nwqid) {
389 *nwqid = local_nwqid;
390 }
391 if (wqid) {
392 *wqid = g_malloc(local_nwqid * 13);
393 v9fs_memread(req, *wqid, local_nwqid * 13);
394 }
395 v9fs_req_free(req);
396 }
397
398 static void fs_version(QVirtIO9P *v9p)
399 {
400 const char *version = "9P2000.L";
401 uint16_t server_len;
402 char *server_version;
403 P9Req *req;
404
405 req = v9fs_tversion(v9p, P9_MAX_SIZE, version);
406 v9fs_rversion(req, &server_len, &server_version);
407
408 g_assert_cmpmem(server_version, server_len, version, strlen(version));
409
410 g_free(server_version);
411 }
412
413 static void fs_attach(QVirtIO9P *v9p)
414 {
415 P9Req *req;
416
417 fs_version(v9p);
418 req = v9fs_tattach(v9p, 0, getuid());
419 v9fs_rattach(req, NULL);
420 }
421
422 static void fs_walk(QVirtIO9P *v9p)
423 {
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;
430
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));
435 }
436
437 fs_attach(v9p);
438 req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames);
439 v9fs_rwalk(req, &nwqid, &wqid);
440
441 g_assert_cmpint(nwqid, ==, P9_MAXWELEM);
442
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]);
447 }
448
449 g_free(wqid);
450 }
451
452 static void fs_walk_no_slash(QVirtIO9P *v9p)
453 {
454 char *const wnames[] = { g_strdup(" /") };
455 P9Req *req;
456 uint32_t err;
457
458 fs_attach(v9p);
459 req = v9fs_twalk(v9p, 0, 1, 1, wnames);
460 v9fs_rlerror(req, &err);
461
462 g_assert_cmpint(err, ==, ENOENT);
463
464 g_free(wnames[0]);
465 }
466
467 static void fs_walk_dotdot(QVirtIO9P *v9p)
468 {
469 char *const wnames[] = { g_strdup("..") };
470 v9fs_qid root_qid, *wqid;
471 P9Req *req;
472
473 fs_version(v9p);
474 req = v9fs_tattach(v9p, 0, getuid());
475 v9fs_rattach(req, &root_qid);
476
477 req = v9fs_twalk(v9p, 0, 1, 1, wnames);
478 v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */
479
480 g_assert_cmpmem(&root_qid, 13, wqid[0], 13);
481
482 g_free(wqid);
483 g_free(wnames[0]);
484 }
485
486 typedef void (*v9fs_test_fn)(QVirtIO9P *v9p);
487
488 static void v9fs_run_pci_test(gconstpointer data)
489 {
490 v9fs_test_fn fn = data;
491 QVirtIO9P *v9p = qvirtio_9p_pci_start();
492
493 if (fn) {
494 fn(v9p);
495 }
496 qvirtio_9p_pci_stop(v9p);
497 }
498
499 static void v9fs_qtest_pci_add(const char *path, v9fs_test_fn fn)
500 {
501 qtest_add_data_func(path, fn, v9fs_run_pci_test);
502 }
503
504 int main(int argc, char **argv)
505 {
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);
515
516 return g_test_run();
517 }