xref: /openbmc/qemu/migration/channel.c (revision 954a6c4f)
1 /*
2  * QEMU live migration channel operations
3  *
4  * Copyright Red Hat, Inc. 2016
5  *
6  * Authors:
7  *  Daniel P. Berrange <berrange@redhat.com>
8  *
9  * Contributions after 2012-01-13 are licensed under the terms of the
10  * GNU GPL, version 2 or (at your option) any later version.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "channel.h"
15 #include "tls.h"
16 #include "migration.h"
17 #include "qemu-file.h"
18 #include "trace.h"
19 #include "qapi/error.h"
20 #include "io/channel-tls.h"
21 #include "io/channel-socket.h"
22 #include "qemu/yank.h"
23 #include "yank_functions.h"
24 
25 /**
26  * @migration_channel_process_incoming - Create new incoming migration channel
27  *
28  * Notice that TLS is special.  For it we listen in a listener socket,
29  * and then create a new client socket from the TLS library.
30  *
31  * @ioc: Channel to which we are connecting
32  */
33 void migration_channel_process_incoming(QIOChannel *ioc)
34 {
35     MigrationState *s = migrate_get_current();
36     Error *local_err = NULL;
37 
38     trace_migration_set_incoming_channel(
39         ioc, object_get_typename(OBJECT(ioc)));
40 
41     if (migrate_channel_requires_tls_upgrade(ioc)) {
42         migration_tls_channel_process_incoming(s, ioc, &local_err);
43     } else {
44         migration_ioc_register_yank(ioc);
45         migration_ioc_process_incoming(ioc, &local_err);
46     }
47 
48     if (local_err) {
49         error_report_err(local_err);
50     }
51 }
52 
53 
54 /**
55  * @migration_channel_connect - Create new outgoing migration channel
56  *
57  * @s: Current migration state
58  * @ioc: Channel to which we are connecting
59  * @hostname: Where we want to connect
60  * @error: Error indicating failure to connect, free'd here
61  */
62 void migration_channel_connect(MigrationState *s,
63                                QIOChannel *ioc,
64                                const char *hostname,
65                                Error *error)
66 {
67     trace_migration_set_outgoing_channel(
68         ioc, object_get_typename(OBJECT(ioc)), hostname, error);
69 
70     if (!error) {
71         if (migrate_channel_requires_tls_upgrade(ioc)) {
72             migration_tls_channel_connect(s, ioc, hostname, &error);
73 
74             if (!error) {
75                 /* tls_channel_connect will call back to this
76                  * function after the TLS handshake,
77                  * so we mustn't call migrate_fd_connect until then
78                  */
79 
80                 return;
81             }
82         } else {
83             QEMUFile *f = qemu_file_new_output(ioc);
84 
85             migration_ioc_register_yank(ioc);
86 
87             qemu_mutex_lock(&s->qemu_file_lock);
88             s->to_dst_file = f;
89             qemu_mutex_unlock(&s->qemu_file_lock);
90         }
91     }
92     migrate_fd_connect(s, error);
93     error_free(error);
94 }
95