Skip to content
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

Support shared mmap #2

Open
wants to merge 3 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
2 changes: 1 addition & 1 deletion docs/mmds/mmds-user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ The session must start with an HTTP `PUT` request that generates the session tok
In order to be successful, the request must respect the following constraints:

- must be directed towards `/latest/api/token` path
- must contain a `X-ametadata-token-ttl-seconds` header specifying the token lifetime
- must contain a `X-metadata-token-ttl-seconds` header specifying the token lifetime
in seconds. The value cannot be lower than 1 or greater than 21600 (6 hours).
- must not contain a `X-Forwarded-For` header.

Expand Down
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 @@ -596,6 +596,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
13 changes: 13 additions & 0 deletions resources/seccomp/x86_64-unknown-linux-musl.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,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
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 @@ -17,6 +17,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_backing_file::parse_put_memory_backing_file;
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 @@ -112,6 +113,7 @@ impl ParsedRequest {
(Method::Put, "network-interfaces", Some(body)) => {
parse_put_net(body, path_tokens.get(1))
}
(Method::Put, "memory-backing-file", Some(body)) => parse_put_memory_backing_file(body),
(Method::Put, "shutdown-internal", None) => {
Ok(ParsedRequest::new(RequestAction::ShutdownInternal))
}
Expand Down
42 changes: 42 additions & 0 deletions src/api_server/src/request/memory_backing_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 logger::{IncMetric, METRICS};
use vmm::vmm_config::memory_backing_file::MemoryBackingFileConfig;

pub(crate) fn parse_put_memory_backing_file(body: &Body) -> Result<ParsedRequest, Error> {
METRICS.put_api_requests.memory_backing_file_cfg_count.inc();
Ok(ParsedRequest::new_sync(VmmAction::SetMemoryBackingFile(
serde_json::from_slice::<MemoryBackingFileConfig>(body.raw()).map_err(|e| {
METRICS.put_api_requests.memory_backing_file_cfg_fails.inc();
Error::SerdeJson(e)
})?,
)))
}

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

use super::*;

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

let body = r#"{
"path": "./memory.snap"
}"#;
let same_body = MemoryBackingFileConfig {
path: PathBuf::from("./memory.snap"),
};
let result = parse_put_memory_backing_file(&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::SetMemoryBackingFile(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 @@ -8,6 +8,7 @@ pub mod drive;
pub mod instance_info;
pub mod logger;
pub mod machine_configuration;
pub mod memory_backing_file;
pub mod metrics;
pub mod mmds;
pub mod net;
Expand Down
31 changes: 31 additions & 0 deletions src/api_server/swagger/firecracker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,29 @@ paths:
description: Internal server error
schema:
$ref: "#/definitions/Error"

/memory-backing-file:
put:
summary: Configures a memory backing file to sync the memory changes to during the runtime of the vm
operationId: putMemoryBackingFile
parameters:
- name: body
in: body
description: Path to memory backing file
required: true
schema:
$ref: "#/definitions/MemoryBackingFile"
responses:
204:
description: Memory backing file configured
400:
description: Memory backing file failed
schema:
$ref: "#/definitions/Error"
default:
description: Internal server error.
schema:
$ref: "#/definitions/Error"

/metrics:
put:
Expand Down Expand Up @@ -1047,6 +1070,14 @@ definitions:
tx_rate_limiter:
$ref: "#/definitions/RateLimiter"

MemoryBackingFile:
type: object
required:
- path
properties:
path:
type: string

PartialDrive:
type: object
required:
Expand Down
3 changes: 3 additions & 0 deletions src/cpuid/src/transformer/amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ impl CpuidTransformer for AmdCpuidTransformer {
leaf_0x8000001d::LEAF_NUM => Some(amd::update_extended_cache_topology_entry),
leaf_0x8000001e::LEAF_NUM => Some(amd::update_extended_apic_id_entry),
0x8000_0002..=0x8000_0004 => Some(common::update_brand_string_entry),

// hypervisor stuff
0x4000_0001 => Some(common::disable_kvm_feature_async_pf),
_ => None,
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/cpuid/src/transformer/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ pub fn update_brand_string_entry(
Ok(())
}

// KVM feature bits
#[cfg(target_arch = "x86_64")]
const KVM_FEATURE_ASYNC_PF_INT_BIT: u32 = 14;

pub fn disable_kvm_feature_async_pf(
entry: &mut kvm_cpuid_entry2,
vm_spec: &VmSpec,
) -> Result<(), Error> {
entry.eax.write_bit(KVM_FEATURE_ASYNC_PF_INT_BIT, false);

Ok(())
}

pub fn update_cache_parameters_entry(
entry: &mut kvm_cpuid_entry2,
vm_spec: &VmSpec,
Expand Down
2 changes: 2 additions & 0 deletions src/cpuid/src/transformer/intel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ impl CpuidTransformer for IntelCpuidTransformer {
leaf_0xa::LEAF_NUM => Some(intel::update_perf_mon_entry),
leaf_0xb::LEAF_NUM => Some(intel::update_extended_topology_entry),
0x8000_0002..=0x8000_0004 => Some(common::update_brand_string_entry),
// hypervisor stuff
0x4000_0001 => Some(common::disable_kvm_feature_async_pf),
_ => None,
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/logger/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ pub struct PutRequestsMetrics {
pub machine_cfg_count: SharedIncMetric,
/// Number of failures in configuring the machine.
pub machine_cfg_fails: SharedIncMetric,
/// Number of PUTs for setting memory backing file.
pub memory_backing_file_cfg_count: SharedIncMetric,
/// Number of failures in configuring the machine.
pub memory_backing_file_cfg_fails: SharedIncMetric,
/// Number of PUTs for initializing the metrics system.
pub metrics_count: SharedIncMetric,
/// Number of failures in initializing the metrics system.
Expand Down
6 changes: 3 additions & 3 deletions src/mmds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl fmt::Display for Error {
),
Error::NoTtlProvided => write!(
f,
"Token time to live value not found. Use `X-metadata-token-ttl_seconds` header to \
"Token time to live value not found. Use `X-metadata-token-ttl-seconds` header to \
specify the token's lifetime."
),
Error::ResourceNotFound(ref uri) => {
Expand Down Expand Up @@ -705,8 +705,8 @@ mod tests {

assert_eq!(
Error::NoTtlProvided.to_string(),
"Token time to live value not found. Use `X-metadata-token-ttl_seconds` header to \
specify the token's lifetime."
"Token time to live value not found. Use `X-metadata-token-ttl-seconds` header to \
specify the token's lifetime."
);

assert_eq!(
Expand Down
22 changes: 20 additions & 2 deletions src/mmds/src/token_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ impl TokenHeaders {
/// Return `TokenHeaders` from headers map.
pub fn try_from(map: &HashMap<String, String>) -> Result<TokenHeaders, RequestError> {
let mut headers = Self::default();
let lowercased_headers: HashMap<String, String> = map
.iter()
.map(|(k, v)| (k.to_lowercase(), v.clone()))
.collect();

if let Some(token) = map.get(TokenHeaders::X_METADATA_TOKEN) {
if let Some(token) = lowercased_headers.get(&TokenHeaders::X_METADATA_TOKEN.to_lowercase())
{
headers.x_metadata_token = Some(token.to_string());
}

if let Some(value) = map.get(TokenHeaders::X_METADATA_TOKEN_TTL_SECONDS) {
if let Some(value) =
lowercased_headers.get(&TokenHeaders::X_METADATA_TOKEN_TTL_SECONDS.to_lowercase())
{
match value.parse::<u32>() {
Ok(seconds) => {
headers.x_metadata_token_ttl_seconds = Some(seconds);
Expand Down Expand Up @@ -127,6 +134,17 @@ mod tests {
let headers = TokenHeaders::try_from(&map).unwrap();
assert_eq!(*headers.x_metadata_token().unwrap(), "".to_string());

// Lowercased headers
let mut map: HashMap<String, String> = HashMap::default();
map.insert(
TokenHeaders::X_METADATA_TOKEN_TTL_SECONDS
.to_string()
.to_lowercase(),
"60".to_string(),
);
let headers = TokenHeaders::try_from(&map).unwrap();
assert_eq!(headers.x_metadata_token_ttl_seconds().unwrap(), 60);

// Invalid value.
let mut map: HashMap<String, String> = HashMap::default();
map.insert(
Expand Down
2 changes: 1 addition & 1 deletion src/vm-memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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
Loading