| 1 | # SNMP::Info::CiscoStpExtensions | ||
| 2 | # | ||
| 3 | # Copyright (c)2009 Carlos Vicente | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # * Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # * Redistributions in binary form must reproduce the above copyright notice, | ||
| 12 | # this list of conditions and the following disclaimer in the documentation | ||
| 13 | # and/or other materials provided with the distribution. | ||
| 14 | # * Neither the name of the author nor the | ||
| 15 | # names of its contributors may be used to endorse or promote products | ||
| 16 | # derived from this software without specific prior written permission. | ||
| 17 | # | ||
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| 19 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 20 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
| 22 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| 23 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 24 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
| 25 | # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 27 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | |||
| 29 | package SNMP::Info::CiscoStpExtensions; | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | use Exporter; | ||
| 34 | use SNMP::Info; | ||
| 35 | use SNMP::Info::Bridge; | ||
| 36 | |||
| 37 | our ($VERSION, $DEBUG, %MIBS, %FUNCS, %GLOBALS, %MUNGE, %PORTSTAT, $INIT); | ||
| 38 | |||
| 39 | $VERSION = '3.985003'; | ||
| 40 | |||
| 41 | @SNMP::Info::CiscoStpExtensions::ISA = qw/SNMP::Info::Bridge SNMP::Info Exporter/; | ||
| 42 | @SNMP::Info::CiscoStpExtensions::EXPORT_OK = qw//; | ||
| 43 | |||
| 44 | %MIBS = ( | ||
| 45 | %SNMP::Info::Bridge::MIBS, | ||
| 46 | 'CISCO-STP-EXTENSIONS-MIB' => 'stpxSpanningTreeType', | ||
| 47 | ); | ||
| 48 | |||
| 49 | %GLOBALS = ( | ||
| 50 | %SNMP::Info::Bridge::GLOBALS, | ||
| 51 | 'stpx_mst_config_digest' => 'stpxSMSTConfigDigest', | ||
| 52 | 'stpx_mst_region_name' => 'stpxMSTRegionName', | ||
| 53 | 'stpx_mst_region_rev' => 'stpxSMSTRegionRevision', | ||
| 54 | 'stpx_stp_type' => 'stpxSpanningTreeType', | ||
| 55 | 'stpx_bpduguard_enable' => 'stpxFastStartBpduGuardEnable', | ||
| 56 | 'stpx_bpdufilter_enable' => 'stpxFastStartBpduFilterEnable', | ||
| 57 | 'stpx_faststart_default' => 'stpxFastStartGlobalDefaultMode', | ||
| 58 | ); | ||
| 59 | |||
| 60 | %FUNCS = ( | ||
| 61 | %SNMP::Info::Bridge::FUNCS, | ||
| 62 | 'stpx_rootguard_enabled' => 'stpxRootGuardConfigEnabled', | ||
| 63 | 'stpx_loopguard_enabled' => 'stpxLoopGuardConfigEnabled', | ||
| 64 | 'stpx_faststart_enabled' => 'stpxFastStartPortEnable', | ||
| 65 | 'stpx_faststart_operational' => 'stpxFastStartPortMode', | ||
| 66 | 'stpx_port_bpduguard_mode' => 'stpxFastStartPortBpduGuardMode', | ||
| 67 | 'stpx_port_bpdufilter_mode' => 'stpxFastStartPortBpduFilterMode', | ||
| 68 | 'stpx_smst_root' => 'stpxSMSTInstanceCISTRegionalRoot', | ||
| 69 | 'stpx_smst_vlans_mapped_1k2k' => 'stpxSMSTInstanceVlansMapped1k2k', | ||
| 70 | 'stpx_smst_vlans_mapped_3k4k' => 'stpxSMSTInstanceVlansMapped3k4k', | ||
| 71 | 'stpx_pvst_vlan_enable' => 'stpxPVSTVlanEnable', | ||
| 72 | 'stpx_faststart_oper' => 'stpxFastStartOperMode', | ||
| 73 | 'stpx_rstp_port_role' => 'stpxRSTPPortRoleValue', | ||
| 74 | ); | ||
| 75 | |||
| 76 | %MUNGE = ( | ||
| 77 | %SNMP::Info::Bridge::MUNGE, | ||
| 78 | 'stpx_mst_config_digest' => \&SNMP::Info::CiscoStpExtensions::oct2str, | ||
| 79 | 'stpx_rstp_port_role' => \&SNMP::Info::munge_stpx_port_role, | ||
| 80 | ); | ||
| 81 | |||
| 82 | |||
| 83 | # Report version of STP via standard method | ||
| 84 | sub stp_ver { | ||
| 85 | my $self = shift; | ||
| 86 | my $stp_ver = $self->SUPER::stp_ver(); | ||
| 87 | if ( !defined($stp_ver) || $stp_ver eq 'unknown' ){ | ||
| 88 | if ( defined $self->stpx_stp_type() ){ | ||
| 89 | $stp_ver = $self->stpx_stp_type(); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | return $stp_ver; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub mst_config_digest { | ||
| 96 | my $self = shift; | ||
| 97 | return $self->stpx_mst_config_digest; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub mst_region_name { | ||
| 101 | my $self = shift; | ||
| 102 | return $self->stpx_mst_region_name; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub mst_region_rev { | ||
| 106 | my $self = shift; | ||
| 107 | return $self->stpx_mst_region_rev; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub mst_vlan2instance { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | # Get MST vlan-to-instance mapping | ||
| 115 | my $m1k2k = $self->stpx_smst_vlans_mapped_1k2k; | ||
| 116 | my $m3k4k = $self->stpx_smst_vlans_mapped_3k4k; | ||
| 117 | |||
| 118 | # Get list of VLANs | ||
| 119 | my $vlan_membership = $self->i_vlan_membership; | ||
| 120 | my @vlans; | ||
| 121 | foreach my $iid ( keys %$vlan_membership ){ | ||
| 122 | if ( my $vm = $vlan_membership->{$iid} ){ | ||
| 123 | foreach my $vid ( @$vm ){ | ||
| 124 | push @vlans, $vid; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | my %res; | ||
| 129 | foreach my $vlan ( @vlans ){ | ||
| 130 | if ( $vlan < 2048 ){ | ||
| 131 | foreach my $inst ( keys %$m1k2k ){ | ||
| 132 | my $list = $m1k2k->{$inst}; | ||
| 133 | my $vlanlist = [split(//, unpack("B*", $list))]; | ||
| 134 | if ( @$vlanlist[$vlan] ){ | ||
| 135 | $res{$vlan} = $inst; | ||
| 136 | last; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | }else{ | ||
| 140 | foreach my $inst ( keys %$m3k4k ){ | ||
| 141 | my $list = $m3k4k->{$inst}; | ||
| 142 | my $vlanlist = [split(//, unpack("B*", $list))]; | ||
| 143 | if ( @$vlanlist[$vlan-2048] ){ | ||
| 144 | $res{$vlan} = $inst; | ||
| 145 | last; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | return \%res; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub i_rootguard_enabled { | ||
| 154 | my $self = shift; | ||
| 155 | my $partial = shift; | ||
| 156 | |||
| 157 | my $rg_enabled = $self->stpx_rootguard_enabled(); | ||
| 158 | my $bp_index = $self->bp_index($partial); | ||
| 159 | |||
| 160 | my %res; | ||
| 161 | foreach my $index ( keys %$rg_enabled ){ | ||
| 162 | my $enabled = $rg_enabled->{$index}; | ||
| 163 | my $iid = $bp_index->{$index}; | ||
| 164 | next unless defined $iid; | ||
| 165 | next unless defined $enabled; | ||
| 166 | $res{$iid} = $enabled; | ||
| 167 | } | ||
| 168 | return \%res; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub i_loopguard_enabled { | ||
| 172 | my $self = shift; | ||
| 173 | my $partial = shift; | ||
| 174 | |||
| 175 | my $lg_enabled = $self->stpx_loopguard_enabled(); | ||
| 176 | my $bp_index = $self->bp_index($partial); | ||
| 177 | |||
| 178 | my %res; | ||
| 179 | foreach my $index ( keys %$lg_enabled ){ | ||
| 180 | my $enabled = $lg_enabled->{$index}; | ||
| 181 | my $iid = $bp_index->{$index}; | ||
| 182 | next unless defined $iid; | ||
| 183 | next unless defined $enabled; | ||
| 184 | $res{$iid} = $enabled; | ||
| 185 | } | ||
| 186 | return \%res; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub i_bpduguard_enabled { | ||
| 190 | my $self = shift; | ||
| 191 | my $partial = shift; | ||
| 192 | |||
| 193 | my $bpdugm_default = $self->stpx_bpduguard_enable(); | ||
| 194 | my $bp_index = $self->bp_index($partial); | ||
| 195 | my $bpdugm = $self->stpx_port_bpduguard_mode(); | ||
| 196 | |||
| 197 | my %res; | ||
| 198 | foreach my $index ( keys %$bpdugm ){ | ||
| 199 | my $mode = $bpdugm->{$index}; | ||
| 200 | my $iid = $bp_index->{$index}; | ||
| 201 | next unless defined $iid; | ||
| 202 | next unless defined $mode; | ||
| 203 | if ( $mode eq 'default' ){ | ||
| 204 | $res{$iid} = $bpdugm_default; | ||
| 205 | }else{ | ||
| 206 | $res{$iid} = $mode; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | return \%res; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub i_bpdufilter_enabled { | ||
| 213 | my $self = shift; | ||
| 214 | my $partial = shift; | ||
| 215 | |||
| 216 | my $bpdufm_default = $self->stpx_bpdufilter_enable(); | ||
| 217 | my $bp_index = $self->bp_index($partial); | ||
| 218 | my $bpdufm = $self->stpx_port_bpdufilter_mode(); | ||
| 219 | |||
| 220 | my %res; | ||
| 221 | foreach my $index ( keys %$bpdufm ){ | ||
| 222 | my $mode = $bpdufm->{$index}; | ||
| 223 | my $iid = $bp_index->{$index}; | ||
| 224 | next unless defined $iid; | ||
| 225 | next unless defined $mode; | ||
| 226 | if ( $mode eq 'default' ){ | ||
| 227 | $res{$iid} = $bpdufm_default; | ||
| 228 | }else{ | ||
| 229 | $res{$iid} = $mode; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | return \%res; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub i_faststart_enabled { | ||
| 236 | my $self = shift; | ||
| 237 | my $partial = shift; | ||
| 238 | |||
| 239 | my $faststart_default = $self->stpx_faststart_default(); | ||
| 240 | my $bp_index = $self->bp_index($partial); | ||
| 241 | my $faststart = $self->stpx_faststart_enabled(); | ||
| 242 | my $faststart_oper = $self->stpx_faststart_operational(); | ||
| 243 | |||
| 244 | my %res; | ||
| 245 | # stpxFastStartPortEnable is deprecated in favour of stpxFastStartPortMode | ||
| 246 | # see https://github.com/netdisco/netdisco/issues/12 | ||
| 247 | foreach my $index ( keys %$faststart, keys %$faststart_oper ){ | ||
| 248 | my $mode = $faststart_oper->{$index} || $faststart->{$index}; | ||
| 249 | my $iid = $bp_index->{$index}; | ||
| 250 | next unless defined $iid; | ||
| 251 | next unless defined $mode; | ||
| 252 | if ( $mode eq 'default' ){ | ||
| 253 | $res{$iid} = $faststart_default; | ||
| 254 | }else{ | ||
| 255 | $res{$iid} = $mode; | ||
| 256 | } | ||
| 257 | $res{$iid} = 'enable' if $res{$iid} eq 'true'; | ||
| 258 | $res{$iid} = 'disable' if $res{$iid} eq 'false'; | ||
| 259 | $res{$iid} = 1 if $res{$iid} =~ m/enable/i; # enableForTrunk | ||
| 260 | $res{$iid} = 0 if $res{$iid} eq 'disable'; | ||
| 261 | } | ||
| 262 | return \%res; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub i_edgeport_admin { | ||
| 266 | my $self = shift; | ||
| 267 | my $partial = shift; | ||
| 268 | |||
| 269 | my $faststart = $self->i_faststart_enabled($partial); | ||
| 270 | |||
| 271 | my %res; | ||
| 272 | foreach my $iid ( keys %$faststart ){ | ||
| 273 | my $mode = $faststart->{$iid}; | ||
| 274 | next unless defined $mode; | ||
| 275 | $res{$iid} = 1 if $mode eq '1'; | ||
| 276 | $res{$iid} = 0 if $mode eq '0' or $mode eq 'network'; | ||
| 277 | } | ||
| 278 | return \%res; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub i_edgeport_oper { | ||
| 282 | my $self = shift; | ||
| 283 | my $partial = shift; | ||
| 284 | |||
| 285 | my $instance_of = $self->_stpx_edge_instance($partial) or return {}; | ||
| 286 | my $bp_index = $self->bp_index() || {}; | ||
| 287 | my $oper = $self->stpx_faststart_oper() || {}; | ||
| 288 | |||
| 289 | my %res; | ||
| 290 | foreach my $bridge_port ( keys %$bp_index ) { | ||
| 291 | my $iid = $bp_index->{$bridge_port}; | ||
| 292 | next unless defined $instance_of->{$iid}; | ||
| 293 | my $mode = $oper->{ $instance_of->{$iid} . ".$bridge_port" }; | ||
| 294 | next unless defined $mode; | ||
| 295 | $res{$iid} = 1 if $mode eq 'enabled' or $mode eq '1'; | ||
| 296 | $res{$iid} = 0 if $mode eq 'disabled' or $mode eq 'network' | ||
| 297 | or $mode eq '2' or $mode eq '3'; | ||
| 298 | } | ||
| 299 | return \%res; | ||
| 300 | } | ||
| 301 | |||
| 302 | # ifIndex => tree instance whose FastStart oper rows describe that port | ||
| 303 | sub _stpx_edge_instance { | ||
| 304 | my $self = shift; | ||
| 305 | my $partial = shift; | ||
| 306 | |||
| 307 | my $protocol = $self->_stpx_protocol() || ''; | ||
| 308 | if ( $protocol eq 'pvst' or $protocol eq 'rapid-pvst' ) { | ||
| 309 | return $self->i_vlan($partial); | ||
| 310 | } | ||
| 311 | return unless $protocol eq 'mstp'; | ||
| 312 | |||
| 313 | my $bp_index = $self->bp_index() || {}; | ||
| 314 | return { map { $_ => 0 } values %$bp_index }; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub oct2str { | ||
| 318 | my ($v) = @_; | ||
| 319 | return sprintf('%s', unpack('H*', $v)); | ||
| 320 | } | ||
| 321 | |||
| 322 | # stpxSpanningTreeType; mistp and mistpPvstPlus have no tree record form | ||
| 323 | my %stpx_protocol = ( | ||
| 324 | 'pvstPlus' => 'pvst', | ||
| 325 | 1 => 'pvst', | ||
| 326 | 'rapidPvstPlus' => 'rapid-pvst', | ||
| 327 | 5 => 'rapid-pvst', | ||
| 328 | 'mst' => 'mstp', | ||
| 329 | 4 => 'mstp', | ||
| 330 | ); | ||
| 331 | |||
| 332 | sub _stpx_protocol { | ||
| 333 | my $stpx = shift; | ||
| 334 | |||
| 335 | my $type = $stpx->stpx_stp_type(); | ||
| 336 | return unless defined $type; | ||
| 337 | return $stpx_protocol{$type}; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub hasPVST { | ||
| 341 | my $stpx = shift; | ||
| 342 | return ( $stpx->_stpx_protocol() || '' ) eq 'pvst' ? 1 : 0; | ||
| 343 | } | ||
| 344 | |||
| 345 | sub hasRapidPVST { | ||
| 346 | my $stpx = shift; | ||
| 347 | return ( $stpx->_stpx_protocol() || '' ) eq 'rapid-pvst' ? 1 : 0; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub hasMSTP { | ||
| 351 | my $stpx = shift; | ||
| 352 | return ( $stpx->_stpx_protocol() || '' ) eq 'mstp' ? 1 : 0; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub stp_protocols { | ||
| 356 | my $stpx = shift; | ||
| 357 | |||
| 358 | my $protocol = $stpx->_stpx_protocol(); | ||
| 359 | return unless defined $protocol; | ||
| 360 | return [$protocol]; | ||
| 361 | } | ||
| 362 | |||
| 363 | # Called by SNMP::Info::context_kinds, which a mixin cannot override | ||
| 364 | sub _context_kinds { | ||
| 365 | my ( $stpx, $feature ) = @_; | ||
| 366 | |||
| 367 | return ['vlan'] if $feature eq 'stp'; | ||
| 368 | return; | ||
| 369 | } | ||
| 370 | |||
| 371 | # Called by SNMP::Info::snmp_context, which a mixin cannot override | ||
| 372 | sub _snmp_context { | ||
| 373 | my ( $stpx, $kind, $id ) = @_; | ||
| 374 | |||
| 375 | return unless defined $kind and $kind eq 'vlan' and defined $id; | ||
| 376 | return { kind => 'vlan', id => $id, v2c => "%s\@$id", v3 => "vlan-$id" }; | ||
| 377 | } | ||
| 378 | |||
| 379 | # MSTI => [VLANs]: 1k2k bit N is VLAN N, 3k4k bit N is VLAN 2048 + N. The | ||
| 380 | # agent may serve fewer than 256 octets; the missing ones are zero. | ||
| 381 | sub _stpx_mst_vlans { | ||
| 382 | my $stpx = shift; | ||
| 383 | |||
| 384 | my $low = $stpx->stpx_smst_vlans_mapped_1k2k() || {}; | ||
| 385 | my $high = $stpx->stpx_smst_vlans_mapped_3k4k() || {}; | ||
| 386 | |||
| 387 | my %vlans; | ||
| 388 | foreach my $inst ( keys %{ { %$low, %$high } } ) { | ||
| 389 | my $bits = join '', | ||
| 390 | map { substr( unpack( 'B*', $_ // '' ) . '0' x 2048, 0, 2048 ) } | ||
| 391 | $low->{$inst}, $high->{$inst}; | ||
| 392 | $vlans{$inst} | ||
| 393 | = [ grep { substr( $bits, $_, 1 ) } 1 .. 4094 ]; | ||
| 394 | } | ||
| 395 | return \%vlans; | ||
| 396 | } | ||
| 397 | |||
| 398 | # VLANs running a tree: a per-port row names them; stpxPVSTVlanEnable also | ||
| 399 | # enables the VTP defaults 1002 to 1005, which run none | ||
| 400 | sub _stpx_pvst_vlans { | ||
| 401 | my $stpx = shift; | ||
| 402 | |||
| 403 | my %vlans; | ||
| 404 | foreach my $table (qw/stpx_faststart_oper stpx_rstp_port_role/) { | ||
| 405 | my $rows = $stpx->$table() || {}; | ||
| 406 | $vlans{ ( split /\./, $_ )[0] } = 1 foreach keys %$rows; | ||
| 407 | } | ||
| 408 | return [ sort { $a <=> $b } keys %vlans ] if keys %vlans; | ||
| 409 | |||
| 410 | my $enabled = $stpx->stpx_pvst_vlan_enable() || {}; | ||
| 411 | my $types = $stpx->can('v_type') ? ( $stpx->v_type() || {} ) : {}; | ||
| 412 | my %type = map { ( split /\./, $_ )[-1] => $types->{$_} } keys %$types; | ||
| 413 | return [ | ||
| 414 | sort { $a <=> $b } | ||
| 415 | grep { | ||
| 416 | ( $enabled->{$_} || '' ) =~ /^(?:enabled|1)$/ | ||
| 417 | and ( !%type or ( $type{$_} || '' ) =~ /^(?:ethernet|1)$/ ) | ||
| 418 | } keys %$enabled | ||
| 419 | ]; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub _stpx_measured { | ||
| 423 | my ( $stpx, $protocol ) = @_; | ||
| 424 | |||
| 425 | my $os = $stpx->can('os') ? ( $stpx->os() || '' ) : ''; | ||
| 426 | return 1 if $os eq 'ios' or $os eq 'ios-xe'; | ||
| 427 | return 1 if $os eq 'nx-os' and $protocol ne 'pvst'; | ||
| 428 | return 0; | ||
| 429 | } | ||
| 430 | |||
| 431 | # VLAN 1's tree is the default context's | ||
| 432 | sub _stpx_vlan_contexts { | ||
| 433 | my ( $stpx, $vlan ) = @_; | ||
| 434 | |||
| 435 | return [ { kind => 'none' } ] if $vlan == 1; | ||
| 436 | my $context = $stpx->snmp_context( 'vlan', $vlan ); | ||
| 437 | return $context ? [$context] : []; | ||
| 438 | } | ||
| 439 | |||
| 440 | sub _stpx_pvst_trees { | ||
| 441 | my ( $stpx, $protocol ) = @_; | ||
| 442 | |||
| 443 | my $measured = $stpx->_stpx_measured($protocol); | ||
| 444 | my %trees; | ||
| 445 | foreach my $vlan ( @{ $stpx->_stpx_pvst_vlans() } ) { | ||
| 446 | $trees{"vlan.$vlan"} = { | ||
| 447 | type => 'vlan', | ||
| 448 | id => $vlan, | ||
| 449 | vlan => $vlan, | ||
| 450 | protocol => $protocol, | ||
| 451 | measured => $measured, | ||
| 452 | ports_complete => 1, | ||
| 453 | contexts => $stpx->_stpx_vlan_contexts($vlan), | ||
| 454 | bridge => undef, | ||
| 455 | root => $vlan == 1 | ||
| 456 | ? scalar SNMP::Info::Bridge::_stp_decode_bridge_id( | ||
| 457 | $stpx->stp_root() ) | ||
| 458 | : undef, | ||
| 459 | }; | ||
| 460 | } | ||
| 461 | return \%trees; | ||
| 462 | } | ||
| 463 | |||
| 464 | # The CIST's trunk ports have no row in any IOS context. NX-OS serves them in | ||
| 465 | # mst-0, but whether mst-0 maps bridge ports to ifIndex is unmeasured, so the | ||
| 466 | # CIST is never claimed complete | ||
| 467 | sub _stpx_mst_trees { | ||
| 468 | my $stpx = shift; | ||
| 469 | |||
| 470 | my $measured = $stpx->_stpx_measured('mstp'); | ||
| 471 | my $mapped = $stpx->_stpx_mst_vlans(); | ||
| 472 | my %listed = map { $_ => 1 } values %{ $stpx->v_index() || {} }; | ||
| 473 | |||
| 474 | my %trees; | ||
| 475 | foreach my $inst ( keys %$mapped ) { | ||
| 476 | my $own = $stpx->snmp_context( 'mst', $inst ); | ||
| 477 | my $tree = { | ||
| 478 | type => $inst ? 'msti' : 'cist', | ||
| 479 | id => $inst, | ||
| 480 | protocol => 'mstp', | ||
| 481 | measured => $measured, | ||
| 482 | ports_complete => 1, | ||
| 483 | bridge => undef, | ||
| 484 | root => undef, | ||
| 485 | }; | ||
| 486 | if ($inst) { | ||
| 487 | my @vlans | ||
| 488 | = grep { !%listed or $listed{$_} } @{ $mapped->{$inst} }; | ||
| 489 | $tree->{contexts} = [ ( $own ? $own : () ), | ||
| 490 | map { @{ $stpx->_stpx_vlan_contexts($_) } } @vlans ]; | ||
| 491 | } | ||
| 492 | else { | ||
| 493 | $tree->{contexts} = [ $own || { kind => 'none' } ]; | ||
| 494 | $tree->{ports_complete} = 0; | ||
| 495 | $tree->{root} = SNMP::Info::Bridge::_stp_decode_bridge_id( | ||
| 496 | $stpx->stp_root() ); | ||
| 497 | } | ||
| 498 | $trees{ $inst ? "msti.$inst" : 'cist.0' } = $tree; | ||
| 499 | } | ||
| 500 | return \%trees; | ||
| 501 | } | ||
| 502 | |||
| 503 | sub stp_trees { | ||
| 504 | my $stpx = shift; | ||
| 505 | |||
| 506 | return unless $stpx->_stp_default_context_ok(); | ||
| 507 | return $stpx->{_stp_trees} if $stpx->{_stp_trees}; | ||
| 508 | |||
| 509 | my $protocol = $stpx->_stpx_protocol(); | ||
| 510 | return {} unless defined $protocol; | ||
| 511 | |||
| 512 | my $trees | ||
| 513 | = $protocol eq 'mstp' | ||
| 514 | ? $stpx->_stpx_mst_trees() | ||
| 515 | : $stpx->_stpx_pvst_trees($protocol); | ||
| 516 | return {} unless keys %$trees; | ||
| 517 | $stpx->{_stp_trees} = $trees; | ||
| 518 | return $trees; | ||
| 519 | } | ||
| 520 | |||
| 521 | sub stp_trees_complete { | ||
| 522 | my $stpx = shift; | ||
| 523 | |||
| 524 | my $trees = $stpx->stp_trees() || {}; | ||
| 525 | my @listing = ( $stpx->_stpx_protocol() || '' ) eq 'mstp' | ||
| 526 | ? qw/stpx_smst_vlans_mapped_1k2k stpx_smst_vlans_mapped_3k4k/ | ||
| 527 | : qw/stpx_faststart_oper stpx_rstp_port_role stpx_pvst_vlan_enable/; | ||
| 528 | return 0 if $stpx->_stp_walk_incomplete(@listing); | ||
| 529 | return keys %$trees ? 1 : 0; | ||
| 530 | } | ||
| 531 | |||
| 532 | sub stp_vlan_map { | ||
| 533 | my $stpx = shift; | ||
| 534 | |||
| 535 | return unless ( $stpx->_stpx_protocol() || '' ) eq 'mstp'; | ||
| 536 | my $mapped = $stpx->_stpx_mst_vlans(); | ||
| 537 | |||
| 538 | my %map; | ||
| 539 | foreach my $inst ( keys %$mapped ) { | ||
| 540 | $map{$_} = $inst ? "msti.$inst" : 'cist.0' | ||
| 541 | foreach @{ $mapped->{$inst} }; | ||
| 542 | } | ||
| 543 | return unless keys %map; | ||
| 544 | return \%map; | ||
| 545 | } | ||
| 546 | |||
| 547 | # In the default context the key must be a tree read there; in a tree's own | ||
| 548 | # context the list cannot be read, so rows are labeled from the key alone | ||
| 549 | sub _stpx_tree_key_ok { | ||
| 550 | my ( $stpx, $key ) = @_; | ||
| 551 | |||
| 552 | return 1 if length $stpx->_walk_context(); | ||
| 553 | my $tree = ( $stpx->stp_trees() || {} )->{$key}; | ||
| 554 | unless ($tree) { | ||
| 555 | $stpx->error_throw( "SNMP::Info::CiscoStpExtensions::stp_tree_ports()" | ||
| 556 | . " has no tree '$key'. Pass a key from stp_trees(), read in" | ||
| 557 | . " the default context.\n" ); | ||
| 558 | return 0; | ||
| 559 | } | ||
| 560 | return 1 if grep { $_->{kind} eq 'none' } @{ $tree->{contexts} }; | ||
| 561 | |||
| 562 | my $names = join( ', ', | ||
| 563 | map { "v2c '" . ( $_->{v2c} // 'none' ) . "', v3 '" | ||
| 564 | . ( $_->{v3} // 'none' ) . "'" } @{ $tree->{contexts} } ); | ||
| 565 | $stpx->error_throw( "SNMP::Info::CiscoStpExtensions::stp_tree_ports():" | ||
| 566 | . " tree '$key' is served in its own context ($names): switch to" | ||
| 567 | . " one with update() and call stp_tree_ports('$key') there.\n" ); | ||
| 568 | return 0; | ||
| 569 | } | ||
| 570 | |||
| 571 | sub stp_tree_ports { | ||
| 572 | my $stpx = shift; | ||
| 573 | my $key = shift; | ||
| 574 | |||
| 575 | my ( $type, $id ) = ( $key // '' ) =~ /^(vlan|cist|msti)\.(\d+)$/; | ||
| 576 | unless ( defined $type ) { | ||
| 577 | my $shown = $key // 'undef'; | ||
| 578 | $stpx->error_throw( "SNMP::Info::CiscoStpExtensions::stp_tree_ports()" | ||
| 579 | . " takes a tree key from stp_trees(), of the form vlan.N," | ||
| 580 | . " cist.0 or msti.N; got '$shown'.\n" ); | ||
| 581 | return; | ||
| 582 | } | ||
| 583 | return unless $stpx->_stpx_tree_key_ok($key); | ||
| 584 | |||
| 585 | my $rows = $stpx->_stp_dot1d_port_rows( { type => $type, id => $id } ); | ||
| 586 | my $roles = $stpx->stpx_rstp_port_role() || {}; | ||
| 587 | my $raw_roles = $stpx->stpx_rstp_port_role_raw() || {}; | ||
| 588 | my $bp_index = $stpx->bp_index() || {}; | ||
| 589 | foreach my $idx ( keys %$roles ) { | ||
| 590 | my ( $inst, $port ) = split /\./, $idx; | ||
| 591 | next unless $inst == $id; | ||
| 592 | my $iid = $bp_index->{$port}; | ||
| 593 | next unless defined $iid; | ||
| 594 | $rows->{$iid} ||= { | ||
| 595 | tree_type => $type, | ||
| 596 | tree_id => $id, | ||
| 597 | state => undef, | ||
| 598 | role => undef, | ||
| 599 | role_raw => undef, | ||
| 600 | enabled => undef, | ||
| 601 | designated_bridge => undef, | ||
| 602 | designated_port => undef, | ||
| 603 | }; | ||
| 604 | $rows->{$iid}{role} = $roles->{$idx}; | ||
| 605 | $rows->{$iid}{role_raw} = $raw_roles->{$idx}; | ||
| 606 | } | ||
| 607 | return $rows; | ||
| 608 | } | ||
| 609 | |||
| 610 | # Default tree is not the common tree: VLAN 1's tree under PVST+, VLAN 1's | ||
| 611 | # ports under MST | ||
| 612 | sub _stp_default_is_common { return 0; } | ||
| 613 | |||
| 614 | 1; | ||
| 615 | __END__ |