blob: dc991c90510cc841238b2ca33009760371039181 [file] [log] [blame]
Daniel P. Berrangea9cfeb32016-04-27 11:04:58 +01001/*
2 * QEMUFile backend for QIOChannel objects
3 *
4 * Copyright (c) 2015-2016 Red Hat, Inc
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
25#include "qemu/osdep.h"
Juan Quintela40014d82017-04-17 19:34:36 +020026#include "qemu-file-channel.h"
Daniel P. Berrangea9cfeb32016-04-27 11:04:58 +010027#include "migration/qemu-file.h"
28#include "io/channel-socket.h"
29#include "qemu/iov.h"
30
31
32static ssize_t channel_writev_buffer(void *opaque,
33 struct iovec *iov,
34 int iovcnt,
35 int64_t pos)
36{
37 QIOChannel *ioc = QIO_CHANNEL(opaque);
38 ssize_t done = 0;
39 struct iovec *local_iov = g_new(struct iovec, iovcnt);
40 struct iovec *local_iov_head = local_iov;
41 unsigned int nlocal_iov = iovcnt;
42
43 nlocal_iov = iov_copy(local_iov, nlocal_iov,
44 iov, iovcnt,
45 0, iov_size(iov, iovcnt));
46
47 while (nlocal_iov > 0) {
48 ssize_t len;
49 len = qio_channel_writev(ioc, local_iov, nlocal_iov, NULL);
50 if (len == QIO_CHANNEL_ERR_BLOCK) {
51 qio_channel_wait(ioc, G_IO_OUT);
52 continue;
53 }
54 if (len < 0) {
55 /* XXX handle Error objects */
56 done = -EIO;
57 goto cleanup;
58 }
59
60 iov_discard_front(&local_iov, &nlocal_iov, len);
61 done += len;
62 }
63
64 cleanup:
65 g_free(local_iov_head);
66 return done;
67}
68
69
70static ssize_t channel_get_buffer(void *opaque,
71 uint8_t *buf,
72 int64_t pos,
73 size_t size)
74{
75 QIOChannel *ioc = QIO_CHANNEL(opaque);
76 ssize_t ret;
77
78 do {
79 ret = qio_channel_read(ioc, (char *)buf, size, NULL);
80 if (ret < 0) {
81 if (ret == QIO_CHANNEL_ERR_BLOCK) {
82 qio_channel_yield(ioc, G_IO_IN);
83 } else {
84 /* XXX handle Error * object */
85 return -EIO;
86 }
87 }
88 } while (ret == QIO_CHANNEL_ERR_BLOCK);
89
90 return ret;
91}
92
93
94static int channel_close(void *opaque)
95{
96 QIOChannel *ioc = QIO_CHANNEL(opaque);
97 qio_channel_close(ioc, NULL);
98 object_unref(OBJECT(ioc));
99 return 0;
100}
101
102
103static int channel_shutdown(void *opaque,
104 bool rd,
105 bool wr)
106{
107 QIOChannel *ioc = QIO_CHANNEL(opaque);
108
109 if (qio_channel_has_feature(ioc,
110 QIO_CHANNEL_FEATURE_SHUTDOWN)) {
111 QIOChannelShutdown mode;
112 if (rd && wr) {
113 mode = QIO_CHANNEL_SHUTDOWN_BOTH;
114 } else if (rd) {
115 mode = QIO_CHANNEL_SHUTDOWN_READ;
116 } else {
117 mode = QIO_CHANNEL_SHUTDOWN_WRITE;
118 }
119 if (qio_channel_shutdown(ioc, mode, NULL) < 0) {
120 /* XXX handler Error * object */
121 return -EIO;
122 }
123 }
124 return 0;
125}
126
127
128static int channel_set_blocking(void *opaque,
129 bool enabled)
130{
131 QIOChannel *ioc = QIO_CHANNEL(opaque);
132
133 if (qio_channel_set_blocking(ioc, enabled, NULL) < 0) {
134 return -1;
135 }
136 return 0;
137}
138
139static QEMUFile *channel_get_input_return_path(void *opaque)
140{
141 QIOChannel *ioc = QIO_CHANNEL(opaque);
142
143 return qemu_fopen_channel_output(ioc);
144}
145
146static QEMUFile *channel_get_output_return_path(void *opaque)
147{
148 QIOChannel *ioc = QIO_CHANNEL(opaque);
149
150 return qemu_fopen_channel_input(ioc);
151}
152
153static const QEMUFileOps channel_input_ops = {
154 .get_buffer = channel_get_buffer,
155 .close = channel_close,
156 .shut_down = channel_shutdown,
157 .set_blocking = channel_set_blocking,
158 .get_return_path = channel_get_input_return_path,
159};
160
161
162static const QEMUFileOps channel_output_ops = {
163 .writev_buffer = channel_writev_buffer,
164 .close = channel_close,
165 .shut_down = channel_shutdown,
166 .set_blocking = channel_set_blocking,
167 .get_return_path = channel_get_output_return_path,
168};
169
170
171QEMUFile *qemu_fopen_channel_input(QIOChannel *ioc)
172{
173 object_ref(OBJECT(ioc));
174 return qemu_fopen_ops(ioc, &channel_input_ops);
175}
176
177QEMUFile *qemu_fopen_channel_output(QIOChannel *ioc)
178{
179 object_ref(OBJECT(ioc));
180 return qemu_fopen_ops(ioc, &channel_output_ops);
181}