blob: 4dda900a0b4a6723885ac225613675d030efc7fc [file] [log] [blame]
Juan Quintelad32ca5a2020-01-22 16:16:07 +01001/*
2 * Multifd common functions
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#ifndef QEMU_MIGRATION_MULTIFD_H
14#define QEMU_MIGRATION_MULTIFD_H
15
Li Zhijianb7acd652021-09-09 09:29:50 +020016bool migrate_multifd_is_allowed(void);
17void migrate_protocol_allow_multifd(bool allow);
Juan Quintelad32ca5a2020-01-22 16:16:07 +010018int multifd_save_setup(Error **errp);
19void multifd_save_cleanup(void);
20int multifd_load_setup(Error **errp);
21int multifd_load_cleanup(Error **errp);
22bool multifd_recv_all_channels_created(void);
23bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp);
24void multifd_recv_sync_main(void);
25void multifd_send_sync_main(QEMUFile *f);
26int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset);
27
Juan Quintela7ec2c2b2019-01-04 15:30:06 +010028/* Multifd Compression flags */
Juan Quintelad32ca5a2020-01-22 16:16:07 +010029#define MULTIFD_FLAG_SYNC (1 << 0)
30
Juan Quintelaab7cbb02019-05-15 13:37:46 +020031/* We reserve 3 bits for compression methods */
32#define MULTIFD_FLAG_COMPRESSION_MASK (7 << 1)
33/* we need to be compatible. Before compression value was 0 */
34#define MULTIFD_FLAG_NOCOMP (0 << 1)
Juan Quintela7ec2c2b2019-01-04 15:30:06 +010035#define MULTIFD_FLAG_ZLIB (1 << 1)
Juan Quintela87dc6f52019-12-13 13:47:14 +010036#define MULTIFD_FLAG_ZSTD (2 << 1)
Juan Quintelaab7cbb02019-05-15 13:37:46 +020037
Juan Quintelad32ca5a2020-01-22 16:16:07 +010038/* This value needs to be a multiple of qemu_target_page_size() */
39#define MULTIFD_PACKET_SIZE (512 * 1024)
40
41typedef struct {
42 uint32_t magic;
43 uint32_t version;
44 uint32_t flags;
45 /* maximum number of allocated pages */
46 uint32_t pages_alloc;
Juan Quintela8c0ec0b2021-11-22 14:13:51 +010047 /* non zero pages */
48 uint32_t normal_pages;
Juan Quintelad32ca5a2020-01-22 16:16:07 +010049 /* size of the next packet that contains pages */
50 uint32_t next_packet_size;
51 uint64_t packet_num;
52 uint64_t unused[4]; /* Reserved for future use */
53 char ramblock[256];
54 uint64_t offset[];
55} __attribute__((packed)) MultiFDPacket_t;
56
57typedef struct {
58 /* number of used pages */
Juan Quintela90a3d2f2021-11-22 11:51:40 +010059 uint32_t num;
Juan Quintelad32ca5a2020-01-22 16:16:07 +010060 /* number of allocated pages */
61 uint32_t allocated;
62 /* global number of generated multifd packets */
63 uint64_t packet_num;
64 /* offset of each page */
65 ram_addr_t *offset;
Juan Quintelad32ca5a2020-01-22 16:16:07 +010066 RAMBlock *block;
67} MultiFDPages_t;
68
69typedef struct {
70 /* this fields are not changed once the thread is created */
71 /* channel number */
72 uint8_t id;
73 /* channel thread name */
74 char *name;
Chuan Zheng8e5fa052020-09-15 11:03:59 +080075 /* tls hostname */
76 char *tls_hostname;
Juan Quintelad32ca5a2020-01-22 16:16:07 +010077 /* channel thread id */
78 QemuThread thread;
79 /* communication channel */
80 QIOChannel *c;
81 /* sem where to wait for more work */
82 QemuSemaphore sem;
83 /* this mutex protects the following parameters */
84 QemuMutex mutex;
85 /* is this channel thread running */
86 bool running;
87 /* should this thread finish */
88 bool quit;
Lukas Straub20171ea2021-09-09 09:18:08 +020089 /* is the yank function registered */
90 bool registered_yank;
Juan Quintelad32ca5a2020-01-22 16:16:07 +010091 /* thread has work to do */
92 int pending_job;
93 /* array of pages to sent */
94 MultiFDPages_t *pages;
95 /* packet allocated len */
96 uint32_t packet_len;
97 /* pointer to the packet */
98 MultiFDPacket_t *packet;
99 /* multifd flags for each packet */
100 uint32_t flags;
101 /* size of the next packet that contains pages */
102 uint32_t next_packet_size;
103 /* global number of generated multifd packets */
104 uint64_t packet_num;
105 /* thread local variables */
106 /* packets sent through this channel */
107 uint64_t num_packets;
Juan Quintela815956f2021-11-22 13:26:18 +0100108 /* non zero pages sent through this channel */
109 uint64_t total_normal_pages;
Juan Quintelad32ca5a2020-01-22 16:16:07 +0100110 /* syncs main thread and channels */
111 QemuSemaphore sem_sync;
Juan Quintela226468b2021-11-19 12:06:05 +0100112 /* buffers to send */
113 struct iovec *iov;
114 /* number of iovs used */
115 uint32_t iovs_num;
Juan Quintela815956f2021-11-22 13:26:18 +0100116 /* Pages that are not zero */
117 ram_addr_t *normal;
118 /* num of non zero pages */
119 uint32_t normal_num;
Juan Quintelaab7cbb02019-05-15 13:37:46 +0200120 /* used for compression methods */
121 void *data;
Juan Quintelad32ca5a2020-01-22 16:16:07 +0100122} MultiFDSendParams;
123
124typedef struct {
125 /* this fields are not changed once the thread is created */
126 /* channel number */
127 uint8_t id;
128 /* channel thread name */
129 char *name;
130 /* channel thread id */
131 QemuThread thread;
132 /* communication channel */
133 QIOChannel *c;
134 /* this mutex protects the following parameters */
135 QemuMutex mutex;
136 /* is this channel thread running */
137 bool running;
138 /* should this thread finish */
139 bool quit;
Juan Quintelafaf60932021-11-22 14:10:57 +0100140 /* ramblock host address */
141 uint8_t *host;
Juan Quintelad32ca5a2020-01-22 16:16:07 +0100142 /* packet allocated len */
143 uint32_t packet_len;
144 /* pointer to the packet */
145 MultiFDPacket_t *packet;
146 /* multifd flags for each packet */
147 uint32_t flags;
148 /* global number of generated multifd packets */
149 uint64_t packet_num;
150 /* thread local variables */
151 /* size of the next packet that contains pages */
152 uint32_t next_packet_size;
153 /* packets sent through this channel */
154 uint64_t num_packets;
Juan Quintelacf2d4aa2021-11-22 13:41:06 +0100155 /* non zero pages recv through this channel */
156 uint64_t total_normal_pages;
Juan Quintelad32ca5a2020-01-22 16:16:07 +0100157 /* syncs main thread and channels */
158 QemuSemaphore sem_sync;
Juan Quintela226468b2021-11-19 12:06:05 +0100159 /* buffers to recv */
160 struct iovec *iov;
Juan Quintelacf2d4aa2021-11-22 13:41:06 +0100161 /* Pages that are not zero */
162 ram_addr_t *normal;
163 /* num of non zero pages */
164 uint32_t normal_num;
Juan Quintelaab7cbb02019-05-15 13:37:46 +0200165 /* used for de-compression methods */
166 void *data;
Juan Quintelad32ca5a2020-01-22 16:16:07 +0100167} MultiFDRecvParams;
168
Juan Quintelaab7cbb02019-05-15 13:37:46 +0200169typedef struct {
170 /* Setup for sending side */
171 int (*send_setup)(MultiFDSendParams *p, Error **errp);
172 /* Cleanup for sending side */
173 void (*send_cleanup)(MultiFDSendParams *p, Error **errp);
174 /* Prepare the send packet */
Juan Quintela02fb8102021-11-22 12:08:08 +0100175 int (*send_prepare)(MultiFDSendParams *p, Error **errp);
Juan Quintelaab7cbb02019-05-15 13:37:46 +0200176 /* Setup for receiving side */
177 int (*recv_setup)(MultiFDRecvParams *p, Error **errp);
178 /* Cleanup for receiving side */
179 void (*recv_cleanup)(MultiFDRecvParams *p);
180 /* Read all pages */
Juan Quintela40a4bfe2021-11-22 12:49:43 +0100181 int (*recv_pages)(MultiFDRecvParams *p, Error **errp);
Juan Quintelaab7cbb02019-05-15 13:37:46 +0200182} MultiFDMethods;
183
Juan Quintela7ec2c2b2019-01-04 15:30:06 +0100184void multifd_register_ops(int method, MultiFDMethods *ops);
185
Juan Quintelad32ca5a2020-01-22 16:16:07 +0100186#endif
187