summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-10-28 12:15:53 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-10-28 12:15:53 +0000
commit9f8c26ecdd72ddec66a4dd640afde41b7a7798b1 (patch)
tree9e5d655cae40783454655c8eae15a135f8bf06ad
parentdb023f7c36ddda7f6a5d730f733a213c0b7e17ca (diff)
parent58b6bd524b0bb1ed84fc5418c7ca671c30585d43 (diff)
downloadapf-9f8c26ecdd72ddec66a4dd640afde41b7a7798b1.tar.gz
Snap for 11018792 from 58b6bd524b0bb1ed84fc5418c7ca671c30585d43 to mainline-media-releaseaml_med_341312300aml_med_341312020android14-mainline-media-release
Change-Id: I10834fa341e14828ce1c408cb43d360e9c0c8e4b
-rw-r--r--Android.bp2
-rw-r--r--apf_run.c50
-rw-r--r--disassembler.c36
-rw-r--r--v5/apf.h4
-rw-r--r--v5/test_buf_allocator.c13
-rw-r--r--v5/test_buf_allocator.h29
6 files changed, 120 insertions, 14 deletions
diff --git a/Android.bp b/Android.bp
index 742f49c..a1abee1 100644
--- a/Android.bp
+++ b/Android.bp
@@ -68,6 +68,8 @@ cc_binary_host {
"apf_run.c",
"apf_interpreter.c",
"disassembler.c",
+ "v5/apf_interpreter.c",
+ "v5/test_buf_allocator.c"
],
cflags: [
"-DAPF_TRACE_HOOK=apf_trace_hook",
diff --git a/apf_run.c b/apf_run.c
index 116c5ff..d40172f 100644
--- a/apf_run.c
+++ b/apf_run.c
@@ -29,6 +29,8 @@
#include "disassembler.h"
#include "apf_interpreter.h"
+#include "v5/apf_interpreter.h"
+#include "v5/test_buf_allocator.h"
#define __unused __attribute__((unused))
@@ -78,6 +80,7 @@ enum {
OPT_DATA,
OPT_AGE,
OPT_TRACE,
+ OPT_V6,
};
const struct option long_options[] = {{"program", 1, NULL, OPT_PROGRAM},
@@ -86,6 +89,7 @@ const struct option long_options[] = {{"program", 1, NULL, OPT_PROGRAM},
{"data", 1, NULL, OPT_DATA},
{"age", 1, NULL, OPT_AGE},
{"trace", 0, NULL, OPT_TRACE},
+ {"v6", 0, NULL, OPT_V6},
{"help", 0, NULL, 'h'},
{"cnt", 0, NULL, 'c'},
{NULL, 0, NULL, 0}};
@@ -156,16 +160,28 @@ void maybe_print_tracing_header() {
}
+void print_transmitted_packet() {
+ printf("transmitted packet: ");
+ print_hex(apf_test_tx_packet, (int) apf_test_tx_packet_len);
+ printf("\n");
+}
+
// Process packet through APF filter
-void packet_handler(uint8_t* program, uint32_t program_len, uint32_t ram_len,
- const char* pkt, uint32_t filter_age) {
+void packet_handler(int use_apf_v6_interpreter, uint8_t* program,
+ uint32_t program_len, uint32_t ram_len, const char* pkt, uint32_t filter_age) {
uint8_t* packet;
uint32_t packet_len = parse_hex(pkt, &packet);
maybe_print_tracing_header();
- int ret = accept_packet(program, program_len, ram_len, packet, packet_len,
+ int ret;
+ if (use_apf_v6_interpreter) {
+ ret = apf_run(program, program_len, ram_len, packet, packet_len,
filter_age);
+ } else {
+ ret = accept_packet(program, program_len, ram_len, packet, packet_len,
+ filter_age);
+ }
printf("Packet %sed\n", ret ? "pass" : "dropp");
free(packet);
@@ -185,7 +201,8 @@ void apf_trace_hook(uint32_t pc, const uint32_t* regs, const uint8_t* program, u
}
// Process pcap file through APF filter and generate output files
-void file_handler(uint8_t* program, uint32_t program_len, uint32_t ram_len, const char* filename,
+void file_handler(int use_apf_v6_interpreter, uint8_t* program,
+ uint32_t program_len, uint32_t ram_len, const char* filename,
uint32_t filter_age) {
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *pcap;
@@ -215,8 +232,14 @@ void file_handler(uint8_t* program, uint32_t program_len, uint32_t ram_len, cons
while ((apf_packet = pcap_next(pcap, &apf_header)) != NULL) {
maybe_print_tracing_header();
- int result = accept_packet(program, program_len, ram_len, apf_packet,
- apf_header.len, filter_age);
+ int result;
+ if (use_apf_v6_interpreter) {
+ result = apf_run(program, program_len, ram_len, apf_packet,
+ apf_header.len, filter_age);
+ } else {
+ result = accept_packet(program, program_len, ram_len, apf_packet,
+ apf_header.len, filter_age);
+ }
if (!result){
drop++;
@@ -244,6 +267,7 @@ void print_usage(char* cmd) {
" --data Data memory contents, in hex.\n"
" --age Age of program in seconds (default: 0).\n"
" --trace Enable APF interpreter debug tracing\n"
+ " --v6 Use APF v6\n"
" -c, --cnt Print the APF counters\n"
" -h, --help Show this message.\n",
basename(cmd));
@@ -258,6 +282,7 @@ int main(int argc, char* argv[]) {
uint32_t data_len = 0;
uint32_t filter_age = 0;
int print_counter_enabled = 0;
+ int use_apf_v6_interpreter = 0;
int opt;
char *endptr;
@@ -315,6 +340,9 @@ int main(int argc, char* argv[]) {
case OPT_TRACE:
tracing_enabled = 1;
break;
+ case OPT_V6:
+ use_apf_v6_interpreter = 1;
+ break;
case 'h':
print_usage(argv[0]);
exit(0);
@@ -349,9 +377,11 @@ int main(int argc, char* argv[]) {
uint32_t ram_len = program_len + data_len;
if (filename)
- file_handler(program, program_len, ram_len, filename, filter_age);
+ file_handler(use_apf_v6_interpreter, program, program_len, ram_len,
+ filename, filter_age);
else
- packet_handler(program, program_len, ram_len, packet, filter_age);
+ packet_handler(use_apf_v6_interpreter, program, program_len, ram_len,
+ packet, filter_age);
if (data_len) {
printf("Data: ");
@@ -363,6 +393,10 @@ int main(int argc, char* argv[]) {
}
}
+ if (use_apf_v6_interpreter && apf_test_tx_packet_len != 0) {
+ print_transmitted_packet();
+ }
+
free(program);
return 0;
}
diff --git a/disassembler.c b/disassembler.c
index 091ff9e..6db8d7d 100644
--- a/disassembler.c
+++ b/disassembler.c
@@ -55,6 +55,7 @@ static const char* opcode_names [] = {
[JNEBS_OPCODE] = "jnebs",
[LDDW_OPCODE] = "lddw",
[STDW_OPCODE] = "stdw",
+ [WRITE_OPCODE] = "write",
};
static int print_jump_target(uint32_t target, uint32_t program_len,
@@ -366,6 +367,20 @@ uint32_t apf_disassemble(const uint8_t* program, uint32_t program_len,
ASSERT_RET_INBOUND(ret);
offset += ret;
break;
+ case EWRITE1_EXT_OPCODE:
+ case EWRITE2_EXT_OPCODE:
+ case EWRITE4_EXT_OPCODE: {
+ ret = print_opcode("write", output_buffer,
+ output_buffer_len, offset);
+ ASSERT_RET_INBOUND(ret);
+ offset += ret;
+ ret = snprintf(output_buffer + offset,
+ output_buffer_len - offset, "r%d, %d",
+ reg_num, 1 << (imm - EWRITE1_EXT_OPCODE));
+ ASSERT_RET_INBOUND(ret);
+ offset += ret;
+ break;
+ }
default:
ret = snprintf(output_buffer + offset,
output_buffer_len - offset, "unknown_ext %u",
@@ -400,7 +415,28 @@ uint32_t apf_disassemble(const uint8_t* program, uint32_t program_len,
offset += ret;
}
break;
+ case WRITE_OPCODE: {
+ ret = PRINT_OPCODE();
+ ASSERT_RET_INBOUND(ret);
+ offset += ret;
+ uint32_t write_len = 1 << (len_field - 1);
+ if (write_len > 0) {
+ ret = snprintf(output_buffer + offset,
+ output_buffer_len - offset, "0x");
+ ASSERT_RET_INBOUND(ret);
+ offset += ret;
+ }
+ for (uint32_t i = 0; i < write_len; ++i) {
+ uint8_t byte =
+ (uint8_t) ((imm >> (write_len - 1 - i) * 8) & 0xff);
+ ret = snprintf(output_buffer + offset,
+ output_buffer_len - offset, "%02x", byte);
+ ASSERT_RET_INBOUND(ret);
+ offset += ret;
+ }
+ break;
+ }
// Unknown opcode
default:
ret = snprintf(output_buffer + offset, output_buffer_len - offset,
diff --git a/v5/apf.h b/v5/apf.h
index c3a7cbc..e981593 100644
--- a/v5/apf.h
+++ b/v5/apf.h
@@ -158,6 +158,7 @@
#define EXT_OPCODE 21 // Immediate value is one of *_EXT_OPCODE
#define LDDW_OPCODE 22 // Load 4 bytes from data address (register + simm): "lddw R0, [5+R1]"
#define STDW_OPCODE 23 // Store 4 bytes to data address (register + simm): "stdw R0, [5+R1]"
+#define WRITE_OPCODE 24 // Write 1, 2 or 4 bytes imm to the output buffer, e.g. "WRITE 5"
// Extended opcodes. These all have an opcode of EXT_OPCODE
// and specify the actual opcode in the immediate field.
@@ -171,6 +172,9 @@
#define MOV_EXT_OPCODE 35 // Move, e.g. "move R0,R1"
#define ALLOC_EXT_OPCODE 36 // Allocate buffer, "e.g. ALLOC R0"
#define TRANS_EXT_OPCODE 37 // Transmit buffer, "e.g. TRANS R0"
+#define EWRITE1_EXT_OPCODE 38 // Write 1 byte from register to the output buffer, e.g. "EWRITE1 R0"
+#define EWRITE2_EXT_OPCODE 39 // Write 2 bytes from register to the output buffer, e.g. "EWRITE2 R0"
+#define EWRITE4_EXT_OPCODE 40 // Write 4 bytes from register to the output buffer, e.g. "EWRITE4 R0"
#define EXTRACT_OPCODE(i) (((i) >> 3) & 31)
#define EXTRACT_REGISTER(i) ((i) & 1)
diff --git a/v5/test_buf_allocator.c b/v5/test_buf_allocator.c
index 2395e3e..1358063 100644
--- a/v5/test_buf_allocator.c
+++ b/v5/test_buf_allocator.c
@@ -17,11 +17,11 @@
#include <string.h>
#include "apf_interpreter.h"
+#include "test_buf_allocator.h"
-#define BUFFER_SIZE 1500
-
-uint8_t apf_test_buffer[BUFFER_SIZE];
-uint8_t apf_test_tx_packet[BUFFER_SIZE];
+uint8_t apf_test_buffer[APF_TX_BUFFER_SIZE];
+uint8_t apf_test_tx_packet[APF_TX_BUFFER_SIZE];
+uint32_t apf_test_tx_packet_len;
uint8_t apf_test_tx_dscp;
/**
@@ -37,10 +37,10 @@ uint32_t apf_version() {
* Clean up the apf_test_buffer and return the pointer to beginning of the buffer region.
*/
uint8_t* apf_allocate_buffer(uint32_t size) {
- if (size > BUFFER_SIZE) {
+ if (size > APF_TX_BUFFER_SIZE) {
return NULL;
}
- memset(apf_test_buffer, 0, BUFFER_SIZE * sizeof(apf_test_buffer[0]));
+ memset(apf_test_buffer, 0, APF_TX_BUFFER_SIZE * sizeof(apf_test_buffer[0]));
return apf_test_buffer;
}
@@ -50,6 +50,7 @@ uint8_t* apf_allocate_buffer(uint32_t size) {
* Copy the content of allocated buffer to the apf_test_tx_packet region.
*/
void apf_transmit_buffer(uint8_t* ptr, uint32_t len, uint8_t dscp) {
+ apf_test_tx_packet_len = len;
apf_test_tx_dscp = dscp;
memcpy(apf_test_tx_packet, ptr, len);
}
diff --git a/v5/test_buf_allocator.h b/v5/test_buf_allocator.h
new file mode 100644
index 0000000..3916cf8
--- /dev/null
+++ b/v5/test_buf_allocator.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2023, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdint.h>
+
+#ifndef TEST_BUF_ALLOCATOR
+#define TEST_BUF_ALLOCATOR
+
+#define APF_TX_BUFFER_SIZE 1500
+
+extern uint8_t apf_test_buffer[APF_TX_BUFFER_SIZE];
+extern uint8_t apf_test_tx_packet[APF_TX_BUFFER_SIZE];
+extern uint32_t apf_test_tx_packet_len;
+extern uint8_t apf_test_tx_dscp;
+
+#endif // TEST_BUF_ALLOCATOR