Statistics
| Branch: | Revision:

root / migration-tcp.c @ dd4239d6

History | View | Annotate | Download (4.8 kB)

1 34c9dd8e aliguori
/*
2 34c9dd8e aliguori
 * QEMU live migration
3 34c9dd8e aliguori
 *
4 34c9dd8e aliguori
 * Copyright IBM, Corp. 2008
5 34c9dd8e aliguori
 *
6 34c9dd8e aliguori
 * Authors:
7 34c9dd8e aliguori
 *  Anthony Liguori   <aliguori@us.ibm.com>
8 34c9dd8e aliguori
 *
9 34c9dd8e aliguori
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 34c9dd8e aliguori
 * the COPYING file in the top-level directory.
11 34c9dd8e aliguori
 *
12 34c9dd8e aliguori
 */
13 34c9dd8e aliguori
14 34c9dd8e aliguori
#include "qemu-common.h"
15 34c9dd8e aliguori
#include "qemu_socket.h"
16 34c9dd8e aliguori
#include "migration.h"
17 34c9dd8e aliguori
#include "qemu-char.h"
18 34c9dd8e aliguori
#include "sysemu.h"
19 34c9dd8e aliguori
#include "buffered_file.h"
20 34c9dd8e aliguori
#include "block.h"
21 34c9dd8e aliguori
22 34c9dd8e aliguori
//#define DEBUG_MIGRATION_TCP
23 34c9dd8e aliguori
24 34c9dd8e aliguori
#ifdef DEBUG_MIGRATION_TCP
25 34c9dd8e aliguori
#define dprintf(fmt, ...) \
26 34c9dd8e aliguori
    do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
27 34c9dd8e aliguori
#else
28 34c9dd8e aliguori
#define dprintf(fmt, ...) \
29 34c9dd8e aliguori
    do { } while (0)
30 34c9dd8e aliguori
#endif
31 34c9dd8e aliguori
32 065e2813 aliguori
static int socket_errno(FdMigrationState *s)
33 34c9dd8e aliguori
{
34 8ad9fa5d aliguori
    return socket_error();
35 34c9dd8e aliguori
}
36 34c9dd8e aliguori
37 065e2813 aliguori
static int socket_write(FdMigrationState *s, const void * buf, size_t size)
38 34c9dd8e aliguori
{
39 065e2813 aliguori
    return send(s->fd, buf, size, 0);
40 34c9dd8e aliguori
}
41 34c9dd8e aliguori
42 065e2813 aliguori
static int tcp_close(FdMigrationState *s)
43 34c9dd8e aliguori
{
44 065e2813 aliguori
    dprintf("tcp_close\n");
45 34c9dd8e aliguori
    if (s->fd != -1) {
46 ff8d81d8 aliguori
        close(s->fd);
47 ff8d81d8 aliguori
        s->fd = -1;
48 34c9dd8e aliguori
    }
49 34c9dd8e aliguori
    return 0;
50 34c9dd8e aliguori
}
51 34c9dd8e aliguori
52 34c9dd8e aliguori
53 34c9dd8e aliguori
static void tcp_wait_for_connect(void *opaque)
54 34c9dd8e aliguori
{
55 34c9dd8e aliguori
    FdMigrationState *s = opaque;
56 34c9dd8e aliguori
    int val, ret;
57 4761a48b blueswir1
    socklen_t valsize = sizeof(val);
58 34c9dd8e aliguori
59 34c9dd8e aliguori
    dprintf("connect completed\n");
60 34c9dd8e aliguori
    do {
61 0a656f5f malc
        ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
62 065e2813 aliguori
    } while (ret == -1 && (s->get_error(s)) == EINTR);
63 34c9dd8e aliguori
64 34c9dd8e aliguori
    if (ret < 0) {
65 065e2813 aliguori
        migrate_fd_error(s);
66 34c9dd8e aliguori
        return;
67 34c9dd8e aliguori
    }
68 34c9dd8e aliguori
69 34c9dd8e aliguori
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
70 34c9dd8e aliguori
71 34c9dd8e aliguori
    if (val == 0)
72 065e2813 aliguori
        migrate_fd_connect(s);
73 34c9dd8e aliguori
    else {
74 34c9dd8e aliguori
        dprintf("error connecting %d\n", val);
75 065e2813 aliguori
        migrate_fd_error(s);
76 34c9dd8e aliguori
    }
77 34c9dd8e aliguori
}
78 34c9dd8e aliguori
79 34c9dd8e aliguori
MigrationState *tcp_start_outgoing_migration(const char *host_port,
80 ff8d81d8 aliguori
                                             int64_t bandwidth_limit,
81 c163b5ca lirans@il.ibm.com
                                             int detach,
82 c163b5ca lirans@il.ibm.com
                                             int blk,
83 c163b5ca lirans@il.ibm.com
                                             int inc)
