Skip to content

Commit dfee07c

Browse files
Alexei Starovoitovdavem330
authored andcommitted
net: filter: doc: expand and improve BPF documentation
In particular, this patch tries to clarify internal BPF calling convention and adds internal BPF examples, JIT guide, use cases. Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 2490155 commit dfee07c

1 file changed

Lines changed: 154 additions & 4 deletions

File tree

‎Documentation/networking/filter.txt‎

Lines changed: 154 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ Some core changes of the new internal format:
613613

614614
Therefore, BPF calling convention is defined as:
615615

616-
* R0 - return value from in-kernel function
616+
* R0 - return value from in-kernel function, and exit value for BPF program
617617
* R1 - R5 - arguments from BPF program to in-kernel function
618618
* R6 - R9 - callee saved registers that in-kernel function will preserve
619619
* R10 - read-only frame pointer to access stack
@@ -659,9 +659,140 @@ Some core changes of the new internal format:
659659
- Introduces bpf_call insn and register passing convention for zero overhead
660660
calls from/to other kernel functions:
661661

662-
After a kernel function call, R1 - R5 are reset to unreadable and R0 has a
663-
return type of the function. Since R6 - R9 are callee saved, their state is
664-
preserved across the call.
662+
Before an in-kernel function call, the internal BPF program needs to
663+
place function arguments into R1 to R5 registers to satisfy calling
664+
convention, then the interpreter will take them from registers and pass
665+
to in-kernel function. If R1 - R5 registers are mapped to CPU registers
666+
that are used for argument passing on given architecture, the JIT compiler
667+
doesn't need to emit extra moves. Function arguments will be in the correct
668+
registers and BPF_CALL instruction will be JITed as single 'call' HW
669+
instruction. This calling convention was picked to cover common call
670+
situations without performance penalty.
671+
672+
After an in-kernel function call, R1 - R5 are reset to unreadable and R0 has
673+
a return value of the function. Since R6 - R9 are callee saved, their state
674+
is preserved across the call.
675+
676+
For example, consider three C functions:
677+
678+
u64 f1() { return (*_f2)(1); }
679+
u64 f2(u64 a) { return f3(a + 1, a); }
680+
u64 f3(u64 a, u64 b) { return a - b; }
681+
682+
GCC can compile f1, f3 into x86_64:
683+
684+
f1:
685+
movl $1, %edi
686+
movq _f2(%rip), %rax
687+
jmp *%rax
688+
f3:
689+
movq %rdi, %rax
690+
subq %rsi, %rax
691+
ret
692+
693+
Function f2 in BPF may look like:
694+
695+
f2:
696+
bpf_mov R2, R1
697+
bpf_add R1, 1
698+
bpf_call f3
699+
bpf_exit
700+
701+
If f2 is JITed and the pointer stored to '_f2'. The calls f1 -> f2 -> f3 and
702+
returns will be seamless. Without JIT, __sk_run_filter() interpreter needs to
703+
be used to call into f2.
704+
705+
For practical reasons all BPF programs have only one argument 'ctx' which is
706+
already placed into R1 (e.g. on __sk_run_filter() startup) and the programs
707+
can call kernel functions with up to 5 arguments. Calls with 6 or more arguments
708+
are currently not supported, but these restrictions can be lifted if necessary
709+
in the future.
710+
711+
On 64-bit architectures all register map to HW registers one to one. For
712+
example, x86_64 JIT compiler can map them as ...
713+
714+
R0 - rax
715+
R1 - rdi
716+
R2 - rsi
717+
R3 - rdx
718+
R4 - rcx
719+
R5 - r8
720+
R6 - rbx
721+
R7 - r13
722+
R8 - r14
723+
R9 - r15
724+
R10 - rbp
725+
726+
... since x86_64 ABI mandates rdi, rsi, rdx, rcx, r8, r9 for argument passing
727+
and rbx, r12 - r15 are callee saved.
728+
729+
Then the following internal BPF pseudo-program:
730+
731+
bpf_mov R6, R1 /* save ctx */
732+
bpf_mov R2, 2
733+
bpf_mov R3, 3
734+
bpf_mov R4, 4
735+
bpf_mov R5, 5
736+
bpf_call foo
737+
bpf_mov R7, R0 /* save foo() return value */
738+
bpf_mov R1, R6 /* restore ctx for next call */
739+
bpf_mov R2, 6
740+
bpf_mov R3, 7
741+
bpf_mov R4, 8
742+
bpf_mov R5, 9
743+
bpf_call bar
744+
bpf_add R0, R7
745+
bpf_exit
746+
747+
After JIT to x86_64 may look like:
748+
749+
push %rbp
750+
mov %rsp,%rbp
751+
sub $0x228,%rsp
752+
mov %rbx,-0x228(%rbp)
753+
mov %r13,-0x220(%rbp)
754+
mov %rdi,%rbx
755+
mov $0x2,%esi
756+
mov $0x3,%edx
757+
mov $0x4,%ecx
758+
mov $0x5,%r8d
759+
callq foo
760+
mov %rax,%r13
761+
mov %rbx,%rdi
762+
mov $0x2,%esi
763+
mov $0x3,%edx
764+
mov $0x4,%ecx
765+
mov $0x5,%r8d
766+
callq bar
767+
add %r13,%rax
768+
mov -0x228(%rbp),%rbx
769+
mov -0x220(%rbp),%r13
770+
leaveq
771+
retq
772+
773+
Which is in this example equivalent in C to:
774+
775+
u64 bpf_filter(u64 ctx)
776+
{
777+
return foo(ctx, 2, 3, 4, 5) + bar(ctx, 6, 7, 8, 9);
778+
}
779+
780+
In-kernel functions foo() and bar() with prototype: u64 (*)(u64 arg1, u64
781+
arg2, u64 arg3, u64 arg4, u64 arg5); will receive arguments in proper
782+
registers and place their return value into '%rax' which is R0 in BPF.
783+
Prologue and epilogue are emitted by JIT and are implicit in the
784+
interpreter. R0-R5 are scratch registers, so BPF program needs to preserve
785+
them across the calls as defined by calling convention.
786+
787+
For example the following program is invalid:
788+
789+
bpf_mov R1, 1
790+
bpf_call foo
791+
bpf_mov R0, R1
792+
bpf_exit
793+
794+
After the call the registers R1-R5 contain junk values and cannot be read.
795+
In the future a BPF verifier can be used to validate internal BPF programs.
665796

