Info/Layer3/H3C.pm untested

filter: Statementstmt 0.0% 0 / 232 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 135 MC/DCmcdc 0.0% 0 / 72 Subroutinesub 0.0% 0 / 40 Podpod 0.0% 0 / 35 totaltotal 0.0% 0 / 614 CC 51 SCAR 29.3
CC
51
Cov
0%
stp_trees
72.0
_h3c_stp_protocol
56.0
_attached_agg_ports
30.0
CRAP 18.7
1 # SNMP::Info::Layer3::H3C
2 #
3 # Copyright (c) 2018 Jeroen van Ingen
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::Layer3::H3C;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::Layer3;
36 use SNMP::Info::IEEE802dot3ad 'agg_ports_lag';
37  
38 @SNMP::Info::Layer3::H3C::ISA = qw/
39 SNMP::Info::IEEE802dot3ad
40 SNMP::Info::Layer3
41 Exporter
42 /;
43 @SNMP::Info::Layer3::H3C::EXPORT_OK = qw/
44 agg_ports
45 /;
46  
47 our ($VERSION, %GLOBALS, %MIBS, %FUNCS, %MUNGE);
48  
49 $VERSION = '3.985003';
50  
51 %MIBS = (
52 %SNMP::Info::Layer3::MIBS,
53 %SNMP::Info::IEEE802dot3ad::MIBS,
54 'HH3C-LswDEVM-MIB' => 'hh3cDevMFanStatus',
55 'HH3C-LswINF-MIB' => 'hh3cSlotPortMax',
56 'HH3C-LSW-DEV-ADM-MIB' => 'hh3cLswSysVersion',
57 'HH3C-PRODUCT-ID-MIB' => 'hh3c-s5500-28C-EI',
58 'HH3C-ENTITY-VENDORTYPE-OID-MIB' => 'hh3cevtOther',
59 'HH3C-LswMSTP-MIB' => 'hh3cdot1sStpForceVersion',
60 'HH3C-IP-ADDRESS-MIB' => 'hh3cIpAddrSetMask',
61 'HH3C-LswVLAN-MIB' => 'hh3cdot1qVlanName',
62 );
63  
64 %GLOBALS = (
65 %SNMP::Info::Layer3::GLOBALS,
66 'fan' => 'hh3cDevMFanStatus.1',
67 'ps1_status' => 'hh3cDevMPowerStatus.1',
68 'ps2_status' => 'hh3cDevMPowerStatus.2',
69  
70 # HH3C-LswMSTP-MIB
71 'h3c_stp_version' => 'hh3cdot1sStpForceVersion',
72 );
73  
74 %FUNCS = (
75 %SNMP::Info::Layer3::FUNCS,
76 %SNMP::Info::IEEE802dot3ad::FUNCS,
77 i_duplex_admin => 'hh3cifEthernetDuplex',
78 ad_port_attached_agg => 'dot3adAggPortAttachedAggID',
79  
80 # HH3C-LswMSTP-MIB::hh3cdot1sInstanceTable, indexed by instance
81 'h3c_stp_i_id' => 'hh3cdot1sInstanceID',
82 'h3c_stp_i_bridge' => 'hh3cdot1sMstiBridgeID',
83 'h3c_stp_i_root' => 'hh3cdot1sMstiDesignedRoot',
84  
85 # HH3C-LswMSTP-MIB::hh3cdot1sPortTable, indexed <instance>.<bridge port>
86 'h3c_stp_p_state' => 'hh3cdot1sMstiState',
87 'h3c_stp_p_bridge' => 'hh3cdot1sMstiDesignatedBridge',
88 'h3c_stp_p_port' => 'hh3cdot1sMstiDesignatedPort',
89  
90 # HH3C-LswMSTP-MIB::hh3cdot1sMstiVlanTable, indexed by VLAN id
91 'h3c_stp_vlan_mst' => 'hh3cdot1sOperMstID',
92  
93 # HH3C-IP-ADDRESS-MIB::hh3cIpAddrSetTable, indexed
94 # ifIndex.addrType.length.address
95 'vrf_h3c_addr_mask' => 'hh3cIpAddrSetMask',
96  
97 # HH3C-LswVLAN-MIB::hh3cdot1qVlanMIBTable, indexed by VLAN id
98 'h3c_v_name' => 'hh3cdot1qVlanName',
99 );
100  
101 %MUNGE = (
102 %SNMP::Info::Layer3::MUNGE,
103 %SNMP::Info::IEEE802dot3ad::MUNGE,
104 'h3c_stp_i_bridge' => \&SNMP::Info::munge_prio_mac,
105 'h3c_stp_i_root' => \&SNMP::Info::munge_prio_mac,
106 'h3c_stp_p_bridge' => \&SNMP::Info::munge_prio_mac,
107 'h3c_stp_p_port' => \&SNMP::Info::munge_prio_port,
108 );
109  
110 sub vendor {
111 my $h3c = shift;
112 my $mfg = $h3c->entPhysicalMfgName(1) || {};
113 return $mfg->{1} || "h3c";
114 }
115  
116 sub model {
117 my $h3c = shift;
118 my $id = $h3c->id() || '';
119 my $descr = $h3c->description() || '';
120  
121 my $entclass = $h3c->entPhysicalClass(2) || {};
122 my $entity = $entclass->{2} || '';
123 my $entname = $h3c->entPhysicalName(2) || {};
124  
125 if ($descr =~ /^.*\n(.*)\n/) {
126 return $1;
127 } elsif ($entity =~ /^(3|chassis)$/) {
128 return $entname->{2};
129 } else {
130 return &SNMP::translateObj($id) || $id;
131 }
132 }
133  
134 sub os {
135 my $h3c = shift;
136 my $descr = $h3c->description();
137  
138 return $1 if ( $descr =~ /(\S+)\s+Platform Software/ );
139 return "H3C";
140 }
141  
142 sub os_ver {
143 my $h3c = shift;
144 my $descr = $h3c->description();
145 # my $version = $h3c->hh3cLswSysVersion(); # Don't use, indicates base version only, no release details
146 my $ver_release = $h3c->entPhysicalSoftwareRev(2) || {};
147 my $os_ver = undef;
148  
149 $os_ver = "$1 $2" if ( $descr =~ /Software Version ([^,]+),.*(Release\s\S+)/i );
150 $os_ver = "$1" if ( $descr =~ /Product Version ([0-9.]+)/i );
151  
152 return $ver_release->{2} || $os_ver;
153 }
154  
155 # Comware 5 on the V1910 and 1920 keeps the default "VLAN 0002" in
156 # dot1qVlanStaticName and the configured name only in hh3cdot1qVlanName.
157 # Replace names only, so v_index keeps the Q-BRIDGE keys.
158 sub v_name {
159 my $h3c = shift;
160 my $partial = shift;
161  
162 my $v_name = $h3c->SUPER::v_name($partial);
163 return $v_name unless ref $v_name eq 'HASH' and %$v_name;
164  
165 my $h3c_names = $h3c->h3c_v_name($partial) || {};
166 my %names = %$v_name;
167 foreach my $vlan ( keys %names ) {
168 my $h3c_name = $h3c_names->{$vlan};
169 next unless defined $h3c_name and length $h3c_name;
170 $names{$vlan} = $h3c_name;
171 }
172 return \%names;
173 }
174  
175 sub i_ignore {
176 my $l3 = shift;
177 my $partial = shift;
178  
179 my $interfaces = $l3->interfaces($partial) || {};
180  
181 my %i_ignore;
182 foreach my $if ( keys %$interfaces ) {
183  
184 # lo0 etc
185 if ( $interfaces->{$if} =~ /\blo\d*\b/i ) {
186 $i_ignore{$if}++;
187 }
188 }
189 return \%i_ignore;
190 }
191  
192 # Some Comware firmware uses a different numbering scheme in the PortList.
193 # AttachedAggID is indexed directly by ifIndex, including unselected members.
194 sub _attached_agg_ports {
195 my $h3c = shift;
196 my $attached = $h3c->ad_port_attached_agg() || {};
197 my $interfaces = $h3c->interfaces() || {};
198 my %members;
199 foreach my $member (keys %$attached) {
200 my $aggregate = $attached->{$member};
201 next unless defined $aggregate && $aggregate =~ /^\d+$/ && $aggregate > 0;
202 next if $member eq $aggregate;
203 next unless ($interfaces->{$member} || '') =~ /Ethernet/;
204 next unless ($interfaces->{$aggregate} || '') =~ /^Bridge-Aggregation\d+$/;
205 $members{$member} = $aggregate;
206 }
207 return \%members;
208 }
209  
210 sub agg_ports {
211 my $h3c = shift;
212 my $members = $h3c->_attached_agg_ports();
213 return $members if scalar keys %$members;
214 return agg_ports_lag($h3c, @_);
215 }
216  
217 sub _agg_ports_walked {
218 return qw/ad_port_attached_agg ad_lag_ports bp_index/;
219 }
220  
221 sub i_stp_state {
222 my ($h3c, $partial) = @_;
223  
224 # dot1dStpPortState there matches no VLAN's tree, so it would label a
225 # port with the wrong tree's state
226 return {} if $h3c->_h3c_comware7()
227 and ( $h3c->_h3c_stp_protocol() || '' ) eq 'pvst';
228 my %states = %{ $h3c->SUPER::i_stp_state($partial) || {} };
229 my $members = $h3c->_attached_agg_ports();
230 delete @states{keys %$members};
231 return \%states;
232 }
233  
234 # CH: overwrite and always return empty mapping for qb_fdb_index
235 # see https://github.com/netdisco/snmp-info/issues/218
236 # if no fdb->vlan mapping is available the fdb is assumed to be the vlan
237 # which is ok for h3c
238 # otherwise h3c will deliver a broken vlan mapping and nodes will jump
239 # other all vlans
240 sub qb_fdb_index {
241 return {};
242 }
243  
244 # The OS version is not in os_ver(), whose regex misses some Comware 7
245 # sysDescr strings
246 sub _h3c_comware7 {
247 my $h3c = shift;
248 return ( $h3c->description() || '' ) =~ /Software Version 7\./ ? 1 : 0;
249 }
250  
251 # 0 when a named walk is known to be incomplete; an absent or empty column
252 # is complete
253 sub _h3c_stp_shape_ok {
254 my ( $h3c, @methods ) = @_;
255  
256 foreach my $method (@methods) {
257 my $status = $h3c->walk_status($method);
258 return 0 if $status and !$status->{complete};
259 }
260 return 1;
261 }
262  
263 # The version is a read-write setting, so it names the protocol the device
264 # runs. Its enumeration has no PVST value, so under rstp the instance table
265 # shape is the only indicator: VLAN rows with no bridge or port-state column
266 sub _h3c_stp_protocol {
267 my $h3c = shift;
268  
269 my $version = $h3c->h3c_stp_version();
270 return unless defined $version;
271 return 'stp' if $version =~ /^(?:stp|0)$/;
272 return 'mstp' if $version =~ /^(?:mstp|3)$/;
273 return unless $version =~ /^(?:rstp|2)$/;
274  
275 # walk_status is set by the walk, so read the columns before checking it
276 my $has_instances = %{ $h3c->h3c_stp_i_id() || {} };
277 my $has_bridges = %{ $h3c->h3c_stp_i_bridge() || {} };
278 my $has_states = %{ $h3c->h3c_stp_p_state() || {} };
279 return unless $h3c->_h3c_stp_shape_ok(
280 qw/h3c_stp_i_id h3c_stp_i_bridge h3c_stp_p_state/);
281 return 'pvst' if $has_instances and !$has_bridges and !$has_states;
282 return 'rstp';
283 }
284  
285 sub hasSTP {
286 my $h3c = shift;
287 return ( $h3c->_h3c_stp_protocol() || '' ) eq 'stp' ? 1 : 0;
288 }
289  
290 sub hasRSTP {
291 my $h3c = shift;
292 return ( $h3c->_h3c_stp_protocol() || '' ) eq 'rstp' ? 1 : 0;
293 }
294  
295 sub hasMSTP {
296 my $h3c = shift;
297 return ( $h3c->_h3c_stp_protocol() || '' ) eq 'mstp' ? 1 : 0;
298 }
299  
300 sub hasPVST {
301 my $h3c = shift;
302 return ( $h3c->_h3c_stp_protocol() || '' ) eq 'pvst' ? 1 : 0;
303 }
304  
305 sub stp_protocols {
306 my $h3c = shift;
307  
308 my $protocol = $h3c->_h3c_stp_protocol();
309 return unless defined $protocol;
310 return [$protocol];
311 }
312  
313 sub stp_trees {
314 my $h3c = shift;
315  
316 return unless $h3c->_stp_default_context_ok();
317 return $h3c->{_stp_trees} if $h3c->{_stp_trees};
318  
319 my $protocol = $h3c->_h3c_stp_protocol() or return {};
320 my $measured
321 = ( $h3c->_h3c_comware7() and $protocol =~ /^(?:mstp|rstp|pvst)$/ )
322 ? 1
323 : 0;
324  
325 my $trees;
326 if ( $protocol eq 'mstp' ) {
327 $trees = $h3c->_h3c_mstp_trees($measured);
328 }
329 elsif ( $protocol eq 'pvst' ) {
330 $trees = $h3c->_h3c_pvst_trees($measured);
331 }
332 else {
333 my $record = $h3c->_stp_dot1d_tree();
334 $trees
335 = $record
336 ? { 'cist.0' => { %$record, protocol => $protocol,
337 measured => $measured } }
338 : {};
339 }
340 $h3c->{_stp_trees} = $trees if keys %$trees;
341 return $trees;
342 }
343  
344 sub _h3c_pvst_trees {
345 my ( $h3c, $measured ) = @_;
346  
347 my $instances = $h3c->h3c_stp_i_id() || {};
348 my %trees;
349 foreach my $vlan ( grep { $_ } keys %$instances ) {
350 $trees{"vlan.$vlan"} = {
351 type => 'vlan',
352 id => $vlan,
353 vlan => $vlan,
354 protocol => 'pvst',
355 measured => $measured,
356 ports_complete => 0,
357 contexts => [],
358 bridge => undef,
359 root => undef,
360 };
361 }
362 return \%trees;
363 }
364  
365 sub _h3c_tree_key {
366 my $instance = shift;
367 return $instance ? "msti.$instance" : 'cist.0';
368 }
369  
370 sub _h3c_mstp_trees {
371 my ( $h3c, $measured ) = @_;
372  
373 my $instances = $h3c->h3c_stp_i_id() || {};
374 my $bridges = $h3c->h3c_stp_i_bridge() || {};
375 my $roots = $h3c->h3c_stp_i_root() || {};
376 my $states = $h3c->h3c_stp_p_state() || {};
377 my $bp_index = $h3c->bp_index() || {};
378  
379 my %unmapped;
380 foreach my $row ( keys %$states ) {
381 my ( $instance, $port ) = split /\./, $row, 2;
382 $unmapped{$instance} = 1 unless defined $bp_index->{$port};
383 }
384  
385 my %trees;
386 foreach my $instance ( keys %$instances ) {
387 $trees{ _h3c_tree_key($instance) } = {
388 type => $instance ? 'msti' : 'cist',
389 id => $instance,
390 protocol => 'mstp',
391 measured => $measured,
392 ports_complete => $unmapped{$instance} ? 0 : 1,
393 contexts => [ { kind => 'none' } ],
394 bridge => scalar SNMP::Info::Bridge::_stp_decode_bridge_id(
395 $bridges->{$instance} ),
396 root => scalar SNMP::Info::Bridge::_stp_decode_bridge_id(
397 $roots->{$instance} ),
398 };
399 }
400 return \%trees;
401 }
402  
403 sub stp_tree_ports {
404 my ( $h3c, $tree_key ) = @_;
405  
406 my $tree = $h3c->_stp_tree_for_key($tree_key) or return;
407 return {} if $tree->{protocol} eq 'pvst';
408 my $rows
409 = $tree->{protocol} eq 'mstp'
410 ? $h3c->_h3c_mstp_port_rows($tree)
411 : $h3c->_stp_dot1d_port_rows($tree);
412  
413 delete @{$rows}{ keys %{ $h3c->_attached_agg_ports() } };
414 return $rows;
415 }
416  
417 sub _h3c_mstp_port_rows {
418 my ( $h3c, $tree ) = @_;
419  
420 my $states = $h3c->h3c_stp_p_state() || {};
421 my $d_bridge = $h3c->h3c_stp_p_bridge() || {};
422 my $d_port = $h3c->h3c_stp_p_port() || {};
423 my $enable = $h3c->stp_p_enable() || {};
424 my $bp_index = $h3c->bp_index() || {};
425  
426 my %rows;
427 foreach my $row ( keys %$states ) {
428 my ( $instance, $port ) = split /\./, $row, 2;
429 next unless $instance == $tree->{id};
430  
431 my $iid = $bp_index->{$port};
432 next unless defined $iid;
433 $rows{$iid} = {
434 tree_type => $tree->{type},
435 tree_id => $tree->{id},
436 state => $states->{$row},
437 role => undef,
438 role_raw => undef,
439 enabled =>
440 scalar SNMP::Info::Bridge::_stp_enabled( $enable->{$port} ),
441 designated_bridge =>
442 scalar SNMP::Info::Bridge::_stp_decode_bridge_id(
443 $d_bridge->{$row} ),
444 designated_port =>
445 scalar SNMP::Info::Bridge::_stp_decode_port_id(
446 $d_port->{$row} ),
447 };
448 }
449 return \%rows;
450 }
451  
452 sub stp_trees_complete {
453 my $h3c = shift;
454  
455 my $trees = $h3c->stp_trees() || {};
456 return 0
457 if ( $h3c->_h3c_stp_protocol() || '' ) eq 'mstp'
458 and $h3c->_stp_walk_incomplete('h3c_stp_i_id');
459 return keys %$trees ? 1 : 0;
460 }
461  
462 sub stp_vlan_map {
463 my $h3c = shift;
464  
465 return unless ( $h3c->_h3c_stp_protocol() || '' ) eq 'mstp';
466 my $instances = $h3c->h3c_stp_vlan_mst() || {};
467 return unless keys %$instances;
468 return {
469 map { $_ => _h3c_tree_key( $instances->{$_} ) } keys %$instances
470 };
471 }
472  
473 # Comware 7 serves ipAddrTable for the default instance only, and no
474 # ipAddressTable. hh3cIpAddrSetTable lists every instance's IPv4 addresses
475 # by port, and IPV6-MIB ipv6AddrTable the IPv6 ones, link-locals included.
476 sub _vrf_vendor_address_rows {
477 my ( $h3c, $name ) = @_;
478  
479 return {
480 %{ $h3c->_vrf_ipv6_mib_address_rows($name) },
481 %{ $h3c->_h3c_ipv4_address_rows($name) },
482 };
483 }
484  
485 sub _h3c_ipv4_address_rows {
486 my ( $h3c, $name ) = @_;
487  
488 my $masks = $h3c->_vrf_column( 'vrf_h3c_addr_mask', $name );
489 my %rows;
490 foreach my $iid ( keys %$masks ) {
491 my ( $ifindex, $type, $length, @octets ) = split /\./, $iid;
492 next unless ( $type // q() ) eq '1' and $length == 4;
493 next unless @octets == 4;
494 my $ip = join q(.), @octets;
495 $rows{"$ifindex.$ip"} = {
496 ifindex => $ifindex,
497 ip => $ip,
498 prefix_len =>
499 scalar SNMP::Info::VRF::_vrf_mask_length( $masks->{$iid} ),
500 family => 'ipv4',
501 };
502 }
503 return \%rows;
504 }
505  
506 # Host link-locals are served as IPv6z in IP-MIB, which ipv6_n2p_addr
507 # does not return; IPV6-MIB ipv6NetToMediaTable has them unzoned
508 sub _vrf_nd_from_ipv6_mib { return 1; }
509  
510 sub _vrf_subnets_from_routes { return 1; }
511  
512 # Default tree is not the common tree: dot1dStp matches no VLAN's tree under
513 # PVST
514 sub _stp_default_is_common { return 0; }
515  
516 # Comware 7 indexes dot1qPvid by ifIndex instead of dot1dBasePort. A key
517 # that is an ifIndex but not a bridge port can only mean that. Comware 5
518 # numbers bridge ports as ifIndexes, so it never trips this.
519 sub i_vlan {
520 my $h3c = shift;
521 my $partial = shift;
522  
523 my $i_pvid = $h3c->qb_i_vlan() || {};
524 return $h3c->SUPER::i_vlan($partial)
525 unless _pvid_by_ifindex(
526 $i_pvid,
527 $h3c->bp_index() || {},
528 $h3c->interfaces() || {}
529 );
530  
531 return {
532 map { $_ => $i_pvid->{$_} }
533 grep { !defined $partial or $_ eq $partial } keys %$i_pvid
534 };
535 }
536  
537 sub i_untagged { goto &i_vlan }
538  
539 sub _pvid_by_ifindex {
540 my ( $i_pvid, $bp_index, $interfaces ) = @_;
541  
542 my @keys = keys %$i_pvid;
543 return 0 if !@keys or grep { !exists $interfaces->{$_} } @keys;
544 return scalar grep { !exists $bp_index->{$_} } @keys;
545 }
546  
547 1;
548 __END__