Skip to content

Commit

Permalink
maint: Move config to it's own package (#139)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
Config is currently part of the assemblers sub-package. As more of the
agent will utilise configuration, it makes sense to move it to it's own
package for easier use.

## Short description of the changes
- Move assemblers/config.go to config.config.go
- Replace usage of assemblers.config with config.config
- Replace direct use of flags with config properties
- Update PacketSource and BpFilter names so they are exported
- Replace all pointer references of config with actual values

## How to verify that this has the expected result
Agent continues to work as it has with no external changes.
  • Loading branch information
MikeGoldsmith committed Sep 7, 2023
1 parent a719936 commit 7571de8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 26 deletions.
21 changes: 11 additions & 10 deletions assemblers/tcp_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/reassembly"
"github.com/honeycombio/ebpf-agent/config"
"github.com/honeycombio/libhoney-go"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -40,38 +41,38 @@ func (c *Context) GetCaptureInfo() gopacket.CaptureInfo {
}

type tcpAssembler struct {
config *config
config config.Config
packetSource *gopacket.PacketSource
streamFactory *tcpStreamFactory
streamPool *reassembly.StreamPool
assembler *reassembly.Assembler
httpEvents chan HttpEvent
}

func NewTcpAssembler(config config, httpEvents chan HttpEvent) tcpAssembler {
func NewTcpAssembler(config config.Config, httpEvents chan HttpEvent) tcpAssembler {
var packetSource *gopacket.PacketSource
var err error

switch config.packetSource {
switch config.PacketSource {
case "pcap":
packetSource, err = newPcapPacketSource(config)
if err != nil {
log.Fatal().Err(err).Msg("Failed to setup pcap handle")
}
// TODO: other data sources (eg afpacket, pfring, etc)
default:
log.Fatal().Str("packet_source", config.packetSource).Msg("Unknown packet source")
log.Fatal().Str("packet_source", config.PacketSource).Msg("Unknown packet source")
}

packetSource.Lazy = config.Lazy
packetSource.NoCopy = true

streamFactory := NewTcpStreamFactory(httpEvents)
streamFactory := NewTcpStreamFactory(config, httpEvents)
streamPool := reassembly.NewStreamPool(&streamFactory)
assembler := reassembly.NewAssembler(streamPool)

return tcpAssembler{
config: &config,
config: config,
packetSource: packetSource,
streamFactory: &streamFactory,
streamPool: streamPool,
Expand Down Expand Up @@ -204,12 +205,12 @@ func (h *tcpAssembler) Stop() {
Msg("Stopping TCP assembler")
}

func newPcapPacketSource(config config) (*gopacket.PacketSource, error) {
func newPcapPacketSource(config config.Config) (*gopacket.PacketSource, error) {
log.Info().
Str("interface", config.Interface).
Int("snaplen", config.Snaplen).
Bool("promiscuous", config.Promiscuous).
Str("bpf_filter", config.bpfFilter).
Str("bpf_filter", config.BpfFilter).
Msg("Configuring pcap packet source")
handle, err := pcap.OpenLive(config.Interface, int32(config.Snaplen), config.Promiscuous, time.Second)
if err != nil {
Expand All @@ -218,8 +219,8 @@ func newPcapPacketSource(config config) (*gopacket.PacketSource, error) {
Msg("Failed to open a pcap handle")
return nil, err
}
if config.bpfFilter != "" {
if err = handle.SetBPFFilter(config.bpfFilter); err != nil {
if config.BpfFilter != "" {
if err = handle.SetBPFFilter(config.BpfFilter); err != nil {
log.Fatal().
Err(err).
Msg("Error setting BPF filter")
Expand Down
10 changes: 6 additions & 4 deletions assemblers/tcp_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/reassembly"
"github.com/honeycombio/ebpf-agent/config"
"github.com/rs/zerolog/log"
)

Expand All @@ -21,6 +22,7 @@ type tcpStream struct {
counter requestCounter
ident string
closed bool
config config.Config
sync.Mutex
matcher httpMatcher
events chan HttpEvent
Expand All @@ -35,7 +37,7 @@ func (t *tcpStream) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir reassem
t.fsmerr = true
stats.rejectConnFsm++
}
if !*ignorefsmerr {
if !t.config.Ignorefsmerr {
return false
}
}
Expand All @@ -44,13 +46,13 @@ func (t *tcpStream) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir reassem
if err != nil {
// Error("OptionChecker", "%s: Packet rejected by OptionChecker: %s\n", t.ident, err)
stats.rejectOpt++
if !*nooptcheck {
if !t.config.Nooptcheck {
return false
}
}
// Checksum
accept := true
if *checksum {
if t.config.Checksum {
c, err := tcp.ComputeChecksum()
if err != nil {
log.Error().
Expand Down Expand Up @@ -122,7 +124,7 @@ func (t *tcpStream) ReassembledSG(sg reassembly.ScatterGather, ac reassembly.Ass
Int("overlap_byte_count", sgStats.OverlapBytes).
Int("overlap_packet_count", sgStats.OverlapPackets).
Msg("SG reassembled packet")
if skip == -1 && *allowmissinginit {
if skip == -1 && t.config.Allowmissinginit {
// this is allowed
} else if skip != 0 {
// Missing bytes in stream: do not even try to parse it
Expand Down
6 changes: 5 additions & 1 deletion assemblers/tcp_stream_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/reassembly"
"github.com/honeycombio/ebpf-agent/config"
"github.com/rs/zerolog/log"
)

var streamId uint64 = 0

type tcpStreamFactory struct {
config config.Config
wg sync.WaitGroup
httpEvents chan HttpEvent
}

func NewTcpStreamFactory(httpEvents chan HttpEvent) tcpStreamFactory {
func NewTcpStreamFactory(config config.Config, httpEvents chan HttpEvent) tcpStreamFactory {
return tcpStreamFactory{
config: config,
httpEvents: httpEvents,
}
}
Expand All @@ -34,6 +37,7 @@ func (factory *tcpStreamFactory) New(net, transport gopacket.Flow, tcp *layers.T
}
streamId := atomic.AddUint64(&streamId, 1)
stream := &tcpStream{
config: factory.config,
id: streamId,
net: net,
transport: transport,
Expand Down
18 changes: 9 additions & 9 deletions assemblers/config.go → config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package assemblers
package config

import (
"encoding/json"
Expand Down Expand Up @@ -32,7 +32,7 @@ var promisc = flag.Bool("promisc", true, "Set promiscuous mode")
var packetSource = flag.String("source", "pcap", "Packet source (defaults to pcap)")
var bpfFilter = flag.String("filter", "tcp", "BPF filter")

type config struct {
type Config struct {
Maxcount int
Statsevery int
Lazy bool
Expand All @@ -51,12 +51,12 @@ type config struct {
Promiscuous bool
CloseTimeout time.Duration
Timeout time.Duration
packetSource string
bpfFilter string
PacketSource string
BpfFilter string
}

func NewConfig() *config {
c := &config{
func NewConfig() Config {
c := Config{
Maxcount: *maxcount,
Statsevery: *statsevery,
Lazy: *lazy,
Expand All @@ -74,8 +74,8 @@ func NewConfig() *config {
TsType: *tstype,
Promiscuous: *promisc,
Timeout: timeout,
packetSource: *packetSource,
bpfFilter: *bpfFilter,
PacketSource: *packetSource,
BpfFilter: *bpfFilter,
}

// Add filters to only capture common HTTP methods
Expand All @@ -94,7 +94,7 @@ func NewConfig() *config {
// HTTP 1.1 is the response start string
"tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x48545450", // 'HTTP' 1.1
}
c.bpfFilter = strings.Join(filters, " or ")
c.BpfFilter = strings.Join(filters, " or ")

if c.Debug {
b, err := json.MarshalIndent(c, "", " ")
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/honeycombio/ebpf-agent/assemblers"
"github.com/honeycombio/ebpf-agent/config"
"github.com/honeycombio/ebpf-agent/utils"
"github.com/honeycombio/libhoney-go"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -93,11 +94,11 @@ func main() {
cachedK8sClient := utils.NewCachedK8sClient(k8sClient)
cachedK8sClient.Start(ctx)

agentConfig := assemblers.NewConfig()
agentConfig := config.NewConfig()

// setup TCP stream reader
httpEvents := make(chan assemblers.HttpEvent, 10000)
assembler := assemblers.NewTcpAssembler(*agentConfig, httpEvents)
assembler := assemblers.NewTcpAssembler(agentConfig, httpEvents)
go handleHttpEvents(httpEvents, cachedK8sClient)
go assembler.Start()
defer assembler.Stop()
Expand Down
60 changes: 60 additions & 0 deletions source/packet_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package source

import (
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"github.com/honeycombio/ebpf-agent/config"
"github.com/rs/zerolog/log"
)

func NewPacketSource(config config.Config) (*gopacket.PacketSource, error) {
var packetSource *gopacket.PacketSource
var err error

switch config.PacketSource {
case "pcap":
packetSource, err = newPcapPacketSource(config)
if err != nil {
log.Fatal().Err(err).Msg("Failed to setup pcap handle")
}
// TODO: other data sources (eg afpacket, pfring, etc)
default:
log.Fatal().Str("packet_source", config.PacketSource).Msg("Unknown packet source")
}

packetSource.Lazy = config.Lazy
packetSource.NoCopy = true

return packetSource, nil
}

func newPcapPacketSource(config config.Config) (*gopacket.PacketSource, error) {
log.Info().
Str("interface", config.Interface).
Int("snaplen", config.Snaplen).
Bool("promiscuous", config.Promiscuous).
Str("bpf_filter", config.BpfFilter).
Msg("Configuring pcap packet source")
handle, err := pcap.OpenLive(config.Interface, int32(config.Snaplen), config.Promiscuous, time.Second)
if err != nil {
log.Fatal().
Err(err).
Msg("Failed to open a pcap handle")
return nil, err
}
if config.BpfFilter != "" {
if err = handle.SetBPFFilter(config.BpfFilter); err != nil {
log.Fatal().
Err(err).
Msg("Error setting BPF filter")
return nil, err
}
}

return gopacket.NewPacketSource(
handle,
handle.LinkType(),
), nil
}

0 comments on commit 7571de8

Please sign in to comment.