Skip to content

Commit b1824fc

Browse files
authored
docs: translate Chinese comments to English in PR #2110 BPF filter files (#2140)
Translate all newly added Chinese doc comments and inline comments in kernel/src/bpf/classic.rs, kernel/src/net/socket/packet/mod.rs, and kernel/src/process/seccomp.rs to English for consistency with the codebase conventions.
1 parent 23247da commit b1824fc

3 files changed

Lines changed: 66 additions & 63 deletions

File tree

kernel/src/bpf/classic.rs

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
//! Classic BPF (cBPF) 解释器与验证器。
1+
//! Classic BPF (cBPF) interpreter and verifier.
22
//!
3-
//! 本模块只实现 cBPF 机器模型、结构验证和输入协议。packet 与 seccomp
4-
//! 的字节序、逻辑偏移和 ancillary 语义由各自的输入实现负责。
3+
//! This module only implements the cBPF machine model, structural validation,
4+
//! and the input protocol. Byte order, logical offsets, and ancillary semantics
5+
//! for packet and seccomp are handled by their respective input implementations.
56
67
use alloc::vec::Vec;
78
use system_error::SystemError;
@@ -10,7 +11,7 @@ use crate::syscall::user_access::UserBufferReader;
1011

1112
// ============ Sock Filter / Sock Fprog ============
1213

