Skip to content

Commit 9d1e4dd

Browse files
authored
Merge pull request #2046 from hermit-os/rm-virtio-newtypes
refactor(virtqueue): remove several newtypes
2 parents 449a08f + 867da85 commit 9d1e4dd

File tree

7 files changed

+94
-187
lines changed

7 files changed

+94
-187
lines changed

src/drivers/console/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
3030
use crate::drivers::virtio::transport::pci::{ComCfg, IsrStatus, NotifCfg};
3131
use crate::drivers::virtio::virtqueue::split::SplitVq;
3232
use crate::drivers::virtio::virtqueue::{
33-
AvailBufferToken, BufferElem, BufferType, UsedBufferToken, VirtQueue, Virtq, VqIndex, VqSize,
33+
AvailBufferToken, BufferElem, BufferType, UsedBufferToken, VirtQueue, Virtq,
3434
};
3535
use crate::drivers::{Driver, InterruptLine};
3636
use crate::errno::Errno;
@@ -124,7 +124,7 @@ impl RxQueue {
124124

125125
pub fn add(&mut self, mut vq: VirtQueue) {
126126
const BUFF_PER_PACKET: u16 = 2;
127-
let num_packets: u16 = u16::from(vq.size()) / BUFF_PER_PACKET;
127+
let num_packets = vq.size() / BUFF_PER_PACKET;
128128
fill_queue(&mut vq, num_packets, self.packet_size);
129129

130130
self.vq = Some(vq);
@@ -348,8 +348,8 @@ impl VirtioConsoleDriver {
348348
SplitVq::new(
349349
&mut self.com_cfg,
350350
&self.notif_cfg,
351-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
352-
VqIndex::from(0u16),
351+
VIRTIO_MAX_QUEUE_SIZE,
352+
0,
353353
self.dev_cfg.features.into(),
354354
)
355355
.unwrap(),
@@ -361,8 +361,8 @@ impl VirtioConsoleDriver {
361361
SplitVq::new(
362362
&mut self.com_cfg,
363363
&self.notif_cfg,
364-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
365-
VqIndex::from(1u16),
364+
VIRTIO_MAX_QUEUE_SIZE,
365+
1,
366366
self.dev_cfg.features.into(),
367367
)
368368
.unwrap(),

src/drivers/fs/virtio_fs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::drivers::virtio::transport::pci::{ComCfg, IsrStatus, NotifCfg};
2323
use crate::drivers::virtio::virtqueue::error::VirtqError;
2424
use crate::drivers::virtio::virtqueue::split::SplitVq;
2525
use crate::drivers::virtio::virtqueue::{
26-
AvailBufferToken, BufferElem, BufferType, VirtQueue, Virtq, VqIndex, VqSize,
26+
AvailBufferToken, BufferElem, BufferType, VirtQueue, Virtq,
2727
};
2828
use crate::errno::Errno;
2929
use crate::fs::fuse::{self, FuseError, FuseInterface, Rsp, RspHeader};
@@ -142,8 +142,8 @@ impl VirtioFsDriver {
142142
SplitVq::new(
143143
&mut self.com_cfg,
144144
&self.notif_cfg,
145-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
146-
VqIndex::from(i),
145+
VIRTIO_MAX_QUEUE_SIZE,
146+
i,
147147
self.dev_cfg.features.into(),
148148
)
149149
.unwrap(),

src/drivers/net/virtio/mod.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::drivers::virtio::transport::pci::{ComCfg, IsrStatus, NotifCfg};
3535
use crate::drivers::virtio::virtqueue::packed::PackedVq;
3636
use crate::drivers::virtio::virtqueue::split::SplitVq;
3737
use crate::drivers::virtio::virtqueue::{
38-
AvailBufferToken, BufferElem, BufferType, UsedBufferToken, VirtQueue, Virtq, VqIndex, VqSize,
38+
AvailBufferToken, BufferElem, BufferType, UsedBufferToken, VirtQueue, Virtq,
3939
};
4040
use crate::drivers::{Driver, InterruptLine};
4141
use crate::mm::device_alloc::DeviceAlloc;
@@ -110,7 +110,7 @@ impl RxQueues {
110110
///
111111
/// Queues are all populated according to Virtio specification v1.1. - 5.1.6.3.1
112112
fn add(&mut self, mut vq: VirtQueue) {
113-
let num_bufs: u16 = u16::from(vq.size()) / constants::BUFF_PER_PACKET;
113+
let num_bufs = vq.size() / constants::BUFF_PER_PACKET;
114114
fill_queue(&mut vq, num_bufs, self.buf_size);
115115
self.vqs.push(vq);
116116
}
@@ -836,8 +836,8 @@ impl VirtioNetDriver<Uninit> {
836836
PackedVq::new(
837837
&mut self.com_cfg,
838838
&self.notif_cfg,
839-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
840-
VqIndex::from(self.num_vqs),
839+
VIRTIO_MAX_QUEUE_SIZE,
840+
self.num_vqs,
841841
self.dev_cfg.features.into(),
842842
)
843843
.unwrap(),
@@ -847,8 +847,8 @@ impl VirtioNetDriver<Uninit> {
847847
SplitVq::new(
848848
&mut self.com_cfg,
849849
&self.notif_cfg,
850-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
851-
VqIndex::from(self.num_vqs),
850+
VIRTIO_MAX_QUEUE_SIZE,
851+
self.num_vqs,
852852
self.dev_cfg.features.into(),
853853
)
854854
.unwrap(),
@@ -896,7 +896,7 @@ impl VirtioNetDriver<Uninit> {
896896
self.num_vqs = 2;
897897
}
898898

899-
// The loop is running from 0 to num_vqs and the indexes are provided to the VqIndex::from function in this way
899+
// The loop is running from 0 to num_vqs and the indexes are provided in this way
900900
// in order to allow the indexes of the queues to be in a form of:
901901
//
902902
// index i for receive queue
@@ -912,8 +912,8 @@ impl VirtioNetDriver<Uninit> {
912912
let mut vq = PackedVq::new(
913913
&mut self.com_cfg,
914914
&self.notif_cfg,
915-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
916-
VqIndex::from(2 * i),
915+
VIRTIO_MAX_QUEUE_SIZE,
916+
2 * i,
917917
self.dev_cfg.features.into(),
918918
)
919919
.unwrap();
@@ -925,22 +925,22 @@ impl VirtioNetDriver<Uninit> {
925925
let mut vq = PackedVq::new(
926926
&mut self.com_cfg,
927927
&self.notif_cfg,
928-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
929-
VqIndex::from(2 * i + 1),
928+
VIRTIO_MAX_QUEUE_SIZE,
929+
2 * i + 1,
930930
self.dev_cfg.features.into(),
931931
)
932932
.unwrap();
933933
// Interrupt for communicating that a sent packet left, is not needed
934934
vq.disable_notifs();
935935

936-
inner.send_capacity += u32::from(u16::from(vq.size()));
936+
inner.send_capacity += u32::from(vq.size());
937937
inner.send_vqs.add(VirtQueue::Packed(vq));
938938
} else {
939939
let mut vq = SplitVq::new(
940940
&mut self.com_cfg,
941941
&self.notif_cfg,
942-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
943-
VqIndex::from(2 * i),
942+
VIRTIO_MAX_QUEUE_SIZE,
943+
2 * i,
944944
self.dev_cfg.features.into(),
945945
)
946946
.unwrap();
@@ -952,14 +952,14 @@ impl VirtioNetDriver<Uninit> {
952952
let mut vq = SplitVq::new(
953953
&mut self.com_cfg,
954954
&self.notif_cfg,
955-
VqSize::from(VIRTIO_MAX_QUEUE_SIZE),
956-
VqIndex::from(2 * i + 1),
955+
VIRTIO_MAX_QUEUE_SIZE,
956+
2 * i + 1,
957957
self.dev_cfg.features.into(),
958958
)
959959
.unwrap();
960960
// Interrupt for communicating that a sent packet left, is not needed
961961
vq.disable_notifs();
962-
inner.send_capacity += u32::from(u16::from(vq.size()));
962+
inner.send_capacity += u32::from(vq.size());
963963
inner.send_vqs.add(VirtQueue::Split(vq));
964964
}
965965
}

src/drivers/virtio/virtqueue/mod.rs

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -27,64 +27,6 @@ use crate::drivers::virtio::virtqueue::packed::PackedVq;
2727
use crate::drivers::virtio::virtqueue::split::SplitVq;
2828
use crate::mm::device_alloc::DeviceAlloc;
2929

30-
/// A u16 newtype. If instantiated via ``VqIndex::from(T)``, the newtype is ensured to be
31-
/// smaller-equal to `min(u16::MAX , T::MAX)`.
32-
///
33-
/// Currently implements `From<u16>` and `From<u32>`.
34-
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Eq)]
35-
pub struct VqIndex(u16);
36-
37-
impl From<u16> for VqIndex {
38-
fn from(val: u16) -> Self {
39-
VqIndex(val)
40-
}
41-
}
42-
43-
impl From<VqIndex> for u16 {
44-
fn from(i: VqIndex) -> Self {
45-
i.0
46-
}
47-
}
48-
49-
impl From<u32> for VqIndex {
50-
fn from(val: u32) -> Self {
51-
if val > u32::from(u16::MAX) {
52-
VqIndex(u16::MAX)
53-
} else {
54-
VqIndex(val as u16)
55-
}
56-
}
57-
}
58-
59-
/// A u16 newtype. If instantiated via ``VqSize::from(T)``, the newtype is ensured to be
60-
/// smaller-equal to `min(u16::MAX , T::MAX)`.
61-
///
62-
/// Currently implements `From<u16>` and `From<u32>`.
63-
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Eq)]
64-
pub struct VqSize(u16);
65-
66-
impl From<u16> for VqSize {
67-
fn from(val: u16) -> Self {
68-
VqSize(val)
69-
}
70-
}
71-
72-
impl From<u32> for VqSize {
73-
fn from(val: u32) -> Self {
74-
if val > u32::from(u16::MAX) {
75-
VqSize(u16::MAX)
76-
} else {
77-
VqSize(val as u16)
78-
}
79-
}
80-
}
81-
82-
impl From<VqSize> for u16 {
83-
fn from(val: VqSize) -> Self {
84-
val.0
85-
}
86-
}
87-
8830
// Public interface of Virtq
8931

9032
/// The Virtq trait unifies access to the two different Virtqueue types
@@ -185,10 +127,10 @@ pub trait Virtq: Send {
185127

186128
/// Returns the size of a Virtqueue. This represents the overall size and not the capacity the
187129
/// queue currently has for new descriptors.
188-
fn size(&self) -> VqSize;
130+
fn size(&self) -> u16;
189131

190132
// Returns the index (ID) of a Virtqueue.
191-
fn index(&self) -> VqIndex;
133+
fn index(&self) -> u16;
192134

193135
fn has_used_buffers(&self) -> bool;
194136
}
@@ -570,30 +512,26 @@ pub enum BufferType {
570512
Indirect,
571513
}
572514

573-
/// A newtype for descriptor ids, for better readability.
574-
#[derive(Clone, Copy)]
575-
struct MemDescrId(pub u16);
576-
577515
/// MemPool allows to easily control, request and provide memory for Virtqueues.
578516
///
579517
/// The struct is initialized with a limit of free running "tracked"
580518
/// memory descriptor ids. As Virtqueus do only allow a limited amount of descriptors in their queue,
581519
/// the independent queues, can control the number of descriptors by this.
582520
struct MemPool {
583-
pool: Vec<MemDescrId>,
521+
pool: Vec<u16>,
584522
limit: u16,
585523
}
586524

587525
impl MemPool {
588526
/// Returns a given id to the id pool
589-
fn ret_id(&mut self, id: MemDescrId) {
527+
fn ret_id(&mut self, id: u16) {
590528
self.pool.push(id);
591529
}
592530

593531
/// Returns a new instance, with a pool of the specified size.
594532
fn new(size: u16) -> MemPool {
595533
MemPool {
596-
pool: (0..size).map(MemDescrId).collect(),
534+
pool: (0..size).collect(),
597535
limit: size,
598536
}
599537
}

0 commit comments

Comments
 (0)