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

Linux 6.18.37 · BPF

HOWTO interact with BPF subsystem

BPF bug report, patch 제출과 CI, tree 선택, stable backport, selftest 및 LLVM BPF back end 협업 절차를 설명합니다.

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

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

1. 요약·해설

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

요약과 해설

bpf_devel_QA.rst:1-704

BPF 개발은 `bpf@vger.kernel.org`를 중심으로 이루어지며 patch는 fix용 `bpf`와 feature용 `bpf-next` tree를 거쳐 각각 `net`과 `net-next`, 이어 mainline으로 이동합니다. 제출 전 BPF CI와 selftest를 실행하고 target tree에 맞는 subject prefix, `Fixes:` tag, 충분한 commit message와 cover letter를 갖춰야 합니다.

stable backport는 maintainer에게 queue를 요청해야 하며 patch description에 `Cc: stable@vger.kernel.org`를 직접 넣지 않습니다. verifier나 instruction 변경에는 `test_verifier.c`, `test_bpf.c` 같은 regression test가 필수이고, 새 instruction은 각 architecture JIT와 LLVM BPF back end에도 함께 반영해야 합니다.

LLVM의 `-mcpu=probe`는 host kernel의 instruction extension을 감지합니다. clang target은 assembly header, ELF section, switch table, pointer width에 영향을 주므로 kernel structure tracing에는 native target을 고려하되 BPF helper·context와 맞닿는 64-bit data layout에는 `--target=bpf`를 사용해야 합니다.

2. 영어 원문 전체

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

