github.com/metacubex/mihomo@v1.18.5/component/ebpf/tc/bpf_bpfel.go (about) 1 // 2 //go:build 386 || amd64 || amd64p32 || arm || arm64 || mips64le || mips64p32le || mipsle || ppc64le || riscv64 || loong64 3 // +build 386 amd64 amd64p32 arm arm64 mips64le mips64p32le mipsle ppc64le riscv64 loong64 4 5 package tc 6 7 import ( 8 "bytes" 9 _ "embed" 10 "fmt" 11 "io" 12 13 "github.com/cilium/ebpf" 14 ) 15 16 // loadBpf returns the embedded CollectionSpec for bpf. 17 func loadBpf() (*ebpf.CollectionSpec, error) { 18 reader := bytes.NewReader(_BpfBytes) 19 spec, err := ebpf.LoadCollectionSpecFromReader(reader) 20 if err != nil { 21 return nil, fmt.Errorf("can't load bpf: %w", err) 22 } 23 24 return spec, err 25 } 26 27 // loadBpfObjects loads bpf and converts it into a struct. 28 // 29 // The following types are suitable as obj argument: 30 // 31 // *bpfObjects 32 // *bpfPrograms 33 // *bpfMaps 34 // 35 // See ebpf.CollectionSpec.LoadAndAssign documentation for details. 36 func loadBpfObjects(obj interface{}, opts *ebpf.CollectionOptions) error { 37 spec, err := loadBpf() 38 if err != nil { 39 return err 40 } 41 42 return spec.LoadAndAssign(obj, opts) 43 } 44 45 // bpfSpecs contains maps and programs before they are loaded into the kernel. 46 // 47 // It can be passed ebpf.CollectionSpec.Assign. 48 type bpfSpecs struct { 49 bpfProgramSpecs 50 bpfMapSpecs 51 } 52 53 // bpfSpecs contains programs before they are loaded into the kernel. 54 // 55 // It can be passed ebpf.CollectionSpec.Assign. 56 type bpfProgramSpecs struct { 57 TcTunFunc *ebpf.ProgramSpec `ebpf:"tc_tun_func"` 58 } 59 60 // bpfMapSpecs contains maps before they are loaded into the kernel. 61 // 62 // It can be passed ebpf.CollectionSpec.Assign. 63 type bpfMapSpecs struct { 64 TcParamsMap *ebpf.MapSpec `ebpf:"tc_params_map"` 65 } 66 67 // bpfObjects contains all objects after they have been loaded into the kernel. 68 // 69 // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 70 type bpfObjects struct { 71 bpfPrograms 72 bpfMaps 73 } 74 75 func (o *bpfObjects) Close() error { 76 return _BpfClose( 77 &o.bpfPrograms, 78 &o.bpfMaps, 79 ) 80 } 81 82 // bpfMaps contains all maps after they have been loaded into the kernel. 83 // 84 // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 85 type bpfMaps struct { 86 TcParamsMap *ebpf.Map `ebpf:"tc_params_map"` 87 } 88 89 func (m *bpfMaps) Close() error { 90 return _BpfClose( 91 m.TcParamsMap, 92 ) 93 } 94 95 // bpfPrograms contains all programs after they have been loaded into the kernel. 96 // 97 // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 98 type bpfPrograms struct { 99 TcTunFunc *ebpf.Program `ebpf:"tc_tun_func"` 100 } 101 102 func (p *bpfPrograms) Close() error { 103 return _BpfClose( 104 p.TcTunFunc, 105 ) 106 } 107 108 func _BpfClose(closers ...io.Closer) error { 109 for _, closer := range closers { 110 if err := closer.Close(); err != nil { 111 return err 112 } 113 } 114 return nil 115 } 116 117 // Do not access this directly. 118 // 119 //go:embed bpf_bpfel.o 120 var _BpfBytes []byte