Staring into the eBPF Abyss

Sasha Goldshtein
Sasha GoldshteinCTO at SELA Group
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Staring into the eBPF Abyss
Sasha Goldshtein
CTO, Sela Group
@goldshtn
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Agenda
• Modern Linux tracing landscape
• BPF
• BCC – BPF Compiler Collection
• Using BCC tools
• Authoring BCC tools
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Prerequisites
• You should …
• Have experience developing on or administering a Linux deployment
• Be familiar with C/Python/Lua (a bonus)
• To use your own machines for this workshop …
• You will need Linux 4.6+
• Clone or install some open source tools (perf, bcc)
• You can also use the instructor-provided VirtualBox appliance or
Strigo workspace
• Instructions and labs:
https://github.com/goldshtn/linux-tracing-workshop
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Tracing on the Performance Spectrum
Invasiveness
Envelope
System completeness
Metrics &
simulations
Development Testing Production
Profilers
jprof, valgrind
Counters
top, vmstat
Debuggers
Function
tracers
Event tracers
Aggregators
SystemTap, BPF
Load
tools
ab
Light-weight profilers
perf
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Tracing Objectives
• Trace function execution, arguments, call graph
• Print lightweight log messages (kernel/user)
• Aggregate statistics (min/max/avg, histogram)
• Low overhead
• Continuous monitoring
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Linux Tracing Tools, Today
Ease of use
BPF/BCC
SysDig
ktap
SystemTap
LTTng
ftrace
perf
custom .ko
new stable dead
Level of detail, features
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Berkeley Packet Filters (BPF)
• Originally designed for, well, packet filtering:
dst port 80 and len >= 100
• Custom instruction set, interpreted/JIT compiled
0: (bf) r6 = r1
1: (85) call 14
2: (67) r0 <<= 32
3: (77) r0 >>= 32
4: (15) if r0 == 0x49f goto pc+40
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Extended BPF (3.18 and ongoing)
• Attach BPF programs to kprobes/uprobes (4.1) and tracepoints (4.7)
• Data structures: array, hash (expandable), stack map (4.6)
• Output to trace buffer (4.3) and perf cyclic buffer (4.4)
• Helper functions: get time, get current comm, get current CPU, etc.
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
BCC: BPF Compiler Collection
• Library and Python/Lua module for compiling, loading, and executing
BPF programs
• Compile BPF program from C source
• Attach BPF program to kprobe/uprobe/tracepoint/USDT/socket
• Poll data from BPF program using Python/Lua
• Can do in-kernel aggregation and filtering
• Growing collection of tracing, networking, and performance tools
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
BCC
• The BCC repository contains a variety of existing scripts and tools to
get you started
• The BPF module (Python/Lua) can be used to build new tools or one-
off scripts
$ ls *.py
argdist.py
bashreadline.py
biolatency.py
biosnoop.py
biotop.py
bitesize.py
btrfsdist.py
btrfsslower.py
cachestat.py
cpudist.py
dcsnoop.py
dcstat.py
execsnoop.py
ext4dist.py
ext4slower.py
filelife.py
fileslower.py
filetop.py
funccount.py
funclatency.py
gethostlatency.py
hardirqs.py
killsnoop.py
mdflush.py
memleak.py
offcputime.py
offwaketime.py
oomkill.py
opensnoop.py
pidpersec.py
runqlat.py
softirqs.py
solisten.py
stackcount.py
stacksnoop.py
statsnoop.py
syncsnoop.py
tcpaccept.py
tcpconnect.py
tcpconnlat.py
tcpretrans.py
tplist.py
trace.py
vfscount.py
vfsstat.py
wakeuptime.py
xfsdist.py
xfsslower.py
zfsdist.py
zfsslower.py
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Specialized Tools
# ./hardirqs.py
Tracing hard irq event time... Hit Ctrl-C to end.
^C
HARDIRQ TOTAL_usecs
virtio0-input.0 959
ahci[0000:00:1f.2] 1290
# ./biolatency.py
Tracing block device I/O... Hit Ctrl-C to end.
^C
usecs : count distribution
64 -> 127 : 7 |********* |
128 -> 255 : 14 |****************** |
256 -> 511 : 5 |****** |
512 -> 1023 : 30 |****************************************|
1024 -> 2047 : 1 |* |
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Specialized Tools
# ./filetop.py
01:35:51 loadavg: 0.01 0.04 0.03 2/139 3611
PID COMM READS WRITES R_Kb W_Kb T FILE
2496 sshd 3 1 48 0 O ptmx
2939 screen 4 1 16 0 O ptmx
2496 sshd 1 3 16 0 S TCP
3611 clear 2 0 8 0 R screen
2939 screen 1 3 4 0 O 0
3589 filetop.py 2 0 2 0 R loadavg
3611 clear 1 0 0 0 R libtinfo.so.5.9
3611 clear 1 0 0 0 R libc-2.21.so
3611 filetop.py 3 0 0 0 R clear
3611 filetop.py 2 0 0 0 R ld-2.21.so
3611 clear 0 1 0 0 O 2
3589 filetop.py 0 3 0 0 O 2
# ./cachestat.py
HITS MISSES DIRTIES READ_HIT% WRITE_HIT% BUFFERS_MB CACHED_MB
0 0 0 0.0% 0.0% 54 482
842 0 0 100.0% 0.0% 54 482
889 128 0 87.4% 12.6% 54 482
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Specialized Tools
# ./stackcount.py __kmalloc
Tracing 1 functions for "__kmalloc"... Hit Ctrl-C to end.
^C
__kmalloc
alloc_fdtable
dup_fd
copy_process.part.31
_do_fork
sys_clone
do_syscall_64
return_from_SYSCALL_64
4
__kmalloc
create_pipe_files
__do_pipe_flags
sys_pipe
entry_SYSCALL_64_fastpath
6
__kmalloc
htree_dirblock_to_tree
ext4_htree_fill_tree
ext4_readdir
iterate_dir
SyS_getdents
entry_SYSCALL_64_fastpath
14
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
BPF Tracing Targets (circa July 2016)
Target Support Overhead
kprobes Native Low
uprobes Native
Medium
handler runs in KM
Kernel tracepoints NativeNEW Low
USDT tracepoints
Temporary
through uprobes
Medium
handler runs in KM
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Multi-Tools: argdist
# ./argdist.py -C 'p:c:write(int fd, const void *buf,
size_t count):size_t:count:fd==1'
[01:49:00]
p:c:write(int fd, const void *buf, size_t count):size_t:count:fd==1
COUNT EVENT
1 count = 3134
1 count = 170
1 count = 181
2 count = 18
3 count = 30
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Multi-Tools: argdist
# ./argdist.py -i 5 -H 'r::__vfs_read(void *file, void *buf,
size_t count):size_t:$entry(count):$latency > 1000000'
[01:51:40]
count : count distribution
0 -> 1 : 20 |****************************************|
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 0 | |
16 -> 31 : 0 | |
32 -> 63 : 0 | |
64 -> 127 : 0 | |
128 -> 255 : 6 |************ |
256 -> 511 : 0 | |
512 -> 1023 : 0 | |
1024 -> 2047 : 1 |** |
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Multi-Tools: trace
# ./trace.py 'r:/usr/bin/bash:readline "%s", retval'
TIME PID COMM FUNC -
02:02:26 3711 bash readline ls –la
02:02:36 3711 bash readline wc -l src.c
# ./tplist.py -v block:block_rq_complete
block:block_rq_complete
dev_t dev;
sector_t sector;
unsigned int nr_sector;
int errors;
char rwbs[8];
# ./trace.py 't:block:block_rq_complete "sectors=%d", tp.nr_sector'
TIME PID COMM FUNC -
02:03:56 0 swapper/0 block_rq_complete sectors=16
02:03:56 0 swapper/0 block_rq_complete sectors=8
02:03:58 0 swapper/0 block_rq_complete sectors=24
02:04:00 0 swapper/0 block_rq_complete sectors=0
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Multi-Tools: trace
# ./tplist.py -l pthread -v libpthread:pthread_create
/usr/lib64/libpthread-2.21.so libpthread:pthread_create
location 0x7c63
8 unsigned bytes @ register %rax
8 unsigned bytes @ -192(%rbp)
8 unsigned bytes @ -168(%rbp)
8 unsigned bytes @ -176(%rbp)
# ./trace.py 'u:pthread:pthread_create "%llx", arg3'
TIME PID COMM FUNC -
02:07:29 4051 contentions pthread_create 400e00
02:07:29 4051 contentions pthread_create 400e00
02:07:29 4051 contentions pthread_create 400e00
02:07:29 4051 contentions pthread_create 400e00
^C
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Multi-Tools: trace
# trace -p $(pidof node) 'u:node:http__server__request
"%s %s (from %s:%d)" arg5, arg6, arg3, arg4'
TIME PID COMM FUNC -
04:50:44 22185 node http__server__request GET /foofoo (from ::1:51056)
04:50:46 22185 node http__server__request GET / (from ::1:51056)
^C
# ./trace.py 'u:/tmp/libjvm.so:thread__start "%s [%d]", arg1, arg4' 
'u:/tmp/libjvm.so:thread__stop "%s [%d]", arg1, arg4'
TIME PID COMM FUNC -
06:55:24 32157 java thread__start Reference Handler [32157]
06:55:24 32158 java thread__start Finalizer [32158]
06:55:24 32159 java thread__start Signal Dispatcher [32159]
06:55:24 32160 java thread__start C2 CompilerThread0 [32160]
06:55:24 32161 java thread__start C2 CompilerThread1 [32161]
06:55:24 32162 java thread__start C1 CompilerThread2 [32162]
06:55:24 32163 java thread__start Service Thread [32163]
06:55:28 32159 java thread__stop Signal Dispatcher [32159]
^C
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Lab
#3 – Chasing a C++ Memory Leak
#4 – MySQL and Disk Stats and Stacks
#5 – Node and JVM USDT Probes
https://s.sashag.net/sreconlabs
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Kernel
Custom Tool Design
BPF program
Tracepoint kprobe
Python/Lua driver
App process
uprobeUSDT
Probe handler
Probe handler
Hash or
histogram
Cyclic buffer
U
K
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
BPF Program: Counting Allocations
#include <linux/ptrace.h>
struct alloc_info_t {
u64 count;
u64 size;
};
BPF_HASH(allocs, u32, struct alloc_info_t);
int handler(struct pt_regs *ctx, size_t size) {
u32 pid = bpf_get_current_pid_tgid();
struct alloc_info_t init = { 0 }, *info;
info = allocs.lookup_or_init(&pid, &init);
info->count += 1;
info->size += size;
return 0;
}
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
BPF Driver
#!/usr/bin/env python
from bcc import BPF
from time import sleep
program = BPF(src_file="allocs.c")
program.attach_kprobe(event="__kmalloc", fn_name="handler")
allocs = program["allocs"]
while True:
sleep(5)
print("n%-8s %-8s %-10s" % ("PID", "COUNT", "SIZE"))
for key, value in sorted(allocs.items(), key=lambda (k, v): k.value):
print("%-8d %-8d %-8d" % (key.value, value.count, value.size))
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
BPF Execution
# ./allocs.py
PID COUNT SIZE
28064 3 456
28157 10 76
28158 5 1116
PID COUNT SIZE
28001 113 1828
28064 8 1216
28110 38 683
28157 46 328
28158 5 1116
28159 41 12894
^C
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Inline BPF Program
#!/usr/bin/env python
from bcc import BPF
from time import sleep
program = BPF(text="""BPF_HASH(counts, u32, u32);
TRACEPOINT_PROBE(irq, irq_handler_entry) {
u32 zero = 0, *existing, irq = args->irq;
existing = counts.lookup_or_init(&irq, &zero);
++(*existing);
return 0;
}""")
counts = program["counts"]
sleep(9999999)
print("n%-8s %-8s" % ("IRQ", "COUNT"))
for key, value in counts.items():
print("%-8d %-8d" % (key.value, value.value))
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Data Types
• Array
• Hash
• Histogram
• Perf buffer (4.4+)NEW
• Stack map (4.6+)NEW
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Example: Histogram
struct dist_key_t {
char op[OP_NAME_LEN];
u64 slot;
};
BPF_HISTOGRAM(dist, struct dist_key_t);
...
struct dist_key_t key = { .slot=bpf_log2l(elapsed_time) };
__builtin_memcpy(&key.op, op, sizeof(key.op));
dist.increment(key);
...
bpf.get_table("dist").print_log2_hist("operation")
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Example: Perf Buffer
bpf = BPF(text="""#include <linux/ptrace.h>
struct data_t { u64 pid; char str[80]; };
BPF_PERF_OUTPUT(events);
int print(struct pt_regs *ctx) {
struct data_t data = {0};
...
events.perf_submit(ctx, &data, sizeof(data));
return 0;
}""")
class Data(ct.Structure):
_fields_ = [ ("pid", ct.c_ulonglong), ("str", ct.c_char*80) ]
bpf.attach_uretprobe(name="/bin/bash", sym="readline", fn_name="print")
b["events"].open_perf_buffer(lambda cpu, data, size:
event = ct.cast(data, ct.POINTER(Data)).contents
print(event)
)
while True: bpf.kprobe_poll()
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Example: Stack Map
BPF_HASH(counts, int);
BPF_STACK_TRACE(stacks, 1024);
...
int key = stacks.get_stackid(ctx, BPF_F_REUSE_STACKID);
u64 zero = 0;
u64 *val = counts.lookup_or_init(&key, &zero);
++(*val);
...
counts, stacks = bpf["counts"], bpf["stacks"]
for k, v in counts:
for addr in stacks.walk(k.value):
print(BPF.ksym(addr))
print(v.value)
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Custom Tool Design Tips
• Try to perform all aggregations in the BPF program and keep UM
copying to a minimum
• Limit hash/histogram/stackmap sizes, prune, keep only top entries
• Clear cyclic buffer often and quickly
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Deployment
• For Python tools, deploy Python + libbcc.so
• For Lua tools, deploy only bcc-lua
• Statically links libbcc.a but allows plugging libbcc.so
• Kernel build flags:
• CONFIG_BPF=y
• CONFIG_BPF_SYSCALL=y
• CONFIG_BPF_JIT=y
• CONFIG_HAVE_BPF_JIT=y
• CONFIG_BPF_EVENTS=y
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Lab
#6 – Contention Stats and Stacks
#7 – From BCC GitHub Issues
https://s.sashag.net/sreconlabs
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Summary
• Tracing can identify bugs and performance issues that no debugger or
profiler can catch
• Tools make low-overhead, dynamic, production tracing possible
• BPF is the next-generation backend for Linux tracing tools
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
Thank You!
Sasha Goldshtein
@goldshtn
1 of 35

