author | David Anderson
<danderson@google.com> 2016-03-29 01:55:07 UTC |
committer | Mikio Hara
<mikioh.mikioh@gmail.com> 2016-03-31 21:48:25 UTC |
parent | c4bb2f7b3d65038072e1e637027388ba2379f1bf |
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},