Skip to content

Commit 1c53e25

Browse files
committed
net: improve error handling for backend creation
With the new API, as we're actively participating in opening the connection to the network proxy, the creation of the virtio-net backend may fail. Avoid using "unwrap" and fail gracefully on device activation instead. Signed-off-by: Sergio Lopez <[email protected]>
1 parent bbf5b2e commit 1c53e25

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::legacy::IrqChip;
88
use crate::virtio::net::{Error, Result};
99
use crate::virtio::net::{QUEUE_SIZES, RX_INDEX, TX_INDEX};
1010
use crate::virtio::queue::Error as QueueError;
11-
use crate::virtio::{ActivateResult, DeviceState, Queue, VirtioDevice, TYPE_NET};
11+
use crate::virtio::{ActivateError, ActivateResult, DeviceState, Queue, VirtioDevice, TYPE_NET};
1212
use crate::Error as DeviceError;
1313

1414
use super::backend::{ReadError, WriteError};
@@ -223,7 +223,8 @@ impl VirtioDevice for Net {
223223
.iter()
224224
.map(|e| e.try_clone().unwrap())
225225
.collect();
226-
let worker = NetWorker::new(
226+
227+
match NetWorker::new(
227228
self.queues.clone(),
228229
queue_evts,
229230
self.interrupt_status.clone(),
@@ -232,11 +233,20 @@ impl VirtioDevice for Net {
232233
self.irq_line,
233234
mem.clone(),
234235
self.cfg_backend.clone(),
235-
);
236-
worker.run();
237-
238-
self.device_state = DeviceState::Activated(mem);
239-
Ok(())
236+
) {
237+
Ok(worker) => {
238+
worker.run();
239+
self.device_state = DeviceState::Activated(mem);
240+
Ok(())
241+
}
242+
Err(err) => {
243+
error!(
244+
"Error activating virtio-net ({}) backend: {err:?}",
245+
self.id()
246+
);
247+
Err(ActivateError::BadActivate)
248+
}
249+
}
240250
}
241251

242252
fn is_activated(&self) -> bool {

src/devices/src/virtio/net/worker.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::legacy::IrqChip;
2+
use crate::virtio::net::backend::ConnectError;
23
#[cfg(target_os = "linux")]
34
use crate::virtio::net::tap::Tap;
45
use crate::virtio::net::unixgram::Unixgram;
@@ -52,27 +53,27 @@ impl NetWorker {
5253
irq_line: Option<u32>,
5354
mem: GuestMemoryMmap,
5455
cfg_backend: VirtioNetBackend,
55-
) -> Self {
56+
) -> Result<Self, ConnectError> {
5657
let backend = match cfg_backend {
5758
VirtioNetBackend::UnixstreamFd(fd) => {
5859
Box::new(Unixstream::new(fd)) as Box<dyn NetBackend + Send>
5960
}
6061
VirtioNetBackend::UnixstreamPath(path) => {
61-
Box::new(Unixstream::open(path).unwrap()) as Box<dyn NetBackend + Send>
62+
Box::new(Unixstream::open(path)?) as Box<dyn NetBackend + Send>
6263
}
6364
VirtioNetBackend::UnixgramFd(fd) => {
6465
Box::new(Unixgram::new(fd)) as Box<dyn NetBackend + Send>
6566
}
6667
VirtioNetBackend::UnixgramPath(path, vfkit_magic) => {
67-
Box::new(Unixgram::open(path, vfkit_magic).unwrap()) as Box<dyn NetBackend + Send>
68+
Box::new(Unixgram::open(path, vfkit_magic)?) as Box<dyn NetBackend + Send>
6869
}
6970
#[cfg(target_os = "linux")]
7071
VirtioNetBackend::Tap(tap_name) => {
71-
Box::new(Tap::new(tap_name).unwrap()) as Box<dyn NetBackend + Send>
72+
Box::new(Tap::new(tap_name)?) as Box<dyn NetBackend + Send>
7273
}
7374
};
7475

75-
Self {
76+
Ok(Self {
7677
queues,
7778
queue_evts,
7879
interrupt_status,
@@ -90,7 +91,7 @@ impl NetWorker {
9091
tx_frame_buf: [0u8; MAX_BUFFER_SIZE],
9192
tx_frame_len: 0,
9293
tx_iovec: Vec::with_capacity(QUEUE_SIZE as usize),
93-
}
94+
})
9495
}
9596

9697
pub fn run(self) {

0 commit comments

Comments
 (0)