blib/lib/Linux/Ethtool/Settings.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 168 CC 16 SCAR 22.4
CC
16
Cov
0%
duplex
30.0
speed
20.0
autoneg
12.0
CRAP 9.4
1 =head1 NAME
2  
3 Linux::Ethtool::Settings - Manipulate link-level network interface settings
4  
5 =head1 SYNOPSIS
6  
7 use Linux::Ethtool::Settings;
8
9 my $settings = Linux::Ethtool::Settings->new("eth0") or die($!);
10
11 # Show the current/supported modes.
12
13 my $current_speed = $settings->speed();
14 my $current_duplex = $settings->duplex() ? "full" : "half";
15
16 print "Current speed: $current_speed Mbps, $current_duplex duplex\n";
17
18 my @supported_modes = $settings->supported_modes();
19
20 print "Supported speeds: @supported_modes\n";
21
22 # Force the interface to 10Mbps, half duplex
23
24 $settings->autoneg(0);
25 $settings->speed(10);
26 $settings->duplex(0);
27
28 $settings->apply() or die($!);
29
30 # Turn auto-negotiation back on. Setting advertising to supported is required
31 # if the object was constructed when auto-negotiation was disabled or Linux
32 # will try to auto-negotiate with no speeds...
33
34 $settings->autoneg(1);
35 $settings->advertising($settings->supported);
36
37 $settings->apply() or die($!);
38  
39 =head1 DESCRIPTION
40  
41 This module provides a wrapper around the C<ethtool_cmd> structure and
42 associated ioctls, mainly used for configuring link-level settings such as
43 speed/duplex and auto-negotiation.
44  
45 All the constants in this module may be imported individually or by using the
46 C<all> import tag.
47  
48 =head1 METHODS
49  
50 =cut
51  
52 package Linux::Ethtool::Settings;
53  
54 use strict;
55 use warnings;
56  
57 our $VERSION = "0.11";
58  
59 require XSLoader;
60 XSLoader::load("Linux::Ethtool::Settings");
61  
62 use Exporter qw(import);
63  
64 our @EXPORT_OK = qw(
65 SUPPORTED_10baseT_Half
66 SUPPORTED_10baseT_Full
67 SUPPORTED_100baseT_Half
68 SUPPORTED_100baseT_Full
69 SUPPORTED_1000baseT_Half
70 SUPPORTED_1000baseT_Full
71 SUPPORTED_Autoneg
72 SUPPORTED_TP
73 SUPPORTED_AUI
74 SUPPORTED_MII
75 SUPPORTED_FIBRE
76 SUPPORTED_BNC
77 SUPPORTED_10000baseT_Full
78 SUPPORTED_Pause
79 SUPPORTED_Asym_Pause
80 SUPPORTED_2500baseX_Full
81 SUPPORTED_Backplane
82 SUPPORTED_1000baseKX_Full
83 SUPPORTED_10000baseKX4_Full
84 SUPPORTED_10000baseKR_Full
85 SUPPORTED_10000baseR_FEC
86
87 ADVERTISED_10baseT_Half
88 ADVERTISED_10baseT_Full
89 ADVERTISED_100baseT_Half
90 ADVERTISED_100baseT_Full
91 ADVERTISED_1000baseT_Half
92 ADVERTISED_1000baseT_Full
93 ADVERTISED_Autoneg
94 ADVERTISED_TP
95 ADVERTISED_AUI
96 ADVERTISED_MII
97 ADVERTISED_FIBRE
98 ADVERTISED_BNC
99 ADVERTISED_10000baseT_Full
100 ADVERTISED_Pause
101 ADVERTISED_Asym_Pause
102 ADVERTISED_2500baseX_Full
103 ADVERTISED_Backplane
104 ADVERTISED_1000baseKX_Full
105 ADVERTISED_10000baseKX4_Full
106 ADVERTISED_10000baseKR_Full
107 ADVERTISED_10000baseR_FEC
108
109 PORT_TP
110 PORT_AUI
111 PORT_MII
112 PORT_FIBRE
113 PORT_BNC
114 PORT_OTHER
115
116 XCVR_INTERNAL
117 XCVR_EXTERNAL
118 XCVR_DUMMY1
119 XCVR_DUMMY2
120 XCVR_DUMMY3
121 );
122  
123 our %EXPORT_TAGS = (
124 all => [ @EXPORT_OK ],
125 );
126  
127 use Linux::Ethtool::Constants;
128  
129 # Duplicate the constants here so we can export them.
130  
131 use constant {
132 SUPPORTED_10baseT_Half => Linux::Ethtool::Constants::SUPPORTED_10baseT_Half,
133 SUPPORTED_10baseT_Full => Linux::Ethtool::Constants::SUPPORTED_10baseT_Full,
134 SUPPORTED_100baseT_Half => Linux::Ethtool::Constants::SUPPORTED_100baseT_Half,
135 SUPPORTED_100baseT_Full => Linux::Ethtool::Constants::SUPPORTED_100baseT_Full,
136 SUPPORTED_1000baseT_Half => Linux::Ethtool::Constants::SUPPORTED_1000baseT_Half,
137 SUPPORTED_1000baseT_Full => Linux::Ethtool::Constants::SUPPORTED_1000baseT_Full,
138 SUPPORTED_Autoneg => Linux::Ethtool::Constants::SUPPORTED_Autoneg,
139 SUPPORTED_TP => Linux::Ethtool::Constants::SUPPORTED_TP,
140 SUPPORTED_AUI => Linux::Ethtool::Constants::SUPPORTED_AUI,
141 SUPPORTED_MII => Linux::Ethtool::Constants::SUPPORTED_MII,
142 SUPPORTED_FIBRE => Linux::Ethtool::Constants::SUPPORTED_FIBRE,
143 SUPPORTED_BNC => Linux::Ethtool::Constants::SUPPORTED_BNC,
144 SUPPORTED_10000baseT_Full => Linux::Ethtool::Constants::SUPPORTED_10000baseT_Full,
145 SUPPORTED_Pause => Linux::Ethtool::Constants::SUPPORTED_Pause,
146 SUPPORTED_Asym_Pause => Linux::Ethtool::Constants::SUPPORTED_Asym_Pause,
147 SUPPORTED_2500baseX_Full => Linux::Ethtool::Constants::SUPPORTED_2500baseX_Full,
148 SUPPORTED_Backplane => Linux::Ethtool::Constants::SUPPORTED_Backplane,
149 SUPPORTED_1000baseKX_Full => Linux::Ethtool::Constants::SUPPORTED_1000baseKX_Full,
150 SUPPORTED_10000baseKX4_Full => Linux::Ethtool::Constants::SUPPORTED_10000baseKX4_Full,
151 SUPPORTED_10000baseKR_Full => Linux::Ethtool::Constants::SUPPORTED_10000baseKR_Full,
152 SUPPORTED_10000baseR_FEC => Linux::Ethtool::Constants::SUPPORTED_10000baseR_FEC,
153  
154 ADVERTISED_10baseT_Half => Linux::Ethtool::Constants::ADVERTISED_10baseT_Half,
155 ADVERTISED_10baseT_Full => Linux::Ethtool::Constants::ADVERTISED_10baseT_Full,
156 ADVERTISED_100baseT_Half => Linux::Ethtool::Constants::ADVERTISED_100baseT_Half,
157 ADVERTISED_100baseT_Full => Linux::Ethtool::Constants::ADVERTISED_100baseT_Full,
158 ADVERTISED_1000baseT_Half => Linux::Ethtool::Constants::ADVERTISED_1000baseT_Half,
159 ADVERTISED_1000baseT_Full => Linux::Ethtool::Constants::ADVERTISED_1000baseT_Full,
160 ADVERTISED_Autoneg => Linux::Ethtool::Constants::ADVERTISED_Autoneg,
161 ADVERTISED_TP => Linux::Ethtool::Constants::ADVERTISED_TP,
162 ADVERTISED_AUI => Linux::Ethtool::Constants::ADVERTISED_AUI,
163 ADVERTISED_MII => Linux::Ethtool::Constants::ADVERTISED_MII,
164 ADVERTISED_FIBRE => Linux::Ethtool::Constants::ADVERTISED_FIBRE,
165 ADVERTISED_BNC => Linux::Ethtool::Constants::ADVERTISED_BNC,
166 ADVERTISED_10000baseT_Full => Linux::Ethtool::Constants::ADVERTISED_10000baseT_Full,
167 ADVERTISED_Pause => Linux::Ethtool::Constants::ADVERTISED_Pause,
168 ADVERTISED_Asym_Pause => Linux::Ethtool::Constants::ADVERTISED_Asym_Pause,
169 ADVERTISED_2500baseX_Full => Linux::Ethtool::Constants::ADVERTISED_2500baseX_Full,
170 ADVERTISED_Backplane => Linux::Ethtool::Constants::ADVERTISED_Backplane,
171 ADVERTISED_1000baseKX_Full => Linux::Ethtool::Constants::ADVERTISED_1000baseKX_Full,
172 ADVERTISED_10000baseKX4_Full => Linux::Ethtool::Constants::ADVERTISED_10000baseKX4_Full,
173 ADVERTISED_10000baseKR_Full => Linux::Ethtool::Constants::ADVERTISED_10000baseKR_Full,
174 ADVERTISED_10000baseR_FEC => Linux::Ethtool::Constants::ADVERTISED_10000baseR_FEC,
175
176 PORT_TP => Linux::Ethtool::Constants::PORT_TP,
177 PORT_AUI => Linux::Ethtool::Constants::PORT_AUI,
178 PORT_MII => Linux::Ethtool::Constants::PORT_MII,
179 PORT_FIBRE => Linux::Ethtool::Constants::PORT_FIBRE,
180 PORT_BNC => Linux::Ethtool::Constants::PORT_BNC,
181 PORT_OTHER => Linux::Ethtool::Constants::PORT_OTHER,
182
183 XCVR_INTERNAL => Linux::Ethtool::Constants::XCVR_INTERNAL,
184 XCVR_EXTERNAL => Linux::Ethtool::Constants::XCVR_EXTERNAL,
185 XCVR_DUMMY1 => Linux::Ethtool::Constants::XCVR_DUMMY1,
186 XCVR_DUMMY2 => Linux::Ethtool::Constants::XCVR_DUMMY2,
187 XCVR_DUMMY3 => Linux::Ethtool::Constants::XCVR_DUMMY3,
188 };
189  
190 use Carp;
191  
192 =head2 new($dev)
193  
194 Construct a new instance using the settings of the named interface.
195  
196 Returns an object instance on success, undef on failure.
197  
198 =cut
199  
200 sub new
201 {
202 my ($class, $dev) = @_;
203
204 my $self = bless({ dev => $dev }, $class);
205
206 if(_ethtool_gset($self, $dev))
207 {
208 return $self;
209 }
210 else{
211 return undef;
212 }
213 }
214  
215 =head2 apply()
216  
217 Apply any changed settings to the interface.
218  
219 Returns true on success, false on failure.
220  
221 =cut
222  
223 sub apply
224 {
225 my ($self) = @_;
226
227 return _ethtool_sset($self->{dev}, $self->{advertising}, $self->{speed}, $self->{duplex}, $self->{port}, $self->{transceiver}, $self->{autoneg});
228 }
229  
230 =head2 supported()
231  
232 Returns the features supported by this interface as a bit field. The following
233 constants are useful here:
234  
235 SUPPORTED_TP
236 SUPPORTED_AUI
237 SUPPORTED_MII
238 SUPPORTED_FIBRE
239 SUPPORTED_BNC
240
241 SUPPORTED_Autoneg
242
243 SUPPORTED_10baseT_Half
244 SUPPORTED_10baseT_Full
245 SUPPORTED_100baseT_Half
246 SUPPORTED_100baseT_Full
247 SUPPORTED_1000baseT_Half
248 SUPPORTED_1000baseT_Full
249 SUPPORTED_2500baseX_Full
250 SUPPORTED_1000baseKX_Full
251 SUPPORTED_10000baseT_Full
252 SUPPORTED_10000baseKX4_Full
253 SUPPORTED_10000baseKR_Full
254 SUPPORTED_10000baseR_FEC
255
256 SUPPORTED_Pause
257 SUPPORTED_Asym_Pause
258 SUPPORTED_Backplane
259  
260 =cut
261  
262 sub supported
263 {
264 my ($self) = @_;
265
266 $self->{supported};
267 }
268  
269 =head2 supported_modes()
270  
271 Returns a sorted list of supported speed/duplex settings suitable for showing to
272 the user.
273  
274 =cut
275  
276 my @feature_mode_bits = (
277 SUPPORTED_10baseT_Half,
278 SUPPORTED_10baseT_Full,
279 SUPPORTED_100baseT_Half,
280 SUPPORTED_100baseT_Full,
281 SUPPORTED_1000baseT_Half,
282 SUPPORTED_1000baseT_Full,
283 SUPPORTED_1000baseKX_Full,
284 SUPPORTED_2500baseX_Full,
285 SUPPORTED_10000baseT_Full,
286 SUPPORTED_10000baseKX4_Full,
287 SUPPORTED_10000baseKR_Full,
288 SUPPORTED_10000baseR_FEC,
289 );
290  
291 my %feature_mode_names = (
292 &SUPPORTED_10baseT_Half => "10baseT/Half",
293 &SUPPORTED_10baseT_Full => "10baseT/Full",
294 &SUPPORTED_100baseT_Half => "100baseT/Half",
295 &SUPPORTED_100baseT_Full => "100baseT/Full",
296 &SUPPORTED_1000baseT_Half => "1000baseT/Half",
297 &SUPPORTED_1000baseT_Full => "1000baseT/Full",
298 &SUPPORTED_1000baseKX_Full => "1000baseKX/Full",
299 &SUPPORTED_2500baseX_Full => "2500baseX/Full",
300 &SUPPORTED_10000baseT_Full => "10000baseT/Full",
301 &SUPPORTED_10000baseKX4_Full => "10000baseKX4/Full",
302 &SUPPORTED_10000baseKR_Full => "10000baseKR/Full",
303 &SUPPORTED_10000baseR_FEC => "10000baseR/FEC",
304 );
305  
306 sub _feature_mode
307 {
308 my ($bits) = @_;
309
310 return map { $feature_mode_names{$_} } grep { $bits & $_ } @feature_mode_bits;
311 }
312  
313 sub supported_modes
314 {
315 my ($self) = @_;
316
317 return _feature_mode($self->supported);
318 }
319  
320 =head2 supported_ports()
321  
322 Returns a list of ports on this interface suitable for showing to
323 the user.
324  
325 B<NOTE>: This is unrelated to cards that have multiple interfaces on them, this
326 is for (old fashioned) cards that have multiple ports (AUI, BNC, etc) for the
327 same interface,only one of which may be used at a time. See the C<port> method
328 for more information.
329  
330 =cut
331  
332 my %feature_port_names = (
333 &SUPPORTED_TP => "Twisted Pair",
334 &SUPPORTED_AUI => "AUI",
335 &SUPPORTED_MII => "MII",
336 &SUPPORTED_FIBRE => "Fibre",
337 &SUPPORTED_BNC => "BNC",
338 );
339  
340 sub supported_ports
341 {
342 my ($self) = @_;
343
344 return map { $feature_port_names{$_} } grep { $self->supported & $_ } keys(%feature_port_names);
345 }
346  
347 =head2 advertising([ $advertising ])
348  
349 Gets or sets the modes being advertised for auto-negotiation as a bit field.
350 Returns the current/new value.
351  
352 The following constants are useful here:
353  
354 ADVERTISED_10baseT_Half
355 ADVERTISED_10baseT_Full
356 ADVERTISED_100baseT_Half
357 ADVERTISED_100baseT_Full
358 ADVERTISED_1000baseT_Half
359 ADVERTISED_1000baseT_Full
360 ADVERTISED_2500baseX_Full
361 ADVERTISED_1000baseKX_Full
362 ADVERTISED_10000baseT_Full
363 ADVERTISED_10000baseKX4_Full
364 ADVERTISED_10000baseKR_Full
365 ADVERTISED_10000baseR_FEC
366 ADVERTISED_Pause
367 ADVERTISED_Asym_Pause
368  
369 =cut
370  
371 sub advertising
372 {
373 my ($self, $advertising) = @_;
374
375 if(defined($advertising))
376 {
377 $self->{advertising} = $advertising;
378 }
379
380 return $self->{advertising};
381 }
382  
383 =head2 advertising_modes()
384  
385 Returns a sorted list of advertised speed/duplex settings suitable for showing
386 to the user.
387  
388 =cut
389  
390 sub advertising_modes
391 {
392 my ($self) = @_;
393
394 return _feature_mode($self->advertising);
395 }
396  
397 =head2 speed([ $speed ])
398  
399 Get or set the link speed in Mbps. Returns the current/new value.
400  
401 Setting this field will only have an effect if auto-negotiation is also disabled
402 before calling apply.
403  
404 =cut
405  
406 sub speed
407 {
408 my ($self, $speed) = @_;
409
410 if(defined($speed))
411 {
412 if($speed !~ m/^\d+$/)
413 {
414 croak("Invalid integer given for speed '$speed'");
415 }
416
417 if(!(grep { $speed == $_ } (10, 100, 1000, 2500, 10000)))
418 {
419 carp("Speed '$speed' is not one of (10, 100, 1000, 2500, 10000)");
420 }
421
422 $self->{speed} = int($speed);
423 }
424
425 return $self->{speed};
426 }
427  
428 =head2 duplex([ $duplex ])
429  
430 Get or set the duplex of the link, full is true, half is false and unknown is
431 undefined. Returns the current/new value.
432  
433 Setting this field will only have an effect if auto-negotiation is also disabled
434 before calling apply.
435  
436 =cut
437  
438 sub duplex
439 {
440 my ($self, $duplex) = @_;
441
442 if(defined($duplex))
443 {
444 $self->{duplex} = ($duplex ? Linux::Ethtool::Constants::DUPLEX_FULL : Linux::Ethtool::Constants::DUPLEX_HALF);
445 }
446
447 if($self->{duplex} == Linux::Ethtool::Constants::DUPLEX_HALF)
448 {
449 return 0;
450 }
451 elsif($self->{duplex} == Linux::Ethtool::Constants::DUPLEX_FULL)
452 {
453 return 1;
454 }
455 else{
456 return undef;
457 }
458 }
459  
460 =head2 autoneg([ $autoneg ])
461  
462 Get or set the auto-negotiation flag. Returns the current/new value.
463  
464 =cut
465  
466 sub autoneg
467 {
468 my ($self, $autoneg) = @_;
469
470 if(defined($autoneg))
471 {
472 $self->{autoneg} = ($autoneg ? 1 : 0);
473 }
474
475 return $self->{autoneg};
476 }
477  
478 =head2 port([ $port ])
479  
480 Get or set the port type. Returns the current/new value.
481  
482 The following constants are useful here:
483  
484 PORT_TP (Twisted Pair)
485 PORT_AUI
486 PORT_MII (Media Independent Interface)
487 PORT_FIBRE
488 PORT_BNC
489 PORT_OTHER
490  
491 =cut
492  
493 sub port
494 {
495 my ($self, $port) = @_;
496
497 if(defined($port))
498 {
499 $self->{port} = int($port);
500 }
501
502 return $self->{port};
503 }
504  
505 =head2 port_name()
506  
507 Return a user-friendly name for the port type.
508  
509 =cut
510  
511 my %port_names = (
512 &PORT_TP => "Twisted Pair",
513 &PORT_AUI => "AUI",
514 &PORT_MII => "MII",
515 &PORT_FIBRE => "Fibre",
516 &PORT_BNC => "BNC",
517 &PORT_OTHER => "Other",
518 );
519  
520 sub port_name
521 {
522 my ($self) = @_;
523
524 if(defined($port_names{ $self->port }))
525 {
526 return $port_names{ $self->port };
527 }
528 else{
529 return "Unknown";
530 }
531 }
532  
533 =head2 transceiver([ $transceiver ])
534  
535 Get or set the transceiver type. Returns the current/new value.
536  
537 The following constants are useful here:
538  
539 XCVR_INTERNAL
540 XCVR_EXTERNAL
541 XCVR_DUMMY1
542 XCVR_DUMMY2
543 XCVR_DUMMY3
544  
545 =cut
546  
547 sub transceiver
548 {
549 my ($self, $transceiver) = @_;
550
551 if(defined($transceiver))
552 {
553 $self->{transceiver} = int($transceiver);
554 }
555
556 return $self->{transceiver};
557 }
558  
559 =head2 transceiver_name()
560  
561 Return a user-friendly name for the transceiver type.
562  
563 =cut
564  
565 my %transceiver_names = (
566 &XCVR_INTERNAL => "Internal",
567 &XCVR_EXTERNAL => "External",
568 );
569  
570 sub transceiver_name
571 {
572 my ($self) = @_;
573
574 if(defined($transceiver_names{ $self->transceiver }))
575 {
576 return $transceiver_names{ $self->transceiver };
577 }
578 else{
579 return "Unknown";
580 }
581 }
582  
583 =head2 lp_advertising()
584  
585 Gets the modes being advertised for auto-negotiation by the other end of the
586 link as a bit field.
587  
588 The following constants are useful here:
589  
590 ADVERTISED_10baseT_Half
591 ADVERTISED_10baseT_Full
592 ADVERTISED_100baseT_Half
593 ADVERTISED_100baseT_Full
594 ADVERTISED_1000baseT_Half
595 ADVERTISED_1000baseT_Full
596 ADVERTISED_2500baseX_Full
597 ADVERTISED_1000baseKX_Full
598 ADVERTISED_10000baseT_Full
599 ADVERTISED_10000baseKX4_Full
600 ADVERTISED_10000baseKR_Full
601 ADVERTISED_10000baseR_FEC
602 ADVERTISED_Pause
603 ADVERTISED_Asym_Pause
604  
605 =cut
606  
607 sub lp_advertising
608 {
609 my ($self) = @_;
610
611 return $self->{lp_advertising};
612 }
613  
614 =head2 lp_advertising_modes()
615  
616 Returns a sorted list of speed/duplex settings advertised by the other end of
617 the link suitable for showing to the user.
618  
619 =cut
620  
621 sub lp_advertising_modes
622 {
623 my ($self) = @_;
624
625 return _feature_mode($self->lp_advertising);
626 }
627  
628 =head1 SEE ALSO
629  
630 L<Linux::Ethtool>, L<Linux::Ethtool::WOL>
631  
632 =cut
633  
634 1;