Daniel P. Berrange | a9cfeb3 | 2016-04-27 11:04:58 +0100 | [diff] [blame] | 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 | |
| 25 | #include "qemu/osdep.h" |
Juan Quintela | 40014d8 | 2017-04-17 19:34:36 +0200 | [diff] [blame] | 26 | #include "qemu-file-channel.h" |
Juan Quintela | 08a0aee | 2017-04-20 18:52:18 +0200 | [diff] [blame] | 27 | #include "exec/cpu-common.h" |
| 28 | #include "qemu-file.h" |
Daniel P. Berrange | a9cfeb3 | 2016-04-27 11:04:58 +0100 | [diff] [blame] | 29 | #include "io/channel-socket.h" |
| 30 | #include "qemu/iov.h" |
| 31 | |
| 32 | |
| 33 | static ssize_t channel_writev_buffer(void *opaque, |
| 34 | struct iovec *iov, |
| 35 | int iovcnt, |
| 36 | int64_t pos) |
| 37 | { |
| 38 | QIOChannel *ioc = QIO_CHANNEL(opaque); |
| 39 | ssize_t done = 0; |
| 40 | struct iovec *local_iov = g_new(struct iovec, iovcnt); |
| 41 | struct iovec *local_iov_head = local_iov; |
| 42 | unsigned int nlocal_iov = iovcnt; |
| 43 | |
| 44 | nlocal_iov = iov_copy(local_iov, nlocal_iov, |
| 45 | iov, iovcnt, |
| 46 | 0, iov_size(iov, iovcnt)); |
| 47 | |
| 48 | while (nlocal_iov > 0) { |
| 49 | ssize_t len; |
| 50 | len = qio_channel_writev(ioc, local_iov, nlocal_iov, NULL); |
| 51 | if (len == QIO_CHANNEL_ERR_BLOCK) { |
| 52 | qio_channel_wait(ioc, G_IO_OUT); |
| 53 | continue; |
| 54 | } |
| 55 | if (len < 0) { |
| 56 | /* XXX handle Error objects */ |
| 57 | done = -EIO; |
| 58 | goto cleanup; |
| 59 | } |
| 60 | |
| 61 | iov_discard_front(&local_iov, &nlocal_iov, len); |
| 62 | done += len; |
| 63 | } |
| 64 | |
| 65 | cleanup: |
| 66 | g_free(local_iov_head); |
| 67 | return done; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static ssize_t channel_get_buffer(void *opaque, |
| 72 | uint8_t *buf, |
| 73 | int64_t pos, |
| 74 | size_t size) |
| 75 | { |
| 76 | QIOChannel *ioc = QIO_CHANNEL(opaque); |
| 77 | ssize_t ret; |
| 78 | |
| 79 | do { |
| 80 | ret = qio_channel_read(ioc, (char *)buf, size, NULL); |
| 81 | if (ret < 0) { |
| 82 | if (ret == QIO_CHANNEL_ERR_BLOCK) { |
| 83 | qio_channel_yield(ioc, G_IO_IN); |
| 84 | } else { |
| 85 | /* XXX handle Error * object */ |
| 86 | return -EIO; |
| 87 | } |
| 88 | } |
| 89 | } while (ret == QIO_CHANNEL_ERR_BLOCK); |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static int channel_close(void *opaque) |
| 96 | { |
| 97 | QIOChannel *ioc = QIO_CHANNEL(opaque); |
| 98 | qio_channel_close(ioc, NULL); |
| 99 | object_unref(OBJECT(ioc)); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static int channel_shutdown(void *opaque, |
| 105 | bool rd, |
| 106 | bool wr) |
| 107 | { |
| 108 | QIOChannel *ioc = QIO_CHANNEL(opaque); |
| 109 | |
| 110 | if (qio_channel_has_feature(ioc, |
| 111 | QIO_CHANNEL_FEATURE_SHUTDOWN)) { |
| 112 | QIOChannelShutdown mode; |
| 113 | if (rd && wr) { |
| 114 | mode = QIO_CHANNEL_SHUTDOWN_BOTH; |
| 115 | } else if (rd) { |
| 116 | mode = QIO_CHANNEL_SHUTDOWN_READ; |
| 117 | } else { |
| 118 | mode = QIO_CHANNEL_SHUTDOWN_WRITE; |
| 119 | } |
| 120 | if (qio_channel_shutdown(ioc, mode, NULL) < 0) { |
| 121 | /* XXX handler Error * object */ |
| 122 | return -EIO; |
| 123 | } |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static int channel_set_blocking(void *opaque, |
| 130 | bool enabled) |
| 131 | { |
| 132 | QIOChannel *ioc = QIO_CHANNEL(opaque); |
| 133 | |
| 134 | if (qio_channel_set_blocking(ioc, enabled, NULL) < 0) { |
| 135 | return -1; |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static QEMUFile *channel_get_input_return_path(void *opaque) |
| 141 | { |
| 142 | QIOChannel *ioc = QIO_CHANNEL(opaque); |
| 143 | |
| 144 | return qemu_fopen_channel_output(ioc); |
| 145 | } |
| 146 | |
| 147 | static QEMUFile *channel_get_output_return_path(void *opaque) |
| 148 | { |
| 149 | QIOChannel *ioc = QIO_CHANNEL(opaque); |
| 150 | |
| 151 | return qemu_fopen_channel_input(ioc); |
| 152 | } |
| 153 | |
| 154 | static const QEMUFileOps channel_input_ops = { |
| 155 | .get_buffer = channel_get_buffer, |
| 156 | .close = channel_close, |
| 157 | .shut_down = channel_shutdown, |
| 158 | .set_blocking = channel_set_blocking, |
| 159 | .get_return_path = channel_get_input_return_path, |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | static const QEMUFileOps channel_output_ops = { |
| 164 | .writev_buffer = channel_writev_buffer, |
| 165 | .close = channel_close, |
| 166 | .shut_down = channel_shutdown, |
| 167 | .set_blocking = channel_set_blocking, |
| 168 | .get_return_path = channel_get_output_return_path, |
| 169 | }; |
| 170 | |
| 171 | |
| 172 | QEMUFile *qemu_fopen_channel_input(QIOChannel *ioc) |
| 173 | { |
| 174 | object_ref(OBJECT(ioc)); |
| 175 | return qemu_fopen_ops(ioc, &channel_input_ops); |
| 176 | } |
| 177 | |
| 178 | QEMUFile *qemu_fopen_channel_output(QIOChannel *ioc) |
| 179 | { |
| 180 | object_ref(OBJECT(ioc)); |
| 181 | return qemu_fopen_ops(ioc, &channel_output_ops); |
| 182 | } |