2023-01-17 20:59:04 +00:00
|
|
|
package link
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-04-03 09:16:17 +00:00
|
|
|
"unsafe"
|
2023-01-17 20:59:04 +00:00
|
|
|
|
|
|
|
"github.com/cilium/ebpf"
|
2023-04-03 09:16:17 +00:00
|
|
|
"github.com/cilium/ebpf/internal/sys"
|
2023-01-17 20:59:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IterOptions struct {
|
|
|
|
// Program must be of type Tracing with attach type
|
|
|
|
// AttachTraceIter. The kind of iterator to attach to is
|
|
|
|
// determined at load time via the AttachTo field.
|
|
|
|
//
|
|
|
|
// AttachTo requires the kernel to include BTF of itself,
|
|
|
|
// and it to be compiled with a recent pahole (>= 1.16).
|
|
|
|
Program *ebpf.Program
|
2023-04-03 09:16:17 +00:00
|
|
|
|
|
|
|
// Map specifies the target map for bpf_map_elem and sockmap iterators.
|
|
|
|
// It may be nil.
|
|
|
|
Map *ebpf.Map
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AttachIter attaches a BPF seq_file iterator.
|
|
|
|
func AttachIter(opts IterOptions) (*Iter, error) {
|
2023-04-03 09:16:17 +00:00
|
|
|
if err := haveBPFLink(); err != nil {
|
|
|
|
return nil, err
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
progFd := opts.Program.FD()
|
|
|
|
if progFd < 0 {
|
|
|
|
return nil, fmt.Errorf("invalid program: %s", sys.ErrClosedFd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var info bpfIterLinkInfoMap
|
|
|
|
if opts.Map != nil {
|
|
|
|
mapFd := opts.Map.FD()
|
|
|
|
if mapFd < 0 {
|
|
|
|
return nil, fmt.Errorf("invalid map: %w", sys.ErrClosedFd)
|
|
|
|
}
|
|
|
|
info.map_fd = uint32(mapFd)
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := sys.LinkCreateIterAttr{
|
|
|
|
ProgFd: uint32(progFd),
|
|
|
|
AttachType: sys.AttachType(ebpf.AttachTraceIter),
|
|
|
|
IterInfo: sys.NewPointer(unsafe.Pointer(&info)),
|
|
|
|
IterInfoLen: uint32(unsafe.Sizeof(info)),
|
|
|
|
}
|
2023-01-17 20:59:04 +00:00
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
fd, err := sys.LinkCreateIter(&attr)
|
2023-01-17 20:59:04 +00:00
|
|
|
if err != nil {
|
2023-04-03 09:16:17 +00:00
|
|
|
return nil, fmt.Errorf("can't link iterator: %w", err)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
return &Iter{RawLink{fd, ""}}, err
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iter represents an attached bpf_iter.
|
|
|
|
type Iter struct {
|
2023-04-03 09:16:17 +00:00
|
|
|
RawLink
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open creates a new instance of the iterator.
|
|
|
|
//
|
|
|
|
// Reading from the returned reader triggers the BPF program.
|
|
|
|
func (it *Iter) Open() (io.ReadCloser, error) {
|
2023-04-03 09:16:17 +00:00
|
|
|
attr := &sys.IterCreateAttr{
|
|
|
|
LinkFd: it.fd.Uint(),
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
fd, err := sys.IterCreate(attr)
|
2023-01-17 20:59:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't create iterator: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd.File("bpf_iter"), nil
|
|
|
|
}
|
2023-04-03 09:16:17 +00:00
|
|
|
|
|
|
|
// union bpf_iter_link_info.map
|
|
|
|
type bpfIterLinkInfoMap struct {
|
|
|
|
map_fd uint32
|
|
|
|
}
|