Info/IEEE802_STP.pm untested

filter: Statementstmt 0.0% 0 / 191 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 440 CC 44 SCAR 30.0
CC
44
Cov
0%
_ieee_stp_protocol
42.0
_ieee_mstp_trees
42.0
stp_trees
30.0
CRAP 20.2
1 # SNMP::Info::IEEE802_STP
2 #
3 # Copyright (c) 2026 SNMP::Info Developers
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
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the University of California, Santa Cruz 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"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29  
30 package SNMP::Info::IEEE802_STP;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::Bridge;
36  
37 @SNMP::Info::IEEE802_STP::ISA = qw/SNMP::Info::Bridge Exporter/;
38 @SNMP::Info::IEEE802_STP::EXPORT_OK = qw//;
39  
40 our ( $VERSION, $DEBUG, %MIBS, %FUNCS, %GLOBALS, %MUNGE, $INIT );
41  
42 $VERSION = '3.985005';
43  
44 %MIBS = (
45 %SNMP::Info::Bridge::MIBS,
46 'IEEE8021-SPANNING-TREE-MIB' => 'ieee8021SpanningTreeVersion',
47 'IEEE8021-MSTP-MIB' => 'ieee8021MstpCistBridgeIdentifier',
48 );
49  
50 %GLOBALS = ( %SNMP::Info::Bridge::GLOBALS, );
51  
52 %FUNCS = (
53 %SNMP::Info::Bridge::FUNCS,
54  
55 # IEEE8021-SPANNING-TREE-MIB::ieee8021SpanningTreeTable
56 'ieee_stp_version' => 'ieee8021SpanningTreeVersion',
57 'ieee_stp_root' => 'ieee8021SpanningTreeDesignatedRoot',
58  
59 # IEEE8021-SPANNING-TREE-MIB::ieee8021SpanningTreePortTable
60 'ieee_stp_p_state' => 'ieee8021SpanningTreePortState',
61 'ieee_stp_p_enable' => 'ieee8021SpanningTreePortEnabled',
62 'ieee_stp_p_bridge' => 'ieee8021SpanningTreePortDesignatedBridge',
63 'ieee_stp_p_port' => 'ieee8021SpanningTreePortDesignatedPort',
64  
65 # IEEE8021-SPANNING-TREE-MIB::ieee8021SpanningTreeRstpPortTable
66 'ieee_rstp_p_edge_admin' => 'ieee8021SpanningTreeRstpPortAdminEdgePort',
67 'ieee_rstp_p_edge_oper' => 'ieee8021SpanningTreeRstpPortOperEdgePort',
68  
69 # IEEE8021-MSTP-MIB::ieee8021MstpCistTable
70 'mstp_cist_bridge' => 'ieee8021MstpCistBridgeIdentifier',
71  
72 # IEEE8021-MSTP-MIB::ieee8021MstpCistPortTable
73 'mstp_cist_p_role' => 'ieee8021MstpCistPortRole',
74 'mstp_cist_p_edge_admin' => 'ieee8021MstpCistPortAdminEdgePort',
75 'mstp_cist_p_edge_oper' => 'ieee8021MstpCistPortOperEdgePort',
76  
77 # IEEE8021-MSTP-MIB::ieee8021MstpTable
78 'mstp_i_bridge' => 'ieee8021MstpBridgeId',
79 'mstp_i_root' => 'ieee8021MstpDesignatedRoot',
80  
81 # IEEE8021-MSTP-MIB::ieee8021MstpPortTable
82 'mstp_p_state' => 'ieee8021MstpPortState',
83 'mstp_p_role' => 'ieee8021MstpPortRole',
84 'mstp_p_bridge' => 'ieee8021MstpPortDesignatedBridge',
85  
86 # IEEE8021-MSTP-MIB::ieee8021MstpVlanTable
87 'mstp_vlan_msti' => 'ieee8021MstpVlanMstId',
88 );
89  
90 %MUNGE = (
91 %SNMP::Info::Bridge::MUNGE,
92 'ieee_stp_root' => \&SNMP::Info::munge_prio_mac,
93 'ieee_stp_p_bridge' => \&SNMP::Info::munge_prio_mac,
94 'ieee_stp_p_port' => \&SNMP::Info::munge_prio_port,
95 'mstp_cist_bridge' => \&SNMP::Info::munge_prio_mac,
96 'mstp_i_bridge' => \&SNMP::Info::munge_prio_mac,
97 'mstp_i_root' => \&SNMP::Info::munge_prio_mac,
98 'mstp_p_bridge' => \&SNMP::Info::munge_prio_mac,
99 );
100  
101 # Tables are indexed by component first; only the lowest one is read
102 sub _ieee_stp_component {
103 my $stp = shift;
104  
105 foreach my $column (
106 qw/ieee_stp_version mstp_cist_bridge
107 mstp_cist_p_role/
108 )
109 {
110 my $rows = $stp->$column() || {};
111 my ($component)
112 = sort { $a <=> $b } map { ( split /\./ )[0] } keys %$rows;
113 return $component if defined $component;
114 }
115 return;
116 }
117  
118 sub _ieee_stp_protocol {
119 my $stp = shift;
120  
121 my $component = $stp->_ieee_stp_component();
122 return unless defined $component;
123  
124 my $version = ( $stp->ieee_stp_version() || {} )->{$component};
125 return unless defined $version;
126 return 'stp' if $version =~ /^(?:stp|0)$/;
127 return 'rstp' if $version =~ /^(?:rstp|2)$/;
128 return 'mstp' if $version =~ /^(?:mstp|3)$/;
129 return;
130 }
131  
132 sub _ieee_stp_measured { return 0; }
133  
134 sub _ieee_stp_is_mstp {
135 my $stp = shift;
136 return ( $stp->_ieee_stp_protocol() || '' ) eq 'mstp';
137 }
138  
139 sub hasSTP {
140 my $stp = shift;
141 return ( $stp->_ieee_stp_protocol() || '' ) eq 'stp' ? 1 : 0;
142 }
143  
144 sub hasRSTP {
145 my $stp = shift;
146 return ( $stp->_ieee_stp_protocol() || '' ) eq 'rstp' ? 1 : 0;
147 }
148  
149 sub hasMSTP {
150 my $stp = shift;
151 return $stp->_ieee_stp_is_mstp() ? 1 : 0;
152 }
153  
154 sub stp_protocols {
155 my $stp = shift;
156  
157 return $stp->SUPER::stp_protocols() unless $stp->_ieee_stp_is_mstp();
158 return ['mstp'];
159 }
160  
161 sub stp_trees {
162 my $stp = shift;
163  
164 return unless $stp->_stp_default_context_ok();
165 return $stp->{_stp_trees} if $stp->{_stp_trees};
166 return $stp->SUPER::stp_trees() unless $stp->_ieee_stp_is_mstp();
167  
168 my $trees = $stp->_ieee_mstp_trees();
169 $stp->{_stp_trees} = $trees if keys %$trees;
170 return $trees;
171 }
172  
173 sub _ieee_mstp_trees {
174 my $stp = shift;
175  
176 my $component = $stp->_ieee_stp_component();
177 return {} unless defined $component;
178 my $bridges = $stp->mstp_i_bridge() || {};
179 my $roots = $stp->mstp_i_root() || {};
180 my $cist_id = ( $stp->mstp_cist_bridge() || {} )->{$component};
181 my $cist_root = ( $stp->ieee_stp_root() || {} )->{$component};
182  
183 my %ids;
184 $ids{0} = [ $cist_id, $cist_root ]
185 if defined $cist_id or defined $cist_root;
186 foreach my $row ( keys %$bridges ) {
187 my ( $row_component, $msti ) = split /\./, $row, 2;
188 next unless $row_component == $component;
189 $ids{$msti} = [ $bridges->{$row}, $roots->{$row} ];
190 }
191  
192 my $incomplete = $stp->_ieee_mstp_incomplete_trees($component);
193 my $measured = $stp->_ieee_stp_measured();
194 my %trees;
195 foreach my $msti ( keys %ids ) {
196 my ( $bridge, $root ) = @{ $ids{$msti} };
197 my $unmapped = $incomplete->{all} || $incomplete->{$msti};
198 $trees{ _ieee_mstp_tree_key($msti) } = {
199 type => $msti ? 'msti' : 'cist',
200 id => $msti + 0,
201 protocol => 'mstp',
202 measured => $measured,
203 ports_complete => $unmapped ? 0 : 1,
204 contexts => [ { kind => 'none' } ],
205 bridge =>
206 scalar SNMP::Info::Bridge::_stp_decode_bridge_id($bridge),
207 root => scalar SNMP::Info::Bridge::_stp_decode_bridge_id($root),
208 };
209 }
210 return \%trees;
211 }
212  
213 sub _ieee_mstp_tree_key {
214 my $msti = shift;
215 return $msti ? "msti.$msti" : 'cist.0';
216 }
217  
218 # { MSTI => 1 } for each tree with a port row on a bridge port bp_index
219 # lacks, and all => 1 when another component serves rows, since this role
220 # reads only the lowest
221 sub _ieee_mstp_incomplete_trees {
222 my ( $stp, $component ) = @_;
223  
224 my $cist_ports = {
225 %{ $stp->mstp_cist_p_role() || {} },
226 %{ $stp->ieee_stp_p_state() || {} },
227 };
228 my $msti_ports = $stp->mstp_p_state() || {};
229 my $bp_index = $stp->bp_index() || {};
230  
231 my @rows = (
232 ( map { [ split /\./ ] } keys %$cist_ports ),
233 ( map { [ ( split /\./ )[ 0, 2, 1 ] ] } keys %$msti_ports ),
234 );
235  
236 my %incomplete;
237 foreach my $row (@rows) {
238 my ( $row_component, $port, $msti ) = @$row;
239 $incomplete{all} = 1 unless $row_component == $component;
240 $incomplete{ $msti // 0 } = 1 unless defined $bp_index->{$port};
241 }
242 $incomplete{all} = 1 if $stp->_ieee_stp_other_component($component);
243 return \%incomplete;
244 }
245  
246 sub _ieee_stp_other_component {
247 my ( $stp, $component ) = @_;
248  
249 my @rows = map { keys %{ $_ || {} } } $stp->ieee_stp_version(),
250 $stp->mstp_cist_bridge(), $stp->mstp_i_bridge();
251 return scalar grep { ( split /\./ )[0] != $component } @rows;
252 }
253  
254 sub stp_tree_ports {
255 my ( $stp, $tree_key ) = @_;
256  
257 return $stp->SUPER::stp_tree_ports($tree_key)
258 unless $stp->_ieee_stp_is_mstp();
259  
260 my $tree = $stp->_stp_tree_for_key($tree_key) or return;
261 return $tree->{type} eq 'cist'
262 ? $stp->_ieee_mstp_cist_port_rows($tree)
263 : $stp->_ieee_mstp_msti_port_rows($tree);
264 }
265  
266 # Rows keyed by ifIndex, from the lowest component only
267 sub _ieee_mstp_cist_port_rows {
268 my ( $stp, $tree ) = @_;
269  
270 my $component = $stp->_ieee_stp_component();
271 my $states = $stp->ieee_stp_p_state() || {};
272 my $roles = $stp->mstp_cist_p_role() || {};
273 my $enable = $stp->ieee_stp_p_enable() || {};
274 my $d_bridge = $stp->ieee_stp_p_bridge() || {};
275 my $d_port = $stp->ieee_stp_p_port() || {};
276 my $bp_index = $stp->bp_index() || {};
277  
278 my %rows;
279 foreach my $index ( keys %{ +{ %$states, %$roles } } ) {
280 my ( $row_component, $port ) = split /\./, $index, 2;
281 next unless $row_component == $component;
282 my $iid = $bp_index->{$port};
283 next unless defined $iid;
284 $rows{$iid} = {
285 tree_type => $tree->{type},
286 tree_id => $tree->{id},
287 state => $states->{$index},
288 role => scalar _ieee_mstp_known_role( $roles->{$index} ),
289 role_raw => $roles->{$index},
290 enabled =>
291 scalar SNMP::Info::Bridge::_stp_enabled( $enable->{$index} ),
292 designated_bridge =>
293 scalar SNMP::Info::Bridge::_stp_decode_bridge_id(
294 $d_bridge->{$index}
295 ),
296 designated_port =>
297 scalar SNMP::Info::Bridge::_stp_decode_port_id(
298 $d_port->{$index} ),
299 };
300 }
301 return \%rows;
302 }
303  
304 sub _ieee_mstp_msti_port_rows {
305 my ( $stp, $tree ) = @_;
306  
307 my $component = $stp->_ieee_stp_component();
308 my $states = $stp->mstp_p_state() || {};
309 my $roles = $stp->mstp_p_role() || {};
310 my $d_bridge = $stp->mstp_p_bridge() || {};
311 my $enable = $stp->ieee_stp_p_enable() || {};
312 my $bp_index = $stp->bp_index() || {};
313  
314 my %rows;
315 foreach my $index ( keys %$states ) {
316 my ( $row_component, $msti, $port ) = split /\./, $index, 3;
317 next unless $row_component == $component and $msti == $tree->{id};
318 my $iid = $bp_index->{$port};
319 next unless defined $iid;
320 $rows{$iid} = {
321 tree_type => $tree->{type},
322 tree_id => $tree->{id},
323 state => $states->{$index},
324 role => scalar _ieee_mstp_known_role( $roles->{$index}, 1 ),
325 role_raw => $roles->{$index},
326 enabled => scalar SNMP::Info::Bridge::_stp_enabled(
327 $enable->{"$component.$port"}
328 ),
329 designated_bridge =>
330 scalar SNMP::Info::Bridge::_stp_decode_bridge_id(
331 $d_bridge->{$index}
332 ),
333 designated_port => undef,
334 };
335 }
336 return \%rows;
337 }
338  
339 # The CIST column serves four roles; an MSTI row may serve any role the
340 # Bridge contract names
341 sub _ieee_mstp_known_role {
342 my ( $role, $any_role ) = @_;
343 return unless defined $role;
344 my $known
345 = $any_role
346 ? qr/^(?:disabled|root|designated|alternate|backup|boundary|master)$/
347 : qr/^(?:root|alternate|designated|backup)$/;
348 return $role if $role =~ $known;
349 return;
350 }
351  
352 sub i_edgeport_admin {
353 my ( $stp, $partial ) = @_;
354  
355 return $stp->_ieee_edge_ports( $partial, 'ieee_rstp_p_edge_admin',
356 'mstp_cist_p_edge_admin' )
357 || $stp->SUPER::i_edgeport_admin($partial);
358 }
359  
360 sub i_edgeport_oper {
361 my ( $stp, $partial ) = @_;
362  
363 return $stp->_ieee_edge_ports( $partial, 'ieee_rstp_p_edge_oper',
364 'mstp_cist_p_edge_oper' )
365 || $stp->SUPER::i_edgeport_oper($partial);
366 }
367  
368 # First of the two columns with rows for the lowest component, as
369 # { ifIndex => 1|0 }; nothing when both are empty. Not gated on protocol.
370 sub _ieee_edge_ports {
371 my ( $stp, $partial, @columns ) = @_;
372  
373 my $component = $stp->_ieee_stp_component();
374 return unless defined $component;
375  
376 foreach my $column (@columns) {
377 my $rows = $stp->$column() || {};
378 my %by_port;
379 foreach my $index ( keys %$rows ) {
380 my ( $row_component, $port ) = split /\./, $index, 2;
381 next unless $row_component == $component;
382 $by_port{$port} = $rows->{$index};
383 }
384 next unless keys %by_port;
385 return SNMP::Info::Bridge::_stp_edge_by_ifindex( $stp, \%by_port,
386 $partial );
387 }
388 return;
389 }
390  
391 sub stp_trees_complete {
392 my $stp = shift;
393  
394 return $stp->SUPER::stp_trees_complete()
395 unless $stp->_ieee_stp_is_mstp();
396  
397 my $trees = $stp->stp_trees() || {};
398 return 0 unless keys %$trees;
399 return 0
400 if $stp->_stp_walk_incomplete(qw/mstp_i_bridge mstp_cist_p_role/);
401 my $component = $stp->_ieee_stp_component();
402 return 0 if $stp->_ieee_stp_other_component($component);
403 return 1;
404 }
405  
406 sub stp_vlan_map {
407 my $stp = shift;
408  
409 return $stp->SUPER::stp_vlan_map() unless $stp->_ieee_stp_is_mstp();
410  
411 my $component = $stp->_ieee_stp_component();
412 return unless defined $component;
413 my $vlan_msti = $stp->mstp_vlan_msti() || {};
414 my %map;
415 foreach my $row ( keys %$vlan_msti ) {
416 my ( $row_component, $vlan ) = split /\./, $row, 2;
417 next unless $row_component == $component;
418 $map{$vlan} = _ieee_mstp_tree_key( $vlan_msti->{$row} );
419 }
420 return unless keys %map;
421 return \%map;
422 }
423  
424 1;
425  
426 __END__