Commit 5db6c935 authored by Robert Ricci's avatar Robert Ricci
Browse files

Two new functions:

getTrunks: Build a structure representing all of the trunks in the switch
	topolgy
getTrunkPath: Find the shortest path between two switches, across the
	trunks found by getTrunks

Note that the getTrunkPath duplicates some work done in assign. However, we
don't currently have a good way to pass this information from assign to
snmpit, and we need to be able to figure this out anyway, in case snmpit
isn't being called as part of experiment swapin.

These functions should soon be used to pick which VLANs are allowed
across which trunks.
parent 248fdcc0
......@@ -39,6 +39,7 @@ my %vlanids=();
sub init($) {
$debug = shift || $debug;
&ReadTranslationTable;
print getTrunkPath(\%trunk,[ keys %trunk ],'cisco1','cisco4'),"\n";
return 0;
}
......@@ -324,6 +325,91 @@ sub getStackType($) {
}
}
#
# Returns a structure representing all trunk links. It's a hash, keyed by
# switch, that contains hash references. Each of the second level hashes
# is keyed by destination, with the value being an array reference that
# contains the card.port pairs to which the trunk is conencted.
#
sub getTrunks() {
my %trunks = ();
my $result = DBQueryFatal("SELECT node_id1, card1, port1, " .
"node_id2, card2, port2 FROM wires WHERE type='Trunk'");
while (my @row = $result->fetchrow()) {
my ($node_id1, $card1, $port1, $node_id2, $card2, $port2) = @row;
push @{ $trunks{$node_id1}{$node_id2} }, "$card1.$port1";
push @{ $trunks{$node_id2}{$node_id1} }, "$card2.$port2";
}
return %trunks;
}
#
# Find the best path from one switch to another. Returns an empty list if no
# path exists, otherwise returns a list of switch names. Arguments are:
# A reference to a hash, as returned by the getTrunks() function
# A reference to an array of unvisited switches: Use [keys %trunks]
# Two siwtch names, the source and the destination
sub getTrunkPath($$$$) {
my ($trunks, $unvisited, $src,$dst) = @_;
if ($src eq $dst) {
#
# The source and destination are the same
#
return ($src);
} elsif ($trunks->{$src}{$dst}) {
#
# The source and destination are directly connected
#
return ($src,$dst);
} else {
# The source and destination aren't directly connected. We'll need to
# recurse across other trunks to find solution
my @minPath = ();
#
# We use the @$unvisited list to pick switches to traverse to, so
# that we don't re-visit switches we've already been to, which would
# cause infinite recursion
#
foreach my $i (0 .. $#{$unvisited}) {
if ($trunks->{$src}{$$unvisited[$i]}) {
#
# We need to pull theswitch out of the unvisted list that we
# pass to it.
#
my @list = @$unvisited;
splice(@list,$i,1);
#
# Check to see if the path we get with this switch is the
# best one found so far
#
my @path = getTrunkPath($trunks,\@list,$$unvisited[$i],$dst);
if (@path && ((!@minPath) || (@path < @minPath))) {
@minPath = @path;
}
}
}
#
# If we found a path, tack ourselves on the front and return. If not,
# return the empty list of failure.
#
if (@minPath) {
return ($src,@minPath);
} else {
return ();
}
}
}
#
# Used to sort a set of nodes in testbed order (ie. pc2 < pc10)
#
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment