Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 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หฎ FOSDEM Feb 1, 2025

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

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

Slide 9

Slide 9 text

Example: 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(struct trace_event_raw_sys_enter* 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 10

Slide 10 text

Example: 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(struct trace_event_raw_sys_enter* 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 11

Slide 11 text

Example: 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(struct trace_event_raw_sys_enter* 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 12

Slide 12 text

Tracing Demo @doniacld

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Why for Kubernetes? @doniacld

Slide 16

Slide 16 text

Why for Kubernetes? @doniacld

Slide 17

Slide 17 text

Efficient Networking: Cilium use of @doniacld

Slide 18

Slide 18 text

How works under the hood? @doniacld

Slide 19

Slide 19 text

How works under the hood? @doniacld

Slide 20

Slide 20 text

How works under the hood? @doniacld

Slide 21

Slide 21 text

How works under the hood? @doniacld

Slide 22

Slide 22 text

How works under the hood? @doniacld

Slide 23

Slide 23 text

How works under the hood? @doniacld

Slide 24

Slide 24 text

Develop with @doniacld C Code ELF Object Linux Kernel clang

Slide 25

Slide 25 text

Bytecode ๐Ÿท๐Ÿถ๐Ÿท๐Ÿท Who wants to do that? ๐Ÿคช Write programs @doniacld C ๓ฐ˜ฌ โ— Huge ecosystem โ— Handle precisely low level resources ๐Ÿ“š Libraries โ— libbpf Rust ๐Ÿฆ€ โ— Memory safety โ— Concurrency made easy โ— Ecosystem in maturation โ— Verifier replaced ๐Ÿ“š Libraries โ— libbpf/libbpf-rs โ— aya

Slide 26

Slide 26 text

Maps in @doniacld C Code ELF Object Maps Linux Kernel clang 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 27

Slide 27 text

@doniacld C Code App Code Maps Linux Kernel Userspace load() read() write() syscalls ELF Object clang Write programs in userspace loading

Slide 28

Slide 28 text

Write programs in userspace loading @doniacld C ๓ฐ˜ฌ ๐Ÿ˜“ painful to run in high level app

Slide 29

Slide 29 text

Python ๐Ÿ โ— ๐Ÿ˜Œ Easy to start with high level language โ— Wrappers for functions and maps Write programs in userspace loading @doniacld C ๓ฐ˜ฌ ๐Ÿ˜“ painful to run in high level app

Slide 30

Slide 30 text

Python ๐Ÿ โ— ๐Ÿ˜Œ Easy to start with high level language โ— Wrappers for functions and maps Write programs in userspace loading @doniacld C ๓ฐ˜ฌ ๐Ÿ˜“ painful to run in high level app Go ๐Ÿฆซ โ— ๐Ÿ›  Simplicity: easy syntax โ— โšก Performance: efficient concurrency โ— Wrappers for functions and maps

Slide 31

Slide 31 text

Go Libraries for aquasecurity/libbpfgo โ— A Go wrapper around libbpf โ— Involves managing CGO and potential challenges with cross-compilation โ— Provides access to the latest eBPF features and updates through libbpf @doniacld cilium/ebpf โ— Load and attach eBPF programs โ— Interact with eBPF maps for dynamic data management โ— May not support the latest libpf features iovisor/gobpf โ— Go bindings for the bcc framework โ— Access to low-level routines โ— Involves managing CGO

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Packet counter: C eBPF program #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 34

Slide 34 text

Packet counter: C eBPF program #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 35

Slide 35 text

bpf2go: Compile to ELF objects //go:generate go run github.com/cilium/ebpf/cmd/bpf2go counter counter.c call clang under the hood

Slide 36

Slide 36 text

Generated Go Objects 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

Slide 37

Slide 37 text

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 38

Slide 38 text

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 39

Slide 39 text

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 40

Slide 40 text

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 41

Slide 41 text

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 42

Slide 42 text

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 43

Slide 43 text

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 44

Slide 44 text

Packet Counter Demo @doniacld

Slide 45

Slide 45 text

Tool Example Cilium @doniacld

Slide 46

Slide 46 text

Overview of Cilium @doniacld

Slide 47

Slide 47 text

Cilium Network Policy @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 48

Slide 48 text

From Endpoint to Identity @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 49

Slide 49 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 50

Slide 50 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 51

Slide 51 text

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