mirror of https://github.com/jetkvm/kvm.git
81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package lldp
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type ManagementAddress struct {
|
|
AddressFamily string `json:"address_family"`
|
|
Address string `json:"address"`
|
|
InterfaceSubtype string `json:"interface_subtype"`
|
|
InterfaceNumber uint32 `json:"interface_number"`
|
|
OID string `json:"oid,omitempty"`
|
|
}
|
|
|
|
type Neighbor struct {
|
|
Mac string `json:"mac"`
|
|
Source string `json:"source"`
|
|
ChassisID string `json:"chassis_id"`
|
|
PortID string `json:"port_id"`
|
|
PortDescription string `json:"port_description"`
|
|
SystemName string `json:"system_name"`
|
|
SystemDescription string `json:"system_description"`
|
|
TTL uint16 `json:"ttl"`
|
|
ManagementAddress *ManagementAddress `json:"management_address,omitempty"`
|
|
Capabilities []string `json:"capabilities"`
|
|
Values map[string]string `json:"values"`
|
|
}
|
|
|
|
func (n *Neighbor) cacheKey() string {
|
|
return fmt.Sprintf("%s-%s", n.Mac, n.Source)
|
|
}
|
|
|
|
func (l *LLDP) addNeighbor(neighbor *Neighbor, ttl time.Duration) {
|
|
logger := l.l.With().
|
|
Str("mac", neighbor.Mac).
|
|
Interface("neighbor", neighbor).
|
|
Logger()
|
|
|
|
key := neighbor.cacheKey()
|
|
|
|
current_neigh := l.neighbors.Get(key)
|
|
if current_neigh != nil {
|
|
current_source := current_neigh.Value().Source
|
|
if current_source == "lldp" && neighbor.Source != "lldp" {
|
|
logger.Info().Msg("skip updating neighbor, as LLDP has higher priority")
|
|
return
|
|
}
|
|
}
|
|
|
|
logger.Info().Msg("adding neighbor")
|
|
l.neighbors.Set(key, *neighbor, ttl)
|
|
}
|
|
|
|
func (l *LLDP) deleteNeighbor(neighbor *Neighbor) {
|
|
logger := l.l.With().
|
|
Str("mac", neighbor.Mac).
|
|
Logger()
|
|
|
|
logger.Info().Msg("deleting neighbor")
|
|
l.neighbors.Delete(neighbor.cacheKey())
|
|
}
|
|
|
|
func (l *LLDP) GetNeighbors() []Neighbor {
|
|
items := l.neighbors.Items()
|
|
neighbors := make([]Neighbor, 0, len(items))
|
|
|
|
for _, item := range items {
|
|
neighbors = append(neighbors, item.Value())
|
|
}
|
|
|
|
// sort based on MAC address
|
|
sort.Slice(neighbors, func(i, j int) bool {
|
|
return strings.Compare(neighbors[i].Mac, neighbors[j].Mac) > 0
|
|
})
|
|
|
|
return neighbors
|
|
}
|