Skip to content

Commit

Permalink
virtq: remove TransferState
Browse files Browse the repository at this point in the history
The TransferState was needed to check the status of the Transfer when it was shared between the caller and the callee. However, this is no longer the case and the caller cannot access a in-progress transfer anymore. Thus, the related checks are no longer needed and the field is no longer necessary.
  • Loading branch information
cagatay-y committed May 13, 2024
1 parent 91afbfd commit da01658
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 42 deletions.
29 changes: 1 addition & 28 deletions src/drivers/virtio/virtqueue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ pub trait Virtq: VirtqPrivate {
///
/// * `TransferTokens` which hold an `await_queue` will be placed into
/// these queues.
/// * All finished `TransferTokens` will have a state of `TransferState::Finished`.
fn poll(&self);

/// Dispatches a batch of transfer token. The buffers of the respective transfers are provided to the queue in
Expand Down Expand Up @@ -280,7 +279,6 @@ pub trait Virtq: VirtqPrivate {
};

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: Some(Buffer::Single {
desc_lst: vec![desc].into_boxed_slice(),
Expand Down Expand Up @@ -320,7 +318,6 @@ pub trait Virtq: VirtqPrivate {
}

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: Some(Buffer::Multiple {
desc_lst: desc_lst.into_boxed_slice(),
Expand Down Expand Up @@ -364,7 +361,6 @@ pub trait Virtq: VirtqPrivate {
};

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: Some(Buffer::Indirect {
desc_lst: desc_lst.into_boxed_slice(),
Expand Down Expand Up @@ -400,7 +396,6 @@ pub trait Virtq: VirtqPrivate {
};

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: None,
recv_buff: Some(Buffer::Single {
Expand Down Expand Up @@ -440,7 +435,6 @@ pub trait Virtq: VirtqPrivate {
}

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: None,
recv_buff: Some(Buffer::Multiple {
Expand Down Expand Up @@ -484,7 +478,6 @@ pub trait Virtq: VirtqPrivate {
};

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: None,
recv_buff: Some(Buffer::Indirect {
Expand Down Expand Up @@ -533,7 +526,6 @@ pub trait Virtq: VirtqPrivate {
};

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: Some(Buffer::Single {
desc_lst: vec![send_desc].into_boxed_slice(),
Expand Down Expand Up @@ -591,7 +583,6 @@ pub trait Virtq: VirtqPrivate {
}

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: Some(Buffer::Single {
desc_lst: vec![send_desc].into_boxed_slice(),
Expand Down Expand Up @@ -659,7 +650,6 @@ pub trait Virtq: VirtqPrivate {
}

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: Some(Buffer::Multiple {
desc_lst: send_desc_lst.into_boxed_slice(),
Expand Down Expand Up @@ -717,7 +707,6 @@ pub trait Virtq: VirtqPrivate {
};

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
send_buff: Some(Buffer::Multiple {
desc_lst: send_desc_lst.into_boxed_slice(),
Expand Down Expand Up @@ -790,7 +779,6 @@ pub trait Virtq: VirtqPrivate {
};

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(BufferToken {
recv_buff: Some(Buffer::Indirect {
desc_lst: recv_desc_lst.into_boxed_slice(),
Expand Down Expand Up @@ -1478,21 +1466,9 @@ pub trait AsSliceU8 {
}
}

/// Enum indicates the current state of a transfer.
#[derive(PartialEq, Copy, Clone, Debug)]
enum TransferState {
/// Queue finished transfer
Finished,
/// Transfer is ongoing and still processed by queue
Processing,
/// Transfer is ready to be sended
Ready,
}

/// The struct represents buffers which are ready to be send via the
/// virtqueue. Buffers can no longer be written or retrieved.
pub struct TransferToken {
state: TransferState,
/// Must be some in order to prevent drop
/// upon reuse.
buff_tkn: Option<BufferToken>,
Expand Down Expand Up @@ -1538,8 +1514,7 @@ impl TransferToken {
/// Dispatches the provided TransferToken to the respectuve queue and does
/// return when, the queue finished the transfer.
///
/// The resultaing [TransferState] in this case is of course
/// finished and the returned [BufferToken] can be reused, copied from
/// The returned [BufferToken] can be reused, copied from
/// or return the underlying buffers.
///
/// **INFO:**
Expand Down Expand Up @@ -2223,7 +2198,6 @@ impl BufferToken {
}

Ok(TransferToken {
state: TransferState::Ready,
buff_tkn: Some(self),
await_queue: None,
})
Expand Down Expand Up @@ -2286,7 +2260,6 @@ impl BufferToken {
/// After this call, the buffers are no longer writable.
pub fn provide(self) -> TransferToken {
TransferToken {
state: TransferState::Ready,
buff_tkn: Some(self),
await_queue: None,
}
Expand Down
15 changes: 4 additions & 11 deletions src/drivers/virtio/virtqueue/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use super::super::transport::mmio::{ComCfg, NotifCfg, NotifCtrl};
use super::super::transport::pci::{ComCfg, NotifCfg, NotifCtrl};
use super::error::VirtqError;
use super::{
BuffSpec, Buffer, BufferToken, Bytes, DescrFlags, MemDescr, MemPool, TransferState,
TransferToken, Virtq, VirtqPrivate, VqIndex, VqSize,
BuffSpec, Buffer, BufferToken, Bytes, DescrFlags, MemDescr, MemPool, TransferToken, Virtq,
VirtqPrivate, VqIndex, VqSize,
};
use crate::arch::mm::paging::{BasePageSize, PageSize};
use crate::arch::mm::{paging, VirtAddr};
Expand Down Expand Up @@ -136,7 +136,6 @@ impl DescriptorRing {
let mut ctrl = self.get_read_ctrler();

if let Some(mut tkn) = ctrl.poll_next() {
tkn.state = TransferState::Finished;
if let Some(queue) = tkn.await_queue.take() {
// Place the TransferToken in a Transfer, which will hold ownership of the token
queue
Expand All @@ -154,7 +153,7 @@ impl DescriptorRing {
let mut first_ctrl_settings: (usize, u16, WrapCount) = (0, 0, WrapCount::new());
let mut first_buffer = None;

for (i, mut tkn) in tkn_lst.into_iter().enumerate() {
for (i, tkn) in tkn_lst.into_iter().enumerate() {
// Check length and if its fits. This should always be true due to the restriction of
// the memory pool, but to be sure.
assert!(tkn.buff_tkn.as_ref().unwrap().num_consuming_descr() <= self.capacity);
Expand Down Expand Up @@ -264,8 +263,6 @@ impl DescriptorRing {
(None, None) => unreachable!("Empty Transfers are not allowed!"), // This should already be caught at creation of BufferToken
}

tkn.state = TransferState::Processing;

if i == 0 {
first_ctrl_settings = (ctrl.start, ctrl.buff_id, ctrl.wrap_at_init);
first_buffer = Some(Box::new(tkn));
Expand All @@ -288,7 +285,7 @@ impl DescriptorRing {
(first_ctrl_settings.0, first_ctrl_settings.2 .0 as u8)
}

fn push(&mut self, mut tkn: TransferToken) -> (usize, u8) {
fn push(&mut self, tkn: TransferToken) -> (usize, u8) {
// Check length and if its fits. This should always be true due to the restriction of
// the memory pool, but to be sure.
assert!(tkn.buff_tkn.as_ref().unwrap().num_consuming_descr() <= self.capacity);
Expand Down Expand Up @@ -391,10 +388,6 @@ impl DescriptorRing {
(None, None) => unreachable!("Empty Transfers are not allowed!"), // This should already be caught at creation of BufferToken
}

fence(Ordering::SeqCst);
// Update the state of the actual Token
tkn.state = TransferState::Processing;

fence(Ordering::SeqCst);
// Update flags of the first descriptor and set new write_index
ctrl.make_avail(Box::new(tkn));
Expand Down
5 changes: 2 additions & 3 deletions src/drivers/virtio/virtqueue/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use super::super::transport::mmio::{ComCfg, NotifCfg, NotifCtrl};
use super::super::transport::pci::{ComCfg, NotifCfg, NotifCtrl};
use super::error::VirtqError;
use super::{
BuffSpec, BufferToken, Bytes, DescrFlags, MemDescr, MemPool, TransferState, TransferToken,
Virtq, VirtqPrivate, VqIndex, VqSize,
BuffSpec, BufferToken, Bytes, DescrFlags, MemDescr, MemPool, TransferToken, Virtq,
VirtqPrivate, VqIndex, VqSize,
};
use crate::arch::memory_barrier;
use crate::arch::mm::{paging, VirtAddr};
Expand Down Expand Up @@ -304,7 +304,6 @@ impl DescrRing {
.restr_size(None, Some(used_elem.len.get() as usize))
.unwrap();
}
tkn.state = TransferState::Finished;
if let Some(queue) = tkn.await_queue.take() {
queue
.borrow_mut()
Expand Down

0 comments on commit da01658

Please sign in to comment.