tencent cloud

TencentOS Server

eBPF Feature Support Notes

다운로드
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-10-19 17:22:48
AI 번역

Feature Overview

This document describes the eBPF (extended Berkeley Packet Filter) support status, capability matrix, toolchain installation, and common use cases for the TencentOS Server 4 kernel, helping you use eBPF to non-intrusively obtain kernel runtime information in performance analysis, network observability, and security auditing scenarios.

Background Information

Traditional kernel performance analysis and network traffic monitoring rely on kernel modules or kernel source code modifications, which introduce significant security risks, high maintenance costs, and cross-version incompatibility. eBPF provides the ability to safely run sandboxed programs in kernel mode, enabling network packet processing (XDP/TC), function tracing (kprobe/tracepoint), performance sampling (perf_event), and security policy enforcement (LSM BPF) without modifying kernel source code or loading kernel modules.
TencentOS Server 4 is built on the Linux 6.6 LTS kernel and provides full eBPF support, including BTF (BPF Type Format) and CO-RE (Compile Once – Run Everywhere) capabilities, allowing BPF programs to run across kernel versions without recompilation. However, using eBPF requires root privileges and the correct toolchain selection (BCC / bpftrace / libbpf) to match different use cases.

Prerequisites

Permission requirements: Loading and running BPF programs requires root privileges, or the CAP_BPF / CAP_SYS_ADMIN capabilities in container environments. Regular containers do not support running BPF tools by default.
Kernel configuration verification: Verify that the following kernel configuration options are enabled:
CONFIG_BPF=y (basic BPF support)
CONFIG_BPF_SYSCALL=y (BPF system call)
CONFIG_BPF_JIT=y (BPF JIT compilation)
CONFIG_DEBUG_INFO_BTF=y (BTF support, required for CO-RE)
CONFIG_BPF_LSM=y (LSM BPF support, required for security policy features)
CONFIG_XDP_SOCKETS=y (XDP socket support, required for high-performance AF_XDP packet reception)
dnf repository availability: Installing BCC, bpftrace, and bpftool requires normal access to the system dnf repositories. You can run dnf info bcc-tools bpftrace to check the specific versions of the BCC/bpftrace packages available on the current system.
Kernel module verification: Verify that the bpf-related modules are loaded (they are compiled into the kernel by default, so no additional modprobe is required).
Kernel headers (for BCC compilation): Runtime compilation of BCC tools requires installing the kernel-devel and kernel-headers packages that match the current kernel version.
Warning:
During use, you must fully evaluate the following risks. Thoroughly validate BPF programs in a test environment before deploying them to production, and never run untested BPF programs directly in a production environment.
Kernel panic risk: An incorrect BPF program may cause a kernel panic, resulting in system unavailability.
Instruction limit: The eBPF instruction limit defaults to 1 million instructions (kernel 6.6). If a complex BPF program exceeds this limit, you can split it using tail calls or optimize the program logic to reduce the number of instructions per program.

API and Parameter Description

eBPF Capability Matrix

Function
Support Status
Mount Point
Typical Use
XDP
Supported
NIC driver layer
High-performance packet processing and Anti-DDoS
TC(Traffic Control)
Supported
Traffic control layer
Traffic classification and QoS
cgroup sock
Supported
cgroup socket
Container network policies
kprobe/kretprobe
Supported
Kernel function entry/exit
Kernel function tracing
tracepoint
Supported
Kernel static tracepoint
System call tracing
perf_event
Supported
Performance event
PMU event sampling
uprobe/uretprobe
Supported
User function entry/exit
Application tracing
LSM(BPF)
Supported (disabled by default; configure the lsm=tee,bpf boot parameter and restart. See step 7.)
Security hook point
Security Policies
sock_ops
Supported
socket operations
TCP tuning
sk_msg/sk_redirect
Supported
socket messages
Proxy redirection
struct_ops
Supported
Struct operations
Custom TCP congestion control

eBPF Limitations

Limit
Value
Description
Instruction quantity limit
1 million entries (default, configurable)
Default value of the 6.6 kernel. It can be adjusted through kernel configuration.
Stack size
512 bytes
Use heap allocation to avoid this.
Unprivileged user
Disabled by default
unprivileged_bpf_disabled values: 0 (allowed), 1 (disabled but can be changed back), 2 (disabled and cannot be changed back). The default value in TencentOS Server 4 is 0.
Kernel version compatibility
CO-RE programs can work across kernel versions; non-CO-RE programs must match the kernel version.
Depends on BTF support.

Operation Steps

1. Confirming eBPF Kernel Support

# Check basic kernel BPF configuration
grep CONFIG_BPF=y /boot/config-$(uname -r)
# Expected output: CONFIG_BPF=y

grep CONFIG_BPF_SYSCALL=y /boot/config-$(uname -r)
# Expected output: CONFIG_BPF_SYSCALL=y

grep CONFIG_BPF_JIT=y /boot/config-$(uname -r)
# Expected output: CONFIG_BPF_JIT=y

