xref: /openbmc/qemu/migration/multifd.h (revision fc700996)
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 void multifd_load_cleanup(void);
20 void multifd_load_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 int 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     /* communication channel */
77     QIOChannel *c;
78     /* is the yank function registered */
79     bool registered_yank;
80     /* packet allocated len */
81     uint32_t packet_len;
82     /* guest page size */
83     uint32_t page_size;
84     /* number of pages in a full packet */
85     uint32_t page_count;
86     /* multifd flags for sending ram */
87     int write_flags;
88 
89     /* sem where to wait for more work */
90     QemuSemaphore sem;
91     /* syncs main thread and channels */
92     QemuSemaphore sem_sync;
93 
94     /* this mutex protects the following parameters */
95     QemuMutex mutex;
96     /* is this channel thread running */
97     bool running;
98     /* should this thread finish */
99     bool quit;
100     /* multifd flags for each packet */
101     uint32_t flags;
102     /* global number of generated multifd packets */
103     uint64_t packet_num;
104     /* thread has work to do */
105     int pending_job;
106     /* array of pages to sent.
107      * The owner of 'pages' depends of 'pending_job' value:
108      * pending_job == 0 -> migration_thread can use it.
109      * pending_job != 0 -> multifd_channel can use it.
110      */
111     MultiFDPages_t *pages;
112 
113     /* thread local variables. No locking required */
114 
115     /* pointer to the packet */
116     MultiFDPacket_t *packet;
117     /* size of the next packet that contains pages */
118     uint32_t next_packet_size;
119     /* packets sent through this channel */
120     uint64_t num_packets;
121     /* non zero pages sent through this channel */
122     uint64_t total_normal_pages;
123     /* buffers to send */
124     struct iovec *iov;
125     /* number of iovs used */
126     uint32_t iovs_num;
127     /* Pages that are not zero */
128     ram_addr_t *normal;
129     /* num of non zero pages */
130     uint32_t normal_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     /* communication channel */
146     QIOChannel *c;
147     /* packet allocated len */
148     uint32_t packet_len;
149     /* guest page size */
150     uint32_t page_size;
151     /* number of pages in a full packet */
152     uint32_t page_count;
153 
154     /* syncs main thread and channels */
155     QemuSemaphore sem_sync;
156 
157     /* this mutex protects the following parameters */
158     QemuMutex mutex;
159     /* is this channel thread running */
160     bool running;
161     /* should this thread finish */
162     bool quit;
163     /* multifd flags for each packet */
164     uint32_t flags;
165     /* global number of generated multifd packets */
166     uint64_t packet_num;
167 
168     /* thread local variables. No locking required */
169 
170     /* pointer to the packet */
171     MultiFDPacket_t *packet;
172     /* size of the next packet that contains pages */
173     uint32_t next_packet_size;
174     /* packets sent through this channel */
175     uint64_t num_packets;
176     /* ramblock */
177     RAMBlock *block;
178     /* ramblock host address */
179     uint8_t *host;
180     /* non zero pages recv through this channel */
181     uint64_t total_normal_pages;
182     /* buffers to recv */
183     struct iovec *iov;
184     /* Pages that are not zero */
185     ram_addr_t *normal;
186     /* num of non zero pages */
187     uint32_t normal_num;
188     /* used for de-compression methods */
189     void *data;
190 } MultiFDRecvParams;
191 
192 typedef struct {
193     /* Setup for sending side */
194     int (*send_setup)(MultiFDSendParams *p, Error **errp);
195     /* Cleanup for sending side */
196     void (*send_cleanup)(MultiFDSendParams *p, Error **errp);
197     /* Prepare the send packet */
198     int (*send_prepare)(MultiFDSendParams *p, Error **errp);
199     /* Setup for receiving side */
200     int (*recv_setup)(MultiFDRecvParams *p, Error **errp);
201     /* Cleanup for receiving side */
202     void (*recv_cleanup)(MultiFDRecvParams *p);
203     /* Read all pages */
204     int (*recv_pages)(MultiFDRecvParams *p, Error **errp);
205 } MultiFDMethods;
206 
207 void multifd_register_ops(int method, MultiFDMethods *ops);
208 
209 #endif
210 
211