-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature fix libbpf examples #327
Draft
hp77-creator
wants to merge
7
commits into
eunomia-bpf:master
Choose a base branch
from
hp77-creator:feature_fix_libbpf_examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c4d83cd
Add macOS command to get all syscall
hp77-creator 05b8002
Add example with update in bpftime_epoll library
hp77-creator 338a26b
Remove main executable
hp77-creator 6a072ce
Add syscall for linux
hp77-creator 9d82754
Add map-op example
hp77-creator a571be3
Add linux headers for macos
hp77-creator 4b019db
Merge branch 'master' into feature_fix_libbpf_examples
yunwei37 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 19 additions & 5 deletions
24
attach/syscall_trace_attach_impl/generate_syscall_id_table.sh
100644 → 100755
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
echo "static const char* table=R\"(` | ||
echo -e '#include <sys/syscall.h>' | \ | ||
cpp -dM | grep '#define __NR_.*[0-9]$' | \ | ||
cut -d' ' -f 2,3 | cut -d_ -f 4- | ||
`)\";" > $1 | ||
#! /bin/sh | ||
|
||
# script to print all the syscalls in a system | ||
# usage <script_name> <file_name> | ||
# usage ./generate_syscall_id_table syscalls.txt | ||
if [ "$(uname)" = "Linux" ]; then | ||
echo "static const char* table=R\"(` | ||
echo -e '#include <sys/syscall.h>' | \ | ||
cpp -dM | grep '#define __NR_.*[0-9]$' | \ | ||
cut -d' ' -f 2,3 | cut -d_ -f 4- | ||
`)\";" > $1 | ||
elif [ "$(uname)" = "Darwin" ]; then | ||
echo "static const char* table=R\"(` | ||
echo '#include <sys/syscall.h>' | \ | ||
gcc -E -dM - | grep '#define SYS_.*[0-9]$' | \ | ||
sed 's/#define SYS_//' | \ | ||
awk '{print $1, $2}' | ||
`)\";" > $1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CXX = g++ | ||
|
||
CXXFLAGS = -I../../runtime/include \ | ||
-I../../vm/compat/include \ | ||
-I../../third_party/spdlog/include \ | ||
-I../../vm/vm-core/include | ||
|
||
SRC = main.cpp | ||
TGT = main | ||
|
||
main: $(SRC) | ||
$(CXX) $(SRC) -o $(TGT) $(CXXFLAGS) | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf $(TGT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## BPF map example | ||
|
||
This example tries to create a bpf program which doesn't have to rely on libbpf and which can be tested on systems | ||
that do not have support from libbpf. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <iostream> | ||
#include <unistd.h> | ||
#if __linux__ | ||
#include <linux/bpf.h> | ||
#include <sys/syscall.h> | ||
#elif defined(__APPLE__) | ||
#include "bpftime_epoll.h" | ||
#endif | ||
|
||
|
||
#if defined(__APPLE__) | ||
using namespace bpftime_epoll; | ||
#endif | ||
|
||
static struct bpf_insn prog[] = { | ||
BPF_MOV64_IMM(BPF_REG_0, 0), | ||
BPF_EXIT_INSN(), | ||
}; | ||
|
||
static long bpf(int cmd, union bpf_attr *attr, unsigned int size) { | ||
#if __linux__ | ||
return syscall(__NR_bpf, cmd, attr, size); | ||
#endif | ||
yunwei37 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
int main() { | ||
union bpf_attr attr = {}; | ||
int prog_fd, map_fd; | ||
|
||
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; | ||
attr.insns = (unsigned long) prog; | ||
attr.insn_cnt = sizeof(prog) / sizeof(prog[0]); | ||
attr.license = (unsigned long) "GPL"; | ||
|
||
prog_fd = bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); | ||
if (prog_fd < 0) { | ||
std::cerr<<"Failed to load eBPF program"; | ||
return 1; | ||
} | ||
printf("eBPF program loaded and fd: %d\n",prog_fd); | ||
|
||
attr = (union bpf_attr){0}; | ||
attr.map_type = BPF_MAP_TYPE_ARRAY; | ||
attr.key_size = sizeof(int); | ||
attr.value_size = sizeof(int); | ||
attr.max_entries = 1; | ||
|
||
map_fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); | ||
if(map_fd < 0) { | ||
std::cerr<<"Failed to create eBPF map"; | ||
return 1; | ||
} | ||
printf("eBPF map created with fd:%d\n", map_fd); | ||
int key = 0, value = 42; | ||
|
||
attr = (union bpf_attr){0}; | ||
attr.map_fd = map_fd; | ||
attr.key = (unsigned long)&key; | ||
attr.value = (unsigned long)&value; | ||
attr.flags = BPF_XDP; | ||
|
||
if(bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)) < 0) { | ||
std::cerr<<"Error in updating eBPF map\n"; | ||
return 1; | ||
} | ||
printf("Map updated successfully\n"); | ||
|
||
close(prog_fd); | ||
close(map_fd); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.dSYM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CXX = clang | ||
CFLAGS = -nostdinc \ | ||
-I../../third_party/macos \ | ||
-I../../runtime/include \ | ||
-I../../vm/compat/include \ | ||
-I../../third_party/spdlog/include \ | ||
-I../../vm/vm-core/include \ | ||
-D__KERNEL__ \ | ||
-D__BPF_TRACING__ \ | ||
-D__TARGET_ARCH_bpf \ | ||
-DCONFIG_64BIT | ||
|
||
SRC = mapop.bpf.c | ||
TGT = main | ||
|
||
main: $(SRC) | ||
$(CXX) -g $(CFLAGS) -target bpf -Wall -O2 -c $(SRC) -o $(TGT) | ||
|
||
.PHONY: clean | ||
|
||
clean: | ||
rm -rf $(TGT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## BPF map example | ||
|
||
This example tries to create a bpf program which doesn't have to rely on libbpf and which can be tested on systems | ||
that do not have support from libbpf. | ||
|
||
To use this program, you would typically need to compile it with an eBPF-capable compiler (like clang with appropriate options), | ||
load it into the kernel using an eBPF loader(`bpftime load` can come to rescue here), | ||
and attach it to the execve system call tracepoint. | ||
The program will then count execve calls per process, which could be read from user space using the exec_count map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <iostream> | ||
#include <unistd.h> | ||
#if __linux__ | ||
#include <linux/bpf.h> | ||
#include <sys/syscall.h> | ||
#elif defined(__APPLE__) | ||
#include "bpftime_epoll.h" | ||
#endif | ||
|
||
|
||
#if defined(__APPLE__) | ||
using namespace bpftime_epoll; | ||
#endif | ||
|
||
static struct bpf_insn prog[] = { | ||
BPF_MOV64_IMM(BPF_REG_0, 0), | ||
BPF_EXIT_INSN(), | ||
}; | ||
|
||
static long bpf(int cmd, union bpf_attr *attr, unsigned int size) { | ||
#if __linux__ | ||
return syscall(__NR_bpf, cmd, attr, size); | ||
#endif | ||
return 0; | ||
} | ||
|
||
int main() { | ||
union bpf_attr attr = {}; | ||
int prog_fd, map_fd; | ||
|
||
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; | ||
attr.insns = (unsigned long) prog; | ||
attr.insn_cnt = sizeof(prog) / sizeof(prog[0]); | ||
attr.license = (unsigned long) "GPL"; | ||
|
||
prog_fd = bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); | ||
if (prog_fd < 0) { | ||
std::cerr<<"Failed to load eBPF program"; | ||
return 1; | ||
} | ||
printf("eBPF program loaded and fd: %d\n",prog_fd); | ||
|
||
attr = (union bpf_attr){0}; | ||
attr.map_type = BPF_MAP_TYPE_ARRAY; | ||
attr.key_size = sizeof(int); | ||
attr.value_size = sizeof(int); | ||
attr.max_entries = 1; | ||
|
||
map_fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); | ||
if(map_fd < 0) { | ||
std::cerr<<"Failed to create eBPF map"; | ||
return 1; | ||
} | ||
printf("eBPF map created with fd:%d\n", map_fd); | ||
int key = 0, value = 42; | ||
|
||
attr = (union bpf_attr){0}; | ||
attr.map_fd = map_fd; | ||
attr.key = (unsigned long)&key; | ||
attr.value = (unsigned long)&value; | ||
attr.flags = BPF_XDP; | ||
|
||
if(bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)) < 0) { | ||
std::cerr<<"Error in updating eBPF map\n"; | ||
return 1; | ||
} | ||
printf("Map updated successfully\n"); | ||
|
||
close(prog_fd); | ||
close(map_fd); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
#include <linux/bpf.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <linux/types.h> | ||
|
||
|
||
struct bpf_map_def { | ||
unsigned int type; | ||
unsigned int key_size; | ||
unsigned int value_size; | ||
unsigned int max_entries; | ||
}; | ||
|
||
struct bpf_map_def exec_count __attribute__((section(".maps"))) = { | ||
.type = BPF_MAP_TYPE_HASH, | ||
.key_size = sizeof(uint32_t), | ||
.value_size = sizeof(uint64_t), | ||
.max_entries = 1024, | ||
}; | ||
|
||
__attribute__((section("tracepoint/syscalls/sys_enter_execve"))) | ||
int count_execve(void *ctx) { | ||
uint32_t pid = bpf_get_current_pid_tgid() >> 32; | ||
uint64_t *count = bpf_map_lookup_elem(&exec_count, &pid); | ||
if (count) { | ||
(*count)++; | ||
} else { | ||
uint64_t initial_count = 1; | ||
bpf_map_update_elem(&exec_count, &pid, &initial_count, BPF_ANY); | ||
} | ||
return 0; | ||
} | ||
|
||
char LICENSE[] __attribute__((section("license"))) = "GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <libkern/OSByteOrder.h> | ||
|
||
#if defined(__LITTLE_ENDIAN__) | ||
#define __LITTLE_ENDIAN_BITFIELD | ||
#else | ||
#define __BIG_ENDIAN_BITFIELD | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include <errno.h> | ||
|
||
#define EUCLEAN 117 /* Structure needs cleaning */ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
typedef __signed__ char __s8; | ||
typedef unsigned char __u8; | ||
|
||
typedef __signed__ short __s16; | ||
typedef unsigned short __u16; | ||
|
||
typedef __signed__ int __s32; | ||
typedef unsigned int __u32; | ||
|
||
__extension__ typedef __signed__ long long __s64; | ||
__extension__ typedef unsigned long long __u64; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#define __NR_perf_event_open -1 | ||
#define __NR_bpf -1 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#define bswap_16(value) \ | ||
((((value) & 0xff) << 8) | ((value) >> 8)) | ||
|
||
#define bswap_32(value) \ | ||
(((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \ | ||
(uint32_t)bswap_16((uint16_t)((value) >> 16))) | ||
|
||
#define bswap_64(value) \ | ||
(((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \ | ||
<< 32) | \ | ||
(uint64_t)bswap_32((uint32_t)((value) >> 32))) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be better to rename the example to map-op?
And, can you try loading a eBPF program and running it in the kernel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure @yunwei37