84 34c9dd8e aliguori
{
85 34c9dd8e aliguori
    struct sockaddr_in addr;
86 34c9dd8e aliguori
    FdMigrationState *s;
87 34c9dd8e aliguori
    int ret;
88 34c9dd8e aliguori
89 34c9dd8e aliguori
    if (parse_host_port(&addr, host_port) < 0)
90 34c9dd8e aliguori
        return NULL;
91 34c9dd8e aliguori
92 34c9dd8e aliguori
    s = qemu_mallocz(sizeof(*s));
93 34c9dd8e aliguori
94 065e2813 aliguori
    s->get_error = socket_errno;
95 065e2813 aliguori
    s->write = socket_write;
96 065e2813 aliguori
    s->close = tcp_close;
97 065e2813 aliguori
    s->mig_state.cancel = migrate_fd_cancel;
98 065e2813 aliguori
    s->mig_state.get_status = migrate_fd_get_status;
99 065e2813 aliguori
    s->mig_state.release = migrate_fd_release;
100 34c9dd8e aliguori
101 c163b5ca lirans@il.ibm.com
    s->mig_state.blk = blk;
102 c163b5ca lirans@il.ibm.com
    s->mig_state.shared = inc;
103 c163b5ca lirans@il.ibm.com
104 34c9dd8e aliguori
    s->state = MIG_STATE_ACTIVE;
105 731b0364 aliguori
    s->mon_resume = NULL;
106 34c9dd8e aliguori
    s->bandwidth_limit = bandwidth_limit;
107 34c9dd8e aliguori
    s->fd = socket(PF_INET, SOCK_STREAM, 0);
108 34c9dd8e aliguori
    if (s->fd == -1) {
109 34c9dd8e aliguori
        qemu_free(s);
110 ff8d81d8 aliguori
        return NULL;
111 34c9dd8e aliguori
    }
112 34c9dd8e aliguori
113 17e90973 aliguori
    socket_set_nonblock(s->fd);
114 34c9dd8e aliguori
115 731b0364 aliguori
    if (!detach)
116 731b0364 aliguori
        migrate_fd_monitor_suspend(s);
117 34c9dd8e aliguori
118 34c9dd8e aliguori
    do {
119 34c9dd8e aliguori
        ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
120 34c9dd8e aliguori
        if (ret == -1)
121 065e2813 aliguori
            ret = -(s->get_error(s));
122 34c9dd8e aliguori
123 c1d36665 aliguori
        if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
124 34c9dd8e aliguori
            qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
125 34c9dd8e aliguori
    } while (ret == -EINTR);
126 34c9dd8e aliguori
127 c1d36665 aliguori
    if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
128 34c9dd8e aliguori
        dprintf("connect failed\n");
129 34c9dd8e aliguori
        close(s->fd);
130 34c9dd8e aliguori
        qemu_free(s);
131 88d2d9e0 aliguori
        return NULL;
132 34c9dd8e aliguori
    } else if (ret >= 0)
133 065e2813 aliguori
        migrate_fd_connect(s);
134 34c9dd8e aliguori
135 34c9dd8e aliguori
    return &s->mig_state;
