Skip to content

Commit d395de6

Browse files
committed
latest work
Signed-off-by: aerosouund <aerosound161@gmail.com>
1 parent 896de7a commit d395de6

5 files changed

Lines changed: 36 additions & 44 deletions

File tree

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const fn frame_hdr_len() -> usize {
6363

6464
// ammar: maybe reduce visibility of those types when done
6565
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
66+
#[serde(rename_all = "snake_case")]
6667
pub enum NetDevBackendType {
6768
Socket(String), // the string denotes the socket path
6869
Tap(String), // denotes the tap device name
@@ -245,7 +246,7 @@ pub struct Net {
245246
pub(crate) id: String,
246247

247248
/// The backend for this device: a tap.
248-
pub tap: Box<dyn NetDevBackend>,
249+
pub backend: Box<dyn NetDevBackend>,
249250

250251
pub(crate) avail_features: u64,
251252
pub(crate) acked_features: u64,
@@ -285,6 +286,7 @@ impl Net {
285286
tx_rate_limiter: RateLimiter,
286287
) -> Result<Self, NetError> {
287288
// ammar: what are those features ?
289+
// what should we advertise for the stream backend ? the same stuff ?
288290
let mut avail_features = (1 << VIRTIO_F_VERSION_1)
289291
| (1 << VIRTIO_NET_F_MRG_RXBUF)
290292
| (1 << VIRTIO_RING_F_EVENT_IDX);
@@ -306,7 +308,7 @@ impl Net {
306308

307309
Ok(Net {
308310
id: id.clone(),
309-
tap: backend,
311+
backend: backend,
310312
avail_features,
311313
acked_features: 0u64,
312314
queues,
@@ -337,12 +339,7 @@ impl Net {
337339
tx_rate_limiter: RateLimiter,
338340
backend_type: NetDevBackendType,
339341
) -> Result<Self, NetError> {
340-
// ammar: here we should do an enum check on the type of the backend to see how to construct the netdev
341-
342342
let vnet_hdr_size = i32::try_from(vnet_hdr_len()).unwrap();
343-
// ammar: there is a set header length size. so the backend we will implement should respect that and only return to us bytes
344-
// per read as much as this header size
345-
346343
let backend: Box<dyn NetDevBackend> = match backend_type {
347344
// ammar: use something other than io error as the return of socket backend creation
348345
NetDevBackendType::Socket(path) => {
@@ -366,9 +363,9 @@ impl Net {
366363
}
367364

368365
/// Provides the host IFACE name of this net device.
366+
/// ammar: maybe this function should be called something else ?
369367
pub fn iface_name(&self) -> String {
370-
// ammar: what should iface name in the non tap case ? this will matter in persistence
371-
self.tap.identifier().to_string()
368+
self.backend.identifier().to_string()
372369
}
373370

374371
/// Provides the MmdsNetworkStack of this net device.
@@ -762,7 +759,7 @@ impl Net {
762759
&mut self.tx_rate_limiter,
763760
&mut self.tx_frame_headers,
764761
&self.tx_buffer,
765-
&mut self.tap,
762+
&mut self.backend,
766763
self.guest_mac,
767764
&self.metrics,
768765
)
@@ -856,7 +853,7 @@ impl Net {
856853
} else {
857854
self.rx_buffer.single_chain_slice_mut()
858855
};
859-
self.tap.read_iovec(slice)
856+
self.backend.read_iovec(slice)
860857
}
861858

862859
fn write_backend(
@@ -1058,8 +1055,8 @@ impl VirtioDevice for Net {
10581055

10591056
// ammar: we should try to fully understand offload
10601057
let supported_flags: u32 = Net::build_tap_offload_features(self.acked_features);
1061-
if let NetDevBackendType::Tap(_) = self.tap.save() {
1062-
self.tap
1058+
if let NetDevBackendType::Tap(_) = self.backend.save() {
1059+
self.backend
10631060
.set_offload(supported_flags)
10641061
.map_err(super::super::ActivateError::TapSetOffload)?;
10651062
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ impl Net {
4646
)) {
4747
error!("Failed to register tx queue event: {}", err);
4848
}
49-
let fd = &self.tap.as_raw_fd();
49+
let fd = &self.backend.as_raw_fd();
5050
if let Err(err) = ops.add(Events::with_data(
5151
fd,
52-
Self::PROCESS_TAP_RX,
52+
Self::PROCESS_TAP_RX, // ammar: should we just keep calling this tap ?
5353
EventSet::IN | EventSet::EDGE_TRIGGERED,
5454
)) {
5555
error!("Failed to register tap event: {}", err);

src/vmm/src/devices/virtio/net/persist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Persist<'_> for Net {
8686
guest_mac: self.guest_mac,
8787
},
8888
virtio_state: VirtioDeviceState::from_device(self),
89-
backend_type: self.tap.save(),
89+
backend_type: self.backend.save(),
9090
}
9191
}
9292

src/vmm/src/devices/virtio/net/tap.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ pub trait NetDevBackend: Debug + Send + Sync + AsRawFd {
123123
// if name as str no. but there should be a way to store and retrieve the interface name for persistence
124124
// set_offload ? idk, don't really understand
125125
// read and write iovec ? for sure
126-
fn set_vnet_hdr_size(&mut self, size: c_int) -> Result<(), TapError>;
127126
// check if we can make them default implementations
128127
fn read_iovec(&mut self, buffer: &mut [libc::iovec]) -> Result<usize, IoError>;
129128
// heyy
@@ -133,6 +132,8 @@ pub trait NetDevBackend: Debug + Send + Sync + AsRawFd {
133132
// fetch back the NetDevBackendType from the trait
134133
fn save(&self) -> NetDevBackendType;
135134
// set offload
135+
// ammar: whats the fate of offload on the socket backend ? it should be: if you know your socket backend will
136+
// handle offload, let it do that. otherwise, tell the vmm to do it itself. does qemu do that ?
136137
fn set_offload(&self, flags: c_uint) -> Result<(), TapError>;
137138
}
138139

@@ -144,7 +145,7 @@ pub struct SocketBacked {
144145
}
145146

146147
impl SocketBacked {
147-
pub fn new(path: String) -> Result<Self, std::io::Error> {
148+
pub fn new(path: String) -> Result<Self, IoError> {
148149
// open a socket and set its path to path
149150
let stream = UnixStream::connect(path.clone())?;
150151
stream.set_nonblocking(true)?;
@@ -163,15 +164,6 @@ impl AsRawFd for SocketBacked {
163164
}
164165

165166
impl NetDevBackend for SocketBacked {
166-
// if i need to make it change the header size then won't i need a mutable reference ? if i
167-
// make the tap function take a mutable reference would this fuck up ownership somewhere ?
168-
// nah didn't cause much damage, just needed to declare the tap as mut
169-
// maybe we don't need that
170-
fn set_vnet_hdr_size(&mut self, size: c_int) -> Result<(), TapError> {
171-
self.hdr_size = size;
172-
Ok(())
173-
}
174-
175167
fn read_iovec(&mut self, buffer: &mut [libc::iovec]) -> Result<usize, IoError> {
176168
let iov = buffer.as_mut_ptr();
177169

@@ -181,6 +173,9 @@ impl NetDevBackend for SocketBacked {
181173
iov_len: unsafe { (*iov).iov_len - vnet_hdr_len() },
182174
};
183175

176+
// ammar: we are saying this is a "Socket" backend. not a passt backend. so should we really
177+
// be having passt specific handling ? also on some level how well do we even understand passt ?
178+
// is using it as in the test the standard way even ?
184179
// create a 4 byte buffer, read the size that passt prepends into the packet and discard those
185180
// bytes. we only care about the data packets
186181
let mut len_buf = [0u8; 4];
@@ -220,13 +215,16 @@ impl NetDevBackend for SocketBacked {
220215
};
221216

222217
let data_iov = unsafe { *(buffer.as_iovec_ptr()) };
218+
// ammar: we need to check if we have a mininum of vnet hdr len bytes
223219
let data_iov = unsafe {
224220
libc::iovec {
225-
iov_base: (data_iov.iov_base as *mut u8).add(12) as *mut core::ffi::c_void,
226-
iov_len: data_iov.iov_len - 12,
221+
iov_base: (data_iov.iov_base as *mut u8).add(vnet_hdr_len())
222+
as *mut core::ffi::c_void,
223+
iov_len: data_iov.iov_len - vnet_hdr_len(),
227224
}
228225
};
229226

227+
// ammar: can this function absolutely guarantee it will get one iov in the buffer
230228
let iovs: [libc::iovec; 2] = [size_iov, data_iov];
231229
iovcnt += 1;
232230

@@ -238,8 +236,7 @@ impl NetDevBackend for SocketBacked {
238236
}
239237

240238
fn identifier(&self) -> String {
241-
// ammar: fix. this sucks
242-
self.path.to_str().unwrap().to_string()
239+
self.path.display().to_string()
243240
}
244241

245242
fn save(&self) -> NetDevBackendType {
@@ -252,19 +249,7 @@ impl NetDevBackend for SocketBacked {
252249
}
253250
}
254251

255-
// should it be a trait or just an enum that contains both?
256252
impl NetDevBackend for Tap {
257-
/// Set the size of the vnet hdr.
258-
// ammar: on a socket backend this would just set a field on the backing struct
259-
fn set_vnet_hdr_size(&mut self, size: c_int) -> Result<(), TapError> {
260-
// SAFETY: ioctl is safe. Called with a valid tap fd, and we check the return.
261-
if unsafe { ioctl_with_ref(&self.tap_file, TUNSETVNETHDRSZ(), &size) } < 0 {
262-
return Err(TapError::SetSizeOfVnetHdr(IoError::last_os_error()));
263-
}
264-
265-
Ok(())
266-
}
267-
268253
/// Write an `IoVecBuffer` to tap
269254
fn write_iovec(&mut self, buffer: &IoVecBuffer) -> Result<usize, IoError> {
270255
let iovcnt = i32::try_from(buffer.iovec_count()).unwrap();
@@ -359,6 +344,16 @@ impl Tap {
359344
.unwrap_or(IFACE_NAME_MAX_LEN);
360345
std::str::from_utf8(&self.if_name[..len]).unwrap_or("")
361346
}
347+
348+
/// Set the size of the vnet hdr.
349+
pub fn set_vnet_hdr_size(&mut self, size: c_int) -> Result<(), TapError> {
350+
// SAFETY: ioctl is safe. Called with a valid tap fd, and we check the return.
351+
if unsafe { ioctl_with_ref(&self.tap_file, TUNSETVNETHDRSZ(), &size) } < 0 {
352+
return Err(TapError::SetSizeOfVnetHdr(IoError::last_os_error()));
353+
}
354+
355+
Ok(())
356+
}
362357
}
363358

364359
impl AsRawFd for Tap {

src/vmm/src/vmm_config/net.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl From<&Net> for NetworkInterfaceConfig {
4545
guest_mac: net.guest_mac().copied(),
4646
rx_rate_limiter: rx_rl.into_option(),
4747
tx_rate_limiter: tx_rl.into_option(),
48-
backend_type: net.tap.save(),
48+
backend_type: net.backend.save(),
4949
}
5050
}
5151
}

0 commit comments

Comments
 (0)