From patchwork Tue Sep 29 16:34:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 524569 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 84D05140D70 for ; Thu, 1 Oct 2015 06:49:17 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=hhdeAQka; dkim-atps=neutral Received: from localhost ([::1]:32974 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZhOJP-0004nv-GG for incoming@patchwork.ozlabs.org; Wed, 30 Sep 2015 16:49:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51623) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zgxsr-0002Gy-PA for qemu-devel@nongnu.org; Tue, 29 Sep 2015 12:36:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zgxsn-0004UJ-Tl for qemu-devel@nongnu.org; Tue, 29 Sep 2015 12:36:05 -0400 Received: from mail-qg0-x22b.google.com ([2607:f8b0:400d:c04::22b]:35930) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zgxsn-0004UE-Pt for qemu-devel@nongnu.org; Tue, 29 Sep 2015 12:36:01 -0400 Received: by qgx61 with SMTP id 61so11017691qgx.3 for ; Tue, 29 Sep 2015 09:36:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=8dRhZOTjUWKunwhMUJwuMN/+LwY8mOiONKqjRKZwHFk=; b=hhdeAQka/A+jYVBQfc8Yy27ZV3F/PJ2/r8OqEDqGRm90H419iA5sZhzOS4NljmpgwY IRENBgYflnkJux0WdpNk47OW6ru29I3zloUS2145REFAE2cjsQPY2qrO1LyeVtX/eZQa +HHmAVXuhxLxEdXIgoJOihnRBtiNXP+ZCtIiaxqdzIBdhy7jRuYQ34UtWO0IVWePDSev s7HiMRjkJTsF6kPDuZR85/siXbI3eenk/8QK8gs0oMruYLihtB6WUgG4TmVJ+GH2qaW0 Kx/kk4DYYMenNqbcyrt8ZwsJJyRv5Yp5JmCx5bs6mIW1eecPLMrrSsJxT6CHcASdL5FU 0lDA== X-Received: by 10.140.146.3 with SMTP id 3mr32363682qhs.76.1443544561544; Tue, 29 Sep 2015 09:36:01 -0700 (PDT) Received: from localhost (bne75-h02-31-39-163-232.dsl.sta.abo.bbox.fr. [31.39.163.232]) by smtp.gmail.com with ESMTPSA id 35sm9612843qkv.42.2015.09.29.09.36.00 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Sep 2015 09:36:00 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Tue, 29 Sep 2015 18:34:52 +0200 Message-Id: <1443544494-28737-23-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1443544494-28737-1-git-send-email-marcandre.lureau@redhat.com> References: <1443544494-28737-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::22b Cc: haifeng.lin@huawei.com, mst@redhat.com, thibaut.collet@6wind.com, jasowang@redhat.com, pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [Qemu-devel] [PATCH v6 22/24] vhost-user-test: add live-migration test X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Marc-André Lureau This test checks that the log fd is given to the migration source, and mark dirty pages during migration. Signed-off-by: Marc-André Lureau --- tests/vhost-user-test.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 168 insertions(+), 3 deletions(-) diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index b2ac454..7b5f130 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -13,6 +13,7 @@ #include "libqtest.h" #include "qemu/option.h" +#include "qemu/range.h" #include "sysemu/char.h" #include "sysemu/sysemu.h" @@ -54,6 +55,9 @@ #define VHOST_MEMORY_MAX_NREGIONS 8 #define VHOST_USER_F_PROTOCOL_FEATURES 30 +#define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1 + +#define VHOST_LOG_PAGE 0x1000 typedef enum VhostUserRequest { VHOST_USER_NONE = 0, @@ -124,6 +128,7 @@ typedef struct TestServer { VhostUserMemory memory; GMutex *data_mutex; GCond *data_cond; + int log_fd; } TestServer; static const char *hugefs; @@ -311,7 +316,8 @@ static void chr_read(void *opaque, const uint8_t *buf, int size) /* send back features to qemu */ msg.flags |= VHOST_USER_REPLY_MASK; msg.size = sizeof(m.u64); - msg.u64 = 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES; + msg.u64 = 0x1ULL << VHOST_F_LOG_ALL | + 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES; p = (uint8_t *) &msg; qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); break; @@ -325,12 +331,11 @@ static void chr_read(void *opaque, const uint8_t *buf, int size) /* send back features to qemu */ msg.flags |= VHOST_USER_REPLY_MASK; msg.size = sizeof(m.u64); - msg.u64 = 0; + msg.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD; p = (uint8_t *) &msg; qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); break; - case VHOST_USER_GET_VRING_BASE: /* send back vring base to qemu */ msg.flags |= VHOST_USER_REPLY_MASK; @@ -360,6 +365,21 @@ static void chr_read(void *opaque, const uint8_t *buf, int size) */ qemu_set_nonblock(fd); break; + + case VHOST_USER_SET_LOG_BASE: + if (s->log_fd != -1) { + close(s->log_fd); + s->log_fd = -1; + } + qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1); + msg.flags |= VHOST_USER_REPLY_MASK; + msg.size = 0; + p = (uint8_t *) &msg; + qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE); + + g_cond_signal(s->data_cond); + break; + default: break; } @@ -386,6 +406,8 @@ static TestServer *test_server_new(const gchar *name) server->data_mutex = _mutex_new(); server->data_cond = _cond_new(); + server->log_fd = -1; + return server; } @@ -407,15 +429,157 @@ static void test_server_free(TestServer *server) close(server->fds[i]); } + if (server->log_fd != -1) { + close(server->log_fd); + } + unlink(server->socket_path); g_free(server->socket_path); _cond_free(server->data_cond); _mutex_free(server->data_mutex); + g_free(server->chr_name); g_free(server); } +static void wait_for_log_fd(TestServer *s) +{ + gint64 end_time; + + g_mutex_lock(s->data_mutex); + end_time = _get_time() + 5 * G_TIME_SPAN_SECOND; + while (s->log_fd == -1) { + if (!_cond_wait_until(s->data_cond, s->data_mutex, end_time)) { + /* timeout has passed */ + g_assert(s->log_fd != -1); + break; + } + } + + g_mutex_unlock(s->data_mutex); +} + +static void write_guest_mem(TestServer *s, uint32 seed) +{ + uint32_t *guest_mem; + int i, j; + size_t size; + + wait_for_fds(s); + + /* iterate all regions */ + for (i = 0; i < s->fds_num; i++) { + + /* We'll write only the region statring at 0x0 */ + if (s->memory.regions[i].guest_phys_addr != 0x0) { + continue; + } + + g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024); + + size = s->memory.regions[i].memory_size + + s->memory.regions[i].mmap_offset; + + guest_mem = mmap(0, size, PROT_READ | PROT_WRITE, + MAP_SHARED, s->fds[i], 0); + + g_assert(guest_mem != MAP_FAILED); + guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem)); + + for (j = 0; j < 256; j++) { + guest_mem[j] = seed + j; + } + + munmap(guest_mem, s->memory.regions[i].memory_size); + break; + } +} + +static guint64 get_log_size(TestServer *s) +{ + guint64 log_size = 0; + int i; + + for (i = 0; i < s->memory.nregions; ++i) { + VhostUserMemoryRegion *reg = &s->memory.regions[i]; + guint64 last = range_get_last(reg->guest_phys_addr, + reg->memory_size); + log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1); + } + + return log_size; +} + +static void test_migrate(void) +{ + TestServer *s = test_server_new("src"); + TestServer *dest = test_server_new("dest"); + const char *uri = "tcp:127.0.0.1:1234"; + QTestState *global = global_qtest, *from, *to; + gchar *cmd; + QDict *rsp; + guint8 *log; + guint64 size; + + cmd = GET_QEMU_CMDE(s, 2, ""); + from = qtest_start(cmd); + g_free(cmd); + + wait_for_fds(s); + 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); + to = qtest_init(cmd); + g_free(cmd); + + /* slow down migration to have time to fiddle with log */ + /* TODO: qtest could learn to break on some places */ + rsp = qmp("{ 'execute': 'migrate_set_speed'," + "'arguments': { 'value': 10 } }"); + g_assert(qdict_haskey(rsp, "return")); + QDECREF(rsp); + + cmd = g_strdup_printf("{ 'execute': 'migrate'," + "'arguments': { 'uri': '%s' } }", + uri); + rsp = qmp(cmd); + g_free(cmd); + g_assert(qdict_haskey(rsp, "return")); + QDECREF(rsp); + + wait_for_log_fd(s); + + log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0); + g_assert(log != MAP_FAILED); + + /* modify first page */ + write_guest_mem(s, 0x42); + log[0] = 1; + munmap(log, size); + + /* speed things up */ + rsp = qmp("{ 'execute': 'migrate_set_speed'," + "'arguments': { 'value': 0 } }"); + g_assert(qdict_haskey(rsp, "return")); + QDECREF(rsp); + + qmp_eventwait("STOP"); + + global_qtest = to; + qmp_eventwait("RESUME"); + + read_guest_mem(dest); + + qtest_quit(to); + test_server_free(dest); + qtest_quit(from); + test_server_free(s); + + global_qtest = global; +} + static const char *init_hugepagefs(void) { const char *path; @@ -474,6 +638,7 @@ int main(int argc, char **argv) g_free(qemu_cmd); qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem); + qtest_add_func("/vhost-user/migrate", test_migrate); ret = g_test_run();