Recommended

Meet cute-between-ebpf-and-tracing by
Meet cute-between-ebpf-and-tracingMeet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingViller Hsiao
8.8K views75 slides
Linux BPF Superpowers by
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF SuperpowersBrendan Gregg
422.9K views60 slides
eBPF Trace from Kernel to Userspace by
eBPF Trace from Kernel to UserspaceeBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to UserspaceSUSE Labs Taipei
8.5K views74 slides
Understanding eBPF in a Hurry! by
Understanding eBPF in a Hurry!Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!Ray Jenkins
1.5K views77 slides
BPF Internals (eBPF) by
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)Brendan Gregg
15.3K views122 slides
Introduction to eBPF by
Introduction to eBPFIntroduction to eBPF
Introduction to eBPFRogerColl2
327 views19 slides

More Related Content

What's hot

BPF - in-kernel virtual machine by
BPF - in-kernel virtual machineBPF - in-kernel virtual machine
BPF - in-kernel virtual machineAlexei Starovoitov
11.7K views41 slides
Performance Wins with eBPF: Getting Started (2021) by
Performance Wins with eBPF: Getting Started (2021)Performance Wins with eBPF: Getting Started (2021)
Performance Wins with eBPF: Getting Started (2021)Brendan Gregg
1.4K views30 slides
Introduction to eBPF and XDP by
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDPlcplcp1
5.5K views57 slides
eBPF Basics by
eBPF BasicseBPF Basics
eBPF BasicsMichael Kehoe
2.7K views63 slides
eBPF - Rethinking the Linux Kernel by
eBPF - Rethinking the Linux KerneleBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KernelThomas Graf
1.2K views24 slides
Linux kernel tracing by
Linux kernel tracingLinux kernel tracing
Linux kernel tracingViller Hsiao
16.9K views70 slides

