1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "qemu-file-channel.h"
27#include "qemu-file.h"
28#include "io/channel-socket.h"
29#include "qemu/iov.h"
30#include "qemu/yank.h"
31#include "yank_functions.h"
32
33
34static ssize_t channel_writev_buffer(void *opaque,
35 struct iovec *iov,
36 int iovcnt,
37 int64_t pos,
38 Error **errp)
39{
40 QIOChannel *ioc = QIO_CHANNEL(opaque);
41 ssize_t done = 0;
42 struct iovec *local_iov = g_new(struct iovec, iovcnt);
43 struct iovec *local_iov_head = local_iov;
44 unsigned int nlocal_iov = iovcnt;
45
46 nlocal_iov = iov_copy(local_iov, nlocal_iov,
47 iov, iovcnt,
48 0, iov_size(iov, iovcnt));
49
50 while (nlocal_iov > 0) {
51 ssize_t len;
52 len = qio_channel_writev(ioc, local_iov, nlocal_iov, errp);
53 if (len == QIO_CHANNEL_ERR_BLOCK) {
54 if (qemu_in_coroutine()) {
55 qio_channel_yield(ioc, G_IO_OUT);
56 } else {
57 qio_channel_wait(ioc, G_IO_OUT);
58 }
59 continue;
60 }
61 if (len < 0) {
62 done = -EIO;
63 goto cleanup;
64 }
65
66 iov_discard_front(&local_iov, &nlocal_iov, len);
67 done += len;
68 }
69
70 cleanup:
71 g_free(local_iov_head);
72 return done;
73}
74
75
76static ssize_t channel_get_buffer(void *opaque,
77 uint8_t *buf,
78 int64_t pos,
79 size_t size,
80 Error **errp)
81{
82 QIOChannel *ioc = QIO_CHANNEL(opaque);
83 ssize_t ret;
84
85 do {
86 ret = qio_channel_read(ioc, (char *)buf, size, errp);
87 if (ret < 0) {
88 if (ret == QIO_CHANNEL_ERR_BLOCK) {
89 if (qemu_in_coroutine()) {
90 qio_channel_yield(ioc, G_IO_IN);
91 } else {
92 qio_channel_wait(ioc, G_IO_IN);
93 }
94 } else {
95 return -EIO;
96 }
97 }
98 } while (ret == QIO_CHANNEL_ERR_BLOCK);
99
100 return ret;
101}
102
103
104static int channel_close(void *opaque, Error **errp)
105{
106 int ret;
107 QIOChannel *ioc = QIO_CHANNEL(opaque);
108 ret = qio_channel_close(ioc, errp);
109 if (object_dynamic_cast(OBJECT(ioc), TYPE_QIO_CHANNEL_SOCKET)
110 && OBJECT(ioc)->ref == 1) {
111 yank_unregister_function(MIGRATION_YANK_INSTANCE,
112 migration_yank_iochannel,
113 QIO_CHANNEL(ioc));
114 }
115 object_unref(OBJECT(ioc));
116 return ret;
117}
118
119
120static int channel_shutdown(void *opaque,
121 bool rd,
122 bool wr,
123 Error **errp)
124{
125 QIOChannel *ioc = QIO_CHANNEL(opaque);
126
127 if (qio_channel_has_feature(ioc,
128 QIO_CHANNEL_FEATURE_SHUTDOWN)) {
129 QIOChannelShutdown mode;
130 if (rd && wr) {
131 mode = QIO_CHANNEL_SHUTDOWN_BOTH;
132 } else if (rd) {
133 mode = QIO_CHANNEL_SHUTDOWN_READ;
134 } else {
135 mode = QIO_CHANNEL_SHUTDOWN_WRITE;
136 }
137 if (qio_channel_shutdown(ioc, mode, errp) < 0) {
138 return -EIO;
139 }
140 }
141 return 0;
142}
143
144
145static int channel_set_blocking(void *opaque,
146 bool enabled,
147 Error **errp)
148{
149 QIOChannel *ioc = QIO_CHANNEL(opaque);
150
151 if (qio_channel_set_blocking(ioc, enabled, errp) < 0) {
152 return -1;
153 }
154 return 0;
155}
156
157static QEMUFile *channel_get_input_return_path(void *opaque)
158{
159 QIOChannel *ioc = QIO_CHANNEL(opaque);
160
161 return qemu_fopen_channel_output(ioc);
162}
163
164static QEMUFile *channel_get_output_return_path(void *opaque)
165{
166 QIOChannel *ioc = QIO_CHANNEL(opaque);
167
168 return qemu_fopen_channel_input(ioc);
169}
170
171static const QEMUFileOps channel_input_ops = {
172 .get_buffer = channel_get_buffer,
173 .close = channel_close,
174 .shut_down = channel_shutdown,
175 .set_blocking = channel_set_blocking,
176 .get_return_path = channel_get_input_return_path,
177};
178
179
180static const QEMUFileOps channel_output_ops = {
181 .writev_buffer = channel_writev_buffer,
182 .close = channel_close,
183 .shut_down = channel_shutdown,
184 .set_blocking = channel_set_blocking,
185 .get_return_path = channel_get_output_return_path,
186};
187
188
189QEMUFile *qemu_fopen_channel_input(QIOChannel *ioc)
190{
191 object_ref(OBJECT(ioc));
192 return qemu_fopen_ops(ioc, &channel_input_ops);
193}
194
195QEMUFile *qemu_fopen_channel_output(QIOChannel *ioc)
196{
197 object_ref(OBJECT(ioc));
198 return qemu_fopen_ops(ioc, &channel_output_ops);
199}
200