Section 2.2

Classic BPF (cBPF)

Full name: classic Berkeley Packet Filter.

Definition

Classic BPF (cBPF) is the original Berkeley Packet Filter: a small virtual machine §2.3, meaning a computer defined in software, that runs inside the kernel, the privileged core of an operating system. An application gives the kernel a short program called a filter. The kernel runs the filter on every received network packet, and the filter’s result says whether the application gets a copy of the packet and how many bytes of it. Steven McCanne and Van Jacobson published the design in 1993. Its successor is eBPF §2.1 (extended Berkeley Packet Filter).

The idea

A network monitoring tool such as tcpdump shows the packets that pass a network interface. A packet is one message as it travels on the network. The tool is an ordinary application, and it usually wants a small part of the traffic. The packets, however, arrive in the kernel, and handing a packet to an application means copying its bytes across the boundary between kernel and application. If the kernel copies every packet and the application discards most of them, most of that copying is wasted.

Classic BPF moves the decision to the place where the packet arrives. The application describes the packets it wants as a small program. The kernel runs that program on each packet where the packet already lies, before any copy is made, and copies only what the program accepts. The 1993 paper states the result in one sentence: “filtering packets early and in place pays off.”

The filter is a program, not a fixed menu of options, so that the kernel needs no knowledge of protocols. One of the paper’s design rules reads: “The kernel should not have to be modified to add new protocol support.” The filter is also a program for a tiny invented machine and not native machine code, which consists of instructions of the real processor and could do everything the kernel can do. The instructions of the invented machine can only read the packet, compute, jump forward and return a number, so the kernel can run a filter from an application that it does not trust.

The paper calls the design the BSD Packet Filter, after BSD (Berkeley Software Distribution), a family of Unix operating systems on which it ran. Later documents expand BPF as Berkeley Packet Filter.

How it works

The paper defines the machine as four parts.

Every instruction is 64 bits long: a 16-bit operation code §2.8, two 8-bit jump distances called jt (jump if true) and jf (jump if false), and a 32-bit value called k whose use depends on the operation. The instructions load a value into A or X, store A or X in the scratch memory store, compute with A, compare A with a value and jump, copy between A and X, and return.

A filter is used in four steps.

  1. The application produces the filter. tcpdump, for example, compiles an expression typed by its user into classic BPF instructions.

  2. The application hands the filter to the kernel. Linux refuses the filter unless it has between 1 and 4096 instructions, uses only known operation codes, keeps every jump inside the program and ends with a return instruction.

  3. The kernel runs the filter on each arriving packet.

  4. The filter returns a number. Zero means that the application does not want the packet. Any other number is a count of bytes: the kernel copies that many bytes into a buffer for the application, or the whole packet if the packet is shorter.

Two rules make a filter safe to run for any application. A load that reaches beyond the end of the packet reads no other memory: it ends the filter with the result zero. And the jump distances are unsigned numbers counted forward from the next instruction, so no jump goes backwards and no instruction runs twice.

Worked example

The Linux kernel documentation gives the following filter. It accepts ARP (Address Resolution Protocol) packets on an Ethernet network and rejects all others. In an Ethernet packet, bytes 12 and 13, counted from 0, hold a number that names the protocol carried inside: 0x0806 means ARP and 0x0800 means IPv4 (Internet Protocol version 4).

no. meaning code jt jf k
0 load the 2 bytes at offset 12 into A 0x28 0 0 0x0000000c
1 if A equals k skip jt, else skip jf 0x15 0 1 0x00000806
2 return k 0x06 0 0 0xffffffff
3 return k 0x06 0 0 0x00000000

An ARP packet of 60 bytes: instruction 0 puts 0x0806 in A. Instruction 1 finds A equal to k and skips jt = 0 instructions, so instruction 2 returns 0xffffffff, which is 4,294,967,295. The kernel copies the smaller of that number and the packet length: all 60 bytes. An IPv4 packet: instruction 0 puts 0x0800 in A. Instruction 1 finds A different from k and skips jf = 1 instruction, so instruction 3 returns 0, and nothing is copied. A damaged packet of 10 bytes: instruction 0 asks for bytes 12 and 13, which do not exist, and the filter ends with the result 0.

