Skip to content

Commit

Permalink
migration/multifd: Add outgoing QIOChannelFile support
Browse files Browse the repository at this point in the history
Allow multifd to open file-backed channels. This will be used when
enabling the mapped-ram migration stream format which expects a
seekable transport.

The QIOChannel read and write methods will use the preadv/pwritev
versions which don't update the file offset at each call so we can
reuse the fd without re-opening for every channel.

Contrary to the socket migration, the file migration doesn't need an
asynchronous channel creation process, so expose
multifd_channel_connect() and call it directly.

Note that this is just setup code and multifd cannot yet make use of
the file channels.

Signed-off-by: Fabiano Rosas <farosas@suse.de>
Reviewed-by: Peter Xu <peterx@redhat.com>
Link: https://lore.kernel.org/r/20240229153017.2221-18-farosas@suse.de
Signed-off-by: Peter Xu <peterx@redhat.com>
  • Loading branch information
Fabiano Rosas authored and xzpeter committed Mar 1, 2024
1 parent a8a3e71 commit b7b03eb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
37 changes: 37 additions & 0 deletions migration/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
#include "channel.h"
#include "file.h"
#include "migration.h"
#include "multifd.h"
#include "io/channel-file.h"
#include "io/channel-util.h"
#include "trace.h"

#define OFFSET_OPTION ",offset="

static struct FileOutgoingArgs {
char *fname;
} outgoing_args;

/* Remove the offset option from @filespec and return it in @offsetp. */

int file_parse_offset(char *filespec, uint64_t *offsetp, Error **errp)
Expand All @@ -37,6 +42,36 @@ int file_parse_offset(char *filespec, uint64_t *offsetp, Error **errp)
return 0;
}

void file_cleanup_outgoing_migration(void)
{
g_free(outgoing_args.fname);
outgoing_args.fname = NULL;
}

bool file_send_channel_create(gpointer opaque, Error **errp)
{
QIOChannelFile *ioc;
int flags = O_WRONLY;
bool ret = true;

ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp);
if (!ioc) {
ret = false;
goto out;
}

multifd_channel_connect(opaque, QIO_CHANNEL(ioc));

out:
/*
* File channel creation is synchronous. However posting this
* semaphore here is simpler than adding a special case.
*/
multifd_send_channel_created();

return ret;
}

void file_start_outgoing_migration(MigrationState *s,
FileMigrationArgs *file_args, Error **errp)
{
Expand All @@ -53,6 +88,8 @@ void file_start_outgoing_migration(MigrationState *s,
return;
}

outgoing_args.fname = g_strdup(filename);

ioc = QIO_CHANNEL(fioc);
if (offset && qio_channel_io_seek(ioc, offset, SEEK_SET, errp) < 0) {
return;
Expand Down
4 changes: 4 additions & 0 deletions migration/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
#define QEMU_MIGRATION_FILE_H

#include "qapi/qapi-types-migration.h"
#include "io/task.h"
#include "channel.h"

void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp);

void file_start_outgoing_migration(MigrationState *s,
FileMigrationArgs *file_args, Error **errp);
int file_parse_offset(char *filespec, uint64_t *offsetp, Error **errp);
void file_cleanup_outgoing_migration(void);
bool file_send_channel_create(gpointer opaque, Error **errp);
#endif
18 changes: 15 additions & 3 deletions migration/multifd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "exec/ramblock.h"
#include "qemu/error-report.h"
#include "qapi/error.h"
#include "file.h"
#include "ram.h"
#include "migration.h"
#include "migration-stats.h"
Expand All @@ -28,6 +29,7 @@
#include "threadinfo.h"
#include "options.h"
#include "qemu/yank.h"
#include "io/channel-file.h"
#include "io/channel-socket.h"
#include "yank_functions.h"

Expand Down Expand Up @@ -694,6 +696,7 @@ static bool multifd_send_cleanup_channel(MultiFDSendParams *p, Error **errp)
{
if (p->c) {
migration_ioc_unregister_yank(p->c);
qio_channel_close(p->c, &error_abort);
object_unref(OBJECT(p->c));
p->c = NULL;
}
Expand All @@ -715,6 +718,7 @@ static bool multifd_send_cleanup_channel(MultiFDSendParams *p, Error **errp)

static void multifd_send_cleanup_state(void)
{
file_cleanup_outgoing_migration();
socket_cleanup_outgoing_migration();
qemu_sem_destroy(&multifd_send_state->channels_created);
qemu_sem_destroy(&multifd_send_state->channels_ready);
Expand Down Expand Up @@ -977,7 +981,7 @@ static bool multifd_tls_channel_connect(MultiFDSendParams *p,
return true;
}

static void multifd_channel_connect(MultiFDSendParams *p, QIOChannel *ioc)
void multifd_channel_connect(MultiFDSendParams *p, QIOChannel *ioc)
{
qio_channel_set_delay(ioc, false);

Expand Down Expand Up @@ -1045,9 +1049,14 @@ static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque)
error_free(local_err);
}

static void multifd_new_send_channel_create(gpointer opaque)
static bool multifd_new_send_channel_create(gpointer opaque, Error **errp)
{
if (!multifd_use_packets()) {
return file_send_channel_create(opaque, errp);
}

socket_send_channel_create(multifd_new_send_channel_async, opaque);
return true;
}

bool multifd_send_setup(void)
Expand Down Expand Up @@ -1096,7 +1105,10 @@ bool multifd_send_setup(void)
p->page_size = qemu_target_page_size();
p->page_count = page_count;
p->write_flags = 0;
multifd_new_send_channel_create(p);

if (!multifd_new_send_channel_create(p, &local_err)) {
return false;
}
}

/*
Expand Down
1 change: 1 addition & 0 deletions migration/multifd.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,6 @@ static inline void multifd_send_prepare_header(MultiFDSendParams *p)
p->iovs_num++;
}

void multifd_channel_connect(MultiFDSendParams *p, QIOChannel *ioc);

#endif

0 comments on commit b7b03eb

Please sign in to comment.