-
Notifications
You must be signed in to change notification settings - Fork 340
/
loader.go
957 lines (836 loc) · 25.1 KB
/
loader.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package program
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/link"
cachedbtf "github.com/cilium/tetragon/pkg/btf"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/option"
"github.com/cilium/tetragon/pkg/sensors/unloader"
"golang.org/x/sys/unix"
)
var (
verifierLogBufferSize = 10 * 1024 * 1024 // 10MB
)
// AttachFunc is the type for the various attachment functions. The function is
// given the program and it's up to it to close it.
type AttachFunc func(*ebpf.Collection, *ebpf.CollectionSpec, *ebpf.Program, *ebpf.ProgramSpec) (unloader.Unloader, error)
type OpenFunc func(*ebpf.CollectionSpec) error
type customInstall struct {
mapName string
secPrefix string
}
type loadOpts struct {
attach AttachFunc
open OpenFunc
ci *customInstall
}
func RawAttach(targetFD int) AttachFunc {
return RawAttachWithFlags(targetFD, 0)
}
func RawAttachWithFlags(targetFD int, flags uint32) AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
err := link.RawAttachProgram(link.RawAttachProgramOptions{
Target: targetFD,
Program: prog,
Attach: spec.AttachType,
Flags: flags,
})
if err != nil {
prog.Close()
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return unloader.ChainUnloader{
unloader.PinUnloader{
Prog: prog,
},
&unloader.RawDetachUnloader{
TargetFD: targetFD,
Name: spec.Name,
Prog: prog,
AttachType: spec.AttachType,
},
}, nil
}
}
func TracepointAttach(load *Program) AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
parts := strings.Split(load.Attach, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("tracepoint attach argument must be in the form category/tracepoint, got: %s", load.Attach)
}
tpLink, err := link.Tracepoint(parts[0], parts[1], prog, nil)
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: tpLink,
RelinkFn: func() (link.Link, error) {
return link.Tracepoint(parts[0], parts[1], prog, nil)
},
}, nil
}
}
func RawTracepointAttach(load *Program) AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
var lnk link.Link
var err error
parts := strings.Split(load.Attach, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("raw_tracepoint attach argument must be in the form category/tracepoint, got: %s", load.Attach)
}
lnk, err = link.AttachRawTracepoint(link.RawTracepointOptions{
Name: parts[1],
Program: prog,
})
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return unloader.ChainUnloader{
unloader.PinUnloader{
Prog: prog,
},
unloader.LinkUnloader{
Link: lnk,
},
}, nil
}
}
func disableProg(coll *ebpf.CollectionSpec, name string) {
if spec, ok := coll.Programs[name]; ok {
spec.Type = ebpf.UnspecifiedProgram
}
}
func KprobeOpen(load *Program) OpenFunc {
return func(coll *ebpf.CollectionSpec) error {
// The generic_kprobe_override program is part of bpf_generic_kprobe.o object,
// so let's disable it if the override is not configured. Otherwise it gets
// loaded and bpftool will show it.
if !load.Override {
disableProg(coll, "generic_kprobe_override")
disableProg(coll, "generic_fmodret_override")
} else {
if load.OverrideFmodRet {
spec, ok := coll.Programs["generic_fmodret_override"]
if !ok {
return fmt.Errorf("failed to find generic_fmodret_override")
}
spec.AttachTo = load.Attach
disableProg(coll, "generic_kprobe_override")
} else {
disableProg(coll, "generic_fmodret_override")
}
}
return nil
}
}
func kprobeAttach(load *Program, prog *ebpf.Program, spec *ebpf.ProgramSpec, symbol string) (unloader.Unloader, error) {
var linkFn func() (link.Link, error)
if load.RetProbe {
linkFn = func() (link.Link, error) { return link.Kretprobe(symbol, prog, nil) }
} else {
linkFn = func() (link.Link, error) { return link.Kprobe(symbol, prog, nil) }
}
lnk, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: lnk,
RelinkFn: linkFn,
}, nil
}
func kprobeAttachOverride(load *Program, bpfDir string,
coll *ebpf.Collection, collSpec *ebpf.CollectionSpec) error {
spec, ok := collSpec.Programs["generic_kprobe_override"]
if !ok {
return fmt.Errorf("spec for generic_kprobe_override program not found")
}
prog, ok := coll.Programs["generic_kprobe_override"]
if !ok {
return fmt.Errorf("program generic_kprobe_override not found")
}
prog, err := prog.Clone()
if err != nil {
return fmt.Errorf("failed to clone generic_kprobe_override program: %w", err)
}
pinPath := filepath.Join(bpfDir, fmt.Sprint(load.PinPath, "-override"))
if err := prog.Pin(pinPath); err != nil {
return fmt.Errorf("pinning '%s' to '%s' failed: %w", load.Label, pinPath, err)
}
load.unloaderOverride, err = kprobeAttach(load, prog, spec, load.Attach)
if err != nil {
logger.GetLogger().Warnf("Failed to attach override program: %w", err)
}
return nil
}
func fmodretAttachOverride(load *Program, bpfDir string,
coll *ebpf.Collection, collSpec *ebpf.CollectionSpec) error {
spec, ok := collSpec.Programs["generic_fmodret_override"]
if !ok {
return fmt.Errorf("spec for generic_fmodret_override program not found")
}
prog, ok := coll.Programs["generic_fmodret_override"]
if !ok {
return fmt.Errorf("program generic_fmodret_override not found")
}
prog, err := prog.Clone()
if err != nil {
return fmt.Errorf("failed to clone generic_fmodret_override program: %w", err)
}
pinPath := filepath.Join(bpfDir, fmt.Sprint(load.PinPath, "-override"))
if err := prog.Pin(pinPath); err != nil {
return fmt.Errorf("pinning '%s' to '%s' failed: %w", load.Label, pinPath, err)
}
linkFn := func() (link.Link, error) {
return link.AttachTracing(link.TracingOptions{
Program: prog,
})
}
lnk, err := linkFn()
if err != nil {
return fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
load.unloaderOverride = &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: lnk,
RelinkFn: linkFn,
}
return nil
}
func KprobeAttach(load *Program, bpfDir string) AttachFunc {
return func(coll *ebpf.Collection, collSpec *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
if load.Override {
if load.OverrideFmodRet {
if err := fmodretAttachOverride(load, bpfDir, coll, collSpec); err != nil {
return nil, err
}
} else {
if err := kprobeAttachOverride(load, bpfDir, coll, collSpec); err != nil {
return nil, err
}
}
}
return kprobeAttach(load, prog, spec, load.Attach)
}
}
func UprobeAttach(load *Program) AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
data, ok := load.AttachData.(*UprobeAttachData)
if !ok {
return nil, fmt.Errorf("attaching '%s' failed: wrong attach data", spec.Name)
}
linkFn := func() (link.Link, error) {
exec, err := link.OpenExecutable(data.Path)
if err != nil {
return nil, err
}
return exec.Uprobe(data.Symbol, prog, nil)
}
lnk, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: lnk,
RelinkFn: linkFn,
}, nil
}
}
func MultiUprobeAttach(load *Program) AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
data, ok := load.AttachData.(*MultiUprobeAttachData)
if !ok {
return nil, fmt.Errorf("attaching '%s' failed: wrong attach data", spec.Name)
}
linkFn := func() ([]link.Link, error) {
var links []link.Link
var lnk link.Link
for path, attach := range data.Attach {
exec, err := link.OpenExecutable(path)
if err != nil {
return nil, err
}
lnk, err = exec.UprobeMulti(attach.Symbols, prog, &link.UprobeMultiOptions{Cookies: attach.Cookies})
if err != nil {
return nil, err
}
links = append(links, lnk)
}
return links, nil
}
links, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.MultiRelinkUnloader{
UnloadProg: unloader.ChainUnloader{
unloader.PinUnloader{
Prog: prog,
},
}.Unload,
IsLinked: true,
Links: links,
RelinkFn: linkFn,
}, nil
}
}
func NoAttach() AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, _ *ebpf.ProgramSpec) (unloader.Unloader, error) {
return unloader.ChainUnloader{
unloader.PinUnloader{
Prog: prog,
},
}, nil
}
}
func TracingAttach() AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
linkFn := func() (link.Link, error) {
return link.AttachTracing(link.TracingOptions{
Program: prog,
})
}
lnk, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: lnk,
RelinkFn: linkFn,
}, nil
}
}
func LSMAttach() AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
linkFn := func() (link.Link, error) {
return link.AttachLSM(link.LSMOptions{
Program: prog,
})
}
lnk, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: lnk,
RelinkFn: linkFn,
}, nil
}
}
func multiKprobeAttach(load *Program, prog *ebpf.Program,
spec *ebpf.ProgramSpec, opts link.KprobeMultiOptions) (unloader.Unloader, error) {
var lnk link.Link
var err error
if load.RetProbe {
lnk, err = link.KretprobeMulti(prog, opts)
} else {
lnk, err = link.KprobeMulti(prog, opts)
}
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return unloader.ChainUnloader{
unloader.PinUnloader{
Prog: prog,
},
unloader.LinkUnloader{
Link: lnk,
},
}, nil
}
func MultiKprobeAttach(load *Program, bpfDir string) AttachFunc {
return func(coll *ebpf.Collection, collSpec *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
data, ok := load.AttachData.(*MultiKprobeAttachData)
if !ok {
return nil, fmt.Errorf("attaching '%s' failed: wrong attach data", spec.Name)
}
if load.Override {
progOverrideSpec, ok := collSpec.Programs["generic_kprobe_override"]
if ok {
progOverrideSpec.Type = ebpf.UnspecifiedProgram
}
progOverride, ok := coll.Programs["generic_kprobe_override"]
if !ok {
return nil, fmt.Errorf("program for section '%s' not found", load.Label)
}
progOverride, err := progOverride.Clone()
if err != nil {
return nil, fmt.Errorf("failed to clone program '%s': %w", load.Label, err)
}
pinPath := filepath.Join(bpfDir, fmt.Sprint(load.PinPath, "-override"))
if err := progOverride.Pin(pinPath); err != nil {
return nil, fmt.Errorf("pinning '%s' to '%s' failed: %w", load.Label, pinPath, err)
}
opts := link.KprobeMultiOptions{
Symbols: data.Overrides,
}
load.unloaderOverride, err = multiKprobeAttach(load, progOverride, progOverrideSpec, opts)
if err != nil {
logger.GetLogger().Warnf("Failed to attach override program: %w", err)
}
}
opts := link.KprobeMultiOptions{
Symbols: data.Symbols,
Cookies: data.Cookies,
}
return multiKprobeAttach(load, prog, spec, opts)
}
}
func LoadTracepointProgram(bpfDir string, load *Program, verbose int) error {
var ci *customInstall
for mName, mPath := range load.PinMap {
if mName == "tp_calls" || mName == "execve_calls" {
ci = &customInstall{mPath, "tracepoint"}
break
}
}
opts := &loadOpts{
attach: TracepointAttach(load),
ci: ci,
}
return loadProgram(bpfDir, load, opts, verbose)
}
func LoadRawTracepointProgram(bpfDir string, load *Program, verbose int) error {
opts := &loadOpts{
attach: RawTracepointAttach(load),
}
return loadProgram(bpfDir, load, opts, verbose)
}
func LoadKprobeProgram(bpfDir string, load *Program, verbose int) error {
var ci *customInstall
for mName, mPath := range load.PinMap {
if mName == "kprobe_calls" || mName == "retkprobe_calls" {
ci = &customInstall{mPath, "kprobe"}
break
}
}
opts := &loadOpts{
attach: KprobeAttach(load, bpfDir),
open: KprobeOpen(load),
ci: ci,
}
return loadProgram(bpfDir, load, opts, verbose)
}
func KprobeAttachMany(load *Program, syms []string) AttachFunc {
return func(_ *ebpf.Collection, _ *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
unloader := unloader.ChainUnloader{
unloader.PinUnloader{
Prog: prog,
},
}
for idx := range syms {
un, err := kprobeAttach(load, prog, spec, syms[idx])
if err != nil {
return nil, err
}
unloader = append(unloader, un)
}
return unloader, nil
}
}
func LoadKprobeProgramAttachMany(bpfDir string, load *Program, syms []string, verbose int) error {
opts := &loadOpts{
attach: KprobeAttachMany(load, syms),
}
return loadProgram(bpfDir, load, opts, verbose)
}
func LoadUprobeProgram(bpfDir string, load *Program, verbose int) error {
var ci *customInstall
for mName, mPath := range load.PinMap {
if mName == "uprobe_calls" {
ci = &customInstall{mPath, "uprobe"}
break
}
}
opts := &loadOpts{
attach: UprobeAttach(load),
ci: ci,
}
return loadProgram(bpfDir, load, opts, verbose)
}
func LoadMultiKprobeProgram(bpfDir string, load *Program, verbose int) error {
var ci *customInstall
for mName, mPath := range load.PinMap {
if mName == "kprobe_calls" || mName == "retkprobe_calls" {
ci = &customInstall{mPath, "kprobe"}
break
}
}
opts := &loadOpts{
attach: MultiKprobeAttach(load, bpfDir),
open: KprobeOpen(load),
ci: ci,
}
return loadProgram(bpfDir, load, opts, verbose)
}
func LoadFmodRetProgram(bpfDir string, load *Program, progName string, verbose int) error {
opts := &loadOpts{
attach: func(
_ *ebpf.Collection,
_ *ebpf.CollectionSpec,
prog *ebpf.Program,
spec *ebpf.ProgramSpec,
) (unloader.Unloader, error) {
linkFn := func() (link.Link, error) {
return link.AttachTracing(link.TracingOptions{
Program: prog,
})
}
lnk, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: lnk,
RelinkFn: linkFn,
}, nil
},
open: func(coll *ebpf.CollectionSpec) error {
progSpec, ok := coll.Programs[progName]
if !ok {
return fmt.Errorf("progName %s not in collecition spec programs: %+v", progName, coll.Programs)
}
progSpec.AttachTo = load.Attach
return nil
},
}
return loadProgram(bpfDir, load, opts, verbose)
}
func LoadTracingProgram(bpfDir string, load *Program, verbose int) error {
opts := &loadOpts{
attach: TracingAttach(),
}
return loadProgram(bpfDir, load, opts, verbose)
}
func LoadLSMProgram(bpfDir string, load *Program, verbose int) error {
opts := &loadOpts{
attach: LSMAttach(),
}
return loadProgram(bpfDir, load, opts, verbose)
}
func LoadMultiUprobeProgram(bpfDir string, load *Program, verbose int) error {
ci := &customInstall{fmt.Sprintf("%s-up_calls", load.PinPath), "uprobe"}
opts := &loadOpts{
attach: MultiUprobeAttach(load),
ci: ci,
}
return loadProgram(bpfDir, load, opts, verbose)
}
func slimVerifierError(errStr string) string {
// The error is potentially up to 'verifierLogBufferSize' bytes long,
// and most of it is not interesting. For a user-friendly output, we'll
// only keep the first and last N lines.
nLines := 30
headLines := 0
headEnd := 0
for ; headEnd < len(errStr); headEnd++ {
c := errStr[headEnd]
if c == '\n' {
headLines++
if headLines >= nLines {
break
}
}
}
tailStart := len(errStr) - 1
tailLines := 0
for ; tailStart > headEnd; tailStart-- {
c := errStr[tailStart]
if c == '\n' {
tailLines++
if tailLines >= nLines {
tailStart++
break
}
}
}
return errStr[:headEnd] + "\n...\n" + errStr[tailStart:]
}
func installTailCalls(bpfDir string, spec *ebpf.CollectionSpec, coll *ebpf.Collection, ci *customInstall) error {
// FIXME(JM): This should be replaced by using the cilium/ebpf prog array initialization.
secToProgName := make(map[string]string)
for name, prog := range spec.Programs {
secToProgName[prog.SectionName] = name
}
install := func(mapName string, secPrefix string) error {
tailCallsMap, err := ebpf.LoadPinnedMap(filepath.Join(bpfDir, mapName), nil)
if err != nil {
return nil
}
defer tailCallsMap.Close()
for i := 0; i < 13; i++ {
secName := fmt.Sprintf("%s/%d", secPrefix, i)
if progName, ok := secToProgName[secName]; ok {
if prog, ok := coll.Programs[progName]; ok {
err := tailCallsMap.Update(uint32(i), uint32(prog.FD()), ebpf.UpdateAny)
if err != nil {
return fmt.Errorf("update of tail-call map '%s' failed: %w", mapName, err)
}
}
}
}
return nil
}
if err := install("http1_calls", "sk_msg"); err != nil {
return err
}
if err := install("http1_calls_skb", "sk_skb/stream_verdict"); err != nil {
return err
}
if err := install("tls_calls", "classifier"); err != nil {
return err
}
if ci != nil {
if err := install(ci.mapName, ci.secPrefix); err != nil {
return err
}
}
return nil
}
func doLoadProgram(
bpfDir string,
load *Program,
loadOpts *loadOpts,
verbose int,
) (*LoadedCollection, error) {
var btfSpec *btf.Spec
if btfFilePath := cachedbtf.GetCachedBTFFile(); btfFilePath != "/sys/kernel/btf/vmlinux" || len(option.Config.KMods) > 0 {
// Non-standard path to BTF, open it and provide it as 'KernelTypes'.
var err error
btfSpec, err = btf.LoadSpec(btfFilePath)
if err != nil {
return nil, fmt.Errorf("opening BTF file '%s' failed: %w", btfFilePath, err)
}
if len(option.Config.KMods) > 0 {
btfSpec, err = cachedbtf.AddModulesToSpec(btfSpec, option.Config.KMods)
if err != nil {
return nil, fmt.Errorf("adding modules to spec failed: %w", err)
}
}
}
spec, err := ebpf.LoadCollectionSpec(load.Name)
if err != nil {
return nil, fmt.Errorf("loading collection spec failed: %w", err)
}
if loadOpts.open != nil {
if err := loadOpts.open(spec); err != nil {
return nil, fmt.Errorf("open spec function failed: %w", err)
}
}
for _, ms := range spec.Maps {
if max, ok := load.MaxEntriesMap[ms.Name]; ok {
ms.MaxEntries = max
}
if innerMax, ok := load.MaxEntriesInnerMap[ms.Name]; ok {
if ms.InnerMap == nil {
return nil, fmt.Errorf("no inner map for %s", ms.Name)
}
ms.InnerMap.MaxEntries = innerMax
}
}
// Find all the maps referenced by the program, so we'll rewrite only
// the ones used.
var progSpec *ebpf.ProgramSpec
refMaps := make(map[string]bool)
for _, prog := range spec.Programs {
if prog.SectionName == load.Label {
progSpec = prog
}
for _, inst := range prog.Instructions {
if inst.Reference() != "" {
refMaps[inst.Reference()] = true
}
}
}
if progSpec == nil {
return nil, fmt.Errorf("program for section '%s' not found", load.Label)
}
pinnedMaps := make(map[string]*ebpf.Map)
for name := range refMaps {
var m *ebpf.Map
var err error
var mapPath string
if pinName, ok := load.PinMap[name]; ok {
mapPath = filepath.Join(bpfDir, pinName)
} else {
mapPath = filepath.Join(bpfDir, name)
}
m, err = ebpf.LoadPinnedMap(mapPath, nil)
if err == nil {
defer m.Close()
pinnedMaps[name] = m
} else {
logger.GetLogger().WithField("prog", load.Label).Debugf("pin file for map '%s' not found, map is not shared!\n", name)
}
}
var opts ebpf.CollectionOptions
if btfSpec != nil {
opts.Programs.KernelTypes = btfSpec
}
opts.MapReplacements = pinnedMaps
coll, err := ebpf.NewCollectionWithOptions(spec, opts)
if err != nil {
// Retry again with logging to capture the verifier log. We don't log by default
// as that makes the loading very slow.
opts.Programs.LogLevel = 1
opts.Programs.LogSize = verifierLogBufferSize
// If we hit ENOSPC that means that our log size is not big enough,
// so keep trying again with log size * 2 until we succeed or the kernel
// complains.
for {
coll, err = ebpf.NewCollectionWithOptions(spec, opts)
if errors.Is(err, unix.ENOSPC) {
opts.Programs.LogSize = opts.Programs.LogSize * 2
continue
}
break
}
if err != nil {
// Log the error directly using the logger so that the verifier log
// gets properly pretty-printed.
if verbose != 0 {
logger.GetLogger().Infof("Opening collection failed, dumping verifier log.")
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
// Print a truncated version if we have verbose=1, otherwise dump the
// full log.
if verbose < 2 {
fmt.Println(slimVerifierError(fmt.Sprintf("%+v", ve)))
} else {
fmt.Printf("%+v\n", ve)
}
}
}
return nil, fmt.Errorf("opening collection '%s' failed: %w", load.Name, err)
}
}
defer coll.Close()
err = installTailCalls(bpfDir, spec, coll, loadOpts.ci)
if err != nil {
return nil, fmt.Errorf("installing tail calls failed: %s", err)
}
for _, mapLoad := range load.MapLoad {
if m, ok := coll.Maps[mapLoad.Name]; ok {
if err := mapLoad.Load(m, mapLoad.Index); err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("populating map failed as map '%s' was not found from collection", mapLoad.Name)
}
}
prog, ok := coll.Programs[progSpec.Name]
if !ok {
return nil, fmt.Errorf("program for section '%s' not found", load.Label)
}
pinPath := filepath.Join(bpfDir, load.PinPath)
if _, err := os.Stat(pinPath); err == nil {
logger.GetLogger().Debugf("Pin file '%s' already exists, repinning", load.PinPath)
if err := os.Remove(pinPath); err != nil {
logger.GetLogger().Warnf("Unpinning '%s' failed: %s", pinPath, err)
}
}
// Clone the program so it can be passed on to attach function and unloader after
// we close the collection.
prog, err = prog.Clone()
if err != nil {
return nil, fmt.Errorf("failed to clone program '%s': %w", load.Label, err)
}
if err := prog.Pin(pinPath); err != nil {
return nil, fmt.Errorf("pinning '%s' to '%s' failed: %w", load.Label, pinPath, err)
}
load.unloader, err = loadOpts.attach(coll, spec, prog, progSpec)
if err != nil {
if err := prog.Unpin(); err != nil {
logger.GetLogger().Warnf("Unpinning '%s' failed: %w", pinPath, err)
}
return nil, err
}
// Copy the loaded collection before it's destroyed
if KeepCollection {
return copyLoadedCollection(coll)
}
return nil, nil
}
// The loadProgram loads and attach bpf object @load. It is expected that user
// provides @loadOpts with mandatory attach function and optional open function.
//
// The load process is roughly as follows:
//
// - load object | ebpf.LoadCollectionSpec
// - open callback | loadOpts.open(spec)
// - open refferenced maps |
// - creates collection | ebpf.NewCollectionWithOptions(spec, opts)
// - install tail calls | loadOpts.ci
// - load maps with values |
// - pin main program |
// - attach callback | loadOpts.attach(coll, spec, prog, progSpec)
// - print loaded progs/maps | if KeepCollection == true
//
// The @loadOpts.open callback can be used to customize ebpf.CollectionSpec
// before it's loaded into kernel (like disable/enable programs).
//
// The @loadOpts.attach callback is used to actually attach main object program
// to desired function/symbol/whatever..
//
// The @loadOpts.ci defines specific installation of tailcalls in object.
func loadProgram(
bpfDir string,
load *Program,
opts *loadOpts,
verbose int,
) error {
// Attach function is mandatory
if opts.attach == nil {
return fmt.Errorf("attach function is not provided")
}
lc, err := doLoadProgram(bpfDir, load, opts, verbose)
if err != nil {
return err
}
if KeepCollection {
load.LC = filterLoadedCollection(lc)
printLoadedCollection(load.Name, load.LC)
}
return nil
}
func LoadProgram(
bpfDir string,
load *Program,
attach AttachFunc,
verbose int,
) error {
opts := &loadOpts{attach: attach}
return loadProgram(bpfDir, load, opts, verbose)
}