Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(driver/net): 将网络设备注册到sysfs, 添加设备类属性文件 #919

Conversation

1037827920
Copy link
Contributor

本次PR的主要修改:

  • 全部设备相关:

    • 增加Device trait的dev_parent()和set_dev_parent()方法,以及在DeviceCommonData中添加了parent字段。这个跟kobject的parent还是不一样的,这个parent主要是指设备的父设备,而kobject的parent指的是设备在sysfs中的类似父目录这样的。原先的代码是将父设备设置到了kobject的parent,然后在add_device时才通过get_device_parent获取实际kobject的parent,但是这样没有相关的字段来存储device的parent了。
    • 给大部分设备的结构体添加了类型为DeviceCommonData和KObjectCommonData的字段,我觉得这样比较好,就是给设备添加共有的字段时不用去每个实现的设备结构体添加,有点麻烦
  • virtio设备相关

  • net设备相关:

    • 创建了/sys/class/net的实例并初始化了net子系统,主要文件:driver/net/class.rs
    • 实现了register_netdevice(),将网络设备注册到sysfs中
    • 创建了net设备的属性文件,目前实现了addr_assign_type/type/address/carrier/operstate
  • kobjec相关:主要是DeviceManager,添加了class_dir_create_and_add,在get_device_parent()中被调用,主要是为了判断该设备是否为设备类,然后在其父设备下创建对应的类目录,在该类目录中创建目标为设备的符号链接。如virtio0下的net类目录创建了eth1的符号链接:
    image-20240908222024905
    对于没有父设备的设备,比如lo,fbcon,tty0等在/sys/devices/virtual下创建类目录,然后链接到设备:
    image-20240908222235570

@github-actions github-actions bot added the enhancement New feature or request label Sep 8, 2024
@fslongjin fslongjin added the A-network Area: 网络子系统 label Sep 9, 2024
Copy link
Member

@Samuka007 Samuka007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好多attribute,没啥问题。
像是KObject的这些方法可以单独约束一条要求提供kobject引用的trait,然后其他method就可以直接在这上面调用,而不用每个结构都impl一遍了。
以及,一些Arc子trait转父trait,在较新的标准中似乎并不是能直接使用as转换的,尽管能通过编译:(E0605)。是否考虑使用intertrait的cast提供的转换?以及,这样是否会移入额外的运行时开销?

let virtio_index = VIRTIO_DEVICE_INDEX_MANAGER.alloc();
dev.set_virtio_device_index(virtio_index);
dev.set_device_name(format!("virtio{}", virtio_index.data()));
virtio_drv.probe(&dev)?;

device_manager().add_device(dev.clone() as Arc<dyn Device>)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

比如这种地方,在较新的RA版本中会报错,使用intertrait cast后可消除该报错:

Suggested change
device_manager().add_device(dev.clone() as Arc<dyn Device>)?;
device_manager().add_device(dev.cast::<dyn Device>().unwrap())?;

@Samuka007 Samuka007 merged commit 28fe4ad into DragonOS-Community:master Sep 11, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-network Area: 网络子系统 enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants