Debian Codegraph @

cmd/bpf2go/gen/output.tpl

raw
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 {{- if .NeedsStructsPkg }}
12 "structs"
13 {{- end }}
14
15 "{{ .Module }}"
16 )
17
18 {{- if .TypeDeclarations }}
19 {{- range $type := .TypeDeclarations }}
20 {{ $type }}
21
22 {{ end }}
23 {{- end }}
24
25 {{- if or .Maps (or .Variables .Programs) }}
26 // Names of all BPF objects in the ELF.
27 //
28 // Used for safe lookups in a Collection or CollectionSpec.
29 const (
30 {{- range $name, $id := .Maps }}
31 {{ $.Name }}Map{{ $id }} = "{{ $name }}"
32 {{- end }}
33 {{- range $name, $id := .Programs }}
34 {{ $.Name }}Prog{{ $id }} = "{{ $name }}"
35 {{- end }}
36 {{- range $name, $id := .Variables }}
37 {{ $.Name }}Var{{ $id }} = "{{ $name }}"
38 {{- end }}
39 )
40 {{- end }}
41
42 // {{ .Name.Load }} returns the embedded CollectionSpec for {{ .Name }}.
43 func {{ .Name.Load }}() (*ebpf.CollectionSpec, error) {
44 reader := bytes.NewReader({{ .Name.Bytes }})
45 spec, err := ebpf.LoadCollectionSpecFromReader(reader)
46 if err != nil {
47 return nil, fmt.Errorf("can't load {{ .Name }}: %w", err)
48 }
49
50 return spec, err
51 }
52
53 // {{ .Name.LoadObjects }} loads {{ .Name }} and converts it into a struct.
54 //
55 // The following types are suitable as obj argument:
56 //
57 // *{{ .Name.Objects }}
58 // *{{ .Name.Programs }}
59 // *{{ .Name.Maps }}
60 //
61 // See ebpf.CollectionSpec.LoadAndAssign documentation for details.
62 func {{ .Name.LoadObjects }}(obj any, opts *ebpf.CollectionOptions) (error) {
63 spec, err := {{ .Name.Load }}()
64 if err != nil {
65 return err
66 }
67
68 return spec.LoadAndAssign(obj, opts)
69 }
70
71 // {{ .Name.Specs }} contains maps and programs before they are loaded into the kernel.
72 //
73 // It can be passed ebpf.CollectionSpec.Assign.
74 type {{ .Name.Specs }} struct {
75 {{ .Name.ProgramSpecs }}
76 {{ .Name.MapSpecs }}
77 {{ .Name.VariableSpecs }}
78 }
79
80 // {{ .Name.ProgramSpecs }} contains programs before they are loaded into the kernel.
81 //
82 // It can be passed ebpf.CollectionSpec.Assign.
83 type {{ .Name.ProgramSpecs }} struct {
84 {{- range $name, $id := .Programs }}
85 {{ $id }} *ebpf.ProgramSpec `ebpf:"{{ $name }}"`
86 {{- end }}
87 }
88
89 // {{ .Name.MapSpecs }} contains maps before they are loaded into the kernel.
90 //
91 // It can be passed ebpf.CollectionSpec.Assign.
92 type {{ .Name.MapSpecs }} struct {
93 {{- range $name, $id := .Maps }}
94 {{ $id }} *ebpf.MapSpec `ebpf:"{{ $name }}"`
95 {{- end }}
96 }
97
98 // {{ .Name.VariableSpecs }} contains global variables before they are loaded into the kernel.
99 //
100 // It can be passed ebpf.CollectionSpec.Assign.
101 type {{ .Name.VariableSpecs }} struct {
102 {{- range $name, $id := .Variables }}
103 {{ $id }} *ebpf.VariableSpec `ebpf:"{{ $name }}"`
104 {{- end }}
105 }
106
107 // {{ .Name.Objects }} contains all objects after they have been loaded into the kernel.
108 //
109 // It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
110 type {{ .Name.Objects }} struct {
111 {{ .Name.Programs }}
112 {{ .Name.Maps }}
113 {{ .Name.Variables }}
114 }
115
116 func (o *{{ .Name.Objects }}) Close() error {
117 return {{ .Name.CloseHelper }}(
118 &o.{{ .Name.Programs }},
119 &o.{{ .Name.Maps }},
120 )
121 }
122
123 // {{ .Name.Maps }} contains all maps after they have been loaded into the kernel.
124 //
125 // It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
126 type {{ .Name.Maps }} struct {
127 {{- range $name, $id := .Maps }}
128 {{ $id }} *ebpf.Map `ebpf:"{{ $name }}"`
129 {{- end }}
130 }
131
132 func (m *{{ .Name.Maps }}) Close() error {
133 return {{ .Name.CloseHelper }}(
134 {{- range $id := .Maps }}
135 m.{{ $id }},
136 {{- end }}
137 )
138 }
139
140 // {{ .Name.Variables }} contains all global variables after they have been loaded into the kernel.
141 //
142 // It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
143 type {{ .Name.Variables }} struct {
144 {{- range $name, $id := .Variables }}
145 {{ $id }} *ebpf.Variable `ebpf:"{{ $name }}"`
146 {{- end }}
147 }
148
149 // {{ .Name.Programs }} contains all programs after they have been loaded into the kernel.
150 //
151 // It can be passed to {{ .Name.LoadObjects }} or ebpf.CollectionSpec.LoadAndAssign.
152 type {{ .Name.Programs }} struct {
153 {{- range $name, $id := .Programs }}
154 {{ $id }} *ebpf.Program `ebpf:"{{ $name }}"`
155 {{- end }}
156 }
157
158 func (p *{{ .Name.Programs }}) Close() error {
159 return {{ .Name.CloseHelper }}(
160 {{- range $id := .Programs }}
161 p.{{ $id }},
162 {{- end }}
163 )
164 }
165
166 func {{ .Name.CloseHelper }}(closers ...io.Closer) error {
167 for _, closer := range closers {
168 if err := closer.Close(); err != nil {
169 return err
170 }
171 }
172 return nil
173 }
174
175 // Do not access this directly.
176 //go:embed {{ .File }}
177 var {{ .Name.Bytes }} []byte