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"
22 #include "migration-stats.h"
25 #include "qemu-file.h"
28 #include "threadinfo.h"
30 #include "qemu/yank.h"
31 #include "io/channel-socket.h"
32 #include "yank_functions.h"
36 #define MULTIFD_MAGIC 0x11223344U
37 #define MULTIFD_VERSION 1
42 unsigned char uuid
[16]; /* QemuUUID */
44 uint8_t unused1
[7]; /* Reserved for future use */
45 uint64_t unused2
[4]; /* Reserved for future use */
46 } __attribute__((packed
)) MultiFDInit_t
;
48 /* Multifd without compression */
51 * nocomp_send_setup: setup send side
53 * For no compression this function does nothing.
55 * Returns 0 for success or -1 for error
57 * @p: Params for the channel that we are using
58 * @errp: pointer to an error
60 static int nocomp_send_setup(MultiFDSendParams
*p
, Error
**errp
)
66 * nocomp_send_cleanup: cleanup send side
68 * For no compression this function does nothing.
70 * @p: Params for the channel that we are using
71 * @errp: pointer to an error
73 static void nocomp_send_cleanup(MultiFDSendParams
*p
, Error
**errp
)
79 * nocomp_send_prepare: prepare date to be able to send
81 * For no compression we just have to calculate the size of the
84 * Returns 0 for success or -1 for error
86 * @p: Params for the channel that we are using
87 * @errp: pointer to an error
89 static int nocomp_send_prepare(MultiFDSendParams
*p
, Error
**errp
)
91 MultiFDPages_t
*pages
= p
->pages
;
93 for (int i
= 0; i
< p
->normal_num
; i
++) {
94 p
->iov
[p
->iovs_num
].iov_base
= pages
->block
->host
+ p
->normal
[i
];
95 p
->iov
[p
->iovs_num
].iov_len
= p
->page_size
;
99 p
->next_packet_size
= p
->normal_num
* p
->page_size
;
100 p
->flags
|= MULTIFD_FLAG_NOCOMP
;
105 * nocomp_recv_setup: setup receive side
107 * For no compression this function does nothing.
109 * Returns 0 for success or -1 for error
111 * @p: Params for the channel that we are using
112 * @errp: pointer to an error
114 static int nocomp_recv_setup(MultiFDRecvParams
*p
, Error
**errp
)
120 * nocomp_recv_cleanup: setup receive side
122 * For no compression this function does nothing.
124 * @p: Params for the channel that we are using
126 static void nocomp_recv_cleanup(MultiFDRecvParams
*p
)
131 * nocomp_recv_pages: read the data from the channel into actual pages
133 * For no compression we just need to read things into the correct place.
135 * Returns 0 for success or -1 for error
137 * @p: Params for the channel that we are using
138 * @errp: pointer to an error
140 static int nocomp_recv_pages(MultiFDRecvParams
*p
, Error
**errp
)
142 uint32_t flags
= p
->flags
& MULTIFD_FLAG_COMPRESSION_MASK
;
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
->normal_num
; i
++) {
150 p
->iov
[i
].iov_base
= p
->host
+ p
->normal
[i
];
151 p
->iov
[i
].iov_len
= p
->page_size
;
153 return qio_channel_readv_all(p
->c
, p
->iov
, p
->normal_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
= {};
178 size_t size
= sizeof(msg
);
181 msg
.magic
= cpu_to_be32(MULTIFD_MAGIC
);
182 msg
.version
= cpu_to_be32(MULTIFD_VERSION
);
184 memcpy(msg
.uuid
, &qemu_uuid
.data
, sizeof(msg
.uuid
));
186 ret
= qio_channel_write_all(p
->c
, (char *)&msg
, size
, errp
);
190 stat64_add(&mig_stats
.multifd_bytes
, size
);
191 stat64_add(&mig_stats
.transferred
, size
);
195 static int multifd_recv_initial_packet(QIOChannel
*c
, Error
**errp
)
200 ret
= qio_channel_read_all(c
, (char *)&msg
, sizeof(msg
), errp
);
205 msg
.magic
= be32_to_cpu(msg
.magic
);
206 msg
.version
= be32_to_cpu(msg
.version
);
208 if (msg
.magic
!= MULTIFD_MAGIC
) {
209 error_setg(errp
, "multifd: received packet magic %x "
210 "expected %x", msg
.magic
, MULTIFD_MAGIC
);
214 if (msg
.version
!= MULTIFD_VERSION
) {
215 error_setg(errp
, "multifd: received packet version %u "
216 "expected %u", msg
.version
, MULTIFD_VERSION
);
220 if (memcmp(msg
.uuid
, &qemu_uuid
, sizeof(qemu_uuid
))) {
221 char *uuid
= qemu_uuid_unparse_strdup(&qemu_uuid
);
222 char *msg_uuid
= qemu_uuid_unparse_strdup((const QemuUUID
*)msg
.uuid
);
224 error_setg(errp
, "multifd: received uuid '%s' and expected "
225 "uuid '%s' for channel %hhd", msg_uuid
, uuid
, msg
.id
);
231 if (msg
.id
> migrate_multifd_channels()) {
232 error_setg(errp
, "multifd: received channel version %u "
233 "expected %u", msg
.version
, MULTIFD_VERSION
);
240 static MultiFDPages_t
*multifd_pages_init(size_t size
)
242 MultiFDPages_t
*pages
= g_new0(MultiFDPages_t
, 1);
244 pages
->allocated
= size
;
245 pages
->offset
= g_new0(ram_addr_t
, size
);
250 static void multifd_pages_clear(MultiFDPages_t
*pages
)
253 pages
->allocated
= 0;
254 pages
->packet_num
= 0;
256 g_free(pages
->offset
);
257 pages
->offset
= NULL
;
261 static void multifd_send_fill_packet(MultiFDSendParams
*p
)
263 MultiFDPacket_t
*packet
= p
->packet
;
266 packet
->flags
= cpu_to_be32(p
->flags
);
267 packet
->pages_alloc
= cpu_to_be32(p
->pages
->allocated
);
268 packet
->normal_pages
= cpu_to_be32(p
->normal_num
);
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
->normal_num
; i
++) {
277 /* there are architectures where ram_addr_t is 32 bit */
278 uint64_t temp
= p
->normal
[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
;
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
> p
->page_count
) {
313 error_setg(errp
, "multifd: received packet "
314 "with size %u and expected a size of %u",
315 packet
->pages_alloc
, p
->page_count
) ;
319 p
->normal_num
= be32_to_cpu(packet
->normal_pages
);
320 if (p
->normal_num
> packet
->pages_alloc
) {
321 error_setg(errp
, "multifd: received packet "
322 "with %u pages and expected maximum pages are %u",
323 p
->normal_num
, packet
->pages_alloc
) ;
327 p
->next_packet_size
= be32_to_cpu(packet
->next_packet_size
);
328 p
->packet_num
= be64_to_cpu(packet
->packet_num
);
330 if (p
->normal_num
== 0) {
334 /* make sure that ramblock is 0 terminated */
335 packet
->ramblock
[255] = 0;
336 p
->block
= qemu_ram_block_by_name(packet
->ramblock
);
338 error_setg(errp
, "multifd: unknown ram block %s",
343 p
->host
= p
->block
->host
;
344 for (i
= 0; i
< p
->normal_num
; i
++) {
345 uint64_t offset
= be64_to_cpu(packet
->offset
[i
]);
347 if (offset
> (p
->block
->used_length
- p
->page_size
)) {
348 error_setg(errp
, "multifd: offset too long %" PRIu64
349 " (max " RAM_ADDR_FMT
")",
350 offset
, p
->block
->used_length
);
353 p
->normal
[i
] = offset
;
360 MultiFDSendParams
*params
;
361 /* array of pages to sent */
362 MultiFDPages_t
*pages
;
363 /* global number of generated multifd packets */
365 /* send channels ready */
366 QemuSemaphore channels_ready
;
368 * Have we already run terminate threads. There is a race when it
369 * happens that we got one error while we are exiting.
370 * We will use atomic operations. Only valid values are 0 and 1.
375 } *multifd_send_state
;
378 * How we use multifd_send_state->pages and channel->pages?
380 * We create a pages for each channel, and a main one. Each time that
381 * we need to send a batch of pages we interchange the ones between
382 * multifd_send_state and the channel that is sending it. There are
383 * two reasons for that:
384 * - to not have to do so many mallocs during migration
385 * - to make easier to know what to free at the end of migration
387 * This way we always know who is the owner of each "pages" struct,
388 * and we don't need any locking. It belongs to the migration thread
389 * or to the channel thread. Switching is safe because the migration
390 * thread is using the channel mutex when changing it, and the channel
391 * have to had finish with its own, otherwise pending_job can't be
395 static int multifd_send_pages(QEMUFile
*f
)
398 static int next_channel
;
399 MultiFDSendParams
*p
= NULL
; /* make happy gcc */
400 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
402 if (qatomic_read(&multifd_send_state
->exiting
)) {
406 qemu_sem_wait(&multifd_send_state
->channels_ready
);
408 * next_channel can remain from a previous migration that was
409 * using more channels, so ensure it doesn't overflow if the
410 * limit is lower now.
412 next_channel
%= migrate_multifd_channels();
413 for (i
= next_channel
;; i
= (i
+ 1) % migrate_multifd_channels()) {
414 p
= &multifd_send_state
->params
[i
];
416 qemu_mutex_lock(&p
->mutex
);
418 error_report("%s: channel %d has already quit!", __func__
, i
);
419 qemu_mutex_unlock(&p
->mutex
);
422 if (!p
->pending_job
) {
424 next_channel
= (i
+ 1) % migrate_multifd_channels();
427 qemu_mutex_unlock(&p
->mutex
);
429 assert(!p
->pages
->num
);
430 assert(!p
->pages
->block
);
432 p
->packet_num
= multifd_send_state
->packet_num
++;
433 multifd_send_state
->pages
= p
->pages
;
435 qemu_mutex_unlock(&p
->mutex
);
436 qemu_sem_post(&p
->sem
);
441 int multifd_queue_page(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
)
443 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
444 bool changed
= false;
447 pages
->block
= block
;
450 if (pages
->block
== block
) {
451 pages
->offset
[pages
->num
] = offset
;
454 if (pages
->num
< pages
->allocated
) {
461 if (multifd_send_pages(f
) < 0) {
466 return multifd_queue_page(f
, block
, offset
);
472 static void multifd_send_terminate_threads(Error
*err
)
476 trace_multifd_send_terminate_threads(err
!= NULL
);
479 MigrationState
*s
= migrate_get_current();
480 migrate_set_error(s
, err
);
481 if (s
->state
== MIGRATION_STATUS_SETUP
||
482 s
->state
== MIGRATION_STATUS_PRE_SWITCHOVER
||
483 s
->state
== MIGRATION_STATUS_DEVICE
||
484 s
->state
== MIGRATION_STATUS_ACTIVE
) {
485 migrate_set_state(&s
->state
, s
->state
,
486 MIGRATION_STATUS_FAILED
);
491 * We don't want to exit each threads twice. Depending on where
492 * we get the error, or if there are two independent errors in two
493 * threads at the same time, we can end calling this function
496 if (qatomic_xchg(&multifd_send_state
->exiting
, 1)) {
500 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
501 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
503 qemu_mutex_lock(&p
->mutex
);
505 qemu_sem_post(&p
->sem
);
507 qio_channel_shutdown(p
->c
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
509 qemu_mutex_unlock(&p
->mutex
);
513 static int multifd_send_channel_destroy(QIOChannel
*send
)
515 return socket_send_channel_destroy(send
);
518 void multifd_save_cleanup(void)
522 if (!migrate_multifd()) {
525 multifd_send_terminate_threads(NULL
);
526 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
527 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
530 qemu_thread_join(&p
->thread
);
533 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
534 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
535 Error
*local_err
= NULL
;
537 if (p
->registered_yank
) {
538 migration_ioc_unregister_yank(p
->c
);
540 multifd_send_channel_destroy(p
->c
);
542 qemu_mutex_destroy(&p
->mutex
);
543 qemu_sem_destroy(&p
->sem
);
544 qemu_sem_destroy(&p
->sem_sync
);
547 multifd_pages_clear(p
->pages
);
556 multifd_send_state
->ops
->send_cleanup(p
, &local_err
);
558 migrate_set_error(migrate_get_current(), local_err
);
559 error_free(local_err
);
562 qemu_sem_destroy(&multifd_send_state
->channels_ready
);
563 g_free(multifd_send_state
->params
);
564 multifd_send_state
->params
= NULL
;
565 multifd_pages_clear(multifd_send_state
->pages
);
566 multifd_send_state
->pages
= NULL
;
567 g_free(multifd_send_state
);
568 multifd_send_state
= NULL
;
571 static int multifd_zero_copy_flush(QIOChannel
*c
)
576 ret
= qio_channel_flush(c
, &err
);
578 error_report_err(err
);
582 stat64_add(&mig_stats
.dirty_sync_missed_zero_copy
, 1);
588 int multifd_send_sync_main(QEMUFile
*f
)
591 bool flush_zero_copy
;
593 if (!migrate_multifd()) {
596 if (multifd_send_state
->pages
->num
) {
597 if (multifd_send_pages(f
) < 0) {
598 error_report("%s: multifd_send_pages fail", __func__
);
604 * When using zero-copy, it's necessary to flush the pages before any of
605 * the pages can be sent again, so we'll make sure the new version of the
606 * pages will always arrive _later_ than the old pages.
608 * Currently we achieve this by flushing the zero-page requested writes
609 * per ram iteration, but in the future we could potentially optimize it
610 * to be less frequent, e.g. only after we finished one whole scanning of
611 * all the dirty bitmaps.
614 flush_zero_copy
= migrate_zero_copy_send();
616 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
617 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
619 trace_multifd_send_sync_main_signal(p
->id
);
621 qemu_mutex_lock(&p
->mutex
);
624 error_report("%s: channel %d has already quit", __func__
, i
);
625 qemu_mutex_unlock(&p
->mutex
);
629 p
->packet_num
= multifd_send_state
->packet_num
++;
630 p
->flags
|= MULTIFD_FLAG_SYNC
;
632 qemu_mutex_unlock(&p
->mutex
);
633 qemu_sem_post(&p
->sem
);
635 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
636 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
638 qemu_sem_wait(&multifd_send_state
->channels_ready
);
639 trace_multifd_send_sync_main_wait(p
->id
);
640 qemu_sem_wait(&p
->sem_sync
);
642 if (flush_zero_copy
&& p
->c
&& (multifd_zero_copy_flush(p
->c
) < 0)) {
646 trace_multifd_send_sync_main(multifd_send_state
->packet_num
);
651 static void *multifd_send_thread(void *opaque
)
653 MultiFDSendParams
*p
= opaque
;
654 MigrationThread
*thread
= NULL
;
655 Error
*local_err
= NULL
;
657 bool use_zero_copy_send
= migrate_zero_copy_send();
659 thread
= migration_threads_add(p
->name
, qemu_get_thread_id());
661 trace_multifd_send_thread_start(p
->id
);
662 rcu_register_thread();
664 if (multifd_send_initial_packet(p
, &local_err
) < 0) {
672 qemu_sem_post(&multifd_send_state
->channels_ready
);
673 qemu_sem_wait(&p
->sem
);
675 if (qatomic_read(&multifd_send_state
->exiting
)) {
678 qemu_mutex_lock(&p
->mutex
);
680 if (p
->pending_job
) {
681 uint64_t packet_num
= p
->packet_num
;
685 if (use_zero_copy_send
) {
691 for (int i
= 0; i
< p
->pages
->num
; i
++) {
692 p
->normal
[p
->normal_num
] = p
->pages
->offset
[i
];
697 ret
= multifd_send_state
->ops
->send_prepare(p
, &local_err
);
699 qemu_mutex_unlock(&p
->mutex
);
703 multifd_send_fill_packet(p
);
707 p
->total_normal_pages
+= p
->normal_num
;
709 p
->pages
->block
= NULL
;
710 qemu_mutex_unlock(&p
->mutex
);
712 trace_multifd_send(p
->id
, packet_num
, p
->normal_num
, flags
,
713 p
->next_packet_size
);
715 if (use_zero_copy_send
) {
716 /* Send header first, without zerocopy */
717 ret
= qio_channel_write_all(p
->c
, (void *)p
->packet
,
718 p
->packet_len
, &local_err
);
723 /* Send header using the same writev call */
724 p
->iov
[0].iov_len
= p
->packet_len
;
725 p
->iov
[0].iov_base
= p
->packet
;
728 ret
= qio_channel_writev_full_all(p
->c
, p
->iov
, p
->iovs_num
, NULL
,
729 0, p
->write_flags
, &local_err
);
734 stat64_add(&mig_stats
.multifd_bytes
,
735 p
->next_packet_size
+ p
->packet_len
);
736 stat64_add(&mig_stats
.transferred
,
737 p
->next_packet_size
+ p
->packet_len
);
738 p
->next_packet_size
= 0;
739 qemu_mutex_lock(&p
->mutex
);
741 qemu_mutex_unlock(&p
->mutex
);
743 if (flags
& MULTIFD_FLAG_SYNC
) {
744 qemu_sem_post(&p
->sem_sync
);
747 qemu_mutex_unlock(&p
->mutex
);
748 /* sometimes there are spurious wakeups */
755 trace_multifd_send_error(p
->id
);
756 multifd_send_terminate_threads(local_err
);
757 qemu_sem_post(&p
->sem_sync
);
758 qemu_sem_post(&multifd_send_state
->channels_ready
);
759 error_free(local_err
);
762 qemu_mutex_lock(&p
->mutex
);
764 qemu_mutex_unlock(&p
->mutex
);
766 rcu_unregister_thread();
767 migration_threads_remove(thread
);
768 trace_multifd_send_thread_end(p
->id
, p
->num_packets
, p
->total_normal_pages
);
773 static bool multifd_channel_connect(MultiFDSendParams
*p
,
777 static void multifd_tls_outgoing_handshake(QIOTask
*task
,
780 MultiFDSendParams
*p
= opaque
;
781 QIOChannel
*ioc
= QIO_CHANNEL(qio_task_get_source(task
));
784 if (!qio_task_propagate_error(task
, &err
)) {
785 trace_multifd_tls_outgoing_handshake_complete(ioc
);
786 if (multifd_channel_connect(p
, ioc
, &err
)) {
791 trace_multifd_tls_outgoing_handshake_error(ioc
, error_get_pretty(err
));
794 * Error happen, mark multifd_send_thread status as 'quit' although it
795 * is not created, and then tell who pay attention to me.
798 qemu_sem_post(&multifd_send_state
->channels_ready
);
799 qemu_sem_post(&p
->sem_sync
);
802 static void *multifd_tls_handshake_thread(void *opaque
)
804 MultiFDSendParams
*p
= opaque
;
805 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(p
->c
);
807 qio_channel_tls_handshake(tioc
,
808 multifd_tls_outgoing_handshake
,
815 static bool multifd_tls_channel_connect(MultiFDSendParams
*p
,
819 MigrationState
*s
= migrate_get_current();
820 const char *hostname
= s
->hostname
;
823 tioc
= migration_tls_client_create(ioc
, hostname
, errp
);
828 object_unref(OBJECT(ioc
));
829 trace_multifd_tls_outgoing_handshake_start(ioc
, tioc
, hostname
);
830 qio_channel_set_name(QIO_CHANNEL(tioc
), "multifd-tls-outgoing");
831 p
->c
= QIO_CHANNEL(tioc
);
832 qemu_thread_create(&p
->thread
, "multifd-tls-handshake-worker",
833 multifd_tls_handshake_thread
, p
,
834 QEMU_THREAD_JOINABLE
);
838 static bool multifd_channel_connect(MultiFDSendParams
*p
,
842 trace_multifd_set_outgoing_channel(
843 ioc
, object_get_typename(OBJECT(ioc
)),
844 migrate_get_current()->hostname
);
846 if (migrate_channel_requires_tls_upgrade(ioc
)) {
848 * tls_channel_connect will call back to this
849 * function after the TLS handshake,
850 * so we mustn't call multifd_send_thread until then
852 return multifd_tls_channel_connect(p
, ioc
, errp
);
855 migration_ioc_register_yank(ioc
);
856 p
->registered_yank
= true;
858 qemu_thread_create(&p
->thread
, p
->name
, multifd_send_thread
, p
,
859 QEMU_THREAD_JOINABLE
);
864 static void multifd_new_send_channel_cleanup(MultiFDSendParams
*p
,
865 QIOChannel
*ioc
, Error
*err
)
867 migrate_set_error(migrate_get_current(), err
);
868 /* Error happen, we need to tell who pay attention to me */
869 qemu_sem_post(&multifd_send_state
->channels_ready
);
870 qemu_sem_post(&p
->sem_sync
);
872 * Although multifd_send_thread is not created, but main migration
873 * thread need to judge whether it is running, so we need to mark
877 object_unref(OBJECT(ioc
));
881 static void multifd_new_send_channel_async(QIOTask
*task
, gpointer opaque
)
883 MultiFDSendParams
*p
= opaque
;
884 QIOChannel
*ioc
= QIO_CHANNEL(qio_task_get_source(task
));
885 Error
*local_err
= NULL
;
887 trace_multifd_new_send_channel_async(p
->id
);
888 if (!qio_task_propagate_error(task
, &local_err
)) {
890 qio_channel_set_delay(p
->c
, false);
892 if (multifd_channel_connect(p
, ioc
, &local_err
)) {
897 trace_multifd_new_send_channel_async_error(p
->id
, local_err
);
898 multifd_new_send_channel_cleanup(p
, ioc
, local_err
);
901 static void multifd_new_send_channel_create(gpointer opaque
)
903 socket_send_channel_create(multifd_new_send_channel_async
, opaque
);
906 int multifd_save_setup(Error
**errp
)
909 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
912 if (!migrate_multifd()) {
916 thread_count
= migrate_multifd_channels();
917 multifd_send_state
= g_malloc0(sizeof(*multifd_send_state
));
918 multifd_send_state
->params
= g_new0(MultiFDSendParams
, thread_count
);
919 multifd_send_state
->pages
= multifd_pages_init(page_count
);
920 qemu_sem_init(&multifd_send_state
->channels_ready
, 0);
921 qatomic_set(&multifd_send_state
->exiting
, 0);
922 multifd_send_state
->ops
= multifd_ops
[migrate_multifd_compression()];
924 for (i
= 0; i
< thread_count
; i
++) {
925 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
927 qemu_mutex_init(&p
->mutex
);
928 qemu_sem_init(&p
->sem
, 0);
929 qemu_sem_init(&p
->sem_sync
, 0);
933 p
->pages
= multifd_pages_init(page_count
);
934 p
->packet_len
= sizeof(MultiFDPacket_t
)
935 + sizeof(uint64_t) * page_count
;
936 p
->packet
= g_malloc0(p
->packet_len
);
937 p
->packet
->magic
= cpu_to_be32(MULTIFD_MAGIC
);
938 p
->packet
->version
= cpu_to_be32(MULTIFD_VERSION
);
939 p
->name
= g_strdup_printf("multifdsend_%d", i
);
940 /* We need one extra place for the packet header */
941 p
->iov
= g_new0(struct iovec
, page_count
+ 1);
942 p
->normal
= g_new0(ram_addr_t
, page_count
);
943 p
->page_size
= qemu_target_page_size();
944 p
->page_count
= page_count
;
946 if (migrate_zero_copy_send()) {
947 p
->write_flags
= QIO_CHANNEL_WRITE_FLAG_ZERO_COPY
;
952 multifd_new_send_channel_create(p
);
955 for (i
= 0; i
< thread_count
; i
++) {
956 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
957 Error
*local_err
= NULL
;
960 ret
= multifd_send_state
->ops
->send_setup(p
, &local_err
);
962 error_propagate(errp
, local_err
);
970 MultiFDRecvParams
*params
;
971 /* number of created threads */
973 /* syncs main thread and channels */
974 QemuSemaphore sem_sync
;
975 /* global number of generated multifd packets */
979 } *multifd_recv_state
;
981 static void multifd_recv_terminate_threads(Error
*err
)
985 trace_multifd_recv_terminate_threads(err
!= NULL
);
988 MigrationState
*s
= migrate_get_current();
989 migrate_set_error(s
, err
);
990 if (s
->state
== MIGRATION_STATUS_SETUP
||
991 s
->state
== MIGRATION_STATUS_ACTIVE
) {
992 migrate_set_state(&s
->state
, s
->state
,
993 MIGRATION_STATUS_FAILED
);
997 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
998 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1000 qemu_mutex_lock(&p
->mutex
);
1003 * We could arrive here for two reasons:
1004 * - normal quit, i.e. everything went fine, just finished
1005 * - error quit: We close the channels so the channel threads
1006 * finish the qio_channel_read_all_eof()
1009 qio_channel_shutdown(p
->c
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
1011 qemu_mutex_unlock(&p
->mutex
);
1015 void multifd_load_shutdown(void)
1017 if (migrate_multifd()) {
1018 multifd_recv_terminate_threads(NULL
);
1022 void multifd_load_cleanup(void)
1026 if (!migrate_multifd()) {
1029 multifd_recv_terminate_threads(NULL
);
1030 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1031 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1035 * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
1036 * however try to wakeup it without harm in cleanup phase.
1038 qemu_sem_post(&p
->sem_sync
);
1041 qemu_thread_join(&p
->thread
);
1043 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1044 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1046 migration_ioc_unregister_yank(p
->c
);
1047 object_unref(OBJECT(p
->c
));
1049 qemu_mutex_destroy(&p
->mutex
);
1050 qemu_sem_destroy(&p
->sem_sync
);
1060 multifd_recv_state
->ops
->recv_cleanup(p
);
1062 qemu_sem_destroy(&multifd_recv_state
->sem_sync
);
1063 g_free(multifd_recv_state
->params
);
1064 multifd_recv_state
->params
= NULL
;
1065 g_free(multifd_recv_state
);
1066 multifd_recv_state
= NULL
;
1069 void multifd_recv_sync_main(void)
1073 if (!migrate_multifd()) {
1076 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1077 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1079 trace_multifd_recv_sync_main_wait(p
->id
);
1080 qemu_sem_wait(&multifd_recv_state
->sem_sync
);
1082 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1083 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1085 WITH_QEMU_LOCK_GUARD(&p
->mutex
) {
1086 if (multifd_recv_state
->packet_num
< p
->packet_num
) {
1087 multifd_recv_state
->packet_num
= p
->packet_num
;
1090 trace_multifd_recv_sync_main_signal(p
->id
);
1091 qemu_sem_post(&p
->sem_sync
);
1093 trace_multifd_recv_sync_main(multifd_recv_state
->packet_num
);
1096 static void *multifd_recv_thread(void *opaque
)
1098 MultiFDRecvParams
*p
= opaque
;
1099 Error
*local_err
= NULL
;
1102 trace_multifd_recv_thread_start(p
->id
);
1103 rcu_register_thread();
1112 ret
= qio_channel_read_all_eof(p
->c
, (void *)p
->packet
,
1113 p
->packet_len
, &local_err
);
1114 if (ret
== 0 || ret
== -1) { /* 0: EOF -1: Error */
1118 qemu_mutex_lock(&p
->mutex
);
1119 ret
= multifd_recv_unfill_packet(p
, &local_err
);
1121 qemu_mutex_unlock(&p
->mutex
);
1126 /* recv methods don't know how to handle the SYNC flag */
1127 p
->flags
&= ~MULTIFD_FLAG_SYNC
;
1128 trace_multifd_recv(p
->id
, p
->packet_num
, p
->normal_num
, flags
,
1129 p
->next_packet_size
);
1131 p
->total_normal_pages
+= p
->normal_num
;
1132 qemu_mutex_unlock(&p
->mutex
);
1134 if (p
->normal_num
) {
1135 ret
= multifd_recv_state
->ops
->recv_pages(p
, &local_err
);
1141 if (flags
& MULTIFD_FLAG_SYNC
) {
1142 qemu_sem_post(&multifd_recv_state
->sem_sync
);
1143 qemu_sem_wait(&p
->sem_sync
);
1148 multifd_recv_terminate_threads(local_err
);
1149 error_free(local_err
);
1151 qemu_mutex_lock(&p
->mutex
);
1153 qemu_mutex_unlock(&p
->mutex
);
1155 rcu_unregister_thread();
1156 trace_multifd_recv_thread_end(p
->id
, p
->num_packets
, p
->total_normal_pages
);
1161 int multifd_load_setup(Error
**errp
)
1164 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
1168 * Return successfully if multiFD recv state is already initialised
1169 * or multiFD is not enabled.
1171 if (multifd_recv_state
|| !migrate_multifd()) {
1175 thread_count
= migrate_multifd_channels();
1176 multifd_recv_state
= g_malloc0(sizeof(*multifd_recv_state
));
1177 multifd_recv_state
->params
= g_new0(MultiFDRecvParams
, thread_count
);
1178 qatomic_set(&multifd_recv_state
->count
, 0);
1179 qemu_sem_init(&multifd_recv_state
->sem_sync
, 0);
1180 multifd_recv_state
->ops
= multifd_ops
[migrate_multifd_compression()];
1182 for (i
= 0; i
< thread_count
; i
++) {
1183 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1185 qemu_mutex_init(&p
->mutex
);
1186 qemu_sem_init(&p
->sem_sync
, 0);
1189 p
->packet_len
= sizeof(MultiFDPacket_t
)
1190 + sizeof(uint64_t) * page_count
;
1191 p
->packet
= g_malloc0(p
->packet_len
);
1192 p
->name
= g_strdup_printf("multifdrecv_%d", i
);
1193 p
->iov
= g_new0(struct iovec
, page_count
);
1194 p
->normal
= g_new0(ram_addr_t
, page_count
);
1195 p
->page_count
= page_count
;
1196 p
->page_size
= qemu_target_page_size();
1199 for (i
= 0; i
< thread_count
; i
++) {
1200 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1201 Error
*local_err
= NULL
;
1204 ret
= multifd_recv_state
->ops
->recv_setup(p
, &local_err
);
1206 error_propagate(errp
, local_err
);
1213 bool multifd_recv_all_channels_created(void)
1215 int thread_count
= migrate_multifd_channels();
1217 if (!migrate_multifd()) {
1221 if (!multifd_recv_state
) {
1222 /* Called before any connections created */
1226 return thread_count
== qatomic_read(&multifd_recv_state
->count
);
1230 * Try to receive all multifd channels to get ready for the migration.
1231 * Sets @errp when failing to receive the current channel.
1233 void multifd_recv_new_channel(QIOChannel
*ioc
, Error
**errp
)
1235 MultiFDRecvParams
*p
;
1236 Error
*local_err
= NULL
;
1239 id
= multifd_recv_initial_packet(ioc
, &local_err
);
1241 multifd_recv_terminate_threads(local_err
);
1242 error_propagate_prepend(errp
, local_err
,
1243 "failed to receive packet"
1244 " via multifd channel %d: ",
1245 qatomic_read(&multifd_recv_state
->count
));
1248 trace_multifd_recv_new_channel(id
);
1250 p
= &multifd_recv_state
->params
[id
];
1252 error_setg(&local_err
, "multifd: received id '%d' already setup'",
1254 multifd_recv_terminate_threads(local_err
);
1255 error_propagate(errp
, local_err
);
1259 object_ref(OBJECT(ioc
));
1260 /* initial packet */
1264 qemu_thread_create(&p
->thread
, p
->name
, multifd_recv_thread
, p
,
1265 QEMU_THREAD_JOINABLE
);
1266 qatomic_inc(&multifd_recv_state
->count
);