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:
Ian Gulliver
2026-01-25 18:01:43 -08:00
parent b503c96252
commit d7bb971edd
2 changed files with 65 additions and 24 deletions

47
link.go
View File

@@ -76,22 +76,20 @@ 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)
if !seen[key] {
targetIface := n.findTargetInterface(target, lastHop, macToNode) seen[key] = true
key := makeLinkKey(lastHop, lastPort, target, targetIface) links = append(links, &Link{
if !seen[key] { TypeID: newTypeID("link"),
seen[key] = true NodeA: lh.node,
links = append(links, &Link{ InterfaceA: lh.port,
TypeID: newTypeID("link"), NodeB: target,
NodeA: lastHop, InterfaceB: targetIface,
InterfaceA: lastPort, })
NodeB: target, }
InterfaceB: targetIface,
})
} }
} }
} }
@@ -99,7 +97,15 @@ func (n *Nodes) getDirectLinks() []*Link {
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 {

View File

@@ -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');