Info/Layer3/Nexus.pm untested

filter: Statementstmt 0.0% 0 / 256 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 102 MC/DCmcdc 0.0% 0 / 61 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 600 CC 59 SCAR 31.7
CC
59
Cov
0%
_vrf_snmp_context
72.0
_get_snmpid_chassis
56.0
_snmp_context
42.0
CRAP 23.8
1 # SNMP::Info::Layer3::Nexus
2 #
3 # Copyright (c) 2014 Eric Miller
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::Nexus;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use Scalar::Util ();
36 use SNMP::Info::Layer3::CiscoSwitch;
37  
38 our ($VERSION, %GLOBALS, %MIBS, %FUNCS, %MUNGE);
39  
40 # NOTE : Top-most items gets precedence for @ISA
41 @SNMP::Info::Layer3::Nexus::ISA = qw/
42 SNMP::Info::Layer3::CiscoSwitch
43 Exporter
44 /;
45  
46 @SNMP::Info::Layer3::Nexus::EXPORT_OK = qw//;
47  
48 $VERSION = '3.985003';
49  
50 # NOTE: Order creates precedence
51 # Example: v_name exists in Bridge.pm and CiscoVTP.pm
52 # Bridge is called from Layer3 and CiscoStpExtensions
53 # So we want CiscoVTP to come last to get the right one.
54 # The @ISA order should be reverse of these orders.
55  
56 %MIBS = (
57 %SNMP::Info::Layer3::CiscoSwitch::MIBS,
58 'CISCO-ENTITY-VENDORTYPE-OID-MIB' => 'cevMIBObjects',
59 'CISCO-CONTEXT-MAPPING-MIB' => 'cContextMappingMIBObjects',
60 'SNMP-COMMUNITY-MIB' => 'snmpCommunityName',
61 'CISCO-VPC-MIB' => 'cVpcRoleStatus',
62 'CISCO-NETWORK-VIRTUALIZATION-OVERLAY-MIB' => 'cnvoVNetVlan',
63 );
64  
65 %GLOBALS = (
66 %SNMP::Info::Layer3::CiscoSwitch::GLOBALS,
67 'mac' => 'dot1dBaseBridgeAddress',
68 # CISCO-NETWORK-VIRTUALIZATION-OVERLAY-MIB::cnvoNvoTable, instance nve1
69 'nve_vnis' => 'cnvoNvoConfiguredVni.1',
70 );
71  
72 %FUNCS = (
73 %SNMP::Info::Layer3::CiscoSwitch::FUNCS,
74 'vrf_name' => 'cContextMappingVrfName',
75 'vrf_comm_name' => 'snmpCommunityName',
76 'vrf_comm_context' => 'snmpCommunityContextName',
77  
78 # CISCO-VPC-MIB::cVpcRoleTable
79 'vpc_role' => 'cVpcRoleStatus',
80 'vpc_system_mac' => 'cVpcSystemOperMacAddress',
81 # CISCO-VPC-MIB::cVpcPeerKeepAliveConfigTable
82 'vpc_keepalive_peer' => 'cVpcPeerKeepAliveDestAddr',
83 # CISCO-VPC-MIB::cVpcPeerKeepAliveTable
84 'vpc_keepalive_stat' => 'cVpcPeerKeepAliveStatus',
85 # CISCO-VPC-MIB::cVpcStatusPeerLinkTable
86 'vpc_peer_link' => 'cVpcStatusPeerLinkIfIndex',
87 # CISCO-VPC-MIB::cVpcStatusHostLinkTable
88 'vpc_host_link_if' => 'cVpcStatusHostLinkIfIndex',
89 'vpc_host_link_stat' => 'cVpcStatusHostLinkStatus',
90 # CISCO-NETWORK-VIRTUALIZATION-OVERLAY-MIB::cnvoVNetTable
91 'vnet_vlan' => 'cnvoVNetVlan',
92 'vnet_vni_type' => 'cnvoVNetVniType',
93 );
94  
95 %MUNGE = (
96 %SNMP::Info::Layer3::CiscoSwitch::MUNGE,
97 'vpc_system_mac' => \&SNMP::Info::munge_mac,
98 'vpc_keepalive_peer' => \&SNMP::Info::munge_inetaddress,
99 );
100  
101 sub os {
102 return 'nx-os';
103 }
104  
105 sub os_ver {
106 my $nexus = shift;
107 my $descr = $nexus->description();
108  
109 return $1 if ( $descr =~ /\),\s+Version\s+(.+?),/ );
110 return $descr;
111 }
112  
113 sub _lag_vendor_walked {
114 return qw/vpc_role vpc_system_mac vpc_keepalive_peer vpc_keepalive_stat
115 vpc_peer_link vpc_host_link_if vpc_host_link_stat/;
116 }
117  
118 # Every vPC domain has a cVpcRoleTable row, indexed by the domain id
119 sub _lag_vendor_domains {
120 my $nexus = shift;
121  
122 my %columns = (
123 role => [ vpc_role => 'cVpcRoleStatus' ],
124 system_mac => [ vpc_system_mac => 'cVpcSystemOperMacAddress' ],
125 peer => [ vpc_keepalive_peer => 'cVpcPeerKeepAliveDestAddr' ],
126 status => [ vpc_keepalive_stat => 'cVpcPeerKeepAliveStatus' ],
127 peer_link => [ vpc_peer_link => 'cVpcStatusPeerLinkIfIndex' ],
128 );
129 my $domains = {};
130 foreach my $domain_id ( keys %{ $nexus->vpc_role() || {} } ) {
131 my $domain = { kind => 'vpc', sources => {} };
132 foreach my $field ( keys %columns ) {
133 my ( $method, $leaf ) = @{ $columns{$field} };
134 my $value = ( $nexus->$method() || {} )->{$domain_id};
135 next if !defined $value or $value eq '';
136 next if $field eq 'peer_link' and !$value;
137 $domain->{$field} = $value;
138 $domain->{sources}{$field} = 'CISCO-VPC-MIB::' . $leaf;
139 }
140 $domains->{$domain_id} = $domain;
141 }
142 return $domains;
143 }
144  
145 sub _lag_vendor_groups {
146 my ( $nexus, $groups ) = @_;
147  
148 $nexus->_vpc_mark_peer_links($groups);
149 $nexus->_vpc_add_host_links($groups);
150 return;
151 }
152  
153 # A channel the device reports only through the vPC tables still gets a record
154 sub _vpc_group_record {
155 my ( $groups, $ifindex ) = @_;
156  
157 return $groups->{$ifindex}
158 ||= SNMP::Info::IEEE802dot3ad::_lag_new_record();
159 }
160  
161 sub _vpc_mark_peer_links {
162 my ( $nexus, $groups ) = @_;
163  
164 my $peer_links = $nexus->vpc_peer_link() || {};
165 foreach my $ifindex ( values %$peer_links ) {
166 next unless $ifindex;
167 my $record = _vpc_group_record( $groups, $ifindex );
168 $record->{peer_link} = 1;
169 $record->{sources}{peer_link}
170 = 'CISCO-VPC-MIB::cVpcStatusPeerLinkIfIndex';
171 }
172 return;
173 }
174  
175 # The host link table is indexed domain.vpcId
176 sub _vpc_add_host_links {
177 my ( $nexus, $groups ) = @_;
178  
179 my $channels = $nexus->vpc_host_link_if() || {};
180 my $states = $nexus->vpc_host_link_stat() || {};
181 foreach my $iid ( keys %$channels ) {
182 next unless $channels->{$iid};
183 my ( $domain_id, $vpc_id ) = split /\./, $iid;
184 my $record = _vpc_group_record( $groups, $channels->{$iid} );
185 $record->{mc} = {
186 kind => 'vpc',
187 group => $vpc_id,
188 domain => $domain_id,
189 state => $states->{$iid},
190 };
191 $record->{sources}{mc} = 'CISCO-VPC-MIB::cVpcStatusHostLinkIfIndex';
192 }
193 return;
194 }
195  
196 sub _l2sb_vendor_walked { return qw/vnet_vlan vnet_vni_type/ }
197  
198 # The nve process was seen to crash on an NX-OSv 10.6(2) virtual image
199 # after a request whose answer was past the end of cnvoPeerTable; a GETBULK
200 # on cnvoVNetTable can reach there when the tables have few rows. To be
201 # safe, walk only when the first VNI nve1 lists has a row, read by an exact
202 # GET, and by GETNEXT, so each walk ends in the next column.
203 sub _l2sb_vendor_rows {
204 my $nexus = shift;
205  
206 my $vnis = $nexus->nve_vnis();
207 return unless defined $vnis and $vnis =~ /(\d+)/;
208 return unless $nexus->_nve_vnet_row_exists($1);
209  
210 my ( $vlans, $types );
211 {
212 local $nexus->{BulkWalk} = 0;
213 $vlans = $nexus->vnet_vlan() || {};
214 $types = $nexus->vnet_vni_type() || {};
215 }
216  
217 my @rows;
218 foreach my $iid ( keys %$vlans ) {
219 next unless $vlans->{$iid};
220 next unless ( $types->{$iid} // q{} ) eq q{l2};
221 my ( undef, $vni ) = split /\./, $iid;
222 push @rows, { vlan => $vlans->{$iid}, type => q{vni}, id => $vni };
223 }
224 return @rows;
225 }
226  
227 # nve1 with no member VNI answers "--"; the feature on without nve1 answers
228 # noSuchInstance. Feature off answers noSuchObject, as does an image without
229 # the MIB, so that stays unknown.
230 sub _l2sb_vendor_none {
231 my $nexus = shift;
232  
233 my $vnis = $nexus->nve_vnis();
234 return $vnis !~ /\d/ if defined $vnis;
235 my $answer = $nexus->_nve_exact_get( '_nve_vni_exception',
236 'cnvoNvoConfiguredVni.1' );
237 return defined $answer && $answer eq 'NOSUCHINSTANCE';
238 }
239  
240 # Offline without a saved answer, trust the cached columns.
241 sub _nve_vnet_row_exists {
242 my ( $nexus, $vni ) = @_;
243  
244 my $vlan = $nexus->_nve_exact_get( "_nve_vnet_row_$vni",
245 "cnvoVNetVlan.1.$vni" );
246 return 1 if !defined $vlan and $nexus->{Offline};
247 return defined $vlan && length $vlan && $vlan !~ /^NOSUCH/;
248 }
249  
250 # A raw GET of one instance: a partial fetch falls through to GETNEXT on
251 # noSuchInstance. Saved under an underscore key, which clear_cache resets;
252 # never sent Offline. An exception is saved as its NOSUCH string, no answer
253 # as the empty string.
254 sub _nve_exact_get {
255 my ( $nexus, $key, $oid ) = @_;
256  
257 return $nexus->{$key} if exists $nexus->{$key};
258 return if $nexus->{Offline};
259 my $sess = $nexus->session();
260 return unless Scalar::Util::blessed($sess) and $sess->can('get');
261 my $answer = $sess->get($oid);
262 return $nexus->{$key} = defined $answer ? $answer : q{};
263 }
264  
265 sub _context_kinds {
266 my ( $nexus, $feature ) = @_;
267  
268 return [ 'vlan', 'mst' ] if $feature eq 'stp';
269 return ['vrf'] if $feature eq 'vrf';
270 return;
271 }
272  
273 # NX-OS refuses a community map for VLAN and MST contexts: SNMPv3 only.
274 # An MST context exists only under MST, so it is offered only where
275 # cContextMappingTable lists it (index: the length-prefixed context name).
276 sub _snmp_context {
277 my ( $nexus, $kind, $id ) = @_;
278  
279 return unless defined $kind and defined $id;
280 return { kind => 'vlan', id => $id, v2c => undef, v3 => "vlan-$id" }
281 if $kind eq 'vlan';
282 return $nexus->_vrf_snmp_context($id) if $kind eq 'vrf';
283 return unless $kind eq 'mst';
284  
285 my $name = "mst-$id";
286 my $mapped = $nexus->vrf_name() || {};
287 my $index = join( '.', length $name, map {ord} split //, $name );
288 return unless exists $mapped->{$index};
289 return { kind => 'mst', id => $id, v2c => undef, v3 => $name };
290 }
291  
292 # Each distinct non-empty VRF name cContextMappingTable maps. An empty
293 # value is a VLAN or MST context, never a VRF; any row lists default.
294 sub _vrf_vendor_instances {
295 my $nexus = shift;
296  
297 my $mapped = $nexus->vrf_name() || {};
298 my %instances;
299 foreach my $name ( grep { defined && length } values %$mapped ) {
300 next if $name eq 'default';
301 my $record = SNMP::Info::VRF::_vrf_record( $name, 'vrf', 'device' );
302 $record->{sources}{list} = 'CISCO-CONTEXT-MAPPING-MIB';
303 $instances{$name} = $record;
304 }
305 $nexus->_vrf_list_default( \%instances, 'CISCO-CONTEXT-MAPPING-MIB' )
306 if %$mapped;
307 return \%instances;
308 }
309  
310 sub _vrf_vendor_list_methods { return ('vrf_name'); }
311  
312 # Layer3::Cisco reads CISCO-VRF-MIB membership; Nexus lists from the
313 # context map, which holds no ports
314 sub _vrf_vendor_membership { return {}; }
315  
316 # Each VRF context serves only its own address rows, but ifTable and the
317 # IPv6 neighbors of other VRFs' ports too
318 sub _vrf_context_filters { return 1; }
319  
320 # The context cContextMappingTable maps to the VRF: the one named like the
321 # VRF, else the lowest sorted name; with none mapped, the VRF name, unless
322 # that name is a context mapped to another VRF. The default VRF is the
323 # default context, and an unnamed one is not a VRF.
324 sub _vrf_snmp_context {
325 my ( $nexus, $name ) = @_;
326  
327 return if !defined $name or $name eq q() or $name eq 'default';
328 my $mapped = $nexus->vrf_name() || {};
329 my ( %contexts, %of_other_vrf );
330 foreach my $index ( keys %$mapped ) {
331 next unless defined $mapped->{$index} and length $mapped->{$index};
332 my ( $length, @octets ) = split /\./, $index;
333 next unless $length == @octets;
334 my $context = join q(), map {chr} @octets;
335 my $owner = $mapped->{$index} eq $name ? \%contexts : \%of_other_vrf;
336 $owner->{$context} = 1;
337 }
338 return if !%contexts and $of_other_vrf{$name};
339 my ($context) = exists $contexts{$name} ? $name : sort keys %contexts;
340 return {
341 kind => 'vrf',
342 id => $name,
343 v2c => undef,
344 v3 => defined $context ? $context : $name,
345 };
346 }
347  
348 # The context the session's own SNMP-COMMUNITY-MIB row maps its community
349 # to. NX-OS serves snmpCommunityName only on the row of the community the
350 # session uses, and the context name on every row. '' is the default
351 # context. Undef when the session is SNMPv3 (its Context decides), the
352 # table is not served, or no row carries the session's community.
353 sub _vrf_community_context {
354 my $nexus = shift;
355  
356 my $session = $nexus->session() || {};
357 my $community = $session->{Community};
358 my $version = $session->{Version};
359 return if defined $version and $version eq '3';
360 return unless defined $community and length $community;
361  
362 my $names = $nexus->vrf_comm_name() || {};
363 foreach my $index ( sort keys %$names ) {
364 next unless defined $names->{$index};
365 next unless $names->{$index} eq $community;
366 return ( $nexus->vrf_comm_context() || {} )->{$index};
367 }
368 return;
369 }
370  
371 sub _get_snmpid_chassis {
372 my $self = shift;
373 my $funcname = (caller(0))[3]; # Name of this sub, including package, used for debug logging
374  
375 my $snmpid_chassis;
376 my $position;
377  
378 my $entity_entry = $self->e_class;
379 for ( keys %$entity_entry ) {
380 # filter by class (chassis is 3, but with proper MIBs loaded we should get the translated textual value)
381 if ( ($entity_entry->{$_} eq 'chassis') or ($entity_entry->{$_} eq '3') ) {
382 printf("%s - chassis with id %s found, position %s\n", $funcname, $_, $self->e_pos->{$_}) if $self->debug();
383  
384 # and if it's the topmost one
385 if ( !defined $position || $self->e_pos->{$_} < $position ) {
386 $snmpid_chassis = $_;
387 $position = $self->e_pos->{$_};
388 }
389 }
390 }
391 if ( defined $snmpid_chassis && defined $position ) {
392 printf(" %s - chassis with id %s, position %s selected\n", $funcname, $snmpid_chassis, $position) if $self->debug();
393 } else {
394 printf(" %s - no chassis found\n", $funcname) if $self->debug();
395 }
396  
397 return $snmpid_chassis;
398 }
399  
400 sub serial {
401 my $nexus = shift;
402 my $snmpid_chassis = $nexus->_get_snmpid_chassis;
403 return $nexus->e_serial($snmpid_chassis)->{$snmpid_chassis} if defined $snmpid_chassis;
404 return;
405 }
406  
407 # sysObjectID returns an IID to an entry in the CISCO-ENTITY-VENDORTYPE-OID-MIB.
408 # Look it up and return it.
409 sub model {
410 my $nexus = shift;
411 my $id = $nexus->id();
412  
413 unless ( defined $id ) {
414 print " SNMP::Info::Layer3::Nexus::model() - Device does not support sysObjectID\n" if $nexus->debug();
415 return;
416 }
417  
418 my $model = &SNMP::translateObj($id);
419  
420 return $id unless defined $model;
421  
422 $model =~ s/^cevChassis//i;
423 return $model;
424 }
425  
426 # nx-os 6 seems to swap the data of lldpLocPortDesc with that of
427 # lldpLocPortId, so remap it to make the main lldp module happy.
428 # nx-os 7 behaves as expected.
429 sub lldp_lport_desc {
430 my $nexus = shift;
431 my $osver = $nexus->os_ver();
432  
433 if (defined ($osver)) {
434 if ($osver =~ /^6.*/) {
435 return $nexus->SUPER::lldp_lport_id();
436 }
437 }
438 return $nexus->SUPER::lldp_lport_desc();
439 }
440  
441 # Reported version 6.x of NX-OS doesn't use the IPv4 address as index
442 # override methods in ipAddrTable
443 sub ip_table {
444 my $nexus = shift;
445 my $orig_ip_table = $nexus->SUPER::ip_table();
446  
447 my %ip_table;
448 foreach my $iid ( keys %$orig_ip_table ) {
449 my $ip = $orig_ip_table->{$iid};
450 next unless defined $ip;
451  
452 $ip_table{$ip} = $ip;
453 }
454  
455 my $local_addrs = $nexus->_local_addr();
456 foreach my $addr (keys %$local_addrs) {
457 $ip_table{$addr} = $addr unless exists $ip_table{$addr};
458 }
459  
460 return \%ip_table;
461 }
462  
463 sub ip_index {
464 my $nexus = shift;
465 my $orig_ip_table = $nexus->SUPER::ip_table();
466 my $orig_ip_index = $nexus->SUPER::ip_index();
467  
468 my %ip_index;
469 foreach my $iid ( keys %$orig_ip_table ) {
470 my $ip = $orig_ip_table->{$iid};
471 my $index = $orig_ip_index->{$iid};
472  
473 next unless ( defined $ip && defined $index );
474  
475 $ip_index{$ip} = $index;
476 }
477  
478 my $local_addrs = $nexus->_local_addr();
479 foreach my $addr (keys %$local_addrs) {
480 $ip_index{$addr} = 0 unless exists $ip_index{$addr};
481 }
482  
483 return \%ip_index;
484 }
485  
486 sub ip_netmask {
487 my $nexus = shift;
488 my $orig_ip_table = $nexus->SUPER::ip_table();
489 my $orig_ip_netmask = $nexus->SUPER::ip_netmask();
490  
491 my %ip_netmask;
492 foreach my $iid ( keys %$orig_ip_table ) {
493 my $ip = $orig_ip_table->{$iid};
494 my $netmask = $orig_ip_netmask->{$iid};
495  
496 next unless ( defined $ip && defined $netmask );
497  
498 $ip_netmask{$ip} = $netmask;
499 }
500  
501 my $local_addrs = $nexus->_local_addr();
502 foreach my $addr (keys %$local_addrs) {
503 $ip_netmask{$addr} = $local_addrs->{$addr} unless exists $ip_netmask{$addr};
504 }
505  
506 return \%ip_netmask;
507 }
508  
509 sub ip_broadcast {
510 my $nexus = shift;
511 my $orig_ip_table = $nexus->SUPER::ip_table();
512 my $orig_ip_broadcast = $nexus->SUPER::ip_broadcast();
513  
514 my %ip_broadcast;
515 foreach my $iid ( keys %$orig_ip_table ) {
516 my $ip = $orig_ip_table->{$iid};
517 my $broadcast = $orig_ip_broadcast->{$iid};
518  
519 next unless ( defined $ip && defined $broadcast );
520  
521 $ip_broadcast{$ip} = $broadcast;
522 }
523  
524 my $local_addrs = $nexus->_local_addr();
525 foreach my $addr (keys %$local_addrs) {
526 $ip_broadcast{$addr} = $addr unless exists $ip_broadcast{$addr};
527 }
528  
529 return \%ip_broadcast;
530 }
531  
532 sub _local_addr {
533 my $nexus = shift;
534 my $listen_addr = $nexus->udpLocalAddress() || {};
535 my %local_addr;
536 foreach my $sock (keys %$listen_addr) {
537 my $addr = $listen_addr->{$sock};
538 next if ($addr =~ /^127\./); # localhost
539 next if ($addr eq '0.0.0.0'); # "any"
540 next if ($addr =~ /^(\d+)\./ and $1 ge 224); # Class D or E space: Multicast or Experimental
541 $local_addr{$addr} = '255.255.255.255'; # Fictional netmask
542 }
543 return \%local_addr;
544 }
545  
546 1;
547 __END__