-
Notifications
You must be signed in to change notification settings - Fork 81
Introduce Virtio-Vsock support #766
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
luigix25
wants to merge
11
commits into
coconut-svsm:main
Choose a base branch
from
luigix25:vsock_attestation
base: main
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eb3e018
kernel: introduce VsockTransport trait for driver abstraction
luigix25 c681b31
Revert "virtio: remove vsock support"
luigix25 f6dd194
virtio-drivers/socket: apply clippy lints
luigix25 3076cb7
virtio-drivers/connectionmanager: track connection established state
luigix25 aece5e1
virtio-drivers/connectionmanager: reject operations after peer shutdown
luigix25 5def08a
virtio-drivers/connectionmanager: add `is_local_port_used`
luigix25 3f51a21
kernel/vsock: introduce virtio-vsock support
luigix25 4080f76
kernel/vsock: introduce VsockStream
luigix25 ea845cd
scripts/launch_guest: add vsock to launch_guest
luigix25 f68932d
github/workflows: enable vhost support in QEMU build and install netcat
n-ramacciotti 831ed1c
kernel/vsock: add in-svsm tests for VsockStream
luigix25 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,110 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // | ||
| // Copyright (c) 2025 Red Hat, Inc. | ||
| // | ||
| // Author: Luigi Leonardi <leonardi@redhat.com> | ||
|
|
||
| use crate::error::SvsmError; | ||
|
|
||
| pub trait VsockTransport: Sync + Send { | ||
| /// Establishes a connection to a remote vsock endpoint. | ||
| /// | ||
| /// This method initiates a connection to the specified remote CID and port | ||
| /// using the provided local port. The call blocks until the connection is | ||
| /// established or fails. | ||
| /// | ||
| /// # Parameters | ||
| /// | ||
| /// * `remote_cid` - The CID of the remote endpoint to connect to | ||
| /// * `local_port` - The local port to use for this connection | ||
| /// * `remote_port` - The remote port to connect to | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// * `Ok()` if the connection was successfully established | ||
| /// * `Err(SvsmError)` if the connection failed | ||
| fn connect(&self, remote_cid: u32, local_port: u32, remote_port: u32) -> Result<(), SvsmError>; | ||
|
|
||
| /// Sends data over an established vsock connection. | ||
| /// | ||
| /// Transmits the contents of the provided buffer to the remote endpoint. | ||
| /// The connection must have been previously established via `connect()`. | ||
| /// | ||
| /// # Parameters | ||
| /// | ||
| /// * `remote_cid` - The CID of the remote endpoint | ||
| /// * `local_port` - The local port of the connection | ||
| /// * `remote_port` - The remote port of the connection | ||
| /// * `buffer` - The data to send | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// * `Ok(usize)` - The number of bytes successfully sent | ||
| /// * `Err(SvsmError)` if the send operation failed | ||
| fn send( | ||
| &self, | ||
| remote_cid: u32, | ||
| local_port: u32, | ||
| remote_port: u32, | ||
| buffer: &[u8], | ||
| ) -> Result<usize, SvsmError>; | ||
|
|
||
| /// Receives data from an established vsock connection. | ||
| /// | ||
| /// Reads data from the remote endpoint into the provided buffer. This method | ||
| /// blocks until all data is available or an error occurs, in such case | ||
| /// returns all the received bytes, if any. | ||
| /// The connection must have been previously established via `connect()`. | ||
| /// | ||
| /// # Parameters | ||
| /// | ||
| /// * `remote_cid` - The CID of the remote endpoint | ||
| /// * `local_port` - The local port of the connection | ||
| /// * `remote_port` - The remote port of the connection | ||
| /// * `buffer` - The buffer to receive data into | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// * `Ok(usize)` - The number of bytes successfully received | ||
| /// * `Err(SvsmError)` if the receive operation failed | ||
| fn recv( | ||
| &self, | ||
| remote_cid: u32, | ||
| local_port: u32, | ||
| remote_port: u32, | ||
| buffer: &mut [u8], | ||
| ) -> Result<usize, SvsmError>; | ||
|
|
||
| /// Shuts down a vsock connection. | ||
| /// | ||
| /// Initiates a graceful shutdown of the connection telling the peer that we won't | ||
| /// send or receive any more data. | ||
| /// | ||
| /// # Parameters | ||
| /// | ||
| /// * `remote_cid` - The CID of the remote endpoint | ||
| /// * `local_port` - The local port of the connection | ||
| /// * `remote_port` - The remote port of the connection | ||
| /// * `force` - Forcibly terminates the connection, without waiting for peer confirm | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// * `Ok()` if the shutdown was successful | ||
| /// * `Err(SvsmError)` if the shutdown failed | ||
| fn shutdown( | ||
| &self, | ||
| remote_cid: u32, | ||
| local_port: u32, | ||
| remote_port: u32, | ||
| force: bool, | ||
| ) -> Result<(), SvsmError>; | ||
|
|
||
| /// Returns whether the given local port is currently in use. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// * `Ok(true)` - The port is in use | ||
| /// * `Ok(false)` - The port is free | ||
| /// * `Err(SvsmError)` if the check could not be performed | ||
| fn is_local_port_used(&self, port: u32) -> Result<bool, SvsmError>; | ||
| } |
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,21 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // | ||
| // Copyright (c) 2025 Red Hat, Inc. | ||
| // | ||
| // Author: Luigi Leonardi <leonardi@redhat.com> | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq)] | ||
| pub enum VsockError { | ||
| /// A connection already exists. | ||
| ConnectionExists, | ||
| /// The device is not connected to any peer. | ||
| NotConnected, | ||
| /// The peer socket has shutdown. | ||
| PeerSocketShutdown, | ||
| /// The local socket has been shutdown. | ||
| SocketShutdown, | ||
| /// No local ports are available. | ||
| NoPortsAvailable, | ||
| /// Generic error for socket operations on a vsock device. | ||
| DriverError, | ||
| } |
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,73 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // | ||
| // Copyright (c) 2025 Red Hat, Inc. | ||
| // | ||
| // Author: Luigi Leonardi <leonardi@redhat.com> | ||
|
|
||
| pub mod api; | ||
| pub mod error; | ||
| pub mod stream; | ||
| #[cfg(feature = "virtio-drivers")] | ||
| pub mod virtio_vsock; | ||
|
|
||
| pub use error::VsockError; | ||
| /// Well-known CID for the host. | ||
| pub const VMADDR_CID_HOST: u32 = 2; | ||
| pub const VMADDR_PORT_ANY: u32 = u32::MAX; | ||
|
|
||
| extern crate alloc; | ||
| use crate::{ | ||
| error::SvsmError, utils::immut_after_init::ImmutAfterInitCell, vsock::api::VsockTransport, | ||
| }; | ||
| use alloc::boxed::Box; | ||
| use core::ops::Deref; | ||
| use core::sync::atomic::{AtomicU32, Ordering}; | ||
|
|
||
| // Currently only one vsock device is supported. | ||
| static VSOCK_DEVICE: ImmutAfterInitCell<VsockDriver> = ImmutAfterInitCell::uninit(); | ||
| // Ports below 1024 are reserved | ||
| const VSOCK_MIN_PORT: u32 = 1024; | ||
| // Number of maximum retries to get a local free port | ||
| const MAX_RETRIES: u32 = 5; | ||
|
|
||
| struct VsockDriver { | ||
| first_free_port: AtomicU32, | ||
| transport: Box<dyn VsockTransport>, | ||
| } | ||
|
|
||
| impl VsockDriver { | ||
| /// Returns a free local port number for a new connection. | ||
| /// | ||
| /// Returns [`VsockError::NoPortsAvailable`] if all | ||
| /// ports are already in use. | ||
| fn get_first_free_port(&self) -> Result<u32, SvsmError> { | ||
| for _ in 0..MAX_RETRIES { | ||
| let candidate_port = | ||
| self.first_free_port | ||
| .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |port| { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this, I'd like some feedback from someone with more experience |
||
| if port >= VMADDR_PORT_ANY - 1 { | ||
| Some(VSOCK_MIN_PORT) | ||
| } else { | ||
| Some(port + 1) | ||
| } | ||
| }); | ||
|
|
||
| // The closure always returns Some, so this never fails. | ||
| let candidate_port = candidate_port.unwrap(); | ||
|
|
||
| if !self.is_local_port_used(candidate_port)? { | ||
| return Ok(candidate_port); | ||
| } | ||
| } | ||
|
|
||
| Err(SvsmError::Vsock(VsockError::NoPortsAvailable)) | ||
| } | ||
| } | ||
|
|
||
| impl Deref for VsockDriver { | ||
| type Target = dyn VsockTransport; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| self.transport.as_ref() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.