forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_dataplane.go
1261 lines (1128 loc) · 39.5 KB
/
int_dataplane.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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2019 Tigera, Inc. 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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 intdataplane
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/projectcalico/felix/bpf"
"github.com/projectcalico/felix/ifacemonitor"
"github.com/projectcalico/felix/ipsets"
"github.com/projectcalico/felix/iptables"
"github.com/projectcalico/felix/jitter"
"github.com/projectcalico/felix/labelindex"
"github.com/projectcalico/felix/proto"
"github.com/projectcalico/felix/routetable"
"github.com/projectcalico/felix/rules"
"github.com/projectcalico/felix/throttle"
"github.com/projectcalico/libcalico-go/lib/health"
"github.com/projectcalico/libcalico-go/lib/set"
)
const (
// msgPeekLimit is the maximum number of messages we'll try to grab from the to-dataplane
// channel before we apply the changes. Higher values allow us to batch up more work on
// the channel for greater throughput when we're under load (at cost of higher latency).
msgPeekLimit = 100
// Interface name used by kube-proxy to bind service ips.
KubeIPVSInterface = "kube-ipvs0"
)
var (
countDataplaneSyncErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "felix_int_dataplane_failures",
Help: "Number of times dataplane updates failed and will be retried.",
})
countMessages = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "felix_int_dataplane_messages",
Help: "Number dataplane messages by type.",
}, []string{"type"})
summaryApplyTime = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "felix_int_dataplane_apply_time_seconds",
Help: "Time in seconds that it took to apply a dataplane update.",
})
summaryBatchSize = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "felix_int_dataplane_msg_batch_size",
Help: "Number of messages processed in each batch. Higher values indicate we're " +
"doing more batching to try to keep up.",
})
summaryIfaceBatchSize = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "felix_int_dataplane_iface_msg_batch_size",
Help: "Number of interface state messages processed in each batch. Higher " +
"values indicate we're doing more batching to try to keep up.",
})
summaryAddrBatchSize = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "felix_int_dataplane_addr_msg_batch_size",
Help: "Number of interface address messages processed in each batch. Higher " +
"values indicate we're doing more batching to try to keep up.",
})
processStartTime time.Time
)
func init() {
prometheus.MustRegister(countDataplaneSyncErrors)
prometheus.MustRegister(summaryApplyTime)
prometheus.MustRegister(countMessages)
prometheus.MustRegister(summaryBatchSize)
prometheus.MustRegister(summaryIfaceBatchSize)
prometheus.MustRegister(summaryAddrBatchSize)
processStartTime = time.Now()
}
type Config struct {
Hostname string
IPv6Enabled bool
RuleRendererOverride rules.RuleRenderer
IPIPMTU int
VXLANMTU int
IgnoreLooseRPF bool
MaxIPSetSize int
IptablesBackend string
IPSetsRefreshInterval time.Duration
RouteRefreshInterval time.Duration
IptablesRefreshInterval time.Duration
IptablesPostWriteCheckInterval time.Duration
IptablesInsertMode string
IptablesLockFilePath string
IptablesLockTimeout time.Duration
IptablesLockProbeInterval time.Duration
XDPRefreshInterval time.Duration
NetlinkTimeout time.Duration
RulesConfig rules.Config
IfaceMonitorConfig ifacemonitor.Config
StatusReportingInterval time.Duration
ConfigChangedRestartCallback func()
PostInSyncCallback func()
HealthAggregator *health.HealthAggregator
DebugSimulateDataplaneHangAfter time.Duration
ExternalNodesCidrs []string
XDPEnabled bool
XDPAllowGeneric bool
SidecarAccelerationEnabled bool
LookPathOverride func(file string) (string, error)
}
// InternalDataplane implements an in-process Felix dataplane driver based on iptables
// and ipsets. It communicates with the datastore-facing part of Felix via the
// Send/RecvMessage methods, which operate on the protobuf-defined API objects.
//
// Architecture
//
// The internal dataplane driver is organised around a main event loop, which handles
// update events from the datastore and dataplane.
//
// Each pass around the main loop has two phases. In the first phase, updates are fanned
// out to "manager" objects, which calculate the changes that are needed and pass them to
// the dataplane programming layer. In the second phase, the dataplane layer applies the
// updates in a consistent sequence. The second phase is skipped until the datastore is
// in sync; this ensures that the first update to the dataplane applies a consistent
// snapshot.
//
// Having the dataplane layer batch updates has several advantages. It is much more
// efficient to batch updates, since each call to iptables/ipsets has a high fixed cost.
// In addition, it allows for different managers to make updates without having to
// coordinate on their sequencing.
//
// Requirements on the API
//
// The internal dataplane does not do consistency checks on the incoming data (as the
// old Python-based driver used to do). It expects to be told about dependent resources
// before they are needed and for their lifetime to exceed that of the resources that
// depend on them. For example, it is important the the datastore layer send an
// IP set create event before it sends a rule that references that IP set.
type InternalDataplane struct {
toDataplane chan interface{}
fromDataplane chan interface{}
allIptablesTables []*iptables.Table
iptablesMangleTables []*iptables.Table
iptablesNATTables []*iptables.Table
iptablesRawTables []*iptables.Table
iptablesFilterTables []*iptables.Table
ipSets []*ipsets.IPSets
ipipManager *ipipManager
ifaceMonitor *ifacemonitor.InterfaceMonitor
ifaceUpdates chan *ifaceUpdate
ifaceAddrUpdates chan *ifaceAddrsUpdate
endpointStatusCombiner *endpointStatusCombiner
allManagers []Manager
ruleRenderer rules.RuleRenderer
interfacePrefixes []string
routeTables []*routetable.RouteTable
// dataplaneNeedsSync is set if the dataplane is dirty in some way, i.e. we need to
// call apply().
dataplaneNeedsSync bool
// forceIPSetsRefresh is set by the IP sets refresh timer to indicate that we should
// check the IP sets in the dataplane.
forceIPSetsRefresh bool
// forceRouteRefresh is set by the route refresh timer to indicate that we should
// check the routes in the dataplane.
forceRouteRefresh bool
// forceXDPRefresh is set by the XDP refresh timer to indicate that we should
// check the XDP state in the dataplane.
forceXDPRefresh bool
// doneFirstApply is set after we finish the first update to the dataplane. It indicates
// that the dataplane should now be in sync.
doneFirstApply bool
reschedTimer *time.Timer
reschedC <-chan time.Time
applyThrottle *throttle.Throttle
config Config
debugHangC <-chan time.Time
xdpState *xdpState
sockmapState *sockmapState
endpointsSourceV4 endpointsSource
ipsetsSourceV4 ipsetsSource
callbacks *callbacks
}
const (
healthName = "int_dataplane"
healthInterval = 10 * time.Second
)
func NewIntDataplaneDriver(config Config) *InternalDataplane {
log.WithField("config", config).Info("Creating internal dataplane driver.")
ruleRenderer := config.RuleRendererOverride
if ruleRenderer == nil {
ruleRenderer = rules.NewRenderer(config.RulesConfig)
}
epMarkMapper := rules.NewEndpointMarkMapper(
config.RulesConfig.IptablesMarkEndpoint,
config.RulesConfig.IptablesMarkNonCaliEndpoint)
dp := &InternalDataplane{
toDataplane: make(chan interface{}, msgPeekLimit),
fromDataplane: make(chan interface{}, 100),
ruleRenderer: ruleRenderer,
interfacePrefixes: config.RulesConfig.WorkloadIfacePrefixes,
ifaceMonitor: ifacemonitor.New(config.IfaceMonitorConfig),
ifaceUpdates: make(chan *ifaceUpdate, 100),
ifaceAddrUpdates: make(chan *ifaceAddrsUpdate, 100),
config: config,
applyThrottle: throttle.New(10),
}
dp.applyThrottle.Refill() // Allow the first apply() immediately.
dp.ifaceMonitor.Callback = dp.onIfaceStateChange
dp.ifaceMonitor.AddrCallback = dp.onIfaceAddrsChange
// Most iptables tables need the same options.
iptablesOptions := iptables.TableOptions{
HistoricChainPrefixes: rules.AllHistoricChainNamePrefixes,
InsertMode: config.IptablesInsertMode,
RefreshInterval: config.IptablesRefreshInterval,
PostWriteInterval: config.IptablesPostWriteCheckInterval,
LockTimeout: config.IptablesLockTimeout,
LockProbeInterval: config.IptablesLockProbeInterval,
BackendMode: config.IptablesBackend,
LookPathOverride: config.LookPathOverride,
}
// However, the NAT tables need an extra cleanup regex.
iptablesNATOptions := iptablesOptions
iptablesNATOptions.ExtraCleanupRegexPattern = rules.HistoricInsertedNATRuleRegex
featureDetector := iptables.NewFeatureDetector()
iptablesFeatures := featureDetector.GetFeatures()
var iptablesLock sync.Locker
if iptablesFeatures.RestoreSupportsLock {
log.Debug("Calico implementation of iptables lock disabled (because detected version of " +
"iptables-restore will use its own implementation).")
iptablesLock = dummyLock{}
} else if config.IptablesLockTimeout <= 0 {
log.Debug("Calico implementation of iptables lock disabled (by configuration).")
iptablesLock = dummyLock{}
} else {
// Create the shared iptables lock. This allows us to block other processes from
// manipulating iptables while we make our updates. We use a shared lock because we
// actually do multiple updates in parallel (but to different tables), which is safe.
log.WithField("timeout", config.IptablesLockTimeout).Debug(
"Calico implementation of iptables lock enabled")
iptablesLock = iptables.NewSharedLock(
config.IptablesLockFilePath,
config.IptablesLockTimeout,
config.IptablesLockProbeInterval,
)
}
mangleTableV4 := iptables.NewTable(
"mangle",
4,
rules.RuleHashPrefix,
iptablesLock,
featureDetector,
iptablesOptions)
natTableV4 := iptables.NewTable(
"nat",
4,
rules.RuleHashPrefix,
iptablesLock,
featureDetector,
iptablesNATOptions,
)
rawTableV4 := iptables.NewTable(
"raw",
4,
rules.RuleHashPrefix,
iptablesLock,
featureDetector,
iptablesOptions)
filterTableV4 := iptables.NewTable(
"filter",
4,
rules.RuleHashPrefix,
iptablesLock,
featureDetector,
iptablesOptions)
ipSetsConfigV4 := config.RulesConfig.IPSetConfigV4
ipSetsV4 := ipsets.NewIPSets(ipSetsConfigV4)
dp.iptablesNATTables = append(dp.iptablesNATTables, natTableV4)
dp.iptablesRawTables = append(dp.iptablesRawTables, rawTableV4)
dp.iptablesMangleTables = append(dp.iptablesMangleTables, mangleTableV4)
dp.iptablesFilterTables = append(dp.iptablesFilterTables, filterTableV4)
dp.ipSets = append(dp.ipSets, ipSetsV4)
routeTableV4 := routetable.New(config.RulesConfig.WorkloadIfacePrefixes, 4, false, config.NetlinkTimeout)
dp.routeTables = append(dp.routeTables, routeTableV4)
if config.RulesConfig.VXLANEnabled {
routeTableVXLAN := routetable.New([]string{"vxlan.calico"}, 4, true, config.NetlinkTimeout)
dp.routeTables = append(dp.routeTables, routeTableVXLAN)
vxlanManager := newVXLANManager(
ipSetsV4,
config.MaxIPSetSize,
config.Hostname,
routeTableVXLAN,
"vxlan.calico",
config.RulesConfig.VXLANVNI,
config.RulesConfig.VXLANPort,
config.ExternalNodesCidrs,
)
go vxlanManager.KeepVXLANDeviceInSync(config.VXLANMTU)
dp.RegisterManager(vxlanManager)
} else {
// If VXLAN is not enabled, check to see if there is a VXLAN device and delete it if there is.
log.Info("Checking if we need to clean up the VXLAN device")
if link, err := netlink.LinkByName("vxlan.calico"); err != nil && err != syscall.ENODEV {
log.WithError(err).Warnf("Failed to query VXLAN device")
} else if err = netlink.LinkDel(link); err != nil {
log.WithError(err).Error("Failed to delete unwanted VXLAN device")
}
}
dp.endpointStatusCombiner = newEndpointStatusCombiner(dp.fromDataplane, config.IPv6Enabled)
callbacks := newCallbacks()
dp.callbacks = callbacks
if config.XDPEnabled {
if err := bpf.SupportsXDP(); err != nil {
log.WithError(err).Warn("Can't enable XDP acceleration.")
} else {
st, err := NewXDPState(config.XDPAllowGeneric)
if err != nil {
log.WithError(err).Warn("Can't enable XDP acceleration.")
} else {
dp.xdpState = st
dp.xdpState.PopulateCallbacks(callbacks)
log.Info("XDP acceleration enabled.")
}
}
} else {
log.Info("XDP acceleration disabled.")
}
if dp.xdpState == nil {
xdpState, err := NewXDPState(config.XDPAllowGeneric)
if err == nil {
if err := xdpState.WipeXDP(); err != nil {
log.WithError(err).Warn("Failed to cleanup preexisting XDP state")
}
}
// if we can't create an XDP state it means we couldn't get a working
// bpffs so there's nothing to clean up
}
if config.SidecarAccelerationEnabled {
if err := bpf.SupportsSockmap(); err != nil {
log.WithError(err).Warn("Can't enable Sockmap acceleration.")
} else {
st, err := NewSockmapState()
if err != nil {
log.WithError(err).Warn("Can't enable Sockmap acceleration.")
} else {
dp.sockmapState = st
dp.sockmapState.PopulateCallbacks(callbacks)
if err := dp.sockmapState.SetupSockmapAcceleration(); err != nil {
dp.sockmapState = nil
log.WithError(err).Warn("Failed to set up Sockmap acceleration")
} else {
log.Info("Sockmap acceleration enabled.")
}
}
}
}
if dp.sockmapState == nil {
st, err := NewSockmapState()
if err == nil {
st.WipeSockmap(bpf.FindInBPFFSOnly)
}
// if we can't create a sockmap state it means we couldn't get a working
// bpffs so there's nothing to clean up
}
ipsetsManager := newIPSetsManager(ipSetsV4, config.MaxIPSetSize, callbacks)
dp.RegisterManager(ipsetsManager)
dp.ipsetsSourceV4 = ipsetsManager
dp.RegisterManager(newHostIPManager(
config.RulesConfig.WorkloadIfacePrefixes,
rules.IPSetIDThisHostIPs,
ipSetsV4,
config.MaxIPSetSize))
dp.RegisterManager(newPolicyManager(rawTableV4, mangleTableV4, filterTableV4, ruleRenderer, 4, callbacks))
epManager := newEndpointManager(
rawTableV4,
mangleTableV4,
filterTableV4,
ruleRenderer,
routeTableV4,
4,
epMarkMapper,
config.RulesConfig.KubeIPVSSupportEnabled,
config.RulesConfig.WorkloadIfacePrefixes,
dp.endpointStatusCombiner.OnEndpointStatusUpdate,
callbacks)
dp.RegisterManager(epManager)
dp.endpointsSourceV4 = epManager
dp.RegisterManager(newFloatingIPManager(natTableV4, ruleRenderer, 4))
dp.RegisterManager(newMasqManager(ipSetsV4, natTableV4, ruleRenderer, config.MaxIPSetSize, 4))
if config.RulesConfig.IPIPEnabled {
// Add a manger to keep the all-hosts IP set up to date.
dp.ipipManager = newIPIPManager(ipSetsV4, config.MaxIPSetSize, config.ExternalNodesCidrs)
dp.RegisterManager(dp.ipipManager) // IPv4-only
}
if config.IPv6Enabled {
mangleTableV6 := iptables.NewTable(
"mangle",
6,
rules.RuleHashPrefix,
iptablesLock,
featureDetector,
iptablesOptions,
)
natTableV6 := iptables.NewTable(
"nat",
6,
rules.RuleHashPrefix,
iptablesLock,
featureDetector,
iptablesNATOptions,
)
rawTableV6 := iptables.NewTable(
"raw",
6,
rules.RuleHashPrefix,
iptablesLock,
featureDetector,
iptablesOptions,
)
filterTableV6 := iptables.NewTable(
"filter",
6,
rules.RuleHashPrefix,
iptablesLock,
featureDetector,
iptablesOptions,
)
ipSetsConfigV6 := config.RulesConfig.IPSetConfigV6
ipSetsV6 := ipsets.NewIPSets(ipSetsConfigV6)
dp.ipSets = append(dp.ipSets, ipSetsV6)
dp.iptablesNATTables = append(dp.iptablesNATTables, natTableV6)
dp.iptablesRawTables = append(dp.iptablesRawTables, rawTableV6)
dp.iptablesMangleTables = append(dp.iptablesMangleTables, mangleTableV6)
dp.iptablesFilterTables = append(dp.iptablesFilterTables, filterTableV6)
routeTableV6 := routetable.New(config.RulesConfig.WorkloadIfacePrefixes, 6, false, config.NetlinkTimeout)
dp.routeTables = append(dp.routeTables, routeTableV6)
dp.RegisterManager(newIPSetsManager(ipSetsV6, config.MaxIPSetSize, callbacks))
dp.RegisterManager(newHostIPManager(
config.RulesConfig.WorkloadIfacePrefixes,
rules.IPSetIDThisHostIPs,
ipSetsV6,
config.MaxIPSetSize))
dp.RegisterManager(newPolicyManager(rawTableV6, mangleTableV6, filterTableV6, ruleRenderer, 6, callbacks))
dp.RegisterManager(newEndpointManager(
rawTableV6,
mangleTableV6,
filterTableV6,
ruleRenderer,
routeTableV6,
6,
epMarkMapper,
config.RulesConfig.KubeIPVSSupportEnabled,
config.RulesConfig.WorkloadIfacePrefixes,
dp.endpointStatusCombiner.OnEndpointStatusUpdate,
callbacks))
dp.RegisterManager(newFloatingIPManager(natTableV6, ruleRenderer, 6))
dp.RegisterManager(newMasqManager(ipSetsV6, natTableV6, ruleRenderer, config.MaxIPSetSize, 6))
}
for _, t := range dp.iptablesMangleTables {
dp.allIptablesTables = append(dp.allIptablesTables, t)
}
for _, t := range dp.iptablesNATTables {
dp.allIptablesTables = append(dp.allIptablesTables, t)
}
for _, t := range dp.iptablesFilterTables {
dp.allIptablesTables = append(dp.allIptablesTables, t)
}
for _, t := range dp.iptablesRawTables {
dp.allIptablesTables = append(dp.allIptablesTables, t)
}
// Register that we will report liveness and readiness.
if config.HealthAggregator != nil {
log.Info("Registering to report health.")
config.HealthAggregator.RegisterReporter(
healthName,
&health.HealthReport{Live: true, Ready: true},
healthInterval*2,
)
}
if config.DebugSimulateDataplaneHangAfter != 0 {
log.WithField("delay", config.DebugSimulateDataplaneHangAfter).Warn(
"Simulating a dataplane hang.")
dp.debugHangC = time.After(config.DebugSimulateDataplaneHangAfter)
}
return dp
}
type Manager interface {
// OnUpdate is called for each protobuf message from the datastore. May either directly
// send updates to the IPSets and iptables.Table objects (which will queue the updates
// until the main loop instructs them to act) or (for efficiency) may wait until
// a call to CompleteDeferredWork() to flush updates to the dataplane.
OnUpdate(protoBufMsg interface{})
// Called before the main loop flushes updates to the dataplane to allow for batched
// work to be completed.
CompleteDeferredWork() error
}
func (d *InternalDataplane) RegisterManager(mgr Manager) {
d.allManagers = append(d.allManagers, mgr)
}
func (d *InternalDataplane) Start() {
// Do our start-of-day configuration.
d.doStaticDataplaneConfig()
// Then, start the worker threads.
go d.loopUpdatingDataplane()
go d.loopReportingStatus()
go d.ifaceMonitor.MonitorInterfaces()
}
// onIfaceStateChange is our interface monitor callback. It gets called from the monitor's thread.
func (d *InternalDataplane) onIfaceStateChange(ifaceName string, state ifacemonitor.State) {
log.WithFields(log.Fields{
"ifaceName": ifaceName,
"state": state,
}).Info("Linux interface state changed.")
d.ifaceUpdates <- &ifaceUpdate{
Name: ifaceName,
State: state,
}
}
type ifaceUpdate struct {
Name string
State ifacemonitor.State
}
// Check if current felix ipvs config is correct when felix gets an kube-ipvs0 interface update.
// If KubeIPVSInterface is UP and felix ipvs support is disabled (kube-proxy switched from iptables to ipvs mode),
// or if KubeIPVSInterface is DOWN and felix ipvs support is enabled (kube-proxy switched from ipvs to iptables mode),
// restart felix to pick up correct ipvs support mode.
func (d *InternalDataplane) checkIPVSConfigOnStateUpdate(state ifacemonitor.State) {
if (!d.config.RulesConfig.KubeIPVSSupportEnabled && state == ifacemonitor.StateUp) ||
(d.config.RulesConfig.KubeIPVSSupportEnabled && state == ifacemonitor.StateDown) {
log.WithFields(log.Fields{
"ipvsIfaceState": state,
"ipvsSupport": d.config.RulesConfig.KubeIPVSSupportEnabled,
}).Info("kube-proxy mode changed. Restart felix.")
d.config.ConfigChangedRestartCallback()
}
}
// onIfaceAddrsChange is our interface address monitor callback. It gets called
// from the monitor's thread.
func (d *InternalDataplane) onIfaceAddrsChange(ifaceName string, addrs set.Set) {
log.WithFields(log.Fields{
"ifaceName": ifaceName,
"addrs": addrs,
}).Info("Linux interface addrs changed.")
d.ifaceAddrUpdates <- &ifaceAddrsUpdate{
Name: ifaceName,
Addrs: addrs,
}
}
type ifaceAddrsUpdate struct {
Name string
Addrs set.Set
}
func (d *InternalDataplane) SendMessage(msg interface{}) error {
d.toDataplane <- msg
return nil
}
func (d *InternalDataplane) RecvMessage() (interface{}, error) {
return <-d.fromDataplane, nil
}
// doStaticDataplaneConfig sets up the kernel and our static iptables chains. Should be called
// once at start of day before starting the main loop. The actual iptables programming is deferred
// to the main loop.
func (d *InternalDataplane) doStaticDataplaneConfig() {
// Check/configure global kernel parameters.
d.configureKernel()
// Endure that the default value of rp_filter is set to "strict" for newly-created
// interfaces. This is required to prevent a race between starting an interface and
// Felix being able to configure it.
writeProcSys("/proc/sys/net/ipv4/conf/default/rp_filter", "1")
for _, t := range d.iptablesRawTables {
rawChains := d.ruleRenderer.StaticRawTableChains(t.IPVersion)
t.UpdateChains(rawChains)
t.SetRuleInsertions("PREROUTING", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainRawPrerouting},
}})
t.SetRuleInsertions("OUTPUT", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainRawOutput},
}})
}
for _, t := range d.iptablesFilterTables {
filterChains := d.ruleRenderer.StaticFilterTableChains(t.IPVersion)
t.UpdateChains(filterChains)
t.SetRuleInsertions("FORWARD", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainFilterForward},
}})
t.SetRuleInsertions("INPUT", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainFilterInput},
}})
t.SetRuleInsertions("OUTPUT", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainFilterOutput},
}})
}
if d.config.RulesConfig.IPIPEnabled {
log.Info("IPIP enabled, starting thread to keep tunnel configuration in sync.")
go d.ipipManager.KeepIPIPDeviceInSync(
d.config.IPIPMTU,
d.config.RulesConfig.IPIPTunnelAddress,
)
} else {
log.Info("IPIP disabled. Not starting tunnel update thread.")
}
for _, t := range d.iptablesNATTables {
t.UpdateChains(d.ruleRenderer.StaticNATTableChains(t.IPVersion))
t.SetRuleInsertions("PREROUTING", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainNATPrerouting},
}})
t.SetRuleInsertions("POSTROUTING", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainNATPostrouting},
}})
t.SetRuleInsertions("OUTPUT", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainNATOutput},
}})
}
for _, t := range d.iptablesMangleTables {
t.UpdateChains(d.ruleRenderer.StaticMangleTableChains(t.IPVersion))
t.SetRuleInsertions("PREROUTING", []iptables.Rule{{
Action: iptables.JumpAction{Target: rules.ChainManglePrerouting},
}})
}
if d.xdpState != nil {
if err := d.setXDPFailsafePorts(); err != nil {
log.Warnf("failed to set XDP failsafe ports, disabling XDP: %v", err)
d.shutdownXDPCompletely()
}
}
}
func stringToProtocol(protocol string) (labelindex.IPSetPortProtocol, error) {
switch protocol {
case "tcp":
return labelindex.ProtocolTCP, nil
case "udp":
return labelindex.ProtocolUDP, nil
}
return labelindex.ProtocolNone, fmt.Errorf("unknown protocol %q", protocol)
}
func (d *InternalDataplane) setXDPFailsafePorts() error {
inboundPorts := d.config.RulesConfig.FailsafeInboundHostPorts
if _, err := d.xdpState.common.bpfLib.NewFailsafeMap(); err != nil {
return err
}
for _, p := range inboundPorts {
proto, err := stringToProtocol(p.Protocol)
if err != nil {
return err
}
if err := d.xdpState.common.bpfLib.UpdateFailsafeMap(uint8(proto), p.Port); err != nil {
return err
}
}
log.Infof("Set XDP failsafe ports: %+v", inboundPorts)
return nil
}
func (d *InternalDataplane) shutdownXDPCompletely() {
if d.xdpState == nil {
return
}
if d.callbacks != nil {
d.xdpState.DepopulateCallbacks(d.callbacks)
}
success := false
maxTries := 10
for i := 0; i < maxTries; i++ {
err := d.xdpState.WipeXDP()
if err == nil {
success = true
break
}
log.WithError(err).WithField("try", i).Warn("failed to wipe the XDP state")
}
if !success {
log.Panicf("Failed to wipe the XDP state after %d tries", maxTries)
}
d.xdpState = nil
}
func (d *InternalDataplane) loopUpdatingDataplane() {
log.Info("Started internal iptables dataplane driver loop")
healthTicks := time.NewTicker(healthInterval).C
d.reportHealth()
// Retry any failed operations every 10s.
retryTicker := time.NewTicker(10 * time.Second)
// If configured, start tickers to refresh the IP sets and routing table entries.
var ipSetsRefreshC <-chan time.Time
if d.config.IPSetsRefreshInterval > 0 {
log.WithField("interval", d.config.IptablesRefreshInterval).Info(
"Will refresh IP sets on timer")
refreshTicker := jitter.NewTicker(
d.config.IPSetsRefreshInterval,
d.config.IPSetsRefreshInterval/10,
)
ipSetsRefreshC = refreshTicker.C
}
var routeRefreshC <-chan time.Time
if d.config.RouteRefreshInterval > 0 {
log.WithField("interval", d.config.RouteRefreshInterval).Info(
"Will refresh routes on timer")
refreshTicker := jitter.NewTicker(
d.config.RouteRefreshInterval,
d.config.RouteRefreshInterval/10,
)
routeRefreshC = refreshTicker.C
}
var xdpRefreshC <-chan time.Time
if d.config.XDPRefreshInterval > 0 && d.xdpState != nil {
log.WithField("interval", d.config.XDPRefreshInterval).Info(
"Will refresh XDP on timer")
refreshTicker := jitter.NewTicker(
d.config.XDPRefreshInterval,
d.config.XDPRefreshInterval/10,
)
xdpRefreshC = refreshTicker.C
}
// Fill the apply throttle leaky bucket.
throttleC := jitter.NewTicker(100*time.Millisecond, 10*time.Millisecond).C
beingThrottled := false
datastoreInSync := false
processMsgFromCalcGraph := func(msg interface{}) {
log.WithField("msg", proto.MsgStringer{Msg: msg}).Infof(
"Received %T update from calculation graph", msg)
d.recordMsgStat(msg)
for _, mgr := range d.allManagers {
mgr.OnUpdate(msg)
}
switch msg.(type) {
case *proto.InSync:
log.WithField("timeSinceStart", time.Since(processStartTime)).Info(
"Datastore in sync, flushing the dataplane for the first time...")
datastoreInSync = true
}
}
processIfaceUpdate := func(ifaceUpdate *ifaceUpdate) {
log.WithField("msg", ifaceUpdate).Info("Received interface update")
if ifaceUpdate.Name == KubeIPVSInterface {
d.checkIPVSConfigOnStateUpdate(ifaceUpdate.State)
return
}
for _, mgr := range d.allManagers {
mgr.OnUpdate(ifaceUpdate)
}
for _, routeTable := range d.routeTables {
routeTable.OnIfaceStateChanged(ifaceUpdate.Name, ifaceUpdate.State)
}
}
processAddrsUpdate := func(ifaceAddrsUpdate *ifaceAddrsUpdate) {
log.WithField("msg", ifaceAddrsUpdate).Info("Received interface addresses update")
for _, mgr := range d.allManagers {
mgr.OnUpdate(ifaceAddrsUpdate)
}
}
for {
select {
case msg := <-d.toDataplane:
// Process the message we received, then opportunistically process any other
// pending messages.
batchSize := 1
processMsgFromCalcGraph(msg)
msgLoop1:
for i := 0; i < msgPeekLimit; i++ {
select {
case msg := <-d.toDataplane:
processMsgFromCalcGraph(msg)
batchSize++
default:
// Channel blocked so we must be caught up.
break msgLoop1
}
}
d.dataplaneNeedsSync = true
summaryBatchSize.Observe(float64(batchSize))
case ifaceUpdate := <-d.ifaceUpdates:
// Process the message we received, then opportunistically process any other
// pending messages.
batchSize := 1
processIfaceUpdate(ifaceUpdate)
msgLoop2:
for i := 0; i < msgPeekLimit; i++ {
select {
case ifaceUpdate := <-d.ifaceUpdates:
processIfaceUpdate(ifaceUpdate)
batchSize++
default:
// Channel blocked so we must be caught up.
break msgLoop2
}
}
d.dataplaneNeedsSync = true
summaryIfaceBatchSize.Observe(float64(batchSize))
case ifaceAddrsUpdate := <-d.ifaceAddrUpdates:
batchSize := 1
processAddrsUpdate(ifaceAddrsUpdate)
msgLoop3:
for i := 0; i < msgPeekLimit; i++ {
select {
case ifaceAddrsUpdate := <-d.ifaceAddrUpdates:
processAddrsUpdate(ifaceAddrsUpdate)
batchSize++
default:
// Channel blocked so we must be caught up.
break msgLoop3
}
}
summaryAddrBatchSize.Observe(float64(batchSize))
d.dataplaneNeedsSync = true
case <-ipSetsRefreshC:
log.Debug("Refreshing IP sets state")
d.forceIPSetsRefresh = true
d.dataplaneNeedsSync = true
case <-routeRefreshC:
log.Debug("Refreshing routes")
d.forceRouteRefresh = true
d.dataplaneNeedsSync = true
case <-xdpRefreshC:
log.Debug("Refreshing XDP")
d.forceXDPRefresh = true
d.dataplaneNeedsSync = true
case <-d.reschedC:
log.Debug("Reschedule kick received")
d.dataplaneNeedsSync = true
// nil out the channel to record that the timer is now inactive.
d.reschedC = nil
case <-throttleC:
d.applyThrottle.Refill()
case <-healthTicks:
d.reportHealth()
case <-retryTicker.C:
case <-d.debugHangC:
log.Warning("Debug hang simulation timer popped, hanging the dataplane!!")
time.Sleep(1 * time.Hour)
log.Panic("Woke up after 1 hour, something's probably wrong with the test.")
}
if datastoreInSync && d.dataplaneNeedsSync {
// Dataplane is out-of-sync, check if we're throttled.
if d.applyThrottle.Admit() {
if beingThrottled && d.applyThrottle.WouldAdmit() {
log.Info("Dataplane updates no longer throttled")
beingThrottled = false
}
log.Info("Applying dataplane updates")
applyStart := time.Now()
// Actually apply the changes to the dataplane.
d.apply()
// Record stats.
applyTime := time.Since(applyStart)
summaryApplyTime.Observe(applyTime.Seconds())
if d.dataplaneNeedsSync {
// Dataplane is still dirty, record an error.
countDataplaneSyncErrors.Inc()
}
log.WithField("msecToApply", applyTime.Seconds()*1000.0).Info(
"Finished applying updates to dataplane.")
if !d.doneFirstApply {
log.WithField(
"secsSinceStart", time.Since(processStartTime).Seconds(),
).Info("Completed first update to dataplane.")
d.doneFirstApply = true
if d.config.PostInSyncCallback != nil {
d.config.PostInSyncCallback()
}
}
d.reportHealth()
} else {
if !beingThrottled {
log.Info("Dataplane updates throttled")
beingThrottled = true
}
}
}
}
}
func (d *InternalDataplane) configureKernel() {
// For IPv4, we rely on the kernel's reverse path filtering to prevent workloads from
// spoofing their IP addresses.
//
// The RPF check for a particular interface is controlled by several sysctls:
//
// - ipv4.conf.all.rp_filter is a global override
// - ipv4.conf.default.rp_filter controls the value that is set on a newly created
// interface
// - ipv4.conf.<interface>.rp_filter controls a particular interface.
//
// The algorithm for combining the global override and per-interface values is to take the
// *numeric* maximum between the two. The values are: 0=off, 1=strict, 2=loose. "loose"
// is not suitable for Calico since it would allow workloads to spoof packets from other
// workloads on the same host. Hence, we need the global override to be <=1 or it would
// override the per-interface setting to "strict" that we require.
//
// Unless the IgnoreLooseRPF flag is set, we bail out rather than simply setting it
// because setting 2, "loose", is unusual and it is likely to have been set deliberately.