Skip to content

Commit c2e80c1

Browse files
committed
refractor: remove unneeded GuestMemoryMmap parameters
A VsockPacket now holds references into guest memory (via pointed inside of the iovec abstraction), so we no longer need to carry around a tuple of (VsockPacket, GuestMemoryMmap) to be able to read or write vsock packets. Therefore, a lot of boilerplate code for passing guest memory references can now go. Signed-off-by: Patrick Roy <[email protected]>
1 parent 1e680c6 commit c2e80c1

File tree

6 files changed

+21
-40
lines changed

6 files changed

+21
-40
lines changed

src/vmm/src/devices/virtio/vsock/csm/connection.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ use super::txbuf::TxBuf;
9595
use super::{defs, ConnState, PendingRx, PendingRxSet, VsockCsmError};
9696
use crate::devices::virtio::vsock::metrics::METRICS;
9797
use crate::logger::IncMetric;
98-
use crate::vstate::memory::GuestMemoryMmap;
9998

10099
/// Trait that vsock connection backends need to implement.
101100
///
@@ -160,11 +159,7 @@ where
160159
/// - `Err(VsockError::NoData)`: there was no data available with which to fill in the packet;
161160
/// - `Err(VsockError::PktBufMissing)`: the packet would've been filled in with data, but it is
162161
/// missing the data buffer.
163-
fn recv_pkt(
164-
&mut self,
165-
pkt: &mut VsockPacket,
166-
_mem: &GuestMemoryMmap,
167-
) -> Result<(), VsockError> {
162+
fn recv_pkt(&mut self, pkt: &mut VsockPacket) -> Result<(), VsockError> {
168163
// Perform some generic initialization that is the same for any packet operation (e.g.
169164
// source, destination, credit, etc).
170165
self.init_pkt(pkt);
@@ -296,7 +291,7 @@ where
296291
///
297292
/// Returns:
298293
/// always `Ok(())`: the packet has been consumed;
299-
fn send_pkt(&mut self, pkt: &VsockPacket, mem: &GuestMemoryMmap) -> Result<(), VsockError> {
294+
fn send_pkt(&mut self, pkt: &VsockPacket) -> Result<(), VsockError> {
300295
// Update the peer credit information.
301296
self.peer_buf_alloc = pkt.buf_alloc();
302297
self.peer_fwd_cnt = Wrapping(pkt.fwd_cnt());
@@ -318,7 +313,7 @@ where
318313
}
319314

320315
// Unwrapping here is safe, since we just checked `pkt.buf()` above.
321-
if let Err(err) = self.send_bytes(mem, pkt) {
316+
if let Err(err) = self.send_bytes(pkt) {
322317
// If we can't write to the host stream, that's an unrecoverable error, so
323318
// we'll terminate this connection.
324319
warn!(
@@ -608,7 +603,7 @@ where
608603
///
609604
/// Raw data can either be sent straight to the host stream, or to our TX buffer, if the
610605
/// former fails.
611-
fn send_bytes(&mut self, _mem: &GuestMemoryMmap, pkt: &VsockPacket) -> Result<(), VsockError> {
606+
fn send_bytes(&mut self, pkt: &VsockPacket) -> Result<(), VsockError> {
612607
let len = pkt.len() as usize;
613608

614609
// If there is data in the TX buffer, that means we're already registered for EPOLLOUT
@@ -899,7 +894,7 @@ mod tests {
899894
PEER_BUF_ALLOC,
900895
);
901896
assert!(conn.has_pending_rx());
902-
conn.recv_pkt(&mut rx_pkt, &vsock_test_ctx.mem).unwrap();
897+
conn.recv_pkt(&mut rx_pkt).unwrap();
903898
assert_eq!(rx_pkt.op(), uapi::VSOCK_OP_RESPONSE);
904899
conn
905900
}
@@ -926,15 +921,11 @@ mod tests {
926921
}
927922

928923
fn send(&mut self) {
929-
self.conn
930-
.send_pkt(&self.tx_pkt, &self._vsock_test_ctx.mem)
931-
.unwrap();
924+
self.conn.send_pkt(&self.tx_pkt).unwrap();
932925
}
933926

934927
fn recv(&mut self) {
935-
self.conn
936-
.recv_pkt(&mut self.rx_pkt, &self._vsock_test_ctx.mem)
937-
.unwrap();
928+
self.conn.recv_pkt(&mut self.rx_pkt).unwrap();
938929
}
939930

940931
fn notify_epollin(&mut self) {
@@ -1028,7 +1019,7 @@ mod tests {
10281019
assert_eq!(&buf, data);
10291020

10301021
// There's no more data in the stream, so `recv_pkt` should yield `VsockError::NoData`.
1031-
match ctx.conn.recv_pkt(&mut ctx.tx_pkt, &ctx._vsock_test_ctx.mem) {
1022+
match ctx.conn.recv_pkt(&mut ctx.tx_pkt) {
10321023
Err(VsockError::NoData) => (),
10331024
other => panic!("{:?}", other),
10341025
}

src/vmm/src/devices/virtio/vsock/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ where
150150
let index = head.index;
151151
let used_len = match VsockPacket::from_rx_virtq_head(head) {
152152
Ok(mut pkt) => {
153-
if self.backend.recv_pkt(&mut pkt, mem).is_ok() {
153+
if self.backend.recv_pkt(&mut pkt).is_ok() {
154154
match pkt.commit_hdr() {
155155
// This addition cannot overflow, because packet length
156156
// is previously validated against `MAX_PKT_BUF_SIZE`
@@ -215,7 +215,7 @@ where
215215
}
216216
};
217217

218-
if self.backend.send_pkt(&pkt, mem).is_err() {
218+
if self.backend.send_pkt(&pkt).is_err() {
219219
self.queues[TXQ_INDEX].undo_pop();
220220
break;
221221
}

src/vmm/src/devices/virtio/vsock/event_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ mod tests {
205205
use super::*;
206206
use crate::devices::virtio::vsock::packet::VSOCK_PKT_HDR_SIZE;
207207
use crate::devices::virtio::vsock::test_utils::{EventHandlerContext, TestContext};
208-
use crate::vstate::memory::{Bytes, GuestMemoryExtension};
208+
use crate::vstate::memory::{Bytes, GuestMemoryExtension, GuestMemoryMmap};
209209

210210
#[test]
211211
fn test_txq_event() {

src/vmm/src/devices/virtio/vsock/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub use self::device::Vsock;
3232
pub use self::unix::{VsockUnixBackend, VsockUnixBackendError};
3333
use crate::devices::virtio::iovec::IoVecError;
3434
use crate::devices::virtio::persist::PersistError as VirtioStateError;
35-
use crate::vstate::memory::GuestMemoryMmap;
3635

3736
mod defs {
3837
use crate::devices::virtio::queue::FIRECRACKER_MAX_QUEUE_SIZE;
@@ -172,10 +171,10 @@ pub trait VsockEpollListener: AsRawFd {
172171
/// - `send_pkt(&pkt)` will fetch data from `pkt`, and place it into the channel.
173172
pub trait VsockChannel {
174173
/// Read/receive an incoming packet from the channel.
175-
fn recv_pkt(&mut self, pkt: &mut VsockPacket, mem: &GuestMemoryMmap) -> Result<(), VsockError>;
174+
fn recv_pkt(&mut self, pkt: &mut VsockPacket) -> Result<(), VsockError>;
176175

177176
/// Write/send a packet through the channel.
178-
fn send_pkt(&mut self, pkt: &VsockPacket, mem: &GuestMemoryMmap) -> Result<(), VsockError>;
177+
fn send_pkt(&mut self, pkt: &VsockPacket) -> Result<(), VsockError>;
179178

180179
/// Checks whether there is pending incoming data inside the channel, meaning that a subsequent
181180
/// call to `recv_pkt()` won't fail.

src/vmm/src/devices/virtio/vsock/test_utils.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ impl Default for TestBackend {
6161
}
6262

6363
impl VsockChannel for TestBackend {
64-
fn recv_pkt(
65-
&mut self,
66-
pkt: &mut VsockPacket,
67-
_mem: &GuestMemoryMmap,
68-
) -> Result<(), VsockError> {
64+
fn recv_pkt(&mut self, pkt: &mut VsockPacket) -> Result<(), VsockError> {
6965
let cool_buf = [0xDu8, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF];
7066
match self.rx_err.take() {
7167
None => {
@@ -84,7 +80,7 @@ impl VsockChannel for TestBackend {
8480
}
8581
}
8682

87-
fn send_pkt(&mut self, _pkt: &VsockPacket, _mem: &GuestMemoryMmap) -> Result<(), VsockError> {
83+
fn send_pkt(&mut self, _pkt: &VsockPacket) -> Result<(), VsockError> {
8884
match self.tx_err.take() {
8985
None => {
9086
self.tx_ok_cnt += 1;

src/vmm/src/devices/virtio/vsock/unix/muxer.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use super::muxer_rxq::MuxerRxQ;
4747
use super::{defs, MuxerConnection, VsockUnixBackendError};
4848
use crate::devices::virtio::vsock::metrics::METRICS;
4949
use crate::logger::IncMetric;
50-
use crate::vstate::memory::GuestMemoryMmap;
5150

5251
/// A unique identifier of a `MuxerConnection` object. Connections are stored in a hash map,
5352
/// keyed by a `ConnMapKey` object.
@@ -116,7 +115,7 @@ impl VsockChannel for VsockMuxer {
116115
/// Retuns:
117116
/// - `Ok(())`: `pkt` has been successfully filled in; or
118117
/// - `Err(VsockError::NoData)`: there was no available data with which to fill in the packet.
119-
fn recv_pkt(&mut self, pkt: &mut VsockPacket, mem: &GuestMemoryMmap) -> Result<(), VsockError> {
118+
fn recv_pkt(&mut self, pkt: &mut VsockPacket) -> Result<(), VsockError> {
120119
// We'll look for instructions on how to build the RX packet in the RX queue. If the
121120
// queue is empty, that doesn't necessarily mean we don't have any pending RX, since
122121
// the queue might be out-of-sync. If that's the case, we'll attempt to sync it first,
@@ -152,7 +151,7 @@ impl VsockChannel for VsockMuxer {
152151
let mut conn_res = Err(VsockError::NoData);
153152
let mut do_pop = true;
154153
self.apply_conn_mutation(key, |conn| {
155-
conn_res = conn.recv_pkt(pkt, mem);
154+
conn_res = conn.recv_pkt(pkt);
156155
do_pop = !conn.has_pending_rx();
157156
});
158157
if do_pop {
@@ -189,7 +188,7 @@ impl VsockChannel for VsockMuxer {
189188
/// Returns:
190189
/// always `Ok(())` - the packet has been consumed, and its virtio TX buffers can be
191190
/// returned to the guest vsock driver.
192-
fn send_pkt(&mut self, pkt: &VsockPacket, mem: &GuestMemoryMmap) -> Result<(), VsockError> {
191+
fn send_pkt(&mut self, pkt: &VsockPacket) -> Result<(), VsockError> {
193192
let conn_key = ConnMapKey {
194193
local_port: pkt.dst_port(),
195194
peer_port: pkt.src_port(),
@@ -243,7 +242,7 @@ impl VsockChannel for VsockMuxer {
243242
// Alright, everything looks in order - forward this packet to its owning connection.
244243
let mut res: Result<(), VsockError> = Ok(());
245244
self.apply_conn_mutation(conn_key, |conn| {
246-
res = conn.send_pkt(pkt, mem);
245+
res = conn.send_pkt(pkt);
247246
});
248247

249248
res
@@ -884,15 +883,11 @@ mod tests {
884883
}
885884

886885
fn send(&mut self) {
887-
self.muxer
888-
.send_pkt(&self.tx_pkt, &self._vsock_test_ctx.mem)
889-
.unwrap();
886+
self.muxer.send_pkt(&self.tx_pkt).unwrap();
890887
}
891888

892889
fn recv(&mut self) {
893-
self.muxer
894-
.recv_pkt(&mut self.rx_pkt, &self._vsock_test_ctx.mem)
895-
.unwrap();
890+
self.muxer.recv_pkt(&mut self.rx_pkt).unwrap();
896891
}
897892

898893
fn notify_muxer(&mut self) {

0 commit comments

Comments
 (0)