Slide 1

Slide 1 text

An intro to eBPF with Go: The Foundation of Modern Kubernetes Networking Donia Chaiehloudj | @doniacld Software Engineer, Isovalent at Cisco Co-author of the book โ€œLearn Go with Pocket-Sized Projectsหฎ Jul 8, 2025

Slide 2

Slide 2 text

Who has an Android phone? Raise your ๐Ÿ– FOSDEM Feb 1, 2025 @doniacld

Slide 3

Slide 3 text

Is eBPF everywhere? ๓ฐกธ FOSDEM Feb 1, 2025 @doniacld

Slide 4

Slide 4 text

Without @doniacld

Slide 5

Slide 5 text

What is ? @doniacld Makes the Linux kernel programmable

Slide 6

Slide 6 text

With @doniacld

Slide 7

Slide 7 text

Overview of @doniacld Program App Linux Kernel Userspace syscalls event

Slide 8

Slide 8 text

Why for Kubernetes? @doniacld Program pod Linux Kernel Userspace event pod pods One kernel for all the containers syscalls

Slide 9

Slide 9 text

Why for Kubernetes? @doniacld

Slide 10

Slide 10 text

Why for Kubernetes? @doniacld

Slide 11

Slide 11 text

Why for Kubernetes? @doniacld

Slide 12

Slide 12 text

@doniacld Go is the ๐Ÿ’Ž in Cloud-native ecosystem โ— eBPF is kernel-level magic โ†’ But hard to interact with directly ๎‚C, bytecode). โ— Most cloud-native projects are written in Go โ†’ Kubernetes, Docker, Prometheus, Cilium, etc. โ— Go offers simplicity + performance โ†’ Ideal for distributed systems and networking. โ— Go + eBPF = productive + powerful โ†’ SDKs like cilium/ebpf let you write networking/security tooling

Slide 13

Slide 13 text

Develop with @doniacld C Code ELF Object Linux Kernel clang

Slide 14

Slide 14 text

@doniacld C Code App Code Maps Linux Kernel Userspace load() read() write() syscalls ELF Object clang Write programs in userspace loading BPF_MAP_TYPE_HASH BPF_MAP_TYPE_ARRAY BPF_MAP_TYPE_PERCPU_HASH BPF_MAP_TYPE_PERCPU_ARRAY BPF_MAP_TYPE_QUEUE BPF_MAP_TYPE_STACK

Slide 15

Slide 15 text

@doniacld 1 Write eBPF C program 2 Generate Go Objects & Methods 3 Write Go main program 4 Run your program Development cycle

Slide 16

Slide 16 text

1. TRACING @doniacld

Slide 17

Slide 17 text

Tracing Open File Events https://github.com/doniacld/ebpf-4-gophers @doniacld

Slide 18

Slide 18 text

1. Write C program: Trace when files are open #include // BPF header for definitions. #include // BPF helper functions. // Attach to sys_enter_openat tracepoint. SEC("tracepoint/syscalls/sys_enter_openat") int trace_openat(void *ctx) { char msg[] = "File opening..."; // Message to log. bpf_trace_printk(msg, sizeof(msg)); // Log message to trace pipe. return 0; } char LICENSE[] SEC("license") = "GPL"; @doniacld

Slide 19

Slide 19 text

1. Write C program: Trace when files are open #include // BPF header for definitions. #include // BPF helper functions. // Attach to sys_enter_openat tracepoint. SEC("tracepoint/syscalls/sys_enter_openat") int trace_openat(void *ctx) { char msg[] = "File opening..."; // Message to log. bpf_trace_printk(msg, sizeof(msg)); // Log message to trace pipe. return 0; } char LICENSE[] SEC("license") = "GPL"; @doniacld

Slide 20

Slide 20 text

