master
c 107 lines 3.32 KB
Raw
1 /*
2 * Copyright (c) 2022, 2024 Oracle and/or its affiliates.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8 #include "qemu/osdep.h"
9 #include "qemu/main-loop.h"
10 #include "qapi/clone-visitor.h"
11 #include "qapi/error.h"
12 #include "qapi/qapi-visit-migration.h"
13 #include "io/channel.h"
14 #include "io/channel-file.h"
15 #include "io/channel-socket.h"
16 #include "io/net-listener.h"
17 #include "migration/cpr.h"
18 #include "migration/migration.h"
19 #include "migration/savevm.h"
20 #include "migration/qemu-file.h"
21 #include "migration/vmstate.h"
22 #include "trace.h"
23
24 QEMUFile *cpr_transfer_output(MigrationChannel *channel, Error **errp)
25 {
26 MigrationAddress *addr = channel->addr;
27
28 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET &&
29 addr->u.socket.type == SOCKET_ADDRESS_TYPE_UNIX) {
30
31 g_autoptr(QIOChannelSocket) sioc = qio_channel_socket_new();
32 QIOChannel *ioc = QIO_CHANNEL(sioc);
33 SocketAddress *saddr = &addr->u.socket;
34
35 if (qio_channel_socket_connect_sync(sioc, saddr, errp) < 0) {
36 return NULL;
37 }
38 trace_cpr_transfer_output(addr->u.socket.u.q_unix.path);
39 qio_channel_set_name(ioc, "cpr-out");
40 return qemu_file_new_output(ioc);
41
42 } else {
43 error_setg(errp, "bad cpr channel address; must be unix");
44 return NULL;
45 }
46 }
47
48 QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
49 {
50 MigrationAddress *addr = channel->addr;
51
52 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET &&
53 (addr->u.socket.type == SOCKET_ADDRESS_TYPE_UNIX ||
54 addr->u.socket.type == SOCKET_ADDRESS_TYPE_FD)) {
55
56 g_autoptr(QIOChannelSocket) sioc = NULL;
57 SocketAddress *saddr = &addr->u.socket;
58 g_autoptr(QIONetListener) listener = qio_net_listener_new();
59 QIOChannel *ioc;
60
61 qio_net_listener_set_name(listener, "cpr-socket-listener");
62 if (qio_net_listener_open_sync(listener, saddr, 1, errp) < 0) {
63 return NULL;
64 }
65
66 sioc = qio_net_listener_wait_client(listener);
67 ioc = QIO_CHANNEL(sioc);
68 trace_cpr_transfer_input(
69 addr->u.socket.type == SOCKET_ADDRESS_TYPE_UNIX ?
70 addr->u.socket.u.q_unix.path : addr->u.socket.u.fd.str);
71 qio_channel_set_name(ioc, "cpr-in");
72 return qemu_file_new_input(ioc);
73
74 } else {
75 error_setg(errp, "bad cpr channel socket type; must be unix");
76 return NULL;
77 }
78 }
79
80 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
81 void *opaque)
82 {
83 assert(bql_locked());
84 s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);
85 g_source_set_callback(s->hup_source,
86 (GSourceFunc)func,
87 QAPI_CLONE(MigrationAddress, opaque),
88 (GDestroyNotify)qapi_free_MigrationAddress);
89 g_source_attach(s->hup_source, NULL);
90 }
91
92 void cpr_transfer_source_destroy(MigrationState *s)
93 {
94 assert(bql_locked());
95 if (s->hup_source) {
96 g_source_destroy(s->hup_source);
97 g_source_unref(s->hup_source);
98 s->hup_source = NULL;
99 }
100 }
101
102 bool cpr_transfer_source_active(MigrationState *s)
103 {
104 /* Whenever the HUP gsource is available, it's active. */
105 assert(bql_locked());
106 return s->hup_source;
107 }