Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

support reading blocks of all sizes #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions blockfile/blockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ func (b *BlockFile) Close() (err error) {
return
}

const blockHeaderSize = 48 // sizeof(tpacket_block_desc);

// allPacketsIter implements Iter.
type allPacketsIter struct {
*BlockFile
blockHeaderData []byte
blockData []byte
block *C.struct_tpacket_hdr_v1
pkt *C.struct_tpacket3_hdr
Expand All @@ -150,24 +153,43 @@ func (a *allPacketsIter) Next() bool {
}
for a.block == nil || a.blockPacketsRead == int(a.block.num_pkts) {
packetBlocksRead.Increment()
a.blockData = make([]byte, 1<<20)
_, err := a.f.ReadAt(a.blockData[:], a.blockOffset)
a.blockHeaderData = make([]byte, blockHeaderSize)
_, err := a.f.ReadAt(a.blockHeaderData[:], a.blockOffset)
if err == io.EOF {
a.done = true
return false
} else if err != nil {
a.err = fmt.Errorf("could not read block at %v: %v", a.blockOffset, err)
return false
}
baseHdr := (*C.struct_tpacket_block_desc)(unsafe.Pointer(&a.blockData[0]))
baseHdr := (*C.struct_tpacket_block_desc)(unsafe.Pointer(&a.blockHeaderData[0]))
a.block = (*C.struct_tpacket_hdr_v1)(unsafe.Pointer(&baseHdr.hdr[0]))
a.blockOffset += 1 << 20
offset_to_first_pkt := int64(a.block.offset_to_first_pkt)
blk_len := int64(a.block.blk_len)
if blk_len == 0 {
a.err = fmt.Errorf("could not read block at %v: blk_len == 0", a.blockOffset)
return false
}

pktsLen := blk_len - offset_to_first_pkt
a.blockData = make([]byte, pktsLen)

_, err = a.f.ReadAt(a.blockData[:], a.blockOffset+offset_to_first_pkt)
if err == io.EOF && a.block.num_pkts == 0 {
a.done = true
return false
} else if err != nil {
a.err = fmt.Errorf("could not read block at %v: %v", a.blockOffset, err)
return false
}

a.blockOffset += blk_len
a.blockPacketsRead = 0
a.pkt = nil
}
a.blockPacketsRead++
if a.pkt == nil {
a.packetOffset = int(a.block.offset_to_first_pkt)
a.packetOffset = 0
} else if a.pkt.tp_next_offset != 0 {
a.packetOffset += int(a.pkt.tp_next_offset)
} else {
Expand Down