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
);
194 static int multifd_recv_initial_packet(QIOChannel
*c
, Error
**errp
)
199 ret
= qio_channel_read_all(c
, (char *)&msg
, sizeof(msg
), errp
);
204 msg
.magic
= be32_to_cpu(msg
.magic
);
205 msg
.version
= be32_to_cpu(msg
.version
);
207 if (msg
.magic
!= MULTIFD_MAGIC
) {
208 error_setg(errp
, "multifd: received packet magic %x "
209 "expected %x", msg
.magic
, MULTIFD_MAGIC
);
213 if (msg
.version
!= MULTIFD_VERSION
) {
214 error_setg(errp
, "multifd: received packet version %u "
215 "expected %u", msg
.version
, MULTIFD_VERSION
);
219 if (memcmp(msg
.uuid
, &qemu_uuid
, sizeof(qemu_uuid
))) {
220 char *uuid
= qemu_uuid_unparse_strdup(&qemu_uuid
);
221 char *msg_uuid
= qemu_uuid_unparse_strdup((const QemuUUID
*)msg
.uuid
);
223 error_setg(errp
, "multifd: received uuid '%s' and expected "
224 "uuid '%s' for channel %hhd", msg_uuid
, uuid
, msg
.id
);
230 if (msg
.id
> migrate_multifd_channels()) {
231 error_setg(errp
, "multifd: received channel version %u "
232 "expected %u", msg
.version
, MULTIFD_VERSION
);
239 static MultiFDPages_t
*multifd_pages_init(size_t size
)
241 MultiFDPages_t
*pages
= g_new0(MultiFDPages_t
, 1);
243 pages
->allocated
= size
;
244 pages
->offset
= g_new0(ram_addr_t
, size
);
249 static void multifd_pages_clear(MultiFDPages_t
*pages
)
252 pages
->allocated
= 0;
253 pages
->packet_num
= 0;
255 g_free(pages
->offset
);
256 pages
->offset
= NULL
;
260 static void multifd_send_fill_packet(MultiFDSendParams
*p
)
262 MultiFDPacket_t
*packet
= p
->packet
;
265 packet
->flags
= cpu_to_be32(p
->flags
);
266 packet
->pages_alloc
= cpu_to_be32(p
->pages
->allocated
);
267 packet
->normal_pages
= cpu_to_be32(p
->normal_num
);
268 packet
->next_packet_size
= cpu_to_be32(p
->next_packet_size
);
269 packet
->packet_num
= cpu_to_be64(p
->packet_num
);
271 if (p
->pages
->block
) {
272 strncpy(packet
->ramblock
, p
->pages
->block
->idstr
, 256);
275 for (i
= 0; i
< p
->normal_num
; i
++) {
276 /* there are architectures where ram_addr_t is 32 bit */
277 uint64_t temp
= p
->normal
[i
];
279 packet
->offset
[i
] = cpu_to_be64(temp
);
283 static int multifd_recv_unfill_packet(MultiFDRecvParams
*p
, Error
**errp
)
285 MultiFDPacket_t
*packet
= p
->packet
;
288 packet
->magic
= be32_to_cpu(packet
->magic
);
289 if (packet
->magic
!= MULTIFD_MAGIC
) {
290 error_setg(errp
, "multifd: received packet "
291 "magic %x and expected magic %x",
292 packet
->magic
, MULTIFD_MAGIC
);
296 packet
->version
= be32_to_cpu(packet
->version
);
297 if (packet
->version
!= MULTIFD_VERSION
) {
298 error_setg(errp
, "multifd: received packet "
299 "version %u and expected version %u",
300 packet
->version
, MULTIFD_VERSION
);
304 p
->flags
= be32_to_cpu(packet
->flags
);
306 packet
->pages_alloc
= be32_to_cpu(packet
->pages_alloc
);
308 * If we received a packet that is 100 times bigger than expected
309 * just stop migration. It is a magic number.
311 if (packet
->pages_alloc
> p
->page_count
) {
312 error_setg(errp
, "multifd: received packet "
313 "with size %u and expected a size of %u",
314 packet
->pages_alloc
, p
->page_count
) ;
318 p
->normal_num
= be32_to_cpu(packet
->normal_pages
);
319 if (p
->normal_num
> packet
->pages_alloc
) {
320 error_setg(errp
, "multifd: received packet "
321 "with %u pages and expected maximum pages are %u",
322 p
->normal_num
, packet
->pages_alloc
) ;
326 p
->next_packet_size
= be32_to_cpu(packet
->next_packet_size
);
327 p
->packet_num
= be64_to_cpu(packet
->packet_num
);
329 if (p
->normal_num
== 0) {
333 /* make sure that ramblock is 0 terminated */
334 packet
->ramblock
[255] = 0;
335 p
->block
= qemu_ram_block_by_name(packet
->ramblock
);
337 error_setg(errp
, "multifd: unknown ram block %s",
342 p
->host
= p
->block
->host
;
343 for (i
= 0; i
< p
->normal_num
; i
++) {
344 uint64_t offset
= be64_to_cpu(packet
->offset
[i
]);
346 if (offset
> (p
->block
->used_length
- p
->page_size
)) {
347 error_setg(errp
, "multifd: offset too long %" PRIu64
348 " (max " RAM_ADDR_FMT
")",
349 offset
, p
->block
->used_length
);
352 p
->normal
[i
] = offset
;
359 MultiFDSendParams
*params
;
360 /* array of pages to sent */
361 MultiFDPages_t
*pages
;
362 /* global number of generated multifd packets */
364 /* send channels ready */
365 QemuSemaphore channels_ready
;
367 * Have we already run terminate threads. There is a race when it
368 * happens that we got one error while we are exiting.
369 * We will use atomic operations. Only valid values are 0 and 1.
374 } *multifd_send_state
;
377 * How we use multifd_send_state->pages and channel->pages?
379 * We create a pages for each channel, and a main one. Each time that
380 * we need to send a batch of pages we interchange the ones between
381 * multifd_send_state and the channel that is sending it. There are
382 * two reasons for that:
383 * - to not have to do so many mallocs during migration
384 * - to make easier to know what to free at the end of migration
386 * This way we always know who is the owner of each "pages" struct,
387 * and we don't need any locking. It belongs to the migration thread
388 * or to the channel thread. Switching is safe because the migration
389 * thread is using the channel mutex when changing it, and the channel
390 * have to had finish with its own, otherwise pending_job can't be
394 static int multifd_send_pages(QEMUFile
*f
)
397 static int next_channel
;
398 MultiFDSendParams
*p
= NULL
; /* make happy gcc */
399 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
401 if (qatomic_read(&multifd_send_state
->exiting
)) {
405 qemu_sem_wait(&multifd_send_state
->channels_ready
);
407 * next_channel can remain from a previous migration that was
408 * using more channels, so ensure it doesn't overflow if the
409 * limit is lower now.
411 next_channel
%= migrate_multifd_channels();
412 for (i
= next_channel
;; i
= (i
+ 1) % migrate_multifd_channels()) {
413 p
= &multifd_send_state
->params
[i
];
415 qemu_mutex_lock(&p
->mutex
);
417 error_report("%s: channel %d has already quit!", __func__
, i
);
418 qemu_mutex_unlock(&p
->mutex
);
421 if (!p
->pending_job
) {
423 next_channel
= (i
+ 1) % migrate_multifd_channels();
426 qemu_mutex_unlock(&p
->mutex
);
428 assert(!p
->pages
->num
);
429 assert(!p
->pages
->block
);
431 p
->packet_num
= multifd_send_state
->packet_num
++;
432 multifd_send_state
->pages
= p
->pages
;
434 qemu_mutex_unlock(&p
->mutex
);
435 qemu_sem_post(&p
->sem
);
440 int multifd_queue_page(QEMUFile
*f
, RAMBlock
*block
, ram_addr_t offset
)
442 MultiFDPages_t
*pages
= multifd_send_state
->pages
;
443 bool changed
= false;
446 pages
->block
= block
;
449 if (pages
->block
== block
) {
450 pages
->offset
[pages
->num
] = offset
;
453 if (pages
->num
< pages
->allocated
) {
460 if (multifd_send_pages(f
) < 0) {
465 return multifd_queue_page(f
, block
, offset
);
471 static void multifd_send_terminate_threads(Error
*err
)
475 trace_multifd_send_terminate_threads(err
!= NULL
);
478 MigrationState
*s
= migrate_get_current();
479 migrate_set_error(s
, err
);
480 if (s
->state
== MIGRATION_STATUS_SETUP
||
481 s
->state
== MIGRATION_STATUS_PRE_SWITCHOVER
||
482 s
->state
== MIGRATION_STATUS_DEVICE
||
483 s
->state
== MIGRATION_STATUS_ACTIVE
) {
484 migrate_set_state(&s
->state
, s
->state
,
485 MIGRATION_STATUS_FAILED
);
490 * We don't want to exit each threads twice. Depending on where
491 * we get the error, or if there are two independent errors in two
492 * threads at the same time, we can end calling this function
495 if (qatomic_xchg(&multifd_send_state
->exiting
, 1)) {
499 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
500 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
502 qemu_mutex_lock(&p
->mutex
);
504 qemu_sem_post(&p
->sem
);
506 qio_channel_shutdown(p
->c
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
508 qemu_mutex_unlock(&p
->mutex
);
512 static int multifd_send_channel_destroy(QIOChannel
*send
)
514 return socket_send_channel_destroy(send
);
517 void multifd_save_cleanup(void)
521 if (!migrate_multifd()) {
524 multifd_send_terminate_threads(NULL
);
525 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
526 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
529 qemu_thread_join(&p
->thread
);
532 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
533 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
534 Error
*local_err
= NULL
;
536 if (p
->registered_yank
) {
537 migration_ioc_unregister_yank(p
->c
);
539 multifd_send_channel_destroy(p
->c
);
541 qemu_mutex_destroy(&p
->mutex
);
542 qemu_sem_destroy(&p
->sem
);
543 qemu_sem_destroy(&p
->sem_sync
);
546 multifd_pages_clear(p
->pages
);
555 multifd_send_state
->ops
->send_cleanup(p
, &local_err
);
557 migrate_set_error(migrate_get_current(), local_err
);
558 error_free(local_err
);
561 qemu_sem_destroy(&multifd_send_state
->channels_ready
);
562 g_free(multifd_send_state
->params
);
563 multifd_send_state
->params
= NULL
;
564 multifd_pages_clear(multifd_send_state
->pages
);
565 multifd_send_state
->pages
= NULL
;
566 g_free(multifd_send_state
);
567 multifd_send_state
= NULL
;
570 static int multifd_zero_copy_flush(QIOChannel
*c
)
575 ret
= qio_channel_flush(c
, &err
);
577 error_report_err(err
);
581 stat64_add(&mig_stats
.dirty_sync_missed_zero_copy
, 1);
587 int multifd_send_sync_main(QEMUFile
*f
)
590 bool flush_zero_copy
;
592 if (!migrate_multifd()) {
595 if (multifd_send_state
->pages
->num
) {
596 if (multifd_send_pages(f
) < 0) {
597 error_report("%s: multifd_send_pages fail", __func__
);
603 * When using zero-copy, it's necessary to flush the pages before any of
604 * the pages can be sent again, so we'll make sure the new version of the
605 * pages will always arrive _later_ than the old pages.
607 * Currently we achieve this by flushing the zero-page requested writes
608 * per ram iteration, but in the future we could potentially optimize it
609 * to be less frequent, e.g. only after we finished one whole scanning of
610 * all the dirty bitmaps.
613 flush_zero_copy
= migrate_zero_copy_send();
615 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
616 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
618 trace_multifd_send_sync_main_signal(p
->id
);
620 qemu_mutex_lock(&p
->mutex
);
623 error_report("%s: channel %d has already quit", __func__
, i
);
624 qemu_mutex_unlock(&p
->mutex
);
628 p
->packet_num
= multifd_send_state
->packet_num
++;
629 p
->flags
|= MULTIFD_FLAG_SYNC
;
631 qemu_mutex_unlock(&p
->mutex
);
632 qemu_sem_post(&p
->sem
);
634 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
635 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
637 qemu_sem_wait(&multifd_send_state
->channels_ready
);
638 trace_multifd_send_sync_main_wait(p
->id
);
639 qemu_sem_wait(&p
->sem_sync
);
641 if (flush_zero_copy
&& p
->c
&& (multifd_zero_copy_flush(p
->c
) < 0)) {
645 trace_multifd_send_sync_main(multifd_send_state
->packet_num
);
650 static void *multifd_send_thread(void *opaque
)
652 MultiFDSendParams
*p
= opaque
;
653 MigrationThread
*thread
= NULL
;
654 Error
*local_err
= NULL
;
656 bool use_zero_copy_send
= migrate_zero_copy_send();
658 thread
= migration_threads_add(p
->name
, qemu_get_thread_id());
660 trace_multifd_send_thread_start(p
->id
);
661 rcu_register_thread();
663 if (multifd_send_initial_packet(p
, &local_err
) < 0) {
671 qemu_sem_post(&multifd_send_state
->channels_ready
);
672 qemu_sem_wait(&p
->sem
);
674 if (qatomic_read(&multifd_send_state
->exiting
)) {
677 qemu_mutex_lock(&p
->mutex
);
679 if (p
->pending_job
) {
680 uint64_t packet_num
= p
->packet_num
;
684 if (use_zero_copy_send
) {
690 for (int i
= 0; i
< p
->pages
->num
; i
++) {
691 p
->normal
[p
->normal_num
] = p
->pages
->offset
[i
];
696 ret
= multifd_send_state
->ops
->send_prepare(p
, &local_err
);
698 qemu_mutex_unlock(&p
->mutex
);
702 multifd_send_fill_packet(p
);
706 p
->total_normal_pages
+= p
->normal_num
;
708 p
->pages
->block
= NULL
;
709 qemu_mutex_unlock(&p
->mutex
);
711 trace_multifd_send(p
->id
, packet_num
, p
->normal_num
, flags
,
712 p
->next_packet_size
);
714 if (use_zero_copy_send
) {
715 /* Send header first, without zerocopy */
716 ret
= qio_channel_write_all(p
->c
, (void *)p
->packet
,
717 p
->packet_len
, &local_err
);
722 /* Send header using the same writev call */
723 p
->iov
[0].iov_len
= p
->packet_len
;
724 p
->iov
[0].iov_base
= p
->packet
;
727 ret
= qio_channel_writev_full_all(p
->c
, p
->iov
, p
->iovs_num
, NULL
,
728 0, p
->write_flags
, &local_err
);
733 stat64_add(&mig_stats
.multifd_bytes
,
734 p
->next_packet_size
+ p
->packet_len
);
735 p
->next_packet_size
= 0;
736 qemu_mutex_lock(&p
->mutex
);
738 qemu_mutex_unlock(&p
->mutex
);
740 if (flags
& MULTIFD_FLAG_SYNC
) {
741 qemu_sem_post(&p
->sem_sync
);
744 qemu_mutex_unlock(&p
->mutex
);
745 /* sometimes there are spurious wakeups */
752 trace_multifd_send_error(p
->id
);
753 multifd_send_terminate_threads(local_err
);
754 qemu_sem_post(&p
->sem_sync
);
755 qemu_sem_post(&multifd_send_state
->channels_ready
);
756 error_free(local_err
);
759 qemu_mutex_lock(&p
->mutex
);
761 qemu_mutex_unlock(&p
->mutex
);
763 rcu_unregister_thread();
764 migration_threads_remove(thread
);
765 trace_multifd_send_thread_end(p
->id
, p
->num_packets
, p
->total_normal_pages
);
770 static bool multifd_channel_connect(MultiFDSendParams
*p
,
774 static void multifd_tls_outgoing_handshake(QIOTask
*task
,
777 MultiFDSendParams
*p
= opaque
;
778 QIOChannel
*ioc
= QIO_CHANNEL(qio_task_get_source(task
));
781 if (!qio_task_propagate_error(task
, &err
)) {
782 trace_multifd_tls_outgoing_handshake_complete(ioc
);
783 if (multifd_channel_connect(p
, ioc
, &err
)) {
788 trace_multifd_tls_outgoing_handshake_error(ioc
, error_get_pretty(err
));
791 * Error happen, mark multifd_send_thread status as 'quit' although it
792 * is not created, and then tell who pay attention to me.
795 qemu_sem_post(&multifd_send_state
->channels_ready
);
796 qemu_sem_post(&p
->sem_sync
);
799 static void *multifd_tls_handshake_thread(void *opaque
)
801 MultiFDSendParams
*p
= opaque
;
802 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(p
->c
);
804 qio_channel_tls_handshake(tioc
,
805 multifd_tls_outgoing_handshake
,
812 static bool multifd_tls_channel_connect(MultiFDSendParams
*p
,
816 MigrationState
*s
= migrate_get_current();
817 const char *hostname
= s
->hostname
;
820 tioc
= migration_tls_client_create(ioc
, hostname
, errp
);
825 object_unref(OBJECT(ioc
));
826 trace_multifd_tls_outgoing_handshake_start(ioc
, tioc
, hostname
);
827 qio_channel_set_name(QIO_CHANNEL(tioc
), "multifd-tls-outgoing");
828 p
->c
= QIO_CHANNEL(tioc
);
829 qemu_thread_create(&p
->thread
, "multifd-tls-handshake-worker",
830 multifd_tls_handshake_thread
, p
,
831 QEMU_THREAD_JOINABLE
);
835 static bool multifd_channel_connect(MultiFDSendParams
*p
,
839 trace_multifd_set_outgoing_channel(
840 ioc
, object_get_typename(OBJECT(ioc
)),
841 migrate_get_current()->hostname
);
843 if (migrate_channel_requires_tls_upgrade(ioc
)) {
845 * tls_channel_connect will call back to this
846 * function after the TLS handshake,
847 * so we mustn't call multifd_send_thread until then
849 return multifd_tls_channel_connect(p
, ioc
, errp
);
852 migration_ioc_register_yank(ioc
);
853 p
->registered_yank
= true;
855 qemu_thread_create(&p
->thread
, p
->name
, multifd_send_thread
, p
,
856 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 need 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
*ioc
= 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
)) {
887 qio_channel_set_delay(p
->c
, false);
889 if (multifd_channel_connect(p
, ioc
, &local_err
)) {
894 trace_multifd_new_send_channel_async_error(p
->id
, local_err
);
895 multifd_new_send_channel_cleanup(p
, ioc
, local_err
);
898 static void multifd_new_send_channel_create(gpointer opaque
)
900 socket_send_channel_create(multifd_new_send_channel_async
, opaque
);
903 int multifd_save_setup(Error
**errp
)
906 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
909 if (!migrate_multifd()) {
913 thread_count
= migrate_multifd_channels();
914 multifd_send_state
= g_malloc0(sizeof(*multifd_send_state
));
915 multifd_send_state
->params
= g_new0(MultiFDSendParams
, thread_count
);
916 multifd_send_state
->pages
= multifd_pages_init(page_count
);
917 qemu_sem_init(&multifd_send_state
->channels_ready
, 0);
918 qatomic_set(&multifd_send_state
->exiting
, 0);
919 multifd_send_state
->ops
= multifd_ops
[migrate_multifd_compression()];
921 for (i
= 0; i
< thread_count
; i
++) {
922 MultiFDSendParams
*p
= &multifd_send_state
->params
[i
];
924 qemu_mutex_init(&p
->mutex
);
925 qemu_sem_init(&p
->sem
, 0);
926 qemu_sem_init(&p
->sem_sync
, 0);
930 p
->pages
= multifd_pages_init(page_count
);
931 p
->packet_len
= sizeof(MultiFDPacket_t
)
932 + sizeof(uint64_t) * page_count
;
933 p
->packet
= g_malloc0(p
->packet_len
);
934 p
->packet
->magic
= cpu_to_be32(MULTIFD_MAGIC
);
935 p
->packet
->version
= cpu_to_be32(MULTIFD_VERSION
);
936 p
->name
= g_strdup_printf("multifdsend_%d", i
);
937 /* We need one extra place for the packet header */
938 p
->iov
= g_new0(struct iovec
, page_count
+ 1);
939 p
->normal
= g_new0(ram_addr_t
, page_count
);
940 p
->page_size
= qemu_target_page_size();
941 p
->page_count
= page_count
;
943 if (migrate_zero_copy_send()) {
944 p
->write_flags
= QIO_CHANNEL_WRITE_FLAG_ZERO_COPY
;
949 multifd_new_send_channel_create(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 void multifd_load_shutdown(void)
1014 if (migrate_multifd()) {
1015 multifd_recv_terminate_threads(NULL
);
1019 void multifd_load_cleanup(void)
1023 if (!migrate_multifd()) {
1026 multifd_recv_terminate_threads(NULL
);
1027 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1028 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1032 * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
1033 * however try to wakeup it without harm in cleanup phase.
1035 qemu_sem_post(&p
->sem_sync
);
1038 qemu_thread_join(&p
->thread
);
1040 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1041 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1043 migration_ioc_unregister_yank(p
->c
);
1044 object_unref(OBJECT(p
->c
));
1046 qemu_mutex_destroy(&p
->mutex
);
1047 qemu_sem_destroy(&p
->sem_sync
);
1057 multifd_recv_state
->ops
->recv_cleanup(p
);
1059 qemu_sem_destroy(&multifd_recv_state
->sem_sync
);
1060 g_free(multifd_recv_state
->params
);
1061 multifd_recv_state
->params
= NULL
;
1062 g_free(multifd_recv_state
);
1063 multifd_recv_state
= NULL
;
1066 void multifd_recv_sync_main(void)
1070 if (!migrate_multifd()) {
1073 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1074 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1076 trace_multifd_recv_sync_main_wait(p
->id
);
1077 qemu_sem_wait(&multifd_recv_state
->sem_sync
);
1079 for (i
= 0; i
< migrate_multifd_channels(); i
++) {
1080 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1082 WITH_QEMU_LOCK_GUARD(&p
->mutex
) {
1083 if (multifd_recv_state
->packet_num
< p
->packet_num
) {
1084 multifd_recv_state
->packet_num
= p
->packet_num
;
1087 trace_multifd_recv_sync_main_signal(p
->id
);
1088 qemu_sem_post(&p
->sem_sync
);
1090 trace_multifd_recv_sync_main(multifd_recv_state
->packet_num
);
1093 static void *multifd_recv_thread(void *opaque
)
1095 MultiFDRecvParams
*p
= opaque
;
1096 Error
*local_err
= NULL
;
1099 trace_multifd_recv_thread_start(p
->id
);
1100 rcu_register_thread();
1109 ret
= qio_channel_read_all_eof(p
->c
, (void *)p
->packet
,
1110 p
->packet_len
, &local_err
);
1111 if (ret
== 0 || ret
== -1) { /* 0: EOF -1: Error */
1115 qemu_mutex_lock(&p
->mutex
);
1116 ret
= multifd_recv_unfill_packet(p
, &local_err
);
1118 qemu_mutex_unlock(&p
->mutex
);
1123 /* recv methods don't know how to handle the SYNC flag */
1124 p
->flags
&= ~MULTIFD_FLAG_SYNC
;
1125 trace_multifd_recv(p
->id
, p
->packet_num
, p
->normal_num
, flags
,
1126 p
->next_packet_size
);
1128 p
->total_normal_pages
+= p
->normal_num
;
1129 qemu_mutex_unlock(&p
->mutex
);
1131 if (p
->normal_num
) {
1132 ret
= multifd_recv_state
->ops
->recv_pages(p
, &local_err
);
1138 if (flags
& MULTIFD_FLAG_SYNC
) {
1139 qemu_sem_post(&multifd_recv_state
->sem_sync
);
1140 qemu_sem_wait(&p
->sem_sync
);
1145 multifd_recv_terminate_threads(local_err
);
1146 error_free(local_err
);
1148 qemu_mutex_lock(&p
->mutex
);
1150 qemu_mutex_unlock(&p
->mutex
);
1152 rcu_unregister_thread();
1153 trace_multifd_recv_thread_end(p
->id
, p
->num_packets
, p
->total_normal_pages
);
1158 int multifd_load_setup(Error
**errp
)
1161 uint32_t page_count
= MULTIFD_PACKET_SIZE
/ qemu_target_page_size();
1165 * Return successfully if multiFD recv state is already initialised
1166 * or multiFD is not enabled.
1168 if (multifd_recv_state
|| !migrate_multifd()) {
1172 thread_count
= migrate_multifd_channels();
1173 multifd_recv_state
= g_malloc0(sizeof(*multifd_recv_state
));
1174 multifd_recv_state
->params
= g_new0(MultiFDRecvParams
, thread_count
);
1175 qatomic_set(&multifd_recv_state
->count
, 0);
1176 qemu_sem_init(&multifd_recv_state
->sem_sync
, 0);
1177 multifd_recv_state
->ops
= multifd_ops
[migrate_multifd_compression()];
1179 for (i
= 0; i
< thread_count
; i
++) {
1180 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1182 qemu_mutex_init(&p
->mutex
);
1183 qemu_sem_init(&p
->sem_sync
, 0);
1186 p
->packet_len
= sizeof(MultiFDPacket_t
)
1187 + sizeof(uint64_t) * page_count
;
1188 p
->packet
= g_malloc0(p
->packet_len
);
1189 p
->name
= g_strdup_printf("multifdrecv_%d", i
);
1190 p
->iov
= g_new0(struct iovec
, page_count
);
1191 p
->normal
= g_new0(ram_addr_t
, page_count
);
1192 p
->page_count
= page_count
;
1193 p
->page_size
= qemu_target_page_size();
1196 for (i
= 0; i
< thread_count
; i
++) {
1197 MultiFDRecvParams
*p
= &multifd_recv_state
->params
[i
];
1198 Error
*local_err
= NULL
;
1201 ret
= multifd_recv_state
->ops
->recv_setup(p
, &local_err
);
1203 error_propagate(errp
, local_err
);
1210 bool multifd_recv_all_channels_created(void)
1212 int thread_count
= migrate_multifd_channels();
1214 if (!migrate_multifd()) {
1218 if (!multifd_recv_state
) {
1219 /* Called before any connections created */
1223 return thread_count
== qatomic_read(&multifd_recv_state
->count
);
1227 * Try to receive all multifd channels to get ready for the migration.
1228 * Sets @errp when failing to receive the current channel.
1230 void multifd_recv_new_channel(QIOChannel
*ioc
, Error
**errp
)
1232 MultiFDRecvParams
*p
;
1233 Error
*local_err
= NULL
;
1236 id
= multifd_recv_initial_packet(ioc
, &local_err
);
1238 multifd_recv_terminate_threads(local_err
);
1239 error_propagate_prepend(errp
, local_err
,
1240 "failed to receive packet"
1241 " via multifd channel %d: ",
1242 qatomic_read(&multifd_recv_state
->count
));
1245 trace_multifd_recv_new_channel(id
);
1247 p
= &multifd_recv_state
->params
[id
];
1249 error_setg(&local_err
, "multifd: received id '%d' already setup'",
1251 multifd_recv_terminate_threads(local_err
);
1252 error_propagate(errp
, local_err
);
1256 object_ref(OBJECT(ioc
));
1257 /* initial packet */
1261 qemu_thread_create(&p
->thread
, p
->name
, multifd_recv_thread
, p
,
1262 QEMU_THREAD_JOINABLE
);
1263 qatomic_inc(&multifd_recv_state
->count
);