Info/Layer3/Nexus.pm untested

filter: Statementstmt 0.0% 0 / 259 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 / 603 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.985007';
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 to create a community with an @ or map one to a VLAN
274 # context, yet answers community@N for VLAN N and community@mst-N for MST
275 # instance N (live NX-OS 10.6(2)). A bare community@N is never an instance.
276 # An MST context exists only under MST, so it is offered only where
277 # cContextMappingTable lists it (index: the length-prefixed context name).
278 sub _snmp_context {
279 my ( $nexus, $kind, $id ) = @_;
280  
281 return unless defined $kind and defined $id;
282 return { kind => 'vlan', id => $id, v2c => "%s\@$id", v3 => "vlan-$id" }
283 if $kind eq 'vlan';
284 return $nexus->_vrf_snmp_context($id) if $kind eq 'vrf';
285 return unless $kind eq 'mst';
286  
287 my $name = "mst-$id";
288 my $mapped = $nexus->vrf_name() || {};
289 my $index = join( '.', length $name, map {ord} split //, $name );
290 return unless exists $mapped->{$index};
291 return { kind => 'mst', id => $id, v2c => "%s\@$name", v3 => $name };
292 }
293  
294 # Each distinct non-empty VRF name cContextMappingTable maps. An empty
295 # value is a VLAN or MST context, never a VRF; any row lists default.
296 sub _vrf_vendor_instances {
297 my $nexus = shift;
298  
299 my $mapped = $nexus->vrf_name() || {};
300 my %instances;
301 foreach my $name ( grep { defined && length } values %$mapped ) {
302 next if $name eq 'default';
303 my $record = SNMP::Info::VRF::_vrf_record( $name, 'vrf', 'device' );
304 $record->{sources}{list} = 'CISCO-CONTEXT-MAPPING-MIB';
305 $instances{$name} = $record;
306 }
307 $nexus->_vrf_list_default( \%instances, 'CISCO-CONTEXT-MAPPING-MIB' )
308 if %$mapped;
309 return \%instances;
310 }
311  
312 sub _vrf_vendor_list_methods { return ('vrf_name'); }
313  
314 # Layer3::Cisco reads CISCO-VRF-MIB membership; Nexus lists from the
315 # context map, which holds no ports
316 sub _vrf_vendor_membership { return {}; }
317  
318 # Each VRF context serves only its own address rows, but ifTable and the
319 # IPv6 neighbors of other VRFs' ports too
320 sub _vrf_context_filters { return 1; }
321  
322 # The context cContextMappingTable maps to the VRF: the one named like the
323 # VRF, else the lowest sorted name; with none mapped, the VRF name, unless
324 # that name is a context mapped to another VRF. The default VRF is the
325 # default context, and an unnamed one is not a VRF.
326 sub _vrf_snmp_context {
327 my ( $nexus, $name ) = @_;
328  
329 return if !defined $name or $name eq q() or $name eq 'default';
330 my $mapped = $nexus->vrf_name() || {};
331 my ( %contexts, %of_other_vrf );
332 foreach my $index ( keys %$mapped ) {
333 next unless defined $mapped->{$index} and length $mapped->{$index};
334 my ( $length, @octets ) = split /\./, $index;
335 next unless $length == @octets;
336 my $context = join q(), map {chr} @octets;
337 my $owner = $mapped->{$index} eq $name ? \%contexts : \%of_other_vrf;
338 $owner->{$context} = 1;
339 }
340 return if !%contexts and $of_other_vrf{$name};
341 my ($context) = exists $contexts{$name} ? $name : sort keys %contexts;
342 $context = $name unless defined $context;
343 ( my $suffix = $context ) =~ s/%/%%/g;
344 return {
345 kind => 'vrf',
346 id => $name,
347 v2c => "%s\@$suffix",
348 v3 => $context,
349 };
350 }
351  
352 # The context the session's own SNMP-COMMUNITY-MIB row maps its community
353 # to. NX-OS serves snmpCommunityName only on the row of the community the
354 # session uses, and the context name on every row. '' is the default
355 # context. Undef when the session is SNMPv3 (its Context decides), the
356 # table is not served, or no row carries the session's community.
357 sub _vrf_community_context {
358 my $nexus = shift;
359  
360 my $session = $nexus->session() || {};
361 my $community = $session->{Community};
362 my $version = $session->{Version};
363 return if defined $version and $version eq '3';
364 return unless defined $community and length $community;
365  
366 my $names = $nexus->vrf_comm_name() || {};
367 foreach my $index ( sort keys %$names ) {
368 next unless defined $names->{$index};
369 next unless $names->{$index} eq $community;
370 return ( $nexus->vrf_comm_context() || {} )->{$index};
371 }
372 return;
373 }
374  
375 sub _get_snmpid_chassis {
376 my $self = shift;
377 my $funcname = (caller(0))[3]; # Name of this sub, including package, used for debug logging
378  
379 my $snmpid_chassis;
380 my $position;
381  
382 my $entity_entry = $self->e_class;
383 for ( keys %$entity_entry ) {
384 # filter by class (chassis is 3, but with proper MIBs loaded we should get the translated textual value)
385 if ( ($entity_entry->{$_} eq 'chassis') or ($entity_entry->{$_} eq '3') ) {
386 printf("%s - chassis with id %s found, position %s\n", $funcname, $_, $self->e_pos->{$_}) if $self->debug();
387  
388 # and if it's the topmost one
389 if ( !defined $position || $self->e_pos->{$_} < $position ) {
390 $snmpid_chassis = $_;
391 $position = $self->e_pos->{$_};
392 }
393 }
394 }
395 if ( defined $snmpid_chassis && defined $position ) {
396 printf(" %s - chassis with id %s, position %s selected\n", $funcname, $snmpid_chassis, $position) if $self->debug();
397 } else {
398 printf(" %s - no chassis found\n", $funcname) if $self->debug();
399 }
400  
401 return $snmpid_chassis;
402 }
403  
404 sub serial {
405 my $nexus = shift;
406 my $snmpid_chassis = $nexus->_get_snmpid_chassis;
407 return $nexus->e_serial($snmpid_chassis)->{$snmpid_chassis} if defined $snmpid_chassis;
408 return;
409 }
410  
411 # sysObjectID returns an IID to an entry in the CISCO-ENTITY-VENDORTYPE-OID-MIB.
412 # Look it up and return it.
413 sub model {
414 my $nexus = shift;
415 my $id = $nexus->id();
416  
417 unless ( defined $id ) {
418 print " SNMP::Info::Layer3::Nexus::model() - Device does not support sysObjectID\n" if $nexus->debug();
419 return;
420 }
421  
422 my $model = &SNMP::translateObj($id);
423  
424 return $id unless defined $model;
425  
426 $model =~ s/^cevChassis//i;
427 return $model;
428 }
429  
430 # nx-os 6 seems to swap the data of lldpLocPortDesc with that of
431 # lldpLocPortId, so remap it to make the main lldp module happy.
432 # nx-os 7 behaves as expected.
433 sub lldp_lport_desc {
434 my $nexus = shift;
435 my $osver = $nexus->os_ver();
436  
437 if (defined ($osver)) {
438 if ($osver =~ /^6.*/) {
439 return $nexus->SUPER::lldp_lport_id();
440 }
441 }
442 return $nexus->SUPER::lldp_lport_desc();
443 }
444  
445 # Reported version 6.x of NX-OS doesn't use the IPv4 address as index
446 # override methods in ipAddrTable
447 sub ip_table {
448 my $nexus = shift;
449 my $orig_ip_table = $nexus->SUPER::ip_table();
450  
451 my %ip_table;
452 foreach my $iid ( keys %$orig_ip_table ) {
453 my $ip = $orig_ip_table->{$iid};
454 next unless defined $ip;
455  
456 $ip_table{$ip} = $ip;
457 }
458  
459 my $local_addrs = $nexus->_local_addr();
460 foreach my $addr (keys %$local_addrs) {
461 $ip_table{$addr} = $addr unless exists $ip_table{$addr};
462 }
463  
464 return \%ip_table;
465 }
466  
467 sub ip_index {
468 my $nexus = shift;
469 my $orig_ip_table = $nexus->SUPER::ip_table();
470 my $orig_ip_index = $nexus->SUPER::ip_index();
471  
472 my %ip_index;
473 foreach my $iid ( keys %$orig_ip_table ) {
474 my $ip = $orig_ip_table->{$iid};
475 my $index = $orig_ip_index->{$iid};
476  
477 next unless ( defined $ip && defined $index );
478  
479 $ip_index{$ip} = $index;
480 }
481  
482 my $local_addrs = $nexus->_local_addr();
483 foreach my $addr (keys %$local_addrs) {
484 $ip_index{$addr} = 0 unless exists $ip_index{$addr};
485 }
486  
487 return \%ip_index;
488 }
489  
490 sub ip_netmask {
491 my $nexus = shift;
492 my $orig_ip_table = $nexus->SUPER::ip_table();
493 my $orig_ip_netmask = $nexus->SUPER::ip_netmask();
494  
495 my %ip_netmask;
496 foreach my $iid ( keys %$orig_ip_table ) {
497 my $ip = $orig_ip_table->{$iid};
498 my $netmask = $orig_ip_netmask->{$iid};
499  
500 next unless ( defined $ip && defined $netmask );
501  
502 $ip_netmask{$ip} = $netmask;
503 }
504  
505 my $local_addrs = $nexus->_local_addr();
506 foreach my $addr (keys %$local_addrs) {
507 $ip_netmask{$addr} = $local_addrs->{$addr} unless exists $ip_netmask{$addr};
508 }
509  
510 return \%ip_netmask;
511 }
512  
513 sub ip_broadcast {
514 my $nexus = shift;
515 my $orig_ip_table = $nexus->SUPER::ip_table();
516 my $orig_ip_broadcast = $nexus->SUPER::ip_broadcast();
517  
518 my %ip_broadcast;
519 foreach my $iid ( keys %$orig_ip_table ) {
520 my $ip = $orig_ip_table->{$iid};
521 my $broadcast = $orig_ip_broadcast->{$iid};
522  
523 next unless ( defined $ip && defined $broadcast );
524  
525 $ip_broadcast{$ip} = $broadcast;
526 }
527  
528 my $local_addrs = $nexus->_local_addr();
529 foreach my $addr (keys %$local_addrs) {
530 $ip_broadcast{$addr} = $addr unless exists $ip_broadcast{$addr};
531 }
532  
533 return \%ip_broadcast;
534 }
535  
536 sub _local_addr {
537 my $nexus = shift;
538 my $listen_addr = $nexus->udpLocalAddress() || {};
539 my %local_addr;
540 foreach my $sock (keys %$listen_addr) {
541 my $addr = $listen_addr->{$sock};
542 next if ($addr =~ /^127\./); # localhost
543 next if ($addr eq '0.0.0.0'); # "any"
544 next if ($addr =~ /^(\d+)\./ and $1 ge 224); # Class D or E space: Multicast or Experimental
545 $local_addr{$addr} = '255.255.255.255'; # Fictional netmask
546 }
547 return \%local_addr;
548 }
549  
550 1;
551 __END__