diff --git a/perl-xCAT/xCAT/DHCP/BootPolicy.pm b/perl-xCAT/xCAT/DHCP/BootPolicy.pm index f8125fb1c8..393ceca075 100644 --- a/perl-xCAT/xCAT/DHCP/BootPolicy.pm +++ b/perl-xCAT/xCAT/DHCP/BootPolicy.pm @@ -149,6 +149,67 @@ sub kea_bootfile_url6_option { return { name => 'bootfile-url', data => $url, 'always-send' => JSON::true() }; } +#--- +# =head3 dhcp6_ipxe_user_class_test +# Descriptions: The Kea DHCPv6 expression that is true for iPXE and false for the firmware that +# loaded it. iPXE sends option 15, the User Class, holding the length-prefixed +# string iPXE: 00:04:69:50:58:45. EDK2 sends no option 15 at all. The two length +# octets are skipped, so the comparison reads the string itself. +# +# The byte order of the DUID cannot do this job. Both clients build a DUID-UUID +# from one vpd.uuid, and which of them sends SMBIOS order and which sends RFC 4122 +# order depends on the order the platform stores the uuid in: measured on QEMU, the +# firmware sends RFC 4122 order and iPXE sends SMBIOS order, the opposite of a +# physical EDK2 machine. The DHCPv4 path splits the same two stages on the iPXE +# feature sub-options of option 175, which is the same kind of test as this one. +# Arguments: none +# Returns: the expression +#--- +sub dhcp6_ipxe_user_class_test { return "substring(option[15].hex, 2, 4) == 'iPXE'"; } + +#--- +# =head3 kea_node_boot_classes6 +# Descriptions: The two DHCPv6 client classes that give one chainloading node its boot file, one +# for each stage. A reservation cannot do this: Kea gives a reservation's option +# data precedence over a class's, so a reservation naming either URL would answer +# both stages with it. The classes carry the boot file and the reservation keeps the +# address. +# +# Each class matches every DUID the node can send, and the stage is decided by +# option 15 alone. The tests are mutually exclusive, so the order of the two in the +# configuration does not matter. +# Arguments: node -- the node name, which names the classes +# duids -- every DUID of the node, as colon-separated hexadecimal octets +# loader_url -- the first-stage loader, which the firmware fetches over tftp +# script_url -- the boot script of the node, which only iPXE can fetch +# Returns: an arrayref of class definitions, empty unless the node has a DUID and both URLs +#--- +sub kea_node_boot_classes6 { + my ( $class, %opts ) = @_; + + my @duids = grep { defined($_) && length($_) } @{ $opts{duids} || [] }; + return [] unless @duids && $opts{node}; + return [] unless defined( $opts{loader_url} ) && defined( $opts{script_url} ); + + # Kea compares option[1].hex against a hexadecimal literal, not against a DUID written with + # colons. Either order identifies the node, so both are accepted here and neither decides + # which stage is answering. + my $identity = join ' or ', + map { my $hex = $_; $hex =~ s/://g; "option[1].hex == 0x$hex" } @duids; + my $ipxe = $class->dhcp6_ipxe_user_class_test(); + + return [ + { name => "$opts{node}-ipxe6", + test => "($identity) and $ipxe", + 'option-data' => [ kea_bootfile_url6_option( $opts{script_url} ) ], + }, + { name => "$opts{node}-firmware6", + test => "($identity) and not ($ipxe)", + 'option-data' => [ kea_bootfile_url6_option( $opts{loader_url} ) ], + }, + ]; +} + #: Every client architecture some class here, or in the per-network classes #: beside it, already answers. The fallback is what is left over. my @RECOGNISED_ARCH_IDS = qw( diff --git a/xCAT-server/lib/xcat/plugins/dhcp.pm b/xCAT-server/lib/xcat/plugins/dhcp.pm index 7ed77340bc..c79d4dd523 100644 --- a/xCAT-server/lib/xcat/plugins/dhcp.pm +++ b/xCAT-server/lib/xcat/plugins/dhcp.pm @@ -3038,6 +3038,7 @@ sub kea_process_request push @deleted6, @{ $backend->delete_reservations($loaded6, $match) } if $loaded6; } $client_classes_changed = kea_remove_node_client_classes($loaded4, $nodes); + kea_remove_node_boot_classes6($loaded6, $nodes) if $loaded6; } else { # An upgrade keeps the option definitions of an older makedhcp -n. ( $option_defs_added, my $defs_error ) = xCAT::DHCP::BootPolicy->kea_declare_ipxe_features($loaded4->{Dhcp4}); @@ -3052,6 +3053,7 @@ sub kea_process_request if ($loaded6) { $reservations6 = kea_build_node_reservations6($backend, $loaded6, $nodes); $backend->upsert_reservations($loaded6, $reservations6, \@replaced6); + kea_sync_node_boot_classes6($loaded6, $nodes); } } @@ -4087,6 +4089,124 @@ sub kea_build_node_reservations6 return \@reservations; } +#: The purposes of a per-node DHCPv6 client class, so a resync finds the classes of this node +#: set and leaves every other class alone. +my %KEA_NODE_CLASS_PURPOSES6 = ( boot6 => 1 ); + +#--- +# =head3 kea_node_boot_classes6_for_nodes +# Descriptions: The per-node DHCPv6 client classes of every chainloading node in the range, two +# for each: one for the firmware and one for the iPXE it loads. A node loaded +# directly by its firmware has one client and keeps the boot file in its +# reservation, so it gets no class here. +# Arguments: $nodes -- the node range +# Returns: an arrayref of class definitions +#--- +sub kea_node_boot_classes6_for_nodes +{ + my ($nodes) = @_; + + my $nrtab = xCAT::Table->new('noderes'); + return [] unless $nrtab; + my $nttab = xCAT::Table->new( 'nodetype', -create => 0 ); + my $nrents = $nrtab->getNodesAttribs( $nodes, + [ 'tftpserver', 'netboot', 'xcatmaster', 'servicenode', 'tftpdir' ] ); + my $ntents = $nttab ? $nttab->getNodesAttribs( $nodes, ['arch'] ) : undef; + my $duids = dhcp6_client_duids($nodes); + + my @classes; + foreach my $node (@$nodes) { + my $nrent = $nrents && $nrents->{$node} ? $nrents->{$node}->[0] : undef; + next unless dhcp6_chainloads($nrent); + my $ntent = $ntents && $ntents->{$node} ? $ntents->{$node}->[0] : undef; + + # Both UUID orders, because either one can be the firmware: the order depends on how the + # platform stores its SMBIOS uuid. The DUID-LL of the Genesis image is left out -- it is + # the booted operating system and asks for no boot file. + my @duids = + map { $_->{duid} } + grep { $_->{duid} && ( $_->{client} || '' ) =~ /\A(?:firmware|ipxe)\z/ } + @{ ( $duids->{$node} || {} )->{duids} || [] }; + next unless @duids; + + my $loader = dhcp6_bootfile_url_for_node( $node, $node, $nrent, $ntent, 'firmware' ); + my $script = dhcp6_bootfile_url_for_node( $node, $node, $nrent, $ntent, 'ipxe' ); + next unless defined($loader) && defined($script); + + foreach my $class ( + @{ xCAT::DHCP::BootPolicy->kea_node_boot_classes6( + node => $node, + duids => \@duids, + loader_url => $loader, + script_url => $script, + ) + } + ) + { + $class->{'user-context'} = { 'xcat-node' => $node, 'xcat-purpose' => 'boot6' }; + push @classes, $class; + } + } + + return \@classes; +} + +#--- +# =head3 kea_remove_node_boot_classes6 +# Descriptions: Take the per-node DHCPv6 boot classes of one node set out of the configuration, +# so a resync or a makedhcp -d leaves nothing behind for a node that changed its +# netboot method or went away. +# Arguments: $config -- the parsed Kea DHCPv6 configuration, $nodes -- the node range +# Returns: 1 when it removed a class +#--- +sub kea_remove_node_boot_classes6 +{ + my ( $config, $nodes ) = @_; + + return 0 unless $config && $config->{Dhcp6}; + my %wanted = map { $_ => 1 } @$nodes; + my @kept; + my $changed = 0; + foreach my $class ( @{ $config->{Dhcp6}{'client-classes'} || [] } ) { + my $context = $class->{'user-context'} || {}; + if ( $KEA_NODE_CLASS_PURPOSES6{ $context->{'xcat-purpose'} || '' } + && $wanted{ $context->{'xcat-node'} || '' } ) + { + $changed = 1; + next; + } + push @kept, $class; + } + $config->{Dhcp6}{'client-classes'} = \@kept if $changed; + + return $changed; +} + +#--- +# =head3 kea_sync_node_boot_classes6 +# Descriptions: Replace the per-node DHCPv6 boot classes of one node set. The new ones go first: +# Kea takes an option from the first class that defines it, and the per-network +# architecture classes define the same bootfile-url for every client of that +# architecture. +# Arguments: $config -- the parsed Kea DHCPv6 configuration, $nodes -- the node range +# Returns: 1 when the configuration changed +#--- +sub kea_sync_node_boot_classes6 +{ + my ( $config, $nodes ) = @_; + + my $changed = kea_remove_node_boot_classes6( $config, $nodes ); + my $classes = kea_node_boot_classes6_for_nodes($nodes); + if (@$classes) { + $config->{Dhcp6} ||= {}; + $config->{Dhcp6}{'client-classes'} = + [ @$classes, @{ $config->{Dhcp6}{'client-classes'} || [] } ]; + $changed = 1; + } + + return $changed; +} + sub kea_prepare_reservation_ownership { my ($nodes) = @_; @@ -4696,8 +4816,17 @@ sub kea_node_reservations6 } else { $reservation{'hw-address'} = $client->{'hw-address'}; } + # A chainloading node is answered by its two per-node client classes, not here. + # Kea gives a reservation's option data precedence over a class's, so a boot file + # written here would answer both boot stages with one URL -- and the DUID byte order + # that the stage label is derived from is not the same on every platform. + my $answered_by_class = + dhcp6_chainloads($nrent) + && ( $client->{client} || '' ) =~ /\A(?:firmware|ipxe)\z/; my $bootfile_url = - dhcp6_bootfile_url_for_node( $node, $hname, $nrent, $ntent, $client->{client} ); + $answered_by_class + ? undef + : dhcp6_bootfile_url_for_node( $node, $hname, $nrent, $ntent, $client->{client} ); if ($bootfile_url) { $reservation{'option-data'} = [ { @@ -4708,8 +4837,10 @@ sub kea_node_reservations6 ]; } - # A client that needs no boot file and holds no address reserves nothing. - next unless $first or $bootfile_url; + # A client that needs no boot file and holds no address reserves nothing. A client + # a class answers still gets an entry: its DUID is what an update or a delete of the + # node finds the client by after the netboot method changed. + next unless $first or $bootfile_url or $answered_by_class; $first = 0; push @reservations, \%reservation; } diff --git a/xCAT-test/unit/dhcp6_boot_stage_classes.t b/xCAT-test/unit/dhcp6_boot_stage_classes.t new file mode 100644 index 0000000000..5dd2d7f584 --- /dev/null +++ b/xCAT-test/unit/dhcp6_boot_stage_classes.t @@ -0,0 +1,108 @@ +#!/usr/bin/perl +# A UEFI node that chainloads is two DHCPv6 clients, and the byte order of the DUID cannot tell +# them apart. +# +# dhcp6_client_duid_list assumes EDK2 sends vpd.uuid with its first three fields in SMBIOS +# little-endian order and iPXE sends the same uuid in RFC 4122 network order, so it labels one +# DUID 'firmware' and the other 'ipxe' and gives each the boot file of that stage. Measured on +# xcat88-mn with Kea DHCPv6 at DEBUG, one boot of xcat88-cn sends the opposite pair: +# +# SOLICIT client-id 00:04:0c:3f:8d:41:... <- the firmware, RFC 4122 order, no option 15 +# SOLICIT client-id 00:04:41:8d:3f:0c:... <- iPXE, SMBIOS order, option 15 = "iPXE" +# +# The order depends on how the platform stores its SMBIOS uuid, so on QEMU the two come out +# swapped against physical EDK2 hardware. The firmware therefore matched the reservation labelled +# 'ipxe' and was given an http:// URL, which a UEFI PXEv6 boot option cannot fetch: +# "PXE-E53: No boot filename received". iPXE matched the one labelled 'firmware', was given the +# shim a second time and stopped at "Failed to open ipxe.efi". +# +# The DHCPv4 path never had this problem because it splits the stages on what the client reports, +# the iPXE feature sub-options of option 175. Over DHCPv6 the equivalent report is option 15, the +# User Class, which iPXE sends as a length-prefixed "iPXE" and the firmware does not send at all. +use strict; +use warnings; +use Test::More tests => 16; +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +require xCAT::DHCP::BootPolicy; + +# --- the discriminator --------------------------------------------------------------------- +# Both subs are named here, because every assertion below reads a value one of them returns. An +# absent sub returns nothing, and nothing satisfies an assertion that an answer is empty -- so +# without these two the emptiness tests at the end would pass on an unfixed tree. +can_ok( 'xCAT::DHCP::BootPolicy', 'dhcp6_ipxe_user_class_test' ); +can_ok( 'xCAT::DHCP::BootPolicy', 'kea_node_boot_classes6' ); + +my $ipxe = eval { xCAT::DHCP::BootPolicy->dhcp6_ipxe_user_class_test() } || ''; +like( $ipxe, qr/option\[15\]/, + 'the iPXE test reads option 15, the DHCPv6 User Class' ); +like( $ipxe, qr/'iPXE'/, + 'it compares against iPXE, the string iPXE puts in that option' ); +like( $ipxe, qr/substring\(\s*option\[15\]\.hex\s*,\s*2\s*,\s*4\s*\)/, + 'it skips the two length octets the User Class option prefixes, so 00:04:69:50:58:45 matches' ); +ok( ( length($ipxe) && $ipxe !~ /option\[1\]|duid/i ), + 'the discriminator names no DUID: byte order must not decide the stage' ); + +# --- the two per-node classes -------------------------------------------------------------- +# Returns whatever the sub returned, so a missing sub gives undef and not an empty list. An +# empty list is a real answer here -- a node with no second stage -- and the two must not look +# the same to an assertion. +sub classes { + my (%opts) = @_; + my $out = eval { xCAT::DHCP::BootPolicy->kea_node_boot_classes6(%opts) }; + diag($@) if $@; + return $out; +} + +my $LOADER = 'tftp://[2001:db8:188::1]/xcat/ipxe/x86_64-sb/snponly-shim.efi'; +my $SCRIPT = 'http://[2001:db8:188::1]/tftpboot/xcat/ipxe/nodes/xcat88-cn.uefi'; +my @DUIDS = ( + '00:04:0c:3f:8d:41:6a:52:4e:7b:9f:10:2d:8c:54:ab:77:31', + '00:04:41:8d:3f:0c:52:6a:7b:4e:9f:10:2d:8c:54:ab:77:31', +); + +my $got = classes( + node => 'xcat88-cn', + duids => [@DUIDS], + loader_url => $LOADER, + script_url => $SCRIPT, +); +is_deeply( [ map { ref } @{ $got || [] } ], [ 'HASH', 'HASH' ], + 'a chainloading node gets one class per boot stage' ); + +my %by_url; +foreach my $class ( @{ $got || [] } ) { + foreach my $opt ( @{ $class->{'option-data'} || [] } ) { + $by_url{ $opt->{data} } = $class if ( $opt->{name} || '' ) eq 'bootfile-url'; + } +} +my $fw_class = $by_url{$LOADER}; +my $ipxe_class = $by_url{$SCRIPT}; +ok( $fw_class, 'one class serves the first-stage loader over tftp' ); +ok( $ipxe_class, 'the other serves the boot script of the node' ); + +ok( ( length($ipxe) && ( $ipxe_class->{test} || '' ) =~ /\Q$ipxe\E/ ), + 'the boot script goes only to a client that reports itself as iPXE' ); +like( $fw_class->{test} || '', qr/not\s*\(/, + 'the loader goes to a client that does not report itself as iPXE' ); + +# Both orders in both tests: whichever order the platform sends, the node is recognised. +foreach my $duid (@DUIDS) { + ( my $hex = $duid ) =~ s/://g; + ok( ( ( $fw_class->{test} || '' ) =~ /\Q$hex\E/i + && ( $ipxe_class->{test} || '' ) =~ /\Q$hex\E/i ), + "both classes match DUID $duid, so neither order is assumed" ); +} + +# --- a node that does not chainload needs none of this ------------------------------------- +is_deeply( classes( node => 'n1', duids => [], loader_url => $LOADER, script_url => $SCRIPT ), + [], 'a node with no DUID gets no boot classes' ); +is_deeply( classes( node => 'n1', duids => [ $DUIDS[0] ], loader_url => undef, script_url => undef ), + [], 'a node with no boot file gets no boot classes' ); + +# A grub2 node is loaded by its firmware and has one client, so its reservation still carries the +# URL and no class is needed. kea_node_boot_classes6 is called only for a chainloading method, so +# the guard that matters here is that it never invents a class without both URLs. +is_deeply( classes( node => 'n1', duids => [ $DUIDS[0] ], loader_url => $LOADER ), + [], 'without a script URL there is no second stage to split off' ); diff --git a/xCAT-test/unit/dhcp_dhcpv6_duid.t b/xCAT-test/unit/dhcp_dhcpv6_duid.t index 3892301ac6..35bf4b4a36 100644 --- a/xCAT-test/unit/dhcp_dhcpv6_duid.t +++ b/xCAT-test/unit/dhcp_dhcpv6_duid.t @@ -197,6 +197,21 @@ local $xCAT_plugin::dhcp::callback = sub { use warnings 'redefine'; my $backend = xCAT::DHCP::Backend::Kea->new(); +# The two per-node client classes of a chainloading node, keyed by the URL each one serves. +# A chainloading node is answered by these and not by its reservation: Kea gives a reservation's +# option data precedence over a class's, so one URL in the reservation would answer both boot +# stages with it. +sub boot_classes6 { + my (@nodes) = @_; + my %by_url; + foreach my $class ( @{ xCAT_plugin::dhcp::kea_node_boot_classes6_for_nodes( \@nodes ) } ) { + foreach my $option ( @{ $class->{'option-data'} || [] } ) { + $by_url{ $option->{data} } = $class if ( $option->{name} || '' ) eq 'bootfile-url'; + } + } + return \%by_url; +} + sub empty_config { return JSON::decode_json( $backend->render_dhcp6_config( { interfaces => ['eth0'], subnets => [ { id => 1, subnet => '2001:db8:176::/64', interface => 'eth0' } ] } ) ); @@ -356,13 +371,16 @@ sub isc_hosts { grep { defined $_->{hostname} } @{ kea_reservations( $config, 'cn02', 'cn01' ) }; my ($with) = grep { $_->{name} eq 'bootfile-url' } @{ $kea{cn02}{'option-data'} || [] }; - is( $with && $with->{data}, "tftp://[2001:db8:176::1]/$loader", - 'an ipxe node gets the UEFI loader from the bracketed IPv6 server' ); + is( $with, undef, 'the reservation of an ipxe node names no boot file' ); + my $cn02_classes = boot_classes6('cn02'); + ok( $cn02_classes->{"tftp://[2001:db8:176::1]/$loader"}, + 'an ipxe node gets the UEFI loader from the bracketed IPv6 server, through its class' ); # Naming a file the TFTP server does not have costs the client a timeout it cannot - # diagnose, so no option is better than a wrong one. - my ($without) = grep { $_->{name} eq 'bootfile-url' } @{ $kea{cn01}{'option-data'} || [] }; - is( $without, undef, 'an ipxe node whose loader is not on disk gets no bootfile-url' ); + # diagnose, so no option is better than a wrong one. The node gets no class at all rather + # than a class naming a file that is not there. + is_deeply( xCAT_plugin::dhcp::kea_node_boot_classes6_for_nodes( ['cn01'] ), [], + 'an ipxe node whose loader is not on disk gets no boot class' ); # BUT IT MUST NOT BE SILENT. makedhcp exits 0 either way, so a node with no boot file and no # warning reaches a firmware timeout and no log on the management node says why. The warning @@ -952,11 +970,25 @@ is( xCAT_plugin::dhcp::radvd_needs_restart( 0, 1 ), 0, 'an unchanged file with t 'an ipxe node is reserved for both of the DUIDs its two clients send' ); my ($first) = grep { $_->{duid} eq $firmware } @cn01; my ($second) = grep { $_->{duid} eq $canonical and !defined $_->{hostname} } @cn01; - is( $url{$firmware}, $shim_url, - 'the firmware-order DUID is given the UEFI shim' ); + is( $url{$firmware}, undef, + 'neither reservation of a chainloading node names a boot file' ); my ($second_option) = grep { $_->{name} eq 'bootfile-url' } @{ $second->{'option-data'} || [] }; - is( $second_option && $second_option->{data}, $script_url, - 'the RFC 4122-order DUID, which is the one iPXE sends, is given the boot script' ); + is( $second_option, undef, '... including the one that holds no address' ); + + # Which order the firmware sends depends on how the platform stores its SMBIOS uuid, so both + # classes match both orders and option 15 alone decides the stage. Measured on xcat88-mn: + # the firmware sent 00:04:0c:3f:8d:41:... and iPXE sent 00:04:41:8d:3f:0c:..., the opposite + # of what a physical EDK2 machine sends. + my $cn01_classes = boot_classes6('cn01'); + ( my $firmware_hex = $firmware ) =~ s/://g; + ( my $canonical_hex = $canonical ) =~ s/://g; + foreach my $pair ( [ $shim_url, 'the UEFI shim' ], [ $script_url, 'the boot script' ] ) { + my $test = ( $cn01_classes->{ $pair->[0] } || {} )->{test} || ''; + ok( ( $test =~ /\Q$firmware_hex\E/i && $test =~ /\Q$canonical_hex\E/i ), + "the class serving $pair->[1] matches both UUID orders of the node" ); + } + ok( $cn01_classes->{$script_url}{test} =~ /option\[15\]/, + 'and the boot script is given only to a client that reports itself as iPXE' ); # Kea refuses a second reservation for one address, and _reservation_matches treats a shared # hostname as the same reservation, so the second entry carries neither. @@ -973,8 +1005,11 @@ is( xCAT_plugin::dhcp::radvd_needs_restart( 0, 1 ), 0, 'an unchanged file with t my ($cn03_addressed) = grep { defined $_->{hostname} && $_->{hostname} eq 'cn03' } @all; is( $cn03_addressed->{duid}, $cn03_canonical, 'mac.duid still decides which client keeps the reserved address' ); - is( $url{$cn03_firmware}, $shim_url, + ok( exists $url{$cn03_firmware}, '... and the other client is reserved for as well' ); + is( $url{$cn03_firmware}, undef, '... carrying no boot file of its own' ); + ok( boot_classes6('cn03')->{$shim_url}, + '... because the loader comes from the class of the node' ); # A grub2 node is loaded by the firmware and asks for nothing more, so a second reservation # would be an entry no client matches. @@ -1055,10 +1090,15 @@ is( xCAT_plugin::dhcp::radvd_needs_restart( 0, 1 ), 0, 'an unchanged file with t my ($found) = grep { $_->{name} eq 'bootfile-url' } @{ $_->{'option-data'} || [] }; $found; } @cn01; - is( scalar( grep { $_ && $_->{'always-send'} } @option ), 3, - 'each of the three clients is sent a boot file URL whether it asks for one or not' ); - is( $option[0] && $option[0]->{data}, $shim_url, 'the firmware is given the UEFI shim' ); - is( $option[1] && $option[1]->{data}, $script_url, 'iPXE is given the boot script instead' ); + # Only the Genesis client is answered from its reservation. The two UUID-order clients are + # the chainload stages, and a reservation cannot tell them apart. + is( scalar( grep { $_ && $_->{'always-send'} } @option ), 1, + 'one of the three clients is sent a boot file URL from its reservation' ); + is( $option[0], undef, 'the firmware is not given one there' ); + is( $option[1], undef, '... nor is iPXE' ); + my $classes = boot_classes6('cn01'); + ok( $classes->{$shim_url}, 'the firmware is given the UEFI shim by its class' ); + ok( $classes->{$script_url}, 'and iPXE the boot script by its own' ); is( $option[2] && $option[2]->{data}, $shim_url, 'and the Genesis client the same file as the firmware' );