| 1 |
package link |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/cilium/ebpf" |
| 7 |
"github.com/cilium/ebpf/internal/sys" |
| 8 |
) |
| 9 |
|
| 10 |
type structOpsLink struct { |
| 11 |
RawLink |
| 12 |
} |
| 13 |
|
| 14 |
func (*structOpsLink) Update(*ebpf.Program) error { |
| 15 |
return fmt.Errorf("update struct_ops link: %w", ErrNotSupported) |
| 16 |
} |
| 17 |
|
| 18 |
type StructOpsOptions struct { |
| 19 |
Map *ebpf.Map |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
func AttachStructOps(opts StructOpsOptions) (Link, error) { |
| 25 |
m := opts.Map |
| 26 |
|
| 27 |
if m == nil { |
| 28 |
return nil, fmt.Errorf("map cannot be nil") |
| 29 |
} |
| 30 |
|
| 31 |
if t := m.Type(); t != ebpf.StructOpsMap { |
| 32 |
return nil, fmt.Errorf("can't attach non-struct_ops map") |
| 33 |
} |
| 34 |
|
| 35 |
mapFD := m.FD() |
| 36 |
if mapFD <= 0 { |
| 37 |
return nil, fmt.Errorf("invalid map: %s", sys.ErrClosedFd) |
| 38 |
} |
| 39 |
|
| 40 |
fd, err := sys.LinkCreate(&sys.LinkCreateAttr{ |
| 41 |
|
| 42 |
ProgFd: uint32(mapFD), |
| 43 |
AttachType: sys.AttachType(ebpf.AttachStructOps), |
| 44 |
TargetFd: 0, |
| 45 |
}) |
| 46 |
if err != nil { |
| 47 |
return nil, fmt.Errorf("attach StructOps: create link: %w", err) |
| 48 |
} |
| 49 |
|
| 50 |
return &structOpsLink{RawLink{fd: fd}}, nil |
| 51 |
} |