2
0

qemu-file-channel.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * QEMUFile backend for QIOChannel objects
  3. *
  4. * Copyright (c) 2015-2016 Red Hat, Inc
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu-file-channel.h"
  26. #include "qemu-file.h"
  27. #include "io/channel-socket.h"
  28. #include "qemu/iov.h"
  29. static ssize_t channel_writev_buffer(void *opaque,
  30. struct iovec *iov,
  31. int iovcnt,
  32. int64_t pos,
  33. Error **errp)
  34. {
  35. QIOChannel *ioc = QIO_CHANNEL(opaque);
  36. ssize_t done = 0;
  37. struct iovec *local_iov = g_new(struct iovec, iovcnt);
  38. struct iovec *local_iov_head = local_iov;
  39. unsigned int nlocal_iov = iovcnt;
  40. nlocal_iov = iov_copy(local_iov, nlocal_iov,
  41. iov, iovcnt,
  42. 0, iov_size(iov, iovcnt));
  43. while (nlocal_iov > 0) {
  44. ssize_t len;
  45. len = qio_channel_writev(ioc, local_iov, nlocal_iov, errp);
  46. if (len == QIO_CHANNEL_ERR_BLOCK) {
  47. if (qemu_in_coroutine()) {
  48. qio_channel_yield(ioc, G_IO_OUT);
  49. } else {
  50. qio_channel_wait(ioc, G_IO_OUT);
  51. }
  52. continue;
  53. }
  54. if (len < 0) {
  55. done = -EIO;
  56. goto cleanup;
  57. }
  58. iov_discard_front(&local_iov, &nlocal_iov, len);
  59. done += len;
  60. }
  61. cleanup:
  62. g_free(local_iov_head);
  63. return done;
  64. }
  65. static ssize_t channel_get_buffer(void *opaque,
  66. uint8_t *buf,
  67. int64_t pos,
  68. size_t size,
  69. Error **errp)
  70. {
  71. QIOChannel *ioc = QIO_CHANNEL(opaque);
  72. ssize_t ret;
  73. do {
  74. ret = qio_channel_read(ioc, (char *)buf, size, errp);
  75. if (ret < 0) {
  76. if (ret == QIO_CHANNEL_ERR_BLOCK) {
  77. if (qemu_in_coroutine()) {
  78. qio_channel_yield(ioc, G_IO_IN);
  79. } else {
  80. qio_channel_wait(ioc, G_IO_IN);
  81. }
  82. } else {
  83. return -EIO;
  84. }
  85. }
  86. } while (ret == QIO_CHANNEL_ERR_BLOCK);
  87. return ret;
  88. }
  89. static int channel_close(void *opaque, Error **errp)
  90. {
  91. int ret;
  92. QIOChannel *ioc = QIO_CHANNEL(opaque);
  93. ret = qio_channel_close(ioc, errp);
  94. object_unref(OBJECT(ioc));
  95. return ret;
  96. }
  97. static int channel_shutdown(void *opaque,
  98. bool rd,
  99. bool wr,
  100. Error **errp)
  101. {
  102. QIOChannel *ioc = QIO_CHANNEL(opaque);
  103. if (qio_channel_has_feature(ioc,
  104. QIO_CHANNEL_FEATURE_SHUTDOWN)) {
  105. QIOChannelShutdown mode;
  106. if (rd && wr) {
  107. mode = QIO_CHANNEL_SHUTDOWN_BOTH;
  108. } else if (rd) {
  109. mode = QIO_CHANNEL_SHUTDOWN_READ;
  110. } else {
  111. mode = QIO_CHANNEL_SHUTDOWN_WRITE;
  112. }
  113. if (qio_channel_shutdown(ioc, mode, errp) < 0) {
  114. return -EIO;
  115. }
  116. }
  117. return 0;
  118. }
  119. static int channel_set_blocking(void *opaque,
  120. bool enabled,
  121. Error **errp)
  122. {
  123. QIOChannel *ioc = QIO_CHANNEL(opaque);
  124. if (qio_channel_set_blocking(ioc, enabled, errp) < 0) {
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. static QEMUFile *channel_get_input_return_path(void *opaque)
  130. {
  131. QIOChannel *ioc = QIO_CHANNEL(opaque);
  132. return qemu_fopen_channel_output(ioc);
  133. }
  134. static QEMUFile *channel_get_output_return_path(void *opaque)
  135. {
  136. QIOChannel *ioc = QIO_CHANNEL(opaque);
  137. return qemu_fopen_channel_input(ioc);
  138. }
  139. static const QEMUFileOps channel_input_ops = {
  140. .get_buffer = channel_get_buffer,
  141. .close = channel_close,
  142. .shut_down = channel_shutdown,
  143. .set_blocking = channel_set_blocking,
  144. .get_return_path = channel_get_input_return_path,
  145. };
  146. static const QEMUFileOps channel_output_ops = {
  147. .writev_buffer = channel_writev_buffer,
  148. .close = channel_close,
  149. .shut_down = channel_shutdown,
  150. .set_blocking = channel_set_blocking,
  151. .get_return_path = channel_get_output_return_path,
  152. };
  153. QEMUFile *qemu_fopen_channel_input(QIOChannel *ioc)
  154. {
  155. object_ref(OBJECT(ioc));
  156. return qemu_fopen_ops(ioc, &channel_input_ops);
  157. }
  158. QEMUFile *qemu_fopen_channel_output(QIOChannel *ioc)
  159. {
  160. object_ref(OBJECT(ioc));
  161. return qemu_fopen_ops(ioc, &channel_output_ops);
  162. }