Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
BasedOnStyle: LLVM,
BreakStringLiterals: true,
AllowShortFunctionsOnASingleLine: InlineOnly,
ColumnLimit: 100,
IndentWidth: 4,
SortIncludes: false,
ReflowComments: false,
BinPackArguments: false,
BinPackParameters: false,
TabWidth: 4,
PointerAlignment: Right,
}
39 changes: 39 additions & 0 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Clang Format Check

on:
push:
branches: [ 'main' ]
pull_request:
branches: [ 'main' ]

permissions:
contents: read

jobs:
clang-format:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 0
persist-credentials: false

- name: Install Clang-Format
run: sudo apt-get install clang-format-19

- name: Check Clang Format
run: |
# Check for modified C/C++ files
files=$(git diff --name-status origin/main...HEAD | awk '$1 != "D" && /\.(c|cpp|h)$/ { print ($3 != "" ? $3 : $2) }' )
if [ -z "$files" ]; then
echo "No C/C++ files modified."
exit 0
fi
# Run clang-format and check for changes
clang-format-19 -i $files
if ! git diff --exit-code $files; then
echo "Error: Some files are not formatted correctly. Please run clang-format on the modified files or use the provided pre-commit hook."
exit 1
fi
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,16 @@ MARKDOWNIMAGE := $(shell awk '$$4=="markdown" {print $$2}' $(DEPENDENCIES_DOCKER
.PHONY: markdown-lint
markdown-lint:
docker run --rm -u $(DOCKER_USER) -v "$(CURDIR):$(WORKDIR)" $(MARKDOWNIMAGE) -c $(WORKDIR)/.markdownlint.yaml -p $(WORKDIR)/.markdownlintignore $(WORKDIR)/**/*.md

.PHONY: clang-format
clang-format:
find ./internal -type f -name "*.c" | xargs -P 0 -n 1 clang-format -i
find ./internal -type f -name "*.h" | xargs -P 0 -n 1 clang-format -i

.PHONY: install-hooks
install-hooks:
@if [ ! -f .git/hooks/pre-commit ]; then \
echo "Installing pre-commit hook..."; \
cp hooks/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit; \
echo "Pre-commit hook installed."; \
fi
15 changes: 15 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# Format all staged C/C++ files
files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(c|cpp|h)$')

if [ -n "$files" ]; then
echo "Running clang-format on:"
echo "$files"

# Run clang-format on the staged files
for file in $files; do
clang-format -i "$file"
git add "$file" # Re-add formatted files to staging
done
fi
57 changes: 20 additions & 37 deletions internal/include/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,58 @@ volatile const u64 total_cpus;
volatile const u64 start_addr;
volatile const u64 end_addr;

struct
{
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__type(key, s32);
__type(value, u64);
__uint(max_entries, MAX_ENTRIES);
__uint(pinning, LIBBPF_PIN_BY_NAME);
} alloc_map SEC(".maps");

static __always_inline u64 get_area_start()
{
static __always_inline u64 get_area_start() {
s64 partition_size = (end_addr - start_addr) / total_cpus;
u32 current_cpu = bpf_get_smp_processor_id();
s32 start_index = 0;
u64 *start = (u64 *)bpf_map_lookup_elem(&alloc_map, &start_index);
if (start == NULL || *start == 0)
{
if (start == NULL || *start == 0) {
u64 current_start_addr = start_addr + (partition_size * current_cpu);
bpf_map_update_elem(&alloc_map, &start_index, &current_start_addr, BPF_ANY);
return current_start_addr;
}
else
{
} else {
return *start;
}
}

static __always_inline u64 get_area_end(u64 start)
{
static __always_inline u64 get_area_end(u64 start) {
s64 partition_size = (end_addr - start_addr) / total_cpus;
s32 end_index = 1;
u64 *end = (u64 *)bpf_map_lookup_elem(&alloc_map, &end_index);
if (end == NULL || *end == 0)
{
if (end == NULL || *end == 0) {
u64 current_end_addr = start + partition_size;
bpf_map_update_elem(&alloc_map, &end_index, &current_end_addr, BPF_ANY);
return current_end_addr;
}
else
{
} else {
return *end;
}
}

static __always_inline s32 bound_number(s32 num, s32 min, s32 max)
{
if (num < min)
{
static __always_inline s32 bound_number(s32 num, s32 min, s32 max) {
if (num < min) {
return min;
}
else if (num > max)
{
} else if (num > max) {
return max;
}
return num;
}

static __always_inline void *write_target_data(void *data, s32 size)
{
if (!data || data == NULL)
{
static __always_inline void *write_target_data(void *data, s32 size) {
if (!data || data == NULL) {
return NULL;
}

u64 start = get_area_start();
u64 end = get_area_end(start);
if (end - start < size)
{
if (end - start < size) {
bpf_printk("reached end of CPU memory block, going to the start again");
s32 start_index = 0;
bpf_map_delete_elem(&alloc_map, &start_index);
Expand All @@ -93,8 +77,7 @@ static __always_inline void *write_target_data(void *data, s32 size)
size = bound_number(size, MIN_BUFFER_SIZE, MAX_BUFFER_SIZE);
u64 page_offset = (u64)target & 0xFFF;
u64 dist_to_next_page = 4096 - page_offset;
if (dist_to_next_page < size)
{
if (dist_to_next_page < size) {
target += dist_to_next_page;
}
u64 target_u = (u64)target;
Expand All @@ -104,8 +87,7 @@ static __always_inline void *write_target_data(void *data, s32 size)
}

long success = bpf_probe_write_user(target, data, size);
if (success == 0)
{
if (success == 0) {
s32 start_index = 0;
// Update the start position of this chunk, taking into account possible adjustments
// we made to be page aligned
Expand All @@ -118,10 +100,11 @@ static __always_inline void *write_target_data(void *data, s32 size)

bpf_map_update_elem(&alloc_map, &start_index, &updated_start, BPF_ANY);
return target;
}
else
{
bpf_printk("failed to write to userspace, error code: %d, addr: %lx, size: %d", success, target, size);
} else {
bpf_printk("failed to write to userspace, error code: %d, addr: %lx, size: %d",
success,
target,
size);
return NULL;
}
}
Expand Down
6 changes: 2 additions & 4 deletions internal/include/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@

#endif

static __always_inline void *get_argument(struct pt_regs *ctx, int index)
{
switch (index)
{
static __always_inline void *get_argument(struct pt_regs *ctx, int index) {
switch (index) {
case 1:
return (void *)GO_PARAM1(ctx);
case 2:
Expand Down
Loading
Loading