diff --git a/perl-xCAT/xCAT/DiscoveryUtils.pm b/perl-xCAT/xCAT/DiscoveryUtils.pm index d7aa7067dd..a14f3bab95 100644 --- a/perl-xCAT/xCAT/DiscoveryUtils.pm +++ b/perl-xCAT/xCAT/DiscoveryUtils.pm @@ -8,6 +8,8 @@ use XML::Simple; $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; use xCAT::MsgUtils; +use xCAT::NetworkUtils; +use Socket qw(AF_INET6 inet_aton inet_ntoa inet_pton inet_ntop); sub is_findme_request { my ($request) = @_; @@ -249,4 +251,120 @@ sub vpd_attribs_from_findme return { uuid => lc($uuid) }; } +#-------------------------------------------------------------------------------- + +=head3 discovery_address_usable + + Can discovery record a node whose address is this? + + Arguments: + the address a node resolved to + Returns: + 1 when the address names a node, 0 when it does not + +=cut + +#-------------------------------------------------------------------------------- +sub discovery_address_usable +{ + my $address = shift; + $address = shift if defined($address) && !ref($address) && $address eq __PACKAGE__; + return 0 unless defined($address) && length($address); + + # A scoped link-local names an interface, not a node on the provisioning network. + return 0 if index( $address, '%' ) >= 0; + return 1 if xCAT::NetworkUtils->isIpaddr($address); + return 1 if xCAT::NetworkUtils->isIpv6addr($address); + return 0; +} + +#-------------------------------------------------------------------------------- + +=head3 nic_network_key + + The network a discovered NIC is on, as a string two NICs on one network share. + + Arguments: + the address of the NIC, and the prefix length + Returns: + the network address, or undef when the value is no address + +=cut + +#-------------------------------------------------------------------------------- +sub nic_network_key +{ + my $address = shift; + $address = shift if defined($address) && !ref($address) && $address eq __PACKAGE__; + my ($netbits) = @_; + + return undef unless defined($address) && length($address); + $netbits = 0 unless defined($netbits) && $netbits =~ /\A\d+\z/; + + if ( xCAT::NetworkUtils->isIpaddr($address) ) { + + # The arithmetic the caller used before, kept so an IPv4 key does not move. + my $ipn = unpack( "N", inet_aton($address) ); + my $mask = $netbits ? ( 2**$netbits - 1 << ( 32 - $netbits ) ) : 0; + return inet_ntoa( pack( "N", $ipn & $mask ) ); + } + + return undef if index( $address, '%' ) >= 0; + return undef unless xCAT::NetworkUtils->isIpv6addr($address); + + my $packed = inet_pton( AF_INET6, $address ); + return undef unless defined($packed) && length($packed) == 16; + + # Mask bits, not bytes: a /60 keeps half of the fourth group. + my @byte = unpack( "C16", $packed ); + foreach my $i ( 0 .. 15 ) { + my $bits = $netbits - $i * 8; + if ( $bits <= 0 ) { $byte[$i] = 0; } + elsif ( $bits < 8 ) { $byte[$i] &= ( 0xFF << ( 8 - $bits ) ) & 0xFF; } + } + return inet_ntop( AF_INET6, pack( "C16", @byte ) ); +} + +#-------------------------------------------------------------------------------- + +=head3 hosttag_for_ipv6_nic + + The hostname tag a discovered IPv6 NIC takes. + + gethosttag reads an A record and my_if_netmap, which prints IPv4 routes only, so the + decision for an IPv6 NIC is made from the addresses: a NIC on the network of the node is + the node, and that is what marks the node reachable on the provisioning network. + + Arguments: + the node, its address, the interface name, the address of the NIC, the prefix length, + and the names already taken + Returns: + the node, an interface-qualified name, or undef when the NIC is on another network + +=cut + +#-------------------------------------------------------------------------------- +sub hosttag_for_ipv6_nic +{ + my $node = shift; + $node = shift if defined($node) && !ref($node) && $node eq __PACKAGE__; + my ( $nodeaddr, $ifname, $nicaddr, $netbits, $usednames ) = @_; + + return undef unless defined($node) && length($node); + return undef unless defined($nicaddr) && length($nicaddr); + + # The IPv4 path owns an IPv4 NIC, and its hostname rules are not reproduced here. + return undef if xCAT::NetworkUtils->isIpaddr($nicaddr); + return undef unless discovery_address_usable($nodeaddr); + return undef if xCAT::NetworkUtils->isIpaddr($nodeaddr); + + my $nicnet = nic_network_key( $nicaddr, $netbits ); + my $nodenet = nic_network_key( $nodeaddr, $netbits ); + return undef unless defined($nicnet) && defined($nodenet) && $nicnet eq $nodenet; + + return $node unless $usednames && $usednames->{$node}; + return "$node-$ifname" if defined($ifname) && length($ifname); + return undef; +} + 1; diff --git a/xCAT-server/lib/xcat/plugins/nodediscover.pm b/xCAT-server/lib/xcat/plugins/nodediscover.pm index 6812d56b4f..70b627c1a0 100644 --- a/xCAT-server/lib/xcat/plugins/nodediscover.pm +++ b/xCAT-server/lib/xcat/plugins/nodediscover.pm @@ -287,7 +287,10 @@ sub process_request { my $macstring = ""; if (defined($request->{mac})) { - if (!inet_aton($node)) { + # inet_aton answers for IPv4 only, so a node whose only address is IPv6 failed here and + # update_discovery_data, at the end of this routine, was never reached. + my $nodeaddr = xCAT::NetworkUtils->getipaddr($node); + if (!xCAT::DiscoveryUtils::discovery_address_usable($nodeaddr)) { xCAT::MsgUtils->message("S", "xcat.discovery.nodediscover: Can not resolve IP for the matching node:$node. Make sure \"makehosts\" and \"makedns\" have been run for $node."); $request->{error} = [0]; $request->{error_msg} = ["Can not resolve IP for the matching node:$node."]; @@ -334,19 +337,30 @@ sub process_request { my $currmac = lc($ifinfo[2]); if ($ifinfo[3]) { (my $ip, my $netbits) = split /\//, $ifinfo[3]; - if ($ip =~ /\d+\.\d+\.\d+\.\d+/) { - my $ipn = unpack("N", inet_aton($ip)); - my $mask = 2**$netbits - 1 << (32 - $netbits); - my $netn = inet_ntoa(pack("N", $ipn & $mask)); - my $hosttag = gethosttag($node, $netn, @ifinfo[1], \%usednames); - unless ($hosttag) { - my $nettagname = $usednames_for_net{$netn}; - # For nics not in the install network, don't deal with them if not an available hostname get - # In case another nic in install network get a hosttag other than nodename, need to compare the IP address they can convert to - if ($nettagname and (inet_aton($nettagname) eq inet_aton($node))) { - $hosttag = "$node-$ifinfo[1]"; - push @hostnames_to_update, $hosttag; + # The address of a nic used to be matched with a dotted-quad pattern, so a nic + # reported as 2001:db8:188::31/64 skipped this whole branch, $localnic stayed 0 + # and the node was reported as not on the deploy network. + my $netn = xCAT::DiscoveryUtils::nic_network_key($ip, $netbits); + if (defined($netn)) { + my $hosttag; + if (xCAT::NetworkUtils->isIpaddr($ip)) { + $hosttag = gethosttag($node, $netn, @ifinfo[1], \%usednames); + unless ($hosttag) { + my $nettagname = $usednames_for_net{$netn}; + # For nics not in the install network, don't deal with them if not an available hostname get + # In case another nic in install network get a hosttag other than nodename, need to compare the IP address they can convert to + if ($nettagname and (inet_aton($nettagname) eq inet_aton($node))) { + $hosttag = "$node-$ifinfo[1]"; + push @hostnames_to_update, $hosttag; + } } + } else { + # gethosttag reads an A record and my_if_netmap, which is netstat -rn and + # prints IPv4 routes only, so an IPv6 nic is matched on the addresses. + $hosttag = xCAT::DiscoveryUtils::hosttag_for_ipv6_nic( + $node, $nodeaddr, $ifinfo[1], $ip, $netbits, \%usednames); + push @hostnames_to_update, $hosttag + if defined($hosttag) && $hosttag ne $node; } #print Dumper($hosttag) . "\n"; if ($hosttag) {