Skip to content

Commit

Permalink
feat(driver/net): 实现Loopback网卡接口 (DragonOS-Community#845)
Browse files Browse the repository at this point in the history
* 初步实现loopback设备
  • Loading branch information
smallcjy authored and BrahmaMantra committed Dec 9, 2024
1 parent 61acf40 commit cc1beb5
Show file tree
Hide file tree
Showing 10 changed files with 613 additions and 5 deletions.
484 changes: 484 additions & 0 deletions kernel/src/driver/net/loopback.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions kernel/src/driver/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use system_error::SystemError;
mod dma;
pub mod e1000e;
pub mod irq_handle;
pub mod loopback;
pub mod virtio_net;

pub trait NetDevice: Device {
Expand Down
4 changes: 0 additions & 4 deletions kernel/src/init/initial_kthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ fn kernel_init() -> Result<(), SystemError> {

#[cfg(target_arch = "x86_64")]
crate::driver::disk::ahci::ahci_init().expect("Failed to initialize AHCI");

virtio_probe();
mount_root_fs().expect("Failed to mount root fs");

e1000e_init();
net_init().unwrap_or_else(|err| {
error!("Failed to initialize network: {:?}", err);
Expand Down Expand Up @@ -97,7 +95,6 @@ fn try_to_run_init_process(path: &str, trap_frame: &mut TrapFrame) -> Result<(),
}
return Err(e);
}

Ok(())
}

Expand All @@ -107,6 +104,5 @@ fn run_init_process(path: String, trap_frame: &mut TrapFrame) -> Result<(), Syst

compiler_fence(Ordering::SeqCst);
Syscall::do_execve(path, argv, envp, trap_frame)?;

Ok(())
}
4 changes: 3 additions & 1 deletion kernel/src/net/net_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ pub fn net_init() -> Result<(), SystemError> {
fn dhcp_query() -> Result<(), SystemError> {
let binding = NET_DEVICES.write_irqsave();

let net_face = binding.get(&0).ok_or(SystemError::ENODEV)?.clone();
//由于现在os未实现在用户态为网卡动态分配内存,而lo网卡的id最先分配且ip固定不能被分配
//所以特判取用id为1的网卡(也就是virto_net)
let net_face = binding.get(&1).ok_or(SystemError::ENODEV)?.clone();

drop(binding);

Expand Down
3 changes: 3 additions & 0 deletions user/apps/test_lo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
Cargo.lock
/install/
7 changes: 7 additions & 0 deletions user/apps/test_lo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_lo"
version = "0.1.0"
edition = "2021"
description = "测试lo网卡功能"
authors = [ "smallc <2628035541@qq.com>" ]

56 changes: 56 additions & 0 deletions user/apps/test_lo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
TOOLCHAIN=
RUSTFLAGS=

ifdef DADK_CURRENT_BUILD_DIR
# 如果是在dadk中编译,那么安装到dadk的安装目录中
INSTALL_DIR = $(DADK_CURRENT_BUILD_DIR)
else
# 如果是在本地编译,那么安装到当前目录下的install目录中
INSTALL_DIR = ./install
endif

ifeq ($(ARCH), x86_64)
export RUST_TARGET=x86_64-unknown-linux-musl
else ifeq ($(ARCH), riscv64)
export RUST_TARGET=riscv64gc-unknown-linux-gnu
else
# 默认为x86_86,用于本地编译
export RUST_TARGET=x86_64-unknown-linux-musl
endif

run:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET)

build:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET)

clean:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET)

test:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET)

doc:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) doc --target $(RUST_TARGET)

fmt:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt

fmt-check:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt --check

run-release:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET) --release

build-release:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET) --release

clean-release:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET) --release

test-release:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET) --release

.PHONY: install
install:
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) install --target $(RUST_TARGET) --path . --no-track --root $(INSTALL_DIR) --force
7 changes: 7 additions & 0 deletions user/apps/test_lo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# test-lo

lo网卡功能测试程序

## 测试过程:

通过创建一个UDP套接字,然后发送一条消息到本地回环地址127.0.0.1(lo网卡),再接收并验证这条消息,以此来测试lo网卡的功能。期望发送的消息和接收到的消息是完全一样的。通过日志输出查看测试是否成功。
25 changes: 25 additions & 0 deletions user/apps/test_lo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::net::UdpSocket;
use std::str;

fn main() -> std::io::Result<()> {
let socket = UdpSocket::bind("127.0.0.1:34254")?;
socket.connect("127.0.0.1:34254")?;

let msg = "Hello, loopback!";
socket.send(msg.as_bytes())?;

let mut buf = [0; 1024];
let (amt, _src) = socket.recv_from(&mut buf)?;

let received_msg = str::from_utf8(&buf[..amt]).expect("Could not read buffer as UTF-8");

println!("Sent: {}", msg);
println!("Received: {}", received_msg);

assert_eq!(
msg, received_msg,
"The sent and received messages do not match!"
);

Ok(())
}
27 changes: 27 additions & 0 deletions user/dadk/config/test_lo_0_1_0.dadk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "test_lo",
"version": "0.1.0",
"description": "test for lo interface",
"rust_target": null,
"task_type": {
"BuildFromSource": {
"Local": {
"path": "apps/test_lo"
}
}
},
"depends": [],
"build": {
"build_command": "make install"
},
"install": {
"in_dragonos_path": "/"
},
"clean": {
"clean_command": "make clean"
},
"envs": [],
"build_once": false,
"install_once": false,
"target_arch": ["x86_64"]
}

0 comments on commit cc1beb5

Please sign in to comment.