summaryrefslogtreecommitdiff
path: root/vendor/github.com/cilium/ebpf/link/netns.go
blob: 3533ff0fa61b77acf423bb306c5df65cd8cc2873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package link

import (
	"fmt"

	"github.com/cilium/ebpf"
)

// NetNsInfo contains metadata about a network namespace link.
type NetNsInfo struct {
	RawLinkInfo
}

// NetNsLink is a program attached to a network namespace.
type NetNsLink struct {
	*RawLink
}

// AttachNetNs attaches a program to a network namespace.
func AttachNetNs(ns int, prog *ebpf.Program) (*NetNsLink, error) {
	var attach ebpf.AttachType
	switch t := prog.Type(); t {
	case ebpf.FlowDissector:
		attach = ebpf.AttachFlowDissector
	case ebpf.SkLookup:
		attach = ebpf.AttachSkLookup
	default:
		return nil, fmt.Errorf("can't attach %v to network namespace", t)
	}

	link, err := AttachRawLink(RawLinkOptions{
		Target:  ns,
		Program: prog,
		Attach:  attach,
	})
	if err != nil {
		return nil, err
	}

	return &NetNsLink{link}, nil
}

// LoadPinnedNetNs loads a network namespace link from bpffs.
func LoadPinnedNetNs(fileName string) (*NetNsLink, error) {
	link, err := loadPinnedRawLink(fileName, NetNsType)
	if err != nil {
		return nil, err
	}

	return &NetNsLink{link}, nil
}

// Info returns information about the link.
func (nns *NetNsLink) Info() (*NetNsInfo, error) {
	info, err := nns.RawLink.Info()
	if err != nil {
		return nil, err
	}
	return &NetNsInfo{*info}, nil
}