Execution of kvm4nfv cyclictest based on job type.
[kvmfornfv.git] / qemu / io / channel-buffer.c
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 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/sockets.h"
25 #include "trace.h"
26
27 QIOChannelBuffer *
28 qio_channel_buffer_new(size_t capacity)
29 {
30     QIOChannelBuffer *ioc;
31
32     ioc = QIO_CHANNEL_BUFFER(object_new(TYPE_QIO_CHANNEL_BUFFER));
33
34     if (capacity) {
35         ioc->data = g_new0(uint8_t, capacity);
36         ioc->capacity = capacity;
37     }
38
39     return ioc;
40 }
41
42
43 static void qio_channel_buffer_finalize(Object *obj)
44 {
45     QIOChannelBuffer *ioc = QIO_CHANNEL_BUFFER(obj);
46     g_free(ioc->data);
47     ioc->capacity = ioc->usage = ioc->offset = 0;
48 }
49
50
51 static ssize_t qio_channel_buffer_readv(QIOChannel *ioc,
52                                         const struct iovec *iov,
53                                         size_t niov,
54                                         int **fds,
55                                         size_t *nfds,
56                                         Error **errp)
57 {
58     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
59     ssize_t ret = 0;
60     size_t i;
61
62     for (i = 0; i < niov; i++) {
63         size_t want = iov[i].iov_len;
64         if (bioc->offset >= bioc->usage) {
65             break;
66         }
67         if ((bioc->offset + want) > bioc->usage)  {
68             want = bioc->usage - bioc->offset;
69         }
70         memcpy(iov[i].iov_base, bioc->data + bioc->offset, want);
71         ret += want;
72         bioc->offset += want;
73     }
74
75     return ret;
76 }
77
78 static ssize_t qio_channel_buffer_writev(QIOChannel *ioc,
79                                          const struct iovec *iov,
80                                          size_t niov,
81                                          int *fds,
82                                          size_t nfds,
83                                          Error **errp)
84 {
85     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
86     ssize_t ret = 0;
87     size_t i;
88     size_t towrite = 0;
89
90     for (i = 0; i < niov; i++) {
91         towrite += iov[i].iov_len;
92     }
93
94     if ((bioc->offset + towrite) > bioc->capacity) {
95         bioc->capacity = bioc->offset + towrite;
96         bioc->data = g_realloc(bioc->data, bioc->capacity);
97     }
98
99     if (bioc->offset > bioc->usage) {
100         memset(bioc->data, 0, bioc->offset - bioc->usage);
101         bioc->usage = bioc->offset;
102     }
103
104     for (i = 0; i < niov; i++) {
105         memcpy(bioc->data + bioc->usage,
106                iov[i].iov_base,
107                iov[i].iov_len);
108         bioc->usage += iov[i].iov_len;
109         bioc->offset += iov[i].iov_len;
110         ret += iov[i].iov_len;
111     }
112
113     return ret;
114 }
115
116 static int qio_channel_buffer_set_blocking(QIOChannel *ioc G_GNUC_UNUSED,
117                                            bool enabled G_GNUC_UNUSED,
118                                            Error **errp G_GNUC_UNUSED)
119 {
120     return 0;
121 }
122
123
124 static off_t qio_channel_buffer_seek(QIOChannel *ioc,
125                                      off_t offset,
126                                      int whence,
127                                      Error **errp)
128 {
129     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
130
131     bioc->offset = offset;
132
133     return offset;
134 }
135
136
137 static int qio_channel_buffer_close(QIOChannel *ioc,
138                                     Error **errp)
139 {
140     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
141
142     g_free(bioc->data);
143     bioc->capacity = bioc->usage = bioc->offset = 0;
144
145     return 0;
146 }
147
148
149 typedef struct QIOChannelBufferSource QIOChannelBufferSource;
150 struct QIOChannelBufferSource {
151     GSource parent;
152     QIOChannelBuffer *bioc;
153     GIOCondition condition;
154 };
155
156 static gboolean
157 qio_channel_buffer_source_prepare(GSource *source,
158                                   gint *timeout)
159 {
160     QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
161
162     *timeout = -1;
163
164     return (G_IO_IN | G_IO_OUT) & bsource->condition;
165 }
166
167 static gboolean
168 qio_channel_buffer_source_check(GSource *source)
169 {
170     QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
171
172     return (G_IO_IN | G_IO_OUT) & bsource->condition;
173 }
174
175 static gboolean
176 qio_channel_buffer_source_dispatch(GSource *source,
177                                    GSourceFunc callback,
178                                    gpointer user_data)
179 {
180     QIOChannelFunc func = (QIOChannelFunc)callback;
181     QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
182
183     return (*func)(QIO_CHANNEL(bsource->bioc),
184                    ((G_IO_IN | G_IO_OUT) & bsource->condition),
185                    user_data);
186 }
187
188 static void
189 qio_channel_buffer_source_finalize(GSource *source)
190 {
191     QIOChannelBufferSource *ssource = (QIOChannelBufferSource *)source;
192
193     object_unref(OBJECT(ssource->bioc));
194 }
195
196 GSourceFuncs qio_channel_buffer_source_funcs = {
197     qio_channel_buffer_source_prepare,
198     qio_channel_buffer_source_check,
199     qio_channel_buffer_source_dispatch,
200     qio_channel_buffer_source_finalize
201 };
202
203 static GSource *qio_channel_buffer_create_watch(QIOChannel *ioc,
204                                                 GIOCondition condition)
205 {
206     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
207     QIOChannelBufferSource *ssource;
208     GSource *source;
209
210     source = g_source_new(&qio_channel_buffer_source_funcs,
211                           sizeof(QIOChannelBufferSource));
212     ssource = (QIOChannelBufferSource *)source;
213
214     ssource->bioc = bioc;
215     object_ref(OBJECT(bioc));
216
217     ssource->condition = condition;
218
219     return source;
220 }
221
222
223 static void qio_channel_buffer_class_init(ObjectClass *klass,
224                                           void *class_data G_GNUC_UNUSED)
225 {
226     QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
227
228     ioc_klass->io_writev = qio_channel_buffer_writev;
229     ioc_klass->io_readv = qio_channel_buffer_readv;
230     ioc_klass->io_set_blocking = qio_channel_buffer_set_blocking;
231     ioc_klass->io_seek = qio_channel_buffer_seek;
232     ioc_klass->io_close = qio_channel_buffer_close;
233     ioc_klass->io_create_watch = qio_channel_buffer_create_watch;
234 }
235
236 static const TypeInfo qio_channel_buffer_info = {
237     .parent = TYPE_QIO_CHANNEL,
238     .name = TYPE_QIO_CHANNEL_BUFFER,
239     .instance_size = sizeof(QIOChannelBuffer),
240     .instance_finalize = qio_channel_buffer_finalize,
241     .class_init = qio_channel_buffer_class_init,
242 };
243
244 static void qio_channel_buffer_register_types(void)
245 {
246     type_register_static(&qio_channel_buffer_info);
247 }
248
249 type_init(qio_channel_buffer_register_types);