Skip to content

Commit

Permalink
add map example
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Aug 30, 2024
1 parent 1ec1cb4 commit 748bb8f
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions example/maps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <cassert>
#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"
"\x63\x1a\xfc\xff\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"
Expand All @@ -52,15 +53,15 @@ 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)

Check warning on line 58 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L58

Added line #L58 was not covered by tests
{
std::cout << "bpf_map_lookup_elem " << map_fd << std::endl;

Check warning on line 60 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L60

Added line #L60 was not covered by tests
if (map_fd == 5) {
return &ctl_array[*(uint32_t *)key];

Check warning on line 62 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L62

Added line #L62 was not covered by tests
} else if (map_fd == 6) {
return &ctrn_array[*(uint32_t *)key];
return &cntrs_array[*(uint32_t *)key];

Check warning on line 64 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L64

Added line #L64 was not covered by tests
} else {
return nullptr;

Check warning on line 66 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L66

Added line #L66 was not covered by tests
}
Expand All @@ -69,28 +70,33 @@ void *bpf_map_lookup_elem(uint64_t map_fd, void *key)

uint64_t map_by_fd(uint32_t fd)

Check warning on line 71 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L71

Added line #L71 was not covered by tests
{
std::cout << "map_by_fd " << fd << std::endl;
return fd;

Check warning on line 74 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L73-L74

Added lines #L73 - L74 were not covered by tests
}
uint64_t map_by_idx(uint32_t idx)

Check warning on line 76 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L76

Added line #L76 was not covered by tests
{
std::cout << "map_by_idx " << idx << std::endl;
return idx;

Check warning on line 79 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L78-L79

Added lines #L78 - L79 were not covered by tests
}
uint64_t map_val(uint64_t val)

Check warning on line 81 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L81

Added line #L81 was not covered by tests
{
std::cout << "map_val " << val << std::endl;

Check warning on line 83 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L83

Added line #L83 was not covered by tests
if (val == 5) {
return (uint64_t)(void *)ctl_array;

Check warning on line 85 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L85

Added line #L85 was not covered by tests
} else if (val == 6) {
return (uint64_t)(void *)ctrn_array;
return (uint64_t)(void *)cntrs_array;

Check warning on line 87 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L87

Added line #L87 was not covered by tests
} else {
return 0;

Check warning on line 89 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L89

Added line #L89 was not covered by tests
}
}
uint64_t var_addr(uint32_t idx)

Check warning on line 92 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L92

Added line #L92 was not covered by tests
{
std::cout << "var_addr " << idx << std::endl;
return idx;

Check warning on line 95 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L94-L95

Added lines #L94 - L95 were not covered by tests
}
uint64_t code_addr(uint32_t idx)

Check warning on line 97 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L97

Added line #L97 was not covered by tests
{
std::cout << "code_addr " << idx << std::endl;
return idx;

Check warning on line 100 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L99-L100

Added lines #L99 - L100 were not covered by tests
}

Expand All @@ -108,7 +114,7 @@ int main(int argc, char *argv[])
vm.get_error_message().c_str());
exit(1);

Check warning on line 115 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L113-L115

Added lines #L113 - L115 were not covered by tests
}
vm.register_external_function(2, "bpf_map_lookup_elem",
vm.register_external_function(1, "bpf_map_lookup_elem",

Check warning on line 117 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L117

Added line #L117 was not covered by tests
(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,

Check warning on line 120 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L120

Added line #L120 was not covered by tests
Expand All @@ -119,11 +125,24 @@ int main(int argc, char *argv[])
vm.get_error_message().c_str());
exit(1);

Check warning on line 126 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L124-L126

Added lines #L124 - L126 were not covered by tests
}
// 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;

Check warning on line 132 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L129-L132

Added lines #L129 - L132 were not covered by tests
// counter should be 1
std::cout << "cntrs_array[0] = " << cntrs_array[0] << std::endl;

Check warning on line 134 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L134

Added line #L134 was not covered by tests

err = vm.exec(&bpf_mem, sizeof(bpf_mem), res);
assert(err == 0 && cntrs_array[0] == 2);
std::cout << "\nreturn value = " << res << std::endl;

Check warning on line 138 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L136-L138

Added lines #L136 - L138 were not covered by tests
// counter should be 2
std::cout << "cntrs_array[0] = " << cntrs_array[0] << std::endl;

Check warning on line 140 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L140

Added line #L140 was not covered by tests

// 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;

Check warning on line 147 in example/maps.cpp

View check run for this annotation

Codecov / codecov/patch

example/maps.cpp#L143-L147

Added lines #L143 - L147 were not covered by tests
}

0 comments on commit 748bb8f

Please sign in to comment.