xref: /openbmc/qemu/migration/multifd.h (revision 9951ba94)
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 void 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     /* this fields are not changed once the thread is created */
69     /* channel number */
70     uint8_t id;
71     /* channel thread name */
72     char *name;
73     /* channel thread id */
74     QemuThread thread;
75     /* communication channel */
76     QIOChannel *c;
77     /* sem where to wait for more work */
78     QemuSemaphore sem;
79     /* this mutex protects the following parameters */
80     QemuMutex mutex;
81     /* is this channel thread running */
82     bool running;
83     /* should this thread finish */
84     bool quit;
85     /* is the yank function registered */
86     bool registered_yank;
87     /* thread has work to do */
88     int pending_job;
89     /* array of pages to sent */
90     MultiFDPages_t *pages;
91     /* packet allocated len */
92     uint32_t packet_len;
93     /* pointer to the packet */
94     MultiFDPacket_t *packet;
95     /* multifd flags for each packet */
96     uint32_t flags;
97     /* size of the next packet that contains pages */
98     uint32_t next_packet_size;
99     /* global number of generated multifd packets */
100     uint64_t packet_num;
101     /* thread local variables */
102     /* packets sent through this channel */
103     uint64_t num_packets;
104     /* non zero pages sent through this channel */
105     uint64_t total_normal_pages;
106     /* syncs main thread and channels */
107     QemuSemaphore sem_sync;
108     /* buffers to send */
109     struct iovec *iov;
110     /* number of iovs used */
111     uint32_t iovs_num;
112     /* Pages that are not zero */
113     ram_addr_t *normal;
114     /* num of non zero pages */
115     uint32_t normal_num;
116     /* used for compression methods */
117     void *data;
118 }  MultiFDSendParams;
119 
120 typedef struct {
121     /* this fields are not changed once the thread is created */
122     /* channel number */
123     uint8_t id;
124     /* channel thread name */
125     char *name;
126     /* channel thread id */
127     QemuThread thread;
128     /* communication channel */
129     QIOChannel *c;
130     /* this mutex protects the following parameters */
131     QemuMutex mutex;
132     /* is this channel thread running */
133     bool running;
134     /* should this thread finish */
135     bool quit;
136     /* ramblock host address */
137     uint8_t *host;
138     /* packet allocated len */
139     uint32_t packet_len;
140     /* pointer to the packet */
141     MultiFDPacket_t *packet;
142     /* multifd flags for each packet */
143     uint32_t flags;
144     /* global number of generated multifd packets */
145     uint64_t packet_num;
146     /* thread local variables */
147     /* size of the next packet that contains pages */
148     uint32_t next_packet_size;
149     /* packets sent through this channel */
150     uint64_t num_packets;
151     /* non zero pages recv through this channel */
152     uint64_t total_normal_pages;
153     /* syncs main thread and channels */
154     QemuSemaphore sem_sync;
155     /* buffers to recv */
156     struct iovec *iov;
157     /* Pages that are not zero */
158     ram_addr_t *normal;
159     /* num of non zero pages */
160     uint32_t normal_num;
161     /* used for de-compression methods */
162     void *data;
163 } MultiFDRecvParams;
164 
165 typedef struct {
166     /* Setup for sending side */
167     int (*send_setup)(MultiFDSendParams *p, Error **errp);
168     /* Cleanup for sending side */
169     void (*send_cleanup)(MultiFDSendParams *p, Error **errp);
170     /* Prepare the send packet */
171     int (*send_prepare)(MultiFDSendParams *p, Error **errp);
172     /* Setup for receiving side */
173     int (*recv_setup)(MultiFDRecvParams *p, Error **errp);
174     /* Cleanup for receiving side */
175     void (*recv_cleanup)(MultiFDRecvParams *p);
176     /* Read all pages */
177     int (*recv_pages)(MultiFDRecvParams *p, Error **errp);
178 } MultiFDMethods;
179 
180 void multifd_register_ops(int method, MultiFDMethods *ops);
181 
182 #endif
183 
184