xref: /qemu/migration/fd.c (revision d4fdb05b)
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 "channel.h"
19 #include "fd.h"
20 #include "file.h"
21 #include "migration.h"
22 #include "monitor/monitor.h"
23 #include "qemu/error-report.h"
24 #include "qemu/sockets.h"
25 #include "io/channel-util.h"
26 #include "trace.h"
27 
28 
29 void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
30 {
31     QIOChannel *ioc;
32     int fd = monitor_get_fd(monitor_cur(), fdname, errp);
33     if (fd == -1) {
34         return;
35     }
36 
37     if (!fd_is_socket(fd)) {
38         warn_report("fd: migration to a file is deprecated."
39                     " Use file: instead.");
40     }
41 
42     trace_migration_fd_outgoing(fd);
43     ioc = qio_channel_new_fd(fd, errp);
44     if (!ioc) {
45         close(fd);
46         return;
47     }
48 
49     qio_channel_set_name(ioc, "migration-fd-outgoing");
50     migration_channel_connect(s, ioc, NULL, NULL);
51     object_unref(OBJECT(ioc));
52 }
53 
54 static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
55                                              GIOCondition condition,
56                                              gpointer opaque)
57 {
58     migration_channel_process_incoming(ioc);
59     object_unref(OBJECT(ioc));
60     return G_SOURCE_REMOVE;
61 }
62 
63 void fd_start_incoming_migration(const char *fdname, Error **errp)
64 {
65     QIOChannel *ioc;
66     int fd = monitor_fd_param(monitor_cur(), fdname, errp);
67     if (fd == -1) {
68         return;
69     }
70 
71     if (!fd_is_socket(fd)) {
72         warn_report("fd: migration to a file is deprecated."
73                     " Use file: instead.");
74     }
75 
76     trace_migration_fd_incoming(fd);
77 
78     ioc = qio_channel_new_fd(fd, errp);
79     if (!ioc) {
80         close(fd);
81         return;
82     }
83 
84     qio_channel_set_name(ioc, "migration-fd-incoming");
85     qio_channel_add_watch_full(ioc, G_IO_IN,
86                                fd_accept_incoming_migration,
87                                NULL, NULL,
88                                g_main_context_get_thread_default());
89 }
90