Commit 0574ac76 authored by Weibin Sun's avatar Weibin Sun

done development, try it now

parent c6b0b06e
......@@ -665,7 +665,24 @@ sub createVlan($$$) {
#
# Put the given ports in the given VLAN. The VLAN is given as an 802.1Q
# tag number.
#
############################################################
# Semantics:
#
# Case mode(port):
# 'free' or 'in default untagged':
# add port to vlan_number untagged.
# 'in use(not in default) untagged':
# add port to vlan_number untagged.
# 'in use(not in default) all tagged':
# add port to vlan_number tagged.
# 'in use(may in default) native tagged':
# add port to vlan_number tagged;
# if native_vlan == default:
# remove native_vlan
#
# Arista 'free': switchportMode='access' AND accessVlan=1
#
############################################################
# usage: setPortVlan($self, $vlan_number, @ports)
# returns 0 on sucess.
# returns the number of failed ports on failure.
......@@ -734,7 +751,9 @@ sub delPortVlan($$@) {
my @swports = $self->convertPortFormat($PORT_FORMAT_SYSDB, @ports);
my $resp = $self->callRPC($id, 'removePortsFromVlan', $vlan_number, \@swports);
my $resp = $self->callRPC($id,
'removePortsFromVlan',
$vlan_number, \@swports);
if (defined($resp)) {
$errors = $resp;
......@@ -767,7 +786,9 @@ sub removePortsFromVlan($@) {
$errors++;
}
my $resp = $self->callRPC($id, 'removePortsFromVlan', $vlan_number, []);
my $resp = $self->callRPC($id,
'removePortsFromVlan',
$vlan_number, []);
if (defined($resp)) {
$errors += $resp;
......@@ -783,6 +804,18 @@ sub removePortsFromVlan($@) {
# Removes and disables some ports in a given VLAN.
# The VLAN is given as a VLAN 802.1Q tag value.
#
# Semantics:
# Case:
# untagged:
# move to default VLAN, put port down
# alltagged:
# untag port
# nativetagged:
# remove native vlan:
# clear native
# nonative vlan:
# untag
#
# usage: removeSomePortsFromVlan(self,vlan,@ports)
# returns 0 on sucess.
# returns the number of failed ports on failure.
......@@ -1064,13 +1097,29 @@ sub setVlansOnTrunk($$$$) {
my ($self, $modport, $value, @vlan_numbers) = @_;
my $id = $self->{NAME} . "::setVlansOnTrunk";
#
# Some error checking (from HP)
#
if (($value != 1) && ($value != 0)) {
warn "Invalid value $value passed to setVlansOnTrunk\n";
return 0;
}
if (grep(/^1$/,@vlan_numbers)) {
warn "VLAN 1 passed to setVlansOnTrunk\n";
return 0;
}
my ($swport) = $self->convertPortFormat($PORT_FORMAT_SYSDB, $modport);
my $resp = $self->callRPC($id, 'setAllowedVlansOnTrunkedPort', $swport,
($value?1:0), \@vlan_numbers);
if ($resp) {
return 1;
} else {
if (int($resp) == -1) {
warn "$id: port $modport is normal port, " .
"refusing to add vlan(s) @vlan_numbers\n";
}
return 0;
} else {
return 1;
}
}
......@@ -1090,6 +1139,16 @@ sub enablePortTrunking2($$$$) {
my $id = $self->{NAME} .
"::enablePortTrunking($port,$native_vlan,$equaltrunking)";
if ((!$equaltrunking) &&
(!defined($native_vlan) || ($native_vlan <= 1))) {
warn "$id: inappropriate or missing PVID for trunk\n";
return 0;
}
if (!defined($native_vlan)) {
$native_vlan = -1
}
my ($swport) = $self->convertPortFormat($PORT_FORMAT_SYSDB, $port);
my $resp = $self->callRPC($id, 'enablePortTrunking',
$swport, $native_vlan, ($equaltrunking?1:0));
......
......@@ -177,6 +177,8 @@ def _removeVlan(vlan_num):
# Assign ports to a VLAN, and enable them.
#
# Return #errors.
#
# Port-channel safe; tagging safe.
#
def _setPortVlan(vlan_num, ports):
errors = 0
......@@ -205,8 +207,9 @@ def _setPortVlan(vlan_num, ports):
else:
set_mode = 'Move'
else: # trunk mode
if pbc.trunkNativeVlan == 0:
if pbc.trunkNativeVlan == 0 or pbc.trunkNativeVlan == 1:
set_mode = 'AllTagAdd'
pbc.trunkNativeVlan = 0
else:
set_mode = 'TagWithUntagNative'
else:
......@@ -242,6 +245,8 @@ def _setPortVlan(vlan_num, ports):
# Return #errors.
# If ports is empty, remove all ports from the VLAN.
#
# Port-channel safe; tagging safe.
#
def _removePortsFromVlan(vlan_num, ports, leavetrunk = False):
errors = 0
sign = "_removePortsFromVlan("+str(vlan_num)+","+str(ports)+")"
......@@ -255,32 +260,23 @@ def _removePortsFromVlan(vlan_num, ports, leavetrunk = False):
for p in ports:
try:
pbc = bc.switchIntfConfig.newMember(p)
if p in bc.switchIntfConfig.keys():
pbc = bc.switchIntfConfig.get(p)
if pbc.switchportMode == 'access':
if pbc.accessVlan == int(vlan_num):
pbc.accessVlan = DEFAULT_VLAN_NUM
setPortAdminStatus(sysdbroot, p, 0)
else:
debug("Warnning: port %s not in VLAN %d"
%(str(p), int(vlan_num)))
else: # trunk mode
if pbc.trunkNativeVlan == int(vlan_num):
pbc.trunkNativeVlan = 0
disableVlanOnTrunkedPort(sysdbroot, p, vlan_num)
except:
debug("removePortsFromVlan: Error when getting port %s - %s"
%(str(p), str(sys_exc_info())))
errors += 1
else:
if pbc.switchportMode == 'access':
if pbc.accessVlan == int(vlan_num):
pbc.accessVlan = DEFAULT_VLAN_NUM
setPortAdminStatus(sysdbroot, p, 0)
else:
debug("Warnning: port %s not in VLAN %d"
%(str(p), int(vlan_num)))
else: # trunk mode
if pbc.trunkNativeVlan = int(vlan_num):
pbc.trunkNativeVlan = 0
disableVlanOnTrunkedPort(sysdbroot, p, vlan_num)
if leavetrunk:
# if no allowed vlans AND native is disabled, it means
# we can go back to access mode now
if pbc.trunkNativeVlan==0 and pbc.trunkAllowedVlans=='':
pbc.switchportMode = 'access'
pbc.accessVlan = DEFAULT_VLAN_NUM
# setPortAdminStatus(sysdbroot, p, 0)
except:
print sign + " error: "+str(sys.exc_info())
errors = 1
......@@ -388,20 +384,47 @@ def _listVlans():
# Note: First clear all existing allowed VLANs, then
# set port mode, and set nativeVlan or allowedVlans.
#
# Port-channel safe; tagging safe.
#
def _enablePortTrunking(port, native_vlan_num, tag_native):
retval = 0
sign = "_enablePortTrunking("+str(port)+","+str(native_vlan_num)+","+str(tag_native)+")"
debug(dign)
try:
pc = getBridgingConfig(sysdbroot).switchIntfConfig.newMember(str(port))
pc.switchportMode = 'trunk'
if int(tag_native) == 1:
pc.trunkNativeVlan = 0
else:
pc.trunkNativeVlan = int(native_vlan_num)
pc.trunkAllowedVlans = str(native_vlan_num)
if pc.switchportMode == 'trunk':
if int(tag_native) == 1:
bmp = string2Bitmap(pc.trunkAllowedVlans)
if pc.trunkNativeVlan != 0: # !=1?
bmp[int(pc.trunkNativeVlan)] = 1
pc.trunkNativeVlan = 0
if int(native_vlan_num) > 1:
bmp[int(native_vlan_num)] = 1
pc.trunkAllowedVlans = bitmap2String(bmp)
else:
oldnative = pc.trunkNativeVlan
pc.trunkNativeVlan = int(native_vlan_num)
bmp = string2Bitmap(pc.trunkAllowedVlans)
if int(native_vlan_num) > 1:
bmp[int(native_vlan_num)] = 0
if oldnative != 0 and oldnative != int(native_vlan_num):
bmp[oldnative] = 1
pc.trunkAllowedVlans = bitmap2String(bmp)
else: # access mode
pc.switchportMode = 'trunk'
if int(native_vlan_num) > 1:
pc.accessVlan = int(native_vlan_num)
if int(tag_native) == 1:
pc.trunkNativeVlan = 0
bmp = string2Bitmap('')
bmp[int(native_vlan_num)] = 1
pc.trunkAllowedVlans = bitmap2String(bmp)
else:
pc.trunkAllowedVlans = ''
pc.trunkNativeVlan = int(native_vlan_num)
pc.enabled = True
setPortStatus(sysdbroot, str(port), 1)
retval = 1
......@@ -418,6 +441,8 @@ def _enablePortTrunking(port, native_vlan_num, tag_native):
#
# Return 1 on success, 0 on failure.
#
# Port-channel safe; tagging safe.
#
def _disablePortTrunking(port):
retval = 0
sign = "_disablePortTrunking("+str(port)+")"
......@@ -427,17 +452,21 @@ def _disablePortTrunking(port):
if pc.switchportMode != 'access':
native_vlan = pc.trunkNativeVlan
pc.trunkAllowedVlans = ''
pc.trunkNativeVlan = 0
pc.switchportMode = 'access'
#
# !!! This is a little bit aggressive.
#
if pc.accessVlan == 1 and native_vlan != 0:
if native_vlan == 0 or native_vlan == 1:
# trunkNativeVlan may not enough:
# a potential extra step:
# remove the following line, just leave accessVlan as what is was
pc.accessVlan = 1
if pc.accessVlan == 1: # to match removed above line (if removed)
setPortStatus(sysdbroot, str(port), 0)
else:
pc.accessVlan = native_vlan
pc.enabled = True
setPortStatus(sysdbroot, str(port), 1)
retval = 1
except:
print sign + " error: "+str(sys.exc_info())
......@@ -505,6 +534,8 @@ def bitmap2String(bmp):
#
# Pre-condition: port must be trunked.
#
# Port-channel safe; tagging safe.
#
def disableVlanOnTrunkedPort(sysdb, port, vlan_num):
sign = "disableVlanOnTrunkedPort("+str(port)+","+str(vlan_num)+")"
debug(sign)
......@@ -517,37 +548,42 @@ def disableVlanOnTrunkedPort(sysdb, port, vlan_num):
#
# Set allowed VLANs on trunked port.
#
# Return 1 on success, 1 on failure.
# Return 0 on success, 1 on failure.
#
# NOTE: Arista switch CLI supports more sophisticated VLAN range, here
# we only need deal with the range value in existing trunkAllowedVlans.
# This works because vnums are passed from snmpit, and are all
# single numbers.
#
# Port-channel safe; tagging safe.
#
def _setAllowedVlansOnTrunkedPort(port, allow, vnums):
retval = 0
sign = "_setAllowedVlansOnTrunkedPort("+str(port)+","+str(allow)+","+str(vnums)+")"
debug(sign)
try:
pc = getBridgingConfig(sysdbroot).switchIntfConfig.newMember(str(port))
setPortStatus(sysdbroot, str(port), 1)
# setPortStatus(sysdbroot, str(port), 1)
if pc.switchportMode == 'access':
pc.switchportMode = 'trunk'
pc.trunkAllowedVlans = ''
retval = -1
else:
bmp = string2Bitmap(pc.trunkAllowedVlans)
vlan_ranges = map(lambda x: (int(x), int(x)), vnums)
if int(allow) == 1:
bmp = setBitmapByRanges(bmp, vlan_ranges)
else:
bmp = setBitmapByRanges(bmp, vlan_ranges, 0)
if pc.trunkNativeVlan != 0 and pc.trunkNativeVlan in map(lambda x: int(x), vnums):
pc.accessVlan = pc.trunkNativeVlan
pc.trunkNativeVlan = 0
bmp[pc.trunkNativeVlan] = 0
pc.trunkAllowedVlans = bitmap2String(bmp)
bmp = string2Bitmap(pc.trunkAllowedVlans)
vlan_ranges = map(lambda x: (int(x), int(x)), vnums)
if int(allow) == 1:
bmp = setBitmapByRanges(bmp, vlan_ranges)
else:
bmp = setBitmapByRanges(bmp, vlan_ranges, 0)
pc.trunkAllowedVlans = bitmap2String(bmp)
retval = 1
except:
print sign + " error: "+ str(sys.exc_info())
retval = 0
retval = 1
debug(sign + " returns " + str(retval))
return retval
......
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