qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Victor Kaplansky" <victork@redhat.com>,
	"Yuanhan Liu" <yuanhan.liu@linux.intel.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [Qemu-devel] [PULL v2 11/32] test: start vhost-user reconnect test
Date: Tue, 14 Jun 2016 22:59:48 +0300	[thread overview]
Message-ID: <20160614225948-mutt-send-email-mst@redhat.com> (raw)
In-Reply-To: <1465934227-16558-1-git-send-email-mst@redhat.com>

From: Marc-André Lureau <marcandre.lureau@redhat.com>

This is a simple reconnect test, that simply checks if vhost-user
reconnection is possible and restore the state. A more complete test
would actually manipulate and check the ring contents (such extended
testing would benefit from the libvhost-user proposed in QEMU list to
avoid duplication of ring manipulations)

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Victor Kaplansky <victork@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 tests/vhost-user-test.c | 136 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 119 insertions(+), 17 deletions(-)

diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index 6961596..6dfd2ae 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -34,7 +34,7 @@
 #define QEMU_CMD_ACCEL  " -machine accel=tcg"
 #define QEMU_CMD_MEM    " -m %d -object memory-backend-file,id=mem,size=%dM,"\
                         "mem-path=%s,share=on -numa node,memdev=mem"
-#define QEMU_CMD_CHR    " -chardev socket,id=%s,path=%s"
+#define QEMU_CMD_CHR    " -chardev socket,id=%s,path=%s%s"
 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
 #define QEMU_CMD_NET    " -device virtio-net-pci,netdev=net0,romfile=./pc-bios/pxe-virtio.rom"
 
@@ -246,7 +246,12 @@ static void chr_read(void *opaque, const uint8_t *buf, int size)
 
     if (msg.size) {
         p += VHOST_USER_HDR_SIZE;
-        g_assert_cmpint(qemu_chr_fe_read_all(chr, p, msg.size), ==, msg.size);
+        size = qemu_chr_fe_read_all(chr, p, msg.size);
+        if (size != msg.size) {
+            g_test_message("Wrong message size received %d != %d\n",
+                           size, msg.size);
+            return;
+        }
     }
 
     switch (msg.request) {
@@ -363,17 +368,10 @@ static const char *init_hugepagefs(const char *path)
 static TestServer *test_server_new(const gchar *name)
 {
     TestServer *server = g_new0(TestServer, 1);
-    gchar *chr_path;
 
     server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
     server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
-
-    chr_path = g_strdup_printf("unix:%s,server,nowait", server->socket_path);
     server->chr_name = g_strdup_printf("chr-%s", name);
-    server->chr = 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, server);
 
     g_mutex_init(&server->data_mutex);
     g_cond_init(&server->data_cond);
@@ -383,13 +381,34 @@ static TestServer *test_server_new(const gchar *name)
     return server;
 }
 
-#define GET_QEMU_CMD(s)                                                        \
-    g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name,                 \
-                    (s)->socket_path, (s)->chr_name)
+static void test_server_create_chr(TestServer *server, const gchar *opt)
+{
+    gchar *chr_path;
 
-#define GET_QEMU_CMDE(s, mem, extra, ...)                                      \
-    g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name,       \
-                    (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
+    chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
+    server->chr = 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, server);
+}
+
+static void test_server_listen(TestServer *server)
+{
+    test_server_create_chr(server, ",server,nowait");
+}
+
+static inline void test_server_connect(TestServer *server)
+{
+    test_server_create_chr(server, ",reconnect=1");
+}
+
+#define GET_QEMU_CMD(s)                                         \
+    g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name,  \
+                    (s)->socket_path, "", (s)->chr_name)
+
+#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...)                     \
+    g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
+                    (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
 
 static gboolean _test_server_free(TestServer *server)
 {
@@ -537,7 +556,10 @@ static void test_migrate(void)
     guint8 *log;
     guint64 size;
 
-    cmd = GET_QEMU_CMDE(s, 2, "");
+    test_server_listen(s);
+    test_server_listen(dest);
+
+    cmd = GET_QEMU_CMDE(s, 2, "", "");
     from = qtest_start(cmd);
     g_free(cmd);
 
@@ -545,7 +567,7 @@ static void test_migrate(void)
     size = get_log_size(s);
     g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
 
-    cmd = GET_QEMU_CMDE(dest, 2, " -incoming %s", uri);
+    cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
     to = qtest_init(cmd);
     g_free(cmd);
 
@@ -605,6 +627,80 @@ static void test_migrate(void)
     global_qtest = global;
 }
 
+#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
+static void wait_for_rings_started(TestServer *s, size_t count)
+{
+    gint64 end_time;
+
+    g_mutex_lock(&s->data_mutex);
+    end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
+    while (ctpop64(s->rings) != count) {
+        if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
+            /* timeout has passed */
+            g_assert_cmpint(ctpop64(s->rings), ==, count);
+            break;
+        }
+    }
+
+    g_mutex_unlock(&s->data_mutex);
+}
+
+static gboolean
+reconnect_cb(gpointer user_data)
+{
+    TestServer *s = user_data;
+
+    qemu_chr_disconnect(s->chr);
+
+    return FALSE;
+}
+
+static gpointer
+connect_thread(gpointer data)
+{
+    TestServer *s = data;
+
+    /* wait for qemu to start before first try, to avoid extra warnings */
+    g_usleep(G_USEC_PER_SEC);
+    test_server_connect(s);
+
+    return NULL;
+}
+
+static void test_reconnect_subprocess(void)
+{
+    TestServer *s = test_server_new("reconnect");
+    char *cmd;
+
+    g_thread_new("connect", connect_thread, s);
+    cmd = GET_QEMU_CMDE(s, 2, ",server", "");
+    qtest_start(cmd);
+    g_free(cmd);
+
+    wait_for_fds(s);
+    wait_for_rings_started(s, 2);
+
+    /* reconnect */
+    s->fds_num = 0;
+    s->rings = 0;
+    g_idle_add(reconnect_cb, s);
+    wait_for_fds(s);
+    wait_for_rings_started(s, 2);
+
+    qtest_end();
+    test_server_free(s);
+    return;
+}
+
+static void test_reconnect(void)
+{
+    gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
+                                  qtest_get_arch());
+    g_test_trap_subprocess(path, 0, 0);
+    g_test_trap_assert_passed();
+}
+#endif
+
 int main(int argc, char **argv)
 {
     QTestState *s = NULL;
@@ -636,6 +732,7 @@ int main(int argc, char **argv)
     }
 
     server = test_server_new("test");
