-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[virtio-mem] Add hotplug/hotunplug support with statically allocated memory region #5462
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
Manciukic
wants to merge
14
commits into
firecracker-microvm:feature/virtio-mem
Choose a base branch
from
Manciukic:virtio-mem/patch-api
base: feature/virtio-mem
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5377a55
feat(virtio-mem): add static allocation of hotpluggable memory
Manciukic c324adc
feat(virtio-mem): wire PATCH support
Manciukic b2f0cfc
doc(virtio-mem): document PATCH API in swagger and docs
Manciukic 84c7db5
test(virtio-mem): add API tests for PATCH
Manciukic 1425c7c
feat(virtio-mem): add virtio request parsing and dummy response
Manciukic a1ae2ca
feat(virtio-mem): implement virtio requests
Manciukic adcdddb
test(virtio-mem): add unit tests for virtio queue request handling
Manciukic 0c8c045
test(metrics): add virtio-mem device metrics to validation
Manciukic d4b0f9a
fix(examples/uffd): unregister range on UFFD Remove event
Manciukic 9efa1b9
feat(test/balloon): include HugePages in RSS measurements
Manciukic 9b27bcf
refactor(test/balloon): move logic to get guest avail mem to framework
Manciukic 9fca25d
test(virtio-mem): add functional integration tests for device
Manciukic 2069fc3
chore(test/virtio-mem): move tests under performance
Manciukic 370eb4e
test(virtio-mem): add rust integration tests
Manciukic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use vm_memory::{ByteValued, GuestAddress}; | ||
|
||
use crate::devices::virtio::generated::virtio_mem; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
pub struct RequestedRange { | ||
pub(crate) addr: GuestAddress, | ||
pub(crate) nb_blocks: usize, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
pub(crate) enum Request { | ||
Plug(RequestedRange), | ||
Unplug(RequestedRange), | ||
UnplugAll, | ||
State(RequestedRange), | ||
Unsupported(u32), | ||
} | ||
|
||
// SAFETY: this is safe, trust me bro | ||
unsafe impl ByteValued for virtio_mem::virtio_mem_req {} | ||
|
||
impl From<virtio_mem::virtio_mem_req> for Request { | ||
fn from(req: virtio_mem::virtio_mem_req) -> Self { | ||
match req.type_.into() { | ||
// SAFETY: union type is checked in the match | ||
virtio_mem::VIRTIO_MEM_REQ_PLUG => unsafe { | ||
Request::Plug(RequestedRange { | ||
addr: GuestAddress(req.u.plug.addr), | ||
nb_blocks: req.u.plug.nb_blocks.into(), | ||
}) | ||
}, | ||
// SAFETY: union type is checked in the match | ||
virtio_mem::VIRTIO_MEM_REQ_UNPLUG => unsafe { | ||
Request::Unplug(RequestedRange { | ||
addr: GuestAddress(req.u.unplug.addr), | ||
nb_blocks: req.u.unplug.nb_blocks.into(), | ||
}) | ||
}, | ||
virtio_mem::VIRTIO_MEM_REQ_UNPLUG_ALL => Request::UnplugAll, | ||
// SAFETY: union type is checked in the match | ||
virtio_mem::VIRTIO_MEM_REQ_STATE => unsafe { | ||
Request::State(RequestedRange { | ||
addr: GuestAddress(req.u.state.addr), | ||
nb_blocks: req.u.state.nb_blocks.into(), | ||
}) | ||
}, | ||
t => Request::Unsupported(t), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum ResponseType { | ||
Ack, | ||
Nack, | ||
Busy, | ||
Error, | ||
} | ||
|
||
impl From<ResponseType> for u16 { | ||
fn from(code: ResponseType) -> Self { | ||
match code { | ||
ResponseType::Ack => virtio_mem::VIRTIO_MEM_RESP_ACK, | ||
ResponseType::Nack => virtio_mem::VIRTIO_MEM_RESP_NACK, | ||
ResponseType::Busy => virtio_mem::VIRTIO_MEM_RESP_BUSY, | ||
ResponseType::Error => virtio_mem::VIRTIO_MEM_RESP_ERROR, | ||
} | ||
.try_into() | ||
.unwrap() | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum BlockRangeState { | ||
Plugged, | ||
Unplugged, | ||
Mixed, | ||
} | ||
|
||
impl From<BlockRangeState> for virtio_mem::virtio_mem_resp_state { | ||
fn from(code: BlockRangeState) -> Self { | ||
virtio_mem::virtio_mem_resp_state { | ||
state: match code { | ||
BlockRangeState::Plugged => virtio_mem::VIRTIO_MEM_STATE_PLUGGED, | ||
BlockRangeState::Unplugged => virtio_mem::VIRTIO_MEM_STATE_UNPLUGGED, | ||
BlockRangeState::Mixed => virtio_mem::VIRTIO_MEM_STATE_MIXED, | ||
} | ||
.try_into() | ||
.unwrap(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Response { | ||
pub resp_type: ResponseType, | ||
// Only for State requests | ||
pub state: Option<BlockRangeState>, | ||
} | ||
|
||
impl Response { | ||
pub(crate) fn error() -> Self { | ||
Response { | ||
resp_type: ResponseType::Error, | ||
state: None, | ||
} | ||
} | ||
|
||
pub(crate) fn ack() -> Self { | ||
Response { | ||
resp_type: ResponseType::Ack, | ||
state: None, | ||
} | ||
} | ||
|
||
pub(crate) fn ack_with_state(state: BlockRangeState) -> Self { | ||
Response { | ||
resp_type: ResponseType::Ack, | ||
state: Some(state), | ||
} | ||
} | ||
} | ||
|
||
// SAFETY: Plain data structures | ||
unsafe impl ByteValued for virtio_mem::virtio_mem_resp {} | ||
|
||
impl From<Response> for virtio_mem::virtio_mem_resp { | ||
fn from(resp: Response) -> Self { | ||
let mut out = virtio_mem::virtio_mem_resp { | ||
type_: resp.resp_type.into(), | ||
..Default::default() | ||
}; | ||
if let Some(state) = resp.state { | ||
out.u.state = state.into(); | ||
} | ||
out | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really needed? This enum just repeats what
VIRTIO_MEM_RESP_
consts are. If you use constants directly, there is no need for this enum or any conversion code.Or at least you can use
repr(u16)
to remove to u16 conversion and totally skipVIRTIO_MEM_RESP_
values since thes enum will have same values.