grep CONFIG_XDP_SOCKETS=y /boot/config-$(uname -r)
# Expected output: CONFIG_XDP_SOCKETS=y

# Check BTF support (required for BPF CO-RE)
grep CONFIG_DEBUG_INFO_BTF=y /boot/config-$(uname -r)
# Expected output: CONFIG_DEBUG_INFO_BTF=y

# Check LSM BPF support (see step 7 if security policy features are required)
grep CONFIG_BPF_LSM=y /boot/config-$(uname -r)
# Expected output: CONFIG_BPF_LSM=y

# View BPF program limits (loading policy for unprivileged users)
cat /proc/sys/kernel/unprivileged_bpf_disabled
# Expected output: TencentOS Server 4 outputs 0 by default. Value meanings: 0=Allow unprivileged users to load BPF programs, 1=Prohibit and can be changed back, 2=Prohibit and cannot be changed back.

2. Viewing the Number of Instructions of Loaded BPF Programs

# If bpftool is not installed yet, run the following command to install it.
dnf install bpftool

# View all loaded BPF programs and their instruction counts
bpftool prog show
# Expected output: List all loaded programs, with each line containing:
# <prog_id>: <type> name <name> tag <tag> gpl
# loaded_at <time> uid 0
# xlated <instruction count> insns limit 1000000 insns jited <JIT instruction count> bytes memlock <byte count>

# View detailed information of a specific program (JSON + verbose mode, replace <prog_id> with the actual ID)
bpftool prog show id <prog_id> -j -d
# Expected output: Program details in JSON format, including the xlated_insns field, which indicates the instruction count.
Note:
In the output of bpftool prog show, xlated <N> insns is the number of compiled instructions of the program, and limit 1000000 insns is the instruction quantity limit.
The instruction limit is a kernel compile-time constant (1 million instructions by default in kernel 6.6) and cannot be modified via sysctl at runtime.

3. Installing BCC Tools

# Install the BCC toolkit.
dnf install bcc-tools
# Expected output: Dependencies resolved and installed successfully with no errors.

# Verify that the tool is installed.
ls /usr/share/bcc/tools/ | head -10
# Expected output: List tools such as opensnoop, execsnoop, biolatency, tcptop, and profile.

# Example: Trace file opens.
/usr/share/bcc/tools/opensnoop -d 5
# Expected output: Records of open() calls from all processes within 5 seconds, including the PID, COMM, FD, ERR, and PATH columns.

4. Installing and Using bpftrace

# Install bpftrace.
dnf install bpftrace
# Expected output: Dependencies resolved and installed successfully with no errors.

# Confirm the version.
bpftrace --version
# Expected output: bpftrace v0.x.x or later.

# List available tracepoints.
bpftrace -l 'tracepoint:syscalls:sys_enter_*' | head -10
# Expected output: List the sys_enter_* series of system call tracepoints.

# Trace all openat() system calls, counted by process.
bpftrace -e 'tracepoint:syscalls:sys_enter_openat { @[comm] = count(); }'
# Expected output: A table of openat call counts aggregated by process name, with statistics printed after pressing Ctrl+C.

# Function latency tracing (vfs_read latency histogram)
bpftrace -e 'kprobe:vfs_read { @start[tid] = nsecs; } kretprobe:vfs_read /@start[tid]/ { @us = hist((nsecs - @start[tid]) / 1000); delete(@start[tid]); }'
# Expected output: A histogram of vfs_read latency in microseconds, printed after pressing Ctrl+C.

5. Installing and Using bpftool

# If bpftool is not installed yet, run the following command to install it.
dnf install bpftool
# Expected output: Installation succeeded with no errors.

# View all loaded BPF programs.
bpftool prog show
# Expected output: List all loaded programs along with their types, names, and instruction counts.

# View all BPF maps.
bpftool map show
# Expected output: List all BPF maps along with their types and sizes.

# View BPF programs attached to the network interface (replace <NIC name> with the actual NIC name).
bpftool net show dev <NIC name>
# Expected output: List the XDP/TC programs attached to the NIC. If no programs are attached, the output is empty.

6. Developing with libbpf + CO-RE

The TencentOS Server 4 kernel supports BTF, allowing you to develop BPF programs with CO-RE without compiling them specifically for the target kernel.
# Install the libbpf development package.
dnf install libbpf-devel
# Expected output: Installation succeeded.

# Verify that BTF information is available.
ls /sys/kernel/btf/vmlinux
# Expected output: /sys/kernel/btf/vmlinux (the existence of this file indicates that BTF is available)

# Clone the libbpf-bootstrap template (requires network access to GitHub).
git clone https://github.com/libbpf/libbpf-bootstrap
# Expected output: Clone succeeded.

7. Enabling LSM BPF Support

# Verify that the kernel supports BPF LSM.
grep CONFIG_BPF_LSM=y /boot/config-$(uname -r)
# Expected output: CONFIG_BPF_LSM=y

