Skip to content

1.1 BPF Virtual Machine OPCODES

Mark Bednarczyk edited this page Nov 11, 2024 · 1 revision

BPF Virtual Machine Instruction Set

The JNetRuntime BPF VM supports both standard Berkeley Packet Filter instructions and extended operations for advanced packet processing capabilities.

Instruction Categories

1. Load Operations (LOAD)

Load operations move data into the accumulator (A) or index register (X) from various sources:

  • Immediate values
  • Packet data (absolute or indexed addressing)
  • Packet length
  • Memory locations

2. Store Operations (STORE)

Store operations move data from registers to memory locations.

3. Arithmetic and Logical Operations (ALU)

ALU operations perform calculations on the accumulator using either:

  • Immediate values
  • X register contents

4. Jump Operations (JUMP)

Jump operations control program flow through:

  • Unconditional jumps
  • Conditional jumps based on accumulator value

5. Return Operations (RET)

Return operations terminate program execution and return a value.

6. Miscellaneous Operations (MISC)

Utility operations for register transfers.

7. Extended Operations (EXTENSION)

Custom extensions for specialized packet processing.

Instruction Set Reference

Load Instructions (LD/LDX)

Opcode Mnemonic Format Description
0x00 ld #k IMMEDIATE Load immediate into A
0x20 ld [k] MEMORY_ABS Load word from packet offset k
0x28 ldh [k] MEMORY_ABS Load half-word from packet offset k
0x30 ldb [k] MEMORY_ABS Load byte from packet offset k
0x40 ld [x + k] MEMORY_IND Load word from packet offset X + k
0x48 ldh [x + k] MEMORY_IND Load half-word from packet offset X + k
0x50 ldb [x + k] MEMORY_IND Load byte from packet offset X + k
0x80 ld len REG_ONLY Load packet length into A
0x60 ld M[k] MEMORY_REG Load word from memory location M[k]
0xA0 ld #([k]&0xf)<<2 MEMORY_ABS Load masked value and shift
0x01 ldx #k IMMEDIATE Load immediate into X
0x61 ldx M[k] MEMORY_REG Load word from memory into X
0x81 ldx len REG_ONLY Load packet length into X
0xA1 ldx #([k]&0xf)<<2 MEMORY_ABS Load masked value into X and shift
0xB1 ldx M[k] MEMORY_REG Load word from indirect memory into X

Store Instructions

Opcode Mnemonic Format Description
0x02 st M[k] MEMORY_REG Store A into memory M[k]
0x03 stx M[k] MEMORY_REG Store X into memory M[k]

ALU Instructions

Opcode Mnemonic Format Description
0x04 add #k IMMEDIATE A += k
0x14 sub #k IMMEDIATE A -= k
0x24 mul #k IMMEDIATE A *= k
0x34 div #k IMMEDIATE A /= k
0x44 or #k IMMEDIATE A |= k
0x54 and #k IMMEDIATE A &= k
0x64 lsh #k IMMEDIATE A <<= k
0x74 rsh #k IMMEDIATE A >>= k
0x94 mod #k IMMEDIATE A %= k
0xA4 xor #k IMMEDIATE A ^= k
0x84 neg REG_ONLY A = -A
0x0C add x REG_ONLY A += X
0x1C sub x REG_ONLY A -= X
0x2C mul x REG_ONLY A *= X
0x3C div x REG_ONLY A /= X
0x4C or x REG_ONLY A |= X
0x5C and x REG_ONLY A &= X
0x6C lsh x REG_ONLY A <<= X
0x7C rsh x REG_ONLY A >>= X
0x9C mod x REG_ONLY A %= X
0xAC xor x REG_ONLY A ^= X

Jump Instructions

Opcode Mnemonic Format Description
0x05 jmp +k JUMP_UNCOND Jump k instructions
0x15 jeq #k,jt,jf JUMP_COND Jump if A == k
0x25 jgt #k,jt,jf JUMP_COND Jump if A > k
0x35 jge #k,jt,jf JUMP_COND Jump if A >= k
0x45 jset #k,jt,jf JUMP_COND Jump if (A & k) != 0
0x1D jeq x,jt,jf JUMP_COND Jump if A == X
0x2D jgt x,jt,jf JUMP_COND Jump if A > X
0x3D jge x,jt,jf JUMP_COND Jump if A >= X
0x4D jset x,jt,jf JUMP_COND Jump if (A & X) != 0

Return Instructions

Opcode Mnemonic Format Description
0x06 ret #k IMMEDIATE Return k
0x16 ret a REG_ONLY Return A

Miscellaneous Instructions

Opcode Mnemonic Format Description
0x07 tax REG_ONLY X = A
0x87 txa REG_ONLY A = X

Extended Instructions

Opcode Mnemonic Format Description
0xE0 chk_crc EXTENDED Check packet CRC
0xE1 chk_l3_csum EXTENDED Verify Layer 3 checksum
0xE2 chk_l4_csum EXTENDED Verify Layer 4 checksum
0xE3 chk_trunc EXTENDED Check packet truncation
0xE4 chk_frame_len EXTENDED Verify frame length
0xE5 chk_proto_loc EXTENDED Check protocol location

Instruction Encoding

Each BPF instruction is encoded in 64 bits:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|    opcode     |    jt         |    jf         |     k         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                               k                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Where:

  • opcode: 8-bit operation code
  • jt: 8-bit jump target offset (true condition)
  • jf: 8-bit jump target offset (false condition)
  • k: 32-bit immediate value or offset