13-
/// Classic BPF 指令(对应 Linux `struct sock_filter`,8 字节)
14+
/// Classic BPF instruction (corresponds to Linux `struct sock_filter`, 8 bytes)
1415
#[repr(C)]
1516
#[derive(Debug, Clone, Copy)]
1617
pub struct SockFilter {
@@ -20,9 +21,9 @@ pub struct SockFilter {
2021
pub k: u32,
2122
}
2223

23-
/// `struct sock_fprog` —— 用户空间传入的 filter 容器。
24+
/// `struct sock_fprog` — userspace filter container.
2425
///
25-
/// 布局:`{ u16 len; u8 _pad[6]; u64 filter; }`(共 16 字节)。
26+
/// Layout: `{ u16 len; u8 _pad[6]; u64 filter; }` (16 bytes total).
2627
#[repr(C)]
2728
#[derive(Debug, Clone, Copy)]
2829
pub struct SockFprog {
@@ -31,10 +32,10 @@ pub struct SockFprog {
3132
pub filter: u64,
3233
}
3334

34-
// ============ BPF 指令常量 ============
35-
// 参考 Linux include/uapi/linux/filter.h
35+
// ============ BPF Instruction Constants ============
36+
// Reference: Linux include/uapi/linux/filter.h
3637

37-
/// 指令类(code & 0x07
38+
/// Instruction class (code & 0x07)
3839
pub const BPF_LD: u16 = 0x00;
3940
pub const BPF_LDX: u16 = 0x01;
4041
pub const BPF_ST: u16 = 0x02;
@@ -44,27 +45,27 @@ pub const BPF_JMP: u16 = 0x05;
4445
pub const BPF_RET: u16 = 0x06;
4546
pub const BPF_MISC: u16 = 0x07;
4647

47-
/// 加载宽度(code & 0x18,仅对 LD/LDX 有意义)
48+
/// Load width (code & 0x18, meaningful only for LD/LDX)
4849
pub const BPF_W: u16 = 0x00;
4950
pub const BPF_H: u16 = 0x08;
5051
pub const BPF_B: u16 = 0x10;
5152

52-
/// 寻址模式(code & 0xe0,仅对 LD/LDX 有意义)
53+
/// Addressing mode (code & 0xe0, meaningful only for LD/LDX)
5354
pub const BPF_IMM: u16 = 0x00;
5455
pub const BPF_ABS: u16 = 0x20;
5556
pub const BPF_IND: u16 = 0x40;
5657
pub const BPF_MEM: u16 = 0x60;
5758
pub const BPF_LEN: u16 = 0x80;
58-
/// BPF_MSH:仅用于 `LDX|BPF_B|BPF_MSH`,提取 IP 头长度
59-
/// `x = (data[k] & 0xf) << 2`)。
59+
/// BPF_MSH: only used for `LDX|BPF_B|BPF_MSH`, extracts IP header length
60+
/// (`x = (data[k] & 0xf) << 2`).
6061
pub const BPF_MSH: u16 = 0xa0;
6162

62-
/// 数据源(code & 0x08,仅对 ALU/JMP 有意义)
63+
/// Data source (code & 0x08, meaningful only for ALU/JMP)
6364
pub const BPF_K: u16 = 0x00;
6465
pub const BPF_X: u16 = 0x08;
6566
pub const BPF_A: u16 = 0x10;
6667

67-
/// ALU 操作(code & 0xf0
68+
/// ALU operation (code & 0xf0)
6869
pub const BPF_ADD: u16 = 0x00;
6970
pub const BPF_SUB: u16 = 0x10;
7071
pub const BPF_MUL: u16 = 0x20;
@@ -77,24 +78,24 @@ pub const BPF_NEG: u16 = 0x80;
7778
pub const BPF_MOD: u16 = 0x90;
7879
pub const BPF_XOR: u16 = 0xa0;
7980

80-
/// JMP 操作(code & 0xf0
81+
/// JMP operation (code & 0xf0)
8182
pub const BPF_JA: u16 = 0x00;
8283
pub const BPF_JEQ: u16 = 0x10;
8384
pub const BPF_JGT: u16 = 0x20;
8485
pub const BPF_JGE: u16 = 0x30;
8586
pub const BPF_JSET: u16 = 0x40;
8687

87-
/// MISC 操作(code & 0xf8
88+
/// MISC operation (code & 0xf8)
8889
pub const BPF_TAX: u16 = 0x00;
8990
pub const BPF_TXA: u16 = 0x80;
9091

91-
/// BPF 程序最大指令数(Linux 限制)
92+
/// Maximum BPF program instruction count (Linux limit)
9293
pub const BPF_MAXINSNS: usize = 4096;
9394

94-
/// BPF 记忆体大小(16 个 u32 字)
95+
/// BPF memory size (16 u32 words)
9596
pub const BPF_MEMWORDS: usize = 16;
9697

97-
/// Linux socket filter ancillary 偏移。
98+
/// Linux socket filter ancillary offset.
9899
pub const SKF_AD_OFF: u32 = (-0x1000i32) as u32;
99100
pub const SKF_AD_PROTOCOL: u32 = 0;
100101
pub const SKF_AD_PKTTYPE: u32 = 4;
@@ -122,7 +123,7 @@ pub enum BpfWidth {
122123
Byte,
123124
}
124125

125-
/// cBPF 的调用域输入。实现必须自行定义普通加载的字节序和负偏移语义。
126+
/// cBPF call-domain input. Implementations must define their own byte order and negative offset semantics for normal loads.
126127
pub trait ClassicBpfInput {
127128
fn len(&self) -> u32;
128129

@@ -157,26 +158,26 @@ impl ClassicBpfInput for [u8] {
157158
}
158159
}
159160

160-
// ============ cBPF 解释器 ============
161+
// ============ cBPF Interpreter ============
161162

162-
/// 运行 classic BPF 程序。
163+
/// Run a classic BPF program.
163164
///
164-
/// cBPF 寄存器模型:
165-
/// - A (u32): 累加器
166-
/// - X (u32): 索引寄存器
167-
/// - pc: 程序计数器
168-
/// - mem\[16\]: 记忆体
165+
/// cBPF register model:
166+
/// - A (u32): accumulator
167+
/// - X (u32): index register
168+
/// - pc: program counter
169+
/// - mem\[16\]: scratch memory
169170
///
170-
/// # 加载语义
171-
/// - `BPF_LD|BPF_W/H/B|BPF_ABS`: input 按调用域语义加载
172-
/// - `BPF_LD|*|BPF_IND`: 同上,但 offset = X + k
173-
/// - 越界读取(offset + size > data.len()):立即返回 0
171+
/// # Load semantics
172+
/// - `BPF_LD|BPF_W/H/B|BPF_ABS`: loaded by input per call-domain semantics
173+
/// - `BPF_LD|*|BPF_IND`: same as above, but offset = X + k
174+
/// - Out-of-bounds read (offset + size > data.len()): immediately return 0
174175
/// - `BPF_LEN`: A = data.len() as u32
175176
///
176-
/// # 返回值
177-
/// - 正常返回 `RET` 指令的值(返回截断长度,0 = 丢弃)
178-
/// - `DIV`/`MOD` 除数为 0:立即返回 0
179-
/// - fall-through(无 RET 结尾):返回 0(丢弃)
177+
/// # Return value
178+
/// - Normal: value of the `RET` instruction (snap length; 0 = drop)
179+
/// - `DIV`/`MOD` divisor is 0: immediately return 0
180+
/// - Fall-through (no RET at end): return 0 (drop)
180181
pub fn run_cbpf<I: ClassicBpfInput + ?Sized>(insns: &[SockFilter], input: &I) -> u32 {
181182
let mut a: u32 = 0;
182183
let mut x: u32 = 0;
@@ -317,7 +318,7 @@ pub fn run_cbpf<I: ClassicBpfInput + ?Sized>(insns: &[SockFilter], input: &I) ->
317318
pc += 1;
318319
}
319320

320-
// fall-through:返回 0(丢弃)
321+
// fall-through: return 0 (drop)
321322
0
322323
}
323324

@@ -336,18 +337,18 @@ fn is_ancillary(offset: u32) -> bool {
336337
offset >= SKF_AD_OFF
337338
}
338339

339-
// ============ cBPF 验证器 ============
340+
// ============ cBPF Verifier ============
340341

341-
/// 验证 cBPF 程序的合法性(通用检查)。
342+
/// Validate the legality of a cBPF program (generic checks).
342343
///
343-
/// 检查项:
344-
/// 1. 非空
344+
/// Checks:
345+
/// 1. Non-empty
345346
/// 2. len <= BPF_MAXINSNS (4096)
346-
/// 3. 末条指令 class == BPF_RET
347-
/// 4. 跳转目标 `checked_add`(溢出 → EINVAL),且 < insns.len()
348-
/// 5. ST/STX k < BPF_MEMWORDS (16)
349-
/// 6. ALU|DIV|K k != 0ALU|MOD|K k != 0(静态拒绝)
350-
/// 7. opcode class 在白名单内(LD 允许 IMM/ABS/IND/MEM/LENLDX 允许 IMM/MEM/LEN/MSH,其中 MSH 仅限 BPF_B 宽度)
347+
/// 3. Last instruction class == BPF_RET
348+
/// 4. Jump target `checked_add` (overflow → EINVAL), and < insns.len()
349+
/// 5. ST/STX k < BPF_MEMWORDS (16)
350+
/// 6. ALU|DIV|K k != 0; ALU|MOD|K k != 0 (statically rejected)
351+
/// 7. Opcode class is whitelisted (LD allows IMM/ABS/IND/MEM/LEN; LDX allows IMM/MEM/LEN/MSH, where MSH is restricted to BPF_B width)
351352
pub fn validate_cbpf(insns: &[SockFilter]) -> Result<(), SystemError> {
352353
if insns.is_empty() {
353354
return Err(SystemError::EINVAL);
@@ -503,14 +504,14 @@ fn check_loads_and_stores(insns: &[SockFilter]) -> Result<(), SystemError> {
503504
Ok(())
504505
}
505506

506-
// ============ 用户空间解析 ============
507+
// ============ Userspace Parsing ============
507508

508-
/// 从 optval 字节切片解析 `sock_fprog` 结构并读取 filter 指令。
509+
/// Parse the `sock_fprog` structure from an optval byte slice and read filter instructions.
509510
///
510-
/// `optval` 必须精确包含一个 native `struct sock_fprog`
511-
/// 内部通过 `UserBufferReader` 安全地从用户空间读取 filter 指令数组。
511+
/// `optval` must contain exactly one native `struct sock_fprog`.
512+
/// Internally uses `UserBufferReader` to safely read the filter instruction array from userspace.
512513
///
513-
/// 返回已解析的 `Vec<SockFilter>`
514+
/// Returns the parsed `Vec<SockFilter>`.
514515
pub(crate) fn parse_sock_fprog(optval: &[u8]) -> Result<SockFprog, SystemError> {
515516
let fprog_size = core::mem::size_of::<SockFprog>();
516517
if optval.len() != fprog_size {
@@ -543,7 +544,7 @@ pub(crate) fn read_sock_fprog(optval: &[u8]) -> Result<Vec<SockFilter>, SystemEr
543544
read_sock_fprog_insns(&fprog)
544545
}
545546

546-
/// 从用户空间读取 filter 指令数组。
547+
/// Read the filter instruction array from userspace.
547548
fn read_filter_insns(ptr: u64, count: usize) -> Result<Vec<SockFilter>, SystemError> {
548549
let insn_size = core::mem::size_of::<SockFilter>();
549550
let byte_len = count.checked_mul(insn_size).ok_or(SystemError::EINVAL)?;

kernel/src/net/socket/packet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ pub struct PacketSocket {
113113
open_files: AtomicUsize,
114114
pub(super) self_ref: Weak<Self>,
115115
pub(super) netns: Arc<NetNamespace>,
116-
/// cBPF filter 程序;空指针是“未安装”的唯一事实来源。
116+
/// cBPF filter program; an empty slot is the single source of truth for “not installed”.
117117
pub(super) filter: RcuOptionArcSlot<Vec<SockFilter>>,
118-
/// 串行化 SO_ATTACH_FILTERSO_DETACH_FILTER SO_LOCK_FILTER
118+
/// Serializes SO_ATTACH_FILTER, SO_DETACH_FILTER, and SO_LOCK_FILTER.
119119
pub(super) filter_locked: Mutex<bool>,
120120
epoll_items: EPollItems,
121121
fasync_items: FAsyncItems,

kernel/src/process/seccomp.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,13 @@ fn native_u64_word(value: u64, index: usize) -> u32 {
225225

226226
// ============ BPF 验证器 ============
227227

228-
/// 验证 seccomp BPF 程序的合法性。
228+
/// Validate the legality of a seccomp BPF program.
229229
///
230-
/// 先调用通用 `validate_cbpf` 进行结构性检查,再叠加 seccomp 专有限制:
231-
/// 1. LD/LDX 只允许 BPF_W 宽度(seccomp 不使用 H/B)
232-
/// 2. 不允许 BPF_IND 变址加载
233-
/// 3. BPF_ABS 偏移必须在 SeccompData 范围内且 4 字节对齐
230+
/// First calls the generic `validate_cbpf` for structural checks, then applies
231+
/// seccomp-specific restrictions:
232+
/// 1. LD/LDX only allow BPF_W width (seccomp does not use H/B)
233+
/// 2. BPF_IND indexed loads are not allowed
234+
/// 3. BPF_ABS offsets must be within the SeccompData range and 4-byte aligned
234235
fn validate_seccomp_filter(insns: &[SockFilter]) -> Result<(), SystemError> {
235236
classic::validate_cbpf(insns)?;
236237

@@ -242,8 +243,9 @@ fn validate_seccomp_filter(insns: &[SockFilter]) -> Result<(), SystemError> {
242243
return Err(SystemError::EINVAL);
243244
}
244245
}
245-
// 与 Linux seccomp_check_filter 的显式白名单一致。特别地,通用
246-
// socket cBPF 接受 MOD,而 seccomp ABI 不接受 MOD K/X。
246+
// Matches the explicit opcode whitelist in Linux's seccomp_check_filter.
247+
// Notably, generic socket cBPF accepts MOD, but the seccomp ABI does not
248+
// accept MOD K/X.
247249
0x06 | 0x16 | 0x04 | 0x0c | 0x14 | 0x1c | 0x24 | 0x2c | 0x34 | 0x3c | 0x54 | 0x5c
248250
| 0x44 | 0x4c | 0xa4 | 0xac | 0x64 | 0x6c | 0x74 | 0x7c | 0x84 | 0x00 | 0x01 | 0x80
249251
| 0x81 | 0x60 | 0x61 | 0x02 | 0x03 | 0x07 | 0x87 | 0x05 | 0x15 | 0x1d | 0x35 | 0x3d
@@ -603,13 +605,13 @@ pub fn seccomp_set_mode_filter(fprog_ptr: u64, flags: u32) -> Result<(), SystemE
603605
let log = (flags & SECCOMP_FILTER_FLAG_LOG) != 0;
604606
let tsync = (flags & SECCOMP_FILTER_FLAG_TSYNC) != 0;
605607

606-
// 从用户空间读取 sock_fprog 结构(16 字节)
608+
// Read the sock_fprog structure from userspace (16 bytes)
607609
let fprog_size = core::mem::size_of::<classic::SockFprog>();
608610
let mut fprog_buf = [0u8; core::mem::size_of::<classic::SockFprog>()];
609611
let reader = UserBufferReader::new(fprog_ptr as *const u8, fprog_size, true)?;
610612
reader.copy_from_user_protected(&mut fprog_buf, 0)?;
611613

612-
// 解析并读取 filter 指令
614+
// Parse and read filter instructions
613615
let insns = classic::read_sock_fprog(&fprog_buf)?;
614616

615617
// 获取当前 filter 链头作为 prev

0 commit comments

Comments
 (0)