]> git.proxmox.com Git - mirror_qemu.git/blame - include/io/channel.h
Merge remote-tracking branch 'mjt/tags/trivial-patches-fetch' into staging
[mirror_qemu.git] / include / io / channel.h
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
2a6a4076
MA
21#ifndef QIO_CHANNEL_H
22#define QIO_CHANNEL_H
666a3af9
DB
23
24#include "qemu-common.h"
666a3af9 25#include "qom/object.h"
c4c497d2 26#include "qemu/coroutine.h"
bf88c124 27#include "block/aio.h"
666a3af9
DB
28
29#define TYPE_QIO_CHANNEL "qio-channel"
30#define QIO_CHANNEL(obj) \
31 OBJECT_CHECK(QIOChannel, (obj), TYPE_QIO_CHANNEL)
32#define QIO_CHANNEL_CLASS(klass) \
33 OBJECT_CLASS_CHECK(QIOChannelClass, klass, TYPE_QIO_CHANNEL)
34#define QIO_CHANNEL_GET_CLASS(obj) \
35 OBJECT_GET_CLASS(QIOChannelClass, obj, TYPE_QIO_CHANNEL)
36
37typedef struct QIOChannel QIOChannel;
38typedef struct QIOChannelClass QIOChannelClass;
39
40#define QIO_CHANNEL_ERR_BLOCK -2
41
42typedef enum QIOChannelFeature QIOChannelFeature;
43
44enum QIOChannelFeature {
8fbf6612
FF
45 QIO_CHANNEL_FEATURE_FD_PASS,
46 QIO_CHANNEL_FEATURE_SHUTDOWN,
47 QIO_CHANNEL_FEATURE_LISTEN,
666a3af9
DB
48};
49
50
51typedef enum QIOChannelShutdown QIOChannelShutdown;
52
53enum QIOChannelShutdown {
54 QIO_CHANNEL_SHUTDOWN_BOTH,
55 QIO_CHANNEL_SHUTDOWN_READ,
56 QIO_CHANNEL_SHUTDOWN_WRITE,
57};
58
59typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc,
60 GIOCondition condition,
61 gpointer data);
62
63/**
64 * QIOChannel:
65 *
66 * The QIOChannel defines the core API for a generic I/O channel
67 * class hierarchy. It is inspired by GIOChannel, but has the
68 * following differences
69 *
70 * - Use QOM to properly support arbitrary subclassing
71 * - Support use of iovecs for efficient I/O with multiple blocks
72 * - None of the character set translation, binary data exclusively
73 * - Direct support for QEMU Error object reporting
74 * - File descriptor passing
75 *
76 * This base class is abstract so cannot be instantiated. There
77 * will be subclasses for dealing with sockets, files, and higher
78 * level protocols such as TLS, WebSocket, etc.
79 */
80
81struct QIOChannel {
82 Object parent;
83 unsigned int features; /* bitmask of QIOChannelFeatures */
20f4aa26 84 char *name;
c4c497d2
PB
85 AioContext *ctx;
86 Coroutine *read_coroutine;
87 Coroutine *write_coroutine;
a5897205
PB
88#ifdef _WIN32
89 HANDLE event; /* For use with GSource on Win32 */
90#endif
666a3af9
DB
91};
92
93/**
94 * QIOChannelClass:
95 *
96 * This class defines the contract that all subclasses
97 * must follow to provide specific channel implementations.
98 * The first five callbacks are mandatory to support, others
99 * provide additional optional features.
100 *
101 * Consult the corresponding public API docs for a description
102 * of the semantics of each callback
103 */
104struct QIOChannelClass {
105 ObjectClass parent;
106
107 /* Mandatory callbacks */
108 ssize_t (*io_writev)(QIOChannel *ioc,
109 const struct iovec *iov,
110 size_t niov,
111 int *fds,
112 size_t nfds,
113 Error **errp);
114 ssize_t (*io_readv)(QIOChannel *ioc,
115 const struct iovec *iov,
116 size_t niov,
117 int **fds,
118 size_t *nfds,
119 Error **errp);
120 int (*io_close)(QIOChannel *ioc,
121 Error **errp);
122 GSource * (*io_create_watch)(QIOChannel *ioc,
123 GIOCondition condition);
124 int (*io_set_blocking)(QIOChannel *ioc,
125 bool enabled,
126 Error **errp);
127
128 /* Optional callbacks */
129 int (*io_shutdown)(QIOChannel *ioc,
130 QIOChannelShutdown how,
131 Error **errp);
132 void (*io_set_cork)(QIOChannel *ioc,
133 bool enabled);
134 void (*io_set_delay)(QIOChannel *ioc,
135 bool enabled);
136 off_t (*io_seek)(QIOChannel *ioc,
137 off_t offset,
138 int whence,
139 Error **errp);
bf88c124
PB
140 void (*io_set_aio_fd_handler)(QIOChannel *ioc,
141 AioContext *ctx,
142 IOHandler *io_read,
143 IOHandler *io_write,
144 void *opaque);
666a3af9
DB
145};
146
147/* General I/O handling functions */
148
149/**
150 * qio_channel_has_feature:
151 * @ioc: the channel object
152 * @feature: the feature to check support of
153 *
154 * Determine whether the channel implementation supports
155 * the optional feature named in @feature.
156 *
157 * Returns: true if supported, false otherwise.
158 */
159bool qio_channel_has_feature(QIOChannel *ioc,
160 QIOChannelFeature feature);
161
d8d3c7cc
FF
162/**
163 * qio_channel_set_feature:
164 * @ioc: the channel object
165 * @feature: the feature to set support for
166 *
167 * Add channel support for the feature named in @feature.
168 */
169void qio_channel_set_feature(QIOChannel *ioc,
170 QIOChannelFeature feature);
171
20f4aa26
DB
172/**
173 * qio_channel_set_name:
174 * @ioc: the channel object
175 * @name: the name of the channel
176 *
177 * Sets the name of the channel, which serves as an aid
178 * to debugging. The name is used when creating GSource
179 * watches for this channel.
180 */
181void qio_channel_set_name(QIOChannel *ioc,
182 const char *name);
183
666a3af9
DB
184/**
185 * qio_channel_readv_full:
186 * @ioc: the channel object
187 * @iov: the array of memory regions to read data into
188 * @niov: the length of the @iov array
189 * @fds: pointer to an array that will received file handles
190 * @nfds: pointer filled with number of elements in @fds on return
821791b5 191 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
192 *
193 * Read data from the IO channel, storing it in the
194 * memory regions referenced by @iov. Each element
195 * in the @iov will be fully populated with data
196 * before the next one is used. The @niov parameter
197 * specifies the total number of elements in @iov.
198 *
199 * It is not required for all @iov to be filled with
200 * data. If the channel is in blocking mode, at least
201 * one byte of data will be read, but no more is
202 * guaranteed. If the channel is non-blocking and no
203 * data is available, it will return QIO_CHANNEL_ERR_BLOCK
204 *
205 * If the channel has passed any file descriptors,
206 * the @fds array pointer will be allocated and
207 * the elements filled with the received file
208 * descriptors. The @nfds pointer will be updated
209 * to indicate the size of the @fds array that
210 * was allocated. It is the callers responsibility
211 * to call close() on each file descriptor and to
212 * call g_free() on the array pointer in @fds.
213 *
214 * It is an error to pass a non-NULL @fds parameter
215 * unless qio_channel_has_feature() returns a true
216 * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
217 *
218 * Returns: the number of bytes read, or -1 on error,
219 * or QIO_CHANNEL_ERR_BLOCK if no data is available
220 * and the channel is non-blocking
221 */
222ssize_t qio_channel_readv_full(QIOChannel *ioc,
223 const struct iovec *iov,
224 size_t niov,
225 int **fds,
226 size_t *nfds,
227 Error **errp);
228
229
230/**
231 * qio_channel_writev_full:
232 * @ioc: the channel object
233 * @iov: the array of memory regions to write data from
234 * @niov: the length of the @iov array
235 * @fds: an array of file handles to send
236 * @nfds: number of file handles in @fds
821791b5 237 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
238 *
239 * Write data to the IO channel, reading it from the
240 * memory regions referenced by @iov. Each element
241 * in the @iov will be fully sent, before the next
242 * one is used. The @niov parameter specifies the
243 * total number of elements in @iov.
244 *
245 * It is not required for all @iov data to be fully
246 * sent. If the channel is in blocking mode, at least
247 * one byte of data will be sent, but no more is
248 * guaranteed. If the channel is non-blocking and no
249 * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
250 *
251 * If there are file descriptors to send, the @fds
252 * array should be non-NULL and provide the handles.
253 * All file descriptors will be sent if at least one
254 * byte of data was sent.
255 *
256 * It is an error to pass a non-NULL @fds parameter
257 * unless qio_channel_has_feature() returns a true
258 * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
259 *
260 * Returns: the number of bytes sent, or -1 on error,
261 * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
262 * and the channel is non-blocking
263 */
264ssize_t qio_channel_writev_full(QIOChannel *ioc,
265 const struct iovec *iov,
266 size_t niov,
267 int *fds,
268 size_t nfds,
269 Error **errp);
270
271/**
272 * qio_channel_readv:
273 * @ioc: the channel object
274 * @iov: the array of memory regions to read data into
275 * @niov: the length of the @iov array
821791b5 276 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
277 *
278 * Behaves as qio_channel_readv_full() but does not support
279 * receiving of file handles.
280 */
281ssize_t qio_channel_readv(QIOChannel *ioc,
282 const struct iovec *iov,
283 size_t niov,
284 Error **errp);
285
286/**
287 * qio_channel_writev:
288 * @ioc: the channel object
289 * @iov: the array of memory regions to write data from
290 * @niov: the length of the @iov array
821791b5 291 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
292 *
293 * Behaves as qio_channel_writev_full() but does not support
294 * sending of file handles.
295 */
296ssize_t qio_channel_writev(QIOChannel *ioc,
297 const struct iovec *iov,
298 size_t niov,
299 Error **errp);
300
301/**
302 * qio_channel_readv:
303 * @ioc: the channel object
304 * @buf: the memory region to read data into
305 * @buflen: the length of @buf
821791b5 306 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
307 *
308 * Behaves as qio_channel_readv_full() but does not support
309 * receiving of file handles, and only supports reading into
310 * a single memory region.
311 */
312ssize_t qio_channel_read(QIOChannel *ioc,
313 char *buf,
314 size_t buflen,
315 Error **errp);
316
317/**
61f7c6a0 318 * qio_channel_write:
666a3af9
DB
319 * @ioc: the channel object
320 * @buf: the memory regions to send data from
321 * @buflen: the length of @buf
821791b5 322 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
323 *
324 * Behaves as qio_channel_writev_full() but does not support
325 * sending of file handles, and only supports writing from a
326 * single memory region.
327 */
328ssize_t qio_channel_write(QIOChannel *ioc,
329 const char *buf,
330 size_t buflen,
331 Error **errp);
332
333/**
334 * qio_channel_set_blocking:
335 * @ioc: the channel object
336 * @enabled: the blocking flag state
821791b5 337 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
338 *
339 * If @enabled is true, then the channel is put into
340 * blocking mode, otherwise it will be non-blocking.
341 *
342 * In non-blocking mode, read/write operations may
343 * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
344 * block on I/O
345 */
346int qio_channel_set_blocking(QIOChannel *ioc,
347 bool enabled,
348 Error **errp);
349
350/**
351 * qio_channel_close:
352 * @ioc: the channel object
821791b5 353 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
354 *
355 * Close the channel, flushing any pending I/O
356 *
357 * Returns: 0 on success, -1 on error
358 */
359int qio_channel_close(QIOChannel *ioc,
360 Error **errp);
361
362/**
363 * qio_channel_shutdown:
364 * @ioc: the channel object
365 * @how: the direction to shutdown
821791b5 366 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
367 *
368 * Shutdowns transmission and/or receiving of data
369 * without closing the underlying transport.
370 *
371 * Not all implementations will support this facility,
372 * so may report an error. To avoid errors, the
373 * caller may check for the feature flag
374 * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling
375 * this method.
376 *
377 * Returns: 0 on success, -1 on error
378 */
379int qio_channel_shutdown(QIOChannel *ioc,
380 QIOChannelShutdown how,
381 Error **errp);
382
383/**
384 * qio_channel_set_delay:
385 * @ioc: the channel object
386 * @enabled: the new flag state
387 *
388 * Controls whether the underlying transport is
389 * permitted to delay writes in order to merge
390 * small packets. If @enabled is true, then the
391 * writes may be delayed in order to opportunistically
392 * merge small packets into larger ones. If @enabled
393 * is false, writes are dispatched immediately with
394 * no delay.
395 *
396 * When @enabled is false, applications may wish to
397 * use the qio_channel_set_cork() method to explicitly
398 * control write merging.
399 *
400 * On channels which are backed by a socket, this
401 * API corresponds to the inverse of TCP_NODELAY flag,
402 * controlling whether the Nagle algorithm is active.
403 *
404 * This setting is merely a hint, so implementations are
405 * free to ignore this without it being considered an
406 * error.
407 */
408void qio_channel_set_delay(QIOChannel *ioc,
409 bool enabled);
410
411/**
412 * qio_channel_set_cork:
413 * @ioc: the channel object
414 * @enabled: the new flag state
415 *
416 * Controls whether the underlying transport is
417 * permitted to dispatch data that is written.
418 * If @enabled is true, then any data written will
419 * be queued in local buffers until @enabled is
420 * set to false once again.
421 *
422 * This feature is typically used when the automatic
423 * write coalescing facility is disabled via the
424 * qio_channel_set_delay() method.
425 *
426 * On channels which are backed by a socket, this
427 * API corresponds to the TCP_CORK flag.
428 *
429 * This setting is merely a hint, so implementations are
430 * free to ignore this without it being considered an
431 * error.
432 */
433void qio_channel_set_cork(QIOChannel *ioc,
434 bool enabled);
435
436
437/**
438 * qio_channel_seek:
439 * @ioc: the channel object
440 * @offset: the position to seek to, relative to @whence
441 * @whence: one of the (POSIX) SEEK_* constants listed below
821791b5 442 * @errp: pointer to a NULL-initialized error object
666a3af9
DB
443 *
444 * Moves the current I/O position within the channel
445 * @ioc, to be @offset. The value of @offset is
446 * interpreted relative to @whence:
447 *
448 * SEEK_SET - the position is set to @offset bytes
449 * SEEK_CUR - the position is moved by @offset bytes
450 * SEEK_END - the position is set to end of the file plus @offset bytes
451 *
452 * Not all implementations will support this facility,
453 * so may report an error.
454 *
455 * Returns: the new position on success, (off_t)-1 on failure
456 */
457off_t qio_channel_io_seek(QIOChannel *ioc,
458 off_t offset,
459 int whence,
460 Error **errp);
461
462
463/**
464 * qio_channel_create_watch:
465 * @ioc: the channel object
466 * @condition: the I/O condition to monitor
467 *
468 * Create a new main loop source that is used to watch
469 * for the I/O condition @condition. Typically the
470 * qio_channel_add_watch() method would be used instead
471 * of this, since it directly attaches a callback to
472 * the source
473 *
474 * Returns: the new main loop source.
475 */
476GSource *qio_channel_create_watch(QIOChannel *ioc,
477 GIOCondition condition);
478
479/**
480 * qio_channel_add_watch:
481 * @ioc: the channel object
482 * @condition: the I/O condition to monitor
483 * @func: callback to invoke when the source becomes ready
484 * @user_data: opaque data to pass to @func
485 * @notify: callback to free @user_data
486 *
487 * Create a new main loop source that is used to watch
488 * for the I/O condition @condition. The callback @func
489 * will be registered against the source, to be invoked
490 * when the source becomes ready. The optional @user_data
491 * will be passed to @func when it is invoked. The @notify
492 * callback will be used to free @user_data when the
493 * watch is deleted
494 *
495 * The returned source ID can be used with g_source_remove()
496 * to remove and free the source when no longer required.
497 * Alternatively the @func callback can return a FALSE
498 * value.
499 *
500 * Returns: the source ID
501 */
502guint qio_channel_add_watch(QIOChannel *ioc,
503 GIOCondition condition,
504 QIOChannelFunc func,
505 gpointer user_data,
506 GDestroyNotify notify);
507
508
c4c497d2
PB
509/**
510 * qio_channel_attach_aio_context:
511 * @ioc: the channel object
512 * @ctx: the #AioContext to set the handlers on
513 *
514 * Request that qio_channel_yield() sets I/O handlers on
515 * the given #AioContext. If @ctx is %NULL, qio_channel_yield()
516 * uses QEMU's main thread event loop.
517 *
518 * You can move a #QIOChannel from one #AioContext to another even if
519 * I/O handlers are set for a coroutine. However, #QIOChannel provides
520 * no synchronization between the calls to qio_channel_yield() and
521 * qio_channel_attach_aio_context().
522 *
523 * Therefore you should first call qio_channel_detach_aio_context()
524 * to ensure that the coroutine is not entered concurrently. Then,
525 * while the coroutine has yielded, call qio_channel_attach_aio_context(),
526 * and then aio_co_schedule() to place the coroutine on the new
527 * #AioContext. The calls to qio_channel_detach_aio_context()
528 * and qio_channel_attach_aio_context() should be protected with
529 * aio_context_acquire() and aio_context_release().
530 */
531void qio_channel_attach_aio_context(QIOChannel *ioc,
532 AioContext *ctx);
533
534/**
535 * qio_channel_detach_aio_context:
536 * @ioc: the channel object
537 *
538 * Disable any I/O handlers set by qio_channel_yield(). With the
539 * help of aio_co_schedule(), this allows moving a coroutine that was
540 * paused by qio_channel_yield() to another context.
541 */
542void qio_channel_detach_aio_context(QIOChannel *ioc);
543
666a3af9
DB
544/**
545 * qio_channel_yield:
546 * @ioc: the channel object
547 * @condition: the I/O condition to wait for
548 *
c4c497d2
PB
549 * Yields execution from the current coroutine until the condition
550 * indicated by @condition becomes available. @condition must
551 * be either %G_IO_IN or %G_IO_OUT; it cannot contain both. In
552 * addition, no two coroutine can be waiting on the same condition
553 * and channel at the same time.
666a3af9
DB
554 *
555 * This must only be called from coroutine context
556 */
557void qio_channel_yield(QIOChannel *ioc,
558 GIOCondition condition);
559
560/**
561 * qio_channel_wait:
562 * @ioc: the channel object
563 * @condition: the I/O condition to wait for
564 *
565 * Block execution from the current thread until
566 * the condition indicated by @condition becomes
567 * available.
568 *
569 * This will enter a nested event loop to perform
570 * the wait.
571 */
572void qio_channel_wait(QIOChannel *ioc,
573 GIOCondition condition);
574
bf88c124
PB
575/**
576 * qio_channel_set_aio_fd_handler:
577 * @ioc: the channel object
578 * @ctx: the AioContext to set the handlers on
579 * @io_read: the read handler
580 * @io_write: the write handler
581 * @opaque: the opaque value passed to the handler
582 *
583 * This is used internally by qio_channel_yield(). It can
584 * be used by channel implementations to forward the handlers
585 * to another channel (e.g. from #QIOChannelTLS to the
586 * underlying socket).
587 */
588void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
589 AioContext *ctx,
590 IOHandler *io_read,
591 IOHandler *io_write,
592 void *opaque);
593
2a6a4076 594#endif /* QIO_CHANNEL_H */