-
Notifications
You must be signed in to change notification settings - Fork 3
/
netns.go
123 lines (93 loc) · 2.38 KB
/
netns.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package ns
import (
"fmt"
"log"
"os"
"path"
"runtime"
"syscall"
virshnetns "github.com/vishvananda/netns"
)
const (
netnsDir = "/var/run/netns/"
)
//NetNS allows communicating with a network namespace
type NetNS struct {
handle virshnetns.NsHandle
name string
}
//Fd provides the file decriptor of the namespace
func (n *NetNS) Fd() int {
return int(n.handle)
}
//CreateNetNS creates a new network namespace and sets a name
func CreateNetNS(name string) (*NetNS, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
origns, _ := virshnetns.Get()
defer origns.Close()
routerNetNs, err := virshnetns.New()
if err != nil {
return nil, err
}
ns := &NetNS{
handle: routerNetNs,
}
if len(name) != 0 {
if err := ns.SetNSName(name); err != nil {
log.Printf("Failed to add a name to NS: %s\n", err)
return ns, fmt.Errorf("failed to add name to ns: %s", err)
}
}
virshnetns.Set(origns)
return ns, nil
}
//SetNSName sets a name for the netns which can be accessible via 'ip netns' commands
func (n *NetNS) SetNSName(name string) error {
//Attempt to make the alias dir if does not exists
if _, err := os.Stat(netnsDir); os.IsNotExist(err) {
os.MkdirAll(netnsDir, 0777)
}
p := path.Join(netnsDir, name)
f, err := os.OpenFile(p, os.O_CREATE|os.O_EXCL, 0444)
if err != nil {
return fmt.Errorf("failed to create netns name: %s", err)
}
f.Close()
nspath := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid())
if err := syscall.Mount(nspath, p, "bind", syscall.MS_BIND, ""); err != nil {
return err
}
n.name = name
return nil
}
//Unbind removes the mount created from setNSName
func (n *NetNS) Unbind() error {
if len(n.name) == 0 {
return fmt.Errorf("ns has no name to unbind")
}
p := path.Join(netnsDir, n.name)
if err := syscall.Unmount(p, 0); err != nil {
return err
}
return os.Remove(p)
}
//Exec switches to the specified netns and executes the closure
//WARNING: CANNOT EXECUTE WITH GOROUTINES _INSIDE_ THE CLOSURE
func (n *NetNS) Exec(fn func() error) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
origns, _ := virshnetns.Get()
defer origns.Close()
//Switch to our namespace
virshnetns.Set(n.handle)
err := fn()
//Switch back to original namespace
virshnetns.Set(origns)
return err
}
//Close unbinds and closes the netns
func (n *NetNS) Close() error {
n.Unbind()
return n.handle.Close()
}