xref: /openbmc/qemu/migration/file.c (revision 5b73b248)
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 static void file_enable_direct_io(int *flags)
54 {
55 #ifdef O_DIRECT
56     *flags |= O_DIRECT;
57 #else
58     /* it should have been rejected when setting the parameter */
59     g_assert_not_reached();
60 #endif
61 }
62 
63 bool file_send_channel_create(gpointer opaque, Error **errp)
64 {
65     QIOChannelFile *ioc;
66     int flags = O_WRONLY;
67     bool ret = true;
68 
69     if (migrate_direct_io()) {
70         /*
71          * Enable O_DIRECT for the secondary channels. These are used
72          * for sending ram pages and writes should be guaranteed to be
73          * aligned to at least page size.
74          */
75         file_enable_direct_io(&flags);
76     }
77 
78     ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp);
79     if (!ioc) {
80         ret = false;
81         goto out;
82     }
83 
84     multifd_channel_connect(opaque, QIO_CHANNEL(ioc));
85 
86 out:
87     /*
88      * File channel creation is synchronous. However posting this
89      * semaphore here is simpler than adding a special case.
90      */
91     multifd_send_channel_created();
92 
93     return ret;
94 }
95 
96 void file_start_outgoing_migration(MigrationState *s,
97                                    FileMigrationArgs *file_args, Error **errp)
98 {
99     g_autoptr(QIOChannelFile) fioc = NULL;
100     g_autofree char *filename = g_strdup(file_args->filename);
101     uint64_t offset = file_args->offset;
102     QIOChannel *ioc;
103 
104     trace_migration_file_outgoing(filename);
105 
106     fioc = qio_channel_file_new_path(filename, O_CREAT | O_WRONLY, 0600, errp);
107     if (!fioc) {
108         return;
109     }
110 
111     if (ftruncate(fioc->fd, offset)) {
112         error_setg_errno(errp, errno,
113                          "failed to truncate migration file to offset %" PRIx64,
114                          offset);
115         return;
116     }
117 
118     outgoing_args.fname = g_strdup(filename);
119 
120     ioc = QIO_CHANNEL(fioc);
121     if (offset && qio_channel_io_seek(ioc, offset, SEEK_SET, errp) < 0) {
122         return;
123     }
124     qio_channel_set_name(ioc, "migration-file-outgoing");
125     migration_channel_connect(s, ioc, NULL, NULL);
126 }
127 
128 static gboolean file_accept_incoming_migration(QIOChannel *ioc,
129                                                GIOCondition condition,
130                                                gpointer opaque)
131 {
132     migration_channel_process_incoming(ioc);
133     object_unref(OBJECT(ioc));
134     return G_SOURCE_REMOVE;
135 }
136 
137 static void file_create_incoming_channels(QIOChannel *ioc, char *filename,
138                                           Error **errp)
139 {
140     int i, channels = 1;
141     g_autofree QIOChannel **iocs = NULL;
142     int flags = O_RDONLY;
143 
144     if (migrate_multifd()) {
145         channels += migrate_multifd_channels();
146         if (migrate_direct_io()) {
147             file_enable_direct_io(&flags);
148         }
149     }
150 
151     iocs = g_new0(QIOChannel *, channels);
152     iocs[0] = ioc;
153 
154     for (i = 1; i < channels; i++) {
155         QIOChannelFile *fioc = qio_channel_file_new_path(filename, flags, 0, errp);
156 
157         if (!fioc) {
158             while (i) {
159                 object_unref(iocs[--i]);
160             }
161             return;
162         }
163 
164         iocs[i] = QIO_CHANNEL(fioc);
165     }
166 
167     for (i = 0; i < channels; i++) {
168         qio_channel_set_name(iocs[i], "migration-file-incoming");
169         qio_channel_add_watch_full(iocs[i], G_IO_IN,
170                                    file_accept_incoming_migration,
171                                    NULL, NULL,
172                                    g_main_context_get_thread_default());
173     }
174 }
175 
176 void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp)
177 {
178     g_autofree char *filename = g_strdup(file_args->filename);
179     QIOChannelFile *fioc = NULL;
180     uint64_t offset = file_args->offset;
181 
182     trace_migration_file_incoming(filename);
183 
184     fioc = qio_channel_file_new_path(filename, O_RDONLY, 0, errp);
185     if (!fioc) {
186         return;
187     }
188 
189     if (offset &&
190         qio_channel_io_seek(QIO_CHANNEL(fioc), offset, SEEK_SET, errp) < 0) {
191         object_unref(OBJECT(fioc));
192         return;
193     }
194 
195     file_create_incoming_channels(QIO_CHANNEL(fioc), filename, errp);
196 }
197 
198 int file_write_ramblock_iov(QIOChannel *ioc, const struct iovec *iov,
199                             int niov, RAMBlock *block, Error **errp)
200 {
201     ssize_t ret = 0;
202     int i, slice_idx, slice_num;
203     uintptr_t base, next, offset;
204     size_t len;
205 
206     slice_idx = 0;
207     slice_num = 1;
208 
209     /*
210      * If the iov array doesn't have contiguous elements, we need to
211      * split it in slices because we only have one file offset for the
212      * whole iov. Do this here so callers don't need to break the iov
213      * array themselves.
214      */
215     for (i = 0; i < niov; i++, slice_num++) {
216         base = (uintptr_t) iov[i].iov_base;
217 
218         if (i != niov - 1) {
219             len = iov[i].iov_len;
220             next = (uintptr_t) iov[i + 1].iov_base;
221 
222             if (base + len == next) {
223                 continue;
224             }
225         }
226 
227         /*
228          * Use the offset of the first element of the segment that
229          * we're sending.
230          */
231         offset = (uintptr_t) iov[slice_idx].iov_base - (uintptr_t) block->host;
232         if (offset >= block->used_length) {
233             error_setg(errp, "offset %" PRIxPTR
234                        "outside of ramblock %s range", offset, block->idstr);
235             ret = -1;
236             break;
237         }
238 
239         ret = qio_channel_pwritev(ioc, &iov[slice_idx], slice_num,
240                                   block->pages_offset + offset, errp);
241         if (ret < 0) {
242             break;
243         }
244 
245         slice_idx += slice_num;
246         slice_num = 0;
247     }
248 
249     return (ret < 0) ? ret : 0;
250 }
251 
252 int multifd_file_recv_data(MultiFDRecvParams *p, Error **errp)
253 {
254     MultiFDRecvData *data = p->data;
255     size_t ret;
256 
257     ret = qio_channel_pread(p->c, (char *) data->opaque,
258                             data->size, data->file_offset, errp);
259     if (ret != data->size) {
260         error_prepend(errp,
261                       "multifd recv (%u): read 0x%zx, expected 0x%zx",
262                       p->id, ret, data->size);
263         return -1;
264     }
265 
266     return 0;
267 }
268