Source File
doc.go
Belonging Package
golang.org/x/net/bpf
// Copyright 2016 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file./*Package bpf implements marshaling and unmarshaling of programs for theBerkeley Packet Filter virtual machine, and provides a Go implementationof the virtual machine.BPF's main use is to specify a packet filter for network taps, so thatthe kernel doesn't have to expensively copy every packet it sees touserspace. However, it's been repurposed to other areas where runninguser code in-kernel is needed. For example, Linux's seccomp uses BPFto apply security policies to system calls. For simplicity, thisdocumentation refers only to packets, but other uses of BPF have theirown data payloads.BPF programs run in a restricted virtual machine. It has almost noaccess to kernel functions, and while conditional branches areallowed, they can only jump forwards, to guarantee that there are noinfinite loops.# The virtual machineThe BPF VM is an accumulator machine. Its main register, calledregister A, is an implicit source and destination in all arithmeticand logic operations. The machine also has 16 scratch registers fortemporary storage, and an indirection register (register X) forindirect memory access. All registers are 32 bits wide.Each run of a BPF program is given one packet, which is placed in theVM's read-only "main memory". LoadAbsolute and LoadIndirectinstructions can fetch up to 32 bits at a time into register A forexamination.The goal of a BPF program is to produce and return a verdict (uint32),which tells the kernel what to do with the packet. In the context ofpacket filtering, the returned value is the number of bytes of thepacket to forward to userspace, or 0 to ignore the packet. Othercontexts like seccomp define their own return values.In order to simplify programs, attempts to read past the end of thepacket terminate the program execution with a verdict of 0 (ignorepacket). This means that the vast majority of BPF programs don't needto do any explicit bounds checking.In addition to the bytes of the packet, some BPF programs have accessto extensions, which are essentially calls to kernel utilityfunctions. Currently, the only extensions supported by this packageare the Linux packet filter extensions.# ExamplesThis packet filter selects all ARP packets.bpf.Assemble([]bpf.Instruction{// Load "EtherType" field from the ethernet header.bpf.LoadAbsolute{Off: 12, Size: 2},// Skip over the next instruction if EtherType is not ARP.bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x0806, SkipTrue: 1},// Verdict is "send up to 4k of the packet to userspace."bpf.RetConstant{Val: 4096},// Verdict is "ignore packet."bpf.RetConstant{Val: 0},})This packet filter captures a random 1% sample of traffic.bpf.Assemble([]bpf.Instruction{// Get a 32-bit random number from the Linux kernel.bpf.LoadExtension{Num: bpf.ExtRand},// 1% dice roll?bpf.JumpIf{Cond: bpf.JumpLessThan, Val: 2^32/100, SkipFalse: 1},// Capture.bpf.RetConstant{Val: 4096},// Ignore.bpf.RetConstant{Val: 0},})*/package bpf // import "golang.org/x/net/bpf"
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |