Skip to content

Commit

Permalink
Merge pull request #1747 from lebauce/fix-lldp-probe-stop
Browse files Browse the repository at this point in the history
lldp: properly stop capture probes
  • Loading branch information
safchain authored Mar 29, 2019
2 parents 494d619 + 34432ae commit 3042e09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
3 changes: 1 addition & 2 deletions flow/probes/gopacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ func (p *GoPacketProbe) Run(packetCallback func(gopacket.Packet), e FlowProbeEve
statsUpdate := config.GetInt("agent.capture.stats_update")
statsTicker := time.NewTicker(time.Duration(statsUpdate) * time.Second)

wg.Add(1)

// manage BPF outside namespace because of syscall
if p.bpf != "" {
if err := p.packetProbe.SetBPFFilter(p.bpf); err != nil {
Expand All @@ -179,6 +177,7 @@ func (p *GoPacketProbe) Run(packetCallback func(gopacket.Packet), e FlowProbeEve

// notify active
if e != nil {
wg.Add(1)
go p.updateStats(p.graph, p.n, statsTicker, statsDone, &wg)

e.OnStarted()
Expand Down
24 changes: 14 additions & 10 deletions topology/probes/lldp/lldp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ type Probe struct {
sync.RWMutex
graph.DefaultGraphListener
g *graph.Graph
hostNode *graph.Node // graph node of the running host
interfaceMap map[string]bool // map interface names to the capturing state
state int64 // state of the probe (running or stopped)
wg sync.WaitGroup // capture goroutines wait group
autoDiscovery bool // capture LLDP traffic on all capable interfaces
hostNode *graph.Node // graph node of the running host
interfaceMap map[string]*probes.GoPacketProbe // map interface names to the packet probes
state int64 // state of the probe (running or stopped)
wg sync.WaitGroup // capture goroutines wait group
autoDiscovery bool // capture LLDP traffic on all capable interfaces
}

type ifreq struct {
Expand Down Expand Up @@ -300,7 +300,7 @@ func (p *Probe) startCapture(ifName, mac string, n *graph.Node) error {
}

// Set capturing state to true
p.interfaceMap[ifName] = true
p.interfaceMap[ifName] = packetProbe

p.wg.Add(1)

Expand All @@ -309,7 +309,7 @@ func (p *Probe) startCapture(ifName, mac string, n *graph.Node) error {
logging.GetLogger().Infof("Stopping LLDP capture on %s", ifName)

p.Lock()
p.interfaceMap[ifName] = false
p.interfaceMap[ifName] = nil
p.Unlock()

p.wg.Done()
Expand Down Expand Up @@ -355,7 +355,7 @@ func (p *Probe) handleNode(n *graph.Node) {
name, _ := n.GetFieldString("Name")

if name != "" && mac != "" && firstLayerType == layers.LayerTypeEthernet {
if active, found := p.interfaceMap[name]; (found || p.autoDiscovery) && !active {
if activeProbe, found := p.interfaceMap[name]; (found || p.autoDiscovery) && activeProbe == nil {
logging.GetLogger().Infof("Starting LLDP capture on %s (MAC: %s)", name, mac)
if err := p.startCapture(name, mac, n); err != nil {
logging.GetLogger().Error(err)
Expand Down Expand Up @@ -412,14 +412,18 @@ func (p *Probe) Start() {
func (p *Probe) Stop() {
p.g.RemoveEventListener(p)
atomic.StoreInt64(&p.state, common.StoppingState)
for intf, activeProbe := range p.interfaceMap {
logging.GetLogger().Debugf("Stopping probe on %s", intf)
activeProbe.Stop()
}
p.wg.Wait()
}

// NewProbe returns a new LLDP probe
func NewProbe(g *graph.Graph, hostNode *graph.Node, interfaces []string) (*Probe, error) {
interfaceMap := make(map[string]bool)
interfaceMap := make(map[string]*probes.GoPacketProbe)
for _, intf := range interfaces {
interfaceMap[intf] = false
interfaceMap[intf] = nil
}

return &Probe{
Expand Down

0 comments on commit 3042e09

Please sign in to comment.