14d496be9SDavid Vernet.. contents:: 24d496be9SDavid Vernet.. sectnum:: 34d496be9SDavid Vernet 4*7d35eb1aSDavid Vernet======================================= 5*7d35eb1aSDavid VernetBPF Instruction Set Specification, v1.0 6*7d35eb1aSDavid Vernet======================================= 74d496be9SDavid Vernet 8*7d35eb1aSDavid VernetThis document specifies version 1.0 of the BPF instruction set. 94d496be9SDavid Vernet 104d496be9SDavid VernetDocumentation conventions 114d496be9SDavid Vernet========================= 124d496be9SDavid Vernet 132369e526SWill HawkinsFor brevity and consistency, this document refers to families 142369e526SWill Hawkinsof types using a shorthand syntax and refers to several expository, 152369e526SWill Hawkinsmnemonic functions when describing the semantics of instructions. 162369e526SWill HawkinsThe range of valid values for those types and the semantics of those 172369e526SWill Hawkinsfunctions are defined in the following subsections. 182369e526SWill Hawkins 192369e526SWill HawkinsTypes 202369e526SWill Hawkins----- 212369e526SWill HawkinsThis document refers to integer types with the notation `SN` to specify 222369e526SWill Hawkinsa type's signedness (`S`) and bit width (`N`), respectively. 232369e526SWill Hawkins 242369e526SWill Hawkins.. table:: Meaning of signedness notation. 252369e526SWill Hawkins 262369e526SWill Hawkins ==== ========= 272369e526SWill Hawkins `S` Meaning 282369e526SWill Hawkins ==== ========= 292369e526SWill Hawkins `u` unsigned 302369e526SWill Hawkins `s` signed 312369e526SWill Hawkins ==== ========= 322369e526SWill Hawkins 332369e526SWill Hawkins.. table:: Meaning of bit-width notation. 342369e526SWill Hawkins 352369e526SWill Hawkins ===== ========= 362369e526SWill Hawkins `N` Bit width 372369e526SWill Hawkins ===== ========= 382369e526SWill Hawkins `8` 8 bits 392369e526SWill Hawkins `16` 16 bits 402369e526SWill Hawkins `32` 32 bits 412369e526SWill Hawkins `64` 64 bits 422369e526SWill Hawkins `128` 128 bits 432369e526SWill Hawkins ===== ========= 442369e526SWill Hawkins 452369e526SWill HawkinsFor example, `u32` is a type whose valid values are all the 32-bit unsigned 462369e526SWill Hawkinsnumbers and `s16` is a types whose valid values are all the 16-bit signed 472369e526SWill Hawkinsnumbers. 482369e526SWill Hawkins 492369e526SWill HawkinsFunctions 502369e526SWill Hawkins--------- 512369e526SWill Hawkins* `htobe16`: Takes an unsigned 16-bit number in host-endian format and 522369e526SWill Hawkins returns the equivalent number as an unsigned 16-bit number in big-endian 532369e526SWill Hawkins format. 542369e526SWill Hawkins* `htobe32`: Takes an unsigned 32-bit number in host-endian format and 552369e526SWill Hawkins returns the equivalent number as an unsigned 32-bit number in big-endian 562369e526SWill Hawkins format. 572369e526SWill Hawkins* `htobe64`: Takes an unsigned 64-bit number in host-endian format and 582369e526SWill Hawkins returns the equivalent number as an unsigned 64-bit number in big-endian 592369e526SWill Hawkins format. 602369e526SWill Hawkins* `htole16`: Takes an unsigned 16-bit number in host-endian format and 612369e526SWill Hawkins returns the equivalent number as an unsigned 16-bit number in little-endian 622369e526SWill Hawkins format. 632369e526SWill Hawkins* `htole32`: Takes an unsigned 32-bit number in host-endian format and 642369e526SWill Hawkins returns the equivalent number as an unsigned 32-bit number in little-endian 652369e526SWill Hawkins format. 662369e526SWill Hawkins* `htole64`: Takes an unsigned 64-bit number in host-endian format and 672369e526SWill Hawkins returns the equivalent number as an unsigned 64-bit number in little-endian 682369e526SWill Hawkins format. 692369e526SWill Hawkins* `bswap16`: Takes an unsigned 16-bit number in either big- or little-endian 702369e526SWill Hawkins format and returns the equivalent number with the same bit width but 712369e526SWill Hawkins opposite endianness. 722369e526SWill Hawkins* `bswap32`: Takes an unsigned 32-bit number in either big- or little-endian 732369e526SWill Hawkins format and returns the equivalent number with the same bit width but 742369e526SWill Hawkins opposite endianness. 752369e526SWill Hawkins* `bswap64`: Takes an unsigned 64-bit number in either big- or little-endian 762369e526SWill Hawkins format and returns the equivalent number with the same bit width but 772369e526SWill Hawkins opposite endianness. 784d496be9SDavid Vernet 79e546a119SWill Hawkins 80e546a119SWill HawkinsDefinitions 81e546a119SWill Hawkins----------- 82e546a119SWill Hawkins 83e546a119SWill Hawkins.. glossary:: 84e546a119SWill Hawkins 85e546a119SWill Hawkins Sign Extend 86e546a119SWill Hawkins To `sign extend an` ``X`` `-bit number, A, to a` ``Y`` `-bit number, B ,` means to 87e546a119SWill Hawkins 88e546a119SWill Hawkins #. Copy all ``X`` bits from `A` to the lower ``X`` bits of `B`. 89e546a119SWill Hawkins #. Set the value of the remaining ``Y`` - ``X`` bits of `B` to the value of 90e546a119SWill Hawkins the most-significant bit of `A`. 91e546a119SWill Hawkins 92e546a119SWill Hawkins.. admonition:: Example 93e546a119SWill Hawkins 94e546a119SWill Hawkins Sign extend an 8-bit number ``A`` to a 16-bit number ``B`` on a big-endian platform: 95e546a119SWill Hawkins :: 96e546a119SWill Hawkins 97e546a119SWill Hawkins A: 10000110 98e546a119SWill Hawkins B: 11111111 10000110 99e546a119SWill Hawkins 1004d496be9SDavid VernetInstruction encoding 1014d496be9SDavid Vernet==================== 1024d496be9SDavid Vernet 103*7d35eb1aSDavid VernetBPF has two instruction encodings: 1044d496be9SDavid Vernet 1054d496be9SDavid Vernet* the basic instruction encoding, which uses 64 bits to encode an instruction 1064d496be9SDavid Vernet* the wide instruction encoding, which appends a second 64-bit immediate (i.e., 1074d496be9SDavid Vernet constant) value after the basic instruction for a total of 128 bits. 1084d496be9SDavid Vernet 1094d496be9SDavid VernetThe fields conforming an encoded basic instruction are stored in the 1104d496be9SDavid Vernetfollowing order:: 1114d496be9SDavid Vernet 1124d496be9SDavid Vernet opcode:8 src_reg:4 dst_reg:4 offset:16 imm:32 // In little-endian BPF. 1134d496be9SDavid Vernet opcode:8 dst_reg:4 src_reg:4 offset:16 imm:32 // In big-endian BPF. 1144d496be9SDavid Vernet 1154d496be9SDavid Vernet**imm** 1164d496be9SDavid Vernet signed integer immediate value 1174d496be9SDavid Vernet 1184d496be9SDavid Vernet**offset** 1194d496be9SDavid Vernet signed integer offset used with pointer arithmetic 1204d496be9SDavid Vernet 1214d496be9SDavid Vernet**src_reg** 1224d496be9SDavid Vernet the source register number (0-10), except where otherwise specified 1234d496be9SDavid Vernet (`64-bit immediate instructions`_ reuse this field for other purposes) 1244d496be9SDavid Vernet 1254d496be9SDavid Vernet**dst_reg** 1264d496be9SDavid Vernet destination register number (0-10) 1274d496be9SDavid Vernet 1284d496be9SDavid Vernet**opcode** 1294d496be9SDavid Vernet operation to perform 1304d496be9SDavid Vernet 1314d496be9SDavid VernetNote that the contents of multi-byte fields ('imm' and 'offset') are 1324d496be9SDavid Vernetstored using big-endian byte ordering in big-endian BPF and 1334d496be9SDavid Vernetlittle-endian byte ordering in little-endian BPF. 1344d496be9SDavid Vernet 1354d496be9SDavid VernetFor example:: 1364d496be9SDavid Vernet 1374d496be9SDavid Vernet opcode offset imm assembly 1384d496be9SDavid Vernet src_reg dst_reg 1394d496be9SDavid Vernet 07 0 1 00 00 44 33 22 11 r1 += 0x11223344 // little 1404d496be9SDavid Vernet dst_reg src_reg 1414d496be9SDavid Vernet 07 1 0 00 00 11 22 33 44 r1 += 0x11223344 // big 1424d496be9SDavid Vernet 1434d496be9SDavid VernetNote that most instructions do not use all of the fields. 1444d496be9SDavid VernetUnused fields shall be cleared to zero. 1454d496be9SDavid Vernet 1464d496be9SDavid VernetAs discussed below in `64-bit immediate instructions`_, a 64-bit immediate 1474d496be9SDavid Vernetinstruction uses a 64-bit immediate value that is constructed as follows. 1484d496be9SDavid VernetThe 64 bits following the basic instruction contain a pseudo instruction 1494d496be9SDavid Vernetusing the same format but with opcode, dst_reg, src_reg, and offset all set to zero, 1504d496be9SDavid Vernetand imm containing the high 32 bits of the immediate value. 1514d496be9SDavid Vernet 1524d496be9SDavid VernetThis is depicted in the following figure:: 1534d496be9SDavid Vernet 1544d496be9SDavid Vernet basic_instruction 1554d496be9SDavid Vernet .-----------------------------. 1564d496be9SDavid Vernet | | 1574d496be9SDavid Vernet code:8 regs:8 offset:16 imm:32 unused:32 imm:32 1584d496be9SDavid Vernet | | 1594d496be9SDavid Vernet '--------------' 1604d496be9SDavid Vernet pseudo instruction 1614d496be9SDavid Vernet 1624d496be9SDavid VernetThus the 64-bit immediate value is constructed as follows: 1634d496be9SDavid Vernet 1644d496be9SDavid Vernet imm64 = (next_imm << 32) | imm 1654d496be9SDavid Vernet 1664d496be9SDavid Vernetwhere 'next_imm' refers to the imm value of the pseudo instruction 1674d496be9SDavid Vernetfollowing the basic instruction. The unused bytes in the pseudo 1684d496be9SDavid Vernetinstruction are reserved and shall be cleared to zero. 1694d496be9SDavid Vernet 1704d496be9SDavid VernetInstruction classes 1714d496be9SDavid Vernet------------------- 1724d496be9SDavid Vernet 1734d496be9SDavid VernetThe three LSB bits of the 'opcode' field store the instruction class: 1744d496be9SDavid Vernet 1754d496be9SDavid Vernet========= ===== =============================== =================================== 1764d496be9SDavid Vernetclass value description reference 1774d496be9SDavid Vernet========= ===== =============================== =================================== 1784d496be9SDavid VernetBPF_LD 0x00 non-standard load operations `Load and store instructions`_ 1794d496be9SDavid VernetBPF_LDX 0x01 load into register operations `Load and store instructions`_ 1804d496be9SDavid VernetBPF_ST 0x02 store from immediate operations `Load and store instructions`_ 1814d496be9SDavid VernetBPF_STX 0x03 store from register operations `Load and store instructions`_ 1824d496be9SDavid VernetBPF_ALU 0x04 32-bit arithmetic operations `Arithmetic and jump instructions`_ 1834d496be9SDavid VernetBPF_JMP 0x05 64-bit jump operations `Arithmetic and jump instructions`_ 1844d496be9SDavid VernetBPF_JMP32 0x06 32-bit jump operations `Arithmetic and jump instructions`_ 1854d496be9SDavid VernetBPF_ALU64 0x07 64-bit arithmetic operations `Arithmetic and jump instructions`_ 1864d496be9SDavid Vernet========= ===== =============================== =================================== 1874d496be9SDavid Vernet 1884d496be9SDavid VernetArithmetic and jump instructions 1894d496be9SDavid Vernet================================ 1904d496be9SDavid Vernet 1914d496be9SDavid VernetFor arithmetic and jump instructions (``BPF_ALU``, ``BPF_ALU64``, ``BPF_JMP`` and 1924d496be9SDavid Vernet``BPF_JMP32``), the 8-bit 'opcode' field is divided into three parts: 1934d496be9SDavid Vernet 1944d496be9SDavid Vernet============== ====== ================= 1954d496be9SDavid Vernet4 bits (MSB) 1 bit 3 bits (LSB) 1964d496be9SDavid Vernet============== ====== ================= 1974d496be9SDavid Vernetcode source instruction class 1984d496be9SDavid Vernet============== ====== ================= 1994d496be9SDavid Vernet 2004d496be9SDavid Vernet**code** 2014d496be9SDavid Vernet the operation code, whose meaning varies by instruction class 2024d496be9SDavid Vernet 2034d496be9SDavid Vernet**source** 2044d496be9SDavid Vernet the source operand location, which unless otherwise specified is one of: 2054d496be9SDavid Vernet 2064d496be9SDavid Vernet ====== ===== ============================================== 2074d496be9SDavid Vernet source value description 2084d496be9SDavid Vernet ====== ===== ============================================== 2094d496be9SDavid Vernet BPF_K 0x00 use 32-bit 'imm' value as source operand 2104d496be9SDavid Vernet BPF_X 0x08 use 'src_reg' register value as source operand 2114d496be9SDavid Vernet ====== ===== ============================================== 2124d496be9SDavid Vernet 2134d496be9SDavid Vernet**instruction class** 2144d496be9SDavid Vernet the instruction class (see `Instruction classes`_) 2154d496be9SDavid Vernet 2164d496be9SDavid VernetArithmetic instructions 2174d496be9SDavid Vernet----------------------- 2184d496be9SDavid Vernet 2194d496be9SDavid Vernet``BPF_ALU`` uses 32-bit wide operands while ``BPF_ALU64`` uses 64-bit wide operands for 2204d496be9SDavid Vernetotherwise identical operations. 2214d496be9SDavid VernetThe 'code' field encodes the operation as below, where 'src' and 'dst' refer 2224d496be9SDavid Vernetto the values of the source and destination registers, respectively. 2234d496be9SDavid Vernet 224fb213ecbSYonghong Song========= ===== ======= ========================================================== 225245d4c40SYonghong Songcode value offset description 226fb213ecbSYonghong Song========= ===== ======= ========================================================== 227245d4c40SYonghong SongBPF_ADD 0x00 0 dst += src 228245d4c40SYonghong SongBPF_SUB 0x10 0 dst -= src 229245d4c40SYonghong SongBPF_MUL 0x20 0 dst \*= src 230245d4c40SYonghong SongBPF_DIV 0x30 0 dst = (src != 0) ? (dst / src) : 0 231245d4c40SYonghong SongBPF_SDIV 0x30 1 dst = (src != 0) ? (dst s/ src) : 0 232245d4c40SYonghong SongBPF_OR 0x40 0 dst \|= src 233245d4c40SYonghong SongBPF_AND 0x50 0 dst &= src 234245d4c40SYonghong SongBPF_LSH 0x60 0 dst <<= (src & mask) 235245d4c40SYonghong SongBPF_RSH 0x70 0 dst >>= (src & mask) 236245d4c40SYonghong SongBPF_NEG 0x80 0 dst = -dst 237245d4c40SYonghong SongBPF_MOD 0x90 0 dst = (src != 0) ? (dst % src) : dst 238245d4c40SYonghong SongBPF_SMOD 0x90 1 dst = (src != 0) ? (dst s% src) : dst 239245d4c40SYonghong SongBPF_XOR 0xa0 0 dst ^= src 240245d4c40SYonghong SongBPF_MOV 0xb0 0 dst = src 241245d4c40SYonghong SongBPF_MOVSX 0xb0 8/16/32 dst = (s8,s16,s32)src 242e546a119SWill HawkinsBPF_ARSH 0xc0 0 :term:`sign extending<Sign Extend>` dst >>= (src & mask) 243245d4c40SYonghong SongBPF_END 0xd0 0 byte swap operations (see `Byte swap instructions`_ below) 244fb213ecbSYonghong Song========= ===== ======= ========================================================== 2454d496be9SDavid Vernet 2464d496be9SDavid VernetUnderflow and overflow are allowed during arithmetic operations, meaning 247*7d35eb1aSDavid Vernetthe 64-bit or 32-bit value will wrap. If BPF program execution would 2484d496be9SDavid Vernetresult in division by zero, the destination register is instead set to zero. 2494d496be9SDavid VernetIf execution would result in modulo by zero, for ``BPF_ALU64`` the value of 2504d496be9SDavid Vernetthe destination register is unchanged whereas for ``BPF_ALU`` the upper 2514d496be9SDavid Vernet32 bits of the destination register are zeroed. 2524d496be9SDavid Vernet 2534d496be9SDavid Vernet``BPF_ADD | BPF_X | BPF_ALU`` means:: 2544d496be9SDavid Vernet 2554d496be9SDavid Vernet dst = (u32) ((u32) dst + (u32) src) 2564d496be9SDavid Vernet 2574d496be9SDavid Vernetwhere '(u32)' indicates that the upper 32 bits are zeroed. 2584d496be9SDavid Vernet 2594d496be9SDavid Vernet``BPF_ADD | BPF_X | BPF_ALU64`` means:: 2604d496be9SDavid Vernet 2614d496be9SDavid Vernet dst = dst + src 2624d496be9SDavid Vernet 2634d496be9SDavid Vernet``BPF_XOR | BPF_K | BPF_ALU`` means:: 2644d496be9SDavid Vernet 2654d496be9SDavid Vernet dst = (u32) dst ^ (u32) imm32 2664d496be9SDavid Vernet 2674d496be9SDavid Vernet``BPF_XOR | BPF_K | BPF_ALU64`` means:: 2684d496be9SDavid Vernet 2694d496be9SDavid Vernet dst = dst ^ imm32 2704d496be9SDavid Vernet 271ee932bf9SYonghong SongNote that most instructions have instruction offset of 0. Only three instructions 272ee932bf9SYonghong Song(``BPF_SDIV``, ``BPF_SMOD``, ``BPF_MOVSX``) have a non-zero offset. 273245d4c40SYonghong Song 274e546a119SWill HawkinsThe division and modulo operations support both unsigned and signed flavors. 275245d4c40SYonghong Song 276ee932bf9SYonghong SongFor unsigned operations (``BPF_DIV`` and ``BPF_MOD``), for ``BPF_ALU``, 277ee932bf9SYonghong Song'imm' is interpreted as a 32-bit unsigned value. For ``BPF_ALU64``, 278e546a119SWill Hawkins'imm' is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then 279e546a119SWill Hawkinsinterpreted as a 64-bit unsigned value. 280ee932bf9SYonghong Song 281ee932bf9SYonghong SongFor signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``, 282ee932bf9SYonghong Song'imm' is interpreted as a 32-bit signed value. For ``BPF_ALU64``, 'imm' 283e546a119SWill Hawkinsis first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then 284e546a119SWill Hawkinsinterpreted as a 64-bit signed value. 285ee932bf9SYonghong Song 286ee932bf9SYonghong SongThe ``BPF_MOVSX`` instruction does a move operation with sign extension. 287e546a119SWill Hawkins``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32 288ee932bf9SYonghong Songbit operands, and zeroes the remaining upper 32 bits. 289e546a119SWill Hawkins``BPF_ALU64 | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit, 16-bit, and 32-bit 290ee932bf9SYonghong Songoperands into 64 bit operands. 2914d496be9SDavid Vernet 2924d496be9SDavid VernetShift operations use a mask of 0x3F (63) for 64-bit operations and 0x1F (31) 2934d496be9SDavid Vernetfor 32-bit operations. 2944d496be9SDavid Vernet 2954d496be9SDavid VernetByte swap instructions 296ee932bf9SYonghong Song---------------------- 2974d496be9SDavid Vernet 298245d4c40SYonghong SongThe byte swap instructions use instruction classes of ``BPF_ALU`` and ``BPF_ALU64`` 299245d4c40SYonghong Songand a 4-bit 'code' field of ``BPF_END``. 3004d496be9SDavid Vernet 3014d496be9SDavid VernetThe byte swap instructions operate on the destination register 3024d496be9SDavid Vernetonly and do not use a separate source register or immediate value. 3034d496be9SDavid Vernet 304ee932bf9SYonghong SongFor ``BPF_ALU``, the 1-bit source operand field in the opcode is used to 305ee932bf9SYonghong Songselect what byte order the operation converts from or to. For 306ee932bf9SYonghong Song``BPF_ALU64``, the 1-bit source operand field in the opcode is reserved 307ee932bf9SYonghong Songand must be set to 0. 3084d496be9SDavid Vernet 309245d4c40SYonghong Song========= ========= ===== ================================================= 310245d4c40SYonghong Songclass source value description 311245d4c40SYonghong Song========= ========= ===== ================================================= 312245d4c40SYonghong SongBPF_ALU BPF_TO_LE 0x00 convert between host byte order and little endian 313245d4c40SYonghong SongBPF_ALU BPF_TO_BE 0x08 convert between host byte order and big endian 314ee932bf9SYonghong SongBPF_ALU64 Reserved 0x00 do byte swap unconditionally 315245d4c40SYonghong Song========= ========= ===== ================================================= 3164d496be9SDavid Vernet 3174d496be9SDavid VernetThe 'imm' field encodes the width of the swap operations. The following widths 3184d496be9SDavid Vernetare supported: 16, 32 and 64. 3194d496be9SDavid Vernet 3204d496be9SDavid VernetExamples: 3214d496be9SDavid Vernet 3222369e526SWill Hawkins``BPF_ALU | BPF_TO_LE | BPF_END`` with imm = 16/32/64 means:: 3234d496be9SDavid Vernet 3244d496be9SDavid Vernet dst = htole16(dst) 3252369e526SWill Hawkins dst = htole32(dst) 3262369e526SWill Hawkins dst = htole64(dst) 3274d496be9SDavid Vernet 3282369e526SWill Hawkins``BPF_ALU | BPF_TO_BE | BPF_END`` with imm = 16/32/64 means:: 3294d496be9SDavid Vernet 3302369e526SWill Hawkins dst = htobe16(dst) 3312369e526SWill Hawkins dst = htobe32(dst) 3324d496be9SDavid Vernet dst = htobe64(dst) 3334d496be9SDavid Vernet 334245d4c40SYonghong Song``BPF_ALU64 | BPF_TO_LE | BPF_END`` with imm = 16/32/64 means:: 335245d4c40SYonghong Song 3362369e526SWill Hawkins dst = bswap16(dst) 3372369e526SWill Hawkins dst = bswap32(dst) 3382369e526SWill Hawkins dst = bswap64(dst) 339245d4c40SYonghong Song 3404d496be9SDavid VernetJump instructions 3414d496be9SDavid Vernet----------------- 3424d496be9SDavid Vernet 3434d496be9SDavid Vernet``BPF_JMP32`` uses 32-bit wide operands while ``BPF_JMP`` uses 64-bit wide operands for 3444d496be9SDavid Vernetotherwise identical operations. 3454d496be9SDavid VernetThe 'code' field encodes the operation as below: 3464d496be9SDavid Vernet 3474d496be9SDavid Vernet======== ===== === =========================================== ========================================= 3484d496be9SDavid Vernetcode value src description notes 3494d496be9SDavid Vernet======== ===== === =========================================== ========================================= 350245d4c40SYonghong SongBPF_JA 0x0 0x0 PC += offset BPF_JMP class 351245d4c40SYonghong SongBPF_JA 0x0 0x0 PC += imm BPF_JMP32 class 3524d496be9SDavid VernetBPF_JEQ 0x1 any PC += offset if dst == src 3534d496be9SDavid VernetBPF_JGT 0x2 any PC += offset if dst > src unsigned 3544d496be9SDavid VernetBPF_JGE 0x3 any PC += offset if dst >= src unsigned 3554d496be9SDavid VernetBPF_JSET 0x4 any PC += offset if dst & src 3564d496be9SDavid VernetBPF_JNE 0x5 any PC += offset if dst != src 3574d496be9SDavid VernetBPF_JSGT 0x6 any PC += offset if dst > src signed 3584d496be9SDavid VernetBPF_JSGE 0x7 any PC += offset if dst >= src signed 3594d496be9SDavid VernetBPF_CALL 0x8 0x0 call helper function by address see `Helper functions`_ 3602d71a90fSWill HawkinsBPF_CALL 0x8 0x1 call PC += imm see `Program-local functions`_ 3614d496be9SDavid VernetBPF_CALL 0x8 0x2 call helper function by BTF ID see `Helper functions`_ 3624d496be9SDavid VernetBPF_EXIT 0x9 0x0 return BPF_JMP only 3634d496be9SDavid VernetBPF_JLT 0xa any PC += offset if dst < src unsigned 3644d496be9SDavid VernetBPF_JLE 0xb any PC += offset if dst <= src unsigned 3654d496be9SDavid VernetBPF_JSLT 0xc any PC += offset if dst < src signed 3664d496be9SDavid VernetBPF_JSLE 0xd any PC += offset if dst <= src signed 3674d496be9SDavid Vernet======== ===== === =========================================== ========================================= 3684d496be9SDavid Vernet 369*7d35eb1aSDavid VernetThe BPF program needs to store the return value into register R0 before doing a 3704d496be9SDavid Vernet``BPF_EXIT``. 3714d496be9SDavid Vernet 3724d496be9SDavid VernetExample: 3734d496be9SDavid Vernet 3744d496be9SDavid Vernet``BPF_JSGE | BPF_X | BPF_JMP32`` (0x7e) means:: 3754d496be9SDavid Vernet 3764d496be9SDavid Vernet if (s32)dst s>= (s32)src goto +offset 3774d496be9SDavid Vernet 3784d496be9SDavid Vernetwhere 's>=' indicates a signed '>=' comparison. 3794d496be9SDavid Vernet 380245d4c40SYonghong Song``BPF_JA | BPF_K | BPF_JMP32`` (0x06) means:: 381245d4c40SYonghong Song 382245d4c40SYonghong Song gotol +imm 383245d4c40SYonghong Song 384245d4c40SYonghong Songwhere 'imm' means the branch offset comes from insn 'imm' field. 385245d4c40SYonghong Song 386ee932bf9SYonghong SongNote that there are two flavors of ``BPF_JA`` instructions. The 387ee932bf9SYonghong Song``BPF_JMP`` class permits a 16-bit jump offset specified by the 'offset' 388ee932bf9SYonghong Songfield, whereas the ``BPF_JMP32`` class permits a 32-bit jump offset 389ee932bf9SYonghong Songspecified by the 'imm' field. A > 16-bit conditional jump may be 390ee932bf9SYonghong Songconverted to a < 16-bit conditional jump plus a 32-bit unconditional 391ee932bf9SYonghong Songjump. 392245d4c40SYonghong Song 3934d496be9SDavid VernetHelper functions 3944d496be9SDavid Vernet~~~~~~~~~~~~~~~~ 3954d496be9SDavid Vernet 3964d496be9SDavid VernetHelper functions are a concept whereby BPF programs can call into a 3974d496be9SDavid Vernetset of function calls exposed by the underlying platform. 3984d496be9SDavid Vernet 3994d496be9SDavid VernetHistorically, each helper function was identified by an address 4004d496be9SDavid Vernetencoded in the imm field. The available helper functions may differ 4014d496be9SDavid Vernetfor each program type, but address values are unique across all program types. 4024d496be9SDavid Vernet 4034d496be9SDavid VernetPlatforms that support the BPF Type Format (BTF) support identifying 4044d496be9SDavid Verneta helper function by a BTF ID encoded in the imm field, where the BTF ID 4054d496be9SDavid Vernetidentifies the helper name and type. 4064d496be9SDavid Vernet 4074d496be9SDavid VernetProgram-local functions 4084d496be9SDavid Vernet~~~~~~~~~~~~~~~~~~~~~~~ 4094d496be9SDavid VernetProgram-local functions are functions exposed by the same BPF program as the 4104d496be9SDavid Vernetcaller, and are referenced by offset from the call instruction, similar to 4112d71a90fSWill Hawkins``BPF_JA``. The offset is encoded in the imm field of the call instruction. 4122d71a90fSWill HawkinsA ``BPF_EXIT`` within the program-local function will return to the caller. 4134d496be9SDavid Vernet 4144d496be9SDavid VernetLoad and store instructions 4154d496be9SDavid Vernet=========================== 4164d496be9SDavid Vernet 4174d496be9SDavid VernetFor load and store instructions (``BPF_LD``, ``BPF_LDX``, ``BPF_ST``, and ``BPF_STX``), the 4184d496be9SDavid Vernet8-bit 'opcode' field is divided as: 4194d496be9SDavid Vernet 4204d496be9SDavid Vernet============ ====== ================= 4214d496be9SDavid Vernet3 bits (MSB) 2 bits 3 bits (LSB) 4224d496be9SDavid Vernet============ ====== ================= 4234d496be9SDavid Vernetmode size instruction class 4244d496be9SDavid Vernet============ ====== ================= 4254d496be9SDavid Vernet 4264d496be9SDavid VernetThe mode modifier is one of: 4274d496be9SDavid Vernet 4284d496be9SDavid Vernet ============= ===== ==================================== ============= 4294d496be9SDavid Vernet mode modifier value description reference 4304d496be9SDavid Vernet ============= ===== ==================================== ============= 4314d496be9SDavid Vernet BPF_IMM 0x00 64-bit immediate instructions `64-bit immediate instructions`_ 4324d496be9SDavid Vernet BPF_ABS 0x20 legacy BPF packet access (absolute) `Legacy BPF Packet access instructions`_ 4334d496be9SDavid Vernet BPF_IND 0x40 legacy BPF packet access (indirect) `Legacy BPF Packet access instructions`_ 4344d496be9SDavid Vernet BPF_MEM 0x60 regular load and store operations `Regular load and store operations`_ 435245d4c40SYonghong Song BPF_MEMSX 0x80 sign-extension load operations `Sign-extension load operations`_ 4364d496be9SDavid Vernet BPF_ATOMIC 0xc0 atomic operations `Atomic operations`_ 4374d496be9SDavid Vernet ============= ===== ==================================== ============= 4384d496be9SDavid Vernet 4394d496be9SDavid VernetThe size modifier is one of: 4404d496be9SDavid Vernet 4414d496be9SDavid Vernet ============= ===== ===================== 4424d496be9SDavid Vernet size modifier value description 4434d496be9SDavid Vernet ============= ===== ===================== 4444d496be9SDavid Vernet BPF_W 0x00 word (4 bytes) 4454d496be9SDavid Vernet BPF_H 0x08 half word (2 bytes) 4464d496be9SDavid Vernet BPF_B 0x10 byte 4474d496be9SDavid Vernet BPF_DW 0x18 double word (8 bytes) 4484d496be9SDavid Vernet ============= ===== ===================== 4494d496be9SDavid Vernet 4504d496be9SDavid VernetRegular load and store operations 4514d496be9SDavid Vernet--------------------------------- 4524d496be9SDavid Vernet 4534d496be9SDavid VernetThe ``BPF_MEM`` mode modifier is used to encode regular load and store 4544d496be9SDavid Vernetinstructions that transfer data between a register and memory. 4554d496be9SDavid Vernet 4564d496be9SDavid Vernet``BPF_MEM | <size> | BPF_STX`` means:: 4574d496be9SDavid Vernet 4584d496be9SDavid Vernet *(size *) (dst + offset) = src 4594d496be9SDavid Vernet 4604d496be9SDavid Vernet``BPF_MEM | <size> | BPF_ST`` means:: 4614d496be9SDavid Vernet 4624d496be9SDavid Vernet *(size *) (dst + offset) = imm32 4634d496be9SDavid Vernet 4644d496be9SDavid Vernet``BPF_MEM | <size> | BPF_LDX`` means:: 4654d496be9SDavid Vernet 466245d4c40SYonghong Song dst = *(unsigned size *) (src + offset) 4674d496be9SDavid Vernet 468245d4c40SYonghong SongWhere size is one of: ``BPF_B``, ``BPF_H``, ``BPF_W``, or ``BPF_DW`` and 469ee932bf9SYonghong Song'unsigned size' is one of u8, u16, u32 or u64. 470245d4c40SYonghong Song 471fb213ecbSYonghong SongSign-extension load operations 472fb213ecbSYonghong Song------------------------------ 473fb213ecbSYonghong Song 474e546a119SWill HawkinsThe ``BPF_MEMSX`` mode modifier is used to encode :term:`sign-extension<Sign Extend>` load 475245d4c40SYonghong Songinstructions that transfer data between a register and memory. 476245d4c40SYonghong Song 477245d4c40SYonghong Song``BPF_MEMSX | <size> | BPF_LDX`` means:: 478245d4c40SYonghong Song 479245d4c40SYonghong Song dst = *(signed size *) (src + offset) 480245d4c40SYonghong Song 481245d4c40SYonghong SongWhere size is one of: ``BPF_B``, ``BPF_H`` or ``BPF_W``, and 482ee932bf9SYonghong Song'signed size' is one of s8, s16 or s32. 4834d496be9SDavid Vernet 4844d496be9SDavid VernetAtomic operations 4854d496be9SDavid Vernet----------------- 4864d496be9SDavid Vernet 4874d496be9SDavid VernetAtomic operations are operations that operate on memory and can not be 4884d496be9SDavid Vernetinterrupted or corrupted by other access to the same memory region 489*7d35eb1aSDavid Vernetby other BPF programs or means outside of this specification. 4904d496be9SDavid Vernet 491*7d35eb1aSDavid VernetAll atomic operations supported by BPF are encoded as store operations 4924d496be9SDavid Vernetthat use the ``BPF_ATOMIC`` mode modifier as follows: 4934d496be9SDavid Vernet 4944d496be9SDavid Vernet* ``BPF_ATOMIC | BPF_W | BPF_STX`` for 32-bit operations 4954d496be9SDavid Vernet* ``BPF_ATOMIC | BPF_DW | BPF_STX`` for 64-bit operations 4964d496be9SDavid Vernet* 8-bit and 16-bit wide atomic operations are not supported. 4974d496be9SDavid Vernet 4984d496be9SDavid VernetThe 'imm' field is used to encode the actual atomic operation. 4994d496be9SDavid VernetSimple atomic operation use a subset of the values defined to encode 5004d496be9SDavid Vernetarithmetic operations in the 'imm' field to encode the atomic operation: 5014d496be9SDavid Vernet 5024d496be9SDavid Vernet======== ===== =========== 5034d496be9SDavid Vernetimm value description 5044d496be9SDavid Vernet======== ===== =========== 5054d496be9SDavid VernetBPF_ADD 0x00 atomic add 5064d496be9SDavid VernetBPF_OR 0x40 atomic or 5074d496be9SDavid VernetBPF_AND 0x50 atomic and 5084d496be9SDavid VernetBPF_XOR 0xa0 atomic xor 5094d496be9SDavid Vernet======== ===== =========== 5104d496be9SDavid Vernet 5114d496be9SDavid Vernet 5124d496be9SDavid Vernet``BPF_ATOMIC | BPF_W | BPF_STX`` with 'imm' = BPF_ADD means:: 5134d496be9SDavid Vernet 5144d496be9SDavid Vernet *(u32 *)(dst + offset) += src 5154d496be9SDavid Vernet 5164d496be9SDavid Vernet``BPF_ATOMIC | BPF_DW | BPF_STX`` with 'imm' = BPF ADD means:: 5174d496be9SDavid Vernet 5184d496be9SDavid Vernet *(u64 *)(dst + offset) += src 5194d496be9SDavid Vernet 5204d496be9SDavid VernetIn addition to the simple atomic operations, there also is a modifier and 5214d496be9SDavid Vernettwo complex atomic operations: 5224d496be9SDavid Vernet 5234d496be9SDavid Vernet=========== ================ =========================== 5244d496be9SDavid Vernetimm value description 5254d496be9SDavid Vernet=========== ================ =========================== 5264d496be9SDavid VernetBPF_FETCH 0x01 modifier: return old value 5274d496be9SDavid VernetBPF_XCHG 0xe0 | BPF_FETCH atomic exchange 5284d496be9SDavid VernetBPF_CMPXCHG 0xf0 | BPF_FETCH atomic compare and exchange 5294d496be9SDavid Vernet=========== ================ =========================== 5304d496be9SDavid Vernet 5314d496be9SDavid VernetThe ``BPF_FETCH`` modifier is optional for simple atomic operations, and 5324d496be9SDavid Vernetalways set for the complex atomic operations. If the ``BPF_FETCH`` flag 5334d496be9SDavid Vernetis set, then the operation also overwrites ``src`` with the value that 5344d496be9SDavid Vernetwas in memory before it was modified. 5354d496be9SDavid Vernet 5364d496be9SDavid VernetThe ``BPF_XCHG`` operation atomically exchanges ``src`` with the value 5374d496be9SDavid Vernetaddressed by ``dst + offset``. 5384d496be9SDavid Vernet 5394d496be9SDavid VernetThe ``BPF_CMPXCHG`` operation atomically compares the value addressed by 5404d496be9SDavid Vernet``dst + offset`` with ``R0``. If they match, the value addressed by 5414d496be9SDavid Vernet``dst + offset`` is replaced with ``src``. In either case, the 5424d496be9SDavid Vernetvalue that was at ``dst + offset`` before the operation is zero-extended 5434d496be9SDavid Vernetand loaded back to ``R0``. 5444d496be9SDavid Vernet 5454d496be9SDavid Vernet64-bit immediate instructions 5464d496be9SDavid Vernet----------------------------- 5474d496be9SDavid Vernet 5484d496be9SDavid VernetInstructions with the ``BPF_IMM`` 'mode' modifier use the wide instruction 5494d496be9SDavid Vernetencoding defined in `Instruction encoding`_, and use the 'src' field of the 5504d496be9SDavid Vernetbasic instruction to hold an opcode subtype. 5514d496be9SDavid Vernet 5524d496be9SDavid VernetThe following table defines a set of ``BPF_IMM | BPF_DW | BPF_LD`` instructions 5534d496be9SDavid Vernetwith opcode subtypes in the 'src' field, using new terms such as "map" 5544d496be9SDavid Vernetdefined further below: 5554d496be9SDavid Vernet 5564d496be9SDavid Vernet========================= ====== === ========================================= =========== ============== 5574d496be9SDavid Vernetopcode construction opcode src pseudocode imm type dst type 5584d496be9SDavid Vernet========================= ====== === ========================================= =========== ============== 5594d496be9SDavid VernetBPF_IMM | BPF_DW | BPF_LD 0x18 0x0 dst = imm64 integer integer 5604d496be9SDavid VernetBPF_IMM | BPF_DW | BPF_LD 0x18 0x1 dst = map_by_fd(imm) map fd map 5614d496be9SDavid VernetBPF_IMM | BPF_DW | BPF_LD 0x18 0x2 dst = map_val(map_by_fd(imm)) + next_imm map fd data pointer 5624d496be9SDavid VernetBPF_IMM | BPF_DW | BPF_LD 0x18 0x3 dst = var_addr(imm) variable id data pointer 5634d496be9SDavid VernetBPF_IMM | BPF_DW | BPF_LD 0x18 0x4 dst = code_addr(imm) integer code pointer 5644d496be9SDavid VernetBPF_IMM | BPF_DW | BPF_LD 0x18 0x5 dst = map_by_idx(imm) map index map 5654d496be9SDavid VernetBPF_IMM | BPF_DW | BPF_LD 0x18 0x6 dst = map_val(map_by_idx(imm)) + next_imm map index data pointer 5664d496be9SDavid Vernet========================= ====== === ========================================= =========== ============== 5674d496be9SDavid Vernet 5684d496be9SDavid Vernetwhere 5694d496be9SDavid Vernet 5704d496be9SDavid Vernet* map_by_fd(imm) means to convert a 32-bit file descriptor into an address of a map (see `Maps`_) 5714d496be9SDavid Vernet* map_by_idx(imm) means to convert a 32-bit index into an address of a map 5724d496be9SDavid Vernet* map_val(map) gets the address of the first value in a given map 5734d496be9SDavid Vernet* var_addr(imm) gets the address of a platform variable (see `Platform Variables`_) with a given id 5744d496be9SDavid Vernet* code_addr(imm) gets the address of the instruction at a specified relative offset in number of (64-bit) instructions 5754d496be9SDavid Vernet* the 'imm type' can be used by disassemblers for display 5764d496be9SDavid Vernet* the 'dst type' can be used for verification and JIT compilation purposes 5774d496be9SDavid Vernet 5784d496be9SDavid VernetMaps 5794d496be9SDavid Vernet~~~~ 5804d496be9SDavid Vernet 581*7d35eb1aSDavid VernetMaps are shared memory regions accessible by BPF programs on some platforms. 5824d496be9SDavid VernetA map can have various semantics as defined in a separate document, and may or 5834d496be9SDavid Vernetmay not have a single contiguous memory region, but the 'map_val(map)' is 5844d496be9SDavid Vernetcurrently only defined for maps that do have a single contiguous memory region. 5854d496be9SDavid Vernet 5864d496be9SDavid VernetEach map can have a file descriptor (fd) if supported by the platform, where 5874d496be9SDavid Vernet'map_by_fd(imm)' means to get the map with the specified file descriptor. Each 5884d496be9SDavid VernetBPF program can also be defined to use a set of maps associated with the 5894d496be9SDavid Vernetprogram at load time, and 'map_by_idx(imm)' means to get the map with the given 5904d496be9SDavid Vernetindex in the set associated with the BPF program containing the instruction. 5914d496be9SDavid Vernet 5924d496be9SDavid VernetPlatform Variables 5934d496be9SDavid Vernet~~~~~~~~~~~~~~~~~~ 5944d496be9SDavid Vernet 5954d496be9SDavid VernetPlatform variables are memory regions, identified by integer ids, exposed by 5964d496be9SDavid Vernetthe runtime and accessible by BPF programs on some platforms. The 5974d496be9SDavid Vernet'var_addr(imm)' operation means to get the address of the memory region 5984d496be9SDavid Vernetidentified by the given id. 5994d496be9SDavid Vernet 6004d496be9SDavid VernetLegacy BPF Packet access instructions 6014d496be9SDavid Vernet------------------------------------- 6024d496be9SDavid Vernet 603*7d35eb1aSDavid VernetBPF previously introduced special instructions for access to packet data that were 6044d496be9SDavid Vernetcarried over from classic BPF. However, these instructions are 6054d496be9SDavid Vernetdeprecated and should no longer be used. 606