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