Commit b422d288 authored by Weibin Sun's avatar Weibin Sun
Browse files

bug fix for switch trunk path, also snmpit_remote friendly

parent b57f385e
......@@ -660,25 +660,6 @@ if (@ports) {
die "Bad format for port ".join(" ", @optports)."\n";
}
# foreach my $port (@ports) {
# if ($port =~ /^([^:]+):\d+$/) {
# push @nodes, $1;
# } elsif ($port =~ /^([^.]+)\.\d+(\/\d+)?$/) {
# push @switchports, $port;
# } elsif ($port =~ /^\d+\/\d+?$/) {
# if ($opt{i} && @{$opt{i}} == 1) {
# $port = $opt{i}->[0] . "." . $port;
# push @switchports, $port;
# } else {
# die "The module/port format is only legal if exactly one -i " .
# "argument has been given\n";
# }
# } else {
# tbreport(SEV_ERROR, 'bad_data', 'port', $port);
# die "Bad format for port $port\n"
# }
# }
if ($UID && !TBNodeAccessCheck($UID,TB_NODEACCESS_MODIFYVLANS,@nodes)) {
tbreport(SEV_ERROR, 'insufficient_permissions');
die "You do not have permission to modify some or all of the nodes\n" .
......@@ -855,20 +836,13 @@ COMMAND: foreach my $command (@commands) {
my @newvlans = getExperimentVlans($pid,$eid,@optvlanids);
my @oldvlans;
VLan->StaleVlanList($experiment,\@oldvlans);
@vlans = uniq(@newvlans, @oldvlans);
if ($supplied_switches) {
debug("Sync: using supplied switches\n");
@devicenames = @supplied_switches;
@vlans = filterVlansBySwitches(\@supplied_switches, @vlans);
if (!@vlans) {
print "snmpit: $pid/$eid has no VLANs to sync ".
"on supplied devices, skipping\n"
if (!$quiet);
next COMMAND;
}
} else {
@devicenames = mapVlansToSwitches(@vlans);
die("-X and -i cannot be used together\n");
}
else {
@devicenames = uniq(mapVlansToSwitches(@newvlans),
mapStaleVlansToSwitches(@oldvlans));
debug("Sync: list from database: " . join(",",@devicenames) .
"\n");
}
......@@ -945,7 +919,7 @@ COMMAND: foreach my $command (@commands) {
}
debug("Device names: " . join(",",@devicenames) . "\n");
debug("Ports: " . join(",", map($_->toIfaceString(), @ports)) . "\n"); #@@@@ debug("Ports: " . join(",",@ports) . "\n");
debug("Ports: " . join(",", map($_->toIfaceString(), @ports)) . "\n");
if (@vlans) { debug("VLANs: " . join(",",@vlans) . "\n") };
my %stacks = ();
......@@ -1244,9 +1218,13 @@ sub doListVlans ($) {
my @results;
foreach my $pair (split ',', $opt{L}) {
my ($out,$in) = split "#", $pair;
my $vlan = "$in#" . ${$vlans{$out}}[0] . "#" .
Port->toStrings(@{${$vlans{$out}}[1]});
# join(' ', @{${$vlans{$out}}[1]});
my $vlan = "$in#" . ${$vlans{$out}}[0] . "#";
my $memberref = ${$vlans{$out}}[1];
if (scalar(@$memberref) > 0 && ref(${@$memberref}[0])) {
$vlan = $vlan.Port->toStrings(@$memberref);
} else {
$vlan = join(' ', @$memberref);
}
push @results, $vlan;
}
print join(',', @results);
......@@ -1347,17 +1325,25 @@ $vlan_id,$ddep, $pideid, $vname, $members
#
if ($opt{M}) {
# Rather than node:port, print out node:MAC (if we know the MAC)
$members = join(" ", map
{
if (scalar(@$memberref) > 0 && ref(${@$memberref}[0])) {
$members = join(" ", map
{
$_->mac()? $_->node_id().":".$_->mac():$_->toString()
} @$memberref);
#$members = join(" ", map
#{
#macport($_)? (split /:/)[0] . ":" . macport($_) : $_
#} @$memberref);
} @$memberref);
} else {
my @ptobjs = map {Port->LookupByStringForced($_)} @$memberref;
$members = join(" ", map
{
defined($_)?($_->mac()? $_->node_id().":".$_->mac():$_->toString()):""
} @ptobjs);
}
} else {
$members = Port->toStrings(@$memberref); #join(" ",@$memberref);
if (scalar(@$memberref) > 0 && ref(${@$memberref}[0])) {
$members = Port->toStrings(@$memberref);
} else {
$members = join(" ",@$memberref);
}
}
write;
}
......@@ -1605,7 +1591,8 @@ sub doRestorePortStatus($@) {
print "Could not yank $port from $source_vlan\n";
$errors++;
}
VLan->RecordVLanModification($source_vlan->id(), undef, [$port->toIfaceString()]);
my @tmp = ($port->toIfaceString());
VLan->RecordVLanModification($source_vlan->id(), undef, \@tmp);
}
if (defined($target_vlan) && $target_vlan->KeepInSync()) {
if ($target_vlan->AddPort($port)) {
......@@ -2506,8 +2493,6 @@ sub doSyncVlansWithDB($) {
foreach my $port (@$memberref) {
push(@{ $allports{$vlan_id} }, $port);
# Convert the ports to node:iface for the next call.
# push(@{ $portmap{$vlan_id} }, portiface($port))
# if ((!($port =~ /\./)) && portiface($port));
push (@{ $portmap{$vlan_id} }, $port);
}
}
......@@ -3128,7 +3113,8 @@ sub doPortControl($$@) {
# As below, only one stack.
my ($stack) = @$stacks;
#@@@@ not sure about the port format:
$errors = RemoteDoPortControl($stack,$command, map($_->toIfaceString(), @ports));
$errors = RemoteDoPortControl($stack,$command, @optports);
#$errors = RemoteDoPortControl($stack,$command, map($_->toIfaceString(), @ports));
goto finish;
}
......@@ -3240,7 +3226,8 @@ sub doTrunkEnable($$$@) {
# As below, only one stack.
my ($stack) = @$stacks;
# not sure about the port format:
$errors = RemoteDoTrunking($stack,$mode,$port->toIfaceString(),@vlans);
$errors = RemoteDoTrunking($stack,$mode,$optports[0],@vlans);
#$errors = RemoteDoTrunking($stack,$mode,$port->toIfaceString(),@vlans);
goto finish;
}
#
......@@ -3278,7 +3265,8 @@ sub doTrunkDisable($$) {
my @vlans=();
# As below, only one stack.
my ($stack) = @$stacks;
$errors = RemoteDoTrunking($stack,"-U",$port,@vlans);
$errors = RemoteDoTrunking($stack,"-U",$optports[0],@vlans);
#$errors = RemoteDoTrunking($stack,"-U",$port,@vlans);
goto finish;
}
#
......
......@@ -432,16 +432,24 @@ sub convertPortFormat($$@) {
return @pos;
}
} elsif ($input == $PORT_FORMAT_PORT) {
if ($output == $PORT_FORMAT_IFINDEX) {
$self->debug("Converting nodeport to ifindex\n",3);
return map $self->{IFINDEX}{(split /:/, $_->getSwitchPort()->toTripleString())[1]}, @ports;
} elsif ($output == $PORT_FORMAT_MODPORT) {
$self->debug("Converting nodeport to modport\n",3);
return map { (split /:/, $_->getSwitchPort()->toTripleString())[1] } @ports;
} elsif ($output == $PORT_FORMAT_NODEPORT) {
$self->debug("Converting modport to nodeport\n",3);
return map $_->getPCPort()->toTripleString(), @ports;
}
if ($output == $PORT_FORMAT_IFINDEX) {
$self->debug("Converting port to ifindex\n",3);
return map $self->{IFINDEX}{(split /:/,
($_->node_id() eq $self->{NAME})?
$_->toTripleString():
$_->getOtherEndTripleString()
)[1]}, @ports;
} elsif ($output == $PORT_FORMAT_MODPORT) {
$self->debug("Converting port to modport\n",3);
return map { (split /:/,
($_->node_id() eq $self->{NAME})?
$_->toTripleString():
$_->getOtherEndTripleString()
)[1] } @ports;
} elsif ($output == $PORT_FORMAT_NODEPORT) {
$self->debug("Converting port to nodeport\n",3);
return map $_->getPCPort()->toTripleString(), @ports;
}
}
#
......
......@@ -525,15 +525,24 @@ sub convertPortFormat($$@) {
}
} elsif ($input == $PORT_FORMAT_PORT) {
if ($output == $PORT_FORMAT_IFINDEX) {
$self->debug("Converting nodeport to ifindex\n",3);
return map $self->{IFINDEX}{(split /:/, $_->getSwitchPort()->toTripleString())[1]}, @ports;
} elsif ($output == $PORT_FORMAT_MODPORT) {
$self->debug("Converting nodeport to modport\n",3);
return map { (split /:/, $_->getSwitchPort()->toTripleString())[1] } @ports;
} elsif ($output == $PORT_FORMAT_NODEPORT) {
$self->debug("Converting modport to nodeport\n",3);
return map $_->getPCPort()->toTripleString(), @ports;
}
$self->debug("Converting port to ifindex\n",3);
return map $self->{IFINDEX}{(split /:/,
($_->node_id() eq $self->{NAME})?
$_->toTripleString():
$_->getOtherEndTripleString()
)[1]}, @ports;
} elsif ($output == $PORT_FORMAT_MODPORT) {
$self->debug("Converting port to modport\n",3);
return map { (split /:/,
($_->node_id() eq $self->{NAME})?
$_->toTripleString():
$_->getOtherEndTripleString()
)[1] } @ports;
} elsif ($output == $PORT_FORMAT_NODEPORT) {
$self->debug("Converting port to nodeport\n",3);
return map $_->getPCPort()->toTripleString(), @ports;
}
}
#
......
......@@ -36,7 +36,7 @@ use Exporter;
uniq isSwitchPort getPathVlanIfaces
reserveVlanTag getReservedVlanTag clearReservedVlanTag
convertPortFromString convertPortsFromStrings
mapVlansToSwitches
mapVlansToSwitches mapStaleVlansToSwitches
);
use English;
......@@ -1105,7 +1105,7 @@ sub getDeviceOptions($) {
# #@@@@ After port refactoring:
# ( 'src' => { 'dst' => [ port1, port2 ] }, ... )
#
sub getTrunks() { #@@@@ OK
sub getTrunks() {
my %trunks = ();
......@@ -1115,15 +1115,6 @@ sub getTrunks() { #@@@@ OK
push @{ $trunks{$p->node_id()}{$p->other_end_node_id()} }, $p;
}
#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;
}
......@@ -1135,7 +1126,7 @@ sub getTrunks() { #@@@@ OK
# A reference to an array of unvisited switches: Use [keys %trunks]
# Two siwtch names, the source and the destination
#
sub getTrunkPath($$$$) { #@@@@ OK
sub getTrunkPath($$$$) {
my ($trunks, $unvisited, $src,$dst) = @_;
if ($src eq $dst) {
#
......@@ -1201,49 +1192,85 @@ sub mapVlansToSwitches(@)
{
my @vlan_ids = @_;
my %switches = ();
my %trunks = getTrunks();
#
# This code is lifted from setPortVlan() in snmpit_stack.pm
#
foreach my $vlan_id (@vlan_ids) {
my %devices = ();
my @ports = getVlanPorts($vlan_id);
my %map = mapPortsToDevices(@ports);
my @ports = uniq(getVlanPorts($vlan_id),
getExperimentVlanPorts($vlan_id));
my @devices = mapPortsToSwitches(@ports);
foreach my $device (keys %map) {
$devices{$device} = 1;
# And update the total set of switches.
foreach my $device (@devices) {
$switches{$device} = 1;
}
}
my @sorted = sort {tbsort($a,$b)} keys %switches;
print "mapVlansToSwitches: @sorted\n";
return @sorted;
}
#
# An alternate version for a "stale" vlan; one that is destroyed cause of
# a swapmod (syncVlansFromTables).
#
sub mapStaleVlansToSwitches(@)
{
my @vlan_ids = @_;
my %switches = ();
foreach my $vlan_id (@vlan_ids) {
#
# Add in the ports that we think are already in the vlan, since
# Get the ports that we think are already in the vlan, since
# this might be a remove/modify operation. Can probably optimize
# this.
#
@ports = getExperimentVlanPorts($vlan_id);
%map = mapPortsToDevices(@ports);
foreach my $device (keys %map) {
$devices{$device} = 1;
}
#
# Find out every switch which might have to transit this VLAN through
# its trunks.
#
my @trunks = getTrunksFromSwitches(\%trunks, keys %devices);
foreach my $trunk (@trunks) {
my ($src,$dst) = @$trunk;
$devices{$src} = $devices{$dst} = 1;
}
my @ports = getExperimentVlanPorts($vlan_id);
my @devices = mapPortsToSwitches(@ports);
# And update the total set of switches.
foreach my $device (keys(%devices)) {
foreach my $device (@devices) {
$switches{$device} = 1;
}
}
my @sorted = sort {tbsort($a,$b)} keys %switches;
print "mapVlansToSwitches: @sorted\n";
print "mapStaleVlansToSwitches: @sorted\n";
return @sorted;
}
#
# Map a set of ports to the devices they are on plus the trunks.
# See above.
#
sub mapPortsToSwitches(@)
{
my @ports = @_;
my %switches = ();
my %trunks = getTrunks();
my %map = mapPortsToDevices(@ports);
my %devices = ();
foreach my $device (keys %map) {
$devices{$device} = 1;
}
#
# This code is lifted from setPortVlan() in snmpit_stack.pm
#
# Find every switch which might have to transit this VLAN through
# its trunks.
#
my @trunks = getTrunksFromSwitches(\%trunks, keys %devices);
foreach my $trunk (@trunks) {
my ($src,$dst) = @$trunk;
$devices{$src} = $devices{$dst} = 1;
}
# And update the total set of switches.
foreach my $device (keys(%devices)) {
$switches{$device} = 1;
}
my @sorted = sort {tbsort($a,$b)} keys %switches;
return @sorted;
}
......
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