Skip to content

Commit

Permalink
btf/cmd/genbtftypes: (cilium#1218) a generator that generates btf_typ…
Browse files Browse the repository at this point in the history
…es.go structs.

This is guaranteed to match the kernel struct defenitions.
  • Loading branch information
OmarTariq612 committed Jan 10, 2024
1 parent 87531ae commit 644f800
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 64 deletions.
47 changes: 47 additions & 0 deletions btf/btf_gen_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 0 additions & 64 deletions btf/btf_types.go
Expand Up @@ -131,28 +131,6 @@ func parseBTFHeader(r io.Reader, bo binary.ByteOrder) (*btfHeader, error) {

var btfTypeLen = binary.Size(btfType{})

// btfType is equivalent to struct btf_type in Documentation/bpf/btf.rst.
type btfType struct {
NameOff uint32
/* "info" bits arrangement
* bits 0-15: vlen (e.g. # of struct's members), linkage
* bits 16-23: unused
* bits 24-28: kind (e.g. int, ptr, array...etc)
* bits 29-30: unused
* bit 31: kind_flag, currently used by
* struct, union and fwd
*/
Info uint32
/* "size" is used by INT, ENUM, STRUCT and UNION.
* "size" tells the size of the type it is describing.
*
* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
* FUNC and FUNC_PROTO.
* "type" is a type_id referring to another type.
*/
SizeType uint32
}

var btfTypeSize = int(unsafe.Sizeof(btfType{}))

func unmarshalBtfType(bt *btfType, b []byte, bo binary.ByteOrder) (int, error) {
Expand Down Expand Up @@ -348,12 +326,6 @@ func (bi *btfInt) SetBits(bits byte) {
bi.Raw = writeBits(bi.Raw, btfIntBitsLen, btfIntBitsShift, uint32(bits))
}

type btfArray struct {
Type TypeID
IndexType TypeID
Nelems uint32
}

var btfArrayLen = int(unsafe.Sizeof(btfArray{}))

func unmarshalBtfArray(ba *btfArray, b []byte, bo binary.ByteOrder) (int, error) {
Expand All @@ -367,12 +339,6 @@ func unmarshalBtfArray(ba *btfArray, b []byte, bo binary.ByteOrder) (int, error)
return btfArrayLen, nil
}

type btfMember struct {
NameOff uint32
Type TypeID
Offset uint32
}

var btfMemberLen = int(unsafe.Sizeof(btfMember{}))

func unmarshalBtfMembers(members []btfMember, b []byte, bo binary.ByteOrder) (int, error) {
Expand All @@ -392,12 +358,6 @@ func unmarshalBtfMembers(members []btfMember, b []byte, bo binary.ByteOrder) (in
return off, nil
}

type btfVarSecinfo struct {
Type TypeID
Offset uint32
Size uint32
}

var btfVarSecinfoLen = int(unsafe.Sizeof(btfVarSecinfo{}))

func unmarshalBtfVarSecInfos(secinfos []btfVarSecinfo, b []byte, bo binary.ByteOrder) (int, error) {
Expand All @@ -417,10 +377,6 @@ func unmarshalBtfVarSecInfos(secinfos []btfVarSecinfo, b []byte, bo binary.ByteO
return off, nil
}

type btfVariable struct {
Linkage uint32
}

var btfVariableLen = int(unsafe.Sizeof(btfVariable{}))

func unmarshalBtfVariable(bv *btfVariable, b []byte, bo binary.ByteOrder) (int, error) {
Expand All @@ -432,11 +388,6 @@ func unmarshalBtfVariable(bv *btfVariable, b []byte, bo binary.ByteOrder) (int,
return btfVariableLen, nil
}

type btfEnum struct {
NameOff uint32
Val uint32
}

var btfEnumLen = int(unsafe.Sizeof(btfEnum{}))

func unmarshalBtfEnums(enums []btfEnum, b []byte, bo binary.ByteOrder) (int, error) {
Expand All @@ -455,12 +406,6 @@ func unmarshalBtfEnums(enums []btfEnum, b []byte, bo binary.ByteOrder) (int, err
return off, nil
}

type btfEnum64 struct {
NameOff uint32
ValLo32 uint32
ValHi32 uint32
}

var btfEnum64Len = int(unsafe.Sizeof(btfEnum64{}))

func unmarshalBtfEnums64(enums []btfEnum64, b []byte, bo binary.ByteOrder) (int, error) {
Expand All @@ -480,11 +425,6 @@ func unmarshalBtfEnums64(enums []btfEnum64, b []byte, bo binary.ByteOrder) (int,
return off, nil
}

type btfParam struct {
NameOff uint32
Type TypeID
}

var btfParamLen = int(unsafe.Sizeof(btfParam{}))

func unmarshalBtfParams(params []btfParam, b []byte, bo binary.ByteOrder) (int, error) {
Expand All @@ -503,10 +443,6 @@ func unmarshalBtfParams(params []btfParam, b []byte, bo binary.ByteOrder) (int,
return off, nil
}

type btfDeclTag struct {
ComponentIdx uint32
}

var btfDeclTagLen = int(unsafe.Sizeof(btfDeclTag{}))

func unmarshalBtfDeclTag(bdt *btfDeclTag, b []byte, bo binary.ByteOrder) (int, error) {
Expand Down
1 change: 1 addition & 0 deletions btf/cmd/genbtftypes/.gitignore
@@ -0,0 +1 @@
genbtftypes

0 comments on commit 644f800

Please sign in to comment.