-
Notifications
You must be signed in to change notification settings - Fork 3
/
net.go
173 lines (138 loc) · 3.75 KB
/
net.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"runtime"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)
type networkConfig struct {
bridgeAddr string
containerVethAddr string
containerPid int
}
type link struct {
link netlink.Link
}
func (l link) up() {
err := netlink.LinkSetUp(l.link)
if err != nil {
panic(fmt.Sprintf("Error bringing link %v up: %s\n", l.link, err))
}
}
func (l link) addAddr(rawAddr string) {
addr, err := netlink.ParseAddr(rawAddr)
if err != nil {
panic(fmt.Sprintf("Error parsing addr %s: %s\n", rawAddr, err))
}
err = netlink.AddrReplace(l.link, addr)
if err != nil {
panic(fmt.Sprintf("Error adding addr to %v: %s\n", l.link, err))
}
}
func (l link) setNs(pid int) {
err := netlink.LinkSetNsPid(l.link, pid)
if err != nil {
panic(fmt.Sprintf("Error moving link %v to pid ns %d: %s\n", l.link, pid, err))
}
}
type netNsExecr struct{}
func (e netNsExecr) exec(pid int, work func()) {
// Lock the OS Thread so we don't accidentally switch namespaces.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Save the current namespace.
currentNs, err := netns.Get()
defer currentNs.Close()
if err != nil {
panic(fmt.Sprintf("Error exec'ing net ns of pid %d: %s\n", pid, err))
}
// Get handle for pid's network namespace.
pidNs, err := netns.GetFromPid(pid)
if err != nil {
panic(fmt.Sprintf("Error exec'ing net ns of pid %d: %s\n", pid, err))
}
// Switch namespace.
err = netns.Set(pidNs)
defer pidNs.Close()
if err != nil {
panic(fmt.Sprintf("Error exec'ing net ns of pid %d: %s\n", pid, err))
}
// Do the namespace-scoped work.
work()
// Switch back to the original namespace.
err = netns.Set(currentNs)
if err != nil {
panic(fmt.Sprintf("Error exec'ing net ns of pid %d: %s\n", pid, err))
}
}
func createVethPair(containerPid int) (hostVeth link, containerVeth link) {
// TODO: don't leak pid in veth name.
hostVethName := fmt.Sprintf("veth%dh", containerPid)
containerVethName := fmt.Sprintf("veth%dc", containerPid)
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = hostVethName
hostVeth.link = &netlink.Veth{
LinkAttrs: linkAttrs,
PeerName: containerVethName,
}
err := netlink.LinkAdd(hostVeth.link)
if err != nil {
panic(fmt.Sprintf("Error creating veth pair: %s\n", err))
}
containerVeth.link, err = netlink.LinkByName(containerVethName)
if err != nil {
panic(fmt.Sprintf("Error creating veth pair: %s\n", err))
}
return hostVeth, containerVeth
}
func createBridge() (bridge link) {
bridgeName := "goContainers0"
link, err := findLink(bridgeName)
if err != nil {
panic(fmt.Sprintf("Error creating bridge: %s\n", err))
}
if link != nil {
bridge.link = link
return bridge
}
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = bridgeName
bridge.link = &netlink.Bridge{LinkAttrs: linkAttrs}
err = netlink.LinkAdd(bridge.link)
if err != nil {
panic(fmt.Sprintf("Error creating bridge: %s\n", err))
}
return bridge
}
func setupNetwork(config networkConfig) {
bridge := createBridge()
bridge.up()
bridge.addAddr(config.bridgeAddr)
hostVeth, containerVeth := createVethPair(config.containerPid)
hostVeth.up()
netlink.LinkSetMaster(hostVeth.link, bridge.link.(*netlink.Bridge))
containerVeth.setNs(config.containerPid)
execer := netNsExecr{}
work := func() {
// Ignore error case; bringing up lo is not that important.
lo, err := netlink.LinkByName("lo")
if err == nil {
link{link: lo}.up()
}
containerVeth.addAddr(config.containerVethAddr)
containerVeth.up()
}
execer.exec(config.containerPid, work)
}
func findLink(name string) (netlink.Link, error) {
link, err := netlink.LinkByName(name)
if err != nil {
switch err.(type) {
case netlink.LinkNotFoundError:
return nil, nil
default:
return nil, err
}
}
return link, nil
}