xref: /qemu/migration/fd.c (revision eac57306)
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 "file.h"
22 #include "migration.h"
23 #include "monitor/monitor.h"
24 #include "io/channel-file.h"
25 #include "io/channel-socket.h"
26 #include "io/channel-util.h"
27 #include "options.h"
28 #include "trace.h"
29 
30 
31 static struct FdOutgoingArgs {
32     int fd;
33 } outgoing_args;
34 
35 int fd_args_get_fd(void)
36 {
37     return outgoing_args.fd;
38 }
39 
40 void fd_cleanup_outgoing_migration(void)
41 {
42     if (outgoing_args.fd > 0) {
43         close(outgoing_args.fd);
44         outgoing_args.fd = -1;
45     }
46 }
47 
48 void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
49 {
50     QIOChannel *ioc;
51     int fd = monitor_get_fd(monitor_cur(), fdname, errp);
52     int newfd;
53 
54     if (fd == -1) {
55         return;
56     }
57 
58     trace_migration_fd_outgoing(fd);
59     ioc = qio_channel_new_fd(fd, errp);
60     if (!ioc) {
61         close(fd);
62         return;
63     }
64 
65     /*
66      * This is dup()ed just to avoid referencing an fd that might
67      * be already closed by the iochannel.
68      */
69     newfd = dup(fd);
70     if (newfd == -1) {
71         error_setg_errno(errp, errno, "Could not dup FD %d", fd);
72         object_unref(ioc);
73         return;
74     }
75     outgoing_args.fd = newfd;
76 
77     qio_channel_set_name(ioc, "migration-fd-outgoing");
78     migration_channel_connect(s, ioc, NULL, NULL);
79     object_unref(OBJECT(ioc));
80 }
81 
82 static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
83                                              GIOCondition condition,
84                                              gpointer opaque)
85 {
86     migration_channel_process_incoming(ioc);
87     object_unref(OBJECT(ioc));
88     return G_SOURCE_REMOVE;
89 }
90 
91 void fd_start_incoming_migration(const char *fdname, Error **errp)
92 {
93     QIOChannel *ioc;
94     int fd = monitor_fd_param(monitor_cur(), fdname, errp);
95     if (fd == -1) {
96         return;
97     }
98 
99     trace_migration_fd_incoming(fd);
100 
101     ioc = qio_channel_new_fd(fd, errp);
102     if (!ioc) {
103         close(fd);
104         return;
105     }
106 
107     if (migrate_multifd()) {
108         if (fd_is_socket(fd)) {
109             error_setg(errp,
110                        "Multifd migration to a socket FD is not supported");
111             object_unref(ioc);
112             return;
113         }
114 
115         file_create_incoming_channels(ioc, errp);
116     } else {
117         qio_channel_set_name(ioc, "migration-fd-incoming");
118         qio_channel_add_watch_full(ioc, G_IO_IN,
119                                    fd_accept_incoming_migration,
120                                    NULL, NULL,
121                                    g_main_context_get_thread_default());
122     }
123 }
124