Info/MAU.pm untested

filter: Statementstmt 0.0% 0 / 201 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 405 CC 41 SCAR 39.6
CC
41
Cov
0%
mau_i_duplex_admin
132.0
mau_i_duplex_admin_old
56.0
mau_i_duplex
42.0
CRAP 52.4
1 # SNMP::Info::MAU - Medium Attachment Unit - RFC 2668
2 #
3 # Copyright (c) 2008 Max Baker changes from version 0.8 and beyond.
4 #
5 # Copyright (c) 2002,2003 Regents of the University of California
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # * Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of the University of California, Santa Cruz nor the
17 # names of its contributors may be used to endorse or promote products
18 # derived from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31  
32 package SNMP::Info::MAU;
33  
34 use strict;
35 use warnings;
36 use Exporter;
37 use SNMP::Info;
38  
39 @SNMP::Info::MAU::ISA = qw/SNMP::Info Exporter/;
40 @SNMP::Info::MAU::EXPORT_OK = qw//;
41  
42 our ($VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE);
43  
44 $VERSION = '3.985003';
45  
46 %MIBS = ( 'MAU-MIB' => 'mauMod', 'IANA-MAU-MIB' => 'dot3MauType' );
47  
48 %GLOBALS = ();
49  
50 %FUNCS = (
51  
52 # Interface MAU Table
53 'mau_index' => 'ifMauIfIndex',
54 'mau_link' => 'ifMauType',
55 'mau_status' => 'ifMauStatus',
56 'mau_up' => 'ifMauMediaAvailable',
57 'mau_type' => 'ifMauTypeList',
58 'mau_type_admin' => 'ifMauDefaultType',
59  
60 # Interface Auto-Negotiation Table
61 'mau_auto' => 'ifMauAutoNegSupported',
62 'mau_autostat' => 'ifMauAutoNegAdminStatus',
63 'mau_autosent' => 'ifMauAutoNegCapAdvertised',
64 'mau_autorec' => 'ifMauAutoNegCapReceived',
65 );
66  
67 %MUNGE = (
68  
69 # Inherit all the built in munging
70 %SNMP::Info::MUNGE,
71  
72 # Add ones for our class
73 'mau_type' => \&munge_int2bin,
74 'mau_autosent' => \&munge_int2bin,
75 'mau_autorec' => \&munge_int2bin,
76 );
77  
78 sub munge_int2bin {
79 my $int = shift;
80 return unless defined $int;
81 return unpack( "B32", pack( "N", $int ) );
82 }
83  
84 # munge_int2bin gives 32 bits, most significant first, so 2**N is at 31 - N
85 sub _isfullduplex {
86 my $mau = shift;
87 my $mautype = shift;
88  
89 my @full_types = qw/11 13 16 18 20/;
90 foreach my $type (@full_types) {
91 return 1 if ( substr( $mautype, 31 - $type, 1 ) eq '1' );
92 }
93 return 0;
94 }
95  
96 sub _ishalfduplex {
97 my $mau = shift;
98 my $mautype = shift;
99  
100 my @half_types = qw/10 12 15 17 19/;
101 foreach my $type (@half_types) {
102 return 1 if ( substr( $mautype, 31 - $type, 1 ) eq '1' );
103 }
104 return 0;
105 }
106  
107 my %_mau_i_speed_map = (
108 '10' => '10 Mbps',
109 '100' => '100 Mbps',
110 '1000' => '1.0 Gbps',
111 '10Gig' => '10 Gbps',
112 );
113  
114 sub mau_i_speed_admin {
115 my $mau = shift;
116  
117 my $mau_index = $mau->mau_index();
118 my $mau_type_admin = $mau->mau_type_admin();
119  
120 my %i_speed_admin;
121 foreach my $mau_port ( keys %$mau_type_admin ) {
122 my $iid = $mau_index->{$mau_port};
123 next unless defined $iid;
124  
125 my $type_adminoid = $mau_type_admin->{$mau_port};
126 my $type_admin = &SNMP::translateObj($type_adminoid);
127 next unless defined $type_admin;
128  
129 if ( $type_adminoid eq '.0.0' ) {
130 $i_speed_admin{$iid} = 'auto';
131 }
132 elsif ($type_admin =~ /^dot3MauType(.*)Base/
133 && $_mau_i_speed_map{$1} )
134 {
135 $i_speed_admin{$iid} = $_mau_i_speed_map{$1};
136 }
137 }
138 return \%i_speed_admin;
139 }
140  
141 sub mau_i_duplex {
142 my $mau = shift;
143  
144 my $mau_index = $mau->mau_index();
145 my $mau_link = $mau->mau_link();
146  
147 my %i_duplex;
148 foreach my $mau_port ( keys %$mau_link ) {
149 my $iid = $mau_index->{$mau_port};
150 next unless defined $iid;
151  
152 my $linkoid = $mau_link->{$mau_port};
153 my $link = &SNMP::translateObj($linkoid);
154 next unless defined $link;
155  
156 my $duplex = undef;
157  
158 if ( $link =~ /fd$/i ) {
159 $duplex = 'full';
160 }
161 elsif ( $link =~ /hd$/i ) {
162 $duplex = 'half';
163 }
164  
165 $i_duplex{$iid} = $duplex if defined $duplex;
166 }
167 return \%i_duplex;
168 }
169  
170 sub mau_i_duplex_admin {
171 my $mau = shift;
172 my $partial = shift;
173  
174 my $mau_index = $mau->mau_index() || {};
175  
176 if ($partial) {
177 my %rev_mau_index = reverse %$mau_index;
178 $partial = $rev_mau_index{$partial};
179 }
180  
181 my $mau_autostat = $mau->mau_autostat($partial) || {};
182 my $mau_type_admin = $mau->mau_type_admin($partial) || {};
183  
184 # Older HP4000's don't implement ifMauDefaultType, but we can
185 # figure out from ifMauAutoNegCapAdvertised what we'd like.
186 if ( !%$mau_type_admin ) {
187 return {} unless %$mau_index;
188 return mau_i_duplex_admin_old( $mau, $mau_index, $mau_autostat,
189 $partial );
190 }
191  
192 my %i_duplex_admin;
193 foreach my $mau_port ( keys %$mau_type_admin ) {
194 my $iid = $mau_index->{$mau_port};
195 next unless defined $iid;
196  
197 my $autostat = $mau_autostat->{$mau_port};
198 if ( defined $autostat and $autostat =~ /enabled/i ) {
199 $i_duplex_admin{$iid} = 'auto';
200 next;
201 }
202  
203 my $type_adminoid = $mau_type_admin->{$mau_port};
204 my $type_admin = &SNMP::translateObj($type_adminoid);
205 next unless defined $type_admin;
206  
207 my $duplex = undef;
208  
209 if ( $type_admin =~ /fd$/i ) {
210 $duplex = 'full';
211 }
212 elsif ( $type_admin =~ /hd$/i ) {
213 $duplex = 'half';
214 }
215 elsif ( $type_admin eq 'zeroDotZero' ) {
216 $duplex = 'auto';
217 }
218  
219 $i_duplex_admin{$iid} = $duplex if defined $duplex;
220 }
221 return \%i_duplex_admin;
222 }
223  
224 sub mau_i_duplex_admin_old {
225 my $mau = shift;
226 my $mau_index = shift || $mau->mau_index() || {};
227 my $mau_autostat = shift || $mau->mau_autostat() || {};
228 my $partial = shift;
229  
230 my $mau_autosent = $mau->mau_autosent($partial) || {};
231  
232 my %i_duplex_admin;
233 foreach my $mau_idx ( keys %$mau_index ) {
234 my $iid = $mau_index->{$mau_idx};
235 next unless defined $iid;
236  
237 my $autostat = $mau_autostat->{$mau_idx};
238  
239 # HP25xx has this value
240 if ( defined $autostat and $autostat =~ /enabled/i ) {
241 $i_duplex_admin{$iid} = 'auto';
242 next;
243 }
244  
245 my $type = $mau_autosent->{$mau_idx};
246  
247 next unless defined $type;
248  
249 if ( $type == 0 ) {
250 $i_duplex_admin{$iid} = 'none';
251 next;
252 }
253  
254 my $full = $mau->_isfullduplex($type);
255 my $half = $mau->_ishalfduplex($type);
256  
257 if ( $full && !$half ) {
258 $i_duplex_admin{$iid} = 'full';
259 }
260 elsif ($half) {
261 $i_duplex_admin{$iid} = 'half';
262 }
263 }
264  
265 return \%i_duplex_admin;
266 }
267  
268 sub mau_set_i_speed_admin {
269 my $mau = shift;
270 my $speed = shift;
271 my $iid = shift;
272  
273 my $rv;
274  
275 $speed = lc($speed);
276 if ( !( $speed =~ /^(10|100|1000|auto)$/i and $iid =~ /\d+/ ) ) {
277 return;
278 }
279  
280 # map a speed value to an integer the switch understands based on duplex
281 my %speeds;
282  
283 # 10 = dot3MauType10BaseTHD, 15 = dot3MauType100BaseTXHD
284 # 29 = dot3MauType1000BaseTHD from IANA-MAU-MIB
285 %{ $speeds{'HD'} } = qw/10 10 100 15 1000 29/; # half duplex settings
286 # 11 = dot3MauType10BaseTFD, 16 = dot3MauType100BaseTXFD
287 # 30 = dot3MauType1000BaseTFD from IANA-MAU-MIB
288 %{ $speeds{'FD'} } = qw/10 11 100 16 1000 30/; # full duplex settings
289  
290 my $myhash = $mau->mau_autostat;
291 my $key = $iid . '.1';
292 my $i_autoneg = $myhash->{$key};
293  
294 my $myduplex;
295  
296 my $i_mau_def_type
297 = &SNMP::translateObj( $mau->mau_type_admin($key)->{ $key } );
298  
299 if ( $i_mau_def_type =~ /^dot3MauType.*Base.*(..)$/
300 && ( $1 eq "HD" or $1 eq "FD" ) )
301 {
302 $myduplex = $1;
303 }
304 else {
305  
306 # this is not a valid speed known, assuming auto
307 $myduplex = "auto";
308 }
309  
310 if ( $speed eq "auto" && $i_autoneg eq "enabled" ) {
311 return (1);
312 }
313 elsif ( $speed eq "auto" ) {
314 $rv = $mau->set_mau_autostat( 'enabled', $key );
315 return ($rv);
316 }
317 else {
318 if ( $i_autoneg eq "enabled" ) {
319 $mau->set_mau_autostat( 'disabled', $key );
320 }
321 $rv
322 = $mau->set_mau_type_admin(
323 '.1.3.6.1.2.1.26.4.' . $speeds{$myduplex}{$speed},
324 $key );
325  
326 return ($rv);
327 }
328 }
329  
330 sub mau_set_i_duplex_admin {
331 my $mau = shift;
332 my $duplex = shift;
333 my $iid = shift;
334  
335 my $rv;
336  
337 $duplex = lc($duplex);
338  
339 if ( !( $duplex =~ /^(full|half|auto)$/i and $iid =~ /\d+/ ) ) {
340 return;
341 }
342  
343 # map a textual duplex setting to an integer value the switch will understand
344 my %duplexes;
345 %{ $duplexes{'10'} } = qw/full 11 half 10/;
346 %{ $duplexes{'100'} } = qw/full 16 half 15/;
347 %{ $duplexes{'1000'} } = qw/full 30 half 29/;
348  
349 # current port values:
350 my $myhash = $mau->mau_autostat;
351 my $key = $iid . '.1';
352 my $i_autoneg = $myhash->{$key};
353  
354 my $i_speed
355 = SNMP::translateObj( $mau->mau_type_admin($key)->{ $key } );
356  
357 if ( $i_speed =~ /^dot3MauType(.*)Base/ && $_mau_i_speed_map{$1} ) {
358 $i_speed = $1;
359 }
360 else {
361  
362 # this is not a valid speed setting, assuming auto
363 $duplex = "auto";
364 }
365  
366 if ( $duplex eq "auto" && $i_autoneg eq "enabled" ) {
367 return (1);
368 }
369 elsif ( $duplex eq "auto" ) {
370 $rv = $mau->set_mau_autostat( 'enabled', $key );
371 return ($rv);
372 }
373 else {
374  
375 # Can't always do it here, if not...
376 if ( $i_autoneg eq "enabled"
377 && defined( $duplexes{$i_speed}{$duplex} ) )
378 {
379 $mau->set_mau_autostat( 'disabled', $key );
380 }
381 $rv
382 = $mau->set_mau_type_admin(
383 '.1.3.6.1.2.1.26.4.' . $duplexes{$i_speed}{$duplex},
384 $key );
385 return ($rv);
386 }
387 }
388  
389 #
390 # mau_set_i_speed_duplex_admin() accepts the following values for speed/duplex
391 #
392  
393 # auto/auto (special case)
394 # 10/half
395 # 10/full
396 # 100/half
397 # 100/full
398 # 1000/half
399 # 1000/full
400  
401 sub mau_set_i_speed_duplex_admin {
402 my $mau = shift;
403 my $speed = shift;
404 my $duplex = shift;
405 my $iid = shift;
406  
407 my $rv;
408  
409 $speed = lc($speed);
410 $duplex = lc($duplex);
411  
412 if ( ( $speed !~ m/auto|10|100|1000/io )
413 or ( $duplex !~ m/full|half|auto/io )
414 or ( $iid !~ /\d+/ ) )
415 {
416 return ("bad arguments");
417 }
418  
419 # map input speed and duplex parameters to 'mau_type_admin' settings
420 # From IANA-MAU-MIB
421 # 11 = dot3MauType10BaseTFD, 10 = dot3MauType10BaseTHD,
422 # 16 = dot3MauType100BaseTXFD, 15 = dot3MauType100BaseTXHD
423 # 30 = dot3MauType1000BaseTFD, 29 = dot3MauType1000BaseTHD
424 my %params;
425 %{ $params{'10'} } = qw/full 11 half 10/;
426 %{ $params{'100'} } = qw/full 16 half 15/;
427 %{ $params{'1000'} } = qw/full 30 half 29/;
428  
429 # if given "auto/auto", set 'mau_autostat' to "enable" and exit
430  
431 if ( ( $speed eq "auto" ) or ( $duplex eq "auto" ) ) {
432 $rv = $mau->set_mau_autostat( 'enabled', $iid . '.1' );
433 return ($rv);
434 }
435  
436 $rv
437 = $mau->set_mau_type_admin(
438 '.1.3.6.1.2.1.26.4.' . $params{$speed}{$duplex},
439 $iid . '.1' );
440 $rv = $mau->set_mau_autostat( 'disabled', $iid . '.1' );
441 return ($rv);
442 }
443  
444 1;
445 __END__