xref: /qemu/migration/channel.c (revision d0fb9657)
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-channel.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 (object_dynamic_cast(OBJECT(ioc), TYPE_QIO_CHANNEL_SOCKET)) {
42         yank_register_function(MIGRATION_YANK_INSTANCE,
43                                migration_yank_iochannel,
44                                QIO_CHANNEL(ioc));
45     }
46 
47     if (s->parameters.tls_creds &&
48         *s->parameters.tls_creds &&
49         !object_dynamic_cast(OBJECT(ioc),
50                              TYPE_QIO_CHANNEL_TLS)) {
51         migration_tls_channel_process_incoming(s, ioc, &local_err);
52     } else {
53         migration_ioc_process_incoming(ioc, &local_err);
54     }
55 
56     if (local_err) {
57         error_report_err(local_err);
58     }
59 }
60 
61 
62 /**
63  * @migration_channel_connect - Create new outgoing migration channel
64  *
65  * @s: Current migration state
66  * @ioc: Channel to which we are connecting
67  * @hostname: Where we want to connect
68  * @error: Error indicating failure to connect, free'd here
69  */
70 void migration_channel_connect(MigrationState *s,
71                                QIOChannel *ioc,
72                                const char *hostname,
73                                Error *error)
74 {
75     trace_migration_set_outgoing_channel(
76         ioc, object_get_typename(OBJECT(ioc)), hostname, error);
77 
78     if (!error) {
79         if (object_dynamic_cast(OBJECT(ioc), TYPE_QIO_CHANNEL_SOCKET)) {
80             yank_register_function(MIGRATION_YANK_INSTANCE,
81                                    migration_yank_iochannel,
82                                    QIO_CHANNEL(ioc));
83         }
84 
85         if (s->parameters.tls_creds &&
86             *s->parameters.tls_creds &&
87             !object_dynamic_cast(OBJECT(ioc),
88                                  TYPE_QIO_CHANNEL_TLS)) {
89             migration_tls_channel_connect(s, ioc, hostname, &error);
90 
91             if (!error) {
92                 /* tls_channel_connect will call back to this
93                  * function after the TLS handshake,
94                  * so we mustn't call migrate_fd_connect until then
95                  */
96 
97                 return;
98             }
99         } else {
100             QEMUFile *f = qemu_fopen_channel_output(ioc);
101 
102             qemu_mutex_lock(&s->qemu_file_lock);
103             s->to_dst_file = f;
104             qemu_mutex_unlock(&s->qemu_file_lock);
105         }
106     }
107     migrate_fd_connect(s, error);
108     g_free(s->hostname);
109     error_free(error);
110 }
111