xref: /qemu/migration/multifd.h (revision 84615a19)
1 /*
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 
16 int multifd_save_setup(Error **errp);
17 void multifd_save_cleanup(void);
18 int multifd_load_setup(Error **errp);
19 int multifd_load_cleanup(Error **errp);
20 bool multifd_recv_all_channels_created(void);
21 bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp);
22 void multifd_recv_sync_main(void);
23 int multifd_send_sync_main(QEMUFile *f);
24 int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset);
25 
26 /* Multifd Compression flags */
27 #define MULTIFD_FLAG_SYNC (1 << 0)
28 
29 /* We reserve 3 bits for compression methods */
30 #define MULTIFD_FLAG_COMPRESSION_MASK (7 << 1)
31 /* we need to be compatible. Before compression value was 0 */
32 #define MULTIFD_FLAG_NOCOMP (0 << 1)
33 #define MULTIFD_FLAG_ZLIB (1 << 1)
34 #define MULTIFD_FLAG_ZSTD (2 << 1)
35 
36 /* This value needs to be a multiple of qemu_target_page_size() */
37 #define MULTIFD_PACKET_SIZE (512 * 1024)
38 
39 typedef struct {
40     uint32_t magic;
41     uint32_t version;
42     uint32_t flags;
43     /* maximum number of allocated pages */
44     uint32_t pages_alloc;
45     /* non zero pages */
46     uint32_t normal_pages;
47     /* size of the next packet that contains pages */
48     uint32_t next_packet_size;
49     uint64_t packet_num;
50     uint64_t unused[4];    /* Reserved for future use */
51     char ramblock[256];
52     uint64_t offset[];
53 } __attribute__((packed)) MultiFDPacket_t;
54 
55 typedef struct {
56     /* number of used pages */
57     uint32_t num;
58     /* number of allocated pages */
59     uint32_t allocated;
60     /* global number of generated multifd packets */
61     uint64_t packet_num;
62     /* offset of each page */
63     ram_addr_t *offset;
64     RAMBlock *block;
65 } MultiFDPages_t;
66 
67 typedef struct {
68     /* Fields are only written at creating/deletion time */
69     /* No lock required for them, they are read only */
70 
71     /* channel number */
72     uint8_t id;
73     /* channel thread name */
74     char *name;
75     /* channel thread id */
76     QemuThread thread;
77     /* communication channel */
78     QIOChannel *c;
79     /* is the yank function registered */
80     bool registered_yank;
81     /* packet allocated len */
82     uint32_t packet_len;
83     /* guest page size */
84     uint32_t page_size;
85     /* number of pages in a full packet */
86     uint32_t page_count;
87     /* multifd flags for sending ram */
88     int write_flags;
89 
90     /* sem where to wait for more work */
91     QemuSemaphore sem;
92     /* syncs main thread and channels */
93     QemuSemaphore sem_sync;
94 
95     /* this mutex protects the following parameters */
96     QemuMutex mutex;
97     /* is this channel thread running */
98     bool running;
99     /* should this thread finish */
100     bool quit;
101     /* multifd flags for each packet */
102     uint32_t flags;
103     /* global number of generated multifd packets */
104     uint64_t packet_num;
105     /* thread has work to do */
106     int pending_job;
107     /* array of pages to sent.
108      * The owner of 'pages' depends of 'pending_job' value:
109      * pending_job == 0 -> migration_thread can use it.
110      * pending_job != 0 -> multifd_channel can use it.
111      */
112     MultiFDPages_t *pages;
113 
114     /* thread local variables. No locking required */
115 
116     /* pointer to the packet */
117     MultiFDPacket_t *packet;
118     /* size of the next packet that contains pages */
119     uint32_t next_packet_size;
120     /* packets sent through this channel */
121     uint64_t num_packets;
122     /* non zero pages sent through this channel */
123     uint64_t total_normal_pages;
124     /* buffers to send */
125     struct iovec *iov;
126     /* number of iovs used */
127     uint32_t iovs_num;
128     /* Pages that are not zero */
129     ram_addr_t *normal;
130     /* num of non zero pages */
131     uint32_t normal_num;
132     /* used for compression methods */
133     void *data;
134 }  MultiFDSendParams;
135 
136 typedef struct {
137     /* Fields are only written at creating/deletion time */
138     /* No lock required for them, they are read only */
139 
140     /* channel number */
141     uint8_t id;
142     /* channel thread name */
143     char *name;
144     /* channel thread id */
145     QemuThread thread;
146     /* communication channel */
147     QIOChannel *c;
148     /* packet allocated len */
149     uint32_t packet_len;
150     /* guest page size */
151     uint32_t page_size;
152     /* number of pages in a full packet */
153     uint32_t page_count;
154 
155     /* syncs main thread and channels */
156     QemuSemaphore sem_sync;
157 
158     /* this mutex protects the following parameters */
159     QemuMutex mutex;
160     /* is this channel thread running */
161     bool running;
162     /* should this thread finish */
163     bool quit;
164     /* multifd flags for each packet */
165     uint32_t flags;
166     /* global number of generated multifd packets */
167     uint64_t packet_num;
168 
169     /* thread local variables. No locking required */
170 
171     /* pointer to the packet */
172     MultiFDPacket_t *packet;
173     /* size of the next packet that contains pages */
174     uint32_t next_packet_size;
175     /* packets sent through this channel */
176     uint64_t num_packets;
177     /* ramblock host address */
178     uint8_t *host;
179     /* non zero pages recv through this channel */
180     uint64_t total_normal_pages;
181     /* buffers to recv */
182     struct iovec *iov;
183     /* Pages that are not zero */
184     ram_addr_t *normal;
185     /* num of non zero pages */
186     uint32_t normal_num;
187     /* used for de-compression methods */
188     void *data;
189 } MultiFDRecvParams;
190 
191 typedef struct {
192     /* Setup for sending side */
193     int (*send_setup)(MultiFDSendParams *p, Error **errp);
194     /* Cleanup for sending side */
195     void (*send_cleanup)(MultiFDSendParams *p, Error **errp);
196     /* Prepare the send packet */
197     int (*send_prepare)(MultiFDSendParams *p, Error **errp);
198     /* Setup for receiving side */
199     int (*recv_setup)(MultiFDRecvParams *p, Error **errp);
200     /* Cleanup for receiving side */
201     void (*recv_cleanup)(MultiFDRecvParams *p);
202     /* Read all pages */
203     int (*recv_pages)(MultiFDRecvParams *p, Error **errp);
204 } MultiFDMethods;
205 
206 void multifd_register_ops(int method, MultiFDMethods *ops);
207 
208 #endif
209 
210