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/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
        migrate_fd_error(data->s, err);
83
        error_free(err);
84
    } else {
85
        trace_migration_socket_outgoing_connected(data->hostname);
86
        migration_channel_connect(data->s, sioc, data->hostname);
C
Chris Lalancette 已提交
87
    }
88
    object_unref(OBJECT(sioc));
C
Chris Lalancette 已提交
89 90
}

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

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

103
    qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
104 105
    qio_channel_socket_connect_async(sioc,
                                     saddr,
106
                                     socket_outgoing_migration,
107 108
                                     data,
                                     socket_connect_data_free);
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 gboolean socket_accept_incoming_migration(QIOChannel *ioc,
                                                 GIOCondition condition,
                                                 gpointer opaque)
C
Chris Lalancette 已提交
136
{
137 138
    QIOChannelSocket *sioc;
    Error *err = NULL;
C
Chris Lalancette 已提交
139

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

148
    trace_migration_socket_incoming_accepted();
149

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

C
Chris Lalancette 已提交
155
out:
156 157 158
    /* Close listening socket as its no longer needed */
    qio_channel_close(ioc, NULL);
    return FALSE; /* unregister */
C
Chris Lalancette 已提交
159 160
}

161

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

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

170 171
    if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) {
        object_unref(OBJECT(listen_ioc));
172
        qapi_free_SocketAddress(saddr);
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);
181
    qapi_free_SocketAddress(saddr);
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 191
    if (!err) {
        socket_start_incoming_migration(saddr, &err);
    }
    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 198
    socket_start_incoming_migration(saddr, errp);
}