github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/link/netns.go (about) 1 package link 2 3 import ( 4 "fmt" 5 6 "github.com/cilium/ebpf" 7 ) 8 9 // NetNsLink is a program attached to a network namespace. 10 type NetNsLink struct { 11 RawLink 12 } 13 14 // AttachNetNs attaches a program to a network namespace. 15 func AttachNetNs(ns int, prog *ebpf.Program) (*NetNsLink, error) { 16 var attach ebpf.AttachType 17 switch t := prog.Type(); t { 18 case ebpf.FlowDissector: 19 attach = ebpf.AttachFlowDissector 20 case ebpf.SkLookup: 21 attach = ebpf.AttachSkLookup 22 default: 23 return nil, fmt.Errorf("can't attach %v to network namespace", t) 24 } 25 26 link, err := AttachRawLink(RawLinkOptions{ 27 Target: ns, 28 Program: prog, 29 Attach: attach, 30 }) 31 if err != nil { 32 return nil, err 33 } 34 35 return &NetNsLink{*link}, nil 36 }