xref: /openbmc/qemu/io/channel-buffer.c (revision d0bf492f)
1 /*
2  * QEMU I/O channels memory buffer driver
3  *
4  * Copyright (c) 2015 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "qemu/osdep.h"
22 #include "io/channel-buffer.h"
23 #include "io/channel-watch.h"
24 #include "qemu/module.h"
25 #include "qemu/sockets.h"
26 #include "trace.h"
27 
28 QIOChannelBuffer *
29 qio_channel_buffer_new(size_t capacity)
30 {
31     QIOChannelBuffer *ioc;
32 
33     ioc = QIO_CHANNEL_BUFFER(object_new(TYPE_QIO_CHANNEL_BUFFER));
34 
35     if (capacity) {
36         ioc->data = g_new0(uint8_t, capacity);
37         ioc->capacity = capacity;
38     }
39 
40     return ioc;
41 }
42 
43 
44 static void qio_channel_buffer_finalize(Object *obj)
45 {
46     QIOChannelBuffer *ioc = QIO_CHANNEL_BUFFER(obj);
47     g_free(ioc->data);
48     ioc->capacity = ioc->usage = ioc->offset = 0;
49 }
50 
51 
52 static ssize_t qio_channel_buffer_readv(QIOChannel *ioc,
53                                         const struct iovec *iov,
54                                         size_t niov,
55                                         int **fds,
56                                         size_t *nfds,
57                                         int flags,
58                                         Error **errp)
59 {
60     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
61     ssize_t ret = 0;
62     size_t i;
63 
64     for (i = 0; i < niov; i++) {
65         size_t want = iov[i].iov_len;
66         if (bioc->offset >= bioc->usage) {
67             break;
68         }
69         if ((bioc->offset + want) > bioc->usage)  {
70             want = bioc->usage - bioc->offset;
71         }
72         memcpy(iov[i].iov_base, bioc->data + bioc->offset, want);
73         ret += want;
74         bioc->offset += want;
75     }
76 
77     return ret;
78 }
79 
80 static ssize_t qio_channel_buffer_writev(QIOChannel *ioc,
81                                          const struct iovec *iov,
82                                          size_t niov,
83                                          int *fds,
84                                          size_t nfds,
85                                          int flags,
86                                          Error **errp)
87 {
88     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
89     ssize_t ret = 0;
90     size_t i;
91     size_t towrite = 0;
92 
93     for (i = 0; i < niov; i++) {
94         towrite += iov[i].iov_len;
95     }
96 
97     if ((bioc->offset + towrite) > bioc->capacity) {
98         bioc->capacity = bioc->offset + towrite;
99         bioc->data = g_realloc(bioc->data, bioc->capacity);
100     }
101 
102     if (bioc->offset > bioc->usage) {
103         memset(bioc->data, 0, bioc->offset - bioc->usage);
104         bioc->usage = bioc->offset;
105     }
106 
107     for (i = 0; i < niov; i++) {
108         memcpy(bioc->data + bioc->usage,
109                iov[i].iov_base,
110                iov[i].iov_len);
111         bioc->usage += iov[i].iov_len;
112         bioc->offset += iov[i].iov_len;
113         ret += iov[i].iov_len;
114     }
115 
116     return ret;
117 }
118 
119 static int qio_channel_buffer_set_blocking(QIOChannel *ioc G_GNUC_UNUSED,
120                                            bool enabled G_GNUC_UNUSED,
121                                            Error **errp G_GNUC_UNUSED)
122 {
123     return 0;
124 }
125 
126 
127 static off_t qio_channel_buffer_seek(QIOChannel *ioc,
128                                      off_t offset,
129                                      int whence,
130                                      Error **errp)
131 {
132     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
133 
134     bioc->offset = offset;
135 
136     return offset;
137 }
138 
139 
140 static int qio_channel_buffer_close(QIOChannel *ioc,
141                                     Error **errp)
142 {
143     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
144 
145     g_free(bioc->data);
146     bioc->data = NULL;
147     bioc->capacity = bioc->usage = bioc->offset = 0;
148 
149     return 0;
150 }
151 
152 
153 typedef struct QIOChannelBufferSource QIOChannelBufferSource;
154 struct QIOChannelBufferSource {
155     GSource parent;
156     QIOChannelBuffer *bioc;
157     GIOCondition condition;
158 };
159 
160 static gboolean
161 qio_channel_buffer_source_prepare(GSource *source,
162                                   gint *timeout)
163 {
164     QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
165 
166     *timeout = -1;
167 
168     return (G_IO_IN | G_IO_OUT) & bsource->condition;
169 }
170 
171 static gboolean
172 qio_channel_buffer_source_check(GSource *source)
173 {
174     QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
175 
176     return (G_IO_IN | G_IO_OUT) & bsource->condition;
177 }
178 
179 static gboolean
180 qio_channel_buffer_source_dispatch(GSource *source,
181                                    GSourceFunc callback,
182                                    gpointer user_data)
183 {
184     QIOChannelFunc func = (QIOChannelFunc)callback;
185     QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
186 
187     return (*func)(QIO_CHANNEL(bsource->bioc),
188                    ((G_IO_IN | G_IO_OUT) & bsource->condition),
189                    user_data);
190 }
191 
192 static void
193 qio_channel_buffer_source_finalize(GSource *source)
194 {
195     QIOChannelBufferSource *ssource = (QIOChannelBufferSource *)source;
196 
197     object_unref(OBJECT(ssource->bioc));
198 }
199 
200 GSourceFuncs qio_channel_buffer_source_funcs = {
201     qio_channel_buffer_source_prepare,
202     qio_channel_buffer_source_check,
203     qio_channel_buffer_source_dispatch,
204     qio_channel_buffer_source_finalize
205 };
206 
207 static GSource *qio_channel_buffer_create_watch(QIOChannel *ioc,
208                                                 GIOCondition condition)
209 {
210     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
211     QIOChannelBufferSource *ssource;
212     GSource *source;
213 
214     source = g_source_new(&qio_channel_buffer_source_funcs,
215                           sizeof(QIOChannelBufferSource));
216     ssource = (QIOChannelBufferSource *)source;
217 
218     ssource->bioc = bioc;
219     object_ref(OBJECT(bioc));
220 
221     ssource->condition = condition;
222 
223     return source;
224 }
225 
226 
227 static void qio_channel_buffer_class_init(ObjectClass *klass,
228                                           void *class_data G_GNUC_UNUSED)
229 {
230     QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
231 
232     ioc_klass->io_writev = qio_channel_buffer_writev;
233     ioc_klass->io_readv = qio_channel_buffer_readv;
234     ioc_klass->io_set_blocking = qio_channel_buffer_set_blocking;
235     ioc_klass->io_seek = qio_channel_buffer_seek;
236     ioc_klass->io_close = qio_channel_buffer_close;
237     ioc_klass->io_create_watch = qio_channel_buffer_create_watch;
238 }
239 
240 static const TypeInfo qio_channel_buffer_info = {
241     .parent = TYPE_QIO_CHANNEL,
242     .name = TYPE_QIO_CHANNEL_BUFFER,
243     .instance_size = sizeof(QIOChannelBuffer),
244     .instance_finalize = qio_channel_buffer_finalize,
245     .class_init = qio_channel_buffer_class_init,
246 };
247 
248 static void qio_channel_buffer_register_types(void)
249 {
250     type_register_static(&qio_channel_buffer_info);
251 }
252 
253 type_init(qio_channel_buffer_register_types);
254