4 * Copyright (c) 2015 Red Hat, Inc.
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.
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.
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/>.
24 #include "qemu-common.h"
25 #include "qom/object.h"
26 #include "qemu/coroutine.h"
27 #include "block/aio.h"
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)
37 typedef struct QIOChannel QIOChannel
;
38 typedef struct QIOChannelClass QIOChannelClass
;
40 #define QIO_CHANNEL_ERR_BLOCK -2
42 typedef enum QIOChannelFeature QIOChannelFeature
;
44 enum QIOChannelFeature
{
45 QIO_CHANNEL_FEATURE_FD_PASS
,
46 QIO_CHANNEL_FEATURE_SHUTDOWN
,
47 QIO_CHANNEL_FEATURE_LISTEN
,
51 typedef enum QIOChannelShutdown QIOChannelShutdown
;
53 enum QIOChannelShutdown
{
54 QIO_CHANNEL_SHUTDOWN_BOTH
,
55 QIO_CHANNEL_SHUTDOWN_READ
,
56 QIO_CHANNEL_SHUTDOWN_WRITE
,
59 typedef gboolean (*QIOChannelFunc
)(QIOChannel
*ioc
,
60 GIOCondition condition
,
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
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
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.
83 unsigned int features
; /* bitmask of QIOChannelFeatures */
86 Coroutine
*read_coroutine
;
87 Coroutine
*write_coroutine
;
89 HANDLE event
; /* For use with GSource on Win32 */
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.
101 * Consult the corresponding public API docs for a description
102 * of the semantics of each callback
104 struct QIOChannelClass
{
107 /* Mandatory callbacks */
108 ssize_t (*io_writev
)(QIOChannel
*ioc
,
109 const struct iovec
*iov
,
114 ssize_t (*io_readv
)(QIOChannel
*ioc
,
115 const struct iovec
*iov
,
120 int (*io_close
)(QIOChannel
*ioc
,
122 GSource
* (*io_create_watch
)(QIOChannel
*ioc
,
123 GIOCondition condition
);
124 int (*io_set_blocking
)(QIOChannel
*ioc
,
128 /* Optional callbacks */
129 int (*io_shutdown
)(QIOChannel
*ioc
,
130 QIOChannelShutdown how
,
132 void (*io_set_cork
)(QIOChannel
*ioc
,
134 void (*io_set_delay
)(QIOChannel
*ioc
,
136 off_t (*io_seek
)(QIOChannel
*ioc
,
140 void (*io_set_aio_fd_handler
)(QIOChannel
*ioc
,
147 /* General I/O handling functions */
150 * qio_channel_has_feature:
151 * @ioc: the channel object
152 * @feature: the feature to check support of
154 * Determine whether the channel implementation supports
155 * the optional feature named in @feature.
157 * Returns: true if supported, false otherwise.
159 bool qio_channel_has_feature(QIOChannel
*ioc
,
160 QIOChannelFeature feature
);
163 * qio_channel_set_feature:
164 * @ioc: the channel object
165 * @feature: the feature to set support for
167 * Add channel support for the feature named in @feature.
169 void qio_channel_set_feature(QIOChannel
*ioc
,
170 QIOChannelFeature feature
);
173 * qio_channel_set_name:
174 * @ioc: the channel object
175 * @name: the name of the channel
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.
181 void qio_channel_set_name(QIOChannel
*ioc
,
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
191 * @errp: pointer to a NULL-initialized error object
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.
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
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.
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.
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
222 ssize_t
qio_channel_readv_full(QIOChannel
*ioc
,
223 const struct iovec
*iov
,
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
237 * @errp: pointer to a NULL-initialized error object
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.
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
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.
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.
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
264 ssize_t
qio_channel_writev_full(QIOChannel
*ioc
,
265 const struct iovec
*iov
,
273 * @ioc: the channel object
274 * @iov: the array of memory regions to read data into
275 * @niov: the length of the @iov array
276 * @errp: pointer to a NULL-initialized error object
278 * Behaves as qio_channel_readv_full() but does not support
279 * receiving of file handles.
281 ssize_t
qio_channel_readv(QIOChannel
*ioc
,
282 const struct iovec
*iov
,
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
291 * @errp: pointer to a NULL-initialized error object
293 * Behaves as qio_channel_writev_full() but does not support
294 * sending of file handles.
296 ssize_t
qio_channel_writev(QIOChannel
*ioc
,
297 const struct iovec
*iov
,
303 * @ioc: the channel object
304 * @buf: the memory region to read data into
305 * @buflen: the length of @buf
306 * @errp: pointer to a NULL-initialized error object
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.
312 ssize_t
qio_channel_read(QIOChannel
*ioc
,
318 * qio_channel_writev:
319 * @ioc: the channel object
320 * @buf: the memory regions to send data from
321 * @buflen: the length of @buf
322 * @errp: pointer to a NULL-initialized error object
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.
328 ssize_t
qio_channel_write(QIOChannel
*ioc
,
334 * qio_channel_set_blocking:
335 * @ioc: the channel object
336 * @enabled: the blocking flag state
337 * @errp: pointer to a NULL-initialized error object
339 * If @enabled is true, then the channel is put into
340 * blocking mode, otherwise it will be non-blocking.
342 * In non-blocking mode, read/write operations may
343 * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
346 int qio_channel_set_blocking(QIOChannel
*ioc
,
352 * @ioc: the channel object
353 * @errp: pointer to a NULL-initialized error object
355 * Close the channel, flushing any pending I/O
357 * Returns: 0 on success, -1 on error
359 int qio_channel_close(QIOChannel
*ioc
,
363 * qio_channel_shutdown:
364 * @ioc: the channel object
365 * @how: the direction to shutdown
366 * @errp: pointer to a NULL-initialized error object
368 * Shutdowns transmission and/or receiving of data
369 * without closing the underlying transport.
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
377 * Returns: 0 on success, -1 on error
379 int qio_channel_shutdown(QIOChannel
*ioc
,
380 QIOChannelShutdown how
,
384 * qio_channel_set_delay:
385 * @ioc: the channel object
386 * @enabled: the new flag state
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
396 * When @enabled is false, applications may wish to
397 * use the qio_channel_set_cork() method to explicitly
398 * control write merging.
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.
404 * This setting is merely a hint, so implementations are
405 * free to ignore this without it being considered an
408 void qio_channel_set_delay(QIOChannel
*ioc
,
412 * qio_channel_set_cork:
413 * @ioc: the channel object
414 * @enabled: the new flag state
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.
422 * This feature is typically used when the automatic
423 * write coalescing facility is disabled via the
424 * qio_channel_set_delay() method.
426 * On channels which are backed by a socket, this
427 * API corresponds to the TCP_CORK flag.
429 * This setting is merely a hint, so implementations are
430 * free to ignore this without it being considered an
433 void qio_channel_set_cork(QIOChannel
*ioc
,
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
442 * @errp: pointer to a NULL-initialized error object
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:
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
452 * Not all implementations will support this facility,
453 * so may report an error.
455 * Returns: the new position on success, (off_t)-1 on failure
457 off_t
qio_channel_io_seek(QIOChannel
*ioc
,
464 * qio_channel_create_watch:
465 * @ioc: the channel object
466 * @condition: the I/O condition to monitor
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
474 * Returns: the new main loop source.
476 GSource
*qio_channel_create_watch(QIOChannel
*ioc
,
477 GIOCondition condition
);
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
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
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
500 * Returns: the source ID
502 guint
qio_channel_add_watch(QIOChannel
*ioc
,
503 GIOCondition condition
,
506 GDestroyNotify notify
);
510 * qio_channel_attach_aio_context:
511 * @ioc: the channel object
512 * @ctx: the #AioContext to set the handlers on
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.
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().
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().
531 void qio_channel_attach_aio_context(QIOChannel
*ioc
,
535 * qio_channel_detach_aio_context:
536 * @ioc: the channel object
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.
542 void qio_channel_detach_aio_context(QIOChannel
*ioc
);
546 * @ioc: the channel object
547 * @condition: the I/O condition to wait for
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.
555 * This must only be called from coroutine context
557 void qio_channel_yield(QIOChannel
*ioc
,
558 GIOCondition condition
);
562 * @ioc: the channel object
563 * @condition: the I/O condition to wait for
565 * Block execution from the current thread until
566 * the condition indicated by @condition becomes
569 * This will enter a nested event loop to perform
572 void qio_channel_wait(QIOChannel
*ioc
,
573 GIOCondition condition
);
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
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).
588 void qio_channel_set_aio_fd_handler(QIOChannel
*ioc
,
594 #endif /* QIO_CHANNEL_H */