4 * Copyright (c) 2019-2020 Red Hat Inc
7 * Juan Quintela <quintela@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "exec/target_page.h"
16 #include "sysemu/sysemu.h"
17 #include "exec/ramblock.h"
18 #include "qemu/error-report.h"
19 #include "qapi/error.h"
21 #include "migration.h"
24 #include "qemu-file.h"
28 #include "qemu/yank.h"
29 #include "io/channel-socket.h"
30 #include "yank_functions.h"
34 #define MULTIFD_MAGIC 0x11223344U
35 #define MULTIFD_VERSION 1
40 unsigned char uuid[16]; /* QemuUUID */
42 uint8_t unused1[7]; /* Reserved for future use */
43 uint64_t unused2[4]; /* Reserved for future use */
44 } __attribute__((packed)) MultiFDInit_t;
46 /* Multifd without compression */
49 * nocomp_send_setup: setup send side
51 * For no compression this function does nothing.
53 * Returns 0 for success or -1 for error
55 * @p: Params for the channel that we are using
56 * @errp: pointer to an error
58 static int nocomp_send_setup(MultiFDSendParams *p, Error **errp)
64 * nocomp_send_cleanup: cleanup send side
66 * For no compression this function does nothing.
68 * @p: Params for the channel that we are using
69 * @errp: pointer to an error
71 static void nocomp_send_cleanup(MultiFDSendParams *p, Error **errp)
77 * nocomp_send_prepare: prepare date to be able to send
79 * For no compression we just have to calculate the size of the
82 * Returns 0 for success or -1 for error
84 * @p: Params for the channel that we are using
85 * @errp: pointer to an error
87 static int nocomp_send_prepare(MultiFDSendParams *p, Error **errp)
89 MultiFDPages_t *pages = p->pages;
90 size_t page_size = qemu_target_page_size();
92 for (int i = 0; i < p->pages->num; i++) {
93 p->iov[p->iovs_num].iov_base = pages->block->host + pages->offset[i];
94 p->iov[p->iovs_num].iov_len = page_size;
98 p->next_packet_size = p->pages->num * page_size;
99 p->flags |= MULTIFD_FLAG_NOCOMP;
104 * nocomp_recv_setup: setup receive side
106 * For no compression this function does nothing.
108 * Returns 0 for success or -1 for error
110 * @p: Params for the channel that we are using
111 * @errp: pointer to an error
113 static int nocomp_recv_setup(MultiFDRecvParams *p, Error **errp)
119 * nocomp_recv_cleanup: setup receive side
121 * For no compression this function does nothing.
123 * @p: Params for the channel that we are using
125 static void nocomp_recv_cleanup(MultiFDRecvParams *p)
130 * nocomp_recv_pages: read the data from the channel into actual pages
132 * For no compression we just need to read things into the correct place.
134 * Returns 0 for success or -1 for error
136 * @p: Params for the channel that we are using
137 * @errp: pointer to an error
139 static int nocomp_recv_pages(MultiFDRecvParams *p, Error **errp)
141 uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
142 size_t page_size = qemu_target_page_size();
144 if (flags != MULTIFD_FLAG_NOCOMP) {
145 error_setg(errp, "multifd %u: flags received %x flags expected %x",
146 p->id, flags, MULTIFD_FLAG_NOCOMP);
149 for (int i = 0; i < p->pages->num; i++) {
150 p->iov[i].iov_base = p->pages->block->host + p->pages->offset[i];
151 p->iov[i].iov_len = page_size;
153 return qio_channel_readv_all(p->c, p->iov, p->pages->num, errp);
156 static MultiFDMethods multifd_nocomp_ops = {
157 .send_setup = nocomp_send_setup,
158 .send_cleanup = nocomp_send_cleanup,
159 .send_prepare = nocomp_send_prepare,
160 .recv_setup = nocomp_recv_setup,
161 .recv_cleanup = nocomp_recv_cleanup,
162 .recv_pages = nocomp_recv_pages
165 static MultiFDMethods *multifd_ops[MULTIFD_COMPRESSION__MAX] = {
166 [MULTIFD_COMPRESSION_NONE] = &multifd_nocomp_ops,
169 void multifd_register_ops(int method, MultiFDMethods *ops)
171 assert(0 < method && method < MULTIFD_COMPRESSION__MAX);
172 multifd_ops[method] = ops;
175 static int multifd_send_initial_packet(MultiFDSendParams *p, Error **errp)
177 MultiFDInit_t msg = {};
180 msg.magic = cpu_to_be32(MULTIFD_MAGIC);
181 msg.version = cpu_to_be32(MULTIFD_VERSION);
183 memcpy(msg.uuid, &qemu_uuid.data, sizeof(msg.uuid));
185 ret = qio_channel_write_all(p->c, (char *)&msg, sizeof(msg), errp);
192 static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
197 ret = qio_channel_read_all(c, (char *)&msg, sizeof(msg), errp);
202 msg.magic = be32_to_cpu(msg.magic);
203 msg.version = be32_to_cpu(msg.version);
205 if (msg.magic != MULTIFD_MAGIC) {
206 error_setg(errp, "multifd: received packet magic %x "
207 "expected %x", msg.magic, MULTIFD_MAGIC);
211 if (msg.version != MULTIFD_VERSION) {
212 error_setg(errp, "multifd: received packet version %u "
213 "expected %u", msg.version, MULTIFD_VERSION);
217 if (memcmp(msg.uuid, &qemu_uuid, sizeof(qemu_uuid))) {
218 char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid);
219 char *msg_uuid = qemu_uuid_unparse_strdup((const QemuUUID *)msg.uuid);
221 error_setg(errp, "multifd: received uuid '%s' and expected "
222 "uuid '%s' for channel %hhd", msg_uuid, uuid, msg.id);
228 if (msg.id > migrate_multifd_channels()) {
229 error_setg(errp, "multifd: received channel version %u "
230 "expected %u", msg.version, MULTIFD_VERSION);
237 static MultiFDPages_t *multifd_pages_init(size_t size)
239 MultiFDPages_t *pages = g_new0(MultiFDPages_t, 1);
241 pages->allocated = size;
242 pages->offset = g_new0(ram_addr_t, size);
247 static void multifd_pages_clear(MultiFDPages_t *pages)
250 pages->allocated = 0;
251 pages->packet_num = 0;
253 g_free(pages->offset);
254 pages->offset = NULL;
258 static void multifd_send_fill_packet(MultiFDSendParams *p)
260 MultiFDPacket_t *packet = p->packet;
263 packet->flags = cpu_to_be32(p->flags);
264 packet->pages_alloc = cpu_to_be32(p->pages->allocated);
265 packet->pages_used = cpu_to_be32(p->pages->num);
266 packet->next_packet_size = cpu_to_be32(p->next_packet_size);
267 packet->packet_num = cpu_to_be64(p->packet_num);
269 if (p->pages->block) {
270 strncpy(packet->ramblock, p->pages->block->idstr, 256);
273 for (i = 0; i < p->pages->num; i++) {
274 /* there are architectures where ram_addr_t is 32 bit */
275 uint64_t temp = p->pages->offset[i];
277 packet->offset[i] = cpu_to_be64(temp);
281 static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)
283 MultiFDPacket_t *packet = p->packet;
284 size_t page_size = qemu_target_page_size();
285 uint32_t pages_max = MULTIFD_PACKET_SIZE / page_size;
289 packet->magic = be32_to_cpu(packet->magic);
290 if (packet->magic != MULTIFD_MAGIC) {
291 error_setg(errp, "multifd: received packet "
292 "magic %x and expected magic %x",
293 packet->magic, MULTIFD_MAGIC);
297 packet->version = be32_to_cpu(packet->version);
298 if (packet->version != MULTIFD_VERSION) {
299 error_setg(errp, "multifd: received packet "
300 "version %u and expected version %u",
301 packet->version, MULTIFD_VERSION);
305 p->flags = be32_to_cpu(packet->flags);
307 packet->pages_alloc = be32_to_cpu(packet->pages_alloc);
309 * If we received a packet that is 100 times bigger than expected
310 * just stop migration. It is a magic number.
312 if (packet->pages_alloc > pages_max * 100) {
313 error_setg(errp, "multifd: received packet "
314 "with size %u and expected a maximum size of %u",
315 packet->pages_alloc, pages_max * 100) ;
319 * We received a packet that is bigger than expected but inside
320 * reasonable limits (see previous comment). Just reallocate.
322 if (packet->pages_alloc > p->pages->allocated) {
323 multifd_pages_clear(p->pages);
324 p->pages = multifd_pages_init(packet->pages_alloc);
327 p->pages->num = be32_to_cpu(packet->pages_used);
328 if (p->pages->num > packet->pages_alloc) {
329 error_setg(errp, "multifd: received packet "
330 "with %u pages and expected maximum pages are %u",
331 p->pages->num, packet->pages_alloc) ;
335 p->next_packet_size = be32_to_cpu(packet->next_packet_size);
336 p->packet_num = be64_to_cpu(packet->packet_num);
338 if (p->pages->num == 0) {
342 /* make sure that ramblock is 0 terminated */
343 packet->ramblock[255] = 0;
344 block = qemu_ram_block_by_name(packet->ramblock);
346 error_setg(errp, "multifd: unknown ram block %s",
351 p->pages->block = block;
352 for (i = 0; i < p->pages->num; i++) {
353 uint64_t offset = be64_to_cpu(packet->offset[i]);
355 if (offset > (block->used_length - page_size)) {
356 error_setg(errp, "multifd: offset too long %" PRIu64
357 " (max " RAM_ADDR_FMT ")",
358 offset, block->used_length);
361 p->pages->offset[i] = offset;
368 MultiFDSendParams *params;
369 /* array of pages to sent */
370 MultiFDPages_t *pages;
371 /* global number of generated multifd packets */
373 /* send channels ready */
374 QemuSemaphore channels_ready;
376 * Have we already run terminate threads. There is a race when it
377 * happens that we got one error while we are exiting.
378 * We will use atomic operations. Only valid values are 0 and 1.
383 } *multifd_send_state;
386 * How we use multifd_send_state->pages and channel->pages?
388 * We create a pages for each channel, and a main one. Each time that
389 * we need to send a batch of pages we interchange the ones between
390 * multifd_send_state and the channel that is sending it. There are
391 * two reasons for that:
392 * - to not have to do so many mallocs during migration
393 * - to make easier to know what to free at the end of migration
395 * This way we always know who is the owner of each "pages" struct,
396 * and we don't need any locking. It belongs to the migration thread
397 * or to the channel thread. Switching is safe because the migration
398 * thread is using the channel mutex when changing it, and the channel
399 * have to had finish with its own, otherwise pending_job can't be
403 static int multifd_send_pages(QEMUFile *f)
406 static int next_channel;
407 MultiFDSendParams *p = NULL; /* make happy gcc */
408 MultiFDPages_t *pages = multifd_send_state->pages;
409 uint64_t transferred;
411 if (qatomic_read(&multifd_send_state->exiting)) {
415 qemu_sem_wait(&multifd_send_state->channels_ready);
417 * next_channel can remain from a previous migration that was
418 * using more channels, so ensure it doesn't overflow if the
419 * limit is lower now.
421 next_channel %= migrate_multifd_channels();
422 for (i = next_channel;; i = (i + 1) % migrate_multifd_channels()) {
423 p = &multifd_send_state->params[i];
425 qemu_mutex_lock(&p->mutex);
427 error_report("%s: channel %d has already quit!", __func__, i);
428 qemu_mutex_unlock(&p->mutex);
431 if (!p->pending_job) {
433 next_channel = (i + 1) % migrate_multifd_channels();
436 qemu_mutex_unlock(&p->mutex);
438 assert(!p->pages->num);
439 assert(!p->pages->block);
441 p->packet_num = multifd_send_state->packet_num++;
442 multifd_send_state->pages = p->pages;
444 transferred = ((uint64_t) pages->num) * qemu_target_page_size()
446 qemu_file_update_transfer(f, transferred);
447 ram_counters.multifd_bytes += transferred;
448 ram_counters.transferred += transferred;
449 qemu_mutex_unlock(&p->mutex);
450 qemu_sem_post(&p->sem);
455 int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset)
457 MultiFDPages_t *pages = multifd_send_state->pages;
460 pages->block = block;
463 if (pages->block == block) {
464 pages->offset[pages->num] = offset;
467 if (pages->num < pages->allocated) {
472 if (multifd_send_pages(f) < 0) {
476 if (pages->block != block) {
477 return multifd_queue_page(f, block, offset);
483 static void multifd_send_terminate_threads(Error *err)
487 trace_multifd_send_terminate_threads(err != NULL);
490 MigrationState *s = migrate_get_current();
491 migrate_set_error(s, err);
492 if (s->state == MIGRATION_STATUS_SETUP ||
493 s->state == MIGRATION_STATUS_PRE_SWITCHOVER ||
494 s->state == MIGRATION_STATUS_DEVICE ||
495 s->state == MIGRATION_STATUS_ACTIVE) {
496 migrate_set_state(&s->state, s->state,
497 MIGRATION_STATUS_FAILED);
502 * We don't want to exit each threads twice. Depending on where
503 * we get the error, or if there are two independent errors in two
504 * threads at the same time, we can end calling this function
507 if (qatomic_xchg(&multifd_send_state->exiting, 1)) {
511 for (i = 0; i < migrate_multifd_channels(); i++) {
512 MultiFDSendParams *p = &multifd_send_state->params[i];
514 qemu_mutex_lock(&p->mutex);
516 qemu_sem_post(&p->sem);
518 qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
520 qemu_mutex_unlock(&p->mutex);
524 void multifd_save_cleanup(void)
528 if (!migrate_use_multifd() || !migrate_multifd_is_allowed()) {
531 multifd_send_terminate_threads(NULL);
532 for (i = 0; i < migrate_multifd_channels(); i++) {
533 MultiFDSendParams *p = &multifd_send_state->params[i];
536 qemu_thread_join(&p->thread);
539 for (i = 0; i < migrate_multifd_channels(); i++) {
540 MultiFDSendParams *p = &multifd_send_state->params[i];
541 Error *local_err = NULL;
543 if (p->registered_yank) {
544 migration_ioc_unregister_yank(p->c);
546 socket_send_channel_destroy(p->c);
548 qemu_mutex_destroy(&p->mutex);
549 qemu_sem_destroy(&p->sem);
550 qemu_sem_destroy(&p->sem_sync);
553 g_free(p->tls_hostname);
554 p->tls_hostname = NULL;
555 multifd_pages_clear(p->pages);
562 multifd_send_state->ops->send_cleanup(p, &local_err);
564 migrate_set_error(migrate_get_current(), local_err);
565 error_free(local_err);
568 qemu_sem_destroy(&multifd_send_state->channels_ready);
569 g_free(multifd_send_state->params);
570 multifd_send_state->params = NULL;
571 multifd_pages_clear(multifd_send_state->pages);
572 multifd_send_state->pages = NULL;
573 g_free(multifd_send_state);
574 multifd_send_state = NULL;
577 void multifd_send_sync_main(QEMUFile *f)
581 if (!migrate_use_multifd()) {
584 if (multifd_send_state->pages->num) {
585 if (multifd_send_pages(f) < 0) {
586 error_report("%s: multifd_send_pages fail", __func__);
590 for (i = 0; i < migrate_multifd_channels(); i++) {
591 MultiFDSendParams *p = &multifd_send_state->params[i];
593 trace_multifd_send_sync_main_signal(p->id);
595 qemu_mutex_lock(&p->mutex);
598 error_report("%s: channel %d has already quit", __func__, i);
599 qemu_mutex_unlock(&p->mutex);
603 p->packet_num = multifd_send_state->packet_num++;
604 p->flags |= MULTIFD_FLAG_SYNC;
606 qemu_file_update_transfer(f, p->packet_len);
607 ram_counters.multifd_bytes += p->packet_len;
608 ram_counters.transferred += p->packet_len;
609 qemu_mutex_unlock(&p->mutex);
610 qemu_sem_post(&p->sem);
612 for (i = 0; i < migrate_multifd_channels(); i++) {
613 MultiFDSendParams *p = &multifd_send_state->params[i];
615 trace_multifd_send_sync_main_wait(p->id);
616 qemu_sem_wait(&p->sem_sync);
618 trace_multifd_send_sync_main(multifd_send_state->packet_num);
621 static void *multifd_send_thread(void *opaque)
623 MultiFDSendParams *p = opaque;
624 Error *local_err = NULL;
627 trace_multifd_send_thread_start(p->id);
628 rcu_register_thread();
630 if (multifd_send_initial_packet(p, &local_err) < 0) {
638 qemu_sem_wait(&p->sem);
640 if (qatomic_read(&multifd_send_state->exiting)) {
643 qemu_mutex_lock(&p->mutex);
645 if (p->pending_job) {
646 uint32_t used = p->pages->num;
647 uint64_t packet_num = p->packet_num;
648 uint32_t flags = p->flags;
652 ret = multifd_send_state->ops->send_prepare(p, &local_err);
654 qemu_mutex_unlock(&p->mutex);
658 multifd_send_fill_packet(p);
661 p->num_pages += used;
663 p->pages->block = NULL;
664 qemu_mutex_unlock(&p->mutex);
666 trace_multifd_send(p->id, packet_num, used, flags,
667 p->next_packet_size);
669 p->iov[0].iov_len = p->packet_len;
670 p->iov[0].iov_base = p->packet;
672 ret = qio_channel_writev_all(p->c, p->iov, p->iovs_num,
678 qemu_mutex_lock(&p->mutex);
680 qemu_mutex_unlock(&p->mutex);
682 if (flags & MULTIFD_FLAG_SYNC) {
683 qemu_sem_post(&p->sem_sync);
685 qemu_sem_post(&multifd_send_state->channels_ready);
686 } else if (p->quit) {
687 qemu_mutex_unlock(&p->mutex);
690 qemu_mutex_unlock(&p->mutex);
691 /* sometimes there are spurious wakeups */
697 trace_multifd_send_error(p->id);
698 multifd_send_terminate_threads(local_err);
699 error_free(local_err);
703 * Error happen, I will exit, but I can't just leave, tell
704 * who pay attention to me.
707 qemu_sem_post(&p->sem_sync);
708 qemu_sem_post(&multifd_send_state->channels_ready);
711 qemu_mutex_lock(&p->mutex);
713 qemu_mutex_unlock(&p->mutex);
715 rcu_unregister_thread();
716 trace_multifd_send_thread_end(p->id, p->num_packets, p->num_pages);
721 static bool multifd_channel_connect(MultiFDSendParams *p,
725 static void multifd_tls_outgoing_handshake(QIOTask *task,
728 MultiFDSendParams *p = opaque;
729 QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
732 if (qio_task_propagate_error(task, &err)) {
733 trace_multifd_tls_outgoing_handshake_error(ioc, error_get_pretty(err));
735 trace_multifd_tls_outgoing_handshake_complete(ioc);
738 if (!multifd_channel_connect(p, ioc, err)) {
740 * Error happen, mark multifd_send_thread status as 'quit' although it
741 * is not created, and then tell who pay attention to me.
744 qemu_sem_post(&multifd_send_state->channels_ready);
745 qemu_sem_post(&p->sem_sync);
749 static void *multifd_tls_handshake_thread(void *opaque)
751 MultiFDSendParams *p = opaque;
752 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(p->c);
754 qio_channel_tls_handshake(tioc,
755 multifd_tls_outgoing_handshake,
762 static void multifd_tls_channel_connect(MultiFDSendParams *p,
766 MigrationState *s = migrate_get_current();
767 const char *hostname = p->tls_hostname;
770 tioc = migration_tls_client_create(s, ioc, hostname, errp);
775 object_unref(OBJECT(ioc));
776 trace_multifd_tls_outgoing_handshake_start(ioc, tioc, hostname);
777 qio_channel_set_name(QIO_CHANNEL(tioc), "multifd-tls-outgoing");
778 p->c = QIO_CHANNEL(tioc);
779 qemu_thread_create(&p->thread, "multifd-tls-handshake-worker",
780 multifd_tls_handshake_thread, p,
781 QEMU_THREAD_JOINABLE);
784 static bool multifd_channel_connect(MultiFDSendParams *p,
788 MigrationState *s = migrate_get_current();
790 trace_multifd_set_outgoing_channel(
791 ioc, object_get_typename(OBJECT(ioc)), p->tls_hostname, error);
794 if (s->parameters.tls_creds &&
795 *s->parameters.tls_creds &&
796 !object_dynamic_cast(OBJECT(ioc),
797 TYPE_QIO_CHANNEL_TLS)) {
798 multifd_tls_channel_connect(p, ioc, &error);
801 * tls_channel_connect will call back to this
802 * function after the TLS handshake,
803 * so we mustn't call multifd_send_thread until then
810 migration_ioc_register_yank(ioc);
811 p->registered_yank = true;
813 qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
814 QEMU_THREAD_JOINABLE);
822 static void multifd_new_send_channel_cleanup(MultiFDSendParams *p,
823 QIOChannel *ioc, Error *err)
825 migrate_set_error(migrate_get_current(), err);
826 /* Error happen, we need to tell who pay attention to me */
827 qemu_sem_post(&multifd_send_state->channels_ready);
828 qemu_sem_post(&p->sem_sync);
830 * Although multifd_send_thread is not created, but main migration
831 * thread neet to judge whether it is running, so we need to mark
835 object_unref(OBJECT(ioc));
839 static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque)
841 MultiFDSendParams *p = opaque;
842 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
843 Error *local_err = NULL;
845 trace_multifd_new_send_channel_async(p->id);
846 if (qio_task_propagate_error(task, &local_err)) {
849 p->c = QIO_CHANNEL(sioc);
850 qio_channel_set_delay(p->c, false);
852 if (!multifd_channel_connect(p, sioc, local_err)) {
859 multifd_new_send_channel_cleanup(p, sioc, local_err);
862 static bool migrate_allow_multifd = true;
863 void migrate_protocol_allow_multifd(bool allow)
865 migrate_allow_multifd = allow;
868 bool migrate_multifd_is_allowed(void)
870 return migrate_allow_multifd;
873 int multifd_save_setup(Error **errp)
876 uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
880 if (!migrate_use_multifd()) {
883 if (!migrate_multifd_is_allowed()) {
884 error_setg(errp, "multifd is not supported by current protocol");
888 s = migrate_get_current();
889 thread_count = migrate_multifd_channels();
890 multifd_send_state = g_malloc0(sizeof(*multifd_send_state));
891 multifd_send_state->params = g_new0(MultiFDSendParams, thread_count);
892 multifd_send_state->pages = multifd_pages_init(page_count);
893 qemu_sem_init(&multifd_send_state->channels_ready, 0);
894 qatomic_set(&multifd_send_state->exiting, 0);
895 multifd_send_state->ops = multifd_ops[migrate_multifd_compression()];
897 for (i = 0; i < thread_count; i++) {
898 MultiFDSendParams *p = &multifd_send_state->params[i];
900 qemu_mutex_init(&p->mutex);
901 qemu_sem_init(&p->sem, 0);
902 qemu_sem_init(&p->sem_sync, 0);
906 p->pages = multifd_pages_init(page_count);
907 p->packet_len = sizeof(MultiFDPacket_t)
908 + sizeof(uint64_t) * page_count;
909 p->packet = g_malloc0(p->packet_len);
910 p->packet->magic = cpu_to_be32(MULTIFD_MAGIC);
911 p->packet->version = cpu_to_be32(MULTIFD_VERSION);
912 p->name = g_strdup_printf("multifdsend_%d", i);
913 p->tls_hostname = g_strdup(s->hostname);
914 /* We need one extra place for the packet header */
915 p->iov = g_new0(struct iovec, page_count + 1);
916 socket_send_channel_create(multifd_new_send_channel_async, p);
919 for (i = 0; i < thread_count; i++) {
920 MultiFDSendParams *p = &multifd_send_state->params[i];
921 Error *local_err = NULL;
924 ret = multifd_send_state->ops->send_setup(p, &local_err);
926 error_propagate(errp, local_err);
934 MultiFDRecvParams *params;
935 /* number of created threads */
937 /* syncs main thread and channels */
938 QemuSemaphore sem_sync;
939 /* global number of generated multifd packets */
943 } *multifd_recv_state;
945 static void multifd_recv_terminate_threads(Error *err)
949 trace_multifd_recv_terminate_threads(err != NULL);
952 MigrationState *s = migrate_get_current();
953 migrate_set_error(s, err);
954 if (s->state == MIGRATION_STATUS_SETUP ||
955 s->state == MIGRATION_STATUS_ACTIVE) {
956 migrate_set_state(&s->state, s->state,
957 MIGRATION_STATUS_FAILED);
961 for (i = 0; i < migrate_multifd_channels(); i++) {
962 MultiFDRecvParams *p = &multifd_recv_state->params[i];
964 qemu_mutex_lock(&p->mutex);
967 * We could arrive here for two reasons:
968 * - normal quit, i.e. everything went fine, just finished
969 * - error quit: We close the channels so the channel threads
970 * finish the qio_channel_read_all_eof()
973 qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
975 qemu_mutex_unlock(&p->mutex);
979 int multifd_load_cleanup(Error **errp)
983 if (!migrate_use_multifd() || !migrate_multifd_is_allowed()) {
986 multifd_recv_terminate_threads(NULL);
987 for (i = 0; i < migrate_multifd_channels(); i++) {
988 MultiFDRecvParams *p = &multifd_recv_state->params[i];
993 * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
994 * however try to wakeup it without harm in cleanup phase.
996 qemu_sem_post(&p->sem_sync);
997 qemu_thread_join(&p->thread);
1000 for (i = 0; i < migrate_multifd_channels(); i++) {
1001 MultiFDRecvParams *p = &multifd_recv_state->params[i];
1003 migration_ioc_unregister_yank(p->c);
1004 object_unref(OBJECT(p->c));
1006 qemu_mutex_destroy(&p->mutex);
1007 qemu_sem_destroy(&p->sem_sync);
1010 multifd_pages_clear(p->pages);
1017 multifd_recv_state->ops->recv_cleanup(p);
1019 qemu_sem_destroy(&multifd_recv_state->sem_sync);
1020 g_free(multifd_recv_state->params);
1021 multifd_recv_state->params = NULL;
1022 g_free(multifd_recv_state);
1023 multifd_recv_state = NULL;
1028 void multifd_recv_sync_main(void)
1032 if (!migrate_use_multifd()) {
1035 for (i = 0; i < migrate_multifd_channels(); i++) {
1036 MultiFDRecvParams *p = &multifd_recv_state->params[i];
1038 trace_multifd_recv_sync_main_wait(p->id);
1039 qemu_sem_wait(&multifd_recv_state->sem_sync);
1041 for (i = 0; i < migrate_multifd_channels(); i++) {
1042 MultiFDRecvParams *p = &multifd_recv_state->params[i];
1044 WITH_QEMU_LOCK_GUARD(&p->mutex) {
1045 if (multifd_recv_state->packet_num < p->packet_num) {
1046 multifd_recv_state->packet_num = p->packet_num;
1049 trace_multifd_recv_sync_main_signal(p->id);
1050 qemu_sem_post(&p->sem_sync);
1052 trace_multifd_recv_sync_main(multifd_recv_state->packet_num);
1055 static void *multifd_recv_thread(void *opaque)
1057 MultiFDRecvParams *p = opaque;
1058 Error *local_err = NULL;
1061 trace_multifd_recv_thread_start(p->id);
1062 rcu_register_thread();
1072 ret = qio_channel_read_all_eof(p->c, (void *)p->packet,
1073 p->packet_len, &local_err);
1074 if (ret == 0) { /* EOF */
1077 if (ret == -1) { /* Error */
1081 qemu_mutex_lock(&p->mutex);
1082 ret = multifd_recv_unfill_packet(p, &local_err);
1084 qemu_mutex_unlock(&p->mutex);
1088 used = p->pages->num;
1090 /* recv methods don't know how to handle the SYNC flag */
1091 p->flags &= ~MULTIFD_FLAG_SYNC;
1092 trace_multifd_recv(p->id, p->packet_num, used, flags,
1093 p->next_packet_size);
1095 p->num_pages += used;
1096 qemu_mutex_unlock(&p->mutex);
1099 ret = multifd_recv_state->ops->recv_pages(p, &local_err);
1105 if (flags & MULTIFD_FLAG_SYNC) {
1106 qemu_sem_post(&multifd_recv_state->sem_sync);
1107 qemu_sem_wait(&p->sem_sync);
1112 multifd_recv_terminate_threads(local_err);
1113 error_free(local_err);
1115 qemu_mutex_lock(&p->mutex);
1117 qemu_mutex_unlock(&p->mutex);
1119 rcu_unregister_thread();
1120 trace_multifd_recv_thread_end(p->id, p->num_packets, p->num_pages);
1125 int multifd_load_setup(Error **errp)
1128 uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
1131 if (!migrate_use_multifd()) {
1134 if (!migrate_multifd_is_allowed()) {
1135 error_setg(errp, "multifd is not supported by current protocol");
1138 thread_count = migrate_multifd_channels();
1139 multifd_recv_state = g_malloc0(sizeof(*multifd_recv_state));
1140 multifd_recv_state->params = g_new0(MultiFDRecvParams, thread_count);
1141 qatomic_set(&multifd_recv_state->count, 0);
1142 qemu_sem_init(&multifd_recv_state->sem_sync, 0);
1143 multifd_recv_state->ops = multifd_ops[migrate_multifd_compression()];
1145 for (i = 0; i < thread_count; i++) {
1146 MultiFDRecvParams *p = &multifd_recv_state->params[i];
1148 qemu_mutex_init(&p->mutex);
1149 qemu_sem_init(&p->sem_sync, 0);
1152 p->pages = multifd_pages_init(page_count);
1153 p->packet_len = sizeof(MultiFDPacket_t)
1154 + sizeof(uint64_t) * page_count;
1155 p->packet = g_malloc0(p->packet_len);
1156 p->name = g_strdup_printf("multifdrecv_%d", i);
1157 p->iov = g_new0(struct iovec, page_count);
1160 for (i = 0; i < thread_count; i++) {
1161 MultiFDRecvParams *p = &multifd_recv_state->params[i];
1162 Error *local_err = NULL;
1165 ret = multifd_recv_state->ops->recv_setup(p, &local_err);
1167 error_propagate(errp, local_err);
1174 bool multifd_recv_all_channels_created(void)
1176 int thread_count = migrate_multifd_channels();
1178 if (!migrate_use_multifd()) {
1182 if (!multifd_recv_state) {
1183 /* Called before any connections created */
1187 return thread_count == qatomic_read(&multifd_recv_state->count);
1191 * Try to receive all multifd channels to get ready for the migration.
1192 * - Return true and do not set @errp when correctly receiving all channels;
1193 * - Return false and do not set @errp when correctly receiving the current one;
1194 * - Return false and set @errp when failing to receive the current channel.
1196 bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp)
1198 MultiFDRecvParams *p;
1199 Error *local_err = NULL;
1202 id = multifd_recv_initial_packet(ioc, &local_err);
1204 multifd_recv_terminate_threads(local_err);
1205 error_propagate_prepend(errp, local_err,
1206 "failed to receive packet"
1207 " via multifd channel %d: ",
1208 qatomic_read(&multifd_recv_state->count));
1211 trace_multifd_recv_new_channel(id);
1213 p = &multifd_recv_state->params[id];
1215 error_setg(&local_err, "multifd: received id '%d' already setup'",
1217 multifd_recv_terminate_threads(local_err);
1218 error_propagate(errp, local_err);
1222 object_ref(OBJECT(ioc));
1223 /* initial packet */
1227 qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p,
1228 QEMU_THREAD_JOINABLE);
1229 qatomic_inc(&multifd_recv_state->count);
1230 return qatomic_read(&multifd_recv_state->count) ==
1231 migrate_multifd_channels();