-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
netlink.go
546 lines (469 loc) · 15.9 KB
/
netlink.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package loader
import (
"context"
"errors"
"fmt"
"net"
"os"
"strings"
"time"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/inctimer"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/mac"
"github.com/cilium/cilium/pkg/maps/policymap"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/sysctl"
)
type baseDeviceMode string
const (
directMode = baseDeviceMode("direct")
tunnelMode = baseDeviceMode("tunnel")
)
func directionToParent(dir string) uint32 {
switch dir {
case dirIngress:
return netlink.HANDLE_MIN_INGRESS
case dirEgress:
return netlink.HANDLE_MIN_EGRESS
}
return 0
}
func replaceQdisc(link netlink.Link) error {
attrs := netlink.QdiscAttrs{
LinkIndex: link.Attrs().Index,
Handle: netlink.MakeHandle(0xffff, 0),
Parent: netlink.HANDLE_CLSACT,
}
qdisc := &netlink.GenericQdisc{
QdiscAttrs: attrs,
QdiscType: "clsact",
}
return netlink.QdiscReplace(qdisc)
}
type progDefinition struct {
progName string
direction string
}
// replaceDatapath replaces the qdisc and BPF program for an endpoint or XDP program.
//
// When successful, returns a finalizer to allow the map cleanup operation to be
// deferred by the caller. On error, any maps pending migration are immediately
// re-pinned to their original paths and a finalizer is not returned.
//
// When replacing multiple programs from the same ELF in a loop, the finalizer
// should only be run when all the interface's programs have been replaced
// since they might share one or more tail call maps.
//
// For example, this is the case with from-netdev and to-netdev. If eth0:to-netdev
// gets its program and maps replaced and unpinned, its eth0:from-netdev counterpart
// will miss tail calls (and drop packets) until it has been replaced as well.
func replaceDatapath(ctx context.Context, ifName, objPath string, progs []progDefinition, xdpMode string) (_ func(), err error) {
// Avoid unnecessarily loading a prog.
if err := ctx.Err(); err != nil {
return nil, err
}
link, err := netlink.LinkByName(ifName)
if err != nil {
return nil, fmt.Errorf("getting interface %s by name: %w", ifName, err)
}
l := log.WithField("device", ifName).WithField("objPath", objPath).
WithField("ifindex", link.Attrs().Index)
// Load the ELF from disk.
l.Debug("Loading CollectionSpec from ELF")
spec, err := bpf.LoadCollectionSpec(objPath)
if err != nil {
return nil, fmt.Errorf("loading eBPF ELF: %w", err)
}
revert := func() {
// Program replacement unsuccessful, revert bpffs migration.
l.Debug("Reverting bpffs map migration")
if err := bpf.FinalizeBPFFSMigration(bpf.TCGlobalsPath(), spec, true); err != nil {
l.WithError(err).Error("Failed to revert bpffs map migration")
}
}
for _, prog := range progs {
if spec.Programs[prog.progName] == nil {
return nil, fmt.Errorf("no program %s found in eBPF ELF", prog.progName)
}
}
// Unconditionally repin cilium_calls_* maps to prevent them from being
// repopulated by the loader.
for key, ms := range spec.Maps {
if !strings.HasPrefix(ms.Name, "cilium_calls_") {
continue
}
if err := bpf.RepinMap(bpf.TCGlobalsPath(), key, ms); err != nil {
return nil, fmt.Errorf("repinning map %s: %w", key, err)
}
defer func() {
revert := false
// This captures named return variable err.
if err != nil {
revert = true
}
if err := bpf.FinalizeMap(bpf.TCGlobalsPath(), key, revert); err != nil {
l.WithError(err).Error("Could not finalize map")
}
}()
// Only one cilium_calls_* per collection, we can stop here.
break
}
// Inserting a program into these maps will immediately cause other BPF
// programs to call into it, even if other maps like cilium_calls haven't been
// fully populated for the current ELF. Save their contents and avoid sending
// them to the ELF loader.
var policyProgs, egressPolicyProgs []ebpf.MapKV
if pm, ok := spec.Maps[policymap.PolicyCallMapName]; ok {
policyProgs = append(policyProgs, pm.Contents...)
pm.Contents = nil
}
if pm, ok := spec.Maps[policymap.PolicyEgressCallMapName]; ok {
egressPolicyProgs = append(egressPolicyProgs, pm.Contents...)
pm.Contents = nil
}
// Load the CollectionSpec into the kernel, picking up any pinned maps from
// bpffs in the process.
finalize := func() {}
opts := ebpf.CollectionOptions{
Maps: ebpf.MapOptions{PinPath: bpf.TCGlobalsPath()},
}
l.Debug("Loading Collection into kernel")
coll, err := bpf.LoadCollection(spec, opts)
if errors.Is(err, ebpf.ErrMapIncompatible) {
// Temporarily rename bpffs pins of maps whose definitions have changed in
// a new version of a datapath ELF.
l.Debug("Starting bpffs map migration")
if err := bpf.StartBPFFSMigration(bpf.TCGlobalsPath(), spec); err != nil {
return nil, fmt.Errorf("Failed to start bpffs map migration: %w", err)
}
finalize = func() {
l.Debug("Finalizing bpffs map migration")
if err := bpf.FinalizeBPFFSMigration(bpf.TCGlobalsPath(), spec, false); err != nil {
l.WithError(err).Error("Could not finalize bpffs map migration")
}
}
// Retry loading the Collection after starting map migration.
l.Debug("Retrying loading Collection into kernel after map migration")
coll, err = bpf.LoadCollection(spec, opts)
}
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
if _, err := fmt.Fprintf(os.Stderr, "Verifier error: %s\nVerifier log: %+v\n", err, ve); err != nil {
return nil, fmt.Errorf("writing verifier log to stderr: %w", err)
}
}
if err != nil {
return nil, fmt.Errorf("loading eBPF collection into the kernel: %w", err)
}
defer coll.Close()
// If an ELF contains one of the policy call maps, resolve and insert the
// programs it refers to into the map. This always needs to happen _before_
// attaching the ELF's entrypoint(s), but after the ELF's internal tail call
// map (cilium_calls) has been populated, as doing so means the ELF's programs
// become reachable through its policy programs, which hold references to the
// endpoint's cilium_calls. Therefore, inserting policy programs is considered
// an 'attachment', just not through the typical bpf hooks.
//
// For example, a packet can enter to-container, jump into the bpf_host policy
// program, which then jumps into the endpoint's policy program that are
// installed by the loops below. If we allow packets to enter the endpoint's
// bpf programs through its tc hook(s), _all_ this plumbing needs to be done
// first, or we risk missing tail calls.
if len(policyProgs) != 0 {
if err := resolveAndInsertCalls(coll, policymap.PolicyCallMapName, policyProgs); err != nil {
revert()
return nil, fmt.Errorf("inserting policy programs: %w", err)
}
}
if len(egressPolicyProgs) != 0 {
if err := resolveAndInsertCalls(coll, policymap.PolicyEgressCallMapName, egressPolicyProgs); err != nil {
revert()
return nil, fmt.Errorf("inserting egress policy programs: %w", err)
}
}
// Finally, attach the endpoint's tc or xdp entry points to allow traffic to
// flow in.
for _, prog := range progs {
scopedLog := l.WithField("progName", prog.progName).WithField("direction", prog.direction)
scopedLog.Debug("Attaching program to interface")
if err := attachProgram(link, coll.Programs[prog.progName], prog.progName, directionToParent(prog.direction), xdpConfigModeToFlag(xdpMode)); err != nil {
revert()
return nil, fmt.Errorf("program %s: %w", prog.progName, err)
}
scopedLog.Debug("Successfully attached program to interface")
}
return finalize, nil
}
// resolveAndInsertCalls resolves a given slice of ebpf.MapKV containing u32 keys
// and string values (typical for a prog array) to the Programs they point to in
// the Collection. The Programs are then inserted into the Map with the given
// mapName contained within the Collection.
func resolveAndInsertCalls(coll *ebpf.Collection, mapName string, calls []ebpf.MapKV) error {
m, ok := coll.Maps[mapName]
if !ok {
return fmt.Errorf("call map %s not found in Collection", mapName)
}
for _, v := range calls {
name := v.Value.(string)
slot := v.Key.(uint32)
p, ok := coll.Programs[name]
if !ok {
return fmt.Errorf("program %s not found in Collection", name)
}
if err := m.Update(slot, p, ebpf.UpdateAny); err != nil {
return fmt.Errorf("inserting program %s into slot %d", name, slot)
}
log.Debugf("Inserted program %s into %s slot %d", name, mapName, slot)
}
return nil
}
// attachProgram attaches prog to link.
// If xdpFlags is non-zero, attaches prog to XDP.
func attachProgram(link netlink.Link, prog *ebpf.Program, progName string, qdiscParent uint32, xdpFlags link.XDPAttachFlags) error {
if prog == nil {
return errors.New("cannot attach a nil program")
}
if xdpFlags != 0 {
// Omitting XDP_FLAGS_UPDATE_IF_NOEXIST equals running 'ip' with -force,
// and will clobber any existing XDP attachment to the interface.
if err := netlink.LinkSetXdpFdWithFlags(link, prog.FD(), int(xdpFlags)); err != nil {
return fmt.Errorf("attaching XDP program to interface %s: %w", link.Attrs().Name, err)
}
return nil
}
if err := replaceQdisc(link); err != nil {
return fmt.Errorf("replacing clsact qdisc for interface %s: %w", link.Attrs().Name, err)
}
filter := &netlink.BpfFilter{
FilterAttrs: netlink.FilterAttrs{
LinkIndex: link.Attrs().Index,
Parent: qdiscParent,
Handle: 1,
Protocol: unix.ETH_P_ALL,
Priority: option.Config.TCFilterPriority,
},
Fd: prog.FD(),
Name: fmt.Sprintf("%s-%s", progName, link.Attrs().Name),
DirectAction: true,
}
if err := netlink.FilterReplace(filter); err != nil {
return fmt.Errorf("replacing tc filter: %w", err)
}
return nil
}
// RemoveTCFilters removes all tc filters from the given interface.
// Direction is passed as netlink.HANDLE_MIN_{INGRESS,EGRESS} via tcDir.
func RemoveTCFilters(ifName string, tcDir uint32) error {
link, err := netlink.LinkByName(ifName)
if err != nil {
return err
}
filters, err := netlink.FilterList(link, tcDir)
if err != nil {
return err
}
for _, f := range filters {
if err := netlink.FilterDel(f); err != nil {
return err
}
}
return nil
}
func setupDev(link netlink.Link) error {
ifName := link.Attrs().Name
if err := netlink.LinkSetUp(link); err != nil {
log.WithError(err).WithField("device", ifName).Warn("Could not set up the link")
return err
}
sysSettings := make([]sysctl.Setting, 0, 5)
if option.Config.EnableIPv6 {
sysSettings = append(sysSettings, sysctl.Setting{
Name: fmt.Sprintf("net.ipv6.conf.%s.forwarding", ifName), Val: "1", IgnoreErr: false})
}
if option.Config.EnableIPv4 {
sysSettings = append(sysSettings, []sysctl.Setting{
{Name: fmt.Sprintf("net.ipv4.conf.%s.forwarding", ifName), Val: "1", IgnoreErr: false},
{Name: fmt.Sprintf("net.ipv4.conf.%s.rp_filter", ifName), Val: "0", IgnoreErr: false},
{Name: fmt.Sprintf("net.ipv4.conf.%s.accept_local", ifName), Val: "1", IgnoreErr: false},
{Name: fmt.Sprintf("net.ipv4.conf.%s.send_redirects", ifName), Val: "0", IgnoreErr: false},
}...)
}
if err := sysctl.ApplySettings(sysSettings); err != nil {
return err
}
return nil
}
func setupVethPair(name, peerName string) error {
// Create the veth pair if it doesn't exist.
if _, err := netlink.LinkByName(name); err != nil {
hostMac, err := mac.GenerateRandMAC()
if err != nil {
return err
}
peerMac, err := mac.GenerateRandMAC()
if err != nil {
return err
}
veth := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: name,
HardwareAddr: net.HardwareAddr(hostMac),
TxQLen: 1000,
},
PeerName: peerName,
PeerHardwareAddr: net.HardwareAddr(peerMac),
}
if err := netlink.LinkAdd(veth); err != nil {
return err
}
}
veth, err := netlink.LinkByName(name)
if err != nil {
return err
}
if err := setupDev(veth); err != nil {
return err
}
peer, err := netlink.LinkByName(peerName)
if err != nil {
return err
}
if err := setupDev(peer); err != nil {
return err
}
return nil
}
// SetupBaseDevice decides which and what kind of interfaces should be set up as
// the first step of datapath initialization, then performs the setup (and
// creation, if needed) of those interfaces. It returns two links and an error.
// By default, it sets up the veth pair - cilium_host and cilium_net.
func SetupBaseDevice(mtu int) (netlink.Link, netlink.Link, error) {
if err := setupVethPair(defaults.HostDevice, defaults.SecondHostDevice); err != nil {
return nil, nil, err
}
linkHost, err := netlink.LinkByName(defaults.HostDevice)
if err != nil {
return nil, nil, err
}
linkNet, err := netlink.LinkByName(defaults.SecondHostDevice)
if err != nil {
return nil, nil, err
}
if err := netlink.LinkSetARPOff(linkHost); err != nil {
return nil, nil, err
}
if err := netlink.LinkSetARPOff(linkNet); err != nil {
return nil, nil, err
}
if err := netlink.LinkSetMTU(linkHost, mtu); err != nil {
return nil, nil, err
}
if err := netlink.LinkSetMTU(linkNet, mtu); err != nil {
return nil, nil, err
}
return linkHost, linkNet, nil
}
// reloadIPSecOnLinkChanges subscribes to link changes to detect newly added devices
// and reinitializes IPsec on changes. Only in effect for ENI mode in which we expect
// new devices at runtime.
func (l *Loader) reloadIPSecOnLinkChanges() {
// settleDuration is the amount of time to wait for further link updates
// before proceeding with reinitialization. This avoids back-to-back
// reinitialization when multiple link changes are made at once.
const settleDuration = 1 * time.Second
if !option.Config.EnableIPSec || option.Config.IPAM != ipamOption.IPAMENI {
return
}
ctx, cancel := context.WithCancel(context.Background())
updates := make(chan netlink.LinkUpdate)
if err := netlink.LinkSubscribe(updates, ctx.Done()); err != nil {
log.WithError(err).Fatal("Failed to subscribe for link changes")
}
go func() {
defer cancel()
timer, stop := inctimer.New()
defer stop()
// If updates arrive during settle duration a single element
// is sent to this channel and we reinitialize right away
// without waiting for further updates.
trigger := make(chan struct{}, 1)
for {
// Wait for first update or trigger before reinitializing.
getUpdate:
select {
case u, ok := <-updates:
if !ok {
return
}
// Ignore veth devices
if u.Type() == "veth" {
goto getUpdate
}
case <-trigger:
}
log.Info("Reinitializing IPsec due to device changes")
err := l.reinitializeIPSec(ctx)
if err != nil {
// We may fail if links have been removed during the reload. In this case
// the updates channel will have queued updates which will retrigger the
// reinitialization.
log.WithError(err).Warn("Failed to reinitialize IPsec after device change")
}
// Avoid reinitializing repeatedly in short period of time
// by draining further updates for 'settleDuration'.
settled := timer.After(settleDuration)
settleLoop:
for {
select {
case <-settled:
break settleLoop
case u := <-updates:
// Ignore veth devices
if u.Type() == "veth" {
continue
}
// Trigger reinit immediately after
// settle duration has passed.
select {
case trigger <- struct{}{}:
default:
}
}
}
}
}()
}
// addHostDeviceAddr add internal ipv4 and ipv6 addresses to the cilium_host device.
func addHostDeviceAddr(hostDev netlink.Link, ipv4, ipv6 net.IP) error {
if ipv4 != nil {
addr := netlink.Addr{
IPNet: &net.IPNet{
IP: ipv4,
Mask: net.CIDRMask(32, 32), // corresponds to /32
},
}
if err := netlink.AddrReplace(hostDev, &addr); err != nil {
return err
}
}
if ipv6 != nil {
addr := netlink.Addr{
IPNet: &net.IPNet{
IP: ipv6,
Mask: net.CIDRMask(128, 128), // corresponds to /128
},
}
if err := netlink.AddrReplace(hostDev, &addr); err != nil {
return err
}
}
return nil
}