xref: /qemu/include/io/channel.h (revision f1cfe39418a837e8dd52a7e75331f1606949deff)
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
3884615a19Smanish.mishra 
39666a3af9SDaniel P. Berrange typedef enum QIOChannelFeature QIOChannelFeature;
40666a3af9SDaniel P. Berrange 
41666a3af9SDaniel P. Berrange enum QIOChannelFeature {
428fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_FD_PASS,
438fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_SHUTDOWN,
448fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_LISTEN,
45b88651cbSLeonardo Bras     QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY,
4684615a19Smanish.mishra     QIO_CHANNEL_FEATURE_READ_MSG_PEEK,
47401e311fSNikolay Borisov     QIO_CHANNEL_FEATURE_SEEKABLE,
48666a3af9SDaniel P. Berrange };
49666a3af9SDaniel P. Berrange 
50666a3af9SDaniel P. Berrange 
51666a3af9SDaniel P. Berrange typedef enum QIOChannelShutdown QIOChannelShutdown;
52666a3af9SDaniel P. Berrange 
53666a3af9SDaniel P. Berrange enum QIOChannelShutdown {
54a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_READ = 1,
55a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_WRITE = 2,
56a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_BOTH = 3,
57666a3af9SDaniel P. Berrange };
58666a3af9SDaniel P. Berrange 
59666a3af9SDaniel P. Berrange typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc,
60666a3af9SDaniel P. Berrange                                    GIOCondition condition,
61666a3af9SDaniel P. Berrange                                    gpointer data);
62666a3af9SDaniel P. Berrange 
63666a3af9SDaniel P. Berrange /**
64666a3af9SDaniel P. Berrange  * QIOChannel:
65666a3af9SDaniel P. Berrange  *
66666a3af9SDaniel P. Berrange  * The QIOChannel defines the core API for a generic I/O channel
67666a3af9SDaniel P. Berrange  * class hierarchy. It is inspired by GIOChannel, but has the
68666a3af9SDaniel P. Berrange  * following differences
69666a3af9SDaniel P. Berrange  *
70666a3af9SDaniel P. Berrange  *  - Use QOM to properly support arbitrary subclassing
71666a3af9SDaniel P. Berrange  *  - Support use of iovecs for efficient I/O with multiple blocks
72666a3af9SDaniel P. Berrange  *  - None of the character set translation, binary data exclusively
73666a3af9SDaniel P. Berrange  *  - Direct support for QEMU Error object reporting
74666a3af9SDaniel P. Berrange  *  - File descriptor passing
75666a3af9SDaniel P. Berrange  *
76666a3af9SDaniel P. Berrange  * This base class is abstract so cannot be instantiated. There
77666a3af9SDaniel P. Berrange  * will be subclasses for dealing with sockets, files, and higher
78666a3af9SDaniel P. Berrange  * level protocols such as TLS, WebSocket, etc.
79666a3af9SDaniel P. Berrange  */
80666a3af9SDaniel P. Berrange 
81666a3af9SDaniel P. Berrange struct QIOChannel {
82666a3af9SDaniel P. Berrange     Object parent;
83666a3af9SDaniel P. Berrange     unsigned int features; /* bitmask of QIOChannelFeatures */
8420f4aa26SDaniel P. Berrange     char *name;
8506e0f098SStefan Hajnoczi     AioContext *read_ctx;
86c4c497d2SPaolo Bonzini     Coroutine *read_coroutine;
8706e0f098SStefan Hajnoczi     AioContext *write_ctx;
88c4c497d2SPaolo Bonzini     Coroutine *write_coroutine;
8906e0f098SStefan Hajnoczi     bool follow_coroutine_ctx;
90a5897205SPaolo Bonzini #ifdef _WIN32
91a5897205SPaolo Bonzini     HANDLE event; /* For use with GSource on Win32 */
92a5897205SPaolo Bonzini #endif
93666a3af9SDaniel P. Berrange };
94666a3af9SDaniel P. Berrange 
95666a3af9SDaniel P. Berrange /**
96666a3af9SDaniel P. Berrange  * QIOChannelClass:
97666a3af9SDaniel P. Berrange  *
98666a3af9SDaniel P. Berrange  * This class defines the contract that all subclasses
99666a3af9SDaniel P. Berrange  * must follow to provide specific channel implementations.
100666a3af9SDaniel P. Berrange  * The first five callbacks are mandatory to support, others
101666a3af9SDaniel P. Berrange  * provide additional optional features.
102666a3af9SDaniel P. Berrange  *
103666a3af9SDaniel P. Berrange  * Consult the corresponding public API docs for a description
1048659f317SLukas Straub  * of the semantics of each callback. io_shutdown in particular
1058659f317SLukas Straub  * must be thread-safe, terminate quickly and must not block.
106666a3af9SDaniel P. Berrange  */
107666a3af9SDaniel P. Berrange struct QIOChannelClass {
108666a3af9SDaniel P. Berrange     ObjectClass parent;
109666a3af9SDaniel P. Berrange 
110666a3af9SDaniel P. Berrange     /* Mandatory callbacks */
111666a3af9SDaniel P. Berrange     ssize_t (*io_writev)(QIOChannel *ioc,
112666a3af9SDaniel P. Berrange                          const struct iovec *iov,
113666a3af9SDaniel P. Berrange                          size_t niov,
114666a3af9SDaniel P. Berrange                          int *fds,
115666a3af9SDaniel P. Berrange                          size_t nfds,
116b88651cbSLeonardo Bras                          int flags,
117666a3af9SDaniel P. Berrange                          Error **errp);
118666a3af9SDaniel P. Berrange     ssize_t (*io_readv)(QIOChannel *ioc,
119666a3af9SDaniel P. Berrange                         const struct iovec *iov,
120666a3af9SDaniel P. Berrange                         size_t niov,
121666a3af9SDaniel P. Berrange                         int **fds,
122666a3af9SDaniel P. Berrange                         size_t *nfds,
12384615a19Smanish.mishra                         int flags,
124666a3af9SDaniel P. Berrange                         Error **errp);
125666a3af9SDaniel P. Berrange     int (*io_close)(QIOChannel *ioc,
126666a3af9SDaniel P. Berrange                     Error **errp);
127666a3af9SDaniel P. Berrange     GSource * (*io_create_watch)(QIOChannel *ioc,
128666a3af9SDaniel P. Berrange                                  GIOCondition condition);
129666a3af9SDaniel P. Berrange     int (*io_set_blocking)(QIOChannel *ioc,
130666a3af9SDaniel P. Berrange                            bool enabled,
131666a3af9SDaniel P. Berrange                            Error **errp);
132666a3af9SDaniel P. Berrange 
133666a3af9SDaniel P. Berrange     /* Optional callbacks */
134*f1cfe394SNikolay Borisov     ssize_t (*io_pwritev)(QIOChannel *ioc,
135*f1cfe394SNikolay Borisov                           const struct iovec *iov,
136*f1cfe394SNikolay Borisov                           size_t niov,
137*f1cfe394SNikolay Borisov                           off_t offset,
138*f1cfe394SNikolay Borisov                           Error **errp);
139*f1cfe394SNikolay Borisov     ssize_t (*io_preadv)(QIOChannel *ioc,
140*f1cfe394SNikolay Borisov                          const struct iovec *iov,
141*f1cfe394SNikolay Borisov                          size_t niov,
142*f1cfe394SNikolay Borisov                          off_t offset,
143*f1cfe394SNikolay Borisov                          Error **errp);
144666a3af9SDaniel P. Berrange     int (*io_shutdown)(QIOChannel *ioc,
145666a3af9SDaniel P. Berrange                        QIOChannelShutdown how,
146666a3af9SDaniel P. Berrange                        Error **errp);
147666a3af9SDaniel P. Berrange     void (*io_set_cork)(QIOChannel *ioc,
148666a3af9SDaniel P. Berrange                         bool enabled);
149666a3af9SDaniel P. Berrange     void (*io_set_delay)(QIOChannel *ioc,
150666a3af9SDaniel P. Berrange                          bool enabled);
151666a3af9SDaniel P. Berrange     off_t (*io_seek)(QIOChannel *ioc,
152666a3af9SDaniel P. Berrange                      off_t offset,
153666a3af9SDaniel P. Berrange                      int whence,
154666a3af9SDaniel P. Berrange                      Error **errp);
155bf88c124SPaolo Bonzini     void (*io_set_aio_fd_handler)(QIOChannel *ioc,
15606e0f098SStefan Hajnoczi                                   AioContext *read_ctx,
157bf88c124SPaolo Bonzini                                   IOHandler *io_read,
15806e0f098SStefan Hajnoczi                                   AioContext *write_ctx,
159bf88c124SPaolo Bonzini                                   IOHandler *io_write,
160bf88c124SPaolo Bonzini                                   void *opaque);
161b88651cbSLeonardo Bras     int (*io_flush)(QIOChannel *ioc,
162b88651cbSLeonardo Bras                     Error **errp);
163666a3af9SDaniel P. Berrange };
164666a3af9SDaniel P. Berrange 
165666a3af9SDaniel P. Berrange /* General I/O handling functions */
166666a3af9SDaniel P. Berrange 
167666a3af9SDaniel P. Berrange /**
168666a3af9SDaniel P. Berrange  * qio_channel_has_feature:
169666a3af9SDaniel P. Berrange  * @ioc: the channel object
170666a3af9SDaniel P. Berrange  * @feature: the feature to check support of
171666a3af9SDaniel P. Berrange  *
172666a3af9SDaniel P. Berrange  * Determine whether the channel implementation supports
173666a3af9SDaniel P. Berrange  * the optional feature named in @feature.
174666a3af9SDaniel P. Berrange  *
175666a3af9SDaniel P. Berrange  * Returns: true if supported, false otherwise.
176666a3af9SDaniel P. Berrange  */
177666a3af9SDaniel P. Berrange bool qio_channel_has_feature(QIOChannel *ioc,
178666a3af9SDaniel P. Berrange                              QIOChannelFeature feature);
179666a3af9SDaniel P. Berrange 
180666a3af9SDaniel P. Berrange /**
181d8d3c7ccSFelipe Franciosi  * qio_channel_set_feature:
182d8d3c7ccSFelipe Franciosi  * @ioc: the channel object
183d8d3c7ccSFelipe Franciosi  * @feature: the feature to set support for
184d8d3c7ccSFelipe Franciosi  *
185d8d3c7ccSFelipe Franciosi  * Add channel support for the feature named in @feature.
186d8d3c7ccSFelipe Franciosi  */
187d8d3c7ccSFelipe Franciosi void qio_channel_set_feature(QIOChannel *ioc,
188d8d3c7ccSFelipe Franciosi                              QIOChannelFeature feature);
189d8d3c7ccSFelipe Franciosi 
190d8d3c7ccSFelipe Franciosi /**
19120f4aa26SDaniel P. Berrange  * qio_channel_set_name:
19220f4aa26SDaniel P. Berrange  * @ioc: the channel object
19320f4aa26SDaniel P. Berrange  * @name: the name of the channel
19420f4aa26SDaniel P. Berrange  *
19520f4aa26SDaniel P. Berrange  * Sets the name of the channel, which serves as an aid
19620f4aa26SDaniel P. Berrange  * to debugging. The name is used when creating GSource
19720f4aa26SDaniel P. Berrange  * watches for this channel.
19820f4aa26SDaniel P. Berrange  */
19920f4aa26SDaniel P. Berrange void qio_channel_set_name(QIOChannel *ioc,
20020f4aa26SDaniel P. Berrange                           const char *name);
20120f4aa26SDaniel P. Berrange 
20220f4aa26SDaniel P. Berrange /**
203666a3af9SDaniel P. Berrange  * qio_channel_readv_full:
204666a3af9SDaniel P. Berrange  * @ioc: the channel object
205666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
206666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
207666a3af9SDaniel P. Berrange  * @fds: pointer to an array that will received file handles
208666a3af9SDaniel P. Berrange  * @nfds: pointer filled with number of elements in @fds on return
20984615a19Smanish.mishra  * @flags: read flags (QIO_CHANNEL_READ_FLAG_*)
210821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
211666a3af9SDaniel P. Berrange  *
212666a3af9SDaniel P. Berrange  * Read data from the IO channel, storing it in the
213666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
214666a3af9SDaniel P. Berrange  * in the @iov will be fully populated with data
215666a3af9SDaniel P. Berrange  * before the next one is used. The @niov parameter
216666a3af9SDaniel P. Berrange  * specifies the total number of elements in @iov.
217666a3af9SDaniel P. Berrange  *
218666a3af9SDaniel P. Berrange  * It is not required for all @iov to be filled with
219666a3af9SDaniel P. Berrange  * data. If the channel is in blocking mode, at least
220666a3af9SDaniel P. Berrange  * one byte of data will be read, but no more is
221666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
222666a3af9SDaniel P. Berrange  * data is available, it will return QIO_CHANNEL_ERR_BLOCK
223666a3af9SDaniel P. Berrange  *
224666a3af9SDaniel P. Berrange  * If the channel has passed any file descriptors,
225666a3af9SDaniel P. Berrange  * the @fds array pointer will be allocated and
226666a3af9SDaniel P. Berrange  * the elements filled with the received file
227666a3af9SDaniel P. Berrange  * descriptors. The @nfds pointer will be updated
228666a3af9SDaniel P. Berrange  * to indicate the size of the @fds array that
229666a3af9SDaniel P. Berrange  * was allocated. It is the callers responsibility
230666a3af9SDaniel P. Berrange  * to call close() on each file descriptor and to
231666a3af9SDaniel P. Berrange  * call g_free() on the array pointer in @fds.
232666a3af9SDaniel P. Berrange  *
233666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
234666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
235666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
236666a3af9SDaniel P. Berrange  *
237666a3af9SDaniel P. Berrange  * Returns: the number of bytes read, or -1 on error,
238666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is available
239666a3af9SDaniel P. Berrange  * and the channel is non-blocking
240666a3af9SDaniel P. Berrange  */
241666a3af9SDaniel P. Berrange ssize_t qio_channel_readv_full(QIOChannel *ioc,
242666a3af9SDaniel P. Berrange                                const struct iovec *iov,
243666a3af9SDaniel P. Berrange                                size_t niov,
244666a3af9SDaniel P. Berrange                                int **fds,
245666a3af9SDaniel P. Berrange                                size_t *nfds,
24684615a19Smanish.mishra                                int flags,
247666a3af9SDaniel P. Berrange                                Error **errp);
248666a3af9SDaniel P. Berrange 
249666a3af9SDaniel P. Berrange 
250666a3af9SDaniel P. Berrange /**
251666a3af9SDaniel P. Berrange  * qio_channel_writev_full:
252666a3af9SDaniel P. Berrange  * @ioc: the channel object
253666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
254666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
255666a3af9SDaniel P. Berrange  * @fds: an array of file handles to send
256666a3af9SDaniel P. Berrange  * @nfds: number of file handles in @fds
257b88651cbSLeonardo Bras  * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
258821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
259666a3af9SDaniel P. Berrange  *
260666a3af9SDaniel P. Berrange  * Write data to the IO channel, reading it from the
261666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
262666a3af9SDaniel P. Berrange  * in the @iov will be fully sent, before the next
263666a3af9SDaniel P. Berrange  * one is used. The @niov parameter specifies the
264666a3af9SDaniel P. Berrange  * total number of elements in @iov.
265666a3af9SDaniel P. Berrange  *
266666a3af9SDaniel P. Berrange  * It is not required for all @iov data to be fully
267666a3af9SDaniel P. Berrange  * sent. If the channel is in blocking mode, at least
268666a3af9SDaniel P. Berrange  * one byte of data will be sent, but no more is
269666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
270666a3af9SDaniel P. Berrange  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
271666a3af9SDaniel P. Berrange  *
272666a3af9SDaniel P. Berrange  * If there are file descriptors to send, the @fds
273666a3af9SDaniel P. Berrange  * array should be non-NULL and provide the handles.
274666a3af9SDaniel P. Berrange  * All file descriptors will be sent if at least one
275666a3af9SDaniel P. Berrange  * byte of data was sent.
276666a3af9SDaniel P. Berrange  *
277666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
278666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
279666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
280666a3af9SDaniel P. Berrange  *
281666a3af9SDaniel P. Berrange  * Returns: the number of bytes sent, or -1 on error,
282666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
283666a3af9SDaniel P. Berrange  * and the channel is non-blocking
284666a3af9SDaniel P. Berrange  */
285666a3af9SDaniel P. Berrange ssize_t qio_channel_writev_full(QIOChannel *ioc,
286666a3af9SDaniel P. Berrange                                 const struct iovec *iov,
287666a3af9SDaniel P. Berrange                                 size_t niov,
288666a3af9SDaniel P. Berrange                                 int *fds,
289666a3af9SDaniel P. Berrange                                 size_t nfds,
290b88651cbSLeonardo Bras                                 int flags,
291666a3af9SDaniel P. Berrange                                 Error **errp);
292666a3af9SDaniel P. Berrange 
293666a3af9SDaniel P. Berrange /**
294e8ffaa31SEric Blake  * qio_channel_readv_all_eof:
295e8ffaa31SEric Blake  * @ioc: the channel object
296e8ffaa31SEric Blake  * @iov: the array of memory regions to read data into
297e8ffaa31SEric Blake  * @niov: the length of the @iov array
298e8ffaa31SEric Blake  * @errp: pointer to a NULL-initialized error object
299e8ffaa31SEric Blake  *
300e8ffaa31SEric Blake  * Read data from the IO channel, storing it in the
301e8ffaa31SEric Blake  * memory regions referenced by @iov. Each element
302e8ffaa31SEric Blake  * in the @iov will be fully populated with data
303e8ffaa31SEric Blake  * before the next one is used. The @niov parameter
304e8ffaa31SEric Blake  * specifies the total number of elements in @iov.
305e8ffaa31SEric Blake  *
306e8ffaa31SEric Blake  * The function will wait for all requested data
307e8ffaa31SEric Blake  * to be read, yielding from the current coroutine
308e8ffaa31SEric Blake  * if required.
309e8ffaa31SEric Blake  *
310e8ffaa31SEric Blake  * If end-of-file occurs before any data is read,
311e8ffaa31SEric Blake  * no error is reported; otherwise, if it occurs
312e8ffaa31SEric Blake  * before all requested data has been read, an error
313e8ffaa31SEric Blake  * will be reported.
314e8ffaa31SEric Blake  *
315e8ffaa31SEric Blake  * Returns: 1 if all bytes were read, 0 if end-of-file
316e8ffaa31SEric Blake  *          occurs without data, or -1 on error
317e8ffaa31SEric Blake  */
3181dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_all_eof(QIOChannel *ioc,
319e8ffaa31SEric Blake                                                  const struct iovec *iov,
320e8ffaa31SEric Blake                                                  size_t niov,
321e8ffaa31SEric Blake                                                  Error **errp);
322e8ffaa31SEric Blake 
323e8ffaa31SEric Blake /**
324d4622e55SDaniel P. Berrange  * qio_channel_readv_all:
325d4622e55SDaniel P. Berrange  * @ioc: the channel object
326d4622e55SDaniel P. Berrange  * @iov: the array of memory regions to read data into
327d4622e55SDaniel P. Berrange  * @niov: the length of the @iov array
328d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
329d4622e55SDaniel P. Berrange  *
330d4622e55SDaniel P. Berrange  * Read data from the IO channel, storing it in the
331d4622e55SDaniel P. Berrange  * memory regions referenced by @iov. Each element
332d4622e55SDaniel P. Berrange  * in the @iov will be fully populated with data
333d4622e55SDaniel P. Berrange  * before the next one is used. The @niov parameter
334d4622e55SDaniel P. Berrange  * specifies the total number of elements in @iov.
335d4622e55SDaniel P. Berrange  *
336d4622e55SDaniel P. Berrange  * The function will wait for all requested data
337d4622e55SDaniel P. Berrange  * to be read, yielding from the current coroutine
338d4622e55SDaniel P. Berrange  * if required.
339d4622e55SDaniel P. Berrange  *
340d4622e55SDaniel P. Berrange  * If end-of-file occurs before all requested data
341d4622e55SDaniel P. Berrange  * has been read, an error will be reported.
342d4622e55SDaniel P. Berrange  *
343d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were read, or -1 on error
344d4622e55SDaniel P. Berrange  */
3451dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_all(QIOChannel *ioc,
346d4622e55SDaniel P. Berrange                                              const struct iovec *iov,
347d4622e55SDaniel P. Berrange                                              size_t niov,
348d4622e55SDaniel P. Berrange                                              Error **errp);
349d4622e55SDaniel P. Berrange 
350d4622e55SDaniel P. Berrange 
351d4622e55SDaniel P. Berrange /**
352d4622e55SDaniel P. Berrange  * qio_channel_writev_all:
353d4622e55SDaniel P. Berrange  * @ioc: the channel object
354d4622e55SDaniel P. Berrange  * @iov: the array of memory regions to write data from
355d4622e55SDaniel P. Berrange  * @niov: the length of the @iov array
356d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
357d4622e55SDaniel P. Berrange  *
358d4622e55SDaniel P. Berrange  * Write data to the IO channel, reading it from the
359d4622e55SDaniel P. Berrange  * memory regions referenced by @iov. Each element
360d4622e55SDaniel P. Berrange  * in the @iov will be fully sent, before the next
361d4622e55SDaniel P. Berrange  * one is used. The @niov parameter specifies the
362d4622e55SDaniel P. Berrange  * total number of elements in @iov.
363d4622e55SDaniel P. Berrange  *
364d4622e55SDaniel P. Berrange  * The function will wait for all requested data
365d4622e55SDaniel P. Berrange  * to be written, yielding from the current coroutine
366d4622e55SDaniel P. Berrange  * if required.
367d4622e55SDaniel P. Berrange  *
368d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were written, or -1 on error
369d4622e55SDaniel P. Berrange  */
3701dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_writev_all(QIOChannel *ioc,
371d4622e55SDaniel P. Berrange                                               const struct iovec *iov,
372d4622e55SDaniel P. Berrange                                               size_t niov,
37310220d2fSMarkus Armbruster                                               Error **errp);
374d4622e55SDaniel P. Berrange 
375d4622e55SDaniel P. Berrange /**
376666a3af9SDaniel P. Berrange  * qio_channel_readv:
377666a3af9SDaniel P. Berrange  * @ioc: the channel object
378666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
379666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
380821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
381666a3af9SDaniel P. Berrange  *
382666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
383666a3af9SDaniel P. Berrange  * receiving of file handles.
384666a3af9SDaniel P. Berrange  */
385666a3af9SDaniel P. Berrange ssize_t qio_channel_readv(QIOChannel *ioc,
386666a3af9SDaniel P. Berrange                           const struct iovec *iov,
387666a3af9SDaniel P. Berrange                           size_t niov,
388666a3af9SDaniel P. Berrange                           Error **errp);
389666a3af9SDaniel P. Berrange 
390666a3af9SDaniel P. Berrange /**
391666a3af9SDaniel P. Berrange  * qio_channel_writev:
392666a3af9SDaniel P. Berrange  * @ioc: the channel object
393666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
394666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
395821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
396666a3af9SDaniel P. Berrange  *
397666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
398666a3af9SDaniel P. Berrange  * sending of file handles.
399666a3af9SDaniel P. Berrange  */
400666a3af9SDaniel P. Berrange ssize_t qio_channel_writev(QIOChannel *ioc,
401666a3af9SDaniel P. Berrange                            const struct iovec *iov,
402666a3af9SDaniel P. Berrange                            size_t niov,
403666a3af9SDaniel P. Berrange                            Error **errp);
404666a3af9SDaniel P. Berrange 
405666a3af9SDaniel P. Berrange /**
40650ea44f0SDaniel P. Berrange  * qio_channel_read:
407666a3af9SDaniel P. Berrange  * @ioc: the channel object
408666a3af9SDaniel P. Berrange  * @buf: the memory region to read data into
409666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
410821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
411666a3af9SDaniel P. Berrange  *
412666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
413666a3af9SDaniel P. Berrange  * receiving of file handles, and only supports reading into
414666a3af9SDaniel P. Berrange  * a single memory region.
415666a3af9SDaniel P. Berrange  */
416666a3af9SDaniel P. Berrange ssize_t qio_channel_read(QIOChannel *ioc,
417666a3af9SDaniel P. Berrange                          char *buf,
418666a3af9SDaniel P. Berrange                          size_t buflen,
419666a3af9SDaniel P. Berrange                          Error **errp);
420666a3af9SDaniel P. Berrange 
421666a3af9SDaniel P. Berrange /**
42261f7c6a0SMarc-André Lureau  * qio_channel_write:
423666a3af9SDaniel P. Berrange  * @ioc: the channel object
424666a3af9SDaniel P. Berrange  * @buf: the memory regions to send data from
425666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
426821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
427666a3af9SDaniel P. Berrange  *
428666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
429666a3af9SDaniel P. Berrange  * sending of file handles, and only supports writing from a
430666a3af9SDaniel P. Berrange  * single memory region.
431666a3af9SDaniel P. Berrange  */
432666a3af9SDaniel P. Berrange ssize_t qio_channel_write(QIOChannel *ioc,
433666a3af9SDaniel P. Berrange                           const char *buf,
434666a3af9SDaniel P. Berrange                           size_t buflen,
435666a3af9SDaniel P. Berrange                           Error **errp);
436666a3af9SDaniel P. Berrange 
437666a3af9SDaniel P. Berrange /**
438e8ffaa31SEric Blake  * qio_channel_read_all_eof:
439e8ffaa31SEric Blake  * @ioc: the channel object
440e8ffaa31SEric Blake  * @buf: the memory region to read data into
441e8ffaa31SEric Blake  * @buflen: the number of bytes to @buf
442e8ffaa31SEric Blake  * @errp: pointer to a NULL-initialized error object
443e8ffaa31SEric Blake  *
444e8ffaa31SEric Blake  * Reads @buflen bytes into @buf, possibly blocking or (if the
445e8ffaa31SEric Blake  * channel is non-blocking) yielding from the current coroutine
446e8ffaa31SEric Blake  * multiple times until the entire content is read. If end-of-file
447e8ffaa31SEric Blake  * occurs immediately it is not an error, but if it occurs after
448e8ffaa31SEric Blake  * data has been read it will return an error rather than a
449e8ffaa31SEric Blake  * short-read. Otherwise behaves as qio_channel_read().
450e8ffaa31SEric Blake  *
451e8ffaa31SEric Blake  * Returns: 1 if all bytes were read, 0 if end-of-file occurs
452e8ffaa31SEric Blake  *          without data, or -1 on error
453e8ffaa31SEric Blake  */
4541dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_read_all_eof(QIOChannel *ioc,
455e8ffaa31SEric Blake                                                 char *buf,
456e8ffaa31SEric Blake                                                 size_t buflen,
457e8ffaa31SEric Blake                                                 Error **errp);
458e8ffaa31SEric Blake 
459e8ffaa31SEric Blake /**
460d4622e55SDaniel P. Berrange  * qio_channel_read_all:
461d4622e55SDaniel P. Berrange  * @ioc: the channel object
462d4622e55SDaniel P. Berrange  * @buf: the memory region to read data into
463d4622e55SDaniel P. Berrange  * @buflen: the number of bytes to @buf
464d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
465d4622e55SDaniel P. Berrange  *
466d4622e55SDaniel P. Berrange  * Reads @buflen bytes into @buf, possibly blocking or (if the
467d4622e55SDaniel P. Berrange  * channel is non-blocking) yielding from the current coroutine
468d4622e55SDaniel P. Berrange  * multiple times until the entire content is read. If end-of-file
469d4622e55SDaniel P. Berrange  * occurs it will return an error rather than a short-read. Otherwise
470d4622e55SDaniel P. Berrange  * behaves as qio_channel_read().
471d4622e55SDaniel P. Berrange  *
472d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were read, or -1 on error
473d4622e55SDaniel P. Berrange  */
4741dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_read_all(QIOChannel *ioc,
475d4622e55SDaniel P. Berrange                                             char *buf,
476d4622e55SDaniel P. Berrange                                             size_t buflen,
477d4622e55SDaniel P. Berrange                                             Error **errp);
478e8ffaa31SEric Blake 
479d4622e55SDaniel P. Berrange /**
480d4622e55SDaniel P. Berrange  * qio_channel_write_all:
481d4622e55SDaniel P. Berrange  * @ioc: the channel object
482d4622e55SDaniel P. Berrange  * @buf: the memory region to write data into
483d4622e55SDaniel P. Berrange  * @buflen: the number of bytes to @buf
484d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
485d4622e55SDaniel P. Berrange  *
486d4622e55SDaniel P. Berrange  * Writes @buflen bytes from @buf, possibly blocking or (if the
487d4622e55SDaniel P. Berrange  * channel is non-blocking) yielding from the current coroutine
488d4622e55SDaniel P. Berrange  * multiple times until the entire content is written.  Otherwise
489d4622e55SDaniel P. Berrange  * behaves as qio_channel_write().
490d4622e55SDaniel P. Berrange  *
491d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were written, or -1 on error
492d4622e55SDaniel P. Berrange  */
4931dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_write_all(QIOChannel *ioc,
494d4622e55SDaniel P. Berrange                                              const char *buf,
495d4622e55SDaniel P. Berrange                                              size_t buflen,
496d4622e55SDaniel P. Berrange                                              Error **errp);
497d4622e55SDaniel P. Berrange 
498d4622e55SDaniel P. Berrange /**
499666a3af9SDaniel P. Berrange  * qio_channel_set_blocking:
500666a3af9SDaniel P. Berrange  * @ioc: the channel object
501666a3af9SDaniel P. Berrange  * @enabled: the blocking flag state
502821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
503666a3af9SDaniel P. Berrange  *
504666a3af9SDaniel P. Berrange  * If @enabled is true, then the channel is put into
505666a3af9SDaniel P. Berrange  * blocking mode, otherwise it will be non-blocking.
506666a3af9SDaniel P. Berrange  *
507666a3af9SDaniel P. Berrange  * In non-blocking mode, read/write operations may
508666a3af9SDaniel P. Berrange  * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
509666a3af9SDaniel P. Berrange  * block on I/O
510666a3af9SDaniel P. Berrange  */
511666a3af9SDaniel P. Berrange int qio_channel_set_blocking(QIOChannel *ioc,
512666a3af9SDaniel P. Berrange                              bool enabled,
513666a3af9SDaniel P. Berrange                              Error **errp);
514666a3af9SDaniel P. Berrange 
515666a3af9SDaniel P. Berrange /**
51606e0f098SStefan Hajnoczi  * qio_channel_set_follow_coroutine_ctx:
51706e0f098SStefan Hajnoczi  * @ioc: the channel object
51806e0f098SStefan Hajnoczi  * @enabled: whether or not to follow the coroutine's AioContext
51906e0f098SStefan Hajnoczi  *
52006e0f098SStefan Hajnoczi  * If @enabled is true, calls to qio_channel_yield() use the current
52106e0f098SStefan Hajnoczi  * coroutine's AioContext. Usually this is desirable.
52206e0f098SStefan Hajnoczi  *
52306e0f098SStefan Hajnoczi  * If @enabled is false, calls to qio_channel_yield() use the global iohandler
52406e0f098SStefan Hajnoczi  * AioContext. This is may be used by coroutines that run in the main loop and
52506e0f098SStefan Hajnoczi  * do not wish to respond to I/O during nested event loops. This is the
52606e0f098SStefan Hajnoczi  * default for compatibility with code that is not aware of AioContexts.
52706e0f098SStefan Hajnoczi  */
52806e0f098SStefan Hajnoczi void qio_channel_set_follow_coroutine_ctx(QIOChannel *ioc, bool enabled);
52906e0f098SStefan Hajnoczi 
53006e0f098SStefan Hajnoczi /**
531666a3af9SDaniel P. Berrange  * qio_channel_close:
532666a3af9SDaniel P. Berrange  * @ioc: the channel object
533821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
534666a3af9SDaniel P. Berrange  *
535666a3af9SDaniel P. Berrange  * Close the channel, flushing any pending I/O
536666a3af9SDaniel P. Berrange  *
537666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
538666a3af9SDaniel P. Berrange  */
539666a3af9SDaniel P. Berrange int qio_channel_close(QIOChannel *ioc,
540666a3af9SDaniel P. Berrange                       Error **errp);
541666a3af9SDaniel P. Berrange 
542666a3af9SDaniel P. Berrange /**
543*f1cfe394SNikolay Borisov  * qio_channel_pwritev
544*f1cfe394SNikolay Borisov  * @ioc: the channel object
545*f1cfe394SNikolay Borisov  * @iov: the array of memory regions to write data from
546*f1cfe394SNikolay Borisov  * @niov: the length of the @iov array
547*f1cfe394SNikolay Borisov  * @offset: offset in the channel where writes should begin
548*f1cfe394SNikolay Borisov  * @errp: pointer to a NULL-initialized error object
549*f1cfe394SNikolay Borisov  *
550*f1cfe394SNikolay Borisov  * Not all implementations will support this facility, so may report
551*f1cfe394SNikolay Borisov  * an error. To avoid errors, the caller may check for the feature
552*f1cfe394SNikolay Borisov  * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method.
553*f1cfe394SNikolay Borisov  *
554*f1cfe394SNikolay Borisov  * Behaves as qio_channel_writev_full, apart from not supporting
555*f1cfe394SNikolay Borisov  * sending of file handles as well as beginning the write at the
556*f1cfe394SNikolay Borisov  * passed @offset
557*f1cfe394SNikolay Borisov  *
558*f1cfe394SNikolay Borisov  */
559*f1cfe394SNikolay Borisov ssize_t qio_channel_pwritev(QIOChannel *ioc, const struct iovec *iov,
560*f1cfe394SNikolay Borisov                             size_t niov, off_t offset, Error **errp);
561*f1cfe394SNikolay Borisov 
562*f1cfe394SNikolay Borisov /**
563*f1cfe394SNikolay Borisov  * qio_channel_pwrite
564*f1cfe394SNikolay Borisov  * @ioc: the channel object
565*f1cfe394SNikolay Borisov  * @buf: the memory region to write data into
566*f1cfe394SNikolay Borisov  * @buflen: the number of bytes to @buf
567*f1cfe394SNikolay Borisov  * @offset: offset in the channel where writes should begin
568*f1cfe394SNikolay Borisov  * @errp: pointer to a NULL-initialized error object
569*f1cfe394SNikolay Borisov  *
570*f1cfe394SNikolay Borisov  * Not all implementations will support this facility, so may report
571*f1cfe394SNikolay Borisov  * an error. To avoid errors, the caller may check for the feature
572*f1cfe394SNikolay Borisov  * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method.
573*f1cfe394SNikolay Borisov  *
574*f1cfe394SNikolay Borisov  */
575*f1cfe394SNikolay Borisov ssize_t qio_channel_pwrite(QIOChannel *ioc, char *buf, size_t buflen,
576*f1cfe394SNikolay Borisov                            off_t offset, Error **errp);
577*f1cfe394SNikolay Borisov 
578*f1cfe394SNikolay Borisov /**
579*f1cfe394SNikolay Borisov  * qio_channel_preadv
580*f1cfe394SNikolay Borisov  * @ioc: the channel object
581*f1cfe394SNikolay Borisov  * @iov: the array of memory regions to read data into
582*f1cfe394SNikolay Borisov  * @niov: the length of the @iov array
583*f1cfe394SNikolay Borisov  * @offset: offset in the channel where writes should begin
584*f1cfe394SNikolay Borisov  * @errp: pointer to a NULL-initialized error object
585*f1cfe394SNikolay Borisov  *
586*f1cfe394SNikolay Borisov  * Not all implementations will support this facility, so may report
587*f1cfe394SNikolay Borisov  * an error.  To avoid errors, the caller may check for the feature
588*f1cfe394SNikolay Borisov  * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method.
589*f1cfe394SNikolay Borisov  *
590*f1cfe394SNikolay Borisov  * Behaves as qio_channel_readv_full, apart from not supporting
591*f1cfe394SNikolay Borisov  * receiving of file handles as well as beginning the read at the
592*f1cfe394SNikolay Borisov  * passed @offset
593*f1cfe394SNikolay Borisov  *
594*f1cfe394SNikolay Borisov  */
595*f1cfe394SNikolay Borisov ssize_t qio_channel_preadv(QIOChannel *ioc, const struct iovec *iov,
596*f1cfe394SNikolay Borisov                            size_t niov, off_t offset, Error **errp);
597*f1cfe394SNikolay Borisov 
598*f1cfe394SNikolay Borisov /**
599*f1cfe394SNikolay Borisov  * qio_channel_pread
600*f1cfe394SNikolay Borisov  * @ioc: the channel object
601*f1cfe394SNikolay Borisov  * @buf: the memory region to write data into
602*f1cfe394SNikolay Borisov  * @buflen: the number of bytes to @buf
603*f1cfe394SNikolay Borisov  * @offset: offset in the channel where writes should begin
604*f1cfe394SNikolay Borisov  * @errp: pointer to a NULL-initialized error object
605*f1cfe394SNikolay Borisov  *
606*f1cfe394SNikolay Borisov  * Not all implementations will support this facility, so may report
607*f1cfe394SNikolay Borisov  * an error.  To avoid errors, the caller may check for the feature
608*f1cfe394SNikolay Borisov  * flag QIO_CHANNEL_FEATURE_SEEKABLE prior to calling this method.
609*f1cfe394SNikolay Borisov  *
610*f1cfe394SNikolay Borisov  */
611*f1cfe394SNikolay Borisov ssize_t qio_channel_pread(QIOChannel *ioc, char *buf, size_t buflen,
612*f1cfe394SNikolay Borisov                           off_t offset, Error **errp);
613*f1cfe394SNikolay Borisov 
614*f1cfe394SNikolay Borisov /**
615666a3af9SDaniel P. Berrange  * qio_channel_shutdown:
616666a3af9SDaniel P. Berrange  * @ioc: the channel object
617666a3af9SDaniel P. Berrange  * @how: the direction to shutdown
618821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
619666a3af9SDaniel P. Berrange  *
620666a3af9SDaniel P. Berrange  * Shutdowns transmission and/or receiving of data
621666a3af9SDaniel P. Berrange  * without closing the underlying transport.
622666a3af9SDaniel P. Berrange  *
623666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
624666a3af9SDaniel P. Berrange  * so may report an error. To avoid errors, the
625666a3af9SDaniel P. Berrange  * caller may check for the feature flag
626666a3af9SDaniel P. Berrange  * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling
627666a3af9SDaniel P. Berrange  * this method.
628666a3af9SDaniel P. Berrange  *
6298659f317SLukas Straub  * This function is thread-safe, terminates quickly and does not block.
6308659f317SLukas Straub  *
631666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
632666a3af9SDaniel P. Berrange  */
633666a3af9SDaniel P. Berrange int qio_channel_shutdown(QIOChannel *ioc,
634666a3af9SDaniel P. Berrange                          QIOChannelShutdown how,
635666a3af9SDaniel P. Berrange                          Error **errp);
636666a3af9SDaniel P. Berrange 
637666a3af9SDaniel P. Berrange /**
638666a3af9SDaniel P. Berrange  * qio_channel_set_delay:
639666a3af9SDaniel P. Berrange  * @ioc: the channel object
640666a3af9SDaniel P. Berrange  * @enabled: the new flag state
641666a3af9SDaniel P. Berrange  *
642666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
643666a3af9SDaniel P. Berrange  * permitted to delay writes in order to merge
644666a3af9SDaniel P. Berrange  * small packets. If @enabled is true, then the
645666a3af9SDaniel P. Berrange  * writes may be delayed in order to opportunistically
646666a3af9SDaniel P. Berrange  * merge small packets into larger ones. If @enabled
647666a3af9SDaniel P. Berrange  * is false, writes are dispatched immediately with
648666a3af9SDaniel P. Berrange  * no delay.
649666a3af9SDaniel P. Berrange  *
650666a3af9SDaniel P. Berrange  * When @enabled is false, applications may wish to
651666a3af9SDaniel P. Berrange  * use the qio_channel_set_cork() method to explicitly
652666a3af9SDaniel P. Berrange  * control write merging.
653666a3af9SDaniel P. Berrange  *
654666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
655666a3af9SDaniel P. Berrange  * API corresponds to the inverse of TCP_NODELAY flag,
656666a3af9SDaniel P. Berrange  * controlling whether the Nagle algorithm is active.
657666a3af9SDaniel P. Berrange  *
658666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
659666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
660666a3af9SDaniel P. Berrange  * error.
661666a3af9SDaniel P. Berrange  */
662666a3af9SDaniel P. Berrange void qio_channel_set_delay(QIOChannel *ioc,
663666a3af9SDaniel P. Berrange                            bool enabled);
664666a3af9SDaniel P. Berrange 
665666a3af9SDaniel P. Berrange /**
666666a3af9SDaniel P. Berrange  * qio_channel_set_cork:
667666a3af9SDaniel P. Berrange  * @ioc: the channel object
668666a3af9SDaniel P. Berrange  * @enabled: the new flag state
669666a3af9SDaniel P. Berrange  *
670666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
671666a3af9SDaniel P. Berrange  * permitted to dispatch data that is written.
672666a3af9SDaniel P. Berrange  * If @enabled is true, then any data written will
673666a3af9SDaniel P. Berrange  * be queued in local buffers until @enabled is
674666a3af9SDaniel P. Berrange  * set to false once again.
675666a3af9SDaniel P. Berrange  *
676666a3af9SDaniel P. Berrange  * This feature is typically used when the automatic
677666a3af9SDaniel P. Berrange  * write coalescing facility is disabled via the
678666a3af9SDaniel P. Berrange  * qio_channel_set_delay() method.
679666a3af9SDaniel P. Berrange  *
680666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
681666a3af9SDaniel P. Berrange  * API corresponds to the TCP_CORK flag.
682666a3af9SDaniel P. Berrange  *
683666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
684666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
685666a3af9SDaniel P. Berrange  * error.
686666a3af9SDaniel P. Berrange  */
687666a3af9SDaniel P. Berrange void qio_channel_set_cork(QIOChannel *ioc,
688666a3af9SDaniel P. Berrange                           bool enabled);
689666a3af9SDaniel P. Berrange 
690666a3af9SDaniel P. Berrange 
691666a3af9SDaniel P. Berrange /**
692666a3af9SDaniel P. Berrange  * qio_channel_seek:
693666a3af9SDaniel P. Berrange  * @ioc: the channel object
694666a3af9SDaniel P. Berrange  * @offset: the position to seek to, relative to @whence
695666a3af9SDaniel P. Berrange  * @whence: one of the (POSIX) SEEK_* constants listed below
696821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
697666a3af9SDaniel P. Berrange  *
698666a3af9SDaniel P. Berrange  * Moves the current I/O position within the channel
699666a3af9SDaniel P. Berrange  * @ioc, to be @offset. The value of @offset is
700666a3af9SDaniel P. Berrange  * interpreted relative to @whence:
701666a3af9SDaniel P. Berrange  *
702666a3af9SDaniel P. Berrange  * SEEK_SET - the position is set to @offset bytes
703666a3af9SDaniel P. Berrange  * SEEK_CUR - the position is moved by @offset bytes
704666a3af9SDaniel P. Berrange  * SEEK_END - the position is set to end of the file plus @offset bytes
705666a3af9SDaniel P. Berrange  *
706666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
707666a3af9SDaniel P. Berrange  * so may report an error.
708666a3af9SDaniel P. Berrange  *
709666a3af9SDaniel P. Berrange  * Returns: the new position on success, (off_t)-1 on failure
710666a3af9SDaniel P. Berrange  */
711666a3af9SDaniel P. Berrange off_t qio_channel_io_seek(QIOChannel *ioc,
712666a3af9SDaniel P. Berrange                           off_t offset,
713666a3af9SDaniel P. Berrange                           int whence,
714666a3af9SDaniel P. Berrange                           Error **errp);
715666a3af9SDaniel P. Berrange 
716666a3af9SDaniel P. Berrange 
717666a3af9SDaniel P. Berrange /**
718666a3af9SDaniel P. Berrange  * qio_channel_create_watch:
719666a3af9SDaniel P. Berrange  * @ioc: the channel object
720666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
721666a3af9SDaniel P. Berrange  *
722666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
723666a3af9SDaniel P. Berrange  * for the I/O condition @condition. Typically the
724666a3af9SDaniel P. Berrange  * qio_channel_add_watch() method would be used instead
725666a3af9SDaniel P. Berrange  * of this, since it directly attaches a callback to
726666a3af9SDaniel P. Berrange  * the source
727666a3af9SDaniel P. Berrange  *
728666a3af9SDaniel P. Berrange  * Returns: the new main loop source.
729666a3af9SDaniel P. Berrange  */
730666a3af9SDaniel P. Berrange GSource *qio_channel_create_watch(QIOChannel *ioc,
731666a3af9SDaniel P. Berrange                                   GIOCondition condition);
732666a3af9SDaniel P. Berrange 
733666a3af9SDaniel P. Berrange /**
734666a3af9SDaniel P. Berrange  * qio_channel_add_watch:
735666a3af9SDaniel P. Berrange  * @ioc: the channel object
736666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
737666a3af9SDaniel P. Berrange  * @func: callback to invoke when the source becomes ready
738666a3af9SDaniel P. Berrange  * @user_data: opaque data to pass to @func
739666a3af9SDaniel P. Berrange  * @notify: callback to free @user_data
740666a3af9SDaniel P. Berrange  *
741666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
742666a3af9SDaniel P. Berrange  * for the I/O condition @condition. The callback @func
743666a3af9SDaniel P. Berrange  * will be registered against the source, to be invoked
744666a3af9SDaniel P. Berrange  * when the source becomes ready. The optional @user_data
745666a3af9SDaniel P. Berrange  * will be passed to @func when it is invoked. The @notify
746666a3af9SDaniel P. Berrange  * callback will be used to free @user_data when the
747666a3af9SDaniel P. Berrange  * watch is deleted
748666a3af9SDaniel P. Berrange  *
749666a3af9SDaniel P. Berrange  * The returned source ID can be used with g_source_remove()
750666a3af9SDaniel P. Berrange  * to remove and free the source when no longer required.
751666a3af9SDaniel P. Berrange  * Alternatively the @func callback can return a FALSE
752666a3af9SDaniel P. Berrange  * value.
753666a3af9SDaniel P. Berrange  *
754666a3af9SDaniel P. Berrange  * Returns: the source ID
755666a3af9SDaniel P. Berrange  */
756666a3af9SDaniel P. Berrange guint qio_channel_add_watch(QIOChannel *ioc,
757666a3af9SDaniel P. Berrange                             GIOCondition condition,
758666a3af9SDaniel P. Berrange                             QIOChannelFunc func,
759666a3af9SDaniel P. Berrange                             gpointer user_data,
760666a3af9SDaniel P. Berrange                             GDestroyNotify notify);
761666a3af9SDaniel P. Berrange 
762315409c7SPeter Xu /**
763315409c7SPeter Xu  * qio_channel_add_watch_full:
764315409c7SPeter Xu  * @ioc: the channel object
765315409c7SPeter Xu  * @condition: the I/O condition to monitor
766315409c7SPeter Xu  * @func: callback to invoke when the source becomes ready
767315409c7SPeter Xu  * @user_data: opaque data to pass to @func
768315409c7SPeter Xu  * @notify: callback to free @user_data
769315409c7SPeter Xu  * @context: the context to run the watch source
770315409c7SPeter Xu  *
771315409c7SPeter Xu  * Similar as qio_channel_add_watch(), but allows to specify context
772315409c7SPeter Xu  * to run the watch source.
773315409c7SPeter Xu  *
774315409c7SPeter Xu  * Returns: the source ID
775315409c7SPeter Xu  */
776315409c7SPeter Xu guint qio_channel_add_watch_full(QIOChannel *ioc,
777315409c7SPeter Xu                                  GIOCondition condition,
778315409c7SPeter Xu                                  QIOChannelFunc func,
779315409c7SPeter Xu                                  gpointer user_data,
780315409c7SPeter Xu                                  GDestroyNotify notify,
781315409c7SPeter Xu                                  GMainContext *context);
782315409c7SPeter Xu 
783315409c7SPeter Xu /**
784315409c7SPeter Xu  * qio_channel_add_watch_source:
785315409c7SPeter Xu  * @ioc: the channel object
786315409c7SPeter Xu  * @condition: the I/O condition to monitor
787315409c7SPeter Xu  * @func: callback to invoke when the source becomes ready
788315409c7SPeter Xu  * @user_data: opaque data to pass to @func
789315409c7SPeter Xu  * @notify: callback to free @user_data
790315409c7SPeter Xu  * @context: gcontext to bind the source to
791315409c7SPeter Xu  *
792315409c7SPeter Xu  * Similar as qio_channel_add_watch(), but allows to specify context
793315409c7SPeter Xu  * to run the watch source, meanwhile return the GSource object
794315409c7SPeter Xu  * instead of tag ID, with the GSource referenced already.
795315409c7SPeter Xu  *
796315409c7SPeter Xu  * Note: callers is responsible to unref the source when not needed.
797315409c7SPeter Xu  *
798315409c7SPeter Xu  * Returns: the source pointer
799315409c7SPeter Xu  */
800315409c7SPeter Xu GSource *qio_channel_add_watch_source(QIOChannel *ioc,
801315409c7SPeter Xu                                       GIOCondition condition,
802315409c7SPeter Xu                                       QIOChannelFunc func,
803315409c7SPeter Xu                                       gpointer user_data,
804315409c7SPeter Xu                                       GDestroyNotify notify,
805315409c7SPeter Xu                                       GMainContext *context);
806666a3af9SDaniel P. Berrange 
807666a3af9SDaniel P. Berrange /**
808666a3af9SDaniel P. Berrange  * qio_channel_yield:
809666a3af9SDaniel P. Berrange  * @ioc: the channel object
810666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
811666a3af9SDaniel P. Berrange  *
812c4c497d2SPaolo Bonzini  * Yields execution from the current coroutine until the condition
813c4c497d2SPaolo Bonzini  * indicated by @condition becomes available.  @condition must
814c4c497d2SPaolo Bonzini  * be either %G_IO_IN or %G_IO_OUT; it cannot contain both.  In
815c4c497d2SPaolo Bonzini  * addition, no two coroutine can be waiting on the same condition
816c4c497d2SPaolo Bonzini  * and channel at the same time.
817666a3af9SDaniel P. Berrange  *
8186886ceafSKevin Wolf  * This must only be called from coroutine context. It is safe to
8196886ceafSKevin Wolf  * reenter the coroutine externally while it is waiting; in this
8206886ceafSKevin Wolf  * case the function will return even if @condition is not yet
8216886ceafSKevin Wolf  * available.
822666a3af9SDaniel P. Berrange  */
8236886ceafSKevin Wolf void coroutine_fn qio_channel_yield(QIOChannel *ioc,
824666a3af9SDaniel P. Berrange                                     GIOCondition condition);
825666a3af9SDaniel P. Berrange 
826666a3af9SDaniel P. Berrange /**
8277c1f51bfSKevin Wolf  * qio_channel_wake_read:
8287c1f51bfSKevin Wolf  * @ioc: the channel object
8297c1f51bfSKevin Wolf  *
8307c1f51bfSKevin Wolf  * If qio_channel_yield() is currently waiting for the channel to become
8317c1f51bfSKevin Wolf  * readable, interrupt it and reenter immediately. This function is safe to call
8327c1f51bfSKevin Wolf  * from any thread.
8337c1f51bfSKevin Wolf  */
8347c1f51bfSKevin Wolf void qio_channel_wake_read(QIOChannel *ioc);
8357c1f51bfSKevin Wolf 
8367c1f51bfSKevin Wolf /**
837666a3af9SDaniel P. Berrange  * qio_channel_wait:
838666a3af9SDaniel P. Berrange  * @ioc: the channel object
839666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
840666a3af9SDaniel P. Berrange  *
841666a3af9SDaniel P. Berrange  * Block execution from the current thread until
842666a3af9SDaniel P. Berrange  * the condition indicated by @condition becomes
843666a3af9SDaniel P. Berrange  * available.
844666a3af9SDaniel P. Berrange  *
845666a3af9SDaniel P. Berrange  * This will enter a nested event loop to perform
846666a3af9SDaniel P. Berrange  * the wait.
847666a3af9SDaniel P. Berrange  */
848666a3af9SDaniel P. Berrange void qio_channel_wait(QIOChannel *ioc,
849666a3af9SDaniel P. Berrange                       GIOCondition condition);
850666a3af9SDaniel P. Berrange 
851bf88c124SPaolo Bonzini /**
852bf88c124SPaolo Bonzini  * qio_channel_set_aio_fd_handler:
853bf88c124SPaolo Bonzini  * @ioc: the channel object
85406e0f098SStefan Hajnoczi  * @read_ctx: the AioContext to set the read handler on or NULL
855bf88c124SPaolo Bonzini  * @io_read: the read handler
85606e0f098SStefan Hajnoczi  * @write_ctx: the AioContext to set the write handler on or NULL
857bf88c124SPaolo Bonzini  * @io_write: the write handler
858bf88c124SPaolo Bonzini  * @opaque: the opaque value passed to the handler
859bf88c124SPaolo Bonzini  *
860bf88c124SPaolo Bonzini  * This is used internally by qio_channel_yield().  It can
861bf88c124SPaolo Bonzini  * be used by channel implementations to forward the handlers
862bf88c124SPaolo Bonzini  * to another channel (e.g. from #QIOChannelTLS to the
863bf88c124SPaolo Bonzini  * underlying socket).
86406e0f098SStefan Hajnoczi  *
86506e0f098SStefan Hajnoczi  * When @read_ctx is NULL, don't touch the read handler. When @write_ctx is
86606e0f098SStefan Hajnoczi  * NULL, don't touch the write handler. Note that setting the read handler
86706e0f098SStefan Hajnoczi  * clears the write handler, and vice versa, if they share the same AioContext.
86806e0f098SStefan Hajnoczi  * Therefore the caller must pass both handlers together when sharing the same
86906e0f098SStefan Hajnoczi  * AioContext.
870bf88c124SPaolo Bonzini  */
871bf88c124SPaolo Bonzini void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
87206e0f098SStefan Hajnoczi                                     AioContext *read_ctx,
873bf88c124SPaolo Bonzini                                     IOHandler *io_read,
87406e0f098SStefan Hajnoczi                                     AioContext *write_ctx,
875bf88c124SPaolo Bonzini                                     IOHandler *io_write,
876bf88c124SPaolo Bonzini                                     void *opaque);
877bf88c124SPaolo Bonzini 
878bfa42387SElena Ufimtseva /**
879bebab91eSElena Ufimtseva  * qio_channel_readv_full_all_eof:
880bebab91eSElena Ufimtseva  * @ioc: the channel object
881bebab91eSElena Ufimtseva  * @iov: the array of memory regions to read data to
882bebab91eSElena Ufimtseva  * @niov: the length of the @iov array
883bebab91eSElena Ufimtseva  * @fds: an array of file handles to read
884bebab91eSElena Ufimtseva  * @nfds: number of file handles in @fds
885bebab91eSElena Ufimtseva  * @errp: pointer to a NULL-initialized error object
886bebab91eSElena Ufimtseva  *
887bebab91eSElena Ufimtseva  *
888bebab91eSElena Ufimtseva  * Performs same function as qio_channel_readv_all_eof.
889bebab91eSElena Ufimtseva  * Additionally, attempts to read file descriptors shared
890bebab91eSElena Ufimtseva  * over the channel. The function will wait for all
891bebab91eSElena Ufimtseva  * requested data to be read, yielding from the current
892bebab91eSElena Ufimtseva  * coroutine if required. data refers to both file
893bebab91eSElena Ufimtseva  * descriptors and the iovs.
894bebab91eSElena Ufimtseva  *
895bebab91eSElena Ufimtseva  * Returns: 1 if all bytes were read, 0 if end-of-file
896bebab91eSElena Ufimtseva  *          occurs without data, or -1 on error
897bebab91eSElena Ufimtseva  */
898bebab91eSElena Ufimtseva 
8991dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_full_all_eof(QIOChannel *ioc,
900bebab91eSElena Ufimtseva                                                       const struct iovec *iov,
901bebab91eSElena Ufimtseva                                                       size_t niov,
902bebab91eSElena Ufimtseva                                                       int **fds, size_t *nfds,
903bebab91eSElena Ufimtseva                                                       Error **errp);
904bebab91eSElena Ufimtseva 
905bebab91eSElena Ufimtseva /**
906bebab91eSElena Ufimtseva  * qio_channel_readv_full_all:
907bebab91eSElena Ufimtseva  * @ioc: the channel object
908bebab91eSElena Ufimtseva  * @iov: the array of memory regions to read data to
909bebab91eSElena Ufimtseva  * @niov: the length of the @iov array
910bebab91eSElena Ufimtseva  * @fds: an array of file handles to read
911bebab91eSElena Ufimtseva  * @nfds: number of file handles in @fds
912bebab91eSElena Ufimtseva  * @errp: pointer to a NULL-initialized error object
913bebab91eSElena Ufimtseva  *
914bebab91eSElena Ufimtseva  *
915bebab91eSElena Ufimtseva  * Performs same function as qio_channel_readv_all_eof.
916bebab91eSElena Ufimtseva  * Additionally, attempts to read file descriptors shared
917bebab91eSElena Ufimtseva  * over the channel. The function will wait for all
918bebab91eSElena Ufimtseva  * requested data to be read, yielding from the current
919bebab91eSElena Ufimtseva  * coroutine if required. data refers to both file
920bebab91eSElena Ufimtseva  * descriptors and the iovs.
921bebab91eSElena Ufimtseva  *
922bebab91eSElena Ufimtseva  * Returns: 0 if all bytes were read, or -1 on error
923bebab91eSElena Ufimtseva  */
924bebab91eSElena Ufimtseva 
9251dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_full_all(QIOChannel *ioc,
926bebab91eSElena Ufimtseva                                                   const struct iovec *iov,
927bebab91eSElena Ufimtseva                                                   size_t niov,
928bebab91eSElena Ufimtseva                                                   int **fds, size_t *nfds,
929bebab91eSElena Ufimtseva                                                   Error **errp);
930bebab91eSElena Ufimtseva 
931bebab91eSElena Ufimtseva /**
932bfa42387SElena Ufimtseva  * qio_channel_writev_full_all:
933bfa42387SElena Ufimtseva  * @ioc: the channel object
934bfa42387SElena Ufimtseva  * @iov: the array of memory regions to write data from
935bfa42387SElena Ufimtseva  * @niov: the length of the @iov array
936bfa42387SElena Ufimtseva  * @fds: an array of file handles to send
937bfa42387SElena Ufimtseva  * @nfds: number of file handles in @fds
938b88651cbSLeonardo Bras  * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
939bfa42387SElena Ufimtseva  * @errp: pointer to a NULL-initialized error object
940bfa42387SElena Ufimtseva  *
941bfa42387SElena Ufimtseva  *
942bfa42387SElena Ufimtseva  * Behaves like qio_channel_writev_full but will attempt
943bfa42387SElena Ufimtseva  * to send all data passed (file handles and memory regions).
944bfa42387SElena Ufimtseva  * The function will wait for all requested data
945bfa42387SElena Ufimtseva  * to be written, yielding from the current coroutine
946bfa42387SElena Ufimtseva  * if required.
947bfa42387SElena Ufimtseva  *
948b88651cbSLeonardo Bras  * If QIO_CHANNEL_WRITE_FLAG_ZERO_COPY is passed in flags,
949b88651cbSLeonardo Bras  * instead of waiting for all requested data to be written,
950b88651cbSLeonardo Bras  * this function will wait until it's all queued for writing.
951b88651cbSLeonardo Bras  * In this case, if the buffer gets changed between queueing and
952b88651cbSLeonardo Bras  * sending, the updated buffer will be sent. If this is not a
953b88651cbSLeonardo Bras  * desired behavior, it's suggested to call qio_channel_flush()
954b88651cbSLeonardo Bras  * before reusing the buffer.
955b88651cbSLeonardo Bras  *
956bfa42387SElena Ufimtseva  * Returns: 0 if all bytes were written, or -1 on error
957bfa42387SElena Ufimtseva  */
958bfa42387SElena Ufimtseva 
9591dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc,
960bfa42387SElena Ufimtseva                                                    const struct iovec *iov,
961bfa42387SElena Ufimtseva                                                    size_t niov,
962bfa42387SElena Ufimtseva                                                    int *fds, size_t nfds,
963b88651cbSLeonardo Bras                                                    int flags, Error **errp);
964b88651cbSLeonardo Bras 
965b88651cbSLeonardo Bras /**
966b88651cbSLeonardo Bras  * qio_channel_flush:
967b88651cbSLeonardo Bras  * @ioc: the channel object
968b88651cbSLeonardo Bras  * @errp: pointer to a NULL-initialized error object
969b88651cbSLeonardo Bras  *
970b88651cbSLeonardo Bras  * Will block until every packet queued with
971b88651cbSLeonardo Bras  * qio_channel_writev_full() + QIO_CHANNEL_WRITE_FLAG_ZERO_COPY
972b88651cbSLeonardo Bras  * is sent, or return in case of any error.
973b88651cbSLeonardo Bras  *
974b88651cbSLeonardo Bras  * If not implemented, acts as a no-op, and returns 0.
975b88651cbSLeonardo Bras  *
976b88651cbSLeonardo Bras  * Returns -1 if any error is found,
977b88651cbSLeonardo Bras  *          1 if every send failed to use zero copy.
978b88651cbSLeonardo Bras  *          0 otherwise.
979b88651cbSLeonardo Bras  */
980b88651cbSLeonardo Bras 
981b88651cbSLeonardo Bras int qio_channel_flush(QIOChannel *ioc,
982bfa42387SElena Ufimtseva                       Error **errp);
983bfa42387SElena Ufimtseva 
9842a6a4076SMarkus Armbruster #endif /* QIO_CHANNEL_H */
985