]> git.proxmox.com Git - mirror_qemu.git/blob - tests/ivshmem-test.c
qtest: convert ivshmem-test to use libqos
[mirror_qemu.git] / tests / ivshmem-test.c
1 /*
2 * QTest testcase for ivshmem
3 *
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 * Copyright (c) 2015 Red Hat, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
11 #include "qemu/osdep.h"
12 #include <glib/gstdio.h>
13 #include "contrib/ivshmem-server/ivshmem-server.h"
14 #include "libqos/libqos-pc.h"
15 #include "libqtest.h"
16 #include "qemu-common.h"
17
18 #define TMPSHMSIZE (1 << 20)
19 static char *tmpshm;
20 static void *tmpshmem;
21 static char *tmpdir;
22 static char *tmpserver;
23
24 static void save_fn(QPCIDevice *dev, int devfn, void *data)
25 {
26 QPCIDevice **pdev = (QPCIDevice **) data;
27
28 *pdev = dev;
29 }
30
31 static QPCIDevice *get_device(QPCIBus *pcibus)
32 {
33 QPCIDevice *dev;
34
35 dev = NULL;
36 qpci_device_foreach(pcibus, 0x1af4, 0x1110, save_fn, &dev);
37 g_assert(dev != NULL);
38
39 return dev;
40 }
41
42 typedef struct _IVState {
43 QOSState *qs;
44 QPCIBar reg_bar, mem_bar;
45 QPCIDevice *dev;
46 } IVState;
47
48 enum Reg {
49 INTRMASK = 0,
50 INTRSTATUS = 4,
51 IVPOSITION = 8,
52 DOORBELL = 12,
53 };
54
55 static const char* reg2str(enum Reg reg) {
56 switch (reg) {
57 case INTRMASK:
58 return "IntrMask";
59 case INTRSTATUS:
60 return "IntrStatus";
61 case IVPOSITION:
62 return "IVPosition";
63 case DOORBELL:
64 return "DoorBell";
65 default:
66 return NULL;
67 }
68 }
69
70 static inline unsigned in_reg(IVState *s, enum Reg reg)
71 {
72 const char *name = reg2str(reg);
73 QTestState *qtest = global_qtest;
74 unsigned res;
75
76 global_qtest = s->qs->qts;
77 res = qpci_io_readl(s->dev, s->reg_bar, reg);
78 g_test_message("*%s -> %x\n", name, res);
79 global_qtest = qtest;
80
81 return res;
82 }
83
84 static inline void out_reg(IVState *s, enum Reg reg, unsigned v)
85 {
86 const char *name = reg2str(reg);
87 QTestState *qtest = global_qtest;
88
89 global_qtest = s->qs->qts;
90 g_test_message("%x -> *%s\n", v, name);
91 qpci_io_writel(s->dev, s->reg_bar, reg, v);
92 global_qtest = qtest;
93 }
94
95 static inline void read_mem(IVState *s, uint64_t off, void *buf, size_t len)
96 {
97 QTestState *qtest = global_qtest;
98
99 global_qtest = s->qs->qts;
100 qpci_memread(s->dev, s->mem_bar, off, buf, len);
101 global_qtest = qtest;
102 }
103
104 static inline void write_mem(IVState *s, uint64_t off,
105 const void *buf, size_t len)
106 {
107 QTestState *qtest = global_qtest;
108
109 global_qtest = s->qs->qts;
110 qpci_memwrite(s->dev, s->mem_bar, off, buf, len);
111 global_qtest = qtest;
112 }
113
114 static void cleanup_vm(IVState *s)
115 {
116 g_free(s->dev);
117 qtest_shutdown(s->qs);
118 }
119
120 static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
121 {
122 uint64_t barsize;
123 const char *arch = qtest_get_arch();
124
125 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
126 s->qs = qtest_pc_boot(cmd);
127 } else {
128 g_printerr("ivshmem-test tests are only available on x86\n");
129 exit(EXIT_FAILURE);
130 }
131 s->dev = get_device(s->qs->pcibus);
132
133 s->reg_bar = qpci_iomap(s->dev, 0, &barsize);
134 g_assert_cmpuint(barsize, ==, 256);
135
136 if (msix) {
137 qpci_msix_enable(s->dev);
138 }
139
140 s->mem_bar = qpci_iomap(s->dev, 2, &barsize);
141 g_assert_cmpuint(barsize, ==, TMPSHMSIZE);
142
143 qpci_device_enable(s->dev);
144 }
145
146 static void setup_vm(IVState *s)
147 {
148 char *cmd = g_strdup_printf("-object memory-backend-file"
149 ",id=mb1,size=1M,share,mem-path=/dev/shm%s"
150 " -device ivshmem-plain,memdev=mb1", tmpshm);
151
152 setup_vm_cmd(s, cmd, false);
153
154 g_free(cmd);
155 }
156
157 static void test_ivshmem_single(void)
158 {
159 IVState state, *s;
160 uint32_t data[1024];
161 int i;
162
163 setup_vm(&state);
164 s = &state;
165
166 /* initial state of readable registers */
167 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0);
168 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
169 g_assert_cmpuint(in_reg(s, IVPOSITION), ==, 0);
170
171 /* trigger interrupt via registers */
172 out_reg(s, INTRMASK, 0xffffffff);
173 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0xffffffff);
174 out_reg(s, INTRSTATUS, 1);
175 /* check interrupt status */
176 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 1);
177 /* reading clears */
178 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
179 /* TODO intercept actual interrupt (needs qtest work) */
180
181 /* invalid register access */
182 out_reg(s, IVPOSITION, 1);
183 in_reg(s, DOORBELL);
184
185 /* ring the (non-functional) doorbell */
186 out_reg(s, DOORBELL, 8 << 16);
187
188 /* write shared memory */
189 for (i = 0; i < G_N_ELEMENTS(data); i++) {
190 data[i] = i;
191 }
192 write_mem(s, 0, data, sizeof(data));
193
194 /* verify write */
195 for (i = 0; i < G_N_ELEMENTS(data); i++) {
196 g_assert_cmpuint(((uint32_t *)tmpshmem)[i], ==, i);
197 }
198
199 /* read it back and verify read */
200 memset(data, 0, sizeof(data));
201 read_mem(s, 0, data, sizeof(data));
202 for (i = 0; i < G_N_ELEMENTS(data); i++) {
203 g_assert_cmpuint(data[i], ==, i);
204 }
205
206 cleanup_vm(s);
207 }
208
209 static void test_ivshmem_pair(void)
210 {
211 IVState state1, state2, *s1, *s2;
212 char *data;
213 int i;
214
215 setup_vm(&state1);
216 s1 = &state1;
217 setup_vm(&state2);
218 s2 = &state2;
219
220 data = g_malloc0(TMPSHMSIZE);
221
222 /* host write, guest 1 & 2 read */
223 memset(tmpshmem, 0x42, TMPSHMSIZE);
224 read_mem(s1, 0, data, TMPSHMSIZE);
225 for (i = 0; i < TMPSHMSIZE; i++) {
226 g_assert_cmpuint(data[i], ==, 0x42);
227 }
228 read_mem(s2, 0, data, TMPSHMSIZE);
229 for (i = 0; i < TMPSHMSIZE; i++) {
230 g_assert_cmpuint(data[i], ==, 0x42);
231 }
232
233 /* guest 1 write, guest 2 read */
234 memset(data, 0x43, TMPSHMSIZE);
235 write_mem(s1, 0, data, TMPSHMSIZE);
236 memset(data, 0, TMPSHMSIZE);
237 read_mem(s2, 0, data, TMPSHMSIZE);
238 for (i = 0; i < TMPSHMSIZE; i++) {
239 g_assert_cmpuint(data[i], ==, 0x43);
240 }
241
242 /* guest 2 write, guest 1 read */
243 memset(data, 0x44, TMPSHMSIZE);
244 write_mem(s2, 0, data, TMPSHMSIZE);
245 memset(data, 0, TMPSHMSIZE);
246 read_mem(s1, 0, data, TMPSHMSIZE);
247 for (i = 0; i < TMPSHMSIZE; i++) {
248 g_assert_cmpuint(data[i], ==, 0x44);
249 }
250
251 cleanup_vm(s1);
252 cleanup_vm(s2);
253 g_free(data);
254 }
255
256 typedef struct ServerThread {
257 GThread *thread;
258 IvshmemServer *server;
259 int pipe[2]; /* to handle quit */
260 } ServerThread;
261
262 static void *server_thread(void *data)
263 {
264 ServerThread *t = data;
265 IvshmemServer *server = t->server;
266
267 while (true) {
268 fd_set fds;
269 int maxfd, ret;
270
271 FD_ZERO(&fds);
272 FD_SET(t->pipe[0], &fds);
273 maxfd = t->pipe[0] + 1;
274
275 ivshmem_server_get_fds(server, &fds, &maxfd);
276
277 ret = select(maxfd, &fds, NULL, NULL, NULL);
278
279 if (ret < 0) {
280 if (errno == EINTR) {
281 continue;
282 }
283
284 g_critical("select error: %s\n", strerror(errno));
285 break;
286 }
287 if (ret == 0) {
288 continue;
289 }
290
291 if (FD_ISSET(t->pipe[0], &fds)) {
292 break;
293 }
294
295 if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
296 g_critical("ivshmem_server_handle_fds() failed\n");
297 break;
298 }
299 }
300
301 return NULL;
302 }
303
304 static void setup_vm_with_server(IVState *s, int nvectors, bool msi)
305 {
306 char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait "
307 "-device ivshmem%s,chardev=chr0,vectors=%d",
308 tmpserver,
309 msi ? "-doorbell" : ",size=1M,msi=off",
310 nvectors);
311
312 setup_vm_cmd(s, cmd, msi);
313
314 g_free(cmd);
315 }
316
317 static void test_ivshmem_server(bool msi)
318 {
319 IVState state1, state2, *s1, *s2;
320 ServerThread thread;
321 IvshmemServer server;
322 int ret, vm1, vm2;
323 int nvectors = 2;
324 guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
325
326 ret = ivshmem_server_init(&server, tmpserver, tmpshm, true,
327 TMPSHMSIZE, nvectors,
328 g_test_verbose());
329 g_assert_cmpint(ret, ==, 0);
330
331 ret = ivshmem_server_start(&server);
332 g_assert_cmpint(ret, ==, 0);
333
334 thread.server = &server;
335 ret = pipe(thread.pipe);
336 g_assert_cmpint(ret, ==, 0);
337 thread.thread = g_thread_new("ivshmem-server", server_thread, &thread);
338 g_assert(thread.thread != NULL);
339
340 setup_vm_with_server(&state1, nvectors, msi);
341 s1 = &state1;
342 setup_vm_with_server(&state2, nvectors, msi);
343 s2 = &state2;
344
345 /* check got different VM ids */
346 vm1 = in_reg(s1, IVPOSITION);
347 vm2 = in_reg(s2, IVPOSITION);
348 g_assert_cmpint(vm1, >=, 0);
349 g_assert_cmpint(vm2, >=, 0);
350 g_assert_cmpint(vm1, !=, vm2);
351
352 /* check number of MSI-X vectors */
353 global_qtest = s1->qs->qts;
354 if (msi) {
355 ret = qpci_msix_table_size(s1->dev);
356 g_assert_cmpuint(ret, ==, nvectors);
357 }
358
359 /* TODO test behavior before MSI-X is enabled */
360
361 /* ping vm2 -> vm1 on vector 0 */
362 if (msi) {
363 ret = qpci_msix_pending(s1->dev, 0);
364 g_assert_cmpuint(ret, ==, 0);
365 } else {
366 g_assert_cmpuint(in_reg(s1, INTRSTATUS), ==, 0);
367 }
368 out_reg(s2, DOORBELL, vm1 << 16);
369 do {
370 g_usleep(10000);
371 ret = msi ? qpci_msix_pending(s1->dev, 0) : in_reg(s1, INTRSTATUS);
372 } while (ret == 0 && g_get_monotonic_time() < end_time);
373 g_assert_cmpuint(ret, !=, 0);
374
375 /* ping vm1 -> vm2 on vector 1 */
376 global_qtest = s2->qs->qts;
377 if (msi) {
378 ret = qpci_msix_pending(s2->dev, 1);
379 g_assert_cmpuint(ret, ==, 0);
380 } else {
381 g_assert_cmpuint(in_reg(s2, INTRSTATUS), ==, 0);
382 }
383 out_reg(s1, DOORBELL, vm2 << 16 | 1);
384 do {
385 g_usleep(10000);
386 ret = msi ? qpci_msix_pending(s2->dev, 1) : in_reg(s2, INTRSTATUS);
387 } while (ret == 0 && g_get_monotonic_time() < end_time);
388 g_assert_cmpuint(ret, !=, 0);
389
390 cleanup_vm(s2);
391 cleanup_vm(s1);
392
393 if (qemu_write_full(thread.pipe[1], "q", 1) != 1) {
394 g_error("qemu_write_full: %s", g_strerror(errno));
395 }
396
397 g_thread_join(thread.thread);
398
399 ivshmem_server_close(&server);
400 close(thread.pipe[1]);
401 close(thread.pipe[0]);
402 }
403
404 static void test_ivshmem_server_msi(void)
405 {
406 test_ivshmem_server(true);
407 }
408
409 static void test_ivshmem_server_irq(void)
410 {
411 test_ivshmem_server(false);
412 }
413
414 #define PCI_SLOT_HP 0x06
415
416 static void test_ivshmem_hotplug(void)
417 {
418 gchar *opts;
419
420 qtest_start("");
421
422 opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
423
424 qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
425 qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
426
427 qtest_end();
428 g_free(opts);
429 }
430
431 static void test_ivshmem_memdev(void)
432 {
433 IVState state;
434
435 /* just for the sake of checking memory-backend property */
436 setup_vm_cmd(&state, "-object memory-backend-ram,size=1M,id=mb1"
437 " -device ivshmem-plain,memdev=mb1", false);
438
439 cleanup_vm(&state);
440 }
441
442 static void cleanup(void)
443 {
444 if (tmpshmem) {
445 munmap(tmpshmem, TMPSHMSIZE);
446 tmpshmem = NULL;
447 }
448
449 if (tmpshm) {
450 shm_unlink(tmpshm);
451 g_free(tmpshm);
452 tmpshm = NULL;
453 }
454
455 if (tmpserver) {
456 g_unlink(tmpserver);
457 g_free(tmpserver);
458 tmpserver = NULL;
459 }
460
461 if (tmpdir) {
462 g_rmdir(tmpdir);
463 tmpdir = NULL;
464 }
465 }
466
467 static void abrt_handler(void *data)
468 {
469 cleanup();
470 }
471
472 static gchar *mktempshm(int size, int *fd)
473 {
474 while (true) {
475 gchar *name;
476
477 name = g_strdup_printf("/qtest-%u-%u", getpid(), g_random_int());
478 *fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL,
479 S_IRWXU|S_IRWXG|S_IRWXO);
480 if (*fd > 0) {
481 g_assert(ftruncate(*fd, size) == 0);
482 return name;
483 }
484
485 g_free(name);
486
487 if (errno != EEXIST) {
488 perror("shm_open");
489 return NULL;
490 }
491 }
492 }
493
494 int main(int argc, char **argv)
495 {
496 int ret, fd;
497 gchar dir[] = "/tmp/ivshmem-test.XXXXXX";
498
499 #if !GLIB_CHECK_VERSION(2, 31, 0)
500 if (!g_thread_supported()) {
501 g_thread_init(NULL);
502 }
503 #endif
504
505 g_test_init(&argc, &argv, NULL);
506
507 qtest_add_abrt_handler(abrt_handler, NULL);
508 /* shm */
509 tmpshm = mktempshm(TMPSHMSIZE, &fd);
510 if (!tmpshm) {
511 return 0;
512 }
513 tmpshmem = mmap(0, TMPSHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
514 g_assert(tmpshmem != MAP_FAILED);
515 /* server */
516 if (mkdtemp(dir) == NULL) {
517 g_error("mkdtemp: %s", g_strerror(errno));
518 }
519 tmpdir = dir;
520 tmpserver = g_strconcat(tmpdir, "/server", NULL);
521
522 qtest_add_func("/ivshmem/single", test_ivshmem_single);
523 qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug);
524 qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev);
525 if (g_test_slow()) {
526 qtest_add_func("/ivshmem/pair", test_ivshmem_pair);
527 qtest_add_func("/ivshmem/server-msi", test_ivshmem_server_msi);
528 qtest_add_func("/ivshmem/server-irq", test_ivshmem_server_irq);
529 }
530
531 ret = g_test_run();
532
533 cleanup();
534
535 return ret;
536 }