11# -*- fill-column: 76; -*-
2- #+TITLE: Tutorial: Basic01
2+ #+TITLE: Tutorial: Basic01 - loading your first BPF program
33#+OPTIONS: ^:nil
44
5- Welcome to the first step in this XDP- tutorial.
5+ Welcome to the first step in this XDP tutorial.
66
7- The programming language for XDP is eBPF (Extended Berkeley Packet Filter) which
8- we will just refer to as BPF. Thus, this tutorial will also be relevant for
9- learning how to write other BPF program. The focus is on BPF programs
10- that can be used in the XDP-hook.
7+ The programming language for XDP is eBPF (Extended Berkeley Packet Filter)
8+ which we will just refer to as BPF. Thus, this tutorial will also be
9+ relevant for learning how to write other BPF programs; however, the main
10+ focus is on BPF programs that can be used in the XDP-hook. In this and the
11+ following couple of lessons we will be focusing on the basics to get up and
12+ running with BPF; the later lessons will then build on this to teach you how
13+ to do packet processing with XDP.
14+
15+ Since this is the first lesson, we will start out softly by not actually
16+ including any assignments. Instead, just read the text below and make sure
17+ you can load the program and that you understand what is going on.
1118
1219* Table of Contents :TOC:
1320- [[#first-step-setup-dependencies][First step: setup dependencies]]
1421- [[#compiling-example-code][Compiling example code]]
1522 - [[#simple-xdp-code][Simple XDP code]]
1623 - [[#compiling-process][Compiling process]]
1724 - [[#looking-into-the-bpf-elf-object][Looking into the BPF-ELF object]]
18- - [[#loading-and-xdp-hook][Loading and XDP- hook]]
25+ - [[#loading-and-the- xdp-hook][Loading and the XDP hook]]
1926 - [[#loading-via-iproute2-ip][Loading via iproute2 ip]]
2027 - [[#loading-using-xdp_pass_user][Loading using xdp_pass_user]]
2128
2229* First step: setup dependencies
2330
2431There are a number of setup dependencies, that are needed in order to
25- compile the source code in this git- repository. Please go read and complete
26- the [[file:../setup_dependencies.org]] guide.
32+ compile the source code in this git repository. Please go read and complete
33+ the [[file:../setup_dependencies.org]] guide if you haven't already .
2734
28- Return here, and see if the next step compiles.
35+ Then return here, and see if the next step compiles.
2936
3037* Compiling example code
3138
@@ -50,14 +57,14 @@ int xdp_prog_simple(struct xdp_md *ctx)
5057** Compiling process
5158
5259The LLVM+clang compiler turns this restricted-C code into BPF-byte-code and
53- stores it in an ELF- object file, named =xdp_pass_kern.o=.
60+ stores it in an ELF object file, named =xdp_pass_kern.o=.
5461
5562** Looking into the BPF-ELF object
5663
5764You can inspect the contents of the =xdp_pass_kern.o= file with different
5865tools like =readelf= or =llvm-objdump=. As the Makefile enables the debug
59- option =-g= (LLVM version >= 4.0), llvm-objdump tool can annotate assembler
60- output with the original C code:
66+ option =-g= (LLVM version >= 4.0), the llvm-objdump tool can annotate
67+ assembler output with the original C code:
6168
6269Run: =llvm-objdump -S xdp_pass_kern.o=
6370#+begin_example asm
@@ -71,20 +78,21 @@ xdp_prog_simple:
7178 1: 95 00 00 00 00 00 00 00 exit
7279#+end_example
7380
74- If you don't want to see the raw- BPF instrutions add: =-no-show-raw-insn=.
81+ If you don't want to see the raw BPF instructions add: =-no-show-raw-insn=.
7582The define/enum XDP_PASS has a value of 2, as can be seen in the dump. The
7683section name "xdp" was defined by =SEC("xdp")=, and the =xdp_prog_simple:=
7784is our C-function name.
7885
79- * Loading and XDP- hook
86+ * Loading and the XDP hook
8087
81- As you should understand by now, the BPF-byte-code is stored in an ELF file.
82- To load this into the kernel, userspace needs an ELF loader. The *libbpf*
83- library provides both an ELF loader and several XDP helper functions. In
84- this tutorial you will learn how to write C-code using this library, which
85- is where our libelf-devel dependency comes from.
88+ As you should understand by now, the BPF byte code is stored in an ELF file.
89+ To load this into the kernel, userspace needs an ELF loader to read the file
90+ and pass it into the kernel in the right format. The *libbpf* library
91+ provides both an ELF loader and several XDP helper functions. In this
92+ tutorial you will learn how to write C code using this library, which is
93+ where our libelf-devel dependency comes from.
8694
87- The C- code in [[file:xdp_pass_user.c]] (which gets compiled to the program
95+ The C code in [[file:xdp_pass_user.c]] (which gets compiled to the program
8896=xdp_pass_user=) shows how to write a BPF loader specifically for our
8997=xdp_pass_kern.o= ELF file. This loader attached the program in the ELF file
9098as an XDP hook on a network device.
@@ -95,9 +103,9 @@ It does seem overkill to write a C program to simply load and attach a
95103specific BPF-program. However, we still include this in the the tutorial
96104since it will help you integrate BPF into other Open Source projects.
97105
98- As an alternative to writing a new loader, the standard iproute2 tool, also
99- contains a BPF ELF loader, although it is not based on libbpf, which
100- unfortunately makes it incompatible when starting to use BPF- maps.
106+ As an alternative to writing a new loader, the standard iproute2 tool also
107+ contains a BPF ELF loader. However, this loader is not based on libbpf,
108+ which unfortunately makes it incompatible when starting to use BPF maps.
101109
102110The iproute2 loader can be used with the standard =ip= tool; so in this case
103111you can actually load our ELF-file =xdp_pass_kern.o= (where we named our
@@ -107,7 +115,7 @@ ELF section "xdp") like this:
107115 ip link set dev lo xdpgeneric obj xdp_pass_kern.o sec xdp
108116#+end_example
109117
110- Listing the device via =ip link show= also show the XDP info:
118+ Listing the device via =ip link show= also shows the XDP info:
111119
112120#+begin_example sh
113121$ ip link show dev lo
0 commit comments