netns.go 705 B

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