BPF.Instruction (BPF v0.1.1)

View Source

Classic BPF instruction definitions and encoding.

BPF has a simple instruction set with:

  • A: 32-bit accumulator
  • X: 32-bit index register
  • M[0-15]: 16 x 32-bit scratch memory slots

Each instruction is represented as a tuple. Examples:

  • {:ld, :b, [:k, 0]} - load byte at offset 0 into A
  • {:add, 4} - add 4 to A
  • {:jmp, :jeq, :k, 5, 1, 0} - if A == 5, skip 1, else skip 0
  • :txa - copy X to A
  • {:ret, :k, 0xFFFFFFFF} - return constant

Summary

Functions

Encode an instruction tuple to its binary representation.

Encode an instruction to its 8-byte binary format.

Types

t()

@type t() :: :atom | tuple()

Functions

encode(instruction)

Encode an instruction tuple to its binary representation.

Returns {code, jt, jf, k} where:

  • code: 16-bit opcode
  • jt: 8-bit jump-true offset
  • jf: 8-bit jump-false offset
  • k: 32-bit constant/offset

to_binary(instruction)

Encode an instruction to its 8-byte binary format.