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