Skip to content

Commit 0ebad2c

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 3205ad8 commit 0ebad2c

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::virtio::net::{Error, Result};
88
use crate::virtio::net::{QUEUE_SIZES, RX_INDEX, TX_INDEX};
99
use crate::virtio::queue::Error as QueueError;
1010
use crate::virtio::{
11-
ActivateResult, DeviceState, InterruptTransport, Queue, VirtioDevice, TYPE_NET,
11+
ActivateError, ActivateResult, DeviceState, InterruptTransport, Queue, VirtioDevice, TYPE_NET,
1212
};
1313
use crate::Error as DeviceError;
1414

@@ -195,17 +195,27 @@ impl VirtioDevice for Net {
195195
.iter()
196196
.map(|e| e.try_clone().unwrap())
197197
.collect();
198-
let worker = NetWorker::new(
198+
199+
match NetWorker::new(
199200
self.queues.clone(),
200201
queue_evts,
201202
interrupt.clone(),
202203
mem.clone(),
203204
self.cfg_backend.clone(),
204-
);
205-
worker.run();
206-
207-
self.device_state = DeviceState::Activated(mem, interrupt);
208-
Ok(())
205+
) {
206+
Ok(worker) => {
207+
worker.run();
208+
self.device_state = DeviceState::Activated(mem, interrupt);
209+
Ok(())
210+
}
211+
Err(err) => {
212+
error!(
213+
"Error activating virtio-net ({}) backend: {err:?}",
214+
self.id()
215+
);
216+
Err(ActivateError::BadActivate)
217+
}
218+
}
209219
}
210220

211221
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,3 +1,4 @@
1+
use crate::virtio::net::backend::ConnectError;
12
#[cfg(target_os = "linux")]
23
use crate::virtio::net::tap::Tap;
34
use crate::virtio::net::unixgram::Unixgram;
@@ -41,27 +42,27 @@ impl NetWorker {
4142
interrupt: InterruptTransport,
4243
mem: GuestMemoryMmap,
4344
cfg_backend: VirtioNetBackend,
44-
) -> Self {
45+
) -> Result<Self, ConnectError> {
4546
let backend = match cfg_backend {
4647
VirtioNetBackend::UnixstreamFd(fd) => {
4748
Box::new(Unixstream::new(fd)) as Box<dyn NetBackend + Send>
4849
}
4950
VirtioNetBackend::UnixstreamPath(path) => {
50-
Box::new(Unixstream::open(path).unwrap()) as Box<dyn NetBackend + Send>
51+
Box::new(Unixstream::open(path)?) as Box<dyn NetBackend + Send>
5152
}
5253
VirtioNetBackend::UnixgramFd(fd) => {
5354
Box::new(Unixgram::new(fd)) as Box<dyn NetBackend + Send>
5455
}
5556
VirtioNetBackend::UnixgramPath(path, vfkit_magic) => {
56-
Box::new(Unixgram::open(path, vfkit_magic).unwrap()) as Box<dyn NetBackend + Send>
57+
Box::new(Unixgram::open(path, vfkit_magic)?) as Box<dyn NetBackend + Send>
5758
}
5859
#[cfg(target_os = "linux")]
5960
VirtioNetBackend::Tap(tap_name) => {
60-
Box::new(Tap::new(tap_name).unwrap()) as Box<dyn NetBackend + Send>
61+
Box::new(Tap::new(tap_name)?) as Box<dyn NetBackend + Send>
6162
}
6263
};
6364

64-
Self {
65+
Ok(Self {
6566
queues,
6667
queue_evts,
6768

@@ -76,7 +77,7 @@ impl NetWorker {
7677
tx_frame_buf: [0u8; MAX_BUFFER_SIZE],
7778
tx_frame_len: 0,
7879
tx_iovec: Vec::with_capacity(QUEUE_SIZE as usize),
79-
}
80+
})
8081
}
8182

8283
pub fn run(self) {

0 commit comments

Comments
 (0)