vhost_user_net: Remove unnecessary checks for unconfigured memory

Simplify the check for the unusual situation where the memory is not
configured by using .ok_or() on the option to convert it to a result.
This cleans up a bunch of extra indentation.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2019-10-15 16:02:40 +01:00 committed by Sebastien Boeuf
parent df336ade57
commit 8663b429b3

View File

@ -83,8 +83,8 @@ pub enum Error {
InvalidVringAddr, InvalidVringAddr,
/// No vring call fd to notify. /// No vring call fd to notify.
NoVringCallFdNotify, NoVringCallFdNotify,
/// No memory while handling tx. /// No memory configured.
NoMemoryForTx, NoMemoryConfigured,
/// Failed to parse sock parameter. /// Failed to parse sock parameter.
ParseSockParam, ParseSockParam,
/// Failed to parse ip parameter. /// Failed to parse ip parameter.
@ -208,8 +208,9 @@ impl VhostUserNetBackend {
// Copies a single frame from `self.rx.frame_buf` into the guest. Returns true // Copies a single frame from `self.rx.frame_buf` into the guest. Returns true
// if a buffer was used, and false if the frame must be deferred until a buffer // if a buffer was used, and false if the frame must be deferred until a buffer
// is made available by the driver. // is made available by the driver.
fn rx_single_frame(&mut self, vring: &mut Vring) -> bool { fn rx_single_frame(&mut self, vring: &mut Vring) -> Result<bool> {
if let Some(mem) = &self.mem { let mem = self.mem.as_ref().ok_or(Error::NoMemoryConfigured)?;
let mut next_desc = vring.mut_queue().iter(&mem).next(); let mut next_desc = vring.mut_queue().iter(&mem).next();
if next_desc.is_none() { if next_desc.is_none() {
@ -226,7 +227,7 @@ impl VhostUserNetBackend {
.unwrap(); .unwrap();
self.rx_tap_listening = false; self.rx_tap_listening = false;
} }
return false; return Ok(false);
} }
// We just checked that the head descriptor exists. // We just checked that the head descriptor exists.
@ -273,11 +274,7 @@ impl VhostUserNetBackend {
// Mark that we have at least one pending packet and we need to interrupt the guest. // Mark that we have at least one pending packet and we need to interrupt the guest.
self.rx.deferred_irqs = true; self.rx.deferred_irqs = true;
write_count >= self.rx.bytes_read Ok(write_count >= self.rx.bytes_read)
} else {
error!("No memory for single frame handling!\n");
false
}
} }
fn process_rx(&mut self, vring: &mut Vring) -> Result<()> { fn process_rx(&mut self, vring: &mut Vring) -> Result<()> {
@ -286,7 +283,7 @@ impl VhostUserNetBackend {
match self.read_tap() { match self.read_tap() {
Ok(count) => { Ok(count) => {
self.rx.bytes_read = count; self.rx.bytes_read = count;
if !self.rx_single_frame(vring) { if !self.rx_single_frame(vring)? {
self.rx.deferred_frame = true; self.rx.deferred_frame = true;
break; break;
} }
@ -316,7 +313,7 @@ impl VhostUserNetBackend {
fn resume_rx(&mut self, vring: &mut Vring) -> Result<()> { fn resume_rx(&mut self, vring: &mut Vring) -> Result<()> {
if self.rx.deferred_frame { if self.rx.deferred_frame {
if self.rx_single_frame(vring) { if self.rx_single_frame(vring)? {
self.rx.deferred_frame = false; self.rx.deferred_frame = false;
// process_rx() was interrupted possibly before consuming all // process_rx() was interrupted possibly before consuming all
// packets in the tap; try continuing now. // packets in the tap; try continuing now.
@ -334,7 +331,8 @@ impl VhostUserNetBackend {
} }
fn process_tx(&mut self, vring: &mut Vring) -> Result<()> { fn process_tx(&mut self, vring: &mut Vring) -> Result<()> {
if let Some(mem) = &self.mem { let mem = self.mem.as_ref().ok_or(Error::NoMemoryConfigured)?;
let mut used_desc_heads = [(0, 0); QUEUE_SIZE]; let mut used_desc_heads = [(0, 0); QUEUE_SIZE];
let mut used_count = 0; let mut used_count = 0;
while let Some(avail_desc) = vring.mut_queue().iter(&mem).next() { while let Some(avail_desc) = vring.mut_queue().iter(&mem).next() {
@ -391,10 +389,7 @@ impl VhostUserNetBackend {
} }
vring.signal_used_queue().unwrap(); vring.signal_used_queue().unwrap();
} }
} else {
error!("No memory for vhost-user-net backend tx handling!\n");
return Err(Error::NoMemoryForTx);
}
Ok(()) Ok(())
} }
@ -463,7 +458,7 @@ impl VhostUserBackend for VhostUserNetBackend {
// Process a deferred frame first if available. Don't read from tap again // Process a deferred frame first if available. Don't read from tap again
// until we manage to receive this deferred frame. // until we manage to receive this deferred frame.
{ {
if self.rx_single_frame(&mut vring) { if self.rx_single_frame(&mut vring).unwrap() {
self.rx.deferred_frame = false; self.rx.deferred_frame = false;
self.process_rx(&mut vring).unwrap(); self.process_rx(&mut vring).unwrap();
} else if self.rx.deferred_irqs { } else if self.rx.deferred_irqs {