]>
Commit | Line | Data |
---|---|---|
2d888c09 AF |
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 | ||
fbc04127 | 10 | #include "qemu/osdep.h" |
2d888c09 AF |
11 | #include "libqtest.h" |
12 | #include "qemu-common.h" | |
a980f7f2 | 13 | #include "libqos/libqos-pc.h" |
30ca440e | 14 | #include "libqos/libqos-spapr.h" |
557a4cc0 GK |
15 | #include "libqos/virtio.h" |
16 | #include "libqos/virtio-pci.h" | |
557a4cc0 GK |
17 | #include "standard-headers/linux/virtio_ids.h" |
18 | #include "standard-headers/linux/virtio_pci.h" | |
6cc9906b | 19 | #include "hw/9pfs/9p.h" |
2893ddd5 | 20 | #include "hw/9pfs/9p-synth.h" |
2d888c09 | 21 | |
65b70fc7 GK |
22 | #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000) |
23 | ||
993f8054 | 24 | static const char mount_tag[] = "qtest"; |
2d888c09 | 25 | |
1211d81b GK |
26 | typedef struct { |
27 | QVirtioDevice *dev; | |
28 | QOSState *qs; | |
29 | QVirtQueue *vq; | |
1211d81b | 30 | } QVirtIO9P; |
a980f7f2 | 31 | |
1211d81b | 32 | static QVirtIO9P *qvirtio_9p_start(const char *driver) |
2d888c09 | 33 | { |
30ca440e | 34 | const char *arch = qtest_get_arch(); |
2893ddd5 | 35 | const char *cmd = "-fsdev synth,id=fsdev0 " |
1211d81b GK |
36 | "-device %s,fsdev=fsdev0,mount_tag=%s"; |
37 | QVirtIO9P *v9p = g_new0(QVirtIO9P, 1); | |
2d888c09 | 38 | |
30ca440e | 39 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
2893ddd5 | 40 | v9p->qs = qtest_pc_boot(cmd, driver, mount_tag); |
1211d81b | 41 | } else if (strcmp(arch, "ppc64") == 0) { |
2893ddd5 | 42 | v9p->qs = qtest_spapr_boot(cmd, driver, mount_tag); |
1211d81b GK |
43 | } else { |
44 | g_printerr("virtio-9p tests are only available on x86 or ppc64\n"); | |
45 | exit(EXIT_FAILURE); | |
30ca440e | 46 | } |
3d95fb97 | 47 | global_qtest = v9p->qs->qts; |
30ca440e | 48 | |
1211d81b | 49 | return v9p; |
993f8054 GK |
50 | } |
51 | ||
1211d81b | 52 | static void qvirtio_9p_stop(QVirtIO9P *v9p) |
993f8054 | 53 | { |
1211d81b | 54 | qtest_shutdown(v9p->qs); |
1211d81b | 55 | g_free(v9p); |
993f8054 GK |
56 | } |
57 | ||
1211d81b | 58 | static QVirtIO9P *qvirtio_9p_pci_start(void) |
557a4cc0 | 59 | { |
1211d81b GK |
60 | QVirtIO9P *v9p = qvirtio_9p_start("virtio-9p-pci"); |
61 | QVirtioPCIDevice *dev = qvirtio_pci_device_find(v9p->qs->pcibus, | |
62 | VIRTIO_ID_9P); | |
557a4cc0 GK |
63 | g_assert_nonnull(dev); |
64 | g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_9P); | |
65 | v9p->dev = (QVirtioDevice *) dev; | |
66 | ||
67 | qvirtio_pci_device_enable(dev); | |
34c97748 | 68 | qvirtio_start_device(v9p->dev); |
557a4cc0 | 69 | |
eb5937ba | 70 | v9p->vq = qvirtqueue_setup(v9p->dev, &v9p->qs->alloc, 0); |
d8803b1a GK |
71 | |
72 | qvirtio_set_driver_ok(v9p->dev); | |
73 | ||
557a4cc0 GK |
74 | return v9p; |
75 | } | |
76 | ||
1211d81b | 77 | static void qvirtio_9p_pci_stop(QVirtIO9P *v9p) |
557a4cc0 | 78 | { |
eb5937ba | 79 | qvirtqueue_cleanup(v9p->dev->bus, v9p->vq, &v9p->qs->alloc); |
557a4cc0 | 80 | qvirtio_pci_device_disable(container_of(v9p->dev, QVirtioPCIDevice, vdev)); |
2b880bcd | 81 | qvirtio_pci_device_free((QVirtioPCIDevice *)v9p->dev); |
1211d81b | 82 | qvirtio_9p_stop(v9p); |
557a4cc0 GK |
83 | } |
84 | ||
1211d81b | 85 | static void pci_config(QVirtIO9P *v9p) |
557a4cc0 | 86 | { |
1211d81b | 87 | size_t tag_len = qvirtio_config_readw(v9p->dev, 0); |
557a4cc0 GK |
88 | char *tag; |
89 | int i; | |
557a4cc0 | 90 | |
557a4cc0 | 91 | g_assert_cmpint(tag_len, ==, strlen(mount_tag)); |
557a4cc0 GK |
92 | |
93 | tag = g_malloc(tag_len); | |
94 | for (i = 0; i < tag_len; i++) { | |
246fc0fb | 95 | tag[i] = qvirtio_config_readb(v9p->dev, i + 2); |
557a4cc0 GK |
96 | } |
97 | g_assert_cmpmem(tag, tag_len, mount_tag, tag_len); | |
98 | g_free(tag); | |
1211d81b GK |
99 | } |
100 | ||
6cc9906b GK |
101 | #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */ |
102 | ||
103 | typedef struct { | |
104 | QVirtIO9P *v9p; | |
105 | uint16_t tag; | |
106 | uint64_t t_msg; | |
107 | uint32_t t_size; | |
108 | uint64_t r_msg; | |
109 | /* No r_size, it is hardcoded to P9_MAX_SIZE */ | |
110 | size_t t_off; | |
111 | size_t r_off; | |
65b70fc7 | 112 | uint32_t free_head; |
6cc9906b GK |
113 | } P9Req; |
114 | ||
115 | static void v9fs_memwrite(P9Req *req, const void *addr, size_t len) | |
116 | { | |
117 | memwrite(req->t_msg + req->t_off, addr, len); | |
118 | req->t_off += len; | |
119 | } | |
120 | ||
121 | static void v9fs_memskip(P9Req *req, size_t len) | |
122 | { | |
123 | req->r_off += len; | |
124 | } | |
125 | ||
6cc9906b GK |
126 | static void v9fs_memread(P9Req *req, void *addr, size_t len) |
127 | { | |
128 | memread(req->r_msg + req->r_off, addr, len); | |
129 | req->r_off += len; | |
130 | } | |
131 | ||
132 | static void v9fs_uint16_write(P9Req *req, uint16_t val) | |
133 | { | |
134 | uint16_t le_val = cpu_to_le16(val); | |
135 | ||
136 | v9fs_memwrite(req, &le_val, 2); | |
137 | } | |
138 | ||
139 | static void v9fs_uint16_read(P9Req *req, uint16_t *val) | |
140 | { | |
141 | v9fs_memread(req, val, 2); | |
142 | le16_to_cpus(val); | |
143 | } | |
144 | ||
145 | static void v9fs_uint32_write(P9Req *req, uint32_t val) | |
146 | { | |
147 | uint32_t le_val = cpu_to_le32(val); | |
148 | ||
149 | v9fs_memwrite(req, &le_val, 4); | |
150 | } | |
151 | ||
354b86f8 GK |
152 | static void v9fs_uint64_write(P9Req *req, uint64_t val) |
153 | { | |
154 | uint64_t le_val = cpu_to_le64(val); | |
155 | ||
156 | v9fs_memwrite(req, &le_val, 8); | |
157 | } | |
158 | ||
6cc9906b GK |
159 | static void v9fs_uint32_read(P9Req *req, uint32_t *val) |
160 | { | |
161 | v9fs_memread(req, val, 4); | |
162 | le32_to_cpus(val); | |
163 | } | |
164 | ||
165 | /* len[2] string[len] */ | |
166 | static uint16_t v9fs_string_size(const char *string) | |
167 | { | |
168 | size_t len = strlen(string); | |
169 | ||
9ea776ee | 170 | g_assert_cmpint(len, <=, UINT16_MAX - 2); |
6cc9906b GK |
171 | |
172 | return 2 + len; | |
173 | } | |
174 | ||
175 | static void v9fs_string_write(P9Req *req, const char *string) | |
176 | { | |
177 | int len = strlen(string); | |
178 | ||
179 | g_assert_cmpint(len, <=, UINT16_MAX); | |
180 | ||
181 | v9fs_uint16_write(req, (uint16_t) len); | |
182 | v9fs_memwrite(req, string, len); | |
183 | } | |
184 | ||
185 | static void v9fs_string_read(P9Req *req, uint16_t *len, char **string) | |
186 | { | |
187 | uint16_t local_len; | |
188 | ||
189 | v9fs_uint16_read(req, &local_len); | |
190 | if (len) { | |
191 | *len = local_len; | |
192 | } | |
193 | if (string) { | |
194 | *string = g_malloc(local_len); | |
195 | v9fs_memread(req, *string, local_len); | |
196 | } else { | |
197 | v9fs_memskip(req, local_len); | |
198 | } | |
199 | } | |
200 | ||
201 | typedef struct { | |
202 | uint32_t size; | |
203 | uint8_t id; | |
204 | uint16_t tag; | |
205 | } QEMU_PACKED P9Hdr; | |
206 | ||
207 | static P9Req *v9fs_req_init(QVirtIO9P *v9p, uint32_t size, uint8_t id, | |
208 | uint16_t tag) | |
209 | { | |
210 | P9Req *req = g_new0(P9Req, 1); | |
9ea776ee | 211 | uint32_t total_size = 7; /* 9P header has well-known size of 7 bytes */ |
6cc9906b | 212 | P9Hdr hdr = { |
6cc9906b GK |
213 | .id = id, |
214 | .tag = cpu_to_le16(tag) | |
215 | }; | |
216 | ||
9ea776ee GK |
217 | g_assert_cmpint(total_size, <=, UINT32_MAX - size); |
218 | total_size += size; | |
219 | hdr.size = cpu_to_le32(total_size); | |
220 | ||
221 | g_assert_cmpint(total_size, <=, P9_MAX_SIZE); | |
6cc9906b GK |
222 | |
223 | req->v9p = v9p; | |
9ea776ee | 224 | req->t_size = total_size; |
eb5937ba | 225 | req->t_msg = guest_alloc(&v9p->qs->alloc, req->t_size); |
6cc9906b GK |
226 | v9fs_memwrite(req, &hdr, 7); |
227 | req->tag = tag; | |
228 | return req; | |
229 | } | |
230 | ||
231 | static void v9fs_req_send(P9Req *req) | |
232 | { | |
233 | QVirtIO9P *v9p = req->v9p; | |
6cc9906b | 234 | |
eb5937ba | 235 | req->r_msg = guest_alloc(&v9p->qs->alloc, P9_MAX_SIZE); |
65b70fc7 GK |
236 | req->free_head = qvirtqueue_add(v9p->vq, req->t_msg, req->t_size, false, |
237 | true); | |
6cc9906b | 238 | qvirtqueue_add(v9p->vq, req->r_msg, P9_MAX_SIZE, true, false); |
65b70fc7 | 239 | qvirtqueue_kick(v9p->dev, v9p->vq, req->free_head); |
6cc9906b GK |
240 | req->t_off = 0; |
241 | } | |
242 | ||
6e37f458 GK |
243 | static const char *rmessage_name(uint8_t id) |
244 | { | |
245 | return | |
246 | id == P9_RLERROR ? "RLERROR" : | |
247 | id == P9_RVERSION ? "RVERSION" : | |
248 | id == P9_RATTACH ? "RATTACH" : | |
249 | id == P9_RWALK ? "RWALK" : | |
82469aae | 250 | id == P9_RLOPEN ? "RLOPEN" : |
354b86f8 | 251 | id == P9_RWRITE ? "RWRITE" : |
357e2f7f | 252 | id == P9_RFLUSH ? "RFLUSH" : |
6e37f458 GK |
253 | "<unknown>"; |
254 | } | |
255 | ||
357e2f7f | 256 | static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len) |
6cc9906b GK |
257 | { |
258 | QVirtIO9P *v9p = req->v9p; | |
6cc9906b | 259 | |
357e2f7f | 260 | qvirtio_wait_used_elem(v9p->dev, v9p->vq, req->free_head, len, |
65b70fc7 | 261 | QVIRTIO_9P_TIMEOUT_US); |
60b1fa9d GK |
262 | } |
263 | ||
264 | static void v9fs_req_recv(P9Req *req, uint8_t id) | |
265 | { | |
266 | P9Hdr hdr; | |
6cc9906b | 267 | |
65b70fc7 GK |
268 | v9fs_memread(req, &hdr, 7); |
269 | hdr.size = ldl_le_p(&hdr.size); | |
270 | hdr.tag = lduw_le_p(&hdr.tag); | |
6cc9906b GK |
271 | |
272 | g_assert_cmpint(hdr.size, >=, 7); | |
273 | g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE); | |
274 | g_assert_cmpint(hdr.tag, ==, req->tag); | |
275 | ||
6e37f458 GK |
276 | if (hdr.id != id) { |
277 | g_printerr("Received response %d (%s) instead of %d (%s)\n", | |
278 | hdr.id, rmessage_name(hdr.id), id, rmessage_name(id)); | |
279 | ||
280 | if (hdr.id == P9_RLERROR) { | |
281 | uint32_t err; | |
282 | v9fs_uint32_read(req, &err); | |
283 | g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err)); | |
284 | } | |
6cc9906b GK |
285 | } |
286 | g_assert_cmpint(hdr.id, ==, id); | |
287 | } | |
288 | ||
289 | static void v9fs_req_free(P9Req *req) | |
290 | { | |
291 | QVirtIO9P *v9p = req->v9p; | |
292 | ||
eb5937ba PB |
293 | guest_free(&v9p->qs->alloc, req->t_msg); |
294 | guest_free(&v9p->qs->alloc, req->r_msg); | |
6cc9906b GK |
295 | g_free(req); |
296 | } | |
297 | ||
ba0d1037 GK |
298 | /* size[4] Rlerror tag[2] ecode[4] */ |
299 | static void v9fs_rlerror(P9Req *req, uint32_t *err) | |
300 | { | |
301 | v9fs_req_recv(req, P9_RLERROR); | |
302 | v9fs_uint32_read(req, err); | |
303 | v9fs_req_free(req); | |
304 | } | |
305 | ||
6cc9906b | 306 | /* size[4] Tversion tag[2] msize[4] version[s] */ |
693b21d2 GK |
307 | static P9Req *v9fs_tversion(QVirtIO9P *v9p, uint32_t msize, const char *version, |
308 | uint16_t tag) | |
6cc9906b | 309 | { |
9ea776ee GK |
310 | P9Req *req; |
311 | uint32_t body_size = 4; | |
312 | uint16_t string_size = v9fs_string_size(version); | |
313 | ||
314 | g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); | |
315 | body_size += string_size; | |
316 | req = v9fs_req_init(v9p, body_size, P9_TVERSION, tag); | |
6cc9906b GK |
317 | |
318 | v9fs_uint32_write(req, msize); | |
319 | v9fs_string_write(req, version); | |
320 | v9fs_req_send(req); | |
321 | return req; | |
322 | } | |
323 | ||
324 | /* size[4] Rversion tag[2] msize[4] version[s] */ | |
325 | static void v9fs_rversion(P9Req *req, uint16_t *len, char **version) | |
326 | { | |
327 | uint32_t msize; | |
328 | ||
329 | v9fs_req_recv(req, P9_RVERSION); | |
330 | v9fs_uint32_read(req, &msize); | |
331 | ||
332 | g_assert_cmpint(msize, ==, P9_MAX_SIZE); | |
333 | ||
334 | if (len || version) { | |
335 | v9fs_string_read(req, len, version); | |
336 | } | |
337 | ||
338 | v9fs_req_free(req); | |
339 | } | |
340 | ||
5c3df1f0 | 341 | /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */ |
693b21d2 GK |
342 | static P9Req *v9fs_tattach(QVirtIO9P *v9p, uint32_t fid, uint32_t n_uname, |
343 | uint16_t tag) | |
5c3df1f0 GK |
344 | { |
345 | const char *uname = ""; /* ignored by QEMU */ | |
346 | const char *aname = ""; /* ignored by QEMU */ | |
693b21d2 | 347 | P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag); |
5c3df1f0 GK |
348 | |
349 | v9fs_uint32_write(req, fid); | |
350 | v9fs_uint32_write(req, P9_NOFID); | |
351 | v9fs_string_write(req, uname); | |
352 | v9fs_string_write(req, aname); | |
353 | v9fs_uint32_write(req, n_uname); | |
354 | v9fs_req_send(req); | |
355 | return req; | |
356 | } | |
357 | ||
358 | typedef char v9fs_qid[13]; | |
359 | ||
360 | /* size[4] Rattach tag[2] qid[13] */ | |
361 | static void v9fs_rattach(P9Req *req, v9fs_qid *qid) | |
362 | { | |
363 | v9fs_req_recv(req, P9_RATTACH); | |
364 | if (qid) { | |
365 | v9fs_memread(req, qid, 13); | |
366 | } | |
367 | v9fs_req_free(req); | |
368 | } | |
369 | ||
04b88c84 GK |
370 | /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */ |
371 | static P9Req *v9fs_twalk(QVirtIO9P *v9p, uint32_t fid, uint32_t newfid, | |
693b21d2 | 372 | uint16_t nwname, char *const wnames[], uint16_t tag) |
04b88c84 GK |
373 | { |
374 | P9Req *req; | |
375 | int i; | |
9ea776ee | 376 | uint32_t body_size = 4 + 4 + 2; |
04b88c84 GK |
377 | |
378 | for (i = 0; i < nwname; i++) { | |
9ea776ee GK |
379 | uint16_t wname_size = v9fs_string_size(wnames[i]); |
380 | ||
381 | g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size); | |
382 | body_size += wname_size; | |
04b88c84 | 383 | } |
9ea776ee | 384 | req = v9fs_req_init(v9p, body_size, P9_TWALK, tag); |
04b88c84 GK |
385 | v9fs_uint32_write(req, fid); |
386 | v9fs_uint32_write(req, newfid); | |
387 | v9fs_uint16_write(req, nwname); | |
388 | for (i = 0; i < nwname; i++) { | |
389 | v9fs_string_write(req, wnames[i]); | |
390 | } | |
391 | v9fs_req_send(req); | |
392 | return req; | |
393 | } | |
394 | ||
395 | /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */ | |
396 | static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid) | |
397 | { | |
398 | uint16_t local_nwqid; | |
399 | ||
400 | v9fs_req_recv(req, P9_RWALK); | |
401 | v9fs_uint16_read(req, &local_nwqid); | |
402 | if (nwqid) { | |
403 | *nwqid = local_nwqid; | |
404 | } | |
405 | if (wqid) { | |
406 | *wqid = g_malloc(local_nwqid * 13); | |
407 | v9fs_memread(req, *wqid, local_nwqid * 13); | |
408 | } | |
409 | v9fs_req_free(req); | |
410 | } | |
411 | ||
82469aae GK |
412 | /* size[4] Tlopen tag[2] fid[4] flags[4] */ |
413 | static P9Req *v9fs_tlopen(QVirtIO9P *v9p, uint32_t fid, uint32_t flags, | |
414 | uint16_t tag) | |
415 | { | |
416 | P9Req *req; | |
417 | ||
418 | req = v9fs_req_init(v9p, 4 + 4, P9_TLOPEN, tag); | |
419 | v9fs_uint32_write(req, fid); | |
420 | v9fs_uint32_write(req, flags); | |
421 | v9fs_req_send(req); | |
422 | return req; | |
423 | } | |
424 | ||
425 | /* size[4] Rlopen tag[2] qid[13] iounit[4] */ | |
426 | static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit) | |
427 | { | |
428 | v9fs_req_recv(req, P9_RLOPEN); | |
429 | if (qid) { | |
430 | v9fs_memread(req, qid, 13); | |
431 | } else { | |
432 | v9fs_memskip(req, 13); | |
433 | } | |
434 | if (iounit) { | |
435 | v9fs_uint32_read(req, iounit); | |
436 | } | |
437 | v9fs_req_free(req); | |
438 | } | |
439 | ||
354b86f8 GK |
440 | /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */ |
441 | static P9Req *v9fs_twrite(QVirtIO9P *v9p, uint32_t fid, uint64_t offset, | |
442 | uint32_t count, const void *data, uint16_t tag) | |
443 | { | |
444 | P9Req *req; | |
445 | uint32_t body_size = 4 + 8 + 4; | |
446 | ||
447 | g_assert_cmpint(body_size, <=, UINT32_MAX - count); | |
448 | body_size += count; | |
449 | req = v9fs_req_init(v9p, body_size, P9_TWRITE, tag); | |
450 | v9fs_uint32_write(req, fid); | |
451 | v9fs_uint64_write(req, offset); | |
452 | v9fs_uint32_write(req, count); | |
453 | v9fs_memwrite(req, data, count); | |
454 | v9fs_req_send(req); | |
455 | return req; | |
456 | } | |
457 | ||
458 | /* size[4] Rwrite tag[2] count[4] */ | |
459 | static void v9fs_rwrite(P9Req *req, uint32_t *count) | |
460 | { | |
461 | v9fs_req_recv(req, P9_RWRITE); | |
462 | if (count) { | |
463 | v9fs_uint32_read(req, count); | |
464 | } | |
465 | v9fs_req_free(req); | |
466 | } | |
467 | ||
357e2f7f GK |
468 | /* size[4] Tflush tag[2] oldtag[2] */ |
469 | static P9Req *v9fs_tflush(QVirtIO9P *v9p, uint16_t oldtag, uint16_t tag) | |
470 | { | |
471 | P9Req *req; | |
472 | ||
473 | req = v9fs_req_init(v9p, 2, P9_TFLUSH, tag); | |
474 | v9fs_uint32_write(req, oldtag); | |
475 | v9fs_req_send(req); | |
476 | return req; | |
477 | } | |
478 | ||
479 | /* size[4] Rflush tag[2] */ | |
480 | static void v9fs_rflush(P9Req *req) | |
481 | { | |
482 | v9fs_req_recv(req, P9_RFLUSH); | |
483 | v9fs_req_free(req); | |
484 | } | |
485 | ||
6cc9906b GK |
486 | static void fs_version(QVirtIO9P *v9p) |
487 | { | |
488 | const char *version = "9P2000.L"; | |
489 | uint16_t server_len; | |
490 | char *server_version; | |
491 | P9Req *req; | |
492 | ||
693b21d2 | 493 | req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG); |
357e2f7f | 494 | v9fs_req_wait_for_reply(req, NULL); |
6cc9906b GK |
495 | v9fs_rversion(req, &server_len, &server_version); |
496 | ||
497 | g_assert_cmpmem(server_version, server_len, version, strlen(version)); | |
498 | ||
499 | g_free(server_version); | |
500 | } | |
501 | ||
5c3df1f0 GK |
502 | static void fs_attach(QVirtIO9P *v9p) |
503 | { | |
504 | P9Req *req; | |
505 | ||
506 | fs_version(v9p); | |
693b21d2 | 507 | req = v9fs_tattach(v9p, 0, getuid(), 0); |
357e2f7f | 508 | v9fs_req_wait_for_reply(req, NULL); |
5c3df1f0 GK |
509 | v9fs_rattach(req, NULL); |
510 | } | |
511 | ||
04b88c84 GK |
512 | static void fs_walk(QVirtIO9P *v9p) |
513 | { | |
2893ddd5 | 514 | char *wnames[P9_MAXWELEM]; |
04b88c84 GK |
515 | uint16_t nwqid; |
516 | v9fs_qid *wqid; | |
517 | int i; | |
518 | P9Req *req; | |
519 | ||
520 | for (i = 0; i < P9_MAXWELEM; i++) { | |
2893ddd5 | 521 | wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i); |
04b88c84 GK |
522 | } |
523 | ||
524 | fs_attach(v9p); | |
693b21d2 | 525 | req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0); |
357e2f7f | 526 | v9fs_req_wait_for_reply(req, NULL); |
04b88c84 GK |
527 | v9fs_rwalk(req, &nwqid, &wqid); |
528 | ||
529 | g_assert_cmpint(nwqid, ==, P9_MAXWELEM); | |
530 | ||
531 | for (i = 0; i < P9_MAXWELEM; i++) { | |
04b88c84 GK |
532 | g_free(wnames[i]); |
533 | } | |
534 | ||
535 | g_free(wqid); | |
536 | } | |
537 | ||
ba0d1037 GK |
538 | static void fs_walk_no_slash(QVirtIO9P *v9p) |
539 | { | |
540 | char *const wnames[] = { g_strdup(" /") }; | |
541 | P9Req *req; | |
542 | uint32_t err; | |
543 | ||
544 | fs_attach(v9p); | |
693b21d2 | 545 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
357e2f7f | 546 | v9fs_req_wait_for_reply(req, NULL); |
ba0d1037 GK |
547 | v9fs_rlerror(req, &err); |
548 | ||
549 | g_assert_cmpint(err, ==, ENOENT); | |
550 | ||
551 | g_free(wnames[0]); | |
552 | } | |
553 | ||
a37c0702 GK |
554 | static void fs_walk_dotdot(QVirtIO9P *v9p) |
555 | { | |
556 | char *const wnames[] = { g_strdup("..") }; | |
557 | v9fs_qid root_qid, *wqid; | |
558 | P9Req *req; | |
559 | ||
560 | fs_version(v9p); | |
693b21d2 | 561 | req = v9fs_tattach(v9p, 0, getuid(), 0); |
357e2f7f | 562 | v9fs_req_wait_for_reply(req, NULL); |
a37c0702 GK |
563 | v9fs_rattach(req, &root_qid); |
564 | ||
693b21d2 | 565 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
357e2f7f | 566 | v9fs_req_wait_for_reply(req, NULL); |
a37c0702 GK |
567 | v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */ |
568 | ||
569 | g_assert_cmpmem(&root_qid, 13, wqid[0], 13); | |
570 | ||
571 | g_free(wqid); | |
572 | g_free(wnames[0]); | |
573 | } | |
574 | ||
82469aae GK |
575 | static void fs_lopen(QVirtIO9P *v9p) |
576 | { | |
577 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) }; | |
578 | P9Req *req; | |
579 | ||
580 | fs_attach(v9p); | |
581 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); | |
357e2f7f | 582 | v9fs_req_wait_for_reply(req, NULL); |
82469aae GK |
583 | v9fs_rwalk(req, NULL, NULL); |
584 | ||
585 | req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); | |
357e2f7f | 586 | v9fs_req_wait_for_reply(req, NULL); |
82469aae GK |
587 | v9fs_rlopen(req, NULL, NULL); |
588 | ||
589 | g_free(wnames[0]); | |
590 | } | |
591 | ||
354b86f8 GK |
592 | static void fs_write(QVirtIO9P *v9p) |
593 | { | |
594 | static const uint32_t write_count = P9_MAX_SIZE / 2; | |
595 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) }; | |
596 | char *buf = g_malloc0(write_count); | |
597 | uint32_t count; | |
598 | P9Req *req; | |
599 | ||
600 | fs_attach(v9p); | |
601 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); | |
357e2f7f | 602 | v9fs_req_wait_for_reply(req, NULL); |
354b86f8 GK |
603 | v9fs_rwalk(req, NULL, NULL); |
604 | ||
605 | req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); | |
357e2f7f | 606 | v9fs_req_wait_for_reply(req, NULL); |
354b86f8 GK |
607 | v9fs_rlopen(req, NULL, NULL); |
608 | ||
609 | req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0); | |
357e2f7f | 610 | v9fs_req_wait_for_reply(req, NULL); |
354b86f8 GK |
611 | v9fs_rwrite(req, &count); |
612 | g_assert_cmpint(count, ==, write_count); | |
613 | ||
614 | g_free(buf); | |
615 | g_free(wnames[0]); | |
616 | } | |
617 | ||
357e2f7f GK |
618 | static void fs_flush_success(QVirtIO9P *v9p) |
619 | { | |
620 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; | |
621 | P9Req *req, *flush_req; | |
622 | uint32_t reply_len; | |
623 | uint8_t should_block; | |
624 | ||
625 | fs_attach(v9p); | |
626 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); | |
627 | v9fs_req_wait_for_reply(req, NULL); | |
628 | v9fs_rwalk(req, NULL, NULL); | |
629 | ||
630 | req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); | |
631 | v9fs_req_wait_for_reply(req, NULL); | |
632 | v9fs_rlopen(req, NULL, NULL); | |
633 | ||
634 | /* This will cause the 9p server to try to write data to the backend, | |
635 | * until the write request gets cancelled. | |
636 | */ | |
637 | should_block = 1; | |
638 | req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); | |
639 | ||
640 | flush_req = v9fs_tflush(v9p, req->tag, 1); | |
641 | ||
642 | /* The write request is supposed to be flushed: the server should just | |
643 | * mark the write request as used and reply to the flush request. | |
644 | */ | |
645 | v9fs_req_wait_for_reply(req, &reply_len); | |
646 | g_assert_cmpint(reply_len, ==, 0); | |
647 | v9fs_req_free(req); | |
648 | v9fs_rflush(flush_req); | |
649 | ||
650 | g_free(wnames[0]); | |
651 | } | |
652 | ||
653 | static void fs_flush_ignored(QVirtIO9P *v9p) | |
654 | { | |
655 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; | |
656 | P9Req *req, *flush_req; | |
657 | uint32_t count; | |
658 | uint8_t should_block; | |
659 | ||
660 | fs_attach(v9p); | |
661 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); | |
662 | v9fs_req_wait_for_reply(req, NULL); | |
663 | v9fs_rwalk(req, NULL, NULL); | |
664 | ||
665 | req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); | |
666 | v9fs_req_wait_for_reply(req, NULL); | |
667 | v9fs_rlopen(req, NULL, NULL); | |
668 | ||
669 | /* This will cause the write request to complete right away, before it | |
670 | * could be actually cancelled. | |
671 | */ | |
672 | should_block = 0; | |
673 | req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); | |
674 | ||
675 | flush_req = v9fs_tflush(v9p, req->tag, 1); | |
676 | ||
677 | /* The write request is supposed to complete. The server should | |
678 | * reply to the write request and the flush request. | |
679 | */ | |
680 | v9fs_req_wait_for_reply(req, NULL); | |
681 | v9fs_rwrite(req, &count); | |
682 | g_assert_cmpint(count, ==, sizeof(should_block)); | |
683 | v9fs_rflush(flush_req); | |
684 | ||
685 | g_free(wnames[0]); | |
686 | } | |
687 | ||
1211d81b GK |
688 | typedef void (*v9fs_test_fn)(QVirtIO9P *v9p); |
689 | ||
690 | static void v9fs_run_pci_test(gconstpointer data) | |
691 | { | |
692 | v9fs_test_fn fn = data; | |
693 | QVirtIO9P *v9p = qvirtio_9p_pci_start(); | |
557a4cc0 | 694 | |
1211d81b GK |
695 | if (fn) { |
696 | fn(v9p); | |
697 | } | |
698 | qvirtio_9p_pci_stop(v9p); | |
699 | } | |
700 | ||
701 | static void v9fs_qtest_pci_add(const char *path, v9fs_test_fn fn) | |
702 | { | |
703 | qtest_add_data_func(path, fn, v9fs_run_pci_test); | |
557a4cc0 GK |
704 | } |
705 | ||
993f8054 GK |
706 | int main(int argc, char **argv) |
707 | { | |
708 | g_test_init(&argc, &argv, NULL); | |
1211d81b GK |
709 | v9fs_qtest_pci_add("/virtio/9p/pci/nop", NULL); |
710 | v9fs_qtest_pci_add("/virtio/9p/pci/config", pci_config); | |
6cc9906b | 711 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/version/basic", fs_version); |
5c3df1f0 | 712 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/attach/basic", fs_attach); |
04b88c84 | 713 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/basic", fs_walk); |
ba0d1037 | 714 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/no_slash", fs_walk_no_slash); |
a37c0702 GK |
715 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/walk/dotdot_from_root", |
716 | fs_walk_dotdot); | |
82469aae | 717 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/lopen/basic", fs_lopen); |
354b86f8 | 718 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/write/basic", fs_write); |
357e2f7f GK |
719 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/flush/success", fs_flush_success); |
720 | v9fs_qtest_pci_add("/virtio/9p/pci/fs/flush/ignored", fs_flush_ignored); | |
2d888c09 | 721 | |
993f8054 | 722 | return g_test_run(); |
2d888c09 | 723 | } |