xref: /qemu/include/io/channel.h (revision 50ea44f07744293b5f503411df8052bf113d8f1d)
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
9666a3af9SDaniel P. Berrange  * version 2 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 "qemu-common.h"
25666a3af9SDaniel P. Berrange #include "qom/object.h"
26c4c497d2SPaolo Bonzini #include "qemu/coroutine.h"
27bf88c124SPaolo Bonzini #include "block/aio.h"
28666a3af9SDaniel P. Berrange 
29666a3af9SDaniel P. Berrange #define TYPE_QIO_CHANNEL "qio-channel"
30666a3af9SDaniel P. Berrange #define QIO_CHANNEL(obj)                                    \
31666a3af9SDaniel P. Berrange     OBJECT_CHECK(QIOChannel, (obj), TYPE_QIO_CHANNEL)
32666a3af9SDaniel P. Berrange #define QIO_CHANNEL_CLASS(klass)                                    \
33666a3af9SDaniel P. Berrange     OBJECT_CLASS_CHECK(QIOChannelClass, klass, TYPE_QIO_CHANNEL)
34666a3af9SDaniel P. Berrange #define QIO_CHANNEL_GET_CLASS(obj)                                  \
35666a3af9SDaniel P. Berrange     OBJECT_GET_CLASS(QIOChannelClass, obj, TYPE_QIO_CHANNEL)
36666a3af9SDaniel P. Berrange 
37666a3af9SDaniel P. Berrange typedef struct QIOChannel QIOChannel;
38666a3af9SDaniel P. Berrange typedef struct QIOChannelClass QIOChannelClass;
39666a3af9SDaniel P. Berrange 
40666a3af9SDaniel P. Berrange #define QIO_CHANNEL_ERR_BLOCK -2
41666a3af9SDaniel P. Berrange 
42666a3af9SDaniel P. Berrange typedef enum QIOChannelFeature QIOChannelFeature;
43666a3af9SDaniel P. Berrange 
44666a3af9SDaniel P. Berrange enum QIOChannelFeature {
458fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_FD_PASS,
468fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_SHUTDOWN,
478fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_LISTEN,
48666a3af9SDaniel P. Berrange };
49666a3af9SDaniel P. Berrange 
50666a3af9SDaniel P. Berrange 
51666a3af9SDaniel P. Berrange typedef enum QIOChannelShutdown QIOChannelShutdown;
52666a3af9SDaniel P. Berrange 
53666a3af9SDaniel P. Berrange enum QIOChannelShutdown {
54666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_BOTH,
55666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_READ,
56666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_WRITE,
57666a3af9SDaniel P. Berrange };
58666a3af9SDaniel P. Berrange 
59666a3af9SDaniel P. Berrange typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc,
60666a3af9SDaniel P. Berrange                                    GIOCondition condition,
61666a3af9SDaniel P. Berrange                                    gpointer data);
62666a3af9SDaniel P. Berrange 
63666a3af9SDaniel P. Berrange /**
64666a3af9SDaniel P. Berrange  * QIOChannel:
65666a3af9SDaniel P. Berrange  *
66666a3af9SDaniel P. Berrange  * The QIOChannel defines the core API for a generic I/O channel
67666a3af9SDaniel P. Berrange  * class hierarchy. It is inspired by GIOChannel, but has the
68666a3af9SDaniel P. Berrange  * following differences
69666a3af9SDaniel P. Berrange  *
70666a3af9SDaniel P. Berrange  *  - Use QOM to properly support arbitrary subclassing
71666a3af9SDaniel P. Berrange  *  - Support use of iovecs for efficient I/O with multiple blocks
72666a3af9SDaniel P. Berrange  *  - None of the character set translation, binary data exclusively
73666a3af9SDaniel P. Berrange  *  - Direct support for QEMU Error object reporting
74666a3af9SDaniel P. Berrange  *  - File descriptor passing
75666a3af9SDaniel P. Berrange  *
76666a3af9SDaniel P. Berrange  * This base class is abstract so cannot be instantiated. There
77666a3af9SDaniel P. Berrange  * will be subclasses for dealing with sockets, files, and higher
78666a3af9SDaniel P. Berrange  * level protocols such as TLS, WebSocket, etc.
79666a3af9SDaniel P. Berrange  */
80666a3af9SDaniel P. Berrange 
81666a3af9SDaniel P. Berrange struct QIOChannel {
82666a3af9SDaniel P. Berrange     Object parent;
83666a3af9SDaniel P. Berrange     unsigned int features; /* bitmask of QIOChannelFeatures */
8420f4aa26SDaniel P. Berrange     char *name;
85c4c497d2SPaolo Bonzini     AioContext *ctx;
86c4c497d2SPaolo Bonzini     Coroutine *read_coroutine;
87c4c497d2SPaolo Bonzini     Coroutine *write_coroutine;
88a5897205SPaolo Bonzini #ifdef _WIN32
89a5897205SPaolo Bonzini     HANDLE event; /* For use with GSource on Win32 */
90a5897205SPaolo Bonzini #endif
91666a3af9SDaniel P. Berrange };
92666a3af9SDaniel P. Berrange 
93666a3af9SDaniel P. Berrange /**
94666a3af9SDaniel P. Berrange  * QIOChannelClass:
95666a3af9SDaniel P. Berrange  *
96666a3af9SDaniel P. Berrange  * This class defines the contract that all subclasses
97666a3af9SDaniel P. Berrange  * must follow to provide specific channel implementations.
98666a3af9SDaniel P. Berrange  * The first five callbacks are mandatory to support, others
99666a3af9SDaniel P. Berrange  * provide additional optional features.
100666a3af9SDaniel P. Berrange  *
101666a3af9SDaniel P. Berrange  * Consult the corresponding public API docs for a description
102666a3af9SDaniel P. Berrange  * of the semantics of each callback
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,
113666a3af9SDaniel P. Berrange                          Error **errp);
114666a3af9SDaniel P. Berrange     ssize_t (*io_readv)(QIOChannel *ioc,
115666a3af9SDaniel P. Berrange                         const struct iovec *iov,
116666a3af9SDaniel P. Berrange                         size_t niov,
117666a3af9SDaniel P. Berrange                         int **fds,
118666a3af9SDaniel P. Berrange                         size_t *nfds,
119666a3af9SDaniel P. Berrange                         Error **errp);
120666a3af9SDaniel P. Berrange     int (*io_close)(QIOChannel *ioc,
121666a3af9SDaniel P. Berrange                     Error **errp);
122666a3af9SDaniel P. Berrange     GSource * (*io_create_watch)(QIOChannel *ioc,
123666a3af9SDaniel P. Berrange                                  GIOCondition condition);
124666a3af9SDaniel P. Berrange     int (*io_set_blocking)(QIOChannel *ioc,
125666a3af9SDaniel P. Berrange                            bool enabled,
126666a3af9SDaniel P. Berrange                            Error **errp);
127666a3af9SDaniel P. Berrange 
128666a3af9SDaniel P. Berrange     /* Optional callbacks */
129666a3af9SDaniel P. Berrange     int (*io_shutdown)(QIOChannel *ioc,
130666a3af9SDaniel P. Berrange                        QIOChannelShutdown how,
131666a3af9SDaniel P. Berrange                        Error **errp);
132666a3af9SDaniel P. Berrange     void (*io_set_cork)(QIOChannel *ioc,
133666a3af9SDaniel P. Berrange                         bool enabled);
134666a3af9SDaniel P. Berrange     void (*io_set_delay)(QIOChannel *ioc,
135666a3af9SDaniel P. Berrange                          bool enabled);
136666a3af9SDaniel P. Berrange     off_t (*io_seek)(QIOChannel *ioc,
137666a3af9SDaniel P. Berrange                      off_t offset,
138666a3af9SDaniel P. Berrange                      int whence,
139666a3af9SDaniel P. Berrange                      Error **errp);
140bf88c124SPaolo Bonzini     void (*io_set_aio_fd_handler)(QIOChannel *ioc,
141bf88c124SPaolo Bonzini                                   AioContext *ctx,
142bf88c124SPaolo Bonzini                                   IOHandler *io_read,
143bf88c124SPaolo Bonzini                                   IOHandler *io_write,
144bf88c124SPaolo Bonzini                                   void *opaque);
145666a3af9SDaniel P. Berrange };
146666a3af9SDaniel P. Berrange 
147666a3af9SDaniel P. Berrange /* General I/O handling functions */
148666a3af9SDaniel P. Berrange 
149666a3af9SDaniel P. Berrange /**
150666a3af9SDaniel P. Berrange  * qio_channel_has_feature:
151666a3af9SDaniel P. Berrange  * @ioc: the channel object
152666a3af9SDaniel P. Berrange  * @feature: the feature to check support of
153666a3af9SDaniel P. Berrange  *
154666a3af9SDaniel P. Berrange  * Determine whether the channel implementation supports
155666a3af9SDaniel P. Berrange  * the optional feature named in @feature.
156666a3af9SDaniel P. Berrange  *
157666a3af9SDaniel P. Berrange  * Returns: true if supported, false otherwise.
158666a3af9SDaniel P. Berrange  */
159666a3af9SDaniel P. Berrange bool qio_channel_has_feature(QIOChannel *ioc,
160666a3af9SDaniel P. Berrange                              QIOChannelFeature feature);
161666a3af9SDaniel P. Berrange 
162666a3af9SDaniel P. Berrange /**
163d8d3c7ccSFelipe Franciosi  * qio_channel_set_feature:
164d8d3c7ccSFelipe Franciosi  * @ioc: the channel object
165d8d3c7ccSFelipe Franciosi  * @feature: the feature to set support for
166d8d3c7ccSFelipe Franciosi  *
167d8d3c7ccSFelipe Franciosi  * Add channel support for the feature named in @feature.
168d8d3c7ccSFelipe Franciosi  */
169d8d3c7ccSFelipe Franciosi void qio_channel_set_feature(QIOChannel *ioc,
170d8d3c7ccSFelipe Franciosi                              QIOChannelFeature feature);
171d8d3c7ccSFelipe Franciosi 
172d8d3c7ccSFelipe Franciosi /**
17320f4aa26SDaniel P. Berrange  * qio_channel_set_name:
17420f4aa26SDaniel P. Berrange  * @ioc: the channel object
17520f4aa26SDaniel P. Berrange  * @name: the name of the channel
17620f4aa26SDaniel P. Berrange  *
17720f4aa26SDaniel P. Berrange  * Sets the name of the channel, which serves as an aid
17820f4aa26SDaniel P. Berrange  * to debugging. The name is used when creating GSource
17920f4aa26SDaniel P. Berrange  * watches for this channel.
18020f4aa26SDaniel P. Berrange  */
18120f4aa26SDaniel P. Berrange void qio_channel_set_name(QIOChannel *ioc,
18220f4aa26SDaniel P. Berrange                           const char *name);
18320f4aa26SDaniel P. Berrange 
18420f4aa26SDaniel P. Berrange /**
185666a3af9SDaniel P. Berrange  * qio_channel_readv_full:
186666a3af9SDaniel P. Berrange  * @ioc: the channel object
187666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
188666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
189666a3af9SDaniel P. Berrange  * @fds: pointer to an array that will received file handles
190666a3af9SDaniel P. Berrange  * @nfds: pointer filled with number of elements in @fds on return
191821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
192666a3af9SDaniel P. Berrange  *
193666a3af9SDaniel P. Berrange  * Read data from the IO channel, storing it in the
194666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
195666a3af9SDaniel P. Berrange  * in the @iov will be fully populated with data
196666a3af9SDaniel P. Berrange  * before the next one is used. The @niov parameter
197666a3af9SDaniel P. Berrange  * specifies the total number of elements in @iov.
198666a3af9SDaniel P. Berrange  *
199666a3af9SDaniel P. Berrange  * It is not required for all @iov to be filled with
200666a3af9SDaniel P. Berrange  * data. If the channel is in blocking mode, at least
201666a3af9SDaniel P. Berrange  * one byte of data will be read, but no more is
202666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
203666a3af9SDaniel P. Berrange  * data is available, it will return QIO_CHANNEL_ERR_BLOCK
204666a3af9SDaniel P. Berrange  *
205666a3af9SDaniel P. Berrange  * If the channel has passed any file descriptors,
206666a3af9SDaniel P. Berrange  * the @fds array pointer will be allocated and
207666a3af9SDaniel P. Berrange  * the elements filled with the received file
208666a3af9SDaniel P. Berrange  * descriptors. The @nfds pointer will be updated
209666a3af9SDaniel P. Berrange  * to indicate the size of the @fds array that
210666a3af9SDaniel P. Berrange  * was allocated. It is the callers responsibility
211666a3af9SDaniel P. Berrange  * to call close() on each file descriptor and to
212666a3af9SDaniel P. Berrange  * call g_free() on the array pointer in @fds.
213666a3af9SDaniel P. Berrange  *
214666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
215666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
216666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
217666a3af9SDaniel P. Berrange  *
218666a3af9SDaniel P. Berrange  * Returns: the number of bytes read, or -1 on error,
219666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is available
220666a3af9SDaniel P. Berrange  * and the channel is non-blocking
221666a3af9SDaniel P. Berrange  */
222666a3af9SDaniel P. Berrange ssize_t qio_channel_readv_full(QIOChannel *ioc,
223666a3af9SDaniel P. Berrange                                const struct iovec *iov,
224666a3af9SDaniel P. Berrange                                size_t niov,
225666a3af9SDaniel P. Berrange                                int **fds,
226666a3af9SDaniel P. Berrange                                size_t *nfds,
227666a3af9SDaniel P. Berrange                                Error **errp);
228666a3af9SDaniel P. Berrange 
229666a3af9SDaniel P. Berrange 
230666a3af9SDaniel P. Berrange /**
231666a3af9SDaniel P. Berrange  * qio_channel_writev_full:
232666a3af9SDaniel P. Berrange  * @ioc: the channel object
233666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
234666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
235666a3af9SDaniel P. Berrange  * @fds: an array of file handles to send
236666a3af9SDaniel P. Berrange  * @nfds: number of file handles in @fds
237821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
238666a3af9SDaniel P. Berrange  *
239666a3af9SDaniel P. Berrange  * Write data to the IO channel, reading it from the
240666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
241666a3af9SDaniel P. Berrange  * in the @iov will be fully sent, before the next
242666a3af9SDaniel P. Berrange  * one is used. The @niov parameter specifies the
243666a3af9SDaniel P. Berrange  * total number of elements in @iov.
244666a3af9SDaniel P. Berrange  *
245666a3af9SDaniel P. Berrange  * It is not required for all @iov data to be fully
246666a3af9SDaniel P. Berrange  * sent. If the channel is in blocking mode, at least
247666a3af9SDaniel P. Berrange  * one byte of data will be sent, but no more is
248666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
249666a3af9SDaniel P. Berrange  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
250666a3af9SDaniel P. Berrange  *
251666a3af9SDaniel P. Berrange  * If there are file descriptors to send, the @fds
252666a3af9SDaniel P. Berrange  * array should be non-NULL and provide the handles.
253666a3af9SDaniel P. Berrange  * All file descriptors will be sent if at least one
254666a3af9SDaniel P. Berrange  * byte of data was sent.
255666a3af9SDaniel P. Berrange  *
256666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
257666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
258666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
259666a3af9SDaniel P. Berrange  *
260666a3af9SDaniel P. Berrange  * Returns: the number of bytes sent, or -1 on error,
261666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
262666a3af9SDaniel P. Berrange  * and the channel is non-blocking
263666a3af9SDaniel P. Berrange  */
264666a3af9SDaniel P. Berrange ssize_t qio_channel_writev_full(QIOChannel *ioc,
265666a3af9SDaniel P. Berrange                                 const struct iovec *iov,
266666a3af9SDaniel P. Berrange                                 size_t niov,
267666a3af9SDaniel P. Berrange                                 int *fds,
268666a3af9SDaniel P. Berrange                                 size_t nfds,
269666a3af9SDaniel P. Berrange                                 Error **errp);
270666a3af9SDaniel P. Berrange 
271666a3af9SDaniel P. Berrange /**
272666a3af9SDaniel P. Berrange  * qio_channel_readv:
273666a3af9SDaniel P. Berrange  * @ioc: the channel object
274666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
275666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
276821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
277666a3af9SDaniel P. Berrange  *
278666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
279666a3af9SDaniel P. Berrange  * receiving of file handles.
280666a3af9SDaniel P. Berrange  */
281666a3af9SDaniel P. Berrange ssize_t qio_channel_readv(QIOChannel *ioc,
282666a3af9SDaniel P. Berrange                           const struct iovec *iov,
283666a3af9SDaniel P. Berrange                           size_t niov,
284666a3af9SDaniel P. Berrange                           Error **errp);
285666a3af9SDaniel P. Berrange 
286666a3af9SDaniel P. Berrange /**
287666a3af9SDaniel P. Berrange  * qio_channel_writev:
288666a3af9SDaniel P. Berrange  * @ioc: the channel object
289666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
290666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
291821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
292666a3af9SDaniel P. Berrange  *
293666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
294666a3af9SDaniel P. Berrange  * sending of file handles.
295666a3af9SDaniel P. Berrange  */
296666a3af9SDaniel P. Berrange ssize_t qio_channel_writev(QIOChannel *ioc,
297666a3af9SDaniel P. Berrange                            const struct iovec *iov,
298666a3af9SDaniel P. Berrange                            size_t niov,
299666a3af9SDaniel P. Berrange                            Error **errp);
300666a3af9SDaniel P. Berrange 
301666a3af9SDaniel P. Berrange /**
302*50ea44f0SDaniel P. Berrange  * qio_channel_read:
303666a3af9SDaniel P. Berrange  * @ioc: the channel object
304666a3af9SDaniel P. Berrange  * @buf: the memory region to read data into
305666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
306821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
307666a3af9SDaniel P. Berrange  *
308666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
309666a3af9SDaniel P. Berrange  * receiving of file handles, and only supports reading into
310666a3af9SDaniel P. Berrange  * a single memory region.
311666a3af9SDaniel P. Berrange  */
312666a3af9SDaniel P. Berrange ssize_t qio_channel_read(QIOChannel *ioc,
313666a3af9SDaniel P. Berrange                          char *buf,
314666a3af9SDaniel P. Berrange                          size_t buflen,
315666a3af9SDaniel P. Berrange                          Error **errp);
316666a3af9SDaniel P. Berrange 
317666a3af9SDaniel P. Berrange /**
31861f7c6a0SMarc-André Lureau  * qio_channel_write:
319666a3af9SDaniel P. Berrange  * @ioc: the channel object
320666a3af9SDaniel P. Berrange  * @buf: the memory regions to send data from
321666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
322821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
323666a3af9SDaniel P. Berrange  *
324666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
325666a3af9SDaniel P. Berrange  * sending of file handles, and only supports writing from a
326666a3af9SDaniel P. Berrange  * single memory region.
327666a3af9SDaniel P. Berrange  */
328666a3af9SDaniel P. Berrange ssize_t qio_channel_write(QIOChannel *ioc,
329666a3af9SDaniel P. Berrange                           const char *buf,
330666a3af9SDaniel P. Berrange                           size_t buflen,
331666a3af9SDaniel P. Berrange                           Error **errp);
332666a3af9SDaniel P. Berrange 
333666a3af9SDaniel P. Berrange /**
334666a3af9SDaniel P. Berrange  * qio_channel_set_blocking:
335666a3af9SDaniel P. Berrange  * @ioc: the channel object
336666a3af9SDaniel P. Berrange  * @enabled: the blocking flag state
337821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
338666a3af9SDaniel P. Berrange  *
339666a3af9SDaniel P. Berrange  * If @enabled is true, then the channel is put into
340666a3af9SDaniel P. Berrange  * blocking mode, otherwise it will be non-blocking.
341666a3af9SDaniel P. Berrange  *
342666a3af9SDaniel P. Berrange  * In non-blocking mode, read/write operations may
343666a3af9SDaniel P. Berrange  * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
344666a3af9SDaniel P. Berrange  * block on I/O
345666a3af9SDaniel P. Berrange  */
346666a3af9SDaniel P. Berrange int qio_channel_set_blocking(QIOChannel *ioc,
347666a3af9SDaniel P. Berrange                              bool enabled,
348666a3af9SDaniel P. Berrange                              Error **errp);
349666a3af9SDaniel P. Berrange 
350666a3af9SDaniel P. Berrange /**
351666a3af9SDaniel P. Berrange  * qio_channel_close:
352666a3af9SDaniel P. Berrange  * @ioc: the channel object
353821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
354666a3af9SDaniel P. Berrange  *
355666a3af9SDaniel P. Berrange  * Close the channel, flushing any pending I/O
356666a3af9SDaniel P. Berrange  *
357666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
358666a3af9SDaniel P. Berrange  */
359666a3af9SDaniel P. Berrange int qio_channel_close(QIOChannel *ioc,
360666a3af9SDaniel P. Berrange                       Error **errp);
361666a3af9SDaniel P. Berrange 
362666a3af9SDaniel P. Berrange /**
363666a3af9SDaniel P. Berrange  * qio_channel_shutdown:
364666a3af9SDaniel P. Berrange  * @ioc: the channel object
365666a3af9SDaniel P. Berrange  * @how: the direction to shutdown
366821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
367666a3af9SDaniel P. Berrange  *
368666a3af9SDaniel P. Berrange  * Shutdowns transmission and/or receiving of data
369666a3af9SDaniel P. Berrange  * without closing the underlying transport.
370666a3af9SDaniel P. Berrange  *
371666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
372666a3af9SDaniel P. Berrange  * so may report an error. To avoid errors, the
373666a3af9SDaniel P. Berrange  * caller may check for the feature flag
374666a3af9SDaniel P. Berrange  * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling
375666a3af9SDaniel P. Berrange  * this method.
376666a3af9SDaniel P. Berrange  *
377666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
378666a3af9SDaniel P. Berrange  */
379666a3af9SDaniel P. Berrange int qio_channel_shutdown(QIOChannel *ioc,
380666a3af9SDaniel P. Berrange                          QIOChannelShutdown how,
381666a3af9SDaniel P. Berrange                          Error **errp);
382666a3af9SDaniel P. Berrange 
383666a3af9SDaniel P. Berrange /**
384666a3af9SDaniel P. Berrange  * qio_channel_set_delay:
385666a3af9SDaniel P. Berrange  * @ioc: the channel object
386666a3af9SDaniel P. Berrange  * @enabled: the new flag state
387666a3af9SDaniel P. Berrange  *
388666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
389666a3af9SDaniel P. Berrange  * permitted to delay writes in order to merge
390666a3af9SDaniel P. Berrange  * small packets. If @enabled is true, then the
391666a3af9SDaniel P. Berrange  * writes may be delayed in order to opportunistically
392666a3af9SDaniel P. Berrange  * merge small packets into larger ones. If @enabled
393666a3af9SDaniel P. Berrange  * is false, writes are dispatched immediately with
394666a3af9SDaniel P. Berrange  * no delay.
395666a3af9SDaniel P. Berrange  *
396666a3af9SDaniel P. Berrange  * When @enabled is false, applications may wish to
397666a3af9SDaniel P. Berrange  * use the qio_channel_set_cork() method to explicitly
398666a3af9SDaniel P. Berrange  * control write merging.
399666a3af9SDaniel P. Berrange  *
400666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
401666a3af9SDaniel P. Berrange  * API corresponds to the inverse of TCP_NODELAY flag,
402666a3af9SDaniel P. Berrange  * controlling whether the Nagle algorithm is active.
403666a3af9SDaniel P. Berrange  *
404666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
405666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
406666a3af9SDaniel P. Berrange  * error.
407666a3af9SDaniel P. Berrange  */
408666a3af9SDaniel P. Berrange void qio_channel_set_delay(QIOChannel *ioc,
409666a3af9SDaniel P. Berrange                            bool enabled);
410666a3af9SDaniel P. Berrange 
411666a3af9SDaniel P. Berrange /**
412666a3af9SDaniel P. Berrange  * qio_channel_set_cork:
413666a3af9SDaniel P. Berrange  * @ioc: the channel object
414666a3af9SDaniel P. Berrange  * @enabled: the new flag state
415666a3af9SDaniel P. Berrange  *
416666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
417666a3af9SDaniel P. Berrange  * permitted to dispatch data that is written.
418666a3af9SDaniel P. Berrange  * If @enabled is true, then any data written will
419666a3af9SDaniel P. Berrange  * be queued in local buffers until @enabled is
420666a3af9SDaniel P. Berrange  * set to false once again.
421666a3af9SDaniel P. Berrange  *
422666a3af9SDaniel P. Berrange  * This feature is typically used when the automatic
423666a3af9SDaniel P. Berrange  * write coalescing facility is disabled via the
424666a3af9SDaniel P. Berrange  * qio_channel_set_delay() method.
425666a3af9SDaniel P. Berrange  *
426666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
427666a3af9SDaniel P. Berrange  * API corresponds to the TCP_CORK flag.
428666a3af9SDaniel P. Berrange  *
429666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
430666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
431666a3af9SDaniel P. Berrange  * error.
432666a3af9SDaniel P. Berrange  */
433666a3af9SDaniel P. Berrange void qio_channel_set_cork(QIOChannel *ioc,
434666a3af9SDaniel P. Berrange                           bool enabled);
435666a3af9SDaniel P. Berrange 
436666a3af9SDaniel P. Berrange 
437666a3af9SDaniel P. Berrange /**
438666a3af9SDaniel P. Berrange  * qio_channel_seek:
439666a3af9SDaniel P. Berrange  * @ioc: the channel object
440666a3af9SDaniel P. Berrange  * @offset: the position to seek to, relative to @whence
441666a3af9SDaniel P. Berrange  * @whence: one of the (POSIX) SEEK_* constants listed below
442821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
443666a3af9SDaniel P. Berrange  *
444666a3af9SDaniel P. Berrange  * Moves the current I/O position within the channel
445666a3af9SDaniel P. Berrange  * @ioc, to be @offset. The value of @offset is
446666a3af9SDaniel P. Berrange  * interpreted relative to @whence:
447666a3af9SDaniel P. Berrange  *
448666a3af9SDaniel P. Berrange  * SEEK_SET - the position is set to @offset bytes
449666a3af9SDaniel P. Berrange  * SEEK_CUR - the position is moved by @offset bytes
450666a3af9SDaniel P. Berrange  * SEEK_END - the position is set to end of the file plus @offset bytes
451666a3af9SDaniel P. Berrange  *
452666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
453666a3af9SDaniel P. Berrange  * so may report an error.
454666a3af9SDaniel P. Berrange  *
455666a3af9SDaniel P. Berrange  * Returns: the new position on success, (off_t)-1 on failure
456666a3af9SDaniel P. Berrange  */
457666a3af9SDaniel P. Berrange off_t qio_channel_io_seek(QIOChannel *ioc,
458666a3af9SDaniel P. Berrange                           off_t offset,
459666a3af9SDaniel P. Berrange                           int whence,
460666a3af9SDaniel P. Berrange                           Error **errp);
461666a3af9SDaniel P. Berrange 
462666a3af9SDaniel P. Berrange 
463666a3af9SDaniel P. Berrange /**
464666a3af9SDaniel P. Berrange  * qio_channel_create_watch:
465666a3af9SDaniel P. Berrange  * @ioc: the channel object
466666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
467666a3af9SDaniel P. Berrange  *
468666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
469666a3af9SDaniel P. Berrange  * for the I/O condition @condition. Typically the
470666a3af9SDaniel P. Berrange  * qio_channel_add_watch() method would be used instead
471666a3af9SDaniel P. Berrange  * of this, since it directly attaches a callback to
472666a3af9SDaniel P. Berrange  * the source
473666a3af9SDaniel P. Berrange  *
474666a3af9SDaniel P. Berrange  * Returns: the new main loop source.
475666a3af9SDaniel P. Berrange  */
476666a3af9SDaniel P. Berrange GSource *qio_channel_create_watch(QIOChannel *ioc,
477666a3af9SDaniel P. Berrange                                   GIOCondition condition);
478666a3af9SDaniel P. Berrange 
479666a3af9SDaniel P. Berrange /**
480666a3af9SDaniel P. Berrange  * qio_channel_add_watch:
481666a3af9SDaniel P. Berrange  * @ioc: the channel object
482666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
483666a3af9SDaniel P. Berrange  * @func: callback to invoke when the source becomes ready
484666a3af9SDaniel P. Berrange  * @user_data: opaque data to pass to @func
485666a3af9SDaniel P. Berrange  * @notify: callback to free @user_data
486666a3af9SDaniel P. Berrange  *
487666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
488666a3af9SDaniel P. Berrange  * for the I/O condition @condition. The callback @func
489666a3af9SDaniel P. Berrange  * will be registered against the source, to be invoked
490666a3af9SDaniel P. Berrange  * when the source becomes ready. The optional @user_data
491666a3af9SDaniel P. Berrange  * will be passed to @func when it is invoked. The @notify
492666a3af9SDaniel P. Berrange  * callback will be used to free @user_data when the
493666a3af9SDaniel P. Berrange  * watch is deleted
494666a3af9SDaniel P. Berrange  *
495666a3af9SDaniel P. Berrange  * The returned source ID can be used with g_source_remove()
496666a3af9SDaniel P. Berrange  * to remove and free the source when no longer required.
497666a3af9SDaniel P. Berrange  * Alternatively the @func callback can return a FALSE
498666a3af9SDaniel P. Berrange  * value.
499666a3af9SDaniel P. Berrange  *
500666a3af9SDaniel P. Berrange  * Returns: the source ID
501666a3af9SDaniel P. Berrange  */
502666a3af9SDaniel P. Berrange guint qio_channel_add_watch(QIOChannel *ioc,
503666a3af9SDaniel P. Berrange                             GIOCondition condition,
504666a3af9SDaniel P. Berrange                             QIOChannelFunc func,
505666a3af9SDaniel P. Berrange                             gpointer user_data,
506666a3af9SDaniel P. Berrange                             GDestroyNotify notify);
507666a3af9SDaniel P. Berrange 
508666a3af9SDaniel P. Berrange 
509666a3af9SDaniel P. Berrange /**
510c4c497d2SPaolo Bonzini  * qio_channel_attach_aio_context:
511c4c497d2SPaolo Bonzini  * @ioc: the channel object
512c4c497d2SPaolo Bonzini  * @ctx: the #AioContext to set the handlers on
513c4c497d2SPaolo Bonzini  *
514c4c497d2SPaolo Bonzini  * Request that qio_channel_yield() sets I/O handlers on
515c4c497d2SPaolo Bonzini  * the given #AioContext.  If @ctx is %NULL, qio_channel_yield()
516c4c497d2SPaolo Bonzini  * uses QEMU's main thread event loop.
517c4c497d2SPaolo Bonzini  *
518c4c497d2SPaolo Bonzini  * You can move a #QIOChannel from one #AioContext to another even if
519c4c497d2SPaolo Bonzini  * I/O handlers are set for a coroutine.  However, #QIOChannel provides
520c4c497d2SPaolo Bonzini  * no synchronization between the calls to qio_channel_yield() and
521c4c497d2SPaolo Bonzini  * qio_channel_attach_aio_context().
522c4c497d2SPaolo Bonzini  *
523c4c497d2SPaolo Bonzini  * Therefore you should first call qio_channel_detach_aio_context()
524c4c497d2SPaolo Bonzini  * to ensure that the coroutine is not entered concurrently.  Then,
525c4c497d2SPaolo Bonzini  * while the coroutine has yielded, call qio_channel_attach_aio_context(),
526c4c497d2SPaolo Bonzini  * and then aio_co_schedule() to place the coroutine on the new
527c4c497d2SPaolo Bonzini  * #AioContext.  The calls to qio_channel_detach_aio_context()
528c4c497d2SPaolo Bonzini  * and qio_channel_attach_aio_context() should be protected with
529c4c497d2SPaolo Bonzini  * aio_context_acquire() and aio_context_release().
530c4c497d2SPaolo Bonzini  */
531c4c497d2SPaolo Bonzini void qio_channel_attach_aio_context(QIOChannel *ioc,
532c4c497d2SPaolo Bonzini                                     AioContext *ctx);
533c4c497d2SPaolo Bonzini 
534c4c497d2SPaolo Bonzini /**
535c4c497d2SPaolo Bonzini  * qio_channel_detach_aio_context:
536c4c497d2SPaolo Bonzini  * @ioc: the channel object
537c4c497d2SPaolo Bonzini  *
538c4c497d2SPaolo Bonzini  * Disable any I/O handlers set by qio_channel_yield().  With the
539c4c497d2SPaolo Bonzini  * help of aio_co_schedule(), this allows moving a coroutine that was
540c4c497d2SPaolo Bonzini  * paused by qio_channel_yield() to another context.
541c4c497d2SPaolo Bonzini  */
542c4c497d2SPaolo Bonzini void qio_channel_detach_aio_context(QIOChannel *ioc);
543c4c497d2SPaolo Bonzini 
544c4c497d2SPaolo Bonzini /**
545666a3af9SDaniel P. Berrange  * qio_channel_yield:
546666a3af9SDaniel P. Berrange  * @ioc: the channel object
547666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
548666a3af9SDaniel P. Berrange  *
549c4c497d2SPaolo Bonzini  * Yields execution from the current coroutine until the condition
550c4c497d2SPaolo Bonzini  * indicated by @condition becomes available.  @condition must
551c4c497d2SPaolo Bonzini  * be either %G_IO_IN or %G_IO_OUT; it cannot contain both.  In
552c4c497d2SPaolo Bonzini  * addition, no two coroutine can be waiting on the same condition
553c4c497d2SPaolo Bonzini  * and channel at the same time.
554666a3af9SDaniel P. Berrange  *
555666a3af9SDaniel P. Berrange  * This must only be called from coroutine context
556666a3af9SDaniel P. Berrange  */
557666a3af9SDaniel P. Berrange void qio_channel_yield(QIOChannel *ioc,
558666a3af9SDaniel P. Berrange                        GIOCondition condition);
559666a3af9SDaniel P. Berrange 
560666a3af9SDaniel P. Berrange /**
561666a3af9SDaniel P. Berrange  * qio_channel_wait:
562666a3af9SDaniel P. Berrange  * @ioc: the channel object
563666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
564666a3af9SDaniel P. Berrange  *
565666a3af9SDaniel P. Berrange  * Block execution from the current thread until
566666a3af9SDaniel P. Berrange  * the condition indicated by @condition becomes
567666a3af9SDaniel P. Berrange  * available.
568666a3af9SDaniel P. Berrange  *
569666a3af9SDaniel P. Berrange  * This will enter a nested event loop to perform
570666a3af9SDaniel P. Berrange  * the wait.
571666a3af9SDaniel P. Berrange  */
572666a3af9SDaniel P. Berrange void qio_channel_wait(QIOChannel *ioc,
573666a3af9SDaniel P. Berrange                       GIOCondition condition);
574666a3af9SDaniel P. Berrange 
575bf88c124SPaolo Bonzini /**
576bf88c124SPaolo Bonzini  * qio_channel_set_aio_fd_handler:
577bf88c124SPaolo Bonzini  * @ioc: the channel object
578bf88c124SPaolo Bonzini  * @ctx: the AioContext to set the handlers on
579bf88c124SPaolo Bonzini  * @io_read: the read handler
580bf88c124SPaolo Bonzini  * @io_write: the write handler
581bf88c124SPaolo Bonzini  * @opaque: the opaque value passed to the handler
582bf88c124SPaolo Bonzini  *
583bf88c124SPaolo Bonzini  * This is used internally by qio_channel_yield().  It can
584bf88c124SPaolo Bonzini  * be used by channel implementations to forward the handlers
585bf88c124SPaolo Bonzini  * to another channel (e.g. from #QIOChannelTLS to the
586bf88c124SPaolo Bonzini  * underlying socket).
587bf88c124SPaolo Bonzini  */
588bf88c124SPaolo Bonzini void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
589bf88c124SPaolo Bonzini                                     AioContext *ctx,
590bf88c124SPaolo Bonzini                                     IOHandler *io_read,
591bf88c124SPaolo Bonzini                                     IOHandler *io_write,
592bf88c124SPaolo Bonzini                                     void *opaque);
593bf88c124SPaolo Bonzini 
5942a6a4076SMarkus Armbruster #endif /* QIO_CHANNEL_H */
595