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