xref: /openbmc/qemu/migration/multifd.h (revision a68694cd)
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     uint32_t pages_used;
46     /* size of the next packet that contains pages */
47     uint32_t next_packet_size;
48     uint64_t packet_num;
49     uint64_t unused[4];    /* Reserved for future use */
50     char ramblock[256];
51     uint64_t offset[];
52 } __attribute__((packed)) MultiFDPacket_t;
53 
54 typedef struct {
55     /* number of used pages */
56     uint32_t used;
57     /* number of allocated pages */
58     uint32_t allocated;
59     /* global number of generated multifd packets */
60     uint64_t packet_num;
61     /* offset of each page */
62     ram_addr_t *offset;
63     /* pointer to each page */
64     struct iovec *iov;
65     RAMBlock *block;
66 } MultiFDPages_t;
67 
68 typedef struct {
69     /* this fields are not changed once the thread is created */
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     /* sem where to wait for more work */
79     QemuSemaphore sem;
80     /* this mutex protects the following parameters */
81     QemuMutex mutex;
82     /* is this channel thread running */
83     bool running;
84     /* should this thread finish */
85     bool quit;
86     /* thread has work to do */
87     int pending_job;
88     /* array of pages to sent */
89     MultiFDPages_t *pages;
90     /* packet allocated len */
91     uint32_t packet_len;
92     /* pointer to the packet */
93     MultiFDPacket_t *packet;
94     /* multifd flags for each packet */
95     uint32_t flags;
96     /* size of the next packet that contains pages */
97     uint32_t next_packet_size;
98     /* global number of generated multifd packets */
99     uint64_t packet_num;
100     /* thread local variables */
101     /* packets sent through this channel */
102     uint64_t num_packets;
103     /* pages sent through this channel */
104     uint64_t num_pages;
105     /* syncs main thread and channels */
106     QemuSemaphore sem_sync;
107     /* used for compression methods */
108     void *data;
109 }  MultiFDSendParams;
110 
111 typedef struct {
112     /* this fields are not changed once the thread is created */
113     /* channel number */
114     uint8_t id;
115     /* channel thread name */
116     char *name;
117     /* channel thread id */
118     QemuThread thread;
119     /* communication channel */
120     QIOChannel *c;
121     /* this mutex protects the following parameters */
122     QemuMutex mutex;
123     /* is this channel thread running */
124     bool running;
125     /* should this thread finish */
126     bool quit;
127     /* array of pages to receive */
128     MultiFDPages_t *pages;
129     /* packet allocated len */
130     uint32_t packet_len;
131     /* pointer to the packet */
132     MultiFDPacket_t *packet;
133     /* multifd flags for each packet */
134     uint32_t flags;
135     /* global number of generated multifd packets */
136     uint64_t packet_num;
137     /* thread local variables */
138     /* size of the next packet that contains pages */
139     uint32_t next_packet_size;
140     /* packets sent through this channel */
141     uint64_t num_packets;
142     /* pages sent through this channel */
143     uint64_t num_pages;
144     /* syncs main thread and channels */
145     QemuSemaphore sem_sync;
146     /* used for de-compression methods */
147     void *data;
148 } MultiFDRecvParams;
149 
150 typedef struct {
151     /* Setup for sending side */
152     int (*send_setup)(MultiFDSendParams *p, Error **errp);
153     /* Cleanup for sending side */
154     void (*send_cleanup)(MultiFDSendParams *p, Error **errp);
155     /* Prepare the send packet */
156     int (*send_prepare)(MultiFDSendParams *p, uint32_t used, Error **errp);
157     /* Write the send packet */
158     int (*send_write)(MultiFDSendParams *p, uint32_t used, Error **errp);
159     /* Setup for receiving side */
160     int (*recv_setup)(MultiFDRecvParams *p, Error **errp);
161     /* Cleanup for receiving side */
162     void (*recv_cleanup)(MultiFDRecvParams *p);
163     /* Read all pages */
164     int (*recv_pages)(MultiFDRecvParams *p, uint32_t used, Error **errp);
165 } MultiFDMethods;
166 
167 void multifd_register_ops(int method, MultiFDMethods *ops);
168 
169 #endif
170 
171