diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index 5926ac70b..78ff205d3 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -1434,15 +1434,7 @@ impl Vmm { common_cpuid, memory_manager_data: vm.memory_manager_data(), }; - let config_data = serde_json::to_vec(&vm_migration_config).unwrap(); - Request::config(config_data.len() as u64).write_to(&mut socket)?; - socket - .write_all(&config_data) - .map_err(MigratableError::MigrateSocket)?; - migration_transport::expect_ok_response( - &mut socket, - MigratableError::MigrateSend(anyhow!("Error during config migration")), - )?; + migration_transport::send_config(&mut socket, &vm_migration_config)?; // Let every Migratable object know about the migration being started. vm.start_migration()?; diff --git a/vmm/src/migration_transport.rs b/vmm/src/migration_transport.rs index 990f14473..6cf3e5bd1 100644 --- a/vmm/src/migration_transport.rs +++ b/vmm/src/migration_transport.rs @@ -3,17 +3,19 @@ // SPDX-License-Identifier: Apache-2.0 // +use std::io::Write; use std::net::{TcpListener, TcpStream}; use std::os::unix::net::{UnixListener, UnixStream}; use std::path::PathBuf; use std::result::Result; -use anyhow::anyhow; +use anyhow::{Context, anyhow}; use log::info; +use serde_json; use vm_migration::MigratableError; use vm_migration::protocol::{Request, Response}; -use crate::SocketStream; +use crate::{SocketStream, VmMigrationConfig}; /// Extract a UNIX socket path from a "unix:" migration URL. fn socket_url_to_path(url: &str) -> Result { @@ -97,3 +99,21 @@ pub(crate) fn send_request_expect_ok( request.write_to(socket)?; expect_ok_response(socket, error) } + +/// Serialize and send the VM configuration payload. +pub(crate) fn send_config( + socket: &mut SocketStream, + config: &VmMigrationConfig, +) -> Result<(), MigratableError> { + let config_data = serde_json::to_vec(config) + .context("Error serializing VM migration config") + .map_err(MigratableError::MigrateSend)?; + Request::config(config_data.len() as u64).write_to(socket)?; + socket + .write_all(&config_data) + .map_err(MigratableError::MigrateSocket)?; + expect_ok_response( + socket, + MigratableError::MigrateSend(anyhow!("Error during config migration")), + ) +}