From nobody Wed Feb 11 01:09:03 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505301242414913.3645246284092; Wed, 13 Sep 2017 04:14:02 -0700 (PDT) Received: from localhost ([::1]:41609 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds5cH-0001iY-LO for importer@patchew.org; Wed, 13 Sep 2017 07:14:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42319) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds5PL-0006RT-VK for qemu-devel@nongnu.org; Wed, 13 Sep 2017 07:00:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ds5PG-0006qt-Fb for qemu-devel@nongnu.org; Wed, 13 Sep 2017 07:00:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38800) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ds5PG-0006qE-7g for qemu-devel@nongnu.org; Wed, 13 Sep 2017 07:00:34 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 43CEC5F798 for ; Wed, 13 Sep 2017 11:00:33 +0000 (UTC) Received: from secure.mitica (ovpn-117-188.ams2.redhat.com [10.36.117.188]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0556361786; Wed, 13 Sep 2017 11:00:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 43CEC5F798 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=quintela@redhat.com From: Juan Quintela To: qemu-devel@nongnu.org Date: Wed, 13 Sep 2017 12:59:45 +0200 Message-Id: <20170913105953.13760-13-quintela@redhat.com> In-Reply-To: <20170913105953.13760-1-quintela@redhat.com> References: <20170913105953.13760-1-quintela@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 13 Sep 2017 11:00:33 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v8 12/20] migration: Start of multiple fd work X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lvivier@redhat.com, dgilbert@redhat.com, peterx@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We create new channels for each new thread created. We send through them a string containing multifd so we are sure that we connect the right channels in both sides. Signed-off-by: Juan Quintela -- Split SocketArgs into incoming and outgoing args Use UUID's on the initial message, so we are sure we are connecting to the right channel. Remove init semaphore. Now that we use uuids on the init message, we know that this is our channel. Fix recv socket destwroy, we were destroying send channels. This was very interesting, because we were using an unreferred object without problems. Move to struct of pointers init channel sooner. split recv thread creation. listen on main thread We count the number of created threads to know when we need to stop listeni= ng Use g_strdup_printf report channel id on errors Add name parameter Use local_err Add Error * parameter to socket_send_channel_create() Use qio_channel_*_all Use asynchronous connect --- migration/migration.c | 5 ++ migration/ram.c | 138 +++++++++++++++++++++++++++++++++++++++++++---= ---- migration/ram.h | 3 ++ migration/socket.c | 34 ++++++++++++- migration/socket.h | 10 ++++ 5 files changed, 172 insertions(+), 18 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 1401841997..679be8e8d4 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -419,6 +419,11 @@ void migration_ioc_process_incoming(QIOChannel *ioc) */ bool migration_has_all_channels(void) { + if (migrate_use_multifd()) { + int thread_count =3D migrate_multifd_channels(); + + return thread_count =3D=3D multifd_created_channels(); + } return true; } =20 diff --git a/migration/ram.c b/migration/ram.c index a3e2abb2a5..8577eeb032 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -36,6 +36,7 @@ #include "xbzrle.h" #include "ram.h" #include "migration.h" +#include "socket.h" #include "migration/register.h" #include "migration/misc.h" #include "qemu-file.h" @@ -46,6 +47,8 @@ #include "exec/ram_addr.h" #include "qemu/rcu_queue.h" #include "migration/colo.h" +#include "sysemu/sysemu.h" +#include "qemu/uuid.h" =20 /***********************************************************/ /* ram save/restore */ @@ -362,6 +365,7 @@ struct MultiFDSendParams { uint8_t id; char *name; QemuThread thread; + QIOChannel *c; QemuSemaphore sem; QemuMutex mutex; bool quit; @@ -378,6 +382,12 @@ static void terminate_multifd_send_threads(Error *errp) { int i; =20 + if (errp) { + MigrationState *s =3D migrate_get_current(); + migrate_set_error(s, errp); + migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE, + MIGRATION_STATUS_FAILED); + } for (i =3D 0; i < multifd_send_state->count; i++) { MultiFDSendParams *p =3D &multifd_send_state->params[i]; =20 @@ -403,6 +413,7 @@ int multifd_save_cleanup(Error **errp) qemu_thread_join(&p->thread); qemu_mutex_destroy(&p->mutex); qemu_sem_destroy(&p->sem); + socket_send_channel_destroy(p->c); g_free(p->name); p->name =3D NULL; } @@ -413,9 +424,32 @@ int multifd_save_cleanup(Error **errp) return ret; } =20 +/* Default uuid for multifd when qemu is not started with uuid */ +static char multifd_uuid[] =3D "5c49fd7e-af88-4a07-b6e8-091fd696ad40"; +/* strlen(multifd) + '-' + + '-' + UUID_FMT + '\0' */ +#define MULTIFD_UUID_MSG (7 + 1 + 3 + 1 + UUID_FMT_LEN + 1) + static void *multifd_send_thread(void *opaque) { MultiFDSendParams *p =3D opaque; + Error *local_err =3D NULL; + char *string; + char *string_uuid; + size_t ret; + + if (qemu_uuid_set) { + string_uuid =3D qemu_uuid_unparse_strdup(&qemu_uuid); + } else { + string_uuid =3D g_strdup(multifd_uuid); + } + string =3D g_strdup_printf("%s multifd %03d", string_uuid, p->id); + g_free(string_uuid); + ret =3D qio_channel_write_all(p->c, string, MULTIFD_UUID_MSG, &local_e= rr); + g_free(string); + if (ret !=3D 0) { + terminate_multifd_send_threads(local_err); + return NULL; + } =20 while (true) { qemu_mutex_lock(&p->mutex); @@ -430,6 +464,27 @@ static void *multifd_send_thread(void *opaque) return NULL; } =20 +static void multifd_new_channel_async(QIOTask *task, gpointer opaque) +{ + MultiFDSendParams *p =3D opaque; + QIOChannel *sioc =3D QIO_CHANNEL(qio_task_get_source(task)); + Error *local_err; + + if (qio_task_propagate_error(task, &local_err)) { + if (multifd_save_cleanup(&local_err) !=3D 0) { + migrate_set_error(migrate_get_current(), local_err); + } + } else { + p->c =3D QIO_CHANNEL(sioc); + qio_channel_set_delay(p->c, false); + + qemu_thread_create(&p->thread, p->name, multifd_send_thread, p, + QEMU_THREAD_JOINABLE); + + multifd_send_state->count++; + } +} + int multifd_save_setup(void) { int thread_count; @@ -450,10 +505,7 @@ int multifd_save_setup(void) p->quit =3D false; p->id =3D i; p->name =3D g_strdup_printf("multifdsend_%d", i); - qemu_thread_create(&p->thread, p->name, multifd_send_thread, p, - QEMU_THREAD_JOINABLE); - - multifd_send_state->count++; + socket_send_channel_create(multifd_new_channel_async, p); } return 0; } @@ -462,6 +514,7 @@ struct MultiFDRecvParams { uint8_t id; char *name; QemuThread thread; + QIOChannel *c; QemuSemaphore sem; QemuMutex mutex; bool quit; @@ -472,12 +525,22 @@ struct { MultiFDRecvParams *params; /* number of created threads */ int count; + /* Should we finish */ + bool quit; } *multifd_recv_state; =20 static void terminate_multifd_recv_threads(Error *errp) { int i; =20 + if (errp) { + MigrationState *s =3D migrate_get_current(); + migrate_set_error(s, errp); + migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE, + MIGRATION_STATUS_FAILED); + } + multifd_recv_state->quit =3D true; + for (i =3D 0; i < multifd_recv_state->count; i++) { MultiFDRecvParams *p =3D &multifd_recv_state->params[i]; =20 @@ -503,6 +566,7 @@ int multifd_load_cleanup(Error **errp) qemu_thread_join(&p->thread); qemu_mutex_destroy(&p->mutex); qemu_sem_destroy(&p->sem); + socket_recv_channel_destroy(p->c); g_free(p->name); p->name =3D NULL; } @@ -531,10 +595,56 @@ static void *multifd_recv_thread(void *opaque) return NULL; } =20 +void multifd_new_channel(QIOChannel *ioc) +{ + MultiFDRecvParams *p; + char string[MULTIFD_UUID_MSG]; + char string_uuid[UUID_FMT_LEN]; + Error *local_err =3D NULL; + char *uuid; + size_t ret; + int id; + + ret =3D qio_channel_read_all(ioc, string, sizeof(string), &local_err); + if (ret !=3D 0) { + terminate_multifd_recv_threads(local_err); + return; + } + sscanf(string, "%s multifd %03d", string_uuid, &id); + + if (qemu_uuid_set) { + uuid =3D qemu_uuid_unparse_strdup(&qemu_uuid); + } else { + uuid =3D g_strdup(multifd_uuid); + } + if (strcmp(string_uuid, uuid)) { + error_setg(&local_err, "multifd: received uuid '%s' and expected " + "uuid '%s' for channel %d", string_uuid, uuid, id); + terminate_multifd_recv_threads(local_err); + return; + } + g_free(uuid); + + p =3D &multifd_recv_state->params[id]; + if (p->id !=3D 0) { + error_setg(&local_err, "multifd: received id '%d' already setup'",= id); + terminate_multifd_recv_threads(local_err); + return; + } + qemu_mutex_init(&p->mutex); + qemu_sem_init(&p->sem, 0); + p->quit =3D false; + p->id =3D id; + p->c =3D ioc; + multifd_recv_state->count++; + p->name =3D g_strdup_printf("multifdrecv_%d", id); + qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p, + QEMU_THREAD_JOINABLE); +} + int multifd_load_setup(void) { int thread_count; - uint8_t i; =20 if (!migrate_use_multifd()) { return 0; @@ -543,21 +653,15 @@ int multifd_load_setup(void) multifd_recv_state =3D g_malloc0(sizeof(*multifd_recv_state)); multifd_recv_state->params =3D g_new0(MultiFDRecvParams, thread_count); multifd_recv_state->count =3D 0; - for (i =3D 0; i < thread_count; i++) { - MultiFDRecvParams *p =3D &multifd_recv_state->params[i]; - - qemu_mutex_init(&p->mutex); - qemu_sem_init(&p->sem, 0); - p->quit =3D false; - p->id =3D i; - p->name =3D g_strdup_printf("multifdrecv_%d", i); - qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p, - QEMU_THREAD_JOINABLE); - multifd_recv_state->count++; - } + multifd_recv_state->quit =3D false; return 0; } =20 +int multifd_created_channels(void) +{ + return multifd_recv_state->count; +} + /** * save_page_header: write page header to wire * diff --git a/migration/ram.h b/migration/ram.h index 4a72d66503..5221bc9beb 100644 --- a/migration/ram.h +++ b/migration/ram.h @@ -31,6 +31,7 @@ =20 #include "qemu-common.h" #include "exec/cpu-common.h" +#include "io/channel.h" =20 extern MigrationStats ram_counters; extern XBZRLECacheStats xbzrle_counters; @@ -43,6 +44,8 @@ int multifd_save_setup(void); int multifd_save_cleanup(Error **errp); int multifd_load_setup(void); int multifd_load_cleanup(Error **errp); +void multifd_new_channel(QIOChannel *ioc); +int multifd_created_channels(void); =20 uint64_t ram_pagesize_summary(void); int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t = len); diff --git a/migration/socket.c b/migration/socket.c index 2d70747a1a..22fb05edc8 100644 --- a/migration/socket.c +++ b/migration/socket.c @@ -26,6 +26,34 @@ #include "io/channel-socket.h" #include "trace.h" =20 +int socket_recv_channel_destroy(QIOChannel *recv) +{ + /* Remove channel */ + object_unref(OBJECT(recv)); + return 0; +} + +struct SocketOutgoingArgs { + SocketAddress *saddr; +} outgoing_args; + +void socket_send_channel_create(void (*f)(QIOTask *, gpointer), void *data) +{ + QIOChannelSocket *sioc =3D qio_channel_socket_new(); + qio_channel_socket_connect_async(sioc, outgoing_args.saddr, + f, data, NULL); +} + +int socket_send_channel_destroy(QIOChannel *send) +{ + /* Remove channel */ + object_unref(OBJECT(send)); + if (outgoing_args.saddr) { + qapi_free_SocketAddress(outgoing_args.saddr); + outgoing_args.saddr =3D NULL; + } + return 0; +} =20 static SocketAddress *tcp_build_address(const char *host_port, Error **err= p) { @@ -95,6 +123,11 @@ static void socket_start_outgoing_migration(MigrationSt= ate *s, struct SocketConnectData *data =3D g_new0(struct SocketConnectData, 1); =20 data->s =3D s; + + /* in case previous migration leaked it */ + qapi_free_SocketAddress(outgoing_args.saddr); + outgoing_args.saddr =3D saddr; + if (saddr->type =3D=3D SOCKET_ADDRESS_TYPE_INET) { data->hostname =3D g_strdup(saddr->u.inet.host); } @@ -105,7 +138,6 @@ static void socket_start_outgoing_migration(MigrationSt= ate *s, socket_outgoing_migration, data, socket_connect_data_free); - qapi_free_SocketAddress(saddr); } =20 void tcp_start_outgoing_migration(MigrationState *s, diff --git a/migration/socket.h b/migration/socket.h index 6b91e9db38..afb0ff0f51 100644 --- a/migration/socket.h +++ b/migration/socket.h @@ -16,6 +16,16 @@ =20 #ifndef QEMU_MIGRATION_SOCKET_H #define QEMU_MIGRATION_SOCKET_H + +#include "io/channel.h" +#include "io/task.h" + +QIOChannel *socket_recv_channel_create(void); +int socket_recv_channel_destroy(QIOChannel *recv); + +void socket_send_channel_create(void (*f)(QIOTask *, gpointer), void *data= ); +int socket_send_channel_destroy(QIOChannel *send); + void tcp_start_incoming_migration(const char *host_port, Error **errp); =20 void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, --=20 2.13.5