Skip to content

Commit

Permalink
fix: 修好了RA的Bug,提供了uevent机制 (#897)
Browse files Browse the repository at this point in the history
* add netlink todo directory

* save the work 2 another system workplace

* save the work 2 another system workplace

* add the demo of netlink_create and locate the kobject_uevent\kobject_uevent_env

* add the uevent directory and demo of kobject_uevent and kobject_uevent_env

* delete sth else in target

* handle existing errors, ready for the next period of dev

* 阶段性提交,完成了kobjectuevent_env的部分逻辑,接下来需要实现uevent_ops结构体的转写

* 调整uevent_suppress和subsystem,修改分支日志打印信息

* 调整uevent_suppress和subsystem,修改分支日志打印信息

* 在kset中引入了KsetUeventOps的trait,待实现

* 阶段性提交, 基本完成了处理kset的uevent_ops相关逻辑,新增一个trait和一个结构体,正在逐步完善kobject_uevent_env

* 阶段性提交:完善大部分kobject_uevent_env函数的功能

* add_uevent_var,kobject_uevent_net_broadcast,zap_modalias_env,完善其他细节的逻辑

* 阶段性提交,开始逐渐深入与netlink

* 阶段性提交,重构netlinktable和netlinksocket

* replace kdebug with log::info!

* 理清了sock和ueventsock的关系,重构了一部分原来的代码

* 阶段性提交,新增了几个函数,进一步完善了netlink

* 阶段性提交,新增了Sk_Buff封装了一层PacketBuffer,需要解决mc_list迭代器实现方案和PacketBuffer提供的接口不足的问题

* 初步解决mc_list迭代问题和consume_skb

* fmt

* 同步更改

* asy to antoher workplace

* 初步解决现有代码中所有权和生命周期等问题

* fix nlk_sk and init test for uevent

* 初步在driver中成功调用kobject_uevent()进行测试,标记需要发送uevent的地方

* ipml socket for netlinksock

* 阶段性提交,完善netlink_proto相关内容

* 阶段性提交,初步实现netlink_add_usersock_entry等

* 实现netlink_proto_init()

* add NetProtoFamily and NetlinkFamulyOps

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

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

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

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

* netlink_insert大体框架,遇到nlk无法修改的问题

* 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/

* 完善netlink_insert 和 netlink_lookup

* 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

* 考虑更换Sk_Buffer实现,留档原有方案

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

* feat(fs): add eventfd syscall support

* 更换Sk_Buffer实现

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

* 需要解决Arc内部可变性问题

* 解决sk:Arc内部可变性问题

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

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

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

* 基本实现netlink单播,todo回调函数

* remove unused import

* 考虑是否引入回调函数,补充sockflags

* updates test-uevent

* add endpoint and complete to_endpoint function for NetLinkEndpoint

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

* 初步实现Cred

* 添加seteuid和setegid

* 添加cred测试程序

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

* 完善root用户相关信息

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

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

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

* success to call NetlinkSock bind fn and add a fn into Socket trait

* modify handle in NetlinkSock

* fix af_netlink problems in the new branch reported by RA

---------

Co-authored-by: val213 <[email protected]>
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]>
  • Loading branch information
8 people authored Aug 26, 2024
1 parent 541ce64 commit ededc46
Show file tree
Hide file tree
Showing 37 changed files with 3,459 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,8 @@
"check",

],
"rust-analyzer.showUnlinkedFileNotification": false,
"files.autoSave": "onFocusChange",
"editor.fontSize": 17,
"editor.accessibilityPageSize": 15,
}
10 changes: 8 additions & 2 deletions kernel/src/driver/acpi/sysfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use log::{debug, error, warn};
use system_error::SystemError;

use super::{acpi_kset, AcpiManager};

use crate::driver::base::uevent::kobject_uevent::kobject_uevent;
use crate::driver::base::uevent::KobjectAction;
static mut __HOTPLUG_KSET_INSTANCE: Option<Arc<KSet>> = None;
static mut __ACPI_TABLES_KSET_INSTANCE: Option<Arc<KSet>> = None;
static mut __ACPI_TABLES_DATA_KSET_INSTANCE: Option<Arc<KSet>> = None;
Expand Down Expand Up @@ -115,7 +116,12 @@ impl AcpiManager {
acpi_table_attr_list().write().push(attr);
self.acpi_table_data_init(&header)?;
}

