Skip to content

Commit

Permalink
feat(net): 重构工作中期 (#895)
Browse files Browse the repository at this point in the history
* feat: add options for socket

* feat: print prettier

* add cloud-seed

* feat: improve debug logging for syscall_handler

* 修复mprotect系统调用未正确设置vm_flags的错误 (#847)

* fix(time): modify update wall time (#836)

更改了时间子系统的update_wall_time函数,通过读取当前周期数,计算delta值进行更新,而不是通过传入delta值进行更新

* chore: 调整triagebot.toml以适应新的组织架构 (#848)

* doc: 完善README.md (#849)

* doc: 完善README.md

* chore: 更新sphinx相关配置,适应read the docs的更新 (#850)

根据read the docs在7月15日blog,进行此修改

https://about.readthedocs.com/blog/2024/07/addons-by-default/

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

* 初步实现loopback设备

* fix: build-scripts和tools目录下的make check指定工具链版本 (#861)

* fix: tcp poll没有正确处理posix socket的listen状态的问题 (#859)

* chore: 将工具链更新到2024-07-23 (#864)

* chore: 将工具链更新到2024-07-23

* remove uneccecary import

* feat(fs): add eventfd syscall support (#858)

* feat(fs): add eventfd syscall support

* refactor: 删除过时的va-pa转换函数,改为统一使用MMArch (#862)

* 默认nightly-2024-07-23 & config改为config.toml (#872)

* fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。 (#870)

* fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。

* make some updates

* chore: 将工具链更新到2024-07-23 (#864)

* chore: 将工具链更新到2024-07-23

* feat(fs): add eventfd syscall support (#858)

* feat(fs): add eventfd syscall support

* refactor: 删除过时的va-pa转换函数,改为统一使用MMArch (#862)

* 默认nightly-2024-07-23 & config改为config.toml (#872)

* fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。 (#870)

* fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。

* feat(cred): 初步实现Cred (#846)

* 初步实现Cred

* 添加seteuid和setegid

* 添加cred测试程序

* 修改Cred::fscmp返回结果为CredFsCmp枚举

* 完善root用户相关信息

* feat: alloc 127.0.0.2 ~ 127.0.0.8 to loopback device

* chore: 将工具链更新到2024-07-23 (#864)

* chore: 将工具链更新到2024-07-23

* fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。 (#870)

* fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。

* feat(cred): 初步实现Cred (#846)

* 初步实现Cred

* 添加seteuid和setegid

* 添加cred测试程序

* 修改Cred::fscmp返回结果为CredFsCmp枚举

* 完善root用户相关信息

* allow deadcode

* allign syscall print

* change iface (origin netdevice)

* dev: rebuild net day1

* fix: 修复键盘码解析器没能正确处理类似ctrl C的控制字符的问题 (#877)

* fix: 修复键盘码解析器没能正确处理类似ctrl C的控制字符的问题

* fix: 解决ntty潜在的panic问题

* 重写datagram,构建inet socket底层

* feat: add tcp, add udp poll

* feat: redefine socket interface

* ci: enable ci workflow on branches other than master (#891)

* 修复unlink、unlinkat系统调用的路径错误 (#892)

* fix: socket shutdown wrong implement (#893)

* implment methods with poll_iface, and complete udp tcp poll.

* feat: add tcp connecting middle state

* fix: correct tcp connect state machine

* feat: tcp connect check

* feat: add inet socket iface

* fix net iface poll

---------

Co-authored-by: MemoryShore <[email protected]>
Co-authored-by: 黄铭涛 <[email protected]>
Co-authored-by: LoGin <[email protected]>
Co-authored-by: SMALLC <[email protected]>
Co-authored-by: linfeng <[email protected]>
Co-authored-by: Jomo <[email protected]>
Co-authored-by: Chiichen <[email protected]>
  • Loading branch information
8 people authored Aug 16, 2024
1 parent f956080 commit 541ce64
Show file tree
Hide file tree
Showing 38 changed files with 4,243 additions and 3,100 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Build Check

on:
push:
branches: [ "master" ]
branches: [ "master", "feat-*", "fix-*"]
pull_request:
branches: [ "master" ]
branches: [ "master", "feat-*", "fix-*"]

jobs:
# ensure the toolchain is cached
Expand Down
69 changes: 14 additions & 55 deletions kernel/src/driver/net/e1000e/e1000e_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
device::{bus::Bus, driver::Driver, Device, DeviceType, IdTable},
kobject::{KObjType, KObject, KObjectState},
},
net::NetDevice,
net::{Iface, IfaceCommon},
},
libs::spinlock::SpinLock,
net::{generate_iface_id, NET_DEVICES},
Expand All @@ -26,9 +26,9 @@ use core::{
use log::info;
use smoltcp::{
phy,
wire::{self, HardwareAddress},
wire::HardwareAddress,
};
use system_error::SystemError;
// use system_error::SystemError;

use super::e1000e::{E1000EBuffer, E1000EDevice};

Expand Down Expand Up @@ -73,10 +73,10 @@ impl Debug for E1000EDriverWrapper {
}
}

#[derive(Debug)]
pub struct E1000EInterface {
driver: E1000EDriverWrapper,
iface_id: usize,
iface: SpinLock<smoltcp::iface::Interface>,
common: IfaceCommon,
name: String,
}
impl phy::RxToken for E1000ERxToken {
Expand Down Expand Up @@ -184,28 +184,16 @@ impl E1000EInterface {
let iface =
smoltcp::iface::Interface::new(iface_config, &mut driver, Instant::now().into());

let driver: E1000EDriverWrapper = E1000EDriverWrapper(UnsafeCell::new(driver));
let result = Arc::new(E1000EInterface {
driver,
iface_id,
iface: SpinLock::new(iface),
driver: E1000EDriverWrapper(UnsafeCell::new(driver)),
common: IfaceCommon::new(iface_id, iface),
name: format!("eth{}", iface_id),
});

return result;
}
}

impl Debug for E1000EInterface {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("E1000EInterface")
.field("iface_id", &self.iface_id)
.field("iface", &"smoltcp::iface::Interface")
.field("name", &self.name)
.finish()
}
}

impl Device for E1000EInterface {
fn dev_type(&self) -> DeviceType {
todo!()
Expand Down Expand Up @@ -248,52 +236,23 @@ impl Device for E1000EInterface {
}
}

impl NetDevice for E1000EInterface {
impl Iface for E1000EInterface {
fn common(&self) -> &IfaceCommon {
return &self.common;
}

fn mac(&self) -> smoltcp::wire::EthernetAddress {
let mac = self.driver.inner.lock().mac_address();
return smoltcp::wire::EthernetAddress::from_bytes(&mac);
}

#[inline]
fn nic_id(&self) -> usize {
return self.iface_id;
}

#[inline]
fn name(&self) -> String {
return self.name.clone();
}

fn update_ip_addrs(&self, ip_addrs: &[wire::IpCidr]) -> Result<(), SystemError> {
if ip_addrs.len() != 1 {
return Err(SystemError::EINVAL);
}

self.iface.lock().update_ip_addrs(|addrs| {
let dest = addrs.iter_mut().next();

if let Some(dest) = dest {
*dest = ip_addrs[0];
} else {
addrs.push(ip_addrs[0]).expect("Push ipCidr failed: full");
}
});
return Ok(());
}

fn poll(&self, sockets: &mut smoltcp::iface::SocketSet) -> Result<(), SystemError> {
let timestamp: smoltcp::time::Instant = Instant::now().into();
let mut guard = self.iface.lock();
let poll_res = guard.poll(timestamp, self.driver.force_get_mut(), sockets);
if poll_res {
return Ok(());
}
return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
}

#[inline(always)]
fn inner_iface(&self) -> &SpinLock<smoltcp::iface::Interface> {
return &self.iface;
fn poll(&self) {
self.common.poll(self.driver.force_get_mut())
}
}

Expand Down
7 changes: 5 additions & 2 deletions kernel/src/driver/net/irq_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
irqdesc::{IrqHandler, IrqReturn},
IrqNumber,
},
net::net_core::poll_ifaces_try_lock_onetime,
// net::net_core::poll_ifaces_try_lock_onetime,
net::net_core::poll_ifaces,
};

/// 默认的网卡中断处理函数
Expand All @@ -21,7 +22,9 @@ impl IrqHandler for DefaultNetIrqHandler {
_static_data: Option<&dyn IrqHandlerData>,
_dynamic_data: Option<Arc<dyn IrqHandlerData>>,
) -> Result<IrqReturn, SystemError> {
poll_ifaces_try_lock_onetime().ok();
// poll_ifaces_try_lock_onetime().ok();
log::warn!("DefaultNetIrqHandler: poll_ifaces_try_lock_onetime -> poll_ifaces");
poll_ifaces();
Ok(IrqReturn::Handled)
}
}
100 changes: 20 additions & 80 deletions kernel/src/driver/net/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use smoltcp::{
};
use system_error::SystemError;

use super::NetDevice;
use super::{Iface, IfaceCommon};

const DEVICE_NAME: &str = "loopback";

Expand Down Expand Up @@ -131,6 +131,7 @@ impl Loopback {
/// 为实现获得不可变引用的Interface的内部可变性,故为Driver提供UnsafeCell包裹器
///
/// 参考virtio_net.rs
#[derive(Debug)]
struct LoopbackDriverWapper(UnsafeCell<LoopbackDriver>);
unsafe impl Send for LoopbackDriverWapper {}
unsafe impl Sync for LoopbackDriverWapper {}
Expand Down Expand Up @@ -235,11 +236,10 @@ impl phy::Device for LoopbackDriver {

/// ## LoopbackInterface结构
/// 封装驱动包裹器和iface,设置接口名称
#[derive(Debug)]
pub struct LoopbackInterface {
driver: LoopbackDriverWapper,
iface_id: usize,
iface: SpinLock<smoltcp::iface::Interface>,
name: String,
common: IfaceCommon,
}

impl LoopbackInterface {
Expand Down Expand Up @@ -270,25 +270,14 @@ impl LoopbackInterface {
.expect("Push ipCidr failed: full");
}
});
let driver = LoopbackDriverWapper(UnsafeCell::new(driver));

Arc::new(LoopbackInterface {
driver,
iface_id,
iface: SpinLock::new(iface),
name: "lo".to_string(),
driver: LoopbackDriverWapper(UnsafeCell::new(driver)),
common: IfaceCommon::new(iface_id, iface),
})
}
}

impl Debug for LoopbackInterface {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("LoopbackInterface")
.field("iface_id", &self.iface_id)
.field("iface", &"smtoltcp::iface::Interface")
.field("name", &self.name)
.finish()
}
}
//TODO: 向sysfs注册lo设备
impl KObject for LoopbackInterface {
fn as_any_ref(&self) -> &dyn core::any::Any {
Expand Down Expand Up @@ -324,7 +313,7 @@ impl KObject for LoopbackInterface {
}

fn name(&self) -> String {
self.name.clone()
"lo".to_string()
}

fn set_name(&self, _name: String) {
Expand Down Expand Up @@ -394,87 +383,38 @@ impl Device for LoopbackInterface {
}
}

impl NetDevice for LoopbackInterface {
/// 由于lo网卡设备不是实际的物理设备,其mac地址需要手动设置为一个默认值,这里默认为0200000001
fn mac(&self) -> smoltcp::wire::EthernetAddress {
let mac = [0x02, 0x00, 0x00, 0x00, 0x00, 0x01];
smoltcp::wire::EthernetAddress(mac)
}

#[inline]
fn nic_id(&self) -> usize {
self.iface_id
impl Iface for LoopbackInterface {
fn common(&self) -> &IfaceCommon {
&self.common
}

#[inline]
fn name(&self) -> String {
self.name.clone()
"lo".to_string()
}
/// ## `update_ip_addrs` 用于更新接口的 IP 地址。
///
/// ## 参数
/// - `&self` :自身引用
/// - `ip_addrs` :一个包含 `smoltcp::wire::IpCidr` 的切片,表示要设置的 IP 地址和子网掩码
///
/// ## 返回值
/// - 如果 `ip_addrs` 的长度不为 1,返回 `Err(SystemError::EINVAL)`,表示输入参数无效
/// - 如果更新成功,返回 `Ok(())`
fn update_ip_addrs(
&self,
ip_addrs: &[smoltcp::wire::IpCidr],
) -> Result<(), system_error::SystemError> {
if ip_addrs.len() != 1 {
return Err(SystemError::EINVAL);
}

self.iface.lock().update_ip_addrs(|addrs| {
let dest = addrs.iter_mut().next();

if let Some(dest) = dest {
*dest = ip_addrs[0];
} else {
addrs.push(ip_addrs[0]).expect("Push ipCidr failed: full");
}
});
return Ok(());
}
/// ## `poll` 用于轮询接口的状态。
///
/// ## 参数
/// - `&self` :自身引用
/// - `sockets` :一个可变引用到 `smoltcp::iface::SocketSet`,表示要轮询的套接字集
///
/// ## 返回值
/// - 如果轮询成功,返回 `Ok(())`
/// - 如果轮询失败,返回 `Err(SystemError::EAGAIN_OR_EWOULDBLOCK)`,表示需要再次尝试或者操作会阻塞
fn poll(&self, sockets: &mut smoltcp::iface::SocketSet) -> Result<(), SystemError> {
let timestamp: smoltcp::time::Instant = Instant::now().into();
let mut guard = self.iface.lock();
let poll_res = guard.poll(timestamp, self.driver.force_get_mut(), sockets);
if poll_res {
return Ok(());
}
return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
/// 由于lo网卡设备不是实际的物理设备,其mac地址需要手动设置为一个默认值,这里默认为0200000001
fn mac(&self) -> smoltcp::wire::EthernetAddress {
let mac = [0x02, 0x00, 0x00, 0x00, 0x00, 0x01];
smoltcp::wire::EthernetAddress(mac)
}

#[inline(always)]
fn inner_iface(&self) -> &SpinLock<smoltcp::iface::Interface> {
return &self.iface;
fn poll(&self) {
self.common.poll(self.driver.force_get_mut())
}
}

pub fn loopback_probe() {
loopback_driver_init();
}
/// ## lo网卡设备初始化函数
/// # lo网卡设备初始化函数
/// 创建驱动和iface,初始化一个lo网卡,添加到全局NET_DEVICES中
pub fn loopback_driver_init() {
let driver = LoopbackDriver::new();
let iface = LoopbackInterface::new(driver);

NET_DEVICES
.write_irqsave()
.insert(iface.iface_id, iface.clone());
.insert(iface.nic_id(), iface.clone());
}

/// ## lo网卡设备的注册函数
Expand Down
Loading

0 comments on commit 541ce64

Please sign in to comment.