SlideShare a Scribd company logo
⾃⼰的eBPF程式⾃⼰做
Create an eBPF program by yourself
Huai-En Tseng
About me
• Huai-En Tseng 曾懷恩

• ChungHwa telecommunication laboratory.

• Associate researcher in Broadband
networks laboratory

• Focus on virtualization, high performance
computing, Linux kernel, system
programming optimization, network protocol
implementation, SDN

• Github: https://github.com/w180112

• Linkedin: https://www.linkedin.com/in/huai-
en-tseng-a10975157/
Contents
• What's eBPF/BPF?

• How to build eBPF from in-kernel source

• Program an eBPF program by hand

• Quick demo
What's eBPF/BPF?
• Berkeley Packet Filter

• in-kernel virtual machine

• basement of tcpdump and Wireshark 

• invented in 1992 at USENIX conference

• BSD socket provides BPF injection custom rules
• Main idea: copy and filter
What's eBPF/BPF?
What's eBPF/BPF?
• extend BPF

• from filter to monitoring, traffic control, kernel tracing

• high level c language to inject the BPF pseudo code

• kernel space - user space can exchange info using BPF map structure

• compiled by llvm/clang, in-kernel verifier

• the traditional BPF is also called classic BPF(cBPF)
eBPF machanism
• An eBPF program can be split into 2 parts

• user space BPF loader

• kernel space BPF elf program

• BPF loader loads BPF program into

kernel space 

• Then BPF program can be executed in in-kernel
BPF virtual machine
eBPF types
• eBPF supports many different features

• kernel tracing

• network monitoring

• traffic control 

• eXpress Data Path

• increasing in each kernel version from v3.17
eBPF types listed in /include/uapi/inux/bpf.h

in kernel version v5.8.9
eBPF maps
• In eBPF, there are several maps structures

• Unlike cBPF using recv(), eBPF exchange
information between kernel space eBPF
program and user space BPF loader

• BPF_MAP_TYPE_ARRAY,
BPF_MAP_TYPE_PERCPU_ARRAY

• BPF_MAP_TYPE_HASH,
BPF_MAP_TYPE_PERCPU_HASH

• and others
eBPF maps listed in /include/uapi/inux/bpf.h

in kernel version v5.8.9
What's eBPF/BPF? - XDP
What's eBPF/BPF? - XDP
Contents
• What's eBPF/BPF?

• How to build eBPF from in-kernel source

• Program an eBPF program by hand

• Quick demo
eBPF tools
• BCC

• TC

• iproute2

• In-kernel source
in-kernel eBPF examples
• Many eBPF example source code is included in Linux kernel source code
under samples/bpf/

• and can be compiled by its own makefile
How to compile
• How to compile in-kernel eBPF source code? (Ubuntu 18.04)

• prerequisite

• verify your kernel version and download the kernel source code
match to your kernel version

• install required packages

• cd to /usr/src/linux-source-5.0.0/linux-source-5.0.0/ and start to
compile
Contents
• What's eBPF/BPF?

• How to build eBPF from in-kernel source

• Program an eBPF program by hand

• Quick demo
eBPF program analyzing
• Each eBPF program has hook point and type, programmer should define
the type in eBPF loader

• eBPF loader will look for SEC() to find eBPF hook point function definition

• The hook point type is depends on what types of eBPF in eBPF loader

• e.g. in XDP eBPF program source code, the parameter of hook point
function is a struct xdp_md pointer variable
eBPF program analyzing - using XDP
head of packet
tail of packet
eBPF program analyzing - using XDP
• Each XDP program should return XDP_* value at the end of XDP function
definition
drop packet directly
allow packet go through 

into network stack
eBPF program analyzing - using XDP
• Our eBPF program is just like this so far.
• Now, let's start to add some code. First, we need to get the packet we
receive
eBPF program analyzing - using XDP
• Next, we can add whatever we want to
implement in this XDP program

• For this example, we try to filter and
drop incoming packets which are UDP
and port 55688
eBPF program analyzing
• We sometimes want to exchange data between user space eBPF loader
using MAP structure

