]> git.proxmox.com Git - mirror_qemu.git/blame - migration/channel.c
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / migration / channel.c
CommitLineData
dd4339c5
JQ
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"
41d64227 15#include "tls.h"
6666c96a 16#include "migration.h"
77ef2dc1 17#include "qemu-file.h"
dd4339c5
JQ
18#include "trace.h"
19#include "qapi/error.h"
20#include "io/channel-tls.h"
b5eea99e
LS
21#include "io/channel-socket.h"
22#include "qemu/yank.h"
1a92d6d5 23#include "yank_functions.h"
dd4339c5 24
8e1a1931
JQ
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 */
54314711 33void migration_channel_process_incoming(QIOChannel *ioc)
dd4339c5 34{
54314711 35 MigrationState *s = migrate_get_current();
49ed0d24 36 Error *local_err = NULL;
54314711 37
dd4339c5
JQ
38 trace_migration_set_incoming_channel(
39 ioc, object_get_typename(OBJECT(ioc)));
40
85a8578e 41 if (migrate_channel_requires_tls_upgrade(ioc)) {
dd4339c5 42 migration_tls_channel_process_incoming(s, ioc, &local_err);
dd4339c5 43 } else {
18711405 44 migration_ioc_register_yank(ioc);
49ed0d24
FL
45 migration_ioc_process_incoming(ioc, &local_err);
46 }
47
48 if (local_err) {
49 error_report_err(local_err);
dd4339c5
JQ
50 }
51}
52
53
8e1a1931
JQ
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
688a3dcb 60 * @error: Error indicating failure to connect, free'd here
8e1a1931 61 */
dd4339c5
JQ
62void migration_channel_connect(MigrationState *s,
63 QIOChannel *ioc,
688a3dcb
DDAG
64 const char *hostname,
65 Error *error)
dd4339c5
JQ
66{
67 trace_migration_set_outgoing_channel(
688a3dcb 68 ioc, object_get_typename(OBJECT(ioc)), hostname, error);
dd4339c5 69
688a3dcb 70 if (!error) {
85a8578e 71 if (migrate_channel_requires_tls_upgrade(ioc)) {
688a3dcb 72 migration_tls_channel_connect(s, ioc, hostname, &error);
8b7bf2ba
DDAG
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 }
688a3dcb 82 } else {
77ef2dc1 83 QEMUFile *f = qemu_file_new_output(ioc);
dd4339c5 84
18711405 85 migration_ioc_register_yank(ioc);
7de2e856 86
62df066f 87 qemu_mutex_lock(&s->qemu_file_lock);
688a3dcb 88 s->to_dst_file = f;
62df066f 89 qemu_mutex_unlock(&s->qemu_file_lock);
688a3dcb 90 }
dd4339c5 91 }
688a3dcb
DDAG
92 migrate_fd_connect(s, error);
93 error_free(error);
dd4339c5 94}
6720c2b3 95
96
97/**
98 * @migration_channel_read_peek - Peek at migration channel, without
99 * actually removing it from channel buffer.
100 *
101 * @ioc: the channel object
102 * @buf: the memory region to read data into
103 * @buflen: the number of bytes to read in @buf
104 * @errp: pointer to a NULL-initialized error object
105 *
106 * Returns 0 if successful, returns -1 and sets @errp if fails.
107 */
108int migration_channel_read_peek(QIOChannel *ioc,
109 const char *buf,
110 const size_t buflen,
111 Error **errp)
112{
113 ssize_t len = 0;
114 struct iovec iov = { .iov_base = (char *)buf, .iov_len = buflen };
115
116 while (true) {
117 len = qio_channel_readv_full(ioc, &iov, 1, NULL, NULL,
118 QIO_CHANNEL_READ_FLAG_MSG_PEEK, errp);
119
4f8cf323
AH
120 if (len < 0 && len != QIO_CHANNEL_ERR_BLOCK) {
121 return -1;
122 }
123
124 if (len == 0) {
125 error_setg(errp, "Failed to peek at channel");
6720c2b3 126 return -1;
127 }
128
129 if (len == buflen) {
130 break;
131 }
132
133 /* 1ms sleep. */
134 if (qemu_in_coroutine()) {
135 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
136 } else {
137 g_usleep(1000);
138 }
139 }
140
141 return 0;
142}