Skip to content

Commit 6166da0

Browse files
dthalerAlexei Starovoitov
authored andcommitted
bpf, docs: Move legacy packet instructions to a separate file
Move legacy packet instructions to a separate file. Signed-off-by: Dave Thaler <dthaler@microsoft.com> Link: https://lore.kernel.org/r/20220927185958.14995-1-dthaler1968@googlemail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 5ee35ab commit 6166da0

File tree

2 files changed

+68
-35
lines changed

2 files changed

+68
-35
lines changed

Documentation/bpf/instruction-set.rst

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,6 @@ arithmetic operations in the imm field to encode the atomic operation:
282282

283283
*(u64 *)(dst_reg + off16) += src_reg
284284

285-
``BPF_XADD`` is a deprecated name for ``BPF_ATOMIC | BPF_ADD``.
286-
287285
In addition to the simple atomic operations, there also is a modifier and
288286
two complex atomic operations:
289287

@@ -331,36 +329,6 @@ There is currently only one such instruction.
331329
Legacy BPF Packet access instructions
332330
-------------------------------------
333331

334-
eBPF has special instructions for access to packet data that have been
335-
carried over from classic BPF to retain the performance of legacy socket
336-
filters running in the eBPF interpreter.
337-
338-
The instructions come in two forms: ``BPF_ABS | <size> | BPF_LD`` and
339-
``BPF_IND | <size> | BPF_LD``.
340-
341-
These instructions are used to access packet data and can only be used when
342-
the program context is a pointer to networking packet. ``BPF_ABS``
343-
accesses packet data at an absolute offset specified by the immediate data
344-
and ``BPF_IND`` access packet data at an offset that includes the value of
345-
a register in addition to the immediate data.
346-
347-
These instructions have seven implicit operands:
348-
349-
* Register R6 is an implicit input that must contain pointer to a
350-
struct sk_buff.
351-
* Register R0 is an implicit output which contains the data fetched from
352-
the packet.
353-
* Registers R1-R5 are scratch registers that are clobbered after a call to
354-
``BPF_ABS | BPF_LD`` or ``BPF_IND | BPF_LD`` instructions.
355-
356-
These instructions have an implicit program exit condition as well. When an
357-
eBPF program is trying to access the data beyond the packet boundary, the
358-
program execution will be aborted.
359-
360-
``BPF_ABS | BPF_W | BPF_LD`` means::
361-
362-
R0 = ntohl(*(u32 *) (((struct sk_buff *) R6)->data + imm32))
363-
364-
``BPF_IND | BPF_W | BPF_LD`` means::
365-
366-
R0 = ntohl(*(u32 *) (((struct sk_buff *) R6)->data + src_reg + imm32))
332+
eBPF previously introduced special instructions for access to packet data that were
333+
carried over from classic BPF. However, these instructions are
334+
deprecated and should no longer be used.

Documentation/bpf/linux-notes.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
.. contents::
2+
.. sectnum::
3+
4+
==========================
5+
Linux implementation notes
6+
==========================
7+
8+
This document provides more details specific to the Linux kernel implementation of the eBPF instruction set.
9+
10+
Legacy BPF Packet access instructions
11+
=====================================
12+
13+
As mentioned in the `ISA standard documentation <instruction-set.rst#legacy-bpf-packet-access-instructions>`_,
14+
Linux has special eBPF instructions for access to packet data that have been
15+
carried over from classic BPF to retain the performance of legacy socket
16+
filters running in the eBPF interpreter.
17+
18+
The instructions come in two forms: ``BPF_ABS | <size> | BPF_LD`` and
19+
``BPF_IND | <size> | BPF_LD``.
20+
21+
These instructions are used to access packet data and can only be used when
22+
the program context is a pointer to a networking packet. ``BPF_ABS``
23+
accesses packet data at an absolute offset specified by the immediate data
24+
and ``BPF_IND`` access packet data at an offset that includes the value of
25+
a register in addition to the immediate data.
26+
27+
These instructions have seven implicit operands:
28+
29+
* Register R6 is an implicit input that must contain a pointer to a
30+
struct sk_buff.
31+
* Register R0 is an implicit output which contains the data fetched from
32+
the packet.
33+
* Registers R1-R5 are scratch registers that are clobbered by the
34+
instruction.
35+
36+
These instructions have an implicit program exit condition as well. If an
37+
eBPF program attempts access data beyond the packet boundary, the
38+
program execution will be aborted.
39+
40+
``BPF_ABS | BPF_W | BPF_LD`` (0x20) means::
41+
42+
R0 = ntohl(*(u32 *) ((struct sk_buff *) R6->data + imm))
43+
44+
where ``ntohl()`` converts a 32-bit value from network byte order to host byte order.
45+
46+
``BPF_IND | BPF_W | BPF_LD`` (0x40) means::
47+
48+
R0 = ntohl(*(u32 *) ((struct sk_buff *) R6->data + src + imm))
49+
50+
Appendix
51+
========
52+
53+
For reference, the following table lists legacy Linux-specific opcodes in order by value.
54+
55+
====== ==== =================================================== =============
56+
opcode imm description reference
57+
====== ==== =================================================== =============
58+
0x20 any dst = ntohl(\*(uint32_t \*)(R6->data + imm)) `Legacy BPF Packet access instructions`_
59+
0x28 any dst = ntohs(\*(uint16_t \*)(R6->data + imm)) `Legacy BPF Packet access instructions`_
60+
0x30 any dst = (\*(uint8_t \*)(R6->data + imm)) `Legacy BPF Packet access instructions`_
61+
0x38 any dst = ntohll(\*(uint64_t \*)(R6->data + imm)) `Legacy BPF Packet access instructions`_
62+
0x40 any dst = ntohl(\*(uint32_t \*)(R6->data + src + imm)) `Legacy BPF Packet access instructions`_
63+
0x48 any dst = ntohs(\*(uint16_t \*)(R6->data + src + imm)) `Legacy BPF Packet access instructions`_
64+
0x50 any dst = \*(uint8_t \*)(R6->data + src + imm)) `Legacy BPF Packet access instructions`_
65+
0x58 any dst = ntohll(\*(uint64_t \*)(R6->data + src + imm)) `Legacy BPF Packet access instructions`_

0 commit comments

Comments
 (0)