]> git.proxmox.com Git - mirror_qemu.git/blob - migration/multifd.c
multifd: Make no compression operations into its own structure
[mirror_qemu.git] / migration / multifd.c
1 /*
2 * Multifd common code
3 *
4 * Copyright (c) 2019-2020 Red Hat Inc
5 *
6 * Authors:
7 * Juan Quintela <quintela@redhat.com>
8 *
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.
11 */
12
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 "socket.h"
23 #include "qemu-file.h"
24 #include "trace.h"
25 #include "multifd.h"
26
27 /* Multiple fd's */
28
29 #define MULTIFD_MAGIC 0x11223344U
30 #define MULTIFD_VERSION 1
31
32 typedef struct {
33 uint32_t magic;
34 uint32_t version;
35 unsigned char uuid[16]; /* QemuUUID */
36 uint8_t id;
37 uint8_t unused1[7]; /* Reserved for future use */
38 uint64_t unused2[4]; /* Reserved for future use */
39 } __attribute__((packed)) MultiFDInit_t;
40
41 /* Multifd without compression */
42
43 /**
44 * nocomp_send_setup: setup send side
45 *
46 * For no compression this function does nothing.
47 *
48 * Returns 0 for success or -1 for error
49 *
50 * @p: Params for the channel that we are using
51 * @errp: pointer to an error
52 */
53 static int nocomp_send_setup(MultiFDSendParams *p, Error **errp)
54 {
55 return 0;
56 }
57
58 /**
59 * nocomp_send_cleanup: cleanup send side
60 *
61 * For no compression this function does nothing.
62 *
63 * @p: Params for the channel that we are using
64 */
65 static void nocomp_send_cleanup(MultiFDSendParams *p, Error **errp)
66 {
67 return;
68 }
69
70 /**
71 * nocomp_send_prepare: prepare date to be able to send
72 *
73 * For no compression we just have to calculate the size of the
74 * packet.
75 *
76 * Returns 0 for success or -1 for error
77 *
78 * @p: Params for the channel that we are using
79 * @used: number of pages used
80 * @errp: pointer to an error
81 */
82 static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
83 Error **errp)
84 {
85 p->next_packet_size = used * qemu_target_page_size();
86 p->flags |= MULTIFD_FLAG_NOCOMP;
87 return 0;
88 }
89
90 /**
91 * nocomp_send_write: do the actual write of the data
92 *
93 * For no compression we just have to write the data.
94 *
95 * Returns 0 for success or -1 for error
96 *
97 * @p: Params for the channel that we are using
98 * @used: number of pages used
99 * @errp: pointer to an error
100 */
101 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
102 {
103 return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
104 }
105
106 /**
107 * nocomp_recv_setup: setup receive side
108 *
109 * For no compression this function does nothing.
110 *
111 * Returns 0 for success or -1 for error
112 *
113 * @p: Params for the channel that we are using
114 * @errp: pointer to an error
115 */
116 static int nocomp_recv_setup(MultiFDRecvParams *p, Error **errp)
117 {
118 return 0;
119 }
120
121 /**
122 * nocomp_recv_cleanup: setup receive side
123 *
124 * For no compression this function does nothing.
125 *
126 * @p: Params for the channel that we are using
127 */
128 static void nocomp_recv_cleanup(MultiFDRecvParams *p)
129 {
130 }
131
132 /**
133 * nocomp_recv_pages: read the data from the channel into actual pages
134 *
135 * For no compression we just need to read things into the correct place.
136 *
137 * Returns 0 for success or -1 for error
138 *
139 * @p: Params for the channel that we are using
140 * @used: number of pages used
141 * @errp: pointer to an error
142 */
143 static int nocomp_recv_pages(MultiFDRecvParams *p, uint32_t used, Error **errp)
144 {
145 uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
146
147 if (flags != MULTIFD_FLAG_NOCOMP) {
148 error_setg(errp, "multifd %d: flags received %x flags expected %x",
149 p->id, flags, MULTIFD_FLAG_NOCOMP);
150 return -1;
151 }
152 return qio_channel_readv_all(p->c, p->pages->iov, used, errp);
153 }
154
155 static MultiFDMethods multifd_nocomp_ops = {
156 .send_setup = nocomp_send_setup,
157 .send_cleanup = nocomp_send_cleanup,
158 .send_prepare = nocomp_send_prepare,
159 .send_write = nocomp_send_write,
160 .recv_setup = nocomp_recv_setup,
161 .recv_cleanup = nocomp_recv_cleanup,
162 .recv_pages = nocomp_recv_pages
163 };
164
165 static MultiFDMethods *multifd_ops[MULTIFD_COMPRESSION__MAX] = {
166 [MULTIFD_COMPRESSION_NONE] = &multifd_nocomp_ops,
167 };
168
169 static int multifd_send_initial_packet(MultiFDSendParams *p, Error **errp)
170 {
171 MultiFDInit_t msg = {};
172 int ret;
173
174 msg.magic = cpu_to_be32(MULTIFD_MAGIC);
175 msg.version = cpu_to_be32(MULTIFD_VERSION);
176 msg.id = p->id;
177 memcpy(msg.uuid, &qemu_uuid.data, sizeof(msg.uuid));
178
179 ret = qio_channel_write_all(p->c, (char *)&msg, sizeof(msg), errp);
180 if (ret != 0) {
181 return -1;
182 }
183 return 0;
184 }
185
186 static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
187 {
188 MultiFDInit_t msg;
189 int ret;
190
191 ret = qio_channel_read_all(c, (char *)&msg, sizeof(msg), errp);
192 if (ret != 0) {
193 return -1;
194 }
195
196 msg.magic = be32_to_cpu(msg.magic);
197 msg.version = be32_to_cpu(msg.version);
198
199 if (msg.magic != MULTIFD_MAGIC) {
200 error_setg(errp, "multifd: received packet magic %x "
201 "expected %x", msg.magic, MULTIFD_MAGIC);
202 return -1;
203 }
204
205 if (msg.version != MULTIFD_VERSION) {
206 error_setg(errp, "multifd: received packet version %d "
207 "expected %d", msg.version, MULTIFD_VERSION);
208 return -1;
209 }
210
211 if (memcmp(msg.uuid, &qemu_uuid, sizeof(qemu_uuid))) {
212 char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid);
213 char *msg_uuid = qemu_uuid_unparse_strdup((const QemuUUID *)msg.uuid);
214
215 error_setg(errp, "multifd: received uuid '%s' and expected "
216 "uuid '%s' for channel %hhd", msg_uuid, uuid, msg.id);
217 g_free(uuid);
218 g_free(msg_uuid);
219 return -1;
220 }
221
222 if (msg.id > migrate_multifd_channels()) {
223 error_setg(errp, "multifd: received channel version %d "
224 "expected %d", msg.version, MULTIFD_VERSION);
225 return -1;
226 }
227
228 return msg.id;
229 }
230
231 static MultiFDPages_t *multifd_pages_init(size_t size)
232 {
233 MultiFDPages_t *pages = g_new0(MultiFDPages_t, 1);
234
235 pages->allocated = size;
236 pages->iov = g_new0(struct iovec, size);
237 pages->offset = g_new0(ram_addr_t, size);
238
239 return pages;
240 }
241
242 static void multifd_pages_clear(MultiFDPages_t *pages)
243 {
244 pages->used = 0;
245 pages->allocated = 0;
246 pages->packet_num = 0;
247 pages->block = NULL;
248 g_free(pages->iov);
249 pages->iov = NULL;
250 g_free(pages->offset);
251 pages->offset = NULL;
252 g_free(pages);
253 }
254
255 static void multifd_send_fill_packet(MultiFDSendParams *p)
256 {
257 MultiFDPacket_t *packet = p->packet;
258 int i;
259
260 packet->flags = cpu_to_be32(p->flags);
261 packet->pages_alloc = cpu_to_be32(p->pages->allocated);
262 packet->pages_used = cpu_to_be32(p->pages->used);
263 packet->next_packet_size = cpu_to_be32(p->next_packet_size);
264 packet->packet_num = cpu_to_be64(p->packet_num);
265
266 if (p->pages->block) {
267 strncpy(packet->ramblock, p->pages->block->idstr, 256);
268 }
269
270 for (i = 0; i < p->pages->used; i++) {
271 /* there are architectures where ram_addr_t is 32 bit */
272 uint64_t temp = p->pages->offset[i];
273
274 packet->offset[i] = cpu_to_be64(temp);
275 }
276 }
277
278 static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)
279 {
280 MultiFDPacket_t *packet = p->packet;
281 uint32_t pages_max = MULTIFD_PACKET_SIZE / qemu_target_page_size();
282 RAMBlock *block;
283 int i;
284
285 packet->magic = be32_to_cpu(packet->magic);
286 if (packet->magic != MULTIFD_MAGIC) {
287 error_setg(errp, "multifd: received packet "
288 "magic %x and expected magic %x",
289 packet->magic, MULTIFD_MAGIC);
290 return -1;
291 }
292
293 packet->version = be32_to_cpu(packet->version);
294 if (packet->version != MULTIFD_VERSION) {
295 error_setg(errp, "multifd: received packet "
296 "version %d and expected version %d",
297 packet->version, MULTIFD_VERSION);
298 return -1;
299 }
300
301 p->flags = be32_to_cpu(packet->flags);
302
303 packet->pages_alloc = be32_to_cpu(packet->pages_alloc);
304 /*
305 * If we received a packet that is 100 times bigger than expected
306 * just stop migration. It is a magic number.
307 */
308 if (packet->pages_alloc > pages_max * 100) {
309 error_setg(errp, "multifd: received packet "
310 "with size %d and expected a maximum size of %d",
311 packet->pages_alloc, pages_max * 100) ;
312 return -1;
313 }
314 /*
315 * We received a packet that is bigger than expected but inside
316 * reasonable limits (see previous comment). Just reallocate.
317 */
318 if (packet->pages_alloc > p->pages->allocated) {
319 multifd_pages_clear(p->pages);
320 p->pages = multifd_pages_init(packet->pages_alloc);
321 }
322
323 p->pages->used = be32_to_cpu(packet->pages_used);
324 if (p->pages->used > packet->pages_alloc) {
325 error_setg(errp, "multifd: received packet "
326 "with %d pages and expected maximum pages are %d",
327 p->pages->used, packet->pages_alloc) ;
328 return -1;
329 }
330
331 p->next_packet_size = be32_to_cpu(packet->next_packet_size);
332 p->packet_num = be64_to_cpu(packet->packet_num);
333
334 if (p->pages->used == 0) {
335 return 0;
336 }
337
338 /* make sure that ramblock is 0 terminated */
339 packet->ramblock[255] = 0;
340 block = qemu_ram_block_by_name(packet->ramblock);
341 if (!block) {
342 error_setg(errp, "multifd: unknown ram block %s",
343 packet->ramblock);
344 return -1;
345 }
346
347 for (i = 0; i < p->pages->used; i++) {
348 uint64_t offset = be64_to_cpu(packet->offset[i]);
349
350 if (offset > (block->used_length - qemu_target_page_size())) {
351 error_setg(errp, "multifd: offset too long %" PRIu64
352 " (max " RAM_ADDR_FMT ")",
353 offset, block->max_length);
354 return -1;
355 }
356 p->pages->iov[i].iov_base = block->host + offset;
357 p->pages->iov[i].iov_len = qemu_target_page_size();
358 }
359
360 return 0;
361 }
362
363 struct {
364 MultiFDSendParams *params;
365 /* array of pages to sent */
366 MultiFDPages_t *pages;
367 /* global number of generated multifd packets */
368 uint64_t packet_num;
369 /* send channels ready */
370 QemuSemaphore channels_ready;
371 /*
372 * Have we already run terminate threads. There is a race when it
373 * happens that we got one error while we are exiting.
374 * We will use atomic operations. Only valid values are 0 and 1.
375 */
376 int exiting;
377 /* multifd ops */
378 MultiFDMethods *ops;
379 } *multifd_send_state;
380
381 /*
382 * How we use multifd_send_state->pages and channel->pages?
383 *
384 * We create a pages for each channel, and a main one. Each time that
385 * we need to send a batch of pages we interchange the ones between
386 * multifd_send_state and the channel that is sending it. There are
387 * two reasons for that:
388 * - to not have to do so many mallocs during migration
389 * - to make easier to know what to free at the end of migration
390 *
391 * This way we always know who is the owner of each "pages" struct,
392 * and we don't need any locking. It belongs to the migration thread
393 * or to the channel thread. Switching is safe because the migration
394 * thread is using the channel mutex when changing it, and the channel
395 * have to had finish with its own, otherwise pending_job can't be
396 * false.
397 */
398
399 static int multifd_send_pages(QEMUFile *f)
400 {
401 int i;
402 static int next_channel;
403 MultiFDSendParams *p = NULL; /* make happy gcc */
404 MultiFDPages_t *pages = multifd_send_state->pages;
405 uint64_t transferred;
406
407 if (atomic_read(&multifd_send_state->exiting)) {
408 return -1;
409 }
410
411 qemu_sem_wait(&multifd_send_state->channels_ready);
412 for (i = next_channel;; i = (i + 1) % migrate_multifd_channels()) {
413 p = &multifd_send_state->params[i];
414
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;
420 }
421 if (!p->pending_job) {
422 p->pending_job++;
423 next_channel = (i + 1) % migrate_multifd_channels();
424 break;
425 }
426 qemu_mutex_unlock(&p->mutex);
427 }
428 assert(!p->pages->used);
429 assert(!p->pages->block);
430
431 p->packet_num = multifd_send_state->packet_num++;
432 multifd_send_state->pages = p->pages;
433 p->pages = pages;
434 transferred = ((uint64_t) pages->used) * qemu_target_page_size()
435 + p->packet_len;
436 qemu_file_update_transfer(f, transferred);
437 ram_counters.multifd_bytes += transferred;
438 ram_counters.transferred += transferred;;
439 qemu_mutex_unlock(&p->mutex);
440 qemu_sem_post(&p->sem);
441
442 return 1;
443 }
444
445 int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset)
446 {
447 MultiFDPages_t *pages = multifd_send_state->pages;
448
449 if (!pages->block) {
450 pages->block = block;
451 }
452
453 if (pages->block == block) {
454 pages->offset[pages->used] = offset;
455 pages->iov[pages->used].iov_base = block->host + offset;
456 pages->iov[pages->used].iov_len = qemu_target_page_size();
457 pages->used++;
458
459 if (pages->used < pages->allocated) {
460 return 1;
461 }
462 }
463
464 if (multifd_send_pages(f) < 0) {
465 return -1;
466 }
467
468 if (pages->block != block) {
469 return multifd_queue_page(f, block, offset);
470 }
471
472 return 1;
473 }
474
475 static void multifd_send_terminate_threads(Error *err)
476 {
477 int i;
478
479 trace_multifd_send_terminate_threads(err != NULL);
480
481 if (err) {
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);
490 }
491 }
492
493 /*
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
497 * twice.
498 */
499 if (atomic_xchg(&multifd_send_state->exiting, 1)) {
500 return;
501 }
502
503 for (i = 0; i < migrate_multifd_channels(); i++) {
504 MultiFDSendParams *p = &multifd_send_state->params[i];
505
506 qemu_mutex_lock(&p->mutex);
507 p->quit = true;
508 qemu_sem_post(&p->sem);
509 qemu_mutex_unlock(&p->mutex);
510 }
511 }
512
513 void multifd_save_cleanup(void)
514 {
515 int i;
516
517 if (!migrate_use_multifd()) {
518 return;
519 }
520 multifd_send_terminate_threads(NULL);
521 for (i = 0; i < migrate_multifd_channels(); i++) {
522 MultiFDSendParams *p = &multifd_send_state->params[i];
523
524 if (p->running) {
525 qemu_thread_join(&p->thread);
526 }
527 }
528 for (i = 0; i < migrate_multifd_channels(); i++) {
529 MultiFDSendParams *p = &multifd_send_state->params[i];
530 Error *local_err = NULL;
531
532 socket_send_channel_destroy(p->c);
533 p->c = NULL;
534 qemu_mutex_destroy(&p->mutex);
535 qemu_sem_destroy(&p->sem);
536 qemu_sem_destroy(&p->sem_sync);
537 g_free(p->name);
538 p->name = NULL;
539 multifd_pages_clear(p->pages);
540 p->pages = NULL;
541 p->packet_len = 0;
542 g_free(p->packet);
543 p->packet = NULL;
544 multifd_send_state->ops->send_cleanup(p, &local_err);
545 if (local_err) {
546 migrate_set_error(migrate_get_current(), local_err);
547 }
548 }
549 qemu_sem_destroy(&multifd_send_state->channels_ready);
550 g_free(multifd_send_state->params);
551 multifd_send_state->params = NULL;
552 multifd_pages_clear(multifd_send_state->pages);
553 multifd_send_state->pages = NULL;
554 g_free(multifd_send_state);
555 multifd_send_state = NULL;
556 }
557
558 void multifd_send_sync_main(QEMUFile *f)
559 {
560 int i;
561
562 if (!migrate_use_multifd()) {
563 return;
564 }
565 if (multifd_send_state->pages->used) {
566 if (multifd_send_pages(f) < 0) {
567 error_report("%s: multifd_send_pages fail", __func__);
568 return;
569 }
570 }
571 for (i = 0; i < migrate_multifd_channels(); i++) {
572 MultiFDSendParams *p = &multifd_send_state->params[i];
573
574 trace_multifd_send_sync_main_signal(p->id);
575
576 qemu_mutex_lock(&p->mutex);
577
578 if (p->quit) {
579 error_report("%s: channel %d has already quit", __func__, i);
580 qemu_mutex_unlock(&p->mutex);
581 return;
582 }
583
584 p->packet_num = multifd_send_state->packet_num++;
585 p->flags |= MULTIFD_FLAG_SYNC;
586 p->pending_job++;
587 qemu_file_update_transfer(f, p->packet_len);
588 ram_counters.multifd_bytes += p->packet_len;
589 ram_counters.transferred += p->packet_len;
590 qemu_mutex_unlock(&p->mutex);
591 qemu_sem_post(&p->sem);
592 }
593 for (i = 0; i < migrate_multifd_channels(); i++) {
594 MultiFDSendParams *p = &multifd_send_state->params[i];
595
596 trace_multifd_send_sync_main_wait(p->id);
597 qemu_sem_wait(&p->sem_sync);
598 }
599 trace_multifd_send_sync_main(multifd_send_state->packet_num);
600 }
601
602 static void *multifd_send_thread(void *opaque)
603 {
604 MultiFDSendParams *p = opaque;
605 Error *local_err = NULL;
606 int ret = 0;
607 uint32_t flags = 0;
608
609 trace_multifd_send_thread_start(p->id);
610 rcu_register_thread();
611
612 if (multifd_send_initial_packet(p, &local_err) < 0) {
613 ret = -1;
614 goto out;
615 }
616 /* initial packet */
617 p->num_packets = 1;
618
619 while (true) {
620 qemu_sem_wait(&p->sem);
621
622 if (atomic_read(&multifd_send_state->exiting)) {
623 break;
624 }
625 qemu_mutex_lock(&p->mutex);
626
627 if (p->pending_job) {
628 uint32_t used = p->pages->used;
629 uint64_t packet_num = p->packet_num;
630 flags = p->flags;
631
632 if (used) {
633 ret = multifd_send_state->ops->send_prepare(p, used,
634 &local_err);
635 if (ret != 0) {
636 qemu_mutex_unlock(&p->mutex);
637 break;
638 }
639 }
640 multifd_send_fill_packet(p);
641 p->flags = 0;
642 p->num_packets++;
643 p->num_pages += used;
644 p->pages->used = 0;
645 p->pages->block = NULL;
646 qemu_mutex_unlock(&p->mutex);
647
648 trace_multifd_send(p->id, packet_num, used, flags,
649 p->next_packet_size);
650
651 ret = qio_channel_write_all(p->c, (void *)p->packet,
652 p->packet_len, &local_err);
653 if (ret != 0) {
654 break;
655 }
656
657 if (used) {
658 ret = multifd_send_state->ops->send_write(p, used, &local_err);
659 if (ret != 0) {
660 break;
661 }
662 }
663
664 qemu_mutex_lock(&p->mutex);
665 p->pending_job--;
666 qemu_mutex_unlock(&p->mutex);
667
668 if (flags & MULTIFD_FLAG_SYNC) {
669 qemu_sem_post(&p->sem_sync);
670 }
671 qemu_sem_post(&multifd_send_state->channels_ready);
672 } else if (p->quit) {
673 qemu_mutex_unlock(&p->mutex);
674 break;
675 } else {
676 qemu_mutex_unlock(&p->mutex);
677 /* sometimes there are spurious wakeups */
678 }
679 }
680
681 out:
682 if (local_err) {
683 trace_multifd_send_error(p->id);
684 multifd_send_terminate_threads(local_err);
685 }
686
687 /*
688 * Error happen, I will exit, but I can't just leave, tell
689 * who pay attention to me.
690 */
691 if (ret != 0) {
692 qemu_sem_post(&p->sem_sync);
693 qemu_sem_post(&multifd_send_state->channels_ready);
694 }
695
696 qemu_mutex_lock(&p->mutex);
697 p->running = false;
698 qemu_mutex_unlock(&p->mutex);
699
700 rcu_unregister_thread();
701 trace_multifd_send_thread_end(p->id, p->num_packets, p->num_pages);
702
703 return NULL;
704 }
705
706 static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque)
707 {
708 MultiFDSendParams *p = opaque;
709 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
710 Error *local_err = NULL;
711
712 trace_multifd_new_send_channel_async(p->id);
713 if (qio_task_propagate_error(task, &local_err)) {
714 migrate_set_error(migrate_get_current(), local_err);
715 /* Error happen, we need to tell who pay attention to me */
716 qemu_sem_post(&multifd_send_state->channels_ready);
717 qemu_sem_post(&p->sem_sync);
718 /*
719 * Although multifd_send_thread is not created, but main migration
720 * thread neet to judge whether it is running, so we need to mark
721 * its status.
722 */
723 p->quit = true;
724 } else {
725 p->c = QIO_CHANNEL(sioc);
726 qio_channel_set_delay(p->c, false);
727 p->running = true;
728 qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
729 QEMU_THREAD_JOINABLE);
730 }
731 }
732
733 int multifd_save_setup(Error **errp)
734 {
735 int thread_count;
736 uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
737 uint8_t i;
738
739 if (!migrate_use_multifd()) {
740 return 0;
741 }
742 thread_count = migrate_multifd_channels();
743 multifd_send_state = g_malloc0(sizeof(*multifd_send_state));
744 multifd_send_state->params = g_new0(MultiFDSendParams, thread_count);
745 multifd_send_state->pages = multifd_pages_init(page_count);
746 qemu_sem_init(&multifd_send_state->channels_ready, 0);
747 atomic_set(&multifd_send_state->exiting, 0);
748 multifd_send_state->ops = multifd_ops[migrate_multifd_compression()];
749
750 for (i = 0; i < thread_count; i++) {
751 MultiFDSendParams *p = &multifd_send_state->params[i];
752
753 qemu_mutex_init(&p->mutex);
754 qemu_sem_init(&p->sem, 0);
755 qemu_sem_init(&p->sem_sync, 0);
756 p->quit = false;
757 p->pending_job = 0;
758 p->id = i;
759 p->pages = multifd_pages_init(page_count);
760 p->packet_len = sizeof(MultiFDPacket_t)
761 + sizeof(uint64_t) * page_count;
762 p->packet = g_malloc0(p->packet_len);
763 p->packet->magic = cpu_to_be32(MULTIFD_MAGIC);
764 p->packet->version = cpu_to_be32(MULTIFD_VERSION);
765 p->name = g_strdup_printf("multifdsend_%d", i);
766 socket_send_channel_create(multifd_new_send_channel_async, p);
767 }
768
769 for (i = 0; i < thread_count; i++) {
770 MultiFDSendParams *p = &multifd_send_state->params[i];
771 Error *local_err = NULL;
772 int ret;
773
774 ret = multifd_send_state->ops->send_setup(p, &local_err);
775 if (ret) {
776 error_propagate(errp, local_err);
777 return ret;
778 }
779 }
780 return 0;
781 }
782
783 struct {
784 MultiFDRecvParams *params;
785 /* number of created threads */
786 int count;
787 /* syncs main thread and channels */
788 QemuSemaphore sem_sync;
789 /* global number of generated multifd packets */
790 uint64_t packet_num;
791 /* multifd ops */
792 MultiFDMethods *ops;
793 } *multifd_recv_state;
794
795 static void multifd_recv_terminate_threads(Error *err)
796 {
797 int i;
798
799 trace_multifd_recv_terminate_threads(err != NULL);
800
801 if (err) {
802 MigrationState *s = migrate_get_current();
803 migrate_set_error(s, err);
804 if (s->state == MIGRATION_STATUS_SETUP ||
805 s->state == MIGRATION_STATUS_ACTIVE) {
806 migrate_set_state(&s->state, s->state,
807 MIGRATION_STATUS_FAILED);
808 }
809 }
810
811 for (i = 0; i < migrate_multifd_channels(); i++) {
812 MultiFDRecvParams *p = &multifd_recv_state->params[i];
813
814 qemu_mutex_lock(&p->mutex);
815 p->quit = true;
816 /*
817 * We could arrive here for two reasons:
818 * - normal quit, i.e. everything went fine, just finished
819 * - error quit: We close the channels so the channel threads
820 * finish the qio_channel_read_all_eof()
821 */
822 if (p->c) {
823 qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
824 }
825 qemu_mutex_unlock(&p->mutex);
826 }
827 }
828
829 int multifd_load_cleanup(Error **errp)
830 {
831 int i;
832
833 if (!migrate_use_multifd()) {
834 return 0;
835 }
836 multifd_recv_terminate_threads(NULL);
837 for (i = 0; i < migrate_multifd_channels(); i++) {
838 MultiFDRecvParams *p = &multifd_recv_state->params[i];
839
840 if (p->running) {
841 p->quit = true;
842 /*
843 * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
844 * however try to wakeup it without harm in cleanup phase.
845 */
846 qemu_sem_post(&p->sem_sync);
847 qemu_thread_join(&p->thread);
848 }
849 }
850 for (i = 0; i < migrate_multifd_channels(); i++) {
851 MultiFDRecvParams *p = &multifd_recv_state->params[i];
852
853 object_unref(OBJECT(p->c));
854 p->c = NULL;
855 qemu_mutex_destroy(&p->mutex);
856 qemu_sem_destroy(&p->sem_sync);
857 g_free(p->name);
858 p->name = NULL;
859 multifd_pages_clear(p->pages);
860 p->pages = NULL;
861 p->packet_len = 0;
862 g_free(p->packet);
863 p->packet = NULL;
864 multifd_recv_state->ops->recv_cleanup(p);
865 }
866 qemu_sem_destroy(&multifd_recv_state->sem_sync);
867 g_free(multifd_recv_state->params);
868 multifd_recv_state->params = NULL;
869 g_free(multifd_recv_state);
870 multifd_recv_state = NULL;
871
872 return 0;
873 }
874
875 void multifd_recv_sync_main(void)
876 {
877 int i;
878
879 if (!migrate_use_multifd()) {
880 return;
881 }
882 for (i = 0; i < migrate_multifd_channels(); i++) {
883 MultiFDRecvParams *p = &multifd_recv_state->params[i];
884
885 trace_multifd_recv_sync_main_wait(p->id);
886 qemu_sem_wait(&multifd_recv_state->sem_sync);
887 }
888 for (i = 0; i < migrate_multifd_channels(); i++) {
889 MultiFDRecvParams *p = &multifd_recv_state->params[i];
890
891 qemu_mutex_lock(&p->mutex);
892 if (multifd_recv_state->packet_num < p->packet_num) {
893 multifd_recv_state->packet_num = p->packet_num;
894 }
895 qemu_mutex_unlock(&p->mutex);
896 trace_multifd_recv_sync_main_signal(p->id);
897 qemu_sem_post(&p->sem_sync);
898 }
899 trace_multifd_recv_sync_main(multifd_recv_state->packet_num);
900 }
901
902 static void *multifd_recv_thread(void *opaque)
903 {
904 MultiFDRecvParams *p = opaque;
905 Error *local_err = NULL;
906 int ret;
907
908 trace_multifd_recv_thread_start(p->id);
909 rcu_register_thread();
910
911 while (true) {
912 uint32_t used;
913 uint32_t flags;
914
915 if (p->quit) {
916 break;
917 }
918
919 ret = qio_channel_read_all_eof(p->c, (void *)p->packet,
920 p->packet_len, &local_err);
921 if (ret == 0) { /* EOF */
922 break;
923 }
924 if (ret == -1) { /* Error */
925 break;
926 }
927
928 qemu_mutex_lock(&p->mutex);
929 ret = multifd_recv_unfill_packet(p, &local_err);
930 if (ret) {
931 qemu_mutex_unlock(&p->mutex);
932 break;
933 }
934
935 used = p->pages->used;
936 flags = p->flags;
937 /* recv methods don't know how to handle the SYNC flag */
938 p->flags &= ~MULTIFD_FLAG_SYNC;
939 trace_multifd_recv(p->id, p->packet_num, used, flags,
940 p->next_packet_size);
941 p->num_packets++;
942 p->num_pages += used;
943 qemu_mutex_unlock(&p->mutex);
944
945 if (used) {
946 ret = multifd_recv_state->ops->recv_pages(p, used, &local_err);
947 if (ret != 0) {
948 break;
949 }
950 }
951
952 if (flags & MULTIFD_FLAG_SYNC) {
953 qemu_sem_post(&multifd_recv_state->sem_sync);
954 qemu_sem_wait(&p->sem_sync);
955 }
956 }
957
958 if (local_err) {
959 multifd_recv_terminate_threads(local_err);
960 }
961 qemu_mutex_lock(&p->mutex);
962 p->running = false;
963 qemu_mutex_unlock(&p->mutex);
964
965 rcu_unregister_thread();
966 trace_multifd_recv_thread_end(p->id, p->num_packets, p->num_pages);
967
968 return NULL;
969 }
970
971 int multifd_load_setup(Error **errp)
972 {
973 int thread_count;
974 uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
975 uint8_t i;
976
977 if (!migrate_use_multifd()) {
978 return 0;
979 }
980 thread_count = migrate_multifd_channels();
981 multifd_recv_state = g_malloc0(sizeof(*multifd_recv_state));
982 multifd_recv_state->params = g_new0(MultiFDRecvParams, thread_count);
983 atomic_set(&multifd_recv_state->count, 0);
984 qemu_sem_init(&multifd_recv_state->sem_sync, 0);
985 multifd_recv_state->ops = multifd_ops[migrate_multifd_compression()];
986
987 for (i = 0; i < thread_count; i++) {
988 MultiFDRecvParams *p = &multifd_recv_state->params[i];
989
990 qemu_mutex_init(&p->mutex);
991 qemu_sem_init(&p->sem_sync, 0);
992 p->quit = false;
993 p->id = i;
994 p->pages = multifd_pages_init(page_count);
995 p->packet_len = sizeof(MultiFDPacket_t)
996 + sizeof(uint64_t) * page_count;
997 p->packet = g_malloc0(p->packet_len);
998 p->name = g_strdup_printf("multifdrecv_%d", i);
999 }
1000
1001 for (i = 0; i < thread_count; i++) {
1002 MultiFDRecvParams *p = &multifd_recv_state->params[i];
1003 Error *local_err = NULL;
1004 int ret;
1005
1006 ret = multifd_recv_state->ops->recv_setup(p, &local_err);
1007 if (ret) {
1008 error_propagate(errp, local_err);
1009 return ret;
1010 }
1011 }
1012 return 0;
1013 }
1014
1015 bool multifd_recv_all_channels_created(void)
1016 {
1017 int thread_count = migrate_multifd_channels();
1018
1019 if (!migrate_use_multifd()) {
1020 return true;
1021 }
1022
1023 return thread_count == atomic_read(&multifd_recv_state->count);
1024 }
1025
1026 /*
1027 * Try to receive all multifd channels to get ready for the migration.
1028 * - Return true and do not set @errp when correctly receving all channels;
1029 * - Return false and do not set @errp when correctly receiving the current one;
1030 * - Return false and set @errp when failing to receive the current channel.
1031 */
1032 bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp)
1033 {
1034 MultiFDRecvParams *p;
1035 Error *local_err = NULL;
1036 int id;
1037
1038 id = multifd_recv_initial_packet(ioc, &local_err);
1039 if (id < 0) {
1040 multifd_recv_terminate_threads(local_err);
1041 error_propagate_prepend(errp, local_err,
1042 "failed to receive packet"
1043 " via multifd channel %d: ",
1044 atomic_read(&multifd_recv_state->count));
1045 return false;
1046 }
1047 trace_multifd_recv_new_channel(id);
1048
1049 p = &multifd_recv_state->params[id];
1050 if (p->c != NULL) {
1051 error_setg(&local_err, "multifd: received id '%d' already setup'",
1052 id);
1053 multifd_recv_terminate_threads(local_err);
1054 error_propagate(errp, local_err);
1055 return false;
1056 }
1057 p->c = ioc;
1058 object_ref(OBJECT(ioc));
1059 /* initial packet */
1060 p->num_packets = 1;
1061
1062 p->running = true;
1063 qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p,
1064 QEMU_THREAD_JOINABLE);
1065 atomic_inc(&multifd_recv_state->count);
1066 return atomic_read(&multifd_recv_state->count) ==
1067 migrate_multifd_channels();
1068 }