← Documents Documentation/bpf/s390.rst GitHub 원문 ↗

Linux 6.18.37 · BPF

Testing BPF on s390

Debian 기반 s390 교차 도구 체인과 rootfs를 준비하고, 커널·BPF selftest를 빌드하여 IBM Z 또는 QEMU s390x 게스트에서 실행하고 디버깅하는 절차입니다.

Source pathDocumentation/bpf/s390.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

s390.rst:1-205

호스트에 s390x GCC와 라이브러리, BPF 대상 Clang, QEMU를 준비하고 debootstrap으로 s390x rootfs 이미지를 만듭니다.

커널과 BPF selftest는 `ARCH=s390`, `CROSS_COMPILE=s390x-linux-gnu-`로 교차 빌드합니다. 9P Virtio 공유를 활성화하면 호스트의 빌드 결과를 게스트에서 바로 실행할 수 있습니다.

QEMU는 s390 커널과 디스크 이미지를 부팅하며, 전체 kselftest 또는 개별 verifier 테스트를 실행합니다. 필요하면 KASLR을 끄고 QEMU GDB stub에 `gdb-multiarch`를 연결해 디버깅합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 ===================
2 Testing BPF on s390
3 ===================
4
5 1. Introduction
6 ***************
7
8 IBM Z are mainframe computers, which are descendants of IBM System/360 from
9 year 1964. They are supported by the Linux kernel under the name "s390". This
10 document describes how to test BPF in an s390 QEMU guest.
11
12 2. One-time setup
13 *****************
14
15 The following is required to build and run the test suite:
16
17 * s390 GCC
18 * s390 development headers and libraries
19 * Clang with BPF support
20 * QEMU with s390 support
21 * Disk image with s390 rootfs
22
23 Debian supports installing compiler and libraries for s390 out of the box.
24 Users of other distros may use debootstrap in order to set up a Debian chroot::
25
26 sudo debootstrap \
27 --variant=minbase \
28 --include=sudo \
29 testing \
30 ./s390-toolchain
31 sudo mount --rbind /dev ./s390-toolchain/dev
32 sudo mount --rbind /proc ./s390-toolchain/proc
33 sudo mount --rbind /sys ./s390-toolchain/sys
34 sudo chroot ./s390-toolchain
35
36 Once on Debian, the build prerequisites can be installed as follows::
37
38 sudo dpkg --add-architecture s390x
39 sudo apt-get update
40 sudo apt-get install \
41 bc \
42 bison \
43 cmake \
44 debootstrap \
45 dwarves \
46 flex \
47 g++ \
48 gcc \
49 g++-s390x-linux-gnu \
50 gcc-s390x-linux-gnu \
51 gdb-multiarch \
52 git \
53 make \
54 python3 \
55 qemu-system-misc \
56 qemu-utils \
57 rsync \
58 libcap-dev:s390x \
59 libelf-dev:s390x \
60 libncurses-dev
61
62 Latest Clang targeting BPF can be installed as follows::
63
64 git clone https://github.com/llvm/llvm-project.git
65 ln -s ../../clang llvm-project/llvm/tools/
66 mkdir llvm-project-build
67 cd llvm-project-build
68 cmake \
69 -DLLVM_TARGETS_TO_BUILD=BPF \
70 -DCMAKE_BUILD_TYPE=Release \
71 -DCMAKE_INSTALL_PREFIX=/opt/clang-bpf \
72 ../llvm-project/llvm
73 make
74 sudo make install
75 export PATH=/opt/clang-bpf/bin:$PATH
76
77 The disk image can be prepared using a loopback mount and debootstrap::
78
79 qemu-img create -f raw ./s390.img 1G
80 sudo losetup -f ./s390.img
81 sudo mkfs.ext4 /dev/loopX
82 mkdir ./s390.rootfs
83 sudo mount /dev/loopX ./s390.rootfs
84 sudo debootstrap \
85 --foreign \
86 --arch=s390x \
87 --variant=minbase \
88 --include=" \
89 iproute2, \
90 iputils-ping, \
91 isc-dhcp-client, \
92 kmod, \
93 libcap2, \
94 libelf1, \
95 netcat, \
96 procps" \
97 testing \
98 ./s390.rootfs
99 sudo umount ./s390.rootfs
100 sudo losetup -d /dev/loopX
101
102 3. Compilation
103 **************
104
105 In addition to the usual Kconfig options required to run the BPF test suite, it
106 is also helpful to select::
107
108 CONFIG_NET_9P=y
109 CONFIG_9P_FS=y
110 CONFIG_NET_9P_VIRTIO=y
111 CONFIG_VIRTIO_PCI=y
112
113 as that would enable a very easy way to share files with the s390 virtual
114 machine.
115
116 Compiling kernel, modules and testsuite, as well as preparing gdb scripts to
117 simplify debugging, can be done using the following commands::
118
119 make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- menuconfig
120 make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- bzImage modules scripts_gdb
121 make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- \
122 -C tools/testing/selftests \
123 TARGETS=bpf \
124 INSTALL_PATH=$PWD/tools/testing/selftests/kselftest_install \
125 install
126
127 4. Running the test suite
128 *************************
129
130 The virtual machine can be started as follows::
131
132 qemu-system-s390x \
133 -cpu max,zpci=on \
134 -smp 2 \
135 -m 4G \
136 -kernel linux/arch/s390/boot/compressed/vmlinux \
137 -drive file=./s390.img,if=virtio,format=raw \
138 -nographic \
139 -append 'root=/dev/vda rw console=ttyS1' \
140 -virtfs local,path=./linux,security_model=none,mount_tag=linux \
141 -object rng-random,filename=/dev/urandom,id=rng0 \
142 -device virtio-rng-ccw,rng=rng0 \
143 -netdev user,id=net0 \
144 -device virtio-net-ccw,netdev=net0
145
146 When using this on a real IBM Z, ``-enable-kvm`` may be added for better
147 performance. When starting the virtual machine for the first time, disk image
148 setup must be finalized using the following command::
149
150 /debootstrap/debootstrap --second-stage
151
152 Directory with the code built on the host as well as ``/proc`` and ``/sys``
153 need to be mounted as follows::
154
155 mkdir -p /linux
156 mount -t 9p linux /linux
157 mount -t proc proc /proc
158 mount -t sysfs sys /sys
159
160 After that, the test suite can be run using the following commands::
161
162 cd /linux/tools/testing/selftests/kselftest_install
163 ./run_kselftest.sh
164
165 As usual, tests can be also run individually::
166
167 cd /linux/tools/testing/selftests/bpf
168 ./test_verifier
169
170 5. Debugging
171 ************
172
173 It is possible to debug the s390 kernel using QEMU GDB stub, which is activated
174 by passing ``-s`` to QEMU.
175
176 It is preferable to turn KASLR off, so that gdb would know where to find the
177 kernel image in memory, by building the kernel with::
178
179 RANDOMIZE_BASE=n
180
181 GDB can then be attached using the following command::
182
183 gdb-multiarch -ex 'target remote localhost:1234' ./vmlinux
184
185 6. Network
186 **********
187
188 In case one needs to use the network in the virtual machine in order to e.g.
189 install additional packages, it can be configured using::
190
191 dhclient eth0
192
193 7. Links
194 ********
195
196 This document is a compilation of techniques, whose more comprehensive
197 descriptions can be found by following these links:
198
199 - `Debootstrap <https://wiki.debian.org/EmDebian/CrossDebootstrap>`_
200 - `Multiarch <https://wiki.debian.org/Multiarch/HOWTO>`_
201 - `Building LLVM <https://llvm.org/docs/CMake.html>`_
202 - `Cross-compiling the kernel <https://wiki.gentoo.org/wiki/Embedded_Handbook/General/Cross-compiling_the_kernel>`_
203 - `QEMU s390x Guest Support <https://wiki.qemu.org/Documentation/Platforms/S390X>`_
204 - `Plan 9 folder sharing over Virtio <https://wiki.qemu.org/Documentation/9psetup>`_
205 - `Using GDB with QEMU <https://wiki.osdev.org/Kernel_Debugging#Use_GDB_with_QEMU>`_
206

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

