Create lldpService then start/stop Rx/Tx

Emit more logging on errors
Eliminated the redundant lldp.Start.
This commit is contained in:
Marc Brooks 2025-11-10 23:45:52 -06:00
parent 96c005b58c
commit 43c1468bcb
No known key found for this signature in database
GPG Key ID: 583A6AF2D6AE1DC6
2 changed files with 20 additions and 40 deletions

View File

@ -54,8 +54,6 @@ type AdvertiseOptions struct {
type Options struct {
InterfaceName string
AdvertiseOptions *AdvertiseOptions
EnableRx bool
EnableTx bool
OnChange func(neighbors []Neighbor)
Logger *zerolog.Logger
}
@ -72,40 +70,15 @@ func NewLLDP(opts *Options) *LLDP {
return &LLDP{
interfaceName: opts.InterfaceName,
advertiseOptions: opts.AdvertiseOptions,
Rx: &RunningState{
Enabled: opts.EnableRx,
},
Tx: &RunningState{
Enabled: opts.EnableTx,
},
rxWaitGroup: &sync.WaitGroup{},
l: opts.Logger,
neighbors: make(map[neighborCacheKey]Neighbor),
onChange: opts.OnChange,
Rx: &RunningState{},
Tx: &RunningState{},
rxWaitGroup: &sync.WaitGroup{},
l: opts.Logger,
}
}
func (l *LLDP) Start() error {
l.mu.RLock()
rxEnabled, txEnabled := l.Rx.Enabled, l.Tx.Enabled
l.mu.RUnlock()
if rxEnabled {
if err := l.startRx(); err != nil {
return fmt.Errorf("failed to start RX: %w", err)
}
}
// Start TX if enabled
if txEnabled {
if err := l.startTx(); err != nil {
return fmt.Errorf("failed to start TX: %w", err)
}
}
return nil
}
// StartRx starts the LLDP receiver if not already running
func (l *LLDP) startRx() error {
l.mu.RLock()

View File

@ -185,12 +185,20 @@ func initNetwork() error {
nm := nmlite.NewNetworkManager(context.Background(), networkLogger)
networkLogger.Info().Interface("networkConfig", nc).Str("hostname", nc.Hostname.String).Str("domain", nc.Domain.String).Msg("initializing network manager")
_ = setHostname(nm, nc.Hostname.String, nc.Domain.String)
if err := setHostname(nm, nc.Hostname.String, nc.Domain.String); err != nil {
networkLogger.Warn().Err(err).Msg("failed to set hostname")
}
nm.SetOnInterfaceStateChange(networkStateChanged)
if err := nm.AddInterface(NetIfName, nc); err != nil {
return fmt.Errorf("failed to add interface: %w", err)
}
_ = nm.CleanUpLegacyDHCPClients()
if err := nm.CleanUpLegacyDHCPClients(); err != nil {
networkLogger.Warn().Err(err).Msg("failed to clean up legacy DHCP clients")
}
networkManager = nm
@ -202,8 +210,6 @@ func initNetwork() error {
advertiseOptions := getLLDPAdvertiseOptions(ifState)
lldpService = lldp.NewLLDP(&lldp.Options{
InterfaceName: NetIfName,
EnableRx: nc.ShouldEnableLLDPReceive(),
EnableTx: nc.ShouldEnableLLDPTransmit(),
AdvertiseOptions: advertiseOptions,
OnChange: func(neighbors []lldp.Neighbor) {
// TODO: send deltas instead of the whole list
@ -212,8 +218,9 @@ func initNetwork() error {
Logger: networkLogger,
})
if err := lldpService.Start(); err != nil {
networkLogger.Error().Err(err).Msg("failed to start LLDP service")
// this will start up the LLDP Tx and Rx as needed
if err := lldpService.SetRxAndTx(nc.ShouldEnableLLDPReceive(), nc.ShouldEnableLLDPTransmit()); err != nil {
networkLogger.Error().Err(err).Msg("failed to initialize LLDP RX and TX")
}
return nil
@ -234,7 +241,7 @@ func updateLLDPOptions(nc *types.NetworkConfig, ifState *types.InterfaceState) {
}
if err := lldpService.SetRxAndTx(nc.ShouldEnableLLDPReceive(), nc.ShouldEnableLLDPTransmit()); err != nil {
networkLogger.Error().Err(err).Msg("failed to set LLDP RX and TX")
networkLogger.Error().Err(err).Msg("failed to update LLDP RX and TX")
}
if ifState == nil {