xref: /qemu/migration/cpr.c (revision aa90f1161bb17a4863e16ec2f75104cff0752d4e)
1 /*
2  * Copyright (c) 2021-2024 Oracle and/or its affiliates.
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7 
8 #include "qemu/osdep.h"
9 #include "qapi/error.h"
10 #include "migration/cpr.h"
11 #include "migration/misc.h"
12 #include "migration/options.h"
13 #include "migration/qemu-file.h"
14 #include "migration/savevm.h"
15 #include "migration/vmstate.h"
16 #include "system/runstate.h"
17 #include "trace.h"
18 
19 /*************************************************************************/
20 /* cpr state container for all information to be saved. */
21 
22 typedef QLIST_HEAD(CprFdList, CprFd) CprFdList;
23 
24 typedef struct CprState {
25     CprFdList fds;
26 } CprState;
27 
28 static CprState cpr_state;
29 
30 /****************************************************************************/
31 
32 typedef struct CprFd {
33     char *name;
34     unsigned int namelen;
35     int id;
36     int fd;
37     QLIST_ENTRY(CprFd) next;
38 } CprFd;
39 
40 static const VMStateDescription vmstate_cpr_fd = {
41     .name = "cpr fd",
42     .version_id = 1,
43     .minimum_version_id = 1,
44     .fields = (VMStateField[]) {
45         VMSTATE_UINT32(namelen, CprFd),
46         VMSTATE_VBUFFER_ALLOC_UINT32(name, CprFd, 0, NULL, namelen),
47         VMSTATE_INT32(id, CprFd),
48         VMSTATE_FD(fd, CprFd),
49         VMSTATE_END_OF_LIST()
50     }
51 };
52 
cpr_save_fd(const char * name,int id,int fd)53 void cpr_save_fd(const char *name, int id, int fd)
54 {
55     CprFd *elem = g_new0(CprFd, 1);
56 
57     trace_cpr_save_fd(name, id, fd);
58     elem->name = g_strdup(name);
59     elem->namelen = strlen(name) + 1;
60     elem->id = id;
61     elem->fd = fd;
62     QLIST_INSERT_HEAD(&cpr_state.fds, elem, next);
63 }
64 
find_fd(CprFdList * head,const char * name,int id)65 static CprFd *find_fd(CprFdList *head, const char *name, int id)
66 {
67     CprFd *elem;
68 
69     QLIST_FOREACH(elem, head, next) {
70         if (!strcmp(elem->name, name) && elem->id == id) {
71             return elem;
72         }
73     }
74     return NULL;
75 }
76 
cpr_delete_fd(const char * name,int id)77 void cpr_delete_fd(const char *name, int id)
78 {
79     CprFd *elem = find_fd(&cpr_state.fds, name, id);
80 
81     if (elem) {
82         QLIST_REMOVE(elem, next);
83         g_free(elem->name);
84         g_free(elem);
85     }
86 
87     trace_cpr_delete_fd(name, id);
88 }
89 
cpr_find_fd(const char * name,int id)90 int cpr_find_fd(const char *name, int id)
91 {
92     CprFd *elem = find_fd(&cpr_state.fds, name, id);
93     int fd = elem ? elem->fd : -1;
94 
95     trace_cpr_find_fd(name, id, fd);
96     return fd;
97 }
98 /*************************************************************************/
99 #define CPR_STATE "CprState"
100 
101 static const VMStateDescription vmstate_cpr_state = {
102     .name = CPR_STATE,
103     .version_id = 1,
104     .minimum_version_id = 1,
105     .fields = (VMStateField[]) {
106         VMSTATE_QLIST_V(fds, CprState, 1, vmstate_cpr_fd, CprFd, next),
107         VMSTATE_END_OF_LIST()
108     }
109 };
110 /*************************************************************************/
111 
112 static QEMUFile *cpr_state_file;
113 
cpr_state_ioc(void)114 QIOChannel *cpr_state_ioc(void)
115 {
116     return qemu_file_get_ioc(cpr_state_file);
117 }
118 
119 static MigMode incoming_mode = MIG_MODE_NONE;
120 
cpr_get_incoming_mode(void)121 MigMode cpr_get_incoming_mode(void)
122 {
123     return incoming_mode;
124 }
125 
cpr_set_incoming_mode(MigMode mode)126 void cpr_set_incoming_mode(MigMode mode)
127 {
128     incoming_mode = mode;
129 }
130 
cpr_is_incoming(void)131 bool cpr_is_incoming(void)
132 {
133     return incoming_mode != MIG_MODE_NONE;
134 }
135 
cpr_state_save(MigrationChannel * channel,Error ** errp)136 int cpr_state_save(MigrationChannel *channel, Error **errp)
137 {
138     int ret;
139     QEMUFile *f;
140     MigMode mode = migrate_mode();
141 
142     trace_cpr_state_save(MigMode_str(mode));
143 
144     if (mode == MIG_MODE_CPR_TRANSFER) {
145         g_assert(channel);
146         f = cpr_transfer_output(channel, errp);
147     } else {
148         return 0;
149     }
150     if (!f) {
151         return -1;
152     }
153 
154     qemu_put_be32(f, QEMU_CPR_FILE_MAGIC);
155     qemu_put_be32(f, QEMU_CPR_FILE_VERSION);
156 
157     ret = vmstate_save_state(f, &vmstate_cpr_state, &cpr_state, 0);
158     if (ret) {
159         error_setg(errp, "vmstate_save_state error %d", ret);
160         qemu_fclose(f);
161         return ret;
162     }
163 
164     /*
165      * Close the socket only partially so we can later detect when the other
166      * end closes by getting a HUP event.
167      */
168     qemu_fflush(f);
169     qio_channel_shutdown(qemu_file_get_ioc(f), QIO_CHANNEL_SHUTDOWN_WRITE,
170                          NULL);
171     cpr_state_file = f;
172     return 0;
173 }
174 
cpr_state_load(MigrationChannel * channel,Error ** errp)175 int cpr_state_load(MigrationChannel *channel, Error **errp)
176 {
177     int ret;
178     uint32_t v;
179     QEMUFile *f;
180     MigMode mode = 0;
181 
182     if (channel) {
183         mode = MIG_MODE_CPR_TRANSFER;
184         cpr_set_incoming_mode(mode);
185         f = cpr_transfer_input(channel, errp);
186     } else {
187         return 0;
188     }
189     if (!f) {
190         return -1;
191     }
192 
193     trace_cpr_state_load(MigMode_str(mode));
194 
195     v = qemu_get_be32(f);
196     if (v != QEMU_CPR_FILE_MAGIC) {
197         error_setg(errp, "Not a migration stream (bad magic %x)", v);
198         qemu_fclose(f);
199         return -EINVAL;
200     }
201     v = qemu_get_be32(f);
202     if (v != QEMU_CPR_FILE_VERSION) {
203         error_setg(errp, "Unsupported migration stream version %d", v);
204         qemu_fclose(f);
205         return -ENOTSUP;
206     }
207 
208     ret = vmstate_load_state(f, &vmstate_cpr_state, &cpr_state, 1);
209     if (ret) {
210         error_setg(errp, "vmstate_load_state error %d", ret);
211         qemu_fclose(f);
212         return ret;
213     }
214 
215     /*
216      * Let the caller decide when to close the socket (and generate a HUP event
217      * for the sending side).
218      */
219     cpr_state_file = f;
220 
221     return ret;
222 }
223 
cpr_state_close(void)224 void cpr_state_close(void)
225 {
226     if (cpr_state_file) {
227         qemu_fclose(cpr_state_file);
228         cpr_state_file = NULL;
229     }
230 }
231