IBM Z와 s390 BPF 테스트

1-11

`Testing BPF on s390` 문서는 1964년 IBM System/360의 후손인 IBM Z 메인프레임을 다룹니다. Linux 커널은 이 아키텍처를 `s390`이라는 이름으로 지원합니다.

이 문서는 s390 QEMU guest 안에서 BPF를 테스트하는 환경을 만드는 방법을 설명합니다.

교차 도구 체인과 디스크 이미지 준비

12-101

테스트 스위트를 빌드하고 실행하려면 다음 구성 요소가 필요합니다.

  • s390 GCC
  • s390 개발 헤더와 라이브러리
  • BPF 지원 Clang
  • s390 지원 QEMU
  • s390 rootfs가 들어 있는 디스크 이미지

Debian은 s390 컴파일러와 라이브러리 설치를 기본으로 지원합니다. 다른 배포판 사용자는 `debootstrap`으로 Debian chroot를 만들 수 있습니다.

sudo debootstrap \
  --variant=minbase \
  --include=sudo \
  testing \
  ./s390-toolchain
sudo mount --rbind /dev ./s390-toolchain/dev
sudo mount --rbind /proc ./s390-toolchain/proc
sudo mount --rbind /sys ./s390-toolchain/sys
sudo chroot ./s390-toolchain

