socket.c 5.8 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
#include "io/channel-socket.h"
27
#include "io/net-listener.h"
28
#include "trace.h"
C
Chris Lalancette 已提交
29 30


31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
struct SocketOutgoingArgs {
    SocketAddress *saddr;
} outgoing_args;

void socket_send_channel_create(QIOTaskFunc f, void *data)
{
    QIOChannelSocket *sioc = qio_channel_socket_new();
    qio_channel_socket_connect_async(sioc, outgoing_args.saddr,
                                     f, data, NULL, NULL);
}

int socket_send_channel_destroy(QIOChannel *send)
{
    /* Remove channel */
    object_unref(OBJECT(send));
    if (outgoing_args.saddr) {
        qapi_free_SocketAddress(outgoing_args.saddr);
        outgoing_args.saddr = NULL;
    }
    return 0;
}

53
static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
54
{
55
    SocketAddress *saddr;
56

57 58 59 60 61
    saddr = g_new0(SocketAddress, 1);
    saddr->type = SOCKET_ADDRESS_TYPE_INET;

    if (inet_parse(&saddr->u.inet, host_port, errp)) {
        qapi_free_SocketAddress(saddr);
62 63 64 65 66 67 68
        return NULL;
    }

    return saddr;
}


69
static SocketAddress *unix_build_address(const char *path)
70
{
71
    SocketAddress *saddr;
72

73 74 75
    saddr = g_new0(SocketAddress, 1);
    saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
    saddr->u.q_unix.path = g_strdup(path);
76 77 78

    return saddr;
}
C
Chris Lalancette 已提交
79

80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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);
}

96
static void socket_outgoing_migration(QIOTask *task,
97
                                      gpointer opaque)
C
Chris Lalancette 已提交
98
{
99
    struct SocketConnectData *data = opaque;
100 101
    QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
    Error *err = NULL;
C
Chris Lalancette 已提交
102

103
    if (qio_task_propagate_error(task, &err)) {
104
        trace_migration_socket_outgoing_error(error_get_pretty(err));
105
    } else {
106
        trace_migration_socket_outgoing_connected(data->hostname);
C
Chris Lalancette 已提交
107
    }
108
    migration_channel_connect(data->s, sioc, data->hostname, err);
109
    object_unref(OBJECT(sioc));
C
Chris Lalancette 已提交
110 111
}

112
static void socket_start_outgoing_migration(MigrationState *s,
113
                                            SocketAddress *saddr,
114
                                            Error **errp)
C
Chris Lalancette 已提交
115
{
116
    QIOChannelSocket *sioc = qio_channel_socket_new();
117
    struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
118

119
    data->s = s;
120 121 122 123 124

    /* in case previous migration leaked it */
    qapi_free_SocketAddress(outgoing_args.saddr);
    outgoing_args.saddr = saddr;

125 126
    if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
        data->hostname = g_strdup(saddr->u.inet.host);
127
    }
128

129
    qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
130 131
    qio_channel_socket_connect_async(sioc,
                                     saddr,
132
                                     socket_outgoing_migration,
133
                                     data,
134 135
                                     socket_connect_data_free,
                                     NULL);
C
Chris Lalancette 已提交
136 137
}

138 139 140 141
void tcp_start_outgoing_migration(MigrationState *s,
                                  const char *host_port,
                                  Error **errp)
{
142
    Error *err = NULL;
143
    SocketAddress *saddr = tcp_build_address(host_port, &err);
144 145 146 147
    if (!err) {
        socket_start_outgoing_migration(s, saddr, &err);
    }
    error_propagate(errp, err);
148 149
}

150 151 152 153
void unix_start_outgoing_migration(MigrationState *s,
                                   const char *path,
                                   Error **errp)
{
154
    SocketAddress *saddr = unix_build_address(path);
155 156 157
    socket_start_outgoing_migration(s, saddr, errp);
}

158

159 160 161
static void socket_accept_incoming_migration(QIONetListener *listener,
                                             QIOChannelSocket *cioc,
                                             gpointer opaque)
C
Chris Lalancette 已提交
162
{
163
    trace_migration_socket_incoming_accepted();
164

165 166
    qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
    migration_channel_process_incoming(QIO_CHANNEL(cioc));
167

168 169
    if (migration_has_all_channels()) {
        /* Close listening socket as its no longer needed */
170 171 172
        qio_net_listener_disconnect(listener);

        object_unref(OBJECT(listener));
173
    }
C
Chris Lalancette 已提交
174 175
}

176

177
static void socket_start_incoming_migration(SocketAddress *saddr,
178
                                            Error **errp)
C
Chris Lalancette 已提交
179
{
180
    QIONetListener *listener = qio_net_listener_new();
C
Chris Lalancette 已提交
181

182
    qio_net_listener_set_name(listener, "migration-socket-listener");
183

184 185
    if (qio_net_listener_open_sync(listener, saddr, errp) < 0) {
        object_unref(OBJECT(listener));
186
        return;
C
Chris Lalancette 已提交
187 188
    }

189 190 191
    qio_net_listener_set_client_func(listener,
                                     socket_accept_incoming_migration,
                                     NULL, NULL);
C
Chris Lalancette 已提交
192
}
193

194 195
void tcp_start_incoming_migration(const char *host_port, Error **errp)
{
196
    Error *err = NULL;
197
    SocketAddress *saddr = tcp_build_address(host_port, &err);
198 199 200
    if (!err) {
        socket_start_incoming_migration(saddr, &err);
    }
201
    qapi_free_SocketAddress(saddr);
202
    error_propagate(errp, err);
203 204
}

205 206
void unix_start_incoming_migration(const char *path, Error **errp)
{
207
    SocketAddress *saddr = unix_build_address(path);
208
    socket_start_incoming_migration(saddr, errp);
209
    qapi_free_SocketAddress(saddr);
210
}