xref: /openbmc/qemu/migration/multifd.c (revision 947701cc)
1d32ca5adSJuan Quintela /*
2d32ca5adSJuan Quintela  * Multifd common code
3d32ca5adSJuan Quintela  *
4d32ca5adSJuan Quintela  * Copyright (c) 2019-2020 Red Hat Inc
5d32ca5adSJuan Quintela  *
6d32ca5adSJuan Quintela  * Authors:
7d32ca5adSJuan Quintela  *  Juan Quintela <quintela@redhat.com>
8d32ca5adSJuan Quintela  *
9d32ca5adSJuan Quintela  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10d32ca5adSJuan Quintela  * See the COPYING file in the top-level directory.
11d32ca5adSJuan Quintela  */
12d32ca5adSJuan Quintela 
13d32ca5adSJuan Quintela #include "qemu/osdep.h"
14d32ca5adSJuan Quintela #include "qemu/rcu.h"
15d32ca5adSJuan Quintela #include "exec/target_page.h"
16d32ca5adSJuan Quintela #include "sysemu/sysemu.h"
17d32ca5adSJuan Quintela #include "exec/ramblock.h"
18d32ca5adSJuan Quintela #include "qemu/error-report.h"
19d32ca5adSJuan Quintela #include "qapi/error.h"
20d32ca5adSJuan Quintela #include "ram.h"
21d32ca5adSJuan Quintela #include "migration.h"
22*947701ccSJuan Quintela #include "migration-stats.h"
23d32ca5adSJuan Quintela #include "socket.h"
2429647140SChuan Zheng #include "tls.h"
25d32ca5adSJuan Quintela #include "qemu-file.h"
26d32ca5adSJuan Quintela #include "trace.h"
27d32ca5adSJuan Quintela #include "multifd.h"
281b1f4ab6SJiang Jiacheng #include "threadinfo.h"
29b4bc342cSJuan Quintela #include "options.h"
30b5eea99eSLukas Straub #include "qemu/yank.h"
31b5eea99eSLukas Straub #include "io/channel-socket.h"
321a92d6d5SLukas Straub #include "yank_functions.h"
33b5eea99eSLukas Straub 
34d32ca5adSJuan Quintela /* Multiple fd's */
35d32ca5adSJuan Quintela 
36d32ca5adSJuan Quintela #define MULTIFD_MAGIC 0x11223344U
37d32ca5adSJuan Quintela #define MULTIFD_VERSION 1
38d32ca5adSJuan Quintela 
39d32ca5adSJuan Quintela typedef struct {
40d32ca5adSJuan Quintela     uint32_t magic;
41d32ca5adSJuan Quintela     uint32_t version;
42d32ca5adSJuan Quintela     unsigned char uuid[16]; /* QemuUUID */
43d32ca5adSJuan Quintela     uint8_t id;
44d32ca5adSJuan Quintela     uint8_t unused1[7];     /* Reserved for future use */
45d32ca5adSJuan Quintela     uint64_t unused2[4];    /* Reserved for future use */
46d32ca5adSJuan Quintela } __attribute__((packed)) MultiFDInit_t;
47d32ca5adSJuan Quintela 
48ab7cbb0bSJuan Quintela /* Multifd without compression */
49ab7cbb0bSJuan Quintela 
50ab7cbb0bSJuan Quintela /**
51ab7cbb0bSJuan Quintela  * nocomp_send_setup: setup send side
52ab7cbb0bSJuan Quintela  *
53ab7cbb0bSJuan Quintela  * For no compression this function does nothing.
54ab7cbb0bSJuan Quintela  *
55ab7cbb0bSJuan Quintela  * Returns 0 for success or -1 for error
56ab7cbb0bSJuan Quintela  *
57ab7cbb0bSJuan Quintela  * @p: Params for the channel that we are using
58ab7cbb0bSJuan Quintela  * @errp: pointer to an error
59ab7cbb0bSJuan Quintela  */
60ab7cbb0bSJuan Quintela static int nocomp_send_setup(MultiFDSendParams *p, Error **errp)
61ab7cbb0bSJuan Quintela {
62ab7cbb0bSJuan Quintela     return 0;
63ab7cbb0bSJuan Quintela }
64ab7cbb0bSJuan Quintela 
65ab7cbb0bSJuan Quintela /**
66ab7cbb0bSJuan Quintela  * nocomp_send_cleanup: cleanup send side
67ab7cbb0bSJuan Quintela  *
68ab7cbb0bSJuan Quintela  * For no compression this function does nothing.
69ab7cbb0bSJuan Quintela  *
70ab7cbb0bSJuan Quintela  * @p: Params for the channel that we are using
7118ede636SJuan Quintela  * @errp: pointer to an error
72ab7cbb0bSJuan Quintela  */
73ab7cbb0bSJuan Quintela static void nocomp_send_cleanup(MultiFDSendParams *p, Error **errp)
74ab7cbb0bSJuan Quintela {
75ab7cbb0bSJuan Quintela     return;
76ab7cbb0bSJuan Quintela }
77ab7cbb0bSJuan Quintela 
78ab7cbb0bSJuan Quintela /**
79ab7cbb0bSJuan Quintela  * nocomp_send_prepare: prepare date to be able to send
80ab7cbb0bSJuan Quintela  *
81ab7cbb0bSJuan Quintela  * For no compression we just have to calculate the size of the
82ab7cbb0bSJuan Quintela  * packet.
83ab7cbb0bSJuan Quintela  *
84ab7cbb0bSJuan Quintela  * Returns 0 for success or -1 for error
85ab7cbb0bSJuan Quintela  *
86ab7cbb0bSJuan Quintela  * @p: Params for the channel that we are using
87ab7cbb0bSJuan Quintela  * @errp: pointer to an error
88ab7cbb0bSJuan Quintela  */
8902fb8104SJuan Quintela static int nocomp_send_prepare(MultiFDSendParams *p, Error **errp)
90ab7cbb0bSJuan Quintela {
91226468baSJuan Quintela     MultiFDPages_t *pages = p->pages;
92226468baSJuan Quintela 
93815956f0SJuan Quintela     for (int i = 0; i < p->normal_num; i++) {
94815956f0SJuan Quintela         p->iov[p->iovs_num].iov_base = pages->block->host + p->normal[i];
95ddec20f8SJuan Quintela         p->iov[p->iovs_num].iov_len = p->page_size;
96226468baSJuan Quintela         p->iovs_num++;
97226468baSJuan Quintela     }
98226468baSJuan Quintela 
99ddec20f8SJuan Quintela     p->next_packet_size = p->normal_num * p->page_size;
100ab7cbb0bSJuan Quintela     p->flags |= MULTIFD_FLAG_NOCOMP;
101ab7cbb0bSJuan Quintela     return 0;
102ab7cbb0bSJuan Quintela }
103ab7cbb0bSJuan Quintela 
104ab7cbb0bSJuan Quintela /**
105ab7cbb0bSJuan Quintela  * nocomp_recv_setup: setup receive side
106ab7cbb0bSJuan Quintela  *
107ab7cbb0bSJuan Quintela  * For no compression this function does nothing.
108ab7cbb0bSJuan Quintela  *
109ab7cbb0bSJuan Quintela  * Returns 0 for success or -1 for error
110ab7cbb0bSJuan Quintela  *
111ab7cbb0bSJuan Quintela  * @p: Params for the channel that we are using
112ab7cbb0bSJuan Quintela  * @errp: pointer to an error
113ab7cbb0bSJuan Quintela  */
114ab7cbb0bSJuan Quintela static int nocomp_recv_setup(MultiFDRecvParams *p, Error **errp)
115ab7cbb0bSJuan Quintela {
116ab7cbb0bSJuan Quintela     return 0;
117ab7cbb0bSJuan Quintela }
118ab7cbb0bSJuan Quintela 
119ab7cbb0bSJuan Quintela /**
120ab7cbb0bSJuan Quintela  * nocomp_recv_cleanup: setup receive side
121ab7cbb0bSJuan Quintela  *
122ab7cbb0bSJuan Quintela  * For no compression this function does nothing.
123ab7cbb0bSJuan Quintela  *
124ab7cbb0bSJuan Quintela  * @p: Params for the channel that we are using
125ab7cbb0bSJuan Quintela  */
126ab7cbb0bSJuan Quintela static void nocomp_recv_cleanup(MultiFDRecvParams *p)
127ab7cbb0bSJuan Quintela {
128ab7cbb0bSJuan Quintela }
129ab7cbb0bSJuan Quintela 
130ab7cbb0bSJuan Quintela /**
131ab7cbb0bSJuan Quintela  * nocomp_recv_pages: read the data from the channel into actual pages
132ab7cbb0bSJuan Quintela  *
133ab7cbb0bSJuan Quintela  * For no compression we just need to read things into the correct place.
134ab7cbb0bSJuan Quintela  *
135ab7cbb0bSJuan Quintela  * Returns 0 for success or -1 for error
136ab7cbb0bSJuan Quintela  *
137ab7cbb0bSJuan Quintela  * @p: Params for the channel that we are using
138ab7cbb0bSJuan Quintela  * @errp: pointer to an error
139ab7cbb0bSJuan Quintela  */
14040a4bfe9SJuan Quintela static int nocomp_recv_pages(MultiFDRecvParams *p, Error **errp)
141ab7cbb0bSJuan Quintela {
142ab7cbb0bSJuan Quintela     uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
143ab7cbb0bSJuan Quintela 
144ab7cbb0bSJuan Quintela     if (flags != MULTIFD_FLAG_NOCOMP) {
14504e11404SJuan Quintela         error_setg(errp, "multifd %u: flags received %x flags expected %x",
146ab7cbb0bSJuan Quintela                    p->id, flags, MULTIFD_FLAG_NOCOMP);
147ab7cbb0bSJuan Quintela         return -1;
148ab7cbb0bSJuan Quintela     }
149cf2d4aa8SJuan Quintela     for (int i = 0; i < p->normal_num; i++) {
150faf60935SJuan Quintela         p->iov[i].iov_base = p->host + p->normal[i];
151ddec20f8SJuan Quintela         p->iov[i].iov_len = p->page_size;
152226468baSJuan Quintela     }
153cf2d4aa8SJuan Quintela     return qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);
154ab7cbb0bSJuan Quintela }
155ab7cbb0bSJuan Quintela 
156ab7cbb0bSJuan Quintela static MultiFDMethods multifd_nocomp_ops = {
157ab7cbb0bSJuan Quintela     .send_setup = nocomp_send_setup,
158ab7cbb0bSJuan Quintela     .send_cleanup = nocomp_send_cleanup,
159ab7cbb0bSJuan Quintela     .send_prepare = nocomp_send_prepare,
160ab7cbb0bSJuan Quintela     .recv_setup = nocomp_recv_setup,
161ab7cbb0bSJuan Quintela     .recv_cleanup = nocomp_recv_cleanup,
162ab7cbb0bSJuan Quintela     .recv_pages = nocomp_recv_pages
163ab7cbb0bSJuan Quintela };
164ab7cbb0bSJuan Quintela 
165ab7cbb0bSJuan Quintela static MultiFDMethods *multifd_ops[MULTIFD_COMPRESSION__MAX] = {
166ab7cbb0bSJuan Quintela     [MULTIFD_COMPRESSION_NONE] = &multifd_nocomp_ops,
167ab7cbb0bSJuan Quintela };
168ab7cbb0bSJuan Quintela 
1697ec2c2b3SJuan Quintela void multifd_register_ops(int method, MultiFDMethods *ops)
1707ec2c2b3SJuan Quintela {
1717ec2c2b3SJuan Quintela     assert(0 < method && method < MULTIFD_COMPRESSION__MAX);
1727ec2c2b3SJuan Quintela     multifd_ops[method] = ops;
1737ec2c2b3SJuan Quintela }
1747ec2c2b3SJuan Quintela 
175d32ca5adSJuan Quintela static int multifd_send_initial_packet(MultiFDSendParams *p, Error **errp)
176d32ca5adSJuan Quintela {
177d32ca5adSJuan Quintela     MultiFDInit_t msg = {};
178d32ca5adSJuan Quintela     int ret;
179d32ca5adSJuan Quintela 
180d32ca5adSJuan Quintela     msg.magic = cpu_to_be32(MULTIFD_MAGIC);
181d32ca5adSJuan Quintela     msg.version = cpu_to_be32(MULTIFD_VERSION);
182d32ca5adSJuan Quintela     msg.id = p->id;
183d32ca5adSJuan Quintela     memcpy(msg.uuid, &qemu_uuid.data, sizeof(msg.uuid));
184d32ca5adSJuan Quintela 
185d32ca5adSJuan Quintela     ret = qio_channel_write_all(p->c, (char *)&msg, sizeof(msg), errp);
186d32ca5adSJuan Quintela     if (ret != 0) {
187d32ca5adSJuan Quintela         return -1;
188d32ca5adSJuan Quintela     }
189d32ca5adSJuan Quintela     return 0;
190d32ca5adSJuan Quintela }
191d32ca5adSJuan Quintela 
192d32ca5adSJuan Quintela static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
193d32ca5adSJuan Quintela {
194d32ca5adSJuan Quintela     MultiFDInit_t msg;
195d32ca5adSJuan Quintela     int ret;
196d32ca5adSJuan Quintela 
197d32ca5adSJuan Quintela     ret = qio_channel_read_all(c, (char *)&msg, sizeof(msg), errp);
198d32ca5adSJuan Quintela     if (ret != 0) {
199d32ca5adSJuan Quintela         return -1;
200d32ca5adSJuan Quintela     }
201d32ca5adSJuan Quintela 
202d32ca5adSJuan Quintela     msg.magic = be32_to_cpu(msg.magic);
203d32ca5adSJuan Quintela     msg.version = be32_to_cpu(msg.version);
204d32ca5adSJuan Quintela 
205d32ca5adSJuan Quintela     if (msg.magic != MULTIFD_MAGIC) {
206d32ca5adSJuan Quintela         error_setg(errp, "multifd: received packet magic %x "
207d32ca5adSJuan Quintela                    "expected %x", msg.magic, MULTIFD_MAGIC);
208d32ca5adSJuan Quintela         return -1;
209d32ca5adSJuan Quintela     }
210d32ca5adSJuan Quintela 
211d32ca5adSJuan Quintela     if (msg.version != MULTIFD_VERSION) {
21204e11404SJuan Quintela         error_setg(errp, "multifd: received packet version %u "
21304e11404SJuan Quintela                    "expected %u", msg.version, MULTIFD_VERSION);
214d32ca5adSJuan Quintela         return -1;
215d32ca5adSJuan Quintela     }
216d32ca5adSJuan Quintela 
217d32ca5adSJuan Quintela     if (memcmp(msg.uuid, &qemu_uuid, sizeof(qemu_uuid))) {
218d32ca5adSJuan Quintela         char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid);
219d32ca5adSJuan Quintela         char *msg_uuid = qemu_uuid_unparse_strdup((const QemuUUID *)msg.uuid);
220d32ca5adSJuan Quintela 
221d32ca5adSJuan Quintela         error_setg(errp, "multifd: received uuid '%s' and expected "
222d32ca5adSJuan Quintela                    "uuid '%s' for channel %hhd", msg_uuid, uuid, msg.id);
223d32ca5adSJuan Quintela         g_free(uuid);
224d32ca5adSJuan Quintela         g_free(msg_uuid);
225d32ca5adSJuan Quintela         return -1;
226d32ca5adSJuan Quintela     }
227d32ca5adSJuan Quintela 
228d32ca5adSJuan Quintela     if (msg.id > migrate_multifd_channels()) {
22904e11404SJuan Quintela         error_setg(errp, "multifd: received channel version %u "
23004e11404SJuan Quintela                    "expected %u", msg.version, MULTIFD_VERSION);
231d32ca5adSJuan Quintela         return -1;
232d32ca5adSJuan Quintela     }
233d32ca5adSJuan Quintela 
234d32ca5adSJuan Quintela     return msg.id;
235d32ca5adSJuan Quintela }
236d32ca5adSJuan Quintela 
237d32ca5adSJuan Quintela static MultiFDPages_t *multifd_pages_init(size_t size)
238d32ca5adSJuan Quintela {
239d32ca5adSJuan Quintela     MultiFDPages_t *pages = g_new0(MultiFDPages_t, 1);
240d32ca5adSJuan Quintela 
241d32ca5adSJuan Quintela     pages->allocated = size;
242d32ca5adSJuan Quintela     pages->offset = g_new0(ram_addr_t, size);
243d32ca5adSJuan Quintela 
244d32ca5adSJuan Quintela     return pages;
245d32ca5adSJuan Quintela }
246d32ca5adSJuan Quintela 
247d32ca5adSJuan Quintela static void multifd_pages_clear(MultiFDPages_t *pages)
248d32ca5adSJuan Quintela {
24990a3d2f9SJuan Quintela     pages->num = 0;
250d32ca5adSJuan Quintela     pages->allocated = 0;
251d32ca5adSJuan Quintela     pages->packet_num = 0;
252d32ca5adSJuan Quintela     pages->block = NULL;
253d32ca5adSJuan Quintela     g_free(pages->offset);
254d32ca5adSJuan Quintela     pages->offset = NULL;
255d32ca5adSJuan Quintela     g_free(pages);
256d32ca5adSJuan Quintela }
257d32ca5adSJuan Quintela 
258d32ca5adSJuan Quintela static void multifd_send_fill_packet(MultiFDSendParams *p)
259d32ca5adSJuan Quintela {
260d32ca5adSJuan Quintela     MultiFDPacket_t *packet = p->packet;
261d32ca5adSJuan Quintela     int i;
262d32ca5adSJuan Quintela 
263d32ca5adSJuan Quintela     packet->flags = cpu_to_be32(p->flags);
264d32ca5adSJuan Quintela     packet->pages_alloc = cpu_to_be32(p->pages->allocated);
2658c0ec0b2SJuan Quintela     packet->normal_pages = cpu_to_be32(p->normal_num);
266d32ca5adSJuan Quintela     packet->next_packet_size = cpu_to_be32(p->next_packet_size);
267d32ca5adSJuan Quintela     packet->packet_num = cpu_to_be64(p->packet_num);
268d32ca5adSJuan Quintela 
269d32ca5adSJuan Quintela     if (p->pages->block) {
270d32ca5adSJuan Quintela         strncpy(packet->ramblock, p->pages->block->idstr, 256);
271d32ca5adSJuan Quintela     }
272d32ca5adSJuan Quintela 
273815956f0SJuan Quintela     for (i = 0; i < p->normal_num; i++) {
274d32ca5adSJuan Quintela         /* there are architectures where ram_addr_t is 32 bit */
275815956f0SJuan Quintela         uint64_t temp = p->normal[i];
276d32ca5adSJuan Quintela 
277d32ca5adSJuan Quintela         packet->offset[i] = cpu_to_be64(temp);
278d32ca5adSJuan Quintela     }
279d32ca5adSJuan Quintela }
280d32ca5adSJuan Quintela 
281d32ca5adSJuan Quintela static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp)
282d32ca5adSJuan Quintela {
283d32ca5adSJuan Quintela     MultiFDPacket_t *packet = p->packet;
284d32ca5adSJuan Quintela     RAMBlock *block;
285d32ca5adSJuan Quintela     int i;
286d32ca5adSJuan Quintela 
287d32ca5adSJuan Quintela     packet->magic = be32_to_cpu(packet->magic);
288d32ca5adSJuan Quintela     if (packet->magic != MULTIFD_MAGIC) {
289d32ca5adSJuan Quintela         error_setg(errp, "multifd: received packet "
290d32ca5adSJuan Quintela                    "magic %x and expected magic %x",
291d32ca5adSJuan Quintela                    packet->magic, MULTIFD_MAGIC);
292d32ca5adSJuan Quintela         return -1;
293d32ca5adSJuan Quintela     }
294d32ca5adSJuan Quintela 
295d32ca5adSJuan Quintela     packet->version = be32_to_cpu(packet->version);
296d32ca5adSJuan Quintela     if (packet->version != MULTIFD_VERSION) {
297d32ca5adSJuan Quintela         error_setg(errp, "multifd: received packet "
29804e11404SJuan Quintela                    "version %u and expected version %u",
299d32ca5adSJuan Quintela                    packet->version, MULTIFD_VERSION);
300d32ca5adSJuan Quintela         return -1;
301d32ca5adSJuan Quintela     }
302d32ca5adSJuan Quintela 
303d32ca5adSJuan Quintela     p->flags = be32_to_cpu(packet->flags);
304d32ca5adSJuan Quintela 
305d32ca5adSJuan Quintela     packet->pages_alloc = be32_to_cpu(packet->pages_alloc);
306d32ca5adSJuan Quintela     /*
307d32ca5adSJuan Quintela      * If we received a packet that is 100 times bigger than expected
308d32ca5adSJuan Quintela      * just stop migration.  It is a magic number.
309d32ca5adSJuan Quintela      */
310d6f45ebaSJuan Quintela     if (packet->pages_alloc > p->page_count) {
311d32ca5adSJuan Quintela         error_setg(errp, "multifd: received packet "
312cf2d4aa8SJuan Quintela                    "with size %u and expected a size of %u",
313d6f45ebaSJuan Quintela                    packet->pages_alloc, p->page_count) ;
314d32ca5adSJuan Quintela         return -1;
315d32ca5adSJuan Quintela     }
316d32ca5adSJuan Quintela 
3178c0ec0b2SJuan Quintela     p->normal_num = be32_to_cpu(packet->normal_pages);
318cf2d4aa8SJuan Quintela     if (p->normal_num > packet->pages_alloc) {
319d32ca5adSJuan Quintela         error_setg(errp, "multifd: received packet "
32004e11404SJuan Quintela                    "with %u pages and expected maximum pages are %u",
321cf2d4aa8SJuan Quintela                    p->normal_num, packet->pages_alloc) ;
322d32ca5adSJuan Quintela         return -1;
323d32ca5adSJuan Quintela     }
324d32ca5adSJuan Quintela 
325d32ca5adSJuan Quintela     p->next_packet_size = be32_to_cpu(packet->next_packet_size);
326d32ca5adSJuan Quintela     p->packet_num = be64_to_cpu(packet->packet_num);
327d32ca5adSJuan Quintela 
328cf2d4aa8SJuan Quintela     if (p->normal_num == 0) {
329d32ca5adSJuan Quintela         return 0;
330d32ca5adSJuan Quintela     }
331d32ca5adSJuan Quintela 
332d32ca5adSJuan Quintela     /* make sure that ramblock is 0 terminated */
333d32ca5adSJuan Quintela     packet->ramblock[255] = 0;
334d32ca5adSJuan Quintela     block = qemu_ram_block_by_name(packet->ramblock);
335d32ca5adSJuan Quintela     if (!block) {
336d32ca5adSJuan Quintela         error_setg(errp, "multifd: unknown ram block %s",
337d32ca5adSJuan Quintela                    packet->ramblock);
338d32ca5adSJuan Quintela         return -1;
339d32ca5adSJuan Quintela     }
340d32ca5adSJuan Quintela 
341faf60935SJuan Quintela     p->host = block->host;
342cf2d4aa8SJuan Quintela     for (i = 0; i < p->normal_num; i++) {
343d32ca5adSJuan Quintela         uint64_t offset = be64_to_cpu(packet->offset[i]);
344d32ca5adSJuan Quintela 
345ddec20f8SJuan Quintela         if (offset > (block->used_length - p->page_size)) {
346d32ca5adSJuan Quintela             error_setg(errp, "multifd: offset too long %" PRIu64
347d32ca5adSJuan Quintela                        " (max " RAM_ADDR_FMT ")",
348c1668bdeSDavid Hildenbrand                        offset, block->used_length);
349d32ca5adSJuan Quintela             return -1;
350d32ca5adSJuan Quintela         }
351cf2d4aa8SJuan Quintela         p->normal[i] = offset;
352d32ca5adSJuan Quintela     }
353d32ca5adSJuan Quintela 
354d32ca5adSJuan Quintela     return 0;
355d32ca5adSJuan Quintela }
356d32ca5adSJuan Quintela 
357d32ca5adSJuan Quintela struct {
358d32ca5adSJuan Quintela     MultiFDSendParams *params;
359d32ca5adSJuan Quintela     /* array of pages to sent */
360d32ca5adSJuan Quintela     MultiFDPages_t *pages;
361d32ca5adSJuan Quintela     /* global number of generated multifd packets */
362d32ca5adSJuan Quintela     uint64_t packet_num;
363d32ca5adSJuan Quintela     /* send channels ready */
364d32ca5adSJuan Quintela     QemuSemaphore channels_ready;
365d32ca5adSJuan Quintela     /*
366d32ca5adSJuan Quintela      * Have we already run terminate threads.  There is a race when it
367d32ca5adSJuan Quintela      * happens that we got one error while we are exiting.
368d32ca5adSJuan Quintela      * We will use atomic operations.  Only valid values are 0 and 1.
369d32ca5adSJuan Quintela      */
370d32ca5adSJuan Quintela     int exiting;
371ab7cbb0bSJuan Quintela     /* multifd ops */
372ab7cbb0bSJuan Quintela     MultiFDMethods *ops;
373d32ca5adSJuan Quintela } *multifd_send_state;
374d32ca5adSJuan Quintela 
375d32ca5adSJuan Quintela /*
376d32ca5adSJuan Quintela  * How we use multifd_send_state->pages and channel->pages?
377d32ca5adSJuan Quintela  *
378d32ca5adSJuan Quintela  * We create a pages for each channel, and a main one.  Each time that
379d32ca5adSJuan Quintela  * we need to send a batch of pages we interchange the ones between
380d32ca5adSJuan Quintela  * multifd_send_state and the channel that is sending it.  There are
381d32ca5adSJuan Quintela  * two reasons for that:
382d32ca5adSJuan Quintela  *    - to not have to do so many mallocs during migration
383d32ca5adSJuan Quintela  *    - to make easier to know what to free at the end of migration
384d32ca5adSJuan Quintela  *
385d32ca5adSJuan Quintela  * This way we always know who is the owner of each "pages" struct,
386d32ca5adSJuan Quintela  * and we don't need any locking.  It belongs to the migration thread
387d32ca5adSJuan Quintela  * or to the channel thread.  Switching is safe because the migration
388d32ca5adSJuan Quintela  * thread is using the channel mutex when changing it, and the channel
389d32ca5adSJuan Quintela  * have to had finish with its own, otherwise pending_job can't be
390d32ca5adSJuan Quintela  * false.
391d32ca5adSJuan Quintela  */
392d32ca5adSJuan Quintela 
393d32ca5adSJuan Quintela static int multifd_send_pages(QEMUFile *f)
394d32ca5adSJuan Quintela {
395d32ca5adSJuan Quintela     int i;
396d32ca5adSJuan Quintela     static int next_channel;
397d32ca5adSJuan Quintela     MultiFDSendParams *p = NULL; /* make happy gcc */
398d32ca5adSJuan Quintela     MultiFDPages_t *pages = multifd_send_state->pages;
399d32ca5adSJuan Quintela     uint64_t transferred;
400d32ca5adSJuan Quintela 
401d73415a3SStefan Hajnoczi     if (qatomic_read(&multifd_send_state->exiting)) {
402d32ca5adSJuan Quintela         return -1;
403d32ca5adSJuan Quintela     }
404d32ca5adSJuan Quintela 
405d32ca5adSJuan Quintela     qemu_sem_wait(&multifd_send_state->channels_ready);
4067e89a140SLaurent Vivier     /*
4077e89a140SLaurent Vivier      * next_channel can remain from a previous migration that was
4087e89a140SLaurent Vivier      * using more channels, so ensure it doesn't overflow if the
4097e89a140SLaurent Vivier      * limit is lower now.
4107e89a140SLaurent Vivier      */
4117e89a140SLaurent Vivier     next_channel %= migrate_multifd_channels();
412d32ca5adSJuan Quintela     for (i = next_channel;; i = (i + 1) % migrate_multifd_channels()) {
413d32ca5adSJuan Quintela         p = &multifd_send_state->params[i];
414d32ca5adSJuan Quintela 
415d32ca5adSJuan Quintela         qemu_mutex_lock(&p->mutex);
416d32ca5adSJuan Quintela         if (p->quit) {
417d32ca5adSJuan Quintela             error_report("%s: channel %d has already quit!", __func__, i);
418d32ca5adSJuan Quintela             qemu_mutex_unlock(&p->mutex);
419d32ca5adSJuan Quintela             return -1;
420d32ca5adSJuan Quintela         }
421d32ca5adSJuan Quintela         if (!p->pending_job) {
422d32ca5adSJuan Quintela             p->pending_job++;
423d32ca5adSJuan Quintela             next_channel = (i + 1) % migrate_multifd_channels();
424d32ca5adSJuan Quintela             break;
425d32ca5adSJuan Quintela         }
426d32ca5adSJuan Quintela         qemu_mutex_unlock(&p->mutex);
427d32ca5adSJuan Quintela     }
42890a3d2f9SJuan Quintela     assert(!p->pages->num);
429d32ca5adSJuan Quintela     assert(!p->pages->block);
430d32ca5adSJuan Quintela 
431d32ca5adSJuan Quintela     p->packet_num = multifd_send_state->packet_num++;
432d32ca5adSJuan Quintela     multifd_send_state->pages = p->pages;
433d32ca5adSJuan Quintela     p->pages = pages;
434ddec20f8SJuan Quintela     transferred = ((uint64_t) pages->num) * p->page_size + p->packet_len;
435bc698c36SDaniel P. Berrangé     qemu_file_acct_rate_limit(f, transferred);
436d32ca5adSJuan Quintela     qemu_mutex_unlock(&p->mutex);
43730fb22cdSJuan Quintela     stat64_add(&ram_counters.transferred, transferred);
438cf671116SJuan Quintela     stat64_add(&ram_counters.multifd_bytes, transferred);
439d32ca5adSJuan Quintela     qemu_sem_post(&p->sem);
440d32ca5adSJuan Quintela 
441d32ca5adSJuan Quintela     return 1;
442d32ca5adSJuan Quintela }
443d32ca5adSJuan Quintela 
444d32ca5adSJuan Quintela int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset)
445d32ca5adSJuan Quintela {
446d32ca5adSJuan Quintela     MultiFDPages_t *pages = multifd_send_state->pages;
447ddbe628cSZhenzhong Duan     bool changed = false;
448d32ca5adSJuan Quintela 
449d32ca5adSJuan Quintela     if (!pages->block) {
450d32ca5adSJuan Quintela         pages->block = block;
451d32ca5adSJuan Quintela     }
452d32ca5adSJuan Quintela 
453d32ca5adSJuan Quintela     if (pages->block == block) {
45490a3d2f9SJuan Quintela         pages->offset[pages->num] = offset;
45590a3d2f9SJuan Quintela         pages->num++;
456d32ca5adSJuan Quintela 
45790a3d2f9SJuan Quintela         if (pages->num < pages->allocated) {
458d32ca5adSJuan Quintela             return 1;
459d32ca5adSJuan Quintela         }
460ddbe628cSZhenzhong Duan     } else {
461ddbe628cSZhenzhong Duan         changed = true;
462d32ca5adSJuan Quintela     }
463d32ca5adSJuan Quintela 
464d32ca5adSJuan Quintela     if (multifd_send_pages(f) < 0) {
465d32ca5adSJuan Quintela         return -1;
466d32ca5adSJuan Quintela     }
467d32ca5adSJuan Quintela 
468ddbe628cSZhenzhong Duan     if (changed) {
469d32ca5adSJuan Quintela         return multifd_queue_page(f, block, offset);
470d32ca5adSJuan Quintela     }
471d32ca5adSJuan Quintela 
472d32ca5adSJuan Quintela     return 1;
473d32ca5adSJuan Quintela }
474d32ca5adSJuan Quintela 
475d32ca5adSJuan Quintela static void multifd_send_terminate_threads(Error *err)
476d32ca5adSJuan Quintela {
477d32ca5adSJuan Quintela     int i;
478d32ca5adSJuan Quintela 
479d32ca5adSJuan Quintela     trace_multifd_send_terminate_threads(err != NULL);
480d32ca5adSJuan Quintela 
481d32ca5adSJuan Quintela     if (err) {
482d32ca5adSJuan Quintela         MigrationState *s = migrate_get_current();
483d32ca5adSJuan Quintela         migrate_set_error(s, err);
484d32ca5adSJuan Quintela         if (s->state == MIGRATION_STATUS_SETUP ||
485d32ca5adSJuan Quintela             s->state == MIGRATION_STATUS_PRE_SWITCHOVER ||
486d32ca5adSJuan Quintela             s->state == MIGRATION_STATUS_DEVICE ||
487d32ca5adSJuan Quintela             s->state == MIGRATION_STATUS_ACTIVE) {
488d32ca5adSJuan Quintela             migrate_set_state(&s->state, s->state,
489d32ca5adSJuan Quintela                               MIGRATION_STATUS_FAILED);
490d32ca5adSJuan Quintela         }
491d32ca5adSJuan Quintela     }
492d32ca5adSJuan Quintela 
493d32ca5adSJuan Quintela     /*
494d32ca5adSJuan Quintela      * We don't want to exit each threads twice.  Depending on where
495d32ca5adSJuan Quintela      * we get the error, or if there are two independent errors in two
496d32ca5adSJuan Quintela      * threads at the same time, we can end calling this function
497d32ca5adSJuan Quintela      * twice.
498d32ca5adSJuan Quintela      */
499d73415a3SStefan Hajnoczi     if (qatomic_xchg(&multifd_send_state->exiting, 1)) {
500d32ca5adSJuan Quintela         return;
501d32ca5adSJuan Quintela     }
502d32ca5adSJuan Quintela 
503d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
504d32ca5adSJuan Quintela         MultiFDSendParams *p = &multifd_send_state->params[i];
505d32ca5adSJuan Quintela 
506d32ca5adSJuan Quintela         qemu_mutex_lock(&p->mutex);
507d32ca5adSJuan Quintela         p->quit = true;
508d32ca5adSJuan Quintela         qemu_sem_post(&p->sem);
509077fbb59SLi Zhang         if (p->c) {
510077fbb59SLi Zhang             qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
511077fbb59SLi Zhang         }
512d32ca5adSJuan Quintela         qemu_mutex_unlock(&p->mutex);
513d32ca5adSJuan Quintela     }
514d32ca5adSJuan Quintela }
515d32ca5adSJuan Quintela 
516d32ca5adSJuan Quintela void multifd_save_cleanup(void)
517d32ca5adSJuan Quintela {
518d32ca5adSJuan Quintela     int i;
519d32ca5adSJuan Quintela 
52051b07548SJuan Quintela     if (!migrate_multifd()) {
521d32ca5adSJuan Quintela         return;
522d32ca5adSJuan Quintela     }
523d32ca5adSJuan Quintela     multifd_send_terminate_threads(NULL);
524d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
525d32ca5adSJuan Quintela         MultiFDSendParams *p = &multifd_send_state->params[i];
526d32ca5adSJuan Quintela 
527d32ca5adSJuan Quintela         if (p->running) {
528d32ca5adSJuan Quintela             qemu_thread_join(&p->thread);
529d32ca5adSJuan Quintela         }
530d32ca5adSJuan Quintela     }
531d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
532d32ca5adSJuan Quintela         MultiFDSendParams *p = &multifd_send_state->params[i];
533ab7cbb0bSJuan Quintela         Error *local_err = NULL;
534d32ca5adSJuan Quintela 
53520171ea8SLukas Straub         if (p->registered_yank) {
53620171ea8SLukas Straub             migration_ioc_unregister_yank(p->c);
53720171ea8SLukas Straub         }
538d32ca5adSJuan Quintela         socket_send_channel_destroy(p->c);
539d32ca5adSJuan Quintela         p->c = NULL;
540d32ca5adSJuan Quintela         qemu_mutex_destroy(&p->mutex);
541d32ca5adSJuan Quintela         qemu_sem_destroy(&p->sem);
542d32ca5adSJuan Quintela         qemu_sem_destroy(&p->sem_sync);
543d32ca5adSJuan Quintela         g_free(p->name);
544d32ca5adSJuan Quintela         p->name = NULL;
545d32ca5adSJuan Quintela         multifd_pages_clear(p->pages);
546d32ca5adSJuan Quintela         p->pages = NULL;
547d32ca5adSJuan Quintela         p->packet_len = 0;
548d32ca5adSJuan Quintela         g_free(p->packet);
549d32ca5adSJuan Quintela         p->packet = NULL;
550226468baSJuan Quintela         g_free(p->iov);
551226468baSJuan Quintela         p->iov = NULL;
552815956f0SJuan Quintela         g_free(p->normal);
553815956f0SJuan Quintela         p->normal = NULL;
554ab7cbb0bSJuan Quintela         multifd_send_state->ops->send_cleanup(p, &local_err);
555ab7cbb0bSJuan Quintela         if (local_err) {
556ab7cbb0bSJuan Quintela             migrate_set_error(migrate_get_current(), local_err);
55713f2cb21SPan Nengyuan             error_free(local_err);
558ab7cbb0bSJuan Quintela         }
559d32ca5adSJuan Quintela     }
560d32ca5adSJuan Quintela     qemu_sem_destroy(&multifd_send_state->channels_ready);
561d32ca5adSJuan Quintela     g_free(multifd_send_state->params);
562d32ca5adSJuan Quintela     multifd_send_state->params = NULL;
563d32ca5adSJuan Quintela     multifd_pages_clear(multifd_send_state->pages);
564d32ca5adSJuan Quintela     multifd_send_state->pages = NULL;
565d32ca5adSJuan Quintela     g_free(multifd_send_state);
566d32ca5adSJuan Quintela     multifd_send_state = NULL;
567d32ca5adSJuan Quintela }
568d32ca5adSJuan Quintela 
5694cc47b43SLeonardo Bras static int multifd_zero_copy_flush(QIOChannel *c)
5704cc47b43SLeonardo Bras {
5714cc47b43SLeonardo Bras     int ret;
5724cc47b43SLeonardo Bras     Error *err = NULL;
5734cc47b43SLeonardo Bras 
5744cc47b43SLeonardo Bras     ret = qio_channel_flush(c, &err);
5754cc47b43SLeonardo Bras     if (ret < 0) {
5764cc47b43SLeonardo Bras         error_report_err(err);
5774cc47b43SLeonardo Bras         return -1;
5784cc47b43SLeonardo Bras     }
5794cc47b43SLeonardo Bras     if (ret == 1) {
58042918236SJuan Quintela         stat64_add(&ram_counters.dirty_sync_missed_zero_copy, 1);
5814cc47b43SLeonardo Bras     }
5824cc47b43SLeonardo Bras 
5834cc47b43SLeonardo Bras     return ret;
5844cc47b43SLeonardo Bras }
5854cc47b43SLeonardo Bras 
58633d70973SLeonardo Bras int multifd_send_sync_main(QEMUFile *f)
587d32ca5adSJuan Quintela {
588d32ca5adSJuan Quintela     int i;
5895b1d9babSLeonardo Bras     bool flush_zero_copy;
590d32ca5adSJuan Quintela 
59151b07548SJuan Quintela     if (!migrate_multifd()) {
59233d70973SLeonardo Bras         return 0;
593d32ca5adSJuan Quintela     }
59490a3d2f9SJuan Quintela     if (multifd_send_state->pages->num) {
595d32ca5adSJuan Quintela         if (multifd_send_pages(f) < 0) {
596d32ca5adSJuan Quintela             error_report("%s: multifd_send_pages fail", __func__);
59733d70973SLeonardo Bras             return -1;
598d32ca5adSJuan Quintela         }
599d32ca5adSJuan Quintela     }
6005b1d9babSLeonardo Bras 
6015b1d9babSLeonardo Bras     /*
6025b1d9babSLeonardo Bras      * When using zero-copy, it's necessary to flush the pages before any of
6035b1d9babSLeonardo Bras      * the pages can be sent again, so we'll make sure the new version of the
6045b1d9babSLeonardo Bras      * pages will always arrive _later_ than the old pages.
6055b1d9babSLeonardo Bras      *
6065b1d9babSLeonardo Bras      * Currently we achieve this by flushing the zero-page requested writes
6075b1d9babSLeonardo Bras      * per ram iteration, but in the future we could potentially optimize it
6085b1d9babSLeonardo Bras      * to be less frequent, e.g. only after we finished one whole scanning of
6095b1d9babSLeonardo Bras      * all the dirty bitmaps.
6105b1d9babSLeonardo Bras      */
6115b1d9babSLeonardo Bras 
612b4bc342cSJuan Quintela     flush_zero_copy = migrate_zero_copy_send();
6135b1d9babSLeonardo Bras 
614d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
615d32ca5adSJuan Quintela         MultiFDSendParams *p = &multifd_send_state->params[i];
616d32ca5adSJuan Quintela 
617d32ca5adSJuan Quintela         trace_multifd_send_sync_main_signal(p->id);
618d32ca5adSJuan Quintela 
619d32ca5adSJuan Quintela         qemu_mutex_lock(&p->mutex);
620d32ca5adSJuan Quintela 
621d32ca5adSJuan Quintela         if (p->quit) {
622d32ca5adSJuan Quintela             error_report("%s: channel %d has already quit", __func__, i);
623d32ca5adSJuan Quintela             qemu_mutex_unlock(&p->mutex);
62433d70973SLeonardo Bras             return -1;
625d32ca5adSJuan Quintela         }
626d32ca5adSJuan Quintela 
627d32ca5adSJuan Quintela         p->packet_num = multifd_send_state->packet_num++;
628d32ca5adSJuan Quintela         p->flags |= MULTIFD_FLAG_SYNC;
629d32ca5adSJuan Quintela         p->pending_job++;
630d32ca5adSJuan Quintela         qemu_mutex_unlock(&p->mutex);
631d32ca5adSJuan Quintela         qemu_sem_post(&p->sem);
632d32ca5adSJuan Quintela     }
633d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
634d32ca5adSJuan Quintela         MultiFDSendParams *p = &multifd_send_state->params[i];
635d32ca5adSJuan Quintela 
636d2026ee1SJuan Quintela         qemu_sem_wait(&multifd_send_state->channels_ready);
637d32ca5adSJuan Quintela         trace_multifd_send_sync_main_wait(p->id);
638d32ca5adSJuan Quintela         qemu_sem_wait(&p->sem_sync);
639ebfc5787SZhenzhong Duan 
640ebfc5787SZhenzhong Duan         if (flush_zero_copy && p->c && (multifd_zero_copy_flush(p->c) < 0)) {
641ebfc5787SZhenzhong Duan             return -1;
642ebfc5787SZhenzhong Duan         }
643d32ca5adSJuan Quintela     }
644d32ca5adSJuan Quintela     trace_multifd_send_sync_main(multifd_send_state->packet_num);
64533d70973SLeonardo Bras 
64633d70973SLeonardo Bras     return 0;
647d32ca5adSJuan Quintela }
648d32ca5adSJuan Quintela 
649d32ca5adSJuan Quintela static void *multifd_send_thread(void *opaque)
650d32ca5adSJuan Quintela {
651d32ca5adSJuan Quintela     MultiFDSendParams *p = opaque;
6521b1f4ab6SJiang Jiacheng     MigrationThread *thread = NULL;
653d32ca5adSJuan Quintela     Error *local_err = NULL;
654d32ca5adSJuan Quintela     int ret = 0;
655b4bc342cSJuan Quintela     bool use_zero_copy_send = migrate_zero_copy_send();
656d32ca5adSJuan Quintela 
6571b1f4ab6SJiang Jiacheng     thread = MigrationThreadAdd(p->name, qemu_get_thread_id());
6581b1f4ab6SJiang Jiacheng 
659d32ca5adSJuan Quintela     trace_multifd_send_thread_start(p->id);
660d32ca5adSJuan Quintela     rcu_register_thread();
661d32ca5adSJuan Quintela 
662d32ca5adSJuan Quintela     if (multifd_send_initial_packet(p, &local_err) < 0) {
663d32ca5adSJuan Quintela         ret = -1;
664d32ca5adSJuan Quintela         goto out;
665d32ca5adSJuan Quintela     }
666d32ca5adSJuan Quintela     /* initial packet */
667d32ca5adSJuan Quintela     p->num_packets = 1;
668d32ca5adSJuan Quintela 
669d32ca5adSJuan Quintela     while (true) {
670d2026ee1SJuan Quintela         qemu_sem_post(&multifd_send_state->channels_ready);
671d32ca5adSJuan Quintela         qemu_sem_wait(&p->sem);
672d32ca5adSJuan Quintela 
673d73415a3SStefan Hajnoczi         if (qatomic_read(&multifd_send_state->exiting)) {
674d32ca5adSJuan Quintela             break;
675d32ca5adSJuan Quintela         }
676d32ca5adSJuan Quintela         qemu_mutex_lock(&p->mutex);
677d32ca5adSJuan Quintela 
678d32ca5adSJuan Quintela         if (p->pending_job) {
679d32ca5adSJuan Quintela             uint64_t packet_num = p->packet_num;
680ff1585d1SWei Wang             uint32_t flags;
681815956f0SJuan Quintela             p->normal_num = 0;
682d32ca5adSJuan Quintela 
683b7dbdd8eSLeonardo Bras             if (use_zero_copy_send) {
684b7dbdd8eSLeonardo Bras                 p->iovs_num = 0;
685b7dbdd8eSLeonardo Bras             } else {
686b7dbdd8eSLeonardo Bras                 p->iovs_num = 1;
687b7dbdd8eSLeonardo Bras             }
688b7dbdd8eSLeonardo Bras 
689815956f0SJuan Quintela             for (int i = 0; i < p->pages->num; i++) {
690815956f0SJuan Quintela                 p->normal[p->normal_num] = p->pages->offset[i];
691815956f0SJuan Quintela                 p->normal_num++;
692815956f0SJuan Quintela             }
693815956f0SJuan Quintela 
694815956f0SJuan Quintela             if (p->normal_num) {
69502fb8104SJuan Quintela                 ret = multifd_send_state->ops->send_prepare(p, &local_err);
696ab7cbb0bSJuan Quintela                 if (ret != 0) {
697ab7cbb0bSJuan Quintela                     qemu_mutex_unlock(&p->mutex);
698ab7cbb0bSJuan Quintela                     break;
699ab7cbb0bSJuan Quintela                 }
700ab7cbb0bSJuan Quintela             }
701d32ca5adSJuan Quintela             multifd_send_fill_packet(p);
702ff1585d1SWei Wang             flags = p->flags;
703d32ca5adSJuan Quintela             p->flags = 0;
704d32ca5adSJuan Quintela             p->num_packets++;
705815956f0SJuan Quintela             p->total_normal_pages += p->normal_num;
70690a3d2f9SJuan Quintela             p->pages->num = 0;
707d32ca5adSJuan Quintela             p->pages->block = NULL;
708d32ca5adSJuan Quintela             qemu_mutex_unlock(&p->mutex);
709d32ca5adSJuan Quintela 
710815956f0SJuan Quintela             trace_multifd_send(p->id, packet_num, p->normal_num, flags,
711d32ca5adSJuan Quintela                                p->next_packet_size);
712d32ca5adSJuan Quintela 
713b7dbdd8eSLeonardo Bras             if (use_zero_copy_send) {
714b7dbdd8eSLeonardo Bras                 /* Send header first, without zerocopy */
715b7dbdd8eSLeonardo Bras                 ret = qio_channel_write_all(p->c, (void *)p->packet,
716b7dbdd8eSLeonardo Bras                                             p->packet_len, &local_err);
717b7dbdd8eSLeonardo Bras                 if (ret != 0) {
718b7dbdd8eSLeonardo Bras                     break;
719b7dbdd8eSLeonardo Bras                 }
720b7dbdd8eSLeonardo Bras             } else {
721b7dbdd8eSLeonardo Bras                 /* Send header using the same writev call */
722d48c3a04SJuan Quintela                 p->iov[0].iov_len = p->packet_len;
723d48c3a04SJuan Quintela                 p->iov[0].iov_base = p->packet;
724b7dbdd8eSLeonardo Bras             }
725d32ca5adSJuan Quintela 
7265b1d9babSLeonardo Bras             ret = qio_channel_writev_full_all(p->c, p->iov, p->iovs_num, NULL,
7275b1d9babSLeonardo Bras                                               0, p->write_flags, &local_err);
728d32ca5adSJuan Quintela             if (ret != 0) {
729d32ca5adSJuan Quintela                 break;
730d32ca5adSJuan Quintela             }
731d32ca5adSJuan Quintela 
732d32ca5adSJuan Quintela             qemu_mutex_lock(&p->mutex);
733d32ca5adSJuan Quintela             p->pending_job--;
734d32ca5adSJuan Quintela             qemu_mutex_unlock(&p->mutex);
735d32ca5adSJuan Quintela 
736d32ca5adSJuan Quintela             if (flags & MULTIFD_FLAG_SYNC) {
737d32ca5adSJuan Quintela                 qemu_sem_post(&p->sem_sync);
738d32ca5adSJuan Quintela             }
739d32ca5adSJuan Quintela         } else if (p->quit) {
740d32ca5adSJuan Quintela             qemu_mutex_unlock(&p->mutex);
741d32ca5adSJuan Quintela             break;
742d32ca5adSJuan Quintela         } else {
743d32ca5adSJuan Quintela             qemu_mutex_unlock(&p->mutex);
744d32ca5adSJuan Quintela             /* sometimes there are spurious wakeups */
745d32ca5adSJuan Quintela         }
746d32ca5adSJuan Quintela     }
747d32ca5adSJuan Quintela 
748d32ca5adSJuan Quintela out:
749d32ca5adSJuan Quintela     if (local_err) {
750d32ca5adSJuan Quintela         trace_multifd_send_error(p->id);
751d32ca5adSJuan Quintela         multifd_send_terminate_threads(local_err);
75213f2cb21SPan Nengyuan         error_free(local_err);
753d32ca5adSJuan Quintela     }
754d32ca5adSJuan Quintela 
755d32ca5adSJuan Quintela     /*
756d32ca5adSJuan Quintela      * Error happen, I will exit, but I can't just leave, tell
757d32ca5adSJuan Quintela      * who pay attention to me.
758d32ca5adSJuan Quintela      */
759d32ca5adSJuan Quintela     if (ret != 0) {
760d32ca5adSJuan Quintela         qemu_sem_post(&p->sem_sync);
761d32ca5adSJuan Quintela         qemu_sem_post(&multifd_send_state->channels_ready);
762d32ca5adSJuan Quintela     }
763d32ca5adSJuan Quintela 
764d32ca5adSJuan Quintela     qemu_mutex_lock(&p->mutex);
765d32ca5adSJuan Quintela     p->running = false;
766d32ca5adSJuan Quintela     qemu_mutex_unlock(&p->mutex);
767d32ca5adSJuan Quintela 
768d32ca5adSJuan Quintela     rcu_unregister_thread();
7691b1f4ab6SJiang Jiacheng     MigrationThreadDel(thread);
770815956f0SJuan Quintela     trace_multifd_send_thread_end(p->id, p->num_packets, p->total_normal_pages);
771d32ca5adSJuan Quintela 
772d32ca5adSJuan Quintela     return NULL;
773d32ca5adSJuan Quintela }
774d32ca5adSJuan Quintela 
77529647140SChuan Zheng static bool multifd_channel_connect(MultiFDSendParams *p,
77629647140SChuan Zheng                                     QIOChannel *ioc,
77729647140SChuan Zheng                                     Error *error);
77829647140SChuan Zheng 
77929647140SChuan Zheng static void multifd_tls_outgoing_handshake(QIOTask *task,
78029647140SChuan Zheng                                            gpointer opaque)
78129647140SChuan Zheng {
78229647140SChuan Zheng     MultiFDSendParams *p = opaque;
78329647140SChuan Zheng     QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
78429647140SChuan Zheng     Error *err = NULL;
78529647140SChuan Zheng 
786894f0214SChuan Zheng     if (qio_task_propagate_error(task, &err)) {
787894f0214SChuan Zheng         trace_multifd_tls_outgoing_handshake_error(ioc, error_get_pretty(err));
788894f0214SChuan Zheng     } else {
789894f0214SChuan Zheng         trace_multifd_tls_outgoing_handshake_complete(ioc);
790894f0214SChuan Zheng     }
791fca67642SHao Wang 
792fca67642SHao Wang     if (!multifd_channel_connect(p, ioc, err)) {
793fca67642SHao Wang         /*
794fca67642SHao Wang          * Error happen, mark multifd_send_thread status as 'quit' although it
795fca67642SHao Wang          * is not created, and then tell who pay attention to me.
796fca67642SHao Wang          */
797fca67642SHao Wang         p->quit = true;
798fca67642SHao Wang         qemu_sem_post(&multifd_send_state->channels_ready);
799fca67642SHao Wang         qemu_sem_post(&p->sem_sync);
800fca67642SHao Wang     }
80129647140SChuan Zheng }
80229647140SChuan Zheng 
803a1af605bSChuan Zheng static void *multifd_tls_handshake_thread(void *opaque)
804a1af605bSChuan Zheng {
805a1af605bSChuan Zheng     MultiFDSendParams *p = opaque;
806a1af605bSChuan Zheng     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(p->c);
807a1af605bSChuan Zheng 
808a1af605bSChuan Zheng     qio_channel_tls_handshake(tioc,
809a1af605bSChuan Zheng                               multifd_tls_outgoing_handshake,
810a1af605bSChuan Zheng                               p,
811a1af605bSChuan Zheng                               NULL,
812a1af605bSChuan Zheng                               NULL);
813a1af605bSChuan Zheng     return NULL;
814a1af605bSChuan Zheng }
815a1af605bSChuan Zheng 
81629647140SChuan Zheng static void multifd_tls_channel_connect(MultiFDSendParams *p,
81729647140SChuan Zheng                                         QIOChannel *ioc,
81829647140SChuan Zheng                                         Error **errp)
81929647140SChuan Zheng {
82029647140SChuan Zheng     MigrationState *s = migrate_get_current();
8217f692ec7SPeter Xu     const char *hostname = s->hostname;
82229647140SChuan Zheng     QIOChannelTLS *tioc;
82329647140SChuan Zheng 
82429647140SChuan Zheng     tioc = migration_tls_client_create(s, ioc, hostname, errp);
82529647140SChuan Zheng     if (!tioc) {
82629647140SChuan Zheng         return;
82729647140SChuan Zheng     }
82829647140SChuan Zheng 
8299e842408SChuan Zheng     object_unref(OBJECT(ioc));
830894f0214SChuan Zheng     trace_multifd_tls_outgoing_handshake_start(ioc, tioc, hostname);
83129647140SChuan Zheng     qio_channel_set_name(QIO_CHANNEL(tioc), "multifd-tls-outgoing");
832a1af605bSChuan Zheng     p->c = QIO_CHANNEL(tioc);
833a1af605bSChuan Zheng     qemu_thread_create(&p->thread, "multifd-tls-handshake-worker",
834a1af605bSChuan Zheng                        multifd_tls_handshake_thread, p,
835a1af605bSChuan Zheng                        QEMU_THREAD_JOINABLE);
83629647140SChuan Zheng }
83729647140SChuan Zheng 
83829647140SChuan Zheng static bool multifd_channel_connect(MultiFDSendParams *p,
83929647140SChuan Zheng                                     QIOChannel *ioc,
84029647140SChuan Zheng                                     Error *error)
84129647140SChuan Zheng {
842894f0214SChuan Zheng     trace_multifd_set_outgoing_channel(
8437f692ec7SPeter Xu         ioc, object_get_typename(OBJECT(ioc)),
8447f692ec7SPeter Xu         migrate_get_current()->hostname, error);
845894f0214SChuan Zheng 
846e3f37b2cSLi Zhang     if (error) {
847e3f37b2cSLi Zhang         return false;
848e3f37b2cSLi Zhang     }
84985a8578eSPeter Xu     if (migrate_channel_requires_tls_upgrade(ioc)) {
85029647140SChuan Zheng         multifd_tls_channel_connect(p, ioc, &error);
85129647140SChuan Zheng         if (!error) {
85229647140SChuan Zheng             /*
85329647140SChuan Zheng              * tls_channel_connect will call back to this
85429647140SChuan Zheng              * function after the TLS handshake,
85529647140SChuan Zheng              * so we mustn't call multifd_send_thread until then
85629647140SChuan Zheng              */
85729647140SChuan Zheng             return true;
858a339149aSHao Wang         } else {
859a339149aSHao Wang             return false;
86029647140SChuan Zheng         }
86129647140SChuan Zheng     } else {
86220171ea8SLukas Straub         migration_ioc_register_yank(ioc);
86320171ea8SLukas Straub         p->registered_yank = true;
86429647140SChuan Zheng         p->c = ioc;
86529647140SChuan Zheng         qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
86629647140SChuan Zheng                            QEMU_THREAD_JOINABLE);
86729647140SChuan Zheng     }
868a339149aSHao Wang     return true;
86929647140SChuan Zheng }
87029647140SChuan Zheng 
87103c7a42dSChuan Zheng static void multifd_new_send_channel_cleanup(MultiFDSendParams *p,
87203c7a42dSChuan Zheng                                              QIOChannel *ioc, Error *err)
87303c7a42dSChuan Zheng {
87403c7a42dSChuan Zheng      migrate_set_error(migrate_get_current(), err);
87503c7a42dSChuan Zheng      /* Error happen, we need to tell who pay attention to me */
87603c7a42dSChuan Zheng      qemu_sem_post(&multifd_send_state->channels_ready);
87703c7a42dSChuan Zheng      qemu_sem_post(&p->sem_sync);
87803c7a42dSChuan Zheng      /*
87903c7a42dSChuan Zheng       * Although multifd_send_thread is not created, but main migration
88003c7a42dSChuan Zheng       * thread neet to judge whether it is running, so we need to mark
88103c7a42dSChuan Zheng       * its status.
88203c7a42dSChuan Zheng       */
88303c7a42dSChuan Zheng      p->quit = true;
88403c7a42dSChuan Zheng      object_unref(OBJECT(ioc));
88503c7a42dSChuan Zheng      error_free(err);
88603c7a42dSChuan Zheng }
88703c7a42dSChuan Zheng 
888d32ca5adSJuan Quintela static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque)
889d32ca5adSJuan Quintela {
890d32ca5adSJuan Quintela     MultiFDSendParams *p = opaque;
891d32ca5adSJuan Quintela     QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
892d32ca5adSJuan Quintela     Error *local_err = NULL;
893d32ca5adSJuan Quintela 
894d32ca5adSJuan Quintela     trace_multifd_new_send_channel_async(p->id);
895bca762c2SLi Zhang     if (!qio_task_propagate_error(task, &local_err)) {
896d32ca5adSJuan Quintela         p->c = QIO_CHANNEL(sioc);
897d32ca5adSJuan Quintela         qio_channel_set_delay(p->c, false);
898d32ca5adSJuan Quintela         p->running = true;
899bca762c2SLi Zhang         if (multifd_channel_connect(p, sioc, local_err)) {
90003c7a42dSChuan Zheng             return;
901d32ca5adSJuan Quintela         }
902bca762c2SLi Zhang     }
90303c7a42dSChuan Zheng 
90403c7a42dSChuan Zheng     multifd_new_send_channel_cleanup(p, sioc, local_err);
905d32ca5adSJuan Quintela }
906d32ca5adSJuan Quintela 
907d32ca5adSJuan Quintela int multifd_save_setup(Error **errp)
908d32ca5adSJuan Quintela {
909d32ca5adSJuan Quintela     int thread_count;
910d32ca5adSJuan Quintela     uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
911d32ca5adSJuan Quintela     uint8_t i;
912d32ca5adSJuan Quintela 
91351b07548SJuan Quintela     if (!migrate_multifd()) {
914d32ca5adSJuan Quintela         return 0;
915d32ca5adSJuan Quintela     }
916b7acd657SLi Zhijian 
917d32ca5adSJuan Quintela     thread_count = migrate_multifd_channels();
918d32ca5adSJuan Quintela     multifd_send_state = g_malloc0(sizeof(*multifd_send_state));
919d32ca5adSJuan Quintela     multifd_send_state->params = g_new0(MultiFDSendParams, thread_count);
920d32ca5adSJuan Quintela     multifd_send_state->pages = multifd_pages_init(page_count);
921d32ca5adSJuan Quintela     qemu_sem_init(&multifd_send_state->channels_ready, 0);
922d73415a3SStefan Hajnoczi     qatomic_set(&multifd_send_state->exiting, 0);
923ab7cbb0bSJuan Quintela     multifd_send_state->ops = multifd_ops[migrate_multifd_compression()];
924d32ca5adSJuan Quintela 
925d32ca5adSJuan Quintela     for (i = 0; i < thread_count; i++) {
926d32ca5adSJuan Quintela         MultiFDSendParams *p = &multifd_send_state->params[i];
927d32ca5adSJuan Quintela 
928d32ca5adSJuan Quintela         qemu_mutex_init(&p->mutex);
929d32ca5adSJuan Quintela         qemu_sem_init(&p->sem, 0);
930d32ca5adSJuan Quintela         qemu_sem_init(&p->sem_sync, 0);
931d32ca5adSJuan Quintela         p->quit = false;
932d32ca5adSJuan Quintela         p->pending_job = 0;
933d32ca5adSJuan Quintela         p->id = i;
934d32ca5adSJuan Quintela         p->pages = multifd_pages_init(page_count);
935d32ca5adSJuan Quintela         p->packet_len = sizeof(MultiFDPacket_t)
936d32ca5adSJuan Quintela                       + sizeof(uint64_t) * page_count;
937d32ca5adSJuan Quintela         p->packet = g_malloc0(p->packet_len);
938d32ca5adSJuan Quintela         p->packet->magic = cpu_to_be32(MULTIFD_MAGIC);
939d32ca5adSJuan Quintela         p->packet->version = cpu_to_be32(MULTIFD_VERSION);
940d32ca5adSJuan Quintela         p->name = g_strdup_printf("multifdsend_%d", i);
941d48c3a04SJuan Quintela         /* We need one extra place for the packet header */
942d48c3a04SJuan Quintela         p->iov = g_new0(struct iovec, page_count + 1);
943815956f0SJuan Quintela         p->normal = g_new0(ram_addr_t, page_count);
944ddec20f8SJuan Quintela         p->page_size = qemu_target_page_size();
945d6f45ebaSJuan Quintela         p->page_count = page_count;
9465b1d9babSLeonardo Bras 
947b4bc342cSJuan Quintela         if (migrate_zero_copy_send()) {
9485b1d9babSLeonardo Bras             p->write_flags = QIO_CHANNEL_WRITE_FLAG_ZERO_COPY;
9495b1d9babSLeonardo Bras         } else {
9505b1d9babSLeonardo Bras             p->write_flags = 0;
9515b1d9babSLeonardo Bras         }
9525b1d9babSLeonardo Bras 
953d32ca5adSJuan Quintela         socket_send_channel_create(multifd_new_send_channel_async, p);
954d32ca5adSJuan Quintela     }
955ab7cbb0bSJuan Quintela 
956ab7cbb0bSJuan Quintela     for (i = 0; i < thread_count; i++) {
957ab7cbb0bSJuan Quintela         MultiFDSendParams *p = &multifd_send_state->params[i];
958ab7cbb0bSJuan Quintela         Error *local_err = NULL;
959ab7cbb0bSJuan Quintela         int ret;
960ab7cbb0bSJuan Quintela 
961ab7cbb0bSJuan Quintela         ret = multifd_send_state->ops->send_setup(p, &local_err);
962ab7cbb0bSJuan Quintela         if (ret) {
963ab7cbb0bSJuan Quintela             error_propagate(errp, local_err);
964ab7cbb0bSJuan Quintela             return ret;
965ab7cbb0bSJuan Quintela         }
966ab7cbb0bSJuan Quintela     }
967d32ca5adSJuan Quintela     return 0;
968d32ca5adSJuan Quintela }
969d32ca5adSJuan Quintela 
970d32ca5adSJuan Quintela struct {
971d32ca5adSJuan Quintela     MultiFDRecvParams *params;
972d32ca5adSJuan Quintela     /* number of created threads */
973d32ca5adSJuan Quintela     int count;
974d32ca5adSJuan Quintela     /* syncs main thread and channels */
975d32ca5adSJuan Quintela     QemuSemaphore sem_sync;
976d32ca5adSJuan Quintela     /* global number of generated multifd packets */
977d32ca5adSJuan Quintela     uint64_t packet_num;
978ab7cbb0bSJuan Quintela     /* multifd ops */
979ab7cbb0bSJuan Quintela     MultiFDMethods *ops;
980d32ca5adSJuan Quintela } *multifd_recv_state;
981d32ca5adSJuan Quintela 
982d32ca5adSJuan Quintela static void multifd_recv_terminate_threads(Error *err)
983d32ca5adSJuan Quintela {
984d32ca5adSJuan Quintela     int i;
985d32ca5adSJuan Quintela 
986d32ca5adSJuan Quintela     trace_multifd_recv_terminate_threads(err != NULL);
987d32ca5adSJuan Quintela 
988d32ca5adSJuan Quintela     if (err) {
989d32ca5adSJuan Quintela         MigrationState *s = migrate_get_current();
990d32ca5adSJuan Quintela         migrate_set_error(s, err);
991d32ca5adSJuan Quintela         if (s->state == MIGRATION_STATUS_SETUP ||
992d32ca5adSJuan Quintela             s->state == MIGRATION_STATUS_ACTIVE) {
993d32ca5adSJuan Quintela             migrate_set_state(&s->state, s->state,
994d32ca5adSJuan Quintela                               MIGRATION_STATUS_FAILED);
995d32ca5adSJuan Quintela         }
996d32ca5adSJuan Quintela     }
997d32ca5adSJuan Quintela 
998d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
999d32ca5adSJuan Quintela         MultiFDRecvParams *p = &multifd_recv_state->params[i];
1000d32ca5adSJuan Quintela 
1001d32ca5adSJuan Quintela         qemu_mutex_lock(&p->mutex);
1002d32ca5adSJuan Quintela         p->quit = true;
1003d32ca5adSJuan Quintela         /*
1004d32ca5adSJuan Quintela          * We could arrive here for two reasons:
1005d32ca5adSJuan Quintela          *  - normal quit, i.e. everything went fine, just finished
1006d32ca5adSJuan Quintela          *  - error quit: We close the channels so the channel threads
1007d32ca5adSJuan Quintela          *    finish the qio_channel_read_all_eof()
1008d32ca5adSJuan Quintela          */
1009d32ca5adSJuan Quintela         if (p->c) {
1010d32ca5adSJuan Quintela             qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
1011d32ca5adSJuan Quintela         }
1012d32ca5adSJuan Quintela         qemu_mutex_unlock(&p->mutex);
1013d32ca5adSJuan Quintela     }
1014d32ca5adSJuan Quintela }
1015d32ca5adSJuan Quintela 
1016cfc3bcf3SLeonardo Bras void multifd_load_shutdown(void)
1017cfc3bcf3SLeonardo Bras {
101851b07548SJuan Quintela     if (migrate_multifd()) {
1019cfc3bcf3SLeonardo Bras         multifd_recv_terminate_threads(NULL);
1020cfc3bcf3SLeonardo Bras     }
1021cfc3bcf3SLeonardo Bras }
1022cfc3bcf3SLeonardo Bras 
1023e5bac1f5SLeonardo Bras void multifd_load_cleanup(void)
1024d32ca5adSJuan Quintela {
1025d32ca5adSJuan Quintela     int i;
1026d32ca5adSJuan Quintela 
102751b07548SJuan Quintela     if (!migrate_multifd()) {
1028e5bac1f5SLeonardo Bras         return;
1029d32ca5adSJuan Quintela     }
1030d32ca5adSJuan Quintela     multifd_recv_terminate_threads(NULL);
1031d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
1032d32ca5adSJuan Quintela         MultiFDRecvParams *p = &multifd_recv_state->params[i];
1033d32ca5adSJuan Quintela 
1034d32ca5adSJuan Quintela         if (p->running) {
1035d32ca5adSJuan Quintela             /*
1036d32ca5adSJuan Quintela              * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code,
1037d32ca5adSJuan Quintela              * however try to wakeup it without harm in cleanup phase.
1038d32ca5adSJuan Quintela              */
1039d32ca5adSJuan Quintela             qemu_sem_post(&p->sem_sync);
1040d32ca5adSJuan Quintela         }
104110351fbaSLeonardo Bras 
104210351fbaSLeonardo Bras         qemu_thread_join(&p->thread);
1043d32ca5adSJuan Quintela     }
1044d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
1045d32ca5adSJuan Quintela         MultiFDRecvParams *p = &multifd_recv_state->params[i];
1046d32ca5adSJuan Quintela 
104718711405SPeter Xu         migration_ioc_unregister_yank(p->c);
1048d32ca5adSJuan Quintela         object_unref(OBJECT(p->c));
1049d32ca5adSJuan Quintela         p->c = NULL;
1050d32ca5adSJuan Quintela         qemu_mutex_destroy(&p->mutex);
1051d32ca5adSJuan Quintela         qemu_sem_destroy(&p->sem_sync);
1052d32ca5adSJuan Quintela         g_free(p->name);
1053d32ca5adSJuan Quintela         p->name = NULL;
1054d32ca5adSJuan Quintela         p->packet_len = 0;
1055d32ca5adSJuan Quintela         g_free(p->packet);
1056d32ca5adSJuan Quintela         p->packet = NULL;
1057226468baSJuan Quintela         g_free(p->iov);
1058226468baSJuan Quintela         p->iov = NULL;
1059cf2d4aa8SJuan Quintela         g_free(p->normal);
1060cf2d4aa8SJuan Quintela         p->normal = NULL;
1061ab7cbb0bSJuan Quintela         multifd_recv_state->ops->recv_cleanup(p);
1062d32ca5adSJuan Quintela     }
1063d32ca5adSJuan Quintela     qemu_sem_destroy(&multifd_recv_state->sem_sync);
1064d32ca5adSJuan Quintela     g_free(multifd_recv_state->params);
1065d32ca5adSJuan Quintela     multifd_recv_state->params = NULL;
1066d32ca5adSJuan Quintela     g_free(multifd_recv_state);
1067d32ca5adSJuan Quintela     multifd_recv_state = NULL;
1068d32ca5adSJuan Quintela }
1069d32ca5adSJuan Quintela 
1070d32ca5adSJuan Quintela void multifd_recv_sync_main(void)
1071d32ca5adSJuan Quintela {
1072d32ca5adSJuan Quintela     int i;
1073d32ca5adSJuan Quintela 
107451b07548SJuan Quintela     if (!migrate_multifd()) {
1075d32ca5adSJuan Quintela         return;
1076d32ca5adSJuan Quintela     }
1077d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
1078d32ca5adSJuan Quintela         MultiFDRecvParams *p = &multifd_recv_state->params[i];
1079d32ca5adSJuan Quintela 
1080d32ca5adSJuan Quintela         trace_multifd_recv_sync_main_wait(p->id);
1081d32ca5adSJuan Quintela         qemu_sem_wait(&multifd_recv_state->sem_sync);
1082d32ca5adSJuan Quintela     }
1083d32ca5adSJuan Quintela     for (i = 0; i < migrate_multifd_channels(); i++) {
1084d32ca5adSJuan Quintela         MultiFDRecvParams *p = &multifd_recv_state->params[i];
1085d32ca5adSJuan Quintela 
10866e8a355dSDaniel Brodsky         WITH_QEMU_LOCK_GUARD(&p->mutex) {
1087d32ca5adSJuan Quintela             if (multifd_recv_state->packet_num < p->packet_num) {
1088d32ca5adSJuan Quintela                 multifd_recv_state->packet_num = p->packet_num;
1089d32ca5adSJuan Quintela             }
10906e8a355dSDaniel Brodsky         }
1091d32ca5adSJuan Quintela         trace_multifd_recv_sync_main_signal(p->id);
1092d32ca5adSJuan Quintela         qemu_sem_post(&p->sem_sync);
1093d32ca5adSJuan Quintela     }
1094d32ca5adSJuan Quintela     trace_multifd_recv_sync_main(multifd_recv_state->packet_num);
1095d32ca5adSJuan Quintela }
1096d32ca5adSJuan Quintela 
1097d32ca5adSJuan Quintela static void *multifd_recv_thread(void *opaque)
1098d32ca5adSJuan Quintela {
1099d32ca5adSJuan Quintela     MultiFDRecvParams *p = opaque;
1100d32ca5adSJuan Quintela     Error *local_err = NULL;
1101d32ca5adSJuan Quintela     int ret;
1102d32ca5adSJuan Quintela 
1103d32ca5adSJuan Quintela     trace_multifd_recv_thread_start(p->id);
1104d32ca5adSJuan Quintela     rcu_register_thread();
1105d32ca5adSJuan Quintela 
1106d32ca5adSJuan Quintela     while (true) {
1107d32ca5adSJuan Quintela         uint32_t flags;
1108d32ca5adSJuan Quintela 
1109d32ca5adSJuan Quintela         if (p->quit) {
1110d32ca5adSJuan Quintela             break;
1111d32ca5adSJuan Quintela         }
1112d32ca5adSJuan Quintela 
1113d32ca5adSJuan Quintela         ret = qio_channel_read_all_eof(p->c, (void *)p->packet,
1114d32ca5adSJuan Quintela                                        p->packet_len, &local_err);
1115bca762c2SLi Zhang         if (ret == 0 || ret == -1) {   /* 0: EOF  -1: Error */
1116d32ca5adSJuan Quintela             break;
1117d32ca5adSJuan Quintela         }
1118d32ca5adSJuan Quintela 
1119d32ca5adSJuan Quintela         qemu_mutex_lock(&p->mutex);
1120d32ca5adSJuan Quintela         ret = multifd_recv_unfill_packet(p, &local_err);
1121d32ca5adSJuan Quintela         if (ret) {
1122d32ca5adSJuan Quintela             qemu_mutex_unlock(&p->mutex);
1123d32ca5adSJuan Quintela             break;
1124d32ca5adSJuan Quintela         }
1125d32ca5adSJuan Quintela 
1126d32ca5adSJuan Quintela         flags = p->flags;
1127ab7cbb0bSJuan Quintela         /* recv methods don't know how to handle the SYNC flag */
1128ab7cbb0bSJuan Quintela         p->flags &= ~MULTIFD_FLAG_SYNC;
1129cf2d4aa8SJuan Quintela         trace_multifd_recv(p->id, p->packet_num, p->normal_num, flags,
1130d32ca5adSJuan Quintela                            p->next_packet_size);
1131d32ca5adSJuan Quintela         p->num_packets++;
1132cf2d4aa8SJuan Quintela         p->total_normal_pages += p->normal_num;
1133d32ca5adSJuan Quintela         qemu_mutex_unlock(&p->mutex);
1134d32ca5adSJuan Quintela 
1135cf2d4aa8SJuan Quintela         if (p->normal_num) {
113640a4bfe9SJuan Quintela             ret = multifd_recv_state->ops->recv_pages(p, &local_err);
1137d32ca5adSJuan Quintela             if (ret != 0) {
1138d32ca5adSJuan Quintela                 break;
1139d32ca5adSJuan Quintela             }
1140d32ca5adSJuan Quintela         }
1141d32ca5adSJuan Quintela 
1142d32ca5adSJuan Quintela         if (flags & MULTIFD_FLAG_SYNC) {
1143d32ca5adSJuan Quintela             qemu_sem_post(&multifd_recv_state->sem_sync);
1144d32ca5adSJuan Quintela             qemu_sem_wait(&p->sem_sync);
1145d32ca5adSJuan Quintela         }
1146d32ca5adSJuan Quintela     }
1147d32ca5adSJuan Quintela 
1148d32ca5adSJuan Quintela     if (local_err) {
1149d32ca5adSJuan Quintela         multifd_recv_terminate_threads(local_err);
115013f2cb21SPan Nengyuan         error_free(local_err);
1151d32ca5adSJuan Quintela     }
1152d32ca5adSJuan Quintela     qemu_mutex_lock(&p->mutex);
1153d32ca5adSJuan Quintela     p->running = false;
1154d32ca5adSJuan Quintela     qemu_mutex_unlock(&p->mutex);
1155d32ca5adSJuan Quintela 
1156d32ca5adSJuan Quintela     rcu_unregister_thread();
1157cf2d4aa8SJuan Quintela     trace_multifd_recv_thread_end(p->id, p->num_packets, p->total_normal_pages);
1158d32ca5adSJuan Quintela 
1159d32ca5adSJuan Quintela     return NULL;
1160d32ca5adSJuan Quintela }
1161d32ca5adSJuan Quintela 
1162d32ca5adSJuan Quintela int multifd_load_setup(Error **errp)
1163d32ca5adSJuan Quintela {
1164d32ca5adSJuan Quintela     int thread_count;
1165d32ca5adSJuan Quintela     uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size();
1166d32ca5adSJuan Quintela     uint8_t i;
1167d32ca5adSJuan Quintela 
11686720c2b3Smanish.mishra     /*
11696720c2b3Smanish.mishra      * Return successfully if multiFD recv state is already initialised
11706720c2b3Smanish.mishra      * or multiFD is not enabled.
11716720c2b3Smanish.mishra      */
117251b07548SJuan Quintela     if (multifd_recv_state || !migrate_multifd()) {
1173d32ca5adSJuan Quintela         return 0;
1174d32ca5adSJuan Quintela     }
11756720c2b3Smanish.mishra 
1176d32ca5adSJuan Quintela     thread_count = migrate_multifd_channels();
1177d32ca5adSJuan Quintela     multifd_recv_state = g_malloc0(sizeof(*multifd_recv_state));
1178d32ca5adSJuan Quintela     multifd_recv_state->params = g_new0(MultiFDRecvParams, thread_count);
1179d73415a3SStefan Hajnoczi     qatomic_set(&multifd_recv_state->count, 0);
1180d32ca5adSJuan Quintela     qemu_sem_init(&multifd_recv_state->sem_sync, 0);
1181ab7cbb0bSJuan Quintela     multifd_recv_state->ops = multifd_ops[migrate_multifd_compression()];
1182d32ca5adSJuan Quintela 
1183d32ca5adSJuan Quintela     for (i = 0; i < thread_count; i++) {
1184d32ca5adSJuan Quintela         MultiFDRecvParams *p = &multifd_recv_state->params[i];
1185d32ca5adSJuan Quintela 
1186d32ca5adSJuan Quintela         qemu_mutex_init(&p->mutex);
1187d32ca5adSJuan Quintela         qemu_sem_init(&p->sem_sync, 0);
1188d32ca5adSJuan Quintela         p->quit = false;
1189d32ca5adSJuan Quintela         p->id = i;
1190d32ca5adSJuan Quintela         p->packet_len = sizeof(MultiFDPacket_t)
1191d32ca5adSJuan Quintela                       + sizeof(uint64_t) * page_count;
1192d32ca5adSJuan Quintela         p->packet = g_malloc0(p->packet_len);
1193d32ca5adSJuan Quintela         p->name = g_strdup_printf("multifdrecv_%d", i);
1194226468baSJuan Quintela         p->iov = g_new0(struct iovec, page_count);
1195cf2d4aa8SJuan Quintela         p->normal = g_new0(ram_addr_t, page_count);
1196d6f45ebaSJuan Quintela         p->page_count = page_count;
1197ddec20f8SJuan Quintela         p->page_size = qemu_target_page_size();
1198d32ca5adSJuan Quintela     }
1199ab7cbb0bSJuan Quintela 
1200ab7cbb0bSJuan Quintela     for (i = 0; i < thread_count; i++) {
1201ab7cbb0bSJuan Quintela         MultiFDRecvParams *p = &multifd_recv_state->params[i];
1202ab7cbb0bSJuan Quintela         Error *local_err = NULL;
1203ab7cbb0bSJuan Quintela         int ret;
1204ab7cbb0bSJuan Quintela 
1205ab7cbb0bSJuan Quintela         ret = multifd_recv_state->ops->recv_setup(p, &local_err);
1206ab7cbb0bSJuan Quintela         if (ret) {
1207ab7cbb0bSJuan Quintela             error_propagate(errp, local_err);
1208ab7cbb0bSJuan Quintela             return ret;
1209ab7cbb0bSJuan Quintela         }
1210ab7cbb0bSJuan Quintela     }
1211d32ca5adSJuan Quintela     return 0;
1212d32ca5adSJuan Quintela }
1213d32ca5adSJuan Quintela 
1214d32ca5adSJuan Quintela bool multifd_recv_all_channels_created(void)
1215d32ca5adSJuan Quintela {
1216d32ca5adSJuan Quintela     int thread_count = migrate_multifd_channels();
1217d32ca5adSJuan Quintela 
121851b07548SJuan Quintela     if (!migrate_multifd()) {
1219d32ca5adSJuan Quintela         return true;
1220d32ca5adSJuan Quintela     }
1221d32ca5adSJuan Quintela 
1222a59136f3SDr. David Alan Gilbert     if (!multifd_recv_state) {
1223a59136f3SDr. David Alan Gilbert         /* Called before any connections created */
1224a59136f3SDr. David Alan Gilbert         return false;
1225a59136f3SDr. David Alan Gilbert     }
1226a59136f3SDr. David Alan Gilbert 
1227d73415a3SStefan Hajnoczi     return thread_count == qatomic_read(&multifd_recv_state->count);
1228d32ca5adSJuan Quintela }
1229d32ca5adSJuan Quintela 
1230d32ca5adSJuan Quintela /*
1231d32ca5adSJuan Quintela  * Try to receive all multifd channels to get ready for the migration.
12326720c2b3Smanish.mishra  * Sets @errp when failing to receive the current channel.
1233d32ca5adSJuan Quintela  */
12346720c2b3Smanish.mishra void multifd_recv_new_channel(QIOChannel *ioc, Error **errp)
1235d32ca5adSJuan Quintela {
1236d32ca5adSJuan Quintela     MultiFDRecvParams *p;
1237d32ca5adSJuan Quintela     Error *local_err = NULL;
1238d32ca5adSJuan Quintela     int id;
1239d32ca5adSJuan Quintela 
1240d32ca5adSJuan Quintela     id = multifd_recv_initial_packet(ioc, &local_err);
1241d32ca5adSJuan Quintela     if (id < 0) {
1242d32ca5adSJuan Quintela         multifd_recv_terminate_threads(local_err);
1243d32ca5adSJuan Quintela         error_propagate_prepend(errp, local_err,
1244d32ca5adSJuan Quintela                                 "failed to receive packet"
1245d32ca5adSJuan Quintela                                 " via multifd channel %d: ",
1246d73415a3SStefan Hajnoczi                                 qatomic_read(&multifd_recv_state->count));
12476720c2b3Smanish.mishra         return;
1248d32ca5adSJuan Quintela     }
1249d32ca5adSJuan Quintela     trace_multifd_recv_new_channel(id);
1250d32ca5adSJuan Quintela 
1251d32ca5adSJuan Quintela     p = &multifd_recv_state->params[id];
1252d32ca5adSJuan Quintela     if (p->c != NULL) {
1253d32ca5adSJuan Quintela         error_setg(&local_err, "multifd: received id '%d' already setup'",
1254d32ca5adSJuan Quintela                    id);
1255d32ca5adSJuan Quintela         multifd_recv_terminate_threads(local_err);
1256d32ca5adSJuan Quintela         error_propagate(errp, local_err);
12576720c2b3Smanish.mishra         return;
1258d32ca5adSJuan Quintela     }
1259d32ca5adSJuan Quintela     p->c = ioc;
1260d32ca5adSJuan Quintela     object_ref(OBJECT(ioc));
1261d32ca5adSJuan Quintela     /* initial packet */
1262d32ca5adSJuan Quintela     p->num_packets = 1;
1263d32ca5adSJuan Quintela 
1264d32ca5adSJuan Quintela     p->running = true;
1265d32ca5adSJuan Quintela     qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p,
1266d32ca5adSJuan Quintela                        QEMU_THREAD_JOINABLE);
1267d73415a3SStefan Hajnoczi     qatomic_inc(&multifd_recv_state->count);
1268d32ca5adSJuan Quintela }
1269