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"
23 #include "qemu-file.h"
29 #define MULTIFD_MAGIC 0x11223344U
30 #define MULTIFD_VERSION 1
35 unsigned char uuid
[16]; /* QemuUUID */
37 uint8_t unused1
[7]; /* Reserved for future use */
38 uint64_t unused2
[4]; /* Reserved for future use */
39 } __attribute__((packed
)) MultiFDInit_t
;
41 static int multifd_send_initial_packet(MultiFDSendParams
*p
, Error
**errp
)
43 MultiFDInit_t msg
= {};
46 msg
.magic
= cpu_to_be32(MULTIFD_MAGIC
);
47 msg
.version
= cpu_to_be32(MULTIFD_VERSION
);
49 memcpy(msg
.uuid
, &qemu_uuid
.data
, sizeof(msg
.uuid
));
51 ret
= qio_channel_write_all(p
->c
, (char *)&msg
, sizeof(msg
), errp
);
58 static int multifd_recv_initial_packet(QIOChannel
*c
, Error
**errp
)
63 ret
= qio_channel_read_all(c
, (char *)&msg
, sizeof(msg
), errp
);
68 msg
.magic
= be32_to_cpu(msg
.magic
);
69 msg
.version
= be32_to_cpu(msg
.version
);
71 if (msg
.magic
!= MULTIFD_MAGIC
) {
72 error_setg(errp
, "multifd: received packet magic %x "
73 "expected %x", msg
.magic
, MULTIFD_MAGIC
);
77 if (msg
.version
!= MULTIFD_VERSION
) {
78 error_setg(errp
, "multifd: received packet version %d "
79 "expected %d", msg
.version
, MULTIFD_VERSION
);
83 if (memcmp(msg
.uuid
, &qemu_uuid
, sizeof(qemu_uuid
))) {
84 char *uuid
= qemu_uuid_unparse_strdup(&qemu_uuid
);
85 char *msg_uuid
= qemu_uuid_unparse_strdup((const QemuUUID
*)msg
.uuid
);
87 error_setg(errp
, "multifd: received uuid '%s' and expected "
88 "uuid '%s' for channel %hhd", msg_uuid
, uuid
, msg
.id
);
94 if (msg
.id
> migrate_multifd_channels()) {
95 error_setg(errp
, "multifd: received channel version %d "
96 "expected %d", msg
.version
, MULTIFD_VERSION
);
103 static MultiFDPages_t
*multifd_pages_init(size_t size
)
105 MultiFDPages_t
*pages
= g_new0(MultiFDPages_t
, 1);
107 pages
->allocated
= size
;
108 pages
->iov
= g_new0(struct iovec
, size
);
109 pages
->offset
= g_new0(ram_addr_t
, size
);
114 static void multifd_pages_clear(MultiFDPages_t
*pages
)
117 pages
->allocated
= 0;
118 pages
->packet_num
= 0;
122 g_free(pages
->offset
);
123 pages
->offset
= NULL
;
127 static void multifd_send_fill_packet(MultiFDSendParams
*p
)
129 MultiFDPacket_t
*packet
= p
->packet
;
132 packet
->flags
= cpu_to_be32(p
->flags
);
133 packet
->pages_alloc
= cpu_to_be32(p
->pages
->allocated
);
134 packet
->pages_used
= cpu_to_be32(p
->pages
->used
);
135 packet
->next_packet_size
= cpu_to_be32(p
->next_packet_size
);
136 packet
->packet_num
= cpu_to_be64(p
->packet_num
);
138 if (p
->pages
->block
) {
139 strncpy(packet
->ramblock
, p
->pages
->block
->idstr
, 256);
142 for (i
= 0; i
< p
->pages
->used
; i
++) {
143 /* there are architectures where ram_addr_t is 32 bit */
144 uint64_t temp
= p
->pages
->offset
[i
];
146 packet
->offset
[i
] = cpu_to_be64(temp
);
150 static int multifd_recv_unfill_packet(MultiFDRecvParams
*p
, Error
**errp
)
152 MultiFDPacket_t
*packet
= p
->packet
;
153 uint32_t pages_max
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
157 packet
->magic
= be32_to_cpu(packet
->magic
);
158 if (packet
->magic
!= MULTIFD_MAGIC
) {
159 error_setg(errp
, "multifd: received packet "
160 "magic %x and expected magic %x",
161 packet
->magic
, MULTIFD_MAGIC
);
165 packet
->version
= be32_to_cpu(packet
->version
);
166 if (packet
->version
!= MULTIFD_VERSION
) {
167 error_setg(errp
, "multifd: received packet "
168 "version %d and expected version %d",
169 packet
->version
, MULTIFD_VERSION
);
173 p
->flags
= be32_to_cpu(packet
->flags
);
175 packet
->pages_alloc
= be32_to_cpu(packet
->pages_alloc
);
177 * If we received a packet that is 100 times bigger than expected
178 * just stop migration. It is a magic number.
180 if (packet
->pages_alloc
> pages_max
* 100) {
181 error_setg(errp
, "multifd: received packet "
182 "with size %d and expected a maximum size of %d",
183 packet
->pages_alloc
, pages_max
* 100) ;
187 * We received a packet that is bigger than expected but inside
188 * reasonable limits (see previous comment). Just reallocate.
190 if (packet
->pages_alloc
> p
->pages
->allocated
) {
191 multifd_pages_clear(p
->pages
);
192 p
->pages
= multifd_pages_init(packet
->pages_alloc
);
195 p
->pages
->used
= be32_to_cpu(packet
->pages_used
);
196 if (p
->pages
->used
> packet
->pages_alloc
) {
197 error_setg(errp
, "multifd: received packet "
198 "with %d pages and expected maximum pages are %d",
199 p
->pages
->used
, packet
->pages_alloc
) ;
203 p
->next_packet_size
= be32_to_cpu(packet
->next_packet_size
);
204 p
->packet_num
= be64_to_cpu(packet
->packet_num
);
206 if (p
->pages
->used
== 0) {
210 /* make sure that ramblock is 0 terminated */
211 packet
->ramblock
[255] = 0;
212 block
= qemu_ram_block_by_name(packet
->ramblock
);
214 error_setg(errp
, "multifd: unknown ram block %s",
219 for (i
= 0; i
< p
->pages
->used
; i
++) {
220 uint64_t offset
= be64_to_cpu(packet
->offset
[i
]);
222 if (offset
> (block
->used_length
- qemu_target_page_size())) {
223 error_setg(errp
, "multifd: offset too long %" PRIu64
224 " (max " RAM_ADDR_FMT
")",
225 offset
, block
->max_length
);
228 p
->pages
->iov
[i
].iov_base
= block
->host
+ offset
;
229 p
->pages
->iov
[i
].iov_len
= qemu_target_page_size();
236 MultiFDSendParams
*params
;
237 /* array of pages to sent */
238 MultiFDPages_t
*pages
;
239 /* global number of generated multifd packets */
241 /* send channels ready */
242 QemuSemaphore channels_ready
;
244 * Have we already run terminate threads. There is a race when it
245 * happens that we got one error while we are exiting.
246 * We will use atomic operations. Only valid values are 0 and 1.
249 } *multifd_send_state
;
252 * How we use multifd_send_state->pages and channel->pages?
254 * We create a pages for each channel, and a main one. Each time that
255 * we need to send a batch of pages we interchange the ones between
256 * multifd_send_state and the channel that is sending it. There are
257 * two reasons for that:
258 * - to not have to do so many mallocs during migration
259 * - to make easier to know what to free at the end of migration
261 * This way we always know who is the owner of each "pages" struct,
262 * and we don't need any locking. It belongs to the migration thread
263 * or to the channel thread. Switching is safe because the migration
264 * thread is using the channel mutex when changing it, and the channel
265 * have to had finish with its own, otherwise pending_job can't be
269 static int multifd_send_pages(QEMUFile
*f
)
272 static int next_channel
;
273 MultiFDSendParams
*p
= NULL
; /* make happy gcc */
274 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
275 uint64_t transferred
;
277 if (atomic_read(&multifd_send_state
->exiting
)) {
281 qemu_sem_wait(&multifd_send_state
->channels_ready
);
282 for (i
= next_channel
;; i
= (i
+ 1) % migrate_multifd_channels()) {
283 p
= &multifd_send_state
->params
[i
];
285 qemu_mutex_lock(&p
->mutex
);
287 error_report("%s: channel %d has already quit!", __func__
, i
);
288 qemu_mutex_unlock(&p
->mutex
);
291 if (!p
->pending_job
) {
293 next_channel
= (i
+ 1) % migrate_multifd_channels();
296 qemu_mutex_unlock(&p
->mutex
);
298 assert(!p
->pages
->used
);
299 assert(!p
->pages
->block
);
301 p
->packet_num
= multifd_send_state
->packet_num
++;
302 multifd_send_state
->pages
= p
->pages
;
304 transferred
= ((uint64_t) pages
->used
) * qemu_target_page_size()
306 qemu_file_update_transfer(f
, transferred
);
307 ram_counters
.multifd_bytes
+= transferred
;
308 ram_counters
.transferred
+= transferred
;;
309 qemu_mutex_unlock(&p
->mutex
);
310 qemu_sem_post(&p
->sem
);
315 int multifd_queue_page(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
)
317 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
320 pages
->block
= block
;
323 if (pages
->block
== block
) {
324 pages
->offset
[pages
->used
] = offset
;
325 pages
->iov
[pages
->used
].iov_base
= block
->host
+ offset
;
326 pages
->iov
[pages
->used
].iov_len
= qemu_target_page_size();
329 if (pages
->used
< pages
->allocated
) {
334 if (multifd_send_pages(f
) < 0) {
338 if (pages
->block
!= block
) {
339 return multifd_queue_page(f
, block
, offset
);
345 static void multifd_send_terminate_threads(Error
*err
)
349 trace_multifd_send_terminate_threads(err
!= NULL
);
352 MigrationState
*s
= migrate_get_current();
353 migrate_set_error(s
, err
);
354 if (s
->state
== MIGRATION_STATUS_SETUP
||
355 s
->state
== MIGRATION_STATUS_PRE_SWITCHOVER
||
356 s
->state
== MIGRATION_STATUS_DEVICE
||
357 s
->state
== MIGRATION_STATUS_ACTIVE
) {
358 migrate_set_state(&s
->state
, s
->state
,
359 MIGRATION_STATUS_FAILED
);
364 * We don't want to exit each threads twice. Depending on where
365 * we get the error, or if there are two independent errors in two
366 * threads at the same time, we can end calling this function
369 if (atomic_xchg(&multifd_send_state
->exiting
, 1)) {
373 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
374 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
376 qemu_mutex_lock(&p
->mutex
);
378 qemu_sem_post(&p
->sem
);
379 qemu_mutex_unlock(&p
->mutex
);
383 void multifd_save_cleanup(void)
387 if (!migrate_use_multifd()) {
390 multifd_send_terminate_threads(NULL
);
391 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
392 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
395 qemu_thread_join(&p
->thread
);
398 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
399 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
401 socket_send_channel_destroy(p
->c
);
403 qemu_mutex_destroy(&p
->mutex
);
404 qemu_sem_destroy(&p
->sem
);
405 qemu_sem_destroy(&p
->sem_sync
);
408 multifd_pages_clear(p
->pages
);
414 qemu_sem_destroy(&multifd_send_state
->channels_ready
);
415 g_free(multifd_send_state
->params
);
416 multifd_send_state
->params
= NULL
;
417 multifd_pages_clear(multifd_send_state
->pages
);
418 multifd_send_state
->pages
= NULL
;
419 g_free(multifd_send_state
);
420 multifd_send_state
= NULL
;
423 void multifd_send_sync_main(QEMUFile
*f
)
427 if (!migrate_use_multifd()) {
430 if (multifd_send_state
->pages
->used
) {
431 if (multifd_send_pages(f
) < 0) {
432 error_report("%s: multifd_send_pages fail", __func__
);
436 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
437 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
439 trace_multifd_send_sync_main_signal(p
->id
);
441 qemu_mutex_lock(&p
->mutex
);
444 error_report("%s: channel %d has already quit", __func__
, i
);
445 qemu_mutex_unlock(&p
->mutex
);
449 p
->packet_num
= multifd_send_state
->packet_num
++;
450 p
->flags
|= MULTIFD_FLAG_SYNC
;
452 qemu_file_update_transfer(f
, p
->packet_len
);
453 ram_counters
.multifd_bytes
+= p
->packet_len
;
454 ram_counters
.transferred
+= p
->packet_len
;
455 qemu_mutex_unlock(&p
->mutex
);
456 qemu_sem_post(&p
->sem
);
458 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
459 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
461 trace_multifd_send_sync_main_wait(p
->id
);
462 qemu_sem_wait(&p
->sem_sync
);
464 trace_multifd_send_sync_main(multifd_send_state
->packet_num
);
467 static void *multifd_send_thread(void *opaque
)
469 MultiFDSendParams
*p
= opaque
;
470 Error
*local_err
= NULL
;
474 trace_multifd_send_thread_start(p
->id
);
475 rcu_register_thread();
477 if (multifd_send_initial_packet(p
, &local_err
) < 0) {
485 qemu_sem_wait(&p
->sem
);
487 if (atomic_read(&multifd_send_state
->exiting
)) {
490 qemu_mutex_lock(&p
->mutex
);
492 if (p
->pending_job
) {
493 uint32_t used
= p
->pages
->used
;
494 uint64_t packet_num
= p
->packet_num
;
497 p
->next_packet_size
= used
* qemu_target_page_size();
498 multifd_send_fill_packet(p
);
501 p
->num_pages
+= used
;
503 p
->pages
->block
= NULL
;
504 qemu_mutex_unlock(&p
->mutex
);
506 trace_multifd_send(p
->id
, packet_num
, used
, flags
,
507 p
->next_packet_size
);
509 ret
= qio_channel_write_all(p
->c
, (void *)p
->packet
,
510 p
->packet_len
, &local_err
);
516 ret
= qio_channel_writev_all(p
->c
, p
->pages
->iov
,
523 qemu_mutex_lock(&p
->mutex
);
525 qemu_mutex_unlock(&p
->mutex
);
527 if (flags
& MULTIFD_FLAG_SYNC
) {
528 qemu_sem_post(&p
->sem_sync
);
530 qemu_sem_post(&multifd_send_state
->channels_ready
);
531 } else if (p
->quit
) {
532 qemu_mutex_unlock(&p
->mutex
);
535 qemu_mutex_unlock(&p
->mutex
);
536 /* sometimes there are spurious wakeups */
542 trace_multifd_send_error(p
->id
);
543 multifd_send_terminate_threads(local_err
);
547 * Error happen, I will exit, but I can't just leave, tell
548 * who pay attention to me.
551 qemu_sem_post(&p
->sem_sync
);
552 qemu_sem_post(&multifd_send_state
->channels_ready
);
555 qemu_mutex_lock(&p
->mutex
);
557 qemu_mutex_unlock(&p
->mutex
);
559 rcu_unregister_thread();
560 trace_multifd_send_thread_end(p
->id
, p
->num_packets
, p
->num_pages
);
565 static void multifd_new_send_channel_async(QIOTask
*task
, gpointer opaque
)
567 MultiFDSendParams
*p
= opaque
;
568 QIOChannel
*sioc
= QIO_CHANNEL(qio_task_get_source(task
));
569 Error
*local_err
= NULL
;
571 trace_multifd_new_send_channel_async(p
->id
);
572 if (qio_task_propagate_error(task
, &local_err
)) {
573 migrate_set_error(migrate_get_current(), local_err
);
574 /* Error happen, we need to tell who pay attention to me */
575 qemu_sem_post(&multifd_send_state
->channels_ready
);
576 qemu_sem_post(&p
->sem_sync
);
578 * Although multifd_send_thread is not created, but main migration
579 * thread neet to judge whether it is running, so we need to mark
584 p
->c
= QIO_CHANNEL(sioc
);
585 qio_channel_set_delay(p
->c
, false);
587 qemu_thread_create(&p
->thread
, p
->name
, multifd_send_thread
, p
,
588 QEMU_THREAD_JOINABLE
);
592 int multifd_save_setup(Error
**errp
)
595 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
598 if (!migrate_use_multifd()) {
601 thread_count
= migrate_multifd_channels();
602 multifd_send_state
= g_malloc0(sizeof(*multifd_send_state
));
603 multifd_send_state
->params
= g_new0(MultiFDSendParams
, thread_count
);
604 multifd_send_state
->pages
= multifd_pages_init(page_count
);
605 qemu_sem_init(&multifd_send_state
->channels_ready
, 0);
606 atomic_set(&multifd_send_state
->exiting
, 0);
608 for (i
= 0; i
< thread_count
; i
++) {
609 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
611 qemu_mutex_init(&p
->mutex
);
612 qemu_sem_init(&p
->sem
, 0);
613 qemu_sem_init(&p
->sem_sync
, 0);
617 p
->pages
= multifd_pages_init(page_count
);
618 p
->packet_len
= sizeof(MultiFDPacket_t
)
619 + sizeof(uint64_t) * page_count
;
620 p
->packet
= g_malloc0(p
->packet_len
);
621 p
->packet
->magic
= cpu_to_be32(MULTIFD_MAGIC
);
622 p
->packet
->version
= cpu_to_be32(MULTIFD_VERSION
);
623 p
->name
= g_strdup_printf("multifdsend_%d", i
);
624 socket_send_channel_create(multifd_new_send_channel_async
, p
);
630 MultiFDRecvParams
*params
;
631 /* number of created threads */
633 /* syncs main thread and channels */
634 QemuSemaphore sem_sync
;
635 /* global number of generated multifd packets */
637 } *multifd_recv_state
;
639 static void multifd_recv_terminate_threads(Error
*err
)
643 trace_multifd_recv_terminate_threads(err
!= NULL
);
646 MigrationState
*s
= migrate_get_current();
647 migrate_set_error(s
, err
);
648 if (s
->state
== MIGRATION_STATUS_SETUP
||
649 s
->state
== MIGRATION_STATUS_ACTIVE
) {
650 migrate_set_state(&s
->state
, s
->state
,
651 MIGRATION_STATUS_FAILED
);
655 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
656 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
658 qemu_mutex_lock(&p
->mutex
);
661 * We could arrive here for two reasons:
662 * - normal quit, i.e. everything went fine, just finished
663 * - error quit: We close the channels so the channel threads
664 * finish the qio_channel_read_all_eof()
667 qio_channel_shutdown(p
->c
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
669 qemu_mutex_unlock(&p
->mutex
);
673 int multifd_load_cleanup(Error
**errp
)
678 if (!migrate_use_multifd()) {
681 multifd_recv_terminate_threads(NULL
);
682 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
683 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
688 * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
689 * however try to wakeup it without harm in cleanup phase.
691 qemu_sem_post(&p
->sem_sync
);
692 qemu_thread_join(&p
->thread
);
695 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
696 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
698 object_unref(OBJECT(p
->c
));
700 qemu_mutex_destroy(&p
->mutex
);
701 qemu_sem_destroy(&p
->sem_sync
);
704 multifd_pages_clear(p
->pages
);
710 qemu_sem_destroy(&multifd_recv_state
->sem_sync
);
711 g_free(multifd_recv_state
->params
);
712 multifd_recv_state
->params
= NULL
;
713 g_free(multifd_recv_state
);
714 multifd_recv_state
= NULL
;
719 void multifd_recv_sync_main(void)
723 if (!migrate_use_multifd()) {
726 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
727 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
729 trace_multifd_recv_sync_main_wait(p
->id
);
730 qemu_sem_wait(&multifd_recv_state
->sem_sync
);
732 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
733 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
735 qemu_mutex_lock(&p
->mutex
);
736 if (multifd_recv_state
->packet_num
< p
->packet_num
) {
737 multifd_recv_state
->packet_num
= p
->packet_num
;
739 qemu_mutex_unlock(&p
->mutex
);
740 trace_multifd_recv_sync_main_signal(p
->id
);
741 qemu_sem_post(&p
->sem_sync
);
743 trace_multifd_recv_sync_main(multifd_recv_state
->packet_num
);
746 static void *multifd_recv_thread(void *opaque
)
748 MultiFDRecvParams
*p
= opaque
;
749 Error
*local_err
= NULL
;
752 trace_multifd_recv_thread_start(p
->id
);
753 rcu_register_thread();
763 ret
= qio_channel_read_all_eof(p
->c
, (void *)p
->packet
,
764 p
->packet_len
, &local_err
);
765 if (ret
== 0) { /* EOF */
768 if (ret
== -1) { /* Error */
772 qemu_mutex_lock(&p
->mutex
);
773 ret
= multifd_recv_unfill_packet(p
, &local_err
);
775 qemu_mutex_unlock(&p
->mutex
);
779 used
= p
->pages
->used
;
781 trace_multifd_recv(p
->id
, p
->packet_num
, used
, flags
,
782 p
->next_packet_size
);
784 p
->num_pages
+= used
;
785 qemu_mutex_unlock(&p
->mutex
);
788 ret
= qio_channel_readv_all(p
->c
, p
->pages
->iov
,
795 if (flags
& MULTIFD_FLAG_SYNC
) {
796 qemu_sem_post(&multifd_recv_state
->sem_sync
);
797 qemu_sem_wait(&p
->sem_sync
);
802 multifd_recv_terminate_threads(local_err
);
804 qemu_mutex_lock(&p
->mutex
);
806 qemu_mutex_unlock(&p
->mutex
);
808 rcu_unregister_thread();
809 trace_multifd_recv_thread_end(p
->id
, p
->num_packets
, p
->num_pages
);
814 int multifd_load_setup(Error
**errp
)
817 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
820 if (!migrate_use_multifd()) {
823 thread_count
= migrate_multifd_channels();
824 multifd_recv_state
= g_malloc0(sizeof(*multifd_recv_state
));
825 multifd_recv_state
->params
= g_new0(MultiFDRecvParams
, thread_count
);
826 atomic_set(&multifd_recv_state
->count
, 0);
827 qemu_sem_init(&multifd_recv_state
->sem_sync
, 0);
829 for (i
= 0; i
< thread_count
; i
++) {
830 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
832 qemu_mutex_init(&p
->mutex
);
833 qemu_sem_init(&p
->sem_sync
, 0);
836 p
->pages
= multifd_pages_init(page_count
);
837 p
->packet_len
= sizeof(MultiFDPacket_t
)
838 + sizeof(uint64_t) * page_count
;
839 p
->packet
= g_malloc0(p
->packet_len
);
840 p
->name
= g_strdup_printf("multifdrecv_%d", i
);
845 bool multifd_recv_all_channels_created(void)
847 int thread_count
= migrate_multifd_channels();
849 if (!migrate_use_multifd()) {
853 return thread_count
== atomic_read(&multifd_recv_state
->count
);
857 * Try to receive all multifd channels to get ready for the migration.
858 * - Return true and do not set @errp when correctly receving all channels;
859 * - Return false and do not set @errp when correctly receiving the current one;
860 * - Return false and set @errp when failing to receive the current channel.
862 bool multifd_recv_new_channel(QIOChannel
*ioc
, Error
**errp
)
864 MultiFDRecvParams
*p
;
865 Error
*local_err
= NULL
;
868 id
= multifd_recv_initial_packet(ioc
, &local_err
);
870 multifd_recv_terminate_threads(local_err
);
871 error_propagate_prepend(errp
, local_err
,
872 "failed to receive packet"
873 " via multifd channel %d: ",
874 atomic_read(&multifd_recv_state
->count
));
877 trace_multifd_recv_new_channel(id
);
879 p
= &multifd_recv_state
->params
[id
];
881 error_setg(&local_err
, "multifd: received id '%d' already setup'",
883 multifd_recv_terminate_threads(local_err
);
884 error_propagate(errp
, local_err
);
888 object_ref(OBJECT(ioc
));
893 qemu_thread_create(&p
->thread
, p
->name
, multifd_recv_thread
, p
,
894 QEMU_THREAD_JOINABLE
);
895 atomic_inc(&multifd_recv_state
->count
);
896 return atomic_read(&multifd_recv_state
->count
) ==
897 migrate_multifd_channels();