What's hot(20)

Performance Wins with eBPF: Getting Started (2021) by Brendan Gregg
Performance Wins with eBPF: Getting Started (2021)Performance Wins with eBPF: Getting Started (2021)
Performance Wins with eBPF: Getting Started (2021)
Brendan Gregg1.4K views
Introduction to eBPF and XDP by lcplcp1
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDP
lcplcp15.5K views
eBPF - Rethinking the Linux Kernel by Thomas Graf
eBPF - Rethinking the Linux KerneleBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux Kernel
Thomas Graf1.2K views
Linux kernel tracing by Viller Hsiao
Linux kernel tracingLinux kernel tracing
Linux kernel tracing
Viller Hsiao16.9K views
LinuxCon 2015 Linux Kernel Networking Walkthrough by Thomas Graf
LinuxCon 2015 Linux Kernel Networking WalkthroughLinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking Walkthrough
Thomas Graf26.3K views
The TCP/IP Stack in the Linux Kernel by Divye Kapoor
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
Divye Kapoor52.3K views
Faster packet processing in Linux: XDP by Daniel T. Lee
Faster packet processing in Linux: XDPFaster packet processing in Linux: XDP
Faster packet processing in Linux: XDP
Daniel T. Lee1.4K views
Linux 4.x Tracing: Performance Analysis with bcc/BPF by Brendan Gregg
Linux 4.x Tracing: Performance Analysis with bcc/BPFLinux 4.x Tracing: Performance Analysis with bcc/BPF
Linux 4.x Tracing: Performance Analysis with bcc/BPF
Brendan Gregg10.7K views
Cilium - BPF & XDP for containers by Docker, Inc.
 Cilium - BPF & XDP for containers Cilium - BPF & XDP for containers
