Skip to content

Commit

Permalink
impl deref for dma, now pass phys addr to read/write_raw, update init…
Browse files Browse the repository at this point in the history
… example
  • Loading branch information
bootreer committed Feb 28, 2024
1 parent 2cb6b35 commit f109ed3
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 81 deletions.
33 changes: 21 additions & 12 deletions examples/init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::env;
use std::process;
use vroom::QUEUE_LENGTH;
use vroom::HUGE_PAGE_SIZE;
use vroom::memory::Dma;

pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = env::args();
Expand All @@ -12,20 +15,21 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
process::exit(1);
}
};

let mut nvme = vroom::init(&pci_addr)?;
nvme.create_io_queue_pair(QUEUE_LENGTH)?;

// Testing stuff
let n = 10;
let n2 = 100_000;
let n2 = 1000;
let blocks = 8;
let mut buffer: Dma<[u8; HUGE_PAGE_SIZE]> = Dma::allocate(HUGE_PAGE_SIZE, true)?;

let mut read = std::time::Duration::new(0, 0);
let mut write = std::time::Duration::new(0, 0);
let mut read_buf = vec![0; blocks * 512];

// let mut write_batched = std::time::Duration::new(0, 0);
// let mut read_batched = std::time::Duration::new(0, 0);
// let mut read_bbuf = vec![0; blocks * 512];

let mut rng = rand::thread_rng();
use rand::seq::SliceRandom;

Expand All @@ -38,12 +42,13 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let rand_block = &(0..(512 * blocks))
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>()[..];
unsafe { (*nvme.buffer.virt)[..rand_block.len()].copy_from_slice(rand_block) };

buffer[..rand_block.len()].copy_from_slice(rand_block);

// write
let before = std::time::Instant::now();
nvme.write_raw(rand_block, lba + (*i * blocks as u64))?;
write += before.elapsed();
let before = std::time::Instant::now();
nvme.write_raw(rand_block, lba + (*i * blocks as u64), buffer.phys as u64)?;
write += before.elapsed();

// let before = Instant::now();
// nvme.batched_write(1, rand_block, lba, 256)?;
Expand All @@ -54,13 +59,17 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
// nvme.batched_read(1, &mut read_bbuf[..], lba, 256)?;
// read_batched += before.elapsed();

buffer[..rand_block.len()].fill_with(Default::default);
let before = std::time::Instant::now();
nvme.read(1, &mut read_buf[..], lba + (*i * blocks as u64))?;
nvme.read(
1,
&buffer[..rand_block.len()],
lba + (*i * blocks as u64),
buffer.phys as u64,
)?;
read += before.elapsed();

// assert_eq!(read_buf, rand_block);
// assert_eq!(read_buf, read_bbuf);

assert_eq!(&buffer[..rand_block.len()], rand_block);
// lba += blocks as u64;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
#[allow(unused)]
mod cmd;
#[allow(dead_code)]
mod memory;
pub mod memory;
mod nvme;
#[allow(dead_code)]
mod pci;
#[allow(dead_code)]
mod queues;

pub use queues::QUEUE_LENGTH;
pub use memory::HUGE_PAGE_SIZE;
use pci::*;
use queues::QUEUE_LENGTH;
use nvme::NvmeDevice;
use std::error::Error;

Expand Down Expand Up @@ -55,7 +56,6 @@ pub fn init(pci_addr: &str) -> Result<NvmeDevice, Box<dyn Error>> {

let mut nvme = NvmeDevice::init(pci_addr)?;
nvme.identify_controller()?;
nvme.create_io_queue_pair(QUEUE_LENGTH)?;
let ns = nvme.identify_namespace_list(0);
for n in ns {
println!("ns_id: {n}");
Expand Down
47 changes: 43 additions & 4 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use lazy_static::lazy_static;
use libc::{c_void, memset};
use libc::munmap;
use libc::c_void;
// use std::rc::Rc;
// use std::ptr::NonNull;
use std::cell::RefCell;
use std::collections::HashMap;
use std::error::Error;
use std::io::{self, Read, Seek};
use std::os::fd::{AsRawFd, RawFd};
use std::rc::Rc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use std::{fs, mem, process, ptr};
use std::ops::{Deref, DerefMut};

// from https://www.kernel.org/doc/Documentation/x86/x86_64/mm.txt
const X86_VA_WIDTH: u8 = 47;
Expand All @@ -28,14 +31,36 @@ lazy_static! {
}

pub struct Dma<T> {
// pub virt: NonNull<T>,
pub virt: *mut T,
pub phys: usize,
size: usize,
}

// should be safe
impl<T> Deref for Dma<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
unsafe {
&*self.virt
// self.virt.as_ref()
}
}
}

impl<T> DerefMut for Dma<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {
&mut *self.virt
// self.virt.as_mut()
}
}
}

impl<T> Dma<T> {
/// Allocates DMA Memory on a huge page
// TODO: vfio support?
#[allow(arithmetic_overflow)]
pub fn allocate(size: usize, require_contiguous: bool) -> Result<Dma<T>, Box<dyn Error>> {
let size = if size % HUGE_PAGE_SIZE != 0 {
((size >> HUGE_PAGE_BITS) + 1) << HUGE_PAGE_BITS
Expand Down Expand Up @@ -63,7 +88,7 @@ impl<T> Dma<T> {
size,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_SHARED | libc::MAP_HUGETLB,
// libc::MAP_SHARED, // cuz MAP_HUGETLB doesn't exist on macOS (for lsp lol)
// libc::MAP_SHARED,
f.as_raw_fd(),
0,
)
Expand All @@ -72,8 +97,10 @@ impl<T> Dma<T> {
Err("failed to mmap huge page - are huge pages enabled and free?".into())
} else if unsafe { libc::mlock(ptr, size) } == 0 {
let memory = Dma {
// virt: NonNull::new(ptr as *mut T).expect("oops"),
virt: ptr as *mut T,
phys: virt_to_phys(ptr as usize)?,
size
};
Ok(memory)
} else {
Expand All @@ -92,6 +119,16 @@ impl<T> Dma<T> {
}
}

// idk if required
impl<T> Drop for Dma<T> {
fn drop(&mut self) {
unsafe {
// munmap(self.virt.as_ptr() as *mut c_void, self.size);
munmap(self.virt as *mut c_void, self.size);
}
}
}

pub struct Mempool {
base_addr: *mut u8,
num_entries: usize,
Expand All @@ -100,6 +137,7 @@ pub struct Mempool {
pub(crate) free_stack: RefCell<Vec<usize>>,
}

/*
impl Mempool {
/// Allocates a new `Mempool`.
///
Expand Down Expand Up @@ -145,6 +183,7 @@ impl Mempool {
Ok(pool)
}
}
*/

/// Translates a virtual address to its physical counterpart
pub(crate) fn virt_to_phys(addr: usize) -> Result<usize, Box<dyn Error>> {
Expand Down
Loading

0 comments on commit f109ed3

Please sign in to comment.