github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/bpfsys/bpf_attr.go (about)

     1  package bpfsys
     2  
     3  import (
     4  	"unsafe"
     5  
     6  	"github.com/dylandreimerink/gobpfld/bpftypes"
     7  	"github.com/dylandreimerink/gobpfld/kernelsupport"
     8  )
     9  
    10  type BPFAttribute interface {
    11  	ToPtr() unsafe.Pointer
    12  	Size() uintptr
    13  }
    14  
    15  // BPFAttrMapCreate is the attribute for the BPF_MAP_CREATE command
    16  type BPFAttrMapCreate struct {
    17  	MapType               bpftypes.BPFMapType  // one of enum bpf_map_type
    18  	KeySize               uint32               // size of key in bytes
    19  	ValueSize             uint32               // size of value in bytes
    20  	MaxEntries            uint32               // max number of entries in a map
    21  	MapFlags              bpftypes.BPFMapFlags // BPF_MAP_CREATE related flags
    22  	InnerMapFD            BPFfd                // fd pointing to the inner map
    23  	NumaNode              uint32               // numa node (effective only if BPF_F_NUMA_NODE is set)
    24  	MapName               [bpftypes.BPF_OBJ_NAME_LEN]byte
    25  	MapIFIndex            uint32 // ifindex of netdev to create on
    26  	BTFFD                 BPFfd  // fd pointing to a BTF type data
    27  	BTFKeyTypeID          uint32 // BTF type_id of the key
    28  	BTFValueTypeID        uint32 // BTF type_id of the value
    29  	BTFVMLinuxValueTypeID uint32 // BTF type_id of a kernel-struct stored as the map value
    30  }
    31  
    32  func (amc *BPFAttrMapCreate) ToPtr() unsafe.Pointer {
    33  	return unsafe.Pointer(amc)
    34  }
    35  
    36  func (amc *BPFAttrMapCreate) Size() uintptr {
    37  	return unsafe.Sizeof(*amc)
    38  }
    39  
    40  // BPFAttrMapElem is used as attribute for the BPF_MAP_*_ELEM commands
    41  type BPFAttrMapElem struct {
    42  	MapFD BPFfd
    43  	Key   uintptr // Pointer to the key value
    44  	// In the kernel this is a union, so depending on context this field is pointer to "Value" or "NextKey"
    45  	Value_NextKey uintptr
    46  	Flags         BPFAttrMapElemFlags
    47  }
    48  
    49  func (ame *BPFAttrMapElem) ToPtr() unsafe.Pointer {
    50  	return unsafe.Pointer(ame)
    51  }
    52  
    53  func (ame *BPFAttrMapElem) Size() uintptr {
    54  	return unsafe.Sizeof(*ame)
    55  }
    56  
    57  // BPFAttrMapElemFlags should be one of the following:
    58  //  * BPFMapElemAny
    59  //  * BPFMapElemNoExists
    60  //  * BPFMapElemExists
    61  //  * BPFMapElemLock
    62  type BPFAttrMapElemFlags uint64
    63  
    64  const (
    65  	// BPFMapElemAny Create new elements or update a existing elements.
    66  	BPFMapElemAny BPFAttrMapElemFlags = iota
    67  	// BPFMapElemNoExists Create new elements only if they do not exist.
    68  	BPFMapElemNoExists
    69  	// BPFMapElemExists Update existing elements.
    70  	BPFMapElemExists
    71  	// BPFMapElemLock Update spin_lock-ed map elements. This must be
    72  	// specified if the map value contains a spinlock.
    73  	BPFMapElemLock
    74  )
    75  
    76  // BPFAttrMapBatch is used as attribute for the BPF_MAP_*_BATCH commands
    77  type BPFAttrMapBatch struct {
    78  	InBatch   uintptr // start batch, NULL to start from beginning
    79  	OutBatch  uintptr // output: next start batch
    80  	Keys      uintptr
    81  	Values    uintptr
    82  	Count     uint32 // input/output:  input: # of key/value elements, output: # of filled elements
    83  	MapFD     BPFfd
    84  	ElemFlags BPFAttrMapElemFlags
    85  	Flags     BPFAttrMapElemFlags
    86  }
    87  
    88  func (amb *BPFAttrMapBatch) ToPtr() unsafe.Pointer {
    89  	return unsafe.Pointer(amb)
    90  }
    91  
    92  func (amb *BPFAttrMapBatch) Size() uintptr {
    93  	return unsafe.Sizeof(*amb)
    94  }
    95  
    96  type bpfAttrProgramLoad3_18 struct {
    97  	ProgramType bpftypes.BPFProgType // one of enum bpf_prog_type
    98  	InsnCnt     uint32               // the amount of bpf instruction in program
    99  	Insns       uintptr              // pointer to the bpf instructions
   100  	License     uintptr              // Pointer to string containing the license
   101  	LogLevel    bpftypes.BPFLogLevel // verbosity level of verifier
   102  	LogSize     uint32               // size of user buffer
   103  	LogBuf      uintptr              // pointer to buffer where verifier log will be written to
   104  }
   105  
   106  type bpfAttrProgramLoad4_1 struct {
   107  	ProgramType bpftypes.BPFProgType // one of enum bpf_prog_type
   108  	InsnCnt     uint32               // the amount of bpf instruction in program
   109  	Insns       uintptr              // pointer to the bpf instructions
   110  	License     uintptr              // Pointer to string containing the license
   111  	LogLevel    bpftypes.BPFLogLevel // verbosity level of verifier
   112  	LogSize     uint32               // size of user buffer
   113  	LogBuf      uintptr              // pointer to buffer where verifier log will be written to
   114  
   115  	KernelVersion uint32 // not used
   116  }
   117  
   118  type bpfAttrProgramLoad4_12 struct {
   119  	ProgramType bpftypes.BPFProgType // one of enum bpf_prog_type
   120  	InsnCnt     uint32               // the amount of bpf instruction in program
   121  	Insns       uintptr              // pointer to the bpf instructions
   122  	License     uintptr              // Pointer to string containing the license
   123  	LogLevel    bpftypes.BPFLogLevel // verbosity level of verifier
   124  	LogSize     uint32               // size of user buffer
   125  	LogBuf      uintptr              // pointer to buffer where verifier log will be written to
   126  
   127  	KernelVersion uint32 // not used
   128  
   129  	ProgFlags bpftypes.BPFProgLoadFlags
   130  }
   131  
   132  type bpfAttrProgramLoad4_15 struct {
   133  	ProgramType bpftypes.BPFProgType // one of enum bpf_prog_type
   134  	InsnCnt     uint32               // the amount of bpf instruction in program
   135  	Insns       uintptr              // pointer to the bpf instructions
   136  	License     uintptr              // Pointer to string containing the license
   137  	LogLevel    bpftypes.BPFLogLevel // verbosity level of verifier
   138  	LogSize     uint32               // size of user buffer
   139  	LogBuf      uintptr              // pointer to buffer where verifier log will be written to
   140  
   141  	KernelVersion uint32 // not used
   142  
   143  	ProgFlags bpftypes.BPFProgLoadFlags
   144  
   145  	ProgName    [bpftypes.BPF_OBJ_NAME_LEN]byte
   146  	ProgIFIndex uint32 // ifindex of netdev to prep for
   147  }
   148  
   149  type bpfAttrProgramLoad4_17 struct {
   150  	ProgramType bpftypes.BPFProgType // one of enum bpf_prog_type
   151  	InsnCnt     uint32               // the amount of bpf instruction in program
   152  	Insns       uintptr              // pointer to the bpf instructions
   153  	License     uintptr              // Pointer to string containing the license
   154  	LogLevel    bpftypes.BPFLogLevel // verbosity level of verifier
   155  	LogSize     uint32               // size of user buffer
   156  	LogBuf      uintptr              // pointer to buffer where verifier log will be written to
   157  
   158  	KernelVersion uint32 // not used
   159  
   160  	ProgFlags bpftypes.BPFProgLoadFlags
   161  
   162  	ProgName    [bpftypes.BPF_OBJ_NAME_LEN]byte
   163  	ProgIFIndex uint32 // ifindex of netdev to prep for
   164  
   165  	// For some prog types expected attach type must be known at
   166  	// load time to verify attach type specific parts of prog
   167  	// (context accesses, allowed helpers, etc).
   168  	ExpectedAttachType bpftypes.BPFAttachType
   169  }
   170  
   171  type bpfAttrProgramLoad5_0 struct {
   172  	ProgramType bpftypes.BPFProgType // one of enum bpf_prog_type
   173  	InsnCnt     uint32               // the amount of bpf instruction in program
   174  	Insns       uintptr              // pointer to the bpf instructions
   175  	License     uintptr              // Pointer to string containing the license
   176  	LogLevel    bpftypes.BPFLogLevel // verbosity level of verifier
   177  	LogSize     uint32               // size of user buffer
   178  	LogBuf      uintptr              // pointer to buffer where verifier log will be written to
   179  
   180  	KernelVersion uint32 // not used
   181  
   182  	ProgFlags bpftypes.BPFProgLoadFlags
   183  
   184  	ProgName    [bpftypes.BPF_OBJ_NAME_LEN]byte
   185  	ProgIFIndex uint32 // ifindex of netdev to prep for
   186  
   187  	// For some prog types expected attach type must be known at
   188  	// load time to verify attach type specific parts of prog
   189  	// (context accesses, allowed helpers, etc).
   190  	ExpectedAttachType bpftypes.BPFAttachType
   191  
   192  	ProgBTFFD       BPFfd   // fd pointing to BTF type data
   193  	FuncInfoRecSize uint32  // userspace bpf_func_info size
   194  	FuncInfo        uintptr // func info
   195  	FuncInfoCnt     uint32  // number of bpf_func_info records
   196  	LineInfoRecSize uint32  // userspace bpf_line_info size
   197  	LineInfo        uintptr // line info
   198  	LineInfoCnt     uint32  // number of bpf_line_info records
   199  }
   200  
   201  type bpfAttrProgramLoad5_5 struct {
   202  	ProgramType bpftypes.BPFProgType // one of enum bpf_prog_type
   203  	InsnCnt     uint32               // the amount of bpf instruction in program
   204  	Insns       uintptr              // pointer to the bpf instructions
   205  	License     uintptr              // Pointer to string containing the license
   206  	LogLevel    bpftypes.BPFLogLevel // verbosity level of verifier
   207  	LogSize     uint32               // size of user buffer
   208  	LogBuf      uintptr              // pointer to buffer where verifier log will be written to
   209  
   210  	KernelVersion uint32 // not used
   211  
   212  	ProgFlags bpftypes.BPFProgLoadFlags
   213  
   214  	ProgName    [bpftypes.BPF_OBJ_NAME_LEN]byte
   215  	ProgIFIndex uint32 // ifindex of netdev to prep for
   216  
   217  	// For some prog types expected attach type must be known at
   218  	// load time to verify attach type specific parts of prog
   219  	// (context accesses, allowed helpers, etc).
   220  	ExpectedAttachType bpftypes.BPFAttachType
   221  
   222  	ProgBTFFD       BPFfd   // fd pointing to BTF type data
   223  	FuncInfoRecSize uint32  // userspace bpf_func_info size
   224  	FuncInfo        uintptr // func info
   225  	FuncInfoCnt     uint32  // number of bpf_func_info records
   226  	LineInfoRecSize uint32  // userspace bpf_line_info size
   227  	LineInfo        uintptr // line info
   228  	LineInfoCnt     uint32  // number of bpf_line_info records
   229  
   230  	AttachBTFID uint32 // in-kernel BTF type id to attach to
   231  	// valid prog_fd to attach to bpf prog or valid module BTF object fd or 0 to attach to vmlinux
   232  	AttachProgFD_AttachBTFObjFD uint32
   233  }
   234  
   235  type BPFAttrProgramLoad bpfAttrProgramLoad5_5
   236  
   237  func (amb *BPFAttrProgramLoad) ToPtr() unsafe.Pointer {
   238  	return unsafe.Pointer(amb)
   239  }
   240  
   241  // Size returns the size of the struct, we always pass a pointer to the full struct, but the kernel will only
   242  // read up to the given amount of bytes. The kernel will throw an error if the size is larger than it expects
   243  // thus we have to return different sizes depending on the current kernel version.
   244  //
   245  // We use internal structs to calculate the size, we do this since the same struct might be a different size
   246  // depending on the architecture(32/64 bit addresses).
   247  func (amb *BPFAttrProgramLoad) Size() uintptr {
   248  	var size uintptr
   249  
   250  	switch {
   251  	case kernelsupport.CurrentVersion.Higher(kernelsupport.Version(5, 5, 0)):
   252  		size = unsafe.Sizeof(bpfAttrProgramLoad5_5{})
   253  	case kernelsupport.CurrentVersion.Higher(kernelsupport.Version(5, 0, 0)):
   254  		size = unsafe.Sizeof(bpfAttrProgramLoad5_0{})
   255  	case kernelsupport.CurrentVersion.Higher(kernelsupport.Version(4, 17, 0)):
   256  		size = unsafe.Sizeof(bpfAttrProgramLoad4_17{})
   257  	case kernelsupport.CurrentVersion.Higher(kernelsupport.Version(4, 15, 0)):
   258  		size = unsafe.Sizeof(bpfAttrProgramLoad4_15{})
   259  	case kernelsupport.CurrentVersion.Higher(kernelsupport.Version(4, 12, 0)):
   260  		size = unsafe.Sizeof(bpfAttrProgramLoad4_12{})
   261  	case kernelsupport.CurrentVersion.Higher(kernelsupport.Version(4, 1, 0)):
   262  		size = unsafe.Sizeof(bpfAttrProgramLoad4_1{})
   263  	default:
   264  		size = unsafe.Sizeof(bpfAttrProgramLoad3_18{})
   265  	}
   266  
   267  	return size
   268  }
   269  
   270  // BPFAttrObj is used as attribute in the BPF_OBJ_* commands
   271  type BPFAttrObj struct {
   272  	Pathname  uintptr // pointer to cstring
   273  	BPFfd     BPFfd
   274  	FileFlags uint32
   275  }
   276  
   277  func (ao *BPFAttrObj) ToPtr() unsafe.Pointer {
   278  	return unsafe.Pointer(ao)
   279  }
   280  
   281  func (ao *BPFAttrObj) Size() uintptr {
   282  	return unsafe.Sizeof(*ao)
   283  }
   284  
   285  // BPFAttrProgAttachDetach is used as attribute in the BPF_PROG_ATTACH/DETACH commands
   286  type BPFAttrProgAttachDetach struct {
   287  	TargetFD     uint32 // container object to attach to
   288  	AttachBPFFD  BPFfd  // eBPF program to attach
   289  	AttachType   bpftypes.BPFAttachType
   290  	AttachFlags  bpftypes.BPFProgAttachFlags
   291  	ReplaceBPFFD BPFfd // previously attached eBPF program to replace if BPF_F_REPLACE is used
   292  }
   293  
   294  func (apa *BPFAttrProgAttachDetach) ToPtr() unsafe.Pointer {
   295  	return unsafe.Pointer(apa)
   296  }
   297  
   298  func (apa *BPFAttrProgAttachDetach) Size() uintptr {
   299  	return unsafe.Sizeof(*apa)
   300  }
   301  
   302  // BPFAttrProgTestRun is the attribute for the BPF_PROG_TEST_RUN command
   303  type BPFAttrProgTestRun struct {
   304  	ProgFD      BPFfd
   305  	Retval      uint32
   306  	DataSizeIn  uint32  // Size of the input data buffer
   307  	DataSizeOut uint32  // Size of the output data buffer
   308  	DataIn      uintptr // Pointer to a buffer with input data
   309  	DataOut     uintptr // Pointer to a buffer with output data
   310  	Repeat      uint32
   311  	Duration    uint32
   312  	CtxSizeIn   uint32  // Size of the input ctx
   313  	CtxSizeOut  uint32  // Size of the output ctx
   314  	CtxIn       uintptr // Pointer to the input ctx
   315  	CtxOut      uintptr // Pointer to the output ctx
   316  	Flags       uint32
   317  	CPU         uint32
   318  }
   319  
   320  func (apt *BPFAttrProgTestRun) ToPtr() unsafe.Pointer {
   321  	return unsafe.Pointer(apt)
   322  }
   323  
   324  func (apt *BPFAttrProgTestRun) Size() uintptr {
   325  	return unsafe.Sizeof(*apt)
   326  }
   327  
   328  // BPFAttrGetID is used as attribute in the BPF_*_GET_*_ID commands
   329  type BPFAttrGetID struct {
   330  	ID        uint32
   331  	NextID    uint32
   332  	OpenFlags uint32
   333  }
   334  
   335  func (agi *BPFAttrGetID) ToPtr() unsafe.Pointer {
   336  	return unsafe.Pointer(agi)
   337  }
   338  
   339  func (agi *BPFAttrGetID) Size() uintptr {
   340  	return unsafe.Sizeof(*agi)
   341  }
   342  
   343  // BPFAttrGetInfoFD is used as attribute in the BPF_OBJ_GET_INFO_BY_FD command
   344  type BPFAttrGetInfoFD struct {
   345  	BPFFD   BPFfd
   346  	InfoLen uint32  // Length of the info buffer
   347  	Info    uintptr // Pointer to buffer where the kernel will store info
   348  }
   349  
   350  func (agi *BPFAttrGetInfoFD) ToPtr() unsafe.Pointer {
   351  	return unsafe.Pointer(agi)
   352  }
   353  
   354  func (agi *BPFAttrGetInfoFD) Size() uintptr {
   355  	return unsafe.Sizeof(*agi)
   356  }
   357  
   358  // BPFAttrProgQuery is used as attribute in the BPF_PROG_QUERY command
   359  type BPFAttrProgQuery struct {
   360  	TargetFD    uint32
   361  	AttachType  bpftypes.BPFAttachType
   362  	QueryFlags  BPFAttrProgQueryFlags
   363  	AttachFlags uint32
   364  	ProgIDs     uintptr // Pointer to buffer where ids will be stored
   365  	ProgCnt     uint32
   366  }
   367  
   368  type BPFAttrProgQueryFlags uint32
   369  
   370  const (
   371  	// ProgQueryQueryEffective Query effective (directly attached + inherited from ancestor cgroups)
   372  	// programs that will be executed for events within a cgroup.
   373  	// attach_flags with this flag are returned only for directly attached programs.
   374  	ProgQueryQueryEffective BPFAttrProgQueryFlags = 1 << 0
   375  )
   376  
   377  func (apq *BPFAttrProgQuery) ToPtr() unsafe.Pointer {
   378  	return unsafe.Pointer(apq)
   379  }
   380  
   381  func (apq *BPFAttrProgQuery) Size() uintptr {
   382  	return unsafe.Sizeof(*apq)
   383  }
   384  
   385  // BPFAttrRawTracepointOpen is used as attribute in the BPF_RAW_TRACEPOINT_OPEN command
   386  type BPFAttrRawTracepointOpen struct {
   387  	Name   uintptr
   388  	ProgFD BPFfd
   389  }
   390  
   391  func (art *BPFAttrRawTracepointOpen) ToPtr() unsafe.Pointer {
   392  	return unsafe.Pointer(art)
   393  }
   394  
   395  func (art *BPFAttrRawTracepointOpen) Size() uintptr {
   396  	return unsafe.Sizeof(*art)
   397  }
   398  
   399  // BPFAttrBTFLoad is the attribute for the BPF_BTF_LOAD command
   400  type BPFAttrBTFLoad struct {
   401  	BTF         uintptr
   402  	BTFLogBuf   uintptr
   403  	BTFSize     uint32
   404  	BTFLogSize  uint32
   405  	BTFLogLevel bpftypes.BPFLogLevel
   406  }
   407  
   408  func (abl *BPFAttrBTFLoad) ToPtr() unsafe.Pointer {
   409  	return unsafe.Pointer(abl)
   410  }
   411  
   412  func (abl *BPFAttrBTFLoad) Size() uintptr {
   413  	return unsafe.Sizeof(*abl)
   414  }
   415  
   416  type BPFAttrTaskFDQuery struct {
   417  	PID         uint32
   418  	FD          BPFfd
   419  	Flags       uint32
   420  	BufLen      uint32
   421  	Buf         uintptr
   422  	ProgID      uint32
   423  	FDType      bpftypes.BPFTaskFDType
   424  	ProbeOffset uint64
   425  	ProbeAddr   uint64
   426  }
   427  
   428  func (atq *BPFAttrTaskFDQuery) ToPtr() unsafe.Pointer {
   429  	return unsafe.Pointer(atq)
   430  }
   431  
   432  func (atq *BPFAttrTaskFDQuery) Size() uintptr {
   433  	return unsafe.Sizeof(*atq)
   434  }
   435  
   436  // BPFAttrLinkCreate is used by BPF_LINK_CREATE command
   437  type BPFAttrLinkCreate struct {
   438  	ProgFD                 BPFfd
   439  	TargetFD_TargetIFIndex uint32
   440  	AttachType             bpftypes.BPFAttachType
   441  	Flags                  uint32
   442  
   443  	// TODO create a type to represent
   444  	// 		union {
   445  	// 			__u32		target_btf_id;	/* btf_id of target to attach to */
   446  	// 			struct {
   447  	// 				__aligned_u64	iter_info;	/* extra bpf_iter_link_info */
   448  	// 				__u32		iter_info_len;	/* iter_info length */
   449  	// 			};
   450  	// 		};
   451  }
   452  
   453  func (alc *BPFAttrLinkCreate) ToPtr() unsafe.Pointer {
   454  	return unsafe.Pointer(alc)
   455  }
   456  
   457  func (alc *BPFAttrLinkCreate) Size() uintptr {
   458  	return unsafe.Sizeof(*alc)
   459  }
   460  
   461  // BPFAttrLinkUpdate is used by BPF_LINK_UPDATE command
   462  type BPFAttrLinkUpdate struct {
   463  	LinkFD    uint32
   464  	NewProgFD BPFfd
   465  	Flags     uint32
   466  	OldProgFD BPFfd
   467  }
   468  
   469  func (alu *BPFAttrLinkUpdate) ToPtr() unsafe.Pointer {
   470  	return unsafe.Pointer(alu)
   471  }
   472  
   473  func (alu *BPFAttrLinkUpdate) Size() uintptr {
   474  	return unsafe.Sizeof(*alu)
   475  }
   476  
   477  type BPFAttrLinkDetach struct {
   478  	LinkID uint32
   479  }
   480  
   481  func (ald *BPFAttrLinkDetach) ToPtr() unsafe.Pointer {
   482  	return unsafe.Pointer(ald)
   483  }
   484  
   485  func (ald *BPFAttrLinkDetach) Size() uintptr {
   486  	return unsafe.Sizeof(*ald)
   487  }
   488  
   489  // BPFAttrEnableStats is used by BPF_ENABLE_STATS command
   490  type BPFAttrEnableStats struct {
   491  	Type uint32
   492  }
   493  
   494  func (aes *BPFAttrEnableStats) ToPtr() unsafe.Pointer {
   495  	return unsafe.Pointer(aes)
   496  }
   497  
   498  func (aes *BPFAttrEnableStats) Size() uintptr {
   499  	return unsafe.Sizeof(*aes)
   500  }
   501  
   502  // BPFAttrIterCreate is used by BPF_ITER_CREATE command
   503  type BPFAttrIterCreate struct {
   504  	LinkFD uint32
   505  	Flags  uint32
   506  }
   507  
   508  func (aic *BPFAttrIterCreate) ToPtr() unsafe.Pointer {
   509  	return unsafe.Pointer(aic)
   510  }
   511  
   512  func (aic *BPFAttrIterCreate) Size() uintptr {
   513  	return unsafe.Sizeof(*aic)
   514  }
   515  
   516  // BPFAttrProgBindMap is uses as attribute for the BPF_PROG_BIND_MAP command
   517  type BPFAttrProgBindMap struct {
   518  	ProgID uint32
   519  	MapFD  BPFfd
   520  	Flags  uint32
   521  }
   522  
   523  func (abm *BPFAttrProgBindMap) ToPtr() unsafe.Pointer {
   524  	return unsafe.Pointer(abm)
   525  }
   526  
   527  func (abm *BPFAttrProgBindMap) Size() uintptr {
   528  	return unsafe.Sizeof(*abm)
   529  }