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

Add support for block IO with block sizes which are not known at compile-time #21

Merged
merged 5 commits into from
Oct 2, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use futures::task::LocalSpawnExt;

use mbedtls::ssl::async_io::ClosedError;

use sel4_async_block_io::{block_sizes, BlockIO};
use sel4_async_block_io::{constant_block_sizes, BlockIO};
use sel4_async_block_io_fat as fat;
use sel4_async_network::{SharedNetwork, TcpSocketError};
use sel4_async_network_mbedtls::{
Expand All @@ -29,14 +29,7 @@ use server::Server;
const HTTP_PORT: u16 = 80;
const HTTPS_PORT: u16 = 443;

type SocketUser<T> = Box<
dyn Fn(
Server<fat::BlockIOWrapper<T>, fat::DummyTimeSource>,
TcpSocketWrapper,
) -> LocalBoxFuture<'static, ()>,
>;

pub async fn run_server<T: BlockIO<BlockSize = block_sizes::BlockSize512> + Clone>(
pub async fn run_server<T: BlockIO<BlockSize = constant_block_sizes::BlockSize512> + Clone>(
_timers_ctx: SharedTimers,
network_ctx: SharedNetwork,
fs_block_io: T,
Expand Down Expand Up @@ -106,6 +99,13 @@ pub async fn run_server<T: BlockIO<BlockSize = block_sizes::BlockSize512> + Clon
future::pending().await
}

type SocketUser<T> = Box<
dyn Fn(
Server<fat::BlockIOWrapper<T>, fat::DummyTimeSource>,
TcpSocketWrapper,
) -> LocalBoxFuture<'static, ()>,
>;

async fn use_socket_for_http<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static>(
server: Server<D, T>,
mut socket: TcpSocketWrapper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use futures::future::LocalBoxFuture;
use smoltcp::iface::Config;
use smoltcp::time::{Duration, Instant};

use sel4_async_block_io::constant_block_sizes::BlockSize512;
use sel4_async_network::{DhcpOverrides, SharedNetwork};
use sel4_async_single_threaded_executor::{LocalPool, LocalSpawner};
use sel4_async_timers::SharedTimers;
Expand All @@ -20,7 +21,7 @@ pub(crate) struct HandlerImpl {
block_driver_channel: sel4_microkit::Channel,
timer: TimerClient,
net_device: DeviceImpl,
shared_block_io: SharedRingBufferBlockIO,
shared_block_io: SharedRingBufferBlockIO<BlockSize512>,
shared_timers: SharedTimers,
shared_network: SharedNetwork,
local_pool: LocalPool,
Expand All @@ -36,7 +37,7 @@ impl HandlerImpl {
timer: TimerClient,
mut net_device: DeviceImpl,
net_config: Config,
shared_block_io: SharedRingBufferBlockIO,
shared_block_io: SharedRingBufferBlockIO<BlockSize512>,
f: impl FnOnce(SharedTimers, SharedNetwork, LocalSpawner) -> T,
) -> Self {
let now = Self::now_with_timer_client(&timer);
Expand Down
14 changes: 8 additions & 6 deletions crates/examples/microkit/http-server/pds/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use smoltcp::iface::Config;
use smoltcp::phy::{Device, Medium};
use smoltcp::wire::{EthernetAddress, HardwareAddress};

use sel4_async_block_io::{disk::Disk, CachedBlockIO};
use sel4_async_block_io::{
constant_block_sizes::BlockSize512, disk::Disk, CachedBlockIO, ConstantBlockSize,
};
use sel4_externally_shared::ExternallySharedRef;
use sel4_logging::{LevelFilter, Logger, LoggerBuilder};
use sel4_microkit::{memory_region_symbol, protection_domain, var, Channel, Handler};
Expand Down Expand Up @@ -123,6 +125,8 @@ fn init() -> impl Handler {
let num_blocks = block_client.get_num_blocks();

let shared_block_io = SharedRingBufferBlockIO::new(
BlockSize512::SINGLETON,
num_blocks,
unsafe {
ExternallySharedRef::<'static, _>::new(
memory_region_symbol!(virtio_blk_client_dma_vaddr: *mut [u8], n = *var!(virtio_blk_client_dma_size: usize = 0)),
Expand All @@ -137,21 +141,19 @@ fn init() -> impl Handler {
true,
)
},
num_blocks,
);

let fs_block_io = shared_block_io.clone();
let fs_block_io = CachedBlockIO::new(fs_block_io.clone(), BLOCK_CACHE_SIZE_IN_BLOCKS);

HandlerImpl::new(
TIMER_DRIVER,
NET_DRIVER,
BLOCK_DRIVER,
timer_client,
net_device,
net_config,
shared_block_io,
shared_block_io.clone(),
|timers_ctx, network_ctx, spawner| async move {
let fs_block_io = shared_block_io.clone();
let fs_block_io = CachedBlockIO::new(fs_block_io.clone(), BLOCK_CACHE_SIZE_IN_BLOCKS);
let disk = Disk::new(fs_block_io);
let entry = disk.read_mbr().await.unwrap().partition(0).unwrap();
let fs_block_io = disk.partition_using_mbr(&entry);
Expand Down
2 changes: 1 addition & 1 deletion crates/sel4-async/block-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
license = "BSD-2-Clause"

[features]
alloc = ["lru"]
alloc = ["futures/alloc", "lru"]
default = ["alloc"]

[dependencies]
Expand Down
6 changes: 4 additions & 2 deletions crates/sel4-async/block-io/fat/src/block_io_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::future;

use sel4_async_block_io::{block_sizes, BlockIO};
use sel4_async_block_io::{constant_block_sizes, BlockIO};

pub use embedded_fat as fat;

Expand All @@ -14,7 +14,9 @@ impl<T> BlockIOWrapper<T> {
}
}

impl<T: BlockIO<BlockSize = block_sizes::BlockSize512>> fat::BlockDevice for BlockIOWrapper<T> {
impl<T: BlockIO<BlockSize = constant_block_sizes::BlockSize512>> fat::BlockDevice
for BlockIOWrapper<T>
{
type Error = !;

async fn read(
Expand Down
111 changes: 72 additions & 39 deletions crates/sel4-async/block-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
extern crate alloc;

use core::fmt;
use core::marker::PhantomData;
use core::ops::Range;

use futures::future;
Expand All @@ -21,50 +20,71 @@ pub mod disk;
mod when_alloc;

#[cfg(feature = "alloc")]
pub use when_alloc::CachedBlockIO;
pub use when_alloc::{CachedBlockIO, DynamicBlockSize};

pub trait BlockIO {
type Error: fmt::Debug;

type BlockSize: BlockSize;

fn block_size(&self) -> Self::BlockSize;

fn num_blocks(&self) -> u64;

async fn read_blocks(&self, start_block_idx: u64, buf: &mut [u8]) -> Result<(), Self::Error>;
}

pub trait BlockSize {
const BYTES: usize;

type Block: AsRef<[u8]> + AsMut<[u8]>;

fn zeroed_block() -> Self::Block;
fn bytes(&self) -> usize;

fn bytes_u64(&self) -> u64 {
self.bytes().try_into().unwrap()
}

fn zeroed_block(&self) -> Self::Block;
}

pub trait HasNextBlockSize: BlockSize {
type NextBlockSize: BlockSize;
pub trait ConstantBlockSize: BlockSize {
const SINGLETON: Self;

const BYTES: usize;
}

pub trait HasPrevBlockSize: BlockSize {
type PrevBlockSize: BlockSize;
pub trait HasNextBlockSize: ConstantBlockSize {
type NextBlockSize: ConstantBlockSize;
}

pub mod block_sizes {
use super::{BlockSize, HasNextBlockSize, HasPrevBlockSize};
pub trait HasPrevBlockSize: ConstantBlockSize {
type PrevBlockSize: ConstantBlockSize;
}

pub mod constant_block_sizes {
use super::{BlockSize, ConstantBlockSize, HasNextBlockSize, HasPrevBlockSize};

macro_rules! declare_block_size {
($ident:ident, $n:literal) => {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct $ident;

impl BlockSize for $ident {
const BYTES: usize = $n;

type Block = [u8; $n];

fn zeroed_block() -> Self::Block {
fn bytes(&self) -> usize {
Self::BYTES
}

fn zeroed_block(&self) -> Self::Block {
[0; $n]
}
}

impl ConstantBlockSize for $ident {
const SINGLETON: Self = $ident;

const BYTES: usize = $n;
}
};
}

Expand Down Expand Up @@ -128,6 +148,10 @@ impl<T: BlockIO<BlockSize: HasNextBlockSize>> BlockIO for NextBlockSizeAdapter<T

type BlockSize = <T::BlockSize as HasNextBlockSize>::NextBlockSize;

fn block_size(&self) -> Self::BlockSize {
Self::BlockSize::SINGLETON
}

fn num_blocks(&self) -> u64 {
let inner_num_blocks = self.inner().num_blocks();
assert_eq!(inner_num_blocks % 2, 0);
Expand Down Expand Up @@ -158,6 +182,10 @@ impl<T: BlockIO<BlockSize: HasPrevBlockSize>> BlockIO for PrevBlockSizeAdapter<T

type BlockSize = <T::BlockSize as HasPrevBlockSize>::PrevBlockSize;

fn block_size(&self) -> Self::BlockSize {
Self::BlockSize::SINGLETON
}

fn num_blocks(&self) -> u64 {
self.inner().num_blocks().checked_mul(2).unwrap()
}
Expand Down Expand Up @@ -190,13 +218,17 @@ impl<T: BlockIO> BlockIO for Partition<T> {

type BlockSize = T::BlockSize;

fn block_size(&self) -> Self::BlockSize {
self.inner().block_size()
}

fn num_blocks(&self) -> u64 {
self.range.end - self.range.start
}

async fn read_blocks(&self, start_block_idx: u64, buf: &mut [u8]) -> Result<(), Self::Error> {
assert!(
start_block_idx + u64::try_from(buf.len() / Self::BlockSize::BYTES).unwrap()
start_block_idx + u64::try_from(buf.len()).unwrap() / self.block_size().bytes_u64()
<= self.num_blocks()
);
let inner_block_idx = self.range.start + start_block_idx;
Expand Down Expand Up @@ -229,7 +261,7 @@ impl<T: BlockIO> ByteIO for ByteIOAdapter<T> {
type Error = T::Error;

fn size(&self) -> u64 {
self.inner().num_blocks() * u64::try_from(T::BlockSize::BYTES).unwrap()
self.inner().num_blocks() * self.inner().block_size().bytes_u64()
}

async fn read(&self, offset: u64, buf: &mut [u8]) -> Result<(), Self::Error> {
Expand All @@ -238,34 +270,36 @@ impl<T: BlockIO> ByteIO for ByteIOAdapter<T> {
}

#[derive(Clone, Debug)]
pub struct BlockIOAdapter<T, N: BlockSize> {
pub struct BlockIOAdapter<T, N> {
inner: T,
_phantom: PhantomData<N>,
block_size: N,
}

impl<T, N: BlockSize> BlockIOAdapter<T, N> {
pub fn new(inner: T) -> Self {
Self {
inner,
_phantom: PhantomData,
}
impl<T, N> BlockIOAdapter<T, N> {
pub fn new(inner: T, block_size: N) -> Self {
Self { inner, block_size }
}

wrapper_methods!(T);
}

impl<T: ByteIO, N: BlockSize> BlockIO for BlockIOAdapter<T, N> {
impl<T: ByteIO, N: BlockSize + Copy> BlockIO for BlockIOAdapter<T, N> {
type Error = T::Error;

type BlockSize = N;

fn block_size(&self) -> Self::BlockSize {
self.block_size
}

fn num_blocks(&self) -> u64 {
self.inner().size() / u64::try_from(Self::BlockSize::BYTES).unwrap()
self.inner().size() / self.block_size().bytes_u64()
}

async fn read_blocks(&self, start_block_idx: u64, buf: &mut [u8]) -> Result<(), Self::Error> {
let block_size = Self::BlockSize::BYTES.try_into().unwrap();
let start_byte_idx = start_block_idx.checked_mul(block_size).unwrap();
let start_byte_idx = start_block_idx
.checked_mul(self.block_size().bytes_u64())
.unwrap();
self.inner().read(start_byte_idx, buf).await
}
}
Expand Down Expand Up @@ -302,35 +336,34 @@ async fn read_partial_block<T: BlockIO>(
offset_into_block: usize,
buf: &mut [u8],
) -> Result<(), T::Error> {
assert!(offset_into_block + buf.len() <= T::BlockSize::BYTES);
let mut block_buf = T::BlockSize::zeroed_block();
assert!(offset_into_block + buf.len() <= io.block_size().bytes());
let mut block_buf = io.block_size().zeroed_block();
io.read_blocks(block_idx, block_buf.as_mut()).await?;
buf.copy_from_slice(&block_buf.as_ref()[offset_into_block..][..buf.len()]);
Ok(())
}

async fn read_bytes<T: BlockIO>(io: &T, offset: u64, buf: &mut [u8]) -> Result<(), T::Error> {
let block_size = T::BlockSize::BYTES.try_into().unwrap();
let byte_offset_of_first_full_block = offset.next_multiple_of(block_size);
let block_size = io.block_size().bytes();
let block_size_u64 = io.block_size().bytes_u64();
let byte_offset_of_first_full_block = offset.next_multiple_of(block_size_u64);
let byte_offset_of_first_full_block_in_buf =
usize::try_from(byte_offset_of_first_full_block - offset).unwrap();
let first_full_block_idx = byte_offset_of_first_full_block / block_size;
let num_full_blocks =
(buf.len() - byte_offset_of_first_full_block_in_buf) / T::BlockSize::BYTES;
let first_full_block_idx = byte_offset_of_first_full_block / block_size_u64;
let num_full_blocks = (buf.len() - byte_offset_of_first_full_block_in_buf) / block_size;
if byte_offset_of_first_full_block > offset + u64::try_from(buf.len()).unwrap() {
let block_idx = first_full_block_idx - 1;
let offset_into_block = offset - block_idx * block_size;
let offset_into_block = offset - block_idx * block_size_u64;
read_partial_block(io, block_idx, offset_into_block.try_into().unwrap(), buf).await?;
} else {
let (left_partial_block, rest) = buf.split_at_mut(byte_offset_of_first_full_block_in_buf);
let (full_blocks, right_partial_block) =
rest.split_at_mut(num_full_blocks * T::BlockSize::BYTES);
let (full_blocks, right_partial_block) = rest.split_at_mut(num_full_blocks * block_size);
future::try_join3(
async { io.read_blocks(first_full_block_idx, full_blocks).await },
async {
if !left_partial_block.is_empty() {
let block_idx = first_full_block_idx - 1;
let offset_into_block = T::BlockSize::BYTES - left_partial_block.len();
let offset_into_block = block_size - left_partial_block.len();
read_partial_block(io, block_idx, offset_into_block, left_partial_block)
.await?;
}
Expand Down
Loading