Debian 환경에 들어간 뒤 다음 명령으로 빌드 선행 패키지를 설치합니다. `sudo dpkg --add-architecture s390x`로 multiarch를 활성화하고 `gcc-s390x-linux-gnu`, `g++-s390x-linux-gnu`, QEMU, s390x 개발 라이브러리 등을 설치합니다.

sudo dpkg --add-architecture s390x
sudo apt-get update
sudo apt-get install \
  bc \
  bison \
  cmake \
  debootstrap \
  dwarves \
  flex \
  g++ \
  gcc \
  g++-s390x-linux-gnu \
  gcc-s390x-linux-gnu \
  gdb-multiarch \
  git \
  make \
  python3 \
  qemu-system-misc \
  qemu-utils \
  rsync \
  libcap-dev:s390x \
  libelf-dev:s390x \
  libncurses-dev

BPF를 대상으로 하는 최신 Clang은 LLVM 소스를 받아 `LLVM_TARGETS_TO_BUILD=BPF`로 구성한 뒤 `/opt/clang-bpf`에 설치할 수 있습니다.

git clone https://github.com/llvm/llvm-project.git
ln -s ../../clang llvm-project/llvm/tools/
mkdir llvm-project-build
cd llvm-project-build
cmake \
  -DLLVM_TARGETS_TO_BUILD=BPF \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=/opt/clang-bpf \
  ../llvm-project/llvm
make
sudo make install
export PATH=/opt/clang-bpf/bin:$PATH

디스크 이미지는 loopback mount와 `debootstrap`으로 준비합니다. 1GiB raw 이미지를 만들고 ext4로 포맷한 뒤 `--foreign`, `--arch=s390x` 옵션으로 최소 rootfs를 설치합니다. `/dev/loopX`는 실제 할당된 loop 장치로 바꿔야 합니다.

qemu-img create -f raw ./s390.img 1G
sudo losetup -f ./s390.img
sudo mkfs.ext4 /dev/loopX
mkdir ./s390.rootfs
sudo mount /dev/loopX ./s390.rootfs
sudo debootstrap \
  --foreign \
  --arch=s390x \
  --variant=minbase \
  --include=" \
    iproute2, \
    iputils-ping, \
    isc-dhcp-client, \
    kmod, \
    libcap2, \
    libelf1, \
    netcat, \
    procps" \
  testing \
  ./s390.rootfs
sudo umount ./s390.rootfs
sudo losetup -d /dev/loopX

