-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
netlink.go
888 lines (771 loc) · 27 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package loader
import (
"context"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
"github.com/cilium/ebpf"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/datapath/tunnel"
"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"
"github.com/cilium/cilium/pkg/time"
)
const qdiscClsact = "clsact"
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: qdiscClsact,
}
return netlink.QdiscReplace(qdisc)
}
type progDefinition struct {
progName string
direction string
}
type replaceDatapathOptions struct {
device string // name of the netlink interface we attach to
elf string // path to object file
programs []progDefinition // programs that we want to attach/replace
xdpMode string // XDP driver mode, only applies when attaching XDP programs
linkDir string // path to bpffs dir holding bpf_links for the device/endpoint
}
// 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, opts replaceDatapathOptions) (_ func(), err error) {
// Avoid unnecessarily loading a prog.
if err := ctx.Err(); err != nil {
return nil, err
}
if opts.linkDir == "" {
return nil, errors.New("opts.linkDir not set in replaceDatapath")
}
link, err := netlink.LinkByName(opts.device)
if err != nil {
return nil, fmt.Errorf("getting interface %s by name: %w", opts.device, err)
}
l := log.WithField("device", opts.device).WithField("objPath", opts.elf).
WithField("ifindex", link.Attrs().Index)
// Load the ELF from disk.
l.Debug("Loading CollectionSpec from ELF")
spec, err := bpf.LoadCollectionSpec(opts.elf)
if err != nil {
return nil, fmt.Errorf("loading eBPF ELF %s: %w", opts.elf, 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 opts.programs {
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() {}
pinPath := bpf.TCGlobalsPath()
collOpts := ebpf.CollectionOptions{
Maps: ebpf.MapOptions{PinPath: pinPath},
}
if err := bpf.MkdirBPF(pinPath); err != nil {
return nil, fmt.Errorf("creating bpffs pin path: %w", err)
}
l.Debug("Loading Collection into kernel")
coll, err := bpf.LoadCollection(spec, collOpts)
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, collOpts)
}
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 opts.programs {
scopedLog := l.WithField("progName", prog.progName).WithField("direction", prog.direction)
if err := bpf.MkdirBPF(opts.linkDir); err != nil {
return nil, fmt.Errorf("creating bpffs link dir for device %s: %w", link.Attrs().Name, err)
}
if opts.xdpMode != "" {
scopedLog.Debug("Attaching XDP program to interface")
err = attachXDPProgram(link, coll.Programs[prog.progName], prog.progName, opts.linkDir, xdpConfigModeToFlag(opts.xdpMode))
} else {
scopedLog.Debug("Attaching TC program to interface")
err = attachTCProgram(link, coll.Programs[prog.progName], prog.progName, opts.linkDir, directionToParent(prog.direction))
}
if 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
}
// attachTCProgram attaches the TC program 'prog' to link.
func attachTCProgram(link netlink.Link, prog *ebpf.Program, progName, bpffsDir string, qdiscParent uint32) error {
if prog == nil {
return errors.New("cannot attach a nil program")
}
// Remove tcx bpf_links created by newer versions of Cilium. They cannot be
// overwritten by netlink-based tc attachments, as tcx is a separate hook
// altogether. Remove the tcx link first to avoid tc programs being run twice
// for every packet. This cannot be done seamlessly and will cause a small
// window of connection interruption.
pin := filepath.Join(bpffsDir, progName)
if err := os.Remove(pin); err == nil {
log.WithField("device", link.Attrs().Name).WithField("pinPath", pin).
Info("Removed tcx link before legacy tc downgrade, possible connectivity interruption")
} else if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("unpinning defunct link %s: %w", pin, err)
}
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 for interface %s: %w", link.Attrs().Name, 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
}
// enableForwarding puts the given link into the up state and enables IP forwarding.
func enableForwarding(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 := enableForwarding(veth); err != nil {
return err
}
peer, err := netlink.LinkByName(peerName)
if err != nil {
return err
}
if err := enableForwarding(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
}
// setupTunnelDevice ensures the cilium_{mode} device is created and
// unused leftover devices are cleaned up in case mode changes.
func setupTunnelDevice(mode tunnel.Protocol, port uint16, mtu int) error {
switch mode {
case tunnel.Geneve:
if err := setupGeneveDevice(port, mtu); err != nil {
return fmt.Errorf("setting up geneve device: %w", err)
}
if err := removeDevice(defaults.VxlanDevice); err != nil {
return fmt.Errorf("removing %s: %w", defaults.VxlanDevice, err)
}
case tunnel.VXLAN:
if err := setupVxlanDevice(port, mtu); err != nil {
return fmt.Errorf("setting up vxlan device: %w", err)
}
if err := removeDevice(defaults.GeneveDevice); err != nil {
return fmt.Errorf("removing %s: %w", defaults.GeneveDevice, err)
}
default:
if err := removeDevice(defaults.VxlanDevice); err != nil {
return fmt.Errorf("removing %s: %w", defaults.VxlanDevice, err)
}
if err := removeDevice(defaults.GeneveDevice); err != nil {
return fmt.Errorf("removing %s: %w", defaults.GeneveDevice, err)
}
}
return nil
}
// setupGeneveDevice ensures the cilium_geneve device is created with the given
// destination port and mtu.
//
// Changing the destination port will recreate the device. Changing the MTU will
// modify the device without recreating it.
func setupGeneveDevice(dport uint16, mtu int) error {
mac, err := mac.GenerateRandMAC()
if err != nil {
return err
}
dev := &netlink.Geneve{
LinkAttrs: netlink.LinkAttrs{
Name: defaults.GeneveDevice,
MTU: mtu,
HardwareAddr: net.HardwareAddr(mac),
},
FlowBased: true,
Dport: dport,
}
l, err := ensureDevice(dev)
if err != nil {
return fmt.Errorf("creating geneve device: %w", err)
}
// Recreate the device with the correct destination port. Modifying the device
// without recreating it is not supported.
geneve, _ := l.(*netlink.Geneve)
if geneve.Dport != dport {
if err := netlink.LinkDel(l); err != nil {
return fmt.Errorf("deleting outdated geneve device: %w", err)
}
if _, err := ensureDevice(dev); err != nil {
return fmt.Errorf("recreating geneve device %s: %w", defaults.GeneveDevice, err)
}
}
return nil
}
// setupVxlanDevice ensures the cilium_vxlan device is created with the given
// port and mtu.
//
// Changing the port will recreate the device. Changing the MTU will modify the
// device without recreating it.
func setupVxlanDevice(port uint16, mtu int) error {
mac, err := mac.GenerateRandMAC()
if err != nil {
return err
}
dev := &netlink.Vxlan{
LinkAttrs: netlink.LinkAttrs{
Name: defaults.VxlanDevice,
MTU: mtu,
HardwareAddr: net.HardwareAddr(mac),
},
FlowBased: true,
Port: int(port),
}
l, err := ensureDevice(dev)
if err != nil {
return fmt.Errorf("creating vxlan device: %w", err)
}
// Recreate the device with the correct destination port. Modifying the device
// without recreating it is not supported.
vxlan, _ := l.(*netlink.Vxlan)
if vxlan.Port != int(port) {
if err := netlink.LinkDel(l); err != nil {
return fmt.Errorf("deleting outdated vxlan device: %w", err)
}
if _, err := ensureDevice(dev); err != nil {
return fmt.Errorf("recreating vxlan device %s: %w", defaults.VxlanDevice, err)
}
}
return nil
}
// setupIPIPDevices ensures the specified v4 and/or v6 devices are created and
// configured with their respective sysctls.
//
// Calling this function may result in tunl0 (v4) or ip6tnl0 (v6) fallback
// interfaces being created as a result of loading the ipip and ip6_tunnel
// kernel modules by creating cilium_ tunnel interfaces. These are catch-all
// interfaces for the ipip decapsulation stack. By default, these interfaces
// will be created in new network namespaces, but Cilium disables this behaviour
// by setting net.core.fb_tunnels_only_for_init_net = 2.
//
// In versions of Cilium prior to 1.15, the behaviour was as follows:
// - Repurpose the default tunl0 by setting it into collect_md mode and renaming
// it to cilium_ipip4. Use the interface for production traffic.
// - The same cannot be done for ip6tunl0, as collect_md cannot be enabled on
// this interface. Leave it unused.
// - Rename sit0 to cilium_sit, if present. This was potentially a mistake,
// as the sit module is not involved with ip6tnl interfaces.
//
// As of Cilium 1.15, if present, tunl0 is renamed to cilium_tunl and ip6tnl0 is
// renamed to cilium_ip6tnl. This is to communicate to the user that Cilium has
// taken control of the encapsulation stack on the node, as it currently doesn't
// explicitly support sharing it with other tools/CNIs. Fallback devices are left
// unused for production traffic. Only devices that were explicitly created are used.
func setupIPIPDevices(ipv4, ipv6 bool) error {
// FlowBased sets IFLA_IPTUN_COLLECT_METADATA, the equivalent of 'ip link add
// ... type ipip/ip6tnl external'. This is needed so bpf programs can use
// bpf_skb_[gs]et_tunnel_key() on packets flowing through tunnels.
if ipv4 {
// Set up IPv4 tunnel device if requested.
if _, err := ensureDevice(&netlink.Iptun{
LinkAttrs: netlink.LinkAttrs{Name: defaults.IPIPv4Device},
FlowBased: true,
}); err != nil {
return fmt.Errorf("creating %s: %w", defaults.IPIPv4Device, err)
}
// Rename fallback device created by potential kernel module load after
// creating tunnel interface.
if err := renameDevice("tunl0", "cilium_tunl"); err != nil {
return fmt.Errorf("renaming fallback device %s: %w", "tunl0", err)
}
} else {
if err := removeDevice(defaults.IPIPv4Device); err != nil {
return fmt.Errorf("removing %s: %w", defaults.IPIPv4Device, err)
}
}
if ipv6 {
// Set up IPv6 tunnel device if requested.
if _, err := ensureDevice(&netlink.Ip6tnl{
LinkAttrs: netlink.LinkAttrs{Name: defaults.IPIPv6Device},
FlowBased: true,
}); err != nil {
return fmt.Errorf("creating %s: %w", defaults.IPIPv6Device, err)
}
// Rename fallback device created by potential kernel module load after
// creating tunnel interface.
if err := renameDevice("ip6tnl0", "cilium_ip6tnl"); err != nil {
return fmt.Errorf("renaming fallback device %s: %w", "tunl0", err)
}
} else {
if err := removeDevice(defaults.IPIPv6Device); err != nil {
return fmt.Errorf("removing %s: %w", defaults.IPIPv6Device, err)
}
}
return nil
}
// ensureDevice ensures a device with the given attrs is present on the system.
// If a device with the given name already exists, device creation is skipped and
// the existing device will be used as-is for the subsequent configuration steps.
// The device is never recreated.
//
// The device's state is set to 'up', L3 forwarding sysctls are applied, and MTU
// is set.
func ensureDevice(attrs netlink.Link) (netlink.Link, error) {
name := attrs.Attrs().Name
// Reuse existing tunnel interface created by previous runs.
l, err := netlink.LinkByName(name)
if err != nil {
if err := netlink.LinkAdd(attrs); err != nil {
if errors.Is(err, unix.ENOTSUP) {
err = fmt.Errorf("%w, maybe kernel module for %s is not available?", err, attrs.Type())
}
return nil, fmt.Errorf("creating device %s: %w", name, err)
}
// Fetch the link we've just created.
l, err = netlink.LinkByName(name)
if err != nil {
return nil, fmt.Errorf("retrieving created device %s: %w", name, err)
}
}
if err := enableForwarding(l); err != nil {
return nil, fmt.Errorf("setting up device %s: %w", name, err)
}
// Update MTU on the link if necessary.
wantMTU, gotMTU := attrs.Attrs().MTU, l.Attrs().MTU
if wantMTU != 0 && wantMTU != gotMTU {
if err := netlink.LinkSetMTU(l, wantMTU); err != nil {
return nil, fmt.Errorf("setting MTU on %s: %w", name, err)
}
}
return l, nil
}
// removeDevice removes the device with the given name. Returns error if the
// device exists but was unable to be removed.
func removeDevice(name string) error {
link, err := netlink.LinkByName(name)
if err != nil {
return nil
}
if err := netlink.LinkDel(link); err != nil {
return fmt.Errorf("removing device %s: %w", name, err)
}
return nil
}
// renameDevice renames a network device from and to a given value. Returns nil
// if the device does not exist.
func renameDevice(from, to string) error {
link, err := netlink.LinkByName(from)
if err != nil {
return nil
}
if err := netlink.LinkSetName(link, to); err != nil {
return fmt.Errorf("renaming device %s to %s: %w", from, to, err)
}
return nil
}
// DeviceHasTCProgramLoaded checks whether a given device has tc filter/qdisc progs attached.
func DeviceHasTCProgramLoaded(hostInterface string, checkEgress bool) (bool, error) {
const bpfProgPrefix = "cil_"
l, err := netlink.LinkByName(hostInterface)
if err != nil {
return false, fmt.Errorf("unable to find endpoint link by name: %w", err)
}
dd, err := netlink.QdiscList(l)
if err != nil {
return false, fmt.Errorf("unable to fetch qdisc list for endpoint: %w", err)
}
var found bool
for _, d := range dd {
if d.Type() == qdiscClsact {
found = true
break
}
}
if !found {
return false, nil
}
ff, err := netlink.FilterList(l, netlink.HANDLE_MIN_INGRESS)
if err != nil {
return false, fmt.Errorf("unable to fetch ingress filter list: %w", err)
}
var filtersCount int
for _, f := range ff {
if filter, ok := f.(*netlink.BpfFilter); ok {
if strings.HasPrefix(filter.Name, bpfProgPrefix) {
filtersCount++
}
}
}
if filtersCount == 0 {
return false, nil
}
if !checkEgress {
return true, nil
}
ff, err = netlink.FilterList(l, netlink.HANDLE_MIN_EGRESS)
if err != nil {
return false, fmt.Errorf("unable to fetch egress filter list: %w", err)
}
filtersCount = 0
for _, f := range ff {
if filter, ok := f.(*netlink.BpfFilter); ok {
if strings.HasPrefix(filter.Name, bpfProgPrefix) {
filtersCount++
}
}
}
return len(ff) > 0 && filtersCount > 0, nil
}