hw/input/stellaris_input: Convert to qdev
[qemu/ar7.git] / migration / multifd.c
blobec58c58082db35a884de5254d89b8e97bd91e45c
1 /*
2 * Multifd common code
4 * Copyright (c) 2019-2020 Red Hat Inc
6 * Authors:
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"
14 #include "qemu/rcu.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"
20 #include "ram.h"
21 #include "migration.h"
22 #include "migration-stats.h"
23 #include "socket.h"
24 #include "tls.h"
25 #include "qemu-file.h"
26 #include "trace.h"
27 #include "multifd.h"
28 #include "threadinfo.h"
29 #include "options.h"
30 #include "qemu/yank.h"
31 #include "io/channel-socket.h"
32 #include "yank_functions.h"
34 /* Multiple fd's */
36 #define MULTIFD_MAGIC 0x11223344U
37 #define MULTIFD_VERSION 1
39 typedef struct {
40 uint32_t magic;
41 uint32_t version;
42 unsigned char uuid[16]; /* QemuUUID */
43 uint8_t id;
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 */
50 /**
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)
62 return 0;
65 /**
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)
75 return;
78 /**
79 * nocomp_send_prepare: prepare date to be able to send
81 * For no compression we just have to calculate the size of the
82 * packet.
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;
96 p->iovs_num++;
99 p->next_packet_size = p->normal_num * p->page_size;
100 p->flags |= MULTIFD_FLAG_NOCOMP;
101 return 0;
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)
116 return 0;
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);
147 return -1;
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);
179 int ret;
181 msg.magic = cpu_to_be32(MULTIFD_MAGIC);
182 msg.version = cpu_to_be32(MULTIFD_VERSION);
183 msg.id = p->id;
184 memcpy(msg.uuid, &qemu_uuid.data, sizeof(msg.uuid));
186 ret = qio_channel_write_all(p->c, (char *)&msg, size, errp);
187 if (ret != 0) {
188 return -1;
190 stat64_add(&mig_stats.multifd_bytes, size);
191 return 0;
194 static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
196 MultiFDInit_t msg;
197 int ret;
199 ret = qio_channel_read_all(c, (char *)&msg, sizeof(msg), errp);
200 if (ret != 0) {
201 return -1;
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);
210 return -1;
213 if (msg.version != MULTIFD_VERSION) {
214 error_setg(errp, "multifd: received packet version %u "
215 "expected %u", msg.version, MULTIFD_VERSION);
216 return -1;
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);
225 g_free(uuid);
226 g_free(msg_uuid);
227 return -1;
230 if (msg.id > migrate_multifd_channels()) {
231 error_setg(errp, "multifd: received channel version %u "
232 "expected %u", msg.version, MULTIFD_VERSION);
233 return -1;
236 return msg.id;
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);
246 return pages;
249 static void multifd_pages_clear(MultiFDPages_t *pages)
251 pages->num = 0;
252 pages->allocated = 0;
253 pages->packet_num = 0;
254 pages->block = NULL;
255 g_free(pages->offset);
256 pages->offset = NULL;
257 g_free(pages);
260 static void multifd_send_fill_packet(MultiFDSendParams *p)
262 MultiFDPacket_t *packet = p->packet;
263 int i;
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;
286 int i;
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);
293 return -1;
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);
301 return -1;
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) ;
315 return -1;
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) ;
323 return -1;
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) {
330 return 0;
333 /* make sure that ramblock is 0 terminated */
334 packet->ramblock[255] = 0;
335 p->block = qemu_ram_block_by_name(packet->ramblock);
336 if (!p->block) {
337 error_setg(errp, "multifd: unknown ram block %s",
338 packet->ramblock);
339 return -1;
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);
350 return -1;
352 p->normal[i] = offset;
355 return 0;
358 struct {
359 MultiFDSendParams *params;
360 /* array of pages to sent */
361 MultiFDPages_t *pages;
362 /* global number of generated multifd packets */
363 uint64_t packet_num;
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.
371 int exiting;
372 /* multifd ops */
373 MultiFDMethods *ops;
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
391 * false.
394 static int multifd_send_pages(QEMUFile *f)
396 int i;
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)) {
402 return -1;
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);
416 if (p->quit) {
417 error_report("%s: channel %d has already quit!", __func__, i);
418 qemu_mutex_unlock(&p->mutex);
419 return -1;
421 if (!p->pending_job) {
422 p->pending_job++;
423 next_channel = (i + 1) % migrate_multifd_channels();
424 break;
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;
433 p->pages = pages;
434 qemu_mutex_unlock(&p->mutex);
435 qemu_sem_post(&p->sem);
437 return 1;
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;
445 if (!pages->block) {
446 pages->block = block;
449 if (pages->block == block) {
450 pages->offset[pages->num] = offset;
451 pages->num++;
453 if (pages->num < pages->allocated) {
454 return 1;
456 } else {
457 changed = true;
460 if (multifd_send_pages(f) < 0) {
461 return -1;
464 if (changed) {
465 return multifd_queue_page(f, block, offset);
468 return 1;
471 static void multifd_send_terminate_threads(Error *err)
473 int i;
475 trace_multifd_send_terminate_threads(err != NULL);
477 if (err) {
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
493 * twice.
495 if (qatomic_xchg(&multifd_send_state->exiting, 1)) {
496 return;
499 for (i = 0; i < migrate_multifd_channels(); i++) {
500 MultiFDSendParams *p = &multifd_send_state->params[i];
502 qemu_mutex_lock(&p->mutex);
503 p->quit = true;
504 qemu_sem_post(&p->sem);
505 if (p->c) {
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)
519 int i;
521 if (!migrate_multifd()) {
522 return;
524 multifd_send_terminate_threads(NULL);
525 for (i = 0; i < migrate_multifd_channels(); i++) {
526 MultiFDSendParams *p = &multifd_send_state->params[i];
528 if (p->running) {
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);
540 p->c = NULL;
541 qemu_mutex_destroy(&p->mutex);
542 qemu_sem_destroy(&p->sem);
543 qemu_sem_destroy(&p->sem_sync);
544 g_free(p->name);
545 p->name = NULL;
546 multifd_pages_clear(p->pages);
547 p->pages = NULL;
548 p->packet_len = 0;
549 g_free(p->packet);
550 p->packet = NULL;
551 g_free(p->iov);
552 p->iov = NULL;
553 g_free(p->normal);
554 p->normal = NULL;
555 multifd_send_state->ops->send_cleanup(p, &local_err);
556 if (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)
572 int ret;
573 Error *err = NULL;
575 ret = qio_channel_flush(c, &err);
576 if (ret < 0) {
577 error_report_err(err);
578 return -1;
580 if (ret == 1) {
581 stat64_add(&mig_stats.dirty_sync_missed_zero_copy, 1);
584 return ret;
587 int multifd_send_sync_main(QEMUFile *f)
589 int i;
590 bool flush_zero_copy;
592 if (!migrate_multifd()) {
593 return 0;
595 if (multifd_send_state->pages->num) {
596 if (multifd_send_pages(f) < 0) {
597 error_report("%s: multifd_send_pages fail", __func__);
598 return -1;
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);
622 if (p->quit) {
623 error_report("%s: channel %d has already quit", __func__, i);
624 qemu_mutex_unlock(&p->mutex);
625 return -1;
628 p->packet_num = multifd_send_state->packet_num++;
629 p->flags |= MULTIFD_FLAG_SYNC;
630 p->pending_job++;
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)) {
642 return -1;
645 trace_multifd_send_sync_main(multifd_send_state->packet_num);
647 return 0;
650 static void *multifd_send_thread(void *opaque)
652 MultiFDSendParams *p = opaque;
653 MigrationThread *thread = NULL;
654 Error *local_err = NULL;
655 int ret = 0;
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) {
664 ret = -1;
665 goto out;
667 /* initial packet */
668 p->num_packets = 1;
670 while (true) {
671 qemu_sem_post(&multifd_send_state->channels_ready);
672 qemu_sem_wait(&p->sem);
674 if (qatomic_read(&multifd_send_state->exiting)) {
675 break;
677 qemu_mutex_lock(&p->mutex);
679 if (p->pending_job) {
680 uint64_t packet_num = p->packet_num;
681 uint32_t flags;
682 p->normal_num = 0;
684 if (use_zero_copy_send) {
685 p->iovs_num = 0;
686 } else {
687 p->iovs_num = 1;
690 for (int i = 0; i < p->pages->num; i++) {
691 p->normal[p->normal_num] = p->pages->offset[i];
692 p->normal_num++;
695 if (p->normal_num) {
696 ret = multifd_send_state->ops->send_prepare(p, &local_err);
697 if (ret != 0) {
698 qemu_mutex_unlock(&p->mutex);
699 break;
702 multifd_send_fill_packet(p);
703 flags = p->flags;
704 p->flags = 0;
705 p->num_packets++;
706 p->total_normal_pages += p->normal_num;
707 p->pages->num = 0;
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);
718 if (ret != 0) {
719 break;
721 } else {
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);
729 if (ret != 0) {
730 break;
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);
737 p->pending_job--;
738 qemu_mutex_unlock(&p->mutex);
740 if (flags & MULTIFD_FLAG_SYNC) {
741 qemu_sem_post(&p->sem_sync);
743 } else {
744 qemu_mutex_unlock(&p->mutex);
745 /* sometimes there are spurious wakeups */
749 out:
750 if (ret) {
751 assert(local_err);
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);
760 p->running = false;
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);
767 return NULL;
770 static bool multifd_channel_connect(MultiFDSendParams *p,
771 QIOChannel *ioc,
772 Error **errp);
774 static void multifd_tls_outgoing_handshake(QIOTask *task,
775 gpointer opaque)
777 MultiFDSendParams *p = opaque;
778 QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
779 Error *err = NULL;
781 if (!qio_task_propagate_error(task, &err)) {
782 trace_multifd_tls_outgoing_handshake_complete(ioc);
783 if (multifd_channel_connect(p, ioc, &err)) {
784 return;
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.
794 p->quit = true;
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,
807 NULL,
808 NULL);
809 return NULL;
812 static bool multifd_tls_channel_connect(MultiFDSendParams *p,
813 QIOChannel *ioc,
814 Error **errp)
816 MigrationState *s = migrate_get_current();
817 const char *hostname = s->hostname;
818 QIOChannelTLS *tioc;
820 tioc = migration_tls_client_create(ioc, hostname, errp);
821 if (!tioc) {
822 return false;
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);
832 return true;
835 static bool multifd_channel_connect(MultiFDSendParams *p,
836 QIOChannel *ioc,
837 Error **errp)
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);
851 } else {
852 migration_ioc_register_yank(ioc);
853 p->registered_yank = true;
854 p->c = ioc;
855 qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
856 QEMU_THREAD_JOINABLE);
858 return true;
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
871 * its status.
873 p->quit = true;
874 object_unref(OBJECT(ioc));
875 error_free(err);
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)) {
886 p->c = ioc;
887 qio_channel_set_delay(p->c, false);
888 p->running = true;
889 if (multifd_channel_connect(p, ioc, &local_err)) {
890 return;
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)
905 int thread_count;
906 uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
907 uint8_t i;
909 if (!migrate_multifd()) {
910 return 0;
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);
927 p->quit = false;
928 p->pending_job = 0;
929 p->id = i;
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;
945 } else {
946 p->write_flags = 0;
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;
955 int ret;
957 ret = multifd_send_state->ops->send_setup(p, &local_err);
958 if (ret) {
959 error_propagate(errp, local_err);
960 return ret;
963 return 0;
966 struct {
967 MultiFDRecvParams *params;
968 /* number of created threads */
969 int count;
970 /* syncs main thread and channels */
971 QemuSemaphore sem_sync;
972 /* global number of generated multifd packets */
973 uint64_t packet_num;
974 /* multifd ops */
975 MultiFDMethods *ops;
976 } *multifd_recv_state;
978 static void multifd_recv_terminate_threads(Error *err)
980 int i;
982 trace_multifd_recv_terminate_threads(err != NULL);
984 if (err) {
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);
998 p->quit = true;
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()
1005 if (p->c) {
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)
1021 int i;
1023 if (!migrate_multifd()) {
1024 return;
1026 multifd_recv_terminate_threads(NULL);
1027 for (i = 0; i < migrate_multifd_channels(); i++) {
1028 MultiFDRecvParams *p = &multifd_recv_state->params[i];
1030 if (p->running) {
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));
1045 p->c = NULL;
1046 qemu_mutex_destroy(&p->mutex);
1047 qemu_sem_destroy(&p->sem_sync);
1048 g_free(p->name);
1049 p->name = NULL;
1050 p->packet_len = 0;
1051 g_free(p->packet);
1052 p->packet = NULL;
1053 g_free(p->iov);
1054 p->iov = NULL;
1055 g_free(p->normal);
1056 p->normal = NULL;
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)
1068 int i;
1070 if (!migrate_multifd()) {
1071 return;
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;
1097 int ret;
1099 trace_multifd_recv_thread_start(p->id);
1100 rcu_register_thread();
1102 while (true) {
1103 uint32_t flags;
1105 if (p->quit) {
1106 break;
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 */
1112 break;
1115 qemu_mutex_lock(&p->mutex);
1116 ret = multifd_recv_unfill_packet(p, &local_err);
1117 if (ret) {
1118 qemu_mutex_unlock(&p->mutex);
1119 break;
1122 flags = p->flags;
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);
1127 p->num_packets++;
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);
1133 if (ret != 0) {
1134 break;
1138 if (flags & MULTIFD_FLAG_SYNC) {
1139 qemu_sem_post(&multifd_recv_state->sem_sync);
1140 qemu_sem_wait(&p->sem_sync);
1144 if (local_err) {
1145 multifd_recv_terminate_threads(local_err);
1146 error_free(local_err);
1148 qemu_mutex_lock(&p->mutex);
1149 p->running = false;
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);
1155 return NULL;
1158 int multifd_load_setup(Error **errp)
1160 int thread_count;
1161 uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
1162 uint8_t i;
1165 * Return successfully if multiFD recv state is already initialised
1166 * or multiFD is not enabled.
1168 if (multifd_recv_state || !migrate_multifd()) {
1169 return 0;
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);
1184 p->quit = false;
1185 p->id = i;
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;
1199 int ret;
1201 ret = multifd_recv_state->ops->recv_setup(p, &local_err);
1202 if (ret) {
1203 error_propagate(errp, local_err);
1204 return ret;
1207 return 0;
1210 bool multifd_recv_all_channels_created(void)
1212 int thread_count = migrate_multifd_channels();
1214 if (!migrate_multifd()) {
1215 return true;
1218 if (!multifd_recv_state) {
1219 /* Called before any connections created */
1220 return false;
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;
1234 int id;
1236 id = multifd_recv_initial_packet(ioc, &local_err);
1237 if (id < 0) {
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));
1243 return;
1245 trace_multifd_recv_new_channel(id);
1247 p = &multifd_recv_state->params[id];
1248 if (p->c != NULL) {
1249 error_setg(&local_err, "multifd: received id '%d' already setup'",
1250 id);
1251 multifd_recv_terminate_threads(local_err);
1252 error_propagate(errp, local_err);
1253 return;
1255 p->c = ioc;
1256 object_ref(OBJECT(ioc));
1257 /* initial packet */
1258 p->num_packets = 1;
1260 p->running = true;
1261 qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p,
1262 QEMU_THREAD_JOINABLE);
1263 qatomic_inc(&multifd_recv_state->count);