mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-21 08:26:30 +00:00
Reuse Rxbuffer and update qemu_args.sh
for vsock
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
878e8a88f4
commit
9f67fcdc58
@ -17,14 +17,19 @@ use crate::dma_pool::{DmaPool, DmaSegment};
|
||||
pub struct TxBuffer {
|
||||
dma_stream: DmaStream,
|
||||
nbytes: usize,
|
||||
pool: &'static SpinLock<LinkedList<DmaStream>>,
|
||||
}
|
||||
|
||||
impl TxBuffer {
|
||||
pub fn new<H: Pod>(header: &H, packet: &[u8]) -> Self {
|
||||
pub fn new<H: Pod>(
|
||||
header: &H,
|
||||
packet: &[u8],
|
||||
pool: &'static SpinLock<LinkedList<DmaStream>>,
|
||||
) -> Self {
|
||||
let header = header.as_bytes();
|
||||
let nbytes = header.len() + packet.len();
|
||||
|
||||
let dma_stream = if let Some(stream) = get_tx_stream_from_pool(nbytes) {
|
||||
let dma_stream = if let Some(stream) = get_tx_stream_from_pool(nbytes, pool) {
|
||||
stream
|
||||
} else {
|
||||
let segment = {
|
||||
@ -38,7 +43,11 @@ impl TxBuffer {
|
||||
writer.write(&mut VmReader::from(header));
|
||||
writer.write(&mut VmReader::from(packet));
|
||||
|
||||
let tx_buffer = Self { dma_stream, nbytes };
|
||||
let tx_buffer = Self {
|
||||
dma_stream,
|
||||
nbytes,
|
||||
pool,
|
||||
};
|
||||
tx_buffer.sync();
|
||||
tx_buffer
|
||||
}
|
||||
@ -64,9 +73,7 @@ impl HasDaddr for TxBuffer {
|
||||
|
||||
impl Drop for TxBuffer {
|
||||
fn drop(&mut self) {
|
||||
TX_BUFFER_POOL
|
||||
.get()
|
||||
.unwrap()
|
||||
self.pool
|
||||
.lock_irq_disabled()
|
||||
.push_back(self.dma_stream.clone());
|
||||
}
|
||||
@ -79,9 +86,9 @@ pub struct RxBuffer {
|
||||
}
|
||||
|
||||
impl RxBuffer {
|
||||
pub fn new(header_len: usize) -> Self {
|
||||
assert!(header_len <= RX_BUFFER_LEN);
|
||||
let segment = RX_BUFFER_POOL.get().unwrap().alloc_segment().unwrap();
|
||||
pub fn new(header_len: usize, pool: &Arc<DmaPool>) -> Self {
|
||||
assert!(header_len <= pool.segment_size());
|
||||
let segment = pool.alloc_segment().unwrap();
|
||||
Self {
|
||||
segment,
|
||||
header_len,
|
||||
@ -109,6 +116,16 @@ impl RxBuffer {
|
||||
.limit(self.packet_len)
|
||||
}
|
||||
|
||||
pub fn buf(&self) -> VmReader<'_> {
|
||||
self.segment
|
||||
.sync(0..self.header_len + self.packet_len)
|
||||
.unwrap();
|
||||
self.segment
|
||||
.reader()
|
||||
.unwrap()
|
||||
.limit(self.header_len + self.packet_len)
|
||||
}
|
||||
|
||||
pub const fn buf_len(&self) -> usize {
|
||||
self.segment.size()
|
||||
}
|
||||
@ -121,11 +138,14 @@ impl HasDaddr for RxBuffer {
|
||||
}
|
||||
|
||||
const RX_BUFFER_LEN: usize = 4096;
|
||||
static RX_BUFFER_POOL: Once<Arc<DmaPool>> = Once::new();
|
||||
static TX_BUFFER_POOL: Once<SpinLock<LinkedList<DmaStream>>> = Once::new();
|
||||
pub static RX_BUFFER_POOL: Once<Arc<DmaPool>> = Once::new();
|
||||
pub static TX_BUFFER_POOL: Once<SpinLock<LinkedList<DmaStream>>> = Once::new();
|
||||
|
||||
fn get_tx_stream_from_pool(nbytes: usize) -> Option<DmaStream> {
|
||||
let mut pool = TX_BUFFER_POOL.get().unwrap().lock_irq_disabled();
|
||||
fn get_tx_stream_from_pool(
|
||||
nbytes: usize,
|
||||
tx_buffer_pool: &'static SpinLock<LinkedList<DmaStream>>,
|
||||
) -> Option<DmaStream> {
|
||||
let mut pool = tx_buffer_pool.lock_irq_disabled();
|
||||
let mut cursor = pool.cursor_front_mut();
|
||||
while let Some(current) = cursor.current() {
|
||||
if current.nbytes() >= nbytes {
|
||||
|
@ -127,6 +127,11 @@ impl DmaPool {
|
||||
fn num_pages(&self) -> usize {
|
||||
self.all_pages.lock_irq_disabled().len()
|
||||
}
|
||||
|
||||
/// Return segment size in pool
|
||||
pub fn segment_size(&self) -> usize {
|
||||
self.segment_size
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -17,7 +17,7 @@ use core::{any::Any, fmt::Debug};
|
||||
|
||||
use aster_frame::sync::SpinLock;
|
||||
use aster_util::safe_ptr::Pod;
|
||||
pub use buffer::{RxBuffer, TxBuffer};
|
||||
pub use buffer::{RxBuffer, TxBuffer, RX_BUFFER_POOL, TX_BUFFER_POOL};
|
||||
use component::{init_component, ComponentInitError};
|
||||
pub use dma_pool::DmaSegment;
|
||||
use smoltcp::phy;
|
||||
|
Reference in New Issue
Block a user