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"
33 #define MULTIFD_MAGIC 0x11223344U
34 #define MULTIFD_VERSION 1
39 unsigned char uuid
[16]; /* QemuUUID */
41 uint8_t unused1
[7]; /* Reserved for future use */
42 uint64_t unused2
[4]; /* Reserved for future use */
43 } __attribute__((packed
)) MultiFDInit_t
;
45 /* Multifd without compression */
48 * nocomp_send_setup: setup send side
50 * For no compression this function does nothing.
52 * Returns 0 for success or -1 for error
54 * @p: Params for the channel that we are using
55 * @errp: pointer to an error
57 static int nocomp_send_setup(MultiFDSendParams
*p
, Error
**errp
)
63 * nocomp_send_cleanup: cleanup send side
65 * For no compression this function does nothing.
67 * @p: Params for the channel that we are using
69 static void nocomp_send_cleanup(MultiFDSendParams
*p
, Error
**errp
)
75 * nocomp_send_prepare: prepare date to be able to send
77 * For no compression we just have to calculate the size of the
80 * Returns 0 for success or -1 for error
82 * @p: Params for the channel that we are using
83 * @used: number of pages used
84 * @errp: pointer to an error
86 static int nocomp_send_prepare(MultiFDSendParams
*p
, uint32_t used
,
89 p
->next_packet_size
= used
* qemu_target_page_size();
90 p
->flags
|= MULTIFD_FLAG_NOCOMP
;
95 * nocomp_send_write: do the actual write of the data
97 * For no compression we just have to write the data.
99 * Returns 0 for success or -1 for error
101 * @p: Params for the channel that we are using
102 * @used: number of pages used
103 * @errp: pointer to an error
105 static int nocomp_send_write(MultiFDSendParams
*p
, uint32_t used
, Error
**errp
)
107 return qio_channel_writev_all(p
->c
, p
->pages
->iov
, used
, errp
);
111 * nocomp_recv_setup: setup receive side
113 * For no compression this function does nothing.
115 * Returns 0 for success or -1 for error
117 * @p: Params for the channel that we are using
118 * @errp: pointer to an error
120 static int nocomp_recv_setup(MultiFDRecvParams
*p
, Error
**errp
)
126 * nocomp_recv_cleanup: setup receive side
128 * For no compression this function does nothing.
130 * @p: Params for the channel that we are using
132 static void nocomp_recv_cleanup(MultiFDRecvParams
*p
)
137 * nocomp_recv_pages: read the data from the channel into actual pages
139 * For no compression we just need to read things into the correct place.
141 * Returns 0 for success or -1 for error
143 * @p: Params for the channel that we are using
144 * @used: number of pages used
145 * @errp: pointer to an error
147 static int nocomp_recv_pages(MultiFDRecvParams
*p
, uint32_t used
, Error
**errp
)
149 uint32_t flags
= p
->flags
& MULTIFD_FLAG_COMPRESSION_MASK
;
151 if (flags
!= MULTIFD_FLAG_NOCOMP
) {
152 error_setg(errp
, "multifd %d: flags received %x flags expected %x",
153 p
->id
, flags
, MULTIFD_FLAG_NOCOMP
);
156 return qio_channel_readv_all(p
->c
, p
->pages
->iov
, used
, errp
);
159 static MultiFDMethods multifd_nocomp_ops
= {
160 .send_setup
= nocomp_send_setup
,
161 .send_cleanup
= nocomp_send_cleanup
,
162 .send_prepare
= nocomp_send_prepare
,
163 .send_write
= nocomp_send_write
,
164 .recv_setup
= nocomp_recv_setup
,
165 .recv_cleanup
= nocomp_recv_cleanup
,
166 .recv_pages
= nocomp_recv_pages
169 static MultiFDMethods
*multifd_ops
[MULTIFD_COMPRESSION__MAX
] = {
170 [MULTIFD_COMPRESSION_NONE
] = &multifd_nocomp_ops
,
173 void multifd_register_ops(int method
, MultiFDMethods
*ops
)
175 assert(0 < method
&& method
< MULTIFD_COMPRESSION__MAX
);
176 multifd_ops
[method
] = ops
;
179 static int multifd_send_initial_packet(MultiFDSendParams
*p
, Error
**errp
)
181 MultiFDInit_t msg
= {};
184 msg
.magic
= cpu_to_be32(MULTIFD_MAGIC
);
185 msg
.version
= cpu_to_be32(MULTIFD_VERSION
);
187 memcpy(msg
.uuid
, &qemu_uuid
.data
, sizeof(msg
.uuid
));
189 ret
= qio_channel_write_all(p
->c
, (char *)&msg
, sizeof(msg
), errp
);
196 static int multifd_recv_initial_packet(QIOChannel
*c
, Error
**errp
)
201 ret
= qio_channel_read_all(c
, (char *)&msg
, sizeof(msg
), errp
);
206 msg
.magic
= be32_to_cpu(msg
.magic
);
207 msg
.version
= be32_to_cpu(msg
.version
);
209 if (msg
.magic
!= MULTIFD_MAGIC
) {
210 error_setg(errp
, "multifd: received packet magic %x "
211 "expected %x", msg
.magic
, MULTIFD_MAGIC
);
215 if (msg
.version
!= MULTIFD_VERSION
) {
216 error_setg(errp
, "multifd: received packet version %d "
217 "expected %d", msg
.version
, MULTIFD_VERSION
);
221 if (memcmp(msg
.uuid
, &qemu_uuid
, sizeof(qemu_uuid
))) {
222 char *uuid
= qemu_uuid_unparse_strdup(&qemu_uuid
);
223 char *msg_uuid
= qemu_uuid_unparse_strdup((const QemuUUID
*)msg
.uuid
);
225 error_setg(errp
, "multifd: received uuid '%s' and expected "
226 "uuid '%s' for channel %hhd", msg_uuid
, uuid
, msg
.id
);
232 if (msg
.id
> migrate_multifd_channels()) {
233 error_setg(errp
, "multifd: received channel version %d "
234 "expected %d", msg
.version
, MULTIFD_VERSION
);
241 static MultiFDPages_t
*multifd_pages_init(size_t size
)
243 MultiFDPages_t
*pages
= g_new0(MultiFDPages_t
, 1);
245 pages
->allocated
= size
;
246 pages
->iov
= g_new0(struct iovec
, size
);
247 pages
->offset
= g_new0(ram_addr_t
, size
);
252 static void multifd_pages_clear(MultiFDPages_t
*pages
)
255 pages
->allocated
= 0;
256 pages
->packet_num
= 0;
260 g_free(pages
->offset
);
261 pages
->offset
= NULL
;
265 static void multifd_send_fill_packet(MultiFDSendParams
*p
)
267 MultiFDPacket_t
*packet
= p
->packet
;
270 packet
->flags
= cpu_to_be32(p
->flags
);
271 packet
->pages_alloc
= cpu_to_be32(p
->pages
->allocated
);
272 packet
->pages_used
= cpu_to_be32(p
->pages
->used
);
273 packet
->next_packet_size
= cpu_to_be32(p
->next_packet_size
);
274 packet
->packet_num
= cpu_to_be64(p
->packet_num
);
276 if (p
->pages
->block
) {
277 strncpy(packet
->ramblock
, p
->pages
->block
->idstr
, 256);
280 for (i
= 0; i
< p
->pages
->used
; i
++) {
281 /* there are architectures where ram_addr_t is 32 bit */
282 uint64_t temp
= p
->pages
->offset
[i
];
284 packet
->offset
[i
] = cpu_to_be64(temp
);
288 static int multifd_recv_unfill_packet(MultiFDRecvParams
*p
, Error
**errp
)
290 MultiFDPacket_t
*packet
= p
->packet
;
291 uint32_t pages_max
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
295 packet
->magic
= be32_to_cpu(packet
->magic
);
296 if (packet
->magic
!= MULTIFD_MAGIC
) {
297 error_setg(errp
, "multifd: received packet "
298 "magic %x and expected magic %x",
299 packet
->magic
, MULTIFD_MAGIC
);
303 packet
->version
= be32_to_cpu(packet
->version
);
304 if (packet
->version
!= MULTIFD_VERSION
) {
305 error_setg(errp
, "multifd: received packet "
306 "version %d and expected version %d",
307 packet
->version
, MULTIFD_VERSION
);
311 p
->flags
= be32_to_cpu(packet
->flags
);
313 packet
->pages_alloc
= be32_to_cpu(packet
->pages_alloc
);
315 * If we received a packet that is 100 times bigger than expected
316 * just stop migration. It is a magic number.
318 if (packet
->pages_alloc
> pages_max
* 100) {
319 error_setg(errp
, "multifd: received packet "
320 "with size %d and expected a maximum size of %d",
321 packet
->pages_alloc
, pages_max
* 100) ;
325 * We received a packet that is bigger than expected but inside
326 * reasonable limits (see previous comment). Just reallocate.
328 if (packet
->pages_alloc
> p
->pages
->allocated
) {
329 multifd_pages_clear(p
->pages
);
330 p
->pages
= multifd_pages_init(packet
->pages_alloc
);
333 p
->pages
->used
= be32_to_cpu(packet
->pages_used
);
334 if (p
->pages
->used
> packet
->pages_alloc
) {
335 error_setg(errp
, "multifd: received packet "
336 "with %d pages and expected maximum pages are %d",
337 p
->pages
->used
, packet
->pages_alloc
) ;
341 p
->next_packet_size
= be32_to_cpu(packet
->next_packet_size
);
342 p
->packet_num
= be64_to_cpu(packet
->packet_num
);
344 if (p
->pages
->used
== 0) {
348 /* make sure that ramblock is 0 terminated */
349 packet
->ramblock
[255] = 0;
350 block
= qemu_ram_block_by_name(packet
->ramblock
);
352 error_setg(errp
, "multifd: unknown ram block %s",
357 for (i
= 0; i
< p
->pages
->used
; i
++) {
358 uint64_t offset
= be64_to_cpu(packet
->offset
[i
]);
360 if (offset
> (block
->used_length
- qemu_target_page_size())) {
361 error_setg(errp
, "multifd: offset too long %" PRIu64
362 " (max " RAM_ADDR_FMT
")",
363 offset
, block
->max_length
);
366 p
->pages
->iov
[i
].iov_base
= block
->host
+ offset
;
367 p
->pages
->iov
[i
].iov_len
= qemu_target_page_size();
374 MultiFDSendParams
*params
;
375 /* array of pages to sent */
376 MultiFDPages_t
*pages
;
377 /* global number of generated multifd packets */
379 /* send channels ready */
380 QemuSemaphore channels_ready
;
382 * Have we already run terminate threads. There is a race when it
383 * happens that we got one error while we are exiting.
384 * We will use atomic operations. Only valid values are 0 and 1.
389 } *multifd_send_state
;
392 * How we use multifd_send_state->pages and channel->pages?
394 * We create a pages for each channel, and a main one. Each time that
395 * we need to send a batch of pages we interchange the ones between
396 * multifd_send_state and the channel that is sending it. There are
397 * two reasons for that:
398 * - to not have to do so many mallocs during migration
399 * - to make easier to know what to free at the end of migration
401 * This way we always know who is the owner of each "pages" struct,
402 * and we don't need any locking. It belongs to the migration thread
403 * or to the channel thread. Switching is safe because the migration
404 * thread is using the channel mutex when changing it, and the channel
405 * have to had finish with its own, otherwise pending_job can't be
409 static int multifd_send_pages(QEMUFile
*f
)
412 static int next_channel
;
413 MultiFDSendParams
*p
= NULL
; /* make happy gcc */
414 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
415 uint64_t transferred
;
417 if (qatomic_read(&multifd_send_state
->exiting
)) {
421 qemu_sem_wait(&multifd_send_state
->channels_ready
);
423 * next_channel can remain from a previous migration that was
424 * using more channels, so ensure it doesn't overflow if the
425 * limit is lower now.
427 next_channel
%= migrate_multifd_channels();
428 for (i
= next_channel
;; i
= (i
+ 1) % migrate_multifd_channels()) {
429 p
= &multifd_send_state
->params
[i
];
431 qemu_mutex_lock(&p
->mutex
);
433 error_report("%s: channel %d has already quit!", __func__
, i
);
434 qemu_mutex_unlock(&p
->mutex
);
437 if (!p
->pending_job
) {
439 next_channel
= (i
+ 1) % migrate_multifd_channels();
442 qemu_mutex_unlock(&p
->mutex
);
444 assert(!p
->pages
->used
);
445 assert(!p
->pages
->block
);
447 p
->packet_num
= multifd_send_state
->packet_num
++;
448 multifd_send_state
->pages
= p
->pages
;
450 transferred
= ((uint64_t) pages
->used
) * qemu_target_page_size()
452 qemu_file_update_transfer(f
, transferred
);
453 ram_counters
.multifd_bytes
+= transferred
;
454 ram_counters
.transferred
+= transferred
;
455 qemu_mutex_unlock(&p
->mutex
);
456 qemu_sem_post(&p
->sem
);
461 int multifd_queue_page(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
)
463 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
466 pages
->block
= block
;
469 if (pages
->block
== block
) {
470 pages
->offset
[pages
->used
] = offset
;
471 pages
->iov
[pages
->used
].iov_base
= block
->host
+ offset
;
472 pages
->iov
[pages
->used
].iov_len
= qemu_target_page_size();
475 if (pages
->used
< pages
->allocated
) {
480 if (multifd_send_pages(f
) < 0) {
484 if (pages
->block
!= block
) {
485 return multifd_queue_page(f
, block
, offset
);
491 static void multifd_send_terminate_threads(Error
*err
)
495 trace_multifd_send_terminate_threads(err
!= NULL
);
498 MigrationState
*s
= migrate_get_current();
499 migrate_set_error(s
, err
);
500 if (s
->state
== MIGRATION_STATUS_SETUP
||
501 s
->state
== MIGRATION_STATUS_PRE_SWITCHOVER
||
502 s
->state
== MIGRATION_STATUS_DEVICE
||
503 s
->state
== MIGRATION_STATUS_ACTIVE
) {
504 migrate_set_state(&s
->state
, s
->state
,
505 MIGRATION_STATUS_FAILED
);
510 * We don't want to exit each threads twice. Depending on where
511 * we get the error, or if there are two independent errors in two
512 * threads at the same time, we can end calling this function
515 if (qatomic_xchg(&multifd_send_state
->exiting
, 1)) {
519 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
520 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
522 qemu_mutex_lock(&p
->mutex
);
524 qemu_sem_post(&p
->sem
);
525 qemu_mutex_unlock(&p
->mutex
);
529 void multifd_save_cleanup(void)
533 if (!migrate_use_multifd()) {
536 multifd_send_terminate_threads(NULL
);
537 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
538 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
541 qemu_thread_join(&p
->thread
);
544 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
545 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
546 Error
*local_err
= NULL
;
548 socket_send_channel_destroy(p
->c
);
550 qemu_mutex_destroy(&p
->mutex
);
551 qemu_sem_destroy(&p
->sem
);
552 qemu_sem_destroy(&p
->sem_sync
);
555 g_free(p
->tls_hostname
);
556 p
->tls_hostname
= NULL
;
557 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
->used
) {
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
;
628 trace_multifd_send_thread_start(p
->id
);
629 rcu_register_thread();
631 if (multifd_send_initial_packet(p
, &local_err
) < 0) {
639 qemu_sem_wait(&p
->sem
);
641 if (qatomic_read(&multifd_send_state
->exiting
)) {
644 qemu_mutex_lock(&p
->mutex
);
646 if (p
->pending_job
) {
647 uint32_t used
= p
->pages
->used
;
648 uint64_t packet_num
= p
->packet_num
;
652 ret
= multifd_send_state
->ops
->send_prepare(p
, used
,
655 qemu_mutex_unlock(&p
->mutex
);
659 multifd_send_fill_packet(p
);
662 p
->num_pages
+= used
;
664 p
->pages
->block
= NULL
;
665 qemu_mutex_unlock(&p
->mutex
);
667 trace_multifd_send(p
->id
, packet_num
, used
, flags
,
668 p
->next_packet_size
);
670 ret
= qio_channel_write_all(p
->c
, (void *)p
->packet
,
671 p
->packet_len
, &local_err
);
677 ret
= multifd_send_state
->ops
->send_write(p
, used
, &local_err
);
683 qemu_mutex_lock(&p
->mutex
);
685 qemu_mutex_unlock(&p
->mutex
);
687 if (flags
& MULTIFD_FLAG_SYNC
) {
688 qemu_sem_post(&p
->sem_sync
);
690 qemu_sem_post(&multifd_send_state
->channels_ready
);
691 } else if (p
->quit
) {
692 qemu_mutex_unlock(&p
->mutex
);
695 qemu_mutex_unlock(&p
->mutex
);
696 /* sometimes there are spurious wakeups */
702 trace_multifd_send_error(p
->id
);
703 multifd_send_terminate_threads(local_err
);
704 error_free(local_err
);
708 * Error happen, I will exit, but I can't just leave, tell
709 * who pay attention to me.
712 qemu_sem_post(&p
->sem_sync
);
713 qemu_sem_post(&multifd_send_state
->channels_ready
);
716 qemu_mutex_lock(&p
->mutex
);
718 qemu_mutex_unlock(&p
->mutex
);
720 rcu_unregister_thread();
721 trace_multifd_send_thread_end(p
->id
, p
->num_packets
, p
->num_pages
);
726 static bool multifd_channel_connect(MultiFDSendParams
*p
,
730 static void multifd_tls_outgoing_handshake(QIOTask
*task
,
733 MultiFDSendParams
*p
= opaque
;
734 QIOChannel
*ioc
= QIO_CHANNEL(qio_task_get_source(task
));
737 if (qio_task_propagate_error(task
, &err
)) {
738 trace_multifd_tls_outgoing_handshake_error(ioc
, error_get_pretty(err
));
740 trace_multifd_tls_outgoing_handshake_complete(ioc
);
742 multifd_channel_connect(p
, ioc
, err
);
745 static void *multifd_tls_handshake_thread(void *opaque
)
747 MultiFDSendParams
*p
= opaque
;
748 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(p
->c
);
750 qio_channel_tls_handshake(tioc
,
751 multifd_tls_outgoing_handshake
,
758 static void multifd_tls_channel_connect(MultiFDSendParams
*p
,
762 MigrationState
*s
= migrate_get_current();
763 const char *hostname
= p
->tls_hostname
;
766 tioc
= migration_tls_client_create(s
, ioc
, hostname
, errp
);
771 object_unref(OBJECT(ioc
));
772 trace_multifd_tls_outgoing_handshake_start(ioc
, tioc
, hostname
);
773 qio_channel_set_name(QIO_CHANNEL(tioc
), "multifd-tls-outgoing");
774 p
->c
= QIO_CHANNEL(tioc
);
775 qemu_thread_create(&p
->thread
, "multifd-tls-handshake-worker",
776 multifd_tls_handshake_thread
, p
,
777 QEMU_THREAD_JOINABLE
);
780 static bool multifd_channel_connect(MultiFDSendParams
*p
,
784 MigrationState
*s
= migrate_get_current();
786 trace_multifd_set_outgoing_channel(
787 ioc
, object_get_typename(OBJECT(ioc
)), p
->tls_hostname
, error
);
790 if (s
->parameters
.tls_creds
&&
791 *s
->parameters
.tls_creds
&&
792 !object_dynamic_cast(OBJECT(ioc
),
793 TYPE_QIO_CHANNEL_TLS
)) {
794 multifd_tls_channel_connect(p
, ioc
, &error
);
797 * tls_channel_connect will call back to this
798 * function after the TLS handshake,
799 * so we mustn't call multifd_send_thread until then
806 /* update for tls qio channel */
808 qemu_thread_create(&p
->thread
, p
->name
, multifd_send_thread
, p
,
809 QEMU_THREAD_JOINABLE
);
817 static void multifd_new_send_channel_cleanup(MultiFDSendParams
*p
,
818 QIOChannel
*ioc
, Error
*err
)
820 migrate_set_error(migrate_get_current(), err
);
821 /* Error happen, we need to tell who pay attention to me */
822 qemu_sem_post(&multifd_send_state
->channels_ready
);
823 qemu_sem_post(&p
->sem_sync
);
825 * Although multifd_send_thread is not created, but main migration
826 * thread neet to judge whether it is running, so we need to mark
830 object_unref(OBJECT(ioc
));
834 static void multifd_new_send_channel_async(QIOTask
*task
, gpointer opaque
)
836 MultiFDSendParams
*p
= opaque
;
837 QIOChannel
*sioc
= QIO_CHANNEL(qio_task_get_source(task
));
838 Error
*local_err
= NULL
;
840 trace_multifd_new_send_channel_async(p
->id
);
841 if (qio_task_propagate_error(task
, &local_err
)) {
844 p
->c
= QIO_CHANNEL(sioc
);
845 qio_channel_set_delay(p
->c
, false);
847 if (multifd_channel_connect(p
, sioc
, local_err
)) {
854 multifd_new_send_channel_cleanup(p
, sioc
, local_err
);
857 int multifd_save_setup(Error
**errp
)
860 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
864 if (!migrate_use_multifd()) {
867 s
= migrate_get_current();
868 thread_count
= migrate_multifd_channels();
869 multifd_send_state
= g_malloc0(sizeof(*multifd_send_state
));
870 multifd_send_state
->params
= g_new0(MultiFDSendParams
, thread_count
);
871 multifd_send_state
->pages
= multifd_pages_init(page_count
);
872 qemu_sem_init(&multifd_send_state
->channels_ready
, 0);
873 qatomic_set(&multifd_send_state
->exiting
, 0);
874 multifd_send_state
->ops
= multifd_ops
[migrate_multifd_compression()];
876 for (i
= 0; i
< thread_count
; i
++) {
877 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
879 qemu_mutex_init(&p
->mutex
);
880 qemu_sem_init(&p
->sem
, 0);
881 qemu_sem_init(&p
->sem_sync
, 0);
885 p
->pages
= multifd_pages_init(page_count
);
886 p
->packet_len
= sizeof(MultiFDPacket_t
)
887 + sizeof(uint64_t) * page_count
;
888 p
->packet
= g_malloc0(p
->packet_len
);
889 p
->packet
->magic
= cpu_to_be32(MULTIFD_MAGIC
);
890 p
->packet
->version
= cpu_to_be32(MULTIFD_VERSION
);
891 p
->name
= g_strdup_printf("multifdsend_%d", i
);
892 p
->tls_hostname
= g_strdup(s
->hostname
);
893 socket_send_channel_create(multifd_new_send_channel_async
, p
);
896 for (i
= 0; i
< thread_count
; i
++) {
897 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
898 Error
*local_err
= NULL
;
901 ret
= multifd_send_state
->ops
->send_setup(p
, &local_err
);
903 error_propagate(errp
, local_err
);
911 MultiFDRecvParams
*params
;
912 /* number of created threads */
914 /* syncs main thread and channels */
915 QemuSemaphore sem_sync
;
916 /* global number of generated multifd packets */
920 } *multifd_recv_state
;
922 static void multifd_recv_terminate_threads(Error
*err
)
926 trace_multifd_recv_terminate_threads(err
!= NULL
);
929 MigrationState
*s
= migrate_get_current();
930 migrate_set_error(s
, err
);
931 if (s
->state
== MIGRATION_STATUS_SETUP
||
932 s
->state
== MIGRATION_STATUS_ACTIVE
) {
933 migrate_set_state(&s
->state
, s
->state
,
934 MIGRATION_STATUS_FAILED
);
938 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
939 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
941 qemu_mutex_lock(&p
->mutex
);
944 * We could arrive here for two reasons:
945 * - normal quit, i.e. everything went fine, just finished
946 * - error quit: We close the channels so the channel threads
947 * finish the qio_channel_read_all_eof()
950 qio_channel_shutdown(p
->c
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
952 qemu_mutex_unlock(&p
->mutex
);
956 int multifd_load_cleanup(Error
**errp
)
960 if (!migrate_use_multifd()) {
963 multifd_recv_terminate_threads(NULL
);
964 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
965 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
970 * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
971 * however try to wakeup it without harm in cleanup phase.
973 qemu_sem_post(&p
->sem_sync
);
974 qemu_thread_join(&p
->thread
);
977 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
978 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
980 if (object_dynamic_cast(OBJECT(p
->c
), TYPE_QIO_CHANNEL_SOCKET
)
981 && OBJECT(p
->c
)->ref
== 1) {
982 yank_unregister_function(MIGRATION_YANK_INSTANCE
,
983 yank_generic_iochannel
,
987 object_unref(OBJECT(p
->c
));
989 qemu_mutex_destroy(&p
->mutex
);
990 qemu_sem_destroy(&p
->sem_sync
);
993 multifd_pages_clear(p
->pages
);
998 multifd_recv_state
->ops
->recv_cleanup(p
);
1000 qemu_sem_destroy(&multifd_recv_state
->sem_sync
);
1001 g_free(multifd_recv_state
->params
);
1002 multifd_recv_state
->params
= NULL
;
1003 g_free(multifd_recv_state
);
1004 multifd_recv_state
= NULL
;
1009 void multifd_recv_sync_main(void)
1013 if (!migrate_use_multifd()) {
1016 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1017 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1019 trace_multifd_recv_sync_main_wait(p
->id
);
1020 qemu_sem_wait(&multifd_recv_state
->sem_sync
);
1022 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1023 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1025 WITH_QEMU_LOCK_GUARD(&p
->mutex
) {
1026 if (multifd_recv_state
->packet_num
< p
->packet_num
) {
1027 multifd_recv_state
->packet_num
= p
->packet_num
;
1030 trace_multifd_recv_sync_main_signal(p
->id
);
1031 qemu_sem_post(&p
->sem_sync
);
1033 trace_multifd_recv_sync_main(multifd_recv_state
->packet_num
);
1036 static void *multifd_recv_thread(void *opaque
)
1038 MultiFDRecvParams
*p
= opaque
;
1039 Error
*local_err
= NULL
;
1042 trace_multifd_recv_thread_start(p
->id
);
1043 rcu_register_thread();
1053 ret
= qio_channel_read_all_eof(p
->c
, (void *)p
->packet
,
1054 p
->packet_len
, &local_err
);
1055 if (ret
== 0) { /* EOF */
1058 if (ret
== -1) { /* Error */
1062 qemu_mutex_lock(&p
->mutex
);
1063 ret
= multifd_recv_unfill_packet(p
, &local_err
);
1065 qemu_mutex_unlock(&p
->mutex
);
1069 used
= p
->pages
->used
;
1071 /* recv methods don't know how to handle the SYNC flag */
1072 p
->flags
&= ~MULTIFD_FLAG_SYNC
;
1073 trace_multifd_recv(p
->id
, p
->packet_num
, used
, flags
,
1074 p
->next_packet_size
);
1076 p
->num_pages
+= used
;
1077 qemu_mutex_unlock(&p
->mutex
);
1080 ret
= multifd_recv_state
->ops
->recv_pages(p
, used
, &local_err
);
1086 if (flags
& MULTIFD_FLAG_SYNC
) {
1087 qemu_sem_post(&multifd_recv_state
->sem_sync
);
1088 qemu_sem_wait(&p
->sem_sync
);
1093 multifd_recv_terminate_threads(local_err
);
1094 error_free(local_err
);
1096 qemu_mutex_lock(&p
->mutex
);
1098 qemu_mutex_unlock(&p
->mutex
);
1100 rcu_unregister_thread();
1101 trace_multifd_recv_thread_end(p
->id
, p
->num_packets
, p
->num_pages
);
1106 int multifd_load_setup(Error
**errp
)
1109 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
1112 if (!migrate_use_multifd()) {
1115 thread_count
= migrate_multifd_channels();
1116 multifd_recv_state
= g_malloc0(sizeof(*multifd_recv_state
));
1117 multifd_recv_state
->params
= g_new0(MultiFDRecvParams
, thread_count
);
1118 qatomic_set(&multifd_recv_state
->count
, 0);
1119 qemu_sem_init(&multifd_recv_state
->sem_sync
, 0);
1120 multifd_recv_state
->ops
= multifd_ops
[migrate_multifd_compression()];
1122 for (i
= 0; i
< thread_count
; i
++) {
1123 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1125 qemu_mutex_init(&p
->mutex
);
1126 qemu_sem_init(&p
->sem_sync
, 0);
1129 p
->pages
= multifd_pages_init(page_count
);
1130 p
->packet_len
= sizeof(MultiFDPacket_t
)
1131 + sizeof(uint64_t) * page_count
;
1132 p
->packet
= g_malloc0(p
->packet_len
);
1133 p
->name
= g_strdup_printf("multifdrecv_%d", i
);
1136 for (i
= 0; i
< thread_count
; i
++) {
1137 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1138 Error
*local_err
= NULL
;
1141 ret
= multifd_recv_state
->ops
->recv_setup(p
, &local_err
);
1143 error_propagate(errp
, local_err
);
1150 bool multifd_recv_all_channels_created(void)
1152 int thread_count
= migrate_multifd_channels();
1154 if (!migrate_use_multifd()) {
1158 return thread_count
== qatomic_read(&multifd_recv_state
->count
);
1162 * Try to receive all multifd channels to get ready for the migration.
1163 * - Return true and do not set @errp when correctly receiving all channels;
1164 * - Return false and do not set @errp when correctly receiving the current one;
1165 * - Return false and set @errp when failing to receive the current channel.
1167 bool multifd_recv_new_channel(QIOChannel
*ioc
, Error
**errp
)
1169 MultiFDRecvParams
*p
;
1170 Error
*local_err
= NULL
;
1173 id
= multifd_recv_initial_packet(ioc
, &local_err
);
1175 multifd_recv_terminate_threads(local_err
);
1176 error_propagate_prepend(errp
, local_err
,
1177 "failed to receive packet"
1178 " via multifd channel %d: ",
1179 qatomic_read(&multifd_recv_state
->count
));
1182 trace_multifd_recv_new_channel(id
);
1184 p
= &multifd_recv_state
->params
[id
];
1186 error_setg(&local_err
, "multifd: received id '%d' already setup'",
1188 multifd_recv_terminate_threads(local_err
);
1189 error_propagate(errp
, local_err
);
1193 object_ref(OBJECT(ioc
));
1194 /* initial packet */
1198 qemu_thread_create(&p
->thread
, p
->name
, multifd_recv_thread
, p
,
1199 QEMU_THREAD_JOINABLE
);
1200 qatomic_inc(&multifd_recv_state
->count
);
1201 return qatomic_read(&multifd_recv_state
->count
) ==
1202 migrate_multifd_channels();