Cilium - BPF & XDP for containers
Docker, Inc.5.7K views
Cilium - Container Networking with BPF & XDP by Thomas Graf
Cilium - Container Networking with BPF & XDPCilium - Container Networking with BPF & XDP
Cilium - Container Networking with BPF & XDP
Thomas Graf4.8K views
EBPF and Linux Networking by PLUMgrid
EBPF and Linux NetworkingEBPF and Linux Networking
EBPF and Linux Networking
PLUMgrid14.6K views
re:Invent 2019 BPF Performance Analysis at Netflix by Brendan Gregg
re:Invent 2019 BPF Performance Analysis at Netflixre:Invent 2019 BPF Performance Analysis at Netflix
re:Invent 2019 BPF Performance Analysis at Netflix
Brendan Gregg5.5K views
Linux Networking Explained by Thomas Graf
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
Thomas Graf25.6K views
DockerCon 2017 - Cilium - Network and Application Security with BPF and XDP by Thomas Graf
DockerCon 2017 - Cilium - Network and Application Security with BPF and XDPDockerCon 2017 - Cilium - Network and Application Security with BPF and XDP
DockerCon 2017 - Cilium - Network and Application Security with BPF and XDP
Thomas Graf10.1K views
Systems@Scale 2021 BPF Performance Getting Started by Brendan Gregg
Systems@Scale 2021 BPF Performance Getting StartedSystems@Scale 2021 BPF Performance Getting Started
Systems@Scale 2021 BPF Performance Getting Started
Brendan Gregg1.5K views

Viewers also liked

The Next Linux Superpower: eBPF Primer by
The Next Linux Superpower: eBPF PrimerThe Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF PrimerSasha Goldshtein
4.8K views24 slides
ebpf and IO Visor: The What, how, and what next! by
ebpf and IO Visor: The What, how, and what next!ebpf and IO Visor: The What, how, and what next!
ebpf and IO Visor: The What, how, and what next!Affan Syed
3.4K views38 slides
Linux Performance Analysis: New Tools and Old Secrets by
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsBrendan Gregg
603.9K views75 slides
Security Monitoring with eBPF by
Security Monitoring with eBPFSecurity Monitoring with eBPF
Security Monitoring with eBPFAlex Maestretti
7K views27 slides
Performance Tuning EC2 Instances by
Performance Tuning EC2 InstancesPerformance Tuning EC2 Instances
Performance Tuning EC2 InstancesBrendan Gregg
171.6K views81 slides
Blazing Performance with Flame Graphs by
Blazing Performance with Flame GraphsBlazing Performance with Flame Graphs
Blazing Performance with Flame GraphsBrendan Gregg
323.6K views170 slides

Viewers also liked(14)

The Next Linux Superpower: eBPF Primer by Sasha Goldshtein
The Next Linux Superpower: eBPF PrimerThe Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF Primer
Sasha Goldshtein4.8K views
ebpf and IO Visor: The What, how, and what next! by Affan Syed
ebpf and IO Visor: The What, how, and what next!ebpf and IO Visor: The What, how, and what next!
ebpf and IO Visor: The What, how, and what next!
Affan Syed3.4K views
Linux Performance Analysis: New Tools and Old Secrets by Brendan Gregg
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
Brendan Gregg603.9K views
Performance Tuning EC2 Instances by Brendan Gregg
Performance Tuning EC2 InstancesPerformance Tuning EC2 Instances
Performance Tuning EC2 Instances
Brendan Gregg171.6K views
Blazing Performance with Flame Graphs by Brendan Gregg
Blazing Performance with Flame GraphsBlazing Performance with Flame Graphs
Blazing Performance with Flame Graphs
Brendan Gregg323.6K views
Linux Systems Performance 2016 by Brendan Gregg
Linux Systems Performance 2016Linux Systems Performance 2016
Linux Systems Performance 2016
Brendan Gregg504.5K views
Broken Linux Performance Tools 2016 by Brendan Gregg
Broken Linux Performance Tools 2016Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016
Brendan Gregg822.8K views
Velocity 2015 linux perf tools by Brendan Gregg
Velocity 2015 linux perf toolsVelocity 2015 linux perf tools
Velocity 2015 linux perf tools
Brendan Gregg1.1M views
Extending Sysdig with Chisel by Sysdig
Extending Sysdig with ChiselExtending Sysdig with Chisel
Extending Sysdig with Chisel
Sysdig 941 views
From DTrace to Linux by Brendan Gregg
From DTrace to LinuxFrom DTrace to Linux
From DTrace to Linux
Brendan Gregg23.3K views
(PFC403) Maximizing Amazon S3 Performance | AWS re:Invent 2014 by Amazon Web Services
(PFC403) Maximizing Amazon S3 Performance | AWS re:Invent 2014(PFC403) Maximizing Amazon S3 Performance | AWS re:Invent 2014
(PFC403) Maximizing Amazon S3 Performance | AWS re:Invent 2014
Amazon Web Services49.5K views
ACM Applicative System Methodology 2016 by Brendan Gregg
ACM Applicative System Methodology 2016ACM Applicative System Methodology 2016
ACM Applicative System Methodology 2016
Brendan Gregg158.1K views
Linux 4.x Tracing Tools: Using BPF Superpowers by Brendan Gregg
Linux 4.x Tracing Tools: Using BPF SuperpowersLinux 4.x Tracing Tools: Using BPF Superpowers
Linux 4.x Tracing Tools: Using BPF Superpowers
Brendan Gregg210.2K views

Similar to Staring into the eBPF Abyss

Modern Linux Tracing Landscape by
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing LandscapeSasha Goldshtein
1.9K views30 slides
Origins of Serverless by
Origins of ServerlessOrigins of Serverless
Origins of ServerlessAndrii Soldatenko
361 views77 slides
Continuous SQL with Apache Streaming (FLaNK and FLiP) by
Continuous SQL with Apache Streaming (FLaNK and FLiP)Continuous SQL with Apache Streaming (FLaNK and FLiP)
Continuous SQL with Apache Streaming (FLaNK and FLiP)Timothy Spann
329 views16 slides
Performance Wins with BPF: Getting Started by
Performance Wins with BPF: Getting StartedPerformance Wins with BPF: Getting Started
Performance Wins with BPF: Getting StartedBrendan Gregg
2K views24 slides
Debugging 2013- Jesper Brouer by
Debugging 2013- Jesper BrouerDebugging 2013- Jesper Brouer
Debugging 2013- Jesper BrouerMediehuset Ingeniøren Live
320 views28 slides
MLflow with R by
MLflow with RMLflow with R
MLflow with RDatabricks
2.3K views30 slides

Similar to Staring into the eBPF Abyss(20)

