github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/examples/fentry/bpf_bpfel.go (about)

     1  // Code generated by bpf2go; DO NOT EDIT.
     2  //go:build 386 || amd64 || arm || arm64 || loong64 || mips64le || mipsle || ppc64le || riscv64
     3  
     4  package main
     5  
     6  import (
     7  	"bytes"
     8  	_ "embed"
     9  	"fmt"
    10  	"io"
    11  
    12  	"github.com/cilium/ebpf"
    13  )
    14  
    15  type bpfEvent struct {
    16  	Comm  [16]uint8
    17  	Sport uint16
    18  	Dport uint16
    19  	Saddr uint32
    20  	Daddr uint32
    21  }
    22  
    23  // loadBpf returns the embedded CollectionSpec for bpf.
    24  func loadBpf() (*ebpf.CollectionSpec, error) {
    25  	reader := bytes.NewReader(_BpfBytes)
    26  	spec, err := ebpf.LoadCollectionSpecFromReader(reader)
    27  	if err != nil {
    28  		return nil, fmt.Errorf("can't load bpf: %w", err)
    29  	}
    30  
    31  	return spec, err
    32  }
    33  
    34  // loadBpfObjects loads bpf and converts it into a struct.
    35  //
    36  // The following types are suitable as obj argument:
    37  //
    38  //	*bpfObjects
    39  //	*bpfPrograms
    40  //	*bpfMaps
    41  //
    42  // See ebpf.CollectionSpec.LoadAndAssign documentation for details.
    43  func loadBpfObjects(obj interface{}, opts *ebpf.CollectionOptions) error {
    44  	spec, err := loadBpf()
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	return spec.LoadAndAssign(obj, opts)
    50  }
    51  
    52  // bpfSpecs contains maps and programs before they are loaded into the kernel.
    53  //
    54  // It can be passed ebpf.CollectionSpec.Assign.
    55  type bpfSpecs struct {
    56  	bpfProgramSpecs
    57  	bpfMapSpecs
    58  }
    59  
    60  // bpfSpecs contains programs before they are loaded into the kernel.
    61  //
    62  // It can be passed ebpf.CollectionSpec.Assign.
    63  type bpfProgramSpecs struct {
    64  	TcpConnect *ebpf.ProgramSpec `ebpf:"tcp_connect"`
    65  }
    66  
    67  // bpfMapSpecs contains maps before they are loaded into the kernel.
    68  //
    69  // It can be passed ebpf.CollectionSpec.Assign.
    70  type bpfMapSpecs struct {
    71  	Events *ebpf.MapSpec `ebpf:"events"`
    72  }
    73  
    74  // bpfObjects contains all objects after they have been loaded into the kernel.
    75  //
    76  // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign.
    77  type bpfObjects struct {
    78  	bpfPrograms
    79  	bpfMaps
    80  }
    81  
    82  func (o *bpfObjects) Close() error {
    83  	return _BpfClose(
    84  		&o.bpfPrograms,
    85  		&o.bpfMaps,
    86  	)
    87  }
    88  
    89  // bpfMaps contains all maps after they have been loaded into the kernel.
    90  //
    91  // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign.
    92  type bpfMaps struct {
    93  	Events *ebpf.Map `ebpf:"events"`
    94  }
    95  
    96  func (m *bpfMaps) Close() error {
    97  	return _BpfClose(
    98  		m.Events,
    99  	)
   100  }
   101  
   102  // bpfPrograms contains all programs after they have been loaded into the kernel.
   103  //
   104  // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign.
   105  type bpfPrograms struct {
   106  	TcpConnect *ebpf.Program `ebpf:"tcp_connect"`
   107  }
   108  
   109  func (p *bpfPrograms) Close() error {
   110  	return _BpfClose(
   111  		p.TcpConnect,
   112  	)
   113  }
   114  
   115  func _BpfClose(closers ...io.Closer) error {
   116  	for _, closer := range closers {
   117  		if err := closer.Close(); err != nil {
   118  			return err
   119  		}
   120  	}
   121  	return nil
   122  }
   123  
   124  // Do not access this directly.
   125  //
   126  //go:embed bpf_bpfel.o
   127  var _BpfBytes []byte