• In this example, we try to statistic each incoming udp packet and store
into the map structure
Atomic operation
eBPF loader analyzing
• In eBPF loader, there are several steps to load eBPF program:

• find eBPF elf file and load the eBPF file file

• bpf_prog_load_xattr()

• find the hook point in eBPF program - the string in SEC()

• bpf_object__find_program_by_title()

• load the hook point function followed by the SEC()

• bpf_program__fd()

• In XDP loader, we need to attach the XDP program to network interface

• bpf_set_link_xdp_fd()
eBPF loader analyzing
• If the map structure is used, we should:

• find whether there is map in eBPF program and the map if so

• bpf_map__next()

• bpf_map__fd()

• set the entries in the map to 0

• bpf_map_update_elem()

• Then we can fetch the value in the map in each entry

• bpf_map_lookup_elem()
Modify the makefile in kernel source
• This makefile uses kbuild system to compile.

• Define the compile executable file name 

• hostprogs-y += get_pkts

• Define the object files loader needs

• get_pkts-objs := bpf_load.o get_pkts_user.o

• Add always variable to compile elf file

• always += get_pkts_kern.o
Contents
• What's eBPF/BPF?

• How to build eBPF from in-kernel source

• Program an eBPF program by hand

• Quick demo
Quick demo
Thanks for attending

More Related Content

PPTX
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
PDF
Systems@Scale 2021 BPF Performance Getting Started
PDF
eBPF/XDP
PDF
Velocity 2017 Performance analysis superpowers with Linux eBPF
PDF
BPF Hardware Offload Deep Dive
PDF
eBPF - Rethinking the Linux Kernel
PDF
Replacing iptables with eBPF in Kubernetes with Cilium
PDF
Building Network Functions with eBPF & BCC
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
Systems@Scale 2021 BPF Performance Getting Started
eBPF/XDP
Velocity 2017 Performance analysis superpowers with Linux eBPF
BPF Hardware Offload Deep Dive
eBPF - Rethinking the Linux Kernel
Replacing iptables with eBPF in Kubernetes with Cilium
Building Network Functions with eBPF & BCC

What's hot (20)

PDF
DockerCon 2017 - Cilium - Network and Application Security with BPF and XDP
PDF
Linux Profiling at Netflix
PDF
EBPF and Linux Networking
PDF
Introduction of eBPF - 時下最夯的Linux Technology
PPTX
Understanding eBPF in a Hurry!
ODP
eBPF maps 101
PPTX
eBPF Basics
ODP
Dpdk performance
PDF
Introduction to eBPF
PDF
Kernel Recipes 2017 - EBPF and XDP - Eric Leblond
PDF
Enhancing Network and Runtime Security with Cilium and Tetragon by Raymond De...
PDF
Introduction to eBPF and XDP
PDF
BPF Internals (eBPF)
PDF
VMware ESXi - Intel and Qlogic NIC throughput difference v0.6
PDF
Kubernetes Networking with Cilium - Deep Dive
PPTX
Staring into the eBPF Abyss
PDF
DPDK QoS
PDF
Meet cute-between-ebpf-and-tracing
PDF
DPDK: Multi Architecture High Performance Packet Processing
PDF
Performance Wins with eBPF: Getting Started (2021)
DockerCon 2017 - Cilium - Network and Application Security with BPF and XDP
Linux Profiling at Netflix
EBPF and Linux Networking
Introduction of eBPF - 時下最夯的Linux Technology
Understanding eBPF in a Hurry!
eBPF maps 101
eBPF Basics
Dpdk performance
Introduction to eBPF
Kernel Recipes 2017 - EBPF and XDP - Eric Leblond
Enhancing Network and Runtime Security with Cilium and Tetragon by Raymond De...
Introduction to eBPF and XDP
BPF Internals (eBPF)
VMware ESXi - Intel and Qlogic NIC throughput difference v0.6
Kubernetes Networking with Cilium - Deep Dive
Staring into the eBPF Abyss
DPDK QoS
Meet cute-between-ebpf-and-tracing
DPDK: Multi Architecture High Performance Packet Processing
Performance Wins with eBPF: Getting Started (2021)
Ad