+    test_server_listen(server);
 
     loop = g_main_loop_new(NULL, FALSE);
     /* run the main loop thread so the chardev may operate */
@@ -648,6 +745,11 @@ int main(int argc, char **argv)
 
     qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
     qtest_add_func("/vhost-user/migrate", test_migrate);
+#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
+    qtest_add_func("/vhost-user/reconnect/subprocess",
+                   test_reconnect_subprocess);
+    qtest_add_func("/vhost-user/reconnect", test_reconnect);
+#endif
 
     ret = g_test_run();
 
-- 
MST

  parent reply	other threads:[~2016-06-14 19:59 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1465934227-16558-1-git-send-email-mst@redhat.com>
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 01/32] pci: fix pci_requester_id() Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 02/32] vhost-user: add ability to know vhost-user backend disconnection Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 03/32] tests/vhost-user-bridge: add client mode Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 04/32] tests/vhost-user-bridge: workaround stale vring base Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 05/32] qemu-char: add qemu_chr_disconnect to close a fd accepted by listen fd Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 06/32] vhost-user: disconnect on start failure Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 07/32] vhost-net: do not crash if backend is not present Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 08/32] vhost-net: save & restore vhost-user acked features Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 09/32] vhost-net: save & restore vring enable state Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 10/32] tests: append i386 tests Michael S. Tsirkin
2016-06-14 19:59 ` Michael S. Tsirkin [this message]
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 12/32] smbios: Move table build tools into an include file Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 13/32] ipmi: Add SMBIOS table entry Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 14/32] acpi: Add IPMI table entries Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 15/32] bios: Add tests for the IPMI ACPI and SMBIOS entries Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 16/32] pci core: assert ENOSPC when add capability Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 17/32] fix some coding style problems Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 18/32] msi_init: change return value to 0 on success Michael S. Tsirkin
2016-06-14 19:59 ` [Qemu-devel] [PULL v2 19/32] pc-dimm: introduce get_vmstate_memory_region callback Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 20/32] nvdimm: support nvdimm label Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 21/32] acpi: add aml_object_type Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 22/32] acpi: add aml_call5 Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 23/32] nvdimm acpi: set HDLE properly Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 24/32] nvdimm acpi: save arg3 of _DSM method Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 25/32] nvdimm acpi: check UUID Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 26/32] nvdimm acpi: abstract the operations for root & nvdimm devices Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 27/32] nvdimm acpi: check revision Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 28/32] nvdimm acpi: support Get Namespace Label Size function Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 29/32] nvdimm acpi: support Get Namespace Label Data function Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 30/32] nvdimm acpi: support Set " Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 31/32] docs: add NVDIMM ACPI documentation Michael S. Tsirkin
2016-06-14 20:00 ` [Qemu-devel] [PULL v2 32/32] MAINTAINERS: add Marcel to PCI Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160614225948-mutt-send-email-mst@redhat.com \
    --to=mst@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=victork@redhat.com \
    --cc=yuanhan.liu@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).