tcg: Tidy tcg-runtime-gvec.c types
[qemu/ar7.git] / migration / multifd.c
blobcb6a4a3ab8599ed528a1fd6ee6e2428a67adde80
1 /*
2 * Multifd common code
4 * Copyright (c) 2019-2020 Red Hat Inc
6 * Authors:
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"
14 #include "qemu/rcu.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"
20 #include "ram.h"
21 #include "migration.h"
22 #include "socket.h"
23 #include "qemu-file.h"
24 #include "trace.h"
25 #include "multifd.h"
27 /* Multiple fd's */
29 #define MULTIFD_MAGIC 0x11223344U
30 #define MULTIFD_VERSION 1
32 typedef struct {
33 uint32_t magic;
34 uint32_t version;
35 unsigned char uuid[16]; /* QemuUUID */
36 uint8_t id;
37 uint8_t unused1[7]; /* Reserved for future use */
38 uint64_t unused2[4]; /* Reserved for future use */
39 } __attribute__((packed)) MultiFDInit_t;
41 /* Multifd without compression */
43 /**
44 * nocomp_send_setup: setup send side
46 * For no compression this function does nothing.
48 * Returns 0 for success or -1 for error
50 * @p: Params for the channel that we are using
51 * @errp: pointer to an error
53 static int nocomp_send_setup(MultiFDSendParams *p, Error **errp)
55 return 0;
58 /**
59 * nocomp_send_cleanup: cleanup send side
61 * For no compression this function does nothing.
63 * @p: Params for the channel that we are using
65 static void nocomp_send_cleanup(MultiFDSendParams *p, Error **errp)
67 return;
70 /**
71 * nocomp_send_prepare: prepare date to be able to send
73 * For no compression we just have to calculate the size of the
74 * packet.
76 * Returns 0 for success or -1 for error
78 * @p: Params for the channel that we are using
79 * @used: number of pages used
80 * @errp: pointer to an error
82 static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
83 Error **errp)
85 p->next_packet_size = used * qemu_target_page_size();
86 p->flags |= MULTIFD_FLAG_NOCOMP;
87 return 0;
90 /**
91 * nocomp_send_write: do the actual write of the data
93 * For no compression we just have to write the data.
95 * Returns 0 for success or -1 for error
97 * @p: Params for the channel that we are using
98 * @used: number of pages used
99 * @errp: pointer to an error
101 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
103 return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
107 * nocomp_recv_setup: setup receive side
109 * For no compression this function does nothing.
111 * Returns 0 for success or -1 for error
113 * @p: Params for the channel that we are using
114 * @errp: pointer to an error
116 static int nocomp_recv_setup(MultiFDRecvParams *p, Error **errp)
118 return 0;
122 * nocomp_recv_cleanup: setup receive side
124 * For no compression this function does nothing.
126 * @p: Params for the channel that we are using
128 static void nocomp_recv_cleanup(MultiFDRecvParams *p)
133 * nocomp_recv_pages: read the data from the channel into actual pages
135 * For no compression we just need to read things into the correct place.
137 * Returns 0 for success or -1 for error
139 * @p: Params for the channel that we are using
140 * @used: number of pages used
141 * @errp: pointer to an error
143 static int nocomp_recv_pages(MultiFDRecvParams *p, uint32_t used, Error **errp)
145 uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
147 if (flags != MULTIFD_FLAG_NOCOMP) {
148 error_setg(errp, "multifd %d: flags received %x flags expected %x",
149 p->id, flags, MULTIFD_FLAG_NOCOMP);
150 return -1;
152 return qio_channel_readv_all(p->c, p->pages->iov, used, errp);
155 static MultiFDMethods multifd_nocomp_ops = {
156 .send_setup = nocomp_send_setup,
157 .send_cleanup = nocomp_send_cleanup,
158 .send_prepare = nocomp_send_prepare,
159 .send_write = nocomp_send_write,
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 = {};
178 int ret;
180 msg.magic = cpu_to_be32(MULTIFD_MAGIC);
181 msg.version = cpu_to_be32(MULTIFD_VERSION);
182 msg.id = p->id;
183 memcpy(msg.uuid, &qemu_uuid.data, sizeof(msg.uuid));
185 ret = qio_channel_write_all(p->c, (char *)&msg, sizeof(msg), errp);
186 if (ret != 0) {
187 return -1;
189 return 0;
192 static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
194 MultiFDInit_t msg;
195 int ret;
197 ret = qio_channel_read_all(c, (char *)&msg, sizeof(msg), errp);
198 if (ret != 0) {
199 return -1;
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);
208 return -1;
211 if (msg.version != MULTIFD_VERSION) {
212 error_setg(errp, "multifd: received packet version %d "
213 "expected %d", msg.version, MULTIFD_VERSION);
214 return -1;
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);
223 g_free(uuid);
224 g_free(msg_uuid);
225 return -1;
228 if (msg.id > migrate_multifd_channels()) {
229 error_setg(errp, "multifd: received channel version %d "
230 "expected %d", msg.version, MULTIFD_VERSION);
231 return -1;
234 return msg.id;
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->iov = g_new0(struct iovec, size);
243 pages->offset = g_new0(ram_addr_t, size);
245 return pages;
248 static void multifd_pages_clear(MultiFDPages_t *pages)
250 pages->used = 0;
251 pages->allocated = 0;
252 pages->packet_num = 0;
253 pages->block = NULL;
254 g_free(pages->iov);
255 pages->iov = NULL;
256 g_free(pages->offset);
257 pages->offset = NULL;
258 g_free(pages);
261 static void multifd_send_fill_packet(MultiFDSendParams *p)
263 MultiFDPacket_t *packet = p->packet;
264 int i;
266 packet->flags = cpu_to_be32(p->flags);
267 packet->pages_alloc = cpu_to_be32(p->pages->allocated);
268 packet->pages_used = cpu_to_be32(p->pages->used);
269 packet->next_packet_size = cpu_to_be32(p->next_packet_size);
270 packet->packet_num = cpu_to_be64(p->packet_num);
272 if (p->pages->block) {
273 strncpy(packet->ramblock, p->pages->block->idstr, 256);
276 for (i = 0; i < p->pages->used; i++) {
277 /* there are architectures where ram_addr_t is 32 bit */
278 uint64_t temp = p->pages->offset[i];
280 packet->offset[i] = cpu_to_be64(temp);
284 static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)
286 MultiFDPacket_t *packet = p->packet;
287 uint32_t pages_max = MULTIFD_PACKET_SIZE / qemu_target_page_size();
288 RAMBlock *block;
289 int i;
291 packet->magic = be32_to_cpu(packet->magic);
292 if (packet->magic != MULTIFD_MAGIC) {
293 error_setg(errp, "multifd: received packet "
294 "magic %x and expected magic %x",
295 packet->magic, MULTIFD_MAGIC);
296 return -1;
299 packet->version = be32_to_cpu(packet->version);
300 if (packet->version != MULTIFD_VERSION) {
301 error_setg(errp, "multifd: received packet "
302 "version %d and expected version %d",
303 packet->version, MULTIFD_VERSION);
304 return -1;
307 p->flags = be32_to_cpu(packet->flags);
309 packet->pages_alloc = be32_to_cpu(packet->pages_alloc);
311 * If we received a packet that is 100 times bigger than expected
312 * just stop migration. It is a magic number.
314 if (packet->pages_alloc > pages_max * 100) {
315 error_setg(errp, "multifd: received packet "
316 "with size %d and expected a maximum size of %d",
317 packet->pages_alloc, pages_max * 100) ;
318 return -1;
321 * We received a packet that is bigger than expected but inside
322 * reasonable limits (see previous comment). Just reallocate.
324 if (packet->pages_alloc > p->pages->allocated) {
325 multifd_pages_clear(p->pages);
326 p->pages = multifd_pages_init(packet->pages_alloc);
329 p->pages->used = be32_to_cpu(packet->pages_used);
330 if (p->pages->used > packet->pages_alloc) {
331 error_setg(errp, "multifd: received packet "
332 "with %d pages and expected maximum pages are %d",
333 p->pages->used, packet->pages_alloc) ;
334 return -1;
337 p->next_packet_size = be32_to_cpu(packet->next_packet_size);
338 p->packet_num = be64_to_cpu(packet->packet_num);
340 if (p->pages->used == 0) {
341 return 0;
344 /* make sure that ramblock is 0 terminated */
345 packet->ramblock[255] = 0;
346 block = qemu_ram_block_by_name(packet->ramblock);
347 if (!block) {
348 error_setg(errp, "multifd: unknown ram block %s",
349 packet->ramblock);
350 return -1;
353 for (i = 0; i < p->pages->used; i++) {
354 uint64_t offset = be64_to_cpu(packet->offset[i]);
356 if (offset > (block->used_length - qemu_target_page_size())) {
357 error_setg(errp, "multifd: offset too long %" PRIu64
358 " (max " RAM_ADDR_FMT ")",
359 offset, block->max_length);
360 return -1;
362 p->pages->iov[i].iov_base = block->host + offset;
363 p->pages->iov[i].iov_len = qemu_target_page_size();
366 return 0;
369 struct {
370 MultiFDSendParams *params;
371 /* array of pages to sent */
372 MultiFDPages_t *pages;
373 /* global number of generated multifd packets */
374 uint64_t packet_num;
375 /* send channels ready */
376 QemuSemaphore channels_ready;
378 * Have we already run terminate threads. There is a race when it
379 * happens that we got one error while we are exiting.
380 * We will use atomic operations. Only valid values are 0 and 1.
382 int exiting;
383 /* multifd ops */
384 MultiFDMethods *ops;
385 } *multifd_send_state;
388 * How we use multifd_send_state->pages and channel->pages?
390 * We create a pages for each channel, and a main one. Each time that
391 * we need to send a batch of pages we interchange the ones between
392 * multifd_send_state and the channel that is sending it. There are
393 * two reasons for that:
394 * - to not have to do so many mallocs during migration
395 * - to make easier to know what to free at the end of migration
397 * This way we always know who is the owner of each "pages" struct,
398 * and we don't need any locking. It belongs to the migration thread
399 * or to the channel thread. Switching is safe because the migration
400 * thread is using the channel mutex when changing it, and the channel
401 * have to had finish with its own, otherwise pending_job can't be
402 * false.
405 static int multifd_send_pages(QEMUFile *f)
407 int i;
408 static int next_channel;
409 MultiFDSendParams *p = NULL; /* make happy gcc */
410 MultiFDPages_t *pages = multifd_send_state->pages;
411 uint64_t transferred;
413 if (atomic_read(&multifd_send_state->exiting)) {
414 return -1;
417 qemu_sem_wait(&multifd_send_state->channels_ready);
418 for (i = next_channel;; i = (i + 1) % migrate_multifd_channels()) {
419 p = &multifd_send_state->params[i];
421 qemu_mutex_lock(&p->mutex);
422 if (p->quit) {
423 error_report("%s: channel %d has already quit!", __func__, i);
424 qemu_mutex_unlock(&p->mutex);
425 return -1;
427 if (!p->pending_job) {
428 p->pending_job++;
429 next_channel = (i + 1) % migrate_multifd_channels();
430 break;
432 qemu_mutex_unlock(&p->mutex);
434 assert(!p->pages->used);
435 assert(!p->pages->block);
437 p->packet_num = multifd_send_state->packet_num++;
438 multifd_send_state->pages = p->pages;
439 p->pages = pages;
440 transferred = ((uint64_t) pages->used) * qemu_target_page_size()
441 + p->packet_len;
442 qemu_file_update_transfer(f, transferred);
443 ram_counters.multifd_bytes += transferred;
444 ram_counters.transferred += transferred;;
445 qemu_mutex_unlock(&p->mutex);
446 qemu_sem_post(&p->sem);
448 return 1;
451 int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset)
453 MultiFDPages_t *pages = multifd_send_state->pages;
455 if (!pages->block) {
456 pages->block = block;
459 if (pages->block == block) {
460 pages->offset[pages->used] = offset;
461 pages->iov[pages->used].iov_base = block->host + offset;
462 pages->iov[pages->used].iov_len = qemu_target_page_size();
463 pages->used++;
465 if (pages->used < pages->allocated) {
466 return 1;
470 if (multifd_send_pages(f) < 0) {
471 return -1;
474 if (pages->block != block) {
475 return multifd_queue_page(f, block, offset);
478 return 1;
481 static void multifd_send_terminate_threads(Error *err)
483 int i;
485 trace_multifd_send_terminate_threads(err != NULL);
487 if (err) {
488 MigrationState *s = migrate_get_current();
489 migrate_set_error(s, err);
490 if (s->state == MIGRATION_STATUS_SETUP ||
491 s->state == MIGRATION_STATUS_PRE_SWITCHOVER ||
492 s->state == MIGRATION_STATUS_DEVICE ||
493 s->state == MIGRATION_STATUS_ACTIVE) {
494 migrate_set_state(&s->state, s->state,
495 MIGRATION_STATUS_FAILED);
500 * We don't want to exit each threads twice. Depending on where
501 * we get the error, or if there are two independent errors in two
502 * threads at the same time, we can end calling this function
503 * twice.
505 if (atomic_xchg(&multifd_send_state->exiting, 1)) {
506 return;
509 for (i = 0; i < migrate_multifd_channels(); i++) {
510 MultiFDSendParams *p = &multifd_send_state->params[i];
512 qemu_mutex_lock(&p->mutex);
513 p->quit = true;
514 qemu_sem_post(&p->sem);
515 qemu_mutex_unlock(&p->mutex);
519 void multifd_save_cleanup(void)
521 int i;
523 if (!migrate_use_multifd()) {
524 return;
526 multifd_send_terminate_threads(NULL);
527 for (i = 0; i < migrate_multifd_channels(); i++) {
528 MultiFDSendParams *p = &multifd_send_state->params[i];
530 if (p->running) {
531 qemu_thread_join(&p->thread);
534 for (i = 0; i < migrate_multifd_channels(); i++) {
535 MultiFDSendParams *p = &multifd_send_state->params[i];
536 Error *local_err = NULL;
538 socket_send_channel_destroy(p->c);
539 p->c = NULL;
540 qemu_mutex_destroy(&p->mutex);
541 qemu_sem_destroy(&p->sem);
542 qemu_sem_destroy(&p->sem_sync);
543 g_free(p->name);
544 p->name = NULL;
545 multifd_pages_clear(p->pages);
546 p->pages = NULL;
547 p->packet_len = 0;
548 g_free(p->packet);
549 p->packet = NULL;
550 multifd_send_state->ops->send_cleanup(p, &local_err);
551 if (local_err) {
552 migrate_set_error(migrate_get_current(), local_err);
555 qemu_sem_destroy(&multifd_send_state->channels_ready);
556 g_free(multifd_send_state->params);
557 multifd_send_state->params = NULL;
558 multifd_pages_clear(multifd_send_state->pages);
559 multifd_send_state->pages = NULL;
560 g_free(multifd_send_state);
561 multifd_send_state = NULL;
564 void multifd_send_sync_main(QEMUFile *f)
566 int i;
568 if (!migrate_use_multifd()) {
569 return;
571 if (multifd_send_state->pages->used) {
572 if (multifd_send_pages(f) < 0) {
573 error_report("%s: multifd_send_pages fail", __func__);
574 return;
577 for (i = 0; i < migrate_multifd_channels(); i++) {
578 MultiFDSendParams *p = &multifd_send_state->params[i];
580 trace_multifd_send_sync_main_signal(p->id);
582 qemu_mutex_lock(&p->mutex);
584 if (p->quit) {
585 error_report("%s: channel %d has already quit", __func__, i);
586 qemu_mutex_unlock(&p->mutex);
587 return;
590 p->packet_num = multifd_send_state->packet_num++;
591 p->flags |= MULTIFD_FLAG_SYNC;
592 p->pending_job++;
593 qemu_file_update_transfer(f, p->packet_len);
594 ram_counters.multifd_bytes += p->packet_len;
595 ram_counters.transferred += p->packet_len;
596 qemu_mutex_unlock(&p->mutex);
597 qemu_sem_post(&p->sem);
599 for (i = 0; i < migrate_multifd_channels(); i++) {
600 MultiFDSendParams *p = &multifd_send_state->params[i];
602 trace_multifd_send_sync_main_wait(p->id);
603 qemu_sem_wait(&p->sem_sync);
605 trace_multifd_send_sync_main(multifd_send_state->packet_num);
608 static void *multifd_send_thread(void *opaque)
610 MultiFDSendParams *p = opaque;
611 Error *local_err = NULL;
612 int ret = 0;
613 uint32_t flags = 0;
615 trace_multifd_send_thread_start(p->id);
616 rcu_register_thread();
618 if (multifd_send_initial_packet(p, &local_err) < 0) {
619 ret = -1;
620 goto out;
622 /* initial packet */
623 p->num_packets = 1;
625 while (true) {
626 qemu_sem_wait(&p->sem);
628 if (atomic_read(&multifd_send_state->exiting)) {
629 break;
631 qemu_mutex_lock(&p->mutex);
633 if (p->pending_job) {
634 uint32_t used = p->pages->used;
635 uint64_t packet_num = p->packet_num;
636 flags = p->flags;
638 if (used) {
639 ret = multifd_send_state->ops->send_prepare(p, used,
640 &local_err);
641 if (ret != 0) {
642 qemu_mutex_unlock(&p->mutex);
643 break;
646 multifd_send_fill_packet(p);
647 p->flags = 0;
648 p->num_packets++;
649 p->num_pages += used;
650 p->pages->used = 0;
651 p->pages->block = NULL;
652 qemu_mutex_unlock(&p->mutex);
654 trace_multifd_send(p->id, packet_num, used, flags,
655 p->next_packet_size);
657 ret = qio_channel_write_all(p->c, (void *)p->packet,
658 p->packet_len, &local_err);
659 if (ret != 0) {
660 break;
663 if (used) {
664 ret = multifd_send_state->ops->send_write(p, used, &local_err);
665 if (ret != 0) {
666 break;
670 qemu_mutex_lock(&p->mutex);
671 p->pending_job--;
672 qemu_mutex_unlock(&p->mutex);
674 if (flags & MULTIFD_FLAG_SYNC) {
675 qemu_sem_post(&p->sem_sync);
677 qemu_sem_post(&multifd_send_state->channels_ready);
678 } else if (p->quit) {
679 qemu_mutex_unlock(&p->mutex);
680 break;
681 } else {
682 qemu_mutex_unlock(&p->mutex);
683 /* sometimes there are spurious wakeups */
687 out:
688 if (local_err) {
689 trace_multifd_send_error(p->id);
690 multifd_send_terminate_threads(local_err);
694 * Error happen, I will exit, but I can't just leave, tell
695 * who pay attention to me.
697 if (ret != 0) {
698 qemu_sem_post(&p->sem_sync);
699 qemu_sem_post(&multifd_send_state->channels_ready);
702 qemu_mutex_lock(&p->mutex);
703 p->running = false;
704 qemu_mutex_unlock(&p->mutex);
706 rcu_unregister_thread();
707 trace_multifd_send_thread_end(p->id, p->num_packets, p->num_pages);
709 return NULL;
712 static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque)
714 MultiFDSendParams *p = opaque;
715 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
716 Error *local_err = NULL;
718 trace_multifd_new_send_channel_async(p->id);
719 if (qio_task_propagate_error(task, &local_err)) {
720 migrate_set_error(migrate_get_current(), local_err);
721 /* Error happen, we need to tell who pay attention to me */
722 qemu_sem_post(&multifd_send_state->channels_ready);
723 qemu_sem_post(&p->sem_sync);
725 * Although multifd_send_thread is not created, but main migration
726 * thread neet to judge whether it is running, so we need to mark
727 * its status.
729 p->quit = true;
730 } else {
731 p->c = QIO_CHANNEL(sioc);
732 qio_channel_set_delay(p->c, false);
733 p->running = true;
734 qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
735 QEMU_THREAD_JOINABLE);
739 int multifd_save_setup(Error **errp)
741 int thread_count;
742 uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
743 uint8_t i;
745 if (!migrate_use_multifd()) {
746 return 0;
748 thread_count = migrate_multifd_channels();
749 multifd_send_state = g_malloc0(sizeof(*multifd_send_state));
750 multifd_send_state->params = g_new0(MultiFDSendParams, thread_count);
751 multifd_send_state->pages = multifd_pages_init(page_count);
752 qemu_sem_init(&multifd_send_state->channels_ready, 0);
753 atomic_set(&multifd_send_state->exiting, 0);
754 multifd_send_state->ops = multifd_ops[migrate_multifd_compression()];
756 for (i = 0; i < thread_count; i++) {
757 MultiFDSendParams *p = &multifd_send_state->params[i];
759 qemu_mutex_init(&p->mutex);
760 qemu_sem_init(&p->sem, 0);
761 qemu_sem_init(&p->sem_sync, 0);
762 p->quit = false;
763 p->pending_job = 0;
764 p->id = i;
765 p->pages = multifd_pages_init(page_count);
766 p->packet_len = sizeof(MultiFDPacket_t)
767 + sizeof(uint64_t) * page_count;
768 p->packet = g_malloc0(p->packet_len);
769 p->packet->magic = cpu_to_be32(MULTIFD_MAGIC);
770 p->packet->version = cpu_to_be32(MULTIFD_VERSION);
771 p->name = g_strdup_printf("multifdsend_%d", i);
772 socket_send_channel_create(multifd_new_send_channel_async, p);
775 for (i = 0; i < thread_count; i++) {
776 MultiFDSendParams *p = &multifd_send_state->params[i];
777 Error *local_err = NULL;
778 int ret;
780 ret = multifd_send_state->ops->send_setup(p, &local_err);
781 if (ret) {
782 error_propagate(errp, local_err);
783 return ret;
786 return 0;
789 struct {
790 MultiFDRecvParams *params;
791 /* number of created threads */
792 int count;
793 /* syncs main thread and channels */
794 QemuSemaphore sem_sync;
795 /* global number of generated multifd packets */
796 uint64_t packet_num;
797 /* multifd ops */
798 MultiFDMethods *ops;
799 } *multifd_recv_state;
801 static void multifd_recv_terminate_threads(Error *err)
803 int i;
805 trace_multifd_recv_terminate_threads(err != NULL);
807 if (err) {
808 MigrationState *s = migrate_get_current();
809 migrate_set_error(s, err);
810 if (s->state == MIGRATION_STATUS_SETUP ||
811 s->state == MIGRATION_STATUS_ACTIVE) {
812 migrate_set_state(&s->state, s->state,
813 MIGRATION_STATUS_FAILED);
817 for (i = 0; i < migrate_multifd_channels(); i++) {
818 MultiFDRecvParams *p = &multifd_recv_state->params[i];
820 qemu_mutex_lock(&p->mutex);
821 p->quit = true;
823 * We could arrive here for two reasons:
824 * - normal quit, i.e. everything went fine, just finished
825 * - error quit: We close the channels so the channel threads
826 * finish the qio_channel_read_all_eof()
828 if (p->c) {
829 qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
831 qemu_mutex_unlock(&p->mutex);
835 int multifd_load_cleanup(Error **errp)
837 int i;
839 if (!migrate_use_multifd()) {
840 return 0;
842 multifd_recv_terminate_threads(NULL);
843 for (i = 0; i < migrate_multifd_channels(); i++) {
844 MultiFDRecvParams *p = &multifd_recv_state->params[i];
846 if (p->running) {
847 p->quit = true;
849 * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
850 * however try to wakeup it without harm in cleanup phase.
852 qemu_sem_post(&p->sem_sync);
853 qemu_thread_join(&p->thread);
856 for (i = 0; i < migrate_multifd_channels(); i++) {
857 MultiFDRecvParams *p = &multifd_recv_state->params[i];
859 object_unref(OBJECT(p->c));
860 p->c = NULL;
861 qemu_mutex_destroy(&p->mutex);
862 qemu_sem_destroy(&p->sem_sync);
863 g_free(p->name);
864 p->name = NULL;
865 multifd_pages_clear(p->pages);
866 p->pages = NULL;
867 p->packet_len = 0;
868 g_free(p->packet);
869 p->packet = NULL;
870 multifd_recv_state->ops->recv_cleanup(p);
872 qemu_sem_destroy(&multifd_recv_state->sem_sync);
873 g_free(multifd_recv_state->params);
874 multifd_recv_state->params = NULL;
875 g_free(multifd_recv_state);
876 multifd_recv_state = NULL;
878 return 0;
881 void multifd_recv_sync_main(void)
883 int i;
885 if (!migrate_use_multifd()) {
886 return;
888 for (i = 0; i < migrate_multifd_channels(); i++) {
889 MultiFDRecvParams *p = &multifd_recv_state->params[i];
891 trace_multifd_recv_sync_main_wait(p->id);
892 qemu_sem_wait(&multifd_recv_state->sem_sync);
894 for (i = 0; i < migrate_multifd_channels(); i++) {
895 MultiFDRecvParams *p = &multifd_recv_state->params[i];
897 qemu_mutex_lock(&p->mutex);
898 if (multifd_recv_state->packet_num < p->packet_num) {
899 multifd_recv_state->packet_num = p->packet_num;
901 qemu_mutex_unlock(&p->mutex);
902 trace_multifd_recv_sync_main_signal(p->id);
903 qemu_sem_post(&p->sem_sync);
905 trace_multifd_recv_sync_main(multifd_recv_state->packet_num);
908 static void *multifd_recv_thread(void *opaque)
910 MultiFDRecvParams *p = opaque;
911 Error *local_err = NULL;
912 int ret;
914 trace_multifd_recv_thread_start(p->id);
915 rcu_register_thread();
917 while (true) {
918 uint32_t used;
919 uint32_t flags;
921 if (p->quit) {
922 break;
925 ret = qio_channel_read_all_eof(p->c, (void *)p->packet,
926 p->packet_len, &local_err);
927 if (ret == 0) { /* EOF */
928 break;
930 if (ret == -1) { /* Error */
931 break;
934 qemu_mutex_lock(&p->mutex);
935 ret = multifd_recv_unfill_packet(p, &local_err);
936 if (ret) {
937 qemu_mutex_unlock(&p->mutex);
938 break;
941 used = p->pages->used;
942 flags = p->flags;
943 /* recv methods don't know how to handle the SYNC flag */
944 p->flags &= ~MULTIFD_FLAG_SYNC;
945 trace_multifd_recv(p->id, p->packet_num, used, flags,
946 p->next_packet_size);
947 p->num_packets++;
948 p->num_pages += used;
949 qemu_mutex_unlock(&p->mutex);
951 if (used) {
952 ret = multifd_recv_state->ops->recv_pages(p, used, &local_err);
953 if (ret != 0) {
954 break;
958 if (flags & MULTIFD_FLAG_SYNC) {
959 qemu_sem_post(&multifd_recv_state->sem_sync);
960 qemu_sem_wait(&p->sem_sync);
964 if (local_err) {
965 multifd_recv_terminate_threads(local_err);
967 qemu_mutex_lock(&p->mutex);
968 p->running = false;
969 qemu_mutex_unlock(&p->mutex);
971 rcu_unregister_thread();
972 trace_multifd_recv_thread_end(p->id, p->num_packets, p->num_pages);
974 return NULL;
977 int multifd_load_setup(Error **errp)
979 int thread_count;
980 uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
981 uint8_t i;
983 if (!migrate_use_multifd()) {
984 return 0;
986 thread_count = migrate_multifd_channels();
987 multifd_recv_state = g_malloc0(sizeof(*multifd_recv_state));
988 multifd_recv_state->params = g_new0(MultiFDRecvParams, thread_count);
989 atomic_set(&multifd_recv_state->count, 0);
990 qemu_sem_init(&multifd_recv_state->sem_sync, 0);
991 multifd_recv_state->ops = multifd_ops[migrate_multifd_compression()];
993 for (i = 0; i < thread_count; i++) {
994 MultiFDRecvParams *p = &multifd_recv_state->params[i];
996 qemu_mutex_init(&p->mutex);
997 qemu_sem_init(&p->sem_sync, 0);
998 p->quit = false;
999 p->id = i;
1000 p->pages = multifd_pages_init(page_count);
1001 p->packet_len = sizeof(MultiFDPacket_t)
1002 + sizeof(uint64_t) * page_count;
1003 p->packet = g_malloc0(p->packet_len);
1004 p->name = g_strdup_printf("multifdrecv_%d", i);
1007 for (i = 0; i < thread_count; i++) {
1008 MultiFDRecvParams *p = &multifd_recv_state->params[i];
1009 Error *local_err = NULL;
1010 int ret;
1012 ret = multifd_recv_state->ops->recv_setup(p, &local_err);
1013 if (ret) {
1014 error_propagate(errp, local_err);
1015 return ret;
1018 return 0;
1021 bool multifd_recv_all_channels_created(void)
1023 int thread_count = migrate_multifd_channels();
1025 if (!migrate_use_multifd()) {
1026 return true;
1029 return thread_count == atomic_read(&multifd_recv_state->count);
1033 * Try to receive all multifd channels to get ready for the migration.
1034 * - Return true and do not set @errp when correctly receving all channels;
1035 * - Return false and do not set @errp when correctly receiving the current one;
1036 * - Return false and set @errp when failing to receive the current channel.
1038 bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp)
1040 MultiFDRecvParams *p;
1041 Error *local_err = NULL;
1042 int id;
1044 id = multifd_recv_initial_packet(ioc, &local_err);
1045 if (id < 0) {
1046 multifd_recv_terminate_threads(local_err);
1047 error_propagate_prepend(errp, local_err,
1048 "failed to receive packet"
1049 " via multifd channel %d: ",
1050 atomic_read(&multifd_recv_state->count));
1051 return false;
1053 trace_multifd_recv_new_channel(id);
1055 p = &multifd_recv_state->params[id];
1056 if (p->c != NULL) {
1057 error_setg(&local_err, "multifd: received id '%d' already setup'",
1058 id);
1059 multifd_recv_terminate_threads(local_err);
1060 error_propagate(errp, local_err);
1061 return false;
1063 p->c = ioc;
1064 object_ref(OBJECT(ioc));
1065 /* initial packet */
1066 p->num_packets = 1;
1068 p->running = true;
1069 qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p,
1070 QEMU_THREAD_JOINABLE);
1071 atomic_inc(&multifd_recv_state->count);
1072 return atomic_read(&multifd_recv_state->count) ==
1073 migrate_multifd_channels();