2
0

channel-buffer.c 7.0 KB

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