-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtracer.go
1163 lines (1060 loc) · 37.9 KB
/
tracer.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
package ebpf
import (
"errors"
"fmt"
"io/fs"
"os"
"strings"
"time"
"github.com/netobserv/netobserv-ebpf-agent/pkg/ifaces"
"github.com/netobserv/netobserv-ebpf-agent/pkg/metrics"
"github.com/netobserv/netobserv-ebpf-agent/pkg/utils"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
"github.com/cilium/ebpf/ringbuf"
"github.com/cilium/ebpf/rlimit"
"github.com/gavv/monotime"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
"golang.org/x/sys/unix"
kerrors "k8s.io/apimachinery/pkg/util/errors"
)
// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS -target amd64,arm64,ppc64le,s390x -type flow_metrics_t -type flow_id_t -type flow_record_t -type pkt_drops_t -type dns_record_t -type global_counters_key_t -type direction_t -type filter_action_t Bpf ../../bpf/flows.c -- -I../../bpf/headers
const (
qdiscType = "clsact"
// ebpf map names as defined in bpf/maps_definition.h
aggregatedFlowsMap = "aggregated_flows"
dnsLatencyMap = "dns_flows"
// constants defined in flows.c as "volatile const"
constSampling = "sampling"
constTraceMessages = "trace_messages"
constEnableRtt = "enable_rtt"
constEnableDNSTracking = "enable_dns_tracking"
constEnableFlowFiltering = "enable_flows_filtering"
pktDropHook = "kfree_skb"
constPcaEnable = "enable_pca"
pcaRecordsMap = "packet_record"
tcEgressFilterName = "tc/tc_egress_flow_parse"
tcIngressFilterName = "tc/tc_ingress_flow_parse"
)
var log = logrus.WithField("component", "ebpf.FlowFetcher")
var plog = logrus.WithField("component", "ebpf.PacketFetcher")
// FlowFetcher reads and forwards the Flows from the Traffic Control hooks in the eBPF kernel space.
// It provides access both to flows that are aggregated in the kernel space (via PerfCPU hashmap)
// and to flows that are forwarded by the kernel via ringbuffer because could not be aggregated
// in the map
type FlowFetcher struct {
objects *BpfObjects
qdiscs map[ifaces.Interface]*netlink.GenericQdisc
egressFilters map[ifaces.Interface]*netlink.BpfFilter
ingressFilters map[ifaces.Interface]*netlink.BpfFilter
ringbufReader *ringbuf.Reader
cacheMaxSize int
enableIngress bool
enableEgress bool
pktDropsTracePoint link.Link
rttFentryLink link.Link
rttKprobeLink link.Link
egressTCXLink map[ifaces.Interface]link.Link
ingressTCXLink map[ifaces.Interface]link.Link
lookupAndDeleteSupported bool
}
type FlowFetcherConfig struct {
EnableIngress bool
EnableEgress bool
Debug bool
Sampling int
CacheMaxSize int
PktDrops bool
DNSTracker bool
EnableRTT bool
EnableFlowFilter bool
EnablePCA bool
FilterConfig *FilterConfig
}
func NewFlowFetcher(cfg *FlowFetcherConfig) (*FlowFetcher, error) {
if err := rlimit.RemoveMemlock(); err != nil {
log.WithError(err).
Warn("can't remove mem lock. The agent could not be able to start eBPF programs")
}
spec, err := LoadBpf()
if err != nil {
return nil, fmt.Errorf("loading BPF data: %w", err)
}
// Resize maps according to user-provided configuration
spec.Maps[aggregatedFlowsMap].MaxEntries = uint32(cfg.CacheMaxSize)
traceMsgs := 0
if cfg.Debug {
traceMsgs = 1
}
enableRtt := 0
if cfg.EnableRTT {
enableRtt = 1
}
enableDNSTracking := 0
if cfg.DNSTracker {
enableDNSTracking = 1
}
if enableDNSTracking == 0 {
spec.Maps[dnsLatencyMap].MaxEntries = 1
}
enableFlowFiltering := 0
if cfg.EnableFlowFilter {
enableFlowFiltering = 1
}
if err := spec.RewriteConstants(map[string]interface{}{
constSampling: uint32(cfg.Sampling),
constTraceMessages: uint8(traceMsgs),
constEnableRtt: uint8(enableRtt),
constEnableDNSTracking: uint8(enableDNSTracking),
constEnableFlowFiltering: uint8(enableFlowFiltering),
}); err != nil {
return nil, fmt.Errorf("rewriting BPF constants definition: %w", err)
}
oldKernel := utils.IsKernelOlderThan("5.14.0")
if oldKernel {
log.Infof("kernel older than 5.14.0 detected: not all hooks are supported")
}
objects, err := kernelSpecificLoadAndAssign(oldKernel, spec)
if err != nil {
return nil, err
}
if cfg.EnableFlowFilter {
f := NewFilter(&objects, cfg.FilterConfig)
if err := f.ProgramFilter(); err != nil {
return nil, fmt.Errorf("programming flow filter: %w", err)
}
}
log.Debugf("Deleting specs for PCA")
// Deleting specs for PCA
// Always set pcaRecordsMap to the minimum in FlowFetcher - PCA and Flow Fetcher are mutually exclusive.
spec.Maps[pcaRecordsMap].MaxEntries = 1
objects.TcxEgressPcaParse = nil
objects.TcIngressPcaParse = nil
delete(spec.Programs, constPcaEnable)
var pktDropsLink link.Link
if cfg.PktDrops && !oldKernel {
pktDropsLink, err = link.Tracepoint("skb", pktDropHook, objects.KfreeSkb, nil)
if err != nil {
return nil, fmt.Errorf("failed to attach the BPF program to kfree_skb tracepoint: %w", err)
}
}
var rttFentryLink, rttKprobeLink link.Link
if cfg.EnableRTT {
rttFentryLink, err = link.AttachTracing(link.TracingOptions{
Program: objects.BpfPrograms.TcpRcvFentry,
})
if err != nil {
log.Warningf("failed to attach the BPF program to tcpReceiveFentry: %v fallback to use kprobe", err)
// try to use kprobe for older kernels
rttKprobeLink, err = link.Kprobe("tcp_rcv_established", objects.TcpRcvKprobe, nil)
if err != nil {
return nil, fmt.Errorf("failed to attach the BPF program to tcpReceiveKprobe: %w", err)
}
}
}
// read events from igress+egress ringbuffer
flows, err := ringbuf.NewReader(objects.DirectFlows)
if err != nil {
return nil, fmt.Errorf("accessing to ringbuffer: %w", err)
}
return &FlowFetcher{
objects: &objects,
ringbufReader: flows,
egressFilters: map[ifaces.Interface]*netlink.BpfFilter{},
ingressFilters: map[ifaces.Interface]*netlink.BpfFilter{},
qdiscs: map[ifaces.Interface]*netlink.GenericQdisc{},
cacheMaxSize: cfg.CacheMaxSize,
enableIngress: cfg.EnableIngress,
enableEgress: cfg.EnableEgress,
pktDropsTracePoint: pktDropsLink,
rttFentryLink: rttFentryLink,
rttKprobeLink: rttKprobeLink,
egressTCXLink: map[ifaces.Interface]link.Link{},
ingressTCXLink: map[ifaces.Interface]link.Link{},
lookupAndDeleteSupported: true, // this will be turned off later if found to be not supported
}, nil
}
func (m *FlowFetcher) AttachTCX(iface ifaces.Interface) error {
ilog := log.WithField("iface", iface)
if iface.NetNS != netns.None() {
originalNs, err := netns.Get()
if err != nil {
return fmt.Errorf("failed to get current netns: %w", err)
}
defer func() {
if err := netns.Set(originalNs); err != nil {
ilog.WithError(err).Error("failed to set netns back")
}
originalNs.Close()
}()
if err := unix.Setns(int(iface.NetNS), unix.CLONE_NEWNET); err != nil {
return fmt.Errorf("failed to setns to %s: %w", iface.NetNS, err)
}
}
if m.enableEgress {
egrLink, err := link.AttachTCX(link.TCXOptions{
Program: m.objects.BpfPrograms.TcxEgressFlowParse,
Attach: ebpf.AttachTCXEgress,
Interface: iface.Index,
})
if err != nil {
if errors.Is(err, fs.ErrExist) {
// The interface already has a TCX egress hook
log.WithField("iface", iface.Name).Debug("interface already has a TCX egress hook ignore")
} else {
return fmt.Errorf("failed to attach TCX egress: %w", err)
}
}
m.egressTCXLink[iface] = egrLink
ilog.WithField("interface", iface.Name).Debug("successfully attach egressTCX hook")
}
if m.enableIngress {
ingLink, err := link.AttachTCX(link.TCXOptions{
Program: m.objects.BpfPrograms.TcxIngressFlowParse,
Attach: ebpf.AttachTCXIngress,
Interface: iface.Index,
})
if err != nil {
if errors.Is(err, fs.ErrExist) {
// The interface already has a TCX ingress hook
log.WithField("iface", iface.Name).Debug("interface already has a TCX ingress hook ignore")
} else {
return fmt.Errorf("failed to attach TCX ingress: %w", err)
}
}
m.ingressTCXLink[iface] = ingLink
ilog.WithField("interface", iface.Name).Debug("successfully attach ingressTCX hook")
}
return nil
}
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
}
var errs []error
for _, f := range filters {
if err := netlink.FilterDel(f); err != nil {
errs = append(errs, err)
}
}
return kerrors.NewAggregate(errs)
}
func (m *FlowFetcher) removePreviousFilters(iface ifaces.Interface) error {
ilog := log.WithField("iface", iface)
ilog.Debugf("looking for previously installed TC filters on %s", iface.Name)
links, err := netlink.LinkList()
if err != nil {
return fmt.Errorf("retrieving all netlink devices: %w", err)
}
egressDevs := []netlink.Link{}
ingressDevs := []netlink.Link{}
for _, l := range links {
if l.Attrs().Name != iface.Name {
continue
}
ingressFilters, err := netlink.FilterList(l, netlink.HANDLE_MIN_INGRESS)
if err != nil {
return fmt.Errorf("listing ingress filters: %w", err)
}
for _, filter := range ingressFilters {
if bpfFilter, ok := filter.(*netlink.BpfFilter); ok {
if strings.HasPrefix(bpfFilter.Name, tcIngressFilterName) {
ingressDevs = append(ingressDevs, l)
}
}
}
egressFilters, err := netlink.FilterList(l, netlink.HANDLE_MIN_EGRESS)
if err != nil {
return fmt.Errorf("listing egress filters: %w", err)
}
for _, filter := range egressFilters {
if bpfFilter, ok := filter.(*netlink.BpfFilter); ok {
if strings.HasPrefix(bpfFilter.Name, tcEgressFilterName) {
egressDevs = append(egressDevs, l)
}
}
}
}
for _, dev := range ingressDevs {
ilog.Debugf("removing ingress stale tc filters from %s", dev.Attrs().Name)
err = removeTCFilters(dev.Attrs().Name, netlink.HANDLE_MIN_INGRESS)
if err != nil {
ilog.WithError(err).Errorf("couldn't remove ingress tc filters from %s", dev.Attrs().Name)
}
}
for _, dev := range egressDevs {
ilog.Debugf("removing egress stale tc filters from %s", dev.Attrs().Name)
err = removeTCFilters(dev.Attrs().Name, netlink.HANDLE_MIN_EGRESS)
if err != nil {
ilog.WithError(err).Errorf("couldn't remove egress tc filters from %s", dev.Attrs().Name)
}
}
return nil
}
// Register and links the eBPF fetcher into the system. The program should invoke Unregister
// before exiting.
func (m *FlowFetcher) Register(iface ifaces.Interface) error {
ilog := log.WithField("iface", iface)
handle, err := netlink.NewHandleAt(iface.NetNS)
if err != nil {
return fmt.Errorf("failed to create handle for netns (%s): %w", iface.NetNS.String(), err)
}
defer handle.Delete()
// Load pre-compiled programs and maps into the kernel, and rewrites the configuration
ipvlan, err := handle.LinkByIndex(iface.Index)
if err != nil {
return fmt.Errorf("failed to lookup ipvlan device %d (%s): %w", iface.Index, iface.Name, err)
}
qdiscAttrs := netlink.QdiscAttrs{
LinkIndex: ipvlan.Attrs().Index,
Handle: netlink.MakeHandle(0xffff, 0),
Parent: netlink.HANDLE_CLSACT,
}
qdisc := &netlink.GenericQdisc{
QdiscAttrs: qdiscAttrs,
QdiscType: qdiscType,
}
if err := handle.QdiscDel(qdisc); err == nil {
ilog.Warn("qdisc clsact already existed. Deleted it")
}
if err := handle.QdiscAdd(qdisc); err != nil {
if errors.Is(err, fs.ErrExist) {
ilog.WithError(err).Warn("qdisc clsact already exists. Ignoring")
} else {
return fmt.Errorf("failed to create clsact qdisc on %d (%s): %w", iface.Index, iface.Name, err)
}
}
m.qdiscs[iface] = qdisc
// Remove previously installed filters
if err := m.removePreviousFilters(iface); err != nil {
return fmt.Errorf("failed to remove previous filters: %w", err)
}
if err := m.registerEgress(iface, ipvlan, handle); err != nil {
return err
}
return m.registerIngress(iface, ipvlan, handle)
}
func (m *FlowFetcher) registerEgress(iface ifaces.Interface, ipvlan netlink.Link, handle *netlink.Handle) error {
ilog := log.WithField("iface", iface)
if !m.enableEgress {
ilog.Debug("ignoring egress traffic, according to user configuration")
return nil
}
// Fetch events on egress
egressAttrs := netlink.FilterAttrs{
LinkIndex: ipvlan.Attrs().Index,
Parent: netlink.HANDLE_MIN_EGRESS,
Handle: netlink.MakeHandle(0, 1),
Protocol: 3,
Priority: 1,
}
egressFilter := &netlink.BpfFilter{
FilterAttrs: egressAttrs,
Fd: m.objects.TcEgressFlowParse.FD(),
Name: tcEgressFilterName,
DirectAction: true,
}
if err := handle.FilterDel(egressFilter); err == nil {
ilog.Warn("egress filter already existed. Deleted it")
}
if err := handle.FilterAdd(egressFilter); err != nil {
if errors.Is(err, fs.ErrExist) {
ilog.WithError(err).Warn("egress filter already exists. Ignoring")
} else {
return fmt.Errorf("failed to create egress filter: %w", err)
}
}
m.egressFilters[iface] = egressFilter
return nil
}
func (m *FlowFetcher) registerIngress(iface ifaces.Interface, ipvlan netlink.Link, handle *netlink.Handle) error {
ilog := log.WithField("iface", iface)
if !m.enableIngress {
ilog.Debug("ignoring ingress traffic, according to user configuration")
return nil
}
// Fetch events on ingress
ingressAttrs := netlink.FilterAttrs{
LinkIndex: ipvlan.Attrs().Index,
Parent: netlink.HANDLE_MIN_INGRESS,
Handle: netlink.MakeHandle(0, 1),
Protocol: unix.ETH_P_ALL,
Priority: 1,
}
ingressFilter := &netlink.BpfFilter{
FilterAttrs: ingressAttrs,
Fd: m.objects.TcIngressFlowParse.FD(),
Name: tcIngressFilterName,
DirectAction: true,
}
if err := handle.FilterDel(ingressFilter); err == nil {
ilog.Warn("ingress filter already existed. Deleted it")
}
if err := handle.FilterAdd(ingressFilter); err != nil {
if errors.Is(err, fs.ErrExist) {
ilog.WithError(err).Warn("ingress filter already exists. Ignoring")
} else {
return fmt.Errorf("failed to create ingress filter: %w", err)
}
}
m.ingressFilters[iface] = ingressFilter
return nil
}
// Close the eBPF fetcher from the system.
// We don't need a "Close(iface)" method because the filters and qdiscs
// are automatically removed when the interface is down
// nolint:cyclop
func (m *FlowFetcher) Close() error {
log.Debug("unregistering eBPF objects")
var errs []error
if m.pktDropsTracePoint != nil {
if err := m.pktDropsTracePoint.Close(); err != nil {
errs = append(errs, err)
}
}
if m.rttFentryLink != nil {
if err := m.rttFentryLink.Close(); err != nil {
errs = append(errs, err)
}
}
if m.rttKprobeLink != nil {
if err := m.rttKprobeLink.Close(); err != nil {
errs = append(errs, err)
}
}
// m.ringbufReader.Read is a blocking operation, so we need to close the ring buffer
// from another goroutine to avoid the system not being able to exit if there
// isn't traffic in a given interface
if m.ringbufReader != nil {
if err := m.ringbufReader.Close(); err != nil {
errs = append(errs, err)
}
}
if m.objects != nil {
if err := m.objects.TcEgressFlowParse.Close(); err != nil {
errs = append(errs, err)
}
if err := m.objects.TcIngressFlowParse.Close(); err != nil {
errs = append(errs, err)
}
if err := m.objects.TcxEgressFlowParse.Close(); err != nil {
errs = append(errs, err)
}
if err := m.objects.TcxIngressFlowParse.Close(); err != nil {
errs = append(errs, err)
}
if err := m.objects.AggregatedFlows.Close(); err != nil {
errs = append(errs, err)
}
if err := m.objects.DirectFlows.Close(); err != nil {
errs = append(errs, err)
}
if err := m.objects.DnsFlows.Close(); err != nil {
errs = append(errs, err)
}
if err := m.objects.GlobalCounters.Close(); err != nil {
errs = append(errs, err)
}
if err := m.objects.FilterMap.Close(); err != nil {
errs = append(errs, err)
}
if len(errs) == 0 {
m.objects = nil
}
}
for iface, ef := range m.egressFilters {
log := log.WithField("interface", iface)
log.Debug("deleting egress filter")
if err := doIgnoreNoDev(netlink.FilterDel, netlink.Filter(ef), log); err != nil {
errs = append(errs, fmt.Errorf("deleting egress filter: %w", err))
}
}
m.egressFilters = map[ifaces.Interface]*netlink.BpfFilter{}
for iface, igf := range m.ingressFilters {
log := log.WithField("interface", iface)
log.Debug("deleting ingress filter")
if err := doIgnoreNoDev(netlink.FilterDel, netlink.Filter(igf), log); err != nil {
errs = append(errs, fmt.Errorf("deleting ingress filter: %w", err))
}
}
m.ingressFilters = map[ifaces.Interface]*netlink.BpfFilter{}
for iface, qd := range m.qdiscs {
log := log.WithField("interface", iface)
log.Debug("deleting Qdisc")
if err := doIgnoreNoDev(netlink.QdiscDel, netlink.Qdisc(qd), log); err != nil {
errs = append(errs, fmt.Errorf("deleting qdisc: %w", err))
}
}
m.qdiscs = map[ifaces.Interface]*netlink.GenericQdisc{}
if len(errs) == 0 {
return nil
}
for iface, l := range m.egressTCXLink {
log := log.WithField("interface", iface)
log.Debug("detach egress TCX hook")
l.Close()
}
m.egressTCXLink = map[ifaces.Interface]link.Link{}
for iface, l := range m.ingressTCXLink {
log := log.WithField("interface", iface)
log.Debug("detach ingress TCX hook")
l.Close()
}
m.ingressTCXLink = map[ifaces.Interface]link.Link{}
var errStrings []string
for _, err := range errs {
errStrings = append(errStrings, err.Error())
}
return errors.New(`errors: "` + strings.Join(errStrings, `", "`) + `"`)
}
// doIgnoreNoDev runs the provided syscall over the provided device and ignores the error
// if the cause is a non-existing device (just logs the error as debug).
// If the agent is deployed as part of the Network Observability pipeline, normally
// undeploying the FlowCollector could cause the agent to try to remove resources
// from Pods that have been removed immediately before (e.g. flowlogs-pipeline or the
// console plugin), so we avoid logging some errors that would unnecessarily raise the
// user's attention.
// This function uses generics because the set of provided functions accept different argument
// types.
func doIgnoreNoDev[T any](sysCall func(T) error, dev T, log *logrus.Entry) error {
if err := sysCall(dev); err != nil {
if errors.Is(err, unix.ENODEV) {
log.WithError(err).Error("can't delete. Ignore this error if other pods or interfaces " +
" are also being deleted at this moment. For example, if you are undeploying " +
" a FlowCollector or Deployment where this agent is part of")
} else {
return err
}
}
return nil
}
func (m *FlowFetcher) ReadRingBuf() (ringbuf.Record, error) {
return m.ringbufReader.Read()
}
// LookupAndDeleteMap reads all the entries from the eBPF map and removes them from it.
// TODO: detect whether BatchLookupAndDelete is supported (Kernel>=5.6) and use it selectively
// Supported Lookup/Delete operations by kernel: https://github.com/iovisor/bcc/blob/master/docs/kernel-versions.md
func (m *FlowFetcher) LookupAndDeleteMap(met *metrics.Metrics) map[BpfFlowId][]BpfFlowMetrics {
if !m.lookupAndDeleteSupported {
return m.legacyLookupAndDeleteMap(met)
}
flowMap := m.objects.AggregatedFlows
iterator := flowMap.Iterate()
var flows = make(map[BpfFlowId][]BpfFlowMetrics, m.cacheMaxSize)
var ids []BpfFlowId
var id BpfFlowId
var metrics []BpfFlowMetrics
// First, get all ids and don't care about metrics (we need lookup+delete to be atomic)
for iterator.Next(&id, &metrics) {
ids = append(ids, id)
}
count := 0
// Run the atomic Lookup+Delete; if new ids have been inserted in the meantime, they'll be fetched next time
for i, id := range ids {
count++
if err := flowMap.LookupAndDelete(&id, &metrics); err != nil {
if i == 0 && errors.Is(err, ebpf.ErrNotSupported) {
log.WithError(err).Warnf("switching to legacy mode")
m.lookupAndDeleteSupported = false
return m.legacyLookupAndDeleteMap(met)
}
log.WithError(err).WithField("flowId", id).Warnf("couldn't delete flow entry")
met.Errors.WithErrorName("flow-fetcher", "CannotDeleteFlows").Inc()
continue
}
flows[id] = metrics
}
met.BufferSizeGauge.WithBufferName("hashmap-total").Set(float64(count))
met.BufferSizeGauge.WithBufferName("hashmap-unique").Set(float64(len(flows)))
m.ReadGlobalCounter(met)
return flows
}
// ReadGlobalCounter reads the global counter and updates drop flows counter metrics
func (m *FlowFetcher) ReadGlobalCounter(met *metrics.Metrics) {
var allCPUValue []uint32
reasons := []string{
"CannotUpdateHashMapCounter",
"FilterRejectCounter",
"FilterAcceptCounter",
"FilterNoMatchCounter",
}
zeroCounters := make([]uint32, ebpf.MustPossibleCPU())
for key := BpfGlobalCountersKeyTHASHMAP_FLOWS_DROPPED_KEY; key < BpfGlobalCountersKeyTMAX_DROPPED_FLOWS_KEY; key++ {
if err := m.objects.GlobalCounters.Lookup(key, &allCPUValue); err != nil {
log.WithError(err).Warnf("couldn't read global counter")
return
}
// aggregate all the counters
for _, counter := range allCPUValue {
if key == BpfGlobalCountersKeyTHASHMAP_FLOWS_DROPPED_KEY {
met.DroppedFlowsCounter.WithSourceAndReason("flow-fetcher", reasons[key]).Add(float64(counter))
} else {
met.FilteredFlowsCounter.WithSourceAndReason("flow-fetcher", reasons[key]).Add(float64(counter))
}
}
// reset the global counter-map entry
if err := m.objects.GlobalCounters.Put(key, zeroCounters); err != nil {
log.WithError(err).Warnf("coudn't reset global counter")
return
}
}
}
// DeleteMapsStaleEntries Look for any stale entries in the features maps and delete them
func (m *FlowFetcher) DeleteMapsStaleEntries(timeOut time.Duration) {
m.lookupAndDeleteDNSMap(timeOut)
}
// lookupAndDeleteDNSMap iterate over DNS queries map and delete any stale DNS requests
// entries which never get responses for.
func (m *FlowFetcher) lookupAndDeleteDNSMap(timeOut time.Duration) {
monotonicTimeNow := monotime.Now()
dnsMap := m.objects.DnsFlows
var dnsKey BpfDnsFlowId
var keysToDelete []BpfDnsFlowId
var dnsVal uint64
if dnsMap != nil {
// Ideally the Lookup + Delete should be atomic, however we cannot use LookupAndDelete since the deletion is conditional
// Do not delete while iterating, as it causes severe performance degradation
iterator := dnsMap.Iterate()
for iterator.Next(&dnsKey, &dnsVal) {
if time.Duration(uint64(monotonicTimeNow)-dnsVal) >= timeOut {
keysToDelete = append(keysToDelete, dnsKey)
}
}
for _, dnsKey = range keysToDelete {
if err := dnsMap.Delete(dnsKey); err != nil {
log.WithError(err).WithField("dnsKey", dnsKey).Warnf("couldn't delete DNS record entry")
}
}
}
}
// kernelSpecificLoadAndAssign based on kernel version it will load only the supported ebPF hooks
func kernelSpecificLoadAndAssign(oldKernel bool, spec *ebpf.CollectionSpec) (BpfObjects, error) {
objects := BpfObjects{}
// For older kernel (< 5.14) kfree_sbk drop hook doesn't exists
if oldKernel {
// Here we define another structure similar to the bpf2go created one but w/o the hooks that does not exist in older kernel
// Note: if new hooks are added in the future we need to update the following structures manually
type NewBpfPrograms struct {
TcEgressFlowParse *ebpf.Program `ebpf:"tc_egress_flow_parse"`
TcIngressFlowParse *ebpf.Program `ebpf:"tc_ingress_flow_parse"`
TcxEgressFlowParse *ebpf.Program `ebpf:"tcx_egress_flow_parse"`
TcxIngressFlowParse *ebpf.Program `ebpf:"tcx_ingress_flow_parse"`
TcEgressPcaParse *ebpf.Program `ebpf:"tc_egress_pca_parse"`
TcIngressPcaParse *ebpf.Program `ebpf:"tc_ingress_pca_parse"`
TcxEgressPcaParse *ebpf.Program `ebpf:"tcx_egress_pca_parse"`
TcxIngressPcaParse *ebpf.Program `ebpf:"tcx_ingress_pca_parse"`
TCPRcvFentry *ebpf.Program `ebpf:"tcp_rcv_fentry"`
TCPRcvKprobe *ebpf.Program `ebpf:"tcp_rcv_kprobe"`
}
type NewBpfObjects struct {
NewBpfPrograms
BpfMaps
}
var newObjects NewBpfObjects
// remove pktdrop hook from the spec
delete(spec.Programs, pktDropHook)
newObjects.NewBpfPrograms = NewBpfPrograms{}
if err := spec.LoadAndAssign(&newObjects, nil); err != nil {
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
// Using %+v will print the whole verifier error, not just the last
// few lines.
log.Infof("Verifier error: %+v", ve)
}
return objects, fmt.Errorf("loading and assigning BPF objects: %w", err)
}
// Manually assign maps and programs to the original objects variable
// Note for any future maps or programs make sure to copy them manually here
objects.DirectFlows = newObjects.DirectFlows
objects.AggregatedFlows = newObjects.AggregatedFlows
objects.DnsFlows = newObjects.DnsFlows
objects.FilterMap = newObjects.FilterMap
objects.GlobalCounters = newObjects.GlobalCounters
objects.TcEgressFlowParse = newObjects.TcEgressFlowParse
objects.TcIngressFlowParse = newObjects.TcIngressFlowParse
objects.TcxEgressFlowParse = newObjects.TcxEgressFlowParse
objects.TcxIngressFlowParse = newObjects.TcxIngressFlowParse
objects.TcEgressPcaParse = newObjects.TcEgressPcaParse
objects.TcIngressPcaParse = newObjects.TcIngressPcaParse
objects.TcxEgressPcaParse = newObjects.TcxEgressPcaParse
objects.TcxIngressPcaParse = newObjects.TcxIngressPcaParse
objects.TcpRcvFentry = newObjects.TCPRcvFentry
objects.TcpRcvKprobe = newObjects.TCPRcvKprobe
objects.KfreeSkb = nil
} else {
if err := spec.LoadAndAssign(&objects, nil); err != nil {
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
// Using %+v will print the whole verifier error, not just the last
// few lines.
log.Infof("Verifier error: %+v", ve)
}
return objects, fmt.Errorf("loading and assigning BPF objects: %w", err)
}
}
/*
* since we load the program only when the we start we need to release
* memory used by cached kernel BTF see https://github.com/cilium/ebpf/issues/1063
* for more details.
*/
btf.FlushKernelSpec()
return objects, nil
}
// It provides access to packets from the kernel space (via PerfCPU hashmap)
type PacketFetcher struct {
objects *BpfObjects
qdiscs map[ifaces.Interface]*netlink.GenericQdisc
egressFilters map[ifaces.Interface]*netlink.BpfFilter
ingressFilters map[ifaces.Interface]*netlink.BpfFilter
perfReader *perf.Reader
cacheMaxSize int
enableIngress bool
enableEgress bool
egressTCXLink map[ifaces.Interface]link.Link
ingressTCXLink map[ifaces.Interface]link.Link
lookupAndDeleteSupported bool
}
func NewPacketFetcher(cfg *FlowFetcherConfig) (*PacketFetcher, error) {
if err := rlimit.RemoveMemlock(); err != nil {
log.WithError(err).
Warn("can't remove mem lock. The agent could not be able to start eBPF programs")
}
objects := BpfObjects{}
spec, err := LoadBpf()
if err != nil {
return nil, err
}
// Removing Specs for flow agent
objects.TcEgressFlowParse = nil
objects.TcIngressFlowParse = nil
objects.TcxEgressFlowParse = nil
objects.TcxIngressFlowParse = nil
objects.DirectFlows = nil
objects.AggregatedFlows = nil
delete(spec.Programs, aggregatedFlowsMap)
delete(spec.Programs, constSampling)
delete(spec.Programs, constTraceMessages)
delete(spec.Programs, constEnableDNSTracking)
delete(spec.Programs, constEnableFlowFiltering)
pcaEnable := 0
if cfg.EnablePCA {
pcaEnable = 1
}
if err := spec.RewriteConstants(map[string]interface{}{
constSampling: uint32(cfg.Sampling),
constPcaEnable: uint8(pcaEnable),
}); err != nil {
return nil, fmt.Errorf("rewriting BPF constants definition: %w", err)
}
if err := spec.LoadAndAssign(&objects, nil); err != nil {
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
// Using %+v will print the whole verifier error, not just the last
// few lines.
plog.Infof("Verifier error: %+v", ve)
}
return nil, fmt.Errorf("loading and assigning BPF objects: %w", err)
}
f := NewFilter(&objects, cfg.FilterConfig)
if err := f.ProgramFilter(); err != nil {
return nil, fmt.Errorf("programming flow filter: %w", err)
}
// read packets from igress+egress perf array
packets, err := perf.NewReader(objects.PacketRecord, os.Getpagesize())
if err != nil {
return nil, fmt.Errorf("accessing to perf: %w", err)
}
return &PacketFetcher{
objects: &objects,
perfReader: packets,
egressFilters: map[ifaces.Interface]*netlink.BpfFilter{},
ingressFilters: map[ifaces.Interface]*netlink.BpfFilter{},
qdiscs: map[ifaces.Interface]*netlink.GenericQdisc{},
cacheMaxSize: cfg.CacheMaxSize,
enableIngress: cfg.EnableIngress,
enableEgress: cfg.EnableEgress,
egressTCXLink: map[ifaces.Interface]link.Link{},
ingressTCXLink: map[ifaces.Interface]link.Link{},
lookupAndDeleteSupported: true, // this will be turned off later if found to be not supported
}, nil
}
func registerInterface(iface ifaces.Interface) (*netlink.GenericQdisc, netlink.Link, error) {
ilog := plog.WithField("iface", iface)
handle, err := netlink.NewHandleAt(iface.NetNS)
if err != nil {
return nil, nil, fmt.Errorf("failed to create handle for netns (%s): %w", iface.NetNS.String(), err)
}
defer handle.Delete()
// Load pre-compiled programs and maps into the kernel, and rewrites the configuration
ipvlan, err := handle.LinkByIndex(iface.Index)
if err != nil {
return nil, nil, fmt.Errorf("failed to lookup ipvlan device %d (%s): %w", iface.Index, iface.Name, err)
}
qdiscAttrs := netlink.QdiscAttrs{
LinkIndex: ipvlan.Attrs().Index,
Handle: netlink.MakeHandle(0xffff, 0),
Parent: netlink.HANDLE_CLSACT,
}
qdisc := &netlink.GenericQdisc{
QdiscAttrs: qdiscAttrs,
QdiscType: qdiscType,
}
if err := handle.QdiscDel(qdisc); err == nil {
ilog.Warn("qdisc clsact already existed. Deleted it")
}
if err := handle.QdiscAdd(qdisc); err != nil {
if errors.Is(err, fs.ErrExist) {
ilog.WithError(err).Warn("qdisc clsact already exists. Ignoring")
} else {
return nil, nil, fmt.Errorf("failed to create clsact qdisc on %d (%s): %w", iface.Index, iface.Name, err)
}
}
return qdisc, ipvlan, nil
}
func (p *PacketFetcher) Register(iface ifaces.Interface) error {
qdisc, ipvlan, err := registerInterface(iface)
if err != nil {
return err
}
p.qdiscs[iface] = qdisc
if err := p.registerEgress(iface, ipvlan); err != nil {
return err
}
return p.registerIngress(iface, ipvlan)
}
func (p *PacketFetcher) AttachTCX(iface ifaces.Interface) error {
ilog := log.WithField("iface", iface)
if iface.NetNS != netns.None() {
originalNs, err := netns.Get()
if err != nil {
return fmt.Errorf("PCA failed to get current netns: %w", err)
}
defer func() {
if err := netns.Set(originalNs); err != nil {
ilog.WithError(err).Error("PCA failed to set netns back")
}
originalNs.Close()
}()
if err := unix.Setns(int(iface.NetNS), unix.CLONE_NEWNET); err != nil {
return fmt.Errorf("PCA failed to setns to %s: %w", iface.NetNS, err)
}
}
if p.enableEgress {
egrLink, err := link.AttachTCX(link.TCXOptions{
Program: p.objects.BpfPrograms.TcxEgressPcaParse,
Attach: ebpf.AttachTCXEgress,
Interface: iface.Index,
})
if err != nil {
if errors.Is(err, fs.ErrExist) {
// The interface already has a TCX egress hook
log.WithField("iface", iface.Name).Debug("interface already has a TCX PCA egress hook ignore")
} else {
return fmt.Errorf("failed to attach PCA TCX egress: %w", err)
}
}
p.egressTCXLink[iface] = egrLink
ilog.WithField("interface", iface.Name).Debug("successfully attach PCA egressTCX hook")
}
if p.enableIngress {
ingLink, err := link.AttachTCX(link.TCXOptions{
Program: p.objects.BpfPrograms.TcxIngressPcaParse,
Attach: ebpf.AttachTCXIngress,
Interface: iface.Index,
})
if err != nil {
if errors.Is(err, fs.ErrExist) {
// The interface already has a TCX ingress hook
log.WithField("iface", iface.Name).Debug("interface already has a TCX PCA ingress hook ignore")
} else {
return fmt.Errorf("failed to attach PCA TCX ingress: %w", err)
}
}
p.ingressTCXLink[iface] = ingLink
ilog.WithField("interface", iface.Name).Debug("successfully attach PCA ingressTCX hook")
}
return nil
}
func fetchEgressEvents(iface ifaces.Interface, ipvlan netlink.Link, parser *ebpf.Program, name string) (*netlink.BpfFilter, error) {
ilog := plog.WithField("iface", iface)
egressAttrs := netlink.FilterAttrs{
LinkIndex: ipvlan.Attrs().Index,
Parent: netlink.HANDLE_MIN_EGRESS,
Handle: netlink.MakeHandle(0, 1),
Protocol: 3,
Priority: 1,
}
egressFilter := &netlink.BpfFilter{
FilterAttrs: egressAttrs,
Fd: parser.FD(),
Name: "tc/" + name,
DirectAction: true,
}
if err := netlink.FilterDel(egressFilter); err == nil {
ilog.Warn("egress filter already existed. Deleted it")
}
if err := netlink.FilterAdd(egressFilter); err != nil {
if errors.Is(err, fs.ErrExist) {
ilog.WithError(err).Warn("egress filter already exists. Ignoring")
} else {
return nil, fmt.Errorf("failed to create egress filter: %w", err)
}
}
return egressFilter, nil
}