1
2
3
4
5
6
7
8
9
10#include "qemu/osdep.h"
11#include "libqtest.h"
12#include "hw/9pfs/9p.h"
13#include "hw/9pfs/9p-synth.h"
14#include "libqos/virtio-9p.h"
15#include "libqos/qgraph.h"
16
17#define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
18static QGuestAllocator *alloc;
19
20static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc)
21{
22 QVirtio9P *v9p = obj;
23 alloc = t_alloc;
24 size_t tag_len = qvirtio_config_readw(v9p->vdev, 0);
25 char *tag;
26 int i;
27
28 g_assert_cmpint(tag_len, ==, strlen(MOUNT_TAG));
29
30 tag = g_malloc(tag_len);
31 for (i = 0; i < tag_len; i++) {
32 tag[i] = qvirtio_config_readb(v9p->vdev, i + 2);
33 }
34 g_assert_cmpmem(tag, tag_len, MOUNT_TAG, tag_len);
35 g_free(tag);
36}
37
38#define P9_MAX_SIZE 4096
39
40typedef struct {
41 QVirtio9P *v9p;
42 uint16_t tag;
43 uint64_t t_msg;
44 uint32_t t_size;
45 uint64_t r_msg;
46
47 size_t t_off;
48 size_t r_off;
49 uint32_t free_head;
50} P9Req;
51
52static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
53{
54 memwrite(req->t_msg + req->t_off, addr, len);
55 req->t_off += len;
56}
57
58static void v9fs_memskip(P9Req *req, size_t len)
59{
60 req->r_off += len;
61}
62
63static void v9fs_memread(P9Req *req, void *addr, size_t len)
64{
65 memread(req->r_msg + req->r_off, addr, len);
66 req->r_off += len;
67}
68
69static void v9fs_uint16_write(P9Req *req, uint16_t val)
70{
71 uint16_t le_val = cpu_to_le16(val);
72
73 v9fs_memwrite(req, &le_val, 2);
74}
75
76static void v9fs_uint16_read(P9Req *req, uint16_t *val)
77{
78 v9fs_memread(req, val, 2);
79 le16_to_cpus(val);
80}
81
82static void v9fs_uint32_write(P9Req *req, uint32_t val)
83{
84 uint32_t le_val = cpu_to_le32(val);
85
86 v9fs_memwrite(req, &le_val, 4);
87}
88
89static void v9fs_uint64_write(P9Req *req, uint64_t val)
90{
91 uint64_t le_val = cpu_to_le64(val);
92
93 v9fs_memwrite(req, &le_val, 8);
94}
95
96static void v9fs_uint32_read(P9Req *req, uint32_t *val)
97{
98 v9fs_memread(req, val, 4);
99 le32_to_cpus(val);
100}
101
102
103static uint16_t v9fs_string_size(const char *string)
104{
105 size_t len = strlen(string);
106
107 g_assert_cmpint(len, <=, UINT16_MAX - 2);
108
109 return 2 + len;
110}
111
112static void v9fs_string_write(P9Req *req, const char *string)
113{
114 int len = strlen(string);
115
116 g_assert_cmpint(len, <=, UINT16_MAX);
117
118 v9fs_uint16_write(req, (uint16_t) len);
119 v9fs_memwrite(req, string, len);
120}
121
122static void v9fs_string_read(P9Req *req, uint16_t *len, char **string)
123{
124 uint16_t local_len;
125
126 v9fs_uint16_read(req, &local_len);
127 if (len) {
128 *len = local_len;
129 }
130 if (string) {
131 *string = g_malloc(local_len);
132 v9fs_memread(req, *string, local_len);
133 } else {
134 v9fs_memskip(req, local_len);
135 }
136}
137
138 typedef struct {
139 uint32_t size;
140 uint8_t id;
141 uint16_t tag;
142} QEMU_PACKED P9Hdr;
143
144static P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id,
145 uint16_t tag)
146{
147 P9Req *req = g_new0(P9Req, 1);
148 uint32_t total_size = 7;
149 P9Hdr hdr = {
150 .id = id,
151 .tag = cpu_to_le16(tag)
152 };
153
154 g_assert_cmpint(total_size, <=, UINT32_MAX - size);
155 total_size += size;
156 hdr.size = cpu_to_le32(total_size);
157
158 g_assert_cmpint(total_size, <=, P9_MAX_SIZE);
159
160 req->v9p = v9p;
161 req->t_size = total_size;
162 req->t_msg = guest_alloc(alloc, req->t_size);
163 v9fs_memwrite(req, &hdr, 7);
164 req->tag = tag;
165 return req;
166}
167
168static void v9fs_req_send(P9Req *req)
169{
170 QVirtio9P *v9p = req->v9p;
171
172 req->r_msg = guest_alloc(alloc, P9_MAX_SIZE);
173 req->free_head = qvirtqueue_add(v9p->vq, req->t_msg, req->t_size, false,
174 true);
175 qvirtqueue_add(v9p->vq, req->r_msg, P9_MAX_SIZE, true, false);
176 qvirtqueue_kick(v9p->vdev, v9p->vq, req->free_head);
177 req->t_off = 0;
178}
179
180static const char *rmessage_name(uint8_t id)
181{
182 return
183 id == P9_RLERROR ? "RLERROR" :
184 id == P9_RVERSION ? "RVERSION" :
185 id == P9_RATTACH ? "RATTACH" :
186 id == P9_RWALK ? "RWALK" :
187 id == P9_RLOPEN ? "RLOPEN" :
188 id == P9_RWRITE ? "RWRITE" :
189 id == P9_RFLUSH ? "RFLUSH" :
190 "<unknown>";
191}
192
193static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len)
194{
195 QVirtio9P *v9p = req->v9p;
196
197 qvirtio_wait_used_elem(v9p->vdev, v9p->vq, req->free_head, len,
198 QVIRTIO_9P_TIMEOUT_US);
199}
200
201static void v9fs_req_recv(P9Req *req, uint8_t id)
202{
203 P9Hdr hdr;
204
205 v9fs_memread(req, &hdr, 7);
206 hdr.size = ldl_le_p(&hdr.size);
207 hdr.tag = lduw_le_p(&hdr.tag);
208
209 g_assert_cmpint(hdr.size, >=, 7);
210 g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE);
211 g_assert_cmpint(hdr.tag, ==, req->tag);
212
213 if (hdr.id != id) {
214 g_printerr("Received response %d (%s) instead of %d (%s)\n",
215 hdr.id, rmessage_name(hdr.id), id, rmessage_name(id));
216
217 if (hdr.id == P9_RLERROR) {
218 uint32_t err;
219 v9fs_uint32_read(req, &err);
220 g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err));
221 }
222 }
223 g_assert_cmpint(hdr.id, ==, id);
224}
225
226static void v9fs_req_free(P9Req *req)
227{
228 guest_free(alloc, req->t_msg);
229 guest_free(alloc, req->r_msg);
230 g_free(req);
231}
232
233
234static void v9fs_rlerror(P9Req *req, uint32_t *err)
235{
236 v9fs_req_recv(req, P9_RLERROR);
237 v9fs_uint32_read(req, err);
238 v9fs_req_free(req);
239}
240
241
242static P9Req *v9fs_tversion(QVirtio9P *v9p, uint32_t msize, const char *version,
243 uint16_t tag)
244{
245 P9Req *req;
246 uint32_t body_size = 4;
247 uint16_t string_size = v9fs_string_size(version);
248
249 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
250 body_size += string_size;
251 req = v9fs_req_init(v9p, body_size, P9_TVERSION, tag);
252
253 v9fs_uint32_write(req, msize);
254 v9fs_string_write(req, version);
255 v9fs_req_send(req);
256 return req;
257}
258
259
260static void v9fs_rversion(P9Req *req, uint16_t *len, char **version)
261{
262 uint32_t msize;
263
264 v9fs_req_recv(req, P9_RVERSION);
265 v9fs_uint32_read(req, &msize);
266
267 g_assert_cmpint(msize, ==, P9_MAX_SIZE);
268
269 if (len || version) {
270 v9fs_string_read(req, len, version);
271 }
272
273 v9fs_req_free(req);
274}
275
276
277static P9Req *v9fs_tattach(QVirtio9P *v9p, uint32_t fid, uint32_t n_uname,
278 uint16_t tag)
279{
280 const char *uname = "";
281 const char *aname = "";
282 P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag);
283
284 v9fs_uint32_write(req, fid);
285 v9fs_uint32_write(req, P9_NOFID);
286 v9fs_string_write(req, uname);
287 v9fs_string_write(req, aname);
288 v9fs_uint32_write(req, n_uname);
289 v9fs_req_send(req);
290 return req;
291}
292
293typedef char v9fs_qid[13];
294
295
296static void v9fs_rattach(P9Req *req, v9fs_qid *qid)
297{
298 v9fs_req_recv(req, P9_RATTACH);
299 if (qid) {
300 v9fs_memread(req, qid, 13);
301 }
302 v9fs_req_free(req);
303}
304
305
306static P9Req *v9fs_twalk(QVirtio9P *v9p, uint32_t fid, uint32_t newfid,
307 uint16_t nwname, char *const wnames[], uint16_t tag)
308{
309 P9Req *req;
310 int i;
311 uint32_t body_size = 4 + 4 + 2;
312
313 for (i = 0; i < nwname; i++) {
314 uint16_t wname_size = v9fs_string_size(wnames[i]);
315
316 g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size);
317 body_size += wname_size;
318 }
319 req = v9fs_req_init(v9p, body_size, P9_TWALK, tag);
320 v9fs_uint32_write(req, fid);
321 v9fs_uint32_write(req, newfid);
322 v9fs_uint16_write(req, nwname);
323 for (i = 0; i < nwname; i++) {
324 v9fs_string_write(req, wnames[i]);
325 }
326 v9fs_req_send(req);
327 return req;
328}
329
330
331static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid)
332{
333 uint16_t local_nwqid;
334
335 v9fs_req_recv(req, P9_RWALK);
336 v9fs_uint16_read(req, &local_nwqid);
337 if (nwqid) {
338 *nwqid = local_nwqid;
339 }
340 if (wqid) {
341 *wqid = g_malloc(local_nwqid * 13);
342 v9fs_memread(req, *wqid, local_nwqid * 13);
343 }
344 v9fs_req_free(req);
345}
346
347
348static P9Req *v9fs_tlopen(QVirtio9P *v9p, uint32_t fid, uint32_t flags,
349 uint16_t tag)
350{
351 P9Req *req;
352
353 req = v9fs_req_init(v9p, 4 + 4, P9_TLOPEN, tag);
354 v9fs_uint32_write(req, fid);
355 v9fs_uint32_write(req, flags);
356 v9fs_req_send(req);
357 return req;
358}
359
360
361static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
362{
363 v9fs_req_recv(req, P9_RLOPEN);
364 if (qid) {
365 v9fs_memread(req, qid, 13);
366 } else {
367 v9fs_memskip(req, 13);
368 }
369 if (iounit) {
370 v9fs_uint32_read(req, iounit);
371 }
372 v9fs_req_free(req);
373}
374
375
376static P9Req *v9fs_twrite(QVirtio9P *v9p, uint32_t fid, uint64_t offset,
377 uint32_t count, const void *data, uint16_t tag)
378{
379 P9Req *req;
380 uint32_t body_size = 4 + 8 + 4;
381
382 g_assert_cmpint(body_size, <=, UINT32_MAX - count);
383 body_size += count;
384 req = v9fs_req_init(v9p, body_size, P9_TWRITE, tag);
385 v9fs_uint32_write(req, fid);
386 v9fs_uint64_write(req, offset);
387 v9fs_uint32_write(req, count);
388 v9fs_memwrite(req, data, count);
389 v9fs_req_send(req);
390 return req;
391}
392
393
394static void v9fs_rwrite(P9Req *req, uint32_t *count)
395{
396 v9fs_req_recv(req, P9_RWRITE);
397 if (count) {
398 v9fs_uint32_read(req, count);
399 }
400 v9fs_req_free(req);
401}
402
403
404static P9Req *v9fs_tflush(QVirtio9P *v9p, uint16_t oldtag, uint16_t tag)
405{
406 P9Req *req;
407
408 req = v9fs_req_init(v9p, 2, P9_TFLUSH, tag);
409 v9fs_uint32_write(req, oldtag);
410 v9fs_req_send(req);
411 return req;
412}
413
414
415static void v9fs_rflush(P9Req *req)
416{
417 v9fs_req_recv(req, P9_RFLUSH);
418 v9fs_req_free(req);
419}
420
421static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
422{
423 QVirtio9P *v9p = obj;
424 alloc = t_alloc;
425 const char *version = "9P2000.L";
426 uint16_t server_len;
427 char *server_version;
428 P9Req *req;
429
430 req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG);
431 v9fs_req_wait_for_reply(req, NULL);
432 v9fs_rversion(req, &server_len, &server_version);
433
434 g_assert_cmpmem(server_version, server_len, version, strlen(version));
435
436 g_free(server_version);
437}
438
439static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
440{
441 QVirtio9P *v9p = obj;
442 alloc = t_alloc;
443 P9Req *req;
444
445 fs_version(v9p, NULL, t_alloc);
446 req = v9fs_tattach(v9p, 0, getuid(), 0);
447 v9fs_req_wait_for_reply(req, NULL);
448 v9fs_rattach(req, NULL);
449}
450
451static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc)
452{
453 QVirtio9P *v9p = obj;
454 alloc = t_alloc;
455 char *wnames[P9_MAXWELEM];
456 uint16_t nwqid;
457 v9fs_qid *wqid;
458 int i;
459 P9Req *req;
460
461 for (i = 0; i < P9_MAXWELEM; i++) {
462 wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
463 }
464
465 fs_attach(v9p, NULL, t_alloc);
466 req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0);
467 v9fs_req_wait_for_reply(req, NULL);
468 v9fs_rwalk(req, &nwqid, &wqid);
469
470 g_assert_cmpint(nwqid, ==, P9_MAXWELEM);
471
472 for (i = 0; i < P9_MAXWELEM; i++) {
473 g_free(wnames[i]);
474 }
475
476 g_free(wqid);
477}
478
479static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc)
480{
481 QVirtio9P *v9p = obj;
482 alloc = t_alloc;
483 char *const wnames[] = { g_strdup(" /") };
484 P9Req *req;
485 uint32_t err;
486
487 fs_attach(v9p, NULL, t_alloc);
488 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
489 v9fs_req_wait_for_reply(req, NULL);
490 v9fs_rlerror(req, &err);
491
492 g_assert_cmpint(err, ==, ENOENT);
493
494 g_free(wnames[0]);
495}
496
497static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc)
498{
499 QVirtio9P *v9p = obj;
500 alloc = t_alloc;
501 char *const wnames[] = { g_strdup("..") };
502 v9fs_qid root_qid, *wqid;
503 P9Req *req;
504
505 fs_version(v9p, NULL, t_alloc);
506 req = v9fs_tattach(v9p, 0, getuid(), 0);
507 v9fs_req_wait_for_reply(req, NULL);
508 v9fs_rattach(req, &root_qid);
509
510 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
511 v9fs_req_wait_for_reply(req, NULL);
512 v9fs_rwalk(req, NULL, &wqid);
513
514 g_assert_cmpmem(&root_qid, 13, wqid[0], 13);
515
516 g_free(wqid);
517 g_free(wnames[0]);
518}
519
520static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc)
521{
522 QVirtio9P *v9p = obj;
523 alloc = t_alloc;
524 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) };
525 P9Req *req;
526
527 fs_attach(v9p, NULL, t_alloc);
528 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
529 v9fs_req_wait_for_reply(req, NULL);
530 v9fs_rwalk(req, NULL, NULL);
531
532 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
533 v9fs_req_wait_for_reply(req, NULL);
534 v9fs_rlopen(req, NULL, NULL);
535
536 g_free(wnames[0]);
537}
538
539static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc)
540{
541 QVirtio9P *v9p = obj;
542 alloc = t_alloc;
543 static const uint32_t write_count = P9_MAX_SIZE / 2;
544 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) };
545 char *buf = g_malloc0(write_count);
546 uint32_t count;
547 P9Req *req;
548
549 fs_attach(v9p, NULL, t_alloc);
550 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
551 v9fs_req_wait_for_reply(req, NULL);
552 v9fs_rwalk(req, NULL, NULL);
553
554 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
555 v9fs_req_wait_for_reply(req, NULL);
556 v9fs_rlopen(req, NULL, NULL);
557
558 req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0);
559 v9fs_req_wait_for_reply(req, NULL);
560 v9fs_rwrite(req, &count);
561 g_assert_cmpint(count, ==, write_count);
562
563 g_free(buf);
564 g_free(wnames[0]);
565}
566
567static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc)
568{
569 QVirtio9P *v9p = obj;
570 alloc = t_alloc;
571 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
572 P9Req *req, *flush_req;
573 uint32_t reply_len;
574 uint8_t should_block;
575
576 fs_attach(v9p, NULL, t_alloc);
577 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
578 v9fs_req_wait_for_reply(req, NULL);
579 v9fs_rwalk(req, NULL, NULL);
580
581 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
582 v9fs_req_wait_for_reply(req, NULL);
583 v9fs_rlopen(req, NULL, NULL);
584
585
586
587
588 should_block = 1;
589 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
590
591 flush_req = v9fs_tflush(v9p, req->tag, 1);
592
593
594
595
596 v9fs_req_wait_for_reply(req, &reply_len);
597 g_assert_cmpint(reply_len, ==, 0);
598 v9fs_req_free(req);
599 v9fs_rflush(flush_req);
600
601 g_free(wnames[0]);
602}
603
604static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc)
605{
606 QVirtio9P *v9p = obj;
607 alloc = t_alloc;
608 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
609 P9Req *req, *flush_req;
610 uint32_t count;
611 uint8_t should_block;
612
613 fs_attach(v9p, NULL, t_alloc);
614 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
615 v9fs_req_wait_for_reply(req, NULL);
616 v9fs_rwalk(req, NULL, NULL);
617
618 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
619 v9fs_req_wait_for_reply(req, NULL);
620 v9fs_rlopen(req, NULL, NULL);
621
622
623
624
625 should_block = 0;
626 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
627
628 flush_req = v9fs_tflush(v9p, req->tag, 1);
629
630
631
632
633 v9fs_req_wait_for_reply(req, NULL);
634 v9fs_rwrite(req, &count);
635 g_assert_cmpint(count, ==, sizeof(should_block));
636 v9fs_rflush(flush_req);
637
638 g_free(wnames[0]);
639}
640
641static void register_virtio_9p_test(void)
642{
643 qos_add_test("config", "virtio-9p", pci_config, NULL);
644 qos_add_test("fs/version/basic", "virtio-9p", fs_version, NULL);
645 qos_add_test("fs/attach/basic", "virtio-9p", fs_attach, NULL);
646 qos_add_test("fs/walk/basic", "virtio-9p", fs_walk, NULL);
647 qos_add_test("fs/walk/no_slash", "virtio-9p", fs_walk_no_slash,
648 NULL);
649 qos_add_test("fs/walk/dotdot_from_root", "virtio-9p",
650 fs_walk_dotdot, NULL);
651 qos_add_test("fs/lopen/basic", "virtio-9p", fs_lopen, NULL);
652 qos_add_test("fs/write/basic", "virtio-9p", fs_write, NULL);
653 qos_add_test("fs/flush/success", "virtio-9p", fs_flush_success,
654 NULL);
655 qos_add_test("fs/flush/ignored", "virtio-9p", fs_flush_ignored,
656 NULL);
657}
658
659libqos_init(register_virtio_9p_test);
660