xref: /qemu/include/io/channel.h (revision bf88c1247f80ac6d62710d5d0d0d9ce3a53e99ec)
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"
26*bf88c124SPaolo Bonzini #include "block/aio.h"
27666a3af9SDaniel P. Berrange 
28666a3af9SDaniel P. Berrange #define TYPE_QIO_CHANNEL "qio-channel"
29666a3af9SDaniel P. Berrange #define QIO_CHANNEL(obj)                                    \
30666a3af9SDaniel P. Berrange     OBJECT_CHECK(QIOChannel, (obj), TYPE_QIO_CHANNEL)
31666a3af9SDaniel P. Berrange #define QIO_CHANNEL_CLASS(klass)                                    \
32666a3af9SDaniel P. Berrange     OBJECT_CLASS_CHECK(QIOChannelClass, klass, TYPE_QIO_CHANNEL)
33666a3af9SDaniel P. Berrange #define QIO_CHANNEL_GET_CLASS(obj)                                  \
34666a3af9SDaniel P. Berrange     OBJECT_GET_CLASS(QIOChannelClass, obj, TYPE_QIO_CHANNEL)
35666a3af9SDaniel P. Berrange 
36666a3af9SDaniel P. Berrange typedef struct QIOChannel QIOChannel;
37666a3af9SDaniel P. Berrange typedef struct QIOChannelClass QIOChannelClass;
38666a3af9SDaniel P. Berrange 
39666a3af9SDaniel P. Berrange #define QIO_CHANNEL_ERR_BLOCK -2
40666a3af9SDaniel P. Berrange 
41666a3af9SDaniel P. Berrange typedef enum QIOChannelFeature QIOChannelFeature;
42666a3af9SDaniel P. Berrange 
43666a3af9SDaniel P. Berrange enum QIOChannelFeature {
448fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_FD_PASS,
458fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_SHUTDOWN,
468fbf6612SFelipe Franciosi     QIO_CHANNEL_FEATURE_LISTEN,
47666a3af9SDaniel P. Berrange };
48666a3af9SDaniel P. Berrange 
49666a3af9SDaniel P. Berrange 
50666a3af9SDaniel P. Berrange typedef enum QIOChannelShutdown QIOChannelShutdown;
51666a3af9SDaniel P. Berrange 
52666a3af9SDaniel P. Berrange enum QIOChannelShutdown {
53666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_BOTH,
54666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_READ,
55666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_WRITE,
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;
84a5897205SPaolo Bonzini #ifdef _WIN32
85a5897205SPaolo Bonzini     HANDLE event; /* For use with GSource on Win32 */
86a5897205SPaolo Bonzini #endif
87666a3af9SDaniel P. Berrange };
88666a3af9SDaniel P. Berrange 
89666a3af9SDaniel P. Berrange /**
90666a3af9SDaniel P. Berrange  * QIOChannelClass:
91666a3af9SDaniel P. Berrange  *
92666a3af9SDaniel P. Berrange  * This class defines the contract that all subclasses
93666a3af9SDaniel P. Berrange  * must follow to provide specific channel implementations.
94666a3af9SDaniel P. Berrange  * The first five callbacks are mandatory to support, others
95666a3af9SDaniel P. Berrange  * provide additional optional features.
96666a3af9SDaniel P. Berrange  *
97666a3af9SDaniel P. Berrange  * Consult the corresponding public API docs for a description
98666a3af9SDaniel P. Berrange  * of the semantics of each callback
99666a3af9SDaniel P. Berrange  */
100666a3af9SDaniel P. Berrange struct QIOChannelClass {
101666a3af9SDaniel P. Berrange     ObjectClass parent;
102666a3af9SDaniel P. Berrange 
103666a3af9SDaniel P. Berrange     /* Mandatory callbacks */
104666a3af9SDaniel P. Berrange     ssize_t (*io_writev)(QIOChannel *ioc,
105666a3af9SDaniel P. Berrange                          const struct iovec *iov,
106666a3af9SDaniel P. Berrange                          size_t niov,
107666a3af9SDaniel P. Berrange                          int *fds,
108666a3af9SDaniel P. Berrange                          size_t nfds,
109666a3af9SDaniel P. Berrange                          Error **errp);
110666a3af9SDaniel P. Berrange     ssize_t (*io_readv)(QIOChannel *ioc,
111666a3af9SDaniel P. Berrange                         const struct iovec *iov,
112666a3af9SDaniel P. Berrange                         size_t niov,
113666a3af9SDaniel P. Berrange                         int **fds,
114666a3af9SDaniel P. Berrange                         size_t *nfds,
115666a3af9SDaniel P. Berrange                         Error **errp);
116666a3af9SDaniel P. Berrange     int (*io_close)(QIOChannel *ioc,
117666a3af9SDaniel P. Berrange                     Error **errp);
118666a3af9SDaniel P. Berrange     GSource * (*io_create_watch)(QIOChannel *ioc,
119666a3af9SDaniel P. Berrange                                  GIOCondition condition);
120666a3af9SDaniel P. Berrange     int (*io_set_blocking)(QIOChannel *ioc,
121666a3af9SDaniel P. Berrange                            bool enabled,
122666a3af9SDaniel P. Berrange                            Error **errp);
123666a3af9SDaniel P. Berrange 
124666a3af9SDaniel P. Berrange     /* Optional callbacks */
125666a3af9SDaniel P. Berrange     int (*io_shutdown)(QIOChannel *ioc,
126666a3af9SDaniel P. Berrange                        QIOChannelShutdown how,
127666a3af9SDaniel P. Berrange                        Error **errp);
128666a3af9SDaniel P. Berrange     void (*io_set_cork)(QIOChannel *ioc,
129666a3af9SDaniel P. Berrange                         bool enabled);
130666a3af9SDaniel P. Berrange     void (*io_set_delay)(QIOChannel *ioc,
131666a3af9SDaniel P. Berrange                          bool enabled);
132666a3af9SDaniel P. Berrange     off_t (*io_seek)(QIOChannel *ioc,
133666a3af9SDaniel P. Berrange                      off_t offset,
134666a3af9SDaniel P. Berrange                      int whence,
135666a3af9SDaniel P. Berrange                      Error **errp);
136*bf88c124SPaolo Bonzini     void (*io_set_aio_fd_handler)(QIOChannel *ioc,
137*bf88c124SPaolo Bonzini                                   AioContext *ctx,
138*bf88c124SPaolo Bonzini                                   IOHandler *io_read,
139*bf88c124SPaolo Bonzini                                   IOHandler *io_write,
140*bf88c124SPaolo Bonzini                                   void *opaque);
141666a3af9SDaniel P. Berrange };
142666a3af9SDaniel P. Berrange 
143666a3af9SDaniel P. Berrange /* General I/O handling functions */
144666a3af9SDaniel P. Berrange 
145666a3af9SDaniel P. Berrange /**
146666a3af9SDaniel P. Berrange  * qio_channel_has_feature:
147666a3af9SDaniel P. Berrange  * @ioc: the channel object
148666a3af9SDaniel P. Berrange  * @feature: the feature to check support of
149666a3af9SDaniel P. Berrange  *
150666a3af9SDaniel P. Berrange  * Determine whether the channel implementation supports
151666a3af9SDaniel P. Berrange  * the optional feature named in @feature.
152666a3af9SDaniel P. Berrange  *
153666a3af9SDaniel P. Berrange  * Returns: true if supported, false otherwise.
154666a3af9SDaniel P. Berrange  */
155666a3af9SDaniel P. Berrange bool qio_channel_has_feature(QIOChannel *ioc,
156666a3af9SDaniel P. Berrange                              QIOChannelFeature feature);
157666a3af9SDaniel P. Berrange 
158666a3af9SDaniel P. Berrange /**
159d8d3c7ccSFelipe Franciosi  * qio_channel_set_feature:
160d8d3c7ccSFelipe Franciosi  * @ioc: the channel object
161d8d3c7ccSFelipe Franciosi  * @feature: the feature to set support for
162d8d3c7ccSFelipe Franciosi  *
163d8d3c7ccSFelipe Franciosi  * Add channel support for the feature named in @feature.
164d8d3c7ccSFelipe Franciosi  */
165d8d3c7ccSFelipe Franciosi void qio_channel_set_feature(QIOChannel *ioc,
166d8d3c7ccSFelipe Franciosi                              QIOChannelFeature feature);
167d8d3c7ccSFelipe Franciosi 
168d8d3c7ccSFelipe Franciosi /**
16920f4aa26SDaniel P. Berrange  * qio_channel_set_name:
17020f4aa26SDaniel P. Berrange  * @ioc: the channel object
17120f4aa26SDaniel P. Berrange  * @name: the name of the channel
17220f4aa26SDaniel P. Berrange  *
17320f4aa26SDaniel P. Berrange  * Sets the name of the channel, which serves as an aid
17420f4aa26SDaniel P. Berrange  * to debugging. The name is used when creating GSource
17520f4aa26SDaniel P. Berrange  * watches for this channel.
17620f4aa26SDaniel P. Berrange  */
17720f4aa26SDaniel P. Berrange void qio_channel_set_name(QIOChannel *ioc,
17820f4aa26SDaniel P. Berrange                           const char *name);
17920f4aa26SDaniel P. Berrange 
18020f4aa26SDaniel P. Berrange /**
181666a3af9SDaniel P. Berrange  * qio_channel_readv_full:
182666a3af9SDaniel P. Berrange  * @ioc: the channel object
183666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
184666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
185666a3af9SDaniel P. Berrange  * @fds: pointer to an array that will received file handles
186666a3af9SDaniel P. Berrange  * @nfds: pointer filled with number of elements in @fds on return
187821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
188666a3af9SDaniel P. Berrange  *
189666a3af9SDaniel P. Berrange  * Read data from the IO channel, storing it in the
190666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
191666a3af9SDaniel P. Berrange  * in the @iov will be fully populated with data
192666a3af9SDaniel P. Berrange  * before the next one is used. The @niov parameter
193666a3af9SDaniel P. Berrange  * specifies the total number of elements in @iov.
194666a3af9SDaniel P. Berrange  *
195666a3af9SDaniel P. Berrange  * It is not required for all @iov to be filled with
196666a3af9SDaniel P. Berrange  * data. If the channel is in blocking mode, at least
197666a3af9SDaniel P. Berrange  * one byte of data will be read, but no more is
198666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
199666a3af9SDaniel P. Berrange  * data is available, it will return QIO_CHANNEL_ERR_BLOCK
200666a3af9SDaniel P. Berrange  *
201666a3af9SDaniel P. Berrange  * If the channel has passed any file descriptors,
202666a3af9SDaniel P. Berrange  * the @fds array pointer will be allocated and
203666a3af9SDaniel P. Berrange  * the elements filled with the received file
204666a3af9SDaniel P. Berrange  * descriptors. The @nfds pointer will be updated
205666a3af9SDaniel P. Berrange  * to indicate the size of the @fds array that
206666a3af9SDaniel P. Berrange  * was allocated. It is the callers responsibility
207666a3af9SDaniel P. Berrange  * to call close() on each file descriptor and to
208666a3af9SDaniel P. Berrange  * call g_free() on the array pointer in @fds.
209666a3af9SDaniel P. Berrange  *
210666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
211666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
212666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
213666a3af9SDaniel P. Berrange  *
214666a3af9SDaniel P. Berrange  * Returns: the number of bytes read, or -1 on error,
215666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is available
216666a3af9SDaniel P. Berrange  * and the channel is non-blocking
217666a3af9SDaniel P. Berrange  */
218666a3af9SDaniel P. Berrange ssize_t qio_channel_readv_full(QIOChannel *ioc,
219666a3af9SDaniel P. Berrange                                const struct iovec *iov,
220666a3af9SDaniel P. Berrange                                size_t niov,
221666a3af9SDaniel P. Berrange                                int **fds,
222666a3af9SDaniel P. Berrange                                size_t *nfds,
223666a3af9SDaniel P. Berrange                                Error **errp);
224666a3af9SDaniel P. Berrange 
225666a3af9SDaniel P. Berrange 
226666a3af9SDaniel P. Berrange /**
227666a3af9SDaniel P. Berrange  * qio_channel_writev_full:
228666a3af9SDaniel P. Berrange  * @ioc: the channel object
229666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
230666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
231666a3af9SDaniel P. Berrange  * @fds: an array of file handles to send
232666a3af9SDaniel P. Berrange  * @nfds: number of file handles in @fds
233821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
234666a3af9SDaniel P. Berrange  *
235666a3af9SDaniel P. Berrange  * Write data to the IO channel, reading it from the
236666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
237666a3af9SDaniel P. Berrange  * in the @iov will be fully sent, before the next
238666a3af9SDaniel P. Berrange  * one is used. The @niov parameter specifies the
239666a3af9SDaniel P. Berrange  * total number of elements in @iov.
240666a3af9SDaniel P. Berrange  *
241666a3af9SDaniel P. Berrange  * It is not required for all @iov data to be fully
242666a3af9SDaniel P. Berrange  * sent. If the channel is in blocking mode, at least
243666a3af9SDaniel P. Berrange  * one byte of data will be sent, but no more is
244666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
245666a3af9SDaniel P. Berrange  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
246666a3af9SDaniel P. Berrange  *
247666a3af9SDaniel P. Berrange  * If there are file descriptors to send, the @fds
248666a3af9SDaniel P. Berrange  * array should be non-NULL and provide the handles.
249666a3af9SDaniel P. Berrange  * All file descriptors will be sent if at least one
250666a3af9SDaniel P. Berrange  * byte of data was sent.
251666a3af9SDaniel P. Berrange  *
252666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
253666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
254666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
255666a3af9SDaniel P. Berrange  *
256666a3af9SDaniel P. Berrange  * Returns: the number of bytes sent, or -1 on error,
257666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
258666a3af9SDaniel P. Berrange  * and the channel is non-blocking
259666a3af9SDaniel P. Berrange  */
260666a3af9SDaniel P. Berrange ssize_t qio_channel_writev_full(QIOChannel *ioc,
261666a3af9SDaniel P. Berrange                                 const struct iovec *iov,
262666a3af9SDaniel P. Berrange                                 size_t niov,
263666a3af9SDaniel P. Berrange                                 int *fds,
264666a3af9SDaniel P. Berrange                                 size_t nfds,
265666a3af9SDaniel P. Berrange                                 Error **errp);
266666a3af9SDaniel P. Berrange 
267666a3af9SDaniel P. Berrange /**
268666a3af9SDaniel P. Berrange  * qio_channel_readv:
269666a3af9SDaniel P. Berrange  * @ioc: the channel object
270666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
271666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
272821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
273666a3af9SDaniel P. Berrange  *
274666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
275666a3af9SDaniel P. Berrange  * receiving of file handles.
276666a3af9SDaniel P. Berrange  */
277666a3af9SDaniel P. Berrange ssize_t qio_channel_readv(QIOChannel *ioc,
278666a3af9SDaniel P. Berrange                           const struct iovec *iov,
279666a3af9SDaniel P. Berrange                           size_t niov,
280666a3af9SDaniel P. Berrange                           Error **errp);
281666a3af9SDaniel P. Berrange 
282666a3af9SDaniel P. Berrange /**
283666a3af9SDaniel P. Berrange  * qio_channel_writev:
284666a3af9SDaniel P. Berrange  * @ioc: the channel object
285666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
286666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
287821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
288666a3af9SDaniel P. Berrange  *
289666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
290666a3af9SDaniel P. Berrange  * sending of file handles.
291666a3af9SDaniel P. Berrange  */
292666a3af9SDaniel P. Berrange ssize_t qio_channel_writev(QIOChannel *ioc,
293666a3af9SDaniel P. Berrange                            const struct iovec *iov,
294666a3af9SDaniel P. Berrange                            size_t niov,
295666a3af9SDaniel P. Berrange                            Error **errp);
296666a3af9SDaniel P. Berrange 
297666a3af9SDaniel P. Berrange /**
298666a3af9SDaniel P. Berrange  * qio_channel_readv:
299666a3af9SDaniel P. Berrange  * @ioc: the channel object
300666a3af9SDaniel P. Berrange  * @buf: the memory region to read data into
301666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
302821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
303666a3af9SDaniel P. Berrange  *
304666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
305666a3af9SDaniel P. Berrange  * receiving of file handles, and only supports reading into
306666a3af9SDaniel P. Berrange  * a single memory region.
307666a3af9SDaniel P. Berrange  */
308666a3af9SDaniel P. Berrange ssize_t qio_channel_read(QIOChannel *ioc,
309666a3af9SDaniel P. Berrange                          char *buf,
310666a3af9SDaniel P. Berrange                          size_t buflen,
311666a3af9SDaniel P. Berrange                          Error **errp);
312666a3af9SDaniel P. Berrange 
313666a3af9SDaniel P. Berrange /**
314666a3af9SDaniel P. Berrange  * qio_channel_writev:
315666a3af9SDaniel P. Berrange  * @ioc: the channel object
316666a3af9SDaniel P. Berrange  * @buf: the memory regions to send data from
317666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
318821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
319666a3af9SDaniel P. Berrange  *
320666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
321666a3af9SDaniel P. Berrange  * sending of file handles, and only supports writing from a
322666a3af9SDaniel P. Berrange  * single memory region.
323666a3af9SDaniel P. Berrange  */
324666a3af9SDaniel P. Berrange ssize_t qio_channel_write(QIOChannel *ioc,
325666a3af9SDaniel P. Berrange                           const char *buf,
326666a3af9SDaniel P. Berrange                           size_t buflen,
327666a3af9SDaniel P. Berrange                           Error **errp);
328666a3af9SDaniel P. Berrange 
329666a3af9SDaniel P. Berrange /**
330666a3af9SDaniel P. Berrange  * qio_channel_set_blocking:
331666a3af9SDaniel P. Berrange  * @ioc: the channel object
332666a3af9SDaniel P. Berrange  * @enabled: the blocking flag state
333821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
334666a3af9SDaniel P. Berrange  *
335666a3af9SDaniel P. Berrange  * If @enabled is true, then the channel is put into
336666a3af9SDaniel P. Berrange  * blocking mode, otherwise it will be non-blocking.
337666a3af9SDaniel P. Berrange  *
338666a3af9SDaniel P. Berrange  * In non-blocking mode, read/write operations may
339666a3af9SDaniel P. Berrange  * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
340666a3af9SDaniel P. Berrange  * block on I/O
341666a3af9SDaniel P. Berrange  */
342666a3af9SDaniel P. Berrange int qio_channel_set_blocking(QIOChannel *ioc,
343666a3af9SDaniel P. Berrange                              bool enabled,
344666a3af9SDaniel P. Berrange                              Error **errp);
345666a3af9SDaniel P. Berrange 
346666a3af9SDaniel P. Berrange /**
347666a3af9SDaniel P. Berrange  * qio_channel_close:
348666a3af9SDaniel P. Berrange  * @ioc: the channel object
349821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
350666a3af9SDaniel P. Berrange  *
351666a3af9SDaniel P. Berrange  * Close the channel, flushing any pending I/O
352666a3af9SDaniel P. Berrange  *
353666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
354666a3af9SDaniel P. Berrange  */
355666a3af9SDaniel P. Berrange int qio_channel_close(QIOChannel *ioc,
356666a3af9SDaniel P. Berrange                       Error **errp);
357666a3af9SDaniel P. Berrange 
358666a3af9SDaniel P. Berrange /**
359666a3af9SDaniel P. Berrange  * qio_channel_shutdown:
360666a3af9SDaniel P. Berrange  * @ioc: the channel object
361666a3af9SDaniel P. Berrange  * @how: the direction to shutdown
362821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
363666a3af9SDaniel P. Berrange  *
364666a3af9SDaniel P. Berrange  * Shutdowns transmission and/or receiving of data
365666a3af9SDaniel P. Berrange  * without closing the underlying transport.
366666a3af9SDaniel P. Berrange  *
367666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
368666a3af9SDaniel P. Berrange  * so may report an error. To avoid errors, the
369666a3af9SDaniel P. Berrange  * caller may check for the feature flag
370666a3af9SDaniel P. Berrange  * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling
371666a3af9SDaniel P. Berrange  * this method.
372666a3af9SDaniel P. Berrange  *
373666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
374666a3af9SDaniel P. Berrange  */
375666a3af9SDaniel P. Berrange int qio_channel_shutdown(QIOChannel *ioc,
376666a3af9SDaniel P. Berrange                          QIOChannelShutdown how,
377666a3af9SDaniel P. Berrange                          Error **errp);
378666a3af9SDaniel P. Berrange 
379666a3af9SDaniel P. Berrange /**
380666a3af9SDaniel P. Berrange  * qio_channel_set_delay:
381666a3af9SDaniel P. Berrange  * @ioc: the channel object
382666a3af9SDaniel P. Berrange  * @enabled: the new flag state
383666a3af9SDaniel P. Berrange  *
384666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
385666a3af9SDaniel P. Berrange  * permitted to delay writes in order to merge
386666a3af9SDaniel P. Berrange  * small packets. If @enabled is true, then the
387666a3af9SDaniel P. Berrange  * writes may be delayed in order to opportunistically
388666a3af9SDaniel P. Berrange  * merge small packets into larger ones. If @enabled
389666a3af9SDaniel P. Berrange  * is false, writes are dispatched immediately with
390666a3af9SDaniel P. Berrange  * no delay.
391666a3af9SDaniel P. Berrange  *
392666a3af9SDaniel P. Berrange  * When @enabled is false, applications may wish to
393666a3af9SDaniel P. Berrange  * use the qio_channel_set_cork() method to explicitly
394666a3af9SDaniel P. Berrange  * control write merging.
395666a3af9SDaniel P. Berrange  *
396666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
397666a3af9SDaniel P. Berrange  * API corresponds to the inverse of TCP_NODELAY flag,
398666a3af9SDaniel P. Berrange  * controlling whether the Nagle algorithm is active.
399666a3af9SDaniel P. Berrange  *
400666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
401666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
402666a3af9SDaniel P. Berrange  * error.
403666a3af9SDaniel P. Berrange  */
404666a3af9SDaniel P. Berrange void qio_channel_set_delay(QIOChannel *ioc,
405666a3af9SDaniel P. Berrange                            bool enabled);
406666a3af9SDaniel P. Berrange 
407666a3af9SDaniel P. Berrange /**
408666a3af9SDaniel P. Berrange  * qio_channel_set_cork:
409666a3af9SDaniel P. Berrange  * @ioc: the channel object
410666a3af9SDaniel P. Berrange  * @enabled: the new flag state
411666a3af9SDaniel P. Berrange  *
412666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
413666a3af9SDaniel P. Berrange  * permitted to dispatch data that is written.
414666a3af9SDaniel P. Berrange  * If @enabled is true, then any data written will
415666a3af9SDaniel P. Berrange  * be queued in local buffers until @enabled is
416666a3af9SDaniel P. Berrange  * set to false once again.
417666a3af9SDaniel P. Berrange  *
418666a3af9SDaniel P. Berrange  * This feature is typically used when the automatic
419666a3af9SDaniel P. Berrange  * write coalescing facility is disabled via the
420666a3af9SDaniel P. Berrange  * qio_channel_set_delay() method.
421666a3af9SDaniel P. Berrange  *
422666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
423666a3af9SDaniel P. Berrange  * API corresponds to the TCP_CORK flag.
424666a3af9SDaniel P. Berrange  *
425666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
426666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
427666a3af9SDaniel P. Berrange  * error.
428666a3af9SDaniel P. Berrange  */
429666a3af9SDaniel P. Berrange void qio_channel_set_cork(QIOChannel *ioc,
430666a3af9SDaniel P. Berrange                           bool enabled);
431666a3af9SDaniel P. Berrange 
432666a3af9SDaniel P. Berrange 
433666a3af9SDaniel P. Berrange /**
434666a3af9SDaniel P. Berrange  * qio_channel_seek:
435666a3af9SDaniel P. Berrange  * @ioc: the channel object
436666a3af9SDaniel P. Berrange  * @offset: the position to seek to, relative to @whence
437666a3af9SDaniel P. Berrange  * @whence: one of the (POSIX) SEEK_* constants listed below
438821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
439666a3af9SDaniel P. Berrange  *
440666a3af9SDaniel P. Berrange  * Moves the current I/O position within the channel
441666a3af9SDaniel P. Berrange  * @ioc, to be @offset. The value of @offset is
442666a3af9SDaniel P. Berrange  * interpreted relative to @whence:
443666a3af9SDaniel P. Berrange  *
444666a3af9SDaniel P. Berrange  * SEEK_SET - the position is set to @offset bytes
445666a3af9SDaniel P. Berrange  * SEEK_CUR - the position is moved by @offset bytes
446666a3af9SDaniel P. Berrange  * SEEK_END - the position is set to end of the file plus @offset bytes
447666a3af9SDaniel P. Berrange  *
448666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
449666a3af9SDaniel P. Berrange  * so may report an error.
450666a3af9SDaniel P. Berrange  *
451666a3af9SDaniel P. Berrange  * Returns: the new position on success, (off_t)-1 on failure
452666a3af9SDaniel P. Berrange  */
453666a3af9SDaniel P. Berrange off_t qio_channel_io_seek(QIOChannel *ioc,
454666a3af9SDaniel P. Berrange                           off_t offset,
455666a3af9SDaniel P. Berrange                           int whence,
456666a3af9SDaniel P. Berrange                           Error **errp);
457666a3af9SDaniel P. Berrange 
458666a3af9SDaniel P. Berrange 
459666a3af9SDaniel P. Berrange /**
460666a3af9SDaniel P. Berrange  * qio_channel_create_watch:
461666a3af9SDaniel P. Berrange  * @ioc: the channel object
462666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
463666a3af9SDaniel P. Berrange  *
464666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
465666a3af9SDaniel P. Berrange  * for the I/O condition @condition. Typically the
466666a3af9SDaniel P. Berrange  * qio_channel_add_watch() method would be used instead
467666a3af9SDaniel P. Berrange  * of this, since it directly attaches a callback to
468666a3af9SDaniel P. Berrange  * the source
469666a3af9SDaniel P. Berrange  *
470666a3af9SDaniel P. Berrange  * Returns: the new main loop source.
471666a3af9SDaniel P. Berrange  */
472666a3af9SDaniel P. Berrange GSource *qio_channel_create_watch(QIOChannel *ioc,
473666a3af9SDaniel P. Berrange                                   GIOCondition condition);
474666a3af9SDaniel P. Berrange 
475666a3af9SDaniel P. Berrange /**
476666a3af9SDaniel P. Berrange  * qio_channel_add_watch:
477666a3af9SDaniel P. Berrange  * @ioc: the channel object
478666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
479666a3af9SDaniel P. Berrange  * @func: callback to invoke when the source becomes ready
480666a3af9SDaniel P. Berrange  * @user_data: opaque data to pass to @func
481666a3af9SDaniel P. Berrange  * @notify: callback to free @user_data
482666a3af9SDaniel P. Berrange  *
483666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
484666a3af9SDaniel P. Berrange  * for the I/O condition @condition. The callback @func
485666a3af9SDaniel P. Berrange  * will be registered against the source, to be invoked
486666a3af9SDaniel P. Berrange  * when the source becomes ready. The optional @user_data
487666a3af9SDaniel P. Berrange  * will be passed to @func when it is invoked. The @notify
488666a3af9SDaniel P. Berrange  * callback will be used to free @user_data when the
489666a3af9SDaniel P. Berrange  * watch is deleted
490666a3af9SDaniel P. Berrange  *
491666a3af9SDaniel P. Berrange  * The returned source ID can be used with g_source_remove()
492666a3af9SDaniel P. Berrange  * to remove and free the source when no longer required.
493666a3af9SDaniel P. Berrange  * Alternatively the @func callback can return a FALSE
494666a3af9SDaniel P. Berrange  * value.
495666a3af9SDaniel P. Berrange  *
496666a3af9SDaniel P. Berrange  * Returns: the source ID
497666a3af9SDaniel P. Berrange  */
498666a3af9SDaniel P. Berrange guint qio_channel_add_watch(QIOChannel *ioc,
499666a3af9SDaniel P. Berrange                             GIOCondition condition,
500666a3af9SDaniel P. Berrange                             QIOChannelFunc func,
501666a3af9SDaniel P. Berrange                             gpointer user_data,
502666a3af9SDaniel P. Berrange                             GDestroyNotify notify);
503666a3af9SDaniel P. Berrange 
504666a3af9SDaniel P. Berrange 
505666a3af9SDaniel P. Berrange /**
506666a3af9SDaniel P. Berrange  * qio_channel_yield:
507666a3af9SDaniel P. Berrange  * @ioc: the channel object
508666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
509666a3af9SDaniel P. Berrange  *
510666a3af9SDaniel P. Berrange  * Yields execution from the current coroutine until
511666a3af9SDaniel P. Berrange  * the condition indicated by @condition becomes
512666a3af9SDaniel P. Berrange  * available.
513666a3af9SDaniel P. Berrange  *
514666a3af9SDaniel P. Berrange  * This must only be called from coroutine context
515666a3af9SDaniel P. Berrange  */
516666a3af9SDaniel P. Berrange void qio_channel_yield(QIOChannel *ioc,
517666a3af9SDaniel P. Berrange                        GIOCondition condition);
518666a3af9SDaniel P. Berrange 
519666a3af9SDaniel P. Berrange /**
520666a3af9SDaniel P. Berrange  * qio_channel_wait:
521666a3af9SDaniel P. Berrange  * @ioc: the channel object
522666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
523666a3af9SDaniel P. Berrange  *
524666a3af9SDaniel P. Berrange  * Block execution from the current thread until
525666a3af9SDaniel P. Berrange  * the condition indicated by @condition becomes
526666a3af9SDaniel P. Berrange  * available.
527666a3af9SDaniel P. Berrange  *
528666a3af9SDaniel P. Berrange  * This will enter a nested event loop to perform
529666a3af9SDaniel P. Berrange  * the wait.
530666a3af9SDaniel P. Berrange  */
531666a3af9SDaniel P. Berrange void qio_channel_wait(QIOChannel *ioc,
532666a3af9SDaniel P. Berrange                       GIOCondition condition);
533666a3af9SDaniel P. Berrange 
534*bf88c124SPaolo Bonzini /**
535*bf88c124SPaolo Bonzini  * qio_channel_set_aio_fd_handler:
536*bf88c124SPaolo Bonzini  * @ioc: the channel object
537*bf88c124SPaolo Bonzini  * @ctx: the AioContext to set the handlers on
538*bf88c124SPaolo Bonzini  * @io_read: the read handler
539*bf88c124SPaolo Bonzini  * @io_write: the write handler
540*bf88c124SPaolo Bonzini  * @opaque: the opaque value passed to the handler
541*bf88c124SPaolo Bonzini  *
542*bf88c124SPaolo Bonzini  * This is used internally by qio_channel_yield().  It can
543*bf88c124SPaolo Bonzini  * be used by channel implementations to forward the handlers
544*bf88c124SPaolo Bonzini  * to another channel (e.g. from #QIOChannelTLS to the
545*bf88c124SPaolo Bonzini  * underlying socket).
546*bf88c124SPaolo Bonzini  */
547*bf88c124SPaolo Bonzini void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
548*bf88c124SPaolo Bonzini                                     AioContext *ctx,
549*bf88c124SPaolo Bonzini                                     IOHandler *io_read,
550*bf88c124SPaolo Bonzini                                     IOHandler *io_write,
551*bf88c124SPaolo Bonzini                                     void *opaque);
552*bf88c124SPaolo Bonzini 
5532a6a4076SMarkus Armbruster #endif /* QIO_CHANNEL_H */
554