| 1 |
package link |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/cilium/ebpf/internal" |
| 7 |
"github.com/cilium/ebpf/internal/efw" |
| 8 |
"github.com/cilium/ebpf/internal/platform" |
| 9 |
"github.com/cilium/ebpf/internal/sys" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
func AttachRawLink(opts RawLinkOptions) (*RawLink, error) { |
| 14 |
if opts.Target != 0 || opts.BTF != 0 || opts.Flags != 0 { |
| 15 |
return nil, fmt.Errorf("specified option(s) %w", internal.ErrNotSupportedOnOS) |
| 16 |
} |
| 17 |
|
| 18 |
plat, attachType := platform.DecodeConstant(opts.Attach) |
| 19 |
if plat != platform.Windows { |
| 20 |
return nil, fmt.Errorf("attach type %s: %w", opts.Attach, internal.ErrNotSupportedOnOS) |
| 21 |
} |
| 22 |
|
| 23 |
attachTypeGUID, err := efw.EbpfGetEbpfAttachType(attachType) |
| 24 |
if err != nil { |
| 25 |
return nil, fmt.Errorf("get attach type: %w", err) |
| 26 |
} |
| 27 |
|
| 28 |
progFd := opts.Program.FD() |
| 29 |
if progFd < 0 { |
| 30 |
return nil, fmt.Errorf("invalid program: %s", sys.ErrClosedFd) |
| 31 |
} |
| 32 |
|
| 33 |
raw, err := efw.EbpfProgramAttachFds(progFd, attachTypeGUID, nil, 0) |
| 34 |
if err != nil { |
| 35 |
return nil, fmt.Errorf("attach link: %w", err) |
| 36 |
} |
| 37 |
|
| 38 |
fd, err := sys.NewFD(int(raw)) |
| 39 |
if err != nil { |
| 40 |
return nil, err |
| 41 |
} |
| 42 |
|
| 43 |
return &RawLink{fd: fd}, nil |
| 44 |
} |
| 45 |
|
| 46 |
func wrapRawLink(raw *RawLink) (Link, error) { |
| 47 |
return raw, nil |
| 48 |
} |