1. Write C program: Trace when files are open #include // BPF header for definitions. #include // BPF helper functions. // Attach to sys_enter_openat tracepoint. SEC("tracepoint/syscalls/sys_enter_openat") int trace_openat(void *ctx) { char msg[] = "File opening..."; // Message to log. bpf_trace_printk(msg, sizeof(msg)); // Log message to trace pipe. return 0; } char LICENSE[] SEC("license") = "GPL"; @doniacld $ sudo cat /sys/kernel/debug/tracing/trace_pipe systemd-journal-326 [000] ...21 53765.037027: bpf_trace_printk: File opening... systemd-journal-326 [000] ...21 53765.037048: bpf_trace_printk: File opening... cat-35252 [000] ...21 53765.037948: bpf_trace_printk: File opening... cat-35252 [000] ...21 53765.037966: bpf_trace_printk: File opening... cat-35252 [000] ...21 53765.038107: bpf_trace_printk: File opening... process id

Slide 21

Slide 21 text

2. Compile & Generate Go Objects: bpf2go @doniacld //go:generate go run github.com/cilium/ebpf/cmd/bpf2go openfile openfile.c call clang under the hood

Slide 22

Slide 22 text

@doniacld 2. Generated Go Objects // openfilePrograms contains all programs after they have been loaded into the kernel. //type openfilePrograms struct { TraceOpenat *ebpf.Program `ebpf:"trace_openat"` } Objects to manipulate // openfileObjects contains all objects after they have been loaded into the kernel. type openfileObjects struct { openfilePrograms openfileMaps openfileVariables } //go:generate go run github.com/cilium/ebpf/cmd/bpf2go openfile openfile.c eBPF program bytecodes

Slide 23

Slide 23 text

