Browse Source

bpf: rename LoadIPv4HeaderLen to the more generic LoadMemShift.

The instruction itself doesn't care what the bits it's twiddling represents,
even though the it was introduced to more efficiently manipulate IPv4 packets.

Change-Id: Ie65a6df4041ad2090060636ccf7128680fcf75b7
Reviewed-on: https://go-review.googlesource.com/21244
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
David Anderson 9 years ago
parent
commit
3e8a7b0329
3 changed files with 14 additions and 11 deletions
  1. 2 3
      bpf/constants.go
  2. 11 7
      bpf/instructions.go
  3. 1 1
      bpf/instructions_test.go

+ 2 - 3
bpf/constants.go

@@ -179,9 +179,8 @@ const (
 	opAddrModeAbsolute
 	opAddrModeIndirect
 	opAddrModeScratch
-	// These are actually extensions, not addressing modes.
-	opAddrModePacketLen
-	opAddrModeIPv4HeaderLen
+	opAddrModePacketLen // actually an extension, not an addressing mode.
+	opAddrModeMemShift
 )
 
 const (

+ 11 - 7
bpf/instructions.go

@@ -65,8 +65,8 @@ func (ri RawInstruction) Disassemble() Instruction {
 				return ri
 			}
 			return LoadExtension{Num: ExtLen}
-		case opAddrModeIPv4HeaderLen:
-			return LoadIPv4HeaderLen{Off: ri.K}
+		case opAddrModeMemShift:
+			return LoadMemShift{Off: ri.K}
 		default:
 			return ri
 		}
@@ -209,15 +209,19 @@ func (a LoadIndirect) Assemble() (RawInstruction, error) {
 	return assembleLoad(RegA, a.Size, opAddrModeIndirect, a.Off)
 }
 
-// LoadIPv4HeaderLen loads into register X the length of the IPv4
-// header whose first byte is packet[Off].
-type LoadIPv4HeaderLen struct {
+// LoadMemShift multiplies the first 4 bits of the byte at packet[Off]
+// by 4 and stores the result in register X.
+//
+// This instruction is mainly useful to load into X the length of an
+// IPv4 packet header in a single instruction, rather than have to do
+// the arithmetic on the header's first byte by hand.
+type LoadMemShift struct {
 	Off uint32
 }
 
 // Assemble implements the Instruction Assemble method.
-func (a LoadIPv4HeaderLen) Assemble() (RawInstruction, error) {
-	return assembleLoad(RegX, 1, opAddrModeIPv4HeaderLen, a.Off)
+func (a LoadMemShift) Assemble() (RawInstruction, error) {
+	return assembleLoad(RegX, 1, opAddrModeMemShift, a.Off)
 }
 
 // LoadExtension invokes a linux-specific extension and stores the

+ 1 - 1
bpf/instructions_test.go

@@ -29,7 +29,7 @@ var allInstructions = []Instruction{
 	LoadIndirect{Off: 42, Size: 2},
 	LoadIndirect{Off: 42, Size: 4},
 
-	LoadIPv4HeaderLen{Off: 42},
+	LoadMemShift{Off: 42},
 
 	LoadExtension{Num: ExtLen},
 	LoadExtension{Num: ExtProto},