channel-buffer.c 7.1 KB

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