xref: /openbmc/qemu/migration/fd.c (revision 4a1babe5)
1 /*
2  * QEMU live migration via generic fd
3  *
4  * Copyright Red Hat, Inc. 2009-2016
5  *
6  * Authors:
7  *  Chris Lalancette <clalance@redhat.com>
8  *  Daniel P. Berrange <berrange@redhat.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2.  See
11  * the COPYING file in the top-level directory.
12  *
13  * Contributions after 2012-01-13 are licensed under the terms of the
14  * GNU GPL, version 2 or (at your option) any later version.
15  */
16 
17 #include "qemu/osdep.h"
18 #include "qapi/error.h"
19 #include "channel.h"
20 #include "fd.h"
21 #include "migration.h"
22 #include "monitor/monitor.h"
23 #include "io/channel-file.h"
24 #include "io/channel-util.h"
25 #include "options.h"
26 #include "trace.h"
27 
28 
29 static struct FdOutgoingArgs {
30     int fd;
31 } outgoing_args;
32 
33 int fd_args_get_fd(void)
34 {
35     return outgoing_args.fd;
36 }
37 
38 void fd_cleanup_outgoing_migration(void)
39 {
40     if (outgoing_args.fd > 0) {
41         close(outgoing_args.fd);
42         outgoing_args.fd = -1;
43     }
44 }
45 
46 void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
47 {
48     QIOChannel *ioc;
49     int fd = monitor_get_fd(monitor_cur(), fdname, errp);
50 
51     outgoing_args.fd = -1;
52 
53     if (fd == -1) {
54         return;
55     }
56 
57     trace_migration_fd_outgoing(fd);
58     ioc = qio_channel_new_fd(fd, errp);
59     if (!ioc) {
60         close(fd);
61         return;
62     }
63 
64     outgoing_args.fd = fd;
65 
66     qio_channel_set_name(ioc, "migration-fd-outgoing");
67     migration_channel_connect(s, ioc, NULL, NULL);
68     object_unref(OBJECT(ioc));
69 }
70 
71 static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
72                                              GIOCondition condition,
73                                              gpointer opaque)
74 {
75     migration_channel_process_incoming(ioc);
76     object_unref(OBJECT(ioc));
77     return G_SOURCE_REMOVE;
78 }
79 
80 void fd_start_incoming_migration(const char *fdname, Error **errp)
81 {
82     QIOChannel *ioc;
83     int fd = monitor_fd_param(monitor_cur(), fdname, errp);
84     if (fd == -1) {
85         return;
86     }
87 
88     trace_migration_fd_incoming(fd);
89 
90     ioc = qio_channel_new_fd(fd, errp);
91     if (!ioc) {
92         close(fd);
93         return;
94     }
95 
96     qio_channel_set_name(ioc, "migration-fd-incoming");
97     qio_channel_add_watch_full(ioc, G_IO_IN,
98                                fd_accept_incoming_migration,
99                                NULL, NULL,
100                                g_main_context_get_thread_default());
101 
102     if (migrate_multifd()) {
103         int channels = migrate_multifd_channels();
104 
105         while (channels--) {
106             ioc = QIO_CHANNEL(qio_channel_file_new_fd(dup(fd)));
107 
108             if (QIO_CHANNEL_FILE(ioc)->fd == -1) {
109                 error_setg(errp, "Failed to duplicate fd %d", fd);
110                 return;
111             }
112 
113             qio_channel_set_name(ioc, "migration-fd-incoming");
114             qio_channel_add_watch_full(ioc, G_IO_IN,
115                                        fd_accept_incoming_migration,
116                                        NULL, NULL,
117                                        g_main_context_get_thread_default());
118         }
119     }
120 }
121