Fix switch-to-switch link detection by finding all valid last hops
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
33
link.go
33
link.go
@@ -76,30 +76,36 @@ func (n *Nodes) getDirectLinks() []*Link {
|
|||||||
}
|
}
|
||||||
seenMACs[mac] = true
|
seenMACs[mac] = true
|
||||||
|
|
||||||
lastHop, lastPort := n.findLastHop(target, mac, macToNode)
|
lastHops := n.findAllLastHops(target, mac, macToNode)
|
||||||
if lastHop == nil {
|
for _, lh := range lastHops {
|
||||||
continue
|
targetIface := n.findTargetInterface(target, lh.node, macToNode)
|
||||||
}
|
key := makeLinkKey(lh.node, lh.port, target, targetIface)
|
||||||
|
|
||||||
targetIface := n.findTargetInterface(target, lastHop, macToNode)
|
|
||||||
key := makeLinkKey(lastHop, lastPort, target, targetIface)
|
|
||||||
if !seen[key] {
|
if !seen[key] {
|
||||||
seen[key] = true
|
seen[key] = true
|
||||||
links = append(links, &Link{
|
links = append(links, &Link{
|
||||||
TypeID: newTypeID("link"),
|
TypeID: newTypeID("link"),
|
||||||
NodeA: lastHop,
|
NodeA: lh.node,
|
||||||
InterfaceA: lastPort,
|
InterfaceA: lh.port,
|
||||||
NodeB: target,
|
NodeB: target,
|
||||||
InterfaceB: targetIface,
|
InterfaceB: targetIface,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return links
|
return links
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Nodes) findLastHop(target *Node, mac string, macToNode map[string]*Node) (*Node, string) {
|
func (n *Nodes) findAllLastHops(target *Node, mac string, macToNode map[string]*Node) []struct {
|
||||||
|
node *Node
|
||||||
|
port string
|
||||||
|
} {
|
||||||
|
var results []struct {
|
||||||
|
node *Node
|
||||||
|
port string
|
||||||
|
}
|
||||||
|
|
||||||
for _, node := range n.nodes {
|
for _, node := range n.nodes {
|
||||||
port, sees := node.MACTable[mac]
|
port, sees := node.MACTable[mac]
|
||||||
if !sees || node == target {
|
if !sees || node == target {
|
||||||
@@ -107,10 +113,13 @@ func (n *Nodes) findLastHop(target *Node, mac string, macToNode map[string]*Node
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !n.hasCloserNode(node, target, mac, port, macToNode) {
|
if !n.hasCloserNode(node, target, mac, port, macToNode) {
|
||||||
return node, port
|
results = append(results, struct {
|
||||||
|
node *Node
|
||||||
|
port string
|
||||||
|
}{node, port})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, ""
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Nodes) hasCloserNode(node, target *Node, mac, port string, macToNode map[string]*Node) bool {
|
func (n *Nodes) hasCloserNode(node, target *Node, mac, port string, macToNode map[string]*Node) bool {
|
||||||
|
|||||||
@@ -112,6 +112,19 @@
|
|||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.node .root-label {
|
||||||
|
position: absolute;
|
||||||
|
top: -8px;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
font-size: 9px;
|
||||||
|
background: #664;
|
||||||
|
color: #ffa;
|
||||||
|
padding: 1px 6px;
|
||||||
|
border-radius: 8px;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
.node:hover {
|
.node:hover {
|
||||||
filter: brightness(1.2);
|
filter: brightness(1.2);
|
||||||
}
|
}
|
||||||
@@ -277,7 +290,12 @@
|
|||||||
labelEl.textContent = getLabel(node);
|
labelEl.textContent = getLabel(node);
|
||||||
div.appendChild(labelEl);
|
div.appendChild(labelEl);
|
||||||
|
|
||||||
if (isSwitch(node) && uplinkInfo) {
|
if (isSwitch(node) && uplinkInfo === 'ROOT') {
|
||||||
|
const rootEl = document.createElement('div');
|
||||||
|
rootEl.className = 'root-label';
|
||||||
|
rootEl.textContent = 'ROOT';
|
||||||
|
div.appendChild(rootEl);
|
||||||
|
} else if (isSwitch(node) && uplinkInfo) {
|
||||||
const uplinkEl = document.createElement('div');
|
const uplinkEl = document.createElement('div');
|
||||||
uplinkEl.className = 'uplink';
|
uplinkEl.className = 'uplink';
|
||||||
uplinkEl.textContent = uplinkInfo.localPort + ' → ' + uplinkInfo.parentName + ':' + uplinkInfo.remotePort;
|
uplinkEl.textContent = uplinkInfo.localPort + ' → ' + uplinkInfo.parentName + ':' + uplinkInfo.remotePort;
|
||||||
@@ -447,10 +465,18 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
let bestRoot = allSwitches[0];
|
for (const edges of adjacency.values()) {
|
||||||
|
edges.sort((a, b) => getLabel(a.neighbor).localeCompare(getLabel(b.neighbor)));
|
||||||
|
}
|
||||||
|
|
||||||
|
const sortedSwitches = [...allSwitches].sort((a, b) =>
|
||||||
|
getLabel(a).localeCompare(getLabel(b)));
|
||||||
|
|
||||||
|
let bestRoot = sortedSwitches[0];
|
||||||
|
let bestReachable = 0;
|
||||||
let bestMaxDepth = Infinity;
|
let bestMaxDepth = Infinity;
|
||||||
|
|
||||||
allSwitches.forEach(candidate => {
|
for (const candidate of sortedSwitches) {
|
||||||
const visited = new Set([candidate.typeid]);
|
const visited = new Set([candidate.typeid]);
|
||||||
const queue = [{ sw: candidate, depth: 0 }];
|
const queue = [{ sw: candidate, depth: 0 }];
|
||||||
let maxDepth = 0;
|
let maxDepth = 0;
|
||||||
@@ -466,11 +492,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxDepth < bestMaxDepth) {
|
const reachable = visited.size;
|
||||||
|
if (reachable > bestReachable ||
|
||||||
|
(reachable === bestReachable && maxDepth < bestMaxDepth)) {
|
||||||
|
bestReachable = reachable;
|
||||||
bestMaxDepth = maxDepth;
|
bestMaxDepth = maxDepth;
|
||||||
bestRoot = candidate;
|
bestRoot = candidate;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
switchUplinks.set(bestRoot.typeid, 'ROOT');
|
||||||
|
|
||||||
const visited = new Set([bestRoot.typeid]);
|
const visited = new Set([bestRoot.typeid]);
|
||||||
const queue = [bestRoot];
|
const queue = [bestRoot];
|
||||||
@@ -489,6 +520,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const container = document.getElementById('container');
|
const container = document.getElementById('container');
|
||||||
|
|||||||
Reference in New Issue
Block a user