Skip to content

Commit

Permalink
fix: workflow test bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hky1999 committed Nov 12, 2023
1 parent d0cfe7f commit 574d692
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
1 change: 1 addition & 0 deletions examples/aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use virtio_drivers::{
blk::VirtIOBlk,
console::VirtIOConsole,
gpu::VirtIOGpu,
net::VirtIONetRaw,
socket::{
VirtIOSocket, VsockAddr, VsockConnectionManager, VsockEventType, VMADDR_CID_HOST,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/riscv/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<T: Transport> TxToken for VirtioTxToken<T> {
F: FnOnce(&mut [u8]) -> R,
{
let mut dev = self.0.borrow_mut();
let mut tx_buf = dev.new_tx_buffer(len).unwrap();
let mut tx_buf = dev.new_tx_buffer(len);
let result = f(tx_buf.packet_mut());
trace!("SEND {} bytes: {:02X?}", len, tx_buf.packet());
dev.transmit_wait(tx_buf).unwrap();
Expand Down
6 changes: 0 additions & 6 deletions examples/x86_64/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ else
BUILD_ARGS += --no-default-features
endif

ifeq ($(tcp), on)
BUILD_ARGS += --features tcp
else
BUILD_ARGS += --no-default-features
endif

QEMU_ARGS += \
-machine q35 \
-serial mon:stdio \
Expand Down
10 changes: 5 additions & 5 deletions examples/x86_64/src/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Simple echo server over TCP.
//!
//! Ref: https://github.com/smoltcp-rs/smoltcp/blob/master/examples/server.rs
//! Ref: <https://github.com/smoltcp-rs/smoltcp/blob/master/examples/server.rs>

use alloc::{borrow::ToOwned, rc::Rc, vec, vec::Vec};
use core::{cell::RefCell, str::FromStr};
Expand All @@ -9,7 +9,7 @@ use smoltcp::iface::{Config, Interface, SocketSet};
use smoltcp::phy::{Device, DeviceCapabilities, Medium, RxToken, TxToken};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
use smoltcp::{socket::tcp, time::Instant};
use virtio_drivers::device::net::{NetBuffer, VirtIONet};
use virtio_drivers::device::net::{RxBuffer, VirtIONet};
use virtio_drivers::{transport::Transport, Error};

use super::{HalImpl, NET_QUEUE_SIZE};
Expand Down Expand Up @@ -64,7 +64,7 @@ impl<T: Transport> Device for DeviceWrapper<T> {
}
}

struct VirtioRxToken<T: Transport>(Rc<RefCell<DeviceImpl<T>>>, NetBuffer);
struct VirtioRxToken<T: Transport>(Rc<RefCell<DeviceImpl<T>>>, RxBuffer);
struct VirtioTxToken<T: Transport>(Rc<RefCell<DeviceImpl<T>>>);

impl<T: Transport> RxToken for VirtioRxToken<T> {
Expand All @@ -90,10 +90,10 @@ impl<T: Transport> TxToken for VirtioTxToken<T> {
F: FnOnce(&mut [u8]) -> R,
{
let mut dev = self.0.borrow_mut();
let mut tx_buf = dev.new_tx_buffer(len).unwrap();
let mut tx_buf = dev.new_tx_buffer(len);
let result = f(tx_buf.packet_mut());
trace!("SEND {} bytes: {:02X?}", len, tx_buf.packet());
dev.transmit_wait(tx_buf).unwrap();
dev.transmit(tx_buf).unwrap();
result
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/device/net/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{hal::Hal, transport::Transport, Error, Result};

/// Driver for a VirtIO block device.
///
/// Unlike [`VirtIONetRaw`], it uses [`NetBuffer`]s for transmission and
/// Unlike [`VirtIONetRaw`], it uses [`RxBuffer`]s for transmission and
/// reception rather than the raw slices. On initialization, it pre-allocates
/// all receive buffers and puts them all in the receive queue.
///
Expand Down Expand Up @@ -117,9 +117,15 @@ impl<H: Hal, T: Transport, const QUEUE_SIZE: usize> VirtIONet<H, T, QUEUE_SIZE>
TxBuffer(vec![0; buf_len])
}

/// Sends a [`TxBuffer`] to the network, and blocks until the request
/// completed. Returns number of bytes transmitted.
pub fn transmit_wait(&mut self, tx_buf: TxBuffer) -> Result<usize> {
self.inner.transmit_wait(tx_buf.packet())
}

/// Sends a [`TxBuffer`] to the network, and blocks until the request
/// completed.
pub fn send(&mut self, tx_buf: TxBuffer) -> Result {
self.inner.send(tx_buf)
self.inner.send(tx_buf.packet())
}
}
39 changes: 19 additions & 20 deletions src/device/net/dev_raw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::TxBuffer;
use super::{Config, EthernetAddress, Features, VirtioNetHdr};
use super::{MIN_BUFFER_LEN, NET_HDR_SIZE, QUEUE_RECEIVE, QUEUE_TRANSMIT};
use crate::hal::Hal;
Expand Down Expand Up @@ -242,11 +241,26 @@ impl<H: Hal, T: Transport, const QUEUE_SIZE: usize> VirtIONetRaw<H, T, QUEUE_SIZ
Ok((NET_HDR_SIZE, packet_len))
}

/// Sends a [`TxBuffer`] to the network, and blocks until the request
/// Transmits a packet to the network, and blocks until the request
/// completed. Returns number of bytes transmitted.
///
/// The caller needs to fill the `tx_buf` with a header by calling
/// [`fill_buffer_header`] before transmission.
///
/// [`fill_buffer_header`]: Self::fill_buffer_header
pub fn transmit_wait(&mut self, tx_buf: &[u8]) -> Result<usize> {
let token = unsafe { self.transmit_begin(tx_buf)? };
while self.poll_transmit().is_none() {
core::hint::spin_loop();
}
unsafe { self.transmit_complete(token, tx_buf) }
}

/// Sends a packet to the network, and blocks until the request
/// completed.
pub fn send(&mut self, tx_buf: TxBuffer) -> Result {
pub fn send(&mut self, tx_buf: &[u8]) -> Result {
let header = VirtioNetHdr::default();
if tx_buf.packet_len() == 0 {
if tx_buf.len() == 0 {
// Special case sending an empty packet, to avoid adding an empty buffer to the
// virtqueue.
self.send_queue.add_notify_wait_pop(
Expand All @@ -256,29 +270,14 @@ impl<H: Hal, T: Transport, const QUEUE_SIZE: usize> VirtIONetRaw<H, T, QUEUE_SIZ
)?;
} else {
self.send_queue.add_notify_wait_pop(
&[header.as_bytes(), tx_buf.packet()],
&[header.as_bytes(), tx_buf],
&mut [],
&mut self.transport,
)?;
}
Ok(())
}

/// Transmits a packet to the network, and blocks until the request
/// completed. Returns number of bytes transmitted.
///
/// The caller needs to fill the `tx_buf` with a header by calling
/// [`fill_buffer_header`] before transmission.
///
/// [`fill_buffer_header`]: Self::fill_buffer_header
pub fn transmit_wait(&mut self, tx_buf: &[u8]) -> Result<usize> {
let token = unsafe { self.transmit_begin(tx_buf)? };
while self.poll_transmit().is_none() {
core::hint::spin_loop();
}
unsafe { self.transmit_complete(token, tx_buf) }
}

/// Blocks and waits for a packet to be received.
///
/// After completion, the `rx_buf` will contain a header followed by the
Expand Down

0 comments on commit 574d692

Please sign in to comment.