xref: /qemu/include/io/channel.h (revision 7c1f51bf38de8cea4ed5030467646c37b46edeb7)
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,
47666a3af9SDaniel P. Berrange };
48666a3af9SDaniel P. Berrange 
49666a3af9SDaniel P. Berrange 
50666a3af9SDaniel P. Berrange typedef enum QIOChannelShutdown QIOChannelShutdown;
51666a3af9SDaniel P. Berrange 
52666a3af9SDaniel P. Berrange enum QIOChannelShutdown {
53a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_READ = 1,
54a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_WRITE = 2,
55a2458b6fSDaniel P. Berrangé     QIO_CHANNEL_SHUTDOWN_BOTH = 3,
56666a3af9SDaniel P. Berrange };
57666a3af9SDaniel P. Berrange 
58666a3af9SDaniel P. Berrange typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc,
59666a3af9SDaniel P. Berrange                                    GIOCondition condition,
60666a3af9SDaniel P. Berrange                                    gpointer data);
61666a3af9SDaniel P. Berrange 
62666a3af9SDaniel P. Berrange /**
63666a3af9SDaniel P. Berrange  * QIOChannel:
64666a3af9SDaniel P. Berrange  *
65666a3af9SDaniel P. Berrange  * The QIOChannel defines the core API for a generic I/O channel
66666a3af9SDaniel P. Berrange  * class hierarchy. It is inspired by GIOChannel, but has the
67666a3af9SDaniel P. Berrange  * following differences
68666a3af9SDaniel P. Berrange  *
69666a3af9SDaniel P. Berrange  *  - Use QOM to properly support arbitrary subclassing
70666a3af9SDaniel P. Berrange  *  - Support use of iovecs for efficient I/O with multiple blocks
71666a3af9SDaniel P. Berrange  *  - None of the character set translation, binary data exclusively
72666a3af9SDaniel P. Berrange  *  - Direct support for QEMU Error object reporting
73666a3af9SDaniel P. Berrange  *  - File descriptor passing
74666a3af9SDaniel P. Berrange  *
75666a3af9SDaniel P. Berrange  * This base class is abstract so cannot be instantiated. There
76666a3af9SDaniel P. Berrange  * will be subclasses for dealing with sockets, files, and higher
77666a3af9SDaniel P. Berrange  * level protocols such as TLS, WebSocket, etc.
78666a3af9SDaniel P. Berrange  */
79666a3af9SDaniel P. Berrange 
80666a3af9SDaniel P. Berrange struct QIOChannel {
81666a3af9SDaniel P. Berrange     Object parent;
82666a3af9SDaniel P. Berrange     unsigned int features; /* bitmask of QIOChannelFeatures */
8320f4aa26SDaniel P. Berrange     char *name;
84c4c497d2SPaolo Bonzini     AioContext *ctx;
85c4c497d2SPaolo Bonzini     Coroutine *read_coroutine;
86c4c497d2SPaolo Bonzini     Coroutine *write_coroutine;
87a5897205SPaolo Bonzini #ifdef _WIN32
88a5897205SPaolo Bonzini     HANDLE event; /* For use with GSource on Win32 */
89a5897205SPaolo Bonzini #endif
90666a3af9SDaniel P. Berrange };
91666a3af9SDaniel P. Berrange 
92666a3af9SDaniel P. Berrange /**
93666a3af9SDaniel P. Berrange  * QIOChannelClass:
94666a3af9SDaniel P. Berrange  *
95666a3af9SDaniel P. Berrange  * This class defines the contract that all subclasses
96666a3af9SDaniel P. Berrange  * must follow to provide specific channel implementations.
97666a3af9SDaniel P. Berrange  * The first five callbacks are mandatory to support, others
98666a3af9SDaniel P. Berrange  * provide additional optional features.
99666a3af9SDaniel P. Berrange  *
100666a3af9SDaniel P. Berrange  * Consult the corresponding public API docs for a description
1018659f317SLukas Straub  * of the semantics of each callback. io_shutdown in particular
1028659f317SLukas Straub  * must be thread-safe, terminate quickly and must not block.
103666a3af9SDaniel P. Berrange  */
104666a3af9SDaniel P. Berrange struct QIOChannelClass {
105666a3af9SDaniel P. Berrange     ObjectClass parent;
106666a3af9SDaniel P. Berrange 
107666a3af9SDaniel P. Berrange     /* Mandatory callbacks */
108666a3af9SDaniel P. Berrange     ssize_t (*io_writev)(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,
113b88651cbSLeonardo Bras                          int flags,
114666a3af9SDaniel P. Berrange                          Error **errp);
115666a3af9SDaniel P. Berrange     ssize_t (*io_readv)(QIOChannel *ioc,
116666a3af9SDaniel P. Berrange                         const struct iovec *iov,
117666a3af9SDaniel P. Berrange                         size_t niov,
118666a3af9SDaniel P. Berrange                         int **fds,
119666a3af9SDaniel P. Berrange                         size_t *nfds,
12084615a19Smanish.mishra                         int flags,
121666a3af9SDaniel P. Berrange                         Error **errp);
122666a3af9SDaniel P. Berrange     int (*io_close)(QIOChannel *ioc,
123666a3af9SDaniel P. Berrange                     Error **errp);
124666a3af9SDaniel P. Berrange     GSource * (*io_create_watch)(QIOChannel *ioc,
125666a3af9SDaniel P. Berrange                                  GIOCondition condition);
126666a3af9SDaniel P. Berrange     int (*io_set_blocking)(QIOChannel *ioc,
127666a3af9SDaniel P. Berrange                            bool enabled,
128666a3af9SDaniel P. Berrange                            Error **errp);
129666a3af9SDaniel P. Berrange 
130666a3af9SDaniel P. Berrange     /* Optional callbacks */
131666a3af9SDaniel P. Berrange     int (*io_shutdown)(QIOChannel *ioc,
132666a3af9SDaniel P. Berrange                        QIOChannelShutdown how,
133666a3af9SDaniel P. Berrange                        Error **errp);
134666a3af9SDaniel P. Berrange     void (*io_set_cork)(QIOChannel *ioc,
135666a3af9SDaniel P. Berrange                         bool enabled);
136666a3af9SDaniel P. Berrange     void (*io_set_delay)(QIOChannel *ioc,
137666a3af9SDaniel P. Berrange                          bool enabled);
138666a3af9SDaniel P. Berrange     off_t (*io_seek)(QIOChannel *ioc,
139666a3af9SDaniel P. Berrange                      off_t offset,
140666a3af9SDaniel P. Berrange                      int whence,
141666a3af9SDaniel P. Berrange                      Error **errp);
142bf88c124SPaolo Bonzini     void (*io_set_aio_fd_handler)(QIOChannel *ioc,
143bf88c124SPaolo Bonzini                                   AioContext *ctx,
144bf88c124SPaolo Bonzini                                   IOHandler *io_read,
145bf88c124SPaolo Bonzini                                   IOHandler *io_write,
146bf88c124SPaolo Bonzini                                   void *opaque);
147b88651cbSLeonardo Bras     int (*io_flush)(QIOChannel *ioc,
148b88651cbSLeonardo Bras                     Error **errp);
149666a3af9SDaniel P. Berrange };
150666a3af9SDaniel P. Berrange 
151666a3af9SDaniel P. Berrange /* General I/O handling functions */
152666a3af9SDaniel P. Berrange 
153666a3af9SDaniel P. Berrange /**
154666a3af9SDaniel P. Berrange  * qio_channel_has_feature:
155666a3af9SDaniel P. Berrange  * @ioc: the channel object
156666a3af9SDaniel P. Berrange  * @feature: the feature to check support of
157666a3af9SDaniel P. Berrange  *
158666a3af9SDaniel P. Berrange  * Determine whether the channel implementation supports
159666a3af9SDaniel P. Berrange  * the optional feature named in @feature.
160666a3af9SDaniel P. Berrange  *
161666a3af9SDaniel P. Berrange  * Returns: true if supported, false otherwise.
162666a3af9SDaniel P. Berrange  */
163666a3af9SDaniel P. Berrange bool qio_channel_has_feature(QIOChannel *ioc,
164666a3af9SDaniel P. Berrange                              QIOChannelFeature feature);
165666a3af9SDaniel P. Berrange 
166666a3af9SDaniel P. Berrange /**
167d8d3c7ccSFelipe Franciosi  * qio_channel_set_feature:
168d8d3c7ccSFelipe Franciosi  * @ioc: the channel object
169d8d3c7ccSFelipe Franciosi  * @feature: the feature to set support for
170d8d3c7ccSFelipe Franciosi  *
171d8d3c7ccSFelipe Franciosi  * Add channel support for the feature named in @feature.
172d8d3c7ccSFelipe Franciosi  */
173d8d3c7ccSFelipe Franciosi void qio_channel_set_feature(QIOChannel *ioc,
174d8d3c7ccSFelipe Franciosi                              QIOChannelFeature feature);
175d8d3c7ccSFelipe Franciosi 
176d8d3c7ccSFelipe Franciosi /**
17720f4aa26SDaniel P. Berrange  * qio_channel_set_name:
17820f4aa26SDaniel P. Berrange  * @ioc: the channel object
17920f4aa26SDaniel P. Berrange  * @name: the name of the channel
18020f4aa26SDaniel P. Berrange  *
18120f4aa26SDaniel P. Berrange  * Sets the name of the channel, which serves as an aid
18220f4aa26SDaniel P. Berrange  * to debugging. The name is used when creating GSource
18320f4aa26SDaniel P. Berrange  * watches for this channel.
18420f4aa26SDaniel P. Berrange  */
18520f4aa26SDaniel P. Berrange void qio_channel_set_name(QIOChannel *ioc,
18620f4aa26SDaniel P. Berrange                           const char *name);
18720f4aa26SDaniel P. Berrange 
18820f4aa26SDaniel P. Berrange /**
189666a3af9SDaniel P. Berrange  * qio_channel_readv_full:
190666a3af9SDaniel P. Berrange  * @ioc: the channel object
191666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
192666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
193666a3af9SDaniel P. Berrange  * @fds: pointer to an array that will received file handles
194666a3af9SDaniel P. Berrange  * @nfds: pointer filled with number of elements in @fds on return
19584615a19Smanish.mishra  * @flags: read flags (QIO_CHANNEL_READ_FLAG_*)
196821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
197666a3af9SDaniel P. Berrange  *
198666a3af9SDaniel P. Berrange  * Read data from the IO channel, storing it in the
199666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
200666a3af9SDaniel P. Berrange  * in the @iov will be fully populated with data
201666a3af9SDaniel P. Berrange  * before the next one is used. The @niov parameter
202666a3af9SDaniel P. Berrange  * specifies the total number of elements in @iov.
203666a3af9SDaniel P. Berrange  *
204666a3af9SDaniel P. Berrange  * It is not required for all @iov to be filled with
205666a3af9SDaniel P. Berrange  * data. If the channel is in blocking mode, at least
206666a3af9SDaniel P. Berrange  * one byte of data will be read, but no more is
207666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
208666a3af9SDaniel P. Berrange  * data is available, it will return QIO_CHANNEL_ERR_BLOCK
209666a3af9SDaniel P. Berrange  *
210666a3af9SDaniel P. Berrange  * If the channel has passed any file descriptors,
211666a3af9SDaniel P. Berrange  * the @fds array pointer will be allocated and
212666a3af9SDaniel P. Berrange  * the elements filled with the received file
213666a3af9SDaniel P. Berrange  * descriptors. The @nfds pointer will be updated
214666a3af9SDaniel P. Berrange  * to indicate the size of the @fds array that
215666a3af9SDaniel P. Berrange  * was allocated. It is the callers responsibility
216666a3af9SDaniel P. Berrange  * to call close() on each file descriptor and to
217666a3af9SDaniel P. Berrange  * call g_free() on the array pointer in @fds.
218666a3af9SDaniel P. Berrange  *
219666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
220666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
221666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
222666a3af9SDaniel P. Berrange  *
223666a3af9SDaniel P. Berrange  * Returns: the number of bytes read, or -1 on error,
224666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is available
225666a3af9SDaniel P. Berrange  * and the channel is non-blocking
226666a3af9SDaniel P. Berrange  */
227666a3af9SDaniel P. Berrange ssize_t qio_channel_readv_full(QIOChannel *ioc,
228666a3af9SDaniel P. Berrange                                const struct iovec *iov,
229666a3af9SDaniel P. Berrange                                size_t niov,
230666a3af9SDaniel P. Berrange                                int **fds,
231666a3af9SDaniel P. Berrange                                size_t *nfds,
23284615a19Smanish.mishra                                int flags,
233666a3af9SDaniel P. Berrange                                Error **errp);
234666a3af9SDaniel P. Berrange 
235666a3af9SDaniel P. Berrange 
236666a3af9SDaniel P. Berrange /**
237666a3af9SDaniel P. Berrange  * qio_channel_writev_full:
238666a3af9SDaniel P. Berrange  * @ioc: the channel object
239666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
240666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
241666a3af9SDaniel P. Berrange  * @fds: an array of file handles to send
242666a3af9SDaniel P. Berrange  * @nfds: number of file handles in @fds
243b88651cbSLeonardo Bras  * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
244821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
245666a3af9SDaniel P. Berrange  *
246666a3af9SDaniel P. Berrange  * Write data to the IO channel, reading it from the
247666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
248666a3af9SDaniel P. Berrange  * in the @iov will be fully sent, before the next
249666a3af9SDaniel P. Berrange  * one is used. The @niov parameter specifies the
250666a3af9SDaniel P. Berrange  * total number of elements in @iov.
251666a3af9SDaniel P. Berrange  *
252666a3af9SDaniel P. Berrange  * It is not required for all @iov data to be fully
253666a3af9SDaniel P. Berrange  * sent. If the channel is in blocking mode, at least
254666a3af9SDaniel P. Berrange  * one byte of data will be sent, but no more is
255666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
256666a3af9SDaniel P. Berrange  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
257666a3af9SDaniel P. Berrange  *
258666a3af9SDaniel P. Berrange  * If there are file descriptors to send, the @fds
259666a3af9SDaniel P. Berrange  * array should be non-NULL and provide the handles.
260666a3af9SDaniel P. Berrange  * All file descriptors will be sent if at least one
261666a3af9SDaniel P. Berrange  * byte of data was sent.
262666a3af9SDaniel P. Berrange  *
263666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
264666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
265666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
266666a3af9SDaniel P. Berrange  *
267666a3af9SDaniel P. Berrange  * Returns: the number of bytes sent, or -1 on error,
268666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
269666a3af9SDaniel P. Berrange  * and the channel is non-blocking
270666a3af9SDaniel P. Berrange  */
271666a3af9SDaniel P. Berrange ssize_t qio_channel_writev_full(QIOChannel *ioc,
272666a3af9SDaniel P. Berrange                                 const struct iovec *iov,
273666a3af9SDaniel P. Berrange                                 size_t niov,
274666a3af9SDaniel P. Berrange                                 int *fds,
275666a3af9SDaniel P. Berrange                                 size_t nfds,
276b88651cbSLeonardo Bras                                 int flags,
277666a3af9SDaniel P. Berrange                                 Error **errp);
278666a3af9SDaniel P. Berrange 
279666a3af9SDaniel P. Berrange /**
280e8ffaa31SEric Blake  * qio_channel_readv_all_eof:
281e8ffaa31SEric Blake  * @ioc: the channel object
282e8ffaa31SEric Blake  * @iov: the array of memory regions to read data into
283e8ffaa31SEric Blake  * @niov: the length of the @iov array
284e8ffaa31SEric Blake  * @errp: pointer to a NULL-initialized error object
285e8ffaa31SEric Blake  *
286e8ffaa31SEric Blake  * Read data from the IO channel, storing it in the
287e8ffaa31SEric Blake  * memory regions referenced by @iov. Each element
288e8ffaa31SEric Blake  * in the @iov will be fully populated with data
289e8ffaa31SEric Blake  * before the next one is used. The @niov parameter
290e8ffaa31SEric Blake  * specifies the total number of elements in @iov.
291e8ffaa31SEric Blake  *
292e8ffaa31SEric Blake  * The function will wait for all requested data
293e8ffaa31SEric Blake  * to be read, yielding from the current coroutine
294e8ffaa31SEric Blake  * if required.
295e8ffaa31SEric Blake  *
296e8ffaa31SEric Blake  * If end-of-file occurs before any data is read,
297e8ffaa31SEric Blake  * no error is reported; otherwise, if it occurs
298e8ffaa31SEric Blake  * before all requested data has been read, an error
299e8ffaa31SEric Blake  * will be reported.
300e8ffaa31SEric Blake  *
301e8ffaa31SEric Blake  * Returns: 1 if all bytes were read, 0 if end-of-file
302e8ffaa31SEric Blake  *          occurs without data, or -1 on error
303e8ffaa31SEric Blake  */
3041dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_all_eof(QIOChannel *ioc,
305e8ffaa31SEric Blake                                                  const struct iovec *iov,
306e8ffaa31SEric Blake                                                  size_t niov,
307e8ffaa31SEric Blake                                                  Error **errp);
308e8ffaa31SEric Blake 
309e8ffaa31SEric Blake /**
310d4622e55SDaniel P. Berrange  * qio_channel_readv_all:
311d4622e55SDaniel P. Berrange  * @ioc: the channel object
312d4622e55SDaniel P. Berrange  * @iov: the array of memory regions to read data into
313d4622e55SDaniel P. Berrange  * @niov: the length of the @iov array
314d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
315d4622e55SDaniel P. Berrange  *
316d4622e55SDaniel P. Berrange  * Read data from the IO channel, storing it in the
317d4622e55SDaniel P. Berrange  * memory regions referenced by @iov. Each element
318d4622e55SDaniel P. Berrange  * in the @iov will be fully populated with data
319d4622e55SDaniel P. Berrange  * before the next one is used. The @niov parameter
320d4622e55SDaniel P. Berrange  * specifies the total number of elements in @iov.
321d4622e55SDaniel P. Berrange  *
322d4622e55SDaniel P. Berrange  * The function will wait for all requested data
323d4622e55SDaniel P. Berrange  * to be read, yielding from the current coroutine
324d4622e55SDaniel P. Berrange  * if required.
325d4622e55SDaniel P. Berrange  *
326d4622e55SDaniel P. Berrange  * If end-of-file occurs before all requested data
327d4622e55SDaniel P. Berrange  * has been read, an error will be reported.
328d4622e55SDaniel P. Berrange  *
329d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were read, or -1 on error
330d4622e55SDaniel P. Berrange  */
3311dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_all(QIOChannel *ioc,
332d4622e55SDaniel P. Berrange                                              const struct iovec *iov,
333d4622e55SDaniel P. Berrange                                              size_t niov,
334d4622e55SDaniel P. Berrange                                              Error **errp);
335d4622e55SDaniel P. Berrange 
336d4622e55SDaniel P. Berrange 
337d4622e55SDaniel P. Berrange /**
338d4622e55SDaniel P. Berrange  * qio_channel_writev_all:
339d4622e55SDaniel P. Berrange  * @ioc: the channel object
340d4622e55SDaniel P. Berrange  * @iov: the array of memory regions to write data from
341d4622e55SDaniel P. Berrange  * @niov: the length of the @iov array
342d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
343d4622e55SDaniel P. Berrange  *
344d4622e55SDaniel P. Berrange  * Write data to the IO channel, reading it from the
345d4622e55SDaniel P. Berrange  * memory regions referenced by @iov. Each element
346d4622e55SDaniel P. Berrange  * in the @iov will be fully sent, before the next
347d4622e55SDaniel P. Berrange  * one is used. The @niov parameter specifies the
348d4622e55SDaniel P. Berrange  * total number of elements in @iov.
349d4622e55SDaniel P. Berrange  *
350d4622e55SDaniel P. Berrange  * The function will wait for all requested data
351d4622e55SDaniel P. Berrange  * to be written, yielding from the current coroutine
352d4622e55SDaniel P. Berrange  * if required.
353d4622e55SDaniel P. Berrange  *
354d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were written, or -1 on error
355d4622e55SDaniel P. Berrange  */
3561dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_writev_all(QIOChannel *ioc,
357d4622e55SDaniel P. Berrange                                               const struct iovec *iov,
358d4622e55SDaniel P. Berrange                                               size_t niov,
35910220d2fSMarkus Armbruster                                               Error **errp);
360d4622e55SDaniel P. Berrange 
361d4622e55SDaniel P. Berrange /**
362666a3af9SDaniel P. Berrange  * qio_channel_readv:
363666a3af9SDaniel P. Berrange  * @ioc: the channel object
364666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
365666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
366821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
367666a3af9SDaniel P. Berrange  *
368666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
369666a3af9SDaniel P. Berrange  * receiving of file handles.
370666a3af9SDaniel P. Berrange  */
371666a3af9SDaniel P. Berrange ssize_t qio_channel_readv(QIOChannel *ioc,
372666a3af9SDaniel P. Berrange                           const struct iovec *iov,
373666a3af9SDaniel P. Berrange                           size_t niov,
374666a3af9SDaniel P. Berrange                           Error **errp);
375666a3af9SDaniel P. Berrange 
376666a3af9SDaniel P. Berrange /**
377666a3af9SDaniel P. Berrange  * qio_channel_writev:
378666a3af9SDaniel P. Berrange  * @ioc: the channel object
379666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
380666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
381821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
382666a3af9SDaniel P. Berrange  *
383666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
384666a3af9SDaniel P. Berrange  * sending of file handles.
385666a3af9SDaniel P. Berrange  */
386666a3af9SDaniel P. Berrange ssize_t qio_channel_writev(QIOChannel *ioc,
387666a3af9SDaniel P. Berrange                            const struct iovec *iov,
388666a3af9SDaniel P. Berrange                            size_t niov,
389666a3af9SDaniel P. Berrange                            Error **errp);
390666a3af9SDaniel P. Berrange 
391666a3af9SDaniel P. Berrange /**
39250ea44f0SDaniel P. Berrange  * qio_channel_read:
393666a3af9SDaniel P. Berrange  * @ioc: the channel object
394666a3af9SDaniel P. Berrange  * @buf: the memory region to read data into
395666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
396821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
397666a3af9SDaniel P. Berrange  *
398666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
399666a3af9SDaniel P. Berrange  * receiving of file handles, and only supports reading into
400666a3af9SDaniel P. Berrange  * a single memory region.
401666a3af9SDaniel P. Berrange  */
402666a3af9SDaniel P. Berrange ssize_t qio_channel_read(QIOChannel *ioc,
403666a3af9SDaniel P. Berrange                          char *buf,
404666a3af9SDaniel P. Berrange                          size_t buflen,
405666a3af9SDaniel P. Berrange                          Error **errp);
406666a3af9SDaniel P. Berrange 
407666a3af9SDaniel P. Berrange /**
40861f7c6a0SMarc-André Lureau  * qio_channel_write:
409666a3af9SDaniel P. Berrange  * @ioc: the channel object
410666a3af9SDaniel P. Berrange  * @buf: the memory regions to send data from
411666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
412821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
413666a3af9SDaniel P. Berrange  *
414666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
415666a3af9SDaniel P. Berrange  * sending of file handles, and only supports writing from a
416666a3af9SDaniel P. Berrange  * single memory region.
417666a3af9SDaniel P. Berrange  */
418666a3af9SDaniel P. Berrange ssize_t qio_channel_write(QIOChannel *ioc,
419666a3af9SDaniel P. Berrange                           const char *buf,
420666a3af9SDaniel P. Berrange                           size_t buflen,
421666a3af9SDaniel P. Berrange                           Error **errp);
422666a3af9SDaniel P. Berrange 
423666a3af9SDaniel P. Berrange /**
424e8ffaa31SEric Blake  * qio_channel_read_all_eof:
425e8ffaa31SEric Blake  * @ioc: the channel object
426e8ffaa31SEric Blake  * @buf: the memory region to read data into
427e8ffaa31SEric Blake  * @buflen: the number of bytes to @buf
428e8ffaa31SEric Blake  * @errp: pointer to a NULL-initialized error object
429e8ffaa31SEric Blake  *
430e8ffaa31SEric Blake  * Reads @buflen bytes into @buf, possibly blocking or (if the
431e8ffaa31SEric Blake  * channel is non-blocking) yielding from the current coroutine
432e8ffaa31SEric Blake  * multiple times until the entire content is read. If end-of-file
433e8ffaa31SEric Blake  * occurs immediately it is not an error, but if it occurs after
434e8ffaa31SEric Blake  * data has been read it will return an error rather than a
435e8ffaa31SEric Blake  * short-read. Otherwise behaves as qio_channel_read().
436e8ffaa31SEric Blake  *
437e8ffaa31SEric Blake  * Returns: 1 if all bytes were read, 0 if end-of-file occurs
438e8ffaa31SEric Blake  *          without data, or -1 on error
439e8ffaa31SEric Blake  */
4401dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_read_all_eof(QIOChannel *ioc,
441e8ffaa31SEric Blake                                                 char *buf,
442e8ffaa31SEric Blake                                                 size_t buflen,
443e8ffaa31SEric Blake                                                 Error **errp);
444e8ffaa31SEric Blake 
445e8ffaa31SEric Blake /**
446d4622e55SDaniel P. Berrange  * qio_channel_read_all:
447d4622e55SDaniel P. Berrange  * @ioc: the channel object
448d4622e55SDaniel P. Berrange  * @buf: the memory region to read data into
449d4622e55SDaniel P. Berrange  * @buflen: the number of bytes to @buf
450d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
451d4622e55SDaniel P. Berrange  *
452d4622e55SDaniel P. Berrange  * Reads @buflen bytes into @buf, possibly blocking or (if the
453d4622e55SDaniel P. Berrange  * channel is non-blocking) yielding from the current coroutine
454d4622e55SDaniel P. Berrange  * multiple times until the entire content is read. If end-of-file
455d4622e55SDaniel P. Berrange  * occurs it will return an error rather than a short-read. Otherwise
456d4622e55SDaniel P. Berrange  * behaves as qio_channel_read().
457d4622e55SDaniel P. Berrange  *
458d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were read, or -1 on error
459d4622e55SDaniel P. Berrange  */
4601dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_read_all(QIOChannel *ioc,
461d4622e55SDaniel P. Berrange                                             char *buf,
462d4622e55SDaniel P. Berrange                                             size_t buflen,
463d4622e55SDaniel P. Berrange                                             Error **errp);
464e8ffaa31SEric Blake 
465d4622e55SDaniel P. Berrange /**
466d4622e55SDaniel P. Berrange  * qio_channel_write_all:
467d4622e55SDaniel P. Berrange  * @ioc: the channel object
468d4622e55SDaniel P. Berrange  * @buf: the memory region to write data into
469d4622e55SDaniel P. Berrange  * @buflen: the number of bytes to @buf
470d4622e55SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
471d4622e55SDaniel P. Berrange  *
472d4622e55SDaniel P. Berrange  * Writes @buflen bytes from @buf, possibly blocking or (if the
473d4622e55SDaniel P. Berrange  * channel is non-blocking) yielding from the current coroutine
474d4622e55SDaniel P. Berrange  * multiple times until the entire content is written.  Otherwise
475d4622e55SDaniel P. Berrange  * behaves as qio_channel_write().
476d4622e55SDaniel P. Berrange  *
477d4622e55SDaniel P. Berrange  * Returns: 0 if all bytes were written, or -1 on error
478d4622e55SDaniel P. Berrange  */
4791dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_write_all(QIOChannel *ioc,
480d4622e55SDaniel P. Berrange                                              const char *buf,
481d4622e55SDaniel P. Berrange                                              size_t buflen,
482d4622e55SDaniel P. Berrange                                              Error **errp);
483d4622e55SDaniel P. Berrange 
484d4622e55SDaniel P. Berrange /**
485666a3af9SDaniel P. Berrange  * qio_channel_set_blocking:
486666a3af9SDaniel P. Berrange  * @ioc: the channel object
487666a3af9SDaniel P. Berrange  * @enabled: the blocking flag state
488821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
489666a3af9SDaniel P. Berrange  *
490666a3af9SDaniel P. Berrange  * If @enabled is true, then the channel is put into
491666a3af9SDaniel P. Berrange  * blocking mode, otherwise it will be non-blocking.
492666a3af9SDaniel P. Berrange  *
493666a3af9SDaniel P. Berrange  * In non-blocking mode, read/write operations may
494666a3af9SDaniel P. Berrange  * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
495666a3af9SDaniel P. Berrange  * block on I/O
496666a3af9SDaniel P. Berrange  */
497666a3af9SDaniel P. Berrange int qio_channel_set_blocking(QIOChannel *ioc,
498666a3af9SDaniel P. Berrange                              bool enabled,
499666a3af9SDaniel P. Berrange                              Error **errp);
500666a3af9SDaniel P. Berrange 
501666a3af9SDaniel P. Berrange /**
502666a3af9SDaniel P. Berrange  * qio_channel_close:
503666a3af9SDaniel P. Berrange  * @ioc: the channel object
504821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
505666a3af9SDaniel P. Berrange  *
506666a3af9SDaniel P. Berrange  * Close the channel, flushing any pending I/O
507666a3af9SDaniel P. Berrange  *
508666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
509666a3af9SDaniel P. Berrange  */
510666a3af9SDaniel P. Berrange int qio_channel_close(QIOChannel *ioc,
511666a3af9SDaniel P. Berrange                       Error **errp);
512666a3af9SDaniel P. Berrange 
513666a3af9SDaniel P. Berrange /**
514666a3af9SDaniel P. Berrange  * qio_channel_shutdown:
515666a3af9SDaniel P. Berrange  * @ioc: the channel object
516666a3af9SDaniel P. Berrange  * @how: the direction to shutdown
517821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
518666a3af9SDaniel P. Berrange  *
519666a3af9SDaniel P. Berrange  * Shutdowns transmission and/or receiving of data
520666a3af9SDaniel P. Berrange  * without closing the underlying transport.
521666a3af9SDaniel P. Berrange  *
522666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
523666a3af9SDaniel P. Berrange  * so may report an error. To avoid errors, the
524666a3af9SDaniel P. Berrange  * caller may check for the feature flag
525666a3af9SDaniel P. Berrange  * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling
526666a3af9SDaniel P. Berrange  * this method.
527666a3af9SDaniel P. Berrange  *
5288659f317SLukas Straub  * This function is thread-safe, terminates quickly and does not block.
5298659f317SLukas Straub  *
530666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
531666a3af9SDaniel P. Berrange  */
532666a3af9SDaniel P. Berrange int qio_channel_shutdown(QIOChannel *ioc,
533666a3af9SDaniel P. Berrange                          QIOChannelShutdown how,
534666a3af9SDaniel P. Berrange                          Error **errp);
535666a3af9SDaniel P. Berrange 
536666a3af9SDaniel P. Berrange /**
537666a3af9SDaniel P. Berrange  * qio_channel_set_delay:
538666a3af9SDaniel P. Berrange  * @ioc: the channel object
539666a3af9SDaniel P. Berrange  * @enabled: the new flag state
540666a3af9SDaniel P. Berrange  *
541666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
542666a3af9SDaniel P. Berrange  * permitted to delay writes in order to merge
543666a3af9SDaniel P. Berrange  * small packets. If @enabled is true, then the
544666a3af9SDaniel P. Berrange  * writes may be delayed in order to opportunistically
545666a3af9SDaniel P. Berrange  * merge small packets into larger ones. If @enabled
546666a3af9SDaniel P. Berrange  * is false, writes are dispatched immediately with
547666a3af9SDaniel P. Berrange  * no delay.
548666a3af9SDaniel P. Berrange  *
549666a3af9SDaniel P. Berrange  * When @enabled is false, applications may wish to
550666a3af9SDaniel P. Berrange  * use the qio_channel_set_cork() method to explicitly
551666a3af9SDaniel P. Berrange  * control write merging.
552666a3af9SDaniel P. Berrange  *
553666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
554666a3af9SDaniel P. Berrange  * API corresponds to the inverse of TCP_NODELAY flag,
555666a3af9SDaniel P. Berrange  * controlling whether the Nagle algorithm is active.
556666a3af9SDaniel P. Berrange  *
557666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
558666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
559666a3af9SDaniel P. Berrange  * error.
560666a3af9SDaniel P. Berrange  */
561666a3af9SDaniel P. Berrange void qio_channel_set_delay(QIOChannel *ioc,
562666a3af9SDaniel P. Berrange                            bool enabled);
563666a3af9SDaniel P. Berrange 
564666a3af9SDaniel P. Berrange /**
565666a3af9SDaniel P. Berrange  * qio_channel_set_cork:
566666a3af9SDaniel P. Berrange  * @ioc: the channel object
567666a3af9SDaniel P. Berrange  * @enabled: the new flag state
568666a3af9SDaniel P. Berrange  *
569666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
570666a3af9SDaniel P. Berrange  * permitted to dispatch data that is written.
571666a3af9SDaniel P. Berrange  * If @enabled is true, then any data written will
572666a3af9SDaniel P. Berrange  * be queued in local buffers until @enabled is
573666a3af9SDaniel P. Berrange  * set to false once again.
574666a3af9SDaniel P. Berrange  *
575666a3af9SDaniel P. Berrange  * This feature is typically used when the automatic
576666a3af9SDaniel P. Berrange  * write coalescing facility is disabled via the
577666a3af9SDaniel P. Berrange  * qio_channel_set_delay() method.
578666a3af9SDaniel P. Berrange  *
579666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
580666a3af9SDaniel P. Berrange  * API corresponds to the TCP_CORK flag.
581666a3af9SDaniel P. Berrange  *
582666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
583666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
584666a3af9SDaniel P. Berrange  * error.
585666a3af9SDaniel P. Berrange  */
586666a3af9SDaniel P. Berrange void qio_channel_set_cork(QIOChannel *ioc,
587666a3af9SDaniel P. Berrange                           bool enabled);
588666a3af9SDaniel P. Berrange 
589666a3af9SDaniel P. Berrange 
590666a3af9SDaniel P. Berrange /**
591666a3af9SDaniel P. Berrange  * qio_channel_seek:
592666a3af9SDaniel P. Berrange  * @ioc: the channel object
593666a3af9SDaniel P. Berrange  * @offset: the position to seek to, relative to @whence
594666a3af9SDaniel P. Berrange  * @whence: one of the (POSIX) SEEK_* constants listed below
595821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
596666a3af9SDaniel P. Berrange  *
597666a3af9SDaniel P. Berrange  * Moves the current I/O position within the channel
598666a3af9SDaniel P. Berrange  * @ioc, to be @offset. The value of @offset is
599666a3af9SDaniel P. Berrange  * interpreted relative to @whence:
600666a3af9SDaniel P. Berrange  *
601666a3af9SDaniel P. Berrange  * SEEK_SET - the position is set to @offset bytes
602666a3af9SDaniel P. Berrange  * SEEK_CUR - the position is moved by @offset bytes
603666a3af9SDaniel P. Berrange  * SEEK_END - the position is set to end of the file plus @offset bytes
604666a3af9SDaniel P. Berrange  *
605666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
606666a3af9SDaniel P. Berrange  * so may report an error.
607666a3af9SDaniel P. Berrange  *
608666a3af9SDaniel P. Berrange  * Returns: the new position on success, (off_t)-1 on failure
609666a3af9SDaniel P. Berrange  */
610666a3af9SDaniel P. Berrange off_t qio_channel_io_seek(QIOChannel *ioc,
611666a3af9SDaniel P. Berrange                           off_t offset,
612666a3af9SDaniel P. Berrange                           int whence,
613666a3af9SDaniel P. Berrange                           Error **errp);
614666a3af9SDaniel P. Berrange 
615666a3af9SDaniel P. Berrange 
616666a3af9SDaniel P. Berrange /**
617666a3af9SDaniel P. Berrange  * qio_channel_create_watch:
618666a3af9SDaniel P. Berrange  * @ioc: the channel object
619666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
620666a3af9SDaniel P. Berrange  *
621666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
622666a3af9SDaniel P. Berrange  * for the I/O condition @condition. Typically the
623666a3af9SDaniel P. Berrange  * qio_channel_add_watch() method would be used instead
624666a3af9SDaniel P. Berrange  * of this, since it directly attaches a callback to
625666a3af9SDaniel P. Berrange  * the source
626666a3af9SDaniel P. Berrange  *
627666a3af9SDaniel P. Berrange  * Returns: the new main loop source.
628666a3af9SDaniel P. Berrange  */
629666a3af9SDaniel P. Berrange GSource *qio_channel_create_watch(QIOChannel *ioc,
630666a3af9SDaniel P. Berrange                                   GIOCondition condition);
631666a3af9SDaniel P. Berrange 
632666a3af9SDaniel P. Berrange /**
633666a3af9SDaniel P. Berrange  * qio_channel_add_watch:
634666a3af9SDaniel P. Berrange  * @ioc: the channel object
635666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
636666a3af9SDaniel P. Berrange  * @func: callback to invoke when the source becomes ready
637666a3af9SDaniel P. Berrange  * @user_data: opaque data to pass to @func
638666a3af9SDaniel P. Berrange  * @notify: callback to free @user_data
639666a3af9SDaniel P. Berrange  *
640666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
641666a3af9SDaniel P. Berrange  * for the I/O condition @condition. The callback @func
642666a3af9SDaniel P. Berrange  * will be registered against the source, to be invoked
643666a3af9SDaniel P. Berrange  * when the source becomes ready. The optional @user_data
644666a3af9SDaniel P. Berrange  * will be passed to @func when it is invoked. The @notify
645666a3af9SDaniel P. Berrange  * callback will be used to free @user_data when the
646666a3af9SDaniel P. Berrange  * watch is deleted
647666a3af9SDaniel P. Berrange  *
648666a3af9SDaniel P. Berrange  * The returned source ID can be used with g_source_remove()
649666a3af9SDaniel P. Berrange  * to remove and free the source when no longer required.
650666a3af9SDaniel P. Berrange  * Alternatively the @func callback can return a FALSE
651666a3af9SDaniel P. Berrange  * value.
652666a3af9SDaniel P. Berrange  *
653666a3af9SDaniel P. Berrange  * Returns: the source ID
654666a3af9SDaniel P. Berrange  */
655666a3af9SDaniel P. Berrange guint qio_channel_add_watch(QIOChannel *ioc,
656666a3af9SDaniel P. Berrange                             GIOCondition condition,
657666a3af9SDaniel P. Berrange                             QIOChannelFunc func,
658666a3af9SDaniel P. Berrange                             gpointer user_data,
659666a3af9SDaniel P. Berrange                             GDestroyNotify notify);
660666a3af9SDaniel P. Berrange 
661315409c7SPeter Xu /**
662315409c7SPeter Xu  * qio_channel_add_watch_full:
663315409c7SPeter Xu  * @ioc: the channel object
664315409c7SPeter Xu  * @condition: the I/O condition to monitor
665315409c7SPeter Xu  * @func: callback to invoke when the source becomes ready
666315409c7SPeter Xu  * @user_data: opaque data to pass to @func
667315409c7SPeter Xu  * @notify: callback to free @user_data
668315409c7SPeter Xu  * @context: the context to run the watch source
669315409c7SPeter Xu  *
670315409c7SPeter Xu  * Similar as qio_channel_add_watch(), but allows to specify context
671315409c7SPeter Xu  * to run the watch source.
672315409c7SPeter Xu  *
673315409c7SPeter Xu  * Returns: the source ID
674315409c7SPeter Xu  */
675315409c7SPeter Xu guint qio_channel_add_watch_full(QIOChannel *ioc,
676315409c7SPeter Xu                                  GIOCondition condition,
677315409c7SPeter Xu                                  QIOChannelFunc func,
678315409c7SPeter Xu                                  gpointer user_data,
679315409c7SPeter Xu                                  GDestroyNotify notify,
680315409c7SPeter Xu                                  GMainContext *context);
681315409c7SPeter Xu 
682315409c7SPeter Xu /**
683315409c7SPeter Xu  * qio_channel_add_watch_source:
684315409c7SPeter Xu  * @ioc: the channel object
685315409c7SPeter Xu  * @condition: the I/O condition to monitor
686315409c7SPeter Xu  * @func: callback to invoke when the source becomes ready
687315409c7SPeter Xu  * @user_data: opaque data to pass to @func
688315409c7SPeter Xu  * @notify: callback to free @user_data
689315409c7SPeter Xu  * @context: gcontext to bind the source to
690315409c7SPeter Xu  *
691315409c7SPeter Xu  * Similar as qio_channel_add_watch(), but allows to specify context
692315409c7SPeter Xu  * to run the watch source, meanwhile return the GSource object
693315409c7SPeter Xu  * instead of tag ID, with the GSource referenced already.
694315409c7SPeter Xu  *
695315409c7SPeter Xu  * Note: callers is responsible to unref the source when not needed.
696315409c7SPeter Xu  *
697315409c7SPeter Xu  * Returns: the source pointer
698315409c7SPeter Xu  */
699315409c7SPeter Xu GSource *qio_channel_add_watch_source(QIOChannel *ioc,
700315409c7SPeter Xu                                       GIOCondition condition,
701315409c7SPeter Xu                                       QIOChannelFunc func,
702315409c7SPeter Xu                                       gpointer user_data,
703315409c7SPeter Xu                                       GDestroyNotify notify,
704315409c7SPeter Xu                                       GMainContext *context);
705666a3af9SDaniel P. Berrange 
706666a3af9SDaniel P. Berrange /**
707c4c497d2SPaolo Bonzini  * qio_channel_attach_aio_context:
708c4c497d2SPaolo Bonzini  * @ioc: the channel object
709c4c497d2SPaolo Bonzini  * @ctx: the #AioContext to set the handlers on
710c4c497d2SPaolo Bonzini  *
711c4c497d2SPaolo Bonzini  * Request that qio_channel_yield() sets I/O handlers on
712c4c497d2SPaolo Bonzini  * the given #AioContext.  If @ctx is %NULL, qio_channel_yield()
713c4c497d2SPaolo Bonzini  * uses QEMU's main thread event loop.
714c4c497d2SPaolo Bonzini  *
715c4c497d2SPaolo Bonzini  * You can move a #QIOChannel from one #AioContext to another even if
716c4c497d2SPaolo Bonzini  * I/O handlers are set for a coroutine.  However, #QIOChannel provides
717c4c497d2SPaolo Bonzini  * no synchronization between the calls to qio_channel_yield() and
718c4c497d2SPaolo Bonzini  * qio_channel_attach_aio_context().
719c4c497d2SPaolo Bonzini  *
720c4c497d2SPaolo Bonzini  * Therefore you should first call qio_channel_detach_aio_context()
721c4c497d2SPaolo Bonzini  * to ensure that the coroutine is not entered concurrently.  Then,
722c4c497d2SPaolo Bonzini  * while the coroutine has yielded, call qio_channel_attach_aio_context(),
723c4c497d2SPaolo Bonzini  * and then aio_co_schedule() to place the coroutine on the new
724c4c497d2SPaolo Bonzini  * #AioContext.  The calls to qio_channel_detach_aio_context()
725c4c497d2SPaolo Bonzini  * and qio_channel_attach_aio_context() should be protected with
726c4c497d2SPaolo Bonzini  * aio_context_acquire() and aio_context_release().
727c4c497d2SPaolo Bonzini  */
728c4c497d2SPaolo Bonzini void qio_channel_attach_aio_context(QIOChannel *ioc,
729c4c497d2SPaolo Bonzini                                     AioContext *ctx);
730c4c497d2SPaolo Bonzini 
731c4c497d2SPaolo Bonzini /**
732c4c497d2SPaolo Bonzini  * qio_channel_detach_aio_context:
733c4c497d2SPaolo Bonzini  * @ioc: the channel object
734c4c497d2SPaolo Bonzini  *
735c4c497d2SPaolo Bonzini  * Disable any I/O handlers set by qio_channel_yield().  With the
736c4c497d2SPaolo Bonzini  * help of aio_co_schedule(), this allows moving a coroutine that was
737c4c497d2SPaolo Bonzini  * paused by qio_channel_yield() to another context.
738c4c497d2SPaolo Bonzini  */
739c4c497d2SPaolo Bonzini void qio_channel_detach_aio_context(QIOChannel *ioc);
740c4c497d2SPaolo Bonzini 
741c4c497d2SPaolo Bonzini /**
742666a3af9SDaniel P. Berrange  * qio_channel_yield:
743666a3af9SDaniel P. Berrange  * @ioc: the channel object
744666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
745666a3af9SDaniel P. Berrange  *
746c4c497d2SPaolo Bonzini  * Yields execution from the current coroutine until the condition
747c4c497d2SPaolo Bonzini  * indicated by @condition becomes available.  @condition must
748c4c497d2SPaolo Bonzini  * be either %G_IO_IN or %G_IO_OUT; it cannot contain both.  In
749c4c497d2SPaolo Bonzini  * addition, no two coroutine can be waiting on the same condition
750c4c497d2SPaolo Bonzini  * and channel at the same time.
751666a3af9SDaniel P. Berrange  *
7526886ceafSKevin Wolf  * This must only be called from coroutine context. It is safe to
7536886ceafSKevin Wolf  * reenter the coroutine externally while it is waiting; in this
7546886ceafSKevin Wolf  * case the function will return even if @condition is not yet
7556886ceafSKevin Wolf  * available.
756666a3af9SDaniel P. Berrange  */
7576886ceafSKevin Wolf void coroutine_fn qio_channel_yield(QIOChannel *ioc,
758666a3af9SDaniel P. Berrange                                     GIOCondition condition);
759666a3af9SDaniel P. Berrange 
760666a3af9SDaniel P. Berrange /**
761*7c1f51bfSKevin Wolf  * qio_channel_wake_read:
762*7c1f51bfSKevin Wolf  * @ioc: the channel object
763*7c1f51bfSKevin Wolf  *
764*7c1f51bfSKevin Wolf  * If qio_channel_yield() is currently waiting for the channel to become
765*7c1f51bfSKevin Wolf  * readable, interrupt it and reenter immediately. This function is safe to call
766*7c1f51bfSKevin Wolf  * from any thread.
767*7c1f51bfSKevin Wolf  */
768*7c1f51bfSKevin Wolf void qio_channel_wake_read(QIOChannel *ioc);
769*7c1f51bfSKevin Wolf 
770*7c1f51bfSKevin Wolf /**
771666a3af9SDaniel P. Berrange  * qio_channel_wait:
772666a3af9SDaniel P. Berrange  * @ioc: the channel object
773666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
774666a3af9SDaniel P. Berrange  *
775666a3af9SDaniel P. Berrange  * Block execution from the current thread until
776666a3af9SDaniel P. Berrange  * the condition indicated by @condition becomes
777666a3af9SDaniel P. Berrange  * available.
778666a3af9SDaniel P. Berrange  *
779666a3af9SDaniel P. Berrange  * This will enter a nested event loop to perform
780666a3af9SDaniel P. Berrange  * the wait.
781666a3af9SDaniel P. Berrange  */
782666a3af9SDaniel P. Berrange void qio_channel_wait(QIOChannel *ioc,
783666a3af9SDaniel P. Berrange                       GIOCondition condition);
784666a3af9SDaniel P. Berrange 
785bf88c124SPaolo Bonzini /**
786bf88c124SPaolo Bonzini  * qio_channel_set_aio_fd_handler:
787bf88c124SPaolo Bonzini  * @ioc: the channel object
788bf88c124SPaolo Bonzini  * @ctx: the AioContext to set the handlers on
789bf88c124SPaolo Bonzini  * @io_read: the read handler
790bf88c124SPaolo Bonzini  * @io_write: the write handler
791bf88c124SPaolo Bonzini  * @opaque: the opaque value passed to the handler
792bf88c124SPaolo Bonzini  *
793bf88c124SPaolo Bonzini  * This is used internally by qio_channel_yield().  It can
794bf88c124SPaolo Bonzini  * be used by channel implementations to forward the handlers
795bf88c124SPaolo Bonzini  * to another channel (e.g. from #QIOChannelTLS to the
796bf88c124SPaolo Bonzini  * underlying socket).
797bf88c124SPaolo Bonzini  */
798bf88c124SPaolo Bonzini void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
799bf88c124SPaolo Bonzini                                     AioContext *ctx,
800bf88c124SPaolo Bonzini                                     IOHandler *io_read,
801bf88c124SPaolo Bonzini                                     IOHandler *io_write,
802bf88c124SPaolo Bonzini                                     void *opaque);
803bf88c124SPaolo Bonzini 
804bfa42387SElena Ufimtseva /**
805bebab91eSElena Ufimtseva  * qio_channel_readv_full_all_eof:
806bebab91eSElena Ufimtseva  * @ioc: the channel object
807bebab91eSElena Ufimtseva  * @iov: the array of memory regions to read data to
808bebab91eSElena Ufimtseva  * @niov: the length of the @iov array
809bebab91eSElena Ufimtseva  * @fds: an array of file handles to read
810bebab91eSElena Ufimtseva  * @nfds: number of file handles in @fds
811bebab91eSElena Ufimtseva  * @errp: pointer to a NULL-initialized error object
812bebab91eSElena Ufimtseva  *
813bebab91eSElena Ufimtseva  *
814bebab91eSElena Ufimtseva  * Performs same function as qio_channel_readv_all_eof.
815bebab91eSElena Ufimtseva  * Additionally, attempts to read file descriptors shared
816bebab91eSElena Ufimtseva  * over the channel. The function will wait for all
817bebab91eSElena Ufimtseva  * requested data to be read, yielding from the current
818bebab91eSElena Ufimtseva  * coroutine if required. data refers to both file
819bebab91eSElena Ufimtseva  * descriptors and the iovs.
820bebab91eSElena Ufimtseva  *
821bebab91eSElena Ufimtseva  * Returns: 1 if all bytes were read, 0 if end-of-file
822bebab91eSElena Ufimtseva  *          occurs without data, or -1 on error
823bebab91eSElena Ufimtseva  */
824bebab91eSElena Ufimtseva 
8251dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_full_all_eof(QIOChannel *ioc,
826bebab91eSElena Ufimtseva                                                       const struct iovec *iov,
827bebab91eSElena Ufimtseva                                                       size_t niov,
828bebab91eSElena Ufimtseva                                                       int **fds, size_t *nfds,
829bebab91eSElena Ufimtseva                                                       Error **errp);
830bebab91eSElena Ufimtseva 
831bebab91eSElena Ufimtseva /**
832bebab91eSElena Ufimtseva  * qio_channel_readv_full_all:
833bebab91eSElena Ufimtseva  * @ioc: the channel object
834bebab91eSElena Ufimtseva  * @iov: the array of memory regions to read data to
835bebab91eSElena Ufimtseva  * @niov: the length of the @iov array
836bebab91eSElena Ufimtseva  * @fds: an array of file handles to read
837bebab91eSElena Ufimtseva  * @nfds: number of file handles in @fds
838bebab91eSElena Ufimtseva  * @errp: pointer to a NULL-initialized error object
839bebab91eSElena Ufimtseva  *
840bebab91eSElena Ufimtseva  *
841bebab91eSElena Ufimtseva  * Performs same function as qio_channel_readv_all_eof.
842bebab91eSElena Ufimtseva  * Additionally, attempts to read file descriptors shared
843bebab91eSElena Ufimtseva  * over the channel. The function will wait for all
844bebab91eSElena Ufimtseva  * requested data to be read, yielding from the current
845bebab91eSElena Ufimtseva  * coroutine if required. data refers to both file
846bebab91eSElena Ufimtseva  * descriptors and the iovs.
847bebab91eSElena Ufimtseva  *
848bebab91eSElena Ufimtseva  * Returns: 0 if all bytes were read, or -1 on error
849bebab91eSElena Ufimtseva  */
850bebab91eSElena Ufimtseva 
8511dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_readv_full_all(QIOChannel *ioc,
852bebab91eSElena Ufimtseva                                                   const struct iovec *iov,
853bebab91eSElena Ufimtseva                                                   size_t niov,
854bebab91eSElena Ufimtseva                                                   int **fds, size_t *nfds,
855bebab91eSElena Ufimtseva                                                   Error **errp);
856bebab91eSElena Ufimtseva 
857bebab91eSElena Ufimtseva /**
858bfa42387SElena Ufimtseva  * qio_channel_writev_full_all:
859bfa42387SElena Ufimtseva  * @ioc: the channel object
860bfa42387SElena Ufimtseva  * @iov: the array of memory regions to write data from
861bfa42387SElena Ufimtseva  * @niov: the length of the @iov array
862bfa42387SElena Ufimtseva  * @fds: an array of file handles to send
863bfa42387SElena Ufimtseva  * @nfds: number of file handles in @fds
864b88651cbSLeonardo Bras  * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
865bfa42387SElena Ufimtseva  * @errp: pointer to a NULL-initialized error object
866bfa42387SElena Ufimtseva  *
867bfa42387SElena Ufimtseva  *
868bfa42387SElena Ufimtseva  * Behaves like qio_channel_writev_full but will attempt
869bfa42387SElena Ufimtseva  * to send all data passed (file handles and memory regions).
870bfa42387SElena Ufimtseva  * The function will wait for all requested data
871bfa42387SElena Ufimtseva  * to be written, yielding from the current coroutine
872bfa42387SElena Ufimtseva  * if required.
873bfa42387SElena Ufimtseva  *
874b88651cbSLeonardo Bras  * If QIO_CHANNEL_WRITE_FLAG_ZERO_COPY is passed in flags,
875b88651cbSLeonardo Bras  * instead of waiting for all requested data to be written,
876b88651cbSLeonardo Bras  * this function will wait until it's all queued for writing.
877b88651cbSLeonardo Bras  * In this case, if the buffer gets changed between queueing and
878b88651cbSLeonardo Bras  * sending, the updated buffer will be sent. If this is not a
879b88651cbSLeonardo Bras  * desired behavior, it's suggested to call qio_channel_flush()
880b88651cbSLeonardo Bras  * before reusing the buffer.
881b88651cbSLeonardo Bras  *
882bfa42387SElena Ufimtseva  * Returns: 0 if all bytes were written, or -1 on error
883bfa42387SElena Ufimtseva  */
884bfa42387SElena Ufimtseva 
8851dd91b22SPaolo Bonzini int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc,
886bfa42387SElena Ufimtseva                                                    const struct iovec *iov,
887bfa42387SElena Ufimtseva                                                    size_t niov,
888bfa42387SElena Ufimtseva                                                    int *fds, size_t nfds,
889b88651cbSLeonardo Bras                                                    int flags, Error **errp);
890b88651cbSLeonardo Bras 
891b88651cbSLeonardo Bras /**
892b88651cbSLeonardo Bras  * qio_channel_flush:
893b88651cbSLeonardo Bras  * @ioc: the channel object
894b88651cbSLeonardo Bras  * @errp: pointer to a NULL-initialized error object
895b88651cbSLeonardo Bras  *
896b88651cbSLeonardo Bras  * Will block until every packet queued with
897b88651cbSLeonardo Bras  * qio_channel_writev_full() + QIO_CHANNEL_WRITE_FLAG_ZERO_COPY
898b88651cbSLeonardo Bras  * is sent, or return in case of any error.
899b88651cbSLeonardo Bras  *
900b88651cbSLeonardo Bras  * If not implemented, acts as a no-op, and returns 0.
901b88651cbSLeonardo Bras  *
902b88651cbSLeonardo Bras  * Returns -1 if any error is found,
903b88651cbSLeonardo Bras  *          1 if every send failed to use zero copy.
904b88651cbSLeonardo Bras  *          0 otherwise.
905b88651cbSLeonardo Bras  */
906b88651cbSLeonardo Bras 
907b88651cbSLeonardo Bras int qio_channel_flush(QIOChannel *ioc,
908bfa42387SElena Ufimtseva                       Error **errp);
909bfa42387SElena Ufimtseva 
9102a6a4076SMarkus Armbruster #endif /* QIO_CHANNEL_H */
911