-
Notifications
You must be signed in to change notification settings - Fork 13.3k
std: sys: net: uefi: Implement TCP4 connect #139254
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
Ayush1325
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
Ayush1325:uefi-tcp4-connect
base: master
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.
+184
−30
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use super::tcp4; | ||
use crate::io; | ||
use crate::net::SocketAddr; | ||
|
||
pub(crate) enum Tcp { | ||
V4(#[expect(dead_code)] tcp4::Tcp4), | ||
} | ||
|
||
impl Tcp { | ||
pub(crate) fn connect(addr: &SocketAddr) -> io::Result<Self> { | ||
match addr { | ||
SocketAddr::V4(x) => { | ||
let temp = tcp4::Tcp4::new()?; | ||
temp.configure(true, Some(x), None)?; | ||
temp.connect()?; | ||
Ok(Tcp::V4(temp)) | ||
} | ||
SocketAddr::V6(_) => todo!(), | ||
} | ||
} | ||
} |
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,124 @@ | ||
use r_efi::efi::{self, Status}; | ||
use r_efi::protocols::tcp4; | ||
|
||
use crate::io; | ||
use crate::net::SocketAddrV4; | ||
use crate::ptr::NonNull; | ||
use crate::sync::atomic::{AtomicBool, Ordering}; | ||
use crate::sys::pal::helpers; | ||
|
||
const TYPE_OF_SERVICE: u8 = 8; | ||
const TIME_TO_LIVE: u8 = 255; | ||
|
||
pub(crate) struct Tcp4 { | ||
protocol: NonNull<tcp4::Protocol>, | ||
flag: AtomicBool, | ||
#[expect(dead_code)] | ||
service_binding: helpers::ServiceProtocol, | ||
} | ||
|
||
const DEFAULT_ADDR: efi::Ipv4Address = efi::Ipv4Address { addr: [0u8; 4] }; | ||
|
||
impl Tcp4 { | ||
pub(crate) fn new() -> io::Result<Self> { | ||
let service_binding = helpers::ServiceProtocol::open(tcp4::SERVICE_BINDING_PROTOCOL_GUID)?; | ||
let protocol = helpers::open_protocol(service_binding.child_handle(), tcp4::PROTOCOL_GUID)?; | ||
|
||
Ok(Self { service_binding, protocol, flag: AtomicBool::new(false) }) | ||
} | ||
|
||
pub(crate) fn configure( | ||
&self, | ||
active: bool, | ||
remote_address: Option<&SocketAddrV4>, | ||
station_address: Option<&SocketAddrV4>, | ||
) -> io::Result<()> { | ||
let protocol = self.protocol.as_ptr(); | ||
|
||
let (remote_address, remote_port) = if let Some(x) = remote_address { | ||
(helpers::ipv4_to_r_efi(*x.ip()), x.port()) | ||
} else { | ||
(DEFAULT_ADDR, 0) | ||
}; | ||
|
||
// FIXME: Remove when passive connections with proper subnet handling are added | ||
assert!(station_address.is_none()); | ||
let use_default_address = efi::Boolean::TRUE; | ||
let (station_address, station_port) = (DEFAULT_ADDR, 0); | ||
let subnet_mask = helpers::ipv4_to_r_efi(crate::net::Ipv4Addr::new(0, 0, 0, 0)); | ||
|
||
let mut config_data = tcp4::ConfigData { | ||
type_of_service: TYPE_OF_SERVICE, | ||
time_to_live: TIME_TO_LIVE, | ||
access_point: tcp4::AccessPoint { | ||
use_default_address, | ||
remote_address, | ||
remote_port, | ||
active_flag: active.into(), | ||
station_address, | ||
station_port, | ||
subnet_mask, | ||
}, | ||
control_option: crate::ptr::null_mut(), | ||
}; | ||
|
||
let r = unsafe { ((*protocol).configure)(protocol, &mut config_data) }; | ||
if r.is_error() { Err(crate::io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) } | ||
} | ||
|
||
pub(crate) fn connect(&self) -> io::Result<()> { | ||
let evt = unsafe { self.create_evt() }?; | ||
let completion_token = | ||
tcp4::CompletionToken { event: evt.as_ptr(), status: Status::SUCCESS }; | ||
|
||
let protocol = self.protocol.as_ptr(); | ||
let mut conn_token = tcp4::ConnectionToken { completion_token }; | ||
|
||
let r = unsafe { ((*protocol).connect)(protocol, &mut conn_token) }; | ||
if r.is_error() { | ||
return Err(io::Error::from_raw_os_error(r.as_usize())); | ||
} | ||
|
||
self.wait_for_flag().unwrap(); | ||
|
||
if completion_token.status.is_error() { | ||
Err(io::Error::from_raw_os_error(completion_token.status.as_usize())) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
unsafe fn create_evt(&self) -> io::Result<helpers::OwnedEvent> { | ||
self.flag.store(false, Ordering::Relaxed); | ||
helpers::OwnedEvent::new( | ||
efi::EVT_NOTIFY_SIGNAL, | ||
efi::TPL_CALLBACK, | ||
Some(toggle_atomic_flag), | ||
Some(unsafe { NonNull::new_unchecked(self.flag.as_ptr().cast()) }), | ||
) | ||
} | ||
|
||
fn wait_for_flag(&self) -> io::Result<()> { | ||
while !self.flag.load(Ordering::Relaxed) { | ||
self.poll()?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn poll(&self) -> io::Result<()> { | ||
let protocol = self.protocol.as_ptr(); | ||
let r = unsafe { ((*protocol).poll)(protocol) }; | ||
|
||
if r.is_error() { | ||
return Err(io::Error::from_raw_os_error(r.as_usize())); | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
extern "efiapi" fn toggle_atomic_flag(_: r_efi::efi::Event, ctx: *mut crate::ffi::c_void) { | ||
let flag = unsafe { AtomicBool::from_ptr(ctx.cast()) }; | ||
flag.store(true, Ordering::Relaxed); | ||
} |
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
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.
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.
That makes
wait_for_flag
unsound – there is no guarantee that the structure will not be moved in the time between the calls, which would invalidate this pointer.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.
Should I use Condvar then? Or is there a way to have Atomic on Heap or something?
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.
I'd remove the flag, make the notification function a no-op and use
CheckEvent
on the event.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.
The event is of type Signal. The CheckEvent doc states the following
The reason the event is of type Signal is because the tcp4 protocol connect doc states the following
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.
Ah, I missed that. Then the easiest way is probably just to ensure that the flag outlives the event. Why not just inline
wait_for_flag
and makeflag
a local variable? Alternatively, makecreate_evt
unsafe
.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.
I would prefer not to inline
wait_for_flag
since that will be repeated in almost all other implementations soon enough (read, write, close, etc). I have madecreate_evt
unsafe. Is that ok?