Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/migration/cpr.c
Line
Count
Source
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 "qemu/error-report.h"
10
#include "qapi/error.h"
11
#include "qemu/error-report.h"
12
#include "hw/vfio/vfio-cpr.h"
13
#include "migration/cpr.h"
14
#include "migration/misc.h"
15
#include "migration/options.h"
16
#include "migration/qemu-file.h"
17
#include "migration/savevm.h"
18
#include "migration/vmstate.h"
19
#include "monitor/monitor.h"
20
#include "system/runstate.h"
21
#include "trace.h"
22
23
/*************************************************************************/
24
/* cpr state container for all information to be saved. */
25
26
CprState cpr_state;
27
static GHashTable *cpr_fds_hash;
28
29
/****************************************************************************/
30
31
typedef struct CprFd {
32
    char *name;
33
    unsigned int namelen;
34
    int id;
35
    int fd;
36
    QLIST_ENTRY(CprFd) next;
37
} CprFd;
38
39
static const VMStateDescription vmstate_cpr_fd = {
40
    .name = "cpr fd",
41
    .version_id = 1,
42
    .minimum_version_id = 1,
43
    .fields = (VMStateField[]) {
44
        VMSTATE_UINT32(namelen, CprFd),
45
        VMSTATE_VBUFFER_ALLOC_UINT32(name, CprFd, 0, NULL, namelen),
46
        VMSTATE_INT32(id, CprFd),
47
        VMSTATE_FD(fd, CprFd),
48
        VMSTATE_END_OF_LIST()
49
    }
50
};
51
52
static guint cpr_fd_hash(gconstpointer v)
53
0
{
54
0
    const CprFd *elem = v;
55
56
0
    return g_str_hash(elem->name) ^ elem->id;
57
0
}
58
59
static gboolean cpr_fd_equal(gconstpointer a, gconstpointer b)
60
0
{
61
0
    const CprFd *elem_a = a;
62
0
    const CprFd *elem_b = b;
63
64
0
    return !strcmp(elem_a->name, elem_b->name) && elem_a->id == elem_b->id;
65
0
}
66
67
static void cpr_fd_destroy(gpointer data)
68
0
{
69
0
    CprFd *elem = data;
70
71
0
    g_free(elem->name);
72
0
    g_free(elem);
73
0
}
74
75
static GHashTable *get_cpr_fds_hash(void)
76
0
{
77
0
    if (!cpr_fds_hash) {
78
0
        cpr_fds_hash = g_hash_table_new_full(cpr_fd_hash, cpr_fd_equal,
79
0
                                             cpr_fd_destroy, NULL);
80
0
    }
81
82
0
    return cpr_fds_hash;
83
0
}
84
85
static CprFd *find_fd(const char *name, int id)
86
0
{
87
0
    CprFd key = {
88
0
        .name = (char *)name,
89
0
        .id = id,
90
0
    };
91
92
0
    return g_hash_table_lookup(get_cpr_fds_hash(), &key);
93
0
}
94
95
static void cpr_fd_hash_insert(CprFd *elem)
96
0
{
97
0
    GHashTable *hash = get_cpr_fds_hash();
98
99
    /*
100
     * We prohibit inserting duplicate keys into the hash table.  All callers
101
     * are responsible for checking that each added key is unique.
102
     *
103
     * 'elem' serves as both key and value, and a replacing insert would
104
     * silently free it.  Therefore we assert, not overwrite.
105
     */
106
0
    g_assert(!g_hash_table_contains(hash, elem));
107
0
    g_hash_table_insert(hash, elem, elem);
108
0
}
109
110
static int cpr_fd_pre_save(void *opaque)
111
0
{
112
0
    CprState *state = (CprState *)opaque;
113
0
    GHashTableIter iter;
114
0
    CprFd *elem;
115
116
0
    QLIST_INIT(&state->fds);
117
118
0
    g_hash_table_iter_init(&iter, get_cpr_fds_hash());
119
0
    while (g_hash_table_iter_next(&iter, (gpointer *)&elem, NULL)) {
120
0
        QLIST_INSERT_HEAD(&state->fds, elem, next);
121
0
    }
122
123
0
    return 0;
124
0
}
125
126
static int cpr_fd_post_load(void *opaque, int version_id)
127
0
{
128
0
    CprState *state = (CprState *)opaque;
129
0
    CprFd *elem;
130
131
0
    while ((elem = QLIST_FIRST(&state->fds))) {
132
0
        QLIST_REMOVE(elem, next);
133
134
        /*
135
         * Preserve legacy QLIST lookup semantics if duplicate keys exist in
136
         * the incoming stream: the first matching entry wins.
137
         */
138
0
        if (g_hash_table_contains(get_cpr_fds_hash(), elem)) {
139
0
            cpr_fd_destroy(elem);
140
0
            continue;
141
0
        }
142
143
0
        cpr_fd_hash_insert(elem);
144
0
    }
145
146
0
    return 0;
147
0
}
148
149
bool cpr_save_fd(const char *name, int id, int fd, Error **errp)
150
0
{
151
0
    CprFd *elem;
152
153
0
    if (find_fd(name, id)) {
154
0
        error_setg(errp, "cpr fd '%s' id %d is already registered", name, id);
155
0
        return false;
156
0
    }
157
158
0
    elem = g_new0(CprFd, 1);
159
0
    trace_cpr_save_fd(name, id, fd);
160
0
    elem->name = g_strdup(name);
161
0
    elem->namelen = strlen(name) + 1;
162
0
    elem->id = id;
163
0
    elem->fd = fd;
164
0
    cpr_fd_hash_insert(elem);
165
166
0
    return true;
167
0
}
168
169
void cpr_delete_fd(const char *name, int id)
170
0
{
171
0
    CprFd key = {
172
0
        .name = (char *)name,
173
0
        .id = id,
174
0
    };
175
176
0
    g_hash_table_remove(get_cpr_fds_hash(), &key);
177
178
0
    trace_cpr_delete_fd(name, id);
179
0
}
180
181
int cpr_find_fd(const char *name, int id)
182
0
{
183
0
    CprFd *elem = find_fd(name, id);
184
0
    int fd = elem ? elem->fd : -1;
185
186
0
    trace_cpr_find_fd(name, id, fd);
187
0
    return fd;
188
0
}
189
190
void cpr_resave_fd(const char *name, int id, int fd)
191
0
{
192
0
    CprFd *elem = find_fd(name, id);
193
194
0
    if (!elem) {
195
0
        cpr_save_fd(name, id, fd, &error_abort);
196
0
    } else if (elem->fd != fd) {
197
0
        error_report("internal error: cpr fd '%s' id %d value %d "
198
0
                     "already saved with a different value %d",
199
0
                     name, id, fd, elem->fd);
200
0
        g_assert_not_reached();
201
0
    }
202
0
}
203
204
int cpr_open_fd(const char *path, int flags, const char *name, int id,
205
                Error **errp)
