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