| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
|
| 8 |
"github.com/cilium/ebpf" |
| 9 |
"github.com/cilium/ebpf/internal/sys" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
type NetNsLink struct { |
| 14 |
RawLink |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
func AttachNetNs(ns int, prog *ebpf.Program) (*NetNsLink, error) { |
| 19 |
var attach ebpf.AttachType |
| 20 |
switch t := prog.Type(); t { |
| 21 |
case ebpf.FlowDissector: |
| 22 |
attach = ebpf.AttachFlowDissector |
| 23 |
case ebpf.SkLookup: |
| 24 |
attach = ebpf.AttachSkLookup |
| 25 |
default: |
| 26 |
return nil, fmt.Errorf("can't attach %v to network namespace", t) |
| 27 |
} |
| 28 |
|
| 29 |
link, err := AttachRawLink(RawLinkOptions{ |
| 30 |
Target: ns, |
| 31 |
Program: prog, |
| 32 |
Attach: attach, |
| 33 |
}) |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
|
| 38 |
return &NetNsLink{*link}, nil |
| 39 |
} |
| 40 |
|
| 41 |
func (ns *NetNsLink) Info() (*Info, error) { |
| 42 |
var info sys.NetNsLinkInfo |
| 43 |
if err := sys.ObjInfo(ns.fd, &info); err != nil { |
| 44 |
return nil, fmt.Errorf("netns link info: %s", err) |
| 45 |
} |
| 46 |
extra := &NetNsInfo{ |
| 47 |
NetnsInode: info.NetnsIno, |
| 48 |
AttachType: info.AttachType, |
| 49 |
} |
| 50 |
|
| 51 |
return &Info{ |
| 52 |
info.Type, |
| 53 |
info.Id, |
| 54 |
ebpf.ProgramID(info.ProgId), |
| 55 |
extra, |
| 56 |
}, nil |
| 57 |
} |