This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
setup_container_linux.go
365 lines (318 loc) · 9.53 KB
/
setup_container_linux.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// +build linux
package allocate
import (
"encoding/binary"
"errors"
"fmt"
"math/rand"
"net"
"reflect"
"time"
"github.com/Netflix/titus-executor/vpc"
"github.com/Netflix/titus-executor/vpc/context"
"github.com/Netflix/titus-executor/vpc/types"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
"golang.org/x/sys/unix"
)
const (
mtu = 9000
hz = 100.0
networkSetupWait = 30 * time.Second
)
var (
errLinkNotFound = errors.New("Link not found")
errAddressSetupTimeout = errors.New("IPv6 address setup timed out")
)
func doSetupContainer(parentCtx *context.VPCContext, netnsfd int, bandwidth uint64, burst, jumbo bool, allocation types.Allocation) (netlink.Link, error) {
ip4 := net.ParseIP(allocation.IPV4Address)
ip6 := net.ParseIP(allocation.IPV6Address)
parentLink, err := getLink(allocation.ENIMACAddress)
if err != nil {
return nil, err
}
nsHandle, err := netlink.NewHandleAt(netns.NsHandle(netnsfd))
if err != nil {
return nil, err
}
defer nsHandle.Delete()
mtu := parentLink.Attrs().MTU
if !jumbo {
mtu = 1500
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
containerInterfaceName := fmt.Sprintf("tmp-%d", r.Intn(10000))
ipvlan := netlink.IPVlan{
LinkAttrs: netlink.LinkAttrs{
Name: containerInterfaceName,
ParentIndex: parentLink.Attrs().Index,
Namespace: netlink.NsFd(netnsfd),
MTU: mtu,
TxQLen: -1,
},
Mode: netlink.IPVLAN_MODE_L2,
}
err = netlink.LinkAdd(&ipvlan)
if err != nil {
parentCtx.Logger.Error("Could not add link: ", err)
return nil, err
}
// If things fail here, it's fairly bad, because we've added the link to the namespace, but we don't know
// what it's index is, so there's no point returning it.
parentCtx.Logger.Debugf("Added link: %+v ", ipvlan)
newLink, err := nsHandle.LinkByName(containerInterfaceName)
if err != nil {
parentCtx.Logger.Error("Could not find after adding link: ", err)
return nil, err
}
return newLink, configureLink(parentCtx, nsHandle, newLink, bandwidth, mtu, burst, allocation.SubnetID, ip4, ip6)
}
func addIPv4AddressAndRoute(parentCtx *context.VPCContext, subnetId string, nsHandle *netlink.Handle, link netlink.Link, ip net.IP) error {
subnet, err := parentCtx.Cache.DescribeSubnet(parentCtx, subnetId)
if err != nil {
return err
}
// We assume that it always gives us the subnet
_, ipnet, err := net.ParseCIDR(*subnet.CidrBlock)
if err != nil {
return err
}
// The netlink package appears to automatically calculate broadcast
new4Addr := netlink.Addr{
IPNet: &net.IPNet{IP: ip, Mask: ipnet.Mask},
}
err = nsHandle.AddrAdd(link, &new4Addr)
if err != nil {
parentCtx.Logger.Error("Unable to add IPv4 addr to link: ", err)
return err
}
gateway := cidr.Inc(ipnet.IP)
newRoute := netlink.Route{
Gw: gateway,
Src: ip,
LinkIndex: link.Attrs().Index,
}
err = nsHandle.RouteAdd(&newRoute)
if err != nil {
parentCtx.Logger.Error("Unable to add route to link: ", err)
return err
}
return nil
}
func configureLink(parentCtx *context.VPCContext, nsHandle *netlink.Handle, link netlink.Link, bandwidth uint64, mtu int, burst bool, subnetId string, ip4, ip6 net.IP) error {
// Rename link
err := nsHandle.LinkSetName(link, "eth0")
if err != nil {
parentCtx.Logger.Error("Unable to set link name: ", err)
return err
}
err = nsHandle.LinkSetUp(link)
if err != nil {
parentCtx.Logger.Error("Unable to set link up: ", err)
return err
}
err = nsHandle.LinkSetMTU(link, mtu)
if err != nil {
parentCtx.Logger.Error("Unable to set link mtu: ", err)
return err
}
if ip6 != nil {
// Amazon only gives out /128s
new6Addr := netlink.Addr{
IPNet: &net.IPNet{IP: ip6, Mask: net.CIDRMask(128, 128)},
}
err = nsHandle.AddrAdd(link, &new6Addr)
if err != nil {
parentCtx.Logger.Error("Unable to add IPv6 addr to link: ", err)
return err
}
}
err = addIPv4AddressAndRoute(parentCtx, subnetId, nsHandle, link, ip4)
if err != nil {
return err
}
// TODO: Wire up IFB / BPF / Bandwidth limits for IPv6
err = setupIFBClasses(parentCtx, bandwidth, burst, ip4)
if err != nil {
return err
}
if ip6 != nil {
return waitForAddressUp(parentCtx, nsHandle, link)
}
return nil
}
// This checks if we have a globally-scoped, non-tentative IPv6 address
func isIPv6Ready(nsHandle *netlink.Handle, link netlink.Link) (bool, error) {
addrs, err := nsHandle.AddrList(link, netlink.FAMILY_V6)
if err != nil {
return false, err
}
for _, addr := range addrs {
if addr.Scope == int(netlink.SCOPE_UNIVERSE) && addr.Flags&unix.IFA_F_TENTATIVE == 0 {
return true, nil
}
}
return false, nil
}
func waitForAddressUp(parentCtx *context.VPCContext, nsHandle *netlink.Handle, link netlink.Link) error {
ctx, cancel := parentCtx.WithTimeout(networkSetupWait)
defer cancel()
pollTimer := time.NewTicker(100 * time.Millisecond)
defer pollTimer.Stop()
for {
select {
case <-ctx.Done():
return errAddressSetupTimeout
case <-pollTimer.C:
if found, err := isIPv6Ready(nsHandle, link); err != nil {
return err
} else if found {
return nil
}
}
}
}
func setupIFBClasses(parentCtx *context.VPCContext, bandwidth uint64, burst bool, ip net.IP) error {
// The class is based on the last two parts of the IPv4 address
// The reasoning is that 0 and 1 of the subnet are reserved by Amazon, so we will never get those IPs
// and VPCs can never have subnets that are larger than /16. Each instance is scoped to a single subnet,
// so it should never have a collision
// At least this is true as of the time of writing
ifbEgress, err := netlink.LinkByName(vpc.EgressIFB)
if err != nil {
return err
}
ifbIngress, err := netlink.LinkByName(vpc.IngressIFB)
if err != nil {
return err
}
err = setupIFBClass(parentCtx, bandwidth, burst, ip, ifbEgress)
if err != nil {
return err
}
err = setupIFBSubqdisc(parentCtx, ip, ifbEgress)
if err != nil {
return err
}
err = setupIFBClass(parentCtx, bandwidth, burst, ip, ifbIngress)
if err != nil {
return err
}
return setupIFBSubqdisc(parentCtx, ip, ifbIngress)
}
func setupIFBSubqdisc(parentCtx *context.VPCContext, ip net.IP, link netlink.Link) error {
handle := ipaddressToHandle(ip)
// The qdisc wasn't found, add it
attrs := netlink.QdiscAttrs{
LinkIndex: link.Attrs().Index,
Handle: netlink.MakeHandle(handle, 0),
Parent: netlink.MakeHandle(1, handle),
}
qdisc := netlink.NewFqCodel(attrs)
qdisc.Quantum = 9001
err := netlink.QdiscAdd(qdisc)
if err != nil && err != unix.EEXIST {
return err
}
return nil
}
func setupIFBClass(parentCtx *context.VPCContext, bandwidth uint64, burst bool, ip net.IP, link netlink.Link) error {
handle := ipaddressToHandle(ip)
classattrs := netlink.ClassAttrs{
LinkIndex: link.Attrs().Index,
Parent: netlink.MakeHandle(1, 1),
// We assume that
Handle: netlink.MakeHandle(1, handle),
}
ceil := bandwidth
if burst {
ceil = vpc.GetMaxNetworkbps(parentCtx.InstanceType)
}
htbclassattrs := netlink.HtbClassAttrs{
Rate: bandwidth,
Ceil: ceil,
Buffer: uint32(float64(bandwidth)/(hz*8) + float64(mtu)),
Cbuffer: uint32(float64(ceil)/(hz*8) + float64(mtu)),
}
class := netlink.NewHtbClass(classattrs, htbclassattrs)
parentCtx.Logger.Debug("Setting up HTB class: ", class)
err := netlink.ClassAdd(class)
if err != nil {
parentCtx.Logger.Warning("Could not add class: ", err)
return netlink.ClassReplace(class)
}
return nil
}
func getLink(macAddress string) (netlink.Link, error) {
links, err := netlink.LinkList()
if err != nil {
return nil, err
}
mac, err := net.ParseMAC(macAddress)
if err != nil {
return nil, err
}
for _, link := range links {
if reflect.DeepEqual(link.Attrs().HardwareAddr, mac) {
return link, nil
}
}
return nil, errLinkNotFound
}
func ipaddressToHandle(ip net.IP) uint16 {
return binary.BigEndian.Uint16([]byte(ip.To4()[2:4]))
}
func teardownNetwork(ctx *context.VPCContext, allocation types.Allocation, link netlink.Link, netnsfd int) {
deleteLink(ctx, link, netnsfd)
ip := net.ParseIP(allocation.IPV4Address)
// Removing the classes automatically removes the qdiscs
ifbEgress, err := netlink.LinkByName(vpc.EgressIFB)
if err == nil {
removeClass(ctx, ip, ifbEgress)
} else {
ctx.Logger.Warning("Unable to find ifb egress, during deallocation: ", err)
}
ifbIngress, err := netlink.LinkByName(vpc.IngressIFB)
if err == nil {
removeClass(ctx, ip, ifbIngress)
} else {
ctx.Logger.Warning("Unable to find ifb ingress, during deallocation: ", err)
}
}
func deleteLink(ctx *context.VPCContext, link netlink.Link, netnsfd int) {
if link == nil {
ctx.Logger.Debug("Link not setup, not deleting link")
return
}
nsHandle, err := netlink.NewHandleAt(netns.NsHandle(netnsfd))
if err != nil {
ctx.Logger.Warning("Unable to get handle")
}
defer nsHandle.Delete()
err = nsHandle.LinkDel(link)
if err != nil {
ctx.Logger.Error("Unable to delete link: ", err)
}
}
func removeClass(ctx *context.VPCContext, ip net.IP, link netlink.Link) {
handle := ipaddressToHandle(ip)
classes, err := netlink.ClassList(link, netlink.MakeHandle(1, 1))
if err != nil {
ctx.Logger.Errorf("Unable to list classes on link %v because %v", link, err)
return
}
for _, class := range classes {
htbClass := class.(*netlink.HtbClass)
ctx.Logger.Debug("Class: ", class)
ctx.Logger.Debug("Class: ", class.Attrs())
if htbClass.Attrs().Handle == netlink.MakeHandle(1, handle) {
err = netlink.ClassDel(class)
if err != nil {
ctx.Logger.Warning("Unable to remove class: ", err)
}
return
}
}
ctx.Logger.Warning("Unable to find class for container")
}