Commit 3fe30c60 authored by David Johnson's avatar David Johnson

Add some useful linux libvnode.pm generic functions, esp for macvlans.

parent 3a9765aa
......@@ -26,9 +26,10 @@
package libvnode;
use Exporter;
@ISA = "Exporter";
@EXPORT = qw( makeIfaceMaps makeBridgeMaps
findControlNet existsIface findIface findMac
@EXPORT = qw( makeIfaceMaps makeBridgeMaps makeMacvlanMaps
findControlNet existsIface findIface findMac getIfaceInfo
existsBridge findBridge findBridgeIfaces
existsMacvlanParent findMacvlanParent findMacvlanIfaces
downloadImage getKernelVersion createExtraFS
forwardPort removePortForward lvSize DoIPtables DoIPtablesNoFail
restartDHCP computeStripeSize
......@@ -435,6 +436,7 @@ my %ip2if = ();
my %ip2mask = ();
my %ip2net = ();
my %ip2maskbits = ();
my %if2info = ();
#
# Grab iface, mac, IP info from /sys and /sbin/ip.
......@@ -448,6 +450,7 @@ sub makeIfaceMaps()
%ip2net = ();
%ip2mask = ();
%ip2maskbits = ();
%if2info = ();
my $devdir = '/sys/class/net';
opendir(SD,$devdir)
......@@ -477,6 +480,7 @@ sub makeIfaceMaps()
$mac = lc($mac);
$if2mac{$iface} = $mac;
$mac2if{$mac} = $iface;
$if2info{$iface} = { 'mac' => $mac, 'iface' => $iface };
# also find ip, ugh
my $pip = `ip addr show dev $iface | grep 'inet '`;
......@@ -501,6 +505,11 @@ sub makeIfaceMaps()
$ip2net{$ip} = join('.',@network);
$ip2mask{$ip} = join('.',@netmask);
$ip2maskbits{$ip} = $bits;
$if2info{$iface}->{'ip'} = $ip;
$if2info{$iface}->{'network'} = $ip2net{$ip};
$if2info{$iface}->{'mask'} = $ip2mask{$ip};
$if2info{$iface}->{'maskbits'} = $ip2maskbits{$ip};
}
}
......@@ -562,6 +571,19 @@ sub findIface($) {
return undef;
}
#
# Returns a dict of iface, mac[, ip, network, mask, maskbits], if the
# supplied iface exists. The IPv4 info is only included if it exists.
#
sub getIfaceInfo($) {
my $iface = shift;
return undef
if (!exists($if2info{$iface}));
return $if2info{$iface};
}
sub findMac($) {
my $iface = shift;
......@@ -634,6 +656,63 @@ sub findBridgeIfaces($) {
return undef;
}
my %macvlans = ();
my %if2mv = ();
sub makeMacvlanMaps() {
# clean out anything...
%macvlans = ();
%if2mv = ();
my @lines = `ip link show type macvlan`;
foreach my $line (@lines) {
if ($line =~ /^\d+:\s+([^\@]+)\@([^:]+):/) {
if (!exists($macvlans{$2})) {
$macvlans{$2} = [];
}
push(@{$macvlans{$2}},$1);
}
}
if ($debug > 1) {
print STDERR "makeMacvlanMaps:\n";
print STDERR "macvlans:\n";
print STDERR Dumper(%macvlans) . "\n";
print STDERR "if2mv:\n";
print STDERR Dumper(%if2mv) . "\n";
print STDERR "\n";
}
return 0;
}
sub existsMacvlanParent($) {
my $parent = shift;
return 1
if (exists($macvlans{$parent}));
return 0;
}
sub findMacvlanParent($) {
my $iface = shift;
return $if2mv{$iface}
if (exists($if2mv{$iface}));
return undef;
}
sub findMacvlanIfaces($) {
my $parent = shift;
return @{$macvlans{$parent}}
if (exists($macvlans{$parent}));
return undef;
}
#
# Since (some) vnodes are imageable now, we provide an image fetch
# mechanism. Caller provides an imagepath for frisbee, and a hash of
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment