move Network::get_active_interfaces to ProcFSTools

This avoids a circular dependency between PVE::INotify and
PVE::Network.

Also renamed to get_active_network_interfaces since the
package name now doesn't hint at this anymore.
This commit is contained in:
Wolfgang Bumiller 2016-06-03 11:09:23 +02:00 committed by Dietmar Maurer
parent 19e609fd33
commit f0d1b04fda
3 changed files with 40 additions and 39 deletions

View File

@ -14,6 +14,7 @@ use Fcntl qw(:DEFAULT :flock);
use PVE::SafeSyslog;
use PVE::Exception qw(raise_param_exc);
use PVE::Tools;
use PVE::ProcFSTools;
use Storable qw(dclone);
use Linux::Inotify2;
use base 'Exporter';
@ -785,7 +786,7 @@ my $extract_ovs_option = sub {
sub read_etc_network_interfaces {
my ($filename, $fh) = @_;
my $proc_net_dev = IO::File->new('/proc/net/dev', 'r');
my $active = PVE::Network::get_active_interfaces();
my $active = PVE::ProcFSTools::get_active_network_interfaces();
return __read_etc_network_interfaces($fh, $proc_net_dev, $active);
}

View File

@ -11,12 +11,6 @@ use POSIX qw(ECONNREFUSED);
use Net::IP;
use Socket qw(IPPROTO_IP);
use constant IFF_UP => 1;
use constant IFNAMSIZ => 16;
use constant SIOCGIFFLAGS => 0x8913;
# host network related utility functions
our $ipv4_reverse_mask = [
@ -537,36 +531,4 @@ sub is_ip_in_cidr {
return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP;
}
# struct ifreq { // FOR SIOCGIFFLAGS:
# char ifrn_name[IFNAMSIZ]
# short ifru_flags
# };
my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1';
sub get_active_interfaces {
# Use the interface name list from /proc/net/dev
open my $fh, '<', '/proc/net/dev'
or die "failed to open /proc/net/dev: $!\n";
# And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl:
my $sock;
socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP)
or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP)
or return [];
my $ifaces = [];
while(defined(my $line = <$fh>)) {
next if $line !~ /^\s*([^:\s]+):/;
my $ifname = $1;
my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0);
if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) {
warn "failed to get interface flags for: $ifname\n";
next;
}
my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq);
push @$ifaces, $ifname if ($flags & IFF_UP);
}
close $fh;
close $sock;
return $ifaces;
}
1;

View File

@ -8,6 +8,12 @@ use IO::File;
use PVE::Tools;
use Cwd qw();
use Socket qw(PF_INET PF_INET6 SOCK_DGRAM IPPROTO_IP);
use constant IFF_UP => 1;
use constant IFNAMSIZ => 16;
use constant SIOCGIFFLAGS => 0x8913;
my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK);
my $cpuinfo;
@ -374,4 +380,36 @@ sub upid_wait {
}
}
# struct ifreq { // FOR SIOCGIFFLAGS:
# char ifrn_name[IFNAMSIZ]
# short ifru_flags
# };
my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1';
sub get_active_network_interfaces {
# Use the interface name list from /proc/net/dev
open my $fh, '<', '/proc/net/dev'
or die "failed to open /proc/net/dev: $!\n";
# And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl:
my $sock;
socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP)
or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP)
or return [];
my $ifaces = [];
while(defined(my $line = <$fh>)) {
next if $line !~ /^\s*([^:\s]+):/;
my $ifname = $1;
my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0);
if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) {
warn "failed to get interface flags for: $ifname\n";
next;
}
my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq);
push @$ifaces, $ifname if ($flags & IFF_UP);
}
close $fh;
close $sock;
return $ifaces;
}
1;