migration-tcp.c 2.9 KB
Newer Older
A
aliguori 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * QEMU live migration
 *
 * Copyright IBM, Corp. 2008
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.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.
A
aliguori 已提交
14 15 16 17 18 19 20 21 22 23 24 25
 */

#include "qemu-common.h"
#include "qemu_socket.h"
#include "migration.h"
#include "qemu-char.h"
#include "buffered_file.h"
#include "block.h"

//#define DEBUG_MIGRATION_TCP

#ifdef DEBUG_MIGRATION_TCP
M
malc 已提交
26
#define DPRINTF(fmt, ...) \
A
aliguori 已提交
27 28
    do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
#else
M
malc 已提交
29
#define DPRINTF(fmt, ...) \
A
aliguori 已提交
30 31 32
    do { } while (0)
#endif

33
static int socket_errno(MigrationState *s)
A
aliguori 已提交
34
{
35
    return socket_error();
A
aliguori 已提交
36 37
}

38
static int socket_write(MigrationState *s, const void * buf, size_t size)
A
aliguori 已提交
39
{
40
    return send(s->fd, buf, size, 0);
A
aliguori 已提交
41 42
}

43
static int tcp_close(MigrationState *s)
A
aliguori 已提交
44
{
45
    int r = 0;
M
malc 已提交
46
    DPRINTF("tcp_close\n");
A
aliguori 已提交
47
    if (s->fd != -1) {
48 49 50
        if (close(s->fd) < 0) {
            r = -errno;
        }
A
aliguori 已提交
51
        s->fd = -1;
A
aliguori 已提交
52
    }
53
    return r;
A
aliguori 已提交
54 55
}

56
static void tcp_wait_for_connect(int fd, void *opaque)
A
aliguori 已提交
57
{
58
    MigrationState *s = opaque;
A
aliguori 已提交
59

60 61 62
    if (fd < 0) {
        DPRINTF("migrate connect error\n");
        s->fd = -1;
63
        migrate_fd_error(s);
64 65 66
    } else {
        DPRINTF("migrate connect success\n");
        s->fd = fd;
67
        migrate_fd_connect(s);
A
aliguori 已提交
68 69 70
    }
}

71 72
int tcp_start_outgoing_migration(MigrationState *s, const char *host_port,
                                 Error **errp)
A
aliguori 已提交
73
{
74 75 76
    s->get_error = socket_errno;
    s->write = socket_write;
    s->close = tcp_close;
A
aliguori 已提交
77

78 79
    s->fd = inet_nonblocking_connect(host_port, tcp_wait_for_connect, s,
                                     errp);
80 81 82 83
    if (error_is_set(errp)) {
        migrate_fd_error(s);
        return -1;
    }
A
aliguori 已提交
84

85
    return 0;
A
aliguori 已提交
86 87 88 89 90 91
}

static void tcp_accept_incoming_migration(void *opaque)
{
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
92
    int s = (intptr_t)opaque;
A
aliguori 已提交
93
    QEMUFile *f;
94
    int c;
A
aliguori 已提交
95 96

    do {
K
Kevin Wolf 已提交
97
        c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
98
    } while (c == -1 && socket_error() == EINTR);
A
aliguori 已提交
99

M
malc 已提交
100
    DPRINTF("accepted migration\n");
A
aliguori 已提交
101 102 103

    if (c == -1) {
        fprintf(stderr, "could not accept migration connection\n");
104
        goto out2;
A
aliguori 已提交
105 106
    }

107
    f = qemu_fopen_socket(c);
A
aliguori 已提交
108 109 110 111 112
    if (f == NULL) {
        fprintf(stderr, "could not qemu_fopen socket\n");
        goto out;
    }

113
    process_incoming_migration(f);
A
aliguori 已提交
114 115
    qemu_fclose(f);
out:
116 117
    close(c);
out2:
118 119
    qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
    close(s);
A
aliguori 已提交
120 121
}

122
int tcp_start_incoming_migration(const char *host_port, Error **errp)
A
aliguori 已提交
123 124 125
{
    int s;

126
    s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp);
A
aliguori 已提交
127

128 129
    if (s < 0) {
        return -1;
130
    }
A
aliguori 已提交
131 132

    qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
133
                         (void *)(intptr_t)s);
A
aliguori 已提交
134 135 136

    return 0;
}