4 * Copyright (c) 2019-2020 Red Hat Inc
7 * Juan Quintela <quintela@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "exec/target_page.h"
16 #include "sysemu/sysemu.h"
17 #include "exec/ramblock.h"
18 #include "qemu/error-report.h"
19 #include "qapi/error.h"
21 #include "migration.h"
24 #include "qemu-file.h"
28 #include "qemu/yank.h"
29 #include "io/channel-socket.h"
30 #include "yank_functions.h"
34 #define MULTIFD_MAGIC 0x11223344U
35 #define MULTIFD_VERSION 1
40 unsigned char uuid
[16]; /* QemuUUID */
42 uint8_t unused1
[7]; /* Reserved for future use */
43 uint64_t unused2
[4]; /* Reserved for future use */
44 } __attribute__((packed
)) MultiFDInit_t
;
46 /* Multifd without compression */
49 * nocomp_send_setup: setup send side
51 * For no compression this function does nothing.
53 * Returns 0 for success or -1 for error
55 * @p: Params for the channel that we are using
56 * @errp: pointer to an error
58 static int nocomp_send_setup(MultiFDSendParams
*p
, Error
**errp
)
64 * nocomp_send_cleanup: cleanup send side
66 * For no compression this function does nothing.
68 * @p: Params for the channel that we are using
69 * @errp: pointer to an error
71 static void nocomp_send_cleanup(MultiFDSendParams
*p
, Error
**errp
)
77 * nocomp_send_prepare: prepare date to be able to send
79 * For no compression we just have to calculate the size of the
82 * Returns 0 for success or -1 for error
84 * @p: Params for the channel that we are using
85 * @errp: pointer to an error
87 static int nocomp_send_prepare(MultiFDSendParams
*p
, Error
**errp
)
89 MultiFDPages_t
*pages
= p
->pages
;
90 size_t page_size
= qemu_target_page_size();
92 for (int i
= 0; i
< p
->normal_num
; i
++) {
93 p
->iov
[p
->iovs_num
].iov_base
= pages
->block
->host
+ p
->normal
[i
];
94 p
->iov
[p
->iovs_num
].iov_len
= page_size
;
98 p
->next_packet_size
= p
->normal_num
* page_size
;
99 p
->flags
|= MULTIFD_FLAG_NOCOMP
;
104 * nocomp_recv_setup: setup receive side
106 * For no compression this function does nothing.
108 * Returns 0 for success or -1 for error
110 * @p: Params for the channel that we are using
111 * @errp: pointer to an error
113 static int nocomp_recv_setup(MultiFDRecvParams
*p
, Error
**errp
)
119 * nocomp_recv_cleanup: setup receive side
121 * For no compression this function does nothing.
123 * @p: Params for the channel that we are using
125 static void nocomp_recv_cleanup(MultiFDRecvParams
*p
)
130 * nocomp_recv_pages: read the data from the channel into actual pages
132 * For no compression we just need to read things into the correct place.
134 * Returns 0 for success or -1 for error
136 * @p: Params for the channel that we are using
137 * @errp: pointer to an error
139 static int nocomp_recv_pages(MultiFDRecvParams
*p
, Error
**errp
)
141 uint32_t flags
= p
->flags
& MULTIFD_FLAG_COMPRESSION_MASK
;
142 size_t page_size
= qemu_target_page_size();
144 if (flags
!= MULTIFD_FLAG_NOCOMP
) {
145 error_setg(errp
, "multifd %u: flags received %x flags expected %x",
146 p
->id
, flags
, MULTIFD_FLAG_NOCOMP
);
149 for (int i
= 0; i
< p
->normal_num
; i
++) {
150 p
->iov
[i
].iov_base
= p
->host
+ p
->normal
[i
];
151 p
->iov
[i
].iov_len
= 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
= {};
180 msg
.magic
= cpu_to_be32(MULTIFD_MAGIC
);
181 msg
.version
= cpu_to_be32(MULTIFD_VERSION
);
183 memcpy(msg
.uuid
, &qemu_uuid
.data
, sizeof(msg
.uuid
));
185 ret
= qio_channel_write_all(p
->c
, (char *)&msg
, sizeof(msg
), errp
);
192 static int multifd_recv_initial_packet(QIOChannel
*c
, Error
**errp
)
197 ret
= qio_channel_read_all(c
, (char *)&msg
, sizeof(msg
), errp
);
202 msg
.magic
= be32_to_cpu(msg
.magic
);
203 msg
.version
= be32_to_cpu(msg
.version
);
205 if (msg
.magic
!= MULTIFD_MAGIC
) {
206 error_setg(errp
, "multifd: received packet magic %x "
207 "expected %x", msg
.magic
, MULTIFD_MAGIC
);
211 if (msg
.version
!= MULTIFD_VERSION
) {
212 error_setg(errp
, "multifd: received packet version %u "
213 "expected %u", msg
.version
, MULTIFD_VERSION
);
217 if (memcmp(msg
.uuid
, &qemu_uuid
, sizeof(qemu_uuid
))) {
218 char *uuid
= qemu_uuid_unparse_strdup(&qemu_uuid
);
219 char *msg_uuid
= qemu_uuid_unparse_strdup((const QemuUUID
*)msg
.uuid
);
221 error_setg(errp
, "multifd: received uuid '%s' and expected "
222 "uuid '%s' for channel %hhd", msg_uuid
, uuid
, msg
.id
);
228 if (msg
.id
> migrate_multifd_channels()) {
229 error_setg(errp
, "multifd: received channel version %u "
230 "expected %u", msg
.version
, MULTIFD_VERSION
);
237 static MultiFDPages_t
*multifd_pages_init(size_t size
)
239 MultiFDPages_t
*pages
= g_new0(MultiFDPages_t
, 1);
241 pages
->allocated
= size
;
242 pages
->offset
= g_new0(ram_addr_t
, size
);
247 static void multifd_pages_clear(MultiFDPages_t
*pages
)
250 pages
->allocated
= 0;
251 pages
->packet_num
= 0;
253 g_free(pages
->offset
);
254 pages
->offset
= NULL
;
258 static void multifd_send_fill_packet(MultiFDSendParams
*p
)
260 MultiFDPacket_t
*packet
= p
->packet
;
263 packet
->flags
= cpu_to_be32(p
->flags
);
264 packet
->pages_alloc
= cpu_to_be32(p
->pages
->allocated
);
265 packet
->normal_pages
= cpu_to_be32(p
->normal_num
);
266 packet
->next_packet_size
= cpu_to_be32(p
->next_packet_size
);
267 packet
->packet_num
= cpu_to_be64(p
->packet_num
);
269 if (p
->pages
->block
) {
270 strncpy(packet
->ramblock
, p
->pages
->block
->idstr
, 256);
273 for (i
= 0; i
< p
->normal_num
; i
++) {
274 /* there are architectures where ram_addr_t is 32 bit */
275 uint64_t temp
= p
->normal
[i
];
277 packet
->offset
[i
] = cpu_to_be64(temp
);
281 static int multifd_recv_unfill_packet(MultiFDRecvParams
*p
, Error
**errp
)
283 MultiFDPacket_t
*packet
= p
->packet
;
284 size_t page_size
= qemu_target_page_size();
285 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ page_size
;
289 packet
->magic
= be32_to_cpu(packet
->magic
);
290 if (packet
->magic
!= MULTIFD_MAGIC
) {
291 error_setg(errp
, "multifd: received packet "
292 "magic %x and expected magic %x",
293 packet
->magic
, MULTIFD_MAGIC
);
297 packet
->version
= be32_to_cpu(packet
->version
);
298 if (packet
->version
!= MULTIFD_VERSION
) {
299 error_setg(errp
, "multifd: received packet "
300 "version %u and expected version %u",
301 packet
->version
, MULTIFD_VERSION
);
305 p
->flags
= be32_to_cpu(packet
->flags
);
307 packet
->pages_alloc
= be32_to_cpu(packet
->pages_alloc
);
309 * If we received a packet that is 100 times bigger than expected
310 * just stop migration. It is a magic number.
312 if (packet
->pages_alloc
> page_count
) {
313 error_setg(errp
, "multifd: received packet "
314 "with size %u and expected a size of %u",
315 packet
->pages_alloc
, 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 block
= qemu_ram_block_by_name(packet
->ramblock
);
338 error_setg(errp
, "multifd: unknown ram block %s",
343 p
->host
= block
->host
;
344 for (i
= 0; i
< p
->normal_num
; i
++) {
345 uint64_t offset
= be64_to_cpu(packet
->offset
[i
]);
347 if (offset
> (block
->used_length
- page_size
)) {
348 error_setg(errp
, "multifd: offset too long %" PRIu64
349 " (max " RAM_ADDR_FMT
")",
350 offset
, 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
;
401 uint64_t transferred
;
403 if (qatomic_read(&multifd_send_state
->exiting
)) {
407 qemu_sem_wait(&multifd_send_state
->channels_ready
);
409 * next_channel can remain from a previous migration that was
410 * using more channels, so ensure it doesn't overflow if the
411 * limit is lower now.
413 next_channel
%= migrate_multifd_channels();
414 for (i
= next_channel
;; i
= (i
+ 1) % migrate_multifd_channels()) {
415 p
= &multifd_send_state
->params
[i
];
417 qemu_mutex_lock(&p
->mutex
);
419 error_report("%s: channel %d has already quit!", __func__
, i
);
420 qemu_mutex_unlock(&p
->mutex
);
423 if (!p
->pending_job
) {
425 next_channel
= (i
+ 1) % migrate_multifd_channels();
428 qemu_mutex_unlock(&p
->mutex
);
430 assert(!p
->pages
->num
);
431 assert(!p
->pages
->block
);
433 p
->packet_num
= multifd_send_state
->packet_num
++;
434 multifd_send_state
->pages
= p
->pages
;
436 transferred
= ((uint64_t) pages
->num
) * qemu_target_page_size()
438 qemu_file_update_transfer(f
, transferred
);
439 ram_counters
.multifd_bytes
+= transferred
;
440 ram_counters
.transferred
+= transferred
;
441 qemu_mutex_unlock(&p
->mutex
);
442 qemu_sem_post(&p
->sem
);
447 int multifd_queue_page(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
)
449 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
452 pages
->block
= block
;
455 if (pages
->block
== block
) {
456 pages
->offset
[pages
->num
] = offset
;
459 if (pages
->num
< pages
->allocated
) {
464 if (multifd_send_pages(f
) < 0) {
468 if (pages
->block
!= block
) {
469 return multifd_queue_page(f
, block
, offset
);
475 static void multifd_send_terminate_threads(Error
*err
)
479 trace_multifd_send_terminate_threads(err
!= NULL
);
482 MigrationState
*s
= migrate_get_current();
483 migrate_set_error(s
, err
);
484 if (s
->state
== MIGRATION_STATUS_SETUP
||
485 s
->state
== MIGRATION_STATUS_PRE_SWITCHOVER
||
486 s
->state
== MIGRATION_STATUS_DEVICE
||
487 s
->state
== MIGRATION_STATUS_ACTIVE
) {
488 migrate_set_state(&s
->state
, s
->state
,
489 MIGRATION_STATUS_FAILED
);
494 * We don't want to exit each threads twice. Depending on where
495 * we get the error, or if there are two independent errors in two
496 * threads at the same time, we can end calling this function
499 if (qatomic_xchg(&multifd_send_state
->exiting
, 1)) {
503 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
504 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
506 qemu_mutex_lock(&p
->mutex
);
508 qemu_sem_post(&p
->sem
);
510 qio_channel_shutdown(p
->c
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
512 qemu_mutex_unlock(&p
->mutex
);
516 void multifd_save_cleanup(void)
520 if (!migrate_use_multifd() || !migrate_multi_channels_is_allowed()) {
523 multifd_send_terminate_threads(NULL
);
524 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
525 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
528 qemu_thread_join(&p
->thread
);
531 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
532 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
533 Error
*local_err
= NULL
;
535 if (p
->registered_yank
) {
536 migration_ioc_unregister_yank(p
->c
);
538 socket_send_channel_destroy(p
->c
);
540 qemu_mutex_destroy(&p
->mutex
);
541 qemu_sem_destroy(&p
->sem
);
542 qemu_sem_destroy(&p
->sem_sync
);
545 multifd_pages_clear(p
->pages
);
554 multifd_send_state
->ops
->send_cleanup(p
, &local_err
);
556 migrate_set_error(migrate_get_current(), local_err
);
557 error_free(local_err
);
560 qemu_sem_destroy(&multifd_send_state
->channels_ready
);
561 g_free(multifd_send_state
->params
);
562 multifd_send_state
->params
= NULL
;
563 multifd_pages_clear(multifd_send_state
->pages
);
564 multifd_send_state
->pages
= NULL
;
565 g_free(multifd_send_state
);
566 multifd_send_state
= NULL
;
569 int multifd_send_sync_main(QEMUFile
*f
)
572 bool flush_zero_copy
;
574 if (!migrate_use_multifd()) {
577 if (multifd_send_state
->pages
->num
) {
578 if (multifd_send_pages(f
) < 0) {
579 error_report("%s: multifd_send_pages fail", __func__
);
585 * When using zero-copy, it's necessary to flush the pages before any of
586 * the pages can be sent again, so we'll make sure the new version of the
587 * pages will always arrive _later_ than the old pages.
589 * Currently we achieve this by flushing the zero-page requested writes
590 * per ram iteration, but in the future we could potentially optimize it
591 * to be less frequent, e.g. only after we finished one whole scanning of
592 * all the dirty bitmaps.
595 flush_zero_copy
= migrate_use_zero_copy_send();
597 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
598 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
600 trace_multifd_send_sync_main_signal(p
->id
);
602 qemu_mutex_lock(&p
->mutex
);
605 error_report("%s: channel %d has already quit", __func__
, i
);
606 qemu_mutex_unlock(&p
->mutex
);
610 p
->packet_num
= multifd_send_state
->packet_num
++;
611 p
->flags
|= MULTIFD_FLAG_SYNC
;
613 qemu_file_update_transfer(f
, p
->packet_len
);
614 ram_counters
.multifd_bytes
+= p
->packet_len
;
615 ram_counters
.transferred
+= p
->packet_len
;
616 qemu_mutex_unlock(&p
->mutex
);
617 qemu_sem_post(&p
->sem
);
619 if (flush_zero_copy
&& p
->c
) {
623 ret
= qio_channel_flush(p
->c
, &err
);
625 error_report_err(err
);
630 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
631 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
633 trace_multifd_send_sync_main_wait(p
->id
);
634 qemu_sem_wait(&p
->sem_sync
);
636 trace_multifd_send_sync_main(multifd_send_state
->packet_num
);
641 static void *multifd_send_thread(void *opaque
)
643 MultiFDSendParams
*p
= opaque
;
644 Error
*local_err
= NULL
;
646 bool use_zero_copy_send
= migrate_use_zero_copy_send();
648 trace_multifd_send_thread_start(p
->id
);
649 rcu_register_thread();
651 if (multifd_send_initial_packet(p
, &local_err
) < 0) {
659 qemu_sem_wait(&p
->sem
);
661 if (qatomic_read(&multifd_send_state
->exiting
)) {
664 qemu_mutex_lock(&p
->mutex
);
666 if (p
->pending_job
) {
667 uint64_t packet_num
= p
->packet_num
;
668 uint32_t flags
= p
->flags
;
671 if (use_zero_copy_send
) {
677 for (int i
= 0; i
< p
->pages
->num
; i
++) {
678 p
->normal
[p
->normal_num
] = p
->pages
->offset
[i
];
683 ret
= multifd_send_state
->ops
->send_prepare(p
, &local_err
);
685 qemu_mutex_unlock(&p
->mutex
);
689 multifd_send_fill_packet(p
);
692 p
->total_normal_pages
+= p
->normal_num
;
694 p
->pages
->block
= NULL
;
695 qemu_mutex_unlock(&p
->mutex
);
697 trace_multifd_send(p
->id
, packet_num
, p
->normal_num
, flags
,
698 p
->next_packet_size
);
700 if (use_zero_copy_send
) {
701 /* Send header first, without zerocopy */
702 ret
= qio_channel_write_all(p
->c
, (void *)p
->packet
,
703 p
->packet_len
, &local_err
);
708 /* Send header using the same writev call */
709 p
->iov
[0].iov_len
= p
->packet_len
;
710 p
->iov
[0].iov_base
= p
->packet
;
713 ret
= qio_channel_writev_full_all(p
->c
, p
->iov
, p
->iovs_num
, NULL
,
714 0, p
->write_flags
, &local_err
);
719 qemu_mutex_lock(&p
->mutex
);
721 qemu_mutex_unlock(&p
->mutex
);
723 if (flags
& MULTIFD_FLAG_SYNC
) {
724 qemu_sem_post(&p
->sem_sync
);
726 qemu_sem_post(&multifd_send_state
->channels_ready
);
727 } else if (p
->quit
) {
728 qemu_mutex_unlock(&p
->mutex
);
731 qemu_mutex_unlock(&p
->mutex
);
732 /* sometimes there are spurious wakeups */
738 trace_multifd_send_error(p
->id
);
739 multifd_send_terminate_threads(local_err
);
740 error_free(local_err
);
744 * Error happen, I will exit, but I can't just leave, tell
745 * who pay attention to me.
748 qemu_sem_post(&p
->sem_sync
);
749 qemu_sem_post(&multifd_send_state
->channels_ready
);
752 qemu_mutex_lock(&p
->mutex
);
754 qemu_mutex_unlock(&p
->mutex
);
756 rcu_unregister_thread();
757 trace_multifd_send_thread_end(p
->id
, p
->num_packets
, p
->total_normal_pages
);
762 static bool multifd_channel_connect(MultiFDSendParams
*p
,
766 static void multifd_tls_outgoing_handshake(QIOTask
*task
,
769 MultiFDSendParams
*p
= opaque
;
770 QIOChannel
*ioc
= QIO_CHANNEL(qio_task_get_source(task
));
773 if (qio_task_propagate_error(task
, &err
)) {
774 trace_multifd_tls_outgoing_handshake_error(ioc
, error_get_pretty(err
));
776 trace_multifd_tls_outgoing_handshake_complete(ioc
);
779 if (!multifd_channel_connect(p
, ioc
, err
)) {
781 * Error happen, mark multifd_send_thread status as 'quit' although it
782 * is not created, and then tell who pay attention to me.
785 qemu_sem_post(&multifd_send_state
->channels_ready
);
786 qemu_sem_post(&p
->sem_sync
);
790 static void *multifd_tls_handshake_thread(void *opaque
)
792 MultiFDSendParams
*p
= opaque
;
793 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(p
->c
);
795 qio_channel_tls_handshake(tioc
,
796 multifd_tls_outgoing_handshake
,
803 static void multifd_tls_channel_connect(MultiFDSendParams
*p
,
807 MigrationState
*s
= migrate_get_current();
808 const char *hostname
= s
->hostname
;
811 tioc
= migration_tls_client_create(s
, ioc
, hostname
, errp
);
816 object_unref(OBJECT(ioc
));
817 trace_multifd_tls_outgoing_handshake_start(ioc
, tioc
, hostname
);
818 qio_channel_set_name(QIO_CHANNEL(tioc
), "multifd-tls-outgoing");
819 p
->c
= QIO_CHANNEL(tioc
);
820 qemu_thread_create(&p
->thread
, "multifd-tls-handshake-worker",
821 multifd_tls_handshake_thread
, p
,
822 QEMU_THREAD_JOINABLE
);
825 static bool multifd_channel_connect(MultiFDSendParams
*p
,
829 trace_multifd_set_outgoing_channel(
830 ioc
, object_get_typename(OBJECT(ioc
)),
831 migrate_get_current()->hostname
, error
);
834 if (migrate_use_tls() &&
835 !object_dynamic_cast(OBJECT(ioc
),
836 TYPE_QIO_CHANNEL_TLS
)) {
837 multifd_tls_channel_connect(p
, ioc
, &error
);
840 * tls_channel_connect will call back to this
841 * function after the TLS handshake,
842 * so we mustn't call multifd_send_thread until then
849 migration_ioc_register_yank(ioc
);
850 p
->registered_yank
= true;
852 qemu_thread_create(&p
->thread
, p
->name
, multifd_send_thread
, p
,
853 QEMU_THREAD_JOINABLE
);
861 static void multifd_new_send_channel_cleanup(MultiFDSendParams
*p
,
862 QIOChannel
*ioc
, Error
*err
)
864 migrate_set_error(migrate_get_current(), err
);
865 /* Error happen, we need to tell who pay attention to me */
866 qemu_sem_post(&multifd_send_state
->channels_ready
);
867 qemu_sem_post(&p
->sem_sync
);
869 * Although multifd_send_thread is not created, but main migration
870 * thread neet to judge whether it is running, so we need to mark
874 object_unref(OBJECT(ioc
));
878 static void multifd_new_send_channel_async(QIOTask
*task
, gpointer opaque
)
880 MultiFDSendParams
*p
= opaque
;
881 QIOChannel
*sioc
= QIO_CHANNEL(qio_task_get_source(task
));
882 Error
*local_err
= NULL
;
884 trace_multifd_new_send_channel_async(p
->id
);
885 if (qio_task_propagate_error(task
, &local_err
)) {
888 p
->c
= QIO_CHANNEL(sioc
);
889 qio_channel_set_delay(p
->c
, false);
891 if (!multifd_channel_connect(p
, sioc
, local_err
)) {
898 multifd_new_send_channel_cleanup(p
, sioc
, local_err
);
901 int multifd_save_setup(Error
**errp
)
904 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
907 if (!migrate_use_multifd()) {
910 if (!migrate_multi_channels_is_allowed()) {
911 error_setg(errp
, "multifd is not supported by current protocol");
915 thread_count
= migrate_multifd_channels();
916 multifd_send_state
= g_malloc0(sizeof(*multifd_send_state
));
917 multifd_send_state
->params
= g_new0(MultiFDSendParams
, thread_count
);
918 multifd_send_state
->pages
= multifd_pages_init(page_count
);
919 qemu_sem_init(&multifd_send_state
->channels_ready
, 0);
920 qatomic_set(&multifd_send_state
->exiting
, 0);
921 multifd_send_state
->ops
= multifd_ops
[migrate_multifd_compression()];
923 for (i
= 0; i
< thread_count
; i
++) {
924 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
926 qemu_mutex_init(&p
->mutex
);
927 qemu_sem_init(&p
->sem
, 0);
928 qemu_sem_init(&p
->sem_sync
, 0);
932 p
->pages
= multifd_pages_init(page_count
);
933 p
->packet_len
= sizeof(MultiFDPacket_t
)
934 + sizeof(uint64_t) * page_count
;
935 p
->packet
= g_malloc0(p
->packet_len
);
936 p
->packet
->magic
= cpu_to_be32(MULTIFD_MAGIC
);
937 p
->packet
->version
= cpu_to_be32(MULTIFD_VERSION
);
938 p
->name
= g_strdup_printf("multifdsend_%d", i
);
939 /* We need one extra place for the packet header */
940 p
->iov
= g_new0(struct iovec
, page_count
+ 1);
941 p
->normal
= g_new0(ram_addr_t
, page_count
);
943 if (migrate_use_zero_copy_send()) {
944 p
->write_flags
= QIO_CHANNEL_WRITE_FLAG_ZERO_COPY
;
949 socket_send_channel_create(multifd_new_send_channel_async
, p
);
952 for (i
= 0; i
< thread_count
; i
++) {
953 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
954 Error
*local_err
= NULL
;
957 ret
= multifd_send_state
->ops
->send_setup(p
, &local_err
);
959 error_propagate(errp
, local_err
);
967 MultiFDRecvParams
*params
;
968 /* number of created threads */
970 /* syncs main thread and channels */
971 QemuSemaphore sem_sync
;
972 /* global number of generated multifd packets */
976 } *multifd_recv_state
;
978 static void multifd_recv_terminate_threads(Error
*err
)
982 trace_multifd_recv_terminate_threads(err
!= NULL
);
985 MigrationState
*s
= migrate_get_current();
986 migrate_set_error(s
, err
);
987 if (s
->state
== MIGRATION_STATUS_SETUP
||
988 s
->state
== MIGRATION_STATUS_ACTIVE
) {
989 migrate_set_state(&s
->state
, s
->state
,
990 MIGRATION_STATUS_FAILED
);
994 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
995 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
997 qemu_mutex_lock(&p
->mutex
);
1000 * We could arrive here for two reasons:
1001 * - normal quit, i.e. everything went fine, just finished
1002 * - error quit: We close the channels so the channel threads
1003 * finish the qio_channel_read_all_eof()
1006 qio_channel_shutdown(p
->c
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
1008 qemu_mutex_unlock(&p
->mutex
);
1012 int multifd_load_cleanup(Error
**errp
)
1016 if (!migrate_use_multifd() || !migrate_multi_channels_is_allowed()) {
1019 multifd_recv_terminate_threads(NULL
);
1020 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1021 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1026 * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
1027 * however try to wakeup it without harm in cleanup phase.
1029 qemu_sem_post(&p
->sem_sync
);
1030 qemu_thread_join(&p
->thread
);
1033 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1034 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1036 migration_ioc_unregister_yank(p
->c
);
1037 object_unref(OBJECT(p
->c
));
1039 qemu_mutex_destroy(&p
->mutex
);
1040 qemu_sem_destroy(&p
->sem_sync
);
1050 multifd_recv_state
->ops
->recv_cleanup(p
);
1052 qemu_sem_destroy(&multifd_recv_state
->sem_sync
);
1053 g_free(multifd_recv_state
->params
);
1054 multifd_recv_state
->params
= NULL
;
1055 g_free(multifd_recv_state
);
1056 multifd_recv_state
= NULL
;
1061 void multifd_recv_sync_main(void)
1065 if (!migrate_use_multifd()) {
1068 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1069 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1071 trace_multifd_recv_sync_main_wait(p
->id
);
1072 qemu_sem_wait(&multifd_recv_state
->sem_sync
);
1074 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1075 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1077 WITH_QEMU_LOCK_GUARD(&p
->mutex
) {
1078 if (multifd_recv_state
->packet_num
< p
->packet_num
) {
1079 multifd_recv_state
->packet_num
= p
->packet_num
;
1082 trace_multifd_recv_sync_main_signal(p
->id
);
1083 qemu_sem_post(&p
->sem_sync
);
1085 trace_multifd_recv_sync_main(multifd_recv_state
->packet_num
);
1088 static void *multifd_recv_thread(void *opaque
)
1090 MultiFDRecvParams
*p
= opaque
;
1091 Error
*local_err
= NULL
;
1094 trace_multifd_recv_thread_start(p
->id
);
1095 rcu_register_thread();
1104 ret
= qio_channel_read_all_eof(p
->c
, (void *)p
->packet
,
1105 p
->packet_len
, &local_err
);
1106 if (ret
== 0) { /* EOF */
1109 if (ret
== -1) { /* Error */
1113 qemu_mutex_lock(&p
->mutex
);
1114 ret
= multifd_recv_unfill_packet(p
, &local_err
);
1116 qemu_mutex_unlock(&p
->mutex
);
1121 /* recv methods don't know how to handle the SYNC flag */
1122 p
->flags
&= ~MULTIFD_FLAG_SYNC
;
1123 trace_multifd_recv(p
->id
, p
->packet_num
, p
->normal_num
, flags
,
1124 p
->next_packet_size
);
1126 p
->total_normal_pages
+= p
->normal_num
;
1127 qemu_mutex_unlock(&p
->mutex
);
1129 if (p
->normal_num
) {
1130 ret
= multifd_recv_state
->ops
->recv_pages(p
, &local_err
);
1136 if (flags
& MULTIFD_FLAG_SYNC
) {
1137 qemu_sem_post(&multifd_recv_state
->sem_sync
);
1138 qemu_sem_wait(&p
->sem_sync
);
1143 multifd_recv_terminate_threads(local_err
);
1144 error_free(local_err
);
1146 qemu_mutex_lock(&p
->mutex
);
1148 qemu_mutex_unlock(&p
->mutex
);
1150 rcu_unregister_thread();
1151 trace_multifd_recv_thread_end(p
->id
, p
->num_packets
, p
->total_normal_pages
);
1156 int multifd_load_setup(Error
**errp
)
1159 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
1162 if (!migrate_use_multifd()) {
1165 if (!migrate_multi_channels_is_allowed()) {
1166 error_setg(errp
, "multifd is not supported by current protocol");
1169 thread_count
= migrate_multifd_channels();
1170 multifd_recv_state
= g_malloc0(sizeof(*multifd_recv_state
));
1171 multifd_recv_state
->params
= g_new0(MultiFDRecvParams
, thread_count
);
1172 qatomic_set(&multifd_recv_state
->count
, 0);
1173 qemu_sem_init(&multifd_recv_state
->sem_sync
, 0);
1174 multifd_recv_state
->ops
= multifd_ops
[migrate_multifd_compression()];
1176 for (i
= 0; i
< thread_count
; i
++) {
1177 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1179 qemu_mutex_init(&p
->mutex
);
1180 qemu_sem_init(&p
->sem_sync
, 0);
1183 p
->packet_len
= sizeof(MultiFDPacket_t
)
1184 + sizeof(uint64_t) * page_count
;
1185 p
->packet
= g_malloc0(p
->packet_len
);
1186 p
->name
= g_strdup_printf("multifdrecv_%d", i
);
1187 p
->iov
= g_new0(struct iovec
, page_count
);
1188 p
->normal
= g_new0(ram_addr_t
, page_count
);
1191 for (i
= 0; i
< thread_count
; i
++) {
1192 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1193 Error
*local_err
= NULL
;
1196 ret
= multifd_recv_state
->ops
->recv_setup(p
, &local_err
);
1198 error_propagate(errp
, local_err
);
1205 bool multifd_recv_all_channels_created(void)
1207 int thread_count
= migrate_multifd_channels();
1209 if (!migrate_use_multifd()) {
1213 if (!multifd_recv_state
) {
1214 /* Called before any connections created */
1218 return thread_count
== qatomic_read(&multifd_recv_state
->count
);
1222 * Try to receive all multifd channels to get ready for the migration.
1223 * - Return true and do not set @errp when correctly receiving all channels;
1224 * - Return false and do not set @errp when correctly receiving the current one;
1225 * - Return false and set @errp when failing to receive the current channel.
1227 bool multifd_recv_new_channel(QIOChannel
*ioc
, Error
**errp
)
1229 MultiFDRecvParams
*p
;
1230 Error
*local_err
= NULL
;
1233 id
= multifd_recv_initial_packet(ioc
, &local_err
);
1235 multifd_recv_terminate_threads(local_err
);
1236 error_propagate_prepend(errp
, local_err
,
1237 "failed to receive packet"
1238 " via multifd channel %d: ",
1239 qatomic_read(&multifd_recv_state
->count
));
1242 trace_multifd_recv_new_channel(id
);
1244 p
= &multifd_recv_state
->params
[id
];
1246 error_setg(&local_err
, "multifd: received id '%d' already setup'",
1248 multifd_recv_terminate_threads(local_err
);
1249 error_propagate(errp
, local_err
);
1253 object_ref(OBJECT(ioc
));
1254 /* initial packet */
1258 qemu_thread_create(&p
->thread
, p
->name
, multifd_recv_thread
, p
,
1259 QEMU_THREAD_JOINABLE
);
1260 qatomic_inc(&multifd_recv_state
->count
);
1261 return qatomic_read(&multifd_recv_state
->count
) ==
1262 migrate_multifd_channels();