This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
collection.go
275 lines (246 loc) · 6.93 KB
/
collection.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
package ebpf
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
// CollectionOptions control loading a collection into the kernel.
type CollectionOptions struct {
Programs ProgramOptions
}
// CollectionSpec describes a collection.
type CollectionSpec struct {
Maps map[string]*MapSpec
Programs map[string]*ProgramSpec
}
// Copy returns a recursive copy of the spec.
func (cs *CollectionSpec) Copy() *CollectionSpec {
if cs == nil {
return nil
}
cpy := CollectionSpec{
Maps: make(map[string]*MapSpec, len(cs.Maps)),
Programs: make(map[string]*ProgramSpec, len(cs.Programs)),
}
for name, spec := range cs.Maps {
cpy.Maps[name] = spec.Copy()
}
for name, spec := range cs.Programs {
cpy.Programs[name] = spec.Copy()
}
return &cpy
}
// LoadCollectionSpec parse an object file and convert it to a collection
func LoadCollectionSpec(file string) (*CollectionSpec, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
return LoadCollectionSpecFromReader(f)
}
// Collection is a collection of Programs and Maps associated
// with their symbols
type Collection struct {
Programs map[string]*Program
Maps map[string]*Map
}
// NewCollection creates a Collection from a specification.
//
// Only maps referenced by at least one of the programs are initialized.
func NewCollection(spec *CollectionSpec) (*Collection, error) {
return NewCollectionWithOptions(spec, CollectionOptions{})
}
// NewCollectionWithOptions creates a Collection from a specification.
//
// Only maps referenced by at least one of the programs are initialized.
func NewCollectionWithOptions(spec *CollectionSpec, opts CollectionOptions) (*Collection, error) {
maps := make(map[string]*Map)
for mapName, mapSpec := range spec.Maps {
m, err := NewMap(mapSpec)
if err != nil {
return nil, errors.Wrapf(err, "map %s", mapName)
}
maps[mapName] = m
}
progs := make(map[string]*Program)
for progName, origProgSpec := range spec.Programs {
progSpec := origProgSpec.Copy()
editor := Edit(&progSpec.Instructions)
// Rewrite any Symbol which is a valid Map.
for sym := range editor.ReferenceOffsets {
m, ok := maps[sym]
if !ok {
continue
}
// don't overwrite maps already rewritten, users can rewrite programs in the spec themselves
if err := editor.rewriteMap(sym, m, false); err != nil {
return nil, errors.Wrapf(err, "program %s", progName)
}
}
prog, err := NewProgramWithOptions(progSpec, opts.Programs)
if err != nil {
return nil, errors.Wrapf(err, "program %s", progName)
}
progs[progName] = prog
}
return &Collection{
progs,
maps,
}, nil
}
// LoadCollection parses an object file and converts it to a collection.
func LoadCollection(file string) (*Collection, error) {
spec, err := LoadCollectionSpec(file)
if err != nil {
return nil, err
}
return NewCollection(spec)
}
// Close frees all maps and programs associated with the collection.
//
// The collection mustn't be used afterwards.
func (coll *Collection) Close() {
for _, prog := range coll.Programs {
prog.Close()
}
for _, m := range coll.Maps {
m.Close()
}
}
// DetachMap removes the named map from the Collection.
//
// This means that a later call to Close() will not affect this map.
//
// Returns nil if no map of that name exists.
func (coll *Collection) DetachMap(name string) *Map {
m := coll.Maps[name]
delete(coll.Maps, name)
return m
}
// DetachProgram removes the named program from the Collection.
//
// This means that a later call to Close() will not affect this program.
//
// Returns nil if no program of that name exists.
func (coll *Collection) DetachProgram(name string) *Program {
p := coll.Programs[name]
delete(coll.Programs, name)
return p
}
// Pin persits a Collection beyond the lifetime of the process that created it
//
// This requires bpffs to be mounted above fileName. See http://cilium.readthedocs.io/en/doc-1.0/kubernetes/install/#mounting-the-bpf-fs-optional
func (coll *Collection) Pin(dirName string, fileMode os.FileMode) error {
err := mkdirIfNotExists(dirName, fileMode)
if err != nil {
return err
}
if len(coll.Maps) > 0 {
mapPath := filepath.Join(dirName, "maps")
err = mkdirIfNotExists(mapPath, fileMode)
if err != nil {
return err
}
for k, v := range coll.Maps {
err := v.Pin(filepath.Join(mapPath, k))
if err != nil {
return errors.Wrapf(err, "map %s", k)
}
}
}
if len(coll.Programs) > 0 {
progPath := filepath.Join(dirName, "programs")
err = mkdirIfNotExists(progPath, fileMode)
if err != nil {
return err
}
for k, v := range coll.Programs {
err = v.Pin(filepath.Join(progPath, k))
if err != nil {
return errors.Wrapf(err, "program %s", k)
}
}
}
return nil
}
func mkdirIfNotExists(dirName string, fileMode os.FileMode) error {
_, err := os.Stat(dirName)
if err != nil && os.IsNotExist(err) {
err = os.Mkdir(dirName, fileMode)
}
if err != nil {
return err
}
return nil
}
// LoadPinnedCollection loads a Collection from the pinned directory.
//
// Requires at least Linux 4.13, use LoadPinnedCollectionExplicit on
// earlier versions.
func LoadPinnedCollection(dirName string) (*Collection, error) {
return loadCollection(
dirName,
func(_ string, path string) (*Map, error) {
return LoadPinnedMap(path)
},
func(_ string, path string) (*Program, error) {
return LoadPinnedProgram(path)
},
)
}
// LoadPinnedCollectionExplicit loads a Collection from the pinned directory with explicit parameters.
func LoadPinnedCollectionExplicit(dirName string, maps map[string]*MapABI, progs map[string]*ProgramABI) (*Collection, error) {
return loadCollection(
dirName,
func(name string, path string) (*Map, error) {
return LoadPinnedMapExplicit(path, maps[name])
},
func(name string, path string) (*Program, error) {
return LoadPinnedProgramExplicit(path, progs[name])
},
)
}
func loadCollection(dirName string, loadMap func(string, string) (*Map, error), loadProgram func(string, string) (*Program, error)) (*Collection, error) {
maps, err := readFileNames(filepath.Join(dirName, "maps"))
if err != nil {
return nil, err
}
progs, err := readFileNames(filepath.Join(dirName, "programs"))
if err != nil {
return nil, err
}
bpfColl := &Collection{
Maps: make(map[string]*Map),
Programs: make(map[string]*Program),
}
for _, mf := range maps {
name := filepath.Base(mf)
m, err := loadMap(name, mf)
if err != nil {
return nil, errors.Wrapf(err, "map %s", name)
}
bpfColl.Maps[name] = m
}
for _, pf := range progs {
name := filepath.Base(pf)
prog, err := loadProgram(name, pf)
if err != nil {
return nil, errors.Wrapf(err, "program %s", name)
}
bpfColl.Programs[name] = prog
}
return bpfColl, nil
}
func readFileNames(dirName string) ([]string, error) {
var fileNames []string
files, err := ioutil.ReadDir(dirName)
if err != nil && err != os.ErrNotExist {
return nil, err
}
for _, fi := range files {
fileNames = append(fileNames, fi.Name())
}
return fileNames, nil
}