커널, 모듈과 BPF selftest 교차 빌드

102-126

일반적인 BPF 테스트 스위트 Kconfig 옵션에 더해 다음 옵션을 선택하면 9P/Virtio를 통해 s390 가상 머신과 파일을 쉽게 공유할 수 있습니다.

CONFIG_NET_9P=y
CONFIG_9P_FS=y
CONFIG_NET_9P_VIRTIO=y
CONFIG_VIRTIO_PCI=y

`CONFIG_NET_9P`, `CONFIG_9P_FS`, `CONFIG_NET_9P_VIRTIO`, `CONFIG_VIRTIO_PCI`를 활성화한 뒤 `ARCH=s390`과 `CROSS_COMPILE=s390x-linux-gnu-`를 지정해 커널 `bzImage`, 모듈과 `scripts_gdb`를 빌드합니다. 이어서 `TARGETS=bpf` selftest를 설치 디렉터리에 배치합니다.

make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- menuconfig
make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- bzImage modules scripts_gdb
make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- \
  -C tools/testing/selftests \
  TARGETS=bpf \
  INSTALL_PATH=$PWD/tools/testing/selftests/kselftest_install \
  install

QEMU 부팅과 테스트 실행

127-169

다음 명령은 두 CPU와 4GiB 메모리를 갖춘 `qemu-system-s390x` 가상 머신을 시작합니다. 커널은 `linux/arch/s390/boot/compressed/vmlinux`, 루트 디스크는 `s390.img`를 사용하며, `-virtfs`로 호스트의 Linux 소스 트리를 공유합니다. 네트워크, 난수 장치와 `-cpu max,zpci=on`도 구성합니다.

qemu-system-s390x \
  -cpu max,zpci=on \
  -smp 2 \
  -m 4G \
  -kernel linux/arch/s390/boot/compressed/vmlinux \
  -drive file=./s390.img,if=virtio,format=raw \
  -nographic \
  -append 'root=/dev/vda rw console=ttyS1' \
  -virtfs local,path=./linux,security_model=none,mount_tag=linux \
  -object rng-random,filename=/dev/urandom,id=rng0 \
  -device virtio-rng-ccw,rng=rng0 \
  -netdev user,id=net0 \
  -device virtio-net-ccw,netdev=net0

실제 IBM Z에서 사용할 때는 성능 향상을 위해 `-enable-kvm`을 추가할 수 있습니다. 가상 머신을 처음 시작한 뒤에는 다음 명령으로 디스크 이미지의 debootstrap 두 번째 단계를 마칩니다.

/debootstrap/debootstrap --second-stage

호스트에서 빌드한 코드 디렉터리는 `/linux`에 9P로 마운트하고 `/proc`과 `/sys`도 함께 마운트합니다.

mkdir -p /linux
mount -t 9p linux /linux
mount -t proc proc /proc
mount -t sysfs sys /sys

그런 다음 설치된 kselftest 전체를 실행합니다.

cd /linux/tools/testing/selftests/kselftest_install
./run_kselftest.sh

평소와 마찬가지로 `test_verifier` 같은 테스트를 개별 실행할 수도 있습니다.

cd /linux/tools/testing/selftests/bpf
./test_verifier

QEMU GDB stub 디버깅

170-184

QEMU에 `-s`를 전달해 QEMU GDB stub을 활성화하면 s390 커널을 디버깅할 수 있습니다.

gdb가 메모리에서 커널 이미지 위치를 정확히 찾도록 KASLR을 끄는 편이 좋습니다. 커널 빌드 설정에서 다음 값을 사용합니다.

RANDOMIZE_BASE=n

그 뒤 `gdb-multiarch`를 로컬 QEMU의 1234번 포트에 연결합니다.

gdb-multiarch -ex 'target remote localhost:1234' ./vmlinux

게스트 네트워크 설정

185-192

가상 머신에서 추가 패키지를 설치하는 등 네트워크가 필요하면 DHCP 클라이언트로 `eth0`을 구성합니다.

dhclient eth0