// TODO:UEVENT
unsafe{
let _ = kobject_uevent(acpi_tables_kset.clone() as Arc<dyn KObject>, KobjectAction::KOBJADD);
let _ = kobject_uevent( __ACPI_TABLES_DATA_KSET_INSTANCE.as_ref().map(|kset| kset.clone() as Arc<dyn KObject>).unwrap(), KobjectAction::KOBJADD);
let _ = kobject_uevent(__ACPI_TABLES_DYNAMIC_KSET_INSTANCE.as_ref().map(|kset| kset.clone() as Arc<dyn KObject>).unwrap(), KobjectAction::KOBJADD);
}
return Ok(());
}

Expand Down
1 change: 1 addition & 0 deletions kernel/src/driver/base/device/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ impl DriverManager {
}

// todo: 发送kobj bind的uevent
// kobject_uevent();
}

fn driver_is_bound(&self, device: &Arc<dyn Device>) -> bool {
Expand Down
7 changes: 5 additions & 2 deletions kernel/src/driver/base/device/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::{
bus::{bus_manager, Bus},
Device, DeviceMatchName, DeviceMatcher, IdTable,
};
use crate::driver::base::uevent::kobject_uevent::kobject_uevent;
use crate::driver::base::uevent::KobjectAction;
use crate::{
driver::base::{
device::{bus::BusNotifyEvent, dd::DeviceAttrCoredump, device_manager},
Expand All @@ -17,7 +19,6 @@ use alloc::{
use core::fmt::Debug;
use log::error;
use system_error::SystemError;

/// @brief: Driver error
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -218,7 +219,9 @@ impl DriverManager {
bus_manager().remove_driver(&driver);
})?;

// todo: 发送uevent
// todo: 发送uevent,类型问题
let _ = kobject_uevent(driver.clone() as Arc<dyn KObject>, KobjectAction::KOBJADD);
// deferred_probe_extend_timeout();

return Ok(());
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/driver/base/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ impl DeviceManager {
}

// todo: 发送uevent: KOBJ_ADD

// kobject_uevent();
// probe drivers for a new device
bus_probe_device(&device);

Expand Down
71 changes: 67 additions & 4 deletions kernel/src/driver/base/kobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::{any::Any, fmt::Debug, hash::Hash, ops::Deref};

use alloc::{
boxed::Box,
string::String,
sync::{Arc, Weak},
};
Expand All @@ -21,7 +22,7 @@ use crate::{

use system_error::SystemError;

use super::kset::KSet;
use super::{kset::KSet, uevent::kobject_uevent};

pub trait KObject: Any + Send + Sync + Debug + CastFromSync {
fn as_any_ref(&self) -> &dyn core::any::Any;
Expand Down Expand Up @@ -104,9 +105,7 @@ bitflags! {
const REMOVE_UEVENT_SENT = 1 << 2;
const INITIALIZED = 1 << 3;
}

}

#[derive(Debug)]
pub struct LockedKObjectState(RwLock<KObjectState>);

Expand Down Expand Up @@ -252,7 +251,7 @@ impl KObjectManager {
}

// todo: 发送uevent: KOBJ_REMOVE

// kobject_uevent();
sysfs_instance().remove_dir(&kobj);
kobj.update_kobj_state(None, Some(KObjectState::IN_SYSFS));
let kset = kobj.kset();
Expand All @@ -261,6 +260,70 @@ impl KObjectManager {
}
kobj.set_parent(None);
}

fn get_kobj_path_length(kobj: &Arc<dyn KObject>) -> usize {
let mut length = 1;
let mut parent = kobj.parent().unwrap().upgrade().unwrap();
/* walk up the ancestors until we hit the one pointing to the
* root.
* Add 1 to strlen for leading '/' of each level.
*/
loop {
if parent.name().is_empty() {
break;
}
length += parent.name().len() + 1;
if let Some(weak_parent) = parent.parent() {
parent = weak_parent.upgrade().unwrap();
}
}
return length;
}

/*
static void fill_kobj_path(struct kobject *kobj, char *path, int length)
{
struct kobject *parent;
--length;
for (parent = kobj; parent; parent = parent->parent) {
int cur = strlen(kobject_name(parent));
/* back up enough to print this name with '/' */
length -= cur;
memcpy(path + length, kobject_name(parent), cur);
*(path + --length) = '/';
}
pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
kobj, __func__, path);
}
*/
fn fill_kobj_path(kobj: &Arc<dyn KObject>, path: *mut u8, length: usize) {
let mut parent = kobj.parent().unwrap().upgrade().unwrap();
let mut length = length;
length -= 1;
loop {
let cur = parent.name().len();
length -= cur;
unsafe {
core::ptr::copy_nonoverlapping(parent.name().as_ptr(), path.add(length), cur);
*path.add(length - 1) = b'/';
}
if let Some(weak_parent) = parent.parent() {
parent = weak_parent.upgrade().unwrap();
}
}
}
// TODO: 实现kobject_get_path
// https://code.dragonos.org.cn/xref/linux-6.1.9/lib/kobject.c#139
pub fn kobject_get_path(kobj: &Arc<dyn KObject>) -> String {
let length = Self::get_kobj_path_length(kobj);
let path_raw = vec![0u8; length].into_boxed_slice();
let path = Box::into_raw(path_raw) as *mut u8;
Self::fill_kobj_path(kobj, path, length);
let path_string = unsafe { String::from_raw_parts(path, length, length) };
path_string
}
}