Continuous SQL with Apache Streaming (FLaNK and FLiP) by Timothy Spann
Continuous SQL with Apache Streaming (FLaNK and FLiP)Continuous SQL with Apache Streaming (FLaNK and FLiP)
Continuous SQL with Apache Streaming (FLaNK and FLiP)
Timothy Spann329 views
Performance Wins with BPF: Getting Started by Brendan Gregg
Performance Wins with BPF: Getting StartedPerformance Wins with BPF: Getting Started
Performance Wins with BPF: Getting Started
Brendan Gregg2K views
MLflow with R by Databricks
MLflow with RMLflow with R
MLflow with R
Databricks2.3K views
OSSNA 2017 Performance Analysis Superpowers with Linux BPF by Brendan Gregg
OSSNA 2017 Performance Analysis Superpowers with Linux BPFOSSNA 2017 Performance Analysis Superpowers with Linux BPF
OSSNA 2017 Performance Analysis Superpowers with Linux BPF
Brendan Gregg5.1K views
FØCAL Boston AiR - Computer Vision Tracing and Hardware Simulation by FØCAL
FØCAL Boston AiR - Computer Vision Tracing and Hardware SimulationFØCAL Boston AiR - Computer Vision Tracing and Hardware Simulation
FØCAL Boston AiR - Computer Vision Tracing and Hardware Simulation
FØCAL152 views
The true story_of_hello_world by fantasy zheng
The true story_of_hello_worldThe true story_of_hello_world
The true story_of_hello_world
fantasy zheng2.5K views
BigDataFest Building Modern Data Streaming Apps by ssuser73434e
BigDataFest  Building Modern Data Streaming AppsBigDataFest  Building Modern Data Streaming Apps
BigDataFest Building Modern Data Streaming Apps
ssuser73434e6 views
Native support of Prometheus monitoring in Apache Spark 3 by Dongjoon Hyun
Native support of Prometheus monitoring in Apache Spark 3Native support of Prometheus monitoring in Apache Spark 3
Native support of Prometheus monitoring in Apache Spark 3
Dongjoon Hyun164 views
Tiny ML for spark Fun Edge by 艾鍗科技
Tiny ML for spark Fun EdgeTiny ML for spark Fun Edge
Tiny ML for spark Fun Edge
艾鍗科技778 views
Flink sql for continuous sql etl apps & Apache NiFi devops by Timothy Spann
Flink sql for continuous sql etl apps & Apache NiFi devopsFlink sql for continuous sql etl apps & Apache NiFi devops
Flink sql for continuous sql etl apps & Apache NiFi devops
Timothy Spann618 views
Metasepi team meeting #20: Start! ATS programming on MCU by Kiwamu Okabe
Metasepi team meeting #20: Start! ATS programming on MCUMetasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCU
Kiwamu Okabe14.8K views
Crash_Report_Mechanism_In_Tizen by Lex Yu
Crash_Report_Mechanism_In_TizenCrash_Report_Mechanism_In_Tizen
Crash_Report_Mechanism_In_Tizen
Lex Yu538 views
Piwik elasticsearch kibana at OSC Tokyo 2016 Spring by Takashi Yamamoto
Piwik elasticsearch kibana at OSC Tokyo 2016 SpringPiwik elasticsearch kibana at OSC Tokyo 2016 Spring
Piwik elasticsearch kibana at OSC Tokyo 2016 Spring
Takashi Yamamoto2.6K views
Meetup-js-062516 by Joe Devlin
Meetup-js-062516Meetup-js-062516
Meetup-js-062516
Joe Devlin474 views
IAA Life in Lockdown series: Securing Internet Routing by APNIC
IAA Life in Lockdown series: Securing Internet RoutingIAA Life in Lockdown series: Securing Internet Routing
IAA Life in Lockdown series: Securing Internet Routing
APNIC258 views
[1C2]webrtc 개발, 현재와 미래 by NAVER D2
[1C2]webrtc 개발, 현재와 미래[1C2]webrtc 개발, 현재와 미래
[1C2]webrtc 개발, 현재와 미래
NAVER D214.8K views

More from Sasha Goldshtein

Visual Studio 2015 and the Next .NET Framework by
Visual Studio 2015 and the Next .NET FrameworkVisual Studio 2015 and the Next .NET Framework
Visual Studio 2015 and the Next .NET FrameworkSasha Goldshtein
1.6K views11 slides
Swift: Apple's New Programming Language for iOS and OS X by
Swift: Apple's New Programming Language for iOS and OS XSwift: Apple's New Programming Language for iOS and OS X
Swift: Apple's New Programming Language for iOS and OS XSasha Goldshtein
3.2K views31 slides
C# Everywhere: Cross-Platform Mobile Apps with Xamarin by
C# Everywhere: Cross-Platform Mobile Apps with XamarinC# Everywhere: Cross-Platform Mobile Apps with Xamarin
C# Everywhere: Cross-Platform Mobile Apps with XamarinSasha Goldshtein
1.1K views16 slides
Modern Backends for Mobile Apps by
Modern Backends for Mobile AppsModern Backends for Mobile Apps
Modern Backends for Mobile AppsSasha Goldshtein
785 views15 slides
.NET Debugging Workshop by
.NET Debugging Workshop.NET Debugging Workshop
.NET Debugging WorkshopSasha Goldshtein
3.4K views53 slides
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013 by
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013Sasha Goldshtein
2.6K views15 slides

More from Sasha Goldshtein(20)

