Questions tagged [bpf]

The Berkeley Packet Filter (BPF, or cBPF) was initially introduced to provide a raw interface to data link layers in a protocol independent fashion, on BSD systems and then on Linux. More recently, it has been reworked on Linux to give birth to the extended BPF, or eBPF. The latter can be used for network processing at several levels, as well as for security applications, or even tracing and monitoring use cases. This tag is for all cBPF/eBPF questions.

The Berkeley Packet Filter was initially introduced to provide a raw interface to data link layers in a protocol independent fashion, first on BSD systems in the early 90s, then on Linux a few years later. All packets on the network, even those destined for other hosts, would be accessible through this mechanism.

Since 2013, the older BPF subsystem (or cBPF, for classic BPF) has led to the creation to an extended BPF version, or eBPF, on Linux. eBPF has a different architecture. It is more efficient, more flexible, introduces new features (maps, tail calls, helper functions from kernel, etc.). And programs can be attached to a variety of hooks in the kernel, for networking (sockets, as before, but also TC (traffic control) interface, XDP…), for security (cgroups) or for tracing and monitoring the kernel (kprobes, tracepoints, …).

449 questions
4
votes
1 answer

classic BPF on Linux: filter does not work

I'm trying to test classic BPF for packet filtering by attaching it to raw socket. I want to catch TCP packets with first byte of source port == 8 (tcpdump 'tcp[1:1] = 0x50'), but I see no incoming packets on the socket. Without filter my code works…
Grayscale
  • 73
  • 1
  • 6
4
votes
1 answer

Packet filtering with Netfilter's NFQUEUE vs. Berkeley Packet Filter (BPF)

I've just read in these answers about two options for developing packet filters in linux. The first is using iptables and netfilter, probably with NFQUEUE and libnetfilter_queue library. The second is by using BPF (Berkeley Packet Filter), that…
Reflection
  • 1,936
  • 3
  • 21
  • 39
4
votes
1 answer

Using BPF with SOCK_DGRAM on Linux machine

Is it possible to filter packets using BPF on datagram socket? No error occures when I try to attach a filter, but I don't receive any packet. I compiled a filter using libpcap, and the filter works with tcpdump. Here is shortened version of my…
nans
  • 101
  • 4
3
votes
1 answer

eBPF: BPF stack limit exceeded when storing stack variable in map

I have the following eBPF program: #include #include #include #include #include char LICENSE[] SEC("license") = "GPL"; // msg_data_map carries a key-value pair of (msg_id,…
diviquery
  • 569
  • 5
  • 19
3
votes
1 answer

How does __sync_fetch_and_add() work in bpf programs

I'm writing several bpf programs that will update the same counter which is implemented by a BPF_MAP_TYPE_ARRAY bpf map. To avoid data races, I referred the bpf kernel doc and used the __sync_fetch_and_add() intrinsic. However, I'm confused about…
sk_buff
  • 81
  • 7
3
votes
1 answer

How to make eBPF program sleepable

I've been reading about sleepable eBPF programs, specifically this article provides a nice introduction. However I am struggling to find any documentation or examples on how to actually achieve this in code. Any tips or links to documentation are…
nela
  • 429
  • 5
  • 13
3
votes
1 answer

Thread safe operations on XDP

I was able to confirm from the documentation that bpf_map_update_elem is an atomic operation if done on HASH_MAPs. Source (https://man7.org/linux/man-pages/man2/bpf.2.html). [Cite: map_update_elem() replaces existing elements atomically] My question…
Rishab
  • 73
  • 4
3
votes
2 answers

Why is "anchor build" and "Cargo build-bpf" showing wrong rustc version?

I'm trying to build the (https://github.com/betterclever/solend-anchor) package using anchor build, however I get the following error error: package `uint v0.9.3` cannot be built because it requires rustc 1.56.1 or newer, while the currently active…
Highness
  • 333
  • 4
  • 11
3
votes
1 answer

eBPF: 'bpf_map_update()' returns the 'invalid indirect read from stack' error

I have an eBPF program with the following map definitions: struct bpf_map_def SEC("maps") servers = { .type = BPF_MAP_TYPE_HASH, .key_size = sizeof(struct ip_key), .value_size = sizeof(struct dest_info), .max_entries =…
Ferrar
  • 65
  • 7
3
votes
1 answer

No direct packet access in BPF program with just CAP_BPF?

Up until Linux 5.8 CAP_SYSADMIN was required to load any but the most basic BPF program. The recently introduced CAP_BPF is a welcome addition as it allows to run software leveraging BPF with less privileges. Certain types of BPF programs can access…
Nick Zavaritsky
  • 1,429
  • 8
  • 19
3
votes
1 answer

eBPF: raw_tracepoint arguments

I am getting into eBPF programming and want to use raw tracepoints, but I do not really understand, how to use them and how to access the arguments correctly. I would appreciate any help and hints to documantation. My questions: How do I get the…
Dennis
  • 150
  • 1
  • 11
3
votes
2 answers

Linux BTF: bpftool: Failed to get EHDR from /sys/kernel/btf/vmlinux

I am trying to start with BPF CO:RE Development. Using Ubuntu 20.04 LTS in a VM, I needed to recompile the kernel and install pahole (from apt install dwarves) so that BTF is enabled (I set CONFIG_DEBUG_FS=y and CONFIG_DEBUG_INFO_BTF=y). So my setup…
Dennis
  • 150
  • 1
  • 11
3
votes
2 answers

Can't compile sample bpf program, bpf/bpf.h is missing

I'm trying to compile the sample bpf program in Linux source code. So I downloaded the current kernel source code and entered samples/bpf folder apt source linux cd linux-*/samples/bpf Then I tried to compile a sample program with gcc: # gcc…
daisy
  • 22,498
  • 29
  • 129
  • 265
3
votes
1 answer

bpf_prog_test_run() causes unexpected packet data

I try to perform a test run for an XDP BPF program. The BPF program uses the bpf_xdp_adjust_meta() helper, to adjust the meta data. I tried: to run bpf_prog_test_run() to run bpf_prog_test_run_xattr() 1. bpf_prog_test_run() (The first time I…
n1kb3rt
  • 111
  • 1
  • 8
3
votes
2 answers

eBPF - Cannot read argv and envp from tracepoint sys_enter_execve

I am learning BPF for my own fun, and I am having a hard time figuring out how to read argv and envp from the context passed to my eBPF program for sys_enter_execve I will show my BPF program here and then explain in more details later what I am…
ocampeau
  • 157
  • 1
  • 8