Skip to content

Commit 1ea2daa

Browse files
authored
feat(driver/net): 实现Loopback网卡接口 (#845)
* 初步实现loopback设备
1 parent ef2a79b commit 1ea2daa

File tree

10 files changed

+613
-5
lines changed

10 files changed

+613
-5
lines changed

kernel/src/driver/net/loopback.rs

Lines changed: 484 additions & 0 deletions
Large diffs are not rendered by default.

kernel/src/driver/net/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use system_error::SystemError;
1111
mod dma;
1212
pub mod e1000e;
1313
pub mod irq_handle;
14+
pub mod loopback;
1415
pub mod virtio_net;
1516

1617
pub trait NetDevice: Device {

kernel/src/init/initial_kthread.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ fn kernel_init() -> Result<(), SystemError> {
3535

3636
#[cfg(target_arch = "x86_64")]
3737
crate::driver::disk::ahci::ahci_init().expect("Failed to initialize AHCI");
38-
3938
virtio_probe();
4039
mount_root_fs().expect("Failed to mount root fs");
41-
4240
e1000e_init();
4341
net_init().unwrap_or_else(|err| {
4442
error!("Failed to initialize network: {:?}", err);
@@ -97,7 +95,6 @@ fn try_to_run_init_process(path: &str, trap_frame: &mut TrapFrame) -> Result<(),
9795
}
9896
return Err(e);
9997
}
100-
10198
Ok(())
10299
}
103100

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

108105
compiler_fence(Ordering::SeqCst);
109106
Syscall::do_execve(path, argv, envp, trap_frame)?;
110-
111107
Ok(())
112108
}

kernel/src/net/net_core.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ pub fn net_init() -> Result<(), SystemError> {
4343
fn dhcp_query() -> Result<(), SystemError> {
4444
let binding = NET_DEVICES.write_irqsave();
4545

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

4850
drop(binding);
4951

user/apps/test_lo/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
Cargo.lock
3+
/install/

user/apps/test_lo/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "test_lo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "测试lo网卡功能"
6+
authors = [ "smallc <[email protected]>" ]
7+

user/apps/test_lo/Makefile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
TOOLCHAIN=
2+
RUSTFLAGS=
3+
4+
ifdef DADK_CURRENT_BUILD_DIR
5+
# 如果是在dadk中编译,那么安装到dadk的安装目录中
6+
INSTALL_DIR = $(DADK_CURRENT_BUILD_DIR)
7+
else
8+
# 如果是在本地编译,那么安装到当前目录下的install目录中
9+
INSTALL_DIR = ./install
10+
endif
11+
12+
ifeq ($(ARCH), x86_64)
13+
export RUST_TARGET=x86_64-unknown-linux-musl
14+
else ifeq ($(ARCH), riscv64)
15+
export RUST_TARGET=riscv64gc-unknown-linux-gnu
16+
else
17+
# 默认为x86_86,用于本地编译
18+
export RUST_TARGET=x86_64-unknown-linux-musl
19+
endif
20+
21+
run:
22+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET)
23+
24+
build:
25+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET)
26+
27+
clean:
28+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET)
29+
30+
test:
31+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET)
32+
33+
doc:
34+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) doc --target $(RUST_TARGET)
35+
36+
fmt:
37+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt
38+
39+
fmt-check:
40+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt --check
41+
42+
run-release:
43+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET) --release
44+
45+
build-release:
46+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET) --release
47+
48+
clean-release:
49+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET) --release
50+
51+
test-release:
52+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET) --release
53+
54+
.PHONY: install
55+
install:
56+
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) install --target $(RUST_TARGET) --path . --no-track --root $(INSTALL_DIR) --force

user/apps/test_lo/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# test-lo
2+
3+
lo网卡功能测试程序
4+
5+
## 测试过程:
6+
7+
通过创建一个UDP套接字,然后发送一条消息到本地回环地址127.0.0.1(lo网卡),再接收并验证这条消息,以此来测试lo网卡的功能。期望发送的消息和接收到的消息是完全一样的。通过日志输出查看测试是否成功。

user/apps/test_lo/src/main.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::net::UdpSocket;
2+
use std::str;
3+
4+
fn main() -> std::io::Result<()> {
5+
let socket = UdpSocket::bind("127.0.0.1:34254")?;
6+
socket.connect("127.0.0.1:34254")?;
7+
8+
let msg = "Hello, loopback!";
9+
socket.send(msg.as_bytes())?;
10+
11+
let mut buf = [0; 1024];
12+
let (amt, _src) = socket.recv_from(&mut buf)?;
13+
14+
let received_msg = str::from_utf8(&buf[..amt]).expect("Could not read buffer as UTF-8");
15+
16+
println!("Sent: {}", msg);
17+
println!("Received: {}", received_msg);
18+
19+
assert_eq!(
20+
msg, received_msg,
21+
"The sent and received messages do not match!"
22+
);
23+
24+
Ok(())
25+
}

user/dadk/config/test_lo_0_1_0.dadk

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "test_lo",
3+
"version": "0.1.0",
4+
"description": "test for lo interface",
5+
"rust_target": null,
6+
"task_type": {
7+
"BuildFromSource": {
8+
"Local": {
9+
"path": "apps/test_lo"
10+
}
11+
}
12+
},
13+
"depends": [],
14+
"build": {
15+
"build_command": "make install"
16+
},
17+
"install": {
18+
"in_dragonos_path": "/"
19+
},
20+
"clean": {
21+
"clean_command": "make clean"
22+
},
23+
"envs": [],
24+
"build_once": false,
25+
"install_once": false,
26+
"target_arch": ["x86_64"]
27+
}

0 commit comments

Comments
 (0)