136 34c9dd8e aliguori
}
137 34c9dd8e aliguori
138 34c9dd8e aliguori
static void tcp_accept_incoming_migration(void *opaque)
139 34c9dd8e aliguori
{
140 34c9dd8e aliguori
    struct sockaddr_in addr;
141 34c9dd8e aliguori
    socklen_t addrlen = sizeof(addr);
142 34c9dd8e aliguori
    int s = (unsigned long)opaque;
143 34c9dd8e aliguori
    QEMUFile *f;
144 34c9dd8e aliguori
    int c, ret;
145 34c9dd8e aliguori
146 34c9dd8e aliguori
    do {
147 34c9dd8e aliguori
        c = accept(s, (struct sockaddr *)&addr, &addrlen);
148 c1d36665 aliguori
    } while (c == -1 && socket_error() == EINTR);
149 34c9dd8e aliguori
150 34c9dd8e aliguori
    dprintf("accepted migration\n");
151 34c9dd8e aliguori
152 34c9dd8e aliguori
    if (c == -1) {
153 34c9dd8e aliguori
        fprintf(stderr, "could not accept migration connection\n");
154 34c9dd8e aliguori
        return;
155 34c9dd8e aliguori
    }
156 34c9dd8e aliguori
157 c1d36665 aliguori
    f = qemu_fopen_socket(c);
158 34c9dd8e aliguori
    if (f == NULL) {
159 34c9dd8e aliguori
        fprintf(stderr, "could not qemu_fopen socket\n");
160 34c9dd8e aliguori
        goto out;
161 34c9dd8e aliguori
    }
162 34c9dd8e aliguori
163 34c9dd8e aliguori
    ret = qemu_loadvm_state(f);
164 34c9dd8e aliguori
    if (ret < 0) {
165 34c9dd8e aliguori
        fprintf(stderr, "load of migration failed\n");
166 34c9dd8e aliguori
        goto out_fopen;
167 34c9dd8e aliguori
    }
168 34c9dd8e aliguori
    qemu_announce_self();
169 34c9dd8e aliguori
    dprintf("successfully loaded vm state\n");
170 34c9dd8e aliguori
171 34c9dd8e aliguori
    /* we've successfully migrated, close the server socket */
172 34c9dd8e aliguori
    qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
173 34c9dd8e aliguori
    close(s);
174 d399f677 Paolo Bonzini
    if (autostart)
175 d399f677 Paolo Bonzini
        vm_start();
176 34c9dd8e aliguori
177 34c9dd8e aliguori
out_fopen:
178 34c9dd8e aliguori
    qemu_fclose(f);
179 34c9dd8e aliguori
out:
180 34c9dd8e aliguori
    close(c);
181 34c9dd8e aliguori
}
182 34c9dd8e aliguori
183 34c9dd8e aliguori
int tcp_start_incoming_migration(const char *host_port)
184 34c9dd8e aliguori
{
185 34c9dd8e aliguori
    struct sockaddr_in addr;
186 34c9dd8e aliguori
    int val;
187 34c9dd8e aliguori
    int s;
188 34c9dd8e aliguori
189 34c9dd8e aliguori
    if (parse_host_port(&addr, host_port) < 0) {
190 34c9dd8e aliguori
        fprintf(stderr, "invalid host/port combination: %s\n", host_port);
191 34c9dd8e aliguori
        return -EINVAL;
192 34c9dd8e aliguori
    }
193 34c9dd8e aliguori
194 34c9dd8e aliguori
    s = socket(PF_INET, SOCK_STREAM, 0);
195 34c9dd8e aliguori
    if (s == -1)
196 c1d36665 aliguori
        return -socket_error();
197 34c9dd8e aliguori
198 34c9dd8e aliguori
    val = 1;
199 34c9dd8e aliguori
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
200 34c9dd8e aliguori
201 34c9dd8e aliguori
    if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
202 34c9dd8e aliguori
        goto err;
203 34c9dd8e aliguori
204 34c9dd8e aliguori
    if (listen(s, 1) == -1)
205 34c9dd8e aliguori
        goto err;
206 34c9dd8e aliguori
207 34c9dd8e aliguori
    qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
208 34c9dd8e aliguori
                         (void *)(unsigned long)s);
209 34c9dd8e aliguori
210 34c9dd8e aliguori
    return 0;
211 34c9dd8e aliguori
212 34c9dd8e aliguori
err:
213 34c9dd8e aliguori
    close(s);
214 c1d36665 aliguori
    return -socket_error();
215 34c9dd8e aliguori
}