Similar to Meetup 2009 (20)

PDF
DEF CON 27 - JEFF DILEO - evil e bpf in depth
PDF
The Open Source Ecosystem for eBPF in Kubernetes
PPTX
Dataplane programming with eBPF: architecture and tools
PDF
story_of_bpf-1.pdf
PDF
Ebpf ovsconf-2016
PDF
Calico-eBPF-Dataplane-CNCF-Webinar-Slides.pdf
PDF
Kernel bug hunting
PDF
ebpf and IO Visor: The What, how, and what next!
PPTX
eBPF Workshop
PPTX
Compiling P4 to XDP, IOVISOR Summit 2017
PDF
Building Embedded Linux Full Tutorial for ARM
PDF
Make Your Containers Faster: Linux Container Performance Tools
PPT
Embedded c & working with avr studio
PDF
Transparent eBPF Offload: Playing Nice with the Linux Kernel
PDF
P4, EPBF, and Linux TC Offload
PDF
Kernel Recipes 2019 - BPF at Facebook
PDF
Packaging perl (LPW2010)
PDF
BPF - in-kernel virtual machine
PDF
eBPF Debugging Infrastructure - Current Techniques
PDF
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
DEF CON 27 - JEFF DILEO - evil e bpf in depth
The Open Source Ecosystem for eBPF in Kubernetes
Dataplane programming with eBPF: architecture and tools
story_of_bpf-1.pdf
Ebpf ovsconf-2016
Calico-eBPF-Dataplane-CNCF-Webinar-Slides.pdf
Kernel bug hunting
ebpf and IO Visor: The What, how, and what next!
eBPF Workshop
Compiling P4 to XDP, IOVISOR Summit 2017
Building Embedded Linux Full Tutorial for ARM
Make Your Containers Faster: Linux Container Performance Tools
Embedded c & working with avr studio
Transparent eBPF Offload: Playing Nice with the Linux Kernel
P4, EPBF, and Linux TC Offload
Kernel Recipes 2019 - BPF at Facebook
Packaging perl (LPW2010)
BPF - in-kernel virtual machine
eBPF Debugging Infrastructure - Current Techniques
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Ad

Recently uploaded (20)

PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
System and Network Administraation Chapter 3
PPT
Introduction Database Management System for Course Database
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
ai tools demonstartion for schools and inter college
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Transform Your Business with a Software ERP System
PDF
Digital Strategies for Manufacturing Companies
PPTX
Introduction to Artificial Intelligence
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ISO 45001 Occupational Health and Safety Management System
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
System and Network Administraation Chapter 3
Introduction Database Management System for Course Database
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Odoo POS Development Services by CandidRoot Solutions
CHAPTER 2 - PM Management and IT Context
ai tools demonstartion for schools and inter college
PTS Company Brochure 2025 (1).pdf.......
VVF-Customer-Presentation2025-Ver1.9.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Transform Your Business with a Software ERP System
Digital Strategies for Manufacturing Companies
Introduction to Artificial Intelligence

