xref: /openbmc/qemu/migration/fd.c (revision dc8b6dd98458ffc6d93b110ab7406301e911c6db)
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 "qemu-common.h"
20 #include "channel.h"
21 #include "migration/migration.h"
22 #include "monitor/monitor.h"
23 #include "io/channel-util.h"
24 #include "trace.h"
25 
26 
27 void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
28 {
29     QIOChannel *ioc;
30     int fd = monitor_get_fd(cur_mon, fdname, errp);
31     if (fd == -1) {
32         return;
33     }
34 
35     trace_migration_fd_outgoing(fd);
36     ioc = qio_channel_new_fd(fd, errp);
37     if (!ioc) {
38         close(fd);
39         return;
40     }
41 
42     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-fd-outgoing");
43     migration_channel_connect(s, ioc, NULL);
44     object_unref(OBJECT(ioc));
45 }
46 
47 static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
48                                              GIOCondition condition,
49                                              gpointer opaque)
50 {
51     migration_channel_process_incoming(migrate_get_current(), ioc);
52     object_unref(OBJECT(ioc));
53     return FALSE; /* unregister */
54 }
55 
56 void fd_start_incoming_migration(const char *infd, Error **errp)
57 {
58     QIOChannel *ioc;
59     int fd;
60 
61     fd = strtol(infd, NULL, 0);
62     trace_migration_fd_incoming(fd);
63 
64     ioc = qio_channel_new_fd(fd, errp);
65     if (!ioc) {
66         close(fd);
67         return;
68     }
69 
70     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-fd-incoming");
71     qio_channel_add_watch(ioc,
72                           G_IO_IN,
73                           fd_accept_incoming_migration,
74                           NULL,
75                           NULL);
76 }
77