diff options
author | Alex Harpin <development@landsofshadow.co.uk> | 2015-11-08 09:15:49 +0000 |
---|---|---|
committer | Alex Harpin <development@landsofshadow.co.uk> | 2015-11-08 09:15:49 +0000 |
commit | c651357018d5807ea6926a58b2432d0b58685ad8 (patch) | |
tree | df8bf6dedad92bd531a89e5ba854cf95ff4cefed /scripts/vyatta-interfaces.pl | |
parent | cacbf435f534b3807d775846face9451c1c5f974 (diff) | |
download | vyatta-cfg-system-c651357018d5807ea6926a58b2432d0b58685ad8.tar.gz vyatta-cfg-system-c651357018d5807ea6926a58b2432d0b58685ad8.zip |
vyatta-cfg-system: formatting changes for style consistency
Update scripts/vyatta-interfaces.pl to maintain style consistency
Diffstat (limited to 'scripts/vyatta-interfaces.pl')
-rwxr-xr-x | scripts/vyatta-interfaces.pl | 342 |
1 files changed, 172 insertions, 170 deletions
diff --git a/scripts/vyatta-interfaces.pl b/scripts/vyatta-interfaces.pl index 621596a0..4003489b 100755 --- a/scripts/vyatta-interfaces.pl +++ b/scripts/vyatta-interfaces.pl @@ -1,12 +1,12 @@ #!/usr/bin/perl # # Module: vyatta-interfaces.pl -# +# # **** License **** # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. -# +# # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -18,23 +18,23 @@ # You can also obtain it by writing to the Free Software Foundation, # Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, # MA 02110-1301, USA. -# +# # This code was originally developed by Vyatta, Inc. # Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc. # All Rights Reserved. -# +# # Author: Stig Thormodsrud # Date: November 2007 # Description: Script to assign addresses to interfaces. -# +# # **** End License **** # use lib "/opt/vyatta/share/perl5/"; use Vyatta::Config; use Vyatta::Misc qw(generate_dhclient_intf_files - getInterfaces getIP get_sysfs_value - is_address_enabled is_dhcp_enabled is_ip_v4_or_v6); + getInterfaces getIP get_sysfs_value + is_address_enabled is_dhcp_enabled is_ip_v4_or_v6); use Vyatta::File qw(touch); use Vyatta::Interface; @@ -68,21 +68,22 @@ EOF exit 1; } -GetOptions("valid-addr-commit=s{,}" => \@addr_commit, - "dev=s" => \$dev, - "valid-mac=s" => \$mac, - "set-mac=s" => \$mac_update, - "dhcp=s" => \$dhcp_command, - "check=s" => \$check_name, - "show=s" => \$show_names, - "skip=s" => sub { $skip_interface{$_[1]} = 1 }, - "vif=s" => \$vif_name, - "warn" => \$warn_name, - "isup" => \$check_up, - "speed-duplex=s{2}" => \@speed_duplex, - "check-speed=s{2}" => \@check_speed, - "allowed-speed" => \$allowed_speed, - "offload-settings=s{2}" => \@offload_option, +GetOptions( + "valid-addr-commit=s{,}" => \@addr_commit, + "dev=s" => \$dev, + "valid-mac=s" => \$mac, + "set-mac=s" => \$mac_update, + "dhcp=s" => \$dhcp_command, + "check=s" => \$check_name, + "show=s" => \$show_names, + "skip=s" => sub {$skip_interface{$_[1]} = 1}, + "vif=s" => \$vif_name, + "warn" => \$warn_name, + "isup" => \$check_up, + "speed-duplex=s{2}" => \@speed_duplex, + "check-speed=s{2}" => \@check_speed, + "allowed-speed" => \$allowed_speed, + "offload-settings=s{2}" => \@offload_option, ) or usage(); is_valid_addr_commit($dev, @addr_commit) if (@addr_commit); @@ -101,7 +102,7 @@ exit 0; sub is_ip_configured { my ($intf, $ip) = @_; - my $found = grep { $_ eq $ip } Vyatta::Misc::getIP($intf); + my $found = grep {$_ eq $ip} Vyatta::Misc::getIP($intf); return ($found > 0); } @@ -112,9 +113,9 @@ sub is_ipv4 { sub is_up { my $name = shift; my $intf = new Vyatta::Interface($name); - + die "Unknown interface type for $name" unless $intf; - + exit 0 if ($intf->up()); exit 1; } @@ -156,19 +157,19 @@ sub get_mtu { sub dhcp_update_config { my ($conf_file, $intf) = @_; - + my $output = dhcp_conf_header(); my $hostname = get_hostname(); $output .= "interface \"$intf\" {\n"; if (defined($hostname)) { - $output .= "\tsend host-name \"$hostname\";\n"; + $output .= "\tsend host-name \"$hostname\";\n"; } $output .= "\trequest subnet-mask, broadcast-address, routers, domain-name-servers"; my $domainname = is_domain_name_set(); if (!defined($domainname)) { - $output .= ", domain-name"; - } + $output .= ", domain-name"; + } my $mtu = get_mtu($intf); $output .= ", interface-mtu" unless $mtu; @@ -194,20 +195,20 @@ sub is_intf_disabled { sub run_dhclient { my ($intf, $op_mode) = @_; - my ($intf_config_file, $intf_process_id_file, $intf_leases_file) - = generate_dhclient_intf_files($intf); + my ($intf_config_file, $intf_process_id_file, $intf_leases_file)= generate_dhclient_intf_files($intf); # perform config mode actions if not called from op-mode if (!defined $op_mode) { - dhcp_update_config($intf_config_file, $intf); - return if is_intf_disabled($intf); + dhcp_update_config($intf_config_file, $intf); + return if is_intf_disabled($intf); } my $cmd = "$dhcp_daemon -pf $intf_process_id_file -x $intf 2> /dev/null; rm -f $intf_process_id_file 2> /dev/null;"; $cmd .= "$dhcp_daemon -q -nw -cf $intf_config_file -pf $intf_process_id_file -lf $intf_leases_file $intf 2> /dev/null &"; + # adding & at the end to make the process into a daemon immediately - system ($cmd) == 0 - or warn "start $dhcp_daemon failed: $?\n"; + system($cmd) == 0 + or warn "start $dhcp_daemon failed: $?\n"; } sub stop_dhclient { @@ -215,15 +216,14 @@ sub stop_dhclient { # perform config mode actions if not called from op-mode if (!defined $op_mode) { - return if is_intf_disabled($intf); + return if is_intf_disabled($intf); } - my ($intf_config_file, $intf_process_id_file, $intf_leases_file) - = generate_dhclient_intf_files($intf); + my ($intf_config_file, $intf_process_id_file, $intf_leases_file)= generate_dhclient_intf_files($intf); my $release_cmd = "$dhcp_daemon -q -cf $intf_config_file -pf $intf_process_id_file -lf $intf_leases_file -r $intf 2> /dev/null;"; $release_cmd .= "rm -f $intf_process_id_file 2> /dev/null"; - system ($release_cmd) == 0 - or warn "stop $dhcp_daemon failed: $?\n"; + system($release_cmd) == 0 + or warn "stop $dhcp_daemon failed: $?\n"; } sub update_mac { @@ -237,36 +237,37 @@ sub update_mac { # try the direct approach if (system("ip link set $name address $mac") == 0) { - exit 0; + exit 0; } elsif ($intf->up()) { - # some hardware can not change MAC address if up - system "ip link set $name down" - and die "Could not set $name down\n"; - system "ip link set $name address $mac" - and die "Could not set $name address\n"; - system "ip link set $name up" - and die "Could not set $name up\n"; + + # some hardware can not change MAC address if up + system "ip link set $name down" + and die "Could not set $name down\n"; + system "ip link set $name address $mac" + and die "Could not set $name address\n"; + system "ip link set $name up" + and die "Could not set $name up\n"; } else { - die "Could not set mac address for $name\n"; + die "Could not set mac address for $name\n"; } exit 0; } sub is_vrrp_mac { - my @octets = @_; - return 1 if (hex($octets[0]) == 0 && - hex($octets[1]) == 0 && - hex($octets[2]) == 94 && - hex($octets[3]) == 0 && - hex($octets[4]) == 1); - return 0; + my @octets = @_; + return 1 if ( hex($octets[0]) == 0 + && hex($octets[1]) == 0 + && hex($octets[2]) == 94 + && hex($octets[3]) == 0 + && hex($octets[4]) == 1); + return 0; } sub is_valid_mac { my ($mac, $intf) = @_; my @octets = split /:/, $mac; - + ($#octets == 5) or die "Error: wrong number of octets: $#octets\n"; ((hex($octets[0]) & 1) == 0) or die "Error: $mac is a multicast address\n"; @@ -274,14 +275,14 @@ sub is_valid_mac { is_vrrp_mac(@octets) and die "Error: $mac is a vrrp mac address\n"; my $sum = 0; - $sum += hex( $_) foreach @octets; - ( $sum != 0 ) or die "Error: zero is not a valid address\n"; + $sum += hex($_) foreach @octets; + ($sum != 0) or die "Error: zero is not a valid address\n"; exit 0; } # Validate the set of address values configured on an interface at commit -# Check that full set of address address values are consistent. +# Check that full set of address address values are consistent. # 1. Interface may not be part of bridge or bonding group # 2. Can not have both DHCP and a static IPv4 address. sub is_valid_addr_commit { @@ -294,35 +295,34 @@ sub is_valid_addr_commit { my $bridge = $config->returnValue("bridge-group bridge"); die "Can't configure address on interface that is port of bridge.\n" - if (defined($bridge)); + if (defined($bridge)); my $bond = $config->returnValue("bond-group"); die "Can't configure address on interface that is slaved to bonding interface.\n" - if (defined($bond)); + if (defined($bond)); my $addrmap = Vyatta::Interface::get_cfg_addresses(); my ($dhcp, $static_v4); foreach my $addr (@addrs) { - next if ($addr eq 'dhcpv6'); + next if ($addr eq 'dhcpv6'); - if ($addr eq 'dhcp') { - $dhcp = 1; - next; - } + if ($addr eq 'dhcp') { + $dhcp = 1; + next; + } - my $intfs = $addrmap->{$addr}; - if ($intfs && scalar(@{$intfs}) > 1) { - die "Duplicate address $addr used on interfaces: ", - join(',', @${intfs}), "\n"; - } + my $intfs = $addrmap->{$addr}; + if ($intfs && scalar(@{$intfs}) > 1) { + die "Duplicate address $addr used on interfaces: ",join(',', @${intfs}), "\n"; + } - $static_v4 = 1 - if ( is_ipv4($addr) ); + $static_v4 = 1 + if (is_ipv4($addr)); } die "Can't configure static IPv4 address and DHCP on the same interface.\n" - if ($static_v4 && $dhcp); + if ($static_v4 && $dhcp); exit 0; } @@ -333,45 +333,45 @@ sub is_intf_down { my $intf = new Vyatta::Interface($name); return 1 unless $intf; - return ! $intf->up(); + return !$intf->up(); } sub dhcp { my ($request, $intf) = @_; die "$intf is not using DHCP to get an IP address\n" - unless ($request eq 'start' || is_dhcp_enabled($intf)); - + unless ($request eq 'start' || is_dhcp_enabled($intf)); + die "$intf is disabled.\n" - if ($request ne 'stop' && is_intf_down($intf)); + if ($request ne 'stop' && is_intf_down($intf)); my $tmp_dhclient_dir = '/var/run/vyatta/dhclient/'; my $release_file = $tmp_dhclient_dir . 'dhclient_release_' . $intf; if ($request eq "release") { - die "IP address for $intf has already been released.\n" - if (-e $release_file); + die "IP address for $intf has already been released.\n" + if (-e $release_file); - print "Releasing DHCP lease on $intf ...\n"; - stop_dhclient($intf, 'op_mode'); - mkdir ($tmp_dhclient_dir) if (! -d $tmp_dhclient_dir ); - touch ($release_file); + print "Releasing DHCP lease on $intf ...\n"; + stop_dhclient($intf, 'op_mode'); + mkdir($tmp_dhclient_dir) if (!-d $tmp_dhclient_dir); + touch($release_file); } elsif ($request eq "renew") { print "Renewing DHCP lease on $intf ...\n"; run_dhclient($intf, 'op_mode'); - unlink ($release_file); + unlink($release_file); } elsif ($request eq "start") { - print "Starting DHCP client on $intf ...\n"; - touch("/var/lib/dhcp3/$intf"); - run_dhclient($intf); + print "Starting DHCP client on $intf ...\n"; + touch("/var/lib/dhcp3/$intf"); + run_dhclient($intf); } elsif ($request eq "stop") { - print "Stopping DHCP client on $intf ...\n"; - stop_dhclient($intf); - unlink("/var/lib/dhcp3/dhclient_$intf\_lease"); - unlink("/var/lib/dhcp3/$intf"); - unlink("/var/run/vyatta/dhclient/dhclient_release_$intf"); + print "Stopping DHCP client on $intf ...\n"; + stop_dhclient($intf); + unlink("/var/lib/dhcp3/dhclient_$intf\_lease"); + unlink("/var/lib/dhcp3/$intf"); + unlink("/var/run/vyatta/dhclient/dhclient_release_$intf"); unlink("/var/lib/dhcp3/dhclient_$intf\.conf"); } else { - die "Unknown DHCP request: $request\n"; + die "Unknown DHCP request: $request\n"; } exit 0; @@ -383,18 +383,17 @@ sub is_valid_name { my $intf = new Vyatta::Interface($name); die "$name does not match any known interface name type\n" - unless $intf; + unless $intf; my $vif = $intf->vif(); - die "$name is the name of VIF interface\n" , - "Need to use \"interface ",$intf->physicalDevice()," vif $vif\"\n" - if $vif; + die "$name is the name of VIF interface\n","Need to use \"interface ",$intf->physicalDevice()," vif $vif\"\n" + if $vif; die "$name is a ", $intf->type(), " interface not an $type interface\n" - if ($type ne 'all' and $intf->type() ne $type); + if ($type ne 'all' and $intf->type() ne $type); die "$type interface $name does not exist on system\n" - unless grep { $name eq $_ } getInterfaces(); + unless grep {$name eq $_} getInterfaces(); exit 0; } @@ -404,7 +403,7 @@ sub exists_name { die "Missing --dev argument\n" unless $name; warn "interface $name does not exist on system\n" - unless grep { $name eq $_ } getInterfaces(); + unless grep {$name eq $_} getInterfaces(); exit 0; } @@ -415,20 +414,21 @@ sub show_interfaces { my @match; foreach my $name (@interfaces) { - my $intf = new Vyatta::Interface($name); - next unless $intf; # skip unknown types - next if $skip_interface{$name}; - next unless ($type eq 'all' || $type eq $intf->type()); + my $intf = new Vyatta::Interface($name); + next unless $intf; # skip unknown types + next if $skip_interface{$name}; + next unless ($type eq 'all' || $type eq $intf->type()); if ($intf->vrid()){ push @match, $name; # add all vrrp interfaces } elsif ($vif_name) { - next unless $intf->vif(); - push @match, $intf->vif() - if ($vif_name eq $intf->physicalDevice()); - } else { - push @match, $name - unless $intf->vif() and $type ne 'all'; - } + next unless $intf->vif(); + push @match, $intf->vif() + if ($vif_name eq $intf->physicalDevice()); + } else { + push @match, $name + unless $intf->vif() + and $type ne 'all'; + } } print join(' ', @match), "\n"; } @@ -437,8 +437,8 @@ sub show_interfaces { sub get_ethtool { my $dev = shift; - open( my $ethtool, '-|', "$ETHTOOL $dev 2>&1" ) - or die "ethtool failed: $!\n"; + open(my $ethtool, '-|', "$ETHTOOL $dev 2>&1") + or die "ethtool failed: $!\n"; # ethtool produces: # @@ -452,16 +452,16 @@ sub get_ethtool { my ($rate, $duplex); my $autoneg = 0; while (<$ethtool>) { - chomp; - return if ( /^Cannot get device settings/ ); - - if ( /^\s+Speed: (\d+)Mb/ ) { - $rate = $1; - } elsif ( /^\s+Duplex:\s(.*)$/ ) { - $duplex = lc $1; - } elsif ( /^\s+Auto-negotiation: on/ ) { - $autoneg = 1; - } + chomp; + return if (/^Cannot get device settings/); + + if (/^\s+Speed: (\d+)Mb/) { + $rate = $1; + } elsif (/^\s+Duplex:\s(.*)$/) { + $duplex = lc $1; + } elsif (/^\s+Auto-negotiation: on/) { + $autoneg = 1; + } } close $ethtool; return ($autoneg, $rate, $duplex); @@ -475,18 +475,20 @@ sub set_speed_duplex { my ($autoneg, $ospeed, $oduplex) = get_ethtool($intf); if (defined($autoneg) && $autoneg == 1) { - # Device is already in autonegotiation mode - return if ($nspeed eq 'auto'); + + # Device is already in autonegotiation mode + return if ($nspeed eq 'auto'); } elsif (defined($ospeed) && defined($oduplex)) { - # Device has explicit speed/duplex but they already match - return if (($nspeed eq $ospeed) && ($nduplex eq $oduplex)); + + # Device has explicit speed/duplex but they already match + return if (($nspeed eq $ospeed) && ($nduplex eq $oduplex)); } my $cmd = "$ETHTOOL -s $intf"; if ($nspeed eq 'auto') { - $cmd .= " autoneg on"; + $cmd .= " autoneg on"; } else { - $cmd .= " speed $nspeed duplex $nduplex autoneg off"; + $cmd .= " speed $nspeed duplex $nduplex autoneg off"; } exec $cmd; @@ -497,11 +499,10 @@ sub set_speed_duplex { sub is_supported_speed { my ($dev, $speed, $duplex) = @_; - my $wanted = sprintf("%dbase%s/%s", $speed, - ($speed == 2500) ? 'X' : 'T', ucfirst($duplex)); + my $wanted = sprintf("%dbase%s/%s", $speed,($speed == 2500) ? 'X' : 'T', ucfirst($duplex)); - open( my $ethtool, '-|', "$ETHTOOL $dev 2>/dev/null" ) - or die "ethtool failed: $!\n"; + open(my $ethtool, '-|', "$ETHTOOL $dev 2>/dev/null") + or die "ethtool failed: $!\n"; # ethtool output: # @@ -513,15 +514,15 @@ sub is_supported_speed { # Supports auto-negotiation: Yes my $mode; while (<$ethtool>) { - chomp; - if ($mode) { - last unless /^\t /; - } else { - next unless /^\tSupported link modes: /; - $mode = 1; - } - - return 1 if /$wanted/; + chomp; + if ($mode) { + last unless /^\t /; + } else { + next unless /^\tSupported link modes: /; + $mode = 1; + } + + return 1 if /$wanted/; } close $ethtool; @@ -536,13 +537,13 @@ sub check_speed_duplex { exit 0 if ($speed eq 'auto' && $duplex eq 'auto'); die "If speed is hardcoded, duplex must also be hardcoded\n" - if ($duplex eq 'auto'); + if ($duplex eq 'auto'); die "If duplex is hardcoded, speed must also be hardcoded\n" - if ($speed eq 'auto'); + if ($speed eq 'auto'); die "Speed $speed, duplex $duplex not supported on $dev\n" - unless is_supported_speed($dev, $speed, $duplex); + unless is_supported_speed($dev, $speed, $duplex); exit 0; } @@ -551,24 +552,24 @@ sub check_speed_duplex { sub allowed_speed { my ($dev) = @_; - open( my $ethtool, '-|', "$ETHTOOL $dev 2>/dev/null" ) - or die "ethtool failed: $!\n"; + open(my $ethtool, '-|', "$ETHTOOL $dev 2>/dev/null") + or die "ethtool failed: $!\n"; my %speeds; my $first = 1; while (<$ethtool>) { - chomp; - - if ($first) { - next unless s/\tSupported link modes:\s//; - $first = 0; - } else { - last unless /^\t /; - } - - foreach my $val (split / /) { - $speeds{$1} = 1 if $val =~ /(\d+)base/; - } + chomp; + + if ($first) { + next unless s/\tSupported link modes:\s//; + $first = 0; + } else { + last unless /^\t /; + } + + foreach my $val (split / /) { + $speeds{$1} = 1 if $val =~ /(\d+)base/; + } } close $ethtool; @@ -579,7 +580,7 @@ sub get_offload_setting { my ($dev, $option) = @_; my ($val); - open( my $ethtool, '-|', "$ETHTOOL -k $dev 2>&1" ) or die "ethtool failed: $!\n"; + open(my $ethtool, '-|', "$ETHTOOL -k $dev 2>&1") or die "ethtool failed: $!\n"; while (<$ethtool>) { next if ($_ !~ m/$option:/); chomp; @@ -587,20 +588,21 @@ sub get_offload_setting { } close $ethtool; return ($val); - + } sub set_offload_setting { my ($intf, $option, $nvalue) = @_; die "Missing --dev argument\n" unless $intf; - + my $ovalue = get_offload_setting($intf, $option); - my %ethtool_opts = ( 'generic-receive-offload' => 'gro', - 'generic-segmentation-offload' => 'gso', - 'tcp-segmentation-offload' => 'tso', - 'udp-fragmentation-offload' => 'ufo', - ); + my %ethtool_opts = ( + 'generic-receive-offload' => 'gro', + 'generic-segmentation-offload' => 'gso', + 'tcp-segmentation-offload' => 'tso', + 'udp-fragmentation-offload' => 'ufo', + ); if (defined($nvalue) && $nvalue ne $ovalue) { my $cmd = "$ETHTOOL -K $intf $ethtool_opts{$option} $nvalue"; @@ -608,7 +610,7 @@ sub set_offload_setting { system($cmd); if ($? >> 8) { die "exec of $ETHTOOL failed: '$cmd'"; - } + } } } |