Skip to content

uffd async pf #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
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
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added build.rs
Empty file.
13 changes: 13 additions & 0 deletions resources/seccomp/aarch64-unknown-linux-musl.json
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,19 @@
}
]
},
{
"syscall": "msync",
"comment": "Used to sync memory from mmap to disk",
"args": [
{
"index": 2,
"type": "dword",
"op": "eq",
"val": 4,
"comment": "MS_SYNC"
}
]
},
{
"syscall": "rt_sigaction",
"comment": "rt_sigaction is used by libc::abort during a panic to install the default handler for SIGABRT",
Expand Down
25 changes: 25 additions & 0 deletions resources/seccomp/x86_64-unknown-linux-musl.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,31 @@
}
]
},
{
"syscall": "msync",
"comment": "Used to sync memory from mmap to disk",
"args": [
{
"index": 2,
"type": "dword",
"op": "eq",
"val": 4,
"comment": "MS_SYNC"
}
]
},
{
"syscall": "memfd_create",
"comment": "Used to create a memory backed file descriptor that can be used to save memory to"
},
{
"syscall": "nanosleep",
"comment": "Debugging sleep"
},
{
"syscall": "copy_file_range",
"comment": "debugging"
},
{
"syscall": "rt_sigaction",
"comment": "rt_sigaction is used by libc::abort during a panic to install the default handler for SIGABRT",
Expand Down
2 changes: 2 additions & 0 deletions src/api_server/src/parsed_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::request::logger::parse_put_logger;
use crate::request::machine_configuration::{
parse_get_machine_config, parse_patch_machine_config, parse_put_machine_config,
};
use crate::request::memory_backend::parse_put_memory_backend;
use crate::request::metrics::parse_put_metrics;
use crate::request::mmds::{parse_get_mmds, parse_patch_mmds, parse_put_mmds};
use crate::request::net::{parse_patch_net, parse_put_net};
Expand Down Expand Up @@ -91,6 +92,7 @@ impl TryFrom<&Request> for ParsedRequest {
(Method::Put, "drives", Some(body)) => parse_put_drive(body, path_tokens.next()),
(Method::Put, "logger", Some(body)) => parse_put_logger(body),
(Method::Put, "machine-config", Some(body)) => parse_put_machine_config(body),
(Method::Put, "memory-backend", Some(body)) => parse_put_memory_backend(body),
(Method::Put, "metrics", Some(body)) => parse_put_metrics(body),
(Method::Put, "mmds", Some(body)) => parse_put_mmds(body, path_tokens.next()),
(Method::Put, "network-interfaces", Some(body)) => {
Expand Down
46 changes: 46 additions & 0 deletions src/api_server/src/request/memory_backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use super::super::VmmAction;
use crate::parsed_request::{Error, ParsedRequest};
use crate::request::Body;
use vmm::logger::{IncMetric, METRICS};
use vmm::vmm_config::snapshot::MemBackendConfig;

pub(crate) fn parse_put_memory_backend(body: &Body) -> Result<ParsedRequest, Error> {
METRICS.put_api_requests.memory_backend_cfg_count.inc();
Ok(ParsedRequest::new_sync(VmmAction::SetMemoryBackend(
serde_json::from_slice::<MemBackendConfig>(body.raw()).map_err(|e| {
METRICS.put_api_requests.memory_backend_cfg_fails.inc();
Error::SerdeJson(e)
})?,
)))
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use vmm::vmm_config::snapshot::MemBackendType;

use super::*;

#[test]
fn test_parse_memory_backing_file() {
assert!(parse_put_memory_backend(&Body::new("invalid_payload")).is_err());

let body = r#"{
"backend_type": "File",
"backend_path": "./memory.snap"
}"#;
let same_body = MemBackendConfig {
backend_type: MemBackendType::File,
backend_path: PathBuf::from("./memory.snap"),
};
let result = parse_put_memory_backend(&Body::new(body));
assert!(result.is_ok());
let parsed_req = result.unwrap_or_else(|_e| panic!("Failed test."));

assert!(parsed_req == ParsedRequest::new_sync(VmmAction::SetMemoryBackend(same_body)));
}
}
1 change: 1 addition & 0 deletions src/api_server/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod entropy;
pub mod instance_info;
pub mod logger;
pub mod machine_configuration;
pub mod memory_backend;
pub mod metrics;
pub mod mmds;
pub mod net;
Expand Down
23 changes: 23 additions & 0 deletions src/api_server/swagger/firecracker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,29 @@ paths:
description: Internal server error
schema:
$ref: "#/definitions/Error"

/memory-backend:
put:
summary: Configures a memory backend to sync the memory changes from during the runtime of the vm
operationId: putMemoryBackend
parameters:
- name: body
in: body
description: The memory backend to use
required: true
schema:
$ref: "#/definitions/MemoryBackend"
responses:
204:
description: Memory backend configured
400:
description: Memory backend failed
schema:
$ref: "#/definitions/Error"
default:
description: Internal server error.
schema:
$ref: "#/definitions/Error"

/metrics:
put:
Expand Down
9 changes: 7 additions & 2 deletions src/firecracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ vmm = { path = "../vmm" }

[dev-dependencies]
cargo_toml = "0.16.0"
regex = { version = "1.9.5", default-features = false, features = ["std", "unicode-perl"] }
regex = { version = "1.9.5", default-features = false, features = [
"std",
"unicode-perl",
] }

# Dev-Dependencies for uffd examples
serde = { version = "1.0.188", features = ["derive"] }
userfaultfd = "0.7.0"
userfaultfd = { git = "https://github.com/codesandbox/userfaultfd-rs.git", rev = "27b2ff3bc71774b79338afca051d49cb07146d90", features = [
"linux5_7",
] }

[build-dependencies]
bincode = "1.2.1"
Expand Down
2 changes: 1 addition & 1 deletion src/jailer/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ impl Env {
// section), this latter part is not desirable in Firecracker's
// threat model. Copying prevents 2 Firecracker processes from
// sharing memory.
fs::copy(&self.exec_file_path, &self.chroot_dir).map_err(|err| {
fs::hard_link(&self.exec_file_path, &self.chroot_dir).map_err(|err| {
JailerError::Copy(self.exec_file_path.clone(), self.chroot_dir.clone(), err)
})?;

Expand Down
7 changes: 4 additions & 3 deletions src/snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,10 @@ impl Snapshot {
object
.serialize(&mut writer, &self.version_map, self.target_version)
.map_err(Error::Versionize)?;
writer
.flush()
.map_err(|ref err| Error::Io(err.raw_os_error().unwrap_or(libc::EINVAL)))
// writer
// .flush()
// .map_err(|ref err| Error::Io(err.raw_os_error().unwrap_or(libc::EINVAL)))
Ok(())
}

// Returns the current snapshot format version.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/src/vm_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn create_guest_memory(
for region in regions {
let flags = match region.0 {
None => libc::MAP_NORESERVE | libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
Some(_) => libc::MAP_NORESERVE | libc::MAP_PRIVATE,
Some(_) => libc::MAP_NORESERVE | libc::MAP_SHARED,
};

let mmap_region =
Expand Down
13 changes: 9 additions & 4 deletions src/vmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ bench = false
[dependencies]
aws-lc-rs = "1.0.2"
bitflags = "2.0.2"
derive_more = { version = "0.99.17", default-features = false, features = ["from", "display"] }
derive_more = { version = "0.99.17", default-features = false, features = [
"from",
"display",
] }
event-manager = "0.3.0"
kvm-bindings = { version = "0.6.0", features = ["fam-wrappers"] }
kvm-ioctls = "0.15.0"
Expand All @@ -24,21 +27,23 @@ serde_json = "1.0.78"
timerfd = "1.5.0"
thiserror = "1.0.32"
displaydoc = "0.2.4"
userfaultfd = "0.7.0"
userfaultfd = { git = "https://github.com/codesandbox/userfaultfd-rs.git", rev = "27b2ff3bc71774b79338afca051d49cb07146d90", features = [
"linux5_7",
] }
versionize = "0.1.10"
versionize_derive = "0.1.5"
vm-allocator = "0.1.0"
vm-fdt = "0.2.0"
vm-superio = "0.7.0"
log = { version = "0.4.17", features = ["std", "serde"] }
aes-gcm = { version = "0.10.1", default-features = false, features = ["aes"] }
aes-gcm = { version = "0.10.1", default-features = false, features = ["aes"] }
base64 = "0.13.0"
bincode = "1.2.1"
micro_http = { git = "https://github.com/firecracker-microvm/micro-http" }

net_gen = { path = "../net_gen" }
seccompiler = { path = "../seccompiler" }
snapshot = { path = "../snapshot"}
snapshot = { path = "../snapshot" }
utils = { path = "../utils" }
virtio_gen = { path = "../virtio_gen" }

Expand Down
Loading