diff options
author | Yuyang Huang <yuyanghuang@google.com> | 2023-10-11 06:02:42 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-10-11 06:02:42 +0000 |
commit | 4dc89a6e8204f877ba97dd29275a973c28c3a9bf (patch) | |
tree | 3e6569051d8c07922ff4687afee534405f4148bc | |
parent | 0e8f3161e3ef1e2b9106181025e1b5287f78cd15 (diff) | |
parent | b1b3059aa9ec57e0ab4581bf408bf6d7f901e85c (diff) | |
download | apf-4dc89a6e8204f877ba97dd29275a973c28c3a9bf.tar.gz |
Update apf_run to allow printing APF counter am: b1b3059aa9
Original change: https://android-review.googlesource.com/c/platform/hardware/google/apf/+/2780319
Change-Id: Iff22fb42ba4a761ef7e6d265603035380e0b35d8
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | apf_run.c | 75 |
1 files changed, 74 insertions, 1 deletions
@@ -32,6 +32,45 @@ #define __unused __attribute__((unused)) +// The following list must be in sync with +// https://cs.android.com/android/platform/superproject/main/+/main:packages/modules/NetworkStack/src/android/net/apf/ApfFilter.java;l=125 +static const char* counter_name [] = { + "RESERVED_OOB", + "TOTAL_PACKETS", + "PASSED_ARP", + "PASSED_DHCP", + "PASSED_IPV4", + "PASSED_IPV6_NON_ICMP", + "PASSED_IPV4_UNICAST", + "PASSED_IPV6_ICMP", + "PASSED_IPV6_UNICAST_NON_ICMP", + "PASSED_ARP_NON_IPV4", + "PASSED_ARP_UNKNOWN", + "PASSED_ARP_UNICAST_REPLY", + "PASSED_NON_IP_UNICAST", + "PASSED_MDNS", + "DROPPED_ETH_BROADCAST", + "DROPPED_RA", + "DROPPED_GARP_REPLY", + "DROPPED_ARP_OTHER_HOST", + "DROPPED_IPV4_L2_BROADCAST", + "DROPPED_IPV4_BROADCAST_ADDR", + "DROPPED_IPV4_BROADCAST_NET", + "DROPPED_IPV4_MULTICAST", + "DROPPED_IPV6_ROUTER_SOLICITATION", + "DROPPED_IPV6_MULTICAST_NA", + "DROPPED_IPV6_MULTICAST", + "DROPPED_IPV6_MULTICAST_PING", + "DROPPED_IPV6_NON_ICMP_MULTICAST", + "DROPPED_802_3_FRAME", + "DROPPED_ETHERTYPE_BLACKLISTED", + "DROPPED_ARP_REPLY_SPA_NO_HOST", + "DROPPED_IPV4_KEEPALIVE_ACK", + "DROPPED_IPV6_KEEPALIVE_ACK", + "DROPPED_IPV4_NATT_KEEPALIVE", + "DROPPED_MDNS" +}; + enum { OPT_PROGRAM, OPT_PACKET, @@ -48,8 +87,11 @@ const struct option long_options[] = {{"program", 1, NULL, OPT_PROGRAM}, {"age", 1, NULL, OPT_AGE}, {"trace", 0, NULL, OPT_TRACE}, {"help", 0, NULL, 'h'}, + {"cnt", 0, NULL, 'c'}, {NULL, 0, NULL, 0}}; +const int COUNTER_SIZE = 4; + // Parses hex in "input". Allocates and fills "*output" with parsed bytes. // Returns length in bytes of "*output". size_t parse_hex(const char* input, uint8_t** output) { @@ -82,6 +124,28 @@ void print_hex(const uint8_t* input, int len) { } } +uint32_t get_counter_value(const uint8_t* data, int data_len, int neg_offset) { + if (neg_offset > -COUNTER_SIZE || neg_offset + data_len < 0) { + return 0; + } + uint32_t value = 0; + for (int i = 0; i < 4; ++i) { + value = value << 8 | data[data_len + neg_offset]; + neg_offset++; + } + return value; +} + +void print_counter(const uint8_t* data, int data_len) { + int counter_len = sizeof(counter_name) / sizeof(counter_name[0]); + for (int i = 0; i < counter_len; ++i) { + uint32_t value = get_counter_value(data, data_len, -COUNTER_SIZE * i); + if (value != 0) { + printf("%s : %d \n", counter_name[i], value); + } + } +} + int tracing_enabled = 0; void maybe_print_tracing_header() { @@ -176,6 +240,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" + " -c, --cnt Print the APF counters\n" " -h, --help Show this message.\n", basename(cmd)); } @@ -188,11 +253,12 @@ int main(int argc, char* argv[]) { uint8_t* data = NULL; uint32_t data_len = 0; uint32_t filter_age = 0; + int print_counter_enabled = 0; int opt; char *endptr; - while ((opt = getopt_long_only(argc, argv, "h", long_options, NULL)) != -1) { + while ((opt = getopt_long_only(argc, argv, "ch", long_options, NULL)) != -1) { switch (opt) { case OPT_PROGRAM: program_len = parse_hex(optarg, &program); @@ -249,6 +315,9 @@ int main(int argc, char* argv[]) { print_usage(argv[0]); exit(0); break; + case 'c': + print_counter_enabled = 1; + break; default: print_usage(argv[0]); exit(1); @@ -284,6 +353,10 @@ int main(int argc, char* argv[]) { printf("Data: "); print_hex(program + program_len, data_len); printf("\n"); + if (print_counter_enabled) { + printf("APF packet counters: \n"); + print_counter(program + program_len, data_len); + } } free(program); |