206
0
{
207
0
    int fd = cpr_find_fd(name, id);
208
209
0
    if (fd < 0) {
210
0
        fd = qemu_open(path, flags, errp);
211
0
        if (fd >= 0 && !cpr_save_fd(name, id, fd, errp)) {
212
0
            close(fd);
213
0
            fd = -1;
214
0
        }
215
0
    }
216
0
    return fd;
217
0
}
218
219
bool cpr_walk_fd(cpr_walk_fd_cb cb)
220
0
{
221
0
    GHashTableIter iter;
222
0
    CprFd *elem;
223
224
0
    g_hash_table_iter_init(&iter, get_cpr_fds_hash());
225
0
    while (g_hash_table_iter_next(&iter, (gpointer *)&elem, NULL)) {
226
0
        g_assert(elem->fd >= 0);
227
0
        if (!cb(elem->fd)) {
228
0
            return false;
229
0
        }
230
0
    }
231
0
    return true;
232
0
}
233
234
/*************************************************************************/
235
static const VMStateDescription vmstate_cpr_state = {
236
    .name = CPR_STATE,
237
    .version_id = 1,
238
    .minimum_version_id = 1,
239
    .pre_save = cpr_fd_pre_save,
240
    .post_load = cpr_fd_post_load,
241
    .fields = (VMStateField[]) {
242
        VMSTATE_QLIST_V(fds, CprState, 1, vmstate_cpr_fd, CprFd, next),
243
        VMSTATE_END_OF_LIST()
244
    },
245
    .subsections = (const VMStateDescription * const []) {
246
        &vmstate_cpr_vfio_devices,
247
        NULL
248
    }
249
};
250
/*************************************************************************/
251
252
static QEMUFile *cpr_state_file;
253
254
QIOChannel *cpr_state_ioc(void)
255
0
{
256
0
    return qemu_file_get_ioc(cpr_state_file);
257
0
}
258
259
static MigMode incoming_mode = MIG_MODE_NONE;
260
261
MigMode cpr_get_incoming_mode(void)
262
63
{
263
63
    return incoming_mode;
264
63
}
265
266
void cpr_set_incoming_mode(MigMode mode)
267
0
{
268
0
    incoming_mode = mode;
269
0
}
270
271
bool cpr_is_incoming(void)
272
189
{
273
189
    return incoming_mode != MIG_MODE_NONE;
274
189
}
275
276
bool cpr_state_save(MigrationChannel *channel, Error **errp)
277
0
{
278
0
    QEMUFile *f;
279
0
    MigMode mode = migrate_mode();
280
281
0
    trace_cpr_state_save(MigMode_str(mode));
282
283
0
    if (mode == MIG_MODE_CPR_TRANSFER) {
284
0
        g_assert(channel);
285
0
        f = cpr_transfer_output(channel, errp);
286
0
    } else if (mode == MIG_MODE_CPR_EXEC) {
287
0
        f = cpr_exec_output(errp);
288
0
    } else {
289
0
        return true;
290
0
    }
291
0
    if (!f) {
292
0
        return false;
293
0
    }
294
295
0
    qemu_put_be32(f, QEMU_CPR_FILE_MAGIC);
296
0
    qemu_put_be32(f, QEMU_CPR_FILE_VERSION);
297
298
0
    if (!vmstate_save_vmsd(f, &vmstate_cpr_state, &cpr_state, 0, errp)) {
299
0
        qemu_fclose(f);
300
0
        return false;
301
0
    }
302
303
0
    if (migrate_mode() == MIG_MODE_CPR_EXEC) {
304
0
        if (!cpr_exec_persist_state(f, errp)) {
305
0
            qemu_fclose(f);
306
0
            return false;
307
0
        }
308
0
    }
309
310
    /*
311
     * Close the socket only partially so we can later detect when the other
312
     * end closes by getting a HUP event.
313
     */
314
0
    qemu_fflush(f);
315
0
    qio_channel_shutdown(qemu_file_get_ioc(f), QIO_CHANNEL_SHUTDOWN_WRITE,
316
0
                         NULL);
317
0
    cpr_state_file = f;
318
0
    return true;
319
0
}
320
321
bool cpr_state_load(MigrationChannel *channel, Error **errp)
322
63
{
323
63
    uint32_t v;
324
63
    QEMUFile *f;
325
63
    MigMode mode = 0;
326
327
63
    if (cpr_exec_has_state()) {
328
0
        mode = MIG_MODE_CPR_EXEC;
329
0
        f = cpr_exec_input(errp);
330
0
        if (channel) {
331
0
            warn_report("ignoring cpr channel for migration mode cpr-exec");
332
0
        }
333
63
    } else if (channel) {
334
0
        mode = MIG_MODE_CPR_TRANSFER;
335
0
        cpr_set_incoming_mode(mode);
336
0
        f = cpr_transfer_input(channel, errp);
337
63
    } else {
338
63
        return true;
339
63
    }
340
0
    if (!f) {
341
0
        return false;
342
0
    }
343
344
0
    trace_cpr_state_load(MigMode_str(mode));
345
0
    cpr_set_incoming_mode(mode);
346
347
0
    v = qemu_get_be32(f);
348
0
    if (v != QEMU_CPR_FILE_MAGIC) {
349
0
        error_setg(errp, "Not a migration stream (bad magic %x)", v);
350
0
        qemu_fclose(f);
351
0
        return false;
352
0
    }
353
0
    v = qemu_get_be32(f);
354
0
    if (v != QEMU_CPR_FILE_VERSION) {
355
0
        error_setg(errp, "Unsupported migration stream version %d", v);
356
0
        qemu_fclose(f);
357
0
        return false;
358
0
    }
359
360
0
    if (!vmstate_load_vmsd(f, &vmstate_cpr_state, &cpr_state, 1, errp)) {
361
0
        qemu_fclose(f);
362
0
        return false;
363
0
    }
364
365
0
    if (migrate_mode() == MIG_MODE_CPR_EXEC) {
366
        /* Set cloexec to prevent fd leaks from fork until the next cpr-exec */
367
0
        cpr_exec_unpreserve_fds();
368
0
    }
369
370
    /*
371
     * Let the caller decide when to close the socket (and generate a HUP event
372
     * for the sending side).
373
     */
374
0
    cpr_state_file = f;
375
376
0
    return true;
377
0
}
378
379
void cpr_state_close(void)
380
0
{
381
0
    if (cpr_state_file) {
382
0
        qemu_fclose(cpr_state_file);
383
0
        cpr_state_file = NULL;
384
0
    }
385
0
}
386
387
bool cpr_incoming_needed(void *opaque)
388
0
{
389
0
    MigMode mode = migrate_mode();
390
0
    return mode == MIG_MODE_CPR_TRANSFER || mode == MIG_MODE_CPR_EXEC;
391
0
}
392
393
/*
394
 * cpr_get_fd_param: find a descriptor and return its value.
395
 *
396
 * @name: CPR name for the descriptor
397
 * @fdname: An integer-valued string, or a name passed to a getfd command
398
 * @index: CPR index of the descriptor
399
 * @errp: returned error message
400
 *
401
 * If CPR is not being performed, then use @fdname to find the fd.
402
 * If CPR is being performed, then ignore @fdname, and look for @name
403
 * and @index in CPR state.
404
 *
405
 * On success returns the fd value, else returns -1.
406
 */
407
int cpr_get_fd_param(const char *name, const char *fdname, int index,
408
                     Error **errp)
409
0
{
410
0
    ERRP_GUARD();
411
0
    int fd;
412
413
0
    if (cpr_is_incoming()) {
414
0
        fd = cpr_find_fd(name, index);
415
0
        if (fd < 0) {
416
0
            error_setg(errp, "cannot find saved value for fd %s", fdname);
417
0
        }
418
0
    } else {
419
0
        fd = monitor_fd_param(monitor_cur(), fdname, errp);
420
0
        if (fd < 0) {
421
0
            error_prepend(errp, "Could not parse object fd %s:", fdname);
422
0
        } else if (!cpr_save_fd(name, index, fd, errp)) {
423
0
            close(fd);
424
0
            fd = -1;
425
0
        }
426
0
    }
427
0
    return fd;
428
0
}