feat(driver/net): 实现Loopback网卡接口 (#845)

* 初步实现loopback设备
This commit is contained in:
SMALLC
2024-07-22 16:22:45 +08:00
committed by GitHub
parent ef2a79be60
commit 1ea2daad81
10 changed files with 613 additions and 5 deletions

3
user/apps/test_lo/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
Cargo.lock
/install/

View File

@ -0,0 +1,7 @@
[package]
name = "test_lo"
version = "0.1.0"
edition = "2021"
description = "测试lo网卡功能"
authors = [ "smallc <2628035541@qq.com>" ]

View File

@ -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

View File

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

View File

@ -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(())
}