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

chore: rename project to bpf-go-socket #8

Merged
merged 1 commit into from
Jan 14, 2024
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
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
---
name: Verify Pull Request

name: PR Created
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
pull_request:
branches:
- main

jobs:
verify:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
with:
submodules: recursive

- name: Install Build Dependencies
uses: ./.github/actions/install-dependencies

- name: Compile App
run: |
make

- name: Run App
run: |
sudo timeout --preserve-status 5s ./socket-filter
sudo timeout --preserve-status 5s ./socket
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

.output/

socket-filter
socket-filter.bpf.o
socket
socket.bpf.o
15 changes: 7 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

.PHONY: all
all: socket-filter
all: socket

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

$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
mkdir -p $@
Expand All @@ -45,13 +45,12 @@ $(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPU
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
$(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)
$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) -c socket-filter.bpf.c -o $@
socket.bpf.o: socket.bpf.c $(LIBBPF_OBJ) | $(OUTPUT)
$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) -c socket.bpf.c -o $@
$(LLVM_STRIP) -g $@

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

.PHONY: $(VMLINUX)
Expand All @@ -61,8 +60,8 @@ $(VMLINUX): $(BPFTOOL)
.PHONY: format
format:
$(CLANG_FORMAT) --verbose -i \
socket-filter.bpf.c \
socket-filter.h
socket.bpf.c \
socket.h

# delete failed targets
.DELETE_ON_ERROR:
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# bpf-go-socket-filter
# bpf-go-socket

The `socket-filter` program demonstrates how to load an eBPF program from an ELF file,
and attach it to a raw socket.
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
Expand All @@ -17,12 +17,12 @@ packet.
Clone and change current directory to the cloned repository:

```
git clone --recurse-submodules https://github.com/danielpacak/bpf-go-socket-filter.git
git clone --recurse-submodules https://github.com/danielpacak/bpf-go-socket.git
```
or
```
git clone https://github.com/danielpacak/bpf-go-socket-filter.git
cd bpf-go-socket-filter
git clone https://github.com/danielpacak/bpf-go-socket.git
cd bpf-go-socket
git submodule update --init --recursive
```

Expand All @@ -35,18 +35,18 @@ make
Run the application as root with `sudo`:

``` console
$ sudo ./socket-filter --index=0
$ sudo ./socket --index=0
Filtering as uid=0(root) on eth index: 0
ICMP: 20 TCP: 121 UDP: 12_
```

Run the application as non-root user:

```
sudo setcap 'cap_net_raw=ep cap_bpf=ep' ./socket-filter
sudo setcap 'cap_net_raw=ep cap_bpf=ep' ./socket
```
``` console
$ ./socket-filter
$ ./socket
Filtering as uid=1000(dpacak) on eth index: 0
ICMP: 0 TCP: 6167 UDP: 0_
```
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/danielpacak/bpf-go-socket-filter
module danielpacak/bpf-go-socket

go 1.21

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func run(ctx context.Context) error {
if err != nil {
return err
}
bpfObjectFile := path.Join(filepath.Dir(executable), "socket-filter.bpf.o")
bpfObjectFile := path.Join(filepath.Dir(executable), "socket.bpf.o")

spec, err := ebpf.LoadCollectionSpec(bpfObjectFile)
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions socket-filter.h

This file was deleted.

5 changes: 2 additions & 3 deletions socket-filter.bpf.c → socket.bpf.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//+build ignore

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

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

#define ETH_HLEN \
14 /* Total octets in header (copied from linux/if_ether.h). */
#define ETH_HLEN 14 // Total octets in header (copied from linux/if_ether.h).

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
Expand Down
6 changes: 6 additions & 0 deletions socket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//+build ignore

#ifndef __SOCKET_H
#define __SOCKET_H

#endif /* __SOCKET_H */