Visual Studio 2015 and the Next .NET Framework by Sasha Goldshtein
Visual Studio 2015 and the Next .NET FrameworkVisual Studio 2015 and the Next .NET Framework
Visual Studio 2015 and the Next .NET Framework
Sasha Goldshtein1.6K views
Swift: Apple's New Programming Language for iOS and OS X by Sasha Goldshtein
Swift: Apple's New Programming Language for iOS and OS XSwift: Apple's New Programming Language for iOS and OS X
Swift: Apple's New Programming Language for iOS and OS X
Sasha Goldshtein3.2K views
C# Everywhere: Cross-Platform Mobile Apps with Xamarin by Sasha Goldshtein
C# Everywhere: Cross-Platform Mobile Apps with XamarinC# Everywhere: Cross-Platform Mobile Apps with Xamarin
C# Everywhere: Cross-Platform Mobile Apps with Xamarin
Sasha Goldshtein1.1K views
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013 by Sasha Goldshtein
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Sasha Goldshtein2.6K views
Mastering IntelliTrace in Development and Production by Sasha Goldshtein
Mastering IntelliTrace in Development and ProductionMastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and Production
Sasha Goldshtein2K views
Delivering Millions of Push Notifications in Minutes by Sasha Goldshtein
Delivering Millions of Push Notifications in MinutesDelivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in Minutes
Sasha Goldshtein2.3K views
Building Mobile Apps with a Mobile Services .NET Backend by Sasha Goldshtein
Building Mobile Apps with a Mobile Services .NET BackendBuilding Mobile Apps with a Mobile Services .NET Backend
Building Mobile Apps with a Mobile Services .NET Backend
Sasha Goldshtein2.6K views
Building iOS and Android Apps with Mobile Services by Sasha Goldshtein
Building iOS and Android Apps with Mobile ServicesBuilding iOS and Android Apps with Mobile Services
Building iOS and Android Apps with Mobile Services
Sasha Goldshtein1.8K views
First Steps in Android Development by Sasha Goldshtein
First Steps in Android DevelopmentFirst Steps in Android Development
First Steps in Android Development
Sasha Goldshtein1.5K views
JavaScript, Meet Cloud: Node.js on Windows Azure by Sasha Goldshtein
JavaScript, Meet Cloud: Node.js on Windows AzureJavaScript, Meet Cloud: Node.js on Windows Azure
JavaScript, Meet Cloud: Node.js on Windows Azure
Sasha Goldshtein3.4K views
First Steps in Android Development with Eclipse and Xamarin by Sasha Goldshtein
First Steps in Android Development with Eclipse and XamarinFirst Steps in Android Development with Eclipse and Xamarin
First Steps in Android Development with Eclipse and Xamarin
Sasha Goldshtein3.3K views

Recently uploaded

Top-5-production-devconMunich-2023.pptx by
Top-5-production-devconMunich-2023.pptxTop-5-production-devconMunich-2023.pptx
Top-5-production-devconMunich-2023.pptxTier1 app
7 views40 slides
FIMA 2023 Neo4j & FS - Entity Resolution.pptx by
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptxNeo4j
8 views26 slides
JioEngage_Presentation.pptx by
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptxadmin125455
6 views4 slides
Bootstrapping vs Venture Capital.pptx by
Bootstrapping vs Venture Capital.pptxBootstrapping vs Venture Capital.pptx
Bootstrapping vs Venture Capital.pptxZeljko Svedic
12 views17 slides
Short_Story_PPT.pdf by
Short_Story_PPT.pdfShort_Story_PPT.pdf
Short_Story_PPT.pdfutkarshsatishkumarsh
5 views16 slides
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx by
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptxanimuscrm
15 views19 slides

Recently uploaded(20)

Top-5-production-devconMunich-2023.pptx by Tier1 app
Top-5-production-devconMunich-2023.pptxTop-5-production-devconMunich-2023.pptx
Top-5-production-devconMunich-2023.pptx
Tier1 app7 views
FIMA 2023 Neo4j & FS - Entity Resolution.pptx by Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j8 views
JioEngage_Presentation.pptx by admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254556 views
Bootstrapping vs Venture Capital.pptx by Zeljko Svedic
Bootstrapping vs Venture Capital.pptxBootstrapping vs Venture Capital.pptx
Bootstrapping vs Venture Capital.pptx
Zeljko Svedic12 views
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx by animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm15 views
Airline Booking Software by SharmiMehta
Airline Booking SoftwareAirline Booking Software
Airline Booking Software
SharmiMehta6 views
tecnologia18.docx by nosi6702
tecnologia18.docxtecnologia18.docx
tecnologia18.docx
nosi67025 views
predicting-m3-devopsconMunich-2023.pptx by Tier1 app
predicting-m3-devopsconMunich-2023.pptxpredicting-m3-devopsconMunich-2023.pptx
predicting-m3-devopsconMunich-2023.pptx
Tier1 app7 views
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... by Donato Onofri
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Donato Onofri860 views
Sprint 226 by ManageIQ
Sprint 226Sprint 226
Sprint 226
ManageIQ5 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action by Márton Kodok
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionGen Apps on Google Cloud PaLM2 and Codey APIs in Action
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action
Márton Kodok6 views
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... by Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller40 views
360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info33492122 views
Advanced API Mocking Techniques by Dimpy Adhikary
Advanced API Mocking TechniquesAdvanced API Mocking Techniques
Advanced API Mocking Techniques
Dimpy Adhikary19 views

