git » go-net » commit 3e8a7b0

bpf: rename LoadIPv4HeaderLen to the more generic LoadMemShift.

author David Anderson
2016-03-29 01:55:07 UTC
committer Mikio Hara
2016-03-31 21:48:25 UTC
parent c4bb2f7b3d65038072e1e637027388ba2379f1bf

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>

bpf/constants.go +2 -3
bpf/instructions.go +11 -7
bpf/instructions_test.go +1 -1

diff --git a/bpf/constants.go b/bpf/constants.go
index d784228..2c8bbab 100644
--- a/bpf/constants.go
+++ b/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 (
diff --git a/bpf/instructions.go b/bpf/instructions.go
index e68d353..68ae6f5 100644
--- a/bpf/instructions.go
+++ b/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
diff --git a/bpf/instructions_test.go b/bpf/instructions_test.go
index e492428..833d1e1 100644
--- a/bpf/instructions_test.go
+++ b/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},