BPF
Single-page view · as markdown
BPF Documentation
Peios Kernel / Internals / BPF
This directory contains documentation for the BPF (Berkeley Packet Filter) facility, with a focus on the extended BPF version (eBPF).
This kernel side documentation is still work in progress. The Cilium project also maintains a BPF and XDP Reference Guide that goes into great technical depth about the BPF Architecture.
- eBPF verifier
- libbpf
- BPF Standardization
- BPF Type Format (BTF)
- Frequently asked questions (FAQ)
- Syscall API
- Helper functions
- BPF Kernel Functions (kfuncs)
- BPF cpumask kfuncs
- BPF filesystem kfuncs
- Program Types
- BPF maps
- Running BPF programs from userspace
- Classic BPF vs eBPF
- BPF Iterators
- BPF licensing
- Testing and debugging BPF
- Contents
- Contents
- Other
- Redirect
BPF Design Q&A
Peios Kernel / Internals / BPF
BPF extensibility and applicability to networking, tracing, security in the linux kernel and several user space implementations of BPF virtual machine led to a number of misunderstanding on what BPF actually is. This short QA is an attempt to address that and outline a direction of where BPF is heading long term.
Questions and Answers #
Q: Is BPF a generic instruction set similar to x64 and arm64? #
A: NO.
Q: Is BPF a generic virtual machine ? #
A: NO.
BPF is generic instruction set with C calling convention. #
Q: Why C calling convention was chosen? #
A: Because BPF programs are designed to run in the linux kernel which is written in C, hence BPF defines instruction set compatible with two most used architectures x64 and arm64 (and takes into consideration important quirks of other architectures) and defines calling convention that is compatible with C calling convention of the linux kernel on those architectures.
Q: Can multiple return values be supported in the future? #
A: NO. BPF allows only register R0 to be used as return value.
Q: Can more than 5 function arguments be supported in the future? #
A: NO. BPF calling convention only allows registers R1-R5 to be used as arguments. BPF is not a standalone instruction set. (unlike x64 ISA that allows msft, cdecl and other conventions)
Q: Can BPF programs access instruction pointer or return address? #
A: NO.
Q: Can BPF programs access stack pointer ? #
A: NO.
Only frame pointer (register R10) is accessible. From compiler point of view it's necessary to have stack pointer. For example, LLVM defines register R11 as stack pointer in its BPF backend, but it makes sure that generated code never uses it.
Q: Does C-calling convention diminishes possible use cases? #
A: YES.
BPF design forces addition of major functionality in the form of kernel helper functions and kernel objects like BPF maps with seamless interoperability between them. It lets kernel call into BPF programs and programs call kernel helpers with zero overhead, as all of them were native C code. That is particularly the case for JITed BPF programs that are indistinguishable from native kernel C code.
Q: Does it mean that 'innovative' extensions to BPF code are disallowed? #
A: Soft yes.
At least for now, until BPF core has support for bpf-to-bpf calls, indirect calls, loops, global variables, jump tables, read-only sections, and all other normal constructs that C code can produce.
Q: Can loops be supported in a safe way? #
A: It's not clear yet.
BPF developers are trying to find a way to support bounded loops.
Q: What are the verifier limits? #
A: The only limit known to the user space is BPF_MAXINSNS (4096). It's the maximum number of instructions that the unprivileged bpf program can have. The verifier has various internal limits. Like the maximum number of instructions that can be explored during program analysis. Currently, that limit is set to 1 million. Which essentially means that the largest program can consist of 1 million NOP instructions. There is a limit to the maximum number of subsequent branches, a limit to the number of nested bpf-to-bpf calls, a limit to the number of the verifier states per instruction, a limit to the number of maps used by the program. All these limits can be hit with a sufficiently complex program. There are also non-numerical limits that can cause the program to be rejected. The verifier used to recognize only pointer + constant expressions. Now it can recognize pointer + bounded_register. bpf_lookup_map_elem(key) had a requirement that 'key' must be a pointer to the stack. Now, 'key' can be a pointer to map value. The verifier is steadily getting 'smarter'. The limits are being removed. The only way to know that the program is going to be accepted by the verifier is to try to load it. The bpf development process guarantees that the future kernel versions will accept all bpf programs that were accepted by the earlier versions.
Instruction level questions #
Q: LD_ABS and LD_IND instructions vs C code #
Q: How come LD_ABS and LD_IND instruction are present in BPF whereas C code cannot express them and has to use builtin intrinsics?
A: This is artifact of compatibility with classic BPF. Modern networking code in BPF performs better without them. See 'direct packet access'.
Q: BPF instructions mapping not one-to-one to native CPU #
Q: It seems not all BPF instructions are one-to-one to native CPU. For example why BPF_JNE and other compare and jumps are not cpu-like?
A: This was necessary to avoid introducing flags into ISA which are impossible to make generic and efficient across CPU architectures.
Q: Why BPF_DIV instruction doesn't map to x64 div? #
A: Because if we picked one-to-one relationship to x64 it would have made it more complicated to support on arm64 and other archs. Also it needs div-by-zero runtime check.
Q: Why BPF has implicit prologue and epilogue? #
A: Because architectures like sparc have register windows and in general there are enough subtle differences between architectures, so naive store return address into stack won't work. Another reason is BPF has to be safe from division by zero (and legacy exception path of LD_ABS insn). Those instructions need to invoke epilogue and return implicitly.
Q: Why BPF_JLT and BPF_JLE instructions were not introduced in the beginning? #
A: Because classic BPF didn't have them and BPF authors felt that compiler workaround would be acceptable. Turned out that programs lose performance due to lack of these compare instructions and they were added. These two instructions is a perfect example what kind of new BPF instructions are acceptable and can be added in the future. These two already had equivalent instructions in native CPUs. New instructions that don't have one-to-one mapping to HW instructions will not be accepted.
Q: BPF 32-bit subregister requirements #
Q: BPF 32-bit subregisters have a requirement to zero upper 32-bits of BPF registers which makes BPF inefficient virtual machine for 32-bit CPU architectures and 32-bit HW accelerators. Can true 32-bit registers be added to BPF in the future?
A: NO.
But some optimizations on zero-ing the upper 32 bits for BPF registers are available, and can be leveraged to improve the performance of JITed BPF programs for 32-bit architectures.
Starting with version 7, LLVM is able to generate instructions that operate on 32-bit subregisters, provided the option -mattr=+alu32 is passed for compiling a program. Furthermore, the verifier can now mark the instructions for which zero-ing the upper bits of the destination register is required, and insert an explicit zero-extension (zext) instruction (a mov32 variant). This means that for architectures without zext hardware support, the JIT back-ends do not need to clear the upper bits for subregisters written by alu32 instructions or narrow loads. Instead, the back-ends simply need to support code generation for that mov32 variant, and to overwrite bpf_jit_needs_zext() to make it return "true" (in order to enable zext insertion in the verifier).
Note that it is possible for a JIT back-end to have partial hardware support for zext. In that case, if verifier zext insertion is enabled, it could lead to the insertion of unnecessary zext instructions. Such instructions could be removed by creating a simple peephole inside the JIT back-end: if one instruction has hardware support for zext and if the next instruction is an explicit zext, then the latter can be skipped when doing the code generation.
Q: Does BPF have a stable ABI? #
A: YES. BPF instructions, arguments to BPF programs, set of helper functions and their arguments, recognized return codes are all part of ABI. However there is one specific exception to tracing programs which are using helpers like bpf_probe_read() to walk kernel internal data structures and compile with kernel internal headers. Both of these kernel internals are subject to change and can break with newer kernels such that the program needs to be adapted accordingly.
New BPF functionality is generally added through the use of kfuncs instead of new helpers. Kfuncs are not considered part of the stable API, and have their own lifecycle expectations as described in 3. kfunc lifecycle expectations.
Q: Are tracepoints part of the stable ABI? #
A: NO. Tracepoints are tied to internal implementation details hence they are subject to change and can break with newer kernels. BPF programs need to change accordingly when this happens.
Q: Are places where kprobes can attach part of the stable ABI? #
A: NO. The places to which kprobes can attach are internal implementation details, which means that they are subject to change and can break with newer kernels. BPF programs need to change accordingly when this happens.
Q: How much stack space a BPF program uses? #
A: Currently all program types are limited to 512 bytes of stack space, but the verifier computes the actual amount of stack used and both interpreter and most JITed code consume necessary amount.
Q: Can BPF be offloaded to HW? #
A: YES. BPF HW offload is supported by NFP driver.
Q: Does classic BPF interpreter still exist? #
A: NO. Classic BPF programs are converted into extend BPF instructions.
Q: Can BPF call arbitrary kernel functions? #
A: NO. BPF programs can only call specific functions exposed as BPF helpers or kfuncs. The set of available functions is defined for every program type.
Q: Can BPF overwrite arbitrary kernel memory? #
A: NO.
Tracing bpf programs can read arbitrary memory with bpf_probe_read() and bpf_probe_read_str() helpers. Networking programs cannot read arbitrary memory, since they don't have access to these helpers. Programs can never read or write arbitrary memory directly.
Q: Can BPF overwrite arbitrary user memory? #
A: Sort-of.
Tracing BPF programs can overwrite the user memory of the current task with bpf_probe_write_user(). Every time such program is loaded the kernel will print warning message, so this helper is only useful for experiments and prototypes. Tracing BPF programs are root only.
Q: New functionality via kernel modules? #
Q: Can BPF functionality such as new program or map types, new helpers, etc be added out of kernel module code?
A: Yes, through kfuncs and kptrs
The core BPF functionality such as program types, maps and helpers cannot be added to by modules. However, modules can expose functionality to BPF programs by exporting kfuncs (which may return pointers to module-internal data structures as kptrs).
Q: Directly calling kernel function is an ABI? #
Q: Some kernel functions (e.g. tcp_slow_start) can be called by BPF programs. Do these kernel functions become an ABI?
A: NO.
The kernel function protos will change and the bpf programs will be rejected by the verifier. Also, for example, some of the bpf-callable kernel functions have already been used by other kernel tcp cc (congestion-control) implementations. If any of these kernel functions has changed, both the in-tree and out-of-tree kernel tcp cc implementations have to be changed. The same goes for the bpf programs and they have to be adjusted accordingly. See 3. kfunc lifecycle expectations for details.
Q: Attaching to arbitrary kernel functions is an ABI? #
Q: BPF programs can be attached to many kernel functions. Do these kernel functions become part of the ABI?
A: NO.
The kernel function prototypes will change, and BPF programs attaching to them will need to change. The BPF compile-once-run-everywhere (CO-RE) should be used in order to make it easier to adapt your BPF programs to different versions of the kernel.
Q: Marking a function with BTF_ID makes that function an ABI? #
A: NO.
The BTF_ID macro does not cause a function to become part of the ABI any more than does the EXPORT_SYMBOL_GPL macro.
Q: What is the compatibility story for special BPF types in map values? #
Q: Users are allowed to embed bpf_spin_lock, bpf_timer fields in their BPF map values (when using BTF support for BPF maps). This allows to use helpers for such objects on these fields inside map values. Users are also allowed to embed pointers to some kernel types (with __kptr_untrusted and __kptr BTF tags). Will the kernel preserve backwards compatibility for these features?
A: It depends. For bpf_spin_lock, bpf_timer: YES, for kptr and everything else: NO, but see below.
For struct types that have been added already, like bpf_spin_lock and bpf_timer, the kernel will preserve backwards compatibility, as they are part of UAPI.
For kptrs, they are also part of UAPI, but only with respect to the kptr mechanism. The types that you can use with a __kptr_untrusted and __kptr tagged pointer in your struct are NOT part of the UAPI contract. The supported types can and will change across kernel releases. However, operations like accessing kptr fields and bpf_kptr_xchg() helper will continue to be supported across kernel releases for the supported types.
For any other supported struct type, unless explicitly stated in this document and added to bpf.h UAPI header, such types can and will arbitrarily change their size, type, and alignment, or any other user visible API or ABI detail across kernel releases. The users must adapt their BPF programs to the new changes and update them to make sure their programs continue to work correctly.
NOTE: BPF subsystem specially reserves the 'bpf_' prefix for type names, in order to introduce more special fields in the future. Hence, user programs must avoid defining types with 'bpf_' prefix to not be broken in future releases. In other words, no backwards compatibility is guaranteed if one using a type in BTF with 'bpf_' prefix.
Q: What is the compatibility story for special BPF types in allocated objects? #
Q: Same as above, but for allocated objects (i.e. objects allocated using bpf_obj_new for user defined types). Will the kernel preserve backwards compatibility for these features?
A: NO.
Unlike map value types, the API to work with allocated objects and any support for special fields inside them is exposed through kfuncs, and thus has the same lifecycle expectations as the kfuncs themselves. See 3. kfunc lifecycle expectations for details.
HOWTO interact with BPF subsystem
Peios Kernel / Internals / BPF
This document provides information for the BPF subsystem about various workflows related to reporting bugs, submitting patches, and queueing patches for stable kernels.
For general information about submitting patches, please refer to Documentation/process/submitting-patches.rst. This document only describes additional specifics related to BPF.
Reporting bugs #
Q: How do I report bugs for BPF kernel code? #
A: Since all BPF kernel development as well as bpftool and iproute2 BPF loader development happens through the bpf kernel mailing list, please report any found issues around BPF to the following mailing list:
This may also include issues related to XDP, BPF tracing, etc.
Given netdev has a high volume of traffic, please also add the BPF maintainers to Cc (from kernel MAINTAINERS file):
-
Alexei Starovoitov <ast@kernel.org>
-
Daniel Borkmann <daniel@iogearbox.net>
In case a buggy commit has already been identified, make sure to keep the actual commit authors in Cc as well for the report. They can typically be identified through the kernel's git tree.
Please do NOT report BPF issues to bugzilla.kernel.org since it is a guarantee that the reported issue will be overlooked.
Submitting patches #
Q: How do I run BPF CI on my changes before sending them out for review? #
A: BPF CI is GitHub based and hosted at https://github.com/kernel-patches/bpf. While GitHub also provides a CLI that can be used to accomplish the same results, here we focus on the UI based workflow.
The following steps lay out how to start a CI run for your patches:
-
Create a fork of the aforementioned repository in your own account (one time action)
-
Clone the fork locally, check out a new branch tracking either the bpf-next or bpf branch, and apply your to-be-tested patches on top of it
-
Push the local branch to your fork and create a pull request against kernel-patches/bpf's bpf-next_base or bpf_base branch, respectively
Shortly after the pull request has been created, the CI workflow will run. Note that capacity is shared with patches submitted upstream being checked and so depending on utilization the run can take a while to finish.
Note furthermore that both base branches (bpf-next_base and bpf_base) will be updated as patches are pushed to the respective upstream branches they track. As such, your patch set will automatically (be attempted to) be rebased as well. This behavior can result in a CI run being aborted and restarted with the new base line.
Q: To which mailing list do I need to submit my BPF patches? #
A: Please submit your BPF patches to the bpf kernel mailing list:
In case your patch has changes in various different subsystems (e.g. networking, tracing, security, etc), make sure to Cc the related kernel mailing lists and maintainers from there as well, so they are able to review the changes and provide their Acked-by's to the patches.
Q: Where can I find patches currently under discussion for BPF subsystem? #
A: All patches that are Cc'ed to netdev are queued for review under netdev patchwork project:
Those patches which target BPF, are assigned to a 'bpf' delegate for further processing from BPF maintainers. The current queue with patches under review can be found at:
https://patchwork.kernel.org/project/netdevbpf/list/?delegate=121173
Once the patches have been reviewed by the BPF community as a whole and approved by the BPF maintainers, their status in patchwork will be changed to 'Accepted' and the submitter will be notified by mail. This means that the patches look good from a BPF perspective and have been applied to one of the two BPF kernel trees.
In case feedback from the community requires a respin of the patches, their status in patchwork will be set to 'Changes Requested', and purged from the current review queue. Likewise for cases where patches would get rejected or are not applicable to the BPF trees (but assigned to the 'bpf' delegate).
Q: How do the changes make their way into Linux? #
A: There are two BPF kernel trees (git repositories). Once patches have been accepted by the BPF maintainers, they will be applied to one of the two BPF trees:
The bpf tree itself is for fixes only, whereas bpf-next for features, cleanups or other kind of improvements ("next-like" content). This is analogous to net and net-next trees for networking. Both bpf and bpf-next will only have a master branch in order to simplify against which branch patches should get rebased to.
Accumulated BPF patches in the bpf tree will regularly get pulled into the net kernel tree. Likewise, accumulated BPF patches accepted into the bpf-next tree will make their way into net-next tree. net and net-next are both run by David S. Miller. From there, they will go into the kernel mainline tree run by Linus Torvalds. To read up on the process of net and net-next being merged into the mainline tree, see the documentation on netdev subsystem at Documentation/process/maintainer-netdev.rst.
Occasionally, to prevent merge conflicts, we might send pull requests to other trees (e.g. tracing) with a small subset of the patches, but net and net-next are always the main trees targeted for integration.
The pull requests will contain a high-level summary of the accumulated patches and can be searched on netdev kernel mailing list through the following subject lines (yyyy-mm-dd is the date of the pull request):
pull-request: bpf yyyy-mm-dd
pull-request: bpf-next yyyy-mm-dd
Q: How do I indicate which tree (bpf vs. bpf-next) my patch should be applied to? #
A: The process is the very same as described in the netdev subsystem documentation at Documentation/process/maintainer-netdev.rst, so please read up on it. The subject line must indicate whether the patch is a fix or rather "next-like" content in order to let the maintainers know whether it is targeted at bpf or bpf-next.
For fixes eventually landing in bpf -> net tree, the subject must look like:
git format-patch --subject-prefix='PATCH bpf' start..finish
For features/improvements/etc that should eventually land in bpf-next -> net-next, the subject must look like:
git format-patch --subject-prefix='PATCH bpf-next' start..finish
If unsure whether the patch or patch series should go into bpf or net directly, or bpf-next or net-next directly, it is not a problem either if the subject line says net or net-next as target. It is eventually up to the maintainers to do the delegation of the patches.
If it is clear that patches should go into bpf or bpf-next tree, please make sure to rebase the patches against those trees in order to reduce potential conflicts.
In case the patch or patch series has to be reworked and sent out again in a second or later revision, it is also required to add a version number (v2, v3, ...) into the subject prefix:
git format-patch --subject-prefix='PATCH bpf-next v2' start..finish
When changes have been requested to the patch series, always send the whole patch series again with the feedback incorporated (never send individual diffs on top of the old series).
Q: What does it mean when a patch gets applied to bpf or bpf-next tree? #
A: It means that the patch looks good for mainline inclusion from a BPF point of view.
Be aware that this is not a final verdict that the patch will automatically get accepted into net or net-next trees eventually:
On the bpf kernel mailing list reviews can come in at any point in time. If discussions around a patch conclude that they cannot get included as-is, we will either apply a follow-up fix or drop them from the trees entirely. Therefore, we also reserve to rebase the trees when deemed necessary. After all, the purpose of the tree is to:
-
i) accumulate and stage BPF patches for integration into trees like net and net-next, and
-
ii) run extensive BPF test suite and workloads on the patches before they make their way any further.
Once the BPF pull request was accepted by David S. Miller, then the patches end up in net or net-next tree, respectively, and make their way from there further into mainline. Again, see the documentation for netdev subsystem at Documentation/process/maintainer-netdev.rst for additional information e.g. on how often they are merged to mainline.
Q: How long do I need to wait for feedback on my BPF patches? #
A: We try to keep the latency low. The usual time to feedback will be around 2 or 3 business days. It may vary depending on the complexity of changes and current patch load.
Q: How often do you send pull requests to major kernel trees like net or net-next? #
A: Pull requests will be sent out rather often in order to not accumulate too many patches in bpf or bpf-next.
As a rule of thumb, expect pull requests for each tree regularly at the end of the week. In some cases pull requests could additionally come also in the middle of the week depending on the current patch load or urgency.
Q: Are patches applied to bpf-next when the merge window is open? #
A: For the time when the merge window is open, bpf-next will not be processed. This is roughly analogous to net-next patch processing, so feel free to read up on the netdev docs at Documentation/process/maintainer-netdev.rst about further details.
During those two weeks of merge window, we might ask you to resend your patch series once bpf-next is open again. Once Linus released a v*-rc1 after the merge window, we continue processing of bpf-next.
For non-subscribers to kernel mailing lists, there is also a status page run by David S. Miller on net-next that provides guidance:
Q: Verifier changes and test cases #
Q: I made a BPF verifier change, do I need to add test cases for BPF kernel selftests?
A: If the patch has changes to the behavior of the verifier, then yes, it is absolutely necessary to add test cases to the BPF kernel selftests suite. If they are not present and we think they are needed, then we might ask for them before accepting any changes.
In particular, test_verifier.c is tracking a high number of BPF test cases, including a lot of corner cases that LLVM BPF back end may generate out of the restricted C code. Thus, adding test cases is absolutely crucial to make sure future changes do not accidentally affect prior use-cases. Thus, treat those test cases as: verifier behavior that is not tracked in test_verifier.c could potentially be subject to change.
Q: samples/bpf preference vs selftests? #
Q: When should I add code to samples/bpf/ and when to BPF kernel selftests?
A: In general, we prefer additions to BPF kernel selftests rather than samples/bpf/. The rationale is very simple: kernel selftests are regularly run by various bots to test for kernel regressions.
The more test cases we add to BPF selftests, the better the coverage and the less likely it is that those could accidentally break. It is not that BPF kernel selftests cannot demo how a specific feature can be used.
That said, samples/bpf/ may be a good place for people to get started, so it might be advisable that simple demos of features could go into samples/bpf/, but advanced functional and corner-case testing rather into kernel selftests.
If your sample looks like a test case, then go for BPF kernel selftests instead!
Q: When should I add code to the bpftool? #
A: The main purpose of bpftool (under tools/bpf/bpftool/) is to provide a central user space tool for debugging and introspection of BPF programs and maps that are active in the kernel. If UAPI changes related to BPF enable for dumping additional information of programs or maps, then bpftool should be extended as well to support dumping them.
Q: When should I add code to iproute2's BPF loader? #
A: For UAPI changes related to the XDP or tc layer (e.g. cls_bpf), the convention is that those control-path related changes are added to iproute2's BPF loader as well from user space side. This is not only useful to have UAPI changes properly designed to be usable, but also to make those changes available to a wider user base of major downstream distributions.
Q: Do you accept patches as well for iproute2's BPF loader? #
A: Patches for the iproute2's BPF loader have to be sent to:
While those patches are not processed by the BPF kernel maintainers, please keep them in Cc as well, so they can be reviewed.
The official git repository for iproute2 is run by Stephen Hemminger and can be found at:
https://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git/
The patches need to have a subject prefix of '[PATCH iproute2 master]' or '[PATCH iproute2 net-next]'. 'master' or 'net-next' describes the target branch where the patch should be applied to. Meaning, if kernel changes went into the net-next kernel tree, then the related iproute2 changes need to go into the iproute2 net-next branch, otherwise they can be targeted at master branch. The iproute2 net-next branch will get merged into the master branch after the current iproute2 version from master has been released.
Like BPF, the patches end up in patchwork under the netdev project and are delegated to 'shemminger' for further processing:
http://patchwork.ozlabs.org/project/netdev/list/?delegate=389
Q: What is the minimum requirement before I submit my BPF patches? #
A: When submitting patches, always take the time and properly test your patches prior to submission. Never rush them! If maintainers find that your patches have not been properly tested, it is a good way to get them grumpy. Testing patch submissions is a hard requirement!
Note, fixes that go to bpf tree must have a Fixes: tag included. The same applies to fixes that target bpf-next, where the affected commit is in net-next (or in some cases bpf-next). The Fixes: tag is crucial in order to identify follow-up commits and tremendously helps for people having to do backporting, so it is a must have!
We also don't accept patches with an empty commit message. Take your time and properly write up a high quality commit message, it is essential!
Think about it this way: other developers looking at your code a month from now need to understand why a certain change has been done that way, and whether there have been flaws in the analysis or assumptions that the original author did. Thus providing a proper rationale and describing the use-case for the changes is a must.
Patch submissions with >1 patch must have a cover letter which includes a high level description of the series. This high level summary will then be placed into the merge commit by the BPF maintainers such that it is also accessible from the git log for future reference.
Q: Features changing BPF JIT and/or LLVM #
Q: What do I need to consider when adding a new instruction or feature that would require BPF JIT and/or LLVM integration as well?
A: We try hard to keep all BPF JITs up to date such that the same user experience can be guaranteed when running BPF programs on different architectures without having the program punt to the less efficient interpreter in case the in-kernel BPF JIT is enabled.
If you are unable to implement or test the required JIT changes for certain architectures, please work together with the related BPF JIT developers in order to get the feature implemented in a timely manner. Please refer to the git log (arch/*/net/) to locate the necessary people for helping out.
Also always make sure to add BPF test cases (e.g. test_bpf.c and test_verifier.c) for new instructions, so that they can receive broad test coverage and help run-time testing the various BPF JITs.
In case of new BPF instructions, once the changes have been accepted into the Linux kernel, please implement support into LLVM's BPF back end. See LLVM section below for further information.
Q: What "BPF_INTERNAL" symbol namespace is for? #
A: Symbols exported as BPF_INTERNAL can only be used by BPF infrastructure like preload kernel modules with light skeleton. Most symbols outside of BPF_INTERNAL are not expected to be used by code outside of BPF either. Symbols may lack the designation because they predate the namespaces, or due to an oversight.
Stable submission #
Q: I need a specific BPF commit in stable kernels. What should I do? #
A: In case you need a specific fix in stable kernels, first check whether the commit has already been applied in the related linux-*.y branches:
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/
If not the case, then drop an email to the BPF maintainers with the netdev kernel mailing list in Cc and ask for the fix to be queued up:
The process in general is the same as on netdev itself, see also the the documentation on networking subsystem at Documentation/process/maintainer-netdev.rst.
Q: Do you also backport to kernels not currently maintained as stable? #
A: No. If you need a specific BPF commit in kernels that are currently not maintained by the stable maintainers, then you are on your own.
The current stable and longterm stable kernels are all listed here:
Q: The BPF patch I am about to submit needs to go to stable as well #
What should I do?
A: The same rules apply as with netdev patch submissions in general, see the netdev docs at Documentation/process/maintainer-netdev.rst.
Never add "Cc: stable@vger.kernel.org" to the patch description, but ask the BPF maintainers to queue the patches instead. This can be done with a note, for example, under the --- part of the patch which does not go into the git log. Alternatively, this can be done as a simple request by mail instead.
Q: Queue stable patches #
Q: Where do I find currently queued BPF patches that will be submitted to stable?
A: Once patches that fix critical bugs got applied into the bpf tree, they are queued up for stable submission under:
They will be on hold there at minimum until the related commit made its way into the mainline kernel tree.
After having been under broader exposure, the queued patches will be submitted by the BPF maintainers to the stable maintainers.
Testing patches #
Q: How to run BPF selftests #
A: After you have booted into the newly compiled kernel, navigate to the BPF selftests suite in order to test BPF functionality (current working directory points to the root of the cloned git tree):
$ cd tools/testing/selftests/bpf/
$ make
To run the verifier tests:
$ sudo ./test_verifier
The verifier tests print out all the current checks being performed. The summary at the end of running all tests will dump information of test successes and failures:
Summary: 418 PASSED, 0 FAILED
In order to run through all BPF selftests, the following command is needed:
$ sudo make run_tests
See kernel selftest documentation for details.
To maximize the number of tests passing, the .config of the kernel under test should match the config file fragment in tools/testing/selftests/bpf as closely as possible.
Finally to ensure support for latest BPF Type Format features - discussed in Documentation/bpf/btf.rst - pahole version 1.16 is required for kernels built with CONFIG_DEBUG_INFO_BTF=y. pahole is delivered in the dwarves package or can be built from source at
https://github.com/acmel/dwarves
pahole starts to use libbpf definitions and APIs since v1.13 after the commit 21507cd3e97b ("pahole: add libbpf as submodule under lib/bpf"). It works well with the git repository because the libbpf submodule will use "git submodule update --init --recursive" to update.
Unfortunately, the default github release source code does not contain libbpf submodule source code and this will cause build issues, the tarball from https://git.kernel.org/pub/scm/devel/pahole/pahole.git/ is same with github, you can get the source tarball with corresponding libbpf submodule codes from
https://fedorapeople.org/~acme/dwarves
Some distros have pahole version 1.16 packaged already, e.g. Fedora, Gentoo.
Q: Which BPF kernel selftests version should I run my kernel against? #
A: If you run a kernel xyz, then always run the BPF kernel selftests from that kernel xyz as well. Do not expect that the BPF selftest from the latest mainline tree will pass all the time.
In particular, test_bpf.c and test_verifier.c have a large number of test cases and are constantly updated with new BPF test sequences, or existing ones are adapted to verifier changes e.g. due to verifier becoming smarter and being able to better track certain things.
LLVM #
Q: Where do I find LLVM with BPF support? #
A: The BPF back end for LLVM is upstream in LLVM since version 3.7.1.
All major distributions these days ship LLVM with BPF back end enabled, so for the majority of use-cases it is not required to compile LLVM by hand anymore, just install the distribution provided package.
LLVM's static compiler lists the supported targets through llc --version, make sure BPF targets are listed. Example:
$ 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
For developers in order to utilize the latest features added to LLVM's BPF back end, it is advisable to run the latest LLVM releases. Support for new BPF kernel features such as additions to the BPF instruction set are often developed together.
All LLVM releases can be found at: http://releases.llvm.org/
Q: Got it, so how do I build LLVM manually anyway? #
A: We recommend that developers who want the fastest incremental builds use the Ninja build system, you can find it in your system's package manager, usually the package is ninja or ninja-build.
You need ninja, cmake and gcc-c++ as build requisites for LLVM. Once you have that set up, proceed with building the latest LLVM and clang version from the git repositories:
$ 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
The built binaries can then be found in the build/bin/ directory, where you can point the PATH variable to.
Set -DLLVM_TARGETS_TO_BUILD equal to the target you wish to build, you will find a full list of targets within the llvm-project/llvm/lib/Target directory.
Q: Reporting LLVM BPF issues #
Q: Should I notify BPF kernel maintainers about issues in LLVM's BPF code generation back end or about LLVM generated code that the verifier refuses to accept?
A: Yes, please do!
LLVM's BPF back end is a key piece of the whole BPF infrastructure and it ties deeply into verification of programs from the kernel side. Therefore, any issues on either side need to be investigated and fixed whenever necessary.
Therefore, please make sure to bring them up at netdev kernel mailing list and Cc BPF maintainers for LLVM and kernel bits:
-
Yonghong Song <yhs@fb.com>
-
Alexei Starovoitov <ast@kernel.org>
-
Daniel Borkmann <daniel@iogearbox.net>
LLVM also has an issue tracker where BPF related bugs can be found:
However, it is better to reach out through mailing lists with having maintainers in Cc.
Q: New BPF instruction for kernel and LLVM #
Q: I have added a new BPF instruction to the kernel, how can I integrate it into LLVM?
A: LLVM has a -mcpu selector for the BPF back end in order to allow the selection of BPF instruction set extensions. Before llvm version 20, the generic processor target is used, which is the base instruction set (v1) of BPF. Since llvm 20, the default processor target has changed to instruction set v3.
LLVM has an option to select -mcpu=probe where it will probe the host kernel for supported BPF instruction set extensions and selects the optimal set automatically.
For cross-compilation, a specific version can be select manually as well
$ 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.
[...]
Newly added BPF instructions to the Linux kernel need to follow the same scheme, bump the instruction set version and implement probing for the extensions such that -mcpu=probe users can benefit from the optimization transparently when upgrading their kernels.
If you are unable to implement support for the newly added BPF instruction please reach out to BPF developers for help.
By the way, the BPF kernel selftests run with -mcpu=probe for better test coverage.
Q: clang flag for target bpf? #
Q: In some cases clang flag --target=bpf is used but in other cases the default clang target, which matches the underlying architecture, is used. What is the difference and when I should use which?
A: Although LLVM IR generation and optimization try to stay architecture independent, --target=<arch> still has some impact on generated code:
-
BPF program may recursively include header file(s) with file scope inline assembly codes. The default target can handle this well, while
bpftarget may fail if bpf backend assembler does not understand these assembly codes, which is true in most cases. -
When compiled without
-g, additional elf sections, e.g., .eh_frame and .rela.eh_frame, may be present in the object file with default target, but not withbpftarget. -
The default target may turn a C switch statement into a switch table lookup and jump operation. Since the switch table is placed in the global readonly section, the bpf program will fail to load. The bpf target does not support switch table optimization. The clang option
-fno-jump-tablescan be used to disable switch table generation. -
For clang
--target=bpf, it is guaranteed that pointer or long / unsigned long types will always have a width of 64 bit, no matter whether underlying clang binary or default target (or kernel) is 32 bit. However, when native clang target is used, then it will compile these types based on the underlying architecture's conventions, meaning in case of 32 bit architecture, pointer or long / unsigned long types e.g. in BPF context structure will have width of 32 bit while the BPF LLVM back end still operates in 64 bit. The native target is mostly needed in tracing for the case of walkingpt_regsor other kernel structures where CPU's register width matters. Otherwise,clang --target=bpfis generally recommended.
You should use default target when:
-
Your program includes a header file, e.g., ptrace.h, which eventually pulls in some header files containing file scope host assembly codes.
-
You can add
-fno-jump-tablesto work around the switch table issue.
Otherwise, you can use bpf target. Additionally, you must use bpf target when:
- Your program uses data structures with pointer or long / unsigned long types that interface with BPF helpers or context data structures. Access into these structures is verified by the BPF verifier and may result in verification failures if the native architecture is not aligned with the BPF architecture, e.g. 64-bit. An example of this is BPF_PROG_TYPE_SK_MSG require
--target=bpf
Happy BPF hacking!
BPF Iterators
Peios Kernel / Internals / BPF
Overview #
BPF supports two separate entities collectively known as "BPF iterators": BPF iterator program type and open-coded BPF iterators. The former is a stand-alone BPF program type which, when attached and activated by user, will be called once for each entity (task_struct, cgroup, etc) that is being iterated. The latter is a set of BPF-side APIs implementing iterator functionality and available across multiple BPF program types. Open-coded iterators provide similar functionality to BPF iterator programs, but gives more flexibility and control to all other BPF program types. BPF iterator programs, on the other hand, can be used to implement anonymous or BPF FS-mounted special files, whose contents are generated by attached BPF iterator program, backed by seq_file functionality. Both are useful depending on specific needs.
When adding a new BPF iterator program, it is expected that similar functionality will be added as open-coded iterator for maximum flexibility. It's also expected that iteration logic and code will be maximally shared and reused between two iterator API surfaces.
Open-coded BPF Iterators #
Open-coded BPF iterators are implemented as tightly-coupled trios of kfuncs (constructor, next element fetch, destructor) and iterator-specific type describing on-the-stack iterator state, which is guaranteed by the BPF verifier to not be tampered with outside of the corresponding constructor/destructor/next APIs.
Each kind of open-coded BPF iterator has its own associated struct bpf_iter_<type>, where <type> denotes a specific type of iterator. bpf_iter_<type> state needs to live on BPF program stack, so make sure it's small enough to fit on BPF stack. For performance reasons its best to avoid dynamic memory allocation for iterator state and size the state struct big enough to fit everything necessary. But if necessary, dynamic memory allocation is a way to bypass BPF stack limitations. Note, state struct size is part of iterator's user-visible API, so changing it will break backwards compatibility, so be deliberate about designing it.
All kfuncs (constructor, next, destructor) have to be named consistently as bpf_iter_<type>_{new,next,destroy}(), respectively. <type> represents iterator type, and iterator state should be represented as a matching struct bpf_iter_<type> state type. Also, all iter kfuncs should have a pointer to this struct bpf_iter_<type> as the very first argument.
-
Additionally:
-
Constructor, i.e.,
bpf_iter_<type>_new(), can have arbitrary extra number of arguments. Return type is not enforced either. -
Next method, i.e.,
bpf_iter_<type>_next(), has to return a pointer type and should have exactly one argument:struct bpf_iter_<type> *(const/volatile/restrict and typedefs are ignored). -
Destructor, i.e.,
bpf_iter_<type>_destroy(), should return void and should have exactly one argument, similar to the next method. -
struct bpf_iter_<type>size is enforced to be positive and a multiple of 8 bytes (to fit stack slots correctly).
-
Such strictness and consistency allows to build generic helpers abstracting important, but boilerplate, details to be able to use open-coded iterators effectively and ergonomically (see libbpf's bpf_for_each() macro). This is enforced at kfunc registration point by the kernel.
-
Constructor/next/destructor implementation contract is as follows:
-
constructor,
bpf_iter_<type>_new(), always initializes iterator state on the stack. If any of the input arguments are invalid, constructor should make sure to still initialize it such that subsequent next() calls will return NULL. I.e., on error, return error and construct empty iterator. Constructor kfunc is marked with KF_ITER_NEW flag. -
next method,
bpf_iter_<type>_next(), accepts pointer to iterator state and produces an element. Next method should always return a pointer. The contract between BPF verifier is that next method guarantees that it will eventually return NULL when elements are exhausted. Once NULL is returned, subsequent next calls should keep returning NULL. Next method is marked with KF_ITER_NEXT (and should also have KF_RET_NULL as NULL-returning kfunc, of course). -
destructor,
bpf_iter_<type>_destroy(), is always called once. Even if constructor failed or next returned nothing. Destructor frees up any resources and marks stack space used bystruct bpf_iter_<type>as usable for something else. Destructor is marked with KF_ITER_DESTROY flag.
-
Any open-coded BPF iterator implementation has to implement at least these three methods. It is enforced that for any given type of iterator only applicable constructor/destructor/next are callable. I.e., verifier ensures you can't pass number iterator state into, say, cgroup iterator's next method.
From a 10,000-feet BPF verification point of view, next methods are the points of forking a verification state, which are conceptually similar to what verifier is doing when validating conditional jumps. Verifier is branching out call bpf_iter_<type>_next instruction and simulates two outcomes: NULL (iteration is done) and non-NULL (new element is returned). NULL is simulated first and is supposed to reach exit without looping. After that non-NULL case is validated and it either reaches exit (for trivial examples with no real loop), or reaches another call bpf_iter_<type>_next instruction with the state equivalent to already (partially) validated one. State equivalency at that point means we technically are going to be looping forever without "breaking out" out of established "state envelope" (i.e., subsequent iterations don't add any new knowledge or constraints to the verifier state, so running 1, 2, 10, or a million of them doesn't matter). But taking into account the contract stating that iterator next method has to return NULL eventually, we can conclude that loop body is safe and will eventually terminate. Given we validated logic outside of the loop (NULL case), and concluded that loop body is safe (though potentially looping many times), verifier can claim safety of the overall program logic.
BPF Iterators Motivation #
There are a few existing ways to dump kernel data into user space. The most popular one is the /proc system. For example, cat /proc/net/tcp6 dumps all tcp6 sockets in the system, and cat /proc/net/netlink dumps all netlink sockets in the system. However, their output format tends to be fixed, and if users want more information about these sockets, they have to patch the kernel, which often takes time to publish upstream and release. The same is true for popular tools like ss where any additional information needs a kernel patch.
To solve this problem, the drgn tool is often used to dig out the kernel data with no kernel change. However, the main drawback for drgn is performance, as it cannot do pointer tracing inside the kernel. In addition, drgn cannot validate a pointer value and may read invalid data if the pointer becomes invalid inside the kernel.
The BPF iterator solves the above problem by providing flexibility on what data (e.g., tasks, bpf_maps, etc.) to collect by calling BPF programs for each kernel data object.
How BPF Iterators Work #
A BPF iterator is a type of BPF program that allows users to iterate over specific types of kernel objects. Unlike traditional BPF tracing programs that allow users to define callbacks that are invoked at particular points of execution in the kernel, BPF iterators allow users to define callbacks that should be executed for every entry in a variety of kernel data structures.
For example, users can define a BPF iterator that iterates over every task on the system and dumps the total amount of CPU runtime currently used by each of them. Another BPF task iterator may instead dump the cgroup information for each task. Such flexibility is the core value of BPF iterators.
A BPF program is always loaded into the kernel at the behest of a user space process. A user space process loads a BPF program by opening and initializing the program skeleton as required and then invoking a syscall to have the BPF program verified and loaded by the kernel.
In traditional tracing programs, a program is activated by having user space obtain a bpf_link to the program with bpf_program__attach(). Once activated, the program callback will be invoked whenever the tracepoint is triggered in the main kernel. For BPF iterator programs, a bpf_link to the program is obtained using bpf_link_create(), and the program callback is invoked by issuing system calls from user space.
Next, let us see how you can use the iterators to iterate on kernel objects and read data.
How to Use BPF iterators #
BPF selftests are a great resource to illustrate how to use the iterators. In this section, we’ll walk through a BPF selftest which shows how to load and use a BPF iterator program. To begin, we’ll look at bpf_iter.c, which illustrates how to load and trigger BPF iterators on the user space side. Later, we’ll look at a BPF program that runs in kernel space.
Loading a BPF iterator in the kernel from user space typically involves the following steps:
-
The BPF program is loaded into the kernel through
libbpf. Once the kernel has verified and loaded the program, it returns a file descriptor (fd) to user space. -
Obtain a
link_fdto the BPF program by calling thebpf_link_create()specified with the BPF program file descriptor received from the kernel. -
Next, obtain a BPF iterator file descriptor (
bpf_iter_fd) by calling thebpf_iter_create()specified with thebpf_linkreceived from Step 2. -
Trigger the iteration by calling
read(bpf_iter_fd)until no data is available. -
Close the iterator fd using
close(bpf_iter_fd). -
If needed to reread the data, get a new
bpf_iter_fdand do the read again.
The following are a few examples of selftest BPF iterator programs:
Let us look at bpf_iter_task_file.c, which runs in kernel space:
Here is the definition of bpf_iter__task_file in vmlinux.h. Any struct name in vmlinux.h in the format bpf_iter__<iter_name> represents a BPF iterator. The suffix <iter_name> represents the type of iterator.
struct bpf_iter__task_file {
union {
struct bpf_iter_meta *meta;
};
union {
struct task_struct *task;
};
u32 fd;
union {
struct file *file;
};
};
In the above code, the field 'meta' contains the metadata, which is the same for all BPF iterator programs. The rest of the fields are specific to different iterators. For example, for task_file iterators, the kernel layer provides the 'task', 'fd' and 'file' field values. The 'task' and 'file' are reference counted, so they won't go away when the BPF program runs.
Here is a snippet from the bpf_iter_task_file.c file:
SEC("iter/task_file")
int dump_task_file(struct bpf_iter__task_file *ctx)
{
struct seq_file *seq = ctx->meta->seq;
struct task_struct *task = ctx->task;
struct file *file = ctx->file;
__u32 fd = ctx->fd;
if (task == NULL || file == NULL)
return 0;
if (ctx->meta->seq_num == 0) {
count = 0;
BPF_SEQ_PRINTF(seq, " tgid gid fd file\n");
}
if (tgid == task->tgid && task->tgid != task->pid)
count++;
if (last_tgid != task->tgid) {
last_tgid = task->tgid;
unique_tgid_count++;
}
BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd,
(long)file->f_op);
return 0;
}
In the above example, the section name SEC(iter/task_file), indicates that the program is a BPF iterator program to iterate all files from all tasks. The context of the program is bpf_iter__task_file struct.
The user space program invokes the BPF iterator program running in the kernel by issuing a read() syscall. Once invoked, the BPF program can export data to user space using a variety of BPF helper functions. You can use either bpf_seq_printf() (and BPF_SEQ_PRINTF helper macro) or bpf_seq_write() function based on whether you need formatted output or just binary data, respectively. For binary-encoded data, the user space applications can process the data from bpf_seq_write() as needed. For the formatted data, you can use cat <path> to print the results similar to cat /proc/net/netlink after pinning the BPF iterator to the bpffs mount. Later, use rm -f <path> to remove the pinned iterator.
For example, you can use the following command to create a BPF iterator from the bpf_iter_ipv6_route.o object file and pin it to the /sys/fs/bpf/my_route path:
$ bpftool iter pin ./bpf_iter_ipv6_route.o /sys/fs/bpf/my_route
And then print out the results using the following command:
$ cat /sys/fs/bpf/my_route
Implement Kernel Support for BPF Iterator Program Types #
To implement a BPF iterator in the kernel, the developer must make a one-time change to the following key data structure defined in the bpf.h file.
struct bpf_iter_reg {
const char *target;
bpf_iter_attach_target_t attach_target;
bpf_iter_detach_target_t detach_target;
bpf_iter_show_fdinfo_t show_fdinfo;
bpf_iter_fill_link_info_t fill_link_info;
bpf_iter_get_func_proto_t get_func_proto;
u32 ctx_arg_info_size;
u32 feature;
struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
const struct bpf_iter_seq_info *seq_info;
};
After filling the data structure fields, call bpf_iter_reg_target() to register the iterator to the main BPF iterator subsystem.
The following is the breakdown for each field in struct bpf_iter_reg.
|
Fields |
Description |
|---|---|
|
target |
Specifies the name of the BPF iterator. For example: |
|
attach_target and detach_target |
Allows for target specific |
|
show_fdinfo and fill_link_info |
Called to fill target specific information when user tries to get link info associated with the iterator. |
|
get_func_proto |
Permits a BPF iterator to access BPF helpers specific to the iterator. |
|
ctx_arg_info_size and ctx_arg_info |
Specifies the verifier states for BPF program arguments associated with the bpf iterator. |
|
feature |
Specifies certain action requests in the kernel BPF iterator infrastructure. Currently, only BPF_ITER_RESCHED is supported. This means that the kernel function cond_resched() is called to avoid other kernel subsystem (e.g., rcu) misbehaving. |
|
seq_info |
Specifies the set of seq operations for the BPF iterator and helpers to initialize/free the private data for the corresponding |
Click here to see an implementation of the task_vma BPF iterator in the kernel.
Parameterizing BPF Task Iterators #
By default, BPF iterators walk through all the objects of the specified types (processes, cgroups, maps, etc.) across the entire system to read relevant kernel data. But often, there are cases where we only care about a much smaller subset of iterable kernel objects, such as only iterating tasks within a specific process. Therefore, BPF iterator programs support filtering out objects from iteration by allowing user space to configure the iterator program when it is attached.
BPF Task Iterator Program #
The following code is a BPF iterator program to print files and task information through the seq_file of the iterator. It is a standard BPF iterator program that visits every file of an iterator. We will use this BPF program in our example later.
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
char _license[] SEC("license") = "GPL";
SEC("iter/task_file")
int dump_task_file(struct bpf_iter__task_file *ctx)
{
struct seq_file *seq = ctx->meta->seq;
struct task_struct *task = ctx->task;
struct file *file = ctx->file;
__u32 fd = ctx->fd;
if (task == NULL || file == NULL)
return 0;
if (ctx->meta->seq_num == 0) {
BPF_SEQ_PRINTF(seq, " tgid pid fd file\n");
}
BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd,
(long)file->f_op);
return 0;
}
Creating a File Iterator with Parameters #
Now, let us look at how to create an iterator that includes only files of a process.
First, fill the bpf_iter_attach_opts struct as shown below:
LIBBPF_OPTS(bpf_iter_attach_opts, opts);
union bpf_iter_link_info linfo;
memset(&linfo, 0, sizeof(linfo));
linfo.task.pid = getpid();
opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo);
linfo.task.pid, if it is non-zero, directs the kernel to create an iterator that only includes opened files for the process with the specified pid. In this example, we will only be iterating files for our process. If linfo.task.pid is zero, the iterator will visit every opened file of every process. Similarly, linfo.task.tid directs the kernel to create an iterator that visits opened files of a specific thread, not a process. In this example, linfo.task.tid is different from linfo.task.pid only if the thread has a separate file descriptor table. In most circumstances, all process threads share a single file descriptor table.
Now, in the userspace program, pass the pointer of struct to the bpf_program__attach_iter().
link = bpf_program__attach_iter(prog, &opts);
iter_fd = bpf_iter_create(bpf_link__fd(link));
If both tid and pid are zero, an iterator created from this struct bpf_iter_attach_opts will include every opened file of every task in the system (in the namespace, actually.) It is the same as passing a NULL as the second argument to bpf_program__attach_iter().
The whole program looks like the following code:
#include <stdio.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "bpf_iter_task_ex.skel.h"
static int do_read_opts(struct bpf_program *prog, struct bpf_iter_attach_opts *opts)
{
struct bpf_link *link;
char buf[16] = {};
int iter_fd = -1, len;
int ret = 0;
link = bpf_program__attach_iter(prog, opts);
if (!link) {
fprintf(stderr, "bpf_program__attach_iter() fails\n");
return -1;
}
iter_fd = bpf_iter_create(bpf_link__fd(link));
if (iter_fd < 0) {
fprintf(stderr, "bpf_iter_create() fails\n");
ret = -1;
goto free_link;
}
/* not check contents, but ensure read() ends without error */
while ((len = read(iter_fd, buf, sizeof(buf) - 1)) > 0) {
buf[len] = 0;
printf("%s", buf);
}
printf("\n");
free_link:
if (iter_fd >= 0)
close(iter_fd);
bpf_link__destroy(link);
return 0;
}
static void test_task_file(void)
{
LIBBPF_OPTS(bpf_iter_attach_opts, opts);
struct bpf_iter_task_ex *skel;
union bpf_iter_link_info linfo;
skel = bpf_iter_task_ex__open_and_load();
if (skel == NULL)
return;
memset(&linfo, 0, sizeof(linfo));
linfo.task.pid = getpid();
opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo);
printf("PID %d\n", getpid());
do_read_opts(skel->progs.dump_task_file, &opts);
bpf_iter_task_ex__destroy(skel);
}
int main(int argc, const char * const * argv)
{
test_task_file();
return 0;
}
The following lines are the output of the program.
PID 1859
tgid pid fd file
1859 1859 0 ffffffff82270aa0
1859 1859 1 ffffffff82270aa0
1859 1859 2 ffffffff82270aa0
1859 1859 3 ffffffff82272980
1859 1859 4 ffffffff8225e120
1859 1859 5 ffffffff82255120
1859 1859 6 ffffffff82254f00
1859 1859 7 ffffffff82254d80
1859 1859 8 ffffffff8225abe0
Without Parameters #
Let us look at how a BPF iterator without parameters skips files of other processes in the system. In this case, the BPF program has to check the pid or the tid of tasks, or it will receive every opened file in the system (in the current pid namespace, actually). So, we usually add a global variable in the BPF program to pass a pid to the BPF program.
The BPF program would look like the following block.
...... int target_pid = 0; SEC("iter/task_file") int dump_task_file(struct bpf_iter__task_file *ctx) { ...... if (task->tgid != target_pid) /* Check task->pid instead to check thread IDs */ return 0; BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, (long)file->f_op); return 0; }
The user space program would look like the following block:
...... static void test_task_file(void) { ...... skel = bpf_iter_task_ex__open_and_load(); if (skel == NULL) return; skel->bss->target_pid = getpid(); /* process ID. For thread id, use gettid() */ memset(&linfo, 0, sizeof(linfo)); linfo.task.pid = getpid(); opts.link_info = &linfo; opts.link_info_len = sizeof(linfo); ...... }
target_pid is a global variable in the BPF program. The user space program should initialize the variable with a process ID to skip opened files of other processes in the BPF program. When you parametrize a BPF iterator, the iterator calls the BPF program fewer times which can save significant resources.
Parametrizing VMA Iterators #
By default, a BPF VMA iterator includes every VMA in every process. However, you can still specify a process or a thread to include only its VMAs. Unlike files, a thread can not have a separate address space (since Linux 2.6.0-test6). Here, using tid makes no difference from using pid.
Parametrizing Task Iterators #
A BPF task iterator with pid includes all tasks (threads) of a process. The BPF program receives these tasks one after another. You can specify a BPF task iterator with tid parameter to include only the tasks that match the given tid.
BPF licensing
Peios Kernel / Internals / BPF
Background #
- Classic BPF was BSD licensed
"BPF" was originally introduced as BSD Packet Filter in http://www.tcpdump.org/papers/bpf-usenix93.pdf. The corresponding instruction set and its implementation came from BSD with BSD license. That original instruction set is now known as "classic BPF".
However an instruction set is a specification for machine-language interaction, similar to a programming language. It is not a code. Therefore, the application of a BSD license may be misleading in a certain context, as the instruction set may enjoy no copyright protection.
- eBPF (extended BPF) instruction set continues to be BSD
In 2014, the classic BPF instruction set was significantly extended. We typically refer to this instruction set as eBPF to disambiguate it from cBPF. The eBPF instruction set is still BSD licensed.
Implementations of eBPF #
Using the eBPF instruction set requires implementing code in both kernel space and user space.
In Linux Kernel #
The reference implementations of the eBPF interpreter and various just-in-time compilers are part of Linux and are GPLv2 licensed. The implementation of eBPF helper functions is also GPLv2 licensed. Interpreters, JITs, helpers, and verifiers are called eBPF runtime.
In User Space #
There are also implementations of eBPF runtime (interpreter, JITs, helper functions) under Apache2 (https://github.com/iovisor/ubpf), MIT (https://github.com/qmonnet/rbpf), and BSD (https://github.com/DPDK/dpdk/blob/main/lib/librte_bpf).
In HW #
The HW can choose to execute eBPF instruction natively and provide eBPF runtime in HW or via the use of implementing firmware with a proprietary license.
In other operating systems #
Other kernels or user space implementations of eBPF instruction set and runtime can have proprietary licenses.
Using BPF programs in the Linux kernel #
Linux Kernel (while being GPLv2) allows linking of proprietary kernel modules under these rules: Documentation/process/license-rules.rst
When a kernel module is loaded, the linux kernel checks which functions it intends to use. If any function is marked as "GPL only," the corresponding module or program has to have GPL compatible license.
Loading BPF program into the Linux kernel is similar to loading a kernel module. BPF is loaded at run time and not statically linked to the Linux kernel. BPF program loading follows the same license checking rules as kernel modules. BPF programs can be proprietary if they don't use "GPL only" BPF helper functions.
Further, some BPF program types - Linux Security Modules (LSM) and TCP Congestion Control (struct_ops), as of Aug 2021 - are required to be GPL compatible even if they don't use "GPL only" helper functions directly. The registration step of LSM and TCP congestion control modules of the Linux kernel is done through EXPORT_SYMBOL_GPL kernel functions. In that sense LSM and struct_ops BPF programs are implicitly calling "GPL only" functions. The same restriction applies to BPF programs that call kernel functions directly via unstable interface also known as "kfunc".
Packaging BPF programs with user space applications #
Generally, proprietary-licensed applications and GPL licensed BPF programs written for the Linux kernel in the same package can co-exist because they are separate executable processes. This applies to both cBPF and eBPF programs.
Running BPF programs from userspace
Peios Kernel / Internals / BPF
This document describes the BPF_PROG_RUN facility for running BPF programs from userspace.
Overview #
The BPF_PROG_RUN command can be used through the bpf() syscall to execute a BPF program in the kernel and return the results to userspace. This can be used to unit test BPF programs against user-supplied context objects, and as way to explicitly execute programs in the kernel for their side effects. The command was previously named BPF_PROG_TEST_RUN, and both constants continue to be defined in the UAPI header, aliased to the same value.
The BPF_PROG_RUN command can be used to execute BPF programs of the following types:
-
BPF_PROG_TYPE_SOCKET_FILTER -
BPF_PROG_TYPE_SCHED_CLS -
BPF_PROG_TYPE_SCHED_ACT -
BPF_PROG_TYPE_XDP -
BPF_PROG_TYPE_SK_LOOKUP -
BPF_PROG_TYPE_CGROUP_SKB -
BPF_PROG_TYPE_LWT_IN -
BPF_PROG_TYPE_LWT_OUT -
BPF_PROG_TYPE_LWT_XMIT -
BPF_PROG_TYPE_FLOW_DISSECTOR -
BPF_PROG_TYPE_STRUCT_OPS -
BPF_PROG_TYPE_RAW_TRACEPOINT -
BPF_PROG_TYPE_SYSCALL -
BPF_PROG_TYPE_TRACING -
BPF_PROG_TYPE_NETFILTER
When using the BPF_PROG_RUN command, userspace supplies an input context object and (for program types operating on network packets) a buffer containing the packet data that the BPF program will operate on. The kernel will then execute the program and return the results to userspace. Note that programs will not have any side effects while being run in this mode; in particular, packets will not actually be redirected or dropped, the program return code will just be returned to userspace. A separate mode for live execution of XDP programs is provided, documented separately below.
Running XDP programs in "live frame mode" #
The BPF_PROG_RUN command has a separate mode for running live XDP programs, which can be used to execute XDP programs in a way where packets will actually be processed by the kernel after the execution of the XDP program as if they arrived on a physical interface. This mode is activated by setting the BPF_F_TEST_XDP_LIVE_FRAMES flag when supplying an XDP program to BPF_PROG_RUN.
The live packet mode is optimised for high performance execution of the supplied XDP program many times (suitable for, e.g., running as a traffic generator), which means the semantics are not quite as straight-forward as the regular test run mode. Specifically:
-
When executing an XDP program in live frame mode, the result of the execution will not be returned to userspace; instead, the kernel will perform the operation indicated by the program's return code (drop the packet, redirect it, etc). For this reason, setting the
data_outorctx_outattributes in the syscall parameters when running in this mode will be rejected. In addition, not all failures will be reported back to userspace directly; specifically, only fatal errors in setup or during execution (like memory allocation errors) will halt execution and return an error. If an error occurs in packet processing, like a failure to redirect to a given interface, execution will continue with the next repetition; these errors can be detected via the same trace points as for regular XDP programs. -
Userspace can supply an ifindex as part of the context object, just like in the regular (non-live) mode. The XDP program will be executed as though the packet arrived on this interface; i.e., the
ingress_ifindexof the context object will point to that interface. Furthermore, if the XDP program returnsXDP_PASS, the packet will be injected into the kernel networking stack as though it arrived on that ifindex, and if it returnsXDP_TX, the packet will be transmitted out of that same interface. Do note, though, that because the program execution is not happening in driver context, anXDP_TXis actually turned into the same action as anXDP_REDIRECTto that same interface (i.e., it will only work if the driver has support for thendo_xdp_xmitdriver op). -
When running the program with multiple repetitions, the execution will happen in batches. The batch size defaults to 64 packets (which is same as the maximum NAPI receive batch size), but can be specified by userspace through the
batch_sizeparameter, up to a maximum of 256 packets. For each batch, the kernel executes the XDP program repeatedly, each invocation getting a separate copy of the packet data. For each repetition, if the program drops the packet, the data page is immediately recycled (see below). Otherwise, the packet is buffered until the end of the batch, at which point all packets buffered this way during the batch are transmitted at once. -
When setting up the test run, the kernel will initialise a pool of memory pages of the same size as the batch size. Each memory page will be initialised with the initial packet data supplied by userspace at
BPF_PROG_RUNinvocation. When possible, the pages will be recycled on future program invocations, to improve performance. Pages will generally be recycled a full batch at a time, except when a packet is dropped (by return code or because of, say, a redirection error), in which case that page will be recycled immediately. If a packet ends up being passed to the regular networking stack (because the XDP program returnsXDP_PASS, or because it ends up being redirected to an interface that injects it into the stack), the page will be released and a new one will be allocated when the pool is empty.When recycling, the page content is not rewritten; only the packet boundary pointers (
data,data_endanddata_meta) in the context object will be reset to the original values. This means that if a program rewrites the packet contents, it has to be prepared to see either the original content or the modified version on subsequent invocations.
BPF Type Format (BTF)
Peios Kernel / Internals / BPF
1. Introduction #
BTF (BPF Type Format) is the metadata format which encodes the debug info related to BPF program/map. The name BTF was used initially to describe data types. The BTF was later extended to include function info for defined subroutines, and line info for source/line information.
The debug info is used for map pretty print, function signature, etc. The function signature enables better bpf program/function kernel symbol. The line info helps generate source annotated translated byte code, jited code and verifier log.
-
The BTF specification contains two parts,
-
BTF kernel API
-
BTF ELF file format
-
The kernel API is the contract between user space and kernel. The kernel verifies the BTF info before using it. The ELF file format is a user space contract between ELF file and libbpf loader.
The type and string sections are part of the BTF kernel API, describing the debug info (mostly types related) referenced by the bpf program. These two sections are discussed in details in 2. BTF Type and String Encoding.
2. BTF Type and String Encoding #
The file include/uapi/linux/btf.h provides high-level definition of how types/strings are encoded.
The beginning of data blob must be:
struct btf_header {
__u16 magic;
__u8 version;
__u8 flags;
__u32 hdr_len;
/* All offsets are in bytes relative to the end of this header */
__u32 type_off; /* offset of type section */
__u32 type_len; /* length of type section */
__u32 str_off; /* offset of string section */
__u32 str_len; /* length of string section */
};
The magic is 0xeB9F, which has different encoding for big and little endian systems, and can be used to test whether BTF is generated for big- or little-endian target. The btf_header is designed to be extensible with hdr_len equal to sizeof(struct btf_header) when a data blob is generated.
2.1 String Encoding #
The first string in the string section must be a null string. The rest of string table is a concatenation of other null-terminated strings.
2.2 Type Encoding #
The type id 0 is reserved for void type. The type section is parsed sequentially and type id is assigned to each recognized type starting from id 1. Currently, the following types are supported:
#define BTF_KIND_INT 1 /* Integer */
#define BTF_KIND_PTR 2 /* Pointer */
#define BTF_KIND_ARRAY 3 /* Array */
#define BTF_KIND_STRUCT 4 /* Struct */
#define BTF_KIND_UNION 5 /* Union */
#define BTF_KIND_ENUM 6 /* Enumeration up to 32-bit values */
#define BTF_KIND_FWD 7 /* Forward */
#define BTF_KIND_TYPEDEF 8 /* Typedef */
#define BTF_KIND_VOLATILE 9 /* Volatile */
#define BTF_KIND_CONST 10 /* Const */
#define BTF_KIND_RESTRICT 11 /* Restrict */
#define BTF_KIND_FUNC 12 /* Function */
#define BTF_KIND_FUNC_PROTO 13 /* Function Proto */
#define BTF_KIND_VAR 14 /* Variable */
#define BTF_KIND_DATASEC 15 /* Section */
#define BTF_KIND_FLOAT 16 /* Floating point */
#define BTF_KIND_DECL_TAG 17 /* Decl Tag */
#define BTF_KIND_TYPE_TAG 18 /* Type Tag */
#define BTF_KIND_ENUM64 19 /* Enumeration up to 64-bit values */
Note that the type section encodes debug info, not just pure types. BTF_KIND_FUNC is not a type, and it represents a defined subprogram.
Each type contains the following common data:
struct btf_type {
__u32 name_off;
/* "info" bits arrangement
* bits 0-15: vlen (e.g. # of struct's members)
* bits 16-23: unused
* bits 24-28: kind (e.g. int, ptr, array...etc)
* bits 29-30: unused
* bit 31: kind_flag, currently used by
* struct, union, enum, fwd, enum64,
* decl_tag and type_tag
*/
__u32 info;
/* "size" is used by INT, ENUM, STRUCT, UNION and ENUM64.
* "size" tells the size of the type it is describing.
*
* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
* FUNC, FUNC_PROTO, DECL_TAG and TYPE_TAG.
* "type" is a type_id referring to another type.
*/
union {
__u32 size;
__u32 type;
};
};
For certain kinds, the common data are followed by kind-specific data. The name_off in struct btf_type specifies the offset in the string table. The following sections detail encoding of each kind.
2.2.1 BTF_KIND_INT #
-
struct btf_typeencoding requirement:-
name_off: any valid offset -
info.kind_flag: 0 -
info.kind: BTF_KIND_INT -
info.vlen: 0 -
size: the size of the int type in bytes.
-
btf_type is followed by a u32 with the following bits arrangement:
#define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24)
#define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16)
#define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff)
The BTF_INT_ENCODING has the following attributes:
#define BTF_INT_SIGNED (1 << 0)
#define BTF_INT_CHAR (1 << 1)
#define BTF_INT_BOOL (1 << 2)
The BTF_INT_ENCODING() provides extra information: signedness, char, or bool, for the int type. The char and bool encoding are mostly useful for pretty print. At most one encoding can be specified for the int type.
The BTF_INT_BITS() specifies the number of actual bits held by this int type. For example, a 4-bit bitfield encodes BTF_INT_BITS() equals to 4. The btf_type.size * 8 must be equal to or greater than BTF_INT_BITS() for the type. The maximum value of BTF_INT_BITS() is 128.
The BTF_INT_OFFSET() specifies the starting bit offset to calculate values for this int. For example, a bitfield struct member has:
btf member bit offset 100 from the start of the structure,
btf member pointing to an int type,
the int type has
BTF_INT_OFFSET() = 2andBTF_INT_BITS() = 4
Then in the struct memory layout, this member will occupy 4 bits starting from bits 100 + 2 = 102.
Alternatively, the bitfield struct member can be the following to access the same bits as the above:
btf member bit offset 102,
btf member pointing to an int type,
the int type has
BTF_INT_OFFSET() = 0andBTF_INT_BITS() = 4
The original intention of BTF_INT_OFFSET() is to provide flexibility of bitfield encoding. Currently, both llvm and pahole generate BTF_INT_OFFSET() = 0 for all int types.
2.2.2 BTF_KIND_PTR #
-
struct btf_typeencoding requirement:-
name_off: 0 -
info.kind_flag: 0 -
info.kind: BTF_KIND_PTR -
info.vlen: 0 -
type: the pointee type of the pointer
-
No additional type data follow btf_type.
2.2.3 BTF_KIND_ARRAY #
-
struct btf_typeencoding requirement:-
name_off: 0 -
info.kind_flag: 0 -
info.kind: BTF_KIND_ARRAY -
info.vlen: 0 -
size/type: 0, not used
-
btf_type is followed by one struct btf_array:
struct btf_array {
__u32 type;
__u32 index_type;
__u32 nelems;
};
-
The
struct btf_arrayencoding:-
type: the element type -
index_type: the index type -
nelems: the number of elements for this array (0is also allowed).
-
The index_type can be any regular int type (u8, u16, u32, u64, unsigned __int128). The original design of including index_type follows DWARF, which has an index_type for its array type. Currently in BTF, beyond type verification, the index_type is not used.
The struct btf_array allows chaining through element type to represent multidimensional arrays. For example, for int a[5][6], the following type information illustrates the chaining:
[1]: int
[2]: array,
btf_array.type = [1],btf_array.nelems = 6[3]: array,
btf_array.type = [2],btf_array.nelems = 5
Currently, both pahole and llvm collapse multidimensional array into one-dimensional array, e.g., for a[5][6], the btf_array.nelems is equal to 30. This is because the original use case is map pretty print where the whole array is dumped out so one-dimensional array is enough. As more BTF usage is explored, pahole and llvm can be changed to generate proper chained representation for multidimensional arrays.
2.2.4 BTF_KIND_STRUCT #
2.2.5 BTF_KIND_UNION #
-
struct btf_typeencoding requirement:-
name_off: 0 or offset to a valid C identifier -
info.kind_flag: 0 or 1 -
info.kind: BTF_KIND_STRUCT or BTF_KIND_UNION -
info.vlen: the number of struct/union members -
info.size: the size of the struct/union in bytes
-
btf_type is followed by info.vlen number of struct btf_member.:
struct btf_member {
__u32 name_off;
__u32 type;
__u32 offset;
};
-
struct btf_memberencoding:-
name_off: offset to a valid C identifier -
type: the member type -
offset: <see below>
-
If the type info kind_flag is not set, the offset contains only bit offset of the member. Note that the base type of the bitfield can only be int or enum type. If the bitfield size is 32, the base type can be either int or enum type. If the bitfield size is not 32, the base type must be int, and int type BTF_INT_BITS() encodes the bitfield size.
If the kind_flag is set, the btf_member.offset contains both member bitfield size and bit offset. The bitfield size and bit offset are calculated as below.:
#define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24)
#define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff)
In this case, if the base type is an int type, it must be a regular int type:
BTF_INT_OFFSET()must be 0.
BTF_INT_BITS()must be equal to{1,2,4,8,16} * 8.
Commit 9d5f9f701b18 introduced kind_flag and explains why both modes exist.
2.2.6 BTF_KIND_ENUM #
-
struct btf_typeencoding requirement:-
name_off: 0 or offset to a valid C identifier -
info.kind_flag: 0 for unsigned, 1 for signed -
info.kind: BTF_KIND_ENUM -
info.vlen: number of enum values -
size: 1/2/4/8
-
btf_type is followed by info.vlen number of struct btf_enum.:
struct btf_enum {
__u32 name_off;
__s32 val;
};
-
The
btf_enumencoding:-
name_off: offset to a valid C identifier -
val: any value
-
If the original enum value is signed and the size is less than 4, that value will be sign extended into 4 bytes. If the size is 8, the value will be truncated into 4 bytes.
2.2.7 BTF_KIND_FWD #
-
struct btf_typeencoding requirement:-
name_off: offset to a valid C identifier -
info.kind_flag: 0 for struct, 1 for union -
info.kind: BTF_KIND_FWD -
info.vlen: 0 -
type: 0
-
No additional type data follow btf_type.
2.2.8 BTF_KIND_TYPEDEF #
-
struct btf_typeencoding requirement:-
name_off: offset to a valid C identifier -
info.kind_flag: 0 -
info.kind: BTF_KIND_TYPEDEF -
info.vlen: 0 -
type: the type which can be referred by name atname_off
-
No additional type data follow btf_type.
2.2.9 BTF_KIND_VOLATILE #
-
struct btf_typeencoding requirement:-
name_off: 0 -
info.kind_flag: 0 -
info.kind: BTF_KIND_VOLATILE -
info.vlen: 0 -
type: the type withvolatilequalifier
-
No additional type data follow btf_type.
2.2.10 BTF_KIND_CONST #
-
struct btf_typeencoding requirement:-
name_off: 0 -
info.kind_flag: 0 -
info.kind: BTF_KIND_CONST -
info.vlen: 0 -
type: the type withconstqualifier
-
No additional type data follow btf_type.
2.2.11 BTF_KIND_RESTRICT #
-
struct btf_typeencoding requirement:-
name_off: 0 -
info.kind_flag: 0 -
info.kind: BTF_KIND_RESTRICT -
info.vlen: 0 -
type: the type withrestrictqualifier
-
No additional type data follow btf_type.
2.2.12 BTF_KIND_FUNC #
-
struct btf_typeencoding requirement:-
name_off: offset to a valid C identifier -
info.kind_flag: 0 -
info.kind: BTF_KIND_FUNC -
-
info.vlen: linkage information (BTF_FUNC_STATIC, BTF_FUNC_GLOBALor BTF_FUNC_EXTERN - see 2.3.1 Function Linkage Constant Values)
-
-
type: a BTF_KIND_FUNC_PROTO type
-
No additional type data follow btf_type.
A BTF_KIND_FUNC defines not a type, but a subprogram (function) whose signature is defined by type. The subprogram is thus an instance of that type. The BTF_KIND_FUNC may in turn be referenced by a func_info in the 4.2 .BTF.ext section (ELF) or in the arguments to 3.3 BPF_PROG_LOAD (ABI).
Currently, only linkage values of BTF_FUNC_STATIC and BTF_FUNC_GLOBAL are supported in the kernel.
2.2.13 BTF_KIND_FUNC_PROTO #
-
struct btf_typeencoding requirement:-
name_off: 0 -
info.kind_flag: 0 -
info.kind: BTF_KIND_FUNC_PROTO -
info.vlen: # of parameters -
type: the return type
-
btf_type is followed by info.vlen number of struct btf_param.:
struct btf_param {
__u32 name_off;
__u32 type;
};
If a BTF_KIND_FUNC_PROTO type is referred by a BTF_KIND_FUNC type, then btf_param.name_off must point to a valid C identifier except for the possible last argument representing the variable argument. The btf_param.type refers to parameter type.
If the function has variable arguments, the last parameter is encoded with name_off = 0 and type = 0.
2.2.14 BTF_KIND_VAR #
-
struct btf_typeencoding requirement:-
name_off: offset to a valid C identifier -
info.kind_flag: 0 -
info.kind: BTF_KIND_VAR -
info.vlen: 0 -
type: the type of the variable
-
btf_type is followed by a single struct btf_variable with the following data:
struct btf_var {
__u32 linkage;
};
btf_var.linkage may take the values: BTF_VAR_STATIC, BTF_VAR_GLOBAL_ALLOCATED or BTF_VAR_GLOBAL_EXTERN - see 2.3.2 Variable Linkage Constant Values.
Not all type of global variables are supported by LLVM at this point. The following is currently available:
static variables with or without section attributes
global variables with section attributes
The latter is for future extraction of map key/value type id's from a map definition.
2.2.15 BTF_KIND_DATASEC #
-
struct btf_typeencoding requirement:-
-
name_off: offset to a valid name associated with a variable orone of .data/.bss/.rodata
-
-
info.kind_flag: 0 -
info.kind: BTF_KIND_DATASEC -
info.vlen: # of variables -
-
size: total section size in bytes (0 at compilation time, patchedto actual size by BPF loaders such as libbpf)
-
-
btf_type is followed by info.vlen number of struct btf_var_secinfo.:
struct btf_var_secinfo {
__u32 type;
__u32 offset;
__u32 size;
};
-
struct btf_var_secinfoencoding:-
type: the type of the BTF_KIND_VAR variable -
offset: the in-section offset of the variable -
size: the size of the variable in bytes
-
2.2.16 BTF_KIND_FLOAT #
-
struct btf_typeencoding requirement:-
name_off: any valid offset -
info.kind_flag: 0 -
info.kind: BTF_KIND_FLOAT -
info.vlen: 0 -
size: the size of the float type in bytes: 2, 4, 8, 12 or 16.
-
No additional type data follow btf_type.
2.2.17 BTF_KIND_DECL_TAG #
-
struct btf_typeencoding requirement:-
name_off: offset to a non-empty string -
info.kind_flag: 0 or 1 -
info.kind: BTF_KIND_DECL_TAG -
info.vlen: 0 -
type:struct,union,func,varortypedef
-
btf_type is followed by struct btf_decl_tag.:
struct btf_decl_tag {
__u32 component_idx;
};
The type should be struct, union, func, var or typedef. For var or typedef type, btf_decl_tag.component_idx must be -1. For the other three types, if the btf_decl_tag attribute is applied to the struct, union or func itself, btf_decl_tag.component_idx must be -1. Otherwise, the attribute is applied to a struct/union member or a func argument, and btf_decl_tag.component_idx should be a valid index (starting from 0) pointing to a member or an argument.
If info.kind_flag is 0, then this is a normal decl tag, and the name_off encodes btf_decl_tag attribute string.
If info.kind_flag is 1, then the decl tag represents an arbitrary __attribute__. In this case, name_off encodes a string representing the attribute-list of the attribute specifier. For example, for an __attribute__((aligned(4))) the string's contents is aligned(4).
2.2.18 BTF_KIND_TYPE_TAG #
-
struct btf_typeencoding requirement:-
name_off: offset to a non-empty string -
info.kind_flag: 0 or 1 -
info.kind: BTF_KIND_TYPE_TAG -
info.vlen: 0 -
type: the type withbtf_type_tagattribute
-
Currently, BTF_KIND_TYPE_TAG is only emitted for pointer types. It has the following btf type chain:
ptr -> [type_tag]*
-> [const | volatile | restrict | typedef]*
-> base_type
Basically, a pointer type points to zero or more type_tag, then zero or more const/volatile/restrict/typedef and finally the base type. The base type is one of int, ptr, array, struct, union, enum, func_proto and float types.
Similarly to decl tags, if the info.kind_flag is 0, then this is a normal type tag, and the name_off encodes btf_type_tag attribute string.
If info.kind_flag is 1, then the type tag represents an arbitrary __attribute__, and the name_off encodes a string representing the attribute-list of the attribute specifier.
2.2.19 BTF_KIND_ENUM64 #
-
struct btf_typeencoding requirement:-
name_off: 0 or offset to a valid C identifier -
info.kind_flag: 0 for unsigned, 1 for signed -
info.kind: BTF_KIND_ENUM64 -
info.vlen: number of enum values -
size: 1/2/4/8
-
btf_type is followed by info.vlen number of struct btf_enum64.:
struct btf_enum64 {
__u32 name_off;
__u32 val_lo32;
__u32 val_hi32;
};
-
The
btf_enum64encoding:-
name_off: offset to a valid C identifier -
val_lo32: lower 32-bit value for a 64-bit value -
val_hi32: high 32-bit value for a 64-bit value
-
If the original enum value is signed and the size is less than 8, that value will be sign extended into 8 bytes.
2.3 Constant Values #
2.3.1 Function Linkage Constant Values #
|
kind |
value |
description |
|---|---|---|
|
|
0x0 |
definition of subprogram not visible outside containing compilation unit |
|
|
0x1 |
definition of subprogram visible outside containing compilation unit |
|
|
0x2 |
declaration of a subprogram whose definition is outside the containing compilation unit |
2.3.2 Variable Linkage Constant Values #
|
kind |
value |
description |
|---|---|---|
|
|
0x0 |
definition of global variable not visible outside containing compilation unit |
|
|
0x1 |
definition of global variable visible outside containing compilation unit |
|
|
0x2 |
declaration of global variable whose definition is outside the containing compilation unit |
3. BTF Kernel API #
-
The following bpf syscall command involves BTF:
-
BPF_BTF_LOAD: load a blob of BTF data into kernel
-
BPF_MAP_CREATE: map creation with btf key and value type info.
-
BPF_PROG_LOAD: prog load with btf function and line info.
-
BPF_BTF_GET_FD_BY_ID: get a btf fd
-
BPF_OBJ_GET_INFO_BY_FD: btf, func_info, line_info and other btf related info are returned.
-
The workflow typically looks like:
Application:
BPF_BTF_LOAD
|
v
BPF_MAP_CREATE and BPF_PROG_LOAD
|
V
......
Introspection tool:
......
BPF_{PROG,MAP}_GET_NEXT_ID (get prog/map id's)
|
V
BPF_{PROG,MAP}_GET_FD_BY_ID (get a prog/map fd)
|
V
BPF_OBJ_GET_INFO_BY_FD (get bpf_prog_info/bpf_map_info with btf_id)
| |
V |
BPF_BTF_GET_FD_BY_ID (get btf_fd) |
| |
V |
BPF_OBJ_GET_INFO_BY_FD (get btf) |
| |
V V
pretty print types, dump func signatures and line info, etc.
3.1 BPF_BTF_LOAD #
Load a blob of BTF data into kernel. A blob of data, described in 2. BTF Type and String Encoding, can be directly loaded into the kernel. A btf_fd is returned to a userspace.
3.2 BPF_MAP_CREATE #
A map can be created with btf_fd and specified key/value type id.:
__u32 btf_fd; /* fd pointing to a BTF type data */
__u32 btf_key_type_id; /* BTF type_id of the key */
__u32 btf_value_type_id; /* BTF type_id of the value */
In libbpf, the map can be defined with extra annotation like below:
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, int);
__type(value, struct ipv_counts);
__uint(max_entries, 4);
} btf_map SEC(".maps");
During ELF parsing, libbpf is able to extract key/value type_id's and assign them to BPF_MAP_CREATE attributes automatically.
3.3 BPF_PROG_LOAD #
During prog_load, func_info and line_info can be passed to kernel with proper values for the following attributes:
__u32 insn_cnt;
__aligned_u64 insns;
......
__u32 prog_btf_fd; /* fd pointing to BTF type data */
__u32 func_info_rec_size; /* userspace bpf_func_info size */
__aligned_u64 func_info; /* func info */
__u32 func_info_cnt; /* number of bpf_func_info records */
__u32 line_info_rec_size; /* userspace bpf_line_info size */
__aligned_u64 line_info; /* line info */
__u32 line_info_cnt; /* number of bpf_line_info records */
The func_info and line_info are an array of below, respectively.:
struct bpf_func_info {
__u32 insn_off; /* [0, insn_cnt - 1] */
__u32 type_id; /* pointing to a BTF_KIND_FUNC type */
};
struct bpf_line_info {
__u32 insn_off; /* [0, insn_cnt - 1] */
__u32 file_name_off; /* offset to string table for the filename */
__u32 line_off; /* offset to string table for the source line */
__u32 line_col; /* line number and column number */
};
func_info_rec_size is the size of each func_info record, and line_info_rec_size is the size of each line_info record. Passing the record size to kernel make it possible to extend the record itself in the future.
-
Below are requirements for func_info:
-
func_info[0].insn_off must be 0.
-
the func_info insn_off is in strictly increasing order and matches bpf func boundaries.
-
-
Below are requirements for line_info:
-
the first insn in each func must have a line_info record pointing to it.
-
the line_info insn_off is in strictly increasing order.
-
For line_info, the line number and column number are defined as below:
#define BPF_LINE_INFO_LINE_NUM(line_col) ((line_col) >> 10)
#define BPF_LINE_INFO_LINE_COL(line_col) ((line_col) & 0x3ff)
3.4 BPF_{PROG,MAP}_GET_NEXT_ID #
In kernel, every loaded program, map or btf has a unique id. The id won't change during the lifetime of a program, map, or btf.
The bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID returns all id's, one for each command, to user space, for bpf program or maps, respectively, so an inspection tool can inspect all programs and maps.
3.5 BPF_{PROG,MAP}_GET_FD_BY_ID #
An introspection tool cannot use id to get details about program or maps. A file descriptor needs to be obtained first for reference-counting purpose.
3.6 BPF_OBJ_GET_INFO_BY_FD #
Once a program/map fd is acquired, an introspection tool can get the detailed information from kernel about this fd, some of which are BTF-related. For example, bpf_map_info returns btf_id and key/value type ids. bpf_prog_info returns btf_id, func_info, and line info for translated bpf byte codes, and jited_line_info.
3.7 BPF_BTF_GET_FD_BY_ID #
With btf_id obtained in bpf_map_info and bpf_prog_info, bpf syscall command BPF_BTF_GET_FD_BY_ID can retrieve a btf fd. Then, with command BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally loaded into the kernel with BPF_BTF_LOAD, can be retrieved.
With the btf blob, bpf_map_info, and bpf_prog_info, an introspection tool has full btf knowledge and is able to pretty print map key/values, dump func signatures and line info, along with byte/jit codes.
4. ELF File Format Interface #
4.1 .BTF section #
The .BTF section contains type and string data. The format of this section is same as the one describe in 2. BTF Type and String Encoding.
4.2 .BTF.ext section #
The .BTF.ext section encodes func_info, line_info and CO-RE relocations which needs loader manipulation before loading into the kernel.
The specification for .BTF.ext section is defined at tools/lib/bpf/btf.h and tools/lib/bpf/btf.c.
The current header of .BTF.ext section:
struct btf_ext_header {
__u16 magic;
__u8 version;
__u8 flags;
__u32 hdr_len;
/* All offsets are in bytes relative to the end of this header */
__u32 func_info_off;
__u32 func_info_len;
__u32 line_info_off;
__u32 line_info_len;
/* optional part of .BTF.ext header */
__u32 core_relo_off;
__u32 core_relo_len;
};
It is very similar to .BTF section. Instead of type/string section, it contains func_info, line_info and core_relo sub-sections. See 3.3 BPF_PROG_LOAD for details about func_info and line_info record format.
The func_info is organized as below.:
func_info_rec_size /* __u32 value */
btf_ext_info_sec for section #1 /* func_info for section #1 */
btf_ext_info_sec for section #2 /* func_info for section #2 */
...
func_info_rec_size specifies the size of bpf_func_info structure when .BTF.ext is generated. btf_ext_info_sec, defined below, is a collection of func_info for each specific ELF section.:
struct btf_ext_info_sec {
__u32 sec_name_off; /* offset to section name */
__u32 num_info;
/* Followed by num_info * record_size number of bytes */
__u8 data[0];
};
Here, num_info must be greater than 0.
The line_info is organized as below.:
line_info_rec_size /* __u32 value */
btf_ext_info_sec for section #1 /* line_info for section #1 */
btf_ext_info_sec for section #2 /* line_info for section #2 */
...
line_info_rec_size specifies the size of bpf_line_info structure when .BTF.ext is generated.
The interpretation of bpf_func_info->insn_off and bpf_line_info->insn_off is different between kernel API and ELF API. For kernel API, the insn_off is the instruction offset in the unit of struct bpf_insn. For ELF API, the insn_off is the byte offset from the beginning of section (btf_ext_info_sec->sec_name_off).
The core_relo is organized as below.:
core_relo_rec_size /* __u32 value */
btf_ext_info_sec for section #1 /* core_relo for section #1 */
btf_ext_info_sec for section #2 /* core_relo for section #2 */
core_relo_rec_size specifies the size of bpf_core_relo structure when .BTF.ext is generated. All bpf_core_relo structures within a single btf_ext_info_sec describe relocations applied to section named by btf_ext_info_sec->sec_name_off.
See Documentation/bpf/llvm_reloc.rst for more information on CO-RE relocations.
4.3 .BTF_ids section #
The .BTF_ids section encodes BTF ID values that are used within the kernel.
This section is created during the kernel compilation with the help of macros defined in include/linux/btf_ids.h header file. Kernel code can use them to create lists and sets (sorted lists) of BTF ID values.
The BTF_ID_LIST and BTF_ID macros define unsorted list of BTF ID values, with following syntax:
BTF_ID_LIST(list)
BTF_ID(type1, name1)
BTF_ID(type2, name2)
resulting in following layout in .BTF_ids section:
__BTF_ID__type1__name1__1:
.zero 4
__BTF_ID__type2__name2__2:
.zero 4
The u32 list[]; variable is defined to access the list.
The BTF_ID_UNUSED macro defines 4 zero bytes. It's used when we want to define unused entry in BTF_ID_LIST, like:
BTF_ID_LIST(bpf_skb_output_btf_ids)
BTF_ID(struct, sk_buff)
BTF_ID_UNUSED
BTF_ID(struct, task_struct)
The BTF_SET_START/END macros pair defines sorted list of BTF ID values and their count, with following syntax:
BTF_SET_START(set)
BTF_ID(type1, name1)
BTF_ID(type2, name2)
BTF_SET_END(set)
resulting in following layout in .BTF_ids section:
__BTF_ID__set__set:
.zero 4
__BTF_ID__type1__name1__3:
.zero 4
__BTF_ID__type2__name2__4:
.zero 4
The struct btf_id_set set; variable is defined to access the list.
The typeX name can be one of following:
struct, union, typedef, func
and is used as a filter when resolving the BTF ID value.
All the BTF ID lists and sets are compiled in the .BTF_ids section and resolved during the linking phase of kernel build by resolve_btfids tool.
4.4 .BTF.base section #
Split BTF - where the .BTF section only contains types not in the associated base .BTF section - is an extremely efficient way to encode type information for kernel modules, since they generally consist of a few module-specific types along with a large set of shared kernel types. The former are encoded in split BTF, while the latter are encoded in base BTF, resulting in more compact representations. A type in split BTF that refers to a type in base BTF refers to it using its base BTF ID, and split BTF IDs start at last_base_BTF_ID + 1.
The downside of this approach however is that this makes the split BTF somewhat brittle - when the base BTF changes, base BTF ID references are no longer valid and the split BTF itself becomes useless. The role of the .BTF.base section is to make split BTF more resilient for cases where the base BTF may change, as is the case for kernel modules not built every time the kernel is for example. .BTF.base contains named base types; INTs, FLOATs, STRUCTs, UNIONs, ENUM[64]s and FWDs. INTs and FLOATs are fully described in .BTF.base sections, while composite types like structs and unions are not fully defined - the .BTF.base type simply serves as a description of the type the split BTF referred to, so structs/unions have 0 members in the .BTF.base section. ENUM[64]s are similarly recorded with 0 members. Any other types are added to the split BTF. This distillation process then leaves us with a .BTF.base section with such minimal descriptions of base types and .BTF split section which refers to those base types. Later, we can relocate the split BTF using both the information stored in the .BTF.base section and the new .BTF base; the type information in the .BTF.base section allows us to update the split BTF references to point at the corresponding new base BTF IDs.
BTF relocation happens on kernel module load when a kernel module has a .BTF.base section, and libbpf also provides a btf__relocate() API to accomplish this.
As an example consider the following base BTF:
[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[2] STRUCT 'foo' size=8 vlen=2
'f1' type_id=1 bits_offset=0
'f2' type_id=1 bits_offset=32
...and associated split BTF:
[3] PTR '(anon)' type_id=2
i.e. split BTF describes a pointer to struct foo { int f1; int f2 };
.BTF.base will consist of:
[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[2] STRUCT 'foo' size=8 vlen=0
If we relocate the split BTF later using the following new base BTF:
[1] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
[2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[3] STRUCT 'foo' size=8 vlen=2
'f1' type_id=2 bits_offset=0
'f2' type_id=2 bits_offset=32
...we can use our .BTF.base description to know that the split BTF reference is to struct foo, and relocation results in new split BTF:
[4] PTR '(anon)' type_id=3
Note that we had to update BTF ID and start BTF ID for the split BTF.
So we see how .BTF.base plays the role of facilitating later relocation, leading to more resilient split BTF.
.BTF.base sections will be generated automatically for out-of-tree kernel module builds - i.e. where KBUILD_EXTMOD is set (as it would be for "make M=path/2/mod" cases). .BTF.base generation requires pahole support for the "distilled_base" BTF feature; this is available in pahole v1.28 and later.
5. Using BTF #
5.1 bpftool map pretty print #
With BTF, the map key/value can be printed based on fields rather than simply raw bytes. This is especially valuable for large structure or if your data structure has bitfields. For example, for the following map,:
enum A { A1, A2, A3, A4, A5 };
typedef enum A ___A;
struct tmp_t {
char a1:4;
int a2:4;
int :4;
__u32 a3:4;
int b;
___A b1:4;
enum A b2:4;
};
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, int);
__type(value, struct tmp_t);
__uint(max_entries, 1);
} tmpmap SEC(".maps");
bpftool is able to pretty print like below:
[{
"key": 0,
"value": {
"a1": 0x2,
"a2": 0x4,
"a3": 0x6,
"b": 7,
"b1": 0x8,
"b2": 0xa
}
}
]
5.2 bpftool prog dump #
The following is an example showing how func_info and line_info can help prog dump with better kernel symbol names, function prototypes and line information.:
$ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv
[...]
int test_long_fname_2(struct dummy_tracepoint_args * arg):
bpf_prog_44a040bf25481309_test_long_fname_2:
; static int test_long_fname_2(struct dummy_tracepoint_args *arg)
0: push %rbp
1: mov %rsp,%rbp
4: sub $0x30,%rsp
b: sub $0x28,%rbp
f: mov %rbx,0x0(%rbp)
13: mov %r13,0x8(%rbp)
17: mov %r14,0x10(%rbp)
1b: mov %r15,0x18(%rbp)
1f: xor %eax,%eax
21: mov %rax,0x20(%rbp)
25: xor %esi,%esi
; int key = 0;
27: mov %esi,-0x4(%rbp)
; if (!arg->sock)
2a: mov 0x8(%rdi),%rdi
; if (!arg->sock)
2e: cmp $0x0,%rdi
32: je 0x0000000000000070
34: mov %rbp,%rsi
; counts = bpf_map_lookup_elem(&btf_map, &key);
[...]
5.3 Verifier Log #
The following is an example of how line_info can help debugging verification failure.:
/* The code at tools/testing/selftests/bpf/test_xdp_noinline.c
* is modified as below.
*/
data = (void *)(long)xdp->data;
data_end = (void *)(long)xdp->data_end;
/*
if (data + 4 > data_end)
return XDP_DROP;
*/
*(u32 *)data = dst->dst;
$ bpftool prog load ./test_xdp_noinline.o /sys/fs/bpf/test_xdp_noinline type xdp
; data = (void *)(long)xdp->data;
224: (79) r2 = *(u64 *)(r10 -112)
225: (61) r2 = *(u32 *)(r2 +0)
; *(u32 *)data = dst->dst;
226: (63) *(u32 *)(r2 +0) = r1
invalid access to packet, off=0 size=4, R2(id=0,off=0,r=0)
R2 offset is outside of the packet
6. BTF Generation #
You need latest pahole
or llvm (8.0 or later). The pahole acts as a dwarf2btf converter. It doesn't support .BTF.ext and btf BTF_KIND_FUNC type yet. For example,:
-bash-4.4$ cat t.c
struct t {
int a:2;
int b:3;
int c:2;
} g;
-bash-4.4$ gcc -c -O2 -g t.c
-bash-4.4$ pahole -JV t.o
File t.o:
[1] STRUCT t kind_flag=1 size=4 vlen=3
a type_id=2 bitfield_size=2 bits_offset=0
b type_id=2 bitfield_size=3 bits_offset=2
c type_id=2 bitfield_size=2 bits_offset=5
[2] INT int size=4 bit_offset=0 nr_bits=32 encoding=SIGNED
The llvm is able to generate .BTF and .BTF.ext directly with -g for bpf target only. The assembly code (-S) is able to show the BTF encoding in assembly format.:
-bash-4.4$ cat t2.c
typedef int __int32;
struct t2 {
int a2;
int (*f2)(char q1, __int32 q2, ...);
int (*f3)();
} g2;
int main() { return 0; }
int test() { return 0; }
-bash-4.4$ clang -c -g -O2 --target=bpf t2.c
-bash-4.4$ readelf -S t2.o
......
[ 8] .BTF PROGBITS 0000000000000000 00000247
000000000000016e 0000000000000000 0 0 1
[ 9] .BTF.ext PROGBITS 0000000000000000 000003b5
0000000000000060 0000000000000000 0 0 1
[10] .rel.BTF.ext REL 0000000000000000 000007e0
0000000000000040 0000000000000010 16 9 8
......
-bash-4.4$ clang -S -g -O2 --target=bpf t2.c
-bash-4.4$ cat t2.s
......
.section .BTF,"",@progbits
.short 60319 # 0xeb9f
.byte 1
.byte 0
.long 24
.long 0
.long 220
.long 220
.long 122
.long 0 # BTF_KIND_FUNC_PROTO(id = 1)
.long 218103808 # 0xd000000
.long 2
.long 83 # BTF_KIND_INT(id = 2)
.long 16777216 # 0x1000000
.long 4
.long 16777248 # 0x1000020
......
.byte 0 # string offset=0
.ascii ".text" # string offset=1
.byte 0
.ascii "/home/yhs/tmp-pahole/t2.c" # string offset=7
.byte 0
.ascii "int main() { return 0; }" # string offset=33
.byte 0
.ascii "int test() { return 0; }" # string offset=58
.byte 0
.ascii "int" # string offset=83
......
.section .BTF.ext,"",@progbits
.short 60319 # 0xeb9f
.byte 1
.byte 0
.long 24
.long 0
.long 28
.long 28
.long 44
.long 8 # FuncInfo
.long 1 # FuncInfo section string offset=1
.long 2
.long .Lfunc_begin0
.long 3
.long .Lfunc_begin1
.long 5
.long 16 # LineInfo
.long 1 # LineInfo section string offset=1
.long 2
.long .Ltmp0
.long 7
.long 33
.long 7182 # Line 7 Col 14
.long .Ltmp3
.long 7
.long 58
.long 8206 # Line 8 Col 14
7. Testing #
The kernel BPF selftest tools/testing/selftests/bpf/prog_tests/btf.c provides an extensive set of BTF-related tests.
Contents
Peios Kernel / Internals / BPF
1 Clang implementation notes #
This document provides more details specific to the Clang/LLVM implementation of the eBPF instruction set.
1.1 Versions #
Clang defined "CPU" versions, where a CPU version of 3 corresponds to the current eBPF ISA.
Clang can select the eBPF ISA version using -mcpu=v3 for example to select version 3.
1.2 Arithmetic instructions #
For CPU versions prior to 3, Clang v7.0 and later can enable BPF_ALU support with -Xclang -target-feature -Xclang +alu32. In CPU version 3, support is automatically included.
1.3 Jump instructions #
If -O0 is used, Clang will generate the BPF_CALL | BPF_X | BPF_JMP (0x8d) instruction, which is not supported by the Linux kernel verifier.
1.4 Atomic operations #
Clang can generate atomic instructions by default when -mcpu=v3 is enabled. If a lower version for -mcpu is set, the only atomic instruction Clang can generate is BPF_ADD without BPF_FETCH. If you need to enable the atomics features, while keeping a lower -mcpu version, you can use -Xclang -target-feature -Xclang +alu32.
Classic BPF vs eBPF
Peios Kernel / Internals / BPF
eBPF is designed to be JITed with one to one mapping, which can also open up the possibility for GCC/LLVM compilers to generate optimized eBPF code through an eBPF backend that performs almost as fast as natively compiled code.
Some core changes of the eBPF format from classic BPF:
-
Number of registers increase from 2 to 10:
The old format had two registers A and X, and a hidden frame pointer. The new layout extends this to be 10 internal registers and a read-only frame pointer. Since 64-bit CPUs are passing arguments to functions via registers the number of args from eBPF program to in-kernel function is restricted to 5 and one register is used to accept return value from an in-kernel function. Natively, x86_64 passes first 6 arguments in registers, aarch64/ sparcv9/mips64 have 7 - 8 registers for arguments; x86_64 has 6 callee saved registers, and aarch64/sparcv9/mips64 have 11 or more callee saved registers.
Thus, all eBPF registers map one to one to HW registers on x86_64, aarch64, etc, and eBPF calling convention maps directly to ABIs used by the kernel on 64-bit architectures.
On 32-bit architectures JIT may map programs that use only 32-bit arithmetic and may let more complex programs to be interpreted.
R0 - R5 are scratch registers and eBPF program needs spill/fill them if necessary across calls. Note that there is only one eBPF program (== one eBPF main routine) and it cannot call other eBPF functions, it can only call predefined in-kernel functions, though.
-
Register width increases from 32-bit to 64-bit:
Still, the semantics of the original 32-bit ALU operations are preserved via 32-bit subregisters. All eBPF registers are 64-bit with 32-bit lower subregisters that zero-extend into 64-bit if they are being written to. That behavior maps directly to x86_64 and arm64 subregister definition, but makes other JITs more difficult.
32-bit architectures run 64-bit eBPF programs via interpreter. Their JITs may convert BPF programs that only use 32-bit subregisters into native instruction set and let the rest being interpreted.
Operation is 64-bit, because on 64-bit architectures, pointers are also 64-bit wide, and we want to pass 64-bit values in/out of kernel functions, so 32-bit eBPF registers would otherwise require to define register-pair ABI, thus, there won't be able to use a direct eBPF register to HW register mapping and JIT would need to do combine/split/move operations for every register in and out of the function, which is complex, bug prone and slow. Another reason is the use of atomic 64-bit counters.
-
Conditional jt/jf targets replaced with jt/fall-through:
While the original design has constructs such as
if (cond) jump_true; else jump_false;, they are being replaced into alternative constructs likeif (cond) jump_true; /* else fall-through */. -
Introduces bpf_call insn and register passing convention for zero overhead calls from/to other kernel functions:
Before an in-kernel function call, the eBPF program needs to place function arguments into R1 to R5 registers to satisfy calling convention, then the interpreter will take them from registers and pass to in-kernel function. If R1 - R5 registers are mapped to CPU registers that are used for argument passing on given architecture, the JIT compiler doesn't need to emit extra moves. Function arguments will be in the correct registers and BPF_CALL instruction will be JITed as single 'call' HW instruction. This calling convention was picked to cover common call situations without performance penalty.
After an in-kernel function call, R1 - R5 are reset to unreadable and R0 has a return value of the function. Since R6 - R9 are callee saved, their state is preserved across the call.
For example, consider three C functions:
u64 f1() { return (*_f2)(1); } u64 f2(u64 a) { return f3(a + 1, a); } u64 f3(u64 a, u64 b) { return a - b; }GCC can compile f1, f3 into x86_64:
f1: movl $1, %edi movq _f2(%rip), %rax jmp *%rax f3: movq %rdi, %rax subq %rsi, %rax retFunction f2 in eBPF may look like:
f2: bpf_mov R2, R1 bpf_add R1, 1 bpf_call f3 bpf_exitIf f2 is JITed and the pointer stored to
_f2. The calls f1 -> f2 -> f3 and returns will be seamless. Without JIT, __bpf_prog_run() interpreter needs to be used to call into f2.For practical reasons all eBPF programs have only one argument 'ctx' which is already placed into R1 (e.g. on __bpf_prog_run() startup) and the programs can call kernel functions with up to 5 arguments. Calls with 6 or more arguments are currently not supported, but these restrictions can be lifted if necessary in the future.
On 64-bit architectures all register map to HW registers one to one. For example, x86_64 JIT compiler can map them as ...
R0 - rax R1 - rdi R2 - rsi R3 - rdx R4 - rcx R5 - r8 R6 - rbx R7 - r13 R8 - r14 R9 - r15 R10 - rbp... since x86_64 ABI mandates rdi, rsi, rdx, rcx, r8, r9 for argument passing and rbx, r12 - r15 are callee saved.
Then the following eBPF pseudo-program:
bpf_mov R6, R1 /* save ctx */ bpf_mov R2, 2 bpf_mov R3, 3 bpf_mov R4, 4 bpf_mov R5, 5 bpf_call foo bpf_mov R7, R0 /* save foo() return value */ bpf_mov R1, R6 /* restore ctx for next call */ bpf_mov R2, 6 bpf_mov R3, 7 bpf_mov R4, 8 bpf_mov R5, 9 bpf_call bar bpf_add R0, R7 bpf_exitAfter JIT to x86_64 may look like:
push %rbp mov %rsp,%rbp sub $0x228,%rsp mov %rbx,-0x228(%rbp) mov %r13,-0x220(%rbp) mov %rdi,%rbx mov $0x2,%esi mov $0x3,%edx mov $0x4,%ecx mov $0x5,%r8d callq foo mov %rax,%r13 mov %rbx,%rdi mov $0x6,%esi mov $0x7,%edx mov $0x8,%ecx mov $0x9,%r8d callq bar add %r13,%rax mov -0x228(%rbp),%rbx mov -0x220(%rbp),%r13 leaveq retqWhich is in this example equivalent in C to:
u64 bpf_filter(u64 ctx) { return foo(ctx, 2, 3, 4, 5) + bar(ctx, 6, 7, 8, 9); }In-kernel functions foo() and bar() with prototype: u64 (*)(u64 arg1, u64 arg2, u64 arg3, u64 arg4, u64 arg5); will receive arguments in proper registers and place their return value into
%raxwhich is R0 in eBPF. Prologue and epilogue are emitted by JIT and are implicit in the interpreter. R0-R5 are scratch registers, so eBPF program needs to preserve them across the calls as defined by calling convention.For example the following program is invalid:
bpf_mov R1, 1 bpf_call foo bpf_mov R0, R1 bpf_exitAfter the call the registers R1-R5 contain junk values and cannot be read. An in-kernel verifier.rst is used to validate eBPF programs.
Also in the new design, eBPF is limited to 4096 insns, which means that any program will terminate quickly and will only call a fixed number of kernel functions. Original BPF and eBPF are two operand instructions, which helps to do one-to-one mapping between eBPF insn and x86 insn during JIT.
The input context pointer for invoking the interpreter function is generic, its content is defined by a specific use case. For seccomp register R1 points to seccomp_data, for converted BPF filters R1 points to a skb.
A program, that is translated internally consists of the following elements:
op:16, jt:8, jf:8, k:32 ==> op:8, dst_reg:4, src_reg:4, off:16, imm:32
So far 87 eBPF instructions were implemented. 8-bit 'op' opcode field has room for new instructions. Some of them may use 16/24/32 byte encoding. New instructions must be multiple of 8 bytes to preserve backward compatibility.
eBPF is a general purpose RISC instruction set. Not every register and every instruction are used during translation from original BPF to eBPF. For example, socket filters are not using exclusive add instruction, but tracing filters may do to maintain counters of events, for example. Register R9 is not used by socket filters either, but more complex filters may be running out of registers and would have to resort to spill/fill to stack.
eBPF can be used as a generic assembler for last step performance optimizations, socket filters and seccomp are using it as assembler. Tracing filters may use it as assembler to generate code from kernel. In kernel usage may not be bounded by security considerations, since generated eBPF code may be optimizing internal code path and not being exposed to the user space. Safety of eBPF can come from the verifier.rst. In such use cases as described, it may be used as safe instruction set.
Just like the original BPF, eBPF runs within a controlled environment, is deterministic and the kernel can easily prove that. The safety of the program can be determined in two steps: first step does depth-first-search to disallow loops and other CFG validation; second step starts from the first insn and descends all possible paths. It simulates execution of every insn and observes the state change of registers and stack.
opcode encoding #
eBPF is reusing most of the opcode encoding from classic to simplify conversion of classic BPF to eBPF.
For arithmetic and jump instructions the 8-bit 'code' field is divided into three parts:
+----------------+--------+--------------------+
| 4 bits | 1 bit | 3 bits |
| operation code | source | instruction class |
+----------------+--------+--------------------+
(MSB) (LSB)
Three LSB bits store instruction class which is one of:
Classic BPF classes
eBPF classes
BPF_LD 0x00
BPF_LD 0x00
BPF_LDX 0x01
BPF_LDX 0x01
BPF_ST 0x02
BPF_ST 0x02
BPF_STX 0x03
BPF_STX 0x03
BPF_ALU 0x04
BPF_ALU 0x04
BPF_JMP 0x05
BPF_JMP 0x05
BPF_RET 0x06
BPF_JMP32 0x06
BPF_MISC 0x07
BPF_ALU64 0x07
The 4th bit encodes the source operand ...
BPF_K 0x00 BPF_X 0x08
in classic BPF, this means:
BPF_SRC(code) == BPF_X - use register X as source operand BPF_SRC(code) == BPF_K - use 32-bit immediate as source operandin eBPF, this means:
BPF_SRC(code) == BPF_X - use 'src_reg' register as source operand BPF_SRC(code) == BPF_K - use 32-bit immediate as source operand
... and four MSB bits store operation code.
If BPF_CLASS(code) == BPF_ALU or BPF_ALU64 [ in eBPF ], BPF_OP(code) is one of:
BPF_ADD 0x00
BPF_SUB 0x10
BPF_MUL 0x20
BPF_DIV 0x30
BPF_OR 0x40
BPF_AND 0x50
BPF_LSH 0x60
BPF_RSH 0x70
BPF_NEG 0x80
BPF_MOD 0x90
BPF_XOR 0xa0
BPF_MOV 0xb0 /* eBPF only: mov reg to reg */
BPF_ARSH 0xc0 /* eBPF only: sign extending shift right */
BPF_END 0xd0 /* eBPF only: endianness conversion */
If BPF_CLASS(code) == BPF_JMP or BPF_JMP32 [ in eBPF ], BPF_OP(code) is one of:
BPF_JA 0x00 /* BPF_JMP only */
BPF_JEQ 0x10
BPF_JGT 0x20
BPF_JGE 0x30
BPF_JSET 0x40
BPF_JNE 0x50 /* eBPF only: jump != */
BPF_JSGT 0x60 /* eBPF only: signed '>' */
BPF_JSGE 0x70 /* eBPF only: signed '>=' */
BPF_CALL 0x80 /* eBPF BPF_JMP only: function call */
BPF_EXIT 0x90 /* eBPF BPF_JMP only: function return */
BPF_JLT 0xa0 /* eBPF only: unsigned '<' */
BPF_JLE 0xb0 /* eBPF only: unsigned '<=' */
BPF_JSLT 0xc0 /* eBPF only: signed '<' */
BPF_JSLE 0xd0 /* eBPF only: signed '<=' */
So BPF_ADD | BPF_X | BPF_ALU means 32-bit addition in both classic BPF and eBPF. There are only two registers in classic BPF, so it means A += X. In eBPF it means dst_reg = (u32) dst_reg + (u32) src_reg; similarly, BPF_XOR | BPF_K | BPF_ALU means A ^= imm32 in classic BPF and analogous src_reg = (u32) src_reg ^ (u32) imm32 in eBPF.
Classic BPF is using BPF_MISC class to represent A = X and X = A moves. eBPF is using BPF_MOV | BPF_X | BPF_ALU code instead. Since there are no BPF_MISC operations in eBPF, the class 7 is used as BPF_ALU64 to mean exactly the same operations as BPF_ALU, but with 64-bit wide operands instead. So BPF_ADD | BPF_X | BPF_ALU64 means 64-bit addition, i.e.: dst_reg = dst_reg + src_reg
Classic BPF wastes the whole BPF_RET class to represent a single ret operation. Classic BPF_RET | BPF_K means copy imm32 into return register and perform function exit. eBPF is modeled to match CPU, so BPF_JMP | BPF_EXIT in eBPF means function exit only. The eBPF program needs to store return value into register R0 before doing a BPF_EXIT. Class 6 in eBPF is used as BPF_JMP32 to mean exactly the same operations as BPF_JMP, but with 32-bit wide operands for the comparisons instead.
For load and store instructions the 8-bit 'code' field is divided as:
+--------+--------+-------------------+
| 3 bits | 2 bits | 3 bits |
| mode | size | instruction class |
+--------+--------+-------------------+
(MSB) (LSB)
Size modifier is one of ...
BPF_W 0x00 /* word */
BPF_H 0x08 /* half word */
BPF_B 0x10 /* byte */
BPF_DW 0x18 /* eBPF only, double word */
... which encodes size of load/store operation:
B - 1 byte
H - 2 byte
W - 4 byte
DW - 8 byte (eBPF only)
Mode modifier is one of:
BPF_IMM 0x00 /* used for 32-bit mov in classic BPF and 64-bit in eBPF */
BPF_ABS 0x20
BPF_IND 0x40
BPF_MEM 0x60
BPF_LEN 0x80 /* classic BPF only, reserved in eBPF */
BPF_MSH 0xa0 /* classic BPF only, reserved in eBPF */
BPF_ATOMIC 0xc0 /* eBPF only, atomic operations */
BPF cpumask kfuncs
Peios Kernel / Internals / BPF
1. Introduction #
struct cpumask is a bitmap data structure in the kernel whose indices reflect the CPUs on the system. Commonly, cpumasks are used to track which CPUs a task is affinitized to, but they can also be used to e.g. track which cores are associated with a scheduling domain, which cores on a machine are idle, etc.
BPF provides programs with a set of BPF Kernel Functions (kfuncs) that can be used to allocate, mutate, query, and free cpumasks.
2. BPF cpumask objects #
There are two different types of cpumasks that can be used by BPF programs.
2.1 struct bpf_cpumask * #
struct bpf_cpumask * is a cpumask that is allocated by BPF, on behalf of a BPF program, and whose lifecycle is entirely controlled by BPF. These cpumasks are RCU-protected, can be mutated, can be used as kptrs, and can be safely cast to a struct cpumask *.
2.1.1 struct bpf_cpumask * lifecycle #
A struct bpf_cpumask * is allocated, acquired, and released, using the following functions:
bpf_cpumask_create() #
__bpf_kfunc struct bpf_cpumask * Create a mutable BPF cpumask.
Parameters
void: no arguments
Description
Allocates a cpumask that can be queried, mutated, acquired, and released by
a BPF program. The cpumask returned by this function must either be embedded
in a map as a kptr, or freed with bpf_cpumask_release().
bpf_cpumask_create() allocates memory using the BPF memory allocator, and
will not block. It may return NULL if no memory is available.
Return
- A pointer to a new struct bpf_cpumask instance on success.
- NULL if the BPF memory allocator is out of memory.
bpf_cpumask_acquire() #
__bpf_kfunc struct bpf_cpumask * Acquire a reference to a BPF cpumask.
Parameters
struct bpf_cpumask *cpumask: The BPF cpumask being acquired. The cpumask must be a trusted pointer.
Description
Acquires a reference to a BPF cpumask. The cpumask returned by this function
must either be embedded in a map as a kptr, or freed with
bpf_cpumask_release().
Return
- The struct bpf_cpumask pointer passed to the function.
bpf_cpumask_release() #
__bpf_kfunc void Release a previously acquired BPF cpumask.
Parameters
struct bpf_cpumask *cpumask: The cpumask being released.
Description
Releases a previously acquired reference to a BPF cpumask. When the final reference of the BPF cpumask has been released, it is subsequently freed in an RCU callback in the BPF memory allocator.
For example:
;
cpumask_map ;
static int
/**
* A sample tracepoint showing how a task's cpumask can be queried and
* recorded as a kptr.
*/
int
2.1.1 struct bpf_cpumask * as kptrs #
As mentioned and illustrated above, these struct bpf_cpumask * objects can also be stored in a map and used as kptrs. If a struct bpf_cpumask * is in a map, the reference can be removed from the map with bpf_kptr_xchg(), or opportunistically acquired using RCU:
/* struct containing the struct bpf_cpumask kptr which is stored in the map. */
;
/* The map containing struct cpumasks_kfunc_map_value entries. */
cpumasks_kfunc_map ;
/* ... */
/**
* A simple example tracepoint program showing how a
* struct bpf_cpumask * kptr that is stored in a map can
* be passed to kfuncs using RCU protection.
*/
int
2.2 struct cpumask #
struct cpumask is the object that actually contains the cpumask bitmap being queried, mutated, etc. A struct bpf_cpumask wraps a struct cpumask, which is why it's safe to cast it as such (note however that it is not safe to cast a struct cpumask * to a struct bpf_cpumask *, and the verifier will reject any program that tries to do so).
As we'll see below, any kfunc that mutates its cpumask argument will take a struct bpf_cpumask * as that argument. Any argument that simply queries the cpumask will instead take a struct cpumask *.
3. cpumask kfuncs #
Above, we described the kfuncs that can be used to allocate, acquire, release, etc a struct bpf_cpumask *. This section of the document will describe the kfuncs for mutating and querying cpumasks.
3.1 Mutating cpumasks #
Some cpumask kfuncs are "read-only" in that they don't mutate any of their arguments, whereas others mutate at least one argument (which means that the argument must be a struct bpf_cpumask *, as described above).
This section will describe all of the cpumask kfuncs which mutate at least one argument. 3.2 Querying cpumasks below describes the read-only kfuncs.
3.1.1 Setting and clearing CPUs #
bpf_cpumask_set_cpu() and bpf_cpumask_clear_cpu() can be used to set and clear a CPU in a struct bpf_cpumask respectively:
bpf_cpumask_set_cpu() #
__bpf_kfunc void Set a bit for a CPU in a BPF cpumask.
Parameters
u32 cpu: The CPU to be set in the cpumask.struct bpf_cpumask *cpumask: The BPF cpumask in which a bit is being set.
bpf_cpumask_clear_cpu() #
__bpf_kfunc void Clear a bit for a CPU in a BPF cpumask.
Parameters
u32 cpu: The CPU to be cleared from the cpumask.struct bpf_cpumask *cpumask: The BPF cpumask in which a bit is being cleared.
These kfuncs are pretty straightforward, and can be used, for example, as follows:
/**
* A sample tracepoint showing how a cpumask can be queried.
*/
int
bpf_cpumask_test_and_set_cpu() and bpf_cpumask_test_and_clear_cpu() are complementary kfuncs that allow callers to atomically test and set (or clear) CPUs:
bpf_cpumask_test_and_set_cpu() #
__bpf_kfunc bool Atomically test and set a CPU in a BPF cpumask.
Parameters
u32 cpu: The CPU being set and queried for.struct bpf_cpumask *cpumask: The BPF cpumask being set and queried for containing a CPU.
Return
- true - cpu is set in the cpumask
- false - cpu was not set in the cpumask, or cpu is invalid.
bpf_cpumask_test_and_clear_cpu() #
__bpf_kfunc bool Atomically test and clear a CPU in a BPF cpumask.
Parameters
u32 cpu: The CPU being cleared and queried for.struct bpf_cpumask *cpumask: The BPF cpumask being cleared and queried for containing a CPU.
Return
- true - cpu is set in the cpumask
- false - cpu was not set in the cpumask, or cpu is invalid.
We can also set and clear entire struct bpf_cpumask * objects in one operation using bpf_cpumask_setall() and bpf_cpumask_clear():
bpf_cpumask_setall() #
__bpf_kfunc void Set all of the bits in a BPF cpumask.
Parameters
struct bpf_cpumask *cpumask: The BPF cpumask having all of its bits set.
bpf_cpumask_clear() #
__bpf_kfunc void Clear all of the bits in a BPF cpumask.
Parameters
struct bpf_cpumask *cpumask: The BPF cpumask being cleared.
3.1.2 Operations between cpumasks #
In addition to setting and clearing individual CPUs in a single cpumask, callers can also perform bitwise operations between multiple cpumasks using bpf_cpumask_and(), bpf_cpumask_or(), and bpf_cpumask_xor():
bpf_cpumask_and() #
__bpf_kfunc bool AND two cpumasks and store the result.
Parameters
struct bpf_cpumask *dst: The BPF cpumask where the result is being stored.const struct cpumask *src1: The first input.const struct cpumask *src2: The second input.
Return
- true - dst has at least one bit set following the operation
- false - dst is empty following the operation
Description
struct bpf_cpumask pointers may be safely passed to src1 and src2.
bpf_cpumask_or() #
__bpf_kfunc void OR two cpumasks and store the result.
Parameters
struct bpf_cpumask *dst: The BPF cpumask where the result is being stored.const struct cpumask *src1: The first input.const struct cpumask *src2: The second input.
Description
struct bpf_cpumask pointers may be safely passed to src1 and src2.
bpf_cpumask_xor() #
__bpf_kfunc void XOR two cpumasks and store the result.
Parameters
struct bpf_cpumask *dst: The BPF cpumask where the result is being stored.const struct cpumask *src1: The first input.const struct cpumask *src2: The second input.
Description
struct bpf_cpumask pointers may be safely passed to src1 and src2.
The following is an example of how they may be used. Note that some of the kfuncs shown in this example will be covered in more detail below.
/**
* A sample tracepoint showing how a cpumask can be mutated using
bitwise operators (and queried).
*/
int
The contents of an entire cpumask may be copied to another using bpf_cpumask_copy():
bpf_cpumask_copy() #
__bpf_kfunc void Copy the contents of a cpumask into a BPF cpumask.
Parameters
struct bpf_cpumask *dst: The BPF cpumask being copied into.const struct cpumask *src: The cpumask being copied.
Description
A struct bpf_cpumask pointer may be safely passed to src.
3.2 Querying cpumasks #
In addition to the above kfuncs, there is also a set of read-only kfuncs that can be used to query the contents of cpumasks.
bpf_cpumask_first() #
__bpf_kfunc u32 Get the index of the first nonzero bit in the cpumask.
Parameters
const struct cpumask *cpumask: The cpumask being queried.
Description
Find the index of the first nonzero bit of the cpumask. A struct bpf_cpumask pointer may be safely passed to this function.
Return
- The index of the first nonzero bit in the struct cpumask.
bpf_cpumask_first_zero() #
__bpf_kfunc u32 Get the index of the first unset bit in the cpumask.
Parameters
const struct cpumask *cpumask: The cpumask being queried.
Description
Find the index of the first unset bit of the cpumask. A struct bpf_cpumask pointer may be safely passed to this function.
Return
- The index of the first zero bit in the struct cpumask.
bpf_cpumask_first_and() #
__bpf_kfunc u32 Return the index of the first nonzero bit from the AND of two cpumasks.
Parameters
const struct cpumask *src1: The first cpumask.const struct cpumask *src2: The second cpumask.
Description
Find the index of the first nonzero bit of the AND of two cpumasks. struct bpf_cpumask pointers may be safely passed to src1 and src2.
Return
- The index of the first bit that is nonzero in both cpumask instances.
bpf_cpumask_test_cpu() #
__bpf_kfunc bool Test whether a CPU is set in a cpumask.
Parameters
u32 cpu: The CPU being queried for.const struct cpumask *cpumask: The cpumask being queried for containing a CPU.
Return
- true - cpu is set in the cpumask
- false - cpu was not set in the cpumask, or cpu is an invalid cpu.
bpf_cpumask_weight() #
__bpf_kfunc u32 Return the number of bits in cpumask.
Parameters
const struct cpumask *cpumask: The cpumask being queried.
Description
Count the number of set bits in the given cpumask.
Return
- The number of bits set in the mask.
bpf_cpumask_equal() #
__bpf_kfunc bool Check two cpumasks for equality.
Parameters
const struct cpumask *src1: The first input.const struct cpumask *src2: The second input.
Return
- true - src1 and src2 have the same bits set.
- false - src1 and src2 differ in at least one bit.
Description
struct bpf_cpumask pointers may be safely passed to src1 and src2.
bpf_cpumask_intersects() #
__bpf_kfunc bool Check two cpumasks for overlap.
Parameters
const struct cpumask *src1: The first input.const struct cpumask *src2: The second input.
Return
- true - src1 and src2 have at least one of the same bits set.
- false - src1 and src2 don't have any of the same bits set.
Description
struct bpf_cpumask pointers may be safely passed to src1 and src2.
bpf_cpumask_subset() #
__bpf_kfunc bool Check if a cpumask is a subset of another.
Parameters
const struct cpumask *src1: The first cpumask being checked as a subset.const struct cpumask *src2: The second cpumask being checked as a superset.
Return
- true - All of the bits of src1 are set in src2.
- false - At least one bit in src1 is not set in src2.
Description
struct bpf_cpumask pointers may be safely passed to src1 and src2.
bpf_cpumask_empty() #
__bpf_kfunc bool Check if a cpumask is empty.
Parameters
const struct cpumask *cpumask: The cpumask being checked.
Return
- true - None of the bits in cpumask are set.
- false - At least one bit in cpumask is set.
Description
A struct bpf_cpumask pointer may be safely passed to cpumask.
bpf_cpumask_full() #
__bpf_kfunc bool Check if a cpumask has all bits set.
Parameters
const struct cpumask *cpumask: The cpumask being checked.
Return
- true - All of the bits in cpumask are set.
- false - At least one bit in cpumask is cleared.
Description
A struct bpf_cpumask pointer may be safely passed to cpumask.
bpf_cpumask_any_distribute() #
__bpf_kfunc u32 Return a random set CPU from a cpumask.
Parameters
const struct cpumask *cpumask: The cpumask being queried.
Return
- A random set bit within [0, num_cpus) if at least one bit is set.
-
= num_cpus if no bit is set.
Description
A struct bpf_cpumask pointer may be safely passed to src.
bpf_cpumask_any_and_distribute() #
__bpf_kfunc u32 Return a random set CPU from the AND of two cpumasks.
Parameters
const struct cpumask *src1: The first cpumask.const struct cpumask *src2: The second cpumask.
Return
- A random set bit within [0, num_cpus) from the AND of two cpumasks, if at least one bit is set.
-
= num_cpus if no bit is set.
Description
struct bpf_cpumask pointers may be safely passed to src1 and src2.
Some example usages of these querying kfuncs were shown above. We will not replicate those examples here. Note, however, that all of the aforementioned kfuncs are tested in tools/testing/selftests/bpf/progs/cpumask_success.c, so please take a look there if you're looking for more examples of how they can be used.
4. Adding BPF cpumask kfuncs #
The set of supported BPF cpumask kfuncs are not (yet) a 1-1 match with the cpumask operations in include/linux/cpumask.h. Any of those cpumask operations could easily be encapsulated in a new kfunc if and when required. If you'd like to support a new cpumask operation, please feel free to submit a patch. If you do add a new cpumask kfunc, please document it here, and add any relevant selftest testcases to the cpumask selftest suite.
BPF drgn tools
Peios Kernel / Internals / BPF
drgn scripts is a convenient and easy to use mechanism to retrieve arbitrary kernel data structures. drgn is not relying on kernel UAPI to read the data. Instead it's reading directly from /proc/kcore or vmcore and pretty prints the data based on DWARF debug information from vmlinux.
This document describes BPF related drgn tools.
See drgn/tools for all tools available at the moment and drgn/doc for more details on drgn itself.
bpf_inspect.py #
Description #
bpf_inspect.py is a tool intended to inspect BPF programs and maps. It can iterate over all programs and maps in the system and print basic information about these objects, including id, type and name.
The main use-case bpf_inspect.py covers is to show BPF programs of types BPF_PROG_TYPE_EXT and BPF_PROG_TYPE_TRACING attached to other BPF programs via freplace/fentry/fexit mechanisms, since there is no user-space API to get this information.
Getting started #
List BPF programs (full names are obtained from BTF):
% sudo bpf_inspect.py prog
27: BPF_PROG_TYPE_TRACEPOINT tracepoint__tcp__tcp_send_reset
4632: BPF_PROG_TYPE_CGROUP_SOCK_ADDR tw_ipt_bind
49464: BPF_PROG_TYPE_RAW_TRACEPOINT raw_tracepoint__sched_process_exit
List BPF maps:
% sudo bpf_inspect.py map
2577: BPF_MAP_TYPE_HASH tw_ipt_vips
4050: BPF_MAP_TYPE_STACK_TRACE stack_traces
4069: BPF_MAP_TYPE_PERCPU_ARRAY ned_dctcp_cntr
Find BPF programs attached to BPF program test_pkt_access:
% sudo bpf_inspect.py p | grep test_pkt_access
650: BPF_PROG_TYPE_SCHED_CLS test_pkt_access
654: BPF_PROG_TYPE_TRACING test_main linked:[650->25: BPF_TRAMP_FEXIT test_pkt_access->test_pkt_access()]
655: BPF_PROG_TYPE_TRACING test_subprog1 linked:[650->29: BPF_TRAMP_FEXIT test_pkt_access->test_pkt_access_subprog1()]
656: BPF_PROG_TYPE_TRACING test_subprog2 linked:[650->31: BPF_TRAMP_FEXIT test_pkt_access->test_pkt_access_subprog2()]
657: BPF_PROG_TYPE_TRACING test_subprog3 linked:[650->21: BPF_TRAMP_FEXIT test_pkt_access->test_pkt_access_subprog3()]
658: BPF_PROG_TYPE_EXT new_get_skb_len linked:[650->16: BPF_TRAMP_REPLACE test_pkt_access->get_skb_len()]
659: BPF_PROG_TYPE_EXT new_get_skb_ifindex linked:[650->23: BPF_TRAMP_REPLACE test_pkt_access->get_skb_ifindex()]
660: BPF_PROG_TYPE_EXT new_get_constant linked:[650->19: BPF_TRAMP_REPLACE test_pkt_access->get_constant()]
It can be seen that there is a program test_pkt_access, id 650 and there are multiple other tracing and ext programs attached to functions in test_pkt_access.
For example the line:
658: BPF_PROG_TYPE_EXT new_get_skb_len linked:[650->16: BPF_TRAMP_REPLACE test_pkt_access->get_skb_len()]
, means that BPF program id 658, type BPF_PROG_TYPE_EXT, name new_get_skb_len replaces (BPF_TRAMP_REPLACE) function get_skb_len() that has BTF id 16 in BPF program id 650, name test_pkt_access.
Getting help:
% sudo bpf_inspect.py
usage: bpf_inspect.py [-h] {prog,p,map,m} ...
drgn script to list BPF programs or maps and their properties
unavailable via kernel API.
See https://github.com/osandov/drgn/ for more details on drgn.
optional arguments:
-h, --help show this help message and exit
subcommands:
{prog,p,map,m}
prog (p) list BPF programs
map (m) list BPF maps
Customization #
The script is intended to be customized by developers to print relevant information about BPF programs, maps and other objects.
For example, to print struct bpf_prog_aux for BPF program id 53077:
% git diff
diff --git a/tools/bpf_inspect.py b/tools/bpf_inspect.py
index 650e228..aea2357 100755
--- a/tools/bpf_inspect.py
+++ b/tools/bpf_inspect.py
@@ -112,7 +112,9 @@ def list_bpf_progs(args):
if linked:
linked = f" linked:[{linked}]"
- print(f"{id_:>6}: {type_:32} {name:32} {linked}")
+ if id_ == 53077:
+ print(f"{id_:>6}: {type_:32} {name:32}")
+ print(f"{bpf_prog.aux}")
def list_bpf_maps(args):
It produces the output:
% sudo bpf_inspect.py p
53077: BPF_PROG_TYPE_XDP tw_xdp_policer
*(struct bpf_prog_aux *)0xffff8893fad4b400 = {
.refcnt = (atomic64_t){
.counter = (long)58,
},
.used_map_cnt = (u32)1,
.max_ctx_offset = (u32)8,
.max_pkt_offset = (u32)15,
.max_tp_access = (u32)0,
.stack_depth = (u32)8,
.id = (u32)53077,
.func_cnt = (u32)0,
.func_idx = (u32)0,
.attach_btf_id = (u32)0,
.linked_prog = (struct bpf_prog *)0x0,
.verifier_zext = (bool)0,
.offload_requested = (bool)0,
.attach_btf_trace = (bool)0,
.func_proto_unreliable = (bool)0,
.trampoline_prog_type = (enum bpf_tramp_prog_type)BPF_TRAMP_FENTRY,
.trampoline = (struct bpf_trampoline *)0x0,
.tramp_hlist = (struct hlist_node){
.next = (struct hlist_node *)0x0,
.pprev = (struct hlist_node **)0x0,
},
.attach_func_proto = (const struct btf_type *)0x0,
.attach_func_name = (const char *)0x0,
.func = (struct bpf_prog **)0x0,
.jit_data = (void *)0x0,
.poke_tab = (struct bpf_jit_poke_descriptor *)0x0,
.size_poke_tab = (u32)0,
.ksym_tnode = (struct latch_tree_node){
.node = (struct rb_node [2]){
{
.__rb_parent_color = (unsigned long)18446612956263126665,
.rb_right = (struct rb_node *)0x0,
.rb_left = (struct rb_node *)0xffff88a0be3d0088,
},
{
.__rb_parent_color = (unsigned long)18446612956263126689,
.rb_right = (struct rb_node *)0x0,
.rb_left = (struct rb_node *)0xffff88a0be3d00a0,
},
},
},
.ksym_lnode = (struct list_head){
.next = (struct list_head *)0xffff88bf481830b8,
.prev = (struct list_head *)0xffff888309f536b8,
},
.ops = (const struct bpf_prog_ops *)xdp_prog_ops+0x0 = 0xffffffff820fa350,
.used_maps = (struct bpf_map **)0xffff889ff795de98,
.prog = (struct bpf_prog *)0xffffc9000cf2d000,
.user = (struct user_struct *)root_user+0x0 = 0xffffffff82444820,
.load_time = (u64)2408348759285319,
.cgroup_storage = (struct bpf_map *[2]){},
.name = (char [16])"tw_xdp_policer",
.security = (void *)0xffff889ff795d548,
.offload = (struct bpf_prog_offload *)0x0,
.btf = (struct btf *)0xffff8890ce6d0580,
.func_info = (struct bpf_func_info *)0xffff889ff795d240,
.func_info_aux = (struct bpf_func_info_aux *)0xffff889ff795de20,
.linfo = (struct bpf_line_info *)0xffff888a707afc00,
.jited_linfo = (void **)0xffff8893fad48600,
.func_info_cnt = (u32)1,
.nr_linfo = (u32)37,
.linfo_idx = (u32)0,
.num_exentries = (u32)0,
.extable = (struct exception_table_entry *)0xffffffffa032d950,
.stats = (struct bpf_prog_stats *)0x603fe3a1f6d0,
.work = (struct work_struct){
.data = (atomic_long_t){
.counter = (long)0,
},
.entry = (struct list_head){
.next = (struct list_head *)0x0,
.prev = (struct list_head *)0x0,
},
.func = (work_func_t)0x0,
},
.rcu = (struct callback_head){
.next = (struct callback_head *)0x0,
.func = (void (*)(struct callback_head *))0x0,
},
}
Frequently asked questions (FAQ)
Peios Kernel / Internals / BPF
Two sets of Questions and Answers (Q&A) are maintained.
BPF filesystem kfuncs
Peios Kernel / Internals / BPF
BPF LSM programs need to access filesystem data from LSM hooks. The following BPF kfuncs can be used to get these data.
bpf_get_file_xattr()
bpf_get_fsverity_digest()
To avoid recursions, these kfuncs follow the following rules:
-
These kfuncs are only permitted from BPF LSM function.
-
These kfuncs should not call into other LSM hooks, i.e. security_*(). For example,
bpf_get_file_xattr()does not usevfs_getxattr(), because the latter calls LSM hooksecurity_inode_getxattr.
BPF Graph Data Structures
Peios Kernel / Internals / BPF
This document describes implementation details of new-style "graph" data structures (linked_list, rbtree), with particular focus on the verifier's implementation of semantics specific to those data structures.
Although no specific verifier code is referred to in this document, the document assumes that the reader has general knowledge of BPF verifier internals, BPF maps, and BPF program writing.
Note that the intent of this document is to describe the current state of these graph data structures. No guarantees of stability for either semantics or APIs are made or implied here.
Introduction #
The BPF map API has historically been the main way to expose data structures of various types for use within BPF programs. Some data structures fit naturally with the map API (HASH, ARRAY), others less so. Consequently, programs interacting with the latter group of data structures can be hard to parse for kernel programmers without previous BPF experience.
Luckily, some restrictions which necessitated the use of BPF map semantics are no longer relevant. With the introduction of kfuncs, kptrs, and the any-context BPF allocator, it is now possible to implement BPF data structures whose API and semantics more closely match those exposed to the rest of the kernel.
Two such data structures - linked_list and rbtree - have many verification details in common. Because both have "root"s ("head" for linked_list) and "node"s, the verifier code and this document refer to common functionality as "graph_api", "graph_root", "graph_node", etc.
Unless otherwise stated, examples and semantics below apply to both graph data structures.
Unstable API #
Data structures implemented using the BPF map API have historically used BPF helper functions - either standard map API helpers like bpf_map_update_elem or map-specific helpers. The new-style graph data structures instead use kfuncs to define their manipulation helpers. Because there are no stability guarantees for kfuncs, the API and semantics for these data structures can be evolved in a way that breaks backwards compatibility if necessary.
Root and node types for the new data structures are opaquely defined in the uapi/linux/bpf.h header.
Locking #
The new-style data structures are intrusive and are defined similarly to their vanilla kernel counterparts:
;
struct bpf_spin_lock glock;
struct bpf_rb_root groot ;
The "root" type for both linked_list and rbtree expects to be in a map_value which also contains a bpf_spin_lock - in the above example both global variables are placed in a single-value arraymap. The verifier considers this spin_lock to be associated with the bpf_rb_root by virtue of both being in the same map_value and will enforce that the correct lock is held when verifying BPF programs that manipulate the tree. Since this lock checking happens at verification time, there is no runtime penalty.
Non-owning references #
Motivation
Consider the following BPF code:
struct node_data *n = ; /* ACQUIRED */
;
; /* PASSED */
;
From the verifier's perspective, the pointer n returned from bpf_obj_new has type PTR_TO_BTF_ID | MEM_ALLOC, with a btf_id of struct node_data and a nonzero ref_obj_id. Because it holds n, the program has ownership of the pointee's (object pointed to by n) lifetime. The BPF program must pass off ownership before exiting - either via bpf_obj_drop, which free's the object, or by adding it to tree with bpf_rbtree_add.
(ACQUIRED and PASSED comments in the example denote statements where "ownership is acquired" and "ownership is passed", respectively)
What should the verifier do with n after ownership is passed off? If the object was free'd with bpf_obj_drop the answer is obvious: the verifier should reject programs which attempt to access n after bpf_obj_drop as the object is no longer valid. The underlying memory may have been reused for some other allocation, unmapped, etc.
When ownership is passed to tree via bpf_rbtree_add the answer is less obvious. The verifier could enforce the same semantics as for bpf_obj_drop, but that would result in programs with useful, common coding patterns being rejected, e.g.:
int x;
struct node_data *n = ; /* ACQUIRED */
;
; /* PASSED */
x = n->data;
n->data = 42;
;
Both the read from and write to n->data would be rejected. The verifier can do better, though, by taking advantage of two details:
Graph data structure APIs can only be used when the
bpf_spin_lockassociated with the graph root is heldBoth graph data structures have pointer stability
- Because graph nodes are allocated with
bpf_obj_newand adding / removing from the root involves fiddling with thebpf_{list,rb}_nodefield of the node struct, a graph node will remain at the same address after either operation.
Because the associated bpf_spin_lock must be held by any program adding or removing, if we're in the critical section bounded by that lock, we know that no other program can add or remove until the end of the critical section. This combined with pointer stability means that, until the critical section ends, we can safely access the graph node through n even after it was used to pass ownership.
The verifier considers such a reference a non-owning reference. The ref returned by bpf_obj_new is accordingly considered an owning reference. Both terms currently only have meaning in the context of graph nodes and API.
Details
Let's enumerate the properties of both types of references.
owning reference
This reference controls the lifetime of the pointee
Ownership of pointee must be 'released' by passing it to some graph API kfunc, or via
bpf_obj_drop, whichfree's the pointee
- If not released before program ends, verifier considers program invalid
Access to the pointee's memory will not page fault
non-owning reference
This reference does not own the pointee
- It cannot be used to add the graph node to a graph root, nor
free'd viabpf_obj_dropNo explicit control of lifetime, but can infer valid lifetime based on non-owning ref existence (see explanation below)
Access to the pointee's memory will not page fault
From verifier's perspective non-owning references can only exist between spin_lock and spin_unlock. Why? After spin_unlock another program can do arbitrary operations on the data structure like removing and free-ing via bpf_obj_drop. A non-owning ref to some chunk of memory that was remove'd, free'd, and reused via bpf_obj_new would point to an entirely different thing. Or the memory could go away.
To prevent this logic violation all non-owning references are invalidated by the verifier after a critical section ends. This is necessary to ensure the "will not page fault" property of non-owning references. So if the verifier hasn't invalidated a non-owning ref, accessing it will not page fault.
Currently bpf_obj_drop is not allowed in the critical section, so if there's a valid non-owning ref, we must be in a critical section, and can conclude that the ref's memory hasn't been dropped-and- free'd or dropped-and-reused.
Any reference to a node that is in an rbtree _must_ be non-owning, since the tree has control of the pointee's lifetime. Similarly, any ref to a node that isn't in rbtree _must_ be owning. This results in a nice property: graph API add / remove implementations don't need to check if a node has already been added (or already removed), as the ownership model allows the verifier to prevent such a state from being valid by simply checking types.
However, pointer aliasing poses an issue for the above "nice property". Consider the following example:
struct node_data *n, *m, *o, *p;
n = ; /* 1 */
;
; /* 2 */
m = ; /* 3 */
o = ; /* 4 */
p = ; /* 5 */
;
;
; /* 6 */
Assume the tree is empty before this program runs. If we track verifier state changes here using numbers in above comments:
n is an owning reference
n is a non-owning reference, it's been added to the tree
n and m are non-owning references, they both point to the same node
o is an owning reference, n and m non-owning, all point to same node
o and p are owning, n and m non-owning, all point to the same node
a double-free has occurred, since o and p point to same node and o was
free'd in previous statement
States 4 and 5 violate our "nice property", as there are non-owning refs to a node which is not in an rbtree. Statement 5 will try to remove a node which has already been removed as a result of this violation. State 6 is a dangerous double-free.
At a minimum we should prevent state 6 from being possible. If we can't also prevent state 5 then we must abandon our "nice property" and check whether a node has already been removed at runtime.
We prevent both by generalizing the "invalidate non-owning references" behavior of bpf_spin_unlock and doing similar invalidation after bpf_rbtree_remove. The logic here being that any graph API kfunc which:
takes an arbitrary node argument
removes it from the data structure
returns an owning reference to the removed node
May result in a state where some other non-owning reference points to the same node. So remove-type kfuncs must be considered a non-owning reference invalidation point as well.
Helper functions
Peios Kernel / Internals / BPF
- bpf-helpers(7) maintains a list of helpers available to eBPF programs.
BPF Kernel Functions (kfuncs)
Peios Kernel / Internals / BPF
Peios: Linux capability names in this interface retain their API meaning, but KACS evaluates them through native token privileges or compatibility rules. UID 0,
setcapand Unix mode bits do not independently grant native authority. Object access and PIP checks remain additional constraints where applicable. See Peios kernel behaviour for the mapping and exceptions.
1. Introduction #
BPF Kernel Functions or more commonly known as kfuncs are functions in the Linux kernel which are exposed for use by BPF programs. Unlike normal BPF helpers, kfuncs do not have a stable interface and can change from one kernel release to another. Hence, BPF programs need to be updated in response to changes in the kernel. See 3. kfunc lifecycle expectations for more information.
2. Defining a kfunc #
There are two ways to expose a kernel function to BPF programs, either make an existing function in the kernel visible, or add a new wrapper for BPF. In both cases, care must be taken that BPF program can only call such function in a valid context. To enforce this, visibility of a kfunc can be per program type.
If you are not creating a BPF wrapper for existing kernel function, skip ahead to 2.4 Using an existing kernel function.
2.1 Creating a wrapper kfunc #
When defining a wrapper kfunc, the wrapper function should have extern linkage. This prevents the compiler from optimizing away dead code, as this wrapper kfunc is not invoked anywhere in the kernel itself. It is not necessary to provide a prototype in a header for the wrapper kfunc.
An example is given below:
/* Disables missing prototype warnings */
__bpf_kfunc_start_defs();
__bpf_kfunc struct task_struct *bpf_find_get_task_by_vpid(pid_t nr)
{
return find_get_task_by_vpid(nr);
}
__bpf_kfunc_end_defs();
A wrapper kfunc is often needed when we need to annotate parameters of the kfunc. Otherwise one may directly make the kfunc visible to the BPF program by registering it with the BPF subsystem. See 2.4 Using an existing kernel function.
2.2 kfunc Parameters #
All kfuncs now require trusted arguments by default. This means that all pointer arguments must be valid, and all pointers to BTF objects must be passed in their unmodified form (at a zero offset, and without having been obtained from walking another pointer, with exceptions described below).
There are two types of pointers to kernel objects which are considered "trusted":
-
Pointers which are passed as tracepoint or struct_ops callback arguments.
-
Pointers which were returned from a KF_ACQUIRE kfunc.
Pointers to non-BTF objects (e.g. scalar pointers) may also be passed to kfuncs, and may have a non-zero offset.
The definition of "valid" pointers is subject to change at any time, and has absolutely no ABI stability guarantees.
As mentioned above, a nested pointer obtained from walking a trusted pointer is no longer trusted, with one exception. If a struct type has a field that is guaranteed to be valid (trusted or rcu, as in KF_RCU description below) as long as its parent pointer is valid, the following macros can be used to express that to the verifier:
-
BTF_TYPE_SAFE_TRUSTED -
BTF_TYPE_SAFE_RCU -
BTF_TYPE_SAFE_RCU_OR_NULL
For example,
;
or
;
In other words, you must:
-
Wrap the valid pointer type in a
BTF_TYPE_SAFE_*macro. -
Specify the type and name of the valid nested field. This field must match the field in the original type definition exactly.
A new type declared by a BTF_TYPE_SAFE_* macro also needs to be emitted so that it appears in BTF. For example, BTF_TYPE_SAFE_TRUSTED(struct socket) is emitted in the type_is_trusted() function as follows:
;
2.3 Annotating kfunc parameters #
Similar to BPF helpers, there is sometime need for additional context required by the verifier to make the usage of kernel functions safer and more useful. Hence, we can annotate a parameter by suffixing the name of the argument of the kfunc with a __tag, where tag may be one of the supported annotations.
2.3.1 __sz Annotation #
This annotation is used to indicate a memory and size pair in the argument list. An example is given below:
__bpf_kfunc void bpf_memzero(void *mem, int mem__sz)
{
...
}
Here, the verifier will treat first argument as a PTR_TO_MEM, and second argument as its size. By default, without __sz annotation, the size of the type of the pointer is used. Without __sz annotation, a kfunc cannot accept a void pointer.
2.3.2 __k Annotation #
This annotation is only understood for scalar arguments, where it indicates that the verifier must check the scalar argument to be a known constant, which does not indicate a size parameter, and the value of the constant is relevant to the safety of the program.
An example is given below:
__bpf_kfunc void *bpf_obj_new(u32 local_type_id__k, ...)
{
...
}
Here, bpf_obj_new uses local_type_id argument to find out the size of that type ID in program's BTF and return a sized pointer to it. Each type ID will have a distinct size, hence it is crucial to treat each such call as distinct when values don't match during verifier state pruning checks.
Hence, whenever a constant scalar argument is accepted by a kfunc which is not a size parameter, and the value of the constant matters for program safety, __k suffix should be used.
2.3.3 __uninit Annotation #
This annotation is used to indicate that the argument will be treated as uninitialized.
An example is given below:
__bpf_kfunc int bpf_dynptr_from_skb(..., struct bpf_dynptr_kern *ptr__uninit)
{
...
}
Here, the dynptr will be treated as an uninitialized dynptr. Without this annotation, the verifier will reject the program if the dynptr passed in is not initialized.
2.3.4 __nullable Annotation #
This annotation is used to indicate that the pointer argument may be NULL. The verifier will allow passing NULL for such arguments.
An example is given below:
__bpf_kfunc void bpf_task_release(struct task_struct *task__nullable)
{
...
}
Here, the task pointer may be NULL. The kfunc is responsible for checking if the pointer is NULL before dereferencing it.
The __nullable annotation can be combined with other annotations. For example, when used with __sz or __szk annotations for memory and size pairs, the verifier will skip size validation when a NULL pointer is passed, but will still process the size argument to extract constant size information when needed:
__bpf_kfunc void *bpf_dynptr_slice(..., void *buffer__nullable,
u32 buffer__szk)
Here, the buffer may be NULL. If the buffer is not NULL, it must be at least buffer__szk bytes in size. The kfunc is responsible for checking if the buffer is NULL before using it.
2.3.5 __str Annotation #
This annotation is used to indicate that the argument is a constant string.
An example is given below:
__bpf_kfunc bpf_get_file_xattr(..., const char *name__str, ...)
{
...
}
In this case, bpf_get_file_xattr() can be called as:
bpf_get_file_xattr(..., "xattr_name", ...);
Or:
const char name[] = "xattr_name"; /* This need to be global */
int BPF_PROG(...)
{
...
bpf_get_file_xattr(..., name, ...);
...
}
2.4 Using an existing kernel function #
When an existing function in the kernel is fit for consumption by BPF programs, it can be directly registered with the BPF subsystem. However, care must still be taken to review the context in which it will be invoked by the BPF program and whether it is safe to do so.
2.5 Annotating kfuncs #
In addition to kfuncs' arguments, verifier may need more information about the type of kfunc(s) being registered with the BPF subsystem. To do so, we define flags on a set of kfuncs as follows:
BTF_KFUNCS_START(bpf_task_set)
BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE)
BTF_KFUNCS_END(bpf_task_set)
This set encodes the BTF ID of each kfunc listed above, and encodes the flags along with it. Ofcourse, it is also allowed to specify no flags.
kfunc definitions should also always be annotated with the __bpf_kfunc macro. This prevents issues such as the compiler inlining the kfunc if it's a static kernel function, or the function being elided in an LTO build as it's not used in the rest of the kernel. Developers should not manually add annotations to their kfunc to prevent these issues. If an annotation is required to prevent such an issue with your kfunc, it is a bug and should be added to the definition of the macro so that other kfuncs are similarly protected. An example is given below:
__bpf_kfunc struct task_struct *bpf_get_task_pid(s32 pid)
{
...
}
2.5.1 KF_ACQUIRE flag #
The KF_ACQUIRE flag is used to indicate that the kfunc returns a pointer to a refcounted object. The verifier will then ensure that the pointer to the object is eventually released using a release kfunc, or transferred to a map using a referenced kptr (by invoking bpf_kptr_xchg). If not, the verifier fails the loading of the BPF program until no lingering references remain in all possible explored states of the program.
2.5.2 KF_RET_NULL flag #
The KF_RET_NULL flag is used to indicate that the pointer returned by the kfunc may be NULL. Hence, it forces the user to do a NULL check on the pointer returned from the kfunc before making use of it (dereferencing or passing to another helper). This flag is often used in pairing with KF_ACQUIRE flag, but both are orthogonal to each other.
2.5.3 KF_RELEASE flag #
The KF_RELEASE flag is used to indicate that the kfunc releases the pointer passed in to it. There can be only one referenced pointer that can be passed in. All copies of the pointer being released are invalidated as a result of invoking kfunc with this flag.
2.5.4 KF_SLEEPABLE flag #
The KF_SLEEPABLE flag is used for kfuncs that may sleep. Such kfuncs can only be called by sleepable BPF programs (BPF_F_SLEEPABLE).
2.5.5 KF_DESTRUCTIVE flag #
The KF_DESTRUCTIVE flag is used to indicate functions calling which is destructive to the system. For example such a call can result in system rebooting or panicking. Due to this additional restrictions apply to these calls. At the moment they only require CAP_SYS_BOOT capability, but more can be added later.
2.5.6 KF_RCU flag #
The KF_RCU flag allows kfuncs to opt out of the default trusted args requirement and accept RCU pointers with weaker guarantees. The kfuncs marked with KF_RCU expect either PTR_TRUSTED or MEM_RCU arguments. The verifier guarantees that the objects are valid and there is no use-after-free. The pointers are not NULL, but the object's refcount could have reached zero. The kfuncs need to consider doing refcnt != 0 check, especially when returning a KF_ACQUIRE pointer. Note as well that a KF_ACQUIRE kfunc that is KF_RCU should very likely also be KF_RET_NULL.
2.5.7 KF_RCU_PROTECTED flag #
The KF_RCU_PROTECTED flag is used to indicate that the kfunc must be invoked in an RCU critical section. This is assumed by default in non-sleepable programs, and must be explicitly ensured by calling bpf_rcu_read_lock for sleepable ones.
If the kfunc returns a pointer value, this flag also enforces that the returned pointer is RCU protected, and can only be used while the RCU critical section is active.
The flag is distinct from the KF_RCU flag, which only ensures that its arguments are at least RCU protected pointers. This may transitively imply that RCU protection is ensured, but it does not work in cases of kfuncs which require RCU protection but do not take RCU protected arguments.
2.5.8 KF_DEPRECATED flag #
The KF_DEPRECATED flag is used for kfuncs which are scheduled to be changed or removed in a subsequent kernel release. A kfunc that is marked with KF_DEPRECATED should also have any relevant information captured in its kernel doc. Such information typically includes the kfunc's expected remaining lifespan, a recommendation for new functionality that can replace it if any is available, and possibly a rationale for why it is being removed.
Note that while on some occasions, a KF_DEPRECATED kfunc may continue to be supported and have its KF_DEPRECATED flag removed, it is likely to be far more difficult to remove a KF_DEPRECATED flag after it's been added than it is to prevent it from being added in the first place. As described in 3. kfunc lifecycle expectations, users that rely on specific kfuncs are encouraged to make their use-cases known as early as possible, and participate in upstream discussions regarding whether to keep, change, deprecate, or remove those kfuncs if and when such discussions occur.
2.5.9 KF_IMPLICIT_ARGS flag #
The KF_IMPLICIT_ARGS flag is used to indicate that the BPF signature of the kfunc is different from it's kernel signature, and the values for implicit arguments are provided at load time by the verifier.
Only arguments of specific types are implicit. Currently only struct bpf_prog_aux * type is supported.
A kfunc with KF_IMPLICIT_ARGS flag therefore has two types in BTF: one function matching the kernel declaration (with _impl suffix in the name by convention), and another matching the intended BPF API.
Verifier only allows calls to the non-_impl version of a kfunc, that uses a signature without the implicit arguments.
Example declaration:
__bpf_kfunc int
Example usage in BPF program:
/* note that the last argument is omitted */
;
2.6 Registering the kfuncs #
Once the kfunc is prepared for use, the final step to making it visible is registering it with the BPF subsystem. Registration is done per BPF program type. An example is shown below:
BTF_KFUNCS_START(bpf_task_set)
BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE)
BTF_KFUNCS_END(bpf_task_set)
static const struct btf_kfunc_id_set bpf_task_kfunc_set = {
.owner = THIS_MODULE,
.set = &bpf_task_set,
};
static int init_subsystem(void)
{
return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_task_kfunc_set);
}
late_initcall(init_subsystem);
2.7 Specifying no-cast aliases with ___init #
The verifier will always enforce that the BTF type of a pointer passed to a kfunc by a BPF program, matches the type of pointer specified in the kfunc definition. The verifier, does, however, allow types that are equivalent according to the C standard to be passed to the same kfunc arg, even if their BTF_IDs differ.
For example, for the following type definition:
;
The verifier would allow a struct bpf_cpumask * to be passed to a kfunc taking a cpumask_t * (which is a typedef of struct cpumask *). For instance, both struct cpumask * and struct bpf_cpmuask * can be passed to bpf_cpumask_test_cpu().
In some cases, this type-aliasing behavior is not desired. struct nf_conn___init is one such example:
;
The C standard would consider these types to be equivalent, but it would not always be safe to pass either type to a trusted kfunc. struct nf_conn___init represents an allocated struct nf_conn object that has not yet been initialized, so it would therefore be unsafe to pass a struct nf_conn___init * to a kfunc that's expecting a fully initialized struct nf_conn * (e.g. bpf_ct_change_timeout()).
In order to accommodate such requirements, the verifier will enforce strict PTR_TO_BTF_ID type matching if two types have the exact same name, with one being suffixed with ___init.
3. kfunc lifecycle expectations #
kfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they're defined in when it's deemed necessary.
Like any other change to the kernel, maintainers will not change or remove a kfunc without having a reasonable justification. Whether or not they'll choose to change a kfunc will ultimately depend on a variety of factors, such as how widely used the kfunc is, how long the kfunc has been in the kernel, whether an alternative kfunc exists, what the norm is in terms of stability for the subsystem in question, and of course what the technical cost is of continuing to support the kfunc.
There are several implications of this:
-
a) kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer. In other words, kfuncs that are known to have a lot of users and provide significant value provide stronger incentives for maintainers to invest the time and complexity in supporting them. It is therefore important for developers that are using kfuncs in their BPF programs to communicate and explain how and why those kfuncs are being used, and to participate in discussions regarding those kfuncs when they occur upstream.
-
b) Unlike regular kernel symbols marked with EXPORT_SYMBOL_GPL, BPF programs that call kfuncs are generally not part of the kernel tree. This means that refactoring cannot typically change callers in-place when a kfunc changes, as is done for e.g. an upstreamed driver being updated in place when a kernel symbol is changed.
Unlike with regular kernel symbols, this is expected behavior for BPF symbols, and out-of-tree BPF programs that use kfuncs should be considered relevant to discussions and decisions around modifying and removing those kfuncs. The BPF community will take an active role in participating in upstream discussions when necessary to ensure that the perspectives of such users are taken into account.
-
c) A kfunc will never have any hard stability guarantees. BPF APIs cannot and will not ever hard-block a change in the kernel purely for stability reasons. That being said, kfuncs are features that are meant to solve problems and provide value to users. The decision of whether to change or remove a kfunc is a multivariate technical decision that is made on a case-by-case basis, and which is informed by data points such as those mentioned above. It is expected that a kfunc being removed or changed with no warning will not be a common occurrence or take place without sound justification, but it is a possibility that must be accepted if one is to use kfuncs.
3.1 kfunc deprecation #
As described above, while sometimes a maintainer may find that a kfunc must be changed or removed immediately to accommodate some changes in their subsystem, usually kfuncs will be able to accommodate a longer and more measured deprecation process. For example, if a new kfunc comes along which provides superior functionality to an existing kfunc, the existing kfunc may be deprecated for some period of time to allow users to migrate their BPF programs to use the new one. Or, if a kfunc has no known users, a decision may be made to remove the kfunc (without providing an alternative API) after some deprecation period so as to provide users with a window to notify the kfunc maintainer if it turns out that the kfunc is actually being used.
It's expected that the common case will be that kfuncs will go through a deprecation period rather than being changed or removed without warning. As described in 2.5.8 KF_DEPRECATED flag, the kfunc framework provides the KF_DEPRECATED flag to kfunc developers to signal to users that a kfunc has been deprecated. Once a kfunc has been marked with KF_DEPRECATED, the following procedure is followed for removal:
-
Any relevant information for deprecated kfuncs is documented in the kfunc's kernel docs. This documentation will typically include the kfunc's expected remaining lifespan, a recommendation for new functionality that can replace the usage of the deprecated function (or an explanation as to why no such replacement exists), etc.
-
The deprecated kfunc is kept in the kernel for some period of time after it was first marked as deprecated. This time period will be chosen on a case-by-case basis, and will typically depend on how widespread the use of the kfunc is, how long it has been in the kernel, and how hard it is to move to alternatives. This deprecation time period is "best effort", and as described above, circumstances may sometimes dictate that the kfunc be removed before the full intended deprecation period has elapsed.
-
After the deprecation period the kfunc will be removed. At this point, BPF programs calling the kfunc will be rejected by the verifier.
4. Core kfuncs #
The BPF subsystem provides a number of "core" kfuncs that are potentially applicable to a wide variety of different possible use cases and programs. Those kfuncs are documented here.
4.1 struct task_struct * kfuncs #
There are a number of kfuncs that allow struct task_struct * objects to be used as kptrs:
bpf_task_acquire() #
__bpf_kfunc struct task_struct * Acquire a reference to a task. A task acquired by this kfunc which is not stored in a map as a kptr, must be released by calling bpf_task_release().
Parameters
struct task_struct *p: The task on which a reference is being acquired.
bpf_task_release() #
__bpf_kfunc void Release the reference acquired on a task.
Parameters
struct task_struct *p: The task on which a reference is being released.
These kfuncs are useful when you want to acquire or release a reference to a struct task_struct * that was passed as e.g. a tracepoint arg, or a struct_ops callback arg. For example:
/**
* A trivial example tracepoint program that shows how to
* acquire and release a struct task_struct * pointer.
*/
int
References acquired on struct task_struct * objects are RCU protected. Therefore, when in an RCU read region, you can obtain a pointer to a task embedded in a map value without having to acquire a reference:
static struct task_struct *global;
/**
* A trivial example showing how to access a task stored
* in a map using RCU.
*/
int
A BPF program can also look up a task from a pid. This can be useful if the caller doesn't have a trusted pointer to a struct task_struct * object that it can acquire a reference on with bpf_task_acquire().
bpf_task_from_pid() #
__bpf_kfunc struct task_struct * Find a struct task_struct from its pid by looking it up in the root pid namespace idr. If a task is returned, it must either be stored in a map, or released with bpf_task_release().
Parameters
s32 pid: The pid of the task being looked up.
Here is an example of it being used:
int
4.2 struct cgroup * kfuncs #
struct cgroup * objects also have acquire and release functions:
bpf_cgroup_acquire() #
__bpf_kfunc struct cgroup * Acquire a reference to a cgroup. A cgroup acquired by this kfunc which is not stored in a map as a kptr, must be released by calling bpf_cgroup_release().
Parameters
struct cgroup *cgrp: The cgroup on which a reference is being acquired.
bpf_cgroup_release() #
__bpf_kfunc void Release the reference acquired on a cgroup. If this kfunc is invoked in an RCU read region, the cgroup is guaranteed to not be freed until the current grace period has ended, even if its refcount drops to 0.
Parameters
struct cgroup *cgrp: The cgroup on which a reference is being released.
These kfuncs are used in exactly the same manner as bpf_task_acquire() and bpf_task_release() respectively, so we won't provide examples for them.
Other kfuncs available for interacting with struct cgroup * objects are bpf_cgroup_ancestor() and bpf_cgroup_from_id(), allowing callers to access the ancestor of a cgroup and find a cgroup by its ID, respectively. Both return a cgroup kptr.
bpf_cgroup_ancestor() #
__bpf_kfunc struct cgroup * Perform a lookup on an entry in a cgroup's ancestor array. A cgroup returned by this kfunc which is not subsequently stored in a map, must be released by calling bpf_cgroup_release().
Parameters
struct cgroup *cgrp: The cgroup for which we're performing a lookup.int level: The level of ancestor to look up.
bpf_cgroup_from_id() #
__bpf_kfunc struct cgroup * Find a cgroup from its ID. A cgroup returned by this kfunc which is not subsequently stored in a map, must be released by calling bpf_cgroup_release().
Parameters
u64 cgid: cgroup id.
Eventually, BPF should be updated to allow this to happen with a normal memory load in the program itself. This is currently not possible without more work in the verifier. bpf_cgroup_ancestor() can be used as follows:
/**
* Simple tracepoint example that illustrates how a cgroup's
* ancestor can be accessed using bpf_cgroup_ancestor().
*/
int
4.3 struct cpumask * kfuncs #
BPF provides a set of kfuncs that can be used to query, allocate, mutate, and destroy struct cpumask * objects. Please refer to BPF cpumask kfuncs for more details.
Contents
Peios Kernel / Internals / BPF
1 Linux implementation notes #
This document provides more details specific to the Linux kernel implementation of the eBPF instruction set.
1.1 Byte swap instructions #
BPF_FROM_LE and BPF_FROM_BE exist as aliases for BPF_TO_LE and BPF_TO_BE respectively.
1.2 Jump instructions #
BPF_CALL | BPF_X | BPF_JMP (0x8d), where the helper function integer would be read from a specified register, is not currently supported by the verifier. Any programs with this instruction will fail to load until such support is added.
1.3 Maps #
Linux only supports the 'map_val(map)' operation on array maps with a single element.
Linux uses an fd_array to store maps associated with a BPF program. Thus, map_by_idx(imm) uses the fd at that index in the array.
1.4 Variables #
The following 64-bit immediate instruction specifies that a variable address, which corresponds to some integer stored in the 'imm' field, should be loaded:
|
opcode construction |
opcode |
src |
pseudocode |
imm type |
dst type |
|---|---|---|---|---|---|
|
BPF_IMM | BPF_DW | BPF_LD |
0x18 |
0x3 |
dst = var_addr(imm) |
variable id |
data pointer |
On Linux, this integer is a BTF ID.
1.5 Legacy BPF Packet access instructions #
As mentioned in the ISA standard documentation, Linux has special eBPF instructions for access to packet data that have been carried over from classic BPF to retain the performance of legacy socket filters running in the eBPF interpreter.
The instructions come in two forms: BPF_ABS | <size> | BPF_LD and BPF_IND | <size> | BPF_LD.
These instructions are used to access packet data and can only be used when the program context is a pointer to a networking packet. BPF_ABS accesses packet data at an absolute offset specified by the immediate data and BPF_IND access packet data at an offset that includes the value of a register in addition to the immediate data.
These instructions have seven implicit operands:
-
Register R6 is an implicit input that must contain a pointer to a struct sk_buff.
-
Register R0 is an implicit output which contains the data fetched from the packet.
-
Registers R1-R5 are scratch registers that are clobbered by the instruction.
These instructions have an implicit program exit condition as well. If an eBPF program attempts access data beyond the packet boundary, the program execution will be aborted.
BPF_ABS | BPF_W | BPF_LD (0x20) means:
R0 = ntohl(*(u32 *) ((struct sk_buff *) R6->data + imm))
where ntohl() converts a 32-bit value from network byte order to host byte order.
BPF_IND | BPF_W | BPF_LD (0x40) means:
R0 = ntohl(*(u32 *) ((struct sk_buff *) R6->data + src + imm))
BPF LLVM Relocations
Peios Kernel / Internals / BPF
BPF LLVM Relocations #
This document describes LLVM BPF backend relocation types.
Relocation Record #
LLVM BPF backend records each relocation with the following 16-byte ELF structure:
typedef struct
{
Elf64_Addr r_offset; // Offset from the beginning of section.
Elf64_Xword r_info; // Relocation type and symbol index.
} Elf64_Rel;
For example, for the following code:
int g1 __attribute__((section("sec")));
int g2 __attribute__((section("sec")));
static volatile int l1 __attribute__((section("sec")));
static volatile int l2 __attribute__((section("sec")));
int test() {
return g1 + g2 + l1 + l2;
}
Compiled with clang --target=bpf -O2 -c test.c, the following is the code with llvm-objdump -dr test.o:
0: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 = 0 ll
0000000000000000: R_BPF_64_64 g1
2: 61 11 00 00 00 00 00 00 r1 = *(u32 *)(r1 + 0)
3: 18 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r2 = 0 ll
0000000000000018: R_BPF_64_64 g2
5: 61 20 00 00 00 00 00 00 r0 = *(u32 *)(r2 + 0)
6: 0f 10 00 00 00 00 00 00 r0 += r1
7: 18 01 00 00 08 00 00 00 00 00 00 00 00 00 00 00 r1 = 8 ll
0000000000000038: R_BPF_64_64 sec
9: 61 11 00 00 00 00 00 00 r1 = *(u32 *)(r1 + 0)
10: 0f 10 00 00 00 00 00 00 r0 += r1
11: 18 01 00 00 0c 00 00 00 00 00 00 00 00 00 00 00 r1 = 12 ll
0000000000000058: R_BPF_64_64 sec
13: 61 11 00 00 00 00 00 00 r1 = *(u32 *)(r1 + 0)
14: 0f 10 00 00 00 00 00 00 r0 += r1
15: 95 00 00 00 00 00 00 00 exit
There are four relocations in the above for four LD_imm64 instructions. The following llvm-readelf -r test.o shows the binary values of the four relocations:
Relocation section '.rel.text' at offset 0x190 contains 4 entries:
Offset Info Type Symbol's Value Symbol's Name
0000000000000000 0000000600000001 R_BPF_64_64 0000000000000000 g1
0000000000000018 0000000700000001 R_BPF_64_64 0000000000000004 g2
0000000000000038 0000000400000001 R_BPF_64_64 0000000000000000 sec
0000000000000058 0000000400000001 R_BPF_64_64 0000000000000000 sec
Each relocation is represented by Offset (8 bytes) and Info (8 bytes). For example, the first relocation corresponds to the first instruction (Offset 0x0) and the corresponding Info indicates the relocation type of R_BPF_64_64 (type 1) and the entry in the symbol table (entry 6). The following is the symbol table with llvm-readelf -s test.o:
Symbol table '.symtab' contains 8 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.c
2: 0000000000000008 4 OBJECT LOCAL DEFAULT 4 l1
3: 000000000000000c 4 OBJECT LOCAL DEFAULT 4 l2
4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 sec
5: 0000000000000000 128 FUNC GLOBAL DEFAULT 2 test
6: 0000000000000000 4 OBJECT GLOBAL DEFAULT 4 g1
7: 0000000000000004 4 OBJECT GLOBAL DEFAULT 4 g2
The 6th entry is global variable g1 with value 0.
Similarly, the second relocation is at .text offset 0x18, instruction 3, has a type of R_BPF_64_64 and refers to entry 7 in the symbol table. The second relocation resolves to global variable g2 which has a symbol value 4. The symbol value represents the offset from the start of .data section where the initial value of the global variable g2 is stored.
The third and fourth relocations refer to static variables l1 and l2. From the .rel.text section above, it is not clear to which symbols they really refer as they both refer to symbol table entry 4, symbol sec, which has STT_SECTION type and represents a section. So for a static variable or function, the section offset is written to the original insn buffer, which is called A (addend). Looking at above insn 7 and 11, they have section offset 8 and 12. From symbol table, we can find that they correspond to entries 2 and 3 for l1 and l2.
In general, the A is 0 for global variables and functions, and is the section offset or some computation result based on section offset for static variables/functions. The non-section-offset case refers to function calls. See below for more details.
Different Relocation Types #
Six relocation types are supported. The following is an overview and S represents the value of the symbol in the symbol table:
Enum ELF Reloc Type Description BitSize Offset Calculation
0 R_BPF_NONE None
1 R_BPF_64_64 ld_imm64 insn 32 r_offset + 4 S + A
2 R_BPF_64_ABS64 normal data 64 r_offset S + A
3 R_BPF_64_ABS32 normal data 32 r_offset S + A
4 R_BPF_64_NODYLD32 .BTF[.ext] data 32 r_offset S + A
10 R_BPF_64_32 call insn 32 r_offset + 4 (S + A) / 8 - 1
For example, R_BPF_64_64 relocation type is used for ld_imm64 instruction. The actual to-be-relocated data (0 or section offset) is stored at r_offset + 4 and the read/write data bitsize is 32 (4 bytes). The relocation can be resolved with the symbol value plus implicit addend. Note that the BitSize is 32 which means the section offset must be less than or equal to UINT32_MAX and this is enforced by LLVM BPF backend.
In another case, R_BPF_64_ABS64 relocation type is used for normal 64-bit data. The actual to-be-relocated data is stored at r_offset and the read/write data bitsize is 64 (8 bytes). The relocation can be resolved with the symbol value plus implicit addend.
Both R_BPF_64_ABS32 and R_BPF_64_NODYLD32 types are for 32-bit data. But R_BPF_64_NODYLD32 specifically refers to relocations in .BTF and .BTF.ext sections. For cases like bcc where llvm ExecutionEngine RuntimeDyld is involved, R_BPF_64_NODYLD32 types of relocations should not be resolved to actual function/variable address. Otherwise, .BTF and .BTF.ext become unusable by bcc and kernel.
Type R_BPF_64_32 is used for call instruction. The call target section offset is stored at r_offset + 4 (32bit) and calculated as (S + A) / 8 - 1.
Examples #
Types R_BPF_64_64 and R_BPF_64_32 are used to resolve ld_imm64 and call instructions. For example:
__attribute__((noinline)) __attribute__((section("sec1")))
int gfunc(int a, int b) {
return a * b;
}
static __attribute__((noinline)) __attribute__((section("sec1")))
int lfunc(int a, int b) {
return a + b;
}
int global __attribute__((section("sec2")));
int test(int a, int b) {
return gfunc(a, b) + lfunc(a, b) + global;
}
Compiled with clang --target=bpf -O2 -c test.c, we will have following code with llvm-objdump -dr test.o`:
Disassembly of section .text:
0000000000000000 <test>:
0: bf 26 00 00 00 00 00 00 r6 = r2
1: bf 17 00 00 00 00 00 00 r7 = r1
2: 85 10 00 00 ff ff ff ff call -1
0000000000000010: R_BPF_64_32 gfunc
3: bf 08 00 00 00 00 00 00 r8 = r0
4: bf 71 00 00 00 00 00 00 r1 = r7
5: bf 62 00 00 00 00 00 00 r2 = r6
6: 85 10 00 00 02 00 00 00 call 2
0000000000000030: R_BPF_64_32 sec1
7: 0f 80 00 00 00 00 00 00 r0 += r8
8: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 = 0 ll
0000000000000040: R_BPF_64_64 global
10: 61 11 00 00 00 00 00 00 r1 = *(u32 *)(r1 + 0)
11: 0f 10 00 00 00 00 00 00 r0 += r1
12: 95 00 00 00 00 00 00 00 exit
Disassembly of section sec1:
0000000000000000 <gfunc>:
0: bf 20 00 00 00 00 00 00 r0 = r2
1: 2f 10 00 00 00 00 00 00 r0 *= r1
2: 95 00 00 00 00 00 00 00 exit
0000000000000018 <lfunc>:
3: bf 20 00 00 00 00 00 00 r0 = r2
4: 0f 10 00 00 00 00 00 00 r0 += r1
5: 95 00 00 00 00 00 00 00 exit
The first relocation corresponds to gfunc(a, b) where gfunc has a value of 0, so the call instruction offset is (0 + 0)/8 - 1 = -1. The second relocation corresponds to lfunc(a, b) where lfunc has a section offset 0x18, so the call instruction offset is (0 + 0x18)/8 - 1 = 2. The third relocation corresponds to ld_imm64 of global, which has a section offset 0.
The following is an example to show how R_BPF_64_ABS64 could be generated:
int global() { return 0; }
struct t { void *g; } gbl = { global };
Compiled with clang --target=bpf -O2 -g -c test.c, we will see a relocation below in .data section with command llvm-readelf -r test.o:
Relocation section '.rel.data' at offset 0x458 contains 1 entries:
Offset Info Type Symbol's Value Symbol's Name
0000000000000000 0000000700000002 R_BPF_64_ABS64 0000000000000000 global
The relocation says the first 8-byte of .data section should be filled with address of global variable.
With llvm-readelf output, we can see that dwarf sections have a bunch of R_BPF_64_ABS32 and R_BPF_64_ABS64 relocations:
Relocation section '.rel.debug_info' at offset 0x468 contains 13 entries:
Offset Info Type Symbol's Value Symbol's Name
0000000000000006 0000000300000003 R_BPF_64_ABS32 0000000000000000 .debug_abbrev
000000000000000c 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
0000000000000012 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
0000000000000016 0000000600000003 R_BPF_64_ABS32 0000000000000000 .debug_line
000000000000001a 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
000000000000001e 0000000200000002 R_BPF_64_ABS64 0000000000000000 .text
000000000000002b 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
0000000000000037 0000000800000002 R_BPF_64_ABS64 0000000000000000 gbl
0000000000000040 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
......
The .BTF/.BTF.ext sections has R_BPF_64_NODYLD32 relocations:
Relocation section '.rel.BTF' at offset 0x538 contains 1 entries:
Offset Info Type Symbol's Value Symbol's Name
0000000000000084 0000000800000004 R_BPF_64_NODYLD32 0000000000000000 gbl
Relocation section '.rel.BTF.ext' at offset 0x548 contains 2 entries:
Offset Info Type Symbol's Value Symbol's Name
000000000000002c 0000000200000004 R_BPF_64_NODYLD32 0000000000000000 .text
0000000000000040 0000000200000004 R_BPF_64_NODYLD32 0000000000000000 .text
CO-RE Relocations #
From object file point of view CO-RE mechanism is implemented as a set of CO-RE specific relocation records. These relocation records are not related to ELF relocations and are encoded in .BTF.ext section. See Documentation/bpf/btf.rst for more information on .BTF.ext structure.
CO-RE relocations are applied to BPF instructions to update immediate or offset fields of the instruction at load time with information relevant for target kernel.
Field to patch is selected basing on the instruction class:
-
For BPF_ALU, BPF_ALU64, BPF_LD
immediatefield is patched; -
For BPF_LDX, BPF_STX, BPF_ST
offsetfield is patched; -
BPF_JMP, BPF_JMP32 instructions should not be patched.
Relocation kinds #
There are several kinds of CO-RE relocations that could be split in three groups:
-
Field-based - patch instruction with field related information, e.g. change offset field of the BPF_LDX instruction to reflect offset of a specific structure field in the target kernel.
-
Type-based - patch instruction with type related information, e.g. change immediate field of the BPF_ALU move instruction to 0 or 1 to reflect if specific type is present in the target kernel.
-
Enum-based - patch instruction with enum related information, e.g. change immediate field of the BPF_LD_IMM64 instruction to reflect value of a specific enum literal in the target kernel.
The complete list of relocation kinds is represented by the following enum:
;
Notes:
-
BPF_CORE_FIELD_LSHIFT_U64andBPF_CORE_FIELD_RSHIFT_U64are supposed to be used to read bitfield values using the following algorithm:// To read bitfield ``f`` from ``struct s`` is_signed = off = sz = l = r = // define ``v`` as signed or unsigned integer of size ``sz`` v = * v <<= l v >>= r -
The
BPF_CORE_TYPE_MATCHESqueries matching relation, defined as follows:-
for integers: types match if size and signedness match;
-
for arrays & pointers: target types are recursively matched;
-
for structs & unions:
-
local members need to exist in target with the same name;
-
for each member we recursively check match unless it is already behind a pointer, in which case we only check matching names and compatible kind;
-
-
for enums:
-
local variants have to have a match in target by symbolic name (but not numeric value);
-
size has to match (but enum may match enum64 and vice versa);
-
-
for function pointers:
-
number and position of arguments in local type has to match target;
-
for each argument and the return value we recursively check match.
-
-
CO-RE Relocation Record #
Relocation record is encoded as the following structure:
;
-
insn_off- instruction offset (in bytes) within a code section associated with this relocation; -
type_id- BTF type ID of the "root" (containing) entity of a relocatable type or field; -
access_str_off- offset into corresponding .BTF string section. String interpretation depends on specific relocation kind:-
for field-based relocations, string encodes an accessed field using a sequence of field and array indices, separated by colon (:). It's conceptually very close to LLVM's getelementptr instruction's arguments for identifying offset to a field. For example, consider the following C code:
; struct sample *s;-
Access to
s[0].awould be encoded as0:0:-
0: first element ofs(as ifsis an array); -
0: index of fieldainstruct sample.
-
-
Access to
s->awould be encoded as0:0as well. -
Access to
s->bwould be encoded as0:1:-
0: first element ofs; -
1: index of fieldbinstruct sample.
-
-
Access to
s[1].c[5]would be encoded as1:2:0:5:-
1: second element ofs; -
2: index of anonymous structure field instruct sample; -
0: index of fieldcin anonymous structure; -
5: access to array element #5.
-
-
-
for type-based relocations, string is expected to be just "0";
-
-
for enum value-based relocations, string contains an index of enum
value within its enum type;
-
-
-
kind- one ofenum bpf_core_relo_kind.
CO-RE Relocation Examples #
For the following C code:
;
;
With the following BTF definitions:
...
[2] STRUCT 'foo' size=8 vlen=2
'a' type_id=3 bits_offset=0
'b' type_id=3 bits_offset=32
'c' type_id=4 bits_offset=64 bitfield_size=15
[3] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[4] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
...
[16] ENUM 'bar' encoding=UNSIGNED size=4 vlen=2
'U' val=0
'V' val=1
Field offset relocations are generated automatically when __attribute__((preserve_access_index)) is used, for example:
void
00 <alpha>:
0: r3 = *
00: CO-RE <byte_off> struct foo::
1: * = r3
2: * = 0x1
10: CO-RE <byte_off> struct foo::
3: exit
All relocation kinds could be requested via built-in functions. E.g. field-based relocations:
void
20 <bravo>:
4: r1 = 0x4
20: CO-RE <byte_off> struct foo::
5: * = r1
6: r1 = 0x4
30: CO-RE <byte_sz> struct foo::
7: * = r1
8: r1 = 0x1
40: CO-RE <field_exists> struct foo::
9: * = r1
10: r1 = 0x1
50: CO-RE <signed> struct foo::
11: * = r1
12: r1 = 0x31
60: CO-RE <lshift_u64> struct foo::
13: * = r1
14: r1 = 0x31
70: CO-RE <rshift_u64> struct foo::
15: * = r1
16: exit
Type-based relocations:
void
88 <charlie>:
17: r1 = 0x1
88: CO-RE <type_exists> struct foo
18: * = r1
19: r1 = 0xc
98: CO-RE <type_size> struct foo
20: * = r1
21: r1 = 0x1
a8: CO-RE <type_matches> struct foo
22: * = r1
23: r1 = 0x2 ll
b8: CO-RE <local_type_id> struct foo
25: * = r1
26: r1 = 0x2 ll
d0: CO-RE <target_type_id> struct foo
28: * = r1
29: exit
Enum-based relocations:
void
f0 <delta>:
30: r1 = 0x1 ll
f0: CO-RE <enumval_exists> enum bar::U = 0
32: * = r1
33: r1 = 0x1 ll
108: CO-RE <enumval_value> enum bar::V = 1
35: * = r1
36: exit
BPF_MAP_TYPE_ARRAY and BPF_MAP_TYPE_PERCPU_ARRAY
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_ARRAY and BPF_MAP_TYPE_PERCPU_ARRAY provide generic array storage. The key type is an unsigned 32-bit integer (4 bytes) and the map is of constant size. The size of the array is defined in max_entries at creation time. All array elements are pre-allocated and zero initialized when created. BPF_MAP_TYPE_PERCPU_ARRAY uses a different memory region for each CPU whereas BPF_MAP_TYPE_ARRAY uses the same memory region. The value stored can be of any size for BPF_MAP_TYPE_ARRAY and not more than PCPU_MIN_UNIT_SIZE (32 kB) for BPF_MAP_TYPE_PERCPU_ARRAY. All array elements are aligned to 8 bytes.
Since kernel 5.5, memory mapping may be enabled for BPF_MAP_TYPE_ARRAY by setting the flag BPF_F_MMAPABLE. The map definition is page-aligned and starts on the first page. Sufficient page-sized and page-aligned blocks of memory are allocated to store all array values, starting on the second page, which in some cases will result in over-allocation of memory. The benefit of using this is increased performance and ease of use since userspace programs would not be required to use helper functions to access and mutate data.
Usage #
Kernel BPF #
bpf_map_lookup_elem() #
void *Array elements can be retrieved using the bpf_map_lookup_elem() helper. This helper returns a pointer into the array element, so to avoid data races with userspace reading the value, the user must use primitives like __sync_fetch_and_add() when updating the value in-place.
bpf_map_update_elem() #
long Array elements can be updated using the bpf_map_update_elem() helper.
bpf_map_update_elem() returns 0 on success, or negative error in case of failure.
Since the array is of constant size, bpf_map_delete_elem() is not supported. To clear an array element, you may use bpf_map_update_elem() to insert a zero value to that index.
Per CPU Array #
Values stored in BPF_MAP_TYPE_ARRAY can be accessed by multiple programs across different CPUs. To restrict storage to a single CPU, you may use a BPF_MAP_TYPE_PERCPU_ARRAY.
When using a BPF_MAP_TYPE_PERCPU_ARRAY the bpf_map_update_elem() and bpf_map_lookup_elem() helpers automatically access the slot for the current CPU.
bpf_map_lookup_percpu_elem() #
void *The bpf_map_lookup_percpu_elem() helper can be used to lookup the array value for a specific CPU. Returns value on success , or NULL if no entry was found or cpu is invalid.
Concurrency #
Since kernel version 5.1, the BPF infrastructure provides struct bpf_spin_lock to synchronize access.
Userspace #
Access from userspace uses libbpf APIs with the same names as above, with the map identified by its fd.
Examples #
Please see the tools/testing/selftests/bpf directory for functional examples. The code samples below demonstrate API usage.
Kernel BPF #
This snippet shows how to declare an array in a BPF program.
my_map ;
This example BPF program shows how to access an array element.
int
Userspace #
BPF_MAP_TYPE_ARRAY #
This snippet shows how to create an array, using bpf_map_create_opts to set flags.
int
This snippet shows how to initialize the elements of an array.
int
This snippet shows how to retrieve an element value from an array.
int
BPF_MAP_TYPE_PERCPU_ARRAY #
This snippet shows how to initialize the elements of a per CPU array.
int
This snippet shows how to access the per CPU elements of an array value.
int
Semantics #
As shown in the example above, when accessing a BPF_MAP_TYPE_PERCPU_ARRAY in userspace, each value is an array with ncpus elements.
When calling bpf_map_update_elem() the flag BPF_NOEXIST can not be used for these maps.
BPF_MAP_TYPE_BLOOM_FILTER
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_BLOOM_FILTER provides a BPF bloom filter map. Bloom filters are a space-efficient probabilistic data structure used to quickly test whether an element exists in a set. In a bloom filter, false positives are possible whereas false negatives are not.
The bloom filter map does not have keys, only values. When the bloom filter map is created, it must be created with a key_size of 0. The bloom filter map supports two operations:
-
push: adding an element to the map
-
peek: determining whether an element is present in the map
BPF programs must use bpf_map_push_elem to add an element to the bloom filter map and bpf_map_peek_elem to query the map. These operations are exposed to userspace applications using the existing bpf syscall in the following way:
-
BPF_MAP_UPDATE_ELEM-> push -
BPF_MAP_LOOKUP_ELEM-> peek
The max_entries size that is specified at map creation time is used to approximate a reasonable bitmap size for the bloom filter, and is not otherwise strictly enforced. If the user wishes to insert more entries into the bloom filter than max_entries, this may lead to a higher false positive rate.
The number of hashes to use for the bloom filter is configurable using the lower 4 bits of map_extra in union bpf_attr at map creation time. If no number is specified, the default used will be 5 hash functions. In general, using more hashes decreases both the false positive rate and the speed of a lookup.
It is not possible to delete elements from a bloom filter map. A bloom filter map may be used as an inner map. The user is responsible for synchronising concurrent updates and lookups to ensure no false negative lookups occur.
Usage #
Kernel BPF #
bpf_map_push_elem() #
long A value can be added to a bloom filter using the bpf_map_push_elem() helper. The flags parameter must be set to BPF_ANY when adding an entry to the bloom filter. This helper returns 0 on success, or negative error in case of failure.
bpf_map_peek_elem() #
long The bpf_map_peek_elem() helper is used to determine whether value is present in the bloom filter map. This helper returns 0 if value is probably present in the map, or -ENOENT if value is definitely not present in the map.
Userspace #
bpf_map_update_elem() #
int A userspace program can add a value to a bloom filter using libbpf's bpf_map_update_elem function. The key parameter must be set to NULL and flags must be set to BPF_ANY. Returns 0 on success, or negative error in case of failure.
bpf_map_lookup_elem() #
int A userspace program can determine the presence of value in a bloom filter using libbpf's bpf_map_lookup_elem function. The key parameter must be set to NULL. Returns 0 if value is probably present in the map, or -ENOENT if value is definitely not present in the map.
Examples #
Kernel BPF #
This snippet shows how to declare a bloom filter in a BPF program:
bloom_filter ;
This snippet shows how to determine presence of a value in a bloom filter in a BPF program:
void *
Userspace #
This snippet shows how to use libbpf to create a bloom filter map from userspace:
int
This snippet shows how to add an element to a bloom filter from userspace:
int
References #
https://lwn.net/ml/bpf/20210831225005.2762202-1-joannekoong@fb.com/
BPF_MAP_TYPE_CGROUP_STORAGE
Peios Kernel / Internals / BPF
The BPF_MAP_TYPE_CGROUP_STORAGE map type represents a local fix-sized storage. It is only available with CONFIG_CGROUP_BPF, and to programs that attach to cgroups; the programs are made available by the same Kconfig. The storage is identified by the cgroup the program is attached to.
The map provide a local storage at the cgroup that the BPF program is attached to. It provides a faster and simpler access than the general purpose hash table, which performs a hash table lookups, and requires user to track live cgroups on their own.
This document describes the usage and semantics of the BPF_MAP_TYPE_CGROUP_STORAGE map type. Some of its behaviors was changed in Linux 5.9 and this document will describe the differences.
Usage #
The map uses key of type of either __u64 cgroup_inode_id or struct bpf_cgroup_storage_key, declared in linux/bpf.h:
struct bpf_cgroup_storage_key {
__u64 cgroup_inode_id;
__u32 attach_type;
};
cgroup_inode_id is the inode id of the cgroup directory. attach_type is the program's attach type.
Linux 5.9 added support for type __u64 cgroup_inode_id as the key type. When this key type is used, then all attach types of the particular cgroup and map will share the same storage. Otherwise, if the type is struct bpf_cgroup_storage_key, then programs of different attach types be isolated and see different storages.
To access the storage in a program, use bpf_get_local_storage:
void *bpf_get_local_storage(void *map, u64 flags)
flags is reserved for future use and must be 0.
There is no implicit synchronization. Storages of BPF_MAP_TYPE_CGROUP_STORAGE can be accessed by multiple programs across different CPUs, and user should take care of synchronization by themselves. The bpf infrastructure provides struct bpf_spin_lock to synchronize the storage. See tools/testing/selftests/bpf/progs/test_spin_lock.c.
Examples #
Usage with key type as struct bpf_cgroup_storage_key:
#include <bpf/bpf.h>
struct {
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
__type(key, struct bpf_cgroup_storage_key);
__type(value, __u32);
} cgroup_storage SEC(".maps");
int program(struct __sk_buff *skb)
{
__u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0);
__sync_fetch_and_add(ptr, 1);
return 0;
}
Userspace accessing map declared above:
#include <linux/bpf.h>
#include <linux/libbpf.h>
__u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type)
{
struct bpf_cgroup_storage_key = {
.cgroup_inode_id = cgrp,
.attach_type = type,
};
__u32 value;
bpf_map_lookup_elem(bpf_map__fd(map), &key, &value);
// error checking omitted
return value;
}
Alternatively, using just __u64 cgroup_inode_id as key type:
#include <bpf/bpf.h>
struct {
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
__type(key, __u64);
__type(value, __u32);
} cgroup_storage SEC(".maps");
int program(struct __sk_buff *skb)
{
__u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0);
__sync_fetch_and_add(ptr, 1);
return 0;
}
And userspace:
#include <linux/bpf.h>
#include <linux/libbpf.h>
__u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type)
{
__u32 value;
bpf_map_lookup_elem(bpf_map__fd(map), &cgrp, &value);
// error checking omitted
return value;
}
Semantics #
BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE is a variant of this map type. This per-CPU variant will have different memory regions for each CPU for each storage. The non-per-CPU will have the same memory region for each storage.
Prior to Linux 5.9, the lifetime of a storage is precisely per-attachment, and for a single CGROUP_STORAGE map, there can be at most one program loaded that uses the map. A program may be attached to multiple cgroups or have multiple attach types, and each attach creates a fresh zeroed storage. The storage is freed upon detach.
There is a one-to-one association between the map of each type (per-CPU and non-per-CPU) and the BPF program during load verification time. As a result, each map can only be used by one BPF program and each BPF program can only use one storage map of each type. Because of map can only be used by one BPF program, sharing of this cgroup's storage with other BPF programs were impossible.
Since Linux 5.9, storage can be shared by multiple programs. When a program is attached to a cgroup, the kernel would create a new storage only if the map does not already contain an entry for the cgroup and attach type pair, or else the old storage is reused for the new attachment. If the map is attach type shared, then attach type is simply ignored during comparison. Storage is freed only when either the map or the cgroup attached to is being freed. Detaching will not directly free the storage, but it may cause the reference to the map to reach zero and indirectly freeing all storage in the map.
The map is not associated with any BPF program, thus making sharing possible. However, the BPF program can still only associate with one map of each type (per-CPU and non-per-CPU). A BPF program cannot use more than one BPF_MAP_TYPE_CGROUP_STORAGE or more than one BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE.
In all versions, userspace may use the attach parameters of cgroup and attach type pair in struct bpf_cgroup_storage_key as the key to the BPF map APIs to read or update the storage for a given attachment. For Linux 5.9 attach type shared storages, only the first value in the struct, cgroup inode id, is used during comparison, so userspace may just specify a __u64 directly.
The storage is bound at attach time. Even if the program is attached to parent and triggers in child, the storage still belongs to the parent.
Userspace cannot create a new entry in the map or delete an existing entry. Program test runs always use a temporary storage.
BPF_MAP_TYPE_CGRP_STORAGE
Peios Kernel / Internals / BPF
The BPF_MAP_TYPE_CGRP_STORAGE map type represents a local fix-sized storage for cgroups. It is only available with CONFIG_CGROUPS. The programs are made available by the same Kconfig. The data for a particular cgroup can be retrieved by looking up the map with that cgroup.
This document describes the usage and semantics of the BPF_MAP_TYPE_CGRP_STORAGE map type.
Usage #
The map key must be sizeof(int) representing a cgroup fd. To access the storage in a program, use bpf_cgrp_storage_get:
void *bpf_cgrp_storage_get(struct bpf_map *map, struct cgroup *cgroup, void *value, u64 flags)
flags could be 0 or BPF_LOCAL_STORAGE_GET_F_CREATE which indicates that a new local storage will be created if one does not exist.
The local storage can be removed with bpf_cgrp_storage_delete:
long bpf_cgrp_storage_delete(struct bpf_map *map, struct cgroup *cgroup)
The map is available to all program types.
Examples #
A BPF program example with BPF_MAP_TYPE_CGRP_STORAGE:
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
struct {
__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
__uint(map_flags, BPF_F_NO_PREALLOC);
__type(key, int);
__type(value, long);
} cgrp_storage SEC(".maps");
SEC("tp_btf/sys_enter")
int BPF_PROG(on_enter, struct pt_regs *regs, long id)
{
struct task_struct *task = bpf_get_current_task_btf();
long *ptr;
ptr = bpf_cgrp_storage_get(&cgrp_storage, task->cgroups->dfl_cgrp, 0,
BPF_LOCAL_STORAGE_GET_F_CREATE);
if (ptr)
__sync_fetch_and_add(ptr, 1);
return 0;
}
Userspace accessing map declared above:
#include <linux/bpf.h>
#include <linux/libbpf.h>
__u32 map_lookup(struct bpf_map *map, int cgrp_fd)
{
__u32 *value;
value = bpf_map_lookup_elem(bpf_map__fd(map), &cgrp_fd);
if (value)
return *value;
return 0;
}
Difference Between BPF_MAP_TYPE_CGRP_STORAGE and BPF_MAP_TYPE_CGROUP_STORAGE #
The old cgroup storage map BPF_MAP_TYPE_CGROUP_STORAGE has been marked as deprecated (renamed to BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED). The new BPF_MAP_TYPE_CGRP_STORAGE map should be used instead. The following illusates the main difference between BPF_MAP_TYPE_CGRP_STORAGE and BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED.
-
(1).
BPF_MAP_TYPE_CGRP_STORAGEcan be used by all program types whileBPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATEDis available only to cgroup program types like BPF_CGROUP_INET_INGRESS or BPF_CGROUP_SOCK_OPS, etc. -
(2).
BPF_MAP_TYPE_CGRP_STORAGEsupports local storage for more than onecgroup while
BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATEDonly supports one cgroup which is attached by a BPF program. -
(3).
BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATEDallocates local storage at attach time sobpf_get_local_storage()always returns non-NULL local storage.BPF_MAP_TYPE_CGRP_STORAGEallocates local storage at runtime so it is possible thatbpf_cgrp_storage_get()may return null local storage. To avoid such null local storage issue, user space can dobpf_map_update_elem()to pre-allocate local storage before a BPF program is attached. -
(4).
BPF_MAP_TYPE_CGRP_STORAGEsupports deleting local storage by a BPF programwhile
BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATEDonly deletes storage during prog detach time.
So overall, BPF_MAP_TYPE_CGRP_STORAGE supports all BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED functionality and beyond. It is recommended to use BPF_MAP_TYPE_CGRP_STORAGE instead of BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED.
BPF_MAP_TYPE_CPUMAP
Peios Kernel / Internals / BPF
The 'cpumap' is primarily used as a backend map for XDP BPF helper
call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap'.
Unlike devmap which redirects XDP frames out to another NIC device, this map type redirects raw XDP frames to another CPU. The remote CPU will do SKB-allocation and call the normal network stack.
An example use-case for this map type is software based Receive Side Scaling (RSS).
The CPUMAP represents the CPUs in the system indexed as the map-key, and the map-value is the config setting (per CPUMAP entry). Each CPUMAP entry has a dedicated kernel thread bound to the given CPU to represent the remote CPU execution unit.
Starting from Linux kernel version 5.9 the CPUMAP can run a second XDP program on the remote CPU. This allows an XDP program to split its processing across multiple CPUs. For example, a scenario where the initial CPU (that sees/receives the packets) needs to do minimal packet processing and the remote CPU (to which the packet is directed) can afford to spend more cycles processing the frame. The initial CPU is where the XDP redirect program is executed. The remote CPU receives raw xdp_frame objects.
Usage #
Kernel BPF #
bpf_redirect_map() #
long Redirect the packet to the endpoint referenced by map at index key. For BPF_MAP_TYPE_CPUMAP this map contains references to CPUs.
The lower two bits of flags are used as the return code if the map lookup fails. This is so that the return value can be one of the XDP program return codes up to XDP_TX, as chosen by the caller.
User space #
bpf_map_update_elem() #
int ;
CPU entries can be added or updated using the bpf_map_update_elem() helper. This helper replaces existing elements atomically. The value parameter can be struct bpf_cpumap_val.
;
-
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element.
-
BPF_NOEXIST: Create a new element only if it did not exist.
-
BPF_EXIST: Update an existing element.
-
bpf_map_lookup_elem() #
int ;
CPU entries can be retrieved using the bpf_map_lookup_elem() helper.
bpf_map_delete_elem() #
int ;
CPU entries can be deleted using the bpf_map_delete_elem() helper. This helper will return 0 on success, or negative error in case of failure.
Examples #
Kernel #
The following code snippet shows how to declare a BPF_MAP_TYPE_CPUMAP called cpu_map and how to redirect packets to a remote CPU using a round robin scheme.
cpu_map ;
cpus_available ;
cpus_iterator ;
int
User space #
The following code snippet shows how to dynamically set the max_entries for a CPUMAP to the max number of cpus available on the system.
int
References #
BPF_MAP_TYPE_DEVMAP and BPF_MAP_TYPE_DEVMAP_HASH
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_DEVMAP and BPF_MAP_TYPE_DEVMAP_HASH are BPF maps primarily used as backend maps for the XDP BPF helper call bpf_redirect_map(). BPF_MAP_TYPE_DEVMAP is backed by an array that uses the key as the index to lookup a reference to a net device. While BPF_MAP_TYPE_DEVMAP_HASH is backed by a hash table that uses a key to lookup a reference to a net device. The user provides either <key/ ifindex> or <key/ struct bpf_devmap_val> pairs to update the maps with new net devices.
The setup and packet enqueue/send code is shared between the two types of devmap; only the lookup and insertion is different.
Usage #
Kernel BPF #
bpf_redirect_map() #
long Redirect the packet to the endpoint referenced by map at index key. For BPF_MAP_TYPE_DEVMAP and BPF_MAP_TYPE_DEVMAP_HASH this map contains references to net devices (for forwarding packets through other ports).
The lower two bits of flags are used as the return code if the map lookup fails. This is so that the return value can be one of the XDP program return codes up to XDP_TX, as chosen by the caller. The higher bits of flags can be set to BPF_F_BROADCAST or BPF_F_EXCLUDE_INGRESS as defined below.
With BPF_F_BROADCAST the packet will be broadcast to all the interfaces in the map, with BPF_F_EXCLUDE_INGRESS the ingress interface will be excluded from the broadcast.
This helper will return XDP_REDIRECT on success, or the value of the two lower bits of the flags argument if the map lookup fails.
More information about redirection can be found Redirect
bpf_map_lookup_elem() #
void *Net device entries can be retrieved using the bpf_map_lookup_elem() helper.
User space #
bpf_map_update_elem() #
int ;
Net device entries can be added or updated using the bpf_map_update_elem() helper. This helper replaces existing elements atomically. The value parameter can be struct bpf_devmap_val or a simple int ifindex for backwards compatibility.
;
-
The
flagsargument can be one of the following:-
BPF_ANY: Create a new element or update an existing element. -
BPF_NOEXIST: Create a new element only if it did not exist. -
BPF_EXIST: Update an existing element.
-
DEVMAPs can associate a program with a device entry by adding a bpf_prog.fd to struct bpf_devmap_val. Programs are run after XDP_REDIRECT and have access to both Rx device and Tx device. The program associated with the fd must have type XDP with expected attach type xdp_devmap. When a program is associated with a device index, the program is run on an XDP_REDIRECT and before the buffer is added to the per-cpu queue. Examples of how to attach/use xdp_devmap progs can be found in the kernel selftests:
-
tools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c -
tools/testing/selftests/bpf/progs/test_xdp_with_devmap_helpers.c
bpf_map_lookup_elem() #
int ;
Net device entries can be retrieved using the bpf_map_lookup_elem() helper.
bpf_map_delete_elem() #
int ;
Net device entries can be deleted using the bpf_map_delete_elem() helper. This helper will return 0 on success, or negative error in case of failure.
Examples #
Kernel BPF #
The following code snippet shows how to declare a BPF_MAP_TYPE_DEVMAP called tx_port.
tx_port ;
The following code snippet shows how to declare a BPF_MAP_TYPE_DEVMAP_HASH called forward_map.
forward_map ;
The following code snippet shows a simple xdp_redirect_map program. This program would work with a user space program that populates the devmap forward_map based on ingress ifindexes. The BPF program (below) is redirecting packets using the ingress ifindex as the key.
int
The following code snippet shows a BPF program that is broadcasting packets to all the interfaces in the tx_port devmap.
int
User space #
The following code snippet shows how to update a devmap called tx_port.
int
The following code snippet shows how to update a hash_devmap called forward_map.
int
References #
BPF_MAP_TYPE_HASH, with PERCPU and LRU Variants
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_HASH and BPF_MAP_TYPE_PERCPU_HASH provide general purpose hash map storage. Both the key and the value can be structs, allowing for composite keys and values.
The kernel is responsible for allocating and freeing key/value pairs, up to the max_entries limit that you specify. Hash maps use pre-allocation of hash table elements by default. The BPF_F_NO_PREALLOC flag can be used to disable pre-allocation when it is too memory expensive.
BPF_MAP_TYPE_PERCPU_HASH provides a separate value slot per CPU. The per-cpu values are stored internally in an array.
The BPF_MAP_TYPE_LRU_HASH and BPF_MAP_TYPE_LRU_PERCPU_HASH variants add LRU semantics to their respective hash tables. An LRU hash will automatically evict the least recently used entries when the hash table reaches capacity. An LRU hash maintains an internal LRU list that is used to select elements for eviction. This internal LRU list is shared across CPUs but it is possible to request a per CPU LRU list with the BPF_F_NO_COMMON_LRU flag when calling bpf_map_create. The following table outlines the properties of LRU maps depending on the a map type and the flags used to create the map.
|
Flag |
|
|
|---|---|---|
|
BPF_F_NO_COMMON_LRU |
Per-CPU LRU, global map |
Per-CPU LRU, per-cpu map |
|
!BPF_F_NO_COMMON_LRU |
Global LRU, global map |
Global LRU, per-cpu map |
Usage #
Kernel BPF #
bpf_map_update_elem() #
long Hash entries can be added or updated using the bpf_map_update_elem() helper. This helper replaces existing elements atomically. The flags parameter can be used to control the update behaviour:
-
BPF_ANYwill create a new element or update an existing element -
BPF_NOEXISTwill create a new element only if one did not already exist -
BPF_EXISTwill update an existing element
bpf_map_update_elem() returns 0 on success, or negative error in case of failure.
bpf_map_lookup_elem() #
void *Hash entries can be retrieved using the bpf_map_lookup_elem() helper. This helper returns a pointer to the value associated with key, or NULL if no entry was found.
bpf_map_delete_elem() #
long Hash entries can be deleted using the bpf_map_delete_elem() helper. This helper will return 0 on success, or negative error in case of failure.
Per CPU Hashes #
For BPF_MAP_TYPE_PERCPU_HASH and BPF_MAP_TYPE_LRU_PERCPU_HASH the bpf_map_update_elem() and bpf_map_lookup_elem() helpers automatically access the hash slot for the current CPU.
bpf_map_lookup_percpu_elem() #
void *The bpf_map_lookup_percpu_elem() helper can be used to lookup the value in the hash slot for a specific CPU. Returns value associated with key on cpu , or NULL if no entry was found or cpu is invalid.
Concurrency #
Values stored in BPF_MAP_TYPE_HASH can be accessed concurrently by programs running on different CPUs. Since Kernel version 5.1, the BPF infrastructure provides struct bpf_spin_lock to synchronise access. See tools/testing/selftests/bpf/progs/test_spin_lock.c.
Userspace #
bpf_map_get_next_key() #
int In userspace, it is possible to iterate through the keys of a hash using libbpf's bpf_map_get_next_key() function. The first key can be fetched by calling bpf_map_get_next_key() with cur_key set to NULL. Subsequent calls will fetch the next key that follows the current key. bpf_map_get_next_key() returns 0 on success, -ENOENT if cur_key is the last key in the hash, or negative error in case of failure.
Note that if cur_key gets deleted then bpf_map_get_next_key() will instead return the first key in the hash table which is undesirable. It is recommended to use batched lookup if there is going to be key deletion intermixed with bpf_map_get_next_key().
Examples #
Please see the tools/testing/selftests/bpf directory for functional examples. The code snippets below demonstrates API usage.
This example shows how to declare an LRU Hash with a struct key and a struct value.
;
;
packet_stats ;
This example shows how to create or update hash values using atomic instructions:
static void
Userspace walking the map elements from the map declared above:
static void
Internals #
This section of the document is targeted at Linux developers and describes aspects of the map implementations that are not considered stable ABI. The following details are subject to change in future versions of the kernel.
BPF_MAP_TYPE_LRU_HASH and variants #
Updating elements in LRU maps may trigger eviction behaviour when the capacity of the map is reached. There are various steps that the update algorithm attempts in order to enforce the LRU property which have increasing impacts on other CPUs involved in the following operation attempts:
-
Attempt to use CPU-local state to batch operations
-
Attempt to fetch
target_freefree nodes from global lists -
Attempt to pull any node from a global list and remove it from the hashmap
-
Attempt to pull any node from any CPU's list and remove it from the hashmap
The number of nodes to borrow from the global list in a batch, target_free, depends on the size of the map. Larger batch size reduces lock contention, but may also exhaust the global structure. The value is computed at map init to avoid exhaustion, by limiting aggregate reservation by all CPUs to half the map size. With a minimum of a single element and maximum budget of 128 at a time.
This algorithm is described visually in the following diagram. See the description in commit 3a08c2fd7634 ("bpf: LRU List") for a full explanation of the corresponding operations:
flowchart TB
n5(["begin<br/>bpf_map_update()"])
n6{"Local freelist<br/>node available?"}
n7["Use node owned<br/>by this CPU"]
n8{"Map created with<br/>common LRU?<br/>(!BPF_F_NO_COMMON_LRU)"}
n9["Flush local pending,<br/>Rotate Global list, move<br/>target_free<br/>from global -#62; local"]
n10{"Able to free<br/>target_free<br/>nodes?"}
n11["Shrink inactive list<br/>up to remaining<br/>target_free<br/>(global LRU -#62; local)"]
n12{"#62; 0 entries in<br/>local free list?"}
n13["Steal one node from<br/>inactive, or if empty,<br/>from active global list"]
n14["Try to remove<br/>node from hashtab"]
n15{"Htab removal<br/>successful?"}
n16{"Map created with<br/>common LRU?<br/>(!BPF_F_NO_COMMON_LRU)"}
n22["Use node that was<br/>not recently referenced"]
n23["Use node that was<br/>actively referenced<br/>in global list"]
n24(["return -ENOMEM"])
n25["Use node that was<br/>actively referenced<br/>in (another?) CPU's cache"]
n26["Update hashmap<br/>with new element"]
n27(["return 0"])
n28(["return -EBUSY"])
n29(["return -EEXIST"])
n30(["return -ENOENT"])
subgraph g0["Key<br/>(locks held during operation)"]
direction TB
n0["remote CPU LRU lock"]
n1["hashtab lock"]
n2["LRU lock"]
n3["local CPU LRU lock"]
n4["no locks held"]
end
subgraph g1["Iterate through CPUs<br/>(start from current)"]
direction LR
n17{"Steal a node from<br/>per-cpu freelist?"}
n18["Steal a node from<br/>(1) Unreferenced pending, or<br/>(2) Any pending node"]
n19["Try to remove<br/>node from hashtab"]
n20{"Stole node<br/>from remote<br/>CPU?"}
n21{"Iterated<br/>all CPUs?"}
end
n5 --> n6
n6 -->|"Y"| n7
n6 -->|"N"| n8
n8 -->|"Y"| n9
n8 -->|"N"| n11
n9 --> n10
n10 -->|"Y"| n22
n10 -->|"N"| n11
n11 --> n12
n12 -->|"Y"| n22
n12 -->|"N"| n13
n13 --> n14
n14 --> n15
n15 -->|"Y"| n23
n15 -->|"N"| n16
n16 -->|"Y"| n17
n16 -->|"N"| n24
n17 -->|"Y"| n20
n17 -->|"N"| n18
n18 --> n19
n19 --> n20
n20 -->|"Y"| n25
n20 -->|"N"| n21
n21 -->|"Y"| n24
n21 -->|"N"| n17
n25 --> n26
n7 --> n26
n22 --> n26
n23 --> n26
n26 -->|"Success"| n27
n26 -->|"Hashtab lock failed"| n28
n26 -->|"BPF_EXIST set and<br/>key already exists"| n29
n26 -->|"BPF_NOEXIST set<br/>and no such entry"| n30
style n0 fill:#ffff99,color:#111
style n1 fill:#fdc086,color:#111
style n2 fill:#beaed4,color:#111
style n3 fill:#7fc97f,color:#111
style n6 fill:#7fc97f,color:#111
style n9 fill:#beaed4,color:#111
style n10 fill:#beaed4,color:#111
style n11 fill:#fdc086,color:#111
style n12 fill:#beaed4,color:#111
style n13 fill:#beaed4,color:#111
style n14 fill:#fdc086,color:#111
style n17 fill:#ffff99,color:#111
style n18 fill:#ffff99,color:#111
style n19 fill:#fdc086,color:#111
style n26 fill:#fdc086,color:#111
style g1 stroke-dasharray:6 4
Diagram outlining the LRU eviction steps taken during map update.
LRU hash eviction during map update for BPF_MAP_TYPE_LRU_HASH and
variants. See the dot file source for kernel function name code references.
Map updates start from the oval in the top right "begin bpf_map_update()" and progress through the graph towards the bottom where the result may be either a successful update or a failure with various error codes. The key in the top right provides indicators for which locks may be involved in specific operations. This is intended as a visual hint for reasoning about how map contention may impact update operations, though the map type and flags may impact the actual contention on those locks, based on the logic described in the table above. For instance, if the map is created with type BPF_MAP_TYPE_LRU_PERCPU_HASH and flags BPF_F_NO_COMMON_LRU then all map properties would be per-cpu.
BPF_MAP_TYPE_LPM_TRIE
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_LPM_TRIE provides a longest prefix match algorithm that can be used to match IP addresses to a stored set of prefixes. Internally, data is stored in an unbalanced trie of nodes that uses prefixlen,data pairs as its keys. The data is interpreted in network byte order, i.e. big endian, so data[0] stores the most significant byte.
LPM tries may be created with a maximum prefix length that is a multiple of 8, in the range from 8 to 2048. The key used for lookup and update operations is a struct bpf_lpm_trie_key_u8, extended by max_prefixlen/8 bytes.
-
For IPv4 addresses the data length is 4 bytes
-
For IPv6 addresses the data length is 16 bytes
The value type stored in the LPM trie can be any user defined type.
Usage #
Kernel BPF #
bpf_map_lookup_elem() #
void *The longest prefix entry for a given data value can be found using the bpf_map_lookup_elem() helper. This helper returns a pointer to the value associated with the longest matching key, or NULL if no entry was found.
The key should have prefixlen set to max_prefixlen when performing longest prefix lookups. For example, when searching for the longest prefix match for an IPv4 address, prefixlen should be set to 32.
bpf_map_update_elem() #
long Prefix entries can be added or updated using the bpf_map_update_elem() helper. This helper replaces existing elements atomically.
bpf_map_update_elem() returns 0 on success, or negative error in case of failure.
bpf_map_delete_elem() #
long Prefix entries can be deleted using the bpf_map_delete_elem() helper. This helper will return 0 on success, or negative error in case of failure.
Userspace #
Access from userspace uses libbpf APIs with the same names as above, with the map identified by fd.
bpf_map_get_next_key() #
int A userspace program can iterate through the entries in an LPM trie using libbpf's bpf_map_get_next_key() function. The first key can be fetched by calling bpf_map_get_next_key() with cur_key set to NULL. Subsequent calls will fetch the next key that follows the current key. bpf_map_get_next_key() returns 0 on success, -ENOENT if cur_key is the last key in the trie, or negative error in case of failure.
bpf_map_get_next_key() will iterate through the LPM trie elements from leftmost leaf first. This means that iteration will return more specific keys before less specific ones.
Examples #
Please see tools/testing/selftests/bpf/test_lpm_map.c for examples of LPM trie usage from userspace. The code snippets below demonstrate API usage.
Kernel BPF #
The following BPF code snippet shows how to declare a new LPM trie for IPv4 address prefixes:
;
ipv4_lpm_map ;
The following BPF code snippet shows how to lookup by IPv4 address:
void *
Userspace #
The following snippet shows how to insert an IPv4 prefix entry into an LPM trie:
int
The following snippet shows a userspace program walking through the entries of an LPM trie:
void
BPF_MAP_TYPE_ARRAY_OF_MAPS and BPF_MAP_TYPE_HASH_OF_MAPS
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_ARRAY_OF_MAPS and BPF_MAP_TYPE_HASH_OF_MAPS provide general purpose support for map in map storage. One level of nesting is supported, where an outer map contains instances of a single type of inner map, for example array_of_maps->sock_map.
When creating an outer map, an inner map instance is used to initialize the metadata that the outer map holds about its inner maps. This inner map has a separate lifetime from the outer map and can be deleted after the outer map has been created.
The outer map supports element lookup, update and delete from user space using the syscall API. A BPF program is only allowed to do element lookup in the outer map.
For BPF_MAP_TYPE_ARRAY_OF_MAPS the key is an unsigned 32-bit integer index into the array. The array is a fixed size with max_entries elements that are zero initialized when created.
For BPF_MAP_TYPE_HASH_OF_MAPS the key type can be chosen when defining the map. The kernel is responsible for allocating and freeing key/value pairs, up to the max_entries limit that you specify. Hash maps use pre-allocation of hash table elements by default. The BPF_F_NO_PREALLOC flag can be used to disable pre-allocation when it is too memory expensive.
Usage #
Kernel BPF Helper #
bpf_map_lookup_elem() #
void *Inner maps can be retrieved using the bpf_map_lookup_elem() helper. This helper returns a pointer to the inner map, or NULL if no entry was found.
Examples #
Kernel BPF Example #
This snippet shows how to create and initialise an array of devmaps in a BPF program. Note that the outer array can only be modified from user space using the syscall API.
inner_map1 , inner_map2 ;
outer_map = ;
See progs/test_btf_map_in_map.c in tools/testing/selftests/bpf for more examples of declarative initialisation of outer maps.
User Space #
This snippet shows how to create an array based outer map:
int
This snippet shows how to add an inner map to an outer map:
int
References #
BPF_MAP_TYPE_QUEUE and BPF_MAP_TYPE_STACK
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_QUEUE provides FIFO storage and BPF_MAP_TYPE_STACK provides LIFO storage for BPF programs. These maps support peek, pop and push operations that are exposed to BPF programs through the respective helpers. These operations are exposed to userspace applications using the existing bpf syscall in the following way:
-
BPF_MAP_LOOKUP_ELEM-> peek -
BPF_MAP_LOOKUP_AND_DELETE_ELEM-> pop -
BPF_MAP_UPDATE_ELEM-> push
BPF_MAP_TYPE_QUEUE and BPF_MAP_TYPE_STACK do not support BPF_F_NO_PREALLOC.
Usage #
Kernel BPF #
bpf_map_push_elem() #
long An element value can be added to a queue or stack using the bpf_map_push_elem helper. The flags parameter must be set to BPF_ANY or BPF_EXIST. If flags is set to BPF_EXIST then, when the queue or stack is full, the oldest element will be removed to make room for value to be added. Returns 0 on success, or negative error in case of failure.
bpf_map_peek_elem() #
long This helper fetches an element value from a queue or stack without removing it. Returns 0 on success, or negative error in case of failure.
bpf_map_pop_elem() #
long This helper removes an element into value from a queue or stack. Returns 0 on success, or negative error in case of failure.
Userspace #
bpf_map_update_elem() #
int A userspace program can push value onto a queue or stack using libbpf's bpf_map_update_elem function. The key parameter must be set to NULL and flags must be set to BPF_ANY or BPF_EXIST, with the same semantics as the bpf_map_push_elem kernel helper. Returns 0 on success, or negative error in case of failure.
bpf_map_lookup_elem() #
int A userspace program can peek at the value at the head of a queue or stack using the libbpf bpf_map_lookup_elem function. The key parameter must be set to NULL. Returns 0 on success, or negative error in case of failure.
bpf_map_lookup_and_delete_elem() #
int A userspace program can pop a value from the head of a queue or stack using the libbpf bpf_map_lookup_and_delete_elem function. The key parameter must be set to NULL. Returns 0 on success, or negative error in case of failure.
Examples #
Kernel BPF #
This snippet shows how to declare a queue in a BPF program:
queue ;
Userspace #
This snippet shows how to use libbpf's low-level API to create a queue from userspace:
int
References #
https://lwn.net/ml/netdev/153986858555.9127.14517764371945179514.stgit@kernel/
BPF_MAP_TYPE_SK_STORAGE
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_SK_STORAGE is used to provide socket-local storage for BPF programs. A map of type BPF_MAP_TYPE_SK_STORAGE declares the type of storage to be provided and acts as the handle for accessing the socket-local storage. The values for maps of type BPF_MAP_TYPE_SK_STORAGE are stored locally with each socket instead of with the map. The kernel is responsible for allocating storage for a socket when requested and for freeing the storage when either the map or the socket is deleted.
Usage #
Kernel BPF #
bpf_sk_storage_get() #
void *Socket-local storage for map can be retrieved from socket sk using the bpf_sk_storage_get() helper. If the BPF_LOCAL_STORAGE_GET_F_CREATE flag is used then bpf_sk_storage_get() will create the storage for sk if it does not already exist. value can be used together with BPF_LOCAL_STORAGE_GET_F_CREATE to initialize the storage value, otherwise it will be zero initialized. Returns a pointer to the storage on success, or NULL in case of failure.
bpf_sk_storage_delete() #
long Socket-local storage for map can be deleted from socket sk using the bpf_sk_storage_delete() helper. Returns 0 on success, or negative error in case of failure.
User space #
bpf_map_update_elem() #
int Socket-local storage for map map_fd can be added or updated locally to a socket using the bpf_map_update_elem() libbpf function. The socket is identified by a socket fd stored in the pointer key. The pointer value has the data to be added or updated to the socket fd. The type and size of value should be the same as the value type of the map definition.
The flags parameter can be used to control the update behaviour:
-
BPF_ANYwill create storage forsocketfdor update existing storage. -
BPF_NOEXISTwill create storage forsocketfdonly if it did not already exist, otherwise the call will fail with-EEXIST. -
BPF_EXISTwill update existing storage forsocketfdif it already exists, otherwise the call will fail with-ENOENT.
Returns 0 on success, or negative error in case of failure.
bpf_map_lookup_elem() #
int Socket-local storage for map map_fd can be retrieved from a socket using the bpf_map_lookup_elem() libbpf function. The storage is retrieved from the socket identified by a socket fd stored in the pointer key. Returns 0 on success, or negative error in case of failure.
bpf_map_delete_elem() #
int Socket-local storage for map map_fd can be deleted from a socket using the bpf_map_delete_elem() libbpf function. The storage is deleted from the socket identified by a socket fd stored in the pointer key. Returns 0 on success, or negative error in case of failure.
Examples #
Kernel BPF #
This snippet shows how to declare socket-local storage in a BPF program:
socket_storage ;
This snippet shows how to retrieve socket-local storage in a BPF program:
int
Please see the tools/testing/selftests/bpf directory for functional examples.
References #
https://lwn.net/ml/netdev/20190426171103.61892-1-kafai@fb.com/
BPF_MAP_TYPE_SOCKMAP and BPF_MAP_TYPE_SOCKHASH
Peios Kernel / Internals / BPF
BPF_MAP_TYPE_SOCKMAP and BPF_MAP_TYPE_SOCKHASH maps can be used to redirect skbs between sockets or to apply policy at the socket level based on the result of a BPF (verdict) program with the help of the BPF helpers bpf_sk_redirect_map(), bpf_sk_redirect_hash(), bpf_msg_redirect_map() and bpf_msg_redirect_hash().
BPF_MAP_TYPE_SOCKMAP is backed by an array that uses an integer key as the index to look up a reference to a struct sock. The map values are socket descriptors. Similarly, BPF_MAP_TYPE_SOCKHASH is a hash backed BPF map that holds references to sockets via their socket descriptors.
These maps may have BPF programs attached to them, specifically a parser program and a verdict program. The parser program determines how much data has been parsed and therefore how much data needs to be queued to come to a verdict. The verdict program is essentially the redirect program and can return a verdict of __SK_DROP, __SK_PASS, or __SK_REDIRECT.
When a socket is inserted into one of these maps, its socket callbacks are replaced and a struct sk_psock is attached to it. Additionally, this sk_psock inherits the programs that are attached to the map.
A sock object may be in multiple maps, but can only inherit a single parse or verdict program. If adding a sock object to a map would result in having multiple parser programs the update will return an EBUSY error.
The supported programs to attach to these maps are:
;
The attach types for the map programs are:
-
msg_parserprogram -BPF_SK_MSG_VERDICT. -
stream_parserprogram -BPF_SK_SKB_STREAM_PARSER. -
stream_verdictprogram -BPF_SK_SKB_STREAM_VERDICT. -
skb_verdictprogram -BPF_SK_SKB_VERDICT.
There are additional helpers available to use with the parser and verdict programs: bpf_msg_apply_bytes() and bpf_msg_cork_bytes(). With bpf_msg_apply_bytes() BPF programs can tell the infrastructure how many bytes the given verdict should apply to. The helper bpf_msg_cork_bytes() handles a different case where a BPF program cannot reach a verdict on a msg until it receives more bytes AND the program doesn't want to forward the packet until it is known to be good.
Finally, the helpers bpf_msg_pull_data() and bpf_msg_push_data() are available to BPF_PROG_TYPE_SK_MSG BPF programs to pull in data and set the start and end pointers to given values or to add metadata to the struct sk_msg_buff *msg.
All these helpers will be described in more detail below.
Usage #
Kernel BPF #
bpf_msg_redirect_map() #
long This helper is used in programs implementing policies at the socket level. If the message msg is allowed to pass (i.e., if the verdict BPF program returns SK_PASS), redirect it to the socket referenced by map (of type BPF_MAP_TYPE_SOCKMAP) at index key. Both ingress and egress interfaces can be used for redirection. The BPF_F_INGRESS value in flags is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.
Returns SK_PASS on success, or SK_DROP on error.
bpf_sk_redirect_map() #
long Redirect the packet to the socket referenced by map (of type BPF_MAP_TYPE_SOCKMAP) at index key. Both ingress and egress interfaces can be used for redirection. The BPF_F_INGRESS value in flags is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.
Returns SK_PASS on success, or SK_DROP on error.
bpf_map_lookup_elem() #
void *socket entries of type struct sock * can be retrieved using the bpf_map_lookup_elem() helper.
bpf_sock_map_update() #
long Add an entry to, or update a map referencing sockets. The skops is used as a new value for the entry associated to key. The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element. -
BPF_NOEXIST: Create a new element only if it did not exist. -
BPF_EXIST: Update an existing element.
If the map has BPF programs (parser and verdict), those will be inherited by the socket being added. If the socket is already attached to BPF programs, this results in an error.
Returns 0 on success, or a negative error in case of failure.
bpf_sock_hash_update() #
long Add an entry to, or update a sockhash map referencing sockets. The skops is used as a new value for the entry associated to key.
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element. -
BPF_NOEXIST: Create a new element only if it did not exist. -
BPF_EXIST: Update an existing element.
If the map has BPF programs (parser and verdict), those will be inherited by the socket being added. If the socket is already attached to BPF programs, this results in an error.
Returns 0 on success, or a negative error in case of failure.
bpf_msg_redirect_hash() #
long This helper is used in programs implementing policies at the socket level. If the message msg is allowed to pass (i.e., if the verdict BPF program returns SK_PASS), redirect it to the socket referenced by map (of type BPF_MAP_TYPE_SOCKHASH) using hash key. Both ingress and egress interfaces can be used for redirection. The BPF_F_INGRESS value in flags is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.
Returns SK_PASS on success, or SK_DROP on error.
bpf_sk_redirect_hash() #
long This helper is used in programs implementing policies at the skb socket level. If the sk_buff skb is allowed to pass (i.e., if the verdict BPF program returns SK_PASS), redirect it to the socket referenced by map (of type BPF_MAP_TYPE_SOCKHASH) using hash key. Both ingress and egress interfaces can be used for redirection. The BPF_F_INGRESS value in flags is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.
Returns SK_PASS on success, or SK_DROP on error.
bpf_msg_apply_bytes() #
long For socket policies, apply the verdict of the BPF program to the next (number of bytes) of message msg. For example, this helper can be used in the following cases:
-
A single
sendmsg()orsendfile()system call contains multiple logical messages that the BPF program is supposed to read and for which it should apply a verdict. -
A BPF program only cares to read the first
bytesof amsg. If the message has a large payload, then setting up and calling the BPF program repeatedly for all bytes, even though the verdict is already known, would create unnecessary overhead.
Returns 0
bpf_msg_cork_bytes() #
long For socket policies, prevent the execution of the verdict BPF program for message msg until the number of bytes have been accumulated.
This can be used when one needs a specific number of bytes before a verdict can be assigned, even if the data spans multiple sendmsg() or sendfile() calls.
Returns 0
bpf_msg_pull_data() #
long For socket policies, pull in non-linear data from user space for msg and set pointers msg->data and msg->data_end to start and end bytes offsets into msg, respectively.
If a program of type BPF_PROG_TYPE_SK_MSG is run on a msg it can only parse data that the (data, data_end) pointers have already consumed. For sendmsg() hooks this is likely the first scatterlist element. But for calls relying on MSG_SPLICE_PAGES (e.g., sendfile()) this will be the range (0, 0) because the data is shared with user space and by default the objective is to avoid allowing user space to modify data while (or after) BPF verdict is being decided. This helper can be used to pull in data and to set the start and end pointers to given values. Data will be copied if necessary (i.e., if data was not linear and if start and end pointers do not point to the same chunk).
A call to this helper is susceptible to change the underlying packet buffer. Therefore, at load time, all checks on pointers previously done by the verifier are invalidated and must be performed again, if the helper is used in combination with direct packet access.
All values for flags are reserved for future usage, and must be left at zero.
Returns 0 on success, or a negative error in case of failure.
bpf_map_lookup_elem() #
void *Look up a socket entry in the sockmap or sockhash map.
Returns the socket entry associated to key, or NULL if no entry was found.
bpf_map_update_elem() #
long Add or update a socket entry in a sockmap or sockhash.
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element.
-
BPF_NOEXIST: Create a new element only if it did not exist.
-
BPF_EXIST: Update an existing element.
Returns 0 on success, or a negative error in case of failure.
bpf_map_delete_elem() #
long Delete a socket entry from a sockmap or a sockhash.
Returns 0 on success, or a negative error in case of failure.
User space #
bpf_map_update_elem() #
int Sockmap entries can be added or updated using the bpf_map_update_elem() function. The key parameter is the index value of the sockmap array. And the value parameter is the FD value of that socket.
Under the hood, the sockmap update function uses the socket FD value to retrieve the associated socket and its attached psock.
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element.
-
BPF_NOEXIST: Create a new element only if it did not exist.
-
BPF_EXIST: Update an existing element.
bpf_map_lookup_elem() #
int Sockmap entries can be retrieved using the bpf_map_lookup_elem() function.
bpf_map_delete_elem() #
int Sockmap entries can be deleted using the bpf_map_delete_elem() function.
Returns 0 on success, or negative error in case of failure.
Examples #
Kernel BPF #
Several examples of the use of sockmap APIs can be found in:
The following code snippet shows how to declare a sockmap.
sock_map_rx ;
The following code snippet shows a sample parser program.
int
The following code snippet shows a simple verdict program that interacts with a sockmap to redirect traffic to another socket based on the local port.
int
The following code snippet shows how to declare a sockhash map.
;
sock_hash_rx ;
The following code snippet shows a simple verdict program that interacts with a sockhash to redirect traffic to another socket based on a hash of some of the skb parameters.
static inline
void
int
User space #
Several examples of the use of sockmap APIs can be found in:
The following code sample shows how to create a sockmap, attach a parser and verdict program, as well as add a socket entry.
int
References #
BPF_MAP_TYPE_XSKMAP
Peios Kernel / Internals / BPF
The BPF_MAP_TYPE_XSKMAP is used as a backend map for XDP BPF helper call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap' and 'cpumap'. This map type redirects raw XDP frames to AF_XDP sockets (XSKs), a new type of address family in the kernel that allows redirection of frames from a driver to user space without having to traverse the full network stack. An AF_XDP socket binds to a single netdev queue. A mapping of XSKs to queues is shown below:
+---------------------------------------------------+
| xsk A | xsk B | xsk C |<---+ User space
=========================================================|==========
| Queue 0 | Queue 1 | Queue 2 | | Kernel
+---------------------------------------------------+ |
| Netdev eth0 | |
+---------------------------------------------------+ |
| +=============+ | |
| | key | xsk | | |
| +---------+ +=============+ | |
| | | | 0 | xsk A | | |
| | | +-------------+ | |
| | | | 1 | xsk B | | |
| | BPF |-- redirect -->+-------------+-------------+
| | prog | | 2 | xsk C | |
| | | +-------------+ |
| | | |
| | | |
| +---------+ |
| |
+---------------------------------------------------+
Typically an XSKMAP is created per netdev. This map contains an array of XSK File Descriptors (FDs). The number of array elements is typically set or adjusted using the max_entries map parameter. For AF_XDP max_entries is equal to the number of queues supported by the netdev.
Usage #
Kernel BPF #
bpf_redirect_map() #
long Redirect the packet to the endpoint referenced by map at index key. For BPF_MAP_TYPE_XSKMAP this map contains references to XSK FDs for sockets attached to a netdev's queues.
bpf_map_lookup_elem() #
void *XSK entry references of type struct xdp_sock * can be retrieved using the bpf_map_lookup_elem() helper.
User space #
bpf_map_update_elem() #
int XSK entries can be added or updated using the bpf_map_update_elem() helper. The key parameter is equal to the queue_id of the queue the XSK is attaching to. And the value parameter is the FD value of that socket.
Under the hood, the XSKMAP update function uses the XSK FD value to retrieve the associated struct xdp_sock instance.
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element.
-
BPF_NOEXIST: Create a new element only if it did not exist.
-
BPF_EXIST: Update an existing element.
bpf_map_lookup_elem() #
int Returns struct xdp_sock * or negative error in case of failure.
bpf_map_delete_elem() #
int XSK entries can be deleted using the bpf_map_delete_elem() helper. This helper will return 0 on success, or negative error in case of failure.
Examples #
Kernel #
The following code snippet shows how to declare a BPF_MAP_TYPE_XSKMAP called xsks_map and how to redirect packets to an XSK.
xsks_map ;
int
User space #
The following code snippet shows how to update an XSKMAP with an XSK entry.
int
For an example on how create AF_XDP sockets, please see the AF_XDP-example and AF_XDP-forwarding programs in the bpf-examples directory in the libxdp repository. For a detailed explanation of the AF_XDP interface please see:
-
AF_XDP kernel documentation.
BPF maps
Peios Kernel / Internals / BPF
BPF 'maps' provide generic storage of different types for sharing data between kernel and user space. There are several storage types available, including hash, array, bloom filter and radix-tree. Several of the map types exist to support specific BPF helpers that perform actions based on the map contents. The maps are accessed from BPF programs via BPF helpers which are documented in the man-pages for bpf-helpers(7).
BPF maps are accessed from user space via the bpf syscall, which provides commands to create maps, lookup elements, update elements and delete elements. More details of the BPF syscall are available in ebpf-syscall and in the man-pages for bpf(2).
Map Types #
- BPF_MAP_TYPE_ARRAY and BPF_MAP_TYPE_PERCPU_ARRAY
- BPF_MAP_TYPE_BLOOM_FILTER
- BPF_MAP_TYPE_CGROUP_STORAGE
- BPF_MAP_TYPE_CGRP_STORAGE
- BPF_MAP_TYPE_CPUMAP
- map_devmap
- BPF_MAP_TYPE_HASH, with PERCPU and LRU Variants
- BPF_MAP_TYPE_LPM_TRIE
- BPF_MAP_TYPE_ARRAY_OF_MAPS and BPF_MAP_TYPE_HASH_OF_MAPS
- BPF_MAP_TYPE_QUEUE and BPF_MAP_TYPE_STACK
- BPF_MAP_TYPE_SK_STORAGE
- BPF_MAP_TYPE_SOCKMAP and BPF_MAP_TYPE_SOCKHASH
- BPF_MAP_TYPE_XSKMAP
Usage Notes #
int Use the bpf() system call to perform the operation specified by command. The operation takes parameters provided in attr. The size argument is the size of the union bpf_attr in attr.
BPF_MAP_CREATE
Create a map with the desired type and attributes in attr:
int fd;
union bpf_attr attr = ;
fd = ;
Returns a process-local file descriptor on success, or negative error in case of failure. The map can be deleted by calling close(fd). Maps held by open file descriptors will be deleted automatically when a process exits.
BPF_MAP_LOOKUP_ELEM
Lookup key in a given map using attr->map_fd, attr->key, attr->value. Returns zero and stores found elem into attr->value on success, or negative error on failure.
BPF_MAP_UPDATE_ELEM
Create or update key/value pair in a given map using attr->map_fd, attr->key, attr->value. Returns zero on success or negative error on failure.
BPF_MAP_DELETE_ELEM
Find and delete element by key in a given map using attr->map_fd, attr->key. Returns zero on success or negative error on failure.
Other
Peios Kernel / Internals / BPF
BPF_PROG_TYPE_CGROUP_SOCKOPT
Peios Kernel / Internals / BPF
BPF_PROG_TYPE_CGROUP_SOCKOPT program type can be attached to two cgroup hooks:
-
BPF_CGROUP_GETSOCKOPT- called every time process executesgetsockoptsystem call. -
BPF_CGROUP_SETSOCKOPT- called every time process executessetsockoptsystem call.
The context (struct bpf_sockopt) has associated socket (sk) and all input arguments: level, optname, optval and optlen.
BPF_CGROUP_SETSOCKOPT #
BPF_CGROUP_SETSOCKOPT is triggered before the kernel handling of sockopt and it has writable context: it can modify the supplied arguments before passing them down to the kernel. This hook has access to the cgroup and socket local storage.
If BPF program sets optlen to -1, the control will be returned back to the userspace after all other BPF programs in the cgroup chain finish (i.e. kernel setsockopt handling will not be executed).
Note, that optlen can not be increased beyond the user-supplied value. It can only be decreased or set to -1. Any other value will trigger EFAULT.
Return Type #
-
0- reject the syscall,EPERMwill be returned to the userspace. -
1- success, continue with next BPF program in the cgroup chain.
BPF_CGROUP_GETSOCKOPT #
BPF_CGROUP_GETSOCKOPT is triggered after the kernel handing of sockopt. The BPF hook can observe optval, optlen and retval if it's interested in whatever kernel has returned. BPF hook can override the values above, adjust optlen and reset retval to 0. If optlen has been increased above initial getsockopt value (i.e. userspace buffer is too small), EFAULT is returned.
This hook has access to the cgroup and socket local storage.
Note, that the only acceptable value to set to retval is 0 and the original value that the kernel returned. Any other value will trigger EFAULT.
Return Type #
-
0- reject the syscall,EPERMwill be returned to the userspace. -
1- success: copyoptvalandoptlento userspace, returnretvalfrom the syscall (note that this can be overwritten by the BPF program from the parent cgroup).
Cgroup Inheritance #
Suppose, there is the following cgroup hierarchy where each cgroup has BPF_CGROUP_GETSOCKOPT attached at each level with BPF_F_ALLOW_MULTI flag:
A (root, parent)
\
B (child)
When the application calls getsockopt syscall from the cgroup B, the programs are executed from the bottom up: B, A. First program (B) sees the result of kernel's getsockopt. It can optionally adjust optval, optlen and reset retval to 0. After that control will be passed to the second (A) program which will see the same context as B including any potential modifications.
Same for BPF_CGROUP_SETSOCKOPT: if the program is attached to A and B, the trigger order is B, then A. If B does any changes to the input arguments (level, optname, optval, optlen), then the next program in the chain (A) will see those changes, not the original input setsockopt arguments. The potentially modified values will be then passed down to the kernel.
Large optval #
When the optval is greater than the PAGE_SIZE, the BPF program can access only the first PAGE_SIZE of that data. So it has to options:
-
Set
optlento zero, which indicates that the kernel should use the original buffer from the userspace. Any modifications done by the BPF program to theoptvalare ignored. -
Set
optlento the value less thanPAGE_SIZE, which indicates that the kernel should use BPF's trimmedoptval.
When the BPF program returns with the optlen greater than PAGE_SIZE, the userspace will receive original kernel buffers without any modifications that the BPF program might have applied.
Example #
Recommended way to handle BPF programs is as follows:
int
int
See tools/testing/selftests/bpf/progs/sockopt_sk.c for an example of BPF program that handles socket options.
BPF_PROG_TYPE_CGROUP_SYSCTL
Peios Kernel / Internals / BPF
This document describes BPF_PROG_TYPE_CGROUP_SYSCTL program type that provides cgroup-bpf hook for sysctl.
The hook has to be attached to a cgroup and will be called every time a process inside that cgroup tries to read from or write to sysctl knob in proc.
1. Attach type #
BPF_CGROUP_SYSCTL attach type has to be used to attach BPF_PROG_TYPE_CGROUP_SYSCTL program to a cgroup.
2. Context #
BPF_PROG_TYPE_CGROUP_SYSCTL provides access to the following context from BPF program:
struct bpf_sysctl {
__u32 write;
__u32 file_pos;
};
-
writeindicates whether sysctl value is being read (0) or written (1). This field is read-only. -
file_posindicates file position sysctl is being accessed at, read or written. This field is read-write. Writing to the field sets the starting position in sysctl proc fileread(2)will be reading from orwrite(2)will be writing to. Writing zero to the field can be used e.g. to override whole sysctl value bybpf_sysctl_set_new_value()onwrite(2)even when it's called by user space onfile_pos > 0. Writing non-zero value to the field can be used to access part of sysctl value starting from specifiedfile_pos. Not all sysctl support access withfile_pos != 0, e.g. writes to numeric sysctl entries must always be at file position0. See alsokernel.sysctl_writes_strictsysctl.
See linux/bpf.h for more details on how context field can be accessed.
3. Return code #
BPF_PROG_TYPE_CGROUP_SYSCTL program must return one of the following return codes:
-
0means "reject access to sysctl"; -
1means "proceed with access".
If program returns 0 user space will get -1 from read(2) or write(2) and errno will be set to EPERM.
4. Helpers #
Since sysctl knob is represented by a name and a value, sysctl specific BPF helpers focus on providing access to these properties:
-
bpf_sysctl_get_name()to get sysctl name as it is visible in/proc/sysinto provided by BPF program buffer; -
bpf_sysctl_get_current_value()to get string value currently held by sysctl into provided by BPF program buffer. This helper is available on bothread(2)from andwrite(2)to sysctl; -
bpf_sysctl_get_new_value()to get new string value currently being written to sysctl before actual write happens. This helper can be used only onctx->write == 1; -
bpf_sysctl_set_new_value()to override new string value currently being written to sysctl before actual write happens. Sysctl value will be overridden starting from the currentctx->file_pos. If the whole value has to be overridden BPF program can setfile_posto zero before calling to the helper. This helper can be used only onctx->write == 1. New string value set by the helper is treated and verified by kernel same way as an equivalent string passed by user space.
BPF program sees sysctl value same way as user space does in proc filesystem, i.e. as a string. Since many sysctl values represent an integer or a vector of integers, the following helpers can be used to get numeric value from the string:
-
bpf_strtol()to convert initial part of the string to long integer similar to user space strtol(3); -
bpf_strtoul()to convert initial part of the string to unsigned long integer similar to user space strtoul(3);
See linux/bpf.h for more details on helpers described here.
5. Examples #
See test_sysctl_prog.c for an example of BPF program in C that access sysctl name and value, parses string value to get vector of integers and uses the result to make decision whether to allow or deny access to sysctl.
6. Notes #
BPF_PROG_TYPE_CGROUP_SYSCTL is intended to be used in trusted root environment, for example to monitor sysctl usage or catch unreasonable values an application, running as root in a separate cgroup, is trying to set.
Since task_dfl_cgroup(current) is called at sys_read / sys_write time it may return results different from that at sys_open time, i.e. process that opened sysctl file in proc filesystem may differ from process that is trying to read from / write to it and two such processes may run in different cgroups, what means BPF_PROG_TYPE_CGROUP_SYSCTL should not be used as a security mechanism to limit sysctl usage.
As with any cgroup-bpf program additional care should be taken if an application running as root in a cgroup should not be allowed to detach/replace BPF program attached by administrator.
BPF_PROG_TYPE_FLOW_DISSECTOR
Peios Kernel / Internals / BPF
Overview #
Flow dissector is a routine that parses metadata out of the packets. It's used in the various places in the networking subsystem (RFS, flow hash, etc).
BPF flow dissector is an attempt to reimplement C-based flow dissector logic in BPF to gain all the benefits of BPF verifier (namely, limits on the number of instructions and tail calls).
API #
BPF flow dissector programs operate on an __sk_buff. However, only the limited set of fields is allowed: data, data_end and flow_keys. flow_keys is struct bpf_flow_keys and contains flow dissector input and output arguments.
-
The inputs are:
-
nhoff- initial offset of the networking header -
thoff- initial offset of the transport header, initialized to nhoff -
n_proto- L3 protocol type, parsed out of L2 header -
flags- optional flags
-
Flow dissector BPF program should fill out the rest of the struct bpf_flow_keys fields. Input arguments nhoff/thoff/n_proto should be also adjusted accordingly.
The return code of the BPF program is either BPF_OK to indicate successful dissection, or BPF_DROP to indicate parsing error.
__sk_buff->data #
In the VLAN-less case, this is what the initial state of the BPF flow dissector looks like:
+------+------+------------+-----------+
| DMAC | SMAC | ETHER_TYPE | L3_HEADER |
+------+------+------------+-----------+
^
|
+-- flow dissector starts here
skb->data + flow_keys->nhoff point to the first byte of L3_HEADER
flow_keys->thoff = nhoff
flow_keys->n_proto = ETHER_TYPE
In case of VLAN, flow dissector can be called with the two different states.
Pre-VLAN parsing:
+------+------+------+-----+-----------+-----------+
| DMAC | SMAC | TPID | TCI |ETHER_TYPE | L3_HEADER |
+------+------+------+-----+-----------+-----------+
^
|
+-- flow dissector starts here
skb->data + flow_keys->nhoff point the to first byte of TCI
flow_keys->thoff = nhoff
flow_keys->n_proto = TPID
Please note that TPID can be 802.1AD and, hence, BPF program would have to parse VLAN information twice for double tagged packets.
Post-VLAN parsing:
+------+------+------+-----+-----------+-----------+
| DMAC | SMAC | TPID | TCI |ETHER_TYPE | L3_HEADER |
+------+------+------+-----+-----------+-----------+
^
|
+-- flow dissector starts here
skb->data + flow_keys->nhoff point the to first byte of L3_HEADER
flow_keys->thoff = nhoff
flow_keys->n_proto = ETHER_TYPE
In this case VLAN information has been processed before the flow dissector and BPF flow dissector is not required to handle it.
The takeaway here is as follows: BPF flow dissector program can be called with the optional VLAN header and should gracefully handle both cases: when single or double VLAN is present and when it is not present. The same program can be called for both cases and would have to be written carefully to handle both cases.
Flags #
flow_keys->flags might contain optional input flags that work as follows:
-
BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG- tells BPF flow dissector to continue parsing first fragment; the default expected behavior is that flow dissector returns as soon as it finds out that the packet is fragmented; used byeth_get_headlento estimate length of all headers for GRO. -
BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL- tells BPF flow dissector to stop parsing as soon as it reaches IPv6 flow label; used by___skb_get_hashto get flow hash. -
BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP- tells BPF flow dissector to stop parsing as soon as it reaches encapsulated headers; used by routing infrastructure.
Reference Implementation #
See tools/testing/selftests/bpf/progs/bpf_flow.c for the reference implementation and tools/testing/selftests/bpf/flow_dissector_load.[hc] for the loader. bpftool can be used to load BPF flow dissector program as well.
-
The reference implementation is organized as follows:
-
jmp_tablemap that contains sub-programs for each supported L3 protocol -
_dissectroutine - entry point; it does inputn_protoparsing and doesbpf_tail_callto the appropriate L3 handler
-
Since BPF at this point doesn't support looping (or any jumping back), jmp_table is used instead to handle multiple levels of encapsulation (and IPv6 options).
Current Limitations #
BPF flow dissector doesn't support exporting all the metadata that in-kernel C-based implementation can export. Notable example is single VLAN (802.1Q) and double VLAN (802.1AD) tags. Please refer to the struct bpf_flow_keys for a set of information that's currently can be exported from the BPF context.
When BPF flow dissector is attached to the root network namespace (machine-wide policy), users can't override it in their child network namespaces.
LSM BPF Programs
Peios Kernel / Internals / BPF
Peios: This is reference material for an upstream LSM or its configuration. Peios configures
landlock,lockdown,integrity,pkm; alternative MAC LSMs are incompatible with PKM, and this article is not an instruction to replace the Peios security stack. See Peios kernel behaviour.
These BPF programs allow runtime instrumentation of the LSM hooks by privileged users to implement system-wide MAC (Mandatory Access Control) and Audit policies using eBPF.
Structure #
The example shows an eBPF program that can be attached to the file_mprotect LSM hook:
int ;
Other LSM hooks which can be instrumented can be found in security/security.c.
eBPF programs that use Documentation/bpf/btf.rst do not need to include kernel headers for accessing information from the attached eBPF program's context. They can simply declare the structures in the eBPF program and only specify the fields that need to be accessed.
;
;
This can be further simplified (if one has access to the BTF information at build time) by generating the vmlinux.h with:
# bpftool btf dump file <path-to-btf-vmlinux> format c > vmlinux.h
The vmlinux.h can then simply be included in the BPF programs without requiring the definition of the types.
The eBPF programs can be declared using the``BPF_PROG`` macros defined in tools/lib/bpf/bpf_tracing.h. In this example:
"lsm/file_mprotect"indicates the LSM hook that the program must be attached to
mprotect_auditis the name of the eBPF program
int
The __attribute__((preserve_access_index)) is a clang feature that allows the BPF verifier to update the offsets for the access at runtime using the Documentation/bpf/btf.rst information. Since the BPF verifier is aware of the types, it also validates all the accesses made to the various types in the eBPF program.
Loading #
eBPF programs can be loaded with the bpf(2) syscall's BPF_PROG_LOAD operation:
struct bpf_object *obj;
obj = ;
;
This can be simplified by using a skeleton header generated by bpftool:
# bpftool gen skeleton my_prog.o > my_prog.skel.h
and the program can be loaded by including my_prog.skel.h and using the generated helper, my_prog__open_and_load.
Attachment to LSM Hooks #
The LSM allows attachment of eBPF programs as LSM hooks using bpf(2) syscall's BPF_RAW_TRACEPOINT_OPEN operation or more simply by using the libbpf helper bpf_program__attach_lsm.
The program can be detached from the LSM hook by destroying the link link returned by bpf_program__attach_lsm using bpf_link__destroy.
One can also use the helpers generated in my_prog.skel.h i.e. my_prog__attach for attachment and my_prog__destroy for cleaning up.
Examples #
An example eBPF program can be found in tools/testing/selftests/bpf/progs/lsm.c and the corresponding userspace code in tools/testing/selftests/bpf/prog_tests/test_lsm.c
BPF sk_lookup program
Peios Kernel / Internals / BPF
BPF sk_lookup program type (BPF_PROG_TYPE_SK_LOOKUP) introduces programmability into the socket lookup performed by the transport layer when a packet is to be delivered locally.
When invoked BPF sk_lookup program can select a socket that will receive the incoming packet by calling the bpf_sk_assign() BPF helper function.
Hooks for a common attach point (BPF_SK_LOOKUP) exist for both TCP and UDP.
Motivation #
BPF sk_lookup program type was introduced to address setup scenarios where binding sockets to an address with bind() socket call is impractical, such as:
-
receiving connections on a range of IP addresses, e.g. 192.0.2.0/24, when binding to a wildcard address
INADRR_ANYis not possible due to a port conflict, -
receiving connections on all or a wide range of ports, i.e. an L7 proxy use case.
Such setups would require creating and bind()'ing one socket to each of the IP address/port in the range, leading to resource consumption and potential latency spikes during socket lookup.
Attachment #
BPF sk_lookup program can be attached to a network namespace with bpf(BPF_LINK_CREATE, ...) syscall using the BPF_SK_LOOKUP attach type and a netns FD as attachment target_fd.
Multiple programs can be attached to one network namespace. Programs will be invoked in the same order as they were attached.
Hooks #
The attached BPF sk_lookup programs run whenever the transport layer needs to find a listening (TCP) or an unconnected (UDP) socket for an incoming packet.
Incoming traffic to established (TCP) and connected (UDP) sockets is delivered as usual without triggering the BPF sk_lookup hook.
The attached BPF programs must return with either SK_PASS or SK_DROP verdict code. As for other BPF program types that are network filters, SK_PASS signifies that the socket lookup should continue on to regular hashtable-based lookup, while SK_DROP causes the transport layer to drop the packet.
A BPF sk_lookup program can also select a socket to receive the packet by calling bpf_sk_assign() BPF helper. Typically, the program looks up a socket in a map holding sockets, such as SOCKMAP or SOCKHASH, and passes a struct bpf_sock * to bpf_sk_assign() helper to record the selection. Selecting a socket only takes effect if the program has terminated with SK_PASS code.
When multiple programs are attached, the end result is determined from return codes of all the programs according to the following rules:
-
If any program returned
SK_PASSand selected a valid socket, the socket is used as the result of the socket lookup. -
If more than one program returned
SK_PASSand selected a socket, the last selection takes effect. -
If any program returned
SK_DROP, and no program returnedSK_PASSand selected a socket, socket lookup fails. -
If all programs returned
SK_PASSand none of them selected a socket, socket lookup continues on.
API #
In its context, an instance of struct bpf_sk_lookup, BPF sk_lookup program receives information about the packet that triggered the socket lookup. Namely:
-
IP version (
AF_INETorAF_INET6), -
L4 protocol identifier (
IPPROTO_TCPorIPPROTO_UDP), -
source and destination IP address,
-
source and destination L4 port,
-
the socket that has been selected with
bpf_sk_assign().
Refer to struct bpf_sk_lookup declaration in linux/bpf.h user API header, and bpf-helpers(7) man-page section for bpf_sk_assign() for details.
Example #
See tools/testing/selftests/bpf/prog_tests/sk_lookup.c for the reference implementation.
Program Types
Peios Kernel / Internals / BPF
- BPF_PROG_TYPE_CGROUP_SOCKOPT
- BPF_PROG_TYPE_CGROUP_SYSCTL
- BPF_PROG_TYPE_FLOW_DISSECTOR
- LSM BPF Programs
- BPF sk_lookup program
For a list of all program types, see Program Types and ELF Sections in the libbpf documentation.
Redirect
Peios Kernel / Internals / BPF
XDP_REDIRECT #
Supported maps #
XDP_REDIRECT works with the following map types:
-
BPF_MAP_TYPE_DEVMAP -
BPF_MAP_TYPE_DEVMAP_HASH -
BPF_MAP_TYPE_CPUMAP -
BPF_MAP_TYPE_XSKMAP
For more information on these maps, please see the specific map documentation.
Process #
XDP_REDIRECT works by a three-step process, implemented in the functions below:
-
The
bpf_redirect()andbpf_redirect_map()helpers will lookup the target of the redirect and store it (along with some other metadata) in a per-CPU struct bpf_redirect_info. -
When the program returns the XDP_REDIRECT return code, the driver will call
xdp_do_redirect()which will use the information in struct bpf_redirect_info to actually enqueue the frame into a map type-specific bulk queue structure. -
Before exiting its NAPI poll loop, the driver will call
xdp_do_flush(), which will flush all the different bulk queues, thus completing the redirect. Note thatxdp_do_flush()must be called beforenapi_complete_done()in the driver, as the XDP_REDIRECT logic relies on being inside a single NAPI instance through to thexdp_do_flush()call for RCU protection of all in-kernel data structures.
Debugging packet drops #
Silent packet drops for XDP_REDIRECT can be debugged using:
-
bpf_trace
-
perf_record
bpf_trace #
The following bpftrace command can be used to capture and count all XDP tracepoints:
sudo bpftrace -e 'tracepoint:xdp:* { @cnt[probe] = count(); }'
Attaching 12 probes...
^C
@cnt[tracepoint:xdp:mem_connect]: 18
@cnt[tracepoint:xdp:mem_disconnect]: 18
@cnt[tracepoint:xdp:xdp_exception]: 19605
@cnt[tracepoint:xdp:xdp_devmap_xmit]: 1393604
@cnt[tracepoint:xdp:xdp_redirect]: 22292200
The following bpftrace command can be used to extract the ERRNO being returned as part of the err parameter:
sudo bpftrace -e \
'tracepoint:xdp:xdp_redirect*_err {@redir_errno[-args->err] = count();}
tracepoint:xdp:xdp_devmap_xmit {@devmap_errno[-args->err] = count();}'
perf record #
The perf tool also supports recording tracepoints:
perf record -a -e xdp:xdp_redirect_err \
-e xdp:xdp_redirect_map_err \
-e xdp:xdp_exception \
-e xdp:xdp_devmap_xmit
References #
BPF ring buffer
Peios Kernel / Internals / BPF
This document describes BPF ring buffer design, API, and implementation details.
Motivation #
There are two distinctive motivators for this work, which are not satisfied by existing perf buffer, which prompted creation of a new ring buffer implementation.
-
more efficient memory utilization by sharing ring buffer across CPUs;
-
preserving ordering of events that happen sequentially in time, even across multiple CPUs (e.g., fork/exec/exit events for a task).
These two problems are independent, but perf buffer fails to satisfy both. Both are a result of a choice to have per-CPU perf ring buffer. Both can be also solved by having an MPSC implementation of ring buffer. The ordering problem could technically be solved for perf buffer with some in-kernel counting, but given the first one requires an MPSC buffer, the same solution would solve the second problem automatically.
Semantics and APIs #
Single ring buffer is presented to BPF programs as an instance of BPF map of type BPF_MAP_TYPE_RINGBUF. Two other alternatives considered, but ultimately rejected.
One way would be to, similar to BPF_MAP_TYPE_PERF_EVENT_ARRAY, make BPF_MAP_TYPE_RINGBUF could represent an array of ring buffers, but not enforce "same CPU only" rule. This would be more familiar interface compatible with existing perf buffer use in BPF, but would fail if application needed more advanced logic to lookup ring buffer by arbitrary key. BPF_MAP_TYPE_HASH_OF_MAPS addresses this with current approach. Additionally, given the performance of BPF ringbuf, many use cases would just opt into a simple single ring buffer shared among all CPUs, for which current approach would be an overkill.
Another approach could introduce a new concept, alongside BPF map, to represent generic "container" object, which doesn't necessarily have key/value interface with lookup/update/delete operations. This approach would add a lot of extra infrastructure that has to be built for observability and verifier support. It would also add another concept that BPF developers would have to familiarize themselves with, new syntax in libbpf, etc. But then would really provide no additional benefits over the approach of using a map. BPF_MAP_TYPE_RINGBUF doesn't support lookup/update/delete operations, but so doesn't few other map types (e.g., queue and stack; array doesn't support delete, etc).
The approach chosen has an advantage of re-using existing BPF map infrastructure (introspection APIs in kernel, libbpf support, etc), being familiar concept (no need to teach users a new type of object in BPF program), and utilizing existing tooling (bpftool). For common scenario of using a single ring buffer for all CPUs, it's as simple and straightforward, as would be with a dedicated "container" object. On the other hand, by being a map, it can be combined with ARRAY_OF_MAPS and HASH_OF_MAPS map-in-maps to implement a wide variety of topologies, from one ring buffer for each CPU (e.g., as a replacement for perf buffer use cases), to a complicated application hashing/sharding of ring buffers (e.g., having a small pool of ring buffers with hashed task's tgid being a look up key to preserve order, but reduce contention).
Key and value sizes are enforced to be zero. max_entries is used to specify the size of ring buffer and has to be a power of 2 value.
There are a bunch of similarities between perf buffer (BPF_MAP_TYPE_PERF_EVENT_ARRAY) and new BPF ring buffer semantics:
-
variable-length records;
-
if there is no more space left in ring buffer, reservation fails, no blocking;
-
memory-mappable data area for user-space applications for ease of consumption and high performance;
-
epoll notifications for new incoming data;
-
but still the ability to do busy polling for new data to achieve the lowest latency, if necessary.
BPF ringbuf provides two sets of APIs to BPF programs:
-
bpf_ringbuf_output()allows to copy data from one place to a ring buffer, similarly tobpf_perf_event_output(); -
bpf_ringbuf_reserve()/bpf_ringbuf_commit()/bpf_ringbuf_discard()APIs split the whole process into two steps. First, a fixed amount of space is reserved. If successful, a pointer to a data inside ring buffer data area is returned, which BPF programs can use similarly to a data inside array/hash maps. Once ready, this piece of memory is either committed or discarded. Discard is similar to commit, but makes consumer ignore the record.
bpf_ringbuf_output() has disadvantage of incurring extra memory copy, because record has to be prepared in some other place first. But it allows to submit records of the length that's not known to verifier beforehand. It also closely matches bpf_perf_event_output(), so will simplify migration significantly.
bpf_ringbuf_reserve() avoids the extra copy of memory by providing a memory pointer directly to ring buffer memory. In a lot of cases records are larger than BPF stack space allows, so many programs have use extra per-CPU array as a temporary heap for preparing sample. bpf_ringbuf_reserve() avoid this needs completely. But in exchange, it only allows a known constant size of memory to be reserved, such that verifier can verify that BPF program can't access memory outside its reserved record space. bpf_ringbuf_output(), while slightly slower due to extra memory copy, covers some use cases that are not suitable for bpf_ringbuf_reserve().
The difference between commit and discard is very small. Discard just marks a record as discarded, and such records are supposed to be ignored by consumer code. Discard is useful for some advanced use-cases, such as ensuring all-or-nothing multi-record submission, or emulating temporary malloc()/free() within single BPF program invocation.
Each reserved record is tracked by verifier through existing reference-tracking logic, similar to socket ref-tracking. It is thus impossible to reserve a record, but forget to submit (or discard) it.
bpf_ringbuf_query() helper allows to query various properties of ring buffer. Currently 4 are supported:
-
BPF_RB_AVAIL_DATAreturns amount of unconsumed data in ring buffer; -
BPF_RB_RING_SIZEreturns the size of ring buffer; -
BPF_RB_CONS_POS/BPF_RB_PROD_POSreturns current logical position of consumer/producer, respectively.
Returned values are momentarily snapshots of ring buffer state and could be off by the time helper returns, so this should be used only for debugging/reporting reasons or for implementing various heuristics, that take into account highly-changeable nature of some of those characteristics.
One such heuristic might involve more fine-grained control over poll/epoll notifications about new data availability in ring buffer. Together with BPF_RB_NO_WAKEUP/BPF_RB_FORCE_WAKEUP flags for output/commit/discard helpers, it allows BPF program a high degree of control and, e.g., more efficient batched notifications. Default self-balancing strategy, though, should be adequate for most applications and will work reliable and efficiently already.
Design and Implementation #
This reserve/commit schema allows a natural way for multiple producers, either on different CPUs or even on the same CPU/in the same BPF program, to reserve independent records and work with them without blocking other producers. This means that if BPF program was interrupted by another BPF program sharing the same ring buffer, they will both get a record reserved (provided there is enough space left) and can work with it and submit it independently. This applies to NMI context as well, except that due to using a spinlock during reservation, in NMI context, bpf_ringbuf_reserve() might fail to get a lock, in which case reservation will fail even if ring buffer is not full.
The ring buffer itself internally is implemented as a power-of-2 sized circular buffer, with two logical and ever-increasing counters (which might wrap around on 32-bit architectures, that's not a problem):
-
consumer counter shows up to which logical position consumer consumed the data;
-
producer counter denotes amount of data reserved by all producers.
Each time a record is reserved, producer that "owns" the record will successfully advance producer counter. At that point, data is still not yet ready to be consumed, though. Each record has 8 byte header, which contains the length of reserved record, as well as two extra bits: busy bit to denote that record is still being worked on, and discard bit, which might be set at commit time if record is discarded. In the latter case, consumer is supposed to skip the record and move on to the next one. Record header also encodes record's relative offset from the beginning of ring buffer data area (in pages). This allows bpf_ringbuf_commit()/bpf_ringbuf_discard() to accept only the pointer to the record itself, without requiring also the pointer to ring buffer itself. Ring buffer memory location will be restored from record metadata header. This significantly simplifies verifier, as well as improving API usability.
Producer counter increments are serialized under spinlock, so there is a strict ordering between reservations. Commits, on the other hand, are completely lockless and independent. All records become available to consumer in the order of reservations, but only after all previous records where already committed. It is thus possible for slow producers to temporarily hold off submitted records, that were reserved later.
One interesting implementation bit, that significantly simplifies (and thus speeds up as well) implementation of both producers and consumers is how data area is mapped twice contiguously back-to-back in the virtual memory. This allows to not take any special measures for samples that have to wrap around at the end of the circular buffer data area, because the next page after the last data page would be first data page again, and thus the sample will still appear completely contiguous in virtual memory. See comment and a simple ASCII diagram showing this visually in bpf_ringbuf_area_alloc().
Another feature that distinguishes BPF ringbuf from perf ring buffer is a self-pacing notifications of new data being availability. bpf_ringbuf_commit() implementation will send a notification of new record being available after commit only if consumer has already caught up right up to the record being committed. If not, consumer still has to catch up and thus will see new data anyways without needing an extra poll notification. Benchmarks (see tools/testing/selftests/bpf/benchs/bench_ringbufs.c) show that this allows to achieve a very high throughput without having to resort to tricks like "notify only every Nth sample", which are necessary with perf buffer. For extreme cases, when BPF program wants more manual control of notifications, commit/discard/output helpers accept BPF_RB_NO_WAKEUP and BPF_RB_FORCE_WAKEUP flags, which give full control over notifications of data availability, but require extra caution and diligence in using this API.
Testing BPF on s390
Peios Kernel / Internals / BPF
1. Introduction #
IBM Z are mainframe computers, which are descendants of IBM System/360 from year 1964. They are supported by the Linux kernel under the name "s390". This document describes how to test BPF in an s390 QEMU guest.
2. One-time setup #
The following is required to build and run the test suite:
s390 GCC
s390 development headers and libraries
Clang with BPF support
QEMU with s390 support
Disk image with s390 rootfs
Debian supports installing compiler and libraries for s390 out of the box. Users of other distros may use debootstrap in order to set up a 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
Once on Debian, the build prerequisites can be installed as follows:
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
Latest Clang targeting BPF can be installed as follows:
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
The disk image can be prepared using a loopback mount and debootstrap:
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
3. Compilation #
In addition to the usual Kconfig options required to run the BPF test suite, it is also helpful to select:
CONFIG_NET_9P=y
CONFIG_9P_FS=y
CONFIG_NET_9P_VIRTIO=y
CONFIG_VIRTIO_PCI=y
as that would enable a very easy way to share files with the s390 virtual machine.
Compiling kernel, modules and testsuite, as well as preparing gdb scripts to simplify debugging, can be done using the following commands:
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
4. Running the test suite #
The virtual machine can be started as follows:
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
When using this on a real IBM Z, -enable-kvm may be added for better performance. When starting the virtual machine for the first time, disk image setup must be finalized using the following command:
/debootstrap/debootstrap --second-stage
Directory with the code built on the host as well as /proc and /sys need to be mounted as follows:
mkdir -p /linux
mount -t 9p linux /linux
mount -t proc proc /proc
mount -t sysfs sys /sys
After that, the test suite can be run using the following commands:
cd /linux/tools/testing/selftests/kselftest_install
./run_kselftest.sh
As usual, tests can be also run individually:
cd /linux/tools/testing/selftests/bpf
./test_verifier
5. Debugging #
It is possible to debug the s390 kernel using QEMU GDB stub, which is activated by passing -s to QEMU.
It is preferable to turn KASLR off, so that gdb would know where to find the kernel image in memory, by building the kernel with:
RANDOMIZE_BASE=n
GDB can then be attached using the following command:
gdb-multiarch -ex 'target remote localhost:1234' ./vmlinux
6. Network #
In case one needs to use the network in the virtual machine in order to e.g. install additional packages, it can be configured using:
dhclient eth0
7. Links #
This document is a compilation of techniques, whose more comprehensive descriptions can be found by following these links:
Syscall API
Peios Kernel / Internals / BPF
The primary info for the bpf syscall is available in the man-pages for bpf(2). For more information about the userspace API, see Documentation/userspace-api/ebpf/index.rst.
Testing and debugging BPF
Peios Kernel / Internals / BPF
eBPF verifier
Peios Kernel / Internals / BPF
The safety of the eBPF program is determined in two steps.
First step does DAG check to disallow loops and other CFG validation. In particular it will detect programs that have unreachable instructions. (though classic BPF checker allows them)
Second step starts from the first insn and descends all possible paths. It simulates execution of every insn and observes the state change of registers and stack.
At the start of the program the register R1 contains a pointer to context and has type PTR_TO_CTX. If verifier sees an insn that does R2=R1, then R2 has now type PTR_TO_CTX as well and can be used on the right hand side of expression. If R1=PTR_TO_CTX and insn is R2=R1+R1, then R2=SCALAR_VALUE, since addition of two valid pointers makes invalid pointer. (In 'secure' mode verifier will reject any type of pointer arithmetic to make sure that kernel addresses don't leak to unprivileged users)
If register was never written to, it's not readable:
bpf_mov R0 = R2
bpf_exit
will be rejected, since R2 is unreadable at the start of the program.
After kernel function call, R1-R5 are reset to unreadable and R0 has a return type of the function.
Since R6-R9 are callee saved, their state is preserved across the call.
bpf_mov R6 = 1
bpf_call foo
bpf_mov R0 = R6
bpf_exit
is a correct program. If there was R1 instead of R6, it would have been rejected.
load/store instructions are allowed only with registers of valid types, which are PTR_TO_CTX, PTR_TO_MAP, PTR_TO_STACK. They are bounds and alignment checked. For example:
bpf_mov R1 = 1
bpf_mov R2 = 2
bpf_xadd *(u32 *)(R1 + 3) += R2
bpf_exit
will be rejected, since R1 doesn't have a valid pointer type at the time of execution of instruction bpf_xadd.
At the start R1 type is PTR_TO_CTX (a pointer to generic struct bpf_context) A callback is used to customize verifier to restrict eBPF program access to only certain fields within ctx structure with specified size and alignment.
For example, the following insn:
bpf_ld R0 = *(u32 *)(R6 + 8)
intends to load a word from address R6 + 8 and store it into R0 If R6=PTR_TO_CTX, via is_valid_access() callback the verifier will know that offset 8 of size 4 bytes can be accessed for reading, otherwise the verifier will reject the program. If R6=PTR_TO_STACK, then access should be aligned and be within stack bounds, which are [-MAX_BPF_STACK, 0). In this example offset is 8, so it will fail verification, since it's out of bounds.
The verifier will allow eBPF program to read data from stack only after it wrote into it.
Classic BPF verifier does similar check with M[0-15] memory slots. For example:
bpf_ld R0 = *(u32 *)(R10 - 4)
bpf_exit
is invalid program. Though R10 is correct read-only register and has type PTR_TO_STACK and R10 - 4 is within stack bounds, there were no stores into that location.
Pointer register spill/fill is tracked as well, since four (R6-R9) callee saved registers may not be enough for some programs.
Allowed function calls are customized with bpf_verifier_ops->get_func_proto() The eBPF verifier will check that registers match argument constraints. After the call register R0 will be set to return type of the function.
Function calls is a main mechanism to extend functionality of eBPF programs. Socket filters may let programs to call one set of functions, whereas tracing filters may allow completely different set.
If a function made accessible to eBPF program, it needs to be thought through from safety point of view. The verifier will guarantee that the function is called with valid arguments.
seccomp vs socket filters have different security restrictions for classic BPF. Seccomp solves this by two stage verifier: classic BPF verifier is followed by seccomp verifier. In case of eBPF one configurable verifier is shared for all use cases.
See details of eBPF verifier in kernel/bpf/verifier.c
Register value tracking #
In order to determine the safety of an eBPF program, the verifier must track the range of possible values in each register and also in each stack slot. This is done with struct bpf_reg_state, defined in include/linux/ bpf_verifier.h, which unifies tracking of scalar and pointer values. Each register state has a type, which is either NOT_INIT (the register has not been written to), SCALAR_VALUE (some value which is not usable as a pointer), or a pointer type. The types of pointers describe their base, as follows:
PTR_TO_CTX
Pointer to bpf_context.
CONST_PTR_TO_MAP
Pointer to struct bpf_map. "Const" because arithmetic on these pointers is forbidden.
PTR_TO_MAP_VALUE
Pointer to the value stored in a map element.
PTR_TO_MAP_VALUE_OR_NULL
Either a pointer to a map value, or NULL; map accesses (see maps.rst) return this type, which becomes a PTR_TO_MAP_VALUE when checked != NULL. Arithmetic on these pointers is forbidden.
PTR_TO_STACK
Frame pointer.
PTR_TO_PACKET
skb->data.
PTR_TO_PACKET_END
skb->data + headlen; arithmetic forbidden.
PTR_TO_SOCKET
Pointer to struct bpf_sock_ops, implicitly refcounted.
PTR_TO_SOCKET_OR_NULL
Either a pointer to a socket, or NULL; socket lookup returns this type, which becomes a PTR_TO_SOCKET when checked != NULL. PTR_TO_SOCKET is reference-counted, so programs must release the reference through the socket release function before the end of the program. Arithmetic on these pointers is forbidden.
However, a pointer may be offset from this base (as a result of pointer arithmetic), and this is tracked in two parts: the 'fixed offset' and 'variable offset'. The former is used when an exactly-known value (e.g. an immediate operand) is added to a pointer, while the latter is used for values which are not exactly known. The variable offset is also used in SCALAR_VALUEs, to track the range of possible values in the register.
The verifier's knowledge about the variable offset consists of:
-
minimum and maximum values as unsigned
-
minimum and maximum values as signed
-
knowledge of the values of individual bits, in the form of a 'tnum': a u64 'mask' and a u64 'value'. 1s in the mask represent bits whose value is unknown; 1s in the value represent bits known to be 1. Bits known to be 0 have 0 in both mask and value; no bit should ever be 1 in both. For example, if a byte is read into a register from memory, the register's top 56 bits are known zero, while the low 8 are unknown - which is represented as the tnum (0x0; 0xff). If we then OR this with 0x40, we get (0x40; 0xbf), then if we add 1 we get (0x0; 0x1ff), because of potential carries.
Besides arithmetic, the register state can also be updated by conditional branches. For instance, if a SCALAR_VALUE is compared > 8, in the 'true' branch it will have a umin_value (unsigned minimum value) of 9, whereas in the 'false' branch it will have a umax_value of 8. A signed compare (with BPF_JSGT or BPF_JSGE) would instead update the signed minimum/maximum values. Information from the signed and unsigned bounds can be combined; for instance if a value is first tested < 8 and then tested s> 4, the verifier will conclude that the value is also > 4 and s< 8, since the bounds prevent crossing the sign boundary.
PTR_TO_PACKETs with a variable offset part have an 'id', which is common to all pointers sharing that same variable offset. This is important for packet range checks: after adding a variable to a packet pointer register A, if you then copy it to another register B and then add a constant 4 to A, both registers will share the same 'id' but the A will have a fixed offset of +4. Then if A is bounds-checked and found to be less than a PTR_TO_PACKET_END, the register B is now known to have a safe range of at least 4 bytes. See 'Direct packet access', below, for more on PTR_TO_PACKET ranges.
The 'id' field is also used on PTR_TO_MAP_VALUE_OR_NULL, common to all copies of the pointer returned from a map lookup. This means that when one copy is checked and found to be non-NULL, all copies can become PTR_TO_MAP_VALUEs. As well as range-checking, the tracked information is also used for enforcing alignment of pointer accesses. For instance, on most systems the packet pointer is 2 bytes after a 4-byte alignment. If a program adds 14 bytes to that to jump over the Ethernet header, then reads IHL and adds (IHL * 4), the resulting pointer will have a variable offset known to be 4n+2 for some n, so adding the 2 bytes (NET_IP_ALIGN) gives a 4-byte alignment and so word-sized accesses through that pointer are safe. The 'id' field is also used on PTR_TO_SOCKET and PTR_TO_SOCKET_OR_NULL, common to all copies of the pointer returned from a socket lookup. This has similar behaviour to the handling for PTR_TO_MAP_VALUE_OR_NULL->PTR_TO_MAP_VALUE, but it also handles reference tracking for the pointer. PTR_TO_SOCKET implicitly represents a reference to the corresponding struct sock. To ensure that the reference is not leaked, it is imperative to NULL-check the reference and in the non-NULL case, and pass the valid reference to the socket release function.
Direct packet access #
In cls_bpf and act_bpf programs the verifier allows direct access to the packet data via skb->data and skb->data_end pointers. Ex:
1: r4 = *(u32 *)(r1 +80) /* load skb->data_end */
2: r3 = *(u32 *)(r1 +76) /* load skb->data */
3: r5 = r3
4: r5 += 14
5: if r5 > r4 goto pc+16
R1=ctx R3=pkt(id=0,off=0,r=14) R4=pkt_end R5=pkt(id=0,off=14,r=14) R10=fp
6: r0 = *(u16 *)(r3 +12) /* access 12 and 13 bytes of the packet */
this 2byte load from the packet is safe to do, since the program author did check if (skb->data + 14 > skb->data_end) goto err at insn #5 which means that in the fall-through case the register R3 (which points to skb->data) has at least 14 directly accessible bytes. The verifier marks it as R3=pkt(id=0,off=0,r=14). id=0 means that no additional variables were added to the register. off=0 means that no additional constants were added. r=14 is the range of safe access which means that bytes [R3, R3 + 14) are ok. Note that R5 is marked as R5=pkt(id=0,off=14,r=14). It also points to the packet data, but constant 14 was added to the register, so it now points to skb->data + 14 and accessible range is [R5, R5 + 14 - 14) which is zero bytes.
More complex packet access may look like:
R0=inv1 R1=ctx R3=pkt(id=0,off=0,r=14) R4=pkt_end R5=pkt(id=0,off=14,r=14) R10=fp
6: r0 = *(u8 *)(r3 +7) /* load 7th byte from the packet */
7: r4 = *(u8 *)(r3 +12)
8: r4 *= 14
9: r3 = *(u32 *)(r1 +76) /* load skb->data */
10: r3 += r4
11: r2 = r1
12: r2 <<= 48
13: r2 >>= 48
14: r3 += r2
15: r2 = r3
16: r2 += 8
17: r1 = *(u32 *)(r1 +80) /* load skb->data_end */
18: if r2 > r1 goto pc+2
R0=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R1=pkt_end R2=pkt(id=2,off=8,r=8) R3=pkt(id=2,off=0,r=8) R4=inv(id=0,umax_value=3570,var_off=(0x0; 0xfffe)) R5=pkt(id=0,off=14,r=14) R10=fp
19: r1 = *(u8 *)(r3 +4)
The state of the register R3 is R3=pkt(id=2,off=0,r=8) id=2 means that two r3 += rX instructions were seen, so r3 points to some offset within a packet and since the program author did if (r3 + 8 > r1) goto err at insn #18, the safe range is [R3, R3 + 8). The verifier only allows 'add'/'sub' operations on packet registers. Any other operation will set the register state to 'SCALAR_VALUE' and it won't be available for direct packet access.
Operation r3 += rX may overflow and become less than original skb->data, therefore the verifier has to prevent that. So when it sees r3 += rX instruction and rX is more than 16-bit value, any subsequent bounds-check of r3 against skb->data_end will not give us 'range' information, so attempts to read through the pointer will give "invalid access to packet" error.
Ex. after insn r4 = *(u8 *)(r3 +12) (insn #7 above) the state of r4 is R4=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) which means that upper 56 bits of the register are guaranteed to be zero, and nothing is known about the lower 8 bits. After insn r4 *= 14 the state becomes R4=inv(id=0,umax_value=3570,var_off=(0x0; 0xfffe)), since multiplying an 8-bit value by constant 14 will keep upper 52 bits as zero, also the least significant bit will be zero as 14 is even. Similarly r2 >>= 48 will make R2=inv(id=0,umax_value=65535,var_off=(0x0; 0xffff)), since the shift is not sign extending. This logic is implemented in adjust_reg_min_max_vals() function, which calls adjust_ptr_min_max_vals() for adding pointer to scalar (or vice versa) and adjust_scalar_min_max_vals() for operations on two scalars.
The end result is that bpf program author can access packet directly using normal C code as:
void *data = (void *)(long)skb->data;
void *data_end = (void *)(long)skb->data_end;
struct eth_hdr *eth = data;
struct iphdr *iph = data + sizeof(*eth);
struct udphdr *udp = data + sizeof(*eth) + sizeof(*iph);
if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*udp) > data_end)
return 0;
if (eth->h_proto != htons(ETH_P_IP))
return 0;
if (iph->protocol != IPPROTO_UDP || iph->ihl != 5)
return 0;
if (udp->dest == 53 || udp->source == 9)
...;
which makes such programs easier to write comparing to LD_ABS insn and significantly faster.
Pruning #
The verifier does not actually walk all possible paths through the program. For each new branch to analyse, the verifier looks at all the states it's previously been in when at this instruction. If any of them contain the current state as a subset, the branch is 'pruned' - that is, the fact that the previous state was accepted implies the current state would be as well. For instance, if in the previous state, r1 held a packet-pointer, and in the current state, r1 holds a packet-pointer with a range as long or longer and at least as strict an alignment, then r1 is safe. Similarly, if r2 was NOT_INIT before then it can't have been used by any path from that point, so any value in r2 (including another NOT_INIT) is safe. The implementation is in the function regsafe(). Pruning considers not only the registers but also the stack (and any spilled registers it may hold). They must all be safe for the branch to be pruned. This is implemented in states_equal().
Some technical details about state pruning implementation could be found below.
Register liveness tracking #
In order to make state pruning effective, liveness state is tracked for each register and stack slot. The basic idea is to track which registers and stack slots are actually used during subseqeuent execution of the program, until program exit is reached. Registers and stack slots that were never used could be removed from the cached state thus making more states equivalent to a cached state. This could be illustrated by the following program:
0: call bpf_get_prandom_u32()
1: r1 = 0
2: if r0 == 0 goto +1
3: r0 = 1
--- checkpoint ---
4: r0 = r1
5: exit
Suppose that a state cache entry is created at instruction #4 (such entries are also called "checkpoints" in the text below). The verifier could reach the instruction with one of two possible register states:
-
r0 = 1, r1 = 0
-
r0 = 0, r1 = 0
However, only the value of register r1 is important to successfully finish verification. The goal of the liveness tracking algorithm is to spot this fact and figure out that both states are actually equivalent.
Understanding eBPF verifier messages #
The following are few examples of invalid eBPF programs and verifier error messages as seen in the log:
Program with unreachable instructions:
static struct bpf_insn prog[] = {
BPF_EXIT_INSN(),
BPF_EXIT_INSN(),
};
Error:
unreachable insn 1
Program that reads uninitialized register:
BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
BPF_EXIT_INSN(),
Error:
0: (bf) r0 = r2
R2 !read_ok
Program that doesn't initialize R0 before exiting:
BPF_MOV64_REG(BPF_REG_2, BPF_REG_1),
BPF_EXIT_INSN(),
Error:
0: (bf) r2 = r1
1: (95) exit
R0 !read_ok
Program that accesses stack out of bounds:
BPF_ST_MEM(BPF_DW, BPF_REG_10, 8, 0),
BPF_EXIT_INSN(),
Error:
0: (7a) *(u64 *)(r10 +8) = 0
invalid stack off=8 size=8
Program that doesn't initialize stack before passing its address into function:
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
BPF_LD_MAP_FD(BPF_REG_1, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
BPF_EXIT_INSN(),
Error:
0: (bf) r2 = r10
1: (07) r2 += -8
2: (b7) r1 = 0x0
3: (85) call 1
invalid indirect read from stack off -8+0 size 8
Program that uses invalid map_fd=0 while calling to map_lookup_elem() function:
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
BPF_LD_MAP_FD(BPF_REG_1, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
BPF_EXIT_INSN(),
Error:
0: (7a) *(u64 *)(r10 -8) = 0
1: (bf) r2 = r10
2: (07) r2 += -8
3: (b7) r1 = 0x0
4: (85) call 1
fd 0 is not pointing to valid bpf_map
Program that doesn't check return value of map_lookup_elem() before accessing map element:
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
BPF_LD_MAP_FD(BPF_REG_1, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
BPF_EXIT_INSN(),
Error:
0: (7a) *(u64 *)(r10 -8) = 0
1: (bf) r2 = r10
2: (07) r2 += -8
3: (b7) r1 = 0x0
4: (85) call 1
5: (7a) *(u64 *)(r0 +0) = 0
R0 invalid mem access 'map_value_or_null'
Program that correctly checks map_lookup_elem() returned value for NULL, but accesses the memory with incorrect alignment:
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
BPF_LD_MAP_FD(BPF_REG_1, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
BPF_ST_MEM(BPF_DW, BPF_REG_0, 4, 0),
BPF_EXIT_INSN(),
Error:
0: (7a) *(u64 *)(r10 -8) = 0
1: (bf) r2 = r10
2: (07) r2 += -8
3: (b7) r1 = 1
4: (85) call 1
5: (15) if r0 == 0x0 goto pc+1
R0=map_ptr R10=fp
6: (7a) *(u64 *)(r0 +4) = 0
misaligned access off 4 size 8
Program that correctly checks map_lookup_elem() returned value for NULL and accesses memory with correct alignment in one side of 'if' branch, but fails to do so in the other side of 'if' branch:
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
BPF_LD_MAP_FD(BPF_REG_1, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
BPF_EXIT_INSN(),
BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
Error:
0: (7a) *(u64 *)(r10 -8) = 0
1: (bf) r2 = r10
2: (07) r2 += -8
3: (b7) r1 = 1
4: (85) call 1
5: (15) if r0 == 0x0 goto pc+2
R0=map_ptr R10=fp
6: (7a) *(u64 *)(r0 +0) = 0
7: (95) exit
from 5 to 8: R0=imm0 R10=fp
8: (7a) *(u64 *)(r0 +0) = 1
R0 invalid mem access 'imm'
Program that performs a socket lookup then sets the pointer to NULL without checking it:
BPF_MOV64_IMM(BPF_REG_2, 0),
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
BPF_MOV64_IMM(BPF_REG_3, 4),
BPF_MOV64_IMM(BPF_REG_4, 0),
BPF_MOV64_IMM(BPF_REG_5, 0),
BPF_EMIT_CALL(BPF_FUNC_sk_lookup_tcp),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
Error:
0: (b7) r2 = 0
1: (63) *(u32 *)(r10 -8) = r2
2: (bf) r2 = r10
3: (07) r2 += -8
4: (b7) r3 = 4
5: (b7) r4 = 0
6: (b7) r5 = 0
7: (85) call bpf_sk_lookup_tcp#65
8: (b7) r0 = 0
9: (95) exit
Unreleased reference id=1, alloc_insn=7
Program that performs a socket lookup but does not NULL-check the returned value:
BPF_MOV64_IMM(BPF_REG_2, 0),
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
BPF_MOV64_IMM(BPF_REG_3, 4),
BPF_MOV64_IMM(BPF_REG_4, 0),
BPF_MOV64_IMM(BPF_REG_5, 0),
BPF_EMIT_CALL(BPF_FUNC_sk_lookup_tcp),
BPF_EXIT_INSN(),
Error:
0: (b7) r2 = 0
1: (63) *(u32 *)(r10 -8) = r2
2: (bf) r2 = r10
3: (07) r2 += -8
4: (b7) r3 = 4
5: (b7) r4 = 0
6: (b7) r5 = 0
7: (85) call bpf_sk_lookup_tcp#65
8: (95) exit
Unreleased reference id=1, alloc_insn=7
libbpf
Peios Kernel / Internals / BPF / libbpf
If you are looking to develop BPF applications using the libbpf library, this directory contains important documentation that you should read.
To get started, it is recommended to begin with the libbpf Overview document, which provides a high-level understanding of the libbpf APIs and their usage. This will give you a solid foundation to start exploring and utilizing the various features of libbpf to develop your BPF applications.
- libbpf_overview
- API Documentation
- Program Types and ELF Sections
- API naming convention
- Building libbpf
All general BPF questions, including kernel functionality, libbpf APIs and their application, should be sent to bpf@vger.kernel.org mailing list. You can subscribe to the mailing list search its archive. Please search the archive before asking new questions. It may be that this was already addressed or answered before.
Building libbpf
Peios Kernel / Internals / BPF / libbpf
libelf and zlib are internal dependencies of libbpf and thus are required to link against and must be installed on the system for applications to work. pkg-config is used by default to find libelf, and the program called can be overridden with PKG_CONFIG.
If using pkg-config at build time is not desired, it can be disabled by setting NO_PKG_CONFIG=1 when calling make.
To build both static libbpf.a and shared libbpf.so:
To build only static libbpf.a library in directory build/ and install them together with libbpf headers in a staging directory root/:
To build both static libbpf.a and shared libbpf.so against a custom libelf dependency installed in /build/root/ and install them together with libbpf headers in a build directory /build/root/:
API naming convention
Peios Kernel / Internals / BPF / libbpf
API naming convention #
libbpf API provides access to a few logically separated groups of functions and types. Every group has its own naming convention described here. It's recommended to follow these conventions whenever a new function or type is added to keep libbpf API clean and consistent.
All types and functions provided by libbpf API should have one of the following prefixes: bpf_, btf_, libbpf_, btf_dump_, ring_buffer_, perf_buffer_.
System call wrappers #
System call wrappers are simple wrappers for commands supported by sys_bpf system call. These wrappers should go to bpf.h header file and map one to one to corresponding commands.
For example bpf_map_lookup_elem wraps BPF_MAP_LOOKUP_ELEM command of sys_bpf, bpf_prog_attach wraps BPF_PROG_ATTACH, etc.
Objects #
Another class of types and functions provided by libbpf API is "objects" and functions to work with them. Objects are high-level abstractions such as BPF program or BPF map. They're represented by corresponding structures such as struct bpf_object, struct bpf_program, struct bpf_map, etc.
Structures are forward declared and access to their fields should be provided via corresponding getters and setters rather than directly.
These objects are associated with corresponding parts of ELF object that contains compiled BPF programs.
For example struct bpf_object represents ELF object itself created from an ELF file or from a buffer, struct bpf_program represents a program in ELF object and struct bpf_map is a map.
Functions that work with an object have names built from object name, double underscore and part that describes function purpose.
For example bpf_object__open consists of the name of corresponding object, bpf_object, double underscore and open that defines the purpose of the function to open ELF file and create bpf_object from it.
All objects and corresponding functions other than BTF related should go to libbpf.h. BTF types and functions should go to btf.h.
Auxiliary functions #
Auxiliary functions and types that don't fit well in any of categories described above should have libbpf_ prefix, e.g. libbpf_get_error or libbpf_prog_type_by_name.
ABI #
libbpf can be both linked statically or used as DSO. To avoid possible conflicts with other libraries an application is linked with, all non-static libbpf symbols should have one of the prefixes mentioned in API documentation above. See API naming convention to choose the right name for a new symbol.
Symbol visibility #
libbpf follow the model when all global symbols have visibility "hidden" by default and to make a symbol visible it has to be explicitly attributed with LIBBPF_API macro. For example:
LIBBPF_API int ;
This prevents from accidentally exporting a symbol, that is not supposed to be a part of ABI what, in turn, improves both libbpf developer- and user-experiences.
ABI versioning #
To make future ABI extensions possible libbpf ABI is versioned. Versioning is implemented by libbpf.map version script that is passed to linker.
Version name is LIBBPF_ prefix + three-component numeric version, starting from 0.0.1.
Every time ABI is being changed, e.g. because a new symbol is added or semantic of existing symbol is changed, ABI version should be bumped. This bump in ABI version is at most once per kernel development cycle.
For example, if current state of libbpf.map is:
LIBBPF_0.0.1 {
global:
bpf_func_a;
bpf_func_b;
local:
\*;
};
, and a new symbol bpf_func_c is being introduced, then libbpf.map should be changed like this:
LIBBPF_0.0.1 {
global:
bpf_func_a;
bpf_func_b;
local:
\*;
};
LIBBPF_0.0.2 {
global:
bpf_func_c;
} LIBBPF_0.0.1;
, where new version LIBBPF_0.0.2 depends on the previous LIBBPF_0.0.1.
Format of version script and ways to handle ABI changes, including incompatible ones, described in details in [1].
Stand-alone build #
Under https://github.com/libbpf/libbpf there is a (semi-)automated mirror of the mainline's version of libbpf for a stand-alone build.
However, all changes to libbpf's code base must be upstreamed through the mainline kernel tree.
API documentation convention #
The libbpf API is documented via comments above definitions in header files. These comments can be rendered by doxygen and sphinx for well organized html output. This section describes the convention in which these comments should be formatted.
Here is an example from btf.h:
/**
* @brief **btf__new()** creates a new instance of a BTF object from the raw
* bytes of an ELF's BTF section
* @param data raw bytes
* @param size number of bytes passed in `data`
* @return new BTF object instance which has to be eventually freed with
* **btf__free()**
*
* On error, error-code-encoded-as-pointer is returned, not a NULL. To extract
* error code from such a pointer `libbpf_get_error()` should be used. If
* `libbpf_set_strict_mode(LIBBPF_STRICT_CLEAN_PTRS)` is enabled, NULL is
* returned on error instead. In both cases thread-local `errno` variable is
* always set to error code as well.
*/
The comment must start with a block comment of the form '/**'.
The documentation always starts with a @brief directive. This line is a short description about this API. It starts with the name of the API, denoted in bold like so: api_name. Please include an open and close parenthesis if this is a function. Follow with the short description of the API. A longer form description can be added below the last directive, at the bottom of the comment.
Parameters are denoted with the @param directive, there should be one for each parameter. If this is a function with a non-void return, use the @return directive to document it.
License #
libbpf is dual-licensed under LGPL 2.1 and BSD 2-Clause.
Links #
-
[1] https://www.akkadia.org/drepper/dsohowto.pdf
(Chapter 3. Maintaining APIs and ABIs).
libbpf Overview
Peios Kernel / Internals / BPF / libbpf
libbpf is a C-based library containing a BPF loader that takes compiled BPF object files and prepares and loads them into the Linux kernel. libbpf takes the heavy lifting of loading, verifying, and attaching BPF programs to various kernel hooks, allowing BPF application developers to focus only on BPF program correctness and performance.
The following are the high-level features supported by libbpf:
-
Provides high-level and low-level APIs for user space programs to interact with BPF programs. The low-level APIs wrap all the bpf system call functionality, which is useful when users need more fine-grained control over the interactions between user space and BPF programs.
-
Provides overall support for the BPF object skeleton generated by bpftool. The skeleton file simplifies the process for the user space programs to access global variables and work with BPF programs.
-
Provides BPF-side APIS, including BPF helper definitions, BPF maps support, and tracing helpers, allowing developers to simplify BPF code writing.
-
Supports BPF CO-RE mechanism, enabling BPF developers to write portable BPF programs that can be compiled once and run across different kernel versions.
This document will delve into the above concepts in detail, providing a deeper understanding of the capabilities and advantages of libbpf and how it can help you develop BPF applications efficiently.
BPF App Lifecycle and libbpf APIs #
A BPF application consists of one or more BPF programs (either cooperating or completely independent), BPF maps, and global variables. The global variables are shared between all BPF programs, which allows them to cooperate on a common set of data. libbpf provides APIs that user space programs can use to manipulate the BPF programs by triggering different phases of a BPF application lifecycle.
The following section provides a brief overview of each phase in the BPF life cycle:
-
Open phase: In this phase, libbpf parses the BPF object file and discovers BPF maps, BPF programs, and global variables. After a BPF app is opened, user space apps can make additional adjustments (setting BPF program types, if necessary; pre-setting initial values for global variables, etc.) before all the entities are created and loaded.
-
Load phase: In the load phase, libbpf creates BPF maps, resolves various relocations, and verifies and loads BPF programs into the kernel. At this point, libbpf validates all the parts of a BPF application and loads the BPF program into the kernel, but no BPF program has yet been executed. After the load phase, it’s possible to set up the initial BPF map state without racing with the BPF program code execution.
-
Attachment phase: In this phase, libbpf attaches BPF programs to various BPF hook points (e.g., tracepoints, kprobes, cgroup hooks, network packet processing pipeline, etc.). During this phase, BPF programs perform useful work such as processing packets, or updating BPF maps and global variables that can be read from user space.
-
Tear down phase: In the tear down phase, libbpf detaches BPF programs and unloads them from the kernel. BPF maps are destroyed, and all the resources used by the BPF app are freed.
BPF Object Skeleton File #
BPF skeleton is an alternative interface to libbpf APIs for working with BPF objects. Skeleton code abstract away generic libbpf APIs to significantly simplify code for manipulating BPF programs from user space. Skeleton code includes a bytecode representation of the BPF object file, simplifying the process of distributing your BPF code. With BPF bytecode embedded, there are no extra files to deploy along with your application binary.
You can generate the skeleton header file (.skel.h) for a specific object file by passing the BPF object to the bpftool. The generated BPF skeleton provides the following custom functions that correspond to the BPF lifecycle, each of them prefixed with the specific object name:
-
<name>__open()– creates and opens BPF application (<name>stands for the specific bpf object name) -
<name>__load()– instantiates, loads,and verifies BPF application parts -
<name>__attach()– attaches all auto-attachable BPF programs (it’s optional, you can have more control by using libbpf APIs directly) -
<name>__destroy()– detaches all BPF programs and frees up all used resources
Using the skeleton code is the recommended way to work with bpf programs. Keep in mind, BPF skeleton provides access to the underlying BPF object, so whatever was possible to do with generic libbpf APIs is still possible even when the BPF skeleton is used. It's an additive convenience feature, with no syscalls, and no cumbersome code.
Other Advantages of Using Skeleton File #
-
BPF skeleton provides an interface for user space programs to work with BPF global variables. The skeleton code memory maps global variables as a struct into user space. The struct interface allows user space programs to initialize BPF programs before the BPF load phase and fetch and update data from user space afterward.
-
The
skel.hfile reflects the object file structure by listing out the available maps, programs, etc. BPF skeleton provides direct access to all the BPF maps and BPF programs as struct fields. This eliminates the need for string-based lookups withbpf_object_find_map_by_name()andbpf_object_find_program_by_name()APIs, reducing errors due to BPF source code and user-space code getting out of sync. -
The embedded bytecode representation of the object file ensures that the skeleton and the BPF object file are always in sync.
BPF Helpers #
libbpf provides BPF-side APIs that BPF programs can use to interact with the system. The BPF helpers definition allows developers to use them in BPF code as any other plain C function. For example, there are helper functions to print debugging messages, get the time since the system was booted, interact with BPF maps, manipulate network packets, etc.
For a complete description of what the helpers do, the arguments they take, and the return value, see the bpf-helpers man page.
BPF CO-RE (Compile Once – Run Everywhere) #
BPF programs work in the kernel space and have access to kernel memory and data structures. One limitation that BPF applications come across is the lack of portability across different kernel versions and configurations. BCC is one of the solutions for BPF portability. However, it comes with runtime overhead and a large binary size from embedding the compiler with the application.
libbpf steps up the BPF program portability by supporting the BPF CO-RE concept. BPF CO-RE brings together BTF type information, libbpf, and the compiler to produce a single executable binary that you can run on multiple kernel versions and configurations.
To make BPF programs portable libbpf relies on the BTF type information of the running kernel. Kernel also exposes this self-describing authoritative BTF information through sysfs at /sys/kernel/btf/vmlinux.
You can generate the BTF information for the running kernel with the following command:
$ bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
The command generates a vmlinux.h header file with all kernel types (BTF types) that the running kernel uses. Including vmlinux.h in your BPF program eliminates dependency on system-wide kernel headers.
libbpf enables portability of BPF programs by looking at the BPF program’s recorded BTF type and relocation information and matching them to BTF information (vmlinux) provided by the running kernel. libbpf then resolves and matches all the types and fields, and updates necessary offsets and other relocatable data to ensure that BPF program’s logic functions correctly for a specific kernel on the host. BPF CO-RE concept thus eliminates overhead associated with BPF development and allows developers to write portable BPF applications without modifications and runtime source code compilation on the target machine.
The following code snippet shows how to read the parent field of a kernel task_struct using BPF CO-RE and libbf. The basic helper to read a field in a CO-RE relocatable manner is bpf_core_read(dst, sz, src), which will read sz bytes from the field referenced by src into the memory pointed to by dst.
//...
struct task_struct *task = ;
struct task_struct *parent_task;
int err;
err = ;
if
/* parent_task contains the value of task->parent pointer */
In the code snippet, we first get a pointer to the current task_struct using bpf_get_current_task(). We then use bpf_core_read() to read the parent field of task struct into the parent_task variable. bpf_core_read() is just like bpf_probe_read_kernel() BPF helper, except it records information about the field that should be relocated on the target kernel. i.e, if the parent field gets shifted to a different offset within struct task_struct due to some new field added in front of it, libbpf will automatically adjust the actual offset to the proper value.
Getting Started with libbpf #
Check out the libbpf-bootstrap repository with simple examples of using libbpf to build various BPF applications.
See also libbpf API documentation.
libbpf and Rust #
If you are building BPF applications in Rust, it is recommended to use the Libbpf-rs library instead of bindgen bindings directly to libbpf. Libbpf-rs wraps libbpf functionality in Rust-idiomatic interfaces and provides libbpf-cargo plugin to handle BPF code compilation and skeleton generation. Using Libbpf-rs will make building user space part of the BPF application easier. Note that the BPF program themselves must still be written in plain C.
libbpf logging #
By default, libbpf logs informational and warning messages to stderr. The verbosity of these messages can be controlled by setting the environment variable LIBBPF_LOG_LEVEL to either warn, info, or debug. A custom log callback can be set using libbpf_set_print().
Additional Documentation #
Program Types and ELF Sections
Peios Kernel / Internals / BPF / libbpf
The table below lists the program types, their attach types where relevant and the ELF section names supported by libbpf for them. The ELF section names follow these rules:
-
typeis an exact match, e.g.SEC("socket") -
type+means it can be either exactSEC("type")or well-formedSEC("type/extras")with a '/' separator betweentypeandextras.
When extras are specified, they provide details of how to auto-attach the BPF program. The format of extras depends on the program type, e.g. SEC("tracepoint/<category>/<name>") for tracepoints or SEC("usdt/<path>:<provider>:<name>") for USDT probes. The extras are described in more detail in the footnotes.
|
Program Type |
Attach Type |
ELF Section Name |
Sleepable |
|---|---|---|---|
|
|
|
|
|
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
|
|
|
|
|
||
|
|
|
||
|
|
|||
|
|
|
||
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
Yes |
||
|
|
|||
|
|
Yes |
||
|
|
|||
|
|
Yes |
||
|
|
|
||
|
|
|||
|
|
|
||
|
|
|
||
|
|
Yes |
||
|
|
|||
|
|
Yes |
||
|
|
|
||
|
|
Yes |
||
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
Yes |
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|||
|
|
|
||
|
|
|||
|
|
|
||
|
|
|
||
|
|
|||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
|
|
|
|
|
||
|
|
Yes |
||
|
|
|
Yes |
|
|
|
|
||
|
|
|||
|
|
|
|
|
|
|
Yes |
||
|
|
|
||
|
|
Yes |
||
|
|
|
||
|
|
Yes |
||
|
|
|
||
|
|
Yes |
||
|
|
|
||
|
|
|
|
|
|
|
|||
|
|
|
||
|
|
|||
|
|
|
||
|
|
Footnotes
[1] The fentry attach format is fentry[.s]/<function>.
[2] The kprobe attach format is kprobe/<function>[+<offset>]. Valid characters for function are a-zA-Z0-9_. and offset must be a valid non-negative integer.
[3] The ksyscall attach format is ksyscall/<syscall>.
[4] The uprobe attach format is uprobe[.s]/<path>:<function>[+<offset>].
[5] The uprobe.multi attach format is uprobe.multi[.s]/<path>:<function-pattern> where function-pattern supports * and ? wildcards.
[6] The usdt attach format is usdt/<path>:<provider>:<name>.
[7] The kprobe.multi attach format is kprobe.multi/<pattern> where pattern supports * and ? wildcards. Valid characters for pattern are a-zA-Z0-9_.*?.
[8] The lsm attachment format is lsm[.s]/<hook>.
[9] The raw_tp attach format is raw_tracepoint[.w]/<tracepoint>.
[10] The tc, classifier and action attach types are deprecated, use tcx/* instead.
[11] The struct_ops attach format supports struct_ops[.s]/<name> convention, but name is ignored and it is recommended to just use plain SEC("struct_ops[.s]"). The attachments are defined in a struct initializer that is tagged with SEC(".struct_ops[.link]").
[12] The tracepoint attach format is tracepoint/<category>/<name>.
[13] The iter attach format is iter[.s]/<struct-name>.
BPF Standardization
Peios Kernel / Internals / BPF / standardization
This directory contains documents that are being iterated on as part of the BPF standardization effort with the IETF. See the IETF BPF Working Group page for the working group charter, documents, and more.
Contents
Peios Kernel / Internals / BPF / standardization
1 BPF ABI Recommended Conventions and Guidelines v1.0 #
This is version 1.0 of an informational document containing recommended conventions and guidelines for producing portable BPF program binaries.
1.1 Registers and calling convention #
BPF has 10 general purpose registers and a read-only frame pointer register, all of which are 64-bits wide.
The BPF calling convention is defined as:
-
R0: return value from function calls, and exit value for BPF programs
-
R1 - R5: arguments for function calls
-
R6 - R9: callee saved registers that function calls will preserve
-
R10: read-only frame pointer to access stack
R0 - R5 are scratch registers and BPF programs needs to spill/fill them if necessary across calls.
The BPF program needs to store the return value into register R0 before doing an EXIT.
Contents
Peios Kernel / Internals / BPF / standardization
1 BPF Instruction Set Architecture (ISA) #
eBPF, also commonly referred to as BPF, is a technology with origins in the Linux kernel that can run untrusted programs in a privileged context such as an operating system kernel. This document specifies the BPF instruction set architecture (ISA).
As a historical note, BPF originally stood for Berkeley Packet Filter, but now that it can do so much more than packet filtering, the acronym no longer makes sense. BPF is now considered a standalone term that does not stand for anything. The original BPF is sometimes referred to as cBPF (classic BPF) to distinguish it from the now widely deployed eBPF (extended BPF).
1.1 Documentation conventions #
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 https://www.rfc-editor.org/info/rfc2119 https://www.rfc-editor.org/info/rfc8174 when, and only when, they appear in all capitals, as shown here.
For brevity and consistency, this document refers to families of types using a shorthand syntax and refers to several expository, mnemonic functions when describing the semantics of instructions. The range of valid values for those types and the semantics of those functions are defined in the following subsections.
1.1.1 Types #
This document refers to integer types with the notation SN to specify a type's signedness (S) and bit width (N), respectively.
|
S |
Meaning |
|---|---|
|
u |
unsigned |
|
s |
signed |
|
N |
Bit width |
|---|---|
|
8 |
8 bits |
|
16 |
16 bits |
|
32 |
32 bits |
|
64 |
64 bits |
|
128 |
128 bits |
For example, u32 is a type whose valid values are all the 32-bit unsigned numbers and s16 is a type whose valid values are all the 16-bit signed numbers.
1.1.2 Functions #
The following byteswap functions are direction-agnostic. That is, the same function is used for conversion in either direction discussed below.
-
be16: Takes an unsigned 16-bit number and converts it between host byte order and big-endian (IEN137) byte order.
-
be32: Takes an unsigned 32-bit number and converts it between host byte order and big-endian byte order.
-
be64: Takes an unsigned 64-bit number and converts it between host byte order and big-endian byte order.
-
bswap16: Takes an unsigned 16-bit number in either big- or little-endian format and returns the equivalent number with the same bit width but opposite endianness.
-
bswap32: Takes an unsigned 32-bit number in either big- or little-endian format and returns the equivalent number with the same bit width but opposite endianness.
-
bswap64: Takes an unsigned 64-bit number in either big- or little-endian format and returns the equivalent number with the same bit width but opposite endianness.
-
le16: Takes an unsigned 16-bit number and converts it between host byte order and little-endian byte order.
-
le32: Takes an unsigned 32-bit number and converts it between host byte order and little-endian byte order.
-
le64: Takes an unsigned 64-bit number and converts it between host byte order and little-endian byte order.
1.1.3 Definitions #
-
To
sign extend anX-bit number, A, to aY-bit number, B ,means to-
Copy all
Xbits fromAto the lowerXbits ofB. -
Set the value of the remaining
Y-Xbits ofBto the value of the most-significant bit ofA.
-
1.1.4 Conformance groups #
An implementation does not need to support all instructions specified in this document (e.g., deprecated instructions). Instead, a number of conformance groups are specified. An implementation MUST support the base32 conformance group and MAY support additional conformance groups, where supporting a conformance group means it MUST support all instructions in that conformance group.
The use of named conformance groups enables interoperability between a runtime that executes instructions, and tools such as compilers that generate instructions for the runtime. Thus, capability discovery in terms of conformance groups might be done manually by users or automatically by tools.
Each conformance group has a short ASCII label (e.g., "base32") that corresponds to a set of instructions that are mandatory. That is, each instruction has one or more conformance groups of which it is a member.
This document defines the following conformance groups:
-
base32: includes all instructions defined in this specification unless otherwise noted.
-
base64: includes base32, plus instructions explicitly noted as being in the base64 conformance group.
-
atomic32: includes 32-bit atomic operation instructions (see Atomic operations).
-
atomic64: includes atomic32, plus 64-bit atomic operation instructions.
-
divmul32: includes 32-bit division, multiplication, and modulo instructions.
-
divmul64: includes divmul32, plus 64-bit division, multiplication, and modulo instructions.
-
packet: deprecated packet access instructions.
1.2 Instruction encoding #
BPF has two instruction encodings:
-
the basic instruction encoding, which uses 64 bits to encode an instruction
-
the wide instruction encoding, which appends a second 64 bits after the basic instruction for a total of 128 bits.
1.2.1 Basic instruction encoding #
A basic instruction is encoded as follows:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| opcode | regs | offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| imm |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
opcode
operation to perform, encoded as follows:
+-+-+-+-+-+-+-+-+ |specific |class| +-+-+-+-+-+-+-+-+-
specific
The format of these bits varies by instruction class
-
class
The instruction class (see Instruction classes)
-
-
regs
The source and destination register numbers, encoded as follows on a little-endian host:
+-+-+-+-+-+-+-+-+ |src_reg|dst_reg| +-+-+-+-+-+-+-+-+and as follows on a big-endian host:
+-+-+-+-+-+-+-+-+ |dst_reg|src_reg| +-+-+-+-+-+-+-+-+-
src_reg
the source register number (0-10), except where otherwise specified (64-bit immediate instructions reuse this field for other purposes)
-
dst_reg
destination register number (0-10), unless otherwise specified (future instructions might reuse this field for other purposes)
-
-
offset
signed integer offset used with pointer arithmetic, except where otherwise specified (some arithmetic instructions reuse this field for other purposes)
-
imm
signed integer immediate value
Note that the contents of multi-byte fields ('offset' and 'imm') are stored using big-endian byte ordering on big-endian hosts and little-endian byte ordering on little-endian hosts.
For example:
opcode offset imm assembly
src_reg dst_reg
07 0 1 00 00 44 33 22 11 r1 += 0x11223344 // little
dst_reg src_reg
07 1 0 00 00 11 22 33 44 r1 += 0x11223344 // big
Note that most instructions do not use all of the fields. Unused fields SHALL be cleared to zero.
1.2.2 Wide instruction encoding #
Some instructions are defined to use the wide instruction encoding, which uses two 32-bit immediate values. The 64 bits following the basic instruction format contain a pseudo instruction with 'opcode', 'dst_reg', 'src_reg', and 'offset' all set to zero.
This is depicted in the following figure:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| opcode | regs | offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| imm |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| reserved |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| next_imm |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
opcode
operation to perform, encoded as explained above
-
regs
The source and destination register numbers (unless otherwise specified), encoded as explained above
-
offset
signed integer offset used with pointer arithmetic, unless otherwise specified
-
imm
signed integer immediate value
-
reserved
unused, set to zero
-
next_imm
second signed integer immediate value
1.2.3 Instruction classes #
The three least significant bits of the 'opcode' field store the instruction class:
|
class |
value |
description |
reference |
|---|---|---|---|
|
LD |
0x0 |
non-standard load operations |
|
|
LDX |
0x1 |
load into register operations |
|
|
ST |
0x2 |
store from immediate operations |
|
|
STX |
0x3 |
store from register operations |
|
|
ALU |
0x4 |
32-bit arithmetic operations |
|
|
JMP |
0x5 |
64-bit jump operations |
|
|
JMP32 |
0x6 |
32-bit jump operations |
|
|
ALU64 |
0x7 |
64-bit arithmetic operations |
1.3 Arithmetic and jump instructions #
For arithmetic and jump instructions (ALU, ALU64, JMP and JMP32), the 8-bit 'opcode' field is divided into three parts:
+-+-+-+-+-+-+-+-+
| code |s|class|
+-+-+-+-+-+-+-+-+
-
code
the operation code, whose meaning varies by instruction class
-
s (source)
the source operand location, which unless otherwise specified is one of:
Source operand location
source
value
description
K
0
use 32-bit 'imm' value as source operand
X
1
use 'src_reg' register value as source operand
-
instruction class
the instruction class (see Instruction classes)
1.3.1 Arithmetic instructions #
ALU uses 32-bit wide operands while ALU64 uses 64-bit wide operands for otherwise identical operations. ALU64 instructions belong to the base64 conformance group unless noted otherwise. The 'code' field encodes the operation as below, where 'src' refers to the the source operand and 'dst' refers to the value of the destination register.
|
name |
code |
offset |
description |
|---|---|---|---|
|
ADD |
0x0 |
0 |
dst += src |
|
SUB |
0x1 |
0 |
dst -= src |
|
MUL |
0x2 |
0 |
dst *= src |
|
DIV |
0x3 |
0 |
dst = (src != 0) ? (dst / src) : 0 |
|
SDIV |
0x3 |
1 |
dst = (src == 0) ? 0 : ((src == -1 && dst == LLONG_MIN) ? LLONG_MIN : (dst s/ src)) |
|
OR |
0x4 |
0 |
dst |= src |
|
AND |
0x5 |
0 |
dst &= src |
|
LSH |
0x6 |
0 |
dst <<= (src & mask) |
|
RSH |
0x7 |
0 |
dst >>= (src & mask) |
|
NEG |
0x8 |
0 |
dst = -dst |
|
MOD |
0x9 |
0 |
dst = (src != 0) ? (dst % src) : dst |
|
SMOD |
0x9 |
1 |
dst = (src == 0) ? dst : ((src == -1 && dst == LLONG_MIN) ? 0: (dst s% src)) |
|
XOR |
0xa |
0 |
dst ^= src |
|
MOV |
0xb |
0 |
dst = src |
|
MOVSX |
0xb |
8/16/32 |
dst = (s8,s16,s32)src |
|
ARSH |
0xc |
0 |
sign extending dst >>= (src & mask) |
|
END |
0xd |
0 |
byte swap operations (see Byte swap instructions below) |
Underflow and overflow are allowed during arithmetic operations, meaning the 64-bit or 32-bit value will wrap. If BPF program execution would result in division by zero, the destination register is instead set to zero. Otherwise, for ALU64, if execution would result in LLONG_MIN divided by -1, the destination register is instead set to LLONG_MIN. For ALU, if execution would result in INT_MIN divided by -1, the destination register is instead set to INT_MIN.
If execution would result in modulo by zero, for ALU64 the value of the destination register is unchanged whereas for ALU the upper 32 bits of the destination register are zeroed. Otherwise, for ALU64, if execution would resuslt in LLONG_MIN modulo -1, the destination register is instead set to 0. For ALU, if execution would result in INT_MIN modulo -1, the destination register is instead set to 0.
{ADD, X, ALU}, where 'code' = ADD, 'source' = X, and 'class' = ALU, means:
dst = (u32) ((u32) dst + (u32) src)
where '(u32)' indicates that the upper 32 bits are zeroed.
{ADD, X, ALU64} means:
dst = dst + src
{XOR, K, ALU} means:
dst = (u32) dst ^ (u32) imm
{XOR, K, ALU64} means:
dst = dst ^ imm
Note that most arithmetic instructions have 'offset' set to 0. Only three instructions (SDIV, SMOD, MOVSX) have a non-zero 'offset'.
Division, multiplication, and modulo operations for ALU are part of the "divmul32" conformance group, and division, multiplication, and modulo operations for ALU64 are part of the "divmul64" conformance group. The division and modulo operations support both unsigned and signed flavors.
For unsigned operations (DIV and MOD), for ALU, 'imm' is interpreted as a 32-bit unsigned value. For ALU64, 'imm' is first sign extended from 32 to 64 bits, and then interpreted as a 64-bit unsigned value.
For signed operations (SDIV and SMOD), for ALU, 'imm' is interpreted as a 32-bit signed value. For ALU64, 'imm' is first sign extended from 32 to 64 bits, and then interpreted as a 64-bit signed value.
Note that there are varying definitions of the signed modulo operation when the dividend or divisor are negative, where implementations often vary by language such that Python, Ruby, etc. differ from C, Go, Java, etc. This specification requires that signed modulo MUST use truncated division (where -13 % 3 == -1) as implemented in C, Go, etc.:
a % n = a - n * trunc(a / n)
The MOVSX instruction does a move operation with sign extension. {MOVSX, X, ALU} sign extends 8-bit and 16-bit operands into 32-bit operands, and zeroes the remaining upper 32 bits. {MOVSX, X, ALU64} sign extends 8-bit, 16-bit, and 32-bit operands into 64-bit operands. Unlike other arithmetic instructions, MOVSX is only defined for register source operands (X).
{MOV, K, ALU64} means:
dst = (s64)imm
{MOV, X, ALU} means:
dst = (u32)src
{MOVSX, X, ALU} with 'offset' 8 means:
dst = (u32)(s32)(s8)src
The NEG instruction is only defined when the source bit is clear (K).
Shift operations use a mask of 0x3F (63) for 64-bit operations and 0x1F (31) for 32-bit operations.
1.3.2 Byte swap instructions #
The byte swap instructions use instruction classes of ALU and ALU64 and a 4-bit 'code' field of END.
The byte swap instructions operate on the destination register only and do not use a separate source register or immediate value.
For ALU, the 1-bit source operand field in the opcode is used to select what byte order the operation converts from or to. For ALU64, the 1-bit source operand field in the opcode is reserved and MUST be set to 0.
|
class |
source |
value |
description |
|---|---|---|---|
|
ALU |
LE |
0 |
convert between host byte order and little endian |
|
ALU |
BE |
1 |
convert between host byte order and big endian |
|
ALU64 |
Reserved |
0 |
do byte swap unconditionally |
The 'imm' field encodes the width of the swap operations. The following widths are supported: 16, 32 and 64. Width 64 operations belong to the base64 conformance group and other swap operations belong to the base32 conformance group.
Examples:
{END, LE, ALU} with 'imm' = 16/32/64 means:
dst = le16(dst)
dst = le32(dst)
dst = le64(dst)
{END, BE, ALU} with 'imm' = 16/32/64 means:
dst = be16(dst)
dst = be32(dst)
dst = be64(dst)
{END, TO, ALU64} with 'imm' = 16/32/64 means:
dst = bswap16(dst)
dst = bswap32(dst)
dst = bswap64(dst)
1.3.3 Jump instructions #
JMP32 uses 32-bit wide operands and indicates the base32 conformance group, while JMP uses 64-bit wide operands for otherwise identical operations, and indicates the base64 conformance group unless otherwise specified. The 'code' field encodes the operation as below:
|
code |
value |
src_reg |
description |
notes |
|---|---|---|---|---|
|
JA |
0x0 |
0x0 |
PC += offset |
{JA, K, JMP} only |
|
JA |
0x0 |
0x0 |
PC += imm |
{JA, K, JMP32} only |
|
JEQ |
0x1 |
any |
PC += offset if dst == src |
|
|
JGT |
0x2 |
any |
PC += offset if dst > src |
unsigned |
|
JGE |
0x3 |
any |
PC += offset if dst >= src |
unsigned |
|
JSET |
0x4 |
any |
PC += offset if dst & src |
|
|
JNE |
0x5 |
any |
PC += offset if dst != src |
|
|
JSGT |
0x6 |
any |
PC += offset if dst > src |
signed |
|
JSGE |
0x7 |
any |
PC += offset if dst >= src |
signed |
|
CALL |
0x8 |
0x0 |
call helper function by static ID |
{CALL, K, JMP} only, see Helper functions |
|
CALL |
0x8 |
0x1 |
call PC += imm |
{CALL, K, JMP} only, see Program-local functions |
|
CALL |
0x8 |
0x2 |
call helper function by BTF ID |
{CALL, K, JMP} only, see Helper functions |
|
EXIT |
0x9 |
0x0 |
return |
{CALL, K, JMP} only |
|
JLT |
0xa |
any |
PC += offset if dst < src |
unsigned |
|
JLE |
0xb |
any |
PC += offset if dst <= src |
unsigned |
|
JSLT |
0xc |
any |
PC += offset if dst < src |
signed |
|
JSLE |
0xd |
any |
PC += offset if dst <= src |
signed |
where 'PC' denotes the program counter, and the offset to increment by is in units of 64-bit instructions relative to the instruction following the jump instruction. Thus 'PC += 1' skips execution of the next instruction if it's a basic instruction or results in undefined behavior if the next instruction is a 128-bit wide instruction.
Example:
{JSGE, X, JMP32} means:
if (s32)dst s>= (s32)src goto +offset
where 's>=' indicates a signed '>=' comparison.
{JLE, K, JMP} means:
if dst <= (u64)(s64)imm goto +offset
{JA, K, JMP32} means:
gotol +imm
where 'imm' means the branch offset comes from the 'imm' field.
Note that there are two flavors of JA instructions. The JMP class permits a 16-bit jump offset specified by the 'offset' field, whereas the JMP32 class permits a 32-bit jump offset specified by the 'imm' field. A > 16-bit conditional jump may be converted to a < 16-bit conditional jump plus a 32-bit unconditional jump.
All CALL and JA instructions belong to the base32 conformance group.
1.3.3.1 Helper functions #
Helper functions are a concept whereby BPF programs can call into a set of function calls exposed by the underlying platform.
Historically, each helper function was identified by a static ID encoded in the 'imm' field. Further documentation of helper functions is outside the scope of this document and standardization is left for future work, but use is widely deployed and more information can be found in platform-specific documentation (e.g., Linux kernel documentation).
Platforms that support the BPF Type Format (BTF) support identifying a helper function by a BTF ID encoded in the 'imm' field, where the BTF ID identifies the helper name and type. Further documentation of BTF is outside the scope of this document and standardization is left for future work, but use is widely deployed and more information can be found in platform-specific documentation (e.g., Linux kernel documentation).
1.3.3.2 Program-local functions #
Program-local functions are functions exposed by the same BPF program as the caller, and are referenced by offset from the instruction following the call instruction, similar to JA. The offset is encoded in the 'imm' field of the call instruction. An EXIT within the program-local function will return to the caller.
1.4 Load and store instructions #
For load and store instructions (LD, LDX, ST, and STX), the 8-bit 'opcode' field is divided as follows:
+-+-+-+-+-+-+-+-+
|mode |sz |class|
+-+-+-+-+-+-+-+-+
-
mode
The mode modifier is one of:
Mode modifier
mode modifier
value
description
reference
IMM
0
64-bit immediate instructions
ABS
1
legacy BPF packet access (absolute)
IND
2
legacy BPF packet access (indirect)
MEM
3
regular load and store operations
MEMSX
4
sign-extension load operations
ATOMIC
6
atomic operations
-
sz (size)
The size modifier is one of:
Size modifier
size
value
description
W
0
word (4 bytes)
H
1
half word (2 bytes)
B
2
byte
DW
3
double word (8 bytes)
Instructions using
DWbelong to the base64 conformance group. -
class
The instruction class (see Instruction classes)
1.4.1 Regular load and store operations #
The MEM mode modifier is used to encode regular load and store instructions that transfer data between a register and memory.
{MEM, <size>, STX} means:
*(size *) (dst + offset) = src
{MEM, <size>, ST} means:
*(size *) (dst + offset) = imm
{MEM, <size>, LDX} means:
dst = *(unsigned size *) (src + offset)
Where '<size>' is one of: B, H, W, or DW, and 'unsigned size' is one of: u8, u16, u32, or u64.
1.4.2 Sign-extension load operations #
The MEMSX mode modifier is used to encode sign-extension load instructions that transfer data between a register and memory.
{MEMSX, <size>, LDX} means:
dst = *(signed size *) (src + offset)
Where '<size>' is one of: B, H, or W, and 'signed size' is one of: s8, s16, or s32.
1.4.3 Atomic operations #
Atomic operations are operations that operate on memory and can not be interrupted or corrupted by other access to the same memory region by other BPF programs or means outside of this specification.
All atomic operations supported by BPF are encoded as store operations that use the ATOMIC mode modifier as follows:
-
{ATOMIC, W, STX}for 32-bit operations, which are part of the "atomic32" conformance group. -
{ATOMIC, DW, STX}for 64-bit operations, which are part of the "atomic64" conformance group. -
8-bit and 16-bit wide atomic operations are not supported.
The 'imm' field is used to encode the actual atomic operation. Simple atomic operation use a subset of the values defined to encode arithmetic operations in the 'imm' field to encode the atomic operation:
|
imm |
value |
description |
|---|---|---|
|
ADD |
0x00 |
atomic add |
|
OR |
0x40 |
atomic or |
|
AND |
0x50 |
atomic and |
|
XOR |
0xa0 |
atomic xor |
{ATOMIC, W, STX} with 'imm' = ADD means:
*(u32 *)(dst + offset) += src
{ATOMIC, DW, STX} with 'imm' = ADD means:
*(u64 *)(dst + offset) += src
In addition to the simple atomic operations, there also is a modifier and two complex atomic operations:
|
imm |
value |
description |
|---|---|---|
|
FETCH |
0x01 |
modifier: return old value |
|
XCHG |
0xe0 | FETCH |
atomic exchange |
|
CMPXCHG |
0xf0 | FETCH |
atomic compare and exchange |
The FETCH modifier is optional for simple atomic operations, and always set for the complex atomic operations. If the FETCH flag is set, then the operation also overwrites src with the value that was in memory before it was modified.
The XCHG operation atomically exchanges src with the value addressed by dst + offset.
The CMPXCHG operation atomically compares the value addressed by dst + offset with R0. If they match, the value addressed by dst + offset is replaced with src. In either case, the value that was at dst + offset before the operation is zero-extended and loaded back to R0.
1.4.4 64-bit immediate instructions #
Instructions with the IMM 'mode' modifier use the wide instruction encoding defined in Instruction encoding, and use the 'src_reg' field of the basic instruction to hold an opcode subtype.
The following table defines a set of {IMM, DW, LD} instructions with opcode subtypes in the 'src_reg' field, using new terms such as "map" defined further below:
|
src_reg |
pseudocode |
imm type |
dst type |
|---|---|---|---|
|
0x0 |
dst = (next_imm << 32) | imm |
integer |
integer |
|
0x1 |
dst = map_by_fd(imm) |
map fd |
map |
|
0x2 |
dst = map_val(map_by_fd(imm)) + next_imm |
map fd |
data address |
|
0x3 |
dst = var_addr(imm) |
variable id |
data address |
|
0x4 |
dst = code_addr(imm) |
integer |
code address |
|
0x5 |
dst = map_by_idx(imm) |
map index |
map |
|
0x6 |
dst = map_val(map_by_idx(imm)) + next_imm |
map index |
data address |
where
-
map_by_fd(imm) means to convert a 32-bit file descriptor into an address of a map (see Maps)
-
map_by_idx(imm) means to convert a 32-bit index into an address of a map
-
map_val(map) gets the address of the first value in a given map
-
var_addr(imm) gets the address of a platform variable (see Platform Variables) with a given id
-
code_addr(imm) gets the address of the instruction at a specified relative offset in number of (64-bit) instructions
-
the 'imm type' can be used by disassemblers for display
-
the 'dst type' can be used for verification and JIT compilation purposes
1.4.4.1 Maps #
Maps are shared memory regions accessible by BPF programs on some platforms. A map can have various semantics as defined in a separate document, and may or may not have a single contiguous memory region, but the 'map_val(map)' is currently only defined for maps that do have a single contiguous memory region.
Each map can have a file descriptor (fd) if supported by the platform, where 'map_by_fd(imm)' means to get the map with the specified file descriptor. Each BPF program can also be defined to use a set of maps associated with the program at load time, and 'map_by_idx(imm)' means to get the map with the given index in the set associated with the BPF program containing the instruction.
1.4.4.2 Platform Variables #
Platform variables are memory regions, identified by integer ids, exposed by the runtime and accessible by BPF programs on some platforms. The 'var_addr(imm)' operation means to get the address of the memory region identified by the given id.
1.4.5 Legacy BPF Packet access instructions #
BPF previously introduced special instructions for access to packet data that were carried over from classic BPF. These instructions used an instruction class of LD, a size modifier of W, H, or B, and a mode modifier of ABS or IND. The 'dst_reg' and 'offset' fields were set to zero, and 'src_reg' was set to zero for ABS. However, these instructions are deprecated and SHOULD no longer be used. All legacy packet access instructions belong to the "packet" conformance group.