xref: /openbmc/qemu/migration/multifd.h (revision 2df1eb27)
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 bool multifd_send_setup(void);
17 void multifd_send_shutdown(void);
18 int multifd_recv_setup(Error **errp);
19 void multifd_recv_cleanup(void);
20 void multifd_recv_shutdown(void);
21 bool multifd_recv_all_channels_created(void);
22 void multifd_recv_new_channel(QIOChannel *ioc, Error **errp);
23 void multifd_recv_sync_main(void);
24 int multifd_send_sync_main(void);
25 bool multifd_queue_page(RAMBlock *block, ram_addr_t offset);
26 
27 /* Multifd Compression flags */
28 #define MULTIFD_FLAG_SYNC (1 << 0)
29 
30 /* We reserve 3 bits for compression methods */
31 #define MULTIFD_FLAG_COMPRESSION_MASK (7 << 1)
32 /* we need to be compatible. Before compression value was 0 */
33 #define MULTIFD_FLAG_NOCOMP (0 << 1)
34 #define MULTIFD_FLAG_ZLIB (1 << 1)
35 #define MULTIFD_FLAG_ZSTD (2 << 1)
36 
37 /* This value needs to be a multiple of qemu_target_page_size() */
38 #define MULTIFD_PACKET_SIZE (512 * 1024)
39 
40 typedef struct {
41     uint32_t magic;
42     uint32_t version;
43     uint32_t flags;
44     /* maximum number of allocated pages */
45     uint32_t pages_alloc;
46     /* non zero pages */
47     uint32_t normal_pages;
48     /* size of the next packet that contains pages */
49     uint32_t next_packet_size;
50     uint64_t packet_num;
51     uint64_t unused[4];    /* Reserved for future use */
52     char ramblock[256];
53     uint64_t offset[];
54 } __attribute__((packed)) MultiFDPacket_t;
55 
56 typedef struct {
57     /* number of used pages */
58     uint32_t num;
59     /* number of allocated pages */
60     uint32_t allocated;
61     /* offset of each page */
62     ram_addr_t *offset;
63     RAMBlock *block;
64 } MultiFDPages_t;
65 
66 typedef struct {
67     /* Fields are only written at creating/deletion time */
68     /* No lock required for them, they are read only */
69 
70     /* channel number */
71     uint8_t id;
72     /* channel thread name */
73     char *name;
74     /* channel thread id */
75     QemuThread thread;
76     bool thread_created;
77     QemuThread tls_thread;
78     bool tls_thread_created;
79     /* communication channel */
80     QIOChannel *c;
81     /* is the yank function registered */
82     bool registered_yank;
83     /* packet allocated len */
84     uint32_t packet_len;
85     /* guest page size */
86     uint32_t page_size;
87     /* number of pages in a full packet */
88     uint32_t page_count;
89     /* multifd flags for sending ram */
90     int write_flags;
91 
92     /* sem where to wait for more work */
93     QemuSemaphore sem;
94     /* syncs main thread and channels */
95     QemuSemaphore sem_sync;
96 
97     /* multifd flags for each packet */
98     uint32_t flags;
99     /*
100      * The sender thread has work to do if either of below boolean is set.
101      *
102      * @pending_job:  a job is pending
103      * @pending_sync: a sync request is pending
104      *
105      * For both of these fields, they're only set by the requesters, and
106      * cleared by the multifd sender threads.
107      */
108     bool pending_job;
109     bool pending_sync;
110     /* array of pages to sent.
111      * The owner of 'pages' depends of 'pending_job' value:
112      * pending_job == 0 -> migration_thread can use it.
113      * pending_job != 0 -> multifd_channel can use it.
114      */
115     MultiFDPages_t *pages;
116 
117     /* thread local variables. No locking required */
118 
119     /* pointer to the packet */
120     MultiFDPacket_t *packet;
121     /* size of the next packet that contains pages */
122     uint32_t next_packet_size;
123     /* packets sent through this channel */
124     uint64_t packets_sent;
125     /* non zero pages sent through this channel */
126     uint64_t total_normal_pages;
127     /* buffers to send */
128     struct iovec *iov;
129     /* number of iovs used */
130     uint32_t iovs_num;
131     /* used for compression methods */
132     void *data;
133 }  MultiFDSendParams;
134 
135 typedef struct {
136     /* Fields are only written at creating/deletion time */
137     /* No lock required for them, they are read only */
138 
139     /* channel number */
140     uint8_t id;
141     /* channel thread name */
142     char *name;
143     /* channel thread id */
144     QemuThread thread;
145     bool thread_created;
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     /* should this thread finish */
161     bool quit;
162     /* multifd flags for each packet */
163     uint32_t flags;
164     /* global number of generated multifd packets */
165     uint64_t packet_num;
166 
167     /* thread local variables. No locking required */
168 
169     /* pointer to the packet */
170     MultiFDPacket_t *packet;
171     /* size of the next packet that contains pages */
172     uint32_t next_packet_size;
173     /* packets received through this channel */
174     uint64_t packets_recved;
175     /* ramblock */
176     RAMBlock *block;
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 void multifd_send_fill_packet(MultiFDSendParams *p);
208 
209 static inline void multifd_send_prepare_header(MultiFDSendParams *p)
210 {
211     p->iov[0].iov_len = p->packet_len;
212     p->iov[0].iov_base = p->packet;
213     p->iovs_num++;
214 }
215 
216 
217 #endif
218