Documentation ¶
Index ¶
- Constants
- func AttachTCIngress(progFd int, linkName string, ingress bool) error
- func CheckOrMountCgroup2() error
- func CheckOrMountDebugFS() error
- func CheckOrMountFS(bpfRoot string)
- func ConfigureResourceLimits() error
- func ConvertKeyValue(bKey []byte, bValue []byte, key MapKey, value MapValue) (MapKey, MapValue, error)
- func Environment() []string
- func GetFirstKey(fd int, nextKey unsafe.Pointer) error
- func GetMapRoot() string
- func GetNextKey(fd int, key, nextKey unsafe.Pointer) error
- func GetNextKeyFromPointers(fd int, structPtr, sizeOfStruct uintptr) error
- func GetNumPossibleCPUs() int
- func HasBuildId() bool
- func HasKprobeMulti() bool
- func HasOverrideHelper() bool
- func LocalMapName(name string, id uint16) string
- func LocalMapPath(name string, id uint16) string
- func LookupElement(fd int, key, value unsafe.Pointer) error
- func LookupElementFromPointers(fd int, structPtr, sizeOfStruct uintptr) error
- func MapPath(name string) string
- func MapPrefixPath() string
- func ObjGet(pathname string) (int, error)
- func QdiscTCInsert(linkName string, ingress bool) error
- func SetMapPrefix(path string)
- func SetMapRoot(path string)
- func UpdateElement(fd int, key, value unsafe.Pointer, flags uint64) error
- func UpdateElementFromPointers(fd int, structPtr, sizeOfStruct uintptr) error
- type DesiredAction
- type DumpCallback
- type EPoll
- type ErrorFunc
- type EventMap
- type Feature
- type LostFunc
- type Map
- func (m *Map) Close() error
- func (m *Map) Count() (int, error)
- func (m *Map) DeleteKey(key MapKey) error
- func (m *Map) Dump(hash map[string][]string) error
- func (m *Map) DumpIfExists(hash map[string][]string) error
- func (m *Map) DumpWithCallback(cb DumpCallback) error
- func (m *Map) DumpWithCallbackIfExists(cb DumpCallback) error
- func (m *Map) GetFd() int
- func (m *Map) Lookup(key MapKey) (MapValue, error)
- func (m *Map) Name() string
- func (m *Map) Open() error
- func (m *Map) Path() string
- func (m *Map) Reopen() error
- func (m *Map) Update(key MapKey, value MapValue) error
- type MapInfo
- type MapKey
- type MapType
- type MapValidator
- type MapValue
- type PerCpuEvents
- type PerfEvent
- func (e *PerfEvent) Close()
- func (e *PerfEvent) Debug() string
- func (e *PerfEvent) DebugDump() string
- func (e *PerfEvent) Disable() error
- func (e *PerfEvent) Enable() error
- func (e *PerfEvent) Mmap(pagesize int, npages int) error
- func (e *PerfEvent) Munmap() error
- func (e *PerfEvent) Read(maxEvents int, receive ReceiveFunc, lostFn LostFunc, err ErrorFunc) (nread int)
- type PerfEventConfig
- type PerfEventHeader
- type PerfEventLost
- type PerfEventMmapPage
- type PerfEventSample
- type ReadState
- type ReceiveFunc
Constants ¶
const ( PERF_TYPE_SOFTWARE = 1 PERF_SAMPLE_RAW = 1 << 10 PERF_COUNT_SW_BPF_OUTPUT = 10 // BPF map type constants. Must match enum bpf_map_type from linux/bpf.h BPF_MAP_TYPE_UNSPEC = 0 BPF_MAP_TYPE_HASH = 1 BPF_MAP_TYPE_ARRAY = 2 BPF_MAP_TYPE_PROG_ARRAY = 3 BPF_MAP_TYPE_PERF_EVENT_ARRAY = 4 BPF_MAP_TYPE_PERCPU_HASH = 5 BPF_MAP_TYPE_PERCPU_ARRAY = 6 BPF_MAP_TYPE_STACK_TRACE = 7 BPF_MAP_TYPE_CGROUP_ARRAY = 8 BPF_MAP_TYPE_LRU_HASH = 9 BPF_MAP_TYPE_LRU_PERCPU_HASH = 10 BPF_MAP_TYPE_LPM_TRIE = 11 BPF_MAP_TYPE_ARRAY_OF_MAPS = 12 BPF_MAP_TYPE_HASH_OF_MAPS = 13 BPF_MAP_TYPE_DEVMAP = 14 BPF_MAP_TYPE_SOCKMAP = 15 BPF_MAP_TYPE_CPUMAP = 16 BPF_MAP_TYPE_XSKMAP = 17 BPF_MAP_TYPE_SOCKHASH = 18 BPF_MAP_TYPE_CGROUP_STORAGE = 19 BPF_MAP_TYPE_REUSEPORT_SOCKARRAY = 20 // BPF syscall command constants. Must match enum bpf_cmd from linux/bpf.h BPF_MAP_CREATE = 0 BPF_MAP_LOOKUP_ELEM = 1 BPF_MAP_UPDATE_ELEM = 2 BPF_MAP_DELETE_ELEM = 3 BPF_MAP_GET_NEXT_KEY = 4 BPF_PROG_LOAD = 5 BPF_OBJ_PIN = 6 BPF_OBJ_GET = 7 BPF_PROG_ATTACH = 8 BPF_PROG_DETACH = 9 BPF_PROG_TEST_RUN = 10 BPF_PROG_GET_NEXT_ID = 11 BPF_MAP_GET_NEXT_ID = 12 BPF_PROG_GET_FD_BY_ID = 13 BPF_MAP_GET_FD_BY_ID = 14 BPF_OBJ_GET_INFO_BY_FD = 15 BPF_PROG_QUERY = 16 BPF_RAW_TRACEPOINT_OPEN = 17 BPF_BTF_LOAD = 18 BPF_BTF_GET_FD_BY_ID = 19 BPF_TASK_FD_QUERY = 20 // BPF syscall attach types BPF_CGROUP_INET_INGRESS = 0 BPF_CGROUP_INET_EGRESS = 1 BPF_CGROUP_INET_SOCK_CREATE = 2 BPF_CGROUP_SOCK_OPS = 3 BPF_SK_SKB_STREAM_PARSER = 4 BPF_SK_SKB_STREAM_VERDICT = 5 BPF_CGROUP_DEVICE = 6 BPF_SK_MSG_VERDICT = 7 BPF_CGROUP_INET4_BIND = 8 BPF_CGROUP_INET6_BIND = 9 BPF_CGROUP_INET4_CONNECT = 10 BPF_CGROUP_INET6_CONNECT = 11 BPF_CGROUP_INET4_POST_BIND = 12 BPF_CGROUP_INET6_POST_BIND = 13 BPF_CGROUP_UDP4_SENDMSG = 14 BPF_CGROUP_UDP6_SENDMSG = 15 BPF_LIRC_MODE2 = 16 BPF_FLOW_DISSECTOR = 17 BPF_CGROUP_SYSCTL = 18 BPF_CGROUP_UDP4_RECVMSG = 19 BPF_CGROUP_UDP6_RECVMSG = 20 // Flags for BPF_MAP_UPDATE_ELEM. Must match values from linux/bpf.h BPF_ANY = 0 BPF_NOEXIST = 1 BPF_EXIST = 2 // Flags for BPF_MAP_CREATE. Must match values from linux/bpf.h BPF_F_NO_PREALLOC = 1 << 0 BPF_F_NO_COMMON_LRU = 1 << 1 BPF_F_NUMA_NODE = 1 << 2 // Flags for BPF_PROG_QUERY BPF_F_QUERY_EFFECTVE = 1 << 0 // Flags for accessing BPF object BPF_F_RDONLY = 1 << 3 BPF_F_WRONLY = 1 << 4 // Flag for stack_map, store build_id+offset instead of pointer BPF_F_STACK_BUILD_ID = 1 << 5 // Build ID flags bit for perf_event_open PerfBitBuildId = unix.CBitFieldMaskBit34 )
const ( MAX_POLL_EVENTS = 32 PossibleCPUSysfsPath = "/sys/devices/system/cpu/possible" )
Variables ¶
This section is empty.
Functions ¶
func CheckOrMountCgroup2 ¶
func CheckOrMountCgroup2() error
func CheckOrMountDebugFS ¶
func CheckOrMountDebugFS() error
func CheckOrMountFS ¶
func CheckOrMountFS(bpfRoot string)
CheckOrMountFS checks or mounts the BPF filesystem and then opens/creates/deletes all maps which have previously been scheduled to be opened/created/deleted.
func ConfigureResourceLimits ¶
func ConfigureResourceLimits() error
func ConvertKeyValue ¶
func ConvertKeyValue(bKey []byte, bValue []byte, key MapKey, value MapValue) (MapKey, MapValue, error)
ConvertKeyValue converts key and value from bytes to given Golang struct pointers.
func Environment ¶
func Environment() []string
Environment returns a list of environment variables which are needed to make BPF programs and tc aware of the actual BPFFS mount path.
func GetFirstKey ¶
GetFirstKey fetches the first key in the map.
func GetMapRoot ¶
func GetMapRoot() string
func GetNextKey ¶
GetNextKey stores, in nextKey, the next key after the key of the map in fd. Deprecated, use GetNextKeyFromPointers
func GetNextKeyFromPointers ¶
GetNextKeyFromPointers stores, in nextKey, the next key after the key of the map in fd.
func GetNumPossibleCPUs ¶
func GetNumPossibleCPUs() int
GetNumPossibleCPUs returns a total number of possible CPUS, i.e. CPUs that have been allocated resources and can be brought online if they are present. The number is retrieved by parsing /sys/device/system/cpu/possible.
See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/cpumask.h?h=v4.19#n50 for more details.
func HasBuildId ¶
func HasBuildId() bool
func HasKprobeMulti ¶
func HasKprobeMulti() bool
func HasOverrideHelper ¶
func HasOverrideHelper() bool
func LocalMapName ¶
LocalMapName returns the name for a BPF map that is local to the specified ID.
func LocalMapPath ¶
LocalMapPath returns the path for a BPF map that is local to the specified ID.
func LookupElement ¶
LookupElement looks up for the map value stored in fd with the given key. The value is stored in the value unsafe.Pointer. Deprecated, use LookupElementFromPointers
func LookupElementFromPointers ¶
LookupElement looks up for the map value stored in fd with the given key. The value is stored in the value unsafe.Pointer.
func MapPrefixPath ¶
func MapPrefixPath() string
func QdiscTCInsert ¶
func SetMapPrefix ¶
func SetMapPrefix(path string)
func SetMapRoot ¶
func SetMapRoot(path string)
Types ¶
type DesiredAction ¶
type DesiredAction int
DesiredAction is the action to be performed on the BPF map
const ( // OK indicates that to further action is required and the entry is in // sync OK DesiredAction = iota // Insert indicates that the entry needs to be created or updated Insert // Delete indicates that the entry needs to be deleted Delete )
type DumpCallback ¶
type ErrorFunc ¶
type ErrorFunc func(msg *PerfEvent)
ErrorFunc is run when reading PerfEvent results in an error
type LostFunc ¶
type LostFunc func(msg *PerfEventLost, cpu int)
type Map ¶
type Map struct { MapInfo // contains filtered or unexported fields }
func OpenMap ¶
OpenMap opens the given bpf map and generates the Map info based in the information stored in the bpf map. *Warning*: Calling this function requires the caller to properly setup the MapInfo.MapKey and MapInfo.MapValues fields as those structures are not stored in the bpf map.
func (*Map) Count ¶
Count returns the number of elements in the map by iterating over it with BPF_MAP_GET_NEXT_KEY.
func (*Map) DeleteKey ¶
deleteMapEntry deletes the map entry corresponding to the given key. If ignoreMissing is set to true and the entry is not found, then the error metric is not incremented for missing entries and nil error is returned.
func (*Map) Dump ¶
Dump returns the map (type map[string][]string) which contains all data stored in BPF map.
func (*Map) DumpIfExists ¶
DumpIfExists dumps the contents of the map into hash via Dump() if the map file exists
func (*Map) DumpWithCallback ¶
func (m *Map) DumpWithCallback(cb DumpCallback) error
DumpWithCallback iterates over the Map and calls the given callback function on each iteration. That callback function is receiving the actual key and value. The callback function should consider creating a deepcopy of the key and value on between each iterations to avoid memory corruption.
func (*Map) DumpWithCallbackIfExists ¶
func (m *Map) DumpWithCallbackIfExists(cb DumpCallback) error
DumpWithCallbackIfExists is similar to DumpWithCallback, but returns earlier if the given map does not exist.
type MapInfo ¶
type MapInfo struct { MapType MapType MapKey MapKey KeySize uint32 MapValue MapValue // ReadValueSize is the value size that is used to read from the BPF maps // this value an the ValueSize values can be different for BPF_MAP_TYPE_PERCPU_HASH // for example. ReadValueSize uint32 ValueSize uint32 MaxEntries uint32 Flags uint32 InnerID uint32 }
type MapType ¶
type MapType int
MapType is an enumeration for valid BPF map types
const ( MapTypeUnspec MapType = iota MapTypeHash MapTypeArray MapTypeProgArray MapTypePerfEventArray MapTypePerCPUHash MapTypePerCPUArray MapTypeStackTrace MapTypeCgroupArray MapTypeLRUHash MapTypeLRUPerCPUHash MapTypeLPMTrie MapTypeArrayOfMaps MapTypeHashOfMaps MapTypeDevMap MapTypeSockMap MapTypeCPUMap MapTypeXSKMap MapTypeSockHash // MapTypeMaximum is the maximum supported known map type. MapTypeMaximum )
This enumeration must be in sync with enum bpf_prog_type in <linux/bpf.h>
type MapValidator ¶
type PerCpuEvents ¶
type PerCpuEvents struct { Cpus int Npages int Pagesize int // contains filtered or unexported fields }
func NewPerCpuEvents ¶
func NewPerCpuEvents(config *PerfEventConfig, log logrus.FieldLogger) (*PerCpuEvents, error)
func (*PerCpuEvents) CloseAll ¶
func (e *PerCpuEvents) CloseAll() error
func (*PerCpuEvents) ReadAll ¶
func (e *PerCpuEvents) ReadAll(maxEvents int, receive ReceiveFunc, lost LostFunc, handleError ErrorFunc) error
ReadAll reads perf events
type PerfEvent ¶
type PerfEvent struct { Fd int // contains filtered or unexported fields }
func PerfEventFromMemory ¶
func PerfEventFromMemory(page *PerfEventMmapPage, buf []byte) *PerfEvent
PerfEventFromMemory creates an in-memory PerfEvent object for testing and analysis purposes. No kernel interaction is made.
The caller MUST eventually call Disable() to free event resources.
func PerfEventOpen ¶
func (*PerfEvent) Read ¶
func (e *PerfEvent) Read(maxEvents int, receive ReceiveFunc, lostFn LostFunc, err ErrorFunc) (nread int)
Read attempts to read at most 'maxEvents' events from the perf event buffer, calling one of the receive / lost functions for each event. receiveFn is called when the event is a valid sample; lostFn is called when the kernel has attempted to write an event into the ringbuffer but ran out of space for the event.
If all events are not read within a time period (default 20s), it will call errFn() and stop reading events.
Returns the number of events read from the ring.
type PerfEventConfig ¶
type PerfEventConfig struct { NumCpus int NumPages int MapName string Type int Config int SampleType int WakeupEvents int }
func DefaultPerfEventConfig ¶
func DefaultPerfEventConfig() *PerfEventConfig
DefaultPerfEventConfig returns the default perf event configuration. It relies on the map root to be set.
type PerfEventHeader ¶
PerfEventHeader must match 'struct perf_event_header in <linux/perf_event.h>.
type PerfEventLost ¶
type PerfEventLost struct { PerfEventHeader Id uint64 Lost uint64 }
PerfEventLost must match 'struct perf_event_lost in kernel sources.
type PerfEventMmapPage ¶
type PerfEventMmapPage struct { Version uint32 // version number of this structure CompatVersion uint32 // lowest version this is compat with Lock uint32 // seqlock for synchronization Index uint32 // hardware event identifier Offset int64 // add to hardware event value TimeEnabled uint64 // time event active TimeRunning uint64 // time event on cpu //union { Capabilities uint64 // cap_user_rdpmc : 1, /* The RDPMC instruction can be used to read counts */ // cap_user_time : 1, /* The time_* fields are used */ // cap_user_time_zero : 1, /* The time_zero field is used */ // cap_____res : 59; //}; //}; PmcWidth uint16 TimeShift uint16 TimeMult uint32 TimeOffset uint64 TimeZero uint64 Size uint32 Reserved [118*8 + 4]uint8 // align to 1k. DataHead uint64 // head in the data section DataTail uint64 // user-space written tail DataOffset uint64 // where the buffer starts DataSize uint64 // data buffer size AuxHead uint64 AuxTail uint64 AuxOffset uint64 AuxSize uint64 }
PerfEventMmapPage reflects the Linux 'struct perf_event_mmap_page'
func (*PerfEventMmapPage) Decode ¶
func (p *PerfEventMmapPage) Decode(reader io.ReadSeeker) error
Decode populates 'p' base on the bytes read from the specified reader.
This function should not be used from performance-sensitive code.
type PerfEventSample ¶
type PerfEventSample struct { PerfEventHeader Size uint32 // contains filtered or unexported fields }
PerfEventSample must match 'struct perf_event_sample in kernel sources.
func (*PerfEventSample) DataCopy ¶
func (e *PerfEventSample) DataCopy() []byte
func (*PerfEventSample) DataDirect ¶
func (e *PerfEventSample) DataDirect() []byte
type ReceiveFunc ¶
type ReceiveFunc func(msg *PerfEventSample, cpu int)