Skip to content

Commit

Permalink
Implement ebpf proc fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
schu committed Mar 14, 2017
1 parent 87cbc71 commit c42bc10
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
55 changes: 27 additions & 28 deletions probe/endpoint/connection_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type connectionTrackerConfig struct {
UseEbpfConn bool
ProcRoot string
BufferSize int
ProcessCache *process.CachingWalker
Scanner procspy.ConnectionScanner
DNSSnooper *DNSSnooper
}
Expand All @@ -28,43 +29,37 @@ type connectionTracker struct {
flowWalker flowWalker // Interface
ebpfTracker eventTracker
reverseResolver *reverseResolver
processCache *process.CachingWalker
}

func newProcfsConnectionTracker(conf connectionTrackerConfig) connectionTracker {
if conf.WalkProc && conf.Scanner == nil {
conf.Scanner = procspy.NewConnectionScanner(conf.ProcessCache)
}
return connectionTracker{
conf: conf,
flowWalker: newConntrackFlowWalker(conf.UseConntrack, conf.ProcRoot, conf.BufferSize, "--any-nat"),
ebpfTracker: nil,
reverseResolver: newReverseResolver(),
}
}

func newConnectionTracker(conf connectionTrackerConfig) connectionTracker {
if !conf.UseEbpfConn {
// ebpf OFF, use flowWalker
return connectionTracker{
conf: conf,
flowWalker: newConntrackFlowWalker(conf.UseConntrack, conf.ProcRoot, conf.BufferSize, "--any-nat"),
ebpfTracker: nil,
reverseResolver: newReverseResolver(),
}
// ebpf off, use conntrack for connection tracking
return newProcfsConnectionTracker(conf)
}
// When ebpf will be active by default, check if it starts correctly otherwise fallback to flowWalk
et, err := newEbpfTracker()
if err != nil {
// TODO: fallback to flowWalker, when ebpf is enabled by default
log.Errorf("Error setting up the ebpfTracker, connections will not be reported: %s", err)
noopConnectionTracker := connectionTracker{
conf: conf,
flowWalker: nil,
ebpfTracker: nil,
reverseResolver: nil,
}
return noopConnectionTracker
// ebpf failed, fallback to conntrack for connection tracking
log.Warnf("Error setting up the eBPF tracker: %v", err)
log.Warnf("Fallback to proc scanning")
return newProcfsConnectionTracker(conf)
}

var processCache *process.CachingWalker
processCache = process.NewCachingWalker(process.NewWalker(conf.ProcRoot))
processCache.Tick()

ct := connectionTracker{
conf: conf,
flowWalker: nil,
ebpfTracker: et,
reverseResolver: newReverseResolver(),
processCache: processCache,
}
go ct.getInitialState()
return ct
Expand All @@ -89,8 +84,7 @@ func flowToTuple(f flow) (ft fourTuple) {
return ft
}

// ReportConnections calls trackers accordingly to the configuration.
// When ebpf is enabled, only performEbpfTrack() is called
// ReportConnections calls trackers according to the configuration.
func (t *connectionTracker) ReportConnections(rpt *report.Report) {
hostNodeID := report.MakeHostNodeID(t.conf.HostID)

Expand Down Expand Up @@ -164,9 +158,14 @@ func (t *connectionTracker) performWalkProc(rpt *report.Report, hostNodeID strin
return nil
}

// getInitialState runs conntrack and proc parsing synchronously only
// once to initialize ebpfTracker
func (t *connectionTracker) getInitialState() {
scanner := procspy.NewSyncConnectionScanner(t.processCache)
// Run conntrack and proc parsing synchronously only once to initialize ebpfTracker
var processCache *process.CachingWalker
processCache = process.NewCachingWalker(process.NewWalker(t.conf.ProcRoot))
processCache.Tick()

scanner := procspy.NewSyncConnectionScanner(processCache)
seenTuples := map[string]fourTuple{}
// Consult the flowWalker to get the initial state
if err := IsConntrackSupported(t.conf.ProcRoot); t.conf.UseConntrack && err != nil {
Expand Down
3 changes: 3 additions & 0 deletions probe/endpoint/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/scope/probe/endpoint/procspy"
"github.com/weaveworks/scope/probe/process"
"github.com/weaveworks/scope/report"
)

Expand All @@ -29,6 +30,7 @@ type ReporterConfig struct {
UseEbpfConn bool
ProcRoot string
BufferSize int
ProcessCache *process.CachingWalker
Scanner procspy.ConnectionScanner
DNSSnooper *DNSSnooper
}
Expand Down Expand Up @@ -69,6 +71,7 @@ func NewReporter(conf ReporterConfig) *Reporter {
UseEbpfConn: conf.UseEbpfConn,
ProcRoot: conf.ProcRoot,
BufferSize: conf.BufferSize,
ProcessCache: conf.ProcessCache,
Scanner: conf.Scanner,
DNSSnooper: conf.DNSSnooper,
}),
Expand Down
8 changes: 1 addition & 7 deletions prog/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/weaveworks/scope/probe/controls"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/endpoint"
"github.com/weaveworks/scope/probe/endpoint/procspy"
"github.com/weaveworks/scope/probe/host"
"github.com/weaveworks/scope/probe/kubernetes"
"github.com/weaveworks/scope/probe/overlay"
Expand Down Expand Up @@ -158,13 +157,8 @@ func probeMain(flags probeFlags, targets []appclient.Target) {
p.AddTagger(probe.NewTopologyTagger(), host.NewTagger(hostID))

var processCache *process.CachingWalker
var scanner procspy.ConnectionScanner
if flags.procEnabled {
processCache = process.NewCachingWalker(process.NewWalker(flags.procRoot))
// The eBPF tracker finds connections itself and does not need the connection scanner
if !flags.useEbpfConn {
scanner = procspy.NewConnectionScanner(processCache)
}
p.AddTicker(processCache)
p.AddReporter(process.NewReporter(processCache, hostID, process.GetDeltaTotalJiffies, flags.noCommandLineArguments))
}
Expand All @@ -185,7 +179,7 @@ func probeMain(flags probeFlags, targets []appclient.Target) {
UseEbpfConn: flags.useEbpfConn,
ProcRoot: flags.procRoot,
BufferSize: flags.conntrackBufferSize,
Scanner: scanner,
ProcessCache: processCache,
DNSSnooper: dnsSnooper,
})
defer endpointReporter.Stop()
Expand Down

0 comments on commit c42bc10

Please sign in to comment.