]> git.proxmox.com Git - mirror_qemu.git/blame - io/channel.c
io: add methods to set I/O handlers on AioContext
[mirror_qemu.git] / io / channel.c
CommitLineData
666a3af9
DB
1/*
2 * QEMU I/O channels
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
cae9fc56 21#include "qemu/osdep.h"
666a3af9 22#include "io/channel.h"
da34e65c 23#include "qapi/error.h"
666a3af9
DB
24#include "qemu/coroutine.h"
25
26bool qio_channel_has_feature(QIOChannel *ioc,
27 QIOChannelFeature feature)
28{
29 return ioc->features & (1 << feature);
30}
31
32
d8d3c7cc
FF
33void qio_channel_set_feature(QIOChannel *ioc,
34 QIOChannelFeature feature)
35{
36 ioc->features |= (1 << feature);
37}
38
39
20f4aa26
DB
40void qio_channel_set_name(QIOChannel *ioc,
41 const char *name)
42{
43 g_free(ioc->name);
44 ioc->name = g_strdup(name);
45}
46
47
666a3af9
DB
48ssize_t qio_channel_readv_full(QIOChannel *ioc,
49 const struct iovec *iov,
50 size_t niov,
51 int **fds,
52 size_t *nfds,
53 Error **errp)
54{
55 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
56
57 if ((fds || nfds) &&
e413ae0c 58 !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
666a3af9
DB
59 error_setg_errno(errp, EINVAL,
60 "Channel does not support file descriptor passing");
61 return -1;
62 }
63
64 return klass->io_readv(ioc, iov, niov, fds, nfds, errp);
65}
66
67
68ssize_t qio_channel_writev_full(QIOChannel *ioc,
69 const struct iovec *iov,
70 size_t niov,
71 int *fds,
72 size_t nfds,
73 Error **errp)
74{
75 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
76
77 if ((fds || nfds) &&
e413ae0c 78 !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
666a3af9
DB
79 error_setg_errno(errp, EINVAL,
80 "Channel does not support file descriptor passing");
81 return -1;
82 }
83
84 return klass->io_writev(ioc, iov, niov, fds, nfds, errp);
85}
86
87
88ssize_t qio_channel_readv(QIOChannel *ioc,
89 const struct iovec *iov,
90 size_t niov,
91 Error **errp)
92{
93 return qio_channel_readv_full(ioc, iov, niov, NULL, NULL, errp);
94}
95
96
97ssize_t qio_channel_writev(QIOChannel *ioc,
98 const struct iovec *iov,
99 size_t niov,
100 Error **errp)
101{
102 return qio_channel_writev_full(ioc, iov, niov, NULL, 0, errp);
103}
104
105
106ssize_t qio_channel_read(QIOChannel *ioc,
107 char *buf,
108 size_t buflen,
109 Error **errp)
110{
111 struct iovec iov = { .iov_base = buf, .iov_len = buflen };
112 return qio_channel_readv_full(ioc, &iov, 1, NULL, NULL, errp);
113}
114
115
116ssize_t qio_channel_write(QIOChannel *ioc,
117 const char *buf,
118 size_t buflen,
119 Error **errp)
120{
121 struct iovec iov = { .iov_base = (char *)buf, .iov_len = buflen };
122 return qio_channel_writev_full(ioc, &iov, 1, NULL, 0, errp);
123}
124
125
126int qio_channel_set_blocking(QIOChannel *ioc,
127 bool enabled,
128 Error **errp)
129{
130 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
131 return klass->io_set_blocking(ioc, enabled, errp);
132}
133
134
135int qio_channel_close(QIOChannel *ioc,
136 Error **errp)
137{
138 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
139 return klass->io_close(ioc, errp);
140}
141
142
143GSource *qio_channel_create_watch(QIOChannel *ioc,
144 GIOCondition condition)
145{
146 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
20f4aa26
DB
147 GSource *ret = klass->io_create_watch(ioc, condition);
148
149 if (ioc->name) {
150 g_source_set_name(ret, ioc->name);
151 }
152
153 return ret;
666a3af9
DB
154}
155
156
bf88c124
PB
157void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
158 AioContext *ctx,
159 IOHandler *io_read,
160 IOHandler *io_write,
161 void *opaque)
162{
163 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
164
165 klass->io_set_aio_fd_handler(ioc, ctx, io_read, io_write, opaque);
166}
167
666a3af9
DB
168guint qio_channel_add_watch(QIOChannel *ioc,
169 GIOCondition condition,
170 QIOChannelFunc func,
171 gpointer user_data,
172 GDestroyNotify notify)
173{
174 GSource *source;
175 guint id;
176
177 source = qio_channel_create_watch(ioc, condition);
178
179 g_source_set_callback(source, (GSourceFunc)func, user_data, notify);
180
181 id = g_source_attach(source, NULL);
182 g_source_unref(source);
183
184 return id;
185}
186
187
188int qio_channel_shutdown(QIOChannel *ioc,
189 QIOChannelShutdown how,
190 Error **errp)
191{
192 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
193
194 if (!klass->io_shutdown) {
195 error_setg(errp, "Data path shutdown not supported");
196 return -1;
197 }
198
199 return klass->io_shutdown(ioc, how, errp);
200}
201
202
203void qio_channel_set_delay(QIOChannel *ioc,
204 bool enabled)
205{
206 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
207
208 if (klass->io_set_delay) {
209 klass->io_set_delay(ioc, enabled);
210 }
211}
212
213
214void qio_channel_set_cork(QIOChannel *ioc,
215 bool enabled)
216{
217 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
218
219 if (klass->io_set_cork) {
220 klass->io_set_cork(ioc, enabled);
221 }
222}
223
224
225off_t qio_channel_io_seek(QIOChannel *ioc,
226 off_t offset,
227 int whence,
228 Error **errp)
229{
230 QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
231
232 if (!klass->io_seek) {
233 error_setg(errp, "Channel does not support random access");
234 return -1;
235 }
236
237 return klass->io_seek(ioc, offset, whence, errp);
238}
239
240
241typedef struct QIOChannelYieldData QIOChannelYieldData;
242struct QIOChannelYieldData {
243 QIOChannel *ioc;
244 Coroutine *co;
245};
246
247
248static gboolean qio_channel_yield_enter(QIOChannel *ioc,
249 GIOCondition condition,
250 gpointer opaque)
251{
252 QIOChannelYieldData *data = opaque;
0b8b8753 253 qemu_coroutine_enter(data->co);
666a3af9
DB
254 return FALSE;
255}
256
257
258void coroutine_fn qio_channel_yield(QIOChannel *ioc,
259 GIOCondition condition)
260{
261 QIOChannelYieldData data;
262
263 assert(qemu_in_coroutine());
264 data.ioc = ioc;
265 data.co = qemu_coroutine_self();
266 qio_channel_add_watch(ioc,
267 condition,
268 qio_channel_yield_enter,
269 &data,
270 NULL);
271 qemu_coroutine_yield();
272}
273
274
275static gboolean qio_channel_wait_complete(QIOChannel *ioc,
276 GIOCondition condition,
277 gpointer opaque)
278{
279 GMainLoop *loop = opaque;
280
281 g_main_loop_quit(loop);
282 return FALSE;
283}
284
285
286void qio_channel_wait(QIOChannel *ioc,
287 GIOCondition condition)
288{
289 GMainContext *ctxt = g_main_context_new();
290 GMainLoop *loop = g_main_loop_new(ctxt, TRUE);
291 GSource *source;
292
293 source = qio_channel_create_watch(ioc, condition);
294
295 g_source_set_callback(source,
296 (GSourceFunc)qio_channel_wait_complete,
297 loop,
298 NULL);
299
300 g_source_attach(source, ctxt);
301
302 g_main_loop_run(loop);
303
304 g_source_unref(source);
305 g_main_loop_unref(loop);
306 g_main_context_unref(ctxt);
307}
308
309
a5897205
PB
310static void qio_channel_finalize(Object *obj)
311{
312 QIOChannel *ioc = QIO_CHANNEL(obj);
313
20f4aa26
DB
314 g_free(ioc->name);
315
316#ifdef _WIN32
a5897205
PB
317 if (ioc->event) {
318 CloseHandle(ioc->event);
319 }
a5897205 320#endif
20f4aa26 321}
a5897205 322
666a3af9
DB
323static const TypeInfo qio_channel_info = {
324 .parent = TYPE_OBJECT,
325 .name = TYPE_QIO_CHANNEL,
326 .instance_size = sizeof(QIOChannel),
a5897205 327 .instance_finalize = qio_channel_finalize,
666a3af9
DB
328 .abstract = true,
329 .class_size = sizeof(QIOChannelClass),
330};
331
332
333static void qio_channel_register_types(void)
334{
335 type_register_static(&qio_channel_info);
336}
337
338
339type_init(qio_channel_register_types);