/// 动态创建的kobject对象的ktype
Expand Down
34 changes: 32 additions & 2 deletions kernel/src/driver/base/kset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use alloc::{

use core::hash::Hash;

use super::kobject::{
DynamicKObjKType, KObjType, KObject, KObjectManager, KObjectState, LockedKObjectState,
use super::{
kobject::{
DynamicKObjKType, KObjType, KObject, KObjectManager, KObjectState, LockedKObjectState,
},
uevent::KobjUeventEnv,
};
use crate::{
filesystem::kernfs::KernFSInode,
Expand All @@ -26,6 +29,8 @@ pub struct KSet {
/// 与父节点有关的一些信息
parent_data: RwLock<KSetParentData>,
self_ref: Weak<KSet>,
/// kset用于发送uevent的操作函数集。kset能够发送它所包含的各种子kobj、孙kobj的消息,即kobj或其父辈、爷爷辈,都可以发送消息;优先父辈,然后是爷爷辈,以此类推
pub uevent_ops: Option<Arc<dyn KSetUeventOps>>,
}

impl Hash for KSet {
Expand All @@ -51,6 +56,7 @@ impl KSet {
kobj_state: LockedKObjectState::new(None),
parent_data: RwLock::new(KSetParentData::new(None, None)),
self_ref: Weak::default(),
uevent_ops: Some(Arc::new(KSetUeventOpsDefault)),
};

let r = Arc::new(r);
Expand Down Expand Up @@ -91,6 +97,7 @@ impl KSet {
pub fn register(&self, join_kset: Option<Arc<KSet>>) -> Result<(), SystemError> {
return KObjectManager::add_kobj(self.self_ref.upgrade().unwrap(), join_kset);
// todo: 引入uevent之后,发送uevent
// kobject_uevent();
}

/// 注销一个kset
Expand Down Expand Up @@ -232,3 +239,26 @@ impl InnerKSet {
}
}
}
//https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/kobject.h#137
use core::fmt::Debug;
pub trait KSetUeventOps: Debug + Send + Sync {
fn filter(&self) -> Option<i32>;
fn uevent_name(&self) -> String;
fn uevent(&self, env: &KobjUeventEnv) -> i32;
}
#[derive(Debug)]
pub struct KSetUeventOpsDefault;

impl KSetUeventOps for KSetUeventOpsDefault {
fn filter(&self) -> Option<i32> {
Some(0)
}

fn uevent_name(&self) -> String {
String::new()
}

fn uevent(&self, env: &KobjUeventEnv) -> i32 {
0
}
}
1 change: 1 addition & 0 deletions kernel/src/driver/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod map;
pub mod platform;
pub mod subsys;
pub mod swnode;
pub mod uevent;
Loading

0 comments on commit ededc46

Please sign in to comment.