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

P
Peter Maydell 已提交
17
#include "qemu/osdep.h"
18

C
Chris Lalancette 已提交
19
#include "qemu-common.h"
20
#include "qemu/error-report.h"
21
#include "qapi/error.h"
22
#include "channel.h"
23
#include "socket.h"
24
#include "migration.h"
J
Juan Quintela 已提交
25
#include "qemu-file.h"
26 27
#include "io/channel-socket.h"
#include "trace.h"
C
Chris Lalancette 已提交
28 29


30
static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
31
{
32
    SocketAddress *saddr;
33

34 35 36 37 38
    saddr = g_new0(SocketAddress, 1);
    saddr->type = SOCKET_ADDRESS_TYPE_INET;

    if (inet_parse(&saddr->u.inet, host_port, errp)) {
        qapi_free_SocketAddress(saddr);
39 40 41 42 43 44 45
        return NULL;
    }

    return saddr;
}


46
static SocketAddress *unix_build_address(const char *path)
47
{
48
    SocketAddress *saddr;
49

50 51 52
    saddr = g_new0(SocketAddress, 1);
    saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
    saddr->u.q_unix.path = g_strdup(path);
53 54 55

    return saddr;
}
C
Chris Lalancette 已提交
56

57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
struct SocketConnectData {
    MigrationState *s;
    char *hostname;
};

static void socket_connect_data_free(void *opaque)
{
    struct SocketConnectData *data = opaque;
    if (!data) {
        return;
    }
    g_free(data->hostname);
    g_free(data);
}

73
static void socket_outgoing_migration(QIOTask *task,
74
                                      gpointer opaque)
C
Chris Lalancette 已提交
75
{
76
    struct SocketConnectData *data = opaque;
77 78
    QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
    Error *err = NULL;
C
Chris Lalancette 已提交
79

80
    if (qio_task_propagate_error(task, &err)) {
81
        trace_migration_socket_outgoing_error(error_get_pretty(err));
82
    } else {
83
        trace_migration_socket_outgoing_connected(data->hostname);
C
Chris Lalancette 已提交
84
    }
85
    migration_channel_connect(data->s, sioc, data->hostname, err);
86
    object_unref(OBJECT(sioc));
C
Chris Lalancette 已提交
87 88
}

89
static void socket_start_outgoing_migration(MigrationState *s,
90
                                            SocketAddress *saddr,
91
                                            Error **errp)
C
Chris Lalancette 已提交
92
{
93
    QIOChannelSocket *sioc = qio_channel_socket_new();
94
    struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
95

96
    data->s = s;
97 98
    if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
        data->hostname = g_strdup(saddr->u.inet.host);
99
    }
100

101
    qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
102 103
    qio_channel_socket_connect_async(sioc,
                                     saddr,
104
                                     socket_outgoing_migration,
105 106
                                     data,
                                     socket_connect_data_free);
107
    qapi_free_SocketAddress(saddr);
C
Chris Lalancette 已提交
108 109
}

110 111 112 113
void tcp_start_outgoing_migration(MigrationState *s,
                                  const char *host_port,
                                  Error **errp)
{
114
    Error *err = NULL;
115
    SocketAddress *saddr = tcp_build_address(host_port, &err);
116 117 118 119
    if (!err) {
        socket_start_outgoing_migration(s, saddr, &err);
    }
    error_propagate(errp, err);
120 121
}

122 123 124 125
void unix_start_outgoing_migration(MigrationState *s,
                                   const char *path,
                                   Error **errp)
{
126
    SocketAddress *saddr = unix_build_address(path);
127 128 129
    socket_start_outgoing_migration(s, saddr, errp);
}

130

131 132 133
static gboolean socket_accept_incoming_migration(QIOChannel *ioc,
                                                 GIOCondition condition,
                                                 gpointer opaque)
C
Chris Lalancette 已提交
134
{
135 136
    QIOChannelSocket *sioc;
    Error *err = NULL;
C
Chris Lalancette 已提交
137

138 139 140 141 142
    sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
                                     &err);
    if (!sioc) {
        error_report("could not accept migration connection (%s)",
                     error_get_pretty(err));
C
Chris Lalancette 已提交
143 144 145
        goto out;
    }

146
    trace_migration_socket_incoming_accepted();
147

148
    qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-incoming");
149
    migration_channel_process_incoming(QIO_CHANNEL(sioc));
150
    object_unref(OBJECT(sioc));
151

C
Chris Lalancette 已提交
152
out:
153 154 155 156 157 158 159
    if (migration_has_all_channels()) {
        /* Close listening socket as its no longer needed */
        qio_channel_close(ioc, NULL);
        return G_SOURCE_REMOVE;
    } else {
        return G_SOURCE_CONTINUE;
    }
C
Chris Lalancette 已提交
160 161
}

162

163
static void socket_start_incoming_migration(SocketAddress *saddr,
164
                                            Error **errp)
C
Chris Lalancette 已提交
165
{
166
    QIOChannelSocket *listen_ioc = qio_channel_socket_new();
C
Chris Lalancette 已提交
167

168 169 170
    qio_channel_set_name(QIO_CHANNEL(listen_ioc),
                         "migration-socket-listener");

171 172
    if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) {
        object_unref(OBJECT(listen_ioc));
173
        return;
C
Chris Lalancette 已提交
174 175
    }

176 177
    qio_channel_add_watch(QIO_CHANNEL(listen_ioc),
                          G_IO_IN,
178
                          socket_accept_incoming_migration,
179 180
                          listen_ioc,
                          (GDestroyNotify)object_unref);
C
Chris Lalancette 已提交
181
}
182

183 184
void tcp_start_incoming_migration(const char *host_port, Error **errp)
{
185
    Error *err = NULL;
186
    SocketAddress *saddr = tcp_build_address(host_port, &err);
187 188 189
    if (!err) {
        socket_start_incoming_migration(saddr, &err);
    }
190
    qapi_free_SocketAddress(saddr);
191
    error_propagate(errp, err);
192 193
}

194 195
void unix_start_incoming_migration(const char *path, Error **errp)
{
196
    SocketAddress *saddr = unix_build_address(path);
197
    socket_start_incoming_migration(saddr, errp);
198
    qapi_free_SocketAddress(saddr);
199
}