Staring into the eBPF Abyss

  • 1. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Staring into the eBPF Abyss Sasha Goldshtein CTO, Sela Group @goldshtn
  • 2. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Agenda • Modern Linux tracing landscape • BPF • BCC – BPF Compiler Collection • Using BCC tools • Authoring BCC tools
  • 3. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Prerequisites • You should … • Have experience developing on or administering a Linux deployment • Be familiar with C/Python/Lua (a bonus) • To use your own machines for this workshop … • You will need Linux 4.6+ • Clone or install some open source tools (perf, bcc) • You can also use the instructor-provided VirtualBox appliance or Strigo workspace • Instructions and labs: https://github.com/goldshtn/linux-tracing-workshop
  • 4. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Tracing on the Performance Spectrum Invasiveness Envelope System completeness Metrics & simulations Development Testing Production Profilers jprof, valgrind Counters top, vmstat Debuggers Function tracers Event tracers Aggregators SystemTap, BPF Load tools ab Light-weight profilers perf
  • 5. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Tracing Objectives • Trace function execution, arguments, call graph • Print lightweight log messages (kernel/user) • Aggregate statistics (min/max/avg, histogram) • Low overhead • Continuous monitoring
  • 6. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Linux Tracing Tools, Today Ease of use BPF/BCC SysDig ktap SystemTap LTTng ftrace perf custom .ko new stable dead Level of detail, features
  • 7. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Berkeley Packet Filters (BPF) • Originally designed for, well, packet filtering: dst port 80 and len >= 100 • Custom instruction set, interpreted/JIT compiled 0: (bf) r6 = r1 1: (85) call 14 2: (67) r0 <<= 32 3: (77) r0 >>= 32 4: (15) if r0 == 0x49f goto pc+40
  • 8. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Extended BPF (3.18 and ongoing) • Attach BPF programs to kprobes/uprobes (4.1) and tracepoints (4.7) • Data structures: array, hash (expandable), stack map (4.6) • Output to trace buffer (4.3) and perf cyclic buffer (4.4) • Helper functions: get time, get current comm, get current CPU, etc.
  • 9. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 BCC: BPF Compiler Collection • Library and Python/Lua module for compiling, loading, and executing BPF programs • Compile BPF program from C source • Attach BPF program to kprobe/uprobe/tracepoint/USDT/socket • Poll data from BPF program using Python/Lua • Can do in-kernel aggregation and filtering • Growing collection of tracing, networking, and performance tools
  • 10. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07
  • 11. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 BCC • The BCC repository contains a variety of existing scripts and tools to get you started • The BPF module (Python/Lua) can be used to build new tools or one- off scripts $ ls *.py argdist.py bashreadline.py biolatency.py biosnoop.py biotop.py bitesize.py btrfsdist.py btrfsslower.py cachestat.py cpudist.py dcsnoop.py dcstat.py execsnoop.py ext4dist.py ext4slower.py filelife.py fileslower.py filetop.py funccount.py funclatency.py gethostlatency.py hardirqs.py killsnoop.py mdflush.py memleak.py offcputime.py offwaketime.py oomkill.py opensnoop.py pidpersec.py runqlat.py softirqs.py solisten.py stackcount.py stacksnoop.py statsnoop.py syncsnoop.py tcpaccept.py tcpconnect.py tcpconnlat.py tcpretrans.py tplist.py trace.py vfscount.py vfsstat.py wakeuptime.py xfsdist.py xfsslower.py zfsdist.py zfsslower.py
  • 12. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Specialized Tools # ./hardirqs.py Tracing hard irq event time... Hit Ctrl-C to end. ^C HARDIRQ TOTAL_usecs virtio0-input.0 959 ahci[0000:00:1f.2] 1290 # ./biolatency.py Tracing block device I/O... Hit Ctrl-C to end. ^C usecs : count distribution 64 -> 127 : 7 |********* | 128 -> 255 : 14 |****************** | 256 -> 511 : 5 |****** | 512 -> 1023 : 30 |****************************************| 1024 -> 2047 : 1 |* |
  • 13. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Specialized Tools # ./filetop.py 01:35:51 loadavg: 0.01 0.04 0.03 2/139 3611 PID COMM READS WRITES R_Kb W_Kb T FILE 2496 sshd 3 1 48 0 O ptmx 2939 screen 4 1 16 0 O ptmx 2496 sshd 1 3 16 0 S TCP 3611 clear 2 0 8 0 R screen 2939 screen 1 3 4 0 O 0 3589 filetop.py 2 0 2 0 R loadavg 3611 clear 1 0 0 0 R libtinfo.so.5.9 3611 clear 1 0 0 0 R libc-2.21.so 3611 filetop.py 3 0 0 0 R clear 3611 filetop.py 2 0 0 0 R ld-2.21.so 3611 clear 0 1 0 0 O 2 3589 filetop.py 0 3 0 0 O 2 # ./cachestat.py HITS MISSES DIRTIES READ_HIT% WRITE_HIT% BUFFERS_MB CACHED_MB 0 0 0 0.0% 0.0% 54 482 842 0 0 100.0% 0.0% 54 482 889 128 0 87.4% 12.6% 54 482
  • 14. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Specialized Tools # ./stackcount.py __kmalloc Tracing 1 functions for "__kmalloc"... Hit Ctrl-C to end. ^C __kmalloc alloc_fdtable dup_fd copy_process.part.31 _do_fork sys_clone do_syscall_64 return_from_SYSCALL_64 4 __kmalloc create_pipe_files __do_pipe_flags sys_pipe entry_SYSCALL_64_fastpath 6 __kmalloc htree_dirblock_to_tree ext4_htree_fill_tree ext4_readdir iterate_dir SyS_getdents entry_SYSCALL_64_fastpath 14
  • 15. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 BPF Tracing Targets (circa July 2016) Target Support Overhead kprobes Native Low uprobes Native Medium handler runs in KM Kernel tracepoints NativeNEW Low USDT tracepoints Temporary through uprobes Medium handler runs in KM
  • 16. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Multi-Tools: argdist # ./argdist.py -C 'p:c:write(int fd, const void *buf, size_t count):size_t:count:fd==1' [01:49:00] p:c:write(int fd, const void *buf, size_t count):size_t:count:fd==1 COUNT EVENT 1 count = 3134 1 count = 170 1 count = 181 2 count = 18 3 count = 30
  • 17. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Multi-Tools: argdist # ./argdist.py -i 5 -H 'r::__vfs_read(void *file, void *buf, size_t count):size_t:$entry(count):$latency > 1000000' [01:51:40] count : count distribution 0 -> 1 : 20 |****************************************| 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 0 | | 16 -> 31 : 0 | | 32 -> 63 : 0 | | 64 -> 127 : 0 | | 128 -> 255 : 6 |************ | 256 -> 511 : 0 | | 512 -> 1023 : 0 | | 1024 -> 2047 : 1 |** |
  • 18. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Multi-Tools: trace # ./trace.py 'r:/usr/bin/bash:readline "%s", retval' TIME PID COMM FUNC - 02:02:26 3711 bash readline ls –la 02:02:36 3711 bash readline wc -l src.c # ./tplist.py -v block:block_rq_complete block:block_rq_complete dev_t dev; sector_t sector; unsigned int nr_sector; int errors; char rwbs[8]; # ./trace.py 't:block:block_rq_complete "sectors=%d", tp.nr_sector' TIME PID COMM FUNC - 02:03:56 0 swapper/0 block_rq_complete sectors=16 02:03:56 0 swapper/0 block_rq_complete sectors=8 02:03:58 0 swapper/0 block_rq_complete sectors=24 02:04:00 0 swapper/0 block_rq_complete sectors=0
  • 19. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Multi-Tools: trace # ./tplist.py -l pthread -v libpthread:pthread_create /usr/lib64/libpthread-2.21.so libpthread:pthread_create location 0x7c63 8 unsigned bytes @ register %rax 8 unsigned bytes @ -192(%rbp) 8 unsigned bytes @ -168(%rbp) 8 unsigned bytes @ -176(%rbp) # ./trace.py 'u:pthread:pthread_create "%llx", arg3' TIME PID COMM FUNC - 02:07:29 4051 contentions pthread_create 400e00 02:07:29 4051 contentions pthread_create 400e00 02:07:29 4051 contentions pthread_create 400e00 02:07:29 4051 contentions pthread_create 400e00 ^C
  • 20. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Multi-Tools: trace # trace -p $(pidof node) 'u:node:http__server__request "%s %s (from %s:%d)" arg5, arg6, arg3, arg4' TIME PID COMM FUNC - 04:50:44 22185 node http__server__request GET /foofoo (from ::1:51056) 04:50:46 22185 node http__server__request GET / (from ::1:51056) ^C # ./trace.py 'u:/tmp/libjvm.so:thread__start "%s [%d]", arg1, arg4' 'u:/tmp/libjvm.so:thread__stop "%s [%d]", arg1, arg4' TIME PID COMM FUNC - 06:55:24 32157 java thread__start Reference Handler [32157] 06:55:24 32158 java thread__start Finalizer [32158] 06:55:24 32159 java thread__start Signal Dispatcher [32159] 06:55:24 32160 java thread__start C2 CompilerThread0 [32160] 06:55:24 32161 java thread__start C2 CompilerThread1 [32161] 06:55:24 32162 java thread__start C1 CompilerThread2 [32162] 06:55:24 32163 java thread__start Service Thread [32163] 06:55:28 32159 java thread__stop Signal Dispatcher [32159] ^C
  • 21. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Lab #3 – Chasing a C++ Memory Leak #4 – MySQL and Disk Stats and Stacks #5 – Node and JVM USDT Probes https://s.sashag.net/sreconlabs
  • 22. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Kernel Custom Tool Design BPF program Tracepoint kprobe Python/Lua driver App process uprobeUSDT Probe handler Probe handler Hash or histogram Cyclic buffer U K
  • 23. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 BPF Program: Counting Allocations #include <linux/ptrace.h> struct alloc_info_t { u64 count; u64 size; }; BPF_HASH(allocs, u32, struct alloc_info_t); int handler(struct pt_regs *ctx, size_t size) { u32 pid = bpf_get_current_pid_tgid(); struct alloc_info_t init = { 0 }, *info; info = allocs.lookup_or_init(&pid, &init); info->count += 1; info->size += size; return 0; }
  • 24. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 BPF Driver #!/usr/bin/env python from bcc import BPF from time import sleep program = BPF(src_file="allocs.c") program.attach_kprobe(event="__kmalloc", fn_name="handler") allocs = program["allocs"] while True: sleep(5) print("n%-8s %-8s %-10s" % ("PID", "COUNT", "SIZE")) for key, value in sorted(allocs.items(), key=lambda (k, v): k.value): print("%-8d %-8d %-8d" % (key.value, value.count, value.size))
  • 25. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 BPF Execution # ./allocs.py PID COUNT SIZE 28064 3 456 28157 10 76 28158 5 1116 PID COUNT SIZE 28001 113 1828 28064 8 1216 28110 38 683 28157 46 328 28158 5 1116 28159 41 12894 ^C
  • 26. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Inline BPF Program #!/usr/bin/env python from bcc import BPF from time import sleep program = BPF(text="""BPF_HASH(counts, u32, u32); TRACEPOINT_PROBE(irq, irq_handler_entry) { u32 zero = 0, *existing, irq = args->irq; existing = counts.lookup_or_init(&irq, &zero); ++(*existing); return 0; }""") counts = program["counts"] sleep(9999999) print("n%-8s %-8s" % ("IRQ", "COUNT")) for key, value in counts.items(): print("%-8d %-8d" % (key.value, value.value))
  • 27. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Data Types • Array • Hash • Histogram • Perf buffer (4.4+)NEW • Stack map (4.6+)NEW
  • 28. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Example: Histogram struct dist_key_t { char op[OP_NAME_LEN]; u64 slot; }; BPF_HISTOGRAM(dist, struct dist_key_t); ... struct dist_key_t key = { .slot=bpf_log2l(elapsed_time) }; __builtin_memcpy(&key.op, op, sizeof(key.op)); dist.increment(key); ... bpf.get_table("dist").print_log2_hist("operation")
  • 29. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Example: Perf Buffer bpf = BPF(text="""#include <linux/ptrace.h> struct data_t { u64 pid; char str[80]; }; BPF_PERF_OUTPUT(events); int print(struct pt_regs *ctx) { struct data_t data = {0}; ... events.perf_submit(ctx, &data, sizeof(data)); return 0; }""") class Data(ct.Structure): _fields_ = [ ("pid", ct.c_ulonglong), ("str", ct.c_char*80) ] bpf.attach_uretprobe(name="/bin/bash", sym="readline", fn_name="print") b["events"].open_perf_buffer(lambda cpu, data, size: event = ct.cast(data, ct.POINTER(Data)).contents print(event) ) while True: bpf.kprobe_poll()
  • 30. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Example: Stack Map BPF_HASH(counts, int); BPF_STACK_TRACE(stacks, 1024); ... int key = stacks.get_stackid(ctx, BPF_F_REUSE_STACKID); u64 zero = 0; u64 *val = counts.lookup_or_init(&key, &zero); ++(*val); ... counts, stacks = bpf["counts"], bpf["stacks"] for k, v in counts: for addr in stacks.walk(k.value): print(BPF.ksym(addr)) print(v.value)
  • 31. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Custom Tool Design Tips • Try to perform all aggregations in the BPF program and keep UM copying to a minimum • Limit hash/histogram/stackmap sizes, prune, keep only top entries • Clear cyclic buffer often and quickly
  • 32. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Deployment • For Python tools, deploy Python + libbcc.so • For Lua tools, deploy only bcc-lua • Statically links libbcc.a but allows plugging libbcc.so • Kernel build flags: • CONFIG_BPF=y • CONFIG_BPF_SYSCALL=y • CONFIG_BPF_JIT=y • CONFIG_HAVE_BPF_JIT=y • CONFIG_BPF_EVENTS=y
  • 33. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Lab #6 – Contention Stats and Stacks #7 – From BCC GitHub Issues https://s.sashag.net/sreconlabs
  • 34. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Summary • Tracing can identify bugs and performance issues that no debugger or profiler can catch • Tools make low-overhead, dynamic, production tracing possible • BPF is the next-generation backend for Linux tracing tools
  • 35. SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 SRECon Europe 2016 @goldshtn https://s.sashag.net/bpfws07 Thank You! Sasha Goldshtein @goldshtn

Editor's Notes

  1. Missing: cpudist
  2. This example requires kernel 4.7 for BPF_PROG_TYPE_TRACEPOINT and a version of bcc with PR #602 merged, which introduces TRACEPOINT_PROBE.
  3. Adapted from ext4dist.py
  4. Adapted from tools/bashreadline.py
  5. Adapted from stackcount.py