socket.c 5.6 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
                                     data,
106 107
                                     socket_connect_data_free,
                                     NULL);
108
    qapi_free_SocketAddress(saddr);
C
Chris Lalancette 已提交
109 110
}

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

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

131

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

139 140 141 142 143
    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 已提交
144 145 146
        goto out;
    }

147
    trace_migration_socket_incoming_accepted();
148

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

C
Chris Lalancette 已提交
153
out:
154 155 156 157 158 159 160
    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 已提交
161 162
}

163

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

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

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

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

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

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