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
fd_is_pipe(int fd)28 static bool fd_is_pipe(int fd)
29 {
30 struct stat statbuf;
31
32 if (fstat(fd, &statbuf) == -1) {
33 return false;
34 }
35
36 return S_ISFIFO(statbuf.st_mode);
37 }
38
migration_fd_valid(int fd)39 static bool migration_fd_valid(int fd)
40 {
41 if (fd_is_socket(fd)) {
42 return true;
43 }
44
45 if (fd_is_pipe(fd)) {
46 return true;
47 }
48
49 return false;
50 }
51
fd_start_outgoing_migration(MigrationState * s,const char * fdname,Error ** errp)52 void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
53 {
54 QIOChannel *ioc;
55 int fd = monitor_get_fd(monitor_cur(), fdname, errp);
56 if (fd == -1) {
57 return;
58 }
59
60 if (!migration_fd_valid(fd)) {
61 warn_report("fd: migration to a file is deprecated."
62 " Use file: instead.");
63 }
64
65 trace_migration_fd_outgoing(fd);
66 ioc = qio_channel_new_fd(fd, errp);
67 if (!ioc) {
68 close(fd);
69 return;
70 }
71
72 qio_channel_set_name(ioc, "migration-fd-outgoing");
73 migration_channel_connect(s, ioc, NULL, NULL);
74 object_unref(OBJECT(ioc));
75 }
76
fd_accept_incoming_migration(QIOChannel * ioc,GIOCondition condition,gpointer opaque)77 static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
78 GIOCondition condition,
79 gpointer opaque)
80 {
81 migration_channel_process_incoming(ioc);
82 object_unref(OBJECT(ioc));
83 return G_SOURCE_REMOVE;
84 }
85
fd_start_incoming_migration(const char * fdname,Error ** errp)86 void fd_start_incoming_migration(const char *fdname, Error **errp)
87 {
88 QIOChannel *ioc;
89 int fd = monitor_fd_param(monitor_cur(), fdname, errp);
90 if (fd == -1) {
91 return;
92 }
93
94 if (!migration_fd_valid(fd)) {
95 warn_report("fd: migration to a file is deprecated."
96 " Use file: instead.");
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 qio_channel_set_name(ioc, "migration-fd-incoming");
108 qio_channel_add_watch_full(ioc, G_IO_IN,
109 fd_accept_incoming_migration,
110 NULL, NULL,
111 g_main_context_get_thread_default());
112 }
113