2 * QEMUFile backend for QIOChannel objects
4 * Copyright (c) 2015-2016 Red Hat, Inc
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu-file-channel.h"
27 #include "qemu-file.h"
28 #include "io/channel-socket.h"
30 #include "qemu/yank.h"
33 static ssize_t
channel_writev_buffer(void *opaque
,
39 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
41 struct iovec
*local_iov
= g_new(struct iovec
, iovcnt
);
42 struct iovec
*local_iov_head
= local_iov
;
43 unsigned int nlocal_iov
= iovcnt
;
45 nlocal_iov
= iov_copy(local_iov
, nlocal_iov
,
47 0, iov_size(iov
, iovcnt
));
49 while (nlocal_iov
> 0) {
51 len
= qio_channel_writev(ioc
, local_iov
, nlocal_iov
, errp
);
52 if (len
== QIO_CHANNEL_ERR_BLOCK
) {
53 if (qemu_in_coroutine()) {
54 qio_channel_yield(ioc
, G_IO_OUT
);
56 qio_channel_wait(ioc
, G_IO_OUT
);
65 iov_discard_front(&local_iov
, &nlocal_iov
, len
);
70 g_free(local_iov_head
);
75 static ssize_t
channel_get_buffer(void *opaque
,
81 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
85 ret
= qio_channel_read(ioc
, (char *)buf
, size
, errp
);
87 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
88 if (qemu_in_coroutine()) {
89 qio_channel_yield(ioc
, G_IO_IN
);
91 qio_channel_wait(ioc
, G_IO_IN
);
97 } while (ret
== QIO_CHANNEL_ERR_BLOCK
);
103 static int channel_close(void *opaque
, Error
**errp
)
106 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
107 ret
= qio_channel_close(ioc
, errp
);
108 if (object_dynamic_cast(OBJECT(ioc
), TYPE_QIO_CHANNEL_SOCKET
)
109 && OBJECT(ioc
)->ref
== 1) {
110 yank_unregister_function(MIGRATION_YANK_INSTANCE
,
111 yank_generic_iochannel
,
114 object_unref(OBJECT(ioc
));
119 static int channel_shutdown(void *opaque
,
124 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
126 if (qio_channel_has_feature(ioc
,
127 QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
128 QIOChannelShutdown mode
;
130 mode
= QIO_CHANNEL_SHUTDOWN_BOTH
;
132 mode
= QIO_CHANNEL_SHUTDOWN_READ
;
134 mode
= QIO_CHANNEL_SHUTDOWN_WRITE
;
136 if (qio_channel_shutdown(ioc
, mode
, errp
) < 0) {
144 static int channel_set_blocking(void *opaque
,
148 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
150 if (qio_channel_set_blocking(ioc
, enabled
, errp
) < 0) {
156 static QEMUFile
*channel_get_input_return_path(void *opaque
)
158 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
160 return qemu_fopen_channel_output(ioc
);
163 static QEMUFile
*channel_get_output_return_path(void *opaque
)
165 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
167 return qemu_fopen_channel_input(ioc
);
170 static const QEMUFileOps channel_input_ops
= {
171 .get_buffer
= channel_get_buffer
,
172 .close
= channel_close
,
173 .shut_down
= channel_shutdown
,
174 .set_blocking
= channel_set_blocking
,
175 .get_return_path
= channel_get_input_return_path
,
179 static const QEMUFileOps channel_output_ops
= {
180 .writev_buffer
= channel_writev_buffer
,
181 .close
= channel_close
,
182 .shut_down
= channel_shutdown
,
183 .set_blocking
= channel_set_blocking
,
184 .get_return_path
= channel_get_output_return_path
,
188 QEMUFile
*qemu_fopen_channel_input(QIOChannel
*ioc
)
190 object_ref(OBJECT(ioc
));
191 return qemu_fopen_ops(ioc
, &channel_input_ops
);
194 QEMUFile
*qemu_fopen_channel_output(QIOChannel
*ioc
)
196 object_ref(OBJECT(ioc
));
197 return qemu_fopen_ops(ioc
, &channel_output_ops
);