/
main.go
730 lines (641 loc) · 17.2 KB
/
main.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
// Program gentypes reads a compressed vmlinux .BTF section and generates
// syscall bindings from it.
//
// Output is written to "types.go".
package main
import (
"bytes"
"errors"
"fmt"
"os"
"sort"
"strings"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/sys"
)
type syscallRetval int
const (
retError syscallRetval = iota
retFd
)
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}
func run(args []string) error {
if len(args) != 1 {
return fmt.Errorf("expect location of compressed vmlinux .BTF as argument")
}
raw, err := internal.ReadAllCompressed(args[0])
if err != nil {
return err
}
spec, err := btf.LoadSpecFromReader(bytes.NewReader(raw))
if err != nil {
return err
}
output, err := generateTypes(spec)
if err != nil {
return err
}
w, err := os.Create("types.go")
if err != nil {
return err
}
defer w.Close()
return internal.WriteFormatted(output, w)
}
func generateTypes(spec *btf.Spec) ([]byte, error) {
objName := &btf.Array{Nelems: 16, Type: &btf.Int{Encoding: btf.Char, Size: 1}}
linkID := &btf.Int{Size: 4}
btfID := &btf.Int{Size: 4}
pointer := &btf.Int{Size: 8}
// Pre-declare handwritten types sys.ObjName and sys.Pointer so that
// generated types can refer to them.
var (
_ sys.Pointer
_ sys.ObjName
_ sys.LinkID
)
gf := &btf.GoFormatter{
Names: map[btf.Type]string{
objName: "ObjName",
linkID: "LinkID",
btfID: "BTFID",
pointer: "Pointer",
},
Identifier: internal.Identifier,
EnumIdentifier: func(name, element string) string {
return element
},
}
w := bytes.NewBuffer(nil)
w.WriteString(`// Code generated by internal/cmd/gentypes; DO NOT EDIT.
package sys
import (
"unsafe"
)
`)
enums := []struct {
goType string
cType string
}{
{"Cmd", "bpf_cmd"},
{"MapType", "bpf_map_type"},
{"ProgType", "bpf_prog_type"},
{"AttachType", "bpf_attach_type"},
{"LinkType", "bpf_link_type"},
{"StatsType", "bpf_stats_type"},
{"SkAction", "sk_action"},
{"StackBuildIdStatus", "bpf_stack_build_id_status"},
{"FunctionId", "bpf_func_id"},
{"AdjRoomMode", "bpf_adj_room_mode"},
{"HdrStartOff", "bpf_hdr_start_off"},
{"RetCode", "bpf_ret_code"},
{"XdpAction", "xdp_action"},
}
sort.Slice(enums, func(i, j int) bool {
return enums[i].goType < enums[j].goType
})
enumTypes := make(map[string]btf.Type)
for _, o := range enums {
fmt.Println("enum", o.goType)
var t *btf.Enum
if err := spec.TypeByName(o.cType, &t); err != nil {
return nil, err
}
// Add the enum as a predeclared type so that generated structs
// refer to the Go types.
if name := gf.Names[t]; name != "" {
return nil, fmt.Errorf("type %q is already declared as %s", o.cType, name)
}
gf.Names[t] = o.goType
enumTypes[o.goType] = t
decl, err := gf.TypeDeclaration(o.goType, t)
if err != nil {
return nil, fmt.Errorf("generate %q: %w", o.goType, err)
}
w.WriteString(decl)
w.WriteRune('\n')
}
// Assorted structs
structs := []struct {
goType string
cType string
patches []patch
}{
{
"ProgInfo", "bpf_prog_info",
[]patch{
replace(objName, "name"),
replace(pointer, "xlated_prog_insns"),
replace(pointer, "map_ids"),
},
},
{
"MapInfo", "bpf_map_info",
[]patch{replace(objName, "name")},
},
{
"BtfInfo", "bpf_btf_info",
[]patch{
replace(pointer, "btf", "name"),
replace(btfID, "id"),
},
},
{
"LinkInfo", "bpf_link_info",
[]patch{
replace(enumTypes["LinkType"], "type"),
replace(linkID, "id"),
name(3, "extra"),
replaceWithBytes("extra"),
},
},
{"FuncInfo", "bpf_func_info", nil},
{"LineInfo", "bpf_line_info", nil},
{"XdpMd", "xdp_md", nil},
{
"SkLookup", "bpf_sk_lookup",
[]patch{
choose(0, "cookie"),
replaceWithBytes("remote_ip4", "remote_ip6", "local_ip4", "local_ip6"),
},
},
}
sort.Slice(structs, func(i, j int) bool {
return structs[i].goType < structs[j].goType
})
for _, s := range structs {
fmt.Println("struct", s.goType)
var t *btf.Struct
if err := spec.TypeByName(s.cType, &t); err != nil {
return nil, err
}
if err := outputPatchedStruct(gf, w, s.goType, t, s.patches); err != nil {
return nil, fmt.Errorf("output %q: %w", s.goType, err)
}
}
// Attrs
attrs := []struct {
goType string
ret syscallRetval
cType string
cmd string
patches []patch
}{
{
"MapCreate", retFd, "map_create", "BPF_MAP_CREATE",
[]patch{
replace(objName, "map_name"),
replace(enumTypes["MapType"], "map_type"),
},
},
{
"MapLookupElem", retError, "map_elem", "BPF_MAP_LOOKUP_ELEM",
[]patch{choose(2, "value"), replace(pointer, "key", "value")},
},
{
"MapLookupAndDeleteElem", retError, "map_elem", "BPF_MAP_LOOKUP_AND_DELETE_ELEM",
[]patch{choose(2, "value"), replace(pointer, "key", "value")},
},
{
"MapUpdateElem", retError, "map_elem", "BPF_MAP_UPDATE_ELEM",
[]patch{choose(2, "value"), replace(pointer, "key", "value")},
},
{
"MapDeleteElem", retError, "map_elem", "BPF_MAP_DELETE_ELEM",
[]patch{choose(2, "value"), replace(pointer, "key", "value")},
},
{
"MapGetNextKey", retError, "map_elem", "BPF_MAP_GET_NEXT_KEY",
[]patch{
choose(2, "next_key"), replace(pointer, "key", "next_key"),
truncateAfter("next_key"),
},
},
{
"MapFreeze", retError, "map_elem", "BPF_MAP_FREEZE",
[]patch{truncateAfter("map_fd")},
},
{
"MapLookupBatch", retError, "map_elem_batch", "BPF_MAP_LOOKUP_BATCH",
[]patch{replace(pointer, "in_batch", "out_batch", "keys", "values")},
},
{
"MapLookupAndDeleteBatch", retError, "map_elem_batch", "BPF_MAP_LOOKUP_AND_DELETE_BATCH",
[]patch{replace(pointer, "in_batch", "out_batch", "keys", "values")},
},
{
"MapUpdateBatch", retError, "map_elem_batch", "BPF_MAP_UPDATE_BATCH",
[]patch{replace(pointer, "in_batch", "out_batch", "keys", "values")},
},
{
"MapDeleteBatch", retError, "map_elem_batch", "BPF_MAP_DELETE_BATCH",
[]patch{replace(pointer, "in_batch", "out_batch", "keys", "values")},
},
{
"ProgLoad", retFd, "prog_load", "BPF_PROG_LOAD",
[]patch{
replace(objName, "prog_name"),
replace(enumTypes["ProgType"], "prog_type"),
replace(enumTypes["AttachType"], "expected_attach_type"),
replace(pointer,
"insns",
"license",
"log_buf",
"func_info",
"line_info",
"fd_array",
"core_relos",
),
choose(20, "attach_btf_obj_fd"),
},
},
{
"ProgBindMap", retError, "prog_bind_map", "BPF_PROG_BIND_MAP",
nil,
},
{
"ObjPin", retError, "obj_pin", "BPF_OBJ_PIN",
[]patch{replace(pointer, "pathname")},
},
{
"ObjGet", retFd, "obj_pin", "BPF_OBJ_GET",
[]patch{replace(pointer, "pathname")},
},
{
"ProgAttach", retError, "prog_attach", "BPF_PROG_ATTACH",
nil,
},
{
"ProgDetach", retError, "prog_attach", "BPF_PROG_DETACH",
[]patch{truncateAfter("attach_type")},
},
{
"ProgRun", retError, "prog_run", "BPF_PROG_TEST_RUN",
[]patch{replace(pointer, "data_in", "data_out", "ctx_in", "ctx_out")},
},
{
"ProgGetNextId", retError, "obj_next_id", "BPF_PROG_GET_NEXT_ID",
[]patch{
choose(0, "start_id"), rename("start_id", "id"),
truncateAfter("next_id"),
},
},
{
"MapGetNextId", retError, "obj_next_id", "BPF_MAP_GET_NEXT_ID",
[]patch{
choose(0, "start_id"), rename("start_id", "id"),
truncateAfter("next_id"),
},
},
{
"BtfGetNextId", retError, "obj_next_id", "BPF_BTF_GET_NEXT_ID",
[]patch{
choose(0, "start_id"), rename("start_id", "id"),
replace(btfID, "id", "next_id"),
truncateAfter("next_id"),
},
},
// These piggy back on the obj_next_id decl, but only support the
// first field...
{
"BtfGetFdById", retFd, "obj_next_id", "BPF_BTF_GET_FD_BY_ID",
[]patch{choose(0, "start_id"), rename("start_id", "id"), truncateAfter("id")},
},
{
"MapGetFdById", retFd, "obj_next_id", "BPF_MAP_GET_FD_BY_ID",
[]patch{choose(0, "start_id"), rename("start_id", "id"), truncateAfter("id")},
},
{
"ProgGetFdById", retFd, "obj_next_id", "BPF_PROG_GET_FD_BY_ID",
[]patch{choose(0, "start_id"), rename("start_id", "id"), truncateAfter("id")},
},
{
"ObjGetInfoByFd", retError, "info_by_fd", "BPF_OBJ_GET_INFO_BY_FD",
[]patch{replace(pointer, "info")},
},
{
"RawTracepointOpen", retFd, "raw_tracepoint_open", "BPF_RAW_TRACEPOINT_OPEN",
[]patch{replace(pointer, "name")},
},
{
"BtfLoad", retFd, "btf_load", "BPF_BTF_LOAD",
[]patch{replace(pointer, "btf", "btf_log_buf")},
},
{
"LinkCreate", retFd, "link_create", "BPF_LINK_CREATE",
[]patch{replace(enumTypes["AttachType"], "attach_type")},
},
{
"LinkCreateIter", retFd, "link_create", "BPF_LINK_CREATE",
[]patch{
chooseNth(4, 1),
replace(enumTypes["AttachType"], "attach_type"),
flattenAnon,
replace(pointer, "iter_info"),
},
},
{
"LinkCreatePerfEvent", retFd, "link_create", "BPF_LINK_CREATE",
[]patch{
chooseNth(4, 2),
replace(enumTypes["AttachType"], "attach_type"),
flattenAnon,
},
},
{
"LinkUpdate", retError, "link_update", "BPF_LINK_UPDATE",
nil,
},
{
"EnableStats", retFd, "enable_stats", "BPF_ENABLE_STATS",
nil,
},
{
"IterCreate", retFd, "iter_create", "BPF_ITER_CREATE",
nil,
},
}
sort.Slice(attrs, func(i, j int) bool {
return attrs[i].goType < attrs[j].goType
})
var bpfAttr *btf.Union
if err := spec.TypeByName("bpf_attr", &bpfAttr); err != nil {
return nil, err
}
attrTypes, err := splitUnion(bpfAttr, types{
{"map_create", "map_type"},
{"map_elem", "map_fd"},
{"map_elem_batch", "batch"},
{"prog_load", "prog_type"},
{"obj_pin", "pathname"},
{"prog_attach", "target_fd"},
{"prog_run", "test"},
{"obj_next_id", ""},
{"info_by_fd", "info"},
{"prog_query", "query"},
{"raw_tracepoint_open", "raw_tracepoint"},
{"btf_load", "btf"},
{"task_fd_query", "task_fd_query"},
{"link_create", "link_create"},
{"link_update", "link_update"},
{"link_detach", "link_detach"},
{"enable_stats", "enable_stats"},
{"iter_create", "iter_create"},
{"prog_bind_map", "prog_bind_map"},
})
if err != nil {
return nil, fmt.Errorf("splitting bpf_attr: %w", err)
}
for _, s := range attrs {
fmt.Println("attr", s.goType)
t := attrTypes[s.cType]
if t == nil {
return nil, fmt.Errorf("unknown attr %q", s.cType)
}
goAttrType := s.goType + "Attr"
if err := outputPatchedStruct(gf, w, goAttrType, t, s.patches); err != nil {
return nil, fmt.Errorf("output %q: %w", goAttrType, err)
}
switch s.ret {
case retError:
fmt.Fprintf(w, "func %s(attr *%s) error { _, err := BPF(%s, unsafe.Pointer(attr), unsafe.Sizeof(*attr)); return err }\n\n", s.goType, goAttrType, s.cmd)
case retFd:
fmt.Fprintf(w, "func %s(attr *%s) (*FD, error) { fd, err := BPF(%s, unsafe.Pointer(attr), unsafe.Sizeof(*attr)); if err != nil { return nil, err }; return NewFD(int(fd)) }\n\n", s.goType, goAttrType, s.cmd)
}
}
// Link info type specific
linkInfoExtraTypes := []struct {
goType string
cType string
patches []patch
}{
{"CgroupLinkInfo", "cgroup", []patch{replace(enumTypes["AttachType"], "attach_type")}},
{"IterLinkInfo", "iter", []patch{replace(pointer, "target_name"), truncateAfter("target_name_len")}},
{"NetNsLinkInfo", "netns", []patch{replace(enumTypes["AttachType"], "attach_type")}},
{"RawTracepointLinkInfo", "raw_tracepoint", []patch{replace(pointer, "tp_name")}},
{"TracingLinkInfo", "tracing", []patch{replace(enumTypes["AttachType"], "attach_type")}},
{"XDPLinkInfo", "xdp", nil},
}
sort.Slice(linkInfoExtraTypes, func(i, j int) bool {
return linkInfoExtraTypes[i].goType < linkInfoExtraTypes[j].goType
})
var bpfLinkInfo *btf.Struct
if err := spec.TypeByName("bpf_link_info", &bpfLinkInfo); err != nil {
return nil, err
}
member := bpfLinkInfo.Members[len(bpfLinkInfo.Members)-1]
bpfLinkInfoUnion, ok := member.Type.(*btf.Union)
if !ok {
return nil, fmt.Errorf("there is not type-specific union")
}
linkInfoTypes, err := splitUnion(bpfLinkInfoUnion, types{
{"raw_tracepoint", "raw_tracepoint"},
{"tracing", "tracing"},
{"cgroup", "cgroup"},
{"iter", "iter"},
{"netns", "netns"},
{"xdp", "xdp"},
})
if err != nil {
return nil, fmt.Errorf("splitting linkInfo: %w", err)
}
for _, s := range linkInfoExtraTypes {
t := linkInfoTypes[s.cType]
if err := outputPatchedStruct(gf, w, s.goType, t, s.patches); err != nil {
return nil, fmt.Errorf("output %q: %w", s.goType, err)
}
}
return w.Bytes(), nil
}
func outputPatchedStruct(gf *btf.GoFormatter, w *bytes.Buffer, id string, s *btf.Struct, patches []patch) error {
s = btf.Copy(s, nil).(*btf.Struct)
for i, p := range patches {
if err := p(s); err != nil {
return fmt.Errorf("patch %d: %w", i, err)
}
}
decl, err := gf.TypeDeclaration(id, s)
if err != nil {
return err
}
w.WriteString(decl)
w.WriteString("\n\n")
return nil
}
type types []struct {
name string
cFieldOrFirstMember string
}
func splitUnion(union *btf.Union, types types) (map[string]*btf.Struct, error) {
structs := make(map[string]*btf.Struct)
for i, t := range types {
member := union.Members[i]
s, ok := member.Type.(*btf.Struct)
if !ok {
return nil, fmt.Errorf("%q: %s is not a struct", t.name, member.Type)
}
if member.Name == "" {
// This is an anonymous struct, check the name of the first member instead.
if name := s.Members[0].Name; name != t.cFieldOrFirstMember {
return nil, fmt.Errorf("first field of %q is %q, not %q", t.name, name, t.cFieldOrFirstMember)
}
} else if member.Name != t.cFieldOrFirstMember {
return nil, fmt.Errorf("name for %q is %q, not %q", t.name, member.Name, t.cFieldOrFirstMember)
}
structs[t.name] = s
}
return structs, nil
}
type patch func(*btf.Struct) error
func modify(fn func(*btf.Member) error, members ...string) patch {
return func(s *btf.Struct) error {
want := make(map[string]bool)
for _, name := range members {
want[name] = true
}
for i, m := range s.Members {
if want[m.Name] {
if err := fn(&s.Members[i]); err != nil {
return err
}
delete(want, m.Name)
}
}
if len(want) == 0 {
return nil
}
var missing []string
for name := range want {
missing = append(missing, name)
}
sort.Strings(missing)
return fmt.Errorf("missing members: %v", strings.Join(missing, ", "))
}
}
func modifyNth(fn func(*btf.Member) error, indices ...int) patch {
return func(s *btf.Struct) error {
for _, i := range indices {
if i >= len(s.Members) {
return fmt.Errorf("index %d is out of bounds", i)
}
if err := fn(&s.Members[i]); err != nil {
return fmt.Errorf("member #%d: %w", i, err)
}
}
return nil
}
}
func replace(t btf.Type, members ...string) patch {
return modify(func(m *btf.Member) error {
m.Type = t
return nil
}, members...)
}
func choose(member int, name string) patch {
return modifyNth(func(m *btf.Member) error {
union, ok := m.Type.(*btf.Union)
if !ok {
return fmt.Errorf("member %d is %s, not a union", member, m.Type)
}
for _, um := range union.Members {
if um.Name == name {
m.Name = um.Name
m.Type = um.Type
return nil
}
}
return fmt.Errorf("%s has no member %q", union, name)
}, member)
}
func chooseNth(member int, n int) patch {
return modifyNth(func(m *btf.Member) error {
union, ok := m.Type.(*btf.Union)
if !ok {
return fmt.Errorf("member %d is %s, not a union", member, m.Type)
}
if n >= len(union.Members) {
return fmt.Errorf("member %d is out of bounds", n)
}
um := union.Members[n]
m.Name = um.Name
m.Type = um.Type
return nil
}, member)
}
func flattenAnon(s *btf.Struct) error {
for i := range s.Members {
m := &s.Members[i]
cs, ok := m.Type.(*btf.Struct)
if !ok || cs.TypeName() != "" {
continue
}
for j := range cs.Members {
cs.Members[j].Offset += m.Offset
}
newMembers := make([]btf.Member, 0, len(s.Members)+len(cs.Members)-1)
newMembers = append(newMembers, s.Members[:i]...)
newMembers = append(newMembers, cs.Members...)
newMembers = append(newMembers, s.Members[i+1:]...)
s.Members = newMembers
}
return nil
}
func truncateAfter(name string) patch {
return func(s *btf.Struct) error {
for i, m := range s.Members {
if m.Name != name {
continue
}
size, err := btf.Sizeof(m.Type)
if err != nil {
return err
}
s.Members = s.Members[:i+1]
s.Size = m.Offset.Bytes() + uint32(size)
return nil
}
return fmt.Errorf("no member %q", name)
}
}
func rename(from, to string) patch {
return func(s *btf.Struct) error {
for i, m := range s.Members {
if m.Name == from {
s.Members[i].Name = to
return nil
}
}
return fmt.Errorf("no member named %q", from)
}
}
func name(member int, name string) patch {
return modifyNth(func(m *btf.Member) error {
if m.Name != "" {
return fmt.Errorf("member already has name %q", m.Name)
}
m.Name = name
return nil
}, member)
}
func replaceWithBytes(members ...string) patch {
return modify(func(m *btf.Member) error {
if m.BitfieldSize != 0 {
return errors.New("replaceWithBytes: member is a bitfield")
}
size, err := btf.Sizeof(m.Type)
if err != nil {
return fmt.Errorf("replaceWithBytes: size of %s: %w", m.Type, err)
}
m.Type = &btf.Array{
Type: &btf.Int{Size: 1},
Nelems: uint32(size),
}
return nil
}, members...)
}