1 /* 2 * Copyright (c) 2021-2023 Oracle and/or its affiliates. 3 * 4 * This work is licensed under the terms of the GNU GPL, version 2 or later. 5 * See the COPYING file in the top-level directory. 6 */ 7 8 #include "qemu/osdep.h" 9 #include "exec/ramblock.h" 10 #include "qemu/cutils.h" 11 #include "qemu/error-report.h" 12 #include "qapi/error.h" 13 #include "channel.h" 14 #include "file.h" 15 #include "migration.h" 16 #include "io/channel-file.h" 17 #include "io/channel-socket.h" 18 #include "io/channel-util.h" 19 #include "options.h" 20 #include "trace.h" 21 22 #define OFFSET_OPTION ",offset=" 23 24 static struct FileOutgoingArgs { 25 char *fname; 26 } outgoing_args; 27 28 /* Remove the offset option from @filespec and return it in @offsetp. */ 29 30 int file_parse_offset(char *filespec, uint64_t *offsetp, Error **errp) 31 { 32 char *option = strstr(filespec, OFFSET_OPTION); 33 int ret; 34 35 if (option) { 36 *option = 0; 37 option += sizeof(OFFSET_OPTION) - 1; 38 ret = qemu_strtosz(option, NULL, offsetp); 39 if (ret) { 40 error_setg_errno(errp, -ret, "file URI has bad offset %s", option); 41 return -1; 42 } 43 } 44 return 0; 45 } 46 47 void file_cleanup_outgoing_migration(void) 48 { 49 g_free(outgoing_args.fname); 50 outgoing_args.fname = NULL; 51 } 52 53 bool file_send_channel_create(gpointer opaque, Error **errp) 54 { 55 QIOChannelFile *ioc; 56 int flags = O_WRONLY; 57 bool ret = true; 58 59 ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp); 60 if (!ioc) { 61 ret = false; 62 goto out; 63 } 64 65 multifd_channel_connect(opaque, QIO_CHANNEL(ioc)); 66 67 out: 68 /* 69 * File channel creation is synchronous. However posting this 70 * semaphore here is simpler than adding a special case. 71 */ 72 multifd_send_channel_created(); 73 74 return ret; 75 } 76 77 void file_start_outgoing_migration(MigrationState *s, 78 FileMigrationArgs *file_args, Error **errp) 79 { 80 g_autoptr(QIOChannelFile) fioc = NULL; 81 g_autofree char *filename = g_strdup(file_args->filename); 82 uint64_t offset = file_args->offset; 83 QIOChannel *ioc; 84 85 trace_migration_file_outgoing(filename); 86 87 fioc = qio_channel_file_new_path(filename, O_CREAT | O_WRONLY, 0600, errp); 88 if (!fioc) { 89 return; 90 } 91 92 if (ftruncate(fioc->fd, offset)) { 93 error_setg_errno(errp, errno, 94 "failed to truncate migration file to offset %" PRIx64, 95 offset); 96 object_unref(OBJECT(fioc)); 97 return; 98 } 99 100 outgoing_args.fname = g_strdup(filename); 101 102 ioc = QIO_CHANNEL(fioc); 103 if (offset && qio_channel_io_seek(ioc, offset, SEEK_SET, errp) < 0) { 104 object_unref(OBJECT(fioc)); 105 return; 106 } 107 qio_channel_set_name(ioc, "migration-file-outgoing"); 108 migration_channel_connect(s, ioc, NULL, NULL); 109 } 110 111 static gboolean file_accept_incoming_migration(QIOChannel *ioc, 112 GIOCondition condition, 113 gpointer opaque) 114 { 115 migration_channel_process_incoming(ioc); 116 object_unref(OBJECT(ioc)); 117 return G_SOURCE_REMOVE; 118 } 119 120 void file_create_incoming_channels(QIOChannel *ioc, Error **errp) 121 { 122 int i, fd, channels = 1; 123 g_autofree QIOChannel **iocs = NULL; 124 125 if (migrate_multifd()) { 126 channels += migrate_multifd_channels(); 127 } 128 129 iocs = g_new0(QIOChannel *, channels); 130 fd = QIO_CHANNEL_FILE(ioc)->fd; 131 iocs[0] = ioc; 132 133 for (i = 1; i < channels; i++) { 134 QIOChannelFile *fioc = qio_channel_file_new_dupfd(fd, errp); 135 136 if (!fioc) { 137 while (i) { 138 object_unref(iocs[--i]); 139 } 140 return; 141 } 142 143 iocs[i] = QIO_CHANNEL(fioc); 144 } 145 146 for (i = 0; i < channels; i++) { 147 qio_channel_set_name(iocs[i], "migration-file-incoming"); 148 qio_channel_add_watch_full(iocs[i], G_IO_IN, 149 file_accept_incoming_migration, 150 NULL, NULL, 151 g_main_context_get_thread_default()); 152 } 153 } 154 155 void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp) 156 { 157 g_autofree char *filename = g_strdup(file_args->filename); 158 QIOChannelFile *fioc = NULL; 159 uint64_t offset = file_args->offset; 160 161 trace_migration_file_incoming(filename); 162 163 fioc = qio_channel_file_new_path(filename, O_RDONLY, 0, errp); 164 if (!fioc) { 165 return; 166 } 167 168 if (offset && 169 qio_channel_io_seek(QIO_CHANNEL(fioc), offset, SEEK_SET, errp) < 0) { 170 object_unref(OBJECT(fioc)); 171 return; 172 } 173 174 file_create_incoming_channels(QIO_CHANNEL(fioc), errp); 175 } 176 177 int file_write_ramblock_iov(QIOChannel *ioc, const struct iovec *iov, 178 int niov, RAMBlock *block, Error **errp) 179 { 180 ssize_t ret = 0; 181 int i, slice_idx, slice_num; 182 uintptr_t base, next, offset; 183 size_t len; 184 185 slice_idx = 0; 186 slice_num = 1; 187 188 /* 189 * If the iov array doesn't have contiguous elements, we need to 190 * split it in slices because we only have one file offset for the 191 * whole iov. Do this here so callers don't need to break the iov 192 * array themselves. 193 */ 194 for (i = 0; i < niov; i++, slice_num++) { 195 base = (uintptr_t) iov[i].iov_base; 196 197 if (i != niov - 1) { 198 len = iov[i].iov_len; 199 next = (uintptr_t) iov[i + 1].iov_base; 200 201 if (base + len == next) { 202 continue; 203 } 204 } 205 206 /* 207 * Use the offset of the first element of the segment that 208 * we're sending. 209 */ 210 offset = (uintptr_t) iov[slice_idx].iov_base - (uintptr_t) block->host; 211 if (offset >= block->used_length) { 212 error_setg(errp, "offset %" PRIxPTR 213 "outside of ramblock %s range", offset, block->idstr); 214 ret = -1; 215 break; 216 } 217 218 ret = qio_channel_pwritev(ioc, &iov[slice_idx], slice_num, 219 block->pages_offset + offset, errp); 220 if (ret < 0) { 221 break; 222 } 223 224 slice_idx += slice_num; 225 slice_num = 0; 226 } 227 228 return (ret < 0) ? ret : 0; 229 } 230 231 int multifd_file_recv_data(MultiFDRecvParams *p, Error **errp) 232 { 233 MultiFDRecvData *data = p->data; 234 size_t ret; 235 236 ret = qio_channel_pread(p->c, (char *) data->opaque, 237 data->size, data->file_offset, errp); 238 if (ret != data->size) { 239 error_prepend(errp, 240 "multifd recv (%u): read 0x%zx, expected 0x%zx", 241 p->id, ret, data->size); 242 return -1; 243 } 244 245 return 0; 246 } 247