github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/cmd/bpf2go/output.tpl (about) 1 // Code generated by bpf2go; DO NOT EDIT. 2 {{ with .Constraints }}//go:build {{ . }}{{ end }} 3 4 package {{ .Package }} 5 6 import ( 7 "bytes" 8 _ "embed" 9 "fmt" 10 "io" 11 12 "{{ .Module }}" 13 ) 14 15 {{- if .Types }} 16 {{- range $type := .Types }} 17 {{ $.TypeDeclaration (index $.TypeNames $type) $type }} 18 19 {{ end }} 20 {{- end }} 21 22 // {{ .Name.Load }} returns the embedded CollectionSpec for {{ .Name }}. 23 func {{ .Name.Load }}() (*ebpf.CollectionSpec, error) { 24 reader := bytes.NewReader({{ .Name.Bytes }}) 25 spec, err := ebpf.LoadCollectionSpecFromReader(reader) 26 if err != nil { 27 return nil, fmt.Errorf("can't load {{ .Name }}: %w", err) 28 } 29 30 return spec, err 31 } 32 33 // {{ .Name.LoadObjects }} loads {{ .Name }} and converts it into a struct. 34 // 35 // The following types are suitable as obj argument: 36 // 37 // *{{ .Name.Objects }} 38 // *{{ .Name.Programs }} 39 // *{{ .Name.Maps }} 40 // 41 // See ebpf.CollectionSpec.LoadAndAssign documentation for details. 42 func {{ .Name.LoadObjects }}(obj interface{}, opts *ebpf.CollectionOptions) (error) { 43 spec, err := {{ .Name.Load }}() 44 if err != nil { 45 return err 46 } 47 48 return spec.LoadAndAssign(obj, opts) 49 } 50 51 // {{ .Name.Specs }} contains maps and programs before they are loaded into the kernel. 52 // 53 // It can be passed ebpf.CollectionSpec.Assign. 54 type {{ .Name.Specs }} struct { 55 {{ .Name.ProgramSpecs }} 56 {{ .Name.MapSpecs }} 57 } 58 59 // {{ .Name.Specs }} contains programs before they are loaded into the kernel. 60 // 61 // It can be passed ebpf.CollectionSpec.Assign. 62 type {{ .Name.ProgramSpecs }} struct { 63 {{- range $name, $id := .Programs }} 64 {{ $id }} *ebpf.ProgramSpec `ebpf:"{{ $name }}"` 65 {{- end }} 66 } 67 68 // {{ .Name.MapSpecs }} contains maps before they are loaded into the kernel. 69 // 70 // It can be passed ebpf.CollectionSpec.Assign. 71 type {{ .Name.MapSpecs }} struct { 72 {{- range $name, $id := .Maps }} 73 {{ $id }} *ebpf.MapSpec `ebpf:"{{ $name }}"` 74 {{- end }} 75 } 76 77 // {{ .Name.Objects }} contains all objects after they have been loaded into the kernel. 78 // 79 // It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign. 80 type {{ .Name.Objects }} struct { 81 {{ .Name.Programs }} 82 {{ .Name.Maps }} 83 } 84 85 func (o *{{ .Name.Objects }}) Close() error { 86 return {{ .Name.CloseHelper }}( 87 &o.{{ .Name.Programs }}, 88 &o.{{ .Name.Maps }}, 89 ) 90 } 91 92 // {{ .Name.Maps }} contains all maps after they have been loaded into the kernel. 93 // 94 // It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign. 95 type {{ .Name.Maps }} struct { 96 {{- range $name, $id := .Maps }} 97 {{ $id }} *ebpf.Map `ebpf:"{{ $name }}"` 98 {{- end }} 99 } 100 101 func (m *{{ .Name.Maps }}) Close() error { 102 return {{ .Name.CloseHelper }}( 103 {{- range $id := .Maps }} 104 m.{{ $id }}, 105 {{- end }} 106 ) 107 } 108 109 // {{ .Name.Programs }} contains all programs after they have been loaded into the kernel. 110 // 111 // It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign. 112 type {{ .Name.Programs }} struct { 113 {{- range $name, $id := .Programs }} 114 {{ $id }} *ebpf.Program `ebpf:"{{ $name }}"` 115 {{- end }} 116 } 117 118 func (p *{{ .Name.Programs }}) Close() error { 119 return {{ .Name.CloseHelper }}( 120 {{- range $id := .Programs }} 121 p.{{ $id }}, 122 {{- end }} 123 ) 124 } 125 126 func {{ .Name.CloseHelper }}(closers ...io.Closer) error { 127 for _, closer := range closers { 128 if err := closer.Close(); err != nil { 129 return err 130 } 131 } 132 return nil 133 } 134 135 // Do not access this directly. 136 //go:embed {{ .File }} 137 var {{ .Name.Bytes }} []byte