diff --git a/xCAT-server/lib/xcat/plugins/dhcp.pm b/xCAT-server/lib/xcat/plugins/dhcp.pm index 561bf77ba0..a5b3dc48bc 100644 --- a/xCAT-server/lib/xcat/plugins/dhcp.pm +++ b/xCAT-server/lib/xcat/plugins/dhcp.pm @@ -690,11 +690,68 @@ sub listnode } } +#------------------------------------------------------------------------------- + +=head3 delnode6_commands + + Descriptions: The omshell commands that remove the DHCPv6 host of a node. + Arguments: $node + Returns: the commands, or the empty string when there is no node to remove. + + The sequence is the one addnode6 uses to clear a name conflict before it + creates the host, so a removal and a replacement act on the same object. + +=cut + +#------------------------------------------------------------------------------- +sub delnode6_commands +{ + my ($node) = @_; + + return '' unless defined($node) && length($node); + + my $commands = "new host\n"; + $commands .= "set name = \"$node\"\n"; + $commands .= "open\n"; + $commands .= "remove\n"; + $commands .= "close\n"; + return $commands; +} + +#------------------------------------------------------------------------------- + +=head3 delnode6 + + Descriptions: Remove the DHCPv6 host of a node over an open omapi session. + Arguments: $node, $writer -- the omshell handle, or undef + Returns: 1 when the commands were written, 0 when there was nothing to do. + + makedhcp -d removed the DHCPv4 host alone. addnode6 creates the DHCPv6 host + as a separate object on a separate omapi port, so the reservation survived + the delete and kept the address and the boot file of a node that makedhcp + reported as removed. + +=cut + +#------------------------------------------------------------------------------- +sub delnode6 +{ + my ($node, $writer) = @_; + + return 0 unless defined($writer); + my $commands = delnode6_commands($node); + return 0 unless length($commands); + print {$writer} $commands; + return 1; +} + sub delnode { my $node = shift; my $inetn = inet_aton($node); + delnode6($node, $omshell6) if $usingipv6; + if (_isc_static_host_fallback()) { _delete_isc_static_host($node); $restartdhcp = 1; @@ -5510,6 +5567,77 @@ sub putmyselffirst { #------------------------------------------------------------------------------- +=head3 net_config_for + + Descriptions: The %netcfgs entry of a subnet. + Arguments: the subnet, with or without a prefix length + Returns: the configuration hash reference, or undef when there is none. + + addrangedetection keys %netcfgs on networks.net, which a conf may write + with no prefix length, while addnet6 names a subnet "/" from + the "ip -6 route" output. A lookup under the long form alone found nothing, + so the subnet6 lost its domain-search list and its name servers, while + dynamic_range_for, which already falls back, still found the range. + +=cut + +#------------------------------------------------------------------------------- +sub net_config_for +{ + my ($net) = @_; + + return unless defined $net; + return $netcfgs{$net} if $netcfgs{$net}; + (my $bare = $net) =~ s{/.*$}{}; + return $netcfgs{$bare}; +} + +#------------------------------------------------------------------------------- + +=head3 isc_dhcp6_subnet_statements + + Descriptions: The domain and resolver statements of an ISC subnet6. + Arguments: + %args - net, config (the net_config_for entry), site_domain, + alldomains (arrayref), domain_search (0 on sles10 and rh5, + whose dhcpd does not know the option) + Returns: the statements, as a list. Empty when the subnet has nothing to say. + +=cut + +#------------------------------------------------------------------------------- +sub isc_dhcp6_subnet_statements +{ + my (%args) = @_; + + my $cfg = $args{config} || {}; + my @lines; + + my $netdomain = $cfg->{domain} || $args{site_domain}; + $netdomain = '' unless defined $netdomain; + push @lines, " option domain-name \"" . $netdomain . "\";\n"; + + if ($args{domain_search} and $cfg->{domain}) { + + # We want something like "option domain-search "foo.com", "bar.com";" + my $domainstring = qq~"$cfg->{domain}"~; + foreach my $dom (@{ $args{alldomains} || [] }) { + chomp $dom; + if ($dom ne $cfg->{domain}) { + $domainstring .= qq~, "$dom"~; + } + } + push @lines, " option domain-search $domainstring;\n"; + } + + my $nsstatement = isc_dhcp6_nameserver_statement($args{net}, $cfg->{nameservers}); + push @lines, " $nsstatement\n" if $nsstatement; + + return @lines; +} + +#------------------------------------------------------------------------------- + =head3 dynamic_range_for Descriptions: The dynamic range of a subnet, as ISC writes it. @@ -5592,37 +5720,30 @@ sub addnet6 #posix timezone rfc 4833/tzdb timezone #phase 3 will include whatever is required to do Netboot6. That might be in the october timeframe for lack of implementations to test #boot url/param (rfc 59070) - my $netdomain = $netcfgs{$net}->{domain}; + # The lookup falls back to the bare net, as dynamic_range_for does. See + # net_config_for: a conf may write networks.net with no prefix length, + # while $net names the subnet "/". + my $netcfg = net_config_for($net) || {}; + + my $netdomain = $netcfg->{domain}; unless ($netdomain) { $netdomain = $site_domain; } - push @netent, " option domain-name \"" . $netdomain . "\";\n"; # add domain-search if not sles10 or rh5 my $osv = xCAT::Utils->osver(); - unless (($osv =~ /^sle[sc]10/) || ($osv =~ /^rh.*5$/)) { + push @netent, isc_dhcp6_subnet_statements( + net => $net, + config => $netcfg, + site_domain => $site_domain, + alldomains => \@alldomains, + domain_search => (($osv =~ /^sle[sc]10/) || ($osv =~ /^rh.*5$/)) ? 0 : 1, + ); - # We want something like "option domain-search "foo.com", "bar.com";" - my $domainstring = qq~"$netcfgs{$net}->{domain}"~; - foreach my $dom (@alldomains) { - chomp $dom; - if ($dom ne $netcfgs{$net}->{domain}) { - $domainstring .= qq~, "$dom"~; - } - } - - if ($netcfgs{$net}->{domain}) { - push @netent, " option domain-search $domainstring;\n"; - } - } - - - my $nameservers = $netcfgs{$net}->{nameservers}; - my $nsstatement = isc_dhcp6_nameserver_statement( $net, $nameservers ); - push @netent, " $nsstatement\n" if $nsstatement; + my $nameservers = $netcfg->{nameservers}; my $ddnserver = defined($nameservers) ? $nameservers : ''; $ddnserver =~ s/,.*//; my $ddnsdomain; - if ($netcfgs{$net}->{ddnsdomain}) { - $ddnsdomain = $netcfgs{$net}->{ddnsdomain}; + if ($netcfg->{ddnsdomain}) { + $ddnsdomain = $netcfg->{ddnsdomain}; } my $ddns_on = $::XCATSITEVALS{dnshandler} =~ /ddns/ ? 1 : 0; my $settings; diff --git a/xCAT-test/unit/dhcp_isc_dhcpv6_subnet_and_delete.t b/xCAT-test/unit/dhcp_isc_dhcpv6_subnet_and_delete.t new file mode 100644 index 0000000000..ef0b391aac --- /dev/null +++ b/xCAT-test/unit/dhcp_isc_dhcpv6_subnet_and_delete.t @@ -0,0 +1,145 @@ +#!/usr/bin/env perl +# +# Two gaps in the ISC DHCPv6 path. +# +# A conf may write networks.net with no prefix length, and addrangedetection keys %netcfgs on +# that value, while addnet6 names a subnet "/" from the "ip -6 route" output. The +# range lookup already falls back to the bare net; the domain and the resolver lookups did not, +# so the subnet6 carried no domain-search list and no name servers. +# +# makedhcp -d removed the DHCPv4 host alone. The DHCPv6 host is a separate omapi object on a +# separate port, so the reservation outlived the node it belonged to. +use strict; +use warnings; +no warnings 'once'; + +use FindBin; +use lib "$FindBin::Bin/../../xCAT-server/lib"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use Test::More tests => 19; + +$ENV{XCATCFG} ||= 'SQLite:/tmp'; + +my $source_dhcp_plugin = "$FindBin::Bin/../../xCAT-server/lib/xcat/plugins/dhcp.pm"; +if ( -f $source_dhcp_plugin ) { + require $source_dhcp_plugin; +} else { + require xCAT_plugin::dhcp; +} + +my $LONG = '2001:db8:149::/64'; +my $BARE = '2001:db8:149::'; + +# The resolver list reaches the statement through dhcp6_resolvers_for_network, which resolves +# names. The stub keeps the value it was handed, so the statement shows what the networks row +# carried rather than what this host can look up. +no warnings 'redefine'; +*xCAT_plugin::dhcp::dhcp6_resolvers_for_network = sub { + my ( undef, $nameservers ) = @_; + return $nameservers; +}; +use warnings 'redefine'; + +# --------------------------------------------------------------------------- +# The lookup, under the name the networks table uses and under the long name. + +%xCAT_plugin::dhcp::netcfgs = ( + $BARE => { + domain => 'ipv6.cluster', + nameservers => '2001:db8:149::1', + ddnsdomain => 'ddns.ipv6.cluster', + range => '2001:db8:149::1000 2001:db8:149::1fff', + }, +); + +my $cfg = xCAT_plugin::dhcp::net_config_for($LONG); +is( $cfg->{domain}, 'ipv6.cluster', + 'the domain of a prefixless networks row is found under the subnet name' ); +is( $cfg->{nameservers}, '2001:db8:149::1', + 'the nameservers of a prefixless networks row are found under the subnet name' ); +is( $cfg->{ddnsdomain}, 'ddns.ipv6.cluster', + 'the ddnsdomain of a prefixless networks row is found under the subnet name' ); + +# dynamic_range_for already fell back, and is left alone. It is the control: a lookup that +# stops falling back takes the three assertions above with it and leaves this one standing. +is( xCAT_plugin::dhcp::dynamic_range_for($LONG), + '2001:db8:149::1000 2001:db8:149::1fff', + 'the range lookup still answers for the same subnet' ); + +is( xCAT_plugin::dhcp::net_config_for('2001:db8:999::/64'), undef, + 'a subnet with no networks row has no configuration' ); +is( xCAT_plugin::dhcp::net_config_for(undef), undef, + 'no subnet name yields no configuration' ); + +# A row written WITH the prefix still wins, so the fallback adds a name and replaces none. +%xCAT_plugin::dhcp::netcfgs = ( + $LONG => { domain => 'long.cluster' }, + $BARE => { domain => 'bare.cluster' }, +); +is( xCAT_plugin::dhcp::net_config_for($LONG)->{domain}, 'long.cluster', + 'the row written with a prefix length is preferred over the bare one' ); + +# --------------------------------------------------------------------------- +# The statements the subnet6 carries. + +%xCAT_plugin::dhcp::netcfgs = (); +my @statements = xCAT_plugin::dhcp::isc_dhcp6_subnet_statements( + net => $LONG, + config => { + domain => 'ipv6.cluster', + nameservers => '2001:db8:149::1,2001:db8:149::2', + }, + site_domain => 'site.cluster', + alldomains => [ 'ipv6.cluster', 'other.cluster' ], + domain_search => 1, +); +my $subnet = join( '', @statements ); + +like( $subnet, qr/^\s*option domain-search\s+"ipv6\.cluster", "other\.cluster";$/m, + 'the subnet6 carries the domain-search list of its own network' ); +like( $subnet, qr/^\s*option dhcp6\.name-servers 2001:db8:149::1,2001:db8:149::2;$/m, + 'the subnet6 carries the nameservers the networks row configured' ); +like( $subnet, qr/^\s*option domain-name "ipv6\.cluster";$/m, + 'the network domain is the domain-name, not the site domain' ); +unlike( $subnet, qr/"ipv6\.cluster", "ipv6\.cluster"/, + 'the own domain is not repeated in the search list' ); + +# With no network domain the site domain names the subnet, and there is no search list to write. +my $sited = join( '', xCAT_plugin::dhcp::isc_dhcp6_subnet_statements( + net => $LONG, config => {}, site_domain => 'site.cluster', + alldomains => ['site.cluster'], domain_search => 1, +) ); +like( $sited, qr/^\s*option domain-name "site\.cluster";$/m, + 'a network with no domain falls back to the site domain' ); +unlike( $sited, qr/domain-search/, + 'a network with no domain gets no domain-search statement' ); + +# sles10 and rh5 dhcpd reject the option, and dhcpd refuses the whole file on one it does not know. +my $old = join( '', xCAT_plugin::dhcp::isc_dhcp6_subnet_statements( + net => $LONG, config => { domain => 'ipv6.cluster' }, + alldomains => ['ipv6.cluster'], domain_search => 0, +) ); +unlike( $old, qr/domain-search/, + 'domain-search is withheld where dhcpd does not know the option' ); + +# --------------------------------------------------------------------------- +# makedhcp -d reaches the DHCPv6 host. + +my $written = ''; +open my $omshell6, '>', \$written or die "Cannot open an in-memory handle: $!"; +is( xCAT_plugin::dhcp::delnode6( 'node01', $omshell6 ), 1, + 'a delete with an open DHCPv6 session reports that it acted' ); +close $omshell6; + +like( $written, qr/^set name = "node01"\n/m, + 'makedhcp -d names the node whose DHCPv6 host it removes' ); +like( $written, qr/^open\nremove\nclose\n/m, + 'and opens the host and removes it' ); + +# A node that holds no DHCPv6 address has no session, so nothing is written and nothing dies. +is( xCAT_plugin::dhcp::delnode6( 'node01', undef ), 0, + 'a delete with no DHCPv6 session writes nothing' ); +is( xCAT_plugin::dhcp::delnode6_commands(''), '', + 'an empty node name yields no commands' );