gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

program.go (1862B)


      1 package link
      2 
      3 import (
      4 	"fmt"
      5 
      6 	"github.com/cilium/ebpf"
      7 	"github.com/cilium/ebpf/internal/sys"
      8 )
      9 
     10 type RawAttachProgramOptions struct {
     11 	// File descriptor to attach to. This differs for each attach type.
     12 	Target int
     13 	// Program to attach.
     14 	Program *ebpf.Program
     15 	// Program to replace (cgroups).
     16 	Replace *ebpf.Program
     17 	// Attach must match the attach type of Program (and Replace).
     18 	Attach ebpf.AttachType
     19 	// Flags control the attach behaviour. This differs for each attach type.
     20 	Flags uint32
     21 }
     22 
     23 // RawAttachProgram is a low level wrapper around BPF_PROG_ATTACH.
     24 //
     25 // You should use one of the higher level abstractions available in this
     26 // package if possible.
     27 func RawAttachProgram(opts RawAttachProgramOptions) error {
     28 	if err := haveProgAttach(); err != nil {
     29 		return err
     30 	}
     31 
     32 	var replaceFd uint32
     33 	if opts.Replace != nil {
     34 		replaceFd = uint32(opts.Replace.FD())
     35 	}
     36 
     37 	attr := sys.ProgAttachAttr{
     38 		TargetFd:     uint32(opts.Target),
     39 		AttachBpfFd:  uint32(opts.Program.FD()),
     40 		ReplaceBpfFd: replaceFd,
     41 		AttachType:   uint32(opts.Attach),
     42 		AttachFlags:  uint32(opts.Flags),
     43 	}
     44 
     45 	if err := sys.ProgAttach(&attr); err != nil {
     46 		return fmt.Errorf("can't attach program: %w", err)
     47 	}
     48 	return nil
     49 }
     50 
     51 type RawDetachProgramOptions struct {
     52 	Target  int
     53 	Program *ebpf.Program
     54 	Attach  ebpf.AttachType
     55 }
     56 
     57 // RawDetachProgram is a low level wrapper around BPF_PROG_DETACH.
     58 //
     59 // You should use one of the higher level abstractions available in this
     60 // package if possible.
     61 func RawDetachProgram(opts RawDetachProgramOptions) error {
     62 	if err := haveProgAttach(); err != nil {
     63 		return err
     64 	}
     65 
     66 	attr := sys.ProgDetachAttr{
     67 		TargetFd:    uint32(opts.Target),
     68 		AttachBpfFd: uint32(opts.Program.FD()),
     69 		AttachType:  uint32(opts.Attach),
     70 	}
     71 	if err := sys.ProgDetach(&attr); err != nil {
     72 		return fmt.Errorf("can't detach program: %w", err)
     73 	}
     74 
     75 	return nil
     76 }