Update TRANS opcode in ApfGenerator for discard/transmit buffer
This commit adds update for the "TRANS" opcode to make it matches the
latest design. This opcode allows the apf_interpreter to
discard/transmit the allocated buffer.
* design doc: go/apf-v6-proposal
Bug: 293811969
Test: TH
Change-Id: Id474e500b55cb90874fed661f55d4ee64104ad94
diff --git a/src/android/net/apf/ApfGenerator.java b/src/android/net/apf/ApfGenerator.java
index e63dddb..53839ae 100644
--- a/src/android/net/apf/ApfGenerator.java
+++ b/src/android/net/apf/ApfGenerator.java
@@ -96,7 +96,12 @@
SWAP(34), // Swap, e.g. "swap R0,R1"
MOVE(35), // Move, e.g. "move R0,R1"
ALLOC(36), // Allocate buffer, "e.g. ALLOC R0"
- TRANS(37), // Transmit buffer, "e.g. TRANS R0"
+ // Transmit and deallocate the buffer (transmission can be delayed until the program
+ // terminates). R=0 means discard the buffer, R=1 means transmit the buffer.
+ // "e.g. trans"
+ // "e.g. discard"
+ TRANSMIT(37),
+ DISCARD(37),
EWRITE1(38), // Write 1 byte from register to the output buffer, e.g. "EWRITE1 R0"
EWRITE2(39), // Write 2 bytes from register to the output buffer, e.g. "EWRITE2 R0"
EWRITE4(40), // Write 4 bytes from register to the output buffer, e.g. "EWRITE4 R0"
@@ -988,14 +993,23 @@
}
/**
- * Add an instruction to the end of the program to call the apf_transmit_buffer() function.
- *
- * @param register the register value contains the packet type.
+ * Add an instruction to the end of the program to transmit the allocated buffer.
*/
- public ApfGenerator addTrans(Register register) throws IllegalInstructionException {
- requireApfVersion(5);
- Instruction instruction = new Instruction(Opcodes.EXT, register);
- instruction.addUnsignedImm(ExtendedOpcodes.TRANS.value);
+ public ApfGenerator addTransmit() throws IllegalInstructionException {
+ requireApfVersion(MIN_APF_VERSION_IN_DEV);
+ Instruction instruction = new Instruction(Opcodes.EXT, Register.R0);
+ instruction.addUnsignedImm(ExtendedOpcodes.TRANSMIT.value);
+ addInstruction(instruction);
+ return this;
+ }
+
+ /**
+ * Add an instruction to the end of the program to discard the allocated buffer.
+ */
+ public ApfGenerator addDiscard() throws IllegalInstructionException {
+ requireApfVersion(MIN_APF_VERSION_IN_DEV);
+ Instruction instruction = new Instruction(Opcodes.EXT, Register.R1);
+ instruction.addUnsignedImm(ExtendedOpcodes.DISCARD.value);
addInstruction(instruction);
return this;
}
diff --git a/tests/unit/src/android/net/apf/ApfV5Test.kt b/tests/unit/src/android/net/apf/ApfV5Test.kt
index 1c21bb0..bf32500 100644
--- a/tests/unit/src/android/net/apf/ApfV5Test.kt
+++ b/tests/unit/src/android/net/apf/ApfV5Test.kt
@@ -37,6 +37,8 @@
assertFailsWith<IllegalInstructionException> { gen.addDrop() }
assertFailsWith<IllegalInstructionException> { gen.addCountAndDrop(12) }
assertFailsWith<IllegalInstructionException> { gen.addCountAndPass(1000) }
+ assertFailsWith<IllegalInstructionException> { gen.addTransmit() }
+ assertFailsWith<IllegalInstructionException> { gen.addDiscard() }
}
@Test
@@ -85,10 +87,18 @@
assertContentEquals(arrayOf(" 0: alloc r0"), ApfJniUtils.disassembleApf(program))
gen = ApfGenerator(ApfGenerator.MIN_APF_VERSION_IN_DEV)
- gen.addTrans(ApfGenerator.Register.R1)
+ gen.addTransmit()
+ gen.addDiscard()
program = gen.generate()
- assertContentEquals(byteArrayOf(encodeInstruction(21, 1, 1), 37), program)
- assertContentEquals(arrayOf(" 0: trans r1"), ApfJniUtils.disassembleApf(program))
+ // encoding TRANSMIT/DISCARD opcode: opcode=21(EXT opcode number),
+ // imm=37(TRANSMIT/DISCARD opcode number),
+ // R=0 means discard the buffer. R=1 means transmit the buffer.
+ assertContentEquals(byteArrayOf(
+ encodeInstruction(opcode = 21, immLength = 1, register = 0), 37,
+ encodeInstruction(opcode = 21, immLength = 1, register = 1), 37,
+ ), program)
+ // TODO: add back disassembling test check after we update the apf_disassembler
+ // assertContentEquals(arrayOf(" 0: trans"), ApfJniUtils.disassembleApf(program))
gen = ApfGenerator(ApfGenerator.MIN_APF_VERSION_IN_DEV)
gen.addWrite(0x01, 1)