Meetup 2009

  • 1. ⾃⼰的eBPF程式⾃⼰做 Create an eBPF program by yourself Huai-En Tseng
  • 2. About me • Huai-En Tseng 曾懷恩 • ChungHwa telecommunication laboratory. • Associate researcher in Broadband networks laboratory • Focus on virtualization, high performance computing, Linux kernel, system programming optimization, network protocol implementation, SDN • Github: https://github.com/w180112 • Linkedin: https://www.linkedin.com/in/huai- en-tseng-a10975157/
  • 3. Contents • What's eBPF/BPF? • How to build eBPF from in-kernel source • Program an eBPF program by hand • Quick demo
  • 4. What's eBPF/BPF? • Berkeley Packet Filter • in-kernel virtual machine • basement of tcpdump and Wireshark • invented in 1992 at USENIX conference • BSD socket provides BPF injection custom rules
  • 5. • Main idea: copy and filter What's eBPF/BPF?
  • 6. What's eBPF/BPF? • extend BPF • from filter to monitoring, traffic control, kernel tracing • high level c language to inject the BPF pseudo code • kernel space - user space can exchange info using BPF map structure • compiled by llvm/clang, in-kernel verifier • the traditional BPF is also called classic BPF(cBPF)
  • 7. eBPF machanism • An eBPF program can be split into 2 parts • user space BPF loader • kernel space BPF elf program • BPF loader loads BPF program into
 kernel space • Then BPF program can be executed in in-kernel BPF virtual machine
  • 8. eBPF types • eBPF supports many different features • kernel tracing • network monitoring • traffic control • eXpress Data Path • increasing in each kernel version from v3.17 eBPF types listed in /include/uapi/inux/bpf.h
 in kernel version v5.8.9
  • 9. eBPF maps • In eBPF, there are several maps structures • Unlike cBPF using recv(), eBPF exchange information between kernel space eBPF program and user space BPF loader • BPF_MAP_TYPE_ARRAY, BPF_MAP_TYPE_PERCPU_ARRAY • BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_PERCPU_HASH • and others eBPF maps listed in /include/uapi/inux/bpf.h
 in kernel version v5.8.9
  • 12. Contents • What's eBPF/BPF? • How to build eBPF from in-kernel source • Program an eBPF program by hand • Quick demo
  • 13. eBPF tools • BCC • TC • iproute2 • In-kernel source
  • 14. in-kernel eBPF examples • Many eBPF example source code is included in Linux kernel source code under samples/bpf/ • and can be compiled by its own makefile
  • 15. How to compile • How to compile in-kernel eBPF source code? (Ubuntu 18.04) • prerequisite • verify your kernel version and download the kernel source code match to your kernel version • install required packages • cd to /usr/src/linux-source-5.0.0/linux-source-5.0.0/ and start to compile
  • 16. Contents • What's eBPF/BPF? • How to build eBPF from in-kernel source • Program an eBPF program by hand • Quick demo
  • 17. eBPF program analyzing • Each eBPF program has hook point and type, programmer should define the type in eBPF loader • eBPF loader will look for SEC() to find eBPF hook point function definition • The hook point type is depends on what types of eBPF in eBPF loader • e.g. in XDP eBPF program source code, the parameter of hook point function is a struct xdp_md pointer variable
  • 18. eBPF program analyzing - using XDP head of packet tail of packet
  • 19. eBPF program analyzing - using XDP • Each XDP program should return XDP_* value at the end of XDP function definition drop packet directly allow packet go through 
 into network stack
  • 20. eBPF program analyzing - using XDP • Our eBPF program is just like this so far. • Now, let's start to add some code. First, we need to get the packet we receive
  • 21. eBPF program analyzing - using XDP • Next, we can add whatever we want to implement in this XDP program • For this example, we try to filter and drop incoming packets which are UDP and port 55688
  • 22. eBPF program analyzing • We sometimes want to exchange data between user space eBPF loader using MAP structure • In this example, we try to statistic each incoming udp packet and store into the map structure Atomic operation
  • 23. eBPF loader analyzing • In eBPF loader, there are several steps to load eBPF program: • find eBPF elf file and load the eBPF file file • bpf_prog_load_xattr() • find the hook point in eBPF program - the string in SEC() • bpf_object__find_program_by_title() • load the hook point function followed by the SEC() • bpf_program__fd() • In XDP loader, we need to attach the XDP program to network interface • bpf_set_link_xdp_fd()
  • 24. eBPF loader analyzing • If the map structure is used, we should: • find whether there is map in eBPF program and the map if so • bpf_map__next() • bpf_map__fd() • set the entries in the map to 0 • bpf_map_update_elem() • Then we can fetch the value in the map in each entry • bpf_map_lookup_elem()
  • 25. Modify the makefile in kernel source • This makefile uses kbuild system to compile. • Define the compile executable file name • hostprogs-y += get_pkts • Define the object files loader needs • get_pkts-objs := bpf_load.o get_pkts_user.o • Add always variable to compile elf file • always += get_pkts_kern.o
  • 26. Contents • What's eBPF/BPF? • How to build eBPF from in-kernel source • Program an eBPF program by hand • Quick demo