Skip to content
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

refactor: move code from src to root dir #4

Merged
merged 1 commit into from
Dec 20, 2023
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
6 changes: 3 additions & 3 deletions .github/actions/install-dependencies/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ runs:
using: composite
steps:
- name: Setup Go
uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491
with:
go-version-file: src/go.mod
cache-dependency-path: src/go.sum
go-version-file: go.mod
cache-dependency-path: go.sum
cache: true
- name: Install Compilers & Formatters
shell: bash
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/verify-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ concurrency:

on:
pull_request:
branches: [ "main" ]
branches:
- main

jobs:
verify:
Expand All @@ -22,8 +23,8 @@ jobs:

- name: Compile App
run: |
make -C src
make

- name: Run App
run: |
sudo timeout --preserve-status 5s ./src/socket-filter
sudo timeout --preserve-status 5s ./socket-filter
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
.idea/
.idea/

.output/

socket-filter
socket-filter.bpf.o
24 changes: 12 additions & 12 deletions src/Makefile → Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ CLANG_FORMAT ?= clang-format
LLVM_STRIP ?= llvm-strip
GO ?= go

LIBBPF_SRC := $(abspath ../libbpf/src)
LIBBPF_SRC := $(abspath ./libbpf/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)

BPFTOOL_SRC := $(abspath ../bpftool/src)
BPFTOOL_SRC := $(abspath ./bpftool/src)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool

ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/')
BTFFILE = /sys/kernel/btf/vmlinux
VMLINUX := ../vmlinux/$(ARCH)/vmlinux.h
VMLINUX := ./vmlinux/$(ARCH)/vmlinux.h

# Use our own libbpf API headers and Linux UAPI headers distributed with
# libbpf to avoid dependency on system-wide headers, which could be missing or
Expand All @@ -29,38 +29,38 @@ all: socket-filter

.PHONY: clean
clean:
$(Q)rm -rf $(OUTPUT) socket-filter.bpf.o socket-filter
rm -rf $(OUTPUT) socket-filter.bpf.o socket-filter

$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
$(Q)mkdir -p $@
mkdir -p $@

# Build libbpf
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \
INCLUDEDIR= LIBDIR= UAPIDIR= \
install

# Build bpftool
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap
$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap

# Build BPF code
socket-filter.bpf.o: socket-filter.bpf.c $(LIBBPF_OBJ) | $(OUTPUT)
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) -c socket-filter.bpf.c -o $@
$(Q)$(LLVM_STRIP) -g $@
$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) -c socket-filter.bpf.c -o $@
$(LLVM_STRIP) -g $@

# Build application binary
socket-filter: socket-filter.bpf.o main.go
$(Q)$(GO) build -o $@ main.go
$(GO) build -o $@ main.go

.PHONY: $(VMLINUX)
$(VMLINUX): $(BPFTOOL)
$(Q)$(BPFTOOL) btf dump file $(BTFFILE) format c > $(VMLINUX)
$(BPFTOOL) btf dump file $(BTFFILE) format c > $(VMLINUX)

.PHONY: format
format:
$(Q)$(CLANG_FORMAT) --verbose -i \
$(CLANG_FORMAT) --verbose -i \
socket-filter.bpf.c \
socket-filter.h

Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
The `socket-filter` program demonstrates how to load an eBPF program from an ELF file,
and attach it to a raw socket.

`BPF_PROG_TYPE_SOCKET_FILTER` was the first program type to be added to the
Linux kernel. When you attach a BPF program to a raw socket, you get access to
all the packets processed by that socket. Socket filter programs don't allow
you to modify the contents of those packets or to change the destination for
those packets; they give you access to them for observability purposes only.
The metadata that your program receives contains information related to the
network stack such as the protocol type that's being used to deliver the
packet.

## Usage

Clone and change current directory to the cloned repository:
Expand All @@ -20,13 +29,13 @@ git submodule update --init --recursive
Compile BPF application and Go loader:

```
make -C src all
make
```

Run the application:

``` console
$ sudo ./src/socket-filter --index=0
$ sudo ./socket-filter --index=0
Filtering on eth index: 0
ICMP: 20 TCP: 121 UDP: 12_
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/socket-filter.bpf.c → socket-filter.bpf.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//+build ignore

#include "socket-filter.h"
#include "vmlinux.h"
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "socket-filter.h"

char LICENSE[] SEC("license") = "GPL";

Expand Down
File renamed without changes.
4 changes: 0 additions & 4 deletions src/.gitignore

This file was deleted.