fd.c 2.1 KB
Newer Older
P
Paolo Bonzini 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * QEMU live migration via generic fd
 *
 * Copyright Red Hat, Inc. 2009
 *
 * Authors:
 *  Chris Lalancette <clalance@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
12 13
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
P
Paolo Bonzini 已提交
14 15
 */

P
Peter Maydell 已提交
16
#include "qemu/osdep.h"
17
#include "qapi/error.h"
P
Paolo Bonzini 已提交
18
#include "qemu-common.h"
19
#include "qemu/main-loop.h"
20
#include "qemu/sockets.h"
21
#include "migration/migration.h"
22
#include "monitor/monitor.h"
J
Juan Quintela 已提交
23
#include "migration/qemu-file.h"
24
#include "block/block.h"
P
Paolo Bonzini 已提交
25 26 27 28

//#define DEBUG_MIGRATION_FD

#ifdef DEBUG_MIGRATION_FD
M
malc 已提交
29
#define DPRINTF(fmt, ...) \
P
Paolo Bonzini 已提交
30 31
    do { printf("migration-fd: " fmt, ## __VA_ARGS__); } while (0)
#else
M
malc 已提交
32
#define DPRINTF(fmt, ...) \
P
Paolo Bonzini 已提交
33 34 35
    do { } while (0)
#endif

36 37 38 39 40 41 42 43 44 45 46
static bool fd_is_socket(int fd)
{
    struct stat stat;
    int ret = fstat(fd, &stat);
    if (ret == -1) {
        /* When in doubt say no */
        return false;
    }
    return S_ISSOCK(stat.st_mode);
}

47
void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
P
Paolo Bonzini 已提交
48
{
49 50
    int fd = monitor_get_fd(cur_mon, fdname, errp);
    if (fd == -1) {
51
        return;
P
Paolo Bonzini 已提交
52
    }
53 54

    if (fd_is_socket(fd)) {
55
        s->to_dst_file = qemu_fopen_socket(fd, "wb");
56
    } else {
57
        s->to_dst_file = qemu_fdopen(fd, "wb");
58
    }
P
Paolo Bonzini 已提交
59 60 61 62 63 64 65 66

    migrate_fd_connect(s);
}

static void fd_accept_incoming_migration(void *opaque)
{
    QEMUFile *f = opaque;

67
    qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL);
68
    process_incoming_migration(f);
P
Paolo Bonzini 已提交
69 70
}

71
void fd_start_incoming_migration(const char *infd, Error **errp)
P
Paolo Bonzini 已提交
72 73 74 75
{
    int fd;
    QEMUFile *f;

M
malc 已提交
76
    DPRINTF("Attempting to start an incoming migration via fd\n");
P
Paolo Bonzini 已提交
77 78

    fd = strtol(infd, NULL, 0);
79 80 81 82 83
    if (fd_is_socket(fd)) {
        f = qemu_fopen_socket(fd, "rb");
    } else {
        f = qemu_fdopen(fd, "rb");
    }
P
Paolo Bonzini 已提交
84
    if(f == NULL) {
85 86
        error_setg_errno(errp, errno, "failed to open the source descriptor");
        return;
P
Paolo Bonzini 已提交
87 88
    }

89
    qemu_set_fd_handler(fd, fd_accept_incoming_migration, NULL, f);
P
Paolo Bonzini 已提交
90
}