666797
Also in the new design, BPF is limited to 4096 insns, which means that any
667798
program will terminate quickly and will only call a fixed number of kernel
@@ -676,6 +807,25 @@ A program, that is translated internally consists of the following elements:
676807

677808
op:16, jt:8, jf:8, k:32 ==> op:8, a_reg:4, x_reg:4, off:16, imm:32
678809

810+
So far 87 internal BPF instructions were implemented. 8-bit 'op' opcode field
811+
has room for new instructions. Some of them may use 16/24/32 byte encoding. New
812+
instructions must be multiple of 8 bytes to preserve backward compatibility.
813+
814+
Internal BPF is a general purpose RISC instruction set. Not every register and
815+
every instruction are used during translation from original BPF to new format.
816+
For example, socket filters are not using 'exclusive add' instruction, but
817+
tracing filters may do to maintain counters of events, for example. Register R9
818+
is not used by socket filters either, but more complex filters may be running
819+
out of registers and would have to resort to spill/fill to stack.
820+
821+
Internal BPF can used as generic assembler for last step performance
822+
optimizations, socket filters and seccomp are using it as assembler. Tracing
823+
filters may use it as assembler to generate code from kernel. In kernel usage
824+
may not be bounded by security considerations, since generated internal BPF code
825+
may be optimizing internal code path and not being exposed to the user space.
826+
Safety of internal BPF can come from a verifier (TBD). In such use cases as
827+
described, it may be used as safe instruction set.
828+
679829
Just like the original BPF, the new format runs within a controlled environment,
680830
is deterministic and the kernel can easily prove that. The safety of the program
681831
can be determined in two steps: first step does depth-first-search to disallow

0 commit comments

Comments
 (0)