# View the currently enabled LSM list.
cat /sys/kernel/security/lsm
# Expected output: lsm list (TencentOS Server 4 defaults to capability,yama,selinux,bpf, with bpf enabled by default)

# If bpf is not in the list, add lsm=tee,bpf as a kernel boot parameter (a reboot is required for the change to take effect).
# A restart will cause service interruption, so perform it within a maintenance window. Modifying the LSM list affects system security policy behavior, so verify the change in a test environment first.
grubby --update-kernel=ALL --args="lsm=tee,bpf"
# Expected output: No errors.
# Restart to take effect
reboot

# Confirm after reboot.
cat /sys/kernel/security/lsm
# Expected output: An LSM list that includes bpf.
Note:
LSM BPF is enabled through the lsm=tee,bpf kernel boot parameter, and Secure Boot is not a hard requirement. If the system has other LSMs enabled (such as SELinux), BPF LSM can coexist with them.

8. Running Common Scenario Examples

# CPU off-cpu analysis (5-second sampling)
/usr/share/bcc/tools/offcputime 5
# Expected output: A histogram of off-cpu time aggregated by kernel stack, used to identify blocking hotspots.

# Memory allocation tracing
/usr/share/bcc/tools/memleak
# Expected output: Statistics of memory allocation and deallocation aggregated by call stack, where unreleased memory indicates a leak.

# System call latency statistics
/usr/share/bcc/tools/syscount
# Expected output: A latency statistics table aggregated by system call.
The expected outputs are histograms or count tables aggregated by time dimension, which can be used to identify hot functions or abnormal calls.

FAQs

Q1: Error Loading BPF Program: Operation not permitted

Cause: The program is run by a non-root user, or the container environment lacks the CAP_BPF / CAP_SYS_ADMIN capabilities.
Solution: Switch to the root user or add the corresponding capabilities to the container, and then try again.
# Method 1: Run as the root user
sudo <bpf command>

# Method 2: Add capabilities to the container runtime
docker run --cap-add CAP_BPF --cap-add CAP_SYS_ADMIN ...

# Method 3: Confirm that loading by unprivileged users is disabled (normal state)
cat /proc/sys/kernel/unprivileged_bpf_disabled
# Expected output: 0, 1, or 2 (1 or 2 indicates that loading BPF programs by unprivileged users is prohibited)

Q2: BCC Tool Error: kernel headers not found

Cause: Runtime compilation of BCC tools requires the kernel-devel package, and its version must match the current kernel.
Solution: Install the kernel-devel and kernel-headers packages that match the current kernel version.
# View the current kernel version
uname -r
# Expected output: for example, 6.6.0-xxx.el4.x86_64

# Install the matching kernel-devel and kernel-headers packages.
dnf install "kernel-devel-$(uname -r)" "kernel-headers-$(uname -r)"
# Expected output: Installation succeeded.

# Confirm that the header file path exists
ls /lib/modules/$(uname -r)/build
# Expected output: The path exists and is a symbolic link pointing to the source code directory.

Q3: Error Loading CO-RE Program: BTF not found or failed to load BTF

Cause: The target kernel does not have CONFIG_DEBUG_INFO_BTF=y enabled, or the /sys/kernel/btf/vmlinux file does not exist.
Solution: Check the kernel BTF configuration and file status. If they do not meet the requirements, replace the kernel with one that has BTF enabled.
# Check the kernel BTF configuration
grep CONFIG_DEBUG_INFO_BTF=y /boot/config-$(uname -r)
# Expected output: CONFIG_DEBUG_INFO_BTF=y

# Verify that the BTF file exists.
ls -l /sys/kernel/btf/vmlinux
# Expected output: The file exists.

# If BTF is not enabled on the kernel, replace it with a kernel version that has BTF enabled.
# TencentOS Server 4 enables BTF by default. If BTF is missing, verify the kernel source.

Q4: BPF Program Loading Failure: program too large or Instruction Limit Exceeded

Cause: The number of instructions in the compiled BPF program exceeds the limit of 1 million (the default value in kernel 6.6).
Solution: Check the actual number of instructions in the program, and reduce the number of instructions by splitting the program or optimizing the logic.
# Use bpftool to view the actual instruction count of the program.
bpftool prog show
# Expected output: xlated <instruction count> insns limit 1000000 insns

# If the instruction count approaches the limit, split the program into multiple smaller BPF programs.
# Or optimize the program logic to reduce loops and branches.
# Check whether any loops are not unrolled or any tail calls are not used.

Q5: What Is the Purpose of sysctl kernel.bpf_stats?

Note:
kernel.bpf_stats is the switch for BPF statistics collection. Its value can be 0 (disabled) or 1 (enabled). After it is enabled, you can view the runtime statistics of BPF programs.
# View the current value of bpf_stats
sysctl kernel.bpf_stats
# Expected output: kernel.bpf_stats = 0

# Enable BPF statistics collection (0 to disable, 1 to enable)
sysctl -w kernel.bpf_stats=1
# Expected output: kernel.bpf_stats = 1


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백