@doniacld 3. Main Go Program: Load the eBPF program func main() { // Allow the current process to lock memory for eBPF resources. if err := rlimit.RemoveMemlock(); err != nil { log.Fatalf("failed to remove memlock: %v", err) } var objs openfileObjects err := loadOpenfileObjects(&objs, nil) if err != nil { log.Fatalf("failed to load objects: %v", err) } // Attach the eBPF program to the tracepoint. tp, err := link.Tracepoint("syscalls", "sys_enter_openat", objs.TraceOpenat, nil) if err != nil { log.Fatalf("failed to attach tracepoint: %v", err) } defer tp.Close() log.Println("eBPF program attached. Waiting for events...") // ... // Log โ€œ/sys/kernel/debug/tracing/trace_pipe" output // ... }

Slide 24

Slide 24 text

@doniacld 3. Main Go Program: Attach the eBPF program to tracepoint func main() { // Allow the current process to lock memory for eBPF resources. if err := rlimit.RemoveMemlock(); err != nil { log.Fatalf("failed to remove memlock: %v", err) } var objs openfileObjects err := loadOpenfileObjects(&objs, nil) if err != nil { log.Fatalf("failed to load objects: %v", err) } // Attach the eBPF program to the tracepoint tp, err := link.Tracepoint("syscalls", "sys_enter_openat", objs.TraceOpenat, nil) if err != nil { log.Fatalf("failed to attach tracepoint: %v", err) } defer tp.Close() log.Println("eBPF program attached. Waiting for events...") // ... // Log โ€œ/sys/kernel/debug/tracing/trace_pipe" output // ... }

Slide 25

Slide 25 text

@doniacld Open File Tracing Demo

Slide 26

Slide 26 text

@doniacld Open File Tracing Demo

Slide 27

Slide 27 text

XDP hooks power ๓ฐญˆ https://blog.ippon.tech/ebpf-and-kernel-modules-whats-the-difference @doniacld

Slide 28

Slide 28 text

2. OBSERVABILITY @doniacld

Slide 29

Slide 29 text

Packet Counter with Maps Example https://github.com/doniacld/ebpf-4-gophers @doniacld

Slide 30

Slide 30 text

1. Write C Program: Define a Map to store the counter #include #include struct { __uint(type, BPF_MAP_TYPE_ARRAY); __type(key, __u32); __type(value, __u64); __uint(max_entries, 1); } pkt_count SEC(".maps"); // count_packets atomically increases a packet counter on every invocation. SEC("xdp") int count_packets() { __u32 key = 0; __u64 *count = bpf_map_lookup_elem(&pkt_count, &key); if (count) { __sync_fetch_and_add(count, 1); } return XDP_PASS; } char __license[] SEC("license") = "Dual MIT/GPL";

Slide 31

Slide 31 text

1. Write C Program: Increment packet counter #include #include struct { __uint(type, BPF_MAP_TYPE_ARRAY); __type(key, __u32); __type(value, __u64); __uint(max_entries, 1); } pkt_count SEC(".maps"); // count_packets atomically increases a packet counter on every invocation. SEC("xdp") int count_packets() { __u32 key = 0; __u64 *count = bpf_map_lookup_elem(&pkt_count, &key); if (count) { __sync_fetch_and_add(count, 1); } return XDP_PASS; } char __license[] SEC("license") = "Dual MIT/GPL";

Slide 32

Slide 32 text

2. Compile & Generate Go Objects: bpf2go //go:generate go run github.com/cilium/ebpf/cmd/bpf2go counter counter.c call clang under the hood

Slide 33

Slide 33 text

2. Generated Go Objects Objects to manipulate //go:generate go run github.com/cilium/ebpf/cmd/bpf2go counter counter.c // counterObjects contains all objects after they have been // loaded into the kernel. type counterObjects struct { counterPrograms counterMaps counterVariables }

Slide 34

Slide 34 text

2. Generated Go Objects // counterPrograms contains all programs after they have been loaded into the kernel. type counterPrograms struct { CountPackets *ebpf.Program `ebpf:"count_packets"` } Objects to manipulate // counterObjects contains all objects after they have been // loaded into the kernel. type counterObjects struct { counterPrograms counterMaps counterVariables } //go:generate go run github.com/cilium/ebpf/cmd/bpf2go counter counter.c eBPF program bytecodes

Slide 35

Slide 35 text

2. Generated Go Objects // counterPrograms contains all programs after they have been loaded into the kernel. type counterPrograms struct { CountPackets *ebpf.Program `ebpf:"count_packets"` } Objects to manipulate // counterObjects contains all objects after they have been // loaded into the kernel. type counterObjects struct { counterPrograms counterMaps counterVariables } //go:generate go run github.com/cilium/ebpf/cmd/bpf2go counter counter.c // counterMaps contains all maps after they have been loaded // into the kernel. type counterMaps struct { PktCount *ebpf.Map `ebpf:"pkt_count"` } eBPF program bytecodes Maps to read/write

Slide 36

Slide 36 text

3. Main Go Program: Load the eBPF program package main import ( // โ€ฆ "github.com/cilium/ebpf/link" "github.com/cilium/ebpf/rlimit" ) func main() { err := rlimit.RemoveMemlock() var objs counterObjects err := loadCounterObjects(&objs, nil) if err != nil { //โ€ฆ } defer objs.Close() ifname := "eth0" iface, err := net.InterfaceByName(ifname) if err != nil {//โ€ฆ} link, err := link.AttachXDP(link.XDPOptions{ Program: objs.CountPackets, Interface: iface.Index, }) defer link.Close()

Slide 37

Slide 37 text

3. Main Go Program: Choose an interface package main import ( // โ€ฆ "github.com/cilium/ebpf/link" "github.com/cilium/ebpf/rlimit" ) func main() { err := rlimit.RemoveMemlock() var objs counterObjects err := loadCounterObjects(&objs, nil) if err != nil { //โ€ฆ } defer objs.Close() ifname := "eth0" iface, err := net.InterfaceByName(ifname) if err != nil {//โ€ฆ} link, err := link.AttachXDP(link.XDPOptions{ Program: objs.CountPackets, Interface: iface.Index, }) defer link.Close()

Slide 38

Slide 38 text

3. Main Go Program: Attach the program to a hook package main import ( // โ€ฆ "github.com/cilium/ebpf/link" "github.com/cilium/ebpf/rlimit" ) func main() { err := rlimit.RemoveMemlock() var objs counterObjects err := loadCounterObjects(&objs, nil) if err != nil { //โ€ฆ } defer objs.Close() ifname := "eth0" iface, err := net.InterfaceByName(ifname) if err != nil {//โ€ฆ} link, err := link.AttachXDP(link.XDPOptions{ Program: objs.CountPackets, Interface: iface.Index, }) defer link.Close() ...

Slide 39

Slide 39 text

3. Main Go Program: Read from the map // Periodically fetch the packet counter from PktCount tick := time.Tick(time.Second) stop := make(chan os.Signal, 5) signal.Notify(stop, os.Interrupt) for { select { case <-tick: var count uint64 err := objs.PktCount.Lookup(uint32(0), &count) if err != nil { log.Fatal("Map lookup:", err) } log.Printf("Received %d packets", count) case <-stop: log.Print("Received signal, exiting..") return } } }

Slide 40

Slide 40 text

3. Main Go Program: Log // Periodically fetch the packet counter from PktCount tick := time.Tick(time.Second) stop := make(chan os.Signal, 5) signal.Notify(stop, os.Interrupt) for { select { case <-tick: var count uint64 err := objs.PktCount.Lookup(uint32(0), &count) if err != nil { log.Fatal("Map lookup:", err) } log.Printf("Received %d packets", count) case <-stop: log.Print("Received signal, exiting..") return } }

Slide 41

Slide 41 text

Packet Counter Observability Demo @doniacld

Slide 42

Slide 42 text

@doniacld Packet Counter Observability Demo

Slide 43

Slide 43 text

Packet Counter Observability Demo โœ… @doniacld

Slide 44

Slide 44 text

3. SECURITY @doniacld

Slide 45

Slide 45 text

XDP Drop Example https://github.com/doniacld/ebpf-4-gophers @doniacld

Slide 46

Slide 46 text

Example: XDP Drop packets SEC("xdp") int xdp_drop(struct xdp_md *ctx) { char msg[] = "Hello, World!"; bpf_trace_printk(msg, sizeof(msg)); // Print to trace pipe void *data = (void *)(long)ctx->data; void *data_end = (void *)(long)ctx->data_end; // Check if the packet's destination IP is blocked if (isBlockedIP(data, data_end)) { char msg[] = "Got ping packet"; bpf_trace_printk(msg, sizeof(msg)); // Print to trace pipe return XDP_DROP; } return XDP_PASS; // Allow the packet to pass } // License declaration char _license[] SEC("license") = "GPL"; @doniacld

Slide 47

Slide 47 text

Example: XDP Drop packets SEC("xdp") int xdp_drop(struct xdp_md *ctx) { char msg[] = "Hello, World!"; bpf_trace_printk(msg, sizeof(msg)); // Print to trace pipe void *data = (void *)(long)ctx->data; void *data_end = (void *)(long)ctx->data_end; // Check if the packet's destination IP is blocked if (isBlockedIP(data, data_end)) { char msg[] = "Got ping packet"; bpf_trace_printk(msg, sizeof(msg)); // Print to trace pipe return XDP_DROP; } return XDP_PASS; // Allow the packet to pass } // License declaration char _license[] SEC("license") = "GPL"; @doniacld

Slide 48

Slide 48 text

XDP drop Security Example @doniacld

Slide 49

Slide 49 text

XDP Drop Security Demo โœ… @doniacld

Slide 50

Slide 50 text

4. NETWORKING Real Use Case @doniacld

Slide 51

Slide 51 text

Overview of Cilium @doniacld

Slide 52

Slide 52 text

Efficient Networking: Cilium use of @doniacld

Slide 53

Slide 53 text

Cilium Network Policy ๎‚YAML๎‚‚ @doniacld apiVersion: "cilium.io/v2" kind: CiliumNetworkPolicy metadata: name: "deny-external" spec: endpointSelector: matchLabels: app: "backend" ingress: - fromEndpoints: - matchLabels: app: "frontend" frontend pod backend pod frontend pod frontend pods backend pod backend pods external pod external pods ๐Ÿ’ก Endpoint = Pod

Slide 54

Slide 54 text

From Endpoint to Identity ๎‚Go๎‚‚ @doniacld type Endpoint struct { ID int Labels map[string]string Identity int } ๐Ÿ’ก Endpoint = Pod Endpoints 1: {โ€œappโ€:โ€frontendโ€} 2: {โ€œappโ€:โ€backendโ€} type Policy struct { SourceIdentity int DestinationIdentity int Allowed bool } Policy src: 1 dst: 2 true

Slide 55

Slide 55 text

Store Policies @doniacld struct bpf_map_def SEC("maps") policy_map = { .type = BPF_MAP_TYPE_HASH, .key_size = sizeof(int), .value_size = sizeof(int), .max_entries = 1024, }; eBPF map to store policies

Slide 56

Slide 56 text

Store Policies @doniacld func loadPoliciesToMap(policies []Policy, policyMap *ebpf.Map) error { for _, policy := range policies { err := policyMap.Put(policy.SourceIdentity, policy.DestinationIdentity) if err != nil { return err } } return nil } struct bpf_map_def SEC("maps") policy_map = { .type = BPF_MAP_TYPE_HASH, .key_size = sizeof(int), .value_size = sizeof(int), .max_entries = 1024, }; eBPF map to store policies Function to load policies into the eBPF map

Slide 57

Slide 57 text

eBPF program to enforce policies @doniacld // eBPF map to store policies. struct bpf_map_def SEC("maps") policy_map = { .type = BPF_MAP_TYPE_HASH, .key_size = sizeof(int), .value_size = sizeof(int), .max_entries = 1024, }; // eBPF program to enforce policies. SEC("xdp") int enforce_policy(struct xdp_md *ctx) { int src_identity = get_source_identity(ctx); int dst_identity = get_destination_identity(ctx); int *allowed = bpf_map_lookup_elem(&policy_map, &src_identity); if (allowed && *allowed == dst_identity) { // Allow the packet. return XDP_PASS; } // Drop the packet. return XDP_DROP; }

Slide 58

Slide 58 text

Traffic flows routed with Cilium as CNI @doniacld

Slide 59

Slide 59 text

NETWORKING Cilium @doniacld

Slide 60

Slide 60 text

๐Ÿ“ Key Takeaways @doniacld โ— eBPF lets you run safe, fast programs in the Linux kernel

Slide 61

Slide 61 text

๐Ÿ“ Key Takeaways @doniacld โ— eBPF lets you run safe, fast programs in the Linux kernel โ— You can use it for: โ—‹ ๐Ÿ” Tracing (e.g. file open events) โ—‹ ๐Ÿ“Š Observability (e.g. counting packets by IP๎‚‚ โ—‹ ๐Ÿ›ก Security (e.g. dropping packets with XDP๎‚‚ โ—‹ ๐ŸŒ Networking (e.g. packets routing with Cilium)

Slide 62

Slide 62 text

๐Ÿ“ Key Takeaways @doniacld โ— eBPF lets you run safe, fast programs in the Linux kernel โ— You can use it for: โ—‹ ๐Ÿ” Tracing (e.g. file open events) โ—‹ ๐Ÿ“Š Observability (e.g. counting packets by IP๎‚‚ โ—‹ ๐Ÿ›ก Security (e.g. dropping packets with XDP๎‚‚ โ—‹ ๐ŸŒ Networking (e.g. packets routing with Cilium) โ— Go is the language of the cloud-native ecosystem

Slide 63

Slide 63 text

๐Ÿ“ Key Takeaways @doniacld โ— eBPF lets you run safe, fast programs in the Linux kernel โ— You can use it for: โ—‹ ๐Ÿ” Tracing (e.g. file open events) โ—‹ ๐Ÿ“Š Observability (e.g. counting packets by IP๎‚‚ โ—‹ ๐Ÿ›ก Security (e.g. dropping packets with XDP๎‚‚ โ—‹ ๐ŸŒ Networking (e.g. packets routing with Cilium) โ— Go is the language of the cloud-native ecosystem โ— With libraries like cilium/ebpf, you can use Go to: โ—‹ Load eBPF programs & Interact with kernel data safely โ— Perfect fit for building Kubernetes CNI, agents, operators, and tools, etc

Slide 64

Slide 64 text

Thank you! ebpf.io @ciliumproject @isovalent isovalent.com/labs/ebpf-getting-started/ github.com/doniacld/ebpf-4-gophers/ Letสผs connect @doniacld

Slide 65

Slide 65 text

How works under the hood? @doniacld

Slide 66

Slide 66 text

How works under the hood? @doniacld

Slide 67

Slide 67 text

How works under the hood? @doniacld

Slide 68

Slide 68 text

How works under the hood? @doniacld

Slide 69

Slide 69 text

How works under the hood? @doniacld

Slide 70

Slide 70 text

How works under the hood? @doniacld