Skip to content

Commit f7e3c2a

Browse files
committed
library: std: sys: net: uefi: tcp: Implement write_vectored
- A working vectored write implementation for TCP4. - Also introduces a small helper UefiBox intended to be used with heap allocated UEFI DSTs. - Tested on OVMF Signed-off-by: Ayush Singh <[email protected]>
1 parent f13ef0d commit f7e3c2a

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

library/std/src/sys/net/connection/uefi/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@ impl TcpStream {
7777
}
7878

7979
pub fn write_vectored(&self, buf: &[IoSlice<'_>]) -> io::Result<usize> {
80-
// FIXME: UEFI does support vectored write, so implement that.
81-
crate::io::default_write_vectored(|b| self.write(b), buf)
80+
self.inner.write_vectored(buf, self.write_timeout()?)
8281
}
8382

8483
pub fn is_write_vectored(&self) -> bool {
85-
false
84+
true
8685
}
8786

8887
pub fn peer_addr(&self) -> io::Result<SocketAddr> {

library/std/src/sys/net/connection/uefi/tcp.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::tcp4;
2-
use crate::io;
2+
use crate::io::{self, IoSlice};
33
use crate::net::SocketAddr;
44
use crate::ptr::NonNull;
55
use crate::sys::{helpers, unsupported};
@@ -28,6 +28,16 @@ impl Tcp {
2828
}
2929
}
3030

31+
pub(crate) fn write_vectored(
32+
&self,
33+
buf: &[IoSlice<'_>],
34+
timeout: Option<Duration>,
35+
) -> io::Result<usize> {
36+
match self {
37+
Self::V4(client) => client.write_vectored(buf, timeout),
38+
}
39+
}
40+
3141
pub(crate) fn read(&self, buf: &mut [u8], timeout: Option<Duration>) -> io::Result<usize> {
3242
match self {
3343
Self::V4(client) => client.read(buf, timeout),

library/std/src/sys/net/connection/uefi/tcp4.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use r_efi::efi::{self, Status};
22
use r_efi::protocols::tcp4;
33

4-
use crate::io;
4+
use crate::io::{self, IoSlice};
55
use crate::net::SocketAddrV4;
66
use crate::ptr::NonNull;
77
use crate::sync::atomic::{AtomicBool, Ordering};
@@ -147,6 +147,62 @@ impl Tcp4 {
147147
}
148148
}
149149

150+
pub(crate) fn write_vectored(
151+
&self,
152+
buf: &[IoSlice<'_>],
153+
timeout: Option<Duration>,
154+
) -> io::Result<usize> {
155+
let evt = unsafe { self.create_evt() }?;
156+
let completion_token =
157+
tcp4::CompletionToken { event: evt.as_ptr(), status: Status::SUCCESS };
158+
let mut data_len = 0u32;
159+
let mut fragment_count = 0u32;
160+
161+
// Calculate how many IoSlice in buf can be transmitted.
162+
for i in buf {
163+
match data_len.checked_add(i.as_slice().len() as u32) {
164+
Some(x) => data_len = x,
165+
None => break,
166+
}
167+
fragment_count += 1;
168+
}
169+
170+
let tx_data_size = crate::mem::size_of::<tcp4::TransmitData<0>>()
171+
+ crate::mem::size_of::<tcp4::FragmentData>() * (fragment_count as usize);
172+
let mut tx_data = helpers::UefiBox::<tcp4::TransmitData>::new(tx_data_size);
173+
unsafe {
174+
(*tx_data.as_mut_ptr()).push = r_efi::efi::Boolean::FALSE;
175+
(*tx_data.as_mut_ptr()).urgent = r_efi::efi::Boolean::FALSE;
176+
(*tx_data.as_mut_ptr()).data_length = data_len;
177+
(*tx_data.as_mut_ptr()).fragment_count = fragment_count;
178+
// SAFETY: IoSlice and FragmentData are guaranteed to have same layout.
179+
crate::ptr::copy_nonoverlapping(
180+
buf.as_ptr().cast(),
181+
(*tx_data.as_mut_ptr()).fragment_table.as_mut_ptr(),
182+
fragment_count as usize,
183+
);
184+
};
185+
186+
let protocol = self.protocol.as_ptr();
187+
let mut token = tcp4::IoToken {
188+
completion_token,
189+
packet: tcp4::IoTokenPacket { tx_data: tx_data.as_mut_ptr() },
190+
};
191+
192+
let r = unsafe { ((*protocol).transmit)(protocol, &mut token) };
193+
if r.is_error() {
194+
return Err(io::Error::from_raw_os_error(r.as_usize()));
195+
}
196+
197+
unsafe { self.wait_or_cancel(timeout, &mut token.completion_token) }?;
198+
199+
if completion_token.status.is_error() {
200+
Err(io::Error::from_raw_os_error(completion_token.status.as_usize()))
201+
} else {
202+
Ok(data_len as usize)
203+
}
204+
}
205+
150206
pub(crate) fn read(&self, buf: &mut [u8], timeout: Option<Duration>) -> io::Result<usize> {
151207
let evt = unsafe { self.create_evt() }?;
152208
let completion_token =

library/std/src/sys/pal/uefi/helpers.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,19 @@ pub(crate) const fn ipv4_to_r_efi(addr: crate::net::Ipv4Addr) -> efi::Ipv4Addres
765765
pub(crate) const fn ipv4_from_r_efi(ip: efi::Ipv4Address) -> crate::net::Ipv4Addr {
766766
crate::net::Ipv4Addr::new(ip.addr[0], ip.addr[1], ip.addr[2], ip.addr[3])
767767
}
768+
769+
#[repr(transparent)]
770+
pub(crate) struct UefiBox<T> {
771+
inner: Box<[u8]>,
772+
phantom: PhantomData<T>,
773+
}
774+
775+
impl<T> UefiBox<T> {
776+
pub(crate) fn new(len: usize) -> Self {
777+
Self { inner: vec![0u8; len].into_boxed_slice(), phantom: PhantomData }
778+
}
779+
780+
pub(crate) fn as_mut_ptr(&mut self) -> *mut T {
781+
self.inner.as_mut_ptr().cast()
782+
}
783+
}

0 commit comments

Comments
 (0)