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