public class ApfGenerator extends Object
generate
to get the APF bytecode for the program.Modifier and Type | Class and Description |
---|---|
static class |
ApfGenerator.IllegalInstructionException
This exception is thrown when an attempt is made to generate an illegal instruction.
|
static class |
ApfGenerator.Register |
Modifier and Type | Field and Description |
---|---|
static String |
DROP_LABEL
Jump to this label to terminate the program and indicate the packet
should be dropped.
|
static int |
FILTER_AGE_MEMORY_SLOT
Memory slot number that is prefilled with the age of the filter in seconds.
|
static int |
FIRST_PREFILLED_MEMORY_SLOT
First memory slot containing prefilled values.
|
static int |
IPV4_HEADER_SIZE_MEMORY_SLOT
Memory slot number that is prefilled with the IPv4 header length.
|
static int |
LAST_PREFILLED_MEMORY_SLOT
Last memory slot containing prefilled values.
|
static int |
MEMORY_SLOTS
Number of memory slots available for access via APF stores to memory and loads from memory.
|
static int |
PACKET_SIZE_MEMORY_SLOT
Memory slot number that is prefilled with the size of the packet being filtered in bytes.
|
static String |
PASS_LABEL
Jump to this label to terminate the program and indicate the packet
should be passed to the AP.
|
Constructor and Description |
---|
ApfGenerator() |
Modifier and Type | Method and Description |
---|---|
ApfGenerator |
addAdd(int value)
Add an instruction to the end of the program to add
value to register R0. |
ApfGenerator |
addAddR1()
Add an instruction to the end of the program to add register R1 to register R0.
|
ApfGenerator |
addAnd(int value)
Add an instruction to the end of the program to logically and register R0 with
value . |
ApfGenerator |
addAndR1()
Add an instruction to the end of the program to logically and register R0 with register R1
and store the result back into register R0.
|
ApfGenerator |
addDiv(int value)
Add an instruction to the end of the program to divide register R0 by
value . |
ApfGenerator |
addDivR1()
Add an instruction to the end of the program to divide register R0 by register R1.
|
ApfGenerator |
addJump(String target)
Add an unconditional jump instruction to the end of the program.
|
ApfGenerator |
addJumpIfBytesNotEqual(ApfGenerator.Register register,
byte[] bytes,
String target)
Add an instruction to the end of the program to jump to
target if the bytes of the
packet at, an offset specified by register , match bytes . |
ApfGenerator |
addJumpIfR0AnyBitsSet(int value,
String target)
Add an instruction to the end of the program to jump to
target if register R0's
value has any bits set that are also set in value . |
ApfGenerator |
addJumpIfR0AnyBitsSetR1(String target)
Add an instruction to the end of the program to jump to
target if register R0's
value has any bits set that are also set in R1's value. |
ApfGenerator |
addJumpIfR0Equals(int value,
String target)
Add an instruction to the end of the program to jump to
target if register R0's
value equals value . |
ApfGenerator |
addJumpIfR0EqualsR1(String target)
Add an instruction to the end of the program to jump to
target if register R0's
value equals register R1's value. |
ApfGenerator |
addJumpIfR0GreaterThan(int value,
String target)
Add an instruction to the end of the program to jump to
target if register R0's
value is greater than value . |
ApfGenerator |
addJumpIfR0GreaterThanR1(String target)
Add an instruction to the end of the program to jump to
target if register R0's
value is greater than register R1's value. |
ApfGenerator |
addJumpIfR0LessThan(int value,
String target)
Add an instruction to the end of the program to jump to
target if register R0's
value is less than value . |
ApfGenerator |
addJumpIfR0LessThanR1(String target)
Add an instruction to the end of the program to jump to
target if register R0's
value is less than register R1's value. |
ApfGenerator |
addJumpIfR0NotEquals(int value,
String target)
Add an instruction to the end of the program to jump to
target if register R0's
value does not equal value . |
ApfGenerator |
addJumpIfR0NotEqualsR1(String target)
Add an instruction to the end of the program to jump to
target if register R0's
value does not equal register R1's value. |
ApfGenerator |
addLeftShift(int value)
Add an instruction to the end of the program to shift left register R0 by
value bits. |
ApfGenerator |
addLeftShiftR1()
Add an instruction to the end of the program to shift register R0 left by the value in
register R1.
|
ApfGenerator |
addLoad16(ApfGenerator.Register register,
int offset)
Add an instruction to the end of the program to load 16-bits at offset
offset
bytes from the begining of the packet into register . |
ApfGenerator |
addLoad16Indexed(ApfGenerator.Register register,
int offset)
Add an instruction to the end of the program to load 16-bits from the packet into
register . |
ApfGenerator |
addLoad32(ApfGenerator.Register register,
int offset)
Add an instruction to the end of the program to load 32-bits at offset
offset
bytes from the begining of the packet into register . |
ApfGenerator |
addLoad32Indexed(ApfGenerator.Register register,
int offset)
Add an instruction to the end of the program to load 32-bits from the packet into
register . |
ApfGenerator |
addLoad8(ApfGenerator.Register register,
int offset)
Add an instruction to the end of the program to load the byte at offset
offset
bytes from the begining of the packet into register . |
ApfGenerator |
addLoad8Indexed(ApfGenerator.Register register,
int offset)
Add an instruction to the end of the program to load a byte from the packet into
register . |
ApfGenerator |
addLoadFromMemory(ApfGenerator.Register register,
int slot)
Add an instruction to the end of the program to load memory slot
slot into
register . |
ApfGenerator |
addLoadImmediate(ApfGenerator.Register register,
int value)
Add an instruction to the end of the program to move
value into register . |
ApfGenerator |
addMove(ApfGenerator.Register register)
Add an instruction to the end of the program to move the value into
register from the other register. |
ApfGenerator |
addMul(int value)
Add an instruction to the end of the program to multiply register R0 by
value . |
ApfGenerator |
addMulR1()
Add an instruction to the end of the program to multiply register R0 by register R1.
|
ApfGenerator |
addNeg(ApfGenerator.Register register)
Add an instruction to the end of the program to negate
register . |
ApfGenerator |
addNot(ApfGenerator.Register register)
Add an instruction to the end of the program to logically not
register . |
ApfGenerator |
addOr(int value)
Add an instruction to the end of the program to logically or register R0 with
value . |
ApfGenerator |
addOrR1()
Add an instruction to the end of the program to logically or register R0 with register R1
and store the result back into register R0.
|
ApfGenerator |
addRightShift(int value)
Add an instruction to the end of the program to shift right register R0 by
value
bits. |
ApfGenerator |
addStoreToMemory(ApfGenerator.Register register,
int slot)
Add an instruction to the end of the program to store
register into memory slot
slot . |
ApfGenerator |
addSwap()
Add an instruction to swap the values in register R0 and register R1.
|
ApfGenerator |
defineLabel(String name)
Define a label at the current end of the program.
|
byte[] |
generate()
Generate the bytecode for the APF program.
|
int |
programLengthOverEstimate()
Returns an overestimate of the size of the generated program.
|
boolean |
setApfVersion(int version)
Set version of APF instruction set to generate instructions for.
|
public static final String DROP_LABEL
public static final String PASS_LABEL
public static final int MEMORY_SLOTS
MEMORY_SLOTS
- 1. This must be kept in sync with
the APF interpreter.public static final int IPV4_HEADER_SIZE_MEMORY_SLOT
public static final int PACKET_SIZE_MEMORY_SLOT
public static final int FILTER_AGE_MEMORY_SLOT
public static final int FIRST_PREFILLED_MEMORY_SLOT
public static final int LAST_PREFILLED_MEMORY_SLOT
public boolean setApfVersion(int version)
true
if generating for this version is supported, false
otherwise.public ApfGenerator defineLabel(String name) throws ApfGenerator.IllegalInstructionException
ApfGenerator
might be passed to a function that adds a filter like so:
load from packet compare loaded data, jump if not equal to "next_filter" load from packet compare loaded data, jump if not equal to "next_filter" jump to drop label define "next_filter" hereIn this case "next_filter" may not have any generated code associated with it.
public ApfGenerator addJump(String target)
public ApfGenerator addLoad8(ApfGenerator.Register register, int offset)
offset
bytes from the begining of the packet into register
.public ApfGenerator addLoad16(ApfGenerator.Register register, int offset)
offset
bytes from the begining of the packet into register
.public ApfGenerator addLoad32(ApfGenerator.Register register, int offset)
offset
bytes from the begining of the packet into register
.public ApfGenerator addLoad8Indexed(ApfGenerator.Register register, int offset)
register
. The offset of the loaded byte from the begining of the packet is
the sum of offset
and the value in register R1.public ApfGenerator addLoad16Indexed(ApfGenerator.Register register, int offset)
register
. The offset of the loaded 16-bits from the begining of the packet is
the sum of offset
and the value in register R1.public ApfGenerator addLoad32Indexed(ApfGenerator.Register register, int offset)
register
. The offset of the loaded 32-bits from the begining of the packet is
the sum of offset
and the value in register R1.public ApfGenerator addAdd(int value)
value
to register R0.public ApfGenerator addMul(int value)
value
.public ApfGenerator addDiv(int value)
value
.public ApfGenerator addAnd(int value)
value
.public ApfGenerator addOr(int value)
value
.public ApfGenerator addLeftShift(int value)
value
bits.public ApfGenerator addRightShift(int value)
value
bits.public ApfGenerator addAddR1()
public ApfGenerator addMulR1()
public ApfGenerator addDivR1()
public ApfGenerator addAndR1()
public ApfGenerator addOrR1()
public ApfGenerator addLeftShiftR1()
public ApfGenerator addLoadImmediate(ApfGenerator.Register register, int value)
value
into register
.public ApfGenerator addJumpIfR0Equals(int value, String target)
target
if register R0's
value equals value
.public ApfGenerator addJumpIfR0NotEquals(int value, String target)
target
if register R0's
value does not equal value
.public ApfGenerator addJumpIfR0GreaterThan(int value, String target)
target
if register R0's
value is greater than value
.public ApfGenerator addJumpIfR0LessThan(int value, String target)
target
if register R0's
value is less than value
.public ApfGenerator addJumpIfR0AnyBitsSet(int value, String target)
target
if register R0's
value has any bits set that are also set in value
.public ApfGenerator addJumpIfR0EqualsR1(String target)
target
if register R0's
value equals register R1's value.public ApfGenerator addJumpIfR0NotEqualsR1(String target)
target
if register R0's
value does not equal register R1's value.public ApfGenerator addJumpIfR0GreaterThanR1(String target)
target
if register R0's
value is greater than register R1's value.public ApfGenerator addJumpIfR0LessThanR1(String target)
target
if register R0's
value is less than register R1's value.public ApfGenerator addJumpIfR0AnyBitsSetR1(String target)
target
if register R0's
value has any bits set that are also set in R1's value.public ApfGenerator addJumpIfBytesNotEqual(ApfGenerator.Register register, byte[] bytes, String target) throws ApfGenerator.IllegalInstructionException
target
if the bytes of the
packet at, an offset specified by register
, match bytes
.public ApfGenerator addLoadFromMemory(ApfGenerator.Register register, int slot) throws ApfGenerator.IllegalInstructionException
slot
into
register
.public ApfGenerator addStoreToMemory(ApfGenerator.Register register, int slot) throws ApfGenerator.IllegalInstructionException
register
into memory slot
slot
.public ApfGenerator addNot(ApfGenerator.Register register)
register
.public ApfGenerator addNeg(ApfGenerator.Register register)
register
.public ApfGenerator addSwap()
public ApfGenerator addMove(ApfGenerator.Register register)
register
from the other register.public int programLengthOverEstimate()
generate()
may return
a program that is smaller.public byte[] generate() throws ApfGenerator.IllegalInstructionException
IllegalStateException
- if a label is referenced but not defined.ApfGenerator.IllegalInstructionException