The paper’s measurements show why filtering early pays. On the authors’ workstation, rejecting a packet in place cost about 5 microseconds whatever its size, and accepting a packet cost about 6 microseconds plus 148 nanoseconds for each byte copied. Take an invented traffic mix: each second 1,000 packets of 1,000 bytes arrive, and the application wants 10 of them. One accepted packet costs 6+1000×0.148=1546 + 1000 \times 0.148 = 154 microseconds. Accepting all 1,000 packets costs 1000×154=154,0001000 \times 154 = 154{,}000 microseconds, which is 15.4% of every second. Filtering first costs 990×5+10×154=4950+1540=6490990 \times 5 + 10 \times 154 = 4950 + 1540 = 6490 microseconds, which is 0.65% of every second and about 24 times less. The measured costs are for filters that accept or reject every packet. A longer filter adds its own running time to each packet.

Stated precisely

A classic BPF filter computes one unsigned 32-bit number from one packet. Let nn be the length of the packet in bytes and rr the number the filter returns. The kernel copies min(r,n)\min(r, n) bytes to the application, where min\min means the smaller of the two. If r=0r = 0, it copies nothing.

Let pp be the position of a conditional jump, counting instructions from 0, and let jt\mathrm{jt} and jf\mathrm{jf} be its two jump distances. The next instruction is at position p+1+jtp + 1 + \mathrm{jt} if the comparison is true and at position p+1+jfp + 1 + \mathrm{jf} if it is false. Both distances lie between 0 and 255. The unconditional jump takes its distance from k, which Linux also reads as an unsigned number. No jump distance can be negative, so the position only increases, and a filter of mm instructions executes at most mm instructions for any packet. Linux accepts m≤4096m \le 4096.

Two rules for the running filter complete the contract. A load beyond the end of the packet ends the filter with r=0r = 0. A division by zero ends the filter. The paper does not state the result in that case. Linux refuses a filter that divides by the constant zero when the filter is loaded, and ends a running filter with r=0r = 0 when the divisor in X is zero.

Filtering first is not always the cheaper design. Let ff be the cost of running the filter on one packet, cc the cost of copying one packet to the application, and aa the fraction of packets that the filter accepts. Filtering first costs f+acf + a c per packet on average, copying every packet costs cc, and filtering first is cheaper exactly when f<(1−a)cf < (1 - a)\,c.

Limits and common mistakes

Classic BPF can only select. It has no instruction that writes to the packet and none that calls a function. Linux also refuses a filter that reads a scratch word before writing it, so no value carries over from one packet to the next.

The filter is not free. It runs for every packet, including the rejected ones, so a slow filter can cost more than the copies it avoids.

The argument “no backward jumps, so every filter ends” belongs to classic BPF only. eBPF allows backward jumps, memory access at addresses that the program computes, and calls into the host, so eBPF needs a verifier, a checker that examines a program before it runs, or checks at run time.

The two formats are not interchangeable. Both use 8-byte instructions, but classic BPF has a 16-bit operation code and two jump distances, and eBPF has an 8-bit operation code, two register numbers and a 16-bit offset.

Classic BPF is still in use. Linux accepts classic filters from applications, for packet capture and for seccomp (secure computing), its mechanism for restricting the system calls, meaning the requests to the kernel, that a process may make. The Linux documentation states that the kernel converts such filters to eBPF before running them.

Questions answered

Why did networking systems want executable filters near packet reception?

They wanted to stop copying packets that no application wants, and to do so without teaching the kernel every protocol. Packets arrive in the kernel, the privileged core of the operating system. A monitoring application usually wants a small fraction of them, and passing a packet to an application means copying it across the kernel boundary. A filter that runs in the kernel at the point of reception examines each packet where it already lies. A rejected packet then costs only the few instructions of the filter and is never copied.

The filter is executable, that is, a small program and not a fixed list of options, so that an application can describe any selection, including protocols the kernel has never heard of, without a change to the kernel. Because it is a program for a restricted virtual machine, a computer defined in software, and not machine code of the real processor, the kernel can run it for any application: the machine cannot read outside the packet and cannot loop.

The gain is conditional. The filter runs on every packet, so filtering first is cheaper only when the filter costs less than the copies it avoids. In the measurements of the 1993 paper that introduced classic BPF, rejecting a packet cost about 5 microseconds, and accepting a packet cost about 6 microseconds plus 148 nanoseconds for each byte copied, which comes to about 154 microseconds for a packet of 1,000 bytes.

Related entries: eBPF (§2.1), Register machine (§2.5), Virtual machine (VM) (§2.3), Scratch memory (§2.24). Further reading: [2, 73].