/
driver.go
525 lines (454 loc) · 19.1 KB
/
driver.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package driver
import (
"fmt"
"net"
"os"
"syscall"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/vishvananda/netlink"
"github.com/aws/amazon-vpc-cni-k8s/pkg/ipwrapper"
"github.com/aws/amazon-vpc-cni-k8s/pkg/netlinkwrapper"
"github.com/aws/amazon-vpc-cni-k8s/pkg/networkutils"
"github.com/aws/amazon-vpc-cni-k8s/pkg/nswrapper"
"github.com/aws/amazon-vpc-cni-k8s/pkg/procsyswrapper"
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
)
const (
// vlan rule priority
vlanRulePriority = 10
// IP rules priority, leaving a 512 gap for the future
toContainerRulePriority = 512
// 1024 is reserved for (IP rule not to <VPC's subnet> table main)
fromContainerRulePriority = 1536
// Main routing table number
mainRouteTable = unix.RT_TABLE_MAIN
)
// NetworkAPIs defines network API calls
type NetworkAPIs interface {
SetupNS(hostVethName string, contVethName string, netnsPath string, addr *net.IPNet, deviceNumber int, vpcCIDRs []string, useExternalSNAT bool, mtu int, log logger.Logger) error
TeardownNS(addr *net.IPNet, deviceNumber int, log logger.Logger) error
SetupPodENINetwork(hostVethName string, contVethName string, netnsPath string, addr *net.IPNet, vlanID int, eniMAC string,
subnetGW string, parentIfIndex int, mtu int, log logger.Logger) error
TeardownPodENINetwork(vlanID int, log logger.Logger) error
}
type linuxNetwork struct {
netLink netlinkwrapper.NetLink
ns nswrapper.NS
procSys procsyswrapper.ProcSys
}
// New creates linuxNetwork object
func New() NetworkAPIs {
return &linuxNetwork{
netLink: netlinkwrapper.NewNetLink(),
ns: nswrapper.NewNS(),
procSys: procsyswrapper.NewProcSys(),
}
}
// createVethPairContext wraps the parameters and the method to create the
// veth pair to attach the container namespace
type createVethPairContext struct {
contVethName string
hostVethName string
addr *net.IPNet
netLink netlinkwrapper.NetLink
ip ipwrapper.IP
mtu int
}
func newCreateVethPairContext(contVethName string, hostVethName string, addr *net.IPNet, mtu int) *createVethPairContext {
return &createVethPairContext{
contVethName: contVethName,
hostVethName: hostVethName,
addr: addr,
netLink: netlinkwrapper.NewNetLink(),
ip: ipwrapper.NewIP(),
mtu: mtu,
}
}
// run defines the closure to execute within the container's namespace to create the veth pair
func (createVethContext *createVethPairContext) run(hostNS ns.NetNS) error {
veth := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: createVethContext.contVethName,
Flags: net.FlagUp,
MTU: createVethContext.mtu,
},
PeerName: createVethContext.hostVethName,
}
if err := createVethContext.netLink.LinkAdd(veth); err != nil {
return err
}
hostVeth, err := createVethContext.netLink.LinkByName(createVethContext.hostVethName)
if err != nil {
return errors.Wrapf(err, "setup NS network: failed to find link %q", createVethContext.hostVethName)
}
// Explicitly set the veth to UP state, because netlink doesn't always do that on all the platforms with net.FlagUp.
// veth won't get a link local address unless it's set to UP state.
if err = createVethContext.netLink.LinkSetUp(hostVeth); err != nil {
return errors.Wrapf(err, "setup NS network: failed to set link %q up", createVethContext.hostVethName)
}
contVeth, err := createVethContext.netLink.LinkByName(createVethContext.contVethName)
if err != nil {
return errors.Wrapf(err, "setup NS network: failed to find link %q", createVethContext.contVethName)
}
// Explicitly set the veth to UP state, because netlink doesn't always do that on all the platforms with net.FlagUp.
// veth won't get a link local address unless it's set to UP state.
if err = createVethContext.netLink.LinkSetUp(contVeth); err != nil {
return errors.Wrapf(err, "setup NS network: failed to set link %q up", createVethContext.contVethName)
}
// Add a connected route to a dummy next hop (169.254.1.1)
// # ip route show
// default via 169.254.1.1 dev eth0
// 169.254.1.1 dev eth0
gw := net.IPv4(169, 254, 1, 1)
gwNet := &net.IPNet{IP: gw, Mask: net.CIDRMask(32, 32)}
if err = createVethContext.netLink.RouteReplace(&netlink.Route{
LinkIndex: contVeth.Attrs().Index,
Scope: netlink.SCOPE_LINK,
Dst: gwNet}); err != nil {
return errors.Wrap(err, "setup NS network: failed to add default gateway")
}
// Add a default route via dummy next hop(169.254.1.1). Then all outgoing traffic will be routed by this
// default route via dummy next hop (169.254.1.1).
if err = createVethContext.ip.AddDefaultRoute(gwNet.IP, contVeth); err != nil {
return errors.Wrap(err, "setup NS network: failed to add default route")
}
if err = createVethContext.netLink.AddrAdd(contVeth, &netlink.Addr{IPNet: createVethContext.addr}); err != nil {
return errors.Wrapf(err, "setup NS network: failed to add IP addr to %q", createVethContext.contVethName)
}
// add static ARP entry for default gateway
// we are using routed mode on the host and container need this static ARP entry to resolve its default gateway.
neigh := &netlink.Neigh{
LinkIndex: contVeth.Attrs().Index,
State: netlink.NUD_PERMANENT,
IP: gwNet.IP,
HardwareAddr: hostVeth.Attrs().HardwareAddr,
}
if err = createVethContext.netLink.NeighAdd(neigh); err != nil {
return errors.Wrap(err, "setup NS network: failed to add static ARP")
}
// Now that the everything has been successfully set up in the container, move the "host" end of the
// veth into the host namespace.
if err = createVethContext.netLink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil {
return errors.Wrap(err, "setup NS network: failed to move veth to host netns")
}
return nil
}
// SetupNS wires up linux networking for a pod's network
func (os *linuxNetwork) SetupNS(hostVethName string, contVethName string, netnsPath string, addr *net.IPNet, deviceNumber int, vpcCIDRs []string, useExternalSNAT bool, mtu int, log logger.Logger) error {
log.Debugf("SetupNS: hostVethName=%s, contVethName=%s, netnsPath=%s, deviceNumber=%d, mtu=%d", hostVethName, contVethName, netnsPath, deviceNumber, mtu)
return setupNS(hostVethName, contVethName, netnsPath, addr, deviceNumber, vpcCIDRs, useExternalSNAT, os.netLink, os.ns, mtu, log, os.procSys)
}
func setupNS(hostVethName string, contVethName string, netnsPath string, addr *net.IPNet, deviceNumber int, vpcCIDRs []string, useExternalSNAT bool,
netLink netlinkwrapper.NetLink, ns nswrapper.NS, mtu int, log logger.Logger, procSys procsyswrapper.ProcSys) error {
hostVeth, err := setupVeth(hostVethName, contVethName, netnsPath, addr, netLink, ns, mtu, procSys, log)
if err != nil {
return errors.Wrapf(err, "setupNS network: failed to setup veth pair.")
}
log.Debugf("Setup host route outgoing hostVeth, LinkIndex %d", hostVeth.Attrs().Index)
addrHostAddr := &net.IPNet{
IP: addr.IP,
Mask: net.CIDRMask(32, 32)}
// Add host route
route := netlink.Route{
LinkIndex: hostVeth.Attrs().Index,
Scope: netlink.SCOPE_LINK,
Dst: addrHostAddr}
// Add or replace route
if err := netLink.RouteReplace(&route); err != nil {
return errors.Wrapf(err, "setupNS: unable to add or replace route entry for %s", route.Dst.IP.String())
}
log.Debugf("Successfully set host route to be %s/0", route.Dst.IP.String())
err = addContainerRule(netLink, true, addr, mainRouteTable)
if err != nil {
log.Errorf("Failed to add toContainer rule for %s err=%v, ", addr.String(), err)
return errors.Wrap(err, "setupNS network: failed to add toContainer")
}
log.Infof("Added toContainer rule for %s", addr.String())
// add from-pod rule, only need it when it is not primary ENI
if deviceNumber > 0 {
// To be backwards compatible, we will have to keep this off-by one setting
tableNumber := deviceNumber + 1
if useExternalSNAT {
// add rule: 1536: from <podIP> use table <table>
err = addContainerRule(netLink, false, addr, tableNumber)
if err != nil {
log.Errorf("Failed to add fromContainer rule for %s err: %v", addr.String(), err)
return errors.Wrap(err, "add NS network: failed to add fromContainer rule")
}
log.Infof("Added rule priority %d from %s table %d", fromContainerRulePriority, addr.String(), tableNumber)
} else {
// add rule: 1536: list of from <podIP> to <vpcCIDR> use table <table>
for _, cidr := range vpcCIDRs {
podRule := netLink.NewRule()
_, podRule.Dst, _ = net.ParseCIDR(cidr)
podRule.Src = addr
podRule.Table = tableNumber
podRule.Priority = fromContainerRulePriority
err = netLink.RuleAdd(podRule)
if isRuleExistsError(err) {
log.Warnf("Rule already exists [%v]", podRule)
} else {
if err != nil {
log.Errorf("Failed to add pod IP rule [%v]: %v", podRule, err)
return errors.Wrapf(err, "setupNS: failed to add pod rule [%v]", podRule)
}
}
var toDst string
if podRule.Dst != nil {
toDst = podRule.Dst.String()
}
log.Infof("Successfully added pod rule[%v] to %s", podRule, toDst)
}
}
}
return nil
}
// setupVeth sets up veth for the pod.
func setupVeth(hostVethName string, contVethName string, netnsPath string, addr *net.IPNet, netLink netlinkwrapper.NetLink,
ns nswrapper.NS, mtu int, procSys procsyswrapper.ProcSys, log logger.Logger) (netlink.Link, error) {
// Clean up if hostVeth exists.
if oldHostVeth, err := netLink.LinkByName(hostVethName); err == nil {
if err = netLink.LinkDel(oldHostVeth); err != nil {
return nil, errors.Wrapf(err, "setupVeth network: failed to delete old hostVeth %q", hostVethName)
}
log.Debugf("Cleaned up old hostVeth: %v\n", hostVethName)
}
createVethContext := newCreateVethPairContext(contVethName, hostVethName, addr, mtu)
if err := ns.WithNetNSPath(netnsPath, createVethContext.run); err != nil {
log.Errorf("Failed to setup veth network %v", err)
return nil, errors.Wrap(err, "setupVeth network: failed to setup veth network")
}
hostVeth, err := netLink.LinkByName(hostVethName)
if err != nil {
return nil, errors.Wrapf(err, "setupVeth network: failed to find link %q", hostVethName)
}
// NB: Must be set after move to host namespace, or kernel will reset to defaults.
if err := procSys.Set(fmt.Sprintf("net/ipv6/conf/%s/accept_ra", hostVethName), "0"); err != nil {
if !os.IsNotExist(err) {
return nil, errors.Wrapf(err, "setupVeth network: failed to disable IPv6 router advertisements")
}
log.Debugf("setupVeth network: Ignoring '%s' writing to accept_ra: Assuming kernel lacks IPv6 support", err)
}
if err := procSys.Set(fmt.Sprintf("net/ipv6/conf/%s/accept_redirects", hostVethName), "0"); err != nil {
if !os.IsNotExist(err) {
return nil, errors.Wrapf(err, "setupVeth network: failed to disable IPv6 ICMP redirects")
}
log.Debugf("setupVeth network: Ignoring '%s' writing to accept_redirects: Assuming kernel lacks IPv6 support", err)
}
log.Debugf("setupVeth network: disabled IPv6 RA and ICMP redirects on %s", hostVethName)
// Explicitly set the veth to UP state, because netlink doesn't always do that on all the platforms with net.FlagUp.
// veth won't get a link local address unless it's set to UP state.
if err = netLink.LinkSetUp(hostVeth); err != nil {
return nil, errors.Wrapf(err, "setupVeth network: failed to set link %q up", hostVethName)
}
return hostVeth, nil
}
// SetupPodENINetwork sets up the network ns for pods requesting its own security group
func (os *linuxNetwork) SetupPodENINetwork(hostVethName string, contVethName string, netnsPath string, addr *net.IPNet,
vlanID int, eniMAC string, subnetGW string, parentIfIndex int, mtu int, log logger.Logger) error {
hostVeth, err := setupVeth(hostVethName, contVethName, netnsPath, addr, os.netLink, os.ns, mtu, os.procSys, log)
if err != nil {
return errors.Wrapf(err, "SetupPodENINetwork failed to setup veth pair.")
}
vlanTableID := vlanID + 100
vlanLink := buildVlanLink(vlanID, parentIfIndex, eniMAC)
// 1. clean up if vlan already exists (necessary when trunk ENI changes).
if oldVlan, err := os.netLink.LinkByName(vlanLink.Name); err == nil {
if err = os.netLink.LinkDel(oldVlan); err != nil {
return errors.Wrapf(err, "SetupPodENINetwork: failed to delete old vlan %s", vlanLink.Name)
}
log.Debugf("Cleaned up old vlan: %s", vlanLink.Name)
}
// 2. add new vlan link
err = os.netLink.LinkAdd(vlanLink)
if err != nil {
return errors.Wrapf(err, "SetupPodENINetwork: failed to add vlan link.")
}
// 3. bring up the vlan
if err = os.netLink.LinkSetUp(vlanLink); err != nil {
return errors.Wrapf(err, "SetupPodENINetwork: failed to set link %q up", vlanLink.Name)
}
// 4. create default routes for vlan
routes := buildRoutesForVlan(vlanTableID, vlanLink.Index, net.ParseIP(subnetGW))
for _, r := range routes {
if err := os.netLink.RouteReplace(&r); err != nil {
return errors.Wrapf(err, "SetupPodENINetwork: unable to replace route entry %s via %s", r.Dst.IP.String(), subnetGW)
}
}
// 5. create route entry for hostveth.
route := netlink.Route{
LinkIndex: hostVeth.Attrs().Index,
Scope: netlink.SCOPE_LINK,
Dst: addr,
Table: vlanTableID,
}
if err := os.netLink.RouteReplace(&route); err != nil {
return errors.Wrapf(err, "SetupPodENINetwork: unable to add or replace route entry for %s", route.Dst.IP.String())
}
log.Debugf("Successfully set host route to be %s/0", route.Dst.IP.String())
// 6. Add ip rules for the pod.
vlanRule := os.netLink.NewRule()
vlanRule.Table = vlanTableID
vlanRule.Priority = vlanRulePriority
vlanRule.IifName = vlanLink.Name
err = os.netLink.RuleAdd(vlanRule)
if err != nil && !isRuleExistsError(err) {
return errors.Wrapf(err, "SetupPodENINetwork: unable to add ip rule for vlan link %s ", vlanLink.Name)
}
vlanRule.IifName = hostVeth.Attrs().Name
err = os.netLink.RuleAdd(vlanRule)
if err != nil && !isRuleExistsError(err) {
return errors.Wrapf(err, "SetupPodENINetwork: unable to add ip rule for host veth %s", hostVethName)
}
return nil
}
// buildRoutesForVlan builds routes required for the vlan link.
func buildRoutesForVlan(vlanTableID int, vlanIndex int, gw net.IP) []netlink.Route {
return []netlink.Route{
// Add a direct link route for the pod vlan link only.
{
LinkIndex: vlanIndex,
Dst: &net.IPNet{IP: gw, Mask: net.CIDRMask(32, 32)},
Scope: netlink.SCOPE_LINK,
Table: vlanTableID,
},
{
LinkIndex: vlanIndex,
Dst: &net.IPNet{IP: net.IPv4zero, Mask: net.CIDRMask(0, 32)},
Scope: netlink.SCOPE_UNIVERSE,
Gw: gw,
Table: vlanTableID,
},
}
}
// buildVlanLink builds vlan link for the pod.
func buildVlanLink(vlanID int, parentIfIndex int, eniMAC string) *netlink.Vlan {
la := netlink.NewLinkAttrs()
la.Name = fmt.Sprintf("vlan.eth.%d", vlanID)
la.ParentIndex = parentIfIndex
la.HardwareAddr, _ = net.ParseMAC(eniMAC)
return &netlink.Vlan{LinkAttrs: la, VlanId: vlanID}
}
func addContainerRule(netLink netlinkwrapper.NetLink, isToContainer bool, addr *net.IPNet, table int) error {
if addr == nil {
return errors.New("can't add container rules without an IP address")
}
containerRule := netLink.NewRule()
if isToContainer {
// Example: 512: from all to 10.200.202.222 lookup main
containerRule.Dst = addr
containerRule.Priority = toContainerRulePriority
} else {
// Example: 1536: from 10.200.202.222 to 10.200.0.0/16 lookup 2
containerRule.Src = addr
containerRule.Priority = fromContainerRulePriority
}
containerRule.Table = table
err := netLink.RuleDel(containerRule)
if err != nil && !containsNoSuchRule(err) {
return errors.Wrapf(err, "addContainerRule: failed to delete old container rule for %s", addr.String())
}
err = netLink.RuleAdd(containerRule)
if err != nil {
return errors.Wrapf(err, "addContainerRule: failed to add container rule for %s", addr.String())
}
return nil
}
// TeardownPodNetwork cleanup ip rules
func (os *linuxNetwork) TeardownNS(addr *net.IPNet, deviceNumber int, log logger.Logger) error {
log.Debugf("TeardownNS: addr %s, deviceNumber %d", addr.String(), deviceNumber)
return tearDownNS(addr, deviceNumber, os.netLink, log)
}
func tearDownNS(addr *net.IPNet, deviceNumber int, netLink netlinkwrapper.NetLink, log logger.Logger) error {
if addr == nil {
return errors.New("can't tear down network namespace with no IP address")
}
// Remove to-pod rule
toContainerRule := netLink.NewRule()
toContainerRule.Dst = addr
toContainerRule.Priority = toContainerRulePriority
err := netLink.RuleDel(toContainerRule)
if err != nil {
log.Errorf("Failed to delete toContainer rule for %s err %v", addr.String(), err)
} else {
log.Infof("Delete toContainer rule for %s ", addr.String())
}
if deviceNumber > 0 {
// remove from-pod rule only for non main table
err := deleteRuleListBySrc(*addr)
if err != nil {
log.Errorf("Failed to delete fromContainer for %s %v", addr.String(), err)
return errors.Wrapf(err, "delete NS network: failed to delete fromContainer rule for %s", addr.String())
}
tableNumber := deviceNumber + 1
log.Infof("Delete fromContainer rule for %s in table %d", addr.String(), tableNumber)
}
addrHostAddr := &net.IPNet{
IP: addr.IP,
Mask: net.CIDRMask(32, 32)}
// cleanup host route:
if err = netLink.RouteDel(&netlink.Route{
Scope: netlink.SCOPE_LINK,
Dst: addrHostAddr}); err != nil {
log.Errorf("delete NS network: failed to delete host route for %s, %v", addr.String(), err)
}
log.Debug("Tear down of NS complete")
return nil
}
// TeardownPodENINetwork tears down the vlan and corresponding ip rules.
func (os *linuxNetwork) TeardownPodENINetwork(vlanID int, log logger.Logger) error {
log.Infof("Tear down of pod ENI namespace")
// 1. delete vlan
if vlan, err := os.netLink.LinkByName(fmt.Sprintf("vlan.eth.%d",
vlanID)); err == nil {
err := os.netLink.LinkDel(vlan)
if err != nil {
return errors.Wrapf(err, "TeardownPodENINetwork: failed to delete vlan link for %d", vlanID)
}
}
// 2. delete two ip rules associated with the vlan
vlanRule := os.netLink.NewRule()
vlanRule.Table = vlanID + 100
vlanRule.Priority = vlanRulePriority
for {
// Loop until both the rules are deleted.
// one of them handles vlan traffic and other is for pod host veth traffic.
if err := os.netLink.RuleDel(vlanRule); err != nil {
if !containsNoSuchRule(err) {
return errors.Wrapf(err, "TeardownPodENINetwork: failed to delete container rule for %d", vlanID)
}
break
}
}
return nil
}
func deleteRuleListBySrc(src net.IPNet) error {
networkClient := networkutils.New()
return networkClient.DeleteRuleListBySrc(src)
}
func containsNoSuchRule(err error) bool {
if errno, ok := err.(syscall.Errno); ok {
return errno == syscall.ENOENT
}
return false
}
func isRuleExistsError(err error) bool {
if errno, ok := err.(syscall.Errno); ok {
return errno == syscall.EEXIST
}
return false
}