Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/libcontainer/src/container/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ pub struct CheckpointOptions {
pub link_remap: bool,
pub empty_net_ns: bool,
pub page_server: Option<PageServer>,
pub lazy_pages: bool,
}

/// Address of the CRIU page server that receives the memory pages
Expand Down
1 change: 1 addition & 0 deletions crates/libcontainer/src/container/container_checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl Container {
if let Some(page_server) = &opts.page_server {
criu.set_page_server(page_server.address.clone(), page_server.port.into());
}
criu.set_lazy_pages(opts.lazy_pages);
// youki does not manage network devices, so external dependencies such as the
// host side of a veth pair cannot be described to CRIU. Dumping them makes
// restore fail with "Unknown peer net namespace", so runc sets this unconditionally.
Expand Down
6 changes: 3 additions & 3 deletions crates/liboci-cli/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub struct Checkpoint {
/// Allow shell jobs
#[arg(long)]
pub shell_job: bool,
// TODO: Use lazy migration mechanism
// #[arg(long)]
// pub lazy_pages: bool,
/// Use userfaultfd to lazily restore memory pages
#[arg(long)]
pub lazy_pages: bool,
// TODO: Pass a file descriptor fd to criu. Is u32 the right type?
// #[arg(long)]
// pub status_fd: Option<u32>,
Expand Down
1 change: 1 addition & 0 deletions crates/youki/src/commands/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn checkpoint(args: Checkpoint, root_path: PathBuf) -> Result<()> {
link_remap: args.link_remap,
empty_net_ns: parse_empty_ns(&args.empty_ns)?,
page_server: parse_page_server(args.page_server.as_deref())?,
lazy_pages: args.lazy_pages,
};
container
.checkpoint(&opts)
Expand Down
242 changes: 213 additions & 29 deletions tests/contest/contest/src/tests/lifecycle/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,29 +678,13 @@ impl PageServer {
// The page server writes the received pages to its images dir and exits once
// the dump closes the connection, so the images are complete only after exit.
fn wait_exit(&mut self, timeout: std::time::Duration) -> Result<(), TestResult> {
let deadline = std::time::Instant::now() + timeout;
loop {
match self.child.try_wait() {
Ok(Some(status)) if status.success() => return Ok(()),
Ok(Some(status)) => {
return Err(TestResult::Failed(anyhow!(
"criu page-server exited with failure: {status}"
)));
}
Ok(None) => {}
Err(e) => {
return Err(TestResult::Failed(anyhow!(
"failed to wait for criu page-server: {e}"
)));
}
}
if std::time::Instant::now() >= deadline {
return Err(TestResult::Failed(anyhow!(
"timed out waiting for criu page-server to exit"
)));
}
std::thread::sleep(std::time::Duration::from_millis(100));
let status = wait_child_exit(&mut self.child, "criu page-server", timeout)?;
if !status.success() {
return Err(TestResult::Failed(anyhow!(
"criu page-server exited with failure: {status}"
)));
}
Ok(())
}
}

Expand All @@ -711,6 +695,42 @@ impl Drop for PageServer {
}
}

fn wait_child_exit(
child: &mut std::process::Child,
name: &str,
timeout: std::time::Duration,
) -> Result<std::process::ExitStatus, TestResult> {
let deadline = std::time::Instant::now() + timeout;
loop {
match child.try_wait() {
Ok(Some(status)) => return Ok(status),
Ok(None) => {}
Err(e) => {
return Err(TestResult::Failed(anyhow!(
"failed to wait for {name}: {e}"
)));
}
}
if std::time::Instant::now() >= deadline {
return Err(TestResult::Failed(anyhow!(
"timed out waiting for {name} to exit"
)));
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
}

// Binding to port 0 lets the kernel choose an unused port, which is released
// right away for CRIU to listen on. A fixed port such as runc's 27277 fails with
// "Address already in use" while a page server leaked by an earlier failed run
// is still listening on it.
fn pick_unused_port() -> Result<u16, TestResult> {
std::net::TcpListener::bind("127.0.0.1:0")
.and_then(|l| l.local_addr())
.map(|addr| addr.port())
.map_err(|e| TestResult::Failed(anyhow!("failed to pick an unused port: {e}")))
}

/// Checkpoint a container with `--page-server` and verify that the memory pages
/// were sent to the page server instead of being written to the image path.
pub fn checkpoint_page_server(project_path: &Path, id: &str) -> TestResult {
Expand All @@ -723,13 +743,9 @@ pub fn checkpoint_page_server(project_path: &Path, id: &str) -> TestResult {
Err(e) => return e,
};

// Binding to port 0 lets the kernel choose an unused port, which is released
// right away for criu page-server to listen on. A fixed port such as runc's
// 27277 fails with "Address already in use" while a page server leaked by an
// earlier failed run is still listening on it.
let port = match std::net::TcpListener::bind("127.0.0.1:0").and_then(|l| l.local_addr()) {
Ok(addr) => addr.port(),
Err(e) => return TestResult::Failed(anyhow!("failed to pick an unused port: {e}")),
let port = match pick_unused_port() {
Ok(port) => port,
Err(e) => return e,
};
let mut page_server = match PageServer::start(&page_server_path, port) {
Ok(ps) => ps,
Expand Down Expand Up @@ -785,6 +801,174 @@ pub fn checkpoint_page_server(project_path: &Path, id: &str) -> TestResult {
TestResult::Passed
}

fn is_tcp_listening(port: u16) -> Result<bool, TestResult> {
let entries = procfs::net::tcp()
.map_err(|e| TestResult::Failed(anyhow!("failed to read /proc/net/tcp: {e}")))?;
Ok(entries.iter().any(|entry| {
entry.state == procfs::net::TcpState::Listen && entry.local_address.port() == port
}))
}

fn read_stderr(child: &mut std::process::Child) -> String {
let mut stderr = String::new();
if let Some(mut pipe) = child.stderr.take() {
let _ = pipe.read_to_string(&mut stderr);
}
stderr
}

// With --lazy-pages the dump does not finish until a client fetches the pages
// from the page server that CRIU starts, so the checkpoint runs in the background.
struct LazyPagesCheckpoint {
child: std::process::Child,
port: u16,
}

impl LazyPagesCheckpoint {
fn wait_listening(&mut self, timeout: std::time::Duration) -> Result<(), TestResult> {
let deadline = std::time::Instant::now() + timeout;
loop {
if is_tcp_listening(self.port)? {
return Ok(());
}
match self.child.try_wait() {
Ok(Some(status)) => {
return Err(TestResult::Failed(anyhow!(
"checkpoint exited with {status} before CRIU started listening on \
port {}: {}",
self.port,
read_stderr(&mut self.child)
)));
}
Ok(None) => {}
Err(e) => {
return Err(TestResult::Failed(anyhow!(
"failed to wait for checkpoint: {e}"
)));
}
}
if std::time::Instant::now() >= deadline {
return Err(TestResult::Failed(anyhow!(
"timed out waiting for CRIU to listen on port {}",
self.port
)));
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
}

// A client that closes the connection without requesting any page ends the
// lazy transfer successfully, which lets the dump complete without a restore.
fn finish(&mut self, timeout: std::time::Duration) -> Result<(), TestResult> {
std::net::TcpStream::connect(("127.0.0.1", self.port)).map_err(|e| {
TestResult::Failed(anyhow!(
"failed to connect to the lazy page server on port {}: {e}",
self.port
))
})?;
let status = wait_child_exit(&mut self.child, "checkpoint", timeout)?;
if !status.success() {
return Err(TestResult::Failed(anyhow!(
"checkpoint exited with {status}: {}",
read_stderr(&mut self.child)
)));
}
Ok(())
}
}

impl Drop for LazyPagesCheckpoint {
fn drop(&mut self) {
if let Ok(None) = self.child.try_wait() {
// Killing only the runtime would leave CRIU blocked in accept(), so
// unblock CRIU first and let the checkpoint end by itself.
let _ = std::net::TcpStream::connect(("127.0.0.1", self.port));
if wait_child_exit(
&mut self.child,
"checkpoint",
std::time::Duration::from_secs(10),
)
.is_err()
{
let _ = self.child.kill();
let _ = self.child.wait();
Comment on lines +893 to +894
}
}
}
}

/// Checkpoint a container with `--lazy-pages` and verify that CRIU starts a page
/// server for the memory pages instead of finishing the dump right away.
pub fn checkpoint_lazy_pages(project_path: &Path, id: &str) -> TestResult {
let pid_before = match get_container_pid(project_path, id) {
Ok(p) => p,
Err(e) => return e,
};
let (_temp_dir, image_path) = match create_checkpoint_image_dir() {
Ok(v) => v,
Err(e) => return e,
};
let port = match pick_unused_port() {
Ok(port) => port,
Err(e) => return e,
};

let page_server_addr = format!("127.0.0.1:{port}");
let runtime_path = get_runtime_path();
let child = match Command::new(runtime_path)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--root")
.arg(project_path.join("runtime"))
.arg(match runtime_path {
_ if runtime_path.ends_with("youki") => "checkpointt",
_ => "checkpoint",
})
.arg("--image-path")
.arg(&image_path)
.args([
"--leave-running",
"--lazy-pages",
"--page-server",
&page_server_addr,
])
.arg(id)
.spawn()
{
Ok(child) => child,
Err(e) => return TestResult::Failed(anyhow!("failed to execute checkpoint command: {e}")),
};
let mut checkpoint = LazyPagesCheckpoint { child, port };

// Without --lazy-pages CRIU connects to --page-server as a client and never
// listens on it, so listening proves that the dump went into the lazy mode.
if let Err(e) = checkpoint.wait_listening(std::time::Duration::from_secs(10)) {
return e;
}

// CRIU writes every image before it starts serving the pages lazily.
if !image_path.join("inventory.img").exists() {
return TestResult::Failed(anyhow!(
"{image_path:?}/inventory.img is missing while CRIU serves the pages lazily"
));
}

if let Err(e) = checkpoint.finish(std::time::Duration::from_secs(10)) {
return e;
}

match is_process_running(pid_before) {
Ok(true) => TestResult::Passed,
Ok(false) => TestResult::Failed(anyhow!(
"process (pid {pid_before}) should still be running after checkpoint with \
--leave-running, but it is gone"
)),
Err(err) => TestResult::Failed(anyhow!(
"could not determine whether process (pid {pid_before}) is running: {err}"
)),
}
}

/// Check that a namespace was treated as external by CRIU.
/// Fails if `<img_prefix>-*.img` is absent or lacks `ext_key`.
/// CRIU img files embed protobuf strings as raw UTF-8, so a byte search suffices.
Expand Down
19 changes: 18 additions & 1 deletion tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oci_spec::runtime::{LinuxNamespaceBuilder, LinuxNamespaceType, Spec};
use test_framework::{TestResult, TestableGroup};

use super::{checkpoint, create, delete, exec, kill, start, state};
use crate::utils::{criu_installed, generate_uuid, prepare_bundle, set_config};
use crate::utils::{criu_has_feature, criu_installed, generate_uuid, prepare_bundle, set_config};

/// RAII guard that deletes a named network namespace on drop.
struct NetnsGuard(String);
Expand Down Expand Up @@ -212,6 +212,19 @@ impl ContainerLifecycle {
checkpoint::checkpoint_page_server(self.project_path.path(), &self.container_id)
}

pub fn checkpoint_lazy_pages(&self) -> TestResult {
if !criu_installed() {
return TestResult::Skipped("CRIU is not installed".to_string());
}
if !criu_has_feature("uffd-noncoop") {
return TestResult::Skipped(
"CRIU does not support the uffd-noncoop feature".to_string(),
);
}

checkpoint::checkpoint_lazy_pages(self.project_path.path(), &self.container_id)
}

// NOTE: The following two methods (`checkpoint_link_remap` and
// `checkpoint_with_external_namespaces`) deviate from the pattern used by
// the other checkpoint methods in this impl block. The typical pattern is:
Expand Down Expand Up @@ -354,6 +367,7 @@ impl TestableGroup for ContainerLifecycle {
self.checkpoint_empty_net_ns(),
),
("checkpoint with page-server", self.checkpoint_page_server()),
("checkpoint with lazy-pages", self.checkpoint_lazy_pages()),
("checkpoint with link-remap", Self::checkpoint_link_remap()),
(
"checkpoint with tcp-skip-in-flight",
Expand Down Expand Up @@ -398,6 +412,9 @@ impl TestableGroup for ContainerLifecycle {
"checkpoint_page_server" => {
ret.push(("checkpoint with page-server", self.checkpoint_page_server()))
}
"checkpoint_lazy_pages" => {
ret.push(("checkpoint with lazy-pages", self.checkpoint_lazy_pages()))
}
"checkpoint_link_remap" => {
ret.push(("checkpoint with link-remap", Self::checkpoint_link_remap()))
}
Expand Down
Loading