blob: ae62feb53413e0d024e4e8fd8508b3cee8cfe54d [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001// Copyright 2016 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5/*
6
7Package bpf implements marshaling and unmarshaling of programs for the
8Berkeley Packet Filter virtual machine, and provides a Go implementation
9of the virtual machine.
10
11BPF's main use is to specify a packet filter for network taps, so that
12the kernel doesn't have to expensively copy every packet it sees to
13userspace. However, it's been repurposed to other areas where running
14user code in-kernel is needed. For example, Linux's seccomp uses BPF
15to apply security policies to system calls. For simplicity, this
16documentation refers only to packets, but other uses of BPF have their
17own data payloads.
18
19BPF programs run in a restricted virtual machine. It has almost no
20access to kernel functions, and while conditional branches are
21allowed, they can only jump forwards, to guarantee that there are no
22infinite loops.
23
24The virtual machine
25
26The BPF VM is an accumulator machine. Its main register, called
27register A, is an implicit source and destination in all arithmetic
28and logic operations. The machine also has 16 scratch registers for
29temporary storage, and an indirection register (register X) for
30indirect memory access. All registers are 32 bits wide.
31
32Each run of a BPF program is given one packet, which is placed in the
33VM's read-only "main memory". LoadAbsolute and LoadIndirect
34instructions can fetch up to 32 bits at a time into register A for
35examination.
36
37The goal of a BPF program is to produce and return a verdict (uint32),
38which tells the kernel what to do with the packet. In the context of
39packet filtering, the returned value is the number of bytes of the
40packet to forward to userspace, or 0 to ignore the packet. Other
41contexts like seccomp define their own return values.
42
43In order to simplify programs, attempts to read past the end of the
44packet terminate the program execution with a verdict of 0 (ignore
45packet). This means that the vast majority of BPF programs don't need
46to do any explicit bounds checking.
47
48In addition to the bytes of the packet, some BPF programs have access
49to extensions, which are essentially calls to kernel utility
50functions. Currently, the only extensions supported by this package
51are the Linux packet filter extensions.
52
53Examples
54
55This packet filter selects all ARP packets.
56
57 bpf.Assemble([]bpf.Instruction{
58 // Load "EtherType" field from the ethernet header.
59 bpf.LoadAbsolute{Off: 12, Size: 2},
60 // Skip over the next instruction if EtherType is not ARP.
61 bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x0806, SkipTrue: 1},
62 // Verdict is "send up to 4k of the packet to userspace."
63 bpf.RetConstant{Val: 4096},
64 // Verdict is "ignore packet."
65 bpf.RetConstant{Val: 0},
66 })
67
68This packet filter captures a random 1% sample of traffic.
69
70 bpf.Assemble([]bpf.Instruction{
71 // Get a 32-bit random number from the Linux kernel.
72 bpf.LoadExtension{Num: bpf.ExtRand},
73 // 1% dice roll?
74 bpf.JumpIf{Cond: bpf.JumpLessThan, Val: 2^32/100, SkipFalse: 1},
75 // Capture.
76 bpf.RetConstant{Val: 4096},
77 // Ignore.
78 bpf.RetConstant{Val: 0},
79 })
80
81*/
82package bpf // import "golang.org/x/net/bpf"