2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-20 17:32:15 +00:00

Merge remote-tracking branch 'versatus/fix/ipv6-discovery-neighbour-callback' into feat/ipv6

This commit is contained in:
Daniel Hilst
2026-11-05 10:15:43 -03:00
9 changed files with 461 additions and 85 deletions
+194 -6
View File
@@ -23,6 +23,9 @@ use Sys::Hostname;
use strict;
use warnings "all";
my $socket6support = eval { require Socket6 };
# IO::Socket::INET6 is optional on an older management node. Without it a
# callback to an IPv6 node cannot be opened at all.
our $inet6support = eval { require IO::Socket::INET6; 1; };
my $core_socket_resolver = defined(&Socket::getaddrinfo)
&& defined(&Socket::getnameinfo)
&& defined(&Socket::inet_pton)
@@ -4266,6 +4269,155 @@ sub gen_net_boot_params
return $net_params;
}
#-------------------------------------------------------------------------------
# A link-layer address, as both tools print it: ethernet is six bytes, and
# InfiniBand is twenty.
our $MAC_ADDRESS_PATTERN = qr/[0-9a-fA-F]{1,2}(?::[0-9a-fA-F]{1,2}){5,19}/;
=head3 mac_from_neighbour_table
Description:
The link-layer address a neighbour table holds for one address.
Arguments:
The text of the table, and the address to look up.
Returns:
The address in lower case, or undef when the table holds none.
Comments:
Three tools write three formats, and this sub reads all three:
arp -n 192.0.2.10 ether 52:54:00:aa:bb:cc C eth0
ip neigh show 192.0.2.10 dev eth0 lladdr 52:54:00:aa:bb:cc REACHABLE
ip -6 neigh show 2001:db8::2 dev eth0 lladdr 52:54:00:aa:bb:cc router REACHABLE
The address is compared after both sides are normalized, because
2001:db8::2 and 2001:0db8:0:0:0:0:0:2 are one address written two ways.
The callers used to interpolate the address into a regular expression,
where every dot matched any character.
=cut
#-------------------------------------------------------------------------------
sub mac_from_neighbour_table
{
my $text = shift;
if (defined($text) && $text eq __PACKAGE__)
{
$text = shift;
}
my $address = shift;
return unless defined($text) && defined($address);
my $wanted = _canonical_neighbour_address($address);
return unless defined($wanted);
foreach my $line (split(/\n/, $text))
{
next unless $line =~ /^\s*(\S+)\s+(\S.*)$/;
my ($entry, $rest) = ($1, $2);
my $found = _canonical_neighbour_address($entry);
next unless defined($found) && $found eq $wanted;
# ip(8) names the column; arp(8) puts it third, and writes
# "(incomplete)" in place of the type and the address.
return lc($1) if $rest =~ /\blladdr\s+($MAC_ADDRESS_PATTERN)(?:\s|$)/o;
return lc($1) if $rest =~ /^\S+\s+($MAC_ADDRESS_PATTERN)(?:\s|$)/o;
}
return;
}
#-------------------------------------------------------------------------------
=head3 neighbour_table_text
Description:
Reads the neighbour table that holds one address.
Arguments:
The address to look up.
Returns:
The text of the table, or undef when no tool is available.
Comments:
arp(8) prints the IPv4 table only, so an IPv6 address needs
"ip -6 neigh show". This keeps the IPv4 path on arp(8).
=cut
#-------------------------------------------------------------------------------
sub neighbour_table_text
{
my $address = shift;
if (defined($address) && $address eq __PACKAGE__)
{
$address = shift;
}
return unless defined($address) && length($address);
if (xCAT::NetworkUtils->isIpv6addr($address))
{
my $ipcmd = _neighbour_command('ip');
return unless $ipcmd;
return scalar(`$ipcmd -6 neigh show`);
}
my $arpcmd = _neighbour_command('arp');
return unless $arpcmd;
return scalar(`$arpcmd -n`);
}
#-------------------------------------------------------------------------------
=head3 mac_of_neighbour
Description:
The link-layer address of a neighbour, for either address family.
Arguments:
The address of the neighbour.
Returns:
The address in lower case, or undef.
=cut
#-------------------------------------------------------------------------------
sub mac_of_neighbour
{
my $address = shift;
if (defined($address) && $address eq __PACKAGE__)
{
$address = shift;
}
return mac_from_neighbour_table(neighbour_table_text($address), $address);
}
sub _neighbour_command
{
my $name = shift;
foreach my $dir ('/usr/sbin', '/sbin', '/usr/bin', '/bin')
{
return "$dir/$name" if -x "$dir/$name";
}
return;
}
# One address written two ways is one address. An IPv6 literal is compared
# after it is packed; an IPv4 one has a single spelling already.
sub _canonical_neighbour_address
{
my $address = shift;
return unless defined($address) && length($address);
$address =~ s/\%.*//; # a scoped literal, as in fe80::1%eth0
if (index($address, ':') >= 0)
{
my $packed = _pack_ip_address($address, 6);
return unless defined($packed) && length($packed) == 16;
return unpack('H*', $packed);
}
return unless xCAT::NetworkUtils->isIpaddr($address);
return $address;
}
#--------------------------------------------------------------------------------
=head3 send_tcp_msg
establish a tcp socket to the specified IP address and port, then send the specifid message via the socket
@@ -4283,12 +4435,7 @@ sub send_tcp_msg {
my $destport=shift;
my $msg=shift;
my $sock = new IO::Socket::INET(
PeerAddr => $destip,
PeerPort => $destport,
Timeout => '1',
Proto => 'tcp'
);
my $sock = xCAT::NetworkUtils->open_tcp_socket($destip, $destport, 1);
if ($sock) {
print $sock $msg;
close($sock);
@@ -4298,6 +4445,47 @@ sub send_tcp_msg {
}
}
#--------------------------------------------------------------------------------
=head3 open_tcp_socket
Opens a TCP connection to an address of either family.
Arguments:
$address : the destination address, IPv4 or IPv6
$port : the destination TCP port
$timeout : the connect timeout in seconds, optional
Returns:
the socket, or undef with $! set.
IO::Socket::INET cannot reach an IPv6 address, so a node discovered over
IPv6 never received its restart message and timed out. IO::Socket::INET6
is optional on an older management node, so the IPv4 path is the
fallback.
=cut
#--------------------------------------------------------------------------------
sub open_tcp_socket {
my $address = shift;
if (defined($address) && $address eq __PACKAGE__)
{
$address = shift;
}
my $port = shift;
my $timeout = shift;
return unless defined($address) && length($address) && defined($port);
my %socket_args = (
PeerAddr => $address,
PeerPort => $port,
Proto => 'tcp',
);
$socket_args{Timeout} = $timeout if defined($timeout);
if ($inet6support)
{
my $sock = IO::Socket::INET6->new(%socket_args);
return $sock if $sock;
}
return IO::Socket::INET->new(%socket_args);
}
#-----------------------------------------------------------------------------
=head3 uri_authority
+3 -14
View File
@@ -29,21 +29,10 @@ sub process_request {
}
my $client_ip = $req->{'_xcat_clientip'};
my $arptable;
if (-x "/usr/sbin/arp") {
$arptable = `/usr/sbin/arp -n`;
}
else {
$arptable = `/sbin/arp -n`;
}
my @arpents = split /\n/, $arptable;
# arp(8) prints the IPv4 table only, and the address used to be interpolated
# into a regular expression, where every dot matched any character.
my $mac;
foreach (@arpents) {
if (m/^($client_ip)\s+\S+\s+(\S+)\s/) {
$mac = $2;
last;
}
}
$mac = xCAT::NetworkUtils->mac_of_neighbour($client_ip);
unless ($mac) {
xCAT::MsgUtils->message("S", "xcat.discovery.aaadiscovery: Failed to get MAC address for $client_ip in arp cache.");
$mac = "$req->{mtm}->[0]*$req->{serial}->[0]";
+3 -15
View File
@@ -4492,21 +4492,9 @@ sub process_request {
$invreq{command} = ['rinv'];
my $mac;
my $ip = $request->{'_xcat_clientip'};
my $arptable;
if (-x "/usr/sbin/arp") {
$arptable = `/usr/sbin/arp -n`;
}
else {
$arptable = `/sbin/arp -n`;
}
my @arpents = split /\n/, $arptable;
foreach (@arpents) {
if (m/^($ip)\s+\S+\s+(\S+)\s/) {
$mac = $2;
last;
}
}
# arp(8) prints the IPv4 table only, and the address used to be interpolated
# into a regular expression, where every dot matched any character.
$mac = xCAT::NetworkUtils->mac_of_neighbour($ip);
#Only refresh the the cache when the request permits and no useful answer
if ($macmaptimestamp < (time() - 300)) { #after five minutes, invalidate cache
+3 -15
View File
@@ -727,21 +727,9 @@ sub process_request {
$invreq{command} = ['rinv'];
my $mac;
my $ip = $request->{'_xcat_clientip'};
my $arptable;
if (-x "/usr/sbin/arp") {
$arptable = `/usr/sbin/arp -n`;
}
else {
$arptable = `/sbin/arp -n`;
}
my @arpents = split /\n/, $arptable;
foreach (@arpents) {
if (m/^($ip)\s+\S+\s+(\S+)\s/) {
$mac = $2;
last;
}
}
# arp(8) prints the IPv4 table only, and the address used to be interpolated
# into a regular expression, where every dot matched any character.
$mac = xCAT::NetworkUtils->mac_of_neighbour($ip);
unless ($mac) { return }
#Only refresh the the cache when the request permits and no useful answer
+3 -6
View File
@@ -537,12 +537,9 @@ sub process_request {
}
#now, notify the node to continue life
my $sock = new IO::Socket::INET(
PeerAddr => $clientip,
PeerPort => '3001',
Timeout => '1',
Proto => 'tcp'
);
# IO::Socket::INET cannot reach an IPv6 address, so a node discovered over IPv6
# never received this message and exited on the discovery timeout.
my $sock = xCAT::NetworkUtils->open_tcp_socket($clientip, 3001, 1);
unless ($sock) { xCAT::MsgUtils->message("S", "xcat.discovery.nodediscover: Failed to notify $clientip that it's actually $node. $node has not been discovered: $!"); return; }
print $sock $restartstring;
close($sock);
+3 -14
View File
@@ -1969,20 +1969,9 @@ sub findme {
my $ip = $request->{'_xcat_clientip'};
xCAT::MsgUtils->message('S', "Profield nodes discover: _xcat_clientip is $ip.\n");
my $mac = '';
my $arptable;
if (-x "/usr/sbin/arp") {
$arptable = `/usr/sbin/arp -n`;
}
else {
$arptable = `/sbin/arp -n`;
}
my @arpents = split /\n/, $arptable;
foreach (@arpents) {
if (m/^($ip)\s+\S+\s+(\S+)\s/) {
$mac = $2;
last;
}
}
# arp(8) prints the IPv4 table only, and the address used to be interpolated
# into a regular expression, where every dot matched any character.
$mac = xCAT::NetworkUtils->mac_of_neighbour($ip);
if (!$mac) {
setrsp_errormsg("Profiled nodes discover: Can not get mac address of this node.");
return;
+3 -15
View File
@@ -80,21 +80,9 @@ sub findme {
xCAT::MsgUtils->message("S", "xcat.discovery.seqdiscovery: ($request->{_xcat_clientmac}->[0]) Error: virtual machines is not supported");
return;
}
my $arptable;
if (-x "/usr/sbin/arp") {
$arptable = `/usr/sbin/arp -n`;
}
else {
$arptable = `/sbin/arp -n`;
}
my @arpents = split /\n/, $arptable;
foreach (@arpents) {
if (m/^($ip)\s+\S+\s+(\S+)\s/) {
$mac = $2;
last;
}
}
# arp(8) prints the IPv4 table only, and the address used to be interpolated
# into a regular expression, where every dot matched any character.
$mac = xCAT::NetworkUtils->mac_of_neighbour($ip);
unless ($mac) {
xCAT::MsgUtils->message("S", "xcat.discovery.seqdiscovery: ($request->{_xcat_clientmac}->[0]) Error: Could not find mac of the $ip");
+137
View File
@@ -0,0 +1,137 @@
#!/usr/bin/env perl
# Sequential discovery resolves the address a findme arrived from to a MAC, and looks that
# MAC up in the mac table. Five plugins carried the same arp(8) parse, which reads the IPv4
# table and matched the address as a regular expression. The parse now takes the table text,
# so a table from either tool is a fixture here instead of the test host's own cache.
use strict;
use warnings;
## no critic (Modules::RequireFilenameMatchesPackage, TestingAndDebugging::ProhibitNoWarnings)
use FindBin;
use lib "$FindBin::Bin/../../perl-xCAT";
use Test::More;
BEGIN {
package xCAT::Table;
sub new { return bless {}, shift; }
sub getAllAttribs { return; }
$INC{'xCAT/Table.pm'} = __FILE__;
package xCAT::TableUtils;
sub get_site_attribute { return; }
$INC{'xCAT/TableUtils.pm'} = __FILE__;
}
require xCAT::NetworkUtils;
my $arp_table = <<'TABLE';
Address HWtype HWaddress Flags Mask Iface
192.168.201.10 ether 52:54:00:aa:bb:cc C ens7
192.168.201.11 (incomplete) ens7
192.168.201.120 ether 52:54:00:11:22:33 C ens7
TABLE
my $ip6_table = <<'TABLE';
2001:db8:177::2 dev ens7 lladdr 52:54:00:aa:bb:cc router REACHABLE
2001:db8:177::3 dev ens7 FAILED
fe80::5054:ff:feaa:bbcc dev ens7 lladdr 52:54:00:aa:bb:cc router STALE
2001:db8:177::4 dev ens7 lladdr 52:54:00:de:ad:be STALE
TABLE
my $ip4_table = <<'TABLE';
192.168.201.10 dev ens7 lladdr 52:54:00:aa:bb:cc REACHABLE
192.168.201.11 dev ens7 INCOMPLETE
TABLE
# IPv4, from arp(8): the format the five plugins read before.
is(
xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, '192.168.201.10'),
'52:54:00:aa:bb:cc',
'the MAC of an IPv4 neighbour comes out of the arp table',
);
is(
xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, '192.168.201.11'),
undef,
'an incomplete arp entry has no MAC',
);
# The address used to be interpolated into a regular expression, where a dot matched
# any character, so 192.168.201.1 matched the row of 192.168.201.120 and the wrong
# node was named.
is(
xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, '192.168.201.1'),
undef,
'an address that is a prefix of another row does not match that row',
);
is(
xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, '192.168.201.99'),
undef,
'an address the table does not hold has no MAC',
);
# IPv6, from ip(8): the case that returned nothing at all.
is(
xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, '2001:db8:177::2'),
'52:54:00:aa:bb:cc',
'the MAC of an IPv6 neighbour comes out of the IPv6 neighbour table',
);
is(
xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, '2001:0db8:0177:0000:0000:0000:0000:0002'),
'52:54:00:aa:bb:cc',
'the same address written in full matches the same row',
);
is(
xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, '2001:db8:177::3'),
undef,
'a failed IPv6 entry carries no lladdr and so no MAC',
);
is(
xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, 'fe80::5054:ff:feaa:bbcc%ens7'),
'52:54:00:aa:bb:cc',
'a scoped link-local address matches its unscoped row',
);
is(
xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, '2001:db8:177::9'),
undef,
'an IPv6 address the table does not hold has no MAC',
);
# ip(8) also prints the IPv4 table in that format.
is(
xCAT::NetworkUtils->mac_from_neighbour_table($ip4_table, '192.168.201.10'),
'52:54:00:aa:bb:cc',
'an IPv4 row printed by ip(8) is read as well',
);
is(
xCAT::NetworkUtils->mac_from_neighbour_table($ip4_table, '192.168.201.11'),
undef,
'an incomplete ip(8) entry has no MAC',
);
# An upper-case table is one table.
is(
xCAT::NetworkUtils->mac_from_neighbour_table(
"2001:DB8:177::2 dev ens7 lladdr 52:54:00:AA:BB:CC REACHABLE\n",
'2001:db8:177::2',
),
'52:54:00:aa:bb:cc',
'the MAC is returned in lower case, as the mac table holds it',
);
# A twenty-byte InfiniBand address is a link-layer address too.
is(
xCAT::NetworkUtils->mac_from_neighbour_table(
"192.168.201.10 dev ib0 lladdr 80:00:02:08:fe:80:00:00:00:00:00:00:00:02:c9:03:00:1a:2b:3c REACHABLE\n",
'192.168.201.10',
),
'80:00:02:08:fe:80:00:00:00:00:00:00:00:02:c9:03:00:1a:2b:3c',
'an InfiniBand link-layer address is read whole',
);
is(xCAT::NetworkUtils->mac_from_neighbour_table(undef, '192.168.201.10'), undef,
'no table means no MAC');
is(xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, undef), undef,
'no address means no MAC');
is(xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, 'not-an-address'), undef,
'an address that does not parse means no MAC');
done_testing();
@@ -0,0 +1,112 @@
#!/usr/bin/env perl
# After discovery xcatd opens a TCP connection back to the node and sends "restart".
# nodediscover.pm and send_tcp_msg built that socket with IO::Socket::INET, which cannot
# reach an IPv6 address, so an IPv6 node never got the message and exited on the discovery
# timeout. open_tcp_socket chooses the class. Both classes are replaced here, so nothing
# connects to anything.
use strict;
use warnings;
## no critic (Modules::RequireFilenameMatchesPackage, TestingAndDebugging::ProhibitNoWarnings)
use FindBin;
use lib "$FindBin::Bin/../../perl-xCAT";
use Test::More;
BEGIN {
package xCAT::Table;
sub new { return bless {}, shift; }
sub getAllAttribs { return; }
$INC{'xCAT/Table.pm'} = __FILE__;
package xCAT::TableUtils;
sub get_site_attribute { return; }
$INC{'xCAT/TableUtils.pm'} = __FILE__;
}
require xCAT::NetworkUtils;
my @attempts;
my %answer;
my $written = '';
{
no warnings 'redefine';
foreach my $class (qw(IO::Socket::INET IO::Socket::INET6)) {
no strict 'refs';
*{"${class}::new"} = sub {
my (undef, %args) = @_;
push @attempts, { class => $class, %args };
return undef unless $answer{$class};
# An IO::Socket is a filehandle, and send_tcp_msg prints to it. An
# in-memory handle collects what it writes.
open(my $handle, '>', \$written) or die "Unable to open a scratch handle: $!";
return $handle;
};
$INC{ join('/', split(/::/, $class)) . '.pm' } = __FILE__;
}
}
sub attempt {
@attempts = ();
my $sock = xCAT::NetworkUtils->open_tcp_socket(@_);
return ($sock, [@attempts]);
}
# An IPv6 node: the family-neutral class is asked, and it is the one that can reach it.
local $xCAT::NetworkUtils::inet6support = 1;
%answer = ('IO::Socket::INET6' => 1);
my ($sock, $tried) = attempt('2001:db8:177::2', 3001, 1);
ok($sock, 'a connection to an IPv6 node is opened');
is(scalar(@{$tried}), 1, 'one class is asked for it');
is($tried->[0]{class}, 'IO::Socket::INET6', 'and it is the one that carries AF_INET6');
is($tried->[0]{PeerAddr}, '2001:db8:177::2', 'the node address is passed unbracketed');
is($tried->[0]{PeerPort}, 3001, 'the port is passed');
is($tried->[0]{Proto}, 'tcp', 'the connection is TCP');
is($tried->[0]{Timeout}, 1, 'the connect timeout is passed');
# An IPv4 node keeps working through whichever class answers.
%answer = ('IO::Socket::INET6' => 1);
($sock, $tried) = attempt('192.168.201.10', 3001, 1);
ok($sock, 'a connection to an IPv4 node is opened');
is($tried->[0]{PeerAddr}, '192.168.201.10', 'the IPv4 address is passed');
# A management node without the IPv6 modules keeps the original IPv4 path.
{
local $xCAT::NetworkUtils::inet6support = undef;
%answer = ('IO::Socket::INET' => 1);
($sock, $tried) = attempt('192.168.201.10', 3001, 1);
ok($sock, 'a connection is opened without the IPv6 modules');
is_deeply([ map { $_->{class} } @{$tried} ], ['IO::Socket::INET'],
'only the IPv4 class is asked when the IPv6 modules are absent');
}
# The IPv4 class is the fallback when the family-neutral attempt fails.
%answer = ('IO::Socket::INET' => 1);
($sock, $tried) = attempt('192.168.201.10', 3001, 1);
ok($sock, 'the IPv4 class answers after the family-neutral one does not');
is_deeply([ map { $_->{class} } @{$tried} ],
[ 'IO::Socket::INET6', 'IO::Socket::INET' ],
'the family-neutral class is asked first and the IPv4 one second');
# Nothing answers.
%answer = ();
($sock, $tried) = attempt('2001:db8:177::2', 3001, 1);
ok(!$sock, 'a node that cannot be reached returns no socket');
is(xCAT::NetworkUtils->open_tcp_socket(undef, 3001), undef, 'no address opens nothing');
is(xCAT::NetworkUtils->open_tcp_socket('192.168.201.10', undef), undef, 'no port opens nothing');
# send_tcp_msg is the other caller, and it writes the message it was given.
%answer = ('IO::Socket::INET6' => 1);
@attempts = ();
$written = '';
is(xCAT::NetworkUtils->send_tcp_msg('2001:db8:177::2', 3001, 'restart'), 0,
'send_tcp_msg reports success for an IPv6 node');
is($attempts[0]{class}, 'IO::Socket::INET6', 'and it reached that node through the IPv6 class');
is($written, 'restart', 'the message is written to the socket');
%answer = ();
is(xCAT::NetworkUtils->send_tcp_msg('2001:db8:177::2', 3001, 'restart'), 1,
'send_tcp_msg reports failure when nothing answers');
done_testing();