1666a3af9SDaniel P. Berrange /* 2666a3af9SDaniel P. Berrange * QEMU I/O channels 3666a3af9SDaniel P. Berrange * 4666a3af9SDaniel P. Berrange * Copyright (c) 2015 Red Hat, Inc. 5666a3af9SDaniel P. Berrange * 6666a3af9SDaniel P. Berrange * This library is free software; you can redistribute it and/or 7666a3af9SDaniel P. Berrange * modify it under the terms of the GNU Lesser General Public 8666a3af9SDaniel P. Berrange * License as published by the Free Software Foundation; either 9c8198bd5SChetan Pant * version 2.1 of the License, or (at your option) any later version. 10666a3af9SDaniel P. Berrange * 11666a3af9SDaniel P. Berrange * This library is distributed in the hope that it will be useful, 12666a3af9SDaniel P. Berrange * but WITHOUT ANY WARRANTY; without even the implied warranty of 13666a3af9SDaniel P. Berrange * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14666a3af9SDaniel P. Berrange * Lesser General Public License for more details. 15666a3af9SDaniel P. Berrange * 16666a3af9SDaniel P. Berrange * You should have received a copy of the GNU Lesser General Public 17666a3af9SDaniel P. Berrange * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18666a3af9SDaniel P. Berrange * 19666a3af9SDaniel P. Berrange */ 20666a3af9SDaniel P. Berrange 212a6a4076SMarkus Armbruster #ifndef QIO_CHANNEL_H 222a6a4076SMarkus Armbruster #define QIO_CHANNEL_H 23666a3af9SDaniel P. Berrange 24666a3af9SDaniel P. Berrange #include "qom/object.h" 2568ba85ceSMarkus Armbruster #include "qemu/coroutine-core.h" 26bf88c124SPaolo Bonzini #include "block/aio.h" 27666a3af9SDaniel P. Berrange 28666a3af9SDaniel P. Berrange #define TYPE_QIO_CHANNEL "qio-channel" 29c821774aSEduardo Habkost OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass, 3030b5707cSEduardo Habkost QIO_CHANNEL) 31666a3af9SDaniel P. Berrange 32666a3af9SDaniel P. Berrange 33666a3af9SDaniel P. Berrange #define QIO_CHANNEL_ERR_BLOCK -2 34666a3af9SDaniel P. Berrange 35b88651cbSLeonardo Bras #define QIO_CHANNEL_WRITE_FLAG_ZERO_COPY 0x1 36b88651cbSLeonardo Bras 3784615a19Smanish.mishra #define QIO_CHANNEL_READ_FLAG_MSG_PEEK 0x1 38*322d873bSFabiano Rosas #define QIO_CHANNEL_READ_FLAG_RELAXED_EOF 0x2 3984615a19Smanish.mishra 40666a3af9SDaniel P. Berrange typedef enum QIOChannelFeature QIOChannelFeature; 41666a3af9SDaniel P. Berrange 42666a3af9SDaniel P. Berrange enum QIOChannelFeature { 438fbf6612SFelipe Franciosi QIO_CHANNEL_FEATURE_FD_PASS, 448fbf6612SFelipe Franciosi QIO_CHANNEL_FEATURE_SHUTDOWN, 458fbf6612SFelipe Franciosi QIO_CHANNEL_FEATURE_LISTEN, 46b88651cbSLeonardo Bras QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY, 4784615a19Smanish.mishra QIO_CHANNEL_FEATURE_READ_MSG_PEEK, 48401e311fSNikolay Borisov QIO_CHANNEL_FEATURE_SEEKABLE, 49666a3af9SDaniel P. Berrange }; 50666a3af9SDaniel P. Berrange 51666a3af9SDaniel P. Berrange 52666a3af9SDaniel P. Berrange typedef enum QIOChannelShutdown QIOChannelShutdown; 53666a3af9SDaniel P. Berrange 54666a3af9SDaniel P. Berrange enum QIOChannelShutdown { 55a2458b6fSDaniel P. Berrangé QIO_CHANNEL_SHUTDOWN_READ = 1, 56a2458b6fSDaniel P. Berrangé QIO_CHANNEL_SHUTDOWN_WRITE = 2, 57a2458b6fSDaniel P. Berrangé QIO_CHANNEL_SHUTDOWN_BOTH = 3, 58666a3af9SDaniel P. Berrange }; 59666a3af9SDaniel P. Berrange 60666a3af9SDaniel P. Berrange typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc, 61666a3af9SDaniel P. Berrange GIOCondition condition, 62666a3af9SDaniel P. Berrange gpointer data); 63666a3af9SDaniel P. Berrange 64666a3af9SDaniel P. Berrange /** 65666a3af9SDaniel P. Berrange * QIOChannel: 66666a3af9SDaniel P. Berrange * 67666a3af9SDaniel P. Berrange * The QIOChannel defines the core API for a generic I/O channel 68666a3af9SDaniel P. Berrange * class hierarchy. It is inspired by GIOChannel, but has the 69666a3af9SDaniel P. Berrange * following differences 70666a3af9SDaniel P. Berrange * 71666a3af9SDaniel P. Berrange * - Use QOM to properly support arbitrary subclassing 72666a3af9SDaniel P. Berrange * - Support use of iovecs for efficient I/O with multiple blocks 73666a3af9SDaniel P. Berrange * - None of the character set translation, binary data exclusively 74666a3af9SDaniel P. Berrange * - Direct support for QEMU Error object reporting 75666a3af9SDaniel P. Berrange * - File descriptor passing 76666a3af9SDaniel P. Berrange * 77666a3af9SDaniel P. Berrange * This base class is abstract so cannot be instantiated. There 78666a3af9SDaniel P. Berrange * will be subclasses for dealing with sockets, files, and higher 79666a3af9SDaniel P. Berrange * level protocols such as TLS, WebSocket, etc. 80666a3af9SDaniel P. Berrange */ 81666a3af9SDaniel P. Berrange 82666a3af9SDaniel P. Berrange struct QIOChannel { 83666a3af9SDaniel P. Berrange Object parent; 84666a3af9SDaniel P. Berrange unsigned int features; /* bitmask of QIOChannelFeatures */ 8520f4aa26SDaniel P. Berrange char *name; 8606e0f098SStefan Hajnoczi AioContext *read_ctx; 87c4c497d2SPaolo Bonzini Coroutine *read_coroutine; 8806e0f098SStefan Hajnoczi AioContext *write_ctx; 89c4c497d2SPaolo Bonzini Coroutine *write_coroutine; 9006e0f098SStefan Hajnoczi bool follow_coroutine_ctx; 91a5897205SPaolo Bonzini #ifdef _WIN32 92a5897205SPaolo Bonzini HANDLE event; /* For use with GSource on Win32 */ 93a5897205SPaolo Bonzini #endif 94666a3af9SDaniel P. Berrange }; 95666a3af9SDaniel P. Berrange 96666a3af9SDaniel P. Berrange /** 97666a3af9SDaniel P. Berrange * QIOChannelClass: 98666a3af9SDaniel P. Berrange * 99666a3af9SDaniel P. Berrange * This class defines the contract that all subclasses 100666a3af9SDaniel P. Berrange * must follow to provide specific channel implementations. 101666a3af9SDaniel P. Berrange * The first five callbacks are mandatory to support, others 102666a3af9SDaniel P. Berrange * provide additional optional features. 103666a3af9SDaniel P. Berrange * 104666a3af9SDaniel P. Berrange * Consult the corresponding public API docs for a description 1058659f317SLukas Straub * of the semantics of each callback. io_shutdown in particular 1068659f317SLukas Straub * must be thread-safe, terminate quickly and must not block. 107666a3af9SDaniel P. Berrange */ 108666a3af9SDaniel P. Berrange struct QIOChannelClass { 109666a3af9SDaniel P. Berrange ObjectClass parent; 110666a3af9SDaniel P. Berrange 111666a3af9SDaniel P. Berrange /* Mandatory callbacks */ 112666a3af9SDaniel P. Berrange ssize_t (*io_writev)(QIOChannel *ioc, 113666a3af9SDaniel P. Berrange const struct iovec *iov, 114666a3af9SDaniel P. Berrange size_t niov, 115666a3af9SDaniel P. Berrange int *fds, 116666a3af9SDaniel P. Berrange size_t nfds, 117b88651cbSLeonardo Bras int flags, 118666a3af9SDaniel P. Berrange Error **errp); 119666a3af9SDaniel P. Berrange ssize_t (*io_readv)(QIOChannel *ioc, 120666a3af9SDaniel P. Berrange const struct iovec *iov, 121666a3af9SDaniel P. Berrange size_t niov, 122666a3af9SDaniel P. Berrange int **fds, 123666a3af9SDaniel P. Berrange size_t *nfds, 12484615a19Smanish.mishra int flags, 125666a3af9SDaniel P. Berrange Error **errp); 126666a3af9SDaniel P. Berrange int (*io_close)(QIOChannel *ioc, 127666a3af9SDaniel P. Berrange Error **errp); 128666a3af9SDaniel P. Berrange GSource * (*io_create_watch)(QIOChannel *ioc, 129666a3af9SDaniel P. Berrange GIOCondition condition); 130666a3af9SDaniel P. Berrange int (*io_set_blocking)(QIOChannel *ioc, 131666a3af9SDaniel P. Berrange bool enabled, 132666a3af9SDaniel P. Berrange Error **errp); 133666a3af9SDaniel P. Berrange 134666a3af9SDaniel P. Berrange /* Optional callbacks */ 135f1cfe394SNikolay Borisov ssize_t (*io_pwritev)(QIOChannel *ioc, 136f1cfe394SNikolay Borisov const struct iovec *iov, 137f1cfe394SNikolay Borisov size_t niov, 138f1cfe394SNikolay Borisov off_t offset, 139f1cfe394SNikolay Borisov Error **errp); 140f1cfe394SNikolay Borisov ssize_t (*io_preadv)(QIOChannel *ioc, 141f1cfe394SNikolay Borisov const struct iovec *iov, 142f1cfe394SNikolay Borisov size_t niov, 143f1cfe394SNikolay Borisov off_t offset, 144f1cfe394SNikolay Borisov Error **errp); 145666a3af9SDaniel P. Berrange int (*io_shutdown)(QIOChannel *ioc, 146666a3af9SDaniel P. Berrange QIOChannelShutdown how, 147666a3af9SDaniel P. Berrange Error **errp); 148666a3af9SDaniel P. Berrange void (*io_set_cork)(QIOChannel *ioc, 149666a3af9SDaniel P. Berrange bool enabled); 150666a3af9SDaniel P. Berrange void (*io_set_delay)(QIOChannel *ioc, 151666a3af9SDaniel P. Berrange bool enabled); 152666a3af9SDaniel P. Berrange off_t (*io_seek)(QIOChannel *ioc, 153666a3af9SDaniel P. Berrange off_t offset, 154666a3af9SDaniel P. Berrange int whence, 155666a3af9SDaniel P. Berrange Error **errp); 156bf88c124SPaolo Bonzini void (*io_set_aio_fd_handler)(QIOChannel *ioc, 15706e0f098SStefan Hajnoczi AioContext *read_ctx, 158bf88c124SPaolo Bonzini IOHandler *io_read, 15906e0f098SStefan Hajnoczi AioContext *write_ctx, 160bf88c124SPaolo Bonzini IOHandler *io_write, 161bf88c124SPaolo Bonzini void *opaque); 162b88651cbSLeonardo Bras int (*io_flush)(QIOChannel *ioc, 163b88651cbSLeonardo Bras Error **errp); 16495fa0c79SAnthony Harivel int (*io_peerpid)(QIOChannel *ioc, 16595fa0c79SAnthony Harivel unsigned int *pid, 16695fa0c79SAnthony Harivel Error **errp); 167666a3af9SDaniel P. Berrange }; 168666a3af9SDaniel P. Berrange 169666a3af9SDaniel P. Berrange /* General I/O handling functions */ 170666a3af9SDaniel P. Berrange 171666a3af9SDaniel P. Berrange /** 172666a3af9SDaniel P. Berrange * qio_channel_has_feature: 173666a3af9SDaniel P. Berrange * @ioc: the channel object 174666a3af9SDaniel P. Berrange * @feature: the feature to check support of 175666a3af9SDaniel P. Berrange * 176666a3af9SDaniel P. Berrange * Determine whether the channel implementation supports 177666a3af9SDaniel P. Berrange * the optional feature named in @feature. 178666a3af9SDaniel P. Berrange * 179666a3af9SDaniel P. Berrange * Returns: true if supported, false otherwise. 180666a3af9SDaniel P. Berrange */ 181666a3af9SDaniel P. Berrange bool qio_channel_has_feature(QIOChannel *ioc, 182666a3af9SDaniel P. Berrange QIOChannelFeature feature); 183666a3af9SDaniel P. Berrange 184666a3af9SDaniel P. Berrange /** 185d8d3c7ccSFelipe Franciosi * qio_channel_set_feature: 186d8d3c7ccSFelipe Franciosi * @ioc: the channel object 187d8d3c7ccSFelipe Franciosi * @feature: the feature to set support for 188d8d3c7ccSFelipe Franciosi * 189d8d3c7ccSFelipe Franciosi * Add channel support for the feature named in @feature. 190d8d3c7ccSFelipe Franciosi */ 191d8d3c7ccSFelipe Franciosi void qio_channel_set_feature(QIOChannel *ioc, 192d8d3c7ccSFelipe Franciosi QIOChannelFeature feature); 193d8d3c7ccSFelipe Franciosi 194d8d3c7ccSFelipe Franciosi /** 19520f4aa26SDaniel P. Berrange * qio_channel_set_name: 19620f4aa26SDaniel P. Berrange * @ioc: the channel object 19720f4aa26SDaniel P. Berrange * @name: the name of the channel 19820f4aa26SDaniel P. Berrange * 19920f4aa26SDaniel P. Berrange * Sets the name of the channel, which serves as an aid 20020f4aa26SDaniel P. Berrange * to debugging. The name is used when creating GSource 20120f4aa26SDaniel P. Berrange * watches for this channel. 20220f4aa26SDaniel P. Berrange */ 20320f4aa26SDaniel P. Berrange void qio_channel_set_name(QIOChannel *ioc, 20420f4aa26SDaniel P. Berrange const char *name); 20520f4aa26SDaniel P. Berrange 20620f4aa26SDaniel P. Berrange /** 207666a3af9SDaniel P. Berrange * qio_channel_readv_full: 208666a3af9SDaniel P. Berrange * @ioc: the channel object 209666a3af9SDaniel P. Berrange * @iov: the array of memory regions to read data into 210666a3af9SDaniel P. Berrange * @niov: the length of the @iov array 211666a3af9SDaniel P. Berrange * @fds: pointer to an array that will received file handles 212666a3af9SDaniel P. Berrange * @nfds: pointer filled with number of elements in @fds on return 21384615a19Smanish.mishra * @flags: read flags (QIO_CHANNEL_READ_FLAG_*) 214821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 215666a3af9SDaniel P. Berrange * 216666a3af9SDaniel P. Berrange * Read data from the IO channel, storing it in the 217666a3af9SDaniel P. Berrange * memory regions referenced by @iov. Each element 218666a3af9SDaniel P. Berrange * in the @iov will be fully populated with data 219666a3af9SDaniel P. Berrange * before the next one is used. The @niov parameter 220666a3af9SDaniel P. Berrange * specifies the total number of elements in @iov. 221666a3af9SDaniel P. Berrange * 222666a3af9SDaniel P. Berrange * It is not required for all @iov to be filled with 223666a3af9SDaniel P. Berrange * data. If the channel is in blocking mode, at least 224666a3af9SDaniel P. Berrange * one byte of data will be read, but no more is 225666a3af9SDaniel P. Berrange * guaranteed. If the channel is non-blocking and no 226666a3af9SDaniel P. Berrange * data is available, it will return QIO_CHANNEL_ERR_BLOCK 227666a3af9SDaniel P. Berrange * 228666a3af9SDaniel P. Berrange * If the channel has passed any file descriptors, 229666a3af9SDaniel P. Berrange * the @fds array pointer will be allocated and 230666a3af9SDaniel P. Berrange * the elements filled with the received file 231666a3af9SDaniel P. Berrange * descriptors. The @nfds pointer will be updated 232666a3af9SDaniel P. Berrange * to indicate the size of the @fds array that 233666a3af9SDaniel P. Berrange * was allocated. It is the callers responsibility 234666a3af9SDaniel P. Berrange * to call close() on each file descriptor and to 235666a3af9SDaniel P. Berrange * call g_free() on the array pointer in @fds. 236666a3af9SDaniel P. Berrange * 237666a3af9SDaniel P. Berrange * It is an error to pass a non-NULL @fds parameter 238666a3af9SDaniel P. Berrange * unless qio_channel_has_feature() returns a true 239666a3af9SDaniel P. Berrange * value for the QIO_CHANNEL_FEATURE_FD_PASS constant. 240666a3af9SDaniel P. Berrange * 241666a3af9SDaniel P. Berrange * Returns: the number of bytes read, or -1 on error, 242666a3af9SDaniel P. Berrange * or QIO_CHANNEL_ERR_BLOCK if no data is available 243666a3af9SDaniel P. Berrange * and the channel is non-blocking 244666a3af9SDaniel P. Berrange */ 245666a3af9SDaniel P. Berrange ssize_t qio_channel_readv_full(QIOChannel *ioc, 246666a3af9SDaniel P. Berrange const struct iovec *iov, 247666a3af9SDaniel P. Berrange size_t niov, 248666a3af9SDaniel P. Berrange int **fds, 249666a3af9SDaniel P. Berrange size_t *nfds, 25084615a19Smanish.mishra int flags, 251666a3af9SDaniel P. Berrange Error **errp); 252666a3af9SDaniel P. Berrange 253666a3af9SDaniel P. Berrange 254666a3af9SDaniel P. Berrange /** 255666a3af9SDaniel P. Berrange * qio_channel_writev_full: 256666a3af9SDaniel P. Berrange * @ioc: the channel object 257666a3af9SDaniel P. Berrange * @iov: the array of memory regions to write data from 258666a3af9SDaniel P. Berrange * @niov: the length of the @iov array 259666a3af9SDaniel P. Berrange * @fds: an array of file handles to send 260666a3af9SDaniel P. Berrange * @nfds: number of file handles in @fds 261b88651cbSLeonardo Bras * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*) 262821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 263666a3af9SDaniel P. Berrange * 264666a3af9SDaniel P. Berrange * Write data to the IO channel, reading it from the 265666a3af9SDaniel P. Berrange * memory regions referenced by @iov. Each element 266666a3af9SDaniel P. Berrange * in the @iov will be fully sent, before the next 267666a3af9SDaniel P. Berrange * one is used. The @niov parameter specifies the 268666a3af9SDaniel P. Berrange * total number of elements in @iov. 269666a3af9SDaniel P. Berrange * 270666a3af9SDaniel P. Berrange * It is not required for all @iov data to be fully 271666a3af9SDaniel P. Berrange * sent. If the channel is in blocking mode, at least 272666a3af9SDaniel P. Berrange * one byte of data will be sent, but no more is 273666a3af9SDaniel P. Berrange * guaranteed. If the channel is non-blocking and no 274666a3af9SDaniel P. Berrange * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK 275666a3af9SDaniel P. Berrange * 276666a3af9SDaniel P. Berrange * If there are file descriptors to send, the @fds 277666a3af9SDaniel P. Berrange * array should be non-NULL and provide the handles. 278666a3af9SDaniel P. Berrange * All file descriptors will be sent if at least one 279666a3af9SDaniel P. Berrange * byte of data was sent. 280666a3af9SDaniel P. Berrange * 281666a3af9SDaniel P. Berrange * It is an error to pass a non-NULL @fds parameter 282666a3af9SDaniel P. Berrange * unless qio_channel_has_feature() returns a true 283666a3af9SDaniel P. Berrange * value for the QIO_CHANNEL_FEATURE_FD_PASS constant. 284666a3af9SDaniel P. Berrange * 285666a3af9SDaniel P. Berrange * Returns: the number of bytes sent, or -1 on error, 286666a3af9SDaniel P. Berrange * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent 287666a3af9SDaniel P. Berrange * and the channel is non-blocking 288666a3af9SDaniel P. Berrange */ 289666a3af9SDaniel P. Berrange ssize_t qio_channel_writev_full(QIOChannel *ioc, 290666a3af9SDaniel P. Berrange const struct iovec *iov, 291666a3af9SDaniel P. Berrange size_t niov, 292666a3af9SDaniel P. Berrange int *fds, 293666a3af9SDaniel P. Berrange size_t nfds, 294b88651cbSLeonardo Bras int flags, 295666a3af9SDaniel P. Berrange Error **errp); 296666a3af9SDaniel P. Berrange 297666a3af9SDaniel P. Berrange /** 298e8ffaa31SEric Blake * qio_channel_readv_all_eof: 299e8ffaa31SEric Blake * @ioc: the channel object 300e8ffaa31SEric Blake * @iov: the array of memory regions to read data into 301e8ffaa31SEric Blake * @niov: the length of the @iov array 302e8ffaa31SEric Blake * @errp: pointer to a NULL-initialized error object 303e8ffaa31SEric Blake * 304e8ffaa31SEric Blake * Read data from the IO channel, storing it in the 305e8ffaa31SEric Blake * memory regions referenced by @iov. Each element 306e8ffaa31SEric Blake * in the @iov will be fully populated with data 307e8ffaa31SEric Blake * before the next one is used. The @niov parameter 308e8ffaa31SEric Blake * specifies the total number of elements in @iov. 309e8ffaa31SEric Blake * 310e8ffaa31SEric Blake * The function will wait for all requested data 311e8ffaa31SEric Blake * to be read, yielding from the current coroutine 312e8ffaa31SEric Blake * if required. 313e8ffaa31SEric Blake * 314e8ffaa31SEric Blake * If end-of-file occurs before any data is read, 315e8ffaa31SEric Blake * no error is reported; otherwise, if it occurs 316e8ffaa31SEric Blake * before all requested data has been read, an error 317e8ffaa31SEric Blake * will be reported. 318e8ffaa31SEric Blake * 319e8ffaa31SEric Blake * Returns: 1 if all bytes were read, 0 if end-of-file 320e8ffaa31SEric Blake * occurs without data, or -1 on error 321e8ffaa31SEric Blake */ 3221dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_all_eof(QIOChannel *ioc, 323e8ffaa31SEric Blake const struct iovec *iov, 324e8ffaa31SEric Blake size_t niov, 325e8ffaa31SEric Blake Error **errp); 326e8ffaa31SEric Blake 327e8ffaa31SEric Blake /** 328d4622e55SDaniel P. Berrange * qio_channel_readv_all: 329d4622e55SDaniel P. Berrange * @ioc: the channel object 330d4622e55SDaniel P. Berrange * @iov: the array of memory regions to read data into 331d4622e55SDaniel P. Berrange * @niov: the length of the @iov array 332d4622e55SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 333d4622e55SDaniel P. Berrange * 334d4622e55SDaniel P. Berrange * Read data from the IO channel, storing it in the 335d4622e55SDaniel P. Berrange * memory regions referenced by @iov. Each element 336d4622e55SDaniel P. Berrange * in the @iov will be fully populated with data 337d4622e55SDaniel P. Berrange * before the next one is used. The @niov parameter 338d4622e55SDaniel P. Berrange * specifies the total number of elements in @iov. 339d4622e55SDaniel P. Berrange * 340d4622e55SDaniel P. Berrange * The function will wait for all requested data 341d4622e55SDaniel P. Berrange * to be read, yielding from the current coroutine 342d4622e55SDaniel P. Berrange * if required. 343d4622e55SDaniel P. Berrange * 344d4622e55SDaniel P. Berrange * If end-of-file occurs before all requested data 345d4622e55SDaniel P. Berrange * has been read, an error will be reported. 346d4622e55SDaniel P. Berrange * 347d4622e55SDaniel P. Berrange * Returns: 0 if all bytes were read, or -1 on error 348d4622e55SDaniel P. Berrange */ 3491dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_all(QIOChannel *ioc, 350d4622e55SDaniel P. Berrange const struct iovec *iov, 351d4622e55SDaniel P. Berrange size_t niov, 352d4622e55SDaniel P. Berrange Error **errp); 353d4622e55SDaniel P. Berrange 354d4622e55SDaniel P. Berrange 355d4622e55SDaniel P. Berrange /** 356d4622e55SDaniel P. Berrange * qio_channel_writev_all: 357d4622e55SDaniel P. Berrange * @ioc: the channel object 358d4622e55SDaniel P. Berrange * @iov: the array of memory regions to write data from 359d4622e55SDaniel P. Berrange * @niov: the length of the @iov array 360d4622e55SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 361d4622e55SDaniel P. Berrange * 362d4622e55SDaniel P. Berrange * Write data to the IO channel, reading it from the 363d4622e55SDaniel P. Berrange * memory regions referenced by @iov. Each element 364d4622e55SDaniel P. Berrange * in the @iov will be fully sent, before the next 365d4622e55SDaniel P. Berrange * one is used. The @niov parameter specifies the 366d4622e55SDaniel P. Berrange * total number of elements in @iov. 367d4622e55SDaniel P. Berrange * 368d4622e55SDaniel P. Berrange * The function will wait for all requested data 369d4622e55SDaniel P. Berrange * to be written, yielding from the current coroutine 370d4622e55SDaniel P. Berrange * if required. 371d4622e55SDaniel P. Berrange * 372d4622e55SDaniel P. Berrange * Returns: 0 if all bytes were written, or -1 on error 373d4622e55SDaniel P. Berrange */ 3741dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_writev_all(QIOChannel *ioc, 375d4622e55SDaniel P. Berrange const struct iovec *iov, 376d4622e55SDaniel P. Berrange size_t niov, 37710220d2fSMarkus Armbruster Error **errp); 378d4622e55SDaniel P. Berrange 379d4622e55SDaniel P. Berrange /** 380666a3af9SDaniel P. Berrange * qio_channel_readv: 381666a3af9SDaniel P. Berrange * @ioc: the channel object 382666a3af9SDaniel P. Berrange * @iov: the array of memory regions to read data into 383666a3af9SDaniel P. Berrange * @niov: the length of the @iov array 384821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 385666a3af9SDaniel P. Berrange * 386666a3af9SDaniel P. Berrange * Behaves as qio_channel_readv_full() but does not support 387666a3af9SDaniel P. Berrange * receiving of file handles. 388666a3af9SDaniel P. Berrange */ 389666a3af9SDaniel P. Berrange ssize_t qio_channel_readv(QIOChannel *ioc, 390666a3af9SDaniel P. Berrange const struct iovec *iov, 391666a3af9SDaniel P. Berrange size_t niov, 392666a3af9SDaniel P. Berrange Error **errp); 393666a3af9SDaniel P. Berrange 394666a3af9SDaniel P. Berrange /** 395666a3af9SDaniel P. Berrange * qio_channel_writev: 396666a3af9SDaniel P. Berrange * @ioc: the channel object 397666a3af9SDaniel P. Berrange * @iov: the array of memory regions to write data from 398666a3af9SDaniel P. Berrange * @niov: the length of the @iov array 399821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 400666a3af9SDaniel P. Berrange * 401666a3af9SDaniel P. Berrange * Behaves as qio_channel_writev_full() but does not support 402666a3af9SDaniel P. Berrange * sending of file handles. 403666a3af9SDaniel P. Berrange */ 404666a3af9SDaniel P. Berrange ssize_t qio_channel_writev(QIOChannel *ioc, 405666a3af9SDaniel P. Berrange const struct iovec *iov, 406666a3af9SDaniel P. Berrange size_t niov, 407666a3af9SDaniel P. Berrange Error **errp); 408666a3af9SDaniel P. Berrange 409666a3af9SDaniel P. Berrange /** 41050ea44f0SDaniel P. Berrange * qio_channel_read: 411666a3af9SDaniel P. Berrange * @ioc: the channel object 412666a3af9SDaniel P. Berrange * @buf: the memory region to read data into 413666a3af9SDaniel P. Berrange * @buflen: the length of @buf 414821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 415666a3af9SDaniel P. Berrange * 416666a3af9SDaniel P. Berrange * Behaves as qio_channel_readv_full() but does not support 417666a3af9SDaniel P. Berrange * receiving of file handles, and only supports reading into 418666a3af9SDaniel P. Berrange * a single memory region. 419666a3af9SDaniel P. Berrange */ 420666a3af9SDaniel P. Berrange ssize_t qio_channel_read(QIOChannel *ioc, 421666a3af9SDaniel P. Berrange char *buf, 422666a3af9SDaniel P. Berrange size_t buflen, 423666a3af9SDaniel P. Berrange Error **errp); 424666a3af9SDaniel P. Berrange 425666a3af9SDaniel P. Berrange /** 42661f7c6a0SMarc-André Lureau * qio_channel_write: 427666a3af9SDaniel P. Berrange * @ioc: the channel object 428666a3af9SDaniel P. Berrange * @buf: the memory regions to send data from 429666a3af9SDaniel P. Berrange * @buflen: the length of @buf 430821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 431666a3af9SDaniel P. Berrange * 432666a3af9SDaniel P. Berrange * Behaves as qio_channel_writev_full() but does not support 433666a3af9SDaniel P. Berrange * sending of file handles, and only supports writing from a 434666a3af9SDaniel P. Berrange * single memory region. 435666a3af9SDaniel P. Berrange */ 436666a3af9SDaniel P. Berrange ssize_t qio_channel_write(QIOChannel *ioc, 437666a3af9SDaniel P. Berrange const char *buf, 438666a3af9SDaniel P. Berrange size_t buflen, 439666a3af9SDaniel P. Berrange Error **errp); 440666a3af9SDaniel P. Berrange 441666a3af9SDaniel P. Berrange /** 442e8ffaa31SEric Blake * qio_channel_read_all_eof: 443e8ffaa31SEric Blake * @ioc: the channel object 444e8ffaa31SEric Blake * @buf: the memory region to read data into 445e8ffaa31SEric Blake * @buflen: the number of bytes to @buf 446e8ffaa31SEric Blake * @errp: pointer to a NULL-initialized error object 447e8ffaa31SEric Blake * 448e8ffaa31SEric Blake * Reads @buflen bytes into @buf, possibly blocking or (if the 449e8ffaa31SEric Blake * channel is non-blocking) yielding from the current coroutine 450e8ffaa31SEric Blake * multiple times until the entire content is read. If end-of-file 451e8ffaa31SEric Blake * occurs immediately it is not an error, but if it occurs after 452e8ffaa31SEric Blake * data has been read it will return an error rather than a 453e8ffaa31SEric Blake * short-read. Otherwise behaves as qio_channel_read(). 454e8ffaa31SEric Blake * 455e8ffaa31SEric Blake * Returns: 1 if all bytes were read, 0 if end-of-file occurs 456e8ffaa31SEric Blake * without data, or -1 on error 457e8ffaa31SEric Blake */ 4581dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_read_all_eof(QIOChannel *ioc, 459e8ffaa31SEric Blake char *buf, 460e8ffaa31SEric Blake size_t buflen, 461e8ffaa31SEric Blake Error **errp); 462e8ffaa31SEric Blake 463e8ffaa31SEric Blake /** 464d4622e55SDaniel P. Berrange * qio_channel_read_all: 465d4622e55SDaniel P. Berrange * @ioc: the channel object 466d4622e55SDaniel P. Berrange * @buf: the memory region to read data into 467d4622e55SDaniel P. Berrange * @buflen: the number of bytes to @buf 468d4622e55SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 469d4622e55SDaniel P. Berrange * 470d4622e55SDaniel P. Berrange * Reads @buflen bytes into @buf, possibly blocking or (if the 471d4622e55SDaniel P. Berrange * channel is non-blocking) yielding from the current coroutine 472d4622e55SDaniel P. Berrange * multiple times until the entire content is read. If end-of-file 473d4622e55SDaniel P. Berrange * occurs it will return an error rather than a short-read. Otherwise 474d4622e55SDaniel P. Berrange * behaves as qio_channel_read(). 475d4622e55SDaniel P. Berrange * 476d4622e55SDaniel P. Berrange * Returns: 0 if all bytes were read, or -1 on error 477d4622e55SDaniel P. Berrange */ 4781dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_read_all(QIOChannel *ioc, 479d4622e55SDaniel P. Berrange char *buf, 480d4622e55SDaniel P. Berrange size_t buflen, 481d4622e55SDaniel P. Berrange Error **errp); 482e8ffaa31SEric Blake 483d4622e55SDaniel P. Berrange /** 484d4622e55SDaniel P. Berrange * qio_channel_write_all: 485d4622e55SDaniel P. Berrange * @ioc: the channel object 486d4622e55SDaniel P. Berrange * @buf: the memory region to write data into 487d4622e55SDaniel P. Berrange * @buflen: the number of bytes to @buf 488d4622e55SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 489d4622e55SDaniel P. Berrange * 490d4622e55SDaniel P. Berrange * Writes @buflen bytes from @buf, possibly blocking or (if the 491d4622e55SDaniel P. Berrange * channel is non-blocking) yielding from the current coroutine 492d4622e55SDaniel P. Berrange * multiple times until the entire content is written. Otherwise 493d4622e55SDaniel P. Berrange * behaves as qio_channel_write(). 494d4622e55SDaniel P. Berrange * 495d4622e55SDaniel P. Berrange * Returns: 0 if all bytes were written, or -1 on error 496d4622e55SDaniel P. Berrange */ 4971dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_write_all(QIOChannel *ioc, 498d4622e55SDaniel P. Berrange const char *buf, 499d4622e55SDaniel P. Berrange size_t buflen, 500d4622e55SDaniel P. Berrange Error **errp); 501d4622e55SDaniel P. Berrange 502d4622e55SDaniel P. Berrange /** 503666a3af9SDaniel P. Berrange * qio_channel_set_blocking: 504666a3af9SDaniel P. Berrange * @ioc: the channel object 505666a3af9SDaniel P. Berrange * @enabled: the blocking flag state 506821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 507666a3af9SDaniel P. Berrange * 508666a3af9SDaniel P. Berrange * If @enabled is true, then the channel is put into 509666a3af9SDaniel P. Berrange * blocking mode, otherwise it will be non-blocking. 510666a3af9SDaniel P. Berrange * 511666a3af9SDaniel P. Berrange * In non-blocking mode, read/write operations may 512666a3af9SDaniel P. Berrange * return QIO_CHANNEL_ERR_BLOCK if they would otherwise 513666a3af9SDaniel P. Berrange * block on I/O 514666a3af9SDaniel P. Berrange */ 515666a3af9SDaniel P. Berrange int qio_channel_set_blocking(QIOChannel *ioc, 516666a3af9SDaniel P. Berrange bool enabled, 517666a3af9SDaniel P. Berrange Error **errp); 518666a3af9SDaniel P. Berrange 519666a3af9SDaniel P. Berrange /** 52006e0f098SStefan Hajnoczi * qio_channel_set_follow_coroutine_ctx: 52106e0f098SStefan Hajnoczi * @ioc: the channel object 52206e0f098SStefan Hajnoczi * @enabled: whether or not to follow the coroutine's AioContext 52306e0f098SStefan Hajnoczi * 52406e0f098SStefan Hajnoczi * If @enabled is true, calls to qio_channel_yield() use the current 52506e0f098SStefan Hajnoczi * coroutine's AioContext. Usually this is desirable. 52606e0f098SStefan Hajnoczi * 52706e0f098SStefan Hajnoczi * If @enabled is false, calls to qio_channel_yield() use the global iohandler 52806e0f098SStefan Hajnoczi * AioContext. This is may be used by coroutines that run in the main loop and 52906e0f098SStefan Hajnoczi * do not wish to respond to I/O during nested event loops. This is the 53006e0f098SStefan Hajnoczi * default for compatibility with code that is not aware of AioContexts. 53106e0f098SStefan Hajnoczi */ 53206e0f098SStefan Hajnoczi void qio_channel_set_follow_coroutine_ctx(QIOChannel *ioc, bool enabled); 53306e0f098SStefan Hajnoczi 53406e0f098SStefan Hajnoczi /** 535666a3af9SDaniel P. Berrange * qio_channel_close: 536666a3af9SDaniel P. Berrange * @ioc: the channel object 537821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 538666a3af9SDaniel P. Berrange * 539666a3af9SDaniel P. Berrange * Close the channel, flushing any pending I/O 540666a3af9SDaniel P. Berrange * 541666a3af9SDaniel P. Berrange * Returns: 0 on success, -1 on error 542666a3af9SDaniel P. Berrange */ 543666a3af9SDaniel P. Berrange int qio_channel_close(QIOChannel *ioc, 544666a3af9SDaniel P. Berrange Error **errp); 545666a3af9SDaniel P. Berrange 546666a3af9SDaniel P. Berrange /** 547f1cfe394SNikolay Borisov * qio_channel_pwritev 548f1cfe394SNikolay Borisov * @ioc: the channel object 549f1cfe394SNikolay Borisov * @iov: the array of memory regions to write data from 550f1cfe394SNikolay Borisov * @niov: the length of the @iov array 551f1cfe394SNikolay Borisov * @offset: offset in the channel where writes should begin 552f1cfe394SNikolay Borisov * @errp: pointer to a NULL-initialized error object 553f1cfe394SNikolay Borisov * 554f1cfe394SNikolay Borisov * Not all implementations will support this facility, so may report 555f1cfe394SNikolay Borisov * an error. To avoid errors, the caller may check for the feature 556f1cfe394SNikolay Borisov * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method. 557f1cfe394SNikolay Borisov * 558f1cfe394SNikolay Borisov * Behaves as qio_channel_writev_full, apart from not supporting 559f1cfe394SNikolay Borisov * sending of file handles as well as beginning the write at the 560f1cfe394SNikolay Borisov * passed @offset 561f1cfe394SNikolay Borisov * 562f1cfe394SNikolay Borisov */ 563f1cfe394SNikolay Borisov ssize_t qio_channel_pwritev(QIOChannel *ioc, const struct iovec *iov, 564f1cfe394SNikolay Borisov size_t niov, off_t offset, Error **errp); 565f1cfe394SNikolay Borisov 566f1cfe394SNikolay Borisov /** 567f1cfe394SNikolay Borisov * qio_channel_pwrite 568f1cfe394SNikolay Borisov * @ioc: the channel object 569f1cfe394SNikolay Borisov * @buf: the memory region to write data into 570f1cfe394SNikolay Borisov * @buflen: the number of bytes to @buf 571f1cfe394SNikolay Borisov * @offset: offset in the channel where writes should begin 572f1cfe394SNikolay Borisov * @errp: pointer to a NULL-initialized error object 573f1cfe394SNikolay Borisov * 574f1cfe394SNikolay Borisov * Not all implementations will support this facility, so may report 575f1cfe394SNikolay Borisov * an error. To avoid errors, the caller may check for the feature 576f1cfe394SNikolay Borisov * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method. 577f1cfe394SNikolay Borisov * 578f1cfe394SNikolay Borisov */ 579f1cfe394SNikolay Borisov ssize_t qio_channel_pwrite(QIOChannel *ioc, char *buf, size_t buflen, 580f1cfe394SNikolay Borisov off_t offset, Error **errp); 581f1cfe394SNikolay Borisov 582f1cfe394SNikolay Borisov /** 583f1cfe394SNikolay Borisov * qio_channel_preadv 584f1cfe394SNikolay Borisov * @ioc: the channel object 585f1cfe394SNikolay Borisov * @iov: the array of memory regions to read data into 586f1cfe394SNikolay Borisov * @niov: the length of the @iov array 587f1cfe394SNikolay Borisov * @offset: offset in the channel where writes should begin 588f1cfe394SNikolay Borisov * @errp: pointer to a NULL-initialized error object 589f1cfe394SNikolay Borisov * 590f1cfe394SNikolay Borisov * Not all implementations will support this facility, so may report 591f1cfe394SNikolay Borisov * an error. To avoid errors, the caller may check for the feature 592f1cfe394SNikolay Borisov * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method. 593f1cfe394SNikolay Borisov * 594f1cfe394SNikolay Borisov * Behaves as qio_channel_readv_full, apart from not supporting 595f1cfe394SNikolay Borisov * receiving of file handles as well as beginning the read at the 596f1cfe394SNikolay Borisov * passed @offset 597f1cfe394SNikolay Borisov * 598f1cfe394SNikolay Borisov */ 599f1cfe394SNikolay Borisov ssize_t qio_channel_preadv(QIOChannel *ioc, const struct iovec *iov, 600f1cfe394SNikolay Borisov size_t niov, off_t offset, Error **errp); 601f1cfe394SNikolay Borisov 602f1cfe394SNikolay Borisov /** 603f1cfe394SNikolay Borisov * qio_channel_pread 604f1cfe394SNikolay Borisov * @ioc: the channel object 605f1cfe394SNikolay Borisov * @buf: the memory region to write data into 606f1cfe394SNikolay Borisov * @buflen: the number of bytes to @buf 607f1cfe394SNikolay Borisov * @offset: offset in the channel where writes should begin 608f1cfe394SNikolay Borisov * @errp: pointer to a NULL-initialized error object 609f1cfe394SNikolay Borisov * 610f1cfe394SNikolay Borisov * Not all implementations will support this facility, so may report 611f1cfe394SNikolay Borisov * an error. To avoid errors, the caller may check for the feature 612f1cfe394SNikolay Borisov * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method. 613f1cfe394SNikolay Borisov * 614f1cfe394SNikolay Borisov */ 615f1cfe394SNikolay Borisov ssize_t qio_channel_pread(QIOChannel *ioc, char *buf, size_t buflen, 616f1cfe394SNikolay Borisov off_t offset, Error **errp); 617f1cfe394SNikolay Borisov 618f1cfe394SNikolay Borisov /** 619666a3af9SDaniel P. Berrange * qio_channel_shutdown: 620666a3af9SDaniel P. Berrange * @ioc: the channel object 621666a3af9SDaniel P. Berrange * @how: the direction to shutdown 622821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 623666a3af9SDaniel P. Berrange * 624666a3af9SDaniel P. Berrange * Shutdowns transmission and/or receiving of data 625666a3af9SDaniel P. Berrange * without closing the underlying transport. 626666a3af9SDaniel P. Berrange * 627666a3af9SDaniel P. Berrange * Not all implementations will support this facility, 628666a3af9SDaniel P. Berrange * so may report an error. To avoid errors, the 629666a3af9SDaniel P. Berrange * caller may check for the feature flag 630666a3af9SDaniel P. Berrange * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling 631666a3af9SDaniel P. Berrange * this method. 632666a3af9SDaniel P. Berrange * 6338659f317SLukas Straub * This function is thread-safe, terminates quickly and does not block. 6348659f317SLukas Straub * 635666a3af9SDaniel P. Berrange * Returns: 0 on success, -1 on error 636666a3af9SDaniel P. Berrange */ 637666a3af9SDaniel P. Berrange int qio_channel_shutdown(QIOChannel *ioc, 638666a3af9SDaniel P. Berrange QIOChannelShutdown how, 639666a3af9SDaniel P. Berrange Error **errp); 640666a3af9SDaniel P. Berrange 641666a3af9SDaniel P. Berrange /** 642666a3af9SDaniel P. Berrange * qio_channel_set_delay: 643666a3af9SDaniel P. Berrange * @ioc: the channel object 644666a3af9SDaniel P. Berrange * @enabled: the new flag state 645666a3af9SDaniel P. Berrange * 646666a3af9SDaniel P. Berrange * Controls whether the underlying transport is 647666a3af9SDaniel P. Berrange * permitted to delay writes in order to merge 648666a3af9SDaniel P. Berrange * small packets. If @enabled is true, then the 649666a3af9SDaniel P. Berrange * writes may be delayed in order to opportunistically 650666a3af9SDaniel P. Berrange * merge small packets into larger ones. If @enabled 651666a3af9SDaniel P. Berrange * is false, writes are dispatched immediately with 652666a3af9SDaniel P. Berrange * no delay. 653666a3af9SDaniel P. Berrange * 654666a3af9SDaniel P. Berrange * When @enabled is false, applications may wish to 655666a3af9SDaniel P. Berrange * use the qio_channel_set_cork() method to explicitly 656666a3af9SDaniel P. Berrange * control write merging. 657666a3af9SDaniel P. Berrange * 658666a3af9SDaniel P. Berrange * On channels which are backed by a socket, this 659666a3af9SDaniel P. Berrange * API corresponds to the inverse of TCP_NODELAY flag, 660666a3af9SDaniel P. Berrange * controlling whether the Nagle algorithm is active. 661666a3af9SDaniel P. Berrange * 662666a3af9SDaniel P. Berrange * This setting is merely a hint, so implementations are 663666a3af9SDaniel P. Berrange * free to ignore this without it being considered an 664666a3af9SDaniel P. Berrange * error. 665666a3af9SDaniel P. Berrange */ 666666a3af9SDaniel P. Berrange void qio_channel_set_delay(QIOChannel *ioc, 667666a3af9SDaniel P. Berrange bool enabled); 668666a3af9SDaniel P. Berrange 669666a3af9SDaniel P. Berrange /** 670666a3af9SDaniel P. Berrange * qio_channel_set_cork: 671666a3af9SDaniel P. Berrange * @ioc: the channel object 672666a3af9SDaniel P. Berrange * @enabled: the new flag state 673666a3af9SDaniel P. Berrange * 674666a3af9SDaniel P. Berrange * Controls whether the underlying transport is 675666a3af9SDaniel P. Berrange * permitted to dispatch data that is written. 676666a3af9SDaniel P. Berrange * If @enabled is true, then any data written will 677666a3af9SDaniel P. Berrange * be queued in local buffers until @enabled is 678666a3af9SDaniel P. Berrange * set to false once again. 679666a3af9SDaniel P. Berrange * 680666a3af9SDaniel P. Berrange * This feature is typically used when the automatic 681666a3af9SDaniel P. Berrange * write coalescing facility is disabled via the 682666a3af9SDaniel P. Berrange * qio_channel_set_delay() method. 683666a3af9SDaniel P. Berrange * 684666a3af9SDaniel P. Berrange * On channels which are backed by a socket, this 685666a3af9SDaniel P. Berrange * API corresponds to the TCP_CORK flag. 686666a3af9SDaniel P. Berrange * 687666a3af9SDaniel P. Berrange * This setting is merely a hint, so implementations are 688666a3af9SDaniel P. Berrange * free to ignore this without it being considered an 689666a3af9SDaniel P. Berrange * error. 690666a3af9SDaniel P. Berrange */ 691666a3af9SDaniel P. Berrange void qio_channel_set_cork(QIOChannel *ioc, 692666a3af9SDaniel P. Berrange bool enabled); 693666a3af9SDaniel P. Berrange 694666a3af9SDaniel P. Berrange 695666a3af9SDaniel P. Berrange /** 696666a3af9SDaniel P. Berrange * qio_channel_seek: 697666a3af9SDaniel P. Berrange * @ioc: the channel object 698666a3af9SDaniel P. Berrange * @offset: the position to seek to, relative to @whence 699666a3af9SDaniel P. Berrange * @whence: one of the (POSIX) SEEK_* constants listed below 700821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 701666a3af9SDaniel P. Berrange * 702666a3af9SDaniel P. Berrange * Moves the current I/O position within the channel 703666a3af9SDaniel P. Berrange * @ioc, to be @offset. The value of @offset is 704666a3af9SDaniel P. Berrange * interpreted relative to @whence: 705666a3af9SDaniel P. Berrange * 706666a3af9SDaniel P. Berrange * SEEK_SET - the position is set to @offset bytes 707666a3af9SDaniel P. Berrange * SEEK_CUR - the position is moved by @offset bytes 708666a3af9SDaniel P. Berrange * SEEK_END - the position is set to end of the file plus @offset bytes 709666a3af9SDaniel P. Berrange * 710666a3af9SDaniel P. Berrange * Not all implementations will support this facility, 711666a3af9SDaniel P. Berrange * so may report an error. 712666a3af9SDaniel P. Berrange * 713666a3af9SDaniel P. Berrange * Returns: the new position on success, (off_t)-1 on failure 714666a3af9SDaniel P. Berrange */ 715666a3af9SDaniel P. Berrange off_t qio_channel_io_seek(QIOChannel *ioc, 716666a3af9SDaniel P. Berrange off_t offset, 717666a3af9SDaniel P. Berrange int whence, 718666a3af9SDaniel P. Berrange Error **errp); 719666a3af9SDaniel P. Berrange 720666a3af9SDaniel P. Berrange 721666a3af9SDaniel P. Berrange /** 722666a3af9SDaniel P. Berrange * qio_channel_create_watch: 723666a3af9SDaniel P. Berrange * @ioc: the channel object 724666a3af9SDaniel P. Berrange * @condition: the I/O condition to monitor 725666a3af9SDaniel P. Berrange * 726666a3af9SDaniel P. Berrange * Create a new main loop source that is used to watch 727666a3af9SDaniel P. Berrange * for the I/O condition @condition. Typically the 728666a3af9SDaniel P. Berrange * qio_channel_add_watch() method would be used instead 729666a3af9SDaniel P. Berrange * of this, since it directly attaches a callback to 730666a3af9SDaniel P. Berrange * the source 731666a3af9SDaniel P. Berrange * 732666a3af9SDaniel P. Berrange * Returns: the new main loop source. 733666a3af9SDaniel P. Berrange */ 734666a3af9SDaniel P. Berrange GSource *qio_channel_create_watch(QIOChannel *ioc, 735666a3af9SDaniel P. Berrange GIOCondition condition); 736666a3af9SDaniel P. Berrange 737666a3af9SDaniel P. Berrange /** 738666a3af9SDaniel P. Berrange * qio_channel_add_watch: 739666a3af9SDaniel P. Berrange * @ioc: the channel object 740666a3af9SDaniel P. Berrange * @condition: the I/O condition to monitor 741666a3af9SDaniel P. Berrange * @func: callback to invoke when the source becomes ready 742666a3af9SDaniel P. Berrange * @user_data: opaque data to pass to @func 743666a3af9SDaniel P. Berrange * @notify: callback to free @user_data 744666a3af9SDaniel P. Berrange * 745666a3af9SDaniel P. Berrange * Create a new main loop source that is used to watch 746666a3af9SDaniel P. Berrange * for the I/O condition @condition. The callback @func 747666a3af9SDaniel P. Berrange * will be registered against the source, to be invoked 748666a3af9SDaniel P. Berrange * when the source becomes ready. The optional @user_data 749666a3af9SDaniel P. Berrange * will be passed to @func when it is invoked. The @notify 750666a3af9SDaniel P. Berrange * callback will be used to free @user_data when the 751666a3af9SDaniel P. Berrange * watch is deleted 752666a3af9SDaniel P. Berrange * 753666a3af9SDaniel P. Berrange * The returned source ID can be used with g_source_remove() 754666a3af9SDaniel P. Berrange * to remove and free the source when no longer required. 755666a3af9SDaniel P. Berrange * Alternatively the @func callback can return a FALSE 756666a3af9SDaniel P. Berrange * value. 757666a3af9SDaniel P. Berrange * 758666a3af9SDaniel P. Berrange * Returns: the source ID 759666a3af9SDaniel P. Berrange */ 760666a3af9SDaniel P. Berrange guint qio_channel_add_watch(QIOChannel *ioc, 761666a3af9SDaniel P. Berrange GIOCondition condition, 762666a3af9SDaniel P. Berrange QIOChannelFunc func, 763666a3af9SDaniel P. Berrange gpointer user_data, 764666a3af9SDaniel P. Berrange GDestroyNotify notify); 765666a3af9SDaniel P. Berrange 766315409c7SPeter Xu /** 767315409c7SPeter Xu * qio_channel_add_watch_full: 768315409c7SPeter Xu * @ioc: the channel object 769315409c7SPeter Xu * @condition: the I/O condition to monitor 770315409c7SPeter Xu * @func: callback to invoke when the source becomes ready 771315409c7SPeter Xu * @user_data: opaque data to pass to @func 772315409c7SPeter Xu * @notify: callback to free @user_data 773315409c7SPeter Xu * @context: the context to run the watch source 774315409c7SPeter Xu * 775315409c7SPeter Xu * Similar as qio_channel_add_watch(), but allows to specify context 776315409c7SPeter Xu * to run the watch source. 777315409c7SPeter Xu * 778315409c7SPeter Xu * Returns: the source ID 779315409c7SPeter Xu */ 780315409c7SPeter Xu guint qio_channel_add_watch_full(QIOChannel *ioc, 781315409c7SPeter Xu GIOCondition condition, 782315409c7SPeter Xu QIOChannelFunc func, 783315409c7SPeter Xu gpointer user_data, 784315409c7SPeter Xu GDestroyNotify notify, 785315409c7SPeter Xu GMainContext *context); 786315409c7SPeter Xu 787315409c7SPeter Xu /** 788315409c7SPeter Xu * qio_channel_add_watch_source: 789315409c7SPeter Xu * @ioc: the channel object 790315409c7SPeter Xu * @condition: the I/O condition to monitor 791315409c7SPeter Xu * @func: callback to invoke when the source becomes ready 792315409c7SPeter Xu * @user_data: opaque data to pass to @func 793315409c7SPeter Xu * @notify: callback to free @user_data 794315409c7SPeter Xu * @context: gcontext to bind the source to 795315409c7SPeter Xu * 796315409c7SPeter Xu * Similar as qio_channel_add_watch(), but allows to specify context 797315409c7SPeter Xu * to run the watch source, meanwhile return the GSource object 798315409c7SPeter Xu * instead of tag ID, with the GSource referenced already. 799315409c7SPeter Xu * 800315409c7SPeter Xu * Note: callers is responsible to unref the source when not needed. 801315409c7SPeter Xu * 802315409c7SPeter Xu * Returns: the source pointer 803315409c7SPeter Xu */ 804315409c7SPeter Xu GSource *qio_channel_add_watch_source(QIOChannel *ioc, 805315409c7SPeter Xu GIOCondition condition, 806315409c7SPeter Xu QIOChannelFunc func, 807315409c7SPeter Xu gpointer user_data, 808315409c7SPeter Xu GDestroyNotify notify, 809315409c7SPeter Xu GMainContext *context); 810666a3af9SDaniel P. Berrange 811666a3af9SDaniel P. Berrange /** 812666a3af9SDaniel P. Berrange * qio_channel_yield: 813666a3af9SDaniel P. Berrange * @ioc: the channel object 814666a3af9SDaniel P. Berrange * @condition: the I/O condition to wait for 815666a3af9SDaniel P. Berrange * 816c4c497d2SPaolo Bonzini * Yields execution from the current coroutine until the condition 817c4c497d2SPaolo Bonzini * indicated by @condition becomes available. @condition must 818c4c497d2SPaolo Bonzini * be either %G_IO_IN or %G_IO_OUT; it cannot contain both. In 819c4c497d2SPaolo Bonzini * addition, no two coroutine can be waiting on the same condition 820c4c497d2SPaolo Bonzini * and channel at the same time. 821666a3af9SDaniel P. Berrange * 8226886ceafSKevin Wolf * This must only be called from coroutine context. It is safe to 8236886ceafSKevin Wolf * reenter the coroutine externally while it is waiting; in this 8246886ceafSKevin Wolf * case the function will return even if @condition is not yet 8256886ceafSKevin Wolf * available. 826666a3af9SDaniel P. Berrange */ 8276886ceafSKevin Wolf void coroutine_fn qio_channel_yield(QIOChannel *ioc, 828666a3af9SDaniel P. Berrange GIOCondition condition); 829666a3af9SDaniel P. Berrange 830666a3af9SDaniel P. Berrange /** 8317c1f51bfSKevin Wolf * qio_channel_wake_read: 8327c1f51bfSKevin Wolf * @ioc: the channel object 8337c1f51bfSKevin Wolf * 8347c1f51bfSKevin Wolf * If qio_channel_yield() is currently waiting for the channel to become 8357c1f51bfSKevin Wolf * readable, interrupt it and reenter immediately. This function is safe to call 8367c1f51bfSKevin Wolf * from any thread. 8377c1f51bfSKevin Wolf */ 8387c1f51bfSKevin Wolf void qio_channel_wake_read(QIOChannel *ioc); 8397c1f51bfSKevin Wolf 8407c1f51bfSKevin Wolf /** 841666a3af9SDaniel P. Berrange * qio_channel_wait: 842666a3af9SDaniel P. Berrange * @ioc: the channel object 843666a3af9SDaniel P. Berrange * @condition: the I/O condition to wait for 844666a3af9SDaniel P. Berrange * 845666a3af9SDaniel P. Berrange * Block execution from the current thread until 846666a3af9SDaniel P. Berrange * the condition indicated by @condition becomes 847666a3af9SDaniel P. Berrange * available. 848666a3af9SDaniel P. Berrange * 849666a3af9SDaniel P. Berrange * This will enter a nested event loop to perform 850666a3af9SDaniel P. Berrange * the wait. 851666a3af9SDaniel P. Berrange */ 852666a3af9SDaniel P. Berrange void qio_channel_wait(QIOChannel *ioc, 853666a3af9SDaniel P. Berrange GIOCondition condition); 854666a3af9SDaniel P. Berrange 855bf88c124SPaolo Bonzini /** 856bf88c124SPaolo Bonzini * qio_channel_set_aio_fd_handler: 857bf88c124SPaolo Bonzini * @ioc: the channel object 85806e0f098SStefan Hajnoczi * @read_ctx: the AioContext to set the read handler on or NULL 859bf88c124SPaolo Bonzini * @io_read: the read handler 86006e0f098SStefan Hajnoczi * @write_ctx: the AioContext to set the write handler on or NULL 861bf88c124SPaolo Bonzini * @io_write: the write handler 862bf88c124SPaolo Bonzini * @opaque: the opaque value passed to the handler 863bf88c124SPaolo Bonzini * 864bf88c124SPaolo Bonzini * This is used internally by qio_channel_yield(). It can 865bf88c124SPaolo Bonzini * be used by channel implementations to forward the handlers 866bf88c124SPaolo Bonzini * to another channel (e.g. from #QIOChannelTLS to the 867bf88c124SPaolo Bonzini * underlying socket). 86806e0f098SStefan Hajnoczi * 86906e0f098SStefan Hajnoczi * When @read_ctx is NULL, don't touch the read handler. When @write_ctx is 87006e0f098SStefan Hajnoczi * NULL, don't touch the write handler. Note that setting the read handler 87106e0f098SStefan Hajnoczi * clears the write handler, and vice versa, if they share the same AioContext. 87206e0f098SStefan Hajnoczi * Therefore the caller must pass both handlers together when sharing the same 87306e0f098SStefan Hajnoczi * AioContext. 874bf88c124SPaolo Bonzini */ 875bf88c124SPaolo Bonzini void qio_channel_set_aio_fd_handler(QIOChannel *ioc, 87606e0f098SStefan Hajnoczi AioContext *read_ctx, 877bf88c124SPaolo Bonzini IOHandler *io_read, 87806e0f098SStefan Hajnoczi AioContext *write_ctx, 879bf88c124SPaolo Bonzini IOHandler *io_write, 880bf88c124SPaolo Bonzini void *opaque); 881bf88c124SPaolo Bonzini 882bfa42387SElena Ufimtseva /** 883bebab91eSElena Ufimtseva * qio_channel_readv_full_all_eof: 884bebab91eSElena Ufimtseva * @ioc: the channel object 885bebab91eSElena Ufimtseva * @iov: the array of memory regions to read data to 886bebab91eSElena Ufimtseva * @niov: the length of the @iov array 887bebab91eSElena Ufimtseva * @fds: an array of file handles to read 888bebab91eSElena Ufimtseva * @nfds: number of file handles in @fds 889a25b0130SFabiano Rosas * @flags: read flags (QIO_CHANNEL_READ_FLAG_*) 890bebab91eSElena Ufimtseva * @errp: pointer to a NULL-initialized error object 891bebab91eSElena Ufimtseva * 892bebab91eSElena Ufimtseva * 893bebab91eSElena Ufimtseva * Performs same function as qio_channel_readv_all_eof. 894bebab91eSElena Ufimtseva * Additionally, attempts to read file descriptors shared 895bebab91eSElena Ufimtseva * over the channel. The function will wait for all 896bebab91eSElena Ufimtseva * requested data to be read, yielding from the current 897bebab91eSElena Ufimtseva * coroutine if required. data refers to both file 898bebab91eSElena Ufimtseva * descriptors and the iovs. 899bebab91eSElena Ufimtseva * 900bebab91eSElena Ufimtseva * Returns: 1 if all bytes were read, 0 if end-of-file 901bebab91eSElena Ufimtseva * occurs without data, or -1 on error 902bebab91eSElena Ufimtseva */ 903bebab91eSElena Ufimtseva 9041dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_full_all_eof(QIOChannel *ioc, 905bebab91eSElena Ufimtseva const struct iovec *iov, 906bebab91eSElena Ufimtseva size_t niov, 907bebab91eSElena Ufimtseva int **fds, size_t *nfds, 908a25b0130SFabiano Rosas int flags, 909bebab91eSElena Ufimtseva Error **errp); 910bebab91eSElena Ufimtseva 911bebab91eSElena Ufimtseva /** 912bebab91eSElena Ufimtseva * qio_channel_readv_full_all: 913bebab91eSElena Ufimtseva * @ioc: the channel object 914bebab91eSElena Ufimtseva * @iov: the array of memory regions to read data to 915bebab91eSElena Ufimtseva * @niov: the length of the @iov array 916bebab91eSElena Ufimtseva * @fds: an array of file handles to read 917bebab91eSElena Ufimtseva * @nfds: number of file handles in @fds 918bebab91eSElena Ufimtseva * @errp: pointer to a NULL-initialized error object 919bebab91eSElena Ufimtseva * 920bebab91eSElena Ufimtseva * 921bebab91eSElena Ufimtseva * Performs same function as qio_channel_readv_all_eof. 922bebab91eSElena Ufimtseva * Additionally, attempts to read file descriptors shared 923bebab91eSElena Ufimtseva * over the channel. The function will wait for all 924bebab91eSElena Ufimtseva * requested data to be read, yielding from the current 925bebab91eSElena Ufimtseva * coroutine if required. data refers to both file 926bebab91eSElena Ufimtseva * descriptors and the iovs. 927bebab91eSElena Ufimtseva * 928bebab91eSElena Ufimtseva * Returns: 0 if all bytes were read, or -1 on error 929bebab91eSElena Ufimtseva */ 930bebab91eSElena Ufimtseva 9311dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_full_all(QIOChannel *ioc, 932bebab91eSElena Ufimtseva const struct iovec *iov, 933bebab91eSElena Ufimtseva size_t niov, 934bebab91eSElena Ufimtseva int **fds, size_t *nfds, 935bebab91eSElena Ufimtseva Error **errp); 936bebab91eSElena Ufimtseva 937bebab91eSElena Ufimtseva /** 938bfa42387SElena Ufimtseva * qio_channel_writev_full_all: 939bfa42387SElena Ufimtseva * @ioc: the channel object 940bfa42387SElena Ufimtseva * @iov: the array of memory regions to write data from 941bfa42387SElena Ufimtseva * @niov: the length of the @iov array 942bfa42387SElena Ufimtseva * @fds: an array of file handles to send 943bfa42387SElena Ufimtseva * @nfds: number of file handles in @fds 944b88651cbSLeonardo Bras * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*) 945bfa42387SElena Ufimtseva * @errp: pointer to a NULL-initialized error object 946bfa42387SElena Ufimtseva * 947bfa42387SElena Ufimtseva * 948bfa42387SElena Ufimtseva * Behaves like qio_channel_writev_full but will attempt 949bfa42387SElena Ufimtseva * to send all data passed (file handles and memory regions). 950bfa42387SElena Ufimtseva * The function will wait for all requested data 951bfa42387SElena Ufimtseva * to be written, yielding from the current coroutine 952bfa42387SElena Ufimtseva * if required. 953bfa42387SElena Ufimtseva * 954b88651cbSLeonardo Bras * If QIO_CHANNEL_WRITE_FLAG_ZERO_COPY is passed in flags, 955b88651cbSLeonardo Bras * instead of waiting for all requested data to be written, 956b88651cbSLeonardo Bras * this function will wait until it's all queued for writing. 957b88651cbSLeonardo Bras * In this case, if the buffer gets changed between queueing and 958b88651cbSLeonardo Bras * sending, the updated buffer will be sent. If this is not a 959b88651cbSLeonardo Bras * desired behavior, it's suggested to call qio_channel_flush() 960b88651cbSLeonardo Bras * before reusing the buffer. 961b88651cbSLeonardo Bras * 962bfa42387SElena Ufimtseva * Returns: 0 if all bytes were written, or -1 on error 963bfa42387SElena Ufimtseva */ 964bfa42387SElena Ufimtseva 9651dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc, 966bfa42387SElena Ufimtseva const struct iovec *iov, 967bfa42387SElena Ufimtseva size_t niov, 968bfa42387SElena Ufimtseva int *fds, size_t nfds, 969b88651cbSLeonardo Bras int flags, Error **errp); 970b88651cbSLeonardo Bras 971b88651cbSLeonardo Bras /** 972b88651cbSLeonardo Bras * qio_channel_flush: 973b88651cbSLeonardo Bras * @ioc: the channel object 974b88651cbSLeonardo Bras * @errp: pointer to a NULL-initialized error object 975b88651cbSLeonardo Bras * 976b88651cbSLeonardo Bras * Will block until every packet queued with 977b88651cbSLeonardo Bras * qio_channel_writev_full() + QIO_CHANNEL_WRITE_FLAG_ZERO_COPY 978b88651cbSLeonardo Bras * is sent, or return in case of any error. 979b88651cbSLeonardo Bras * 980b88651cbSLeonardo Bras * If not implemented, acts as a no-op, and returns 0. 981b88651cbSLeonardo Bras * 982b88651cbSLeonardo Bras * Returns -1 if any error is found, 983b88651cbSLeonardo Bras * 1 if every send failed to use zero copy. 984b88651cbSLeonardo Bras * 0 otherwise. 985b88651cbSLeonardo Bras */ 986b88651cbSLeonardo Bras 987b88651cbSLeonardo Bras int qio_channel_flush(QIOChannel *ioc, 988bfa42387SElena Ufimtseva Error **errp); 989bfa42387SElena Ufimtseva 99095fa0c79SAnthony Harivel /** 99195fa0c79SAnthony Harivel * qio_channel_get_peercred: 99295fa0c79SAnthony Harivel * @ioc: the channel object 99395fa0c79SAnthony Harivel * @pid: pointer to pid 99495fa0c79SAnthony Harivel * @errp: pointer to a NULL-initialized error object 99595fa0c79SAnthony Harivel * 99695fa0c79SAnthony Harivel * Returns the pid of the peer process connected to this socket. 99795fa0c79SAnthony Harivel * 99895fa0c79SAnthony Harivel * The use of this function is possible only for connected 99995fa0c79SAnthony Harivel * AF_UNIX stream sockets and for AF_UNIX stream and datagram 100095fa0c79SAnthony Harivel * socket pairs on Linux. 100195fa0c79SAnthony Harivel * Return -1 on error with pid -1 for the non-Linux OS. 100295fa0c79SAnthony Harivel * 100395fa0c79SAnthony Harivel */ 100495fa0c79SAnthony Harivel int qio_channel_get_peerpid(QIOChannel *ioc, 100595fa0c79SAnthony Harivel unsigned int *pid, 100695fa0c79SAnthony Harivel Error **errp); 100795fa0c79SAnthony Harivel 10082a6a4076SMarkus Armbruster #endif /* QIO_CHANNEL_H */ 1009