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