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: add ebpf support #912

Open
wants to merge 6 commits into
base: feat-ebpf
Choose a base branch
from

Conversation

Godones
Copy link
Contributor

@Godones Godones commented Sep 4, 2024

  • 实现bpf()一部分命令,包括几种基本map,相关的helper函数
  • 实现部分perf相关的数据结构
  • 暂时为文件实现简单mmap
  • 实现一个使用kprobe统计syscall 调用次数的ebpf程序

对eBPF支持程度(基本):

  • 简单的eBPF程序(没有指定特殊的Map)
  • 使用内核已经实现的Map的eBPF程序
  • 可以和kprobe配合使用
  • 内核Map相关的接口定义已经实现,添加新的Map较为简单

不支持的功能:

  • 区分不同的eBPF程序类型(Network/Cgroup)并限定可调用的helper函数集
  • 与内核其它跟踪机制配合(tracepoint)
  • 其它helper和Map

todo

  • 修改mmap,需要讨论,因为这个和块缓存层相关
  • 添加文档
  • 修复可能的错误

add basic bpf map type.
add basic perf type.
update kprobe impl.
add user app example.
@github-actions github-actions bot added the enhancement New feature or request label Sep 4, 2024
@Godones
Copy link
Contributor Author

Godones commented Sep 4, 2024

@Chiichen @fslongjin 我重新整理了ebpf的提交代码,麻烦两位看看。

@@ -0,0 +1 @@
((prog-mode . ((lsp-rust-analyzer-linked-projects . ["Cargo.toml" "syscall_ebpf-ebpf/Cargo.toml"]))))
Copy link
Member

Choose a reason for hiding this comment

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

这个多余的文件需要删掉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👌

Copy link
Member

@Chiichen Chiichen left a comment

Choose a reason for hiding this comment

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

@Godones 简单描述下这个 PR 之后对 eBPF 的支持到了什么程度(基本/可用/完整支持),以及支持/不支持哪些功能

Comment on lines 40 to 80
fn lookup_elem(&self, _key: &[u8]) -> Result<Option<&[u8]>> {
panic!("lookup_elem not implemented")
}
/// Update an element in the map.
///
/// See https://ebpf-docs.dylanreimerink.nl/linux/helper-function/bpf_map_update_elem/
fn update_elem(&mut self, _key: &[u8], _value: &[u8], _flags: u64) -> Result<()> {
panic!("update_elem not implemented")
}
/// Delete an element from the map.
///
/// See https://ebpf-docs.dylanreimerink.nl/linux/helper-function/bpf_map_delete_elem/
fn delete_elem(&mut self, _key: &[u8]) -> Result<()> {
panic!("delete_elem not implemented")
}
/// For each element in map, call callback_fn function with map,
/// callback_ctx and other map-specific parameters.
///
/// See https://ebpf-docs.dylanreimerink.nl/linux/helper-function/bpf_for_each_map_elem/
fn for_each_elem(&mut self, _cb: BpfCallBackFn, _ctx: &[u8], _flags: u64) -> Result<u32> {
panic!("for_each_elem not implemented")
}

/// Get the next key in the map. If key is None, get the first key.
///
/// Called from syscall
fn get_next_key(&self, _key: Option<&[u8]>, _next_key: &mut [u8]) -> Result<()> {
panic!("get_next_key not implemented")
}

/// Freeze the map.
///
/// It's useful for .rodata maps.
fn freeze(&self) -> Result<()> {
panic!("freeze not implemented")
}

/// Get the first value pointer.
fn first_value_ptr(&self) -> *const u8 {
panic!("value_ptr not implemented")
}
Copy link
Member

Choose a reason for hiding this comment

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

这里默认实现都是 panic,能确保上层不会调用未实现的方法吗,默认给空实现或者返回 SystemError::unsupported会比较安全吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是方便我初期实现debug都返回了panic,返回SystemError要合理一点

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我已经更新pr的描述,说明当前对eBPF程序的支持程度

kernel/src/bpf/map/mod.rs Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

这一块文件映射的代码跟现在主线的代码应该有点出入,到时候合并主线之前再处理吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@fslongjin
Copy link
Member

最好能在内核文档里面写几个文档,描述你的设计&方案

@Chiichen
Copy link
Member

Chiichen commented Sep 6, 2024

@Godones 交流了一下,会在下次或者下下次社区会议上分享这个 PR 的工作内容,等会议结束后再合并一次主线(应该主要是文件映射那边有一些冲突要解决),再开始 review 这个 PR

@Chiichen
Copy link
Member

@Godones 已经合并了最新的主线,麻烦解决一下冲突,对齐一下主线的实现,我记得在mmap的实现那部分(ucontext)逻辑是有冲突的

@Godones
Copy link
Contributor Author

Godones commented Sep 13, 2024

@Godones 已经合并了最新的主线,麻烦解决一下冲突,对齐一下主线的实现,我记得在mmap的实现那部分(ucontext)逻辑是有冲突的

可以

@Godones
Copy link
Contributor Author

Godones commented Sep 13, 2024

@Chiichen 似乎合并主线后无法通过CI?

@Chiichen
Copy link
Member

@Chiichen 似乎合并主线后无法通过CI?

是的,我看应该是格式检查挂了,因为我在网页上解决的冲突,所以可能没有仔细去格式化,你就基于现有的分支代码解决冲突就可以了,应该格式化之后 ci 是不会挂的

@dragonosbot dragonosbot added A-fs Area: 文件系统 O-riscv64 Target: riscv64 O-x86_64 Target: x86_64 labels Sep 16, 2024
@Godones
Copy link
Contributor Author

Godones commented Sep 16, 2024

@Chiichen 新的修改中主要是适配当前主线的mmap实现,以及修复了一些错误。由于主线的mmap并不能完全适用于所有文件的实现,我在IndexNode添加了一个mmap, 向文件传递一些信息。

@Chiichen
Copy link
Member

@Jomocool 麻烦帮忙看看mmap相关的修改

@dragonosbot dragonosbot added the T-doc Relevant to the document team, which will review and decide on the PR/issue. label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-fs Area: 文件系统 enhancement New feature or request O-riscv64 Target: riscv64 O-x86_64 Target: x86_64 T-doc Relevant to the document team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants