xref: /qemu/include/io/channel.h (revision bfa42387505168782ba7b339d0b13c23648e6207)
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"
25c4c497d2SPaolo Bonzini #include "qemu/coroutine.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 
35666a3af9SDaniel P. Berrange typedef enum QIOChannelFeature QIOChannelFeature;
36666a3af9SDaniel P. Berrange 
37666a3af9SDaniel P. Berrange enum QIOChannelFeature {
388fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_FD_PASS,
398fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_SHUTDOWN,
408fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_LISTEN,
41666a3af9SDaniel P. Berrange };
42666a3af9SDaniel P. Berrange 
43666a3af9SDaniel P. Berrange 
44666a3af9SDaniel P. Berrange typedef enum QIOChannelShutdown QIOChannelShutdown;
45666a3af9SDaniel P. Berrange 
46666a3af9SDaniel P. Berrange enum QIOChannelShutdown {
47a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_READ = 1,
48a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_WRITE = 2,
49a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_BOTH = 3,
50666a3af9SDaniel P. Berrange };
51666a3af9SDaniel P. Berrange 
52666a3af9SDaniel P. Berrange typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc,
53666a3af9SDaniel P. Berrange                                    GIOCondition condition,
54666a3af9SDaniel P. Berrange                                    gpointer data);
55666a3af9SDaniel P. Berrange 
56666a3af9SDaniel P. Berrange /**
57666a3af9SDaniel P. Berrange  * QIOChannel:
58666a3af9SDaniel P. Berrange  *
59666a3af9SDaniel P. Berrange  * The QIOChannel defines the core API for a generic I/O channel
60666a3af9SDaniel P. Berrange  * class hierarchy. It is inspired by GIOChannel, but has the
61666a3af9SDaniel P. Berrange  * following differences
62666a3af9SDaniel P. Berrange  *
63666a3af9SDaniel P. Berrange  *  - Use QOM to properly support arbitrary subclassing
64666a3af9SDaniel P. Berrange  *  - Support use of iovecs for efficient I/O with multiple blocks
65666a3af9SDaniel P. Berrange  *  - None of the character set translation, binary data exclusively
66666a3af9SDaniel P. Berrange  *  - Direct support for QEMU Error object reporting
67666a3af9SDaniel P. Berrange  *  - File descriptor passing
68666a3af9SDaniel P. Berrange  *
69666a3af9SDaniel P. Berrange  * This base class is abstract so cannot be instantiated. There
70666a3af9SDaniel P. Berrange  * will be subclasses for dealing with sockets, files, and higher
71666a3af9SDaniel P. Berrange  * level protocols such as TLS, WebSocket, etc.
72666a3af9SDaniel P. Berrange  */
73666a3af9SDaniel P. Berrange 
74666a3af9SDaniel P. Berrange struct QIOChannel {
75666a3af9SDaniel P. Berrange     Object parent;
76666a3af9SDaniel P. Berrange     unsigned int features; /* bitmask of QIOChannelFeatures */
7720f4aa26SDaniel P. Berrange     char *name;
78c4c497d2SPaolo Bonzini     AioContext *ctx;
79c4c497d2SPaolo Bonzini     Coroutine *read_coroutine;
80c4c497d2SPaolo Bonzini     Coroutine *write_coroutine;
81a5897205SPaolo Bonzini #ifdef _WIN32
82a5897205SPaolo Bonzini     HANDLE event; /* For use with GSource on Win32 */
83a5897205SPaolo Bonzini #endif
84666a3af9SDaniel P. Berrange };
85666a3af9SDaniel P. Berrange 
86666a3af9SDaniel P. Berrange /**
87666a3af9SDaniel P. Berrange  * QIOChannelClass:
88666a3af9SDaniel P. Berrange  *
89666a3af9SDaniel P. Berrange  * This class defines the contract that all subclasses
90666a3af9SDaniel P. Berrange  * must follow to provide specific channel implementations.
91666a3af9SDaniel P. Berrange  * The first five callbacks are mandatory to support, others
92666a3af9SDaniel P. Berrange  * provide additional optional features.
93666a3af9SDaniel P. Berrange  *
94666a3af9SDaniel P. Berrange  * Consult the corresponding public API docs for a description
958659f317SLukas Straub  * of the semantics of each callback. io_shutdown in particular
968659f317SLukas Straub  * must be thread-safe, terminate quickly and must not block.
97666a3af9SDaniel P. Berrange  */
98666a3af9SDaniel P. Berrange struct QIOChannelClass {
99666a3af9SDaniel P. Berrange     ObjectClass parent;
100666a3af9SDaniel P. Berrange 
101666a3af9SDaniel P. Berrange     /* Mandatory callbacks */
102666a3af9SDaniel P. Berrange     ssize_t (*io_writev)(QIOChannel *ioc,
103666a3af9SDaniel P. Berrange                          const struct iovec *iov,
104666a3af9SDaniel P. Berrange                          size_t niov,
105666a3af9SDaniel P. Berrange                          int *fds,
106666a3af9SDaniel P. Berrange                          size_t nfds,
107666a3af9SDaniel P. Berrange                          Error **errp);
108666a3af9SDaniel P. Berrange     ssize_t (*io_readv)(QIOChannel *ioc,
109666a3af9SDaniel P. Berrange                         const struct iovec *iov,
110666a3af9SDaniel P. Berrange                         size_t niov,
111666a3af9SDaniel P. Berrange                         int **fds,
112666a3af9SDaniel P. Berrange                         size_t *nfds,
113666a3af9SDaniel P. Berrange                         Error **errp);
114666a3af9SDaniel P. Berrange     int (*io_close)(QIOChannel *ioc,
115666a3af9SDaniel P. Berrange                     Error **errp);
116666a3af9SDaniel P. Berrange     GSource * (*io_create_watch)(QIOChannel *ioc,
117666a3af9SDaniel P. Berrange                                  GIOCondition condition);
118666a3af9SDaniel P. Berrange     int (*io_set_blocking)(QIOChannel *ioc,
119666a3af9SDaniel P. Berrange                            bool enabled,
120666a3af9SDaniel P. Berrange                            Error **errp);
121666a3af9SDaniel P. Berrange 
122666a3af9SDaniel P. Berrange     /* Optional callbacks */
123666a3af9SDaniel P. Berrange     int (*io_shutdown)(QIOChannel *ioc,
124666a3af9SDaniel P. Berrange                        QIOChannelShutdown how,
125666a3af9SDaniel P. Berrange                        Error **errp);
126666a3af9SDaniel P. Berrange     void (*io_set_cork)(QIOChannel *ioc,
127666a3af9SDaniel P. Berrange                         bool enabled);
128666a3af9SDaniel P. Berrange     void (*io_set_delay)(QIOChannel *ioc,
129666a3af9SDaniel P. Berrange                          bool enabled);
130666a3af9SDaniel P. Berrange     off_t (*io_seek)(QIOChannel *ioc,
131666a3af9SDaniel P. Berrange                      off_t offset,
132666a3af9SDaniel P. Berrange                      int whence,
133666a3af9SDaniel P. Berrange                      Error **errp);
134bf88c124SPaolo Bonzini     void (*io_set_aio_fd_handler)(QIOChannel *ioc,
135bf88c124SPaolo Bonzini                                   AioContext *ctx,
136bf88c124SPaolo Bonzini                                   IOHandler *io_read,
137bf88c124SPaolo Bonzini                                   IOHandler *io_write,
138bf88c124SPaolo Bonzini                                   void *opaque);
139666a3af9SDaniel P. Berrange };
140666a3af9SDaniel P. Berrange 
141666a3af9SDaniel P. Berrange /* General I/O handling functions */
142666a3af9SDaniel P. Berrange 
143666a3af9SDaniel P. Berrange /**
144666a3af9SDaniel P. Berrange  * qio_channel_has_feature:
145666a3af9SDaniel P. Berrange  * @ioc: the channel object
146666a3af9SDaniel P. Berrange  * @feature: the feature to check support of
147666a3af9SDaniel P. Berrange  *
148666a3af9SDaniel P. Berrange  * Determine whether the channel implementation supports
149666a3af9SDaniel P. Berrange  * the optional feature named in @feature.
150666a3af9SDaniel P. Berrange  *
151666a3af9SDaniel P. Berrange  * Returns: true if supported, false otherwise.
152666a3af9SDaniel P. Berrange  */
153666a3af9SDaniel P. Berrange bool qio_channel_has_feature(QIOChannel *ioc,
154666a3af9SDaniel P. Berrange                              QIOChannelFeature feature);
155666a3af9SDaniel P. Berrange 
156666a3af9SDaniel P. Berrange /**
157d8d3c7ccSFelipe Franciosi  * qio_channel_set_feature:
158d8d3c7ccSFelipe Franciosi  * @ioc: the channel object
159d8d3c7ccSFelipe Franciosi  * @feature: the feature to set support for
160d8d3c7ccSFelipe Franciosi  *
161d8d3c7ccSFelipe Franciosi  * Add channel support for the feature named in @feature.
162d8d3c7ccSFelipe Franciosi  */
163d8d3c7ccSFelipe Franciosi void qio_channel_set_feature(QIOChannel *ioc,
164d8d3c7ccSFelipe Franciosi                              QIOChannelFeature feature);
165d8d3c7ccSFelipe Franciosi 
166d8d3c7ccSFelipe Franciosi /**
16720f4aa26SDaniel P. Berrange  * qio_channel_set_name:
16820f4aa26SDaniel P. Berrange  * @ioc: the channel object
16920f4aa26SDaniel P. Berrange  * @name: the name of the channel
17020f4aa26SDaniel P. Berrange  *
17120f4aa26SDaniel P. Berrange  * Sets the name of the channel, which serves as an aid
17220f4aa26SDaniel P. Berrange  * to debugging. The name is used when creating GSource
17320f4aa26SDaniel P. Berrange  * watches for this channel.
17420f4aa26SDaniel P. Berrange  */
17520f4aa26SDaniel P. Berrange void qio_channel_set_name(QIOChannel *ioc,
17620f4aa26SDaniel P. Berrange                           const char *name);
17720f4aa26SDaniel P. Berrange 
17820f4aa26SDaniel P. Berrange /**
179666a3af9SDaniel P. Berrange  * qio_channel_readv_full:
180666a3af9SDaniel P. Berrange  * @ioc: the channel object
181666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
182666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
183666a3af9SDaniel P. Berrange  * @fds: pointer to an array that will received file handles
184666a3af9SDaniel P. Berrange  * @nfds: pointer filled with number of elements in @fds on return
185821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
186666a3af9SDaniel P. Berrange  *
187666a3af9SDaniel P. Berrange  * Read data from the IO channel, storing it in the
188666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
189666a3af9SDaniel P. Berrange  * in the @iov will be fully populated with data
190666a3af9SDaniel P. Berrange  * before the next one is used. The @niov parameter
191666a3af9SDaniel P. Berrange  * specifies the total number of elements in @iov.
192666a3af9SDaniel P. Berrange  *
193666a3af9SDaniel P. Berrange  * It is not required for all @iov to be filled with
194666a3af9SDaniel P. Berrange  * data. If the channel is in blocking mode, at least
195666a3af9SDaniel P. Berrange  * one byte of data will be read, but no more is
196666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
197666a3af9SDaniel P. Berrange  * data is available, it will return QIO_CHANNEL_ERR_BLOCK
198666a3af9SDaniel P. Berrange  *
199666a3af9SDaniel P. Berrange  * If the channel has passed any file descriptors,
200666a3af9SDaniel P. Berrange  * the @fds array pointer will be allocated and
201666a3af9SDaniel P. Berrange  * the elements filled with the received file
202666a3af9SDaniel P. Berrange  * descriptors. The @nfds pointer will be updated
203666a3af9SDaniel P. Berrange  * to indicate the size of the @fds array that
204666a3af9SDaniel P. Berrange  * was allocated. It is the callers responsibility
205666a3af9SDaniel P. Berrange  * to call close() on each file descriptor and to
206666a3af9SDaniel P. Berrange  * call g_free() on the array pointer in @fds.
207666a3af9SDaniel P. Berrange  *
208666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
209666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
210666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
211666a3af9SDaniel P. Berrange  *
212666a3af9SDaniel P. Berrange  * Returns: the number of bytes read, or -1 on error,
213666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is available
214666a3af9SDaniel P. Berrange  * and the channel is non-blocking
215666a3af9SDaniel P. Berrange  */
216666a3af9SDaniel P. Berrange ssize_t qio_channel_readv_full(QIOChannel *ioc,
217666a3af9SDaniel P. Berrange                                const struct iovec *iov,
218666a3af9SDaniel P. Berrange                                size_t niov,
219666a3af9SDaniel P. Berrange                                int **fds,
220666a3af9SDaniel P. Berrange                                size_t *nfds,
221666a3af9SDaniel P. Berrange                                Error **errp);
222666a3af9SDaniel P. Berrange 
223666a3af9SDaniel P. Berrange 
224666a3af9SDaniel P. Berrange /**
225666a3af9SDaniel P. Berrange  * qio_channel_writev_full:
226666a3af9SDaniel P. Berrange  * @ioc: the channel object
227666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
228666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
229666a3af9SDaniel P. Berrange  * @fds: an array of file handles to send
230666a3af9SDaniel P. Berrange  * @nfds: number of file handles in @fds
231821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
232666a3af9SDaniel P. Berrange  *
233666a3af9SDaniel P. Berrange  * Write data to the IO channel, reading it from the
234666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
235666a3af9SDaniel P. Berrange  * in the @iov will be fully sent, before the next
236666a3af9SDaniel P. Berrange  * one is used. The @niov parameter specifies the
237666a3af9SDaniel P. Berrange  * total number of elements in @iov.
238666a3af9SDaniel P. Berrange  *
239666a3af9SDaniel P. Berrange  * It is not required for all @iov data to be fully
240666a3af9SDaniel P. Berrange  * sent. If the channel is in blocking mode, at least
241666a3af9SDaniel P. Berrange  * one byte of data will be sent, but no more is
242666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
243666a3af9SDaniel P. Berrange  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
244666a3af9SDaniel P. Berrange  *
245666a3af9SDaniel P. Berrange  * If there are file descriptors to send, the @fds
246666a3af9SDaniel P. Berrange  * array should be non-NULL and provide the handles.
247666a3af9SDaniel P. Berrange  * All file descriptors will be sent if at least one
248666a3af9SDaniel P. Berrange  * byte of data was sent.
249666a3af9SDaniel P. Berrange  *
250666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
251666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
252666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
253666a3af9SDaniel P. Berrange  *
254666a3af9SDaniel P. Berrange  * Returns: the number of bytes sent, or -1 on error,
255666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
256666a3af9SDaniel P. Berrange  * and the channel is non-blocking
257666a3af9SDaniel P. Berrange  */
258666a3af9SDaniel P. Berrange ssize_t qio_channel_writev_full(QIOChannel *ioc,
259666a3af9SDaniel P. Berrange                                 const struct iovec *iov,
260666a3af9SDaniel P. Berrange                                 size_t niov,
261666a3af9SDaniel P. Berrange                                 int *fds,
262666a3af9SDaniel P. Berrange                                 size_t nfds,
263666a3af9SDaniel P. Berrange                                 Error **errp);
264666a3af9SDaniel P. Berrange 
265666a3af9SDaniel P. Berrange /**
266e8ffaa31SEric Blake  * qio_channel_readv_all_eof:
267e8ffaa31SEric Blake  * @ioc: the channel object
268e8ffaa31SEric Blake  * @iov: the array of memory regions to read data into
269e8ffaa31SEric Blake  * @niov: the length of the @iov array
270e8ffaa31SEric Blake  * @errp: pointer to a NULL-initialized error object
271e8ffaa31SEric Blake  *
272e8ffaa31SEric Blake  * Read data from the IO channel, storing it in the
273e8ffaa31SEric Blake  * memory regions referenced by @iov. Each element
274e8ffaa31SEric Blake  * in the @iov will be fully populated with data
275e8ffaa31SEric Blake  * before the next one is used. The @niov parameter
276e8ffaa31SEric Blake  * specifies the total number of elements in @iov.
277e8ffaa31SEric Blake  *
278e8ffaa31SEric Blake  * The function will wait for all requested data
279e8ffaa31SEric Blake  * to be read, yielding from the current coroutine
280e8ffaa31SEric Blake  * if required.
281e8ffaa31SEric Blake  *
282e8ffaa31SEric Blake  * If end-of-file occurs before any data is read,
283e8ffaa31SEric Blake  * no error is reported; otherwise, if it occurs
284e8ffaa31SEric Blake  * before all requested data has been read, an error
285e8ffaa31SEric Blake  * will be reported.
286e8ffaa31SEric Blake  *
287e8ffaa31SEric Blake  * Returns: 1 if all bytes were read, 0 if end-of-file
288e8ffaa31SEric Blake  *          occurs without data, or -1 on error
289e8ffaa31SEric Blake  */
290e8ffaa31SEric Blake int qio_channel_readv_all_eof(QIOChannel *ioc,
291e8ffaa31SEric Blake                               const struct iovec *iov,
292e8ffaa31SEric Blake                               size_t niov,
293e8ffaa31SEric Blake                               Error **errp);
294e8ffaa31SEric Blake 
295e8ffaa31SEric Blake /**
296d4622e55SDaniel P. Berrange  * qio_channel_readv_all:
297d4622e55SDaniel P. Berrange  * @ioc: the channel object
298d4622e55SDaniel P. Berrange  * @iov: the array of memory regions to read data into
299d4622e55SDaniel P. Berrange  * @niov: the length of the @iov array
300d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
301d4622e55SDaniel P. Berrange  *
302d4622e55SDaniel P. Berrange  * Read data from the IO channel, storing it in the
303d4622e55SDaniel P. Berrange  * memory regions referenced by @iov. Each element
304d4622e55SDaniel P. Berrange  * in the @iov will be fully populated with data
305d4622e55SDaniel P. Berrange  * before the next one is used. The @niov parameter
306d4622e55SDaniel P. Berrange  * specifies the total number of elements in @iov.
307d4622e55SDaniel P. Berrange  *
308d4622e55SDaniel P. Berrange  * The function will wait for all requested data
309d4622e55SDaniel P. Berrange  * to be read, yielding from the current coroutine
310d4622e55SDaniel P. Berrange  * if required.
311d4622e55SDaniel P. Berrange  *
312d4622e55SDaniel P. Berrange  * If end-of-file occurs before all requested data
313d4622e55SDaniel P. Berrange  * has been read, an error will be reported.
314d4622e55SDaniel P. Berrange  *
315d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were read, or -1 on error
316d4622e55SDaniel P. Berrange  */
317d4622e55SDaniel P. Berrange int qio_channel_readv_all(QIOChannel *ioc,
318d4622e55SDaniel P. Berrange                           const struct iovec *iov,
319d4622e55SDaniel P. Berrange                           size_t niov,
320d4622e55SDaniel P. Berrange                           Error **errp);
321d4622e55SDaniel P. Berrange 
322d4622e55SDaniel P. Berrange 
323d4622e55SDaniel P. Berrange /**
324d4622e55SDaniel P. Berrange  * qio_channel_writev_all:
325d4622e55SDaniel P. Berrange  * @ioc: the channel object
326d4622e55SDaniel P. Berrange  * @iov: the array of memory regions to write data from
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  * Write data to the IO channel, reading it from the
331d4622e55SDaniel P. Berrange  * memory regions referenced by @iov. Each element
332d4622e55SDaniel P. Berrange  * in the @iov will be fully sent, before the next
333d4622e55SDaniel P. Berrange  * one is used. The @niov parameter specifies the
334d4622e55SDaniel P. Berrange  * 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 written, yielding from the current coroutine
338d4622e55SDaniel P. Berrange  * if required.
339d4622e55SDaniel P. Berrange  *
340d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were written, or -1 on error
341d4622e55SDaniel P. Berrange  */
342d4622e55SDaniel P. Berrange int qio_channel_writev_all(QIOChannel *ioc,
343d4622e55SDaniel P. Berrange                            const struct iovec *iov,
344d4622e55SDaniel P. Berrange                            size_t niov,
345d4622e55SDaniel P. Berrange                            Error **erp);
346d4622e55SDaniel P. Berrange 
347d4622e55SDaniel P. Berrange /**
348666a3af9SDaniel P. Berrange  * qio_channel_readv:
349666a3af9SDaniel P. Berrange  * @ioc: the channel object
350666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
351666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
352821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
353666a3af9SDaniel P. Berrange  *
354666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
355666a3af9SDaniel P. Berrange  * receiving of file handles.
356666a3af9SDaniel P. Berrange  */
357666a3af9SDaniel P. Berrange ssize_t qio_channel_readv(QIOChannel *ioc,
358666a3af9SDaniel P. Berrange                           const struct iovec *iov,
359666a3af9SDaniel P. Berrange                           size_t niov,
360666a3af9SDaniel P. Berrange                           Error **errp);
361666a3af9SDaniel P. Berrange 
362666a3af9SDaniel P. Berrange /**
363666a3af9SDaniel P. Berrange  * qio_channel_writev:
364666a3af9SDaniel P. Berrange  * @ioc: the channel object
365666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
366666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
367821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
368666a3af9SDaniel P. Berrange  *
369666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
370666a3af9SDaniel P. Berrange  * sending of file handles.
371666a3af9SDaniel P. Berrange  */
372666a3af9SDaniel P. Berrange ssize_t qio_channel_writev(QIOChannel *ioc,
373666a3af9SDaniel P. Berrange                            const struct iovec *iov,
374666a3af9SDaniel P. Berrange                            size_t niov,
375666a3af9SDaniel P. Berrange                            Error **errp);
376666a3af9SDaniel P. Berrange 
377666a3af9SDaniel P. Berrange /**
37850ea44f0SDaniel P. Berrange  * qio_channel_read:
379666a3af9SDaniel P. Berrange  * @ioc: the channel object
380666a3af9SDaniel P. Berrange  * @buf: the memory region to read data into
381666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
382821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
383666a3af9SDaniel P. Berrange  *
384666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
385666a3af9SDaniel P. Berrange  * receiving of file handles, and only supports reading into
386666a3af9SDaniel P. Berrange  * a single memory region.
387666a3af9SDaniel P. Berrange  */
388666a3af9SDaniel P. Berrange ssize_t qio_channel_read(QIOChannel *ioc,
389666a3af9SDaniel P. Berrange                          char *buf,
390666a3af9SDaniel P. Berrange                          size_t buflen,
391666a3af9SDaniel P. Berrange                          Error **errp);
392666a3af9SDaniel P. Berrange 
393666a3af9SDaniel P. Berrange /**
39461f7c6a0SMarc-André Lureau  * qio_channel_write:
395666a3af9SDaniel P. Berrange  * @ioc: the channel object
396666a3af9SDaniel P. Berrange  * @buf: the memory regions to send data from
397666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
398821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
399666a3af9SDaniel P. Berrange  *
400666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
401666a3af9SDaniel P. Berrange  * sending of file handles, and only supports writing from a
402666a3af9SDaniel P. Berrange  * single memory region.
403666a3af9SDaniel P. Berrange  */
404666a3af9SDaniel P. Berrange ssize_t qio_channel_write(QIOChannel *ioc,
405666a3af9SDaniel P. Berrange                           const char *buf,
406666a3af9SDaniel P. Berrange                           size_t buflen,
407666a3af9SDaniel P. Berrange                           Error **errp);
408666a3af9SDaniel P. Berrange 
409666a3af9SDaniel P. Berrange /**
410e8ffaa31SEric Blake  * qio_channel_read_all_eof:
411e8ffaa31SEric Blake  * @ioc: the channel object
412e8ffaa31SEric Blake  * @buf: the memory region to read data into
413e8ffaa31SEric Blake  * @buflen: the number of bytes to @buf
414e8ffaa31SEric Blake  * @errp: pointer to a NULL-initialized error object
415e8ffaa31SEric Blake  *
416e8ffaa31SEric Blake  * Reads @buflen bytes into @buf, possibly blocking or (if the
417e8ffaa31SEric Blake  * channel is non-blocking) yielding from the current coroutine
418e8ffaa31SEric Blake  * multiple times until the entire content is read. If end-of-file
419e8ffaa31SEric Blake  * occurs immediately it is not an error, but if it occurs after
420e8ffaa31SEric Blake  * data has been read it will return an error rather than a
421e8ffaa31SEric Blake  * short-read. Otherwise behaves as qio_channel_read().
422e8ffaa31SEric Blake  *
423e8ffaa31SEric Blake  * Returns: 1 if all bytes were read, 0 if end-of-file occurs
424e8ffaa31SEric Blake  *          without data, or -1 on error
425e8ffaa31SEric Blake  */
426e8ffaa31SEric Blake int qio_channel_read_all_eof(QIOChannel *ioc,
427e8ffaa31SEric Blake                              char *buf,
428e8ffaa31SEric Blake                              size_t buflen,
429e8ffaa31SEric Blake                              Error **errp);
430e8ffaa31SEric Blake 
431e8ffaa31SEric Blake /**
432d4622e55SDaniel P. Berrange  * qio_channel_read_all:
433d4622e55SDaniel P. Berrange  * @ioc: the channel object
434d4622e55SDaniel P. Berrange  * @buf: the memory region to read data into
435d4622e55SDaniel P. Berrange  * @buflen: the number of bytes to @buf
436d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
437d4622e55SDaniel P. Berrange  *
438d4622e55SDaniel P. Berrange  * Reads @buflen bytes into @buf, possibly blocking or (if the
439d4622e55SDaniel P. Berrange  * channel is non-blocking) yielding from the current coroutine
440d4622e55SDaniel P. Berrange  * multiple times until the entire content is read. If end-of-file
441d4622e55SDaniel P. Berrange  * occurs it will return an error rather than a short-read. Otherwise
442d4622e55SDaniel P. Berrange  * behaves as qio_channel_read().
443d4622e55SDaniel P. Berrange  *
444d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were read, or -1 on error
445d4622e55SDaniel P. Berrange  */
446d4622e55SDaniel P. Berrange int qio_channel_read_all(QIOChannel *ioc,
447d4622e55SDaniel P. Berrange                          char *buf,
448d4622e55SDaniel P. Berrange                          size_t buflen,
449d4622e55SDaniel P. Berrange                          Error **errp);
450e8ffaa31SEric Blake 
451d4622e55SDaniel P. Berrange /**
452d4622e55SDaniel P. Berrange  * qio_channel_write_all:
453d4622e55SDaniel P. Berrange  * @ioc: the channel object
454d4622e55SDaniel P. Berrange  * @buf: the memory region to write data into
455d4622e55SDaniel P. Berrange  * @buflen: the number of bytes to @buf
456d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
457d4622e55SDaniel P. Berrange  *
458d4622e55SDaniel P. Berrange  * Writes @buflen bytes from @buf, possibly blocking or (if the
459d4622e55SDaniel P. Berrange  * channel is non-blocking) yielding from the current coroutine
460d4622e55SDaniel P. Berrange  * multiple times until the entire content is written.  Otherwise
461d4622e55SDaniel P. Berrange  * behaves as qio_channel_write().
462d4622e55SDaniel P. Berrange  *
463d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were written, or -1 on error
464d4622e55SDaniel P. Berrange  */
465d4622e55SDaniel P. Berrange int qio_channel_write_all(QIOChannel *ioc,
466d4622e55SDaniel P. Berrange                           const char *buf,
467d4622e55SDaniel P. Berrange                           size_t buflen,
468d4622e55SDaniel P. Berrange                           Error **errp);
469d4622e55SDaniel P. Berrange 
470d4622e55SDaniel P. Berrange /**
471666a3af9SDaniel P. Berrange  * qio_channel_set_blocking:
472666a3af9SDaniel P. Berrange  * @ioc: the channel object
473666a3af9SDaniel P. Berrange  * @enabled: the blocking flag state
474821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
475666a3af9SDaniel P. Berrange  *
476666a3af9SDaniel P. Berrange  * If @enabled is true, then the channel is put into
477666a3af9SDaniel P. Berrange  * blocking mode, otherwise it will be non-blocking.
478666a3af9SDaniel P. Berrange  *
479666a3af9SDaniel P. Berrange  * In non-blocking mode, read/write operations may
480666a3af9SDaniel P. Berrange  * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
481666a3af9SDaniel P. Berrange  * block on I/O
482666a3af9SDaniel P. Berrange  */
483666a3af9SDaniel P. Berrange int qio_channel_set_blocking(QIOChannel *ioc,
484666a3af9SDaniel P. Berrange                              bool enabled,
485666a3af9SDaniel P. Berrange                              Error **errp);
486666a3af9SDaniel P. Berrange 
487666a3af9SDaniel P. Berrange /**
488666a3af9SDaniel P. Berrange  * qio_channel_close:
489666a3af9SDaniel P. Berrange  * @ioc: the channel object
490821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
491666a3af9SDaniel P. Berrange  *
492666a3af9SDaniel P. Berrange  * Close the channel, flushing any pending I/O
493666a3af9SDaniel P. Berrange  *
494666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
495666a3af9SDaniel P. Berrange  */
496666a3af9SDaniel P. Berrange int qio_channel_close(QIOChannel *ioc,
497666a3af9SDaniel P. Berrange                       Error **errp);
498666a3af9SDaniel P. Berrange 
499666a3af9SDaniel P. Berrange /**
500666a3af9SDaniel P. Berrange  * qio_channel_shutdown:
501666a3af9SDaniel P. Berrange  * @ioc: the channel object
502666a3af9SDaniel P. Berrange  * @how: the direction to shutdown
503821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
504666a3af9SDaniel P. Berrange  *
505666a3af9SDaniel P. Berrange  * Shutdowns transmission and/or receiving of data
506666a3af9SDaniel P. Berrange  * without closing the underlying transport.
507666a3af9SDaniel P. Berrange  *
508666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
509666a3af9SDaniel P. Berrange  * so may report an error. To avoid errors, the
510666a3af9SDaniel P. Berrange  * caller may check for the feature flag
511666a3af9SDaniel P. Berrange  * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling
512666a3af9SDaniel P. Berrange  * this method.
513666a3af9SDaniel P. Berrange  *
5148659f317SLukas Straub  * This function is thread-safe, terminates quickly and does not block.
5158659f317SLukas Straub  *
516666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
517666a3af9SDaniel P. Berrange  */
518666a3af9SDaniel P. Berrange int qio_channel_shutdown(QIOChannel *ioc,
519666a3af9SDaniel P. Berrange                          QIOChannelShutdown how,
520666a3af9SDaniel P. Berrange                          Error **errp);
521666a3af9SDaniel P. Berrange 
522666a3af9SDaniel P. Berrange /**
523666a3af9SDaniel P. Berrange  * qio_channel_set_delay:
524666a3af9SDaniel P. Berrange  * @ioc: the channel object
525666a3af9SDaniel P. Berrange  * @enabled: the new flag state
526666a3af9SDaniel P. Berrange  *
527666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
528666a3af9SDaniel P. Berrange  * permitted to delay writes in order to merge
529666a3af9SDaniel P. Berrange  * small packets. If @enabled is true, then the
530666a3af9SDaniel P. Berrange  * writes may be delayed in order to opportunistically
531666a3af9SDaniel P. Berrange  * merge small packets into larger ones. If @enabled
532666a3af9SDaniel P. Berrange  * is false, writes are dispatched immediately with
533666a3af9SDaniel P. Berrange  * no delay.
534666a3af9SDaniel P. Berrange  *
535666a3af9SDaniel P. Berrange  * When @enabled is false, applications may wish to
536666a3af9SDaniel P. Berrange  * use the qio_channel_set_cork() method to explicitly
537666a3af9SDaniel P. Berrange  * control write merging.
538666a3af9SDaniel P. Berrange  *
539666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
540666a3af9SDaniel P. Berrange  * API corresponds to the inverse of TCP_NODELAY flag,
541666a3af9SDaniel P. Berrange  * controlling whether the Nagle algorithm is active.
542666a3af9SDaniel P. Berrange  *
543666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
544666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
545666a3af9SDaniel P. Berrange  * error.
546666a3af9SDaniel P. Berrange  */
547666a3af9SDaniel P. Berrange void qio_channel_set_delay(QIOChannel *ioc,
548666a3af9SDaniel P. Berrange                            bool enabled);
549666a3af9SDaniel P. Berrange 
550666a3af9SDaniel P. Berrange /**
551666a3af9SDaniel P. Berrange  * qio_channel_set_cork:
552666a3af9SDaniel P. Berrange  * @ioc: the channel object
553666a3af9SDaniel P. Berrange  * @enabled: the new flag state
554666a3af9SDaniel P. Berrange  *
555666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
556666a3af9SDaniel P. Berrange  * permitted to dispatch data that is written.
557666a3af9SDaniel P. Berrange  * If @enabled is true, then any data written will
558666a3af9SDaniel P. Berrange  * be queued in local buffers until @enabled is
559666a3af9SDaniel P. Berrange  * set to false once again.
560666a3af9SDaniel P. Berrange  *
561666a3af9SDaniel P. Berrange  * This feature is typically used when the automatic
562666a3af9SDaniel P. Berrange  * write coalescing facility is disabled via the
563666a3af9SDaniel P. Berrange  * qio_channel_set_delay() method.
564666a3af9SDaniel P. Berrange  *
565666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
566666a3af9SDaniel P. Berrange  * API corresponds to the TCP_CORK flag.
567666a3af9SDaniel P. Berrange  *
568666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
569666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
570666a3af9SDaniel P. Berrange  * error.
571666a3af9SDaniel P. Berrange  */
572666a3af9SDaniel P. Berrange void qio_channel_set_cork(QIOChannel *ioc,
573666a3af9SDaniel P. Berrange                           bool enabled);
574666a3af9SDaniel P. Berrange 
575666a3af9SDaniel P. Berrange 
576666a3af9SDaniel P. Berrange /**
577666a3af9SDaniel P. Berrange  * qio_channel_seek:
578666a3af9SDaniel P. Berrange  * @ioc: the channel object
579666a3af9SDaniel P. Berrange  * @offset: the position to seek to, relative to @whence
580666a3af9SDaniel P. Berrange  * @whence: one of the (POSIX) SEEK_* constants listed below
581821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
582666a3af9SDaniel P. Berrange  *
583666a3af9SDaniel P. Berrange  * Moves the current I/O position within the channel
584666a3af9SDaniel P. Berrange  * @ioc, to be @offset. The value of @offset is
585666a3af9SDaniel P. Berrange  * interpreted relative to @whence:
586666a3af9SDaniel P. Berrange  *
587666a3af9SDaniel P. Berrange  * SEEK_SET - the position is set to @offset bytes
588666a3af9SDaniel P. Berrange  * SEEK_CUR - the position is moved by @offset bytes
589666a3af9SDaniel P. Berrange  * SEEK_END - the position is set to end of the file plus @offset bytes
590666a3af9SDaniel P. Berrange  *
591666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
592666a3af9SDaniel P. Berrange  * so may report an error.
593666a3af9SDaniel P. Berrange  *
594666a3af9SDaniel P. Berrange  * Returns: the new position on success, (off_t)-1 on failure
595666a3af9SDaniel P. Berrange  */
596666a3af9SDaniel P. Berrange off_t qio_channel_io_seek(QIOChannel *ioc,
597666a3af9SDaniel P. Berrange                           off_t offset,
598666a3af9SDaniel P. Berrange                           int whence,
599666a3af9SDaniel P. Berrange                           Error **errp);
600666a3af9SDaniel P. Berrange 
601666a3af9SDaniel P. Berrange 
602666a3af9SDaniel P. Berrange /**
603666a3af9SDaniel P. Berrange  * qio_channel_create_watch:
604666a3af9SDaniel P. Berrange  * @ioc: the channel object
605666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
606666a3af9SDaniel P. Berrange  *
607666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
608666a3af9SDaniel P. Berrange  * for the I/O condition @condition. Typically the
609666a3af9SDaniel P. Berrange  * qio_channel_add_watch() method would be used instead
610666a3af9SDaniel P. Berrange  * of this, since it directly attaches a callback to
611666a3af9SDaniel P. Berrange  * the source
612666a3af9SDaniel P. Berrange  *
613666a3af9SDaniel P. Berrange  * Returns: the new main loop source.
614666a3af9SDaniel P. Berrange  */
615666a3af9SDaniel P. Berrange GSource *qio_channel_create_watch(QIOChannel *ioc,
616666a3af9SDaniel P. Berrange                                   GIOCondition condition);
617666a3af9SDaniel P. Berrange 
618666a3af9SDaniel P. Berrange /**
619666a3af9SDaniel P. Berrange  * qio_channel_add_watch:
620666a3af9SDaniel P. Berrange  * @ioc: the channel object
621666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
622666a3af9SDaniel P. Berrange  * @func: callback to invoke when the source becomes ready
623666a3af9SDaniel P. Berrange  * @user_data: opaque data to pass to @func
624666a3af9SDaniel P. Berrange  * @notify: callback to free @user_data
625666a3af9SDaniel P. Berrange  *
626666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
627666a3af9SDaniel P. Berrange  * for the I/O condition @condition. The callback @func
628666a3af9SDaniel P. Berrange  * will be registered against the source, to be invoked
629666a3af9SDaniel P. Berrange  * when the source becomes ready. The optional @user_data
630666a3af9SDaniel P. Berrange  * will be passed to @func when it is invoked. The @notify
631666a3af9SDaniel P. Berrange  * callback will be used to free @user_data when the
632666a3af9SDaniel P. Berrange  * watch is deleted
633666a3af9SDaniel P. Berrange  *
634666a3af9SDaniel P. Berrange  * The returned source ID can be used with g_source_remove()
635666a3af9SDaniel P. Berrange  * to remove and free the source when no longer required.
636666a3af9SDaniel P. Berrange  * Alternatively the @func callback can return a FALSE
637666a3af9SDaniel P. Berrange  * value.
638666a3af9SDaniel P. Berrange  *
639666a3af9SDaniel P. Berrange  * Returns: the source ID
640666a3af9SDaniel P. Berrange  */
641666a3af9SDaniel P. Berrange guint qio_channel_add_watch(QIOChannel *ioc,
642666a3af9SDaniel P. Berrange                             GIOCondition condition,
643666a3af9SDaniel P. Berrange                             QIOChannelFunc func,
644666a3af9SDaniel P. Berrange                             gpointer user_data,
645666a3af9SDaniel P. Berrange                             GDestroyNotify notify);
646666a3af9SDaniel P. Berrange 
647315409c7SPeter Xu /**
648315409c7SPeter Xu  * qio_channel_add_watch_full:
649315409c7SPeter Xu  * @ioc: the channel object
650315409c7SPeter Xu  * @condition: the I/O condition to monitor
651315409c7SPeter Xu  * @func: callback to invoke when the source becomes ready
652315409c7SPeter Xu  * @user_data: opaque data to pass to @func
653315409c7SPeter Xu  * @notify: callback to free @user_data
654315409c7SPeter Xu  * @context: the context to run the watch source
655315409c7SPeter Xu  *
656315409c7SPeter Xu  * Similar as qio_channel_add_watch(), but allows to specify context
657315409c7SPeter Xu  * to run the watch source.
658315409c7SPeter Xu  *
659315409c7SPeter Xu  * Returns: the source ID
660315409c7SPeter Xu  */
661315409c7SPeter Xu guint qio_channel_add_watch_full(QIOChannel *ioc,
662315409c7SPeter Xu                                  GIOCondition condition,
663315409c7SPeter Xu                                  QIOChannelFunc func,
664315409c7SPeter Xu                                  gpointer user_data,
665315409c7SPeter Xu                                  GDestroyNotify notify,
666315409c7SPeter Xu                                  GMainContext *context);
667315409c7SPeter Xu 
668315409c7SPeter Xu /**
669315409c7SPeter Xu  * qio_channel_add_watch_source:
670315409c7SPeter Xu  * @ioc: the channel object
671315409c7SPeter Xu  * @condition: the I/O condition to monitor
672315409c7SPeter Xu  * @func: callback to invoke when the source becomes ready
673315409c7SPeter Xu  * @user_data: opaque data to pass to @func
674315409c7SPeter Xu  * @notify: callback to free @user_data
675315409c7SPeter Xu  * @context: gcontext to bind the source to
676315409c7SPeter Xu  *
677315409c7SPeter Xu  * Similar as qio_channel_add_watch(), but allows to specify context
678315409c7SPeter Xu  * to run the watch source, meanwhile return the GSource object
679315409c7SPeter Xu  * instead of tag ID, with the GSource referenced already.
680315409c7SPeter Xu  *
681315409c7SPeter Xu  * Note: callers is responsible to unref the source when not needed.
682315409c7SPeter Xu  *
683315409c7SPeter Xu  * Returns: the source pointer
684315409c7SPeter Xu  */
685315409c7SPeter Xu GSource *qio_channel_add_watch_source(QIOChannel *ioc,
686315409c7SPeter Xu                                       GIOCondition condition,
687315409c7SPeter Xu                                       QIOChannelFunc func,
688315409c7SPeter Xu                                       gpointer user_data,
689315409c7SPeter Xu                                       GDestroyNotify notify,
690315409c7SPeter Xu                                       GMainContext *context);
691666a3af9SDaniel P. Berrange 
692666a3af9SDaniel P. Berrange /**
693c4c497d2SPaolo Bonzini  * qio_channel_attach_aio_context:
694c4c497d2SPaolo Bonzini  * @ioc: the channel object
695c4c497d2SPaolo Bonzini  * @ctx: the #AioContext to set the handlers on
696c4c497d2SPaolo Bonzini  *
697c4c497d2SPaolo Bonzini  * Request that qio_channel_yield() sets I/O handlers on
698c4c497d2SPaolo Bonzini  * the given #AioContext.  If @ctx is %NULL, qio_channel_yield()
699c4c497d2SPaolo Bonzini  * uses QEMU's main thread event loop.
700c4c497d2SPaolo Bonzini  *
701c4c497d2SPaolo Bonzini  * You can move a #QIOChannel from one #AioContext to another even if
702c4c497d2SPaolo Bonzini  * I/O handlers are set for a coroutine.  However, #QIOChannel provides
703c4c497d2SPaolo Bonzini  * no synchronization between the calls to qio_channel_yield() and
704c4c497d2SPaolo Bonzini  * qio_channel_attach_aio_context().
705c4c497d2SPaolo Bonzini  *
706c4c497d2SPaolo Bonzini  * Therefore you should first call qio_channel_detach_aio_context()
707c4c497d2SPaolo Bonzini  * to ensure that the coroutine is not entered concurrently.  Then,
708c4c497d2SPaolo Bonzini  * while the coroutine has yielded, call qio_channel_attach_aio_context(),
709c4c497d2SPaolo Bonzini  * and then aio_co_schedule() to place the coroutine on the new
710c4c497d2SPaolo Bonzini  * #AioContext.  The calls to qio_channel_detach_aio_context()
711c4c497d2SPaolo Bonzini  * and qio_channel_attach_aio_context() should be protected with
712c4c497d2SPaolo Bonzini  * aio_context_acquire() and aio_context_release().
713c4c497d2SPaolo Bonzini  */
714c4c497d2SPaolo Bonzini void qio_channel_attach_aio_context(QIOChannel *ioc,
715c4c497d2SPaolo Bonzini                                     AioContext *ctx);
716c4c497d2SPaolo Bonzini 
717c4c497d2SPaolo Bonzini /**
718c4c497d2SPaolo Bonzini  * qio_channel_detach_aio_context:
719c4c497d2SPaolo Bonzini  * @ioc: the channel object
720c4c497d2SPaolo Bonzini  *
721c4c497d2SPaolo Bonzini  * Disable any I/O handlers set by qio_channel_yield().  With the
722c4c497d2SPaolo Bonzini  * help of aio_co_schedule(), this allows moving a coroutine that was
723c4c497d2SPaolo Bonzini  * paused by qio_channel_yield() to another context.
724c4c497d2SPaolo Bonzini  */
725c4c497d2SPaolo Bonzini void qio_channel_detach_aio_context(QIOChannel *ioc);
726c4c497d2SPaolo Bonzini 
727c4c497d2SPaolo Bonzini /**
728666a3af9SDaniel P. Berrange  * qio_channel_yield:
729666a3af9SDaniel P. Berrange  * @ioc: the channel object
730666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
731666a3af9SDaniel P. Berrange  *
732c4c497d2SPaolo Bonzini  * Yields execution from the current coroutine until the condition
733c4c497d2SPaolo Bonzini  * indicated by @condition becomes available.  @condition must
734c4c497d2SPaolo Bonzini  * be either %G_IO_IN or %G_IO_OUT; it cannot contain both.  In
735c4c497d2SPaolo Bonzini  * addition, no two coroutine can be waiting on the same condition
736c4c497d2SPaolo Bonzini  * and channel at the same time.
737666a3af9SDaniel P. Berrange  *
7386886ceafSKevin Wolf  * This must only be called from coroutine context. It is safe to
7396886ceafSKevin Wolf  * reenter the coroutine externally while it is waiting; in this
7406886ceafSKevin Wolf  * case the function will return even if @condition is not yet
7416886ceafSKevin Wolf  * available.
742666a3af9SDaniel P. Berrange  */
7436886ceafSKevin Wolf void coroutine_fn qio_channel_yield(QIOChannel *ioc,
744666a3af9SDaniel P. Berrange                                     GIOCondition condition);
745666a3af9SDaniel P. Berrange 
746666a3af9SDaniel P. Berrange /**
747666a3af9SDaniel P. Berrange  * qio_channel_wait:
748666a3af9SDaniel P. Berrange  * @ioc: the channel object
749666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
750666a3af9SDaniel P. Berrange  *
751666a3af9SDaniel P. Berrange  * Block execution from the current thread until
752666a3af9SDaniel P. Berrange  * the condition indicated by @condition becomes
753666a3af9SDaniel P. Berrange  * available.
754666a3af9SDaniel P. Berrange  *
755666a3af9SDaniel P. Berrange  * This will enter a nested event loop to perform
756666a3af9SDaniel P. Berrange  * the wait.
757666a3af9SDaniel P. Berrange  */
758666a3af9SDaniel P. Berrange void qio_channel_wait(QIOChannel *ioc,
759666a3af9SDaniel P. Berrange                       GIOCondition condition);
760666a3af9SDaniel P. Berrange 
761bf88c124SPaolo Bonzini /**
762bf88c124SPaolo Bonzini  * qio_channel_set_aio_fd_handler:
763bf88c124SPaolo Bonzini  * @ioc: the channel object
764bf88c124SPaolo Bonzini  * @ctx: the AioContext to set the handlers on
765bf88c124SPaolo Bonzini  * @io_read: the read handler
766bf88c124SPaolo Bonzini  * @io_write: the write handler
767bf88c124SPaolo Bonzini  * @opaque: the opaque value passed to the handler
768bf88c124SPaolo Bonzini  *
769bf88c124SPaolo Bonzini  * This is used internally by qio_channel_yield().  It can
770bf88c124SPaolo Bonzini  * be used by channel implementations to forward the handlers
771bf88c124SPaolo Bonzini  * to another channel (e.g. from #QIOChannelTLS to the
772bf88c124SPaolo Bonzini  * underlying socket).
773bf88c124SPaolo Bonzini  */
774bf88c124SPaolo Bonzini void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
775bf88c124SPaolo Bonzini                                     AioContext *ctx,
776bf88c124SPaolo Bonzini                                     IOHandler *io_read,
777bf88c124SPaolo Bonzini                                     IOHandler *io_write,
778bf88c124SPaolo Bonzini                                     void *opaque);
779bf88c124SPaolo Bonzini 
780*bfa42387SElena Ufimtseva /**
781*bfa42387SElena Ufimtseva  * qio_channel_writev_full_all:
782*bfa42387SElena Ufimtseva  * @ioc: the channel object
783*bfa42387SElena Ufimtseva  * @iov: the array of memory regions to write data from
784*bfa42387SElena Ufimtseva  * @niov: the length of the @iov array
785*bfa42387SElena Ufimtseva  * @fds: an array of file handles to send
786*bfa42387SElena Ufimtseva  * @nfds: number of file handles in @fds
787*bfa42387SElena Ufimtseva  * @errp: pointer to a NULL-initialized error object
788*bfa42387SElena Ufimtseva  *
789*bfa42387SElena Ufimtseva  *
790*bfa42387SElena Ufimtseva  * Behaves like qio_channel_writev_full but will attempt
791*bfa42387SElena Ufimtseva  * to send all data passed (file handles and memory regions).
792*bfa42387SElena Ufimtseva  * The function will wait for all requested data
793*bfa42387SElena Ufimtseva  * to be written, yielding from the current coroutine
794*bfa42387SElena Ufimtseva  * if required.
795*bfa42387SElena Ufimtseva  *
796*bfa42387SElena Ufimtseva  * Returns: 0 if all bytes were written, or -1 on error
797*bfa42387SElena Ufimtseva  */
798*bfa42387SElena Ufimtseva 
799*bfa42387SElena Ufimtseva int qio_channel_writev_full_all(QIOChannel *ioc,
800*bfa42387SElena Ufimtseva                                 const struct iovec *iov,
801*bfa42387SElena Ufimtseva                                 size_t niov,
802*bfa42387SElena Ufimtseva                                 int *fds, size_t nfds,
803*bfa42387SElena Ufimtseva                                 Error **errp);
804*bfa42387SElena Ufimtseva 
8052a6a4076SMarkus Armbruster #endif /* QIO_CHANNEL_H */
806