Info/IEEE802dot3ad.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 310 CC 23 SCAR 26.4
CC
23
Cov
0%
agg_ports_lag
30.0
_lag_identity_rows
30.0
_lag_drop_port_rows
30.0
CRAP 14.0
1 # SNMP::Info::IEEE802dot3ad
2 #
3 # Copyright (c) 2018 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::IEEE802dot3ad;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::Aggregate;
36  
37 @SNMP::Info::IEEE802dot3ad::ISA = qw/
38 SNMP::Info::Aggregate
39 Exporter
40 /;
41 @SNMP::Info::IEEE802dot3ad::EXPORT_OK = qw/
42 agg_ports_lag
43 /;
44  
45 our ($VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE);
46  
47 $VERSION = '3.985003';
48  
49 %MIBS = (
50 %SNMP::Info::Aggregate::MIBS,
51 'IEEE8023-LAG-MIB' => 'dot3adAggPortSelectedAggID',
52 );
53  
54 %GLOBALS = ();
55  
56 %FUNCS = (
57 'ad_lag_ports' => 'dot3adAggPortListPorts',
58  
59 # IEEE8023-LAG-MIB::dot3adAggTable
60 'lag_actor_system' => 'dot3adAggActorSystemID',
61 'lag_actor_key' => 'dot3adAggActorOperKey',
62 'lag_partner_system' => 'dot3adAggPartnerSystemID',
63 'lag_partner_key' => 'dot3adAggPartnerOperKey',
64  
65 # IEEE8023-LAG-MIB::dot3adAggPortTable
66 'lag_port_attached' => 'dot3adAggPortAttachedAggID',
67 'lag_port_partner_system' => 'dot3adAggPortPartnerOperSystemID',
68 'lag_port_partner_key' => 'dot3adAggPortPartnerOperKey',
69 );
70  
71 %MUNGE = (
72 'ad_lag_ports' => \&SNMP::Info::munge_port_list,
73 'lag_actor_system' => \&SNMP::Info::munge_mac,
74 'lag_partner_system' => \&SNMP::Info::munge_mac,
75 'lag_port_partner_system' => \&SNMP::Info::munge_mac,
76 );
77  
78 my $LAG_MIB = 'IEEE8023-LAG-MIB::';
79 my $NO_PARTNER_SYSTEM = '00:00:00:00:00:00';
80  
81 sub agg_ports_lag {
82 my $dev = shift;
83  
84 # TODO: implement partial
85 my $ports = $dev->ad_lag_ports();
86 my $index = $dev->bp_index() || {};
87  
88 return {} unless ref {} eq ref $ports and scalar keys %$ports;
89  
90 my $ret = {};
91 foreach my $m ( keys %$ports ) {
92 my $idx = $m;
93 my $portlist = $ports->{$m};
94 next unless $portlist;
95  
96 # While dot3adAggTable is indexed by ifIndex, the portlist is indexed
97 # with a dot1dBasePort, so we need to use dot1dBasePortIfIndex to map to
98 # the ifIndex. If we don't have dot1dBasePortIfIndex assume
99 # dot1dBasePort = ifIndex
100 for ( my $i = 0; $i <= scalar(@$portlist); $i++ ) {
101 my $ifindex = $i+1;
102 if ( exists($index->{$i+1}) and defined($index->{$i+1}) ) {
103 $ifindex = $index->{$i+1};
104 }
105 $ret->{$ifindex} = $idx if ( @$portlist[$i] );
106 }
107 }
108  
109 return $ret;
110 }
111  
112 sub lag_groups {
113 my $self = shift;
114  
115 my $groups = {};
116 my $map = $self->_lag_aggregator_map() || {};
117 my $rows = $self->_lag_identity_rows($map);
118 foreach my $key ( keys %$rows ) {
119 $groups->{$key} = $self->_lag_identity_record( $rows->{$key} );
120 }
121 $self->_lag_add_members( $groups, $map );
122 $self->_lag_vendor_groups($groups);
123 $self->_lag_drop_port_rows( $groups, $map );
124  
125 return $groups;
126 }
127  
128 sub mlag_domains {
129 my $self = shift;
130  
131 return $self->_lag_vendor_domains() || {};
132 }
133  
134 sub lag_groups_complete {
135 my $self = shift;
136  
137 my $groups = $self->lag_groups();
138 my $domains = $self->mlag_domains();
139  
140 foreach my $method ( $self->_lag_walked_methods() ) {
141 my $status = $self->walk_status($method) or next;
142 return 0 unless $status->{complete};
143 }
144 return unless scalar keys %$groups or scalar keys %$domains;
145  
146 return 1;
147 }
148  
149 # Private hooks: vendor classes override these.
150 sub _lag_vendor_domains { return {} }
151  
152 sub _lag_vendor_groups { return }
153  
154 sub _lag_vendor_walked { return }
155  
156 sub _agg_ports_walked { return }
157  
158 # Aggregator row index => key of the record that row describes
159 sub _lag_aggregator_map {
160 my $self = shift;
161  
162 my $rows = $self->lag_actor_system() || {};
163 return { map { $_ => $_ } keys %$rows };
164 }
165  
166 sub _lag_walked_methods {
167 my $self = shift;
168  
169 return (
170 qw/lag_actor_system lag_actor_key lag_partner_system lag_partner_key
171 lag_port_attached lag_port_partner_system lag_port_partner_key/,
172 $self->_lag_vendor_walked(),
173 $self->_agg_ports_walked()
174 );
175 }
176  
177 # Hashref: record key => the aggregator row that supplies its identity.
178 # A row indexed by a port attached to a different aggregator is that port's
179 # idle aggregator, so it loses to any other row mapped onto the same key.
180 sub _lag_identity_rows {
181 my ( $self, $map ) = @_;
182  
183 my $rows = $self->lag_actor_system() || {};
184 my $attached = $self->lag_port_attached() || {};
185 my ( %preferred_row, %lowest_row );
186 foreach my $row ( sort { $a <=> $b } keys %$rows ) {
187 my $key = defined $map->{$row} ? $map->{$row} : $row;
188 $lowest_row{$key} = $row unless exists $lowest_row{$key};
189 my $target = $attached->{$row};
190 next if $target and $target != $row;
191 $preferred_row{$key} = $row unless exists $preferred_row{$key};
192 }
193 return { %lowest_row, %preferred_row };
194 }
195  
196 # A member port's own row, when the port is attached to another aggregate
197 # and nothing else describes it, is not an aggregate
198 sub _lag_drop_port_rows {
199 my ( $self, $groups, $map ) = @_;
200  
201 my $attached = $self->lag_port_attached() || {};
202 foreach my $key ( keys %$groups ) {
203 my $record = $groups->{$key};
204 next if scalar keys %{ $record->{members} };
205 next if $record->{mc} or $record->{peer_link};
206 my $target = $attached->{$key} or next;
207 $target = $map->{$target} if defined $map->{$target};
208 delete $groups->{$key} if $target != $key;
209 }
210 return;
211 }
212  
213 sub _lag_partner_or_undef {
214 my $system = shift;
215  
216 return if !defined $system or $system eq $NO_PARTNER_SYSTEM;
217 return $system;
218 }
219  
220 sub _lag_identity_record {
221 my ( $self, $row ) = @_;
222  
223 my $record = _lag_new_record();
224 my %columns = (
225 actor_system => [ lag_actor_system => 'dot3adAggActorSystemID' ],
226 actor_key => [ lag_actor_key => 'dot3adAggActorOperKey' ],
227 partner_system => [ lag_partner_system => 'dot3adAggPartnerSystemID' ],
228 partner_key => [ lag_partner_key => 'dot3adAggPartnerOperKey' ],
229 );
230 foreach my $field ( keys %columns ) {
231 my ( $method, $leaf ) = @{ $columns{$field} };
232 my $value = ( $self->$method() || {} )->{$row};
233 $record->{$field} = $value;
234 $record->{sources}{$field} = $LAG_MIB . $leaf if defined $value;
235 }
236 $self->_lag_settle_partner($record);
237 return $record;
238 }
239  
240 # An all-zero partner system means LACP found no partner
241 sub _lag_settle_partner {
242 my ( $self, $record ) = @_;
243  
244 $record->{partner_system}
245 = _lag_partner_or_undef( $record->{partner_system} );
246 if ( defined $record->{partner_system} ) {
247 $record->{lacp} = 1;
248 return;
249 }
250 $record->{partner_key} = undef;
251 delete $record->{sources}{partner_system};
252 delete $record->{sources}{partner_key};
253 return;
254 }
255  
256 sub _lag_new_record {
257 return {
258 members => {},
259 actor_system => undef,
260 actor_key => undef,
261 partner_system => undef,
262 partner_key => undef,
263 lacp => 0,
264 esi => undef,
265 sources => {},
266 };
267 }
268  
269 sub _lag_agg_ports {
270 my $self = shift;
271  
272 return {} unless $self->can('agg_ports');
273 my $ports = $self->agg_ports();
274 return ref {} eq ref $ports ? $ports : {};
275 }
276  
277 # Members from agg_ports; an aggregate agg_ports names no member of falls
278 # back to the ports that report themselves attached to it
279 sub _lag_add_members {
280 my ( $self, $groups, $map ) = @_;
281  
282 my %from_agg_ports;
283 my $agg_ports = $self->_lag_agg_ports();
284 foreach my $port ( keys %$agg_ports ) {
285 $from_agg_ports{ $agg_ports->{$port} }{$port} = 1;
286 }
287 my %from_attached;
288 my $attached = $self->lag_port_attached() || {};
289 foreach my $port ( keys %$attached ) {
290 next unless $attached->{$port};
291 my $key = defined $map->{ $attached->{$port} }
292 ? $map->{ $attached->{$port} }
293 : $attached->{$port};
294 $from_attached{$key}{$port} = 1;
295 }
296  
297 foreach my $key ( keys %from_agg_ports ) {
298 $self->_lag_place_members( $groups, $key, $from_agg_ports{$key},
299 'agg_ports' );
300 }
301 foreach my $key ( grep { !$from_agg_ports{$_} } keys %from_attached ) {
302 $self->_lag_place_members( $groups, $key, $from_attached{$key},
303 $LAG_MIB . 'dot3adAggPortAttachedAggID' );
304 }
305 return;
306 }
307  
308 sub _lag_place_members {
309 my ( $self, $groups, $key, $ports, $source ) = @_;
310  
311 my $record = $groups->{$key} ||= _lag_new_record();
312 my $systems = $self->lag_port_partner_system() || {};
313 my $keys = $self->lag_port_partner_key() || {};
314 foreach my $port ( keys %$ports ) {
315 my $system = _lag_partner_or_undef( $systems->{$port} );
316 $record->{members}{$port} = {
317 partner_system => $system,
318 partner_key => defined $system ? $keys->{$port} : undef,
319 };
320 }
321 $record->{sources}{members} = $source;
322 return;
323 }
324  
325 1;
326  
327 __END__