From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59398) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZkIZt-00062D-8i for qemu-devel@nongnu.org; Thu, 08 Oct 2015 17:18:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZkIZr-0007QZ-Mw for qemu-devel@nongnu.org; Thu, 08 Oct 2015 17:18:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45508) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZkIZr-0007QS-D1 for qemu-devel@nongnu.org; Thu, 08 Oct 2015 17:18:15 -0400 Date: Fri, 9 Oct 2015 00:18:09 +0300 From: "Michael S. Tsirkin" Message-ID: <1444338957-15293-22-git-send-email-mst@redhat.com> References: <1444338957-15293-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1444338957-15293-1-git-send-email-mst@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 21/25] vhost-user-test: wrap server in TestServer struct List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Marcel Apfelbaum , Peter Maydell , Yuanhan Liu , =?us-ascii?B?PT9VVEYtOD9xP01hcmMtQW5kcj1DMz1BOT0yMEx1cmVhdT89?= From: Marc-Andr=E9 Lureau In the coming patches, a test will use several servers simultaneously. Wrap the server in a struct, out of the global scope. Signed-off-by: Marc-Andr=E9 Lureau Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- tests/vhost-user-test.c | 139 +++++++++++++++++++++++++++++++-----------= ------ 1 file changed, 89 insertions(+), 50 deletions(-) diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index 4be5583..034d89b 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -108,10 +108,16 @@ static VhostUserMsg m __attribute__ ((unused)); #define VHOST_USER_VERSION (0x1) /***********************************************************************= ******/ =20 -int fds_num =3D 0, fds[VHOST_MEMORY_MAX_NREGIONS]; -static VhostUserMemory memory; -static CompatGMutex data_mutex; -static CompatGCond data_cond; +typedef struct TestServer { + gchar *socket_path; + gchar *chr_name; + CharDriverState *chr; + int fds_num; + int fds[VHOST_MEMORY_MAX_NREGIONS]; + VhostUserMemory memory; + GMutex data_mutex; + GCond data_cond; +} TestServer; =20 #if !GLIB_CHECK_VERSION(2, 32, 0) static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex, @@ -126,67 +132,68 @@ static gboolean g_cond_wait_until(CompatGCond cond,= CompatGMutex mutex, } #endif =20 -static void wait_for_fds(void) +static void wait_for_fds(TestServer *s) { gint64 end_time; =20 - g_mutex_lock(&data_mutex); + g_mutex_lock(&s->data_mutex); =20 end_time =3D g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; - while (!fds_num) { - if (!g_cond_wait_until(&data_cond, &data_mutex, end_time)) { + while (!s->fds_num) { + if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time))= { /* timeout has passed */ - g_assert(fds_num); + g_assert(s->fds_num); break; } } =20 /* check for sanity */ - g_assert_cmpint(fds_num, >, 0); - g_assert_cmpint(fds_num, =3D=3D, memory.nregions); + g_assert_cmpint(s->fds_num, >, 0); + g_assert_cmpint(s->fds_num, =3D=3D, s->memory.nregions); =20 - g_mutex_unlock(&data_mutex); + g_mutex_unlock(&s->data_mutex); } =20 -static void read_guest_mem(void) +static void read_guest_mem(TestServer *s) { uint32_t *guest_mem; int i, j; size_t size; =20 - wait_for_fds(); + wait_for_fds(s); =20 - g_mutex_lock(&data_mutex); + g_mutex_lock(&s->data_mutex); =20 /* iterate all regions */ - for (i =3D 0; i < fds_num; i++) { + for (i =3D 0; i < s->fds_num; i++) { =20 /* We'll check only the region statring at 0x0*/ - if (memory.regions[i].guest_phys_addr !=3D 0x0) { + if (s->memory.regions[i].guest_phys_addr !=3D 0x0) { continue; } =20 - g_assert_cmpint(memory.regions[i].memory_size, >, 1024); + g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024); =20 - size =3D memory.regions[i].memory_size + memory.regions[i].mmap= _offset; + size =3D s->memory.regions[i].memory_size + + s->memory.regions[i].mmap_offset; =20 guest_mem =3D mmap(0, size, PROT_READ | PROT_WRITE, - MAP_SHARED, fds[i], 0); + MAP_SHARED, s->fds[i], 0); =20 g_assert(guest_mem !=3D MAP_FAILED); - guest_mem +=3D (memory.regions[i].mmap_offset / sizeof(*guest_me= m)); + guest_mem +=3D (s->memory.regions[i].mmap_offset / sizeof(*guest= _mem)); =20 for (j =3D 0; j < 256; j++) { - uint32_t a =3D readl(memory.regions[i].guest_phys_addr + j*4= ); + uint32_t a =3D readl(s->memory.regions[i].guest_phys_addr + = j*4); uint32_t b =3D guest_mem[j]; =20 g_assert_cmpint(a, =3D=3D, b); } =20 - munmap(guest_mem, memory.regions[i].memory_size); + munmap(guest_mem, s->memory.regions[i].memory_size); } =20 - g_mutex_unlock(&data_mutex); + g_mutex_unlock(&s->data_mutex); } =20 static void *thread_function(void *data) @@ -204,7 +211,8 @@ static int chr_can_read(void *opaque) =20 static void chr_read(void *opaque, const uint8_t *buf, int size) { - CharDriverState *chr =3D opaque; + TestServer *s =3D opaque; + CharDriverState *chr =3D s->chr; VhostUserMsg msg; uint8_t *p =3D (uint8_t *) &msg; int fd; @@ -214,12 +222,12 @@ static void chr_read(void *opaque, const uint8_t *b= uf, int size) return; } =20 - g_mutex_lock(&data_mutex); + g_mutex_lock(&s->data_mutex); memcpy(p, buf, VHOST_USER_HDR_SIZE); =20 if (msg.size) { p +=3D VHOST_USER_HDR_SIZE; - qemu_chr_fe_read_all(chr, p, msg.size); + g_assert_cmpint(qemu_chr_fe_read_all(chr, p, msg.size), =3D=3D, = msg.size); } =20 switch (msg.request) { @@ -257,11 +265,11 @@ static void chr_read(void *opaque, const uint8_t *b= uf, int size) =20 case VHOST_USER_SET_MEM_TABLE: /* received the mem table */ - memcpy(&memory, &msg.memory, sizeof(msg.memory)); - fds_num =3D qemu_chr_fe_get_msgfds(chr, fds, sizeof(fds) / sizeo= f(int)); + memcpy(&s->memory, &msg.memory, sizeof(msg.memory)); + s->fds_num =3D qemu_chr_fe_get_msgfds(chr, s->fds, G_N_ELEMENTS(= s->fds)); =20 /* signal the test that it can continue */ - g_cond_signal(&data_cond); + g_cond_signal(&s->data_cond); break; =20 case VHOST_USER_SET_VRING_KICK: @@ -278,7 +286,8 @@ static void chr_read(void *opaque, const uint8_t *buf= , int size) default: break; } - g_mutex_unlock(&data_mutex); + + g_mutex_unlock(&s->data_mutex); } =20 static const char *init_hugepagefs(const char *path) @@ -308,14 +317,52 @@ static const char *init_hugepagefs(const char *path= ) return path; } =20 +static TestServer *test_server_new(const gchar *tmpfs, const gchar *name= ) +{ + TestServer *server =3D g_new0(TestServer, 1); + gchar *chr_path; + + server->socket_path =3D g_strdup_printf("%s/%s.sock", tmpfs, name); + + chr_path =3D g_strdup_printf("unix:%s,server,nowait", server->socket= _path); + server->chr_name =3D g_strdup_printf("chr-%s", name); + server->chr =3D qemu_chr_new(server->chr_name, chr_path, NULL); + g_free(chr_path); + + qemu_chr_add_handlers(server->chr, chr_can_read, chr_read, NULL, ser= ver); + + g_mutex_init(&server->data_mutex); + g_cond_init(&server->data_cond); + + return server; +} + +#define GET_QEMU_CMD(s, root) \ + g_strdup_printf(QEMU_CMD, (root), (s)->socket_path) + + +static void test_server_free(TestServer *server) +{ + int i; + + qemu_chr_delete(server->chr); + + for (i =3D 0; i < server->fds_num; i++) { + close(server->fds[i]); + } + + unlink(server->socket_path); + g_free(server->socket_path); + + g_free(server); +} + int main(int argc, char **argv) { QTestState *s =3D NULL; - CharDriverState *chr =3D NULL; + TestServer *server =3D NULL; const char *hugefs; - char *socket_path =3D 0; - char *qemu_cmd =3D 0; - char *chr_path =3D 0; + char *qemu_cmd =3D NULL; int ret; char template[] =3D "/tmp/vhost-test-XXXXXX"; const char *tmpfs; @@ -324,10 +371,11 @@ int main(int argc, char **argv) g_test_init(&argc, &argv, NULL); =20 module_call_init(MODULE_INIT_QOM); + qemu_add_opts(&qemu_chardev_opts); =20 tmpfs =3D mkdtemp(template); if (!tmpfs) { - g_test_message("mkdtemp on path (%s): %s\n", template, strerro= r(errno)); + g_test_message("mkdtemp on path (%s): %s\n", template, strerror(= errno)); } g_assert(tmpfs); =20 @@ -339,25 +387,17 @@ int main(int argc, char **argv) root =3D tmpfs; } =20 - socket_path =3D g_strdup_printf("%s/vhost.sock", tmpfs); - - /* create char dev and add read handlers */ - qemu_add_opts(&qemu_chardev_opts); - chr_path =3D g_strdup_printf("unix:%s,server,nowait", socket_path); - chr =3D qemu_chr_new("chr0", chr_path, NULL); - g_free(chr_path); - qemu_chr_add_handlers(chr, chr_can_read, chr_read, NULL, chr); + server =3D test_server_new(tmpfs, "test"); =20 /* run the main loop thread so the chardev may operate */ - g_mutex_init(&data_mutex); - g_cond_init(&data_cond); g_thread_new(NULL, thread_function, NULL); =20 - qemu_cmd =3D g_strdup_printf(QEMU_CMD, root, socket_path); + qemu_cmd =3D GET_QEMU_CMD(server, root); + s =3D qtest_start(qemu_cmd); g_free(qemu_cmd); =20 - qtest_add_func("/vhost-user/read-guest-mem", read_guest_mem); + qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest= _mem); =20 ret =3D g_test_run(); =20 @@ -366,8 +406,7 @@ int main(int argc, char **argv) } =20 /* cleanup */ - unlink(socket_path); - g_free(socket_path); + test_server_free(server); =20 ret =3D rmdir(tmpfs); if (ret !=3D 0) { --=20 MST