Skip to content

Commit

Permalink
porting axnet api
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed May 25, 2024
1 parent 8a729f8 commit a11b126
Show file tree
Hide file tree
Showing 30 changed files with 2,266 additions and 8 deletions.
90 changes: 86 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"crates/flatten_objects",
"crates/lazy_init",
"crates/linked_list",
"crates/lwip_rust",
"crates/page_table",
"crates/page_table_entry",
"crates/percpu",
Expand Down Expand Up @@ -58,7 +59,7 @@ members = [

"apps/display/basic_painting",
"apps/display/draw_map",
"apps/fs/shell",
"apps/fs/shell", "crates/lwip_rust",
]

[profile.release]
Expand Down
4 changes: 2 additions & 2 deletions api/ruxos_posix_api/src/imp/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ impl Socket {
fn shutdown(&self) -> LinuxResult {
match self {
Socket::Udp(udpsocket) => {
let udpsocket = udpsocket.lock();
let mut udpsocket = udpsocket.lock();
udpsocket.peer_addr()?;
udpsocket.shutdown()?;
Ok(())
}

Socket::Tcp(tcpsocket) => {
let tcpsocket = tcpsocket.lock();
let mut tcpsocket = tcpsocket.lock();
tcpsocket.peer_addr()?;
tcpsocket.shutdown()?;
Ok(())
Expand Down
13 changes: 13 additions & 0 deletions crates/driver_net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#![feature(const_slice_from_raw_parts_mut)]
#![feature(box_into_inner)]

extern crate alloc;
use alloc::sync::Arc;

#[cfg(feature = "ixgbe")]
/// ixgbe NIC device driver.
pub mod ixgbe;
Expand Down Expand Up @@ -46,6 +49,16 @@ pub trait NetDriverOps: BaseDriverOps {
/// Size of the transmit queue.
fn tx_queue_size(&self) -> usize;

/// Fills the receive queue with buffers.
///
/// It should be called once when the driver is initialized.
fn fill_rx_buffers(&mut self, buf_pool: &Arc<NetBufPool>) -> DevResult;

/// Prepares a buffer for transmitting.
///
/// e.g., fill the header of the packet.
fn prepare_tx_buffer(&self, tx_buf: &mut NetBuf, packet_len: usize) -> DevResult;

/// Gives back the `rx_buf` to the receive queue for later receiving.
///
/// `rx_buf` should be the same as the one returned by
Expand Down
33 changes: 33 additions & 0 deletions crates/driver_virtio/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl<H: Hal, T: Transport, const QS: usize> VirtIoNetDev<H, T, QS> {
// 1. Fill all rx buffers.
for (i, rx_buf_place) in dev.rx_buffers.iter_mut().enumerate() {
let mut rx_buf = dev.buf_pool.alloc_boxed().ok_or(DevError::NoMemory)?;
info!("lhw debug driver_virtio try_new {:?}", rx_buf.raw_buf_mut().len());
// Safe because the buffer lives as long as the queue.
let token = unsafe {
dev.inner
Expand Down Expand Up @@ -81,6 +82,7 @@ impl<H: Hal, T: Transport, const QS: usize> VirtIoNetDev<H, T, QS> {
}
}

use log::info;
impl<H: Hal, T: Transport, const QS: usize> const BaseDriverOps for VirtIoNetDev<H, T, QS> {
fn device_name(&self) -> &str {
"virtio-net"
Expand Down Expand Up @@ -116,6 +118,24 @@ impl<H: Hal, T: Transport, const QS: usize> NetDriverOps for VirtIoNetDev<H, T,
fn tx_queue_size(&self) -> usize {
QS
}

fn fill_rx_buffers(&mut self, buf_pool: &Arc<NetBufPool>) -> DevResult {
for (i, rx_buf_place) in self.rx_buffers.iter_mut().enumerate() {
let mut rx_buf = buf_pool.alloc_boxed().ok_or(DevError::NoMemory)?;
// Safe because the buffer lives as long as the queue.
info!("lhw debug in fill_rx_buffers before token {:?}", rx_buf.raw_buf_mut().len());
let token = unsafe {
let res = self.inner
.receive_begin(rx_buf.raw_buf_mut())
.map_err(as_dev_err);
info!("lhw debug in fill_rx_buffers token receive_begin res: {:?}",res);
res?
};
assert_eq!(token, i as u16);
*rx_buf_place = Some(rx_buf);
}
Ok(())
}

fn recycle_rx_buffer(&mut self, rx_buf: NetBufPtr) -> DevResult {
let mut rx_buf = unsafe { NetBuf::from_buf_ptr(rx_buf) };
Expand All @@ -135,6 +155,19 @@ impl<H: Hal, T: Transport, const QS: usize> NetDriverOps for VirtIoNetDev<H, T,
Ok(())
}

fn prepare_tx_buffer(&self, tx_buf: &mut NetBuf, pkt_len: usize) -> DevResult {
let hdr_len = self
.inner
.fill_buffer_header(tx_buf.raw_buf_mut())
.or(Err(DevError::InvalidParam))?;
if hdr_len + pkt_len > tx_buf.capacity() {
return Err(DevError::InvalidParam);
}
tx_buf.set_header_len(hdr_len);
tx_buf.set_packet_len(pkt_len);
Ok(())
}

fn recycle_tx_buffers(&mut self) -> DevResult {
while let Some(token) = self.inner.poll_transmit() {
let tx_buf = self.tx_buffers[token as usize]
Expand Down
2 changes: 2 additions & 0 deletions crates/lwip_rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/src/bindings.rs
14 changes: 14 additions & 0 deletions crates/lwip_rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "lwip_rust"
version = "0.1.0"
edition = "2021"
authors = ["Haixuan Tong <[email protected]>"]
description = "Rust wrapper of lwip net stack"
license = "GPL-3.0-or-later OR Apache-2.0"
homepage = "https://github.com/rcore-os/arceos"
repository = "https://github.com/rcore-os/arceos/tree/main/crates/lwip_rust"
documentation = "https://rcore-os.github.io/arceos/lwip_rust/index.html"

[build-dependencies]
bindgen = "0.65.1"
cc = { version = "1.0.79", features = ["parallel"] }
5 changes: 5 additions & 0 deletions crates/lwip_rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# lwip_rust

## dependency

`sudo apt install libclang-dev`
Loading

0 comments on commit a11b126

Please sign in to comment.