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

     1  package ebpf
     2  
     3  import "fmt"
     4  
     5  var (
     6  	_ Instruction = (*JumpSignedSmallerThan)(nil)
     7  	_ Jumper      = (*JumpSignedSmallerThan)(nil)
     8  	_ Valuer      = (*JumpSignedSmallerThan)(nil)
     9  )
    10  
    11  type JumpSignedSmallerThan struct {
    12  	Dest   Register
    13  	Offset int16
    14  	Value  int32
    15  }
    16  
    17  func (a *JumpSignedSmallerThan) Raw() ([]RawInstruction, error) {
    18  	return []RawInstruction{
    19  		{Op: BPF_JSLT | BPF_K | BPF_JMP, Reg: NewReg(0, a.Dest), Off: a.Offset, Imm: a.Value},
    20  	}, nil
    21  }
    22  
    23  func (a *JumpSignedSmallerThan) String() string {
    24  	return fmt.Sprintf("if r%s s< %d goto %+d", a.Dest, a.Value, a.Offset)
    25  }
    26  
    27  func (a *JumpSignedSmallerThan) SetJumpTarget(relAddr int16) {
    28  	a.Offset = relAddr
    29  }
    30  
    31  func (a *JumpSignedSmallerThan) SetValue(value int32) {
    32  	a.Value = value
    33  }
    34  
    35  var (
    36  	_ Instruction = (*JumpSignedSmallerThan32)(nil)
    37  	_ Jumper      = (*JumpSignedSmallerThan32)(nil)
    38  	_ Valuer      = (*JumpSignedSmallerThan32)(nil)
    39  )
    40  
    41  type JumpSignedSmallerThan32 struct {
    42  	Dest   Register
    43  	Offset int16
    44  	Value  int32
    45  }
    46  
    47  func (a *JumpSignedSmallerThan32) Raw() ([]RawInstruction, error) {
    48  	return []RawInstruction{
    49  		{Op: BPF_JSLT | BPF_K | BPF_JMP32, Reg: NewReg(0, a.Dest), Off: a.Offset, Imm: a.Value},
    50  	}, nil
    51  }
    52  
    53  func (a *JumpSignedSmallerThan32) String() string {
    54  	return fmt.Sprintf("if w%d s< %d goto %+d", a.Dest, a.Value, a.Offset)
    55  }
    56  
    57  func (a *JumpSignedSmallerThan32) SetJumpTarget(relAddr int16) {
    58  	a.Offset = relAddr
    59  }
    60  
    61  func (a *JumpSignedSmallerThan32) SetValue(value int32) {
    62  	a.Value = value
    63  }
    64  
    65  var (
    66  	_ Instruction = (*JumpSignedSmallerThanRegister)(nil)
    67  	_ Jumper      = (*JumpSignedSmallerThanRegister)(nil)
    68  )
    69  
    70  type JumpSignedSmallerThanRegister struct {
    71  	Dest   Register
    72  	Src    Register
    73  	Offset int16
    74  }
    75  
    76  func (a *JumpSignedSmallerThanRegister) Raw() ([]RawInstruction, error) {
    77  	return []RawInstruction{
    78  		{Op: BPF_JSLT | BPF_X | BPF_JMP, Reg: NewReg(a.Src, a.Dest), Off: a.Offset},
    79  	}, nil
    80  }
    81  
    82  func (a *JumpSignedSmallerThanRegister) String() string {
    83  	return fmt.Sprintf("if r%s s< r%s goto %+d", a.Dest, a.Src, a.Offset)
    84  }
    85  
    86  func (a *JumpSignedSmallerThanRegister) SetJumpTarget(relAddr int16) {
    87  	a.Offset = relAddr
    88  }
    89  
    90  var (
    91  	_ Instruction = (*JumpSignedSmallerThanRegister32)(nil)
    92  	_ Jumper      = (*JumpSignedSmallerThanRegister32)(nil)
    93  )
    94  
    95  type JumpSignedSmallerThanRegister32 struct {
    96  	Dest   Register
    97  	Src    Register
    98  	Offset int16
    99  }
   100  
   101  func (a *JumpSignedSmallerThanRegister32) Raw() ([]RawInstruction, error) {
   102  	return []RawInstruction{
   103  		{Op: BPF_JSLT | BPF_X | BPF_JMP32, Reg: NewReg(a.Src, a.Dest), Off: a.Offset},
   104  	}, nil
   105  }
   106  
   107  func (a *JumpSignedSmallerThanRegister32) String() string {
   108  	return fmt.Sprintf("if w%d s< w%d goto %+d", a.Dest, a.Src, a.Offset)
   109  }
   110  
   111  func (a *JumpSignedSmallerThanRegister32) SetJumpTarget(relAddr int16) {
   112  	a.Offset = relAddr
   113  }