Commit 5d7b5fd6 authored by Robert Ricci's avatar Robert Ricci

Commit new versions from Keith Sklower @ Berkeley.

parent e710181b
......@@ -308,9 +308,9 @@ sub findVlans($@) {
my $self = shift;
my @vlan_ids = @_;
my %mapping = ();
my ($name, $id, $vlan_number, $vlan_name);
my ($count, $name, $id, $vlan_number, $vlan_name) = ( scalar (@vlan_ids));
if ($#vlan_ids == -1) {
if ($count == 0) {
while ( ($id, $name) = each %{$self->{NEW_NAMES}} ) {
$mapping{$id} = $name;
}
......@@ -319,10 +319,10 @@ sub findVlans($@) {
foreach $id (@vlan_ids) {
if (defined($self->{NEW_NAMES}->{$id})) {
$mapping{$id} = $self->{NEW_NAMES}->{$id};
if (--$count == 0) { return %mapping };
}
}
}
#
# Find all VLAN names. Do one to get the first field...
#
......@@ -336,10 +336,13 @@ sub findVlans($@) {
# We only want the names - we ignore everything else
#
if ($name =~ /snVLanByPortCfgVLanName/) {
if (!@vlan_ids || exists $mapping{$vlan_name}) {
if (($count <= 0) || exists $mapping{$vlan_name}) {
$self->debug("Putting in mapping from $vlan_name to " .
"$vlan_number\n");
"$vlan_number\n",2);
$id = $mapping{$vlan_name};
$mapping{$vlan_name} = $vlan_number;
if (!defined($id) && (--$count == 0))
{ return %mapping };
}
}
......@@ -468,10 +471,23 @@ sub setPortVlan($$@) {
my $obj = "snVLanByPortMemberRowStatus.$vlan_number.$port";
my $RetVal = $self->{SESS}->set( $obj, 4);
if (!$RetVal) {
print STDERR "$port VLAN change failed with $RetVal.\n";
if (!defined($RetVal) || !$RetVal) {
# might fail if the port is untagged and a member of another
# VLAN (e.g. Control, when setting up a firewalled experiment)
# so try to clear it out, and then try again.
my $portIndex = $self->{PORTINDEX}->{$port};
$RetVal = $self->{SESS}->get(["snSwPortInfoTagMode",$portIndex]);
$self->debug("TagMode for portIndex $portIndex is $RetVal\n");
if (defined($RetVal) && ($RetVal eq "untagged")) {
$self->debug("2nd chance at $port return $RetVal\n");
$self->clearAllVlansOnTrunk($port);
$RetVal = $self->{SESS}->set( $obj, 4);
if (defined ($RetVal) && $RetVal ) {
next;
}
}
print STDERR "VLAN change for index $port failed\n";
$errors++;
next;
}
}
......@@ -490,8 +506,8 @@ sub setPortVlan($$@) {
#
if (defined($vlan_id)) {
print " Creating VLAN $vlan_id as VLAN #$vlan_number on " .
"$self->{NAME} ... ";
# print " Creating VLAN $vlan_id as VLAN #$vlan_number on " .
# "$self->{NAME} ... ";
my $obj = "snVLanByPortCfgStpMode";
my $RetVal = $self->{SESS}->set( [$obj, $vlan_number,0,"INTEGER"]);
if (!$RetVal) {
......@@ -500,7 +516,7 @@ sub setPortVlan($$@) {
}
$obj = "snVLanByPortCfgVLanName";
$RetVal = $self->{SESS}->set( [$obj, $vlan_number,"$vlan_id","OCTETSTR"]);
if (!$RetVal) {
if (!defined($RetVal) || !$RetVal) {
print STDERR "can't set name for vlan $vlan_number\n";
$errors++;
}
......@@ -950,20 +966,20 @@ sub setVlansOnTrunk($$$$) {
if (grep(/^1$/,@vlan_numbers)) {
die "VLAN 1 passed to setVlansOnTrunk\n";
}
$self->debug("setVlansOnTrunk m $modport v $value nums @vlan_numbers");
$portindex = $self->{PORTINDEX}{$modport};
$ifIndex = $self->{IFINDEX}{$modport};
foreach my $vlan_number (@vlan_numbers) {
$RetVal = undef;
#make sure they are tagged;
if ($value == 1) {
$RetVal = $self->{SESS}->set(
[["snSwPortInfoTagMode",$portindex,"tagged","INTEGER"]]);
if (!$RetVal) {
print STDERR "couldn't tag port $modport\n";
$errors++;
}
$self->debug("foundry::setVlansOnTrunk" .
"m $modport v $value nums @vlan_numbers\n");
($ifIndex) = $self->convertPortFormat($PORT_FORMAT_IFINDEX,$modport);
$portindex = $self->{PORTINDEX}{$ifIndex};
# Make sure port is tagged
if ($value == 1) {
$RetVal = $self->{SESS}->set(
[["snSwPortInfoTagMode",$portindex,"tagged","INTEGER"]]);
if (!defined($RetVal) || !$RetVal) {
print STDERR "couldn't tag port $modport\n";
$errors++;
}
}
foreach my $vlan_number (@vlan_numbers) {
my $action = ($value == 1) ? "create" : "delete" ;
$RetVal = $self->{SESS}->set(
"snVLanByPortMemberRowStatus.$vlan_number.$ifIndex",$action);
......@@ -989,23 +1005,36 @@ sub clearAllVlansOnTrunk($$) {
my $portIndex = $self->{PORTINDEX}{$ifIndex};
my $tag_obj = ["snSwPortInfoTagMode",$portIndex];
my $tag_state = snmpGet($self->{SESS}, $tag_obj);
if ($tag_state == 1 ) {
snmpSet(["snSwPortVlanId",$portIndex,0,"INTEGER"]);
my ($RetVal, $errors, $member_obj, @vlanlist);
$errors = 0;
$self->debug("foundry::clearAllVlansOnTurn modport $modport "
. " ifIndex $ifIndex portIndex $portIndex\n");
my $tag_state = $self->{SESS}->get( $tag_obj);
$self->debug("tag state is $tag_state ");
if (defined($tag_state) && ($tag_state eq "tagged") ) {
$self->{SESS}->set(["snSwPortVlanId",$portIndex,0,"INTEGER"]);
my %vlaninfo = $self->findVlans();
@vlanlist = values %vlaninfo;
} else {
@vlanlist = ( $self->{SESS}->get([["snSwPortVlanId",$portIndex]]) );
}
my @vlaninfo = $self->listVlans();
while ((scalar @vlaninfo)) {
my ($name, $number, @members) = pop @vlaninfo;
if (grep {$_ eq $modport} @members) {
$self->debug("removing port $portIndex from VLAN $number\n",1);
my $obj = "snVLanByPortMemberRowStatus.$number.$portIndex";
my $RetVal = $self->{SESS}->set( $obj, 3);
foreach my $number (@vlanlist) {
if ($number == 1) { next ; }
$member_obj = "snVLanByPortMemberRowStatus.$number.$ifIndex";
$RetVal = $self->{SESS}->get($member_obj);
if (!defined($RetVal)) { next;}
$self->debug("got $RetVal for $member_obj\n",1);
if ($RetVal eq "valid") {
$self->debug("removing port $ifIndex from VLAN $number\n",1);
my $RetVal = $self->{SESS}->set( $member_obj, 3);
if (!defined($RetVal) || ! $RetVal ) {
$errors++;
print STDERR "Couldn't remove $modport from VLAN $number\n";
}
}
}
return ($errors == 0) ;
}
#
# Enable trunking on a port
......@@ -1035,7 +1064,7 @@ sub enablePortTrunking($$$) {
#
# Add this port to the VLAN as a tagged port
#
$rv = $self->setVlansOnTrunk($port, 1, [ $native_vlan] );
$rv = $self->setVlansOnTrunk($port, 1, ( $native_vlan) );
if (!$rv) {
warn "ERROR: Unable to add port $port to VLAN $native_vlan\n";
return 0;
......@@ -1066,9 +1095,6 @@ sub disablePortTrunking($$) {
my ($ifIndex) = $self->convertPortFormat($PORT_FORMAT_IFINDEX,$port);
my $portIndex = $self->{PORTINDEX}{$ifIndex};
my $vlan_obj = ["snSwPortVlanId",$portIndex];
my $tag_obj = ["snSwPortInfoTagMode",$portIndex];
my $tag_state = snmpitGet($self->{SESS},$tag_obj);
my $native_vlan = snmpitGet($self->{SESS},$vlan_obj);
#
......@@ -1079,12 +1105,15 @@ sub disablePortTrunking($$) {
warn "ERROR: Unable to clear VLANs on trunk\n";
return 0;
}
if ($native_vlan != 0) {
$self->setPortVlan($native_vlan, [$port]);
$rv = $self->{SESS}->set(
[["snSwPortInfoTagMode",$portIndex,"untagged","INTEGER"]]);
if (defined($rv)) {
$self->debug("foundry::disablePortTrunking TagMode set to $rv\n");
}
if ($native_vlan > 1) {
$self->setPortVlan($native_vlan, $port);
}
return 1;
}
#
......
This diff is collapsed.
......@@ -17,6 +17,7 @@ use SNMP;
use snmpit_lib;
use libdb;
use libtestbed;
#
# Creates a new object. A list of devices that will be operated on is given
......@@ -136,21 +137,14 @@ sub new($$$@) {
sub lock($) {
my $self = shift;
my $stackid = $self->{STACKID};
foreach my $i (1 .. 20) {
if (symlink("./$$", "/tmp/$stackid")) {
return 1;
} else {
$self->debug("sleeping on lock (/tmp/$stackid)\n");
sleep(3);
}
}
die "Can't lock /tmp/$stackid\n" ;
my $token = "snmpit_$stackid";
my $old_umask = umask(0);
die if (TBScriptLock($token) != TBSCRIPTLOCK_OKAY());
umask($old_umask);
}
sub unlock($) {
my $self = shift;
my $stackid = $self->{STACKID};
return unlink("/tmp/$stackid");
TBScriptUnlock();
}
......@@ -293,19 +287,15 @@ sub setPortVlan($$@) {
sub newVlanNumber($$) {
my $self = shift;
my $vlan_id = shift;
my @vlans = $self->listVlans();
my @numbers = ();
$self->debug("newVlanNumber: vlans @vlans\n");
if ((grep {@$_[0] eq $vlan_id} @vlans) > 0) {
return 0;
}
# foreach my $ref (@vlans) {
# my ($name , $number , $c) = @$ref;
# push @numbers, $number;
# }
@numbers = map { @$_[1] } @vlans ;
$self->debug("stack::newVlanNumber $vlan_id\n");
my %vlans = $self->findVlans();
my $number = $vlans{$vlan_id};
if (defined($number)) { return 0; }
my @numbers = values %vlans;
$self->debug("newVlanNumbers: numbers ". "@numbers" . " \n");
my $number = $self->{MIN_VLAN}-1;
$number = $self->{MIN_VLAN}-1;
my $lim = $self->{MAX_VLAN};
do { ++$number }
until (!(grep {$_ == $number} @numbers) || ($number > $lim));
......@@ -342,10 +332,10 @@ sub createVlan($$$;$$$) {
my ($vlan_number, $res, $devicename, $device);
$vlan_number = $self->newVlanNumber($vlan_id);
if ($vlan_number == 0) { last LOCKBLOCK;}
print " Creating VLAN $vlan_id as VLAN #$vlan_number on " .
"$self->{STACKID} ... ";
%map = mapPortsToDevices(@ports);
foreach $devicename (sort {tbsort($a,$b)} keys %map) {
$self->debug( "Creating VLAN $vlan_id , number $vlan_number " .
"on member switch $devicename\n") ;
$device = $self->{DEVICES}{$devicename};
$res = $device->createVlan($vlan_id, $vlan_number);
if (!$res) {
......@@ -353,6 +343,7 @@ sub createVlan($$$;$$$) {
# Ooops, failed. Don't try any more
#
$okay = 0;
print " Failed\n";
last;
}
}
......@@ -366,16 +357,25 @@ sub createVlan($$$;$$$) {
$okay = 0;
}
}
print " Succeeded\n";
}
$self->unlock();
return $okay;
}
#
# Given VLAN indentifiers from the database, finds the 802.1Q VLAN
# number for them. If no VLAN id is given, returns mappings for the entire
# switch.
#
# usage: findVlans($self, @vlan_ids)
# returns a hash mapping VLAN ids to 802.1Q VLAN numbers
#
sub findVlans($@) {
my $self = shift;
my @vlan_ids = @_;
my ($device, $devicename);
my ($count, $device, $devicename) = (scalar(@vlan_ids));
my %mapping = ();
$self->debug("snmpit_stack::findVlans( @vlan_ids )\n",2);
......@@ -391,6 +391,8 @@ sub findVlans($@) {
} else
{ $mapping{$id} = $num; }
}
if (($count > 0) && ($count = scalar (keys %mapping)))
{ return %mapping ;}
}
return %mapping;
}
......
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