-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add af_packet capture engine #950
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package capture | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/afpacket" | ||
"github.com/google/gopacket/layers" | ||
"github.com/google/gopacket/pcap" | ||
"golang.org/x/net/bpf" | ||
|
||
_ "github.com/google/gopacket/layers" | ||
) | ||
|
||
type afpacketHandle struct { | ||
TPacket *afpacket.TPacket | ||
} | ||
|
||
func newAfpacketHandle(device string, snaplen int, block_size int, num_blocks int, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't use underscores in Go names; func parameter block_size should be blockSize |
||
useVLAN bool, timeout time.Duration) (*afpacketHandle, error) { | ||
|
||
h := &afpacketHandle{} | ||
var err error | ||
|
||
if device == "any" { | ||
h.TPacket, err = afpacket.NewTPacket( | ||
afpacket.OptFrameSize(snaplen), | ||
afpacket.OptBlockSize(block_size), | ||
afpacket.OptNumBlocks(num_blocks), | ||
afpacket.OptAddVLANHeader(useVLAN), | ||
afpacket.OptPollTimeout(timeout), | ||
afpacket.SocketRaw, | ||
afpacket.TPacketVersion3) | ||
} else { | ||
h.TPacket, err = afpacket.NewTPacket( | ||
afpacket.OptInterface(device), | ||
afpacket.OptFrameSize(snaplen), | ||
afpacket.OptBlockSize(block_size), | ||
afpacket.OptNumBlocks(num_blocks), | ||
afpacket.OptAddVLANHeader(useVLAN), | ||
afpacket.OptPollTimeout(timeout), | ||
afpacket.SocketRaw, | ||
afpacket.TPacketVersion3) | ||
} | ||
return h, err | ||
} | ||
|
||
// ZeroCopyReadPacketData satisfies ZeroCopyPacketDataSource interface | ||
func (h *afpacketHandle) ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) { | ||
return h.TPacket.ReadPacketData() | ||
} | ||
|
||
// SetBPFFilter translates a BPF filter string into BPF RawInstruction and applies them. | ||
func (h *afpacketHandle) SetBPFFilter(filter string, snaplen int) (err error) { | ||
pcapBPF, err := pcap.CompileBPFFilter(layers.LinkTypeEthernet, snaplen, filter) | ||
if err != nil { | ||
return err | ||
} | ||
bpfIns := []bpf.RawInstruction{} | ||
for _, ins := range pcapBPF { | ||
bpfIns2 := bpf.RawInstruction{ | ||
Op: ins.Code, | ||
Jt: ins.Jt, | ||
Jf: ins.Jf, | ||
K: ins.K, | ||
} | ||
bpfIns = append(bpfIns, bpfIns2) | ||
} | ||
if h.TPacket.SetBPF(bpfIns); err != nil { | ||
return err | ||
} | ||
return h.TPacket.SetBPF(bpfIns) | ||
} | ||
|
||
// LinkType returns ethernet link type. | ||
func (h *afpacketHandle) LinkType() layers.LinkType { | ||
return layers.LinkTypeEthernet | ||
} | ||
|
||
// Close will close afpacket source. | ||
func (h *afpacketHandle) Close() { | ||
h.TPacket.Close() | ||
} | ||
|
||
// SocketStats prints received, dropped, queue-freeze packet stats. | ||
func (h *afpacketHandle) SocketStats() (as afpacket.SocketStats, asv afpacket.SocketStatsV3, err error) { | ||
return h.TPacket.SocketStats() | ||
} | ||
|
||
// afpacketComputeSize computes the block_size and the num_blocks in such a way that the | ||
// allocated mmap buffer is close to but smaller than target_size_mb. | ||
// The restriction is that the block_size must be divisible by both the | ||
// frame size and page size. | ||
func afpacketComputeSize(targetSizeMb int, snaplen int, pageSize int) ( | ||
frameSize int, blockSize int, numBlocks int, err error) { | ||
|
||
if snaplen < pageSize { | ||
frameSize = pageSize / (pageSize / snaplen) | ||
} else { | ||
frameSize = (snaplen/pageSize + 1) * pageSize | ||
} | ||
|
||
// 128 is the default from the gopacket library so just use that | ||
blockSize = frameSize * 128 | ||
numBlocks = (targetSizeMb * 1024 * 1024) / blockSize | ||
|
||
fmt.Println(blockSize, (targetSizeMb * 1024 * 1024), pageSize, snaplen) | ||
|
||
if numBlocks == 0 { | ||
return 0, 0, 0, fmt.Errorf("Interface buffersize is too small") | ||
} | ||
|
||
return frameSize, blockSize, numBlocks, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a blank import should be only in a main or test package, or have a comment justifying it