This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpf.go
1189 lines (1015 loc) · 39.2 KB
/
bpf.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 2016-2019 Authors of Cilium
//
// 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 endpoint
import (
"bufio"
"context"
"fmt"
"io"
"os"
"path/filepath"
"syscall"
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/common"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/completion"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/datapath/loader"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/loadinfo"
"github.com/cilium/cilium/pkg/logging/logfields"
bpfconfig "github.com/cilium/cilium/pkg/maps/configmap"
"github.com/cilium/cilium/pkg/maps/ctmap"
"github.com/cilium/cilium/pkg/maps/eppolicymap"
"github.com/cilium/cilium/pkg/maps/lxcmap"
"github.com/cilium/cilium/pkg/maps/policymap"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/policy"
"github.com/cilium/cilium/pkg/policy/trafficdirection"
"github.com/cilium/cilium/pkg/revert"
"github.com/cilium/cilium/pkg/version"
"github.com/sirupsen/logrus"
)
const (
// EndpointGenerationTimeout specifies timeout for proxy completion context
EndpointGenerationTimeout = 330 * time.Second
)
// PolicyMapPathLocked returns the path to the policy map of endpoint.
func (e *Endpoint) PolicyMapPathLocked() string {
return bpf.LocalMapPath(policymap.MapName, e.ID)
}
// CallsMapPathLocked returns the path to cilium tail calls map of an endpoint.
func (e *Endpoint) CallsMapPathLocked() string {
return bpf.LocalMapPath(loader.CallsMapName, e.ID)
}
// BPFConfigMapPath returns the path to the BPF config map of endpoint.
func (e *Endpoint) BPFConfigMapPath() string {
return bpf.LocalMapPath(bpfconfig.MapNamePrefix, e.ID)
}
// BPFIpvlanMapPath returns the path to the ipvlan tail call map of an endpoint.
func (e *Endpoint) BPFIpvlanMapPath() string {
return bpf.LocalMapPath(IpvlanMapName, e.ID)
}
// writeInformationalComments writes annotations to the specified writer,
// including a base64 encoding of the endpoint object, and human-readable
// strings describing the configuration of the datapath.
//
// For configuration of actual datapath behavior, see WriteEndpointConfig().
func (e *Endpoint) writeInformationalComments(w io.Writer) error {
fw := bufio.NewWriter(w)
fmt.Fprint(fw, "/*\n")
epStr64, err := e.base64()
if err == nil {
var verBase64 string
verBase64, err = version.Base64()
if err == nil {
fmt.Fprintf(fw, " * %s%s:%s\n * \n", common.CiliumCHeaderPrefix,
verBase64, epStr64)
}
}
if err != nil {
e.logStatusLocked(BPF, Warning, fmt.Sprintf("Unable to create a base64: %s", err))
}
if e.ContainerID == "" {
fmt.Fprintf(fw, " * Docker Network ID: %s\n", e.DockerNetworkID)
fmt.Fprintf(fw, " * Docker Endpoint ID: %s\n", e.DockerEndpointID)
} else {
fmt.Fprintf(fw, " * Container ID: %s\n", e.ContainerID)
}
fmt.Fprintf(fw, ""+
" * IPv6 address: %s\n"+
" * IPv4 address: %s\n"+
" * Identity: %d\n"+
" * PolicyMap: %s\n"+
" * NodeMAC: %s\n"+
" */\n\n",
e.IPv6.String(), e.IPv4.String(),
e.GetIdentity(), bpf.LocalMapName(policymap.MapName, e.ID),
e.NodeMAC)
fw.WriteString("/*\n")
fw.WriteString(" * Labels:\n")
if e.SecurityIdentity != nil {
if len(e.SecurityIdentity.Labels) == 0 {
fmt.Fprintf(fw, " * - %s\n", "(no labels)")
} else {
for _, v := range e.SecurityIdentity.Labels {
fmt.Fprintf(fw, " * - %s\n", v)
}
}
}
fw.WriteString(" */\n\n")
return fw.Flush()
}
func (e *Endpoint) writeHeaderfile(prefix string) error {
headerPath := filepath.Join(prefix, common.CHeaderFileName)
e.getLogger().WithFields(logrus.Fields{
logfields.Path: headerPath,
}).Debug("writing header file")
f, err := os.Create(headerPath)
if err != nil {
return fmt.Errorf("failed to open file %s for writing: %s", headerPath, err)
}
defer f.Close()
if err = e.writeInformationalComments(f); err != nil {
return err
}
return e.owner.Datapath().WriteEndpointConfig(f, e)
}
// addNewRedirectsFromMap must be called while holding the endpoint lock for
// writing. On success, returns nil; otherwise, returns an error indicating the
// problem that occurred while adding an l7 redirect for the specified policy.
// Must be called with endpoint.Mutex held.
func (e *Endpoint) addNewRedirectsFromMap(m policy.L4PolicyMap, desiredRedirects map[string]bool, proxyWaitGroup *completion.WaitGroup) (error, revert.FinalizeFunc, revert.RevertFunc) {
if option.Config.DryMode {
return nil, nil, nil
}
var finalizeList revert.FinalizeList
var revertStack revert.RevertStack
var updatedStats []*models.ProxyStatistics
insertedDesiredMapState := make(map[policy.Key]struct{})
updatedDesiredMapState := make(policy.MapState)
for _, l4 := range m {
if l4.IsRedirect() {
var redirectPort uint16
var err error
// Only create a redirect if the proxy is NOT running in a sidecar
// container. If running in a sidecar container, just allow traffic
// to the port at L4 by setting the proxy port to 0.
if !e.hasSidecarProxy || l4.L7Parser != policy.ParserTypeHTTP {
var finalizeFunc revert.FinalizeFunc
var revertFunc revert.RevertFunc
redirectPort, err, finalizeFunc, revertFunc = e.owner.UpdateProxyRedirect(e, l4, proxyWaitGroup)
if err != nil {
revertStack.Revert() // Ignore errors while reverting. This is best-effort.
return err, nil, nil
}
finalizeList.Append(finalizeFunc)
revertStack.Push(revertFunc)
proxyID := e.ProxyID(l4)
if e.realizedRedirects == nil {
e.realizedRedirects = make(map[string]uint16)
}
if _, found := e.realizedRedirects[proxyID]; !found {
revertStack.Push(func() error {
delete(e.realizedRedirects, proxyID)
return nil
})
}
e.realizedRedirects[proxyID] = redirectPort
desiredRedirects[proxyID] = true
// Update the endpoint API model to report that Cilium manages a
// redirect for that port.
e.proxyStatisticsMutex.Lock()
proxyStats := e.getProxyStatisticsLocked(proxyID, string(l4.L7Parser), uint16(l4.Port), l4.Ingress)
proxyStats.AllocatedProxyPort = int64(redirectPort)
e.proxyStatisticsMutex.Unlock()
updatedStats = append(updatedStats, proxyStats)
}
// Set the proxy port in the policy map.
var direction trafficdirection.TrafficDirection
if l4.Ingress {
direction = trafficdirection.Ingress
} else {
direction = trafficdirection.Egress
}
keysFromFilter := l4.ToKeys(direction)
for _, keyFromFilter := range keysFromFilter {
if oldEntry, ok := e.desiredPolicy.PolicyMapState[keyFromFilter]; ok {
updatedDesiredMapState[keyFromFilter] = oldEntry
} else {
insertedDesiredMapState[keyFromFilter] = struct{}{}
}
e.desiredPolicy.PolicyMapState[keyFromFilter] = policy.MapStateEntry{ProxyPort: redirectPort}
}
}
}
revertStack.Push(func() error {
// Restore the proxy stats.
e.proxyStatisticsMutex.Lock()
for _, stats := range updatedStats {
stats.AllocatedProxyPort = 0
}
e.proxyStatisticsMutex.Unlock()
// Restore the desired policy map state.
for key := range insertedDesiredMapState {
delete(e.desiredPolicy.PolicyMapState, key)
}
for key, entry := range updatedDesiredMapState {
e.desiredPolicy.PolicyMapState[key] = entry
}
return nil
})
return nil, finalizeList.Finalize, revertStack.Revert
}
// addNewRedirects must be called while holding the endpoint lock for writing.
// On success, returns nil; otherwise, returns an error indicating the problem
// that occurred while adding an l7 redirect for the specified policy.
// The returned map contains the exact set of IDs of proxy redirects that is
// required to implement the given L4 policy.
// Must be called with endpoint.Mutex held.
func (e *Endpoint) addNewRedirects(m *policy.L4Policy, proxyWaitGroup *completion.WaitGroup) (desiredRedirects map[string]bool, err error, finalizeFunc revert.FinalizeFunc, revertFunc revert.RevertFunc) {
desiredRedirects = make(map[string]bool)
var finalizeList revert.FinalizeList
var revertStack revert.RevertStack
var ff revert.FinalizeFunc
var rf revert.RevertFunc
err, ff, rf = e.addNewRedirectsFromMap(m.Ingress, desiredRedirects, proxyWaitGroup)
if err != nil {
return desiredRedirects, fmt.Errorf("unable to allocate ingress redirects: %s", err), nil, nil
}
finalizeList.Append(ff)
revertStack.Push(rf)
err, ff, rf = e.addNewRedirectsFromMap(m.Egress, desiredRedirects, proxyWaitGroup)
if err != nil {
revertStack.Revert() // Ignore errors while reverting. This is best-effort.
return desiredRedirects, fmt.Errorf("unable to allocate egress redirects: %s", err), nil, nil
}
finalizeList.Append(ff)
revertStack.Push(rf)
return desiredRedirects, nil, finalizeList.Finalize, func() error {
e.getLogger().Debug("Reverting proxy redirect additions")
err := revertStack.Revert()
e.getLogger().Debug("Finished reverting proxy redirect additions")
return err
}
}
// Must be called with endpoint.Mutex held.
func (e *Endpoint) removeOldRedirects(desiredRedirects map[string]bool, proxyWaitGroup *completion.WaitGroup) (revert.FinalizeFunc, revert.RevertFunc) {
if option.Config.DryMode {
return nil, nil
}
var finalizeList revert.FinalizeList
var revertStack revert.RevertStack
removedRedirects := make(map[string]uint16, len(e.realizedRedirects))
updatedStats := make(map[uint16]*models.ProxyStatistics, len(e.realizedRedirects))
for id, redirectPort := range e.realizedRedirects {
// Remove only the redirects that are not required.
if desiredRedirects[id] {
continue
}
err, finalizeFunc, revertFunc := e.owner.RemoveProxyRedirect(e, id, proxyWaitGroup)
if err != nil {
e.getLogger().WithError(err).WithField(logfields.L4PolicyID, id).Warn("Error while removing proxy redirect")
continue
}
finalizeList.Append(finalizeFunc)
revertStack.Push(revertFunc)
delete(e.realizedRedirects, id)
removedRedirects[id] = redirectPort
// Update the endpoint API model to report that no redirect is
// active or known for that port anymore. We never delete stats
// until an endpoint is deleted, so we only set the redirect port
// to 0.
e.proxyStatisticsMutex.Lock()
if proxyStats, ok := e.proxyStatistics[id]; ok {
updatedStats[redirectPort] = proxyStats
proxyStats.AllocatedProxyPort = 0
} else {
e.getLogger().WithField(logfields.L4PolicyID, id).Warn("Proxy stats not found")
}
e.proxyStatisticsMutex.Unlock()
}
return finalizeList.Finalize,
func() error {
e.getLogger().Debug("Reverting proxy redirect removals")
// Restore the proxy stats.
e.proxyStatisticsMutex.Lock()
for redirectPort, stats := range updatedStats {
stats.AllocatedProxyPort = int64(redirectPort)
}
e.proxyStatisticsMutex.Unlock()
for id, redirectPort := range removedRedirects {
e.realizedRedirects[id] = redirectPort
}
err := revertStack.Revert()
e.getLogger().Debug("Finished reverting proxy redirect removals")
return err
}
}
// regenerateBPF rewrites all headers and updates all BPF maps to reflect the
// specified endpoint.
// ReloadDatapath forces the datapath programs to be reloaded. It does
// not guarantee recompilation of the programs.
// Must be called with endpoint.Mutex not held and endpoint.BuildMutex held.
// Returns the policy revision number when the regeneration has called, a
// boolean if the BPF compilation was executed and an error in case of an error.
func (e *Endpoint) regenerateBPF(regenContext *regenerationContext) (revnum uint64, compiled bool, reterr error) {
var (
err error
compilationExecuted bool
)
stats := ®enContext.Stats
stats.waitingForLock.Start()
datapathRegenCtxt := regenContext.datapathRegenerationContext
// Make sure that owner is not compiling base programs while we are
// regenerating an endpoint.
e.owner.GetCompilationLock().RLock()
stats.waitingForLock.End(true)
defer e.owner.GetCompilationLock().RUnlock()
datapathRegenCtxt.prepareForProxyUpdates(regenContext.parentContext)
defer datapathRegenCtxt.completionCancel()
err = e.runPreCompilationSteps(regenContext)
// Keep track of the side-effects of the regeneration that need to be
// reverted in case of failure.
// Also keep track of the regeneration finalization code that can't be
// reverted, and execute it in case of regeneration success.
defer func() {
// Ignore finalizing of proxy state in dry mode.
if !option.Config.DryMode {
e.finalizeProxyState(regenContext, reterr)
}
}()
if err != nil {
return 0, compilationExecuted, err
}
// No need to compile BPF in dry mode.
if option.Config.DryMode {
return e.nextPolicyRevision, false, nil
}
// Wait for connection tracking cleaning to complete
stats.waitingForCTClean.Start()
<-datapathRegenCtxt.ctCleaned
stats.waitingForCTClean.End(true)
stats.prepareBuild.End(true)
compilationExecuted, err = e.realizeBPFState(regenContext)
if err != nil {
return datapathRegenCtxt.epInfoCache.revision, compilationExecuted, err
}
// Hook the endpoint into the endpoint and endpoint to policy tables then expose it
stats.mapSync.Start()
epErr := eppolicymap.WriteEndpoint(datapathRegenCtxt.epInfoCache.keys, e.PolicyMap)
err = lxcmap.WriteEndpoint(datapathRegenCtxt.epInfoCache)
stats.mapSync.End(err == nil)
if epErr != nil {
e.logStatusLocked(BPF, Warning, fmt.Sprintf("Unable to sync EpToPolicy Map continue with Sockmap support: %s", epErr))
}
if err != nil {
return 0, compilationExecuted, fmt.Errorf("Exposing new BPF failed: %s", err)
}
// Signal that BPF program has been generated.
// The endpoint has at least L3/L4 connectivity at this point.
e.CloseBPFProgramChannel()
// Allow another builder to start while we wait for the proxy
if regenContext.DoneFunc != nil {
regenContext.DoneFunc()
}
stats.proxyWaitForAck.Start()
err = e.WaitForProxyCompletions(datapathRegenCtxt.proxyWaitGroup)
stats.proxyWaitForAck.End(err == nil)
if err != nil {
return 0, compilationExecuted, fmt.Errorf("Error while configuring proxy redirects: %s", err)
}
stats.waitingForLock.Start()
err = e.LockAlive()
stats.waitingForLock.End(err == nil)
if err != nil {
return 0, compilationExecuted, err
}
defer e.Unlock()
e.ctCleaned = true
// Synchronously try to update PolicyMap for this endpoint. If any
// part of updating the PolicyMap fails, bail out.
// Unfortunately, this means that the map will be in an inconsistent
// state with the current program (if it exists) for this endpoint.
// GH-3897 would fix this by creating a new map to do an atomic swap
// with the old one.
//
// This must be done after allocating the new redirects, to update the
// policy map with the new proxy ports.
stats.mapSync.Start()
err = e.syncPolicyMap()
stats.mapSync.End(err == nil)
if err != nil {
return 0, compilationExecuted, fmt.Errorf("unable to regenerate policy because PolicyMap synchronization failed: %s", err)
}
return datapathRegenCtxt.epInfoCache.revision, compilationExecuted, err
}
func (e *Endpoint) realizeBPFState(regenContext *regenerationContext) (compilationExecuted bool, err error) {
stats := ®enContext.Stats
datapathRegenCtxt := regenContext.datapathRegenerationContext
e.getLogger().WithField(fieldRegenLevel, datapathRegenCtxt.regenerationLevel).Debug("Preparing to compile BPF")
if datapathRegenCtxt.regenerationLevel > regeneration.RegenerateWithoutDatapath {
if e.Options.IsEnabled(option.Debug) {
debugFunc := log.WithFields(logrus.Fields{logfields.EndpointID: e.StringID()}).Debugf
ctx, cancel := context.WithCancel(regenContext.parentContext)
defer cancel()
loadinfo.LogPeriodicSystemLoad(ctx, debugFunc, time.Second)
}
// Compile and install BPF programs for this endpoint
if datapathRegenCtxt.regenerationLevel == regeneration.RegenerateWithDatapathRebuild {
err = loader.CompileAndLoad(datapathRegenCtxt.completionCtx, datapathRegenCtxt.epInfoCache, &stats.datapathRealization)
e.getLogger().WithError(err).Info("Regenerated endpoint BPF program")
compilationExecuted = true
} else if datapathRegenCtxt.regenerationLevel == regeneration.RegenerateWithDatapathRewrite {
err = loader.CompileOrLoad(datapathRegenCtxt.completionCtx, datapathRegenCtxt.epInfoCache, &stats.datapathRealization)
if err == nil {
e.getLogger().Info("Rewrote endpoint BPF program")
} else {
e.getLogger().WithError(err).Error("Error while rewriting endpoint BPF program")
}
compilationExecuted = true
} else { // RegenerateWithDatapathLoad
err = loader.ReloadDatapath(datapathRegenCtxt.completionCtx, datapathRegenCtxt.epInfoCache, &stats.datapathRealization)
if err == nil {
e.getLogger().Info("Reloaded endpoint BPF program")
} else {
e.getLogger().WithError(err).Error("Error while reloading endpoint BPF program")
}
}
if err != nil {
return compilationExecuted, err
}
e.bpfHeaderfileHash = datapathRegenCtxt.bpfHeaderfilesHash
} else {
e.getLogger().WithField(logfields.BPFHeaderfileHash, datapathRegenCtxt.bpfHeaderfilesHash).
Debug("BPF header file unchanged, skipping BPF compilation and installation")
}
return compilationExecuted, nil
}
// runPreCompilationSteps runs all of the regeneration steps that are necessary
// right before compiling the BPF for the given endpoint.
// The endpoint mutex must not be held.
func (e *Endpoint) runPreCompilationSteps(regenContext *regenerationContext) (preCompilationError error) {
stats := ®enContext.Stats
datapathRegenCtxt := regenContext.datapathRegenerationContext
stats.waitingForLock.Start()
err := e.LockAlive()
stats.waitingForLock.End(err == nil)
if err != nil {
return err
}
defer e.Unlock()
currentDir := datapathRegenCtxt.currentDir
nextDir := datapathRegenCtxt.nextDir
// In the first ever regeneration of the endpoint, the conntrack table
// is cleaned from the new endpoint IPs as it is guaranteed that any
// pre-existing connections using that IP are now invalid.
if !e.ctCleaned {
go func() {
if !option.Config.DryMode {
ipv4 := option.Config.EnableIPv4
ipv6 := option.Config.EnableIPv6
created := ctmap.Exists(nil, ipv4, ipv6)
if e.ConntrackLocal() {
created = ctmap.Exists(e, ipv4, ipv6)
}
if created {
e.scrubIPsInConntrackTable()
}
}
close(datapathRegenCtxt.ctCleaned)
}()
} else {
close(datapathRegenCtxt.ctCleaned)
}
// If dry mode is enabled, no further changes to BPF maps are performed
if option.Config.DryMode {
// Compute policy for this endpoint.
if err = e.regeneratePolicy(); err != nil {
return fmt.Errorf("Unable to regenerate policy: %s", err)
}
_ = e.updateAndOverrideEndpointOptions(nil)
// Dry mode needs Network Policy Updates, but the proxy wait group must
// not be initialized, as there is no proxy ACKing the changes.
if err, _ = e.updateNetworkPolicy(nil); err != nil {
return err
}
if err = e.writeHeaderfile(nextDir); err != nil {
return fmt.Errorf("Unable to write header file: %s", err)
}
log.WithField(logfields.EndpointID, e.ID).Debug("Skipping bpf updates due to dry mode")
return nil
}
if e.PolicyMap == nil {
e.PolicyMap, _, err = policymap.OpenOrCreate(e.PolicyMapPathLocked())
if err != nil {
return err
}
// Clean up map contents
e.getLogger().Debug("flushing old PolicyMap")
err = e.PolicyMap.DeleteAll()
if err != nil {
return err
}
// Also reset the in-memory state of the realized state as the
// BPF map content is guaranteed to be empty right now.
e.realizedPolicy.PolicyMapState = make(policy.MapState)
}
if e.bpfConfigMap == nil {
e.bpfConfigMap, _, err = bpfconfig.OpenMapWithName(e.BPFConfigMapPath())
if err != nil {
return err
}
// Also reset the in-memory state of the realized state as the
// BPF map content is guaranteed to be empty right now.
e.realizedBPFConfig = &bpfconfig.EndpointConfig{}
}
// Only generate & populate policy map if a security identity is set up for
// this endpoint.
if e.SecurityIdentity != nil {
stats.policyCalculation.Start()
err = e.regeneratePolicy()
stats.policyCalculation.End(err == nil)
if err != nil {
return fmt.Errorf("unable to regenerate policy for '%s': %s", e.PolicyMap.String(), err)
}
_ = e.updateAndOverrideEndpointOptions(nil)
// Configure the new network policy with the proxies.
// Do this before updating the bpf policy maps, so that the proxy listeners have a chance to be
// ready when new traffic is redirected to them.
stats.proxyPolicyCalculation.Start()
err, networkPolicyRevertFunc := e.updateNetworkPolicy(datapathRegenCtxt.proxyWaitGroup)
stats.proxyPolicyCalculation.End(err == nil)
if err != nil {
return err
}
datapathRegenCtxt.revertStack.Push(networkPolicyRevertFunc)
// Walk the L4Policy to add new redirects and update the desired policy for existing redirects.
// Do this before updating the bpf policy maps, so that the proxies are ready when new traffic
// is redirected to them.
var desiredRedirects map[string]bool
var finalizeFunc revert.FinalizeFunc
var revertFunc revert.RevertFunc
if e.desiredPolicy != nil && e.desiredPolicy.L4Policy != nil && e.desiredPolicy.L4Policy.HasRedirect() {
stats.proxyConfiguration.Start()
desiredRedirects, err, finalizeFunc, revertFunc = e.addNewRedirects(e.desiredPolicy.L4Policy, datapathRegenCtxt.proxyWaitGroup)
stats.proxyConfiguration.End(err == nil)
if err != nil {
return err
}
datapathRegenCtxt.finalizeList.Append(finalizeFunc)
datapathRegenCtxt.revertStack.Push(revertFunc)
}
// realizedBPFConfig may be updated at any point after we figure out
// whether ingress/egress policy is enabled.
e.desiredBPFConfig = bpfconfig.GetConfig(e)
// Synchronously try to update PolicyMap for this endpoint. If any
// part of updating the PolicyMap fails, bail out and do not generate
// BPF. Unfortunately, this means that the map will be in an inconsistent
// state with the current program (if it exists) for this endpoint.
// GH-3897 would fix this by creating a new map to do an atomic swap
// with the old one.
stats.mapSync.Start()
err = e.syncPolicyMap()
stats.mapSync.End(err == nil)
if err != nil {
return fmt.Errorf("unable to regenerate policy because PolicyMap synchronization failed: %s", err)
}
// Synchronously update the BPF ConfigMap for this endpoint.
// This is unlikely to fail, but will have the same
// inconsistency issues as above if there is a failure. Long
// term the solution to this is to templatize this map in the
// ELF file, but there's no solution to this just yet.
if err = e.bpfConfigMap.Update(e.desiredBPFConfig); err != nil {
e.getLogger().WithError(err).Error("unable to update BPF config map")
return err
}
datapathRegenCtxt.revertStack.Push(func() error {
return e.bpfConfigMap.Update(e.realizedBPFConfig)
})
// At this point, traffic is no longer redirected to the proxy for
// now-obsolete redirects, since we synced the updated policy map above.
// It's now safe to remove the redirects from the proxy's configuration.
stats.proxyConfiguration.Start()
finalizeFunc, revertFunc = e.removeOldRedirects(desiredRedirects, datapathRegenCtxt.proxyWaitGroup)
datapathRegenCtxt.finalizeList.Append(finalizeFunc)
datapathRegenCtxt.revertStack.Push(revertFunc)
stats.proxyConfiguration.End(true)
}
stats.prepareBuild.Start()
defer func() {
stats.prepareBuild.End(preCompilationError == nil)
}()
// Avoid BPF program compilation and installation if the headerfile for the endpoint
// or the node have not changed.
var changed bool
datapathRegenCtxt.bpfHeaderfilesHash, err = loader.EndpointHash(e)
if err != nil {
e.getLogger().WithError(err).Warn("Unable to hash header file")
datapathRegenCtxt.bpfHeaderfilesHash = ""
changed = true
} else {
changed = (datapathRegenCtxt.bpfHeaderfilesHash != e.bpfHeaderfileHash)
e.getLogger().WithField(logfields.BPFHeaderfileHash, datapathRegenCtxt.bpfHeaderfilesHash).
Debugf("BPF header file hashed (was: %q)", e.bpfHeaderfileHash)
}
if changed {
datapathRegenCtxt.regenerationLevel = regeneration.RegenerateWithDatapathRewrite
if err = e.writeHeaderfile(nextDir); err != nil {
return fmt.Errorf("unable to write header file: %s", err)
}
}
// Cache endpoint information so that we can release the endpoint lock.
if datapathRegenCtxt.regenerationLevel >= regeneration.RegenerateWithDatapathRewrite {
datapathRegenCtxt.epInfoCache = e.createEpInfoCache(nextDir)
} else {
datapathRegenCtxt.epInfoCache = e.createEpInfoCache(currentDir)
}
if datapathRegenCtxt.epInfoCache == nil {
return fmt.Errorf("Unable to cache endpoint information")
}
return nil
}
func (e *Endpoint) finalizeProxyState(regenContext *regenerationContext, err error) {
datapathRegenCtx := regenContext.datapathRegenerationContext
if err == nil {
// Always execute the finalization code, even if the endpoint is
// terminating, in order to properly release resources.
e.UnconditionalLock()
e.getLogger().Debug("Finalizing successful endpoint regeneration")
datapathRegenCtx.finalizeList.Finalize()
e.Unlock()
} else {
if err := e.LockAlive(); err != nil {
e.getLogger().WithError(err).Debug("Skipping unnecessary reverting of endpoint regeneration changes")
return
}
e.getLogger().Debug("Reverting endpoint changes after BPF regeneration failed")
if err := datapathRegenCtx.revertStack.Revert(); err != nil {
e.getLogger().WithError(err).Error("Reverting endpoint regeneration changes failed")
}
e.getLogger().Debug("Finished reverting endpoint changes after BPF regeneration failed")
e.Unlock()
}
}
// DeleteMapsLocked releases references to all BPF maps associated with this
// endpoint.
//
// For each error that occurs while releasing these references, an error is
// added to the resulting error slice which is returned.
//
// Returns nil on success.
func (e *Endpoint) DeleteMapsLocked() []error {
var errors []error
maps := map[string]string{
"config": e.BPFConfigMapPath(),
"policy": e.PolicyMapPathLocked(),
"calls": e.CallsMapPathLocked(),
"egress": e.BPFIpvlanMapPath(),
}
for name, path := range maps {
if err := os.RemoveAll(path); err != nil {
errors = append(errors, fmt.Errorf("unable to remove %s map file %s: %s", name, path, err))
}
}
if e.ConntrackLocalLocked() {
// Remove local connection tracking maps
for _, m := range ctmap.LocalMaps(e, option.Config.EnableIPv4, option.Config.EnableIPv6) {
ctPath, err := m.Path()
if err == nil {
err = os.RemoveAll(ctPath)
}
if err != nil {
errors = append(errors, fmt.Errorf("unable to remove CT map %s: %s", ctPath, err))
}
}
}
// Remove handle_policy() tail call entry for EP
if err := policymap.RemoveGlobalMapping(uint32(e.ID)); err != nil {
errors = append(errors, fmt.Errorf("unable to remove endpoint from global policy map: %s", err))
}
return errors
}
// DeleteBPFProgramLocked delete the BPF program associated with the endpoint's
// veth interface.
func (e *Endpoint) DeleteBPFProgramLocked() error {
e.getLogger().Debug("deleting bpf program from endpoint")
return loader.DeleteDatapath(context.TODO(), e.IfName, "ingress")
}
// garbageCollectConntrack will run the ctmap.GC() on either the endpoint's
// local conntrack table or the global conntrack table.
//
// The endpoint lock must be held
func (e *Endpoint) garbageCollectConntrack(filter *ctmap.GCFilter) {
var maps []*ctmap.Map
if e.ConntrackLocalLocked() {
maps = ctmap.LocalMaps(e, option.Config.EnableIPv4, option.Config.EnableIPv6)
} else {
maps = ctmap.GlobalMaps(option.Config.EnableIPv4, option.Config.EnableIPv6)
}
for _, m := range maps {
if err := m.Open(); err != nil {
// If the CT table doesn't exist, there's nothing to GC.
scopedLog := log.WithError(err).WithField(logfields.EndpointID, e.ID)
if os.IsNotExist(err) {
scopedLog.WithError(err).Debug("Skipping GC for endpoint")
} else {
scopedLog.WithError(err).Warn("Unable to open map")
}
continue
}
defer m.Close()
ctmap.GC(m, filter)
}
}
func (e *Endpoint) scrubIPsInConntrackTableLocked() {
e.garbageCollectConntrack(&ctmap.GCFilter{
MatchIPs: map[string]struct{}{
e.IPv4.String(): {},
e.IPv6.String(): {},
},
})
}
func (e *Endpoint) scrubIPsInConntrackTable() {
e.UnconditionalLock()
e.scrubIPsInConntrackTableLocked()
e.Unlock()
}
// SkipStateClean can be called on a endpoint before its first build to skip
// the cleaning of state such as the conntrack table. This is useful when an
// endpoint is being restored from state and the datapath state should not be
// claned.
//
// The endpoint lock must NOT be held.
func (e *Endpoint) SkipStateClean() {
// Mark conntrack as already cleaned
e.UnconditionalLock()
e.ctCleaned = true
e.Unlock()
}
// GetBPFKeys returns all keys which should represent this endpoint in the BPF
// endpoints map
func (e *Endpoint) GetBPFKeys() []*lxcmap.EndpointKey {
keys := []*lxcmap.EndpointKey{}
if e.IPv6.IsSet() {
keys = append(keys, lxcmap.NewEndpointKey(e.IPv6.IP()))
}
if e.IPv4.IsSet() {
keys = append(keys, lxcmap.NewEndpointKey(e.IPv4.IP()))
}
return keys
}
// GetBPFValue returns the value which should represent this endpoint in the
// BPF endpoints map
func (e *Endpoint) GetBPFValue() (*lxcmap.EndpointInfo, error) {
mac, err := e.LXCMAC.Uint64()
if err != nil {
return nil, fmt.Errorf("invalid LXC MAC: %v", err)
}
nodeMAC, err := e.NodeMAC.Uint64()
if err != nil {
return nil, fmt.Errorf("invalid node MAC: %v", err)
}
info := &lxcmap.EndpointInfo{
IfIndex: uint32(e.IfIndex),
// Store security identity in network byte order so it can be
// written into the packet without an additional byte order
// conversion.
LxcID: e.ID,
MAC: lxcmap.MAC(mac),
NodeMAC: lxcmap.MAC(nodeMAC),
}
return info, nil
}
func (e *Endpoint) deletePolicyKey(keyToDelete policy.Key, incremental bool) bool {
// Convert from policy.Key to policymap.Key
policymapKey := policymap.PolicyKey{
Identity: keyToDelete.Identity,
DestPort: keyToDelete.DestPort,
Nexthdr: keyToDelete.Nexthdr,
TrafficDirection: keyToDelete.TrafficDirection,
}
// Do not error out if the map entry was already deleted from the bpf map.
// Incremental updates depend on this being OK in cases where identity change
// events overlap with full policy computation.
// In other cases we only delete entries that exist, but even in that case it
// is better to not error out if somebody else has deleted the map entry in the
// meanwhile.
err, errno := e.PolicyMap.DeleteKeyWithErrno(policymapKey)
if err != nil && errno != syscall.ENOENT {
e.getLogger().WithError(err).WithField(logfields.BPFMapKey, policymapKey).Error("Failed to delete PolicyMap key")
return false
}
// Operation was successful, remove from realized state.
delete(e.realizedPolicy.PolicyMapState, keyToDelete)
// Incremental updates need to update the desired state as well.
if incremental && e.desiredPolicy != e.realizedPolicy {
delete(e.desiredPolicy.PolicyMapState, keyToDelete)
}
return true
}
func (e *Endpoint) addPolicyKey(keyToAdd policy.Key, entry policy.MapStateEntry, incremental bool) bool {
// Convert from policy.Key to policymap.Key
policymapKey := policymap.PolicyKey{
Identity: keyToAdd.Identity,
DestPort: keyToAdd.DestPort,
Nexthdr: keyToAdd.Nexthdr,
TrafficDirection: keyToAdd.TrafficDirection,
}
err := e.PolicyMap.AllowKey(policymapKey, entry.ProxyPort)
if err != nil {
e.getLogger().WithError(err).WithFields(logrus.Fields{
logfields.BPFMapKey: policymapKey,
logfields.Port: entry.ProxyPort,
}).Error("Failed to add PolicyMap key")
return false
}
// Operation was successful, add to realized state.
e.realizedPolicy.PolicyMapState[keyToAdd] = entry
// Incremental updates need to update the desired state as well.
if incremental && e.desiredPolicy != e.realizedPolicy {
e.desiredPolicy.PolicyMapState[keyToAdd] = entry
}
return true
}
// applyPolicyMapChanges applies any incremental policy map changes
// collected on the desired policy.
func (e *Endpoint) applyPolicyMapChanges() error {
errors := 0
// Note that after successful endpoint regeneration the
// desired and realized policies are the same pointer. During
// the bpf regeneration possible incremental updates are
// collected on the newly computed desired policy, which is
// not fully realized yet. This is why we get the map changes
// from the desired policy here.
adds, deletes := e.desiredPolicy.PolicyMapChanges.ConsumeMapChanges()
for keyToAdd, entry := range adds {
// Keep the existing proxy port, if any
entry.ProxyPort = e.realizedRedirects[policy.ProxyIDFromKey(e.ID, keyToAdd)]
if !e.addPolicyKey(keyToAdd, entry, true) {
errors++
}
}
for keyToDelete := range deletes {
if !e.deletePolicyKey(keyToDelete, true) {
errors++
}
}
if errors > 0 {
return fmt.Errorf("updating desired PolicyMap state failed")
} else if len(adds)+len(deletes) > 0 {
e.getLogger().WithFields(logrus.Fields{
logfields.AddedPolicyID: adds,
logfields.DeletedPolicyID: deletes,
}).Debug("Applied policy map updates due identity changes")
}