원문 전체 펼치기
1 =================================
2 HOWTO interact with BPF subsystem
3 =================================
4
5 This document provides information for the BPF subsystem about various
6 workflows related to reporting bugs, submitting patches, and queueing
7 patches for stable kernels.
8
9 For general information about submitting patches, please refer to
10 Documentation/process/submitting-patches.rst. This document only describes
11 additional specifics related to BPF.
12
13 .. contents::
14 :local:
15 :depth: 2
16
17 Reporting bugs
18 ==============
19
20 Q: How do I report bugs for BPF kernel code?
21 --------------------------------------------
22 A: Since all BPF kernel development as well as bpftool and iproute2 BPF
23 loader development happens through the bpf kernel mailing list,
24 please report any found issues around BPF to the following mailing
25 list:
26
27 bpf@vger.kernel.org
28
29 This may also include issues related to XDP, BPF tracing, etc.
30
31 Given netdev has a high volume of traffic, please also add the BPF
32 maintainers to Cc (from kernel ``MAINTAINERS`` file):
33
34 * Alexei Starovoitov <ast@kernel.org>
35 * Daniel Borkmann <daniel@iogearbox.net>
36
37 In case a buggy commit has already been identified, make sure to keep
38 the actual commit authors in Cc as well for the report. They can
39 typically be identified through the kernel's git tree.
40
41 **Please do NOT report BPF issues to bugzilla.kernel.org since it
42 is a guarantee that the reported issue will be overlooked.**
43
44 Submitting patches
45 ==================
46
47 Q: How do I run BPF CI on my changes before sending them out for review?
48 ------------------------------------------------------------------------
49 A: BPF CI is GitHub based and hosted at https://github.com/kernel-patches/bpf.
50 While GitHub also provides a CLI that can be used to accomplish the same
51 results, here we focus on the UI based workflow.
52
53 The following steps lay out how to start a CI run for your patches:
54
55 - Create a fork of the aforementioned repository in your own account (one time
56 action)
57
58 - Clone the fork locally, check out a new branch tracking either the bpf-next
59 or bpf branch, and apply your to-be-tested patches on top of it
60
61 - Push the local branch to your fork and create a pull request against
62 kernel-patches/bpf's bpf-next_base or bpf_base branch, respectively
63
64 Shortly after the pull request has been created, the CI workflow will run. Note
65 that capacity is shared with patches submitted upstream being checked and so
66 depending on utilization the run can take a while to finish.
67
68 Note furthermore that both base branches (bpf-next_base and bpf_base) will be
69 updated as patches are pushed to the respective upstream branches they track. As
70 such, your patch set will automatically (be attempted to) be rebased as well.
71 This behavior can result in a CI run being aborted and restarted with the new
72 base line.
73
74 Q: To which mailing list do I need to submit my BPF patches?
75 ------------------------------------------------------------
76 A: Please submit your BPF patches to the bpf kernel mailing list:
77
78 bpf@vger.kernel.org
79
80 In case your patch has changes in various different subsystems (e.g.
81 networking, tracing, security, etc), make sure to Cc the related kernel mailing
82 lists and maintainers from there as well, so they are able to review
83 the changes and provide their Acked-by's to the patches.
84
85 Q: Where can I find patches currently under discussion for BPF subsystem?
86 -------------------------------------------------------------------------
87 A: All patches that are Cc'ed to netdev are queued for review under netdev
88 patchwork project:
89
90 https://patchwork.kernel.org/project/netdevbpf/list/
91
92 Those patches which target BPF, are assigned to a 'bpf' delegate for
93 further processing from BPF maintainers. The current queue with
94 patches under review can be found at:
95
96 https://patchwork.kernel.org/project/netdevbpf/list/?delegate=121173
97
98 Once the patches have been reviewed by the BPF community as a whole
99 and approved by the BPF maintainers, their status in patchwork will be
100 changed to 'Accepted' and the submitter will be notified by mail. This
101 means that the patches look good from a BPF perspective and have been
102 applied to one of the two BPF kernel trees.
103
104 In case feedback from the community requires a respin of the patches,
105 their status in patchwork will be set to 'Changes Requested', and purged
106 from the current review queue. Likewise for cases where patches would
107 get rejected or are not applicable to the BPF trees (but assigned to
108 the 'bpf' delegate).
109
110 Q: How do the changes make their way into Linux?
111 ------------------------------------------------
112 A: There are two BPF kernel trees (git repositories). Once patches have
113 been accepted by the BPF maintainers, they will be applied to one
114 of the two BPF trees:
115
116 * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/
117 * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/
118
119 The bpf tree itself is for fixes only, whereas bpf-next for features,
120 cleanups or other kind of improvements ("next-like" content). This is
121 analogous to net and net-next trees for networking. Both bpf and
122 bpf-next will only have a master branch in order to simplify against
123 which branch patches should get rebased to.
124
125 Accumulated BPF patches in the bpf tree will regularly get pulled
126 into the net kernel tree. Likewise, accumulated BPF patches accepted
127 into the bpf-next tree will make their way into net-next tree. net and
128 net-next are both run by David S. Miller. From there, they will go
129 into the kernel mainline tree run by Linus Torvalds. To read up on the
130 process of net and net-next being merged into the mainline tree, see
131 the documentation on netdev subsystem at
132 Documentation/process/maintainer-netdev.rst.
133
134
135
136 Occasionally, to prevent merge conflicts, we might send pull requests
137 to other trees (e.g. tracing) with a small subset of the patches, but
138 net and net-next are always the main trees targeted for integration.
139
140 The pull requests will contain a high-level summary of the accumulated
141 patches and can be searched on netdev kernel mailing list through the
142 following subject lines (``yyyy-mm-dd`` is the date of the pull
143 request)::
144
145 pull-request: bpf yyyy-mm-dd
146 pull-request: bpf-next yyyy-mm-dd
147
148 Q: How do I indicate which tree (bpf vs. bpf-next) my patch should be applied to?
149 ---------------------------------------------------------------------------------
150
151 A: The process is the very same as described in the netdev subsystem
152 documentation at Documentation/process/maintainer-netdev.rst,
153 so please read up on it. The subject line must indicate whether the
154 patch is a fix or rather "next-like" content in order to let the
155 maintainers know whether it is targeted at bpf or bpf-next.
156
157 For fixes eventually landing in bpf -> net tree, the subject must
158 look like::
159
160 git format-patch --subject-prefix='PATCH bpf' start..finish
161
162 For features/improvements/etc that should eventually land in
163 bpf-next -> net-next, the subject must look like::
164
165 git format-patch --subject-prefix='PATCH bpf-next' start..finish
166
167 If unsure whether the patch or patch series should go into bpf
168 or net directly, or bpf-next or net-next directly, it is not a
169 problem either if the subject line says net or net-next as target.
170 It is eventually up to the maintainers to do the delegation of
171 the patches.
172
173 If it is clear that patches should go into bpf or bpf-next tree,
174 please make sure to rebase the patches against those trees in
175 order to reduce potential conflicts.
176
177 In case the patch or patch series has to be reworked and sent out
178 again in a second or later revision, it is also required to add a
179 version number (``v2``, ``v3``, ...) into the subject prefix::
180
181 git format-patch --subject-prefix='PATCH bpf-next v2' start..finish
182
183 When changes have been requested to the patch series, always send the
184 whole patch series again with the feedback incorporated (never send
185 individual diffs on top of the old series).
186
187 Q: What does it mean when a patch gets applied to bpf or bpf-next tree?
188 -----------------------------------------------------------------------
189 A: It means that the patch looks good for mainline inclusion from
190 a BPF point of view.
191
192 Be aware that this is not a final verdict that the patch will
193 automatically get accepted into net or net-next trees eventually:
194
195 On the bpf kernel mailing list reviews can come in at any point
196 in time. If discussions around a patch conclude that they cannot
197 get included as-is, we will either apply a follow-up fix or drop
198 them from the trees entirely. Therefore, we also reserve to rebase
199 the trees when deemed necessary. After all, the purpose of the tree
200 is to:
201
202 i) accumulate and stage BPF patches for integration into trees
203 like net and net-next, and
204
205 ii) run extensive BPF test suite and
206 workloads on the patches before they make their way any further.
207
208 Once the BPF pull request was accepted by David S. Miller, then
209 the patches end up in net or net-next tree, respectively, and
210 make their way from there further into mainline. Again, see the
211 documentation for netdev subsystem at
212 Documentation/process/maintainer-netdev.rst for additional information
213 e.g. on how often they are merged to mainline.
214
215 Q: How long do I need to wait for feedback on my BPF patches?
216 -------------------------------------------------------------
217 A: We try to keep the latency low. The usual time to feedback will
218 be around 2 or 3 business days. It may vary depending on the
219 complexity of changes and current patch load.
220
221 Q: How often do you send pull requests to major kernel trees like net or net-next?
222 ----------------------------------------------------------------------------------
223
224 A: Pull requests will be sent out rather often in order to not
225 accumulate too many patches in bpf or bpf-next.
226
227 As a rule of thumb, expect pull requests for each tree regularly
228 at the end of the week. In some cases pull requests could additionally
229 come also in the middle of the week depending on the current patch
230 load or urgency.
231
232 Q: Are patches applied to bpf-next when the merge window is open?
233 -----------------------------------------------------------------
234 A: For the time when the merge window is open, bpf-next will not be
235 processed. This is roughly analogous to net-next patch processing,
236 so feel free to read up on the netdev docs at
237 Documentation/process/maintainer-netdev.rst about further details.
238
239 During those two weeks of merge window, we might ask you to resend
240 your patch series once bpf-next is open again. Once Linus released
241 a ``v*-rc1`` after the merge window, we continue processing of bpf-next.
242
243 For non-subscribers to kernel mailing lists, there is also a status
244 page run by David S. Miller on net-next that provides guidance:
245
246 http://vger.kernel.org/~davem/net-next.html
247
248 Q: Verifier changes and test cases
249 ----------------------------------
250 Q: I made a BPF verifier change, do I need to add test cases for
251 BPF kernel selftests_?
252
253 A: If the patch has changes to the behavior of the verifier, then yes,
254 it is absolutely necessary to add test cases to the BPF kernel
255 selftests_ suite. If they are not present and we think they are
256 needed, then we might ask for them before accepting any changes.
257
258 In particular, test_verifier.c is tracking a high number of BPF test
259 cases, including a lot of corner cases that LLVM BPF back end may
260 generate out of the restricted C code. Thus, adding test cases is
261 absolutely crucial to make sure future changes do not accidentally
262 affect prior use-cases. Thus, treat those test cases as: verifier
263 behavior that is not tracked in test_verifier.c could potentially
264 be subject to change.
265
266 Q: samples/bpf preference vs selftests?
267 ---------------------------------------
268 Q: When should I add code to ``samples/bpf/`` and when to BPF kernel
269 selftests_?
270
271 A: In general, we prefer additions to BPF kernel selftests_ rather than
272 ``samples/bpf/``. The rationale is very simple: kernel selftests are
273 regularly run by various bots to test for kernel regressions.
274
275 The more test cases we add to BPF selftests, the better the coverage
276 and the less likely it is that those could accidentally break. It is
277 not that BPF kernel selftests cannot demo how a specific feature can
278 be used.
279
280 That said, ``samples/bpf/`` may be a good place for people to get started,
281 so it might be advisable that simple demos of features could go into
282 ``samples/bpf/``, but advanced functional and corner-case testing rather
283 into kernel selftests.
284
285 If your sample looks like a test case, then go for BPF kernel selftests
286 instead!
287
288 Q: When should I add code to the bpftool?
289 -----------------------------------------
290 A: The main purpose of bpftool (under tools/bpf/bpftool/) is to provide
291 a central user space tool for debugging and introspection of BPF programs
292 and maps that are active in the kernel. If UAPI changes related to BPF
293 enable for dumping additional information of programs or maps, then
294 bpftool should be extended as well to support dumping them.
295
296 Q: When should I add code to iproute2's BPF loader?
297 ---------------------------------------------------
298 A: For UAPI changes related to the XDP or tc layer (e.g. ``cls_bpf``),
299 the convention is that those control-path related changes are added to
300 iproute2's BPF loader as well from user space side. This is not only
301 useful to have UAPI changes properly designed to be usable, but also
302 to make those changes available to a wider user base of major
303 downstream distributions.
304
305 Q: Do you accept patches as well for iproute2's BPF loader?
306 -----------------------------------------------------------
307 A: Patches for the iproute2's BPF loader have to be sent to:
308
309 netdev@vger.kernel.org
310
311 While those patches are not processed by the BPF kernel maintainers,
312 please keep them in Cc as well, so they can be reviewed.
313
314 The official git repository for iproute2 is run by Stephen Hemminger
315 and can be found at:
316
317 https://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git/
318
319 The patches need to have a subject prefix of '``[PATCH iproute2
320 master]``' or '``[PATCH iproute2 net-next]``'. '``master``' or
321 '``net-next``' describes the target branch where the patch should be
322 applied to. Meaning, if kernel changes went into the net-next kernel
323 tree, then the related iproute2 changes need to go into the iproute2
324 net-next branch, otherwise they can be targeted at master branch. The
325 iproute2 net-next branch will get merged into the master branch after
326 the current iproute2 version from master has been released.
327
328 Like BPF, the patches end up in patchwork under the netdev project and
329 are delegated to 'shemminger' for further processing:
330
331 http://patchwork.ozlabs.org/project/netdev/list/?delegate=389
332
333 Q: What is the minimum requirement before I submit my BPF patches?
334 ------------------------------------------------------------------
335 A: When submitting patches, always take the time and properly test your
336 patches *prior* to submission. Never rush them! If maintainers find
337 that your patches have not been properly tested, it is a good way to
338 get them grumpy. Testing patch submissions is a hard requirement!
339
340 Note, fixes that go to bpf tree *must* have a ``Fixes:`` tag included.
341 The same applies to fixes that target bpf-next, where the affected
342 commit is in net-next (or in some cases bpf-next). The ``Fixes:`` tag is
343 crucial in order to identify follow-up commits and tremendously helps
344 for people having to do backporting, so it is a must have!
345
346 We also don't accept patches with an empty commit message. Take your
347 time and properly write up a high quality commit message, it is
348 essential!
349
350 Think about it this way: other developers looking at your code a month
351 from now need to understand *why* a certain change has been done that
352 way, and whether there have been flaws in the analysis or assumptions
353 that the original author did. Thus providing a proper rationale and
354 describing the use-case for the changes is a must.
355
356 Patch submissions with >1 patch must have a cover letter which includes
357 a high level description of the series. This high level summary will
358 then be placed into the merge commit by the BPF maintainers such that
359 it is also accessible from the git log for future reference.
360
361 Q: Features changing BPF JIT and/or LLVM
362 ----------------------------------------
363 Q: What do I need to consider when adding a new instruction or feature
364 that would require BPF JIT and/or LLVM integration as well?
365
366 A: We try hard to keep all BPF JITs up to date such that the same user
367 experience can be guaranteed when running BPF programs on different
368 architectures without having the program punt to the less efficient
369 interpreter in case the in-kernel BPF JIT is enabled.
370
371 If you are unable to implement or test the required JIT changes for
372 certain architectures, please work together with the related BPF JIT
373 developers in order to get the feature implemented in a timely manner.
374 Please refer to the git log (``arch/*/net/``) to locate the necessary
375 people for helping out.
376
377 Also always make sure to add BPF test cases (e.g. test_bpf.c and
378 test_verifier.c) for new instructions, so that they can receive
379 broad test coverage and help run-time testing the various BPF JITs.
380
381 In case of new BPF instructions, once the changes have been accepted
382 into the Linux kernel, please implement support into LLVM's BPF back
383 end. See LLVM_ section below for further information.
384
385 Q: What "BPF_INTERNAL" symbol namespace is for?
386 -----------------------------------------------
387 A: Symbols exported as BPF_INTERNAL can only be used by BPF infrastructure
388 like preload kernel modules with light skeleton. Most symbols outside
389 of BPF_INTERNAL are not expected to be used by code outside of BPF either.
390 Symbols may lack the designation because they predate the namespaces,
391 or due to an oversight.
392
393 Stable submission
394 =================
395
396 Q: I need a specific BPF commit in stable kernels. What should I do?
397 --------------------------------------------------------------------
398 A: In case you need a specific fix in stable kernels, first check whether
399 the commit has already been applied in the related ``linux-*.y`` branches:
400
401 https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/
402
403 If not the case, then drop an email to the BPF maintainers with the
404 netdev kernel mailing list in Cc and ask for the fix to be queued up:
405
406 netdev@vger.kernel.org
407
408 The process in general is the same as on netdev itself, see also the
409 the documentation on networking subsystem at
410 Documentation/process/maintainer-netdev.rst.
411
412 Q: Do you also backport to kernels not currently maintained as stable?
413 ----------------------------------------------------------------------
414 A: No. If you need a specific BPF commit in kernels that are currently not
415 maintained by the stable maintainers, then you are on your own.
416
417 The current stable and longterm stable kernels are all listed here:
418
419 https://www.kernel.org/
420
421 Q: The BPF patch I am about to submit needs to go to stable as well
422 -------------------------------------------------------------------
423 What should I do?
424
425 A: The same rules apply as with netdev patch submissions in general, see
426 the netdev docs at Documentation/process/maintainer-netdev.rst.
427
428 Never add "``Cc: stable@vger.kernel.org``" to the patch description, but
429 ask the BPF maintainers to queue the patches instead. This can be done
430 with a note, for example, under the ``---`` part of the patch which does
431 not go into the git log. Alternatively, this can be done as a simple
432 request by mail instead.
433
434 Q: Queue stable patches
435 -----------------------
436 Q: Where do I find currently queued BPF patches that will be submitted
437 to stable?
438
439 A: Once patches that fix critical bugs got applied into the bpf tree, they
440 are queued up for stable submission under:
441
442 http://patchwork.ozlabs.org/bundle/bpf/stable/?state=*
443
444 They will be on hold there at minimum until the related commit made its
445 way into the mainline kernel tree.
446
447 After having been under broader exposure, the queued patches will be
448 submitted by the BPF maintainers to the stable maintainers.
449
450 Testing patches
451 ===============
452
453 Q: How to run BPF selftests
454 ---------------------------
455 A: After you have booted into the newly compiled kernel, navigate to
456 the BPF selftests_ suite in order to test BPF functionality (current
457 working directory points to the root of the cloned git tree)::
458
459 $ cd tools/testing/selftests/bpf/
460 $ make
461
462 To run the verifier tests::
463
464 $ sudo ./test_verifier
465
466 The verifier tests print out all the current checks being
467 performed. The summary at the end of running all tests will dump
468 information of test successes and failures::
469
470 Summary: 418 PASSED, 0 FAILED
471
472 In order to run through all BPF selftests, the following command is
473 needed::
474
475 $ sudo make run_tests
476
477 See :doc:`kernel selftest documentation </dev-tools/kselftest>`
478 for details.
479
480 To maximize the number of tests passing, the .config of the kernel
481 under test should match the config file fragment in
482 tools/testing/selftests/bpf as closely as possible.
483
484 Finally to ensure support for latest BPF Type Format features -
485 discussed in Documentation/bpf/btf.rst - pahole version 1.16
486 is required for kernels built with CONFIG_DEBUG_INFO_BTF=y.
487 pahole is delivered in the dwarves package or can be built
488 from source at
489
490 https://github.com/acmel/dwarves
491
492 pahole starts to use libbpf definitions and APIs since v1.13 after the
493 commit 21507cd3e97b ("pahole: add libbpf as submodule under lib/bpf").
494 It works well with the git repository because the libbpf submodule will
495 use "git submodule update --init --recursive" to update.
496
497 Unfortunately, the default github release source code does not contain
498 libbpf submodule source code and this will cause build issues, the tarball
499 from https://git.kernel.org/pub/scm/devel/pahole/pahole.git/ is same with
500 github, you can get the source tarball with corresponding libbpf submodule
501 codes from
502
503 https://fedorapeople.org/~acme/dwarves
504
505 Some distros have pahole version 1.16 packaged already, e.g.
506 Fedora, Gentoo.
507
508 Q: Which BPF kernel selftests version should I run my kernel against?
509 ---------------------------------------------------------------------
510 A: If you run a kernel ``xyz``, then always run the BPF kernel selftests
511 from that kernel ``xyz`` as well. Do not expect that the BPF selftest
512 from the latest mainline tree will pass all the time.
513
514 In particular, test_bpf.c and test_verifier.c have a large number of
515 test cases and are constantly updated with new BPF test sequences, or
516 existing ones are adapted to verifier changes e.g. due to verifier
517 becoming smarter and being able to better track certain things.
518
519 LLVM
520 ====
521
522 Q: Where do I find LLVM with BPF support?
523 -----------------------------------------
524 A: The BPF back end for LLVM is upstream in LLVM since version 3.7.1.
525
526 All major distributions these days ship LLVM with BPF back end enabled,
527 so for the majority of use-cases it is not required to compile LLVM by
528 hand anymore, just install the distribution provided package.
529
530 LLVM's static compiler lists the supported targets through
531 ``llc --version``, make sure BPF targets are listed. Example::
532
533 $ llc --version
534 LLVM (http://llvm.org/):
535 LLVM version 10.0.0
536 Optimized build.
537 Default target: x86_64-unknown-linux-gnu
538 Host CPU: skylake
539
540 Registered Targets:
541 aarch64 - AArch64 (little endian)
542 bpf - BPF (host endian)
543 bpfeb - BPF (big endian)
544 bpfel - BPF (little endian)
545 x86 - 32-bit X86: Pentium-Pro and above
546 x86-64 - 64-bit X86: EM64T and AMD64
547
548 For developers in order to utilize the latest features added to LLVM's
549 BPF back end, it is advisable to run the latest LLVM releases. Support
550 for new BPF kernel features such as additions to the BPF instruction
551 set are often developed together.
552
553 All LLVM releases can be found at: http://releases.llvm.org/
554
555 Q: Got it, so how do I build LLVM manually anyway?
556 --------------------------------------------------
557 A: We recommend that developers who want the fastest incremental builds
558 use the Ninja build system, you can find it in your system's package
559 manager, usually the package is ninja or ninja-build.
560
561 You need ninja, cmake and gcc-c++ as build requisites for LLVM. Once you
562 have that set up, proceed with building the latest LLVM and clang version
563 from the git repositories::
564
565 $ git clone https://github.com/llvm/llvm-project.git
566 $ mkdir -p llvm-project/llvm/build
567 $ cd llvm-project/llvm/build
568 $ cmake .. -G "Ninja" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
569 -DLLVM_ENABLE_PROJECTS="clang" \
570 -DCMAKE_BUILD_TYPE=Release \
571 -DLLVM_BUILD_RUNTIME=OFF
572 $ ninja
573
574 The built binaries can then be found in the build/bin/ directory, where
575 you can point the PATH variable to.
576
577 Set ``-DLLVM_TARGETS_TO_BUILD`` equal to the target you wish to build, you
578 will find a full list of targets within the llvm-project/llvm/lib/Target
579 directory.
580
581 Q: Reporting LLVM BPF issues
582 ----------------------------
583 Q: Should I notify BPF kernel maintainers about issues in LLVM's BPF code
584 generation back end or about LLVM generated code that the verifier
585 refuses to accept?
586
587 A: Yes, please do!
588
589 LLVM's BPF back end is a key piece of the whole BPF
590 infrastructure and it ties deeply into verification of programs from the
591 kernel side. Therefore, any issues on either side need to be investigated
592 and fixed whenever necessary.
593
594 Therefore, please make sure to bring them up at netdev kernel mailing
595 list and Cc BPF maintainers for LLVM and kernel bits:
596
597 * Yonghong Song <yhs@fb.com>
598 * Alexei Starovoitov <ast@kernel.org>
599 * Daniel Borkmann <daniel@iogearbox.net>
600
601 LLVM also has an issue tracker where BPF related bugs can be found:
602
603 https://bugs.llvm.org/buglist.cgi?quicksearch=bpf
604
605 However, it is better to reach out through mailing lists with having
606 maintainers in Cc.
607
608 Q: New BPF instruction for kernel and LLVM
609 ------------------------------------------
610 Q: I have added a new BPF instruction to the kernel, how can I integrate
611 it into LLVM?
612
613 A: LLVM has a ``-mcpu`` selector for the BPF back end in order to allow
614 the selection of BPF instruction set extensions. Before llvm version 20,
615 the ``generic`` processor target is used, which is the base instruction
616 set (v1) of BPF. Since llvm 20, the default processor target has changed
617 to instruction set v3.
618
619 LLVM has an option to select ``-mcpu=probe`` where it will probe the host
620 kernel for supported BPF instruction set extensions and selects the
621 optimal set automatically.
622
623 For cross-compilation, a specific version can be select manually as well ::
624
625 $ llc -march bpf -mcpu=help
626 Available CPUs for this target:
627
628 generic - Select the generic processor.
629 probe - Select the probe processor.
630 v1 - Select the v1 processor.
631 v2 - Select the v2 processor.
632 [...]
633
634 Newly added BPF instructions to the Linux kernel need to follow the same
635 scheme, bump the instruction set version and implement probing for the
636 extensions such that ``-mcpu=probe`` users can benefit from the
637 optimization transparently when upgrading their kernels.
638
639 If you are unable to implement support for the newly added BPF instruction
640 please reach out to BPF developers for help.
641
642 By the way, the BPF kernel selftests run with ``-mcpu=probe`` for better
643 test coverage.
644
645 Q: clang flag for target bpf?
646 -----------------------------
647 Q: In some cases clang flag ``--target=bpf`` is used but in other cases the
648 default clang target, which matches the underlying architecture, is used.
649 What is the difference and when I should use which?
650
651 A: Although LLVM IR generation and optimization try to stay architecture
652 independent, ``--target=<arch>`` still has some impact on generated code:
653
654 - BPF program may recursively include header file(s) with file scope
655 inline assembly codes. The default target can handle this well,
656 while ``bpf`` target may fail if bpf backend assembler does not
657 understand these assembly codes, which is true in most cases.
658
659 - When compiled without ``-g``, additional elf sections, e.g.,
660 .eh_frame and .rela.eh_frame, may be present in the object file
661 with default target, but not with ``bpf`` target.
662
663 - The default target may turn a C switch statement into a switch table
664 lookup and jump operation. Since the switch table is placed
665 in the global readonly section, the bpf program will fail to load.
666 The bpf target does not support switch table optimization.
667 The clang option ``-fno-jump-tables`` can be used to disable
668 switch table generation.
669
670 - For clang ``--target=bpf``, it is guaranteed that pointer or long /
671 unsigned long types will always have a width of 64 bit, no matter
672 whether underlying clang binary or default target (or kernel) is
673 32 bit. However, when native clang target is used, then it will
674 compile these types based on the underlying architecture's conventions,
675 meaning in case of 32 bit architecture, pointer or long / unsigned
676 long types e.g. in BPF context structure will have width of 32 bit
677 while the BPF LLVM back end still operates in 64 bit. The native
678 target is mostly needed in tracing for the case of walking ``pt_regs``
679 or other kernel structures where CPU's register width matters.
680 Otherwise, ``clang --target=bpf`` is generally recommended.
681
682 You should use default target when:
683
684 - Your program includes a header file, e.g., ptrace.h, which eventually
685 pulls in some header files containing file scope host assembly codes.
686
687 - You can add ``-fno-jump-tables`` to work around the switch table issue.
688
689 Otherwise, you can use ``bpf`` target. Additionally, you *must* use bpf target
690 when:
691
692 - Your program uses data structures with pointer or long / unsigned long
693 types that interface with BPF helpers or context data structures. Access
694 into these structures is verified by the BPF verifier and may result
695 in verification failures if the native architecture is not aligned with
696 the BPF architecture, e.g. 64-bit. An example of this is
697 BPF_PROG_TYPE_SK_MSG require ``--target=bpf``
698
699
700 .. Links
701 .. _selftests:
702 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/bpf/
703
704 Happy BPF hacking!
705

3. 한국어 전문 번역

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

BPF subsystem 협업 안내

1-16

이 문서는 BPF subsystem에서 bug를 report하고 patch를 제출하며 stable kernel용 patch를 queue하는 여러 workflow를 설명합니다.

일반적인 patch 제출 방법은 `Documentation/process/submitting-patches.rst`를 참조하십시오. 이 문서는 BPF에만 해당하는 추가 사항을 다룹니다.

BPF bug report

17-43

질문: BPF kernel code의 bug는 어떻게 report합니까? 답변: BPF kernel 개발뿐 아니라 bpftool과 iproute2 BPF loader 개발도 모두 BPF kernel mailing list에서 이루어지므로, BPF와 관련해 발견한 문제는 `bpf@vger.kernel.org`로 report하십시오. XDP, BPF tracing 등에 관련된 문제도 여기에 포함됩니다.

netdev traffic 양이 많으므로 kernel `MAINTAINERS` file에 기재된 BPF maintainer도 Cc에 추가하십시오.

  • Alexei Starovoitov <ast@kernel.org>
  • Daniel Borkmann <daniel@iogearbox.net>

문제가 있는 commit을 이미 식별했다면 실제 commit author도 report의 Cc에 반드시 포함하십시오. 보통 kernel git tree에서 author를 찾을 수 있습니다.

BPF 문제를 `bugzilla.kernel.org`에 report하지 마십시오. 그곳에 올린 문제는 간과된다고 보아야 합니다.

Review 전 BPF CI 실행

44-73

질문: 변경 사항을 review에 보내기 전에 BPF CI를 어떻게 실행합니까? 답변: BPF CI는 GitHub 기반이며 `https://github.com/kernel-patches/bpf`에서 운영됩니다. GitHub CLI로도 같은 결과를 얻을 수 있지만 여기서는 UI 기반 workflow를 설명합니다.

patch에 대한 CI run을 시작하는 절차는 다음과 같습니다.

  • 앞서 언급한 repository를 자신의 account로 fork합니다. 이 작업은 한 번만 하면 됩니다.
  • fork를 local에 clone하고 `bpf-next` 또는 `bpf` branch를 tracking하는 새 branch를 checkout한 뒤, test할 patch를 그 위에 적용합니다.
  • local branch를 자신의 fork에 push하고 각각 `kernel-patches/bpf`의 `bpf-next_base` 또는 `bpf_base` branch를 대상으로 pull request를 만듭니다.

pull request를 만들면 곧 CI workflow가 실행됩니다. upstream에 제출된 patch 검사와 capacity를 공유하므로 사용량에 따라 완료까지 시간이 걸릴 수 있습니다.

`bpf-next_base`와 `bpf_base`는 tracking하는 upstream branch에 patch가 push될 때 갱신됩니다. 따라서 해당 patch set도 자동 rebase가 시도됩니다. 이 동작 때문에 CI run이 중단되고 새 baseline에서 다시 시작될 수 있습니다.

Mailing list와 patchwork

74-109

질문: BPF patch를 어느 mailing list에 제출해야 합니까? 답변: BPF kernel mailing list인 `bpf@vger.kernel.org`에 제출하십시오.

patch가 networking, tracing, security 등 여러 subsystem을 변경한다면 관련 kernel mailing list와 maintainer도 Cc에 포함해야 합니다. 그래야 이들이 변경을 review하고 patch에 `Acked-by`를 제공할 수 있습니다.

질문: BPF subsystem에서 현재 논의 중인 patch는 어디에서 찾습니까? 답변: netdev에 Cc된 모든 patch는 netdev patchwork project인 `https://patchwork.kernel.org/project/netdevbpf/list/`에서 review queue에 들어갑니다.

BPF 대상 patch는 BPF maintainer가 처리하도록 `bpf` delegate에 할당됩니다. 현재 review 중인 queue는 `https://patchwork.kernel.org/project/netdevbpf/list/?delegate=121173`에서 확인할 수 있습니다.

BPF community 전체가 review하고 BPF maintainer가 승인하면 patchwork status가 `Accepted`로 바뀌고 제출자에게 mail이 전달됩니다. 이는 BPF 관점에서 patch가 적절하며 두 BPF kernel tree 중 하나에 적용됐다는 뜻입니다.

community feedback 때문에 patch를 다시 작성해야 하면 status는 `Changes Requested`가 되고 현재 review queue에서 제거됩니다. patch가 reject되거나 BPF tree에 적용할 수 없지만 `bpf` delegate에 할당된 경우도 마찬가지입니다.

BPF kernel tree와 mainline 경로

110-147

질문: 변경 사항은 어떤 경로로 Linux에 들어갑니까? 답변: BPF maintainer가 patch를 accept하면 두 BPF kernel tree 중 하나에 적용합니다.

  • https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/
  • https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/

`bpf` tree는 fix 전용이며 `bpf-next`는 feature, cleanup, 그 밖의 improvement 같은 "next-like" content용입니다. networking의 `net`과 `net-next` 관계와 같습니다. rebase할 branch 선택을 단순화하기 위해 `bpf`와 `bpf-next`에는 모두 `master` branch만 있습니다.

`bpf`에 쌓인 patch는 정기적으로 `net` kernel tree로 pull되고, `bpf-next`에서 accept된 patch는 `net-next`로 이동합니다. David S. Miller가 `net`과 `net-next`를 모두 관리하며, 그 뒤 Linus Torvalds가 관리하는 kernel mainline tree로 들어갑니다. `net`과 `net-next`가 mainline에 merge되는 과정은 `Documentation/process/maintainer-netdev.rst`를 참조하십시오.

merge conflict를 막기 위해 가끔 patch 일부를 tracing 같은 다른 tree에 pull request로 보낼 수 있지만, 주된 integration 대상은 항상 `net`과 `net-next`입니다.

pull request에는 누적 patch의 high-level summary가 들어갑니다. `yyyy-mm-dd`가 pull request 날짜일 때 netdev kernel mailing list에서 다음 subject로 찾을 수 있습니다.

pull-request: bpf yyyy-mm-dd
pull-request: bpf-next yyyy-mm-dd

Target tree와 subject prefix

148-186

질문: patch를 `bpf`와 `bpf-next` 중 어느 tree에 적용해야 하는지 어떻게 표시합니까? 답변: 절차는 `Documentation/process/maintainer-netdev.rst`의 netdev subsystem 설명과 같습니다. maintainer가 `bpf` 또는 `bpf-next` 대상을 알 수 있도록 subject line에 fix인지 "next-like" content인지 표시해야 합니다.

결국 `bpf -> net` tree로 들어갈 fix의 subject는 다음처럼 만듭니다.

git format-patch --subject-prefix='PATCH bpf' start..finish

`bpf-next -> net-next`로 들어갈 feature, improvement 등의 subject는 다음처럼 만듭니다.

git format-patch --subject-prefix='PATCH bpf-next' start..finish

patch 또는 series가 `bpf`와 `net` 중 어디로, 또는 `bpf-next`와 `net-next` 중 어디로 직접 가야 하는지 확실하지 않다면 subject line의 target을 `net`이나 `net-next`로 적어도 괜찮습니다. 최종 delegation은 maintainer가 결정합니다.

`bpf` 또는 `bpf-next`로 가야 함이 명확하면 잠재적 conflict를 줄이도록 해당 tree를 기준으로 patch를 rebase하십시오.

patch 또는 series를 수정해 두 번째 이상 revision으로 다시 보낼 때는 subject prefix에 `v2`, `v3` 같은 version number도 넣어야 합니다.

git format-patch --subject-prefix='PATCH bpf-next v2' start..finish

patch series에 변경 요청을 받았다면 feedback을 반영한 전체 series를 항상 다시 보내십시오. 이전 series 위에 개별 diff만 보내면 안 됩니다.

BPF tree 적용의 의미

187-214

질문: patch가 `bpf` 또는 `bpf-next` tree에 적용됐다는 것은 무엇을 뜻합니까? 답변: BPF 관점에서 mainline inclusion에 적합해 보인다는 뜻입니다.

다만 patch가 결국 `net`이나 `net-next`에 자동으로 accept된다는 최종 판정은 아닙니다. BPF kernel mailing list에는 언제든 review가 들어올 수 있습니다. 논의 결과 patch를 그대로 포함할 수 없다면 follow-up fix를 적용하거나 tree에서 완전히 drop할 수 있으며, 필요하면 tree를 rebase할 수도 있습니다.

이 tree의 목적은 다음 두 가지입니다.

  • BPF patch를 누적하고 stage해 `net`, `net-next` 같은 tree로 통합합니다.
  • patch가 더 진행되기 전에 광범위한 BPF test suite와 workload를 실행합니다.

David S. Miller가 BPF pull request를 accept하면 patch는 각각 `net` 또는 `net-next` tree에 들어가고 그곳에서 mainline으로 진행합니다. mainline merge 주기 등 자세한 내용은 `Documentation/process/maintainer-netdev.rst`를 다시 참조하십시오.

Feedback 주기와 merge window

215-247

질문: BPF patch feedback을 얼마나 기다려야 합니까? 답변: latency를 낮게 유지하려 하며 보통 영업일 기준 2~3일 안에 feedback을 줍니다. 변경 복잡도와 현재 patch load에 따라 달라질 수 있습니다.

질문: `net`이나 `net-next` 같은 주요 kernel tree에 pull request를 얼마나 자주 보냅니까? 답변: `bpf` 또는 `bpf-next`에 너무 많은 patch가 쌓이지 않도록 비교적 자주 보냅니다. 대략 각 tree에 대해 매주 말 정기적으로 보내며, patch load나 긴급성에 따라 주중에도 추가로 보낼 수 있습니다.

질문: merge window가 열려 있을 때 `bpf-next`에 patch를 적용합니까? 답변: merge window 동안에는 `bpf-next`를 처리하지 않습니다. `net-next` patch 처리와 대체로 같으므로 자세한 내용은 `Documentation/process/maintainer-netdev.rst`를 참조하십시오.

약 2주인 merge window 동안에는 `bpf-next`가 다시 열린 뒤 patch series를 재전송해 달라고 요청할 수 있습니다. Linus가 merge window 뒤 `v*-rc1`을 release하면 `bpf-next` 처리를 계속합니다.

kernel mailing list 비구독자를 위해 David S. Miller가 운영하는 `net-next` status page `http://vger.kernel.org/~davem/net-next.html`도 안내를 제공합니다.

Verifier 변경과 test case

248-265

질문: BPF verifier를 변경했다면 BPF kernel selftests에 test case를 추가해야 합니까? 답변: patch가 verifier behavior를 바꾼다면 반드시 추가해야 합니다. 필요한데 빠졌다고 판단하면 변경을 accept하기 전에 test 추가를 요청할 수 있습니다.

특히 `test_verifier.c`는 LLVM BPF back end가 제한된 C code에서 생성할 수 있는 많은 corner case를 포함해 수많은 BPF test case를 추적합니다. 미래 변경이 기존 use case에 뜻하지 않은 영향을 주지 않게 하려면 test case 추가가 매우 중요합니다.

따라서 `test_verifier.c`가 추적하지 않는 verifier behavior는 잠재적으로 변경될 수 있다고 간주하십시오.

samples/bpf와 selftests 선택

266-286

질문: 언제 `samples/bpf/`에 code를 추가하고 언제 BPF kernel selftests에 추가해야 합니까? 답변: 일반적으로 `samples/bpf/`보다 BPF kernel selftests 추가를 선호합니다. kernel selftest는 여러 bot이 정기 실행해 kernel regression을 검사하기 때문입니다.

BPF selftest에 test case가 많을수록 coverage가 좋아지고 우연히 깨질 가능성이 낮아집니다. BPF kernel selftest도 특정 feature의 사용법을 시연할 수 있습니다.

다만 `samples/bpf/`는 처음 시작하는 사람에게 좋은 곳이므로 간단한 feature demo는 그곳에 둘 수 있습니다. 고급 functional test와 corner-case test는 kernel selftest에 두는 편이 좋습니다.

sample이 test case처럼 보인다면 BPF kernel selftest를 선택하십시오.

bpftool과 iproute2 BPF loader

287-332

질문: 언제 bpftool에 code를 추가해야 합니까? 답변: `tools/bpf/bpftool/`의 bpftool은 kernel에서 활성 상태인 BPF program과 map을 debug하고 introspection하는 중앙 userspace tool입니다. BPF 관련 UAPI 변경으로 program이나 map의 추가 정보를 dump할 수 있게 된다면 bpftool도 이를 dump하도록 확장해야 합니다.

질문: 언제 iproute2 BPF loader에 code를 추가해야 합니까? 답변: XDP나 tc layer, 예를 들어 `cls_bpf`와 관련된 UAPI 변경은 관례상 userspace 쪽 iproute2 BPF loader의 control path에도 추가합니다. 이는 UAPI를 실제 사용 가능하게 올바르게 설계하는 데 유용할 뿐 아니라 주요 downstream distribution의 더 넓은 user base에 변경을 제공하는 방법이기도 합니다.

질문: iproute2 BPF loader patch도 받습니까? 답변: 해당 patch는 `netdev@vger.kernel.org`로 보내야 합니다. BPF kernel maintainer가 직접 처리하지는 않지만 review할 수 있도록 이들도 Cc에 남겨 두십시오.

Stephen Hemminger가 운영하는 공식 iproute2 git repository는 `https://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git/`입니다.

patch subject prefix는 `[PATCH iproute2 master]` 또는 `[PATCH iproute2 net-next]`여야 합니다. `master`와 `net-next`는 patch를 적용할 target branch를 뜻합니다. kernel 변경이 `net-next` kernel tree에 들어갔다면 관련 iproute2 변경도 iproute2 `net-next` branch로 보내고, 그렇지 않으면 `master`를 대상으로 할 수 있습니다. 현재 iproute2 version이 `master`에서 release된 뒤 iproute2 `net-next` branch가 `master`로 merge됩니다.

BPF와 마찬가지로 patch는 netdev project의 patchwork에 들어가고 `shemminger`에게 delegate됩니다. queue는 `http://patchwork.ozlabs.org/project/netdev/list/?delegate=389`에서 확인할 수 있습니다.

Patch 제출 전 최소 요건

333-360

질문: BPF patch를 제출하기 위한 최소 요건은 무엇입니까? 답변: 제출 전에 충분한 시간을 들여 patch를 올바르게 test하십시오. 서두르지 마십시오. maintainer가 제대로 test하지 않았음을 발견하면 좋지 않은 인상을 주며, 제출 patch의 test는 필수 요건입니다.

`bpf` tree로 가는 fix에는 반드시 `Fixes:` tag가 있어야 합니다. 영향받은 commit이 `net-next` 또는 경우에 따라 `bpf-next`에 있을 때 `bpf-next`를 대상으로 하는 fix에도 같은 규칙이 적용됩니다. `Fixes:` tag는 follow-up commit을 식별하고 backport 작업을 크게 도우므로 필수입니다.

빈 commit message가 있는 patch도 accept하지 않습니다. 시간을 들여 고품질 commit message를 작성해야 합니다.

한 달 뒤 code를 보는 다른 developer가 특정 변경을 왜 그런 방식으로 했는지, 원 author의 분석이나 가정에 결함이 있었는지 이해할 수 있어야 합니다. 따라서 적절한 근거와 변경의 use case를 설명해야 합니다.

patch가 두 개 이상인 제출에는 series의 high-level description을 담은 cover letter가 반드시 있어야 합니다. 이 summary는 BPF maintainer가 merge commit에 넣으므로 이후에도 git log에서 참조할 수 있습니다.

BPF JIT·LLVM과 BPF_INTERNAL

361-392

질문: BPF JIT 또는 LLVM integration이 필요한 새 instruction이나 feature를 추가할 때 무엇을 고려해야 합니까? 답변: in-kernel BPF JIT가 활성화된 환경에서 program이 덜 효율적인 interpreter로 넘어가지 않고 architecture가 달라도 같은 user experience를 보장할 수 있도록 모든 BPF JIT를 최신 상태로 유지하려 노력합니다.

특정 architecture에 필요한 JIT 변경을 구현하거나 test할 수 없다면 관련 BPF JIT developer와 협력해 제때 feature를 구현하십시오. 도움을 줄 사람은 git log의 `arch/*/net/`에서 찾을 수 있습니다.

새 instruction에는 `test_bpf.c`, `test_verifier.c` 같은 BPF test case를 항상 추가해 넓은 coverage를 확보하고 여러 BPF JIT를 runtime에 test할 수 있게 하십시오.

새 BPF instruction 변경이 Linux kernel에 accept된 뒤에는 LLVM BPF back end에도 support를 구현하십시오. 자세한 내용은 아래 LLVM section을 참조하십시오.

질문: `BPF_INTERNAL` symbol namespace는 무엇을 위한 것입니까? 답변: `BPF_INTERNAL`로 export한 symbol은 light skeleton을 사용하는 preload kernel module 같은 BPF infrastructure만 사용할 수 있습니다. `BPF_INTERNAL` 밖의 symbol 대부분도 BPF 외부 code가 사용할 것으로 기대하지 않습니다. namespace보다 오래된 symbol이거나 누락 때문에 designation이 없을 수 있습니다.

Stable kernel에 특정 fix 요청

393-420

질문: stable kernel에 특정 BPF commit이 필요하면 어떻게 합니까? 답변: 먼저 관련 `linux-*.y` branch에 그 commit이 이미 적용됐는지 `https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/`에서 확인하십시오.

적용되지 않았다면 `netdev@vger.kernel.org` kernel mailing list를 Cc에 넣고 BPF maintainer에게 mail을 보내 fix를 queue해 달라고 요청하십시오.

일반 절차는 netdev 자체와 같으며 networking subsystem 문서 `Documentation/process/maintainer-netdev.rst`도 참조하십시오.

질문: 현재 stable로 유지보수되지 않는 kernel에도 backport합니까? 답변: 하지 않습니다. stable maintainer가 현재 유지하지 않는 kernel에 특정 BPF commit이 필요하면 직접 처리해야 합니다.

현재 stable 및 longterm stable kernel 목록은 `https://www.kernel.org/`에 있습니다.

Stable 제출과 queue

421-449

질문: 제출하려는 BPF patch가 stable에도 들어가야 하면 어떻게 합니까? 답변: 일반 netdev patch 제출과 같은 규칙이 적용되므로 `Documentation/process/maintainer-netdev.rst`를 참조하십시오.

patch description에 `Cc: stable@vger.kernel.org`를 절대 추가하지 말고 BPF maintainer에게 patch를 queue해 달라고 요청하십시오. git log에는 들어가지 않는 patch의 `---` 아래에 note를 적거나 단순히 mail로 요청할 수 있습니다.

질문: stable로 제출될 예정인 BPF patch queue는 어디에서 찾습니까? 답변: critical bug를 고친 patch가 `bpf` tree에 적용되면 `http://patchwork.ozlabs.org/bundle/bpf/stable/?state=*` 아래에서 stable 제출용으로 queue됩니다.

관련 commit이 mainline kernel tree에 들어갈 때까지 최소한 그곳에서 hold됩니다. 더 넓은 환경에 노출된 뒤 BPF maintainer가 queue된 patch를 stable maintainer에게 제출합니다.

BPF selftest 실행과 BTF 준비

450-490

질문: BPF selftest를 어떻게 실행합니까? 답변: 새로 compile한 kernel로 boot한 뒤 clone한 git tree root를 현재 directory로 두고 BPF selftests suite로 이동해 build합니다.

$ cd tools/testing/selftests/bpf/
$ make

verifier test는 다음처럼 실행합니다.

$ sudo ./test_verifier

verifier test는 현재 수행하는 모든 check를 출력합니다. 전체 test가 끝나면 성공과 실패 정보를 summary로 표시합니다.

Summary: 418 PASSED, 0 FAILED

모든 BPF selftest를 실행하려면 다음 command가 필요합니다.

$ sudo make run_tests

자세한 내용은 kernel selftest 문서 `</dev-tools/kselftest>`를 참조하십시오. 통과하는 test 수를 최대화하려면 test 대상 kernel의 `.config`를 `tools/testing/selftests/bpf`에 있는 config file fragment와 최대한 가깝게 맞춰야 합니다.

`Documentation/bpf/btf.rst`에서 다루는 최신 BPF Type Format feature를 지원하려면 `CONFIG_DEBUG_INFO_BTF=y`로 build하는 kernel에 pahole version 1.16이 필요합니다. pahole은 dwarves package로 제공되며 `https://github.com/acmel/dwarves` source에서 build할 수도 있습니다.

pahole source와 selftest version

491-518

pahole은 commit `21507cd3e97b`("pahole: add libbpf as submodule under lib/bpf") 이후 v1.13부터 libbpf definition과 API를 사용합니다. git repository에서는 `git submodule update --init --recursive`가 libbpf submodule을 갱신하므로 잘 동작합니다.

하지만 기본 GitHub release source code에는 libbpf submodule source가 없어 build 문제가 생깁니다. `https://git.kernel.org/pub/scm/devel/pahole/pahole.git/`의 tarball도 GitHub와 같습니다. 해당 libbpf submodule code를 포함한 source tarball은 `https://fedorapeople.org/~acme/dwarves`에서 받을 수 있습니다.

Fedora와 Gentoo 등 일부 distribution은 이미 pahole version 1.16 package를 제공합니다.

질문: kernel을 어느 BPF kernel selftest version으로 test해야 합니까? 답변: kernel `xyz`를 실행한다면 항상 같은 kernel `xyz`의 BPF kernel selftest를 실행하십시오. 최신 mainline tree의 BPF selftest가 언제나 통과할 것이라고 기대하면 안 됩니다.

특히 `test_bpf.c`와 `test_verifier.c`에는 많은 test case가 있으며 새 BPF test sequence가 계속 추가됩니다. verifier가 더 똑똑해져 특정 항목을 더 잘 추적하는 등의 변경에 맞춰 기존 sequence도 계속 수정됩니다.

LLVM BPF back end 확인

519-554

질문: BPF를 지원하는 LLVM은 어디에서 찾습니까? 답변: LLVM의 BPF back end는 version 3.7.1부터 upstream에 포함됐습니다.

현재 모든 주요 distribution이 BPF back end를 활성화한 LLVM을 제공하므로 대부분의 use case에서는 LLVM을 직접 compile할 필요 없이 distribution package를 설치하면 됩니다.

LLVM static compiler가 지원하는 target은 `llc --version`으로 나열할 수 있습니다. 출력에 BPF target이 있는지 확인하십시오. 예시는 다음과 같습니다.

$ llc --version
LLVM (http://llvm.org/):
  LLVM version 10.0.0
  Optimized build.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: skylake

  Registered Targets:
    aarch64    - AArch64 (little endian)
    bpf        - BPF (host endian)
    bpfeb      - BPF (big endian)
    bpfel      - BPF (little endian)
    x86        - 32-bit X86: Pentium-Pro and above
    x86-64     - 64-bit X86: EM64T and AMD64

LLVM BPF back end에 추가된 최신 feature를 활용하려는 developer는 최신 LLVM release를 사용하는 편이 좋습니다. BPF instruction set 추가 같은 새 BPF kernel feature는 흔히 LLVM support와 함께 개발됩니다.

모든 LLVM release는 `http://releases.llvm.org/`에서 찾을 수 있습니다.

LLVM 직접 build

555-580

질문: 그래도 LLVM을 직접 build하려면 어떻게 합니까? 답변: 가장 빠른 incremental build를 원하는 developer에게 Ninja build system을 권장합니다. system package manager에서 보통 `ninja` 또는 `ninja-build` package로 찾을 수 있습니다.

LLVM build prerequisite로 ninja, cmake, gcc-c++가 필요합니다. 준비한 뒤 git repository의 최신 LLVM과 clang을 다음처럼 build합니다.

$ git clone https://github.com/llvm/llvm-project.git
$ mkdir -p llvm-project/llvm/build
$ cd llvm-project/llvm/build
$ cmake .. -G "Ninja" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
           -DLLVM_ENABLE_PROJECTS="clang"    \
           -DCMAKE_BUILD_TYPE=Release        \
           -DLLVM_BUILD_RUNTIME=OFF
$ ninja

build된 binary는 `build/bin/` directory에 있으며 이 directory를 `PATH` variable로 지정할 수 있습니다.

`-DLLVM_TARGETS_TO_BUILD`는 build하려는 target과 같게 설정하십시오. 전체 target 목록은 `llvm-project/llvm/lib/Target` directory에서 찾을 수 있습니다.

LLVM BPF 문제 report

581-607

질문: LLVM BPF code generation back end 문제나 LLVM이 생성했지만 verifier가 거부하는 code 문제를 BPF kernel maintainer에게 알려야 합니까? 답변: 반드시 알려야 합니다.

LLVM BPF back end는 전체 BPF infrastructure의 핵심이며 kernel 쪽 program verification과 깊이 연결됩니다. 어느 쪽에서든 발생한 문제는 조사하고 필요하면 수정해야 합니다.

따라서 netdev kernel mailing list에 문제를 제기하고 LLVM 및 kernel 부분의 BPF maintainer를 Cc에 포함하십시오.

  • Yonghong Song <yhs@fb.com>
  • Alexei Starovoitov <ast@kernel.org>
  • Daniel Borkmann <daniel@iogearbox.net>

LLVM에는 BPF 관련 bug를 찾을 수 있는 issue tracker `https://bugs.llvm.org/buglist.cgi?quicksearch=bpf`도 있습니다. 하지만 maintainer를 Cc에 넣어 mailing list로 연락하는 편이 더 좋습니다.

새 BPF instruction의 LLVM 통합

608-644

질문: kernel에 새 BPF instruction을 추가했다면 LLVM에 어떻게 통합합니까? 답변: LLVM BPF back end에는 BPF instruction set extension을 선택하는 `-mcpu` selector가 있습니다. LLVM version 20 전에는 BPF base instruction set v1인 `generic` processor target을 사용했습니다. LLVM 20부터 default processor target은 instruction set v3로 바뀌었습니다.

LLVM에는 `-mcpu=probe` option도 있습니다. host kernel이 지원하는 BPF instruction set extension을 probe해 최적 집합을 자동 선택합니다.

cross-compilation에서는 특정 version을 수동으로 선택할 수도 있습니다.

$ llc -march bpf -mcpu=help
Available CPUs for this target:

  generic - Select the generic processor.
  probe   - Select the probe processor.
  v1      - Select the v1 processor.
  v2      - Select the v2 processor.
[...]

Linux kernel에 새로 추가하는 BPF instruction도 같은 scheme을 따라야 합니다. instruction set version을 올리고 extension probing을 구현해, `-mcpu=probe` user가 kernel upgrade 시 자동으로 optimization의 이점을 얻도록 하십시오.

새 BPF instruction support를 구현할 수 없다면 BPF developer에게 도움을 요청하십시오. BPF kernel selftest도 더 나은 test coverage를 위해 `-mcpu=probe`로 실행됩니다.

clang target이 생성 code에 미치는 영향

645-681

질문: 어떤 경우에는 clang flag `--target=bpf`를 쓰고 다른 경우에는 underlying architecture와 일치하는 default clang target을 씁니다. 차이는 무엇이며 언제 어느 것을 사용해야 합니까? 답변: LLVM IR 생성과 optimization은 architecture independent하게 유지하려 하지만 `--target=<arch>`는 생성 code에 여전히 영향을 줍니다.

  • BPF program이 file-scope inline assembly code가 있는 header file을 재귀적으로 include할 수 있습니다. default target은 이를 잘 처리하지만 BPF backend assembler는 대부분 이 assembly code를 이해하지 못하므로 `bpf` target이 실패할 수 있습니다.
  • `-g` 없이 compile하면 default target의 object file에는 `.eh_frame`, `.rela.eh_frame` 같은 ELF section이 추가될 수 있지만 `bpf` target에는 생기지 않습니다.
  • default target은 C switch statement를 switch table lookup과 jump operation으로 바꿀 수 있습니다. switch table은 global readonly section에 놓이므로 BPF program load가 실패합니다. `bpf` target은 switch table optimization을 지원하지 않습니다. clang option `-fno-jump-tables`로 switch table 생성을 끌 수 있습니다.
  • clang `--target=bpf`에서는 underlying clang binary, default target 또는 kernel이 32-bit인지와 무관하게 pointer와 `long`/`unsigned long` type의 width가 항상 64-bit임을 보장합니다. native clang target은 underlying architecture convention을 따르므로 32-bit architecture에서는 BPF context structure의 해당 type도 32-bit가 되지만 BPF LLVM back end는 여전히 64-bit로 동작합니다. native target은 주로 CPU register width가 중요한 tracing에서 `pt_regs`나 다른 kernel structure를 순회할 때 필요합니다. 그 밖에는 일반적으로 `clang --target=bpf`를 권장합니다.

Default target과 bpf target 선택

682-704

다음 경우에는 default target을 사용해야 합니다.

  • program이 `ptrace.h` 같은 header file을 include하고, 그 header가 결국 file-scope host assembly code를 포함한 header를 가져옵니다.
  • switch table 문제를 피하기 위해 `-fno-jump-tables`를 추가할 수 있습니다.

그 밖의 경우에는 `bpf` target을 사용할 수 있습니다. 다음 경우에는 반드시 `bpf` target을 사용해야 합니다.

  • program이 BPF helper 또는 context data structure와 interface하는 pointer나 `long`/`unsigned long` type의 data structure를 사용합니다. verifier가 이 structure access를 검사하므로 native architecture가 64-bit인 BPF architecture와 일치하지 않으면 verification failure가 발생할 수 있습니다. 예를 들어 `BPF_PROG_TYPE_SK_MSG`는 `--target=bpf`를 요구합니다.

원문의 selftests link는 `https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/bpf/`를 가리킵니다.

즐거운 BPF hacking을 기원합니다.