mirror of https://github.com/jetkvm/kvm.git
fix online state detection
This commit is contained in:
parent
05f2e5babe
commit
6743db6e3d
|
|
@ -690,24 +690,15 @@ func (im *InterfaceManager) updateInterfaceState() error {
|
||||||
return fmt.Errorf("failed to get interface: %w", err)
|
return fmt.Errorf("failed to get interface: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if state changed
|
||||||
|
stateChanged := false
|
||||||
|
|
||||||
attrs := nl.Attrs()
|
attrs := nl.Attrs()
|
||||||
isUp := attrs.OperState == netlink.OperUp
|
isUp := attrs.OperState == netlink.OperUp
|
||||||
|
|
||||||
// check if the interface has unicast addresses
|
// check if the interface has unicast addresses
|
||||||
hasAddrs := false
|
isOnline := isUp && nl.HasGlobalUnicastAddress()
|
||||||
addrs, err := nl.AddrList(link.AfUnspec)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get addresses: %w", err)
|
|
||||||
}
|
|
||||||
for _, addr := range addrs {
|
|
||||||
if addr.IP.IsGlobalUnicast() {
|
|
||||||
hasAddrs = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if state changed
|
|
||||||
stateChanged := false
|
|
||||||
// We should release the lock before calling the callbacks
|
// We should release the lock before calling the callbacks
|
||||||
// to avoid deadlocks
|
// to avoid deadlocks
|
||||||
im.stateMu.Lock()
|
im.stateMu.Lock()
|
||||||
|
|
@ -715,8 +706,9 @@ func (im *InterfaceManager) updateInterfaceState() error {
|
||||||
im.state.Up = isUp
|
im.state.Up = isUp
|
||||||
stateChanged = true
|
stateChanged = true
|
||||||
}
|
}
|
||||||
if im.state.Online != hasAddrs {
|
|
||||||
im.state.Online = hasAddrs
|
if im.state.Online != isOnline {
|
||||||
|
im.state.Online = isOnline
|
||||||
stateChanged = true
|
stateChanged = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -762,7 +754,9 @@ func (im *InterfaceManager) updateIPAddresses(nl *link.Link) error {
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
im.logger.Debug().Str("address", addr.IP.String()).Msg("checking address")
|
im.logger.Debug().
|
||||||
|
IPAddr("address", addr.IP).
|
||||||
|
Msg("checking address")
|
||||||
if addr.IP.To4() != nil {
|
if addr.IP.To4() != nil {
|
||||||
// IPv4 address
|
// IPv4 address
|
||||||
ipv4Addresses = append(ipv4Addresses, addr.IPNet.String())
|
ipv4Addresses = append(ipv4Addresses, addr.IPNet.String())
|
||||||
|
|
@ -811,6 +805,7 @@ func (im *InterfaceManager) updateStateFromDHCPLease(lease *types.DHCPLease) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReconcileLinkAddrs reconciles the link addresses
|
||||||
func (im *InterfaceManager) ReconcileLinkAddrs(addrs []*types.IPAddress) error {
|
func (im *InterfaceManager) ReconcileLinkAddrs(addrs []*types.IPAddress) error {
|
||||||
nl := getNetlinkManager()
|
nl := getNetlinkManager()
|
||||||
link, err := im.link()
|
link, err := im.link()
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,21 @@ func (l *Link) AddrList(family int) ([]netlink.Addr, error) {
|
||||||
return netlink.AddrList(l.Link, family)
|
return netlink.AddrList(l.Link, family)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasGlobalUnicastAddress returns true if the link has a global unicast address
|
||||||
|
func (l *Link) HasGlobalUnicastAddress() bool {
|
||||||
|
addrs, err := l.AddrList(AfUnspec)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if addr.IP.IsGlobalUnicast() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// IsSame checks if the link is the same as another link
|
// IsSame checks if the link is the same as another link
|
||||||
func (l *Link) IsSame(other *Link) bool {
|
func (l *Link) IsSame(other *Link) bool {
|
||||||
if l == nil || other == nil {
|
if l == nil || other == nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue