运行原理

一、eBPF 虚拟机如何工作

区别概念

  • 系统虚拟化(KVM)基于 x86 或 arm64 等通用指令集,这些指令集可以完成完整计算机的所有功能
  • eBPF 只提供有限的指令集。这些指令集可用于完成一部分内核的功能,但却远不足以模拟完整的计算机。为了更高效地与内核进行交互,eBPF 指令还有意采用了 C 调用约定,其提供的辅助函数可以在 C 语言中直接调用,极大地方便了 eBPF 程序的开发。

查看更多

pthread_cond_timedwait 函数

pthread_cond_timedwait 函数

1
2
3
4
5
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);

pthread_cond_timedwait 用于等待一个条件变量,等待条件变量的同时可以设置等待超时。其中 abstime 超时时间是一个绝对值,也就是距离 1970-1-1 的时间值,而不是一个时间段。比如说当前时间为:2023-05-28 17:06:00.100,我想要通过这个函数设置最大超时为 3000ms,那么就需要设置 abstime 的时间为:2023-05-28 17:06:03.100

同时,建议大家使用单调时间,而非系统时间。因为系统时间可能存在跳变的问题。

如下举个例子:

查看更多

netlink 方式的优缺点

netlink 是一种在内核空间和用户空间之间进行通信的机制。它允许用户空间的进程通过 socket 接口与内核通信。通过 netlink,用户空间程序可以发送请求给内核,获取系统状态、配置参数,或者监听系统事件等。

proc 文件系统是一种特殊的虚拟文件系统,它提供了对系统内核运行时状态的访问。通过访问 /proc 目录下的文件,用户可以查看和修改内核的一些参数和状态。

netlink 作为 linux 内核和用户空间之间的通信机制,具有如下优缺点。

优点如下:

查看更多

aarch64 工具链编译

aarch64 工具链编译

1. 先下载对应源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1. binutils-2.33.1
wget -c https://ftp.gnu.org/gnu/binutils/binutils-2.33.1.tar.lz

2. gcc-7.5.0
wget -c https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.gz
gcc 的依赖库:
wget -c https://ftp.gnu.org/gnu/mpfr/mpfr-4.0.1.tar.xz
wget -c https://ftp.gnu.org/gnu/gmp/gmp-6.1.2.tar.xz
wget -c https://ftp.gnu.org/gnu/mpc/mpc-1.1.0.tar.gz
wget -c ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2
wget -c ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-0.16.1.tar.gz

3. linux-5.4.74
wget --no-check-certificate -c https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.4.74.tar.gz

3. glibc-2.28
wget -c https://ftp.gnu.org/gnu/libc/glibc-2.28.tar.xz

2. 设置环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
目录结构
-> zhangyi83@172-18-184-22 /data/App/toolchain ? # tree
.
├── README.md
├── src
│   ├── binutils-2.33.1.tar.lz
│   ├── gcc-7.5.0.tar.gz
│   └── glibc-2.28.tar.gz
└── target
└── aarch64

解压
cd $TOOLCHAIN/src
lzip -d binutils-2.33.1.tar.lz
tar -xf binutils-2.33.1.tar
tar -xzf gcc-7.5.0.tar.gz
tar -xzf glibc-2.28.tar.gz
tar -xf cloog-0.18.1.tar.gz
tar -xf gmp-6.1.2.tar.xz
tar -xf mpfr-4.0.1.tar.xz
tar -xf isl-0.18.tar.bz2
tar -xf mpc-1.1.0.tar.gz

# gcc依赖库的处理
mv cloog-0.18.1 gcc-8.2.0/cloog
mv gmp-6.1.2 gcc-8.2.0/gmp
mv mpfr-4.0.1 gcc-8.2.0/mpfr
mv isl-0.18 gcc-8.2.0/isl
mv mpc-1.1.0 gcc-8.2.0/mpc

安装适用于 aarch64 架构的交叉编译工具链,以便在 x86 上生成 aarch64 代码。
sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu

设置交叉编译环境
export CC=aarch64-linux-gnu-gcc
export CXX=aarch64-linux-gnu-g++
export LD=aarch64-linux-gnu-ld
export AR=aarch64-linux-gnu-ar
export AS=aarch64-linux-gnu-as
export RANLIB=aarch64-linux-gnu-ranlib
# 假设这些交叉编译工具链安装到了 '/usr/bin' 目录下,这一句不要漏掉
export PATH=/usr/bin:$PATH

设置环境变量,(环境变量一定要配置)
export TOOLCHAIN=/data/App/toolchain
cd $TOOLCHAIN
mkdir -p target/aarch64
export AARCH64=$TOOLCHAIN/target/aarch64
export PATH=$AARCH64/bin:$PATH

三、编译

编译 binutils-2.33.1

查看更多

交叉编译问题记录

交叉编译问题记录

一、关于 –sysroot 的设置问题

从官方解释来看:

1
2
3
4
5
6
--sysroot=dir
Use dir as the logical root directory for headers and libraries. For example, if the compiler normally searches for headers in /usr/include and libraries in /usr/lib, it instead searches dir/usr/include and dir/usr/lib.

