diff --git a/example/maps.cpp b/example/maps.cpp index 2a6991c..7b224f1 100644 --- a/example/maps.cpp +++ b/example/maps.cpp @@ -8,10 +8,10 @@ #include #include #include +#include #include "llvmbpf.hpp" using namespace bpftime; - const unsigned char xdp_counter_bytecode[] = "\x79\x16\x00\x00\x00\x00\x00\x00" "\x79\x17\x08\x00\x00\x00\x00\x00" "\xb7\x01\x00\x00\x00\x00\x00\x00" @@ -19,14 +19,15 @@ const unsigned char xdp_counter_bytecode[] = "\x79\x16\x00\x00\x00\x00\x00\x00" "\xbf\xa2\x00\x00\x00\x00\x00\x00" "\x07\x02\x00\x00\xfc\xff\xff\xff" "\x18\x11\x00\x00\x05\x00\x00\x00" - "\x00\x00\x00\x00\x85\x00\x00\x00" - "\x01\x00\x00\x00\xbf\x01\x00\x00" - "\x00\x00\x00\x00\xb7\x00\x00\x00" - "\x02\x00\x00\x00\x15\x01\x18\x00" - "\x00\x00\x00\x00\x61\x11\x00\x00" - "\x00\x00\x00\x00\x55\x01\x16\x00" - "\x00\x00\x00\x00\x18\x21\x00\x00" - "\x06\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\x85\x00\x00\x00\x01\x00\x00\x00" + "\xbf\x01\x00\x00\x00\x00\x00\x00" + "\xb7\x00\x00\x00\x02\x00\x00\x00" + "\x15\x01\x18\x00\x00\x00\x00\x00" + "\x61\x11\x00\x00\x00\x00\x00\x00" + "\x55\x01\x16\x00\x00\x00\x00\x00" + "\x18\x21\x00\x00\x06\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00" "\x79\x12\x00\x00\x00\x00\x00\x00" "\x07\x02\x00\x00\x01\x00\x00\x00" "\x7b\x21\x00\x00\x00\x00\x00\x00" @@ -52,7 +53,7 @@ const unsigned char xdp_counter_bytecode[] = "\x79\x16\x00\x00\x00\x00\x00\x00" uint8_t bpf_mem[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }; uint32_t ctl_array[2] = { 0, 0 }; -uint64_t ctrn_array[2] = { 0, 0 }; +uint64_t cntrs_array[2] = { 0, 0 }; void *bpf_map_lookup_elem(uint64_t map_fd, void *key) { @@ -60,7 +61,7 @@ void *bpf_map_lookup_elem(uint64_t map_fd, void *key) if (map_fd == 5) { return &ctl_array[*(uint32_t *)key]; } else if (map_fd == 6) { - return &ctrn_array[*(uint32_t *)key]; + return &cntrs_array[*(uint32_t *)key]; } else { return nullptr; } @@ -69,28 +70,33 @@ void *bpf_map_lookup_elem(uint64_t map_fd, void *key) uint64_t map_by_fd(uint32_t fd) { + std::cout << "map_by_fd " << fd << std::endl; return fd; } uint64_t map_by_idx(uint32_t idx) { + std::cout << "map_by_idx " << idx << std::endl; return idx; } uint64_t map_val(uint64_t val) { + std::cout << "map_val " << val << std::endl; if (val == 5) { return (uint64_t)(void *)ctl_array; } else if (val == 6) { - return (uint64_t)(void *)ctrn_array; + return (uint64_t)(void *)cntrs_array; } else { return 0; } } uint64_t var_addr(uint32_t idx) { + std::cout << "var_addr " << idx << std::endl; return idx; } uint64_t code_addr(uint32_t idx) { + std::cout << "code_addr " << idx << std::endl; return idx; } @@ -108,7 +114,7 @@ int main(int argc, char *argv[]) vm.get_error_message().c_str()); exit(1); } - vm.register_external_function(2, "bpf_map_lookup_elem", + vm.register_external_function(1, "bpf_map_lookup_elem", (void *)bpf_map_lookup_elem); // set the lddw helpers for accessing maps vm.set_lddw_helpers(map_by_fd, map_by_idx, map_val, var_addr, @@ -119,11 +125,24 @@ int main(int argc, char *argv[]) vm.get_error_message().c_str()); exit(1); } + // Map value (counter) should be 0 + std::cout << "cntrs_array[0] = " << cntrs_array[0] << std::endl; int err = vm.exec(&bpf_mem, sizeof(bpf_mem), res); - if (err != 0) { - fprintf(stderr, "Failed to exec."); - exit(1); - } - printf("res = %" PRIu64 "\n", res); + assert(err == 0 && cntrs_array[0] == 1); + std::cout << "\nreturn value = " << res << std::endl; + // counter should be 1 + std::cout << "cntrs_array[0] = " << cntrs_array[0] << std::endl; + + err = vm.exec(&bpf_mem, sizeof(bpf_mem), res); + assert(err == 0 && cntrs_array[0] == 2); + std::cout << "\nreturn value = " << res << std::endl; + // counter should be 2 + std::cout << "cntrs_array[0] = " << cntrs_array[0] << std::endl; + + // Change the value of the control array can change the return value + ctl_array[0] = 1; + err = vm.exec(&bpf_mem, sizeof(bpf_mem), res); + assert(err == 0); + std::cout << "\nreturn value = " << res << std::endl; return 0; }