socket.c 5.1 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
static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
32
{
33
    SocketAddress *saddr;
34

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

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

    return saddr;
}


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

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

    return saddr;
}
C
Chris Lalancette 已提交
57

58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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);
}

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

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

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

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

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

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

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

132

133 134 135
static void socket_accept_incoming_migration(QIONetListener *listener,
                                             QIOChannelSocket *cioc,
                                             gpointer opaque)
C
Chris Lalancette 已提交
136
{
137
    trace_migration_socket_incoming_accepted();
138

139 140
    qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
    migration_channel_process_incoming(QIO_CHANNEL(cioc));
141

142 143
    if (migration_has_all_channels()) {
        /* Close listening socket as its no longer needed */
144 145 146
        qio_net_listener_disconnect(listener);

        object_unref(OBJECT(listener));
147
    }
C
Chris Lalancette 已提交
148 149
}

150

151
static void socket_start_incoming_migration(SocketAddress *saddr,
152
                                            Error **errp)
C
Chris Lalancette 已提交
153
{
154
    QIONetListener *listener = qio_net_listener_new();
C
Chris Lalancette 已提交
155

156
    qio_net_listener_set_name(listener, "migration-socket-listener");
157

158 159
    if (qio_net_listener_open_sync(listener, saddr, errp) < 0) {
        object_unref(OBJECT(listener));
160
        return;
C
Chris Lalancette 已提交
161 162
    }

163 164 165
    qio_net_listener_set_client_func(listener,
                                     socket_accept_incoming_migration,
                                     NULL, NULL);
C
Chris Lalancette 已提交
166
}
167

168 169
void tcp_start_incoming_migration(const char *host_port, Error **errp)
{
170
    Error *err = NULL;
171
    SocketAddress *saddr = tcp_build_address(host_port, &err);
172 173 174
    if (!err) {
        socket_start_incoming_migration(saddr, &err);
    }
175
    qapi_free_SocketAddress(saddr);
176
    error_propagate(errp, err);
177 178
}

179 180
void unix_start_incoming_migration(const char *path, Error **errp)
{
181
    SocketAddress *saddr = unix_build_address(path);
182
    socket_start_incoming_migration(saddr, errp);
183
    qapi_free_SocketAddress(saddr);
184
}