If you use both this option and the -isysroot option, then the --sysroot option applies to libraries, but the -isysroot option applies to header files.

The GNU linker (beginning with version 2.16) has the necessary support for this option. If your linker does not support this option, the header file aspect of --sysroot still works, but the library aspect does not.

他会对编译和链接过程中,查找头文件和链接库造成影响。

原本会从 /usr/include 目录中搜索头文件、从 /usr/lib 中搜索依赖库。当设置了 --sysroot=dir 后,则从 /dir/usr/include 搜索头文件、从 dir/usr/lib 中搜索依赖库。

查看更多

netlink 相关

1
2
3
4
5
6
7
libnl 库
https://www.infradead.org/~tgr/libnl/
https://github.com/thom311/libnl

Generic Netlink 详解:https://blog.csdn.net/u011638528/article/details/10221557
Netlink 详解:https://e-mailky.github.io/2017-02-14-netlink-user-kernel1#netlink%E7%9A%84%E6%B6%88%E6%81%AF%E6%A0%BC%E5%BC%8F

编译三方库

一、编译 openssl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
准备阶段
wget --no-check-certificate -c https://www.openssl.org/source/old/1.1.1/openssl-1.1.1o.tar.gz
tar -xzf openssl-1.1.1o.tar.gz
cd openssl-1.1.1o
mkdir build-aarch64 && cd build-aarch64

设置交叉编译环境
export CC=aarch64-linux-gnu-gcc
export CXX=aarch64-linux-gnu-g++
export LD=aarch64-linux-gnu-ld
export AR=aarch64-linux-gnu-ar
export AS=aarch64-linux-gnu-as
export RANLIB=aarch64-linux-gnu-ranlib
# 假设这些交叉编译工具链安装到了 '/usr/bin' 目录下,这一句不要漏掉
export PATH=/usr/bin:$PATH

编译
aarch64,编译动态库
../Configure linux-aarch64 --debug \
-DOPENSSL_NO_OPENSSL_MEMORY -DOPENSSL_NO_BUF_FREELISTS \
-DOPENSSL_NO_ASM -DOPENSSL_NO_INLINE_ASM -DOPENSSL_NO_INLINE -DOPENSSL_NO_AUTOLOAD_CONFIG \
-DOPENSSL_NO_DEPRECATED -DOPENSSL_NO_COMP -DOPENSSL_NO_ENGINE -DOPENSSL_NO_STATIC_ENGINE \
-DOPENSSL_NO_HW -DOPENSSL_NO_SSL2 -DOPENSSL_NO_SSL3 -DOPENSSL_NO_TLS1 -DOPENSSL_NO_TLS1_1 \
-DOPENSSL_NO_WEAK_SSL_CIPHERS \
no-tests no-asm no-autoload-config no-deprecated no-zlib no-comp no-engine no-static-engine \
no-ssl2 no-ssl3 no-tls1 no-tls1_1 no-hw no-crypto-mdebug \
--prefix=/data/App/toolchain/target/out_aarch64/openssl-1.1.1o \
--openssldir=/data/App/toolchain/target/out_aarch64/openssl-1.1.1o

make depend
make -j8
make install # 才可以安装到设置的目录中

编译 x86
export CC=gcc
export CXX=g++
export LD=ld
export AR=ar
export AS=as
export RANLIB=ranlib

../Configure linux-x86_64 --debug \
-DOPENSSL_NO_OPENSSL_MEMORY -DOPENSSL_NO_BUF_FREELISTS \
-DOPENSSL_NO_ASM -DOPENSSL_NO_INLINE_ASM -DOPENSSL_NO_INLINE -DOPENSSL_NO_AUTOLOAD_CONFIG \
-DOPENSSL_NO_DEPRECATED -DOPENSSL_NO_COMP -DOPENSSL_NO_ENGINE -DOPENSSL_NO_STATIC_ENGINE \
-DOPENSSL_NO_HW -DOPENSSL_NO_SSL2 -DOPENSSL_NO_SSL3 -DOPENSSL_NO_TLS1 -DOPENSSL_NO_TLS1_1 \
-DOPENSSL_NO_WEAK_SSL_CIPHERS \
no-tests no-asm no-autoload-config no-deprecated no-zlib no-comp no-engine no-static-engine \
no-ssl2 no-ssl3 no-tls1 no-tls1_1 no-hw no-crypto-mdebug \
--prefix=/data/App/toolchain/target/out_x86_64/openssl-1.1.1o \
--openssldir=/data/App/toolchain/target/out_x86_64/openssl-1.1.1o

二、编译 perl

1
2
3
4
5
6
7
8
9
curl -L -O http://www.cpan.org/src/5.0/perl-5.34.0.tar.gz
curl -L -O https://github.com/arsv/perl-cross/releases/download/1.3.6/perl-cross-1.3.6.tar.gz
tar -zxf perl-5.34.0.tar.gz
cd perl-5.34.0
tar --strip-components=1 -zxf ../perl-cross-1.3.6.tar.gz

./configure --prefix=/usr/local/perl-5.34.0 --target=aarch64-linux-gnu -Dldflags="-s -static" -Dusethreads -Duselargefiles -Dusestrict --all-static
make -j4
make install

查看更多