Repository navigation
Expand file tree
/
Copy pathnetwork.go
More file actions
196 lines (173 loc) · 5.38 KB
/
Copy pathnetwork.go
File metadata and controls
196 lines (173 loc) · 5.38 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
"log"
"math/rand"
"net"
)
func createMACAddress() net.HardwareAddr {
hw := make(net.HardwareAddr, 6)
hw[0] = 0x02
hw[1] = 0x42
rand.Read(hw[2:])
return hw
}
func createIPAddress() string {
byte1 := rand.Intn(254)
byte2 := rand.Intn(254)
return fmt.Sprintf("172.29.%d.%d", byte1, byte2)
}
/*
Go through the list of interfaces and return true if the gocker0 bridge is up
*/
func isGockerBridgeUp() (bool, error) {
if links, err := netlink.LinkList(); err != nil {
log.Printf("Unable to get list of links.\n")
return false, err
} else {
for _, link := range links {
if link.Type() == "bridge" && link.Attrs().Name == "gocker0" {
return true, nil
}
}
return false, err
}
}
/*
This function sets up the "gocker0" bridge, which is our main bridge
interface. To keep things simple, we assign the hopefully unassigned
and obscure private IP 172.29.0.1 to it, which is from the range of
IPs which we will also use for our containers.
*/
func setupGockerBridge() error {
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = "gocker0"
gockerBridge := &netlink.Bridge{LinkAttrs: linkAttrs}
if err := netlink.LinkAdd(gockerBridge); err != nil {
return err
}
addr, _ := netlink.ParseAddr("172.29.0.1/16")
netlink.AddrAdd(gockerBridge, addr)
netlink.LinkSetUp(gockerBridge)
return nil
}
func setupVirtualEthOnHost(containerID string) error {
veth0 := "veth0_" + containerID[:6]
veth1 := "veth1_" + containerID[:6]
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = veth0
veth0Struct := &netlink.Veth{
LinkAttrs: linkAttrs,
PeerName: veth1,
PeerHardwareAddr: createMACAddress(),
}
if err := netlink.LinkAdd(veth0Struct); err != nil {
return err
}
netlink.LinkSetUp(veth0Struct)
gockerBridge, _ := netlink.LinkByName("gocker0")
netlink.LinkSetMaster(veth0Struct, gockerBridge)
return nil
}
func setupContainerNetworkInterfaceStep1(containerID string) {
nsMount := getGockerNetNsPath() + "/" + containerID
fd, err := unix.Open(nsMount, unix.O_RDONLY, 0)
defer unix.Close(fd)
if err != nil {
log.Fatalf("Unable to open: %v\n", err)
}
/* Set veth1 of the new container to the new network namespace */
veth1 := "veth1_" + containerID[:6]
veth1Link, err := netlink.LinkByName(veth1)
if err != nil {
log.Fatalf("Unable to fetch veth1: %v\n", err)
}
if err := netlink.LinkSetNsFd(veth1Link, fd); err != nil {
log.Fatalf("Unable to set network namespace for veth1: %v\n", err)
}
}
func setupContainerNetworkInterfaceStep2(containerID string) {
nsMount := getGockerNetNsPath() + "/" + containerID
fd, err := unix.Open(nsMount, unix.O_RDONLY, 0)
defer unix.Close(fd)
if err != nil {
log.Fatalf("Unable to open: %v\n", err)
}
if err := unix.Setns(fd, unix.CLONE_NEWNET); err != nil {
log.Fatalf("Setns system call failed: %v\n", err)
}
veth1 := "veth1_" + containerID[:6]
veth1Link, err := netlink.LinkByName(veth1)
if err != nil {
log.Fatalf("Unable to fetch veth1: %v\n", err)
}
addr, _ := netlink.ParseAddr(createIPAddress() + "/16")
if err := netlink.AddrAdd(veth1Link, addr); err != nil {
log.Fatalf("Error assigning IP to veth1: %v\n", err)
}
/* Bring up the interface */
doOrDieWithMsg(netlink.LinkSetUp(veth1Link), "Unable to bring up veth1")
/* Add a default route */
route := netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: veth1Link.Attrs().Index,
Gw: net.ParseIP("172.29.0.1"),
Dst: nil,
}
doOrDieWithMsg(netlink.RouteAdd(&route), "Unable to add default route")
}
/*
This is the function that sets the IP address for the local interface.
There seems to be a bug in the netlink library in that it does not
succeed in looking up the local interface by name, always returning an
error. As a workaround, we loop through the interfaces, compare the name,
set the IP and make the interface up.
*/
func setupLocalInterface() {
links, _ := netlink.LinkList()
for _, link := range links {
if link.Attrs().Name == "lo" {
loAddr, _ := netlink.ParseAddr("127.0.0.1/32")
if err := netlink.AddrAdd(link, loAddr); err != nil {
log.Println("Unable to configure local interface!")
}
netlink.LinkSetUp(link)
}
}
}
func setupNewNetworkNamespace(containerID string) {
_ = createDirsIfDontExist([]string{getGockerNetNsPath()})
nsMount := getGockerNetNsPath() + "/" + containerID
if _, err := unix.Open(nsMount, unix.O_RDONLY|unix.O_CREAT|unix.O_EXCL, 0644); err != nil {
log.Fatalf("Unable to open bind mount file: :%v\n", err)
}
fd, err := unix.Open("/proc/self/ns/net", unix.O_RDONLY, 0)
defer unix.Close(fd)
if err != nil {
log.Fatalf("Unable to open: %v\n", err)
}
if err := unix.Unshare(unix.CLONE_NEWNET); err != nil {
log.Fatalf("Unshare system call failed: %v\n", err)
}
if err := unix.Mount("/proc/self/ns/net", nsMount, "bind", unix.MS_BIND, ""); err != nil {
log.Fatalf("Mount system call failed: %v\n", err)
}
if err := unix.Setns(fd, unix.CLONE_NEWNET); err != nil {
log.Fatalf("Setns system call failed: %v\n", err)
}
}
func joinContainerNetworkNamespace(containerID string) error {
nsMount := getGockerNetNsPath() + "/" + containerID
fd, err := unix.Open(nsMount, unix.O_RDONLY, 0)
if err != nil {
log.Printf("Unable to open: %v\n", err)
return err
}
if err := unix.Setns(fd, unix.CLONE_NEWNET); err != nil {
log.Printf("Setns system call failed: %v\n", err)
return err
}
return nil
}