You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Cloud/CloudPlacementNetworkServic...

723 lines
28 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudHost;
use App\Models\CloudHostAgent;
use App\Models\CloudHostNetworkAttachment;
use App\Models\CloudPublicIp;
use App\Models\CloudPublicIpPool;
/**
* Exact network admission for new-instance placement.
*
* The caller chooses an exact resource-pool membership and owns the host lock.
* This module resolves only that pool's line into current Agent-verified
* attachments, applied public pools and one concrete IP ledger row.
*/
final class CloudPlacementNetworkService
{
private const MAX_PUBLIC_IPS_PER_POOL = 254;
/**
* Non-locking catalog hint. Payment-time placement repeats the same
* evidence checks under host, attachment, pool and IP row locks.
*
* @return array<int,bool>
*/
public static function advisoryAvailableHosts($candidateRows): array
{
$readyByHostLine = self::advisoryAvailableHostLines($candidateRows);
$available = [];
foreach ($candidateRows as $candidate) {
$hostId = (int) ($candidate->id ?? 0);
$lineId = (int) ($candidate->resource_pool_line_id ?? 0);
if (isset($readyByHostLine[$hostId][$lineId])) {
$available[$hostId] = true;
}
}
return $available;
}
/**
* New-instance catalog hint for one IPv4 + one sparse IPv6 pool on the
* same currently verified attachment. Exact admission is repeated by the
* bundle allocator under locks after payment.
*
* @return array<int,bool>
*/
public static function advisoryDualStackAvailableHosts(
$candidateRows
): array {
$bundles = self::advisoryDualStackBundleEvidence($candidateRows);
$available = [];
foreach ($bundles as $hostId => $lines) {
foreach ($lines as $lineBundles) {
if ($lineBundles !== []) {
$available[(int) $hostId] = true;
break;
}
}
}
ksort($available, SORT_NUMERIC);
return $available;
}
/**
* Read-only host+line evidence for one currently available IPv4+IPv6
* bundle. Both families must use the same current Agent-verified network
* attachment. No address is selected or exposed, and admission must still
* repeat every predicate while holding the placement ledgers.
*
* @return array<int,array<int,list<array<string,mixed>>>>
*/
public static function advisoryDualStackBundleEvidence(
$candidateRows
): array {
$hostLines = [];
foreach ($candidateRows as $candidate) {
$hostId = (int) ($candidate->id ?? 0);
$lineId = (int) ($candidate->resource_pool_line_id ?? 0);
if ($hostId > 0 && $lineId > 0) {
$hostLines[$hostId][$lineId] = true;
}
}
if ($hostLines === []) {
return [];
}
$agents = CloudHostAgent::whereIn('host_id', array_keys($hostLines))
->get()->keyBy('host_id');
$attachments = CloudHostNetworkAttachment::whereIn(
'host_id',
array_keys($hostLines)
)->where('state', 'active')->where('verification_state', 'verified')
->orderBy('priority')->orderBy('id')->get();
if ($attachments->isEmpty()) {
return [];
}
$eligible = [];
foreach ($attachments as $attachment) {
$hostId = (int) $attachment->host_id;
$lineId = (int) $attachment->line_id;
$agent = $agents->get($hostId);
if ($agent instanceof CloudHostAgent
&& isset($hostLines[$hostId][$lineId])
&& (int) $attachment->config_revision > 0
&& self::attachmentReadyForAgent($attachment, $agent)
&& CloudAgentSettingsService::publicNetworkReady(
$agent,
(string) $attachment->network_selector,
(int) $attachment->verified_agent_revision
)) {
$eligible[(int) $attachment->id] = $attachment;
}
}
if ($eligible === []) {
return [];
}
$pools = CloudPublicIpPool::whereIn(
'network_attachment_id',
array_keys($eligible)
)->where('state', 'active')->where('sync_state', 'applied')
->whereColumn('observed_revision', 'desired_revision')
->orderBy('priority')->orderBy('id')->get();
$poolIds = $pools->pluck('id')->map(
static fn ($id): int => (int) $id
)->all();
$ipsByPool = [];
if ($poolIds !== []) {
$ipRows = CloudPublicIp::whereIn('public_ip_pool_id', $poolIds)
->whereIn('state', [
'available', 'reserved', 'changing', 'uncertain',
'assigned',
])->orderBy('id')->get();
foreach ($ipRows as $ip) {
$ipsByPool[(int) $ip->public_ip_pool_id][] = $ip;
}
}
$hosts = CloudHost::whereIn('id', array_keys($hostLines))
->get()->keyBy('id');
$capacity = [];
foreach ($pools as $pool) {
$attachment = $eligible[(int) $pool->network_attachment_id]
?? null;
$family = (int) $pool->address_family;
if ($attachment === null
|| (int) $pool->host_id !== (int) $attachment->host_id
|| !in_array($family, [4, 6], true)
|| preg_match('/^[a-f0-9]{32}$/D',
(string) $pool->pool_key) !== 1) {
continue;
}
$poolIps = $ipsByPool[(int) $pool->id] ?? [];
$availableUnits = $family === 4
? self::advisoryIpv4PoolAvailableUnits(
$pool,
$poolIps,
$hosts->get((int) $pool->host_id)
)
: self::advisoryIpv6PoolAvailableUnits($pool, $poolIps);
if ($availableUnits > 0
&& !isset($capacity[(int) $attachment->id][$family])) {
$capacity[(int) $attachment->id][$family] = [
'public_ip_pool_id' => (int) $pool->id,
'pool_key' => (string) $pool->pool_key,
'network_attachment_id' =>
(int) $pool->network_attachment_id,
'address_family' => $family,
'desired_revision' => (int) $pool->desired_revision,
'observed_revision' => (int) $pool->observed_revision,
'available_units' => $availableUnits,
];
}
}
$available = [];
foreach ($capacity as $attachmentId => $families) {
if (!isset($families[4], $families[6])) {
continue;
}
$attachment = $eligible[(int) $attachmentId];
$hostId = (int) $attachment->host_id;
$lineId = (int) $attachment->line_id;
$available[$hostId][$lineId][] = [
'network_attachment_id' => (int) $attachment->id,
'host_id' => $hostId,
'line_id' => $lineId,
'attachment_key' => (string) $attachment->attachment_key,
'network_selector' => (string) $attachment->network_selector,
'attachment_config_revision' =>
(int) $attachment->config_revision,
'attachment_verified_agent_revision' =>
(int) $attachment->verified_agent_revision,
'ipv4' => $families[4],
'ipv6' => $families[6],
];
}
ksort($available, SORT_NUMERIC);
foreach ($available as &$lines) {
ksort($lines, SORT_NUMERIC);
}
unset($lines);
return $available;
}
/** @return array<int,array<int,bool>> keyed by host ID then line ID. */
public static function advisoryAvailableHostLines($candidateRows): array
{
$details = self::advisoryAvailableHostLineDetails($candidateRows);
$available = [];
foreach ($details as $hostId => $lines) {
foreach ($lines as $lineId => $detail) {
if (($detail['available_ip_count'] ?? 0) > 0) {
$available[(int) $hostId][(int) $lineId] = true;
}
}
}
return $available;
}
/**
* Non-locking inventory for exact host/line restore target presentation.
* Admission must still repeat every predicate through lockedCandidates()
* and lockAvailableSelection().
*
* @return array<int,array<int,array{available_ip_count:int,
* network_selectors:list<string>}>>
*/
public static function advisoryAvailableHostLineDetails(
$candidateRows
): array {
$hostLines = [];
foreach ($candidateRows as $candidate) {
$hostId = (int) ($candidate->id ?? 0);
$lineId = (int) ($candidate->resource_pool_line_id ?? 0);
if ($hostId > 0 && $lineId > 0) {
$hostLines[$hostId][$lineId] = true;
}
}
if ($hostLines === []) {
return [];
}
$rows = CloudPublicIp::query()->join(
'cloud_public_ip_pools as public_pool',
'public_pool.id', '=', 'cloud_public_ips.public_ip_pool_id'
)->join(
'cloud_host_network_attachments as network_attachment',
'network_attachment.id', '=', 'public_pool.network_attachment_id'
)->join(
'cloud_host_agents as host_agent',
'host_agent.host_id', '=', 'cloud_public_ips.host_id'
)->whereIn('cloud_public_ips.host_id', array_keys($hostLines))
->where('cloud_public_ips.state', 'available')
->whereNull('cloud_public_ips.reservation_id')
->whereNull('cloud_public_ips.instance_id')
->whereNull('cloud_public_ips.operation_id')
->whereColumn('cloud_public_ips.host_id', 'public_pool.host_id')
->whereColumn(
'cloud_public_ips.prefix_length',
'public_pool.guest_prefix_length'
)
->whereColumn(
'cloud_public_ips.host_id',
'network_attachment.host_id'
)
->where('host_agent.enabled', 1)
->where('host_agent.last_test_status', 'healthy')
->where('host_agent.tested_revision', '>', 0)
->whereColumn('host_agent.tested_revision', 'host_agent.revision')
->where('network_attachment.state', 'active')
->where('network_attachment.verification_state', 'verified')
->where('public_pool.state', 'active')
->where('public_pool.sync_state', 'applied')
->whereColumn(
'public_pool.observed_revision',
'public_pool.desired_revision'
)
->get([
'cloud_public_ips.host_id',
'cloud_public_ips.address',
'network_attachment.line_id',
'network_attachment.network_selector',
'network_attachment.verified_agent_revision',
'network_attachment.last_verified_at',
'host_agent.host_id as agent_host_id',
'host_agent.enabled as agent_enabled',
'host_agent.revision as agent_revision',
'host_agent.tested_revision as agent_tested_revision',
'host_agent.last_test_status as agent_last_test_status',
'host_agent.network_selectors_json as agent_network_selectors_json',
'public_pool.pool_key',
'public_pool.cidr',
'public_pool.gateway',
'public_pool.dns_servers_json',
'public_pool.reserved_ips_json',
]);
$available = [];
foreach ($rows as $row) {
$hostId = (int) $row->host_id;
$lineId = (int) $row->line_id;
if (!isset($hostLines[$hostId][$lineId])
|| !self::agentEvidenceReady(
self::agentFromEvidenceRow($row),
(string) $row->network_selector,
(int) $row->verified_agent_revision,
(int) $row->last_verified_at
)
|| preg_match(
'/^[a-f0-9]{32}$/D',
(string) $row->pool_key
) !== 1
|| !self::ipv4BelongsToCidr(
(string) $row->address,
(string) $row->cidr
) || !self::addressAllowedByPool(
(string) $row->address,
$row,
null
)) {
continue;
}
if (!isset($available[$hostId][$lineId])) {
$available[$hostId][$lineId] = [
'available_ip_count' => 0,
'network_selectors' => [],
];
}
$available[$hostId][$lineId]['available_ip_count']++;
$available[$hostId][$lineId]['network_selectors'][
(string) $row->network_selector
] = true;
}
foreach ($available as &$lines) {
foreach ($lines as &$detail) {
$selectors = array_keys($detail['network_selectors']);
sort($selectors, SORT_STRING);
$detail['network_selectors'] = $selectors;
}
unset($detail);
}
unset($lines);
return $available;
}
/**
* Lock and return every network candidate for one exact host/line.
*
* The host and current Agent row must already be locked. Attachment and
* public-pool locks are always acquired in primary-key order.
*
* @return list<array{attachment:CloudHostNetworkAttachment,
* public_pool:CloudPublicIpPool}>
*/
public static function lockedCandidates(int $hostId, int $lineId,
CloudHostAgent $agent): array
{
if ($hostId < 1 || $lineId < 1 || !$agent->isReady()
|| (int) $agent->host_id !== $hostId) {
return [];
}
$attachments = CloudHostNetworkAttachment::where('host_id', $hostId)
->where('line_id', $lineId)
->where('state', 'active')
->where('verification_state', 'verified')
->orderBy('id')->lockForUpdate()->get();
$eligibleAttachments = [];
foreach ($attachments as $attachment) {
if (self::attachmentReadyForAgent($attachment, $agent)) {
$eligibleAttachments[(int) $attachment->id] = $attachment;
}
}
if ($eligibleAttachments === []) {
return [];
}
$pools = CloudPublicIpPool::where('host_id', $hostId)
->whereIn('network_attachment_id', array_keys($eligibleAttachments))
->where('state', 'active')
->where('sync_state', 'applied')
->whereColumn('observed_revision', 'desired_revision')
->orderBy('id')->lockForUpdate()->get();
$candidates = [];
foreach ($pools as $pool) {
$attachment = $eligibleAttachments[
(int) $pool->network_attachment_id
] ?? null;
if ($attachment === null
|| preg_match(
'/^[a-f0-9]{32}$/D',
(string) $pool->pool_key
) !== 1) {
continue;
}
$candidates[] = [
'attachment' => $attachment,
'public_pool' => $pool,
];
}
usort($candidates, static function (array $left, array $right): int {
return [
(int) $left['attachment']->priority,
(int) $left['public_pool']->priority,
(int) $left['attachment']->id,
(int) $left['public_pool']->id,
] <=> [
(int) $right['attachment']->priority,
(int) $right['public_pool']->priority,
(int) $right['attachment']->id,
(int) $right['public_pool']->id,
];
});
return $candidates;
}
/**
* New-instance admission requires one IPv4 and one IPv6 pool on the exact
* same verified attachment. Address allocation remains owned by the
* atomic bundle allocator after the resource reservation row is created.
*/
public static function hasCompleteDualStackCandidate(array $candidates,
CloudHostAgent $agent): bool
{
$familiesByAttachment = [];
foreach ($candidates as $candidate) {
$attachment = $candidate['attachment'] ?? null;
$pool = $candidate['public_pool'] ?? null;
if (!$attachment instanceof CloudHostNetworkAttachment
|| !$pool instanceof CloudPublicIpPool
|| (int) $pool->network_attachment_id
!== (int) $attachment->id
|| !in_array((int) $pool->address_family, [4, 6], true)
|| !CloudAgentSettingsService::publicNetworkReady(
$agent,
(string) $attachment->network_selector,
(int) $attachment->verified_agent_revision
)) {
continue;
}
$familiesByAttachment[(int) $attachment->id][
(int) $pool->address_family
] = true;
}
foreach ($familiesByAttachment as $families) {
if (isset($families[4], $families[6])) {
return true;
}
}
return false;
}
/** Current-revision Agent evidence for one already-loaded attachment. */
public static function attachmentReadyForAgent(
CloudHostNetworkAttachment $attachment,
CloudHostAgent $agent
): bool {
return (int) $attachment->host_id === (int) $agent->host_id
&& (string) $attachment->state === 'active'
&& (string) $attachment->verification_state === 'verified'
&& self::agentEvidenceReady(
$agent,
(string) $attachment->network_selector,
(int) $attachment->verified_agent_revision,
(int) $attachment->last_verified_at
);
}
/**
* Lock one available IP after the caller has locked the capacity ledger.
*
* @param list<array{attachment:CloudHostNetworkAttachment,
* public_pool:CloudPublicIpPool}> $candidates
* @return null|array{attachment:CloudHostNetworkAttachment,
* public_pool:CloudPublicIpPool,public_ip:CloudPublicIp}
*/
public static function lockAvailableSelection(int $hostId,
array $candidates): ?array
{
foreach ($candidates as $candidate) {
$attachment = $candidate['attachment'] ?? null;
$pool = $candidate['public_pool'] ?? null;
if (!$attachment instanceof CloudHostNetworkAttachment
|| !$pool instanceof CloudPublicIpPool
|| (int) $attachment->host_id !== $hostId
|| (int) $pool->host_id !== $hostId
|| (int) $pool->network_attachment_id
!== (int) $attachment->id) {
throw new CloudError('公网地址池与网络接入关联不一致', 409);
}
$ips = self::availablePublicIpQuery(
$hostId,
(int) $pool->id,
(int) $pool->guest_prefix_length
)->limit(self::MAX_PUBLIC_IPS_PER_POOL + 1)
->lockForUpdate()->get();
if ($ips->count() > self::MAX_PUBLIC_IPS_PER_POOL) {
continue;
}
foreach ($ips as $ip) {
if (self::publicIpBelongsToPool($ip, $pool)) {
return [
'attachment' => $attachment,
'public_pool' => $pool,
'public_ip' => $ip,
];
}
}
}
return null;
}
private static function agentEvidenceReady(?CloudHostAgent $agent,
string $selector, int $verifiedRevision, int $lastVerifiedAt): bool
{
return $agent !== null
&& $agent->isReady()
&& $verifiedRevision === (int) $agent->revision
&& $verifiedRevision === (int) $agent->tested_revision
&& $lastVerifiedAt > 0
&& in_array(
$selector,
CloudAgentSettingsService::testedNetworkSelectors($agent),
true
);
}
/** Hydrate joined evidence without issuing one Agent query per IP row. */
private static function agentFromEvidenceRow($row): CloudHostAgent
{
$agent = new CloudHostAgent();
$agent->setRawAttributes([
'host_id' => (int) $row->agent_host_id,
'enabled' => (bool) $row->agent_enabled,
'revision' => (int) $row->agent_revision,
'tested_revision' => (int) $row->agent_tested_revision,
'last_test_status' => (string) $row->agent_last_test_status,
'network_selectors_json' =>
(string) $row->agent_network_selectors_json,
], true);
return $agent;
}
private static function availablePublicIpQuery(int $hostId,
int $publicPoolId, int $prefixLength)
{
return CloudPublicIp::where('host_id', $hostId)
->where('public_ip_pool_id', $publicPoolId)
->where('prefix_length', $prefixLength)
->where('state', 'available')
->whereNull('reservation_id')
->whereNull('instance_id')
->whereNull('operation_id')
->orderBy('id');
}
private static function advisoryIpv4PoolAvailableUnits(
CloudPublicIpPool $pool,
array $ips,
?CloudHost $host
): int {
try {
$network = CloudIpPrefix::network((string) $pool->cidr);
$gateway = CloudIpAddress::parse((string) $pool->gateway);
} catch (\InvalidArgumentException) {
return 0;
}
if ($network->family() !== 4 || $network->prefixLength() > 30
|| (int) $pool->guest_prefix_length
!== $network->prefixLength()
|| !$gateway->isPublicUnicast()
|| !$network->contains($gateway)) {
return 0;
}
$available = array_values(array_filter(
$ips,
static fn (CloudPublicIp $ip): bool =>
(string) $ip->state === 'available'
&& $ip->reservation_id === null
&& $ip->instance_id === null
&& $ip->operation_id === null
));
if (count($available) > self::MAX_PUBLIC_IPS_PER_POOL) {
return 0;
}
$count = 0;
foreach ($available as $ip) {
try {
$address = CloudIpAddress::parse((string) $ip->address);
} catch (\InvalidArgumentException) {
continue;
}
if ($address->isPublicUnicast()
&& self::publicIpBelongsToPool($ip, $pool, $host)) {
$count++;
}
}
return $count;
}
private static function advisoryIpv6PoolAvailableUnits(
CloudPublicIpPool $pool,
array $ips
): int {
try {
$network = CloudIpPrefix::network((string) $pool->cidr);
$gateway = CloudIpAddress::parse((string) $pool->gateway);
$start = CloudIpAddress::parse((string) $pool->allocation_start);
$end = CloudIpAddress::parse((string) $pool->allocation_end);
} catch (\InvalidArgumentException) {
return 0;
}
if ($network->family() !== 6 || $network->prefixLength() > 120
|| (int) $pool->guest_prefix_length
!== $network->prefixLength()
|| !$gateway->isPublicUnicast()
|| (!$network->contains($gateway)
&& !(bool) $pool->gateway_on_link)
|| $start->family() !== 6 || $end->family() !== 6
|| !$start->isPublicUnicast() || !$end->isPublicUnicast()
|| !$network->contains($start) || !$network->contains($end)
|| $start->compare($end) > 0
|| (int) $pool->max_allocations < 1) {
return 0;
}
$active = count(array_filter(
$ips,
static fn (CloudPublicIp $ip): bool =>
(int) $ip->address_family === 6
&& in_array((string) $ip->state,
['reserved', 'changing', 'uncertain', 'assigned'], true)
));
return max(0, (int) $pool->max_allocations - $active);
}
private static function publicIpBelongsToPool(CloudPublicIp $ip,
CloudPublicIpPool $pool, ?CloudHost $host = null): bool
{
return (int) $ip->host_id === (int) $pool->host_id
&& (int) $ip->public_ip_pool_id === (int) $pool->id
&& (int) $ip->prefix_length === (int) $pool->guest_prefix_length
&& self::ipv4BelongsToCidr(
(string) $ip->address,
(string) $pool->cidr
) && self::addressAllowedByPool(
(string) $ip->address,
$pool,
$host ?? CloudHost::find((int) $pool->host_id)
);
}
private static function addressAllowedByPool(string $address, $pool,
?CloudHost $host): bool
{
$blocked = [(string) $pool->gateway];
$managementAddress = CloudPanelUsageService::managementAddress($host);
if ($managementAddress !== null) {
$blocked[] = $managementAddress;
}
foreach (['dns_servers_json', 'reserved_ips_json'] as $field) {
try {
$values = json_decode(
(string) $pool->{$field},
true,
16,
JSON_THROW_ON_ERROR
);
} catch (\Throwable $error) {
return false;
}
if (!is_array($values) || !array_is_list($values)) {
return false;
}
foreach ($values as $value) {
if (!is_string($value)
|| filter_var(
$value,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4
) === false) {
return false;
}
$blocked[] = $value;
}
}
return !in_array($address, $blocked, true);
}
private static function ipv4BelongsToCidr(string $addressValue,
string $cidr): bool
{
$parts = explode('/', $cidr, 2);
if (count($parts) !== 2 || !ctype_digit($parts[1])) {
return false;
}
$prefix = (int) $parts[1];
$address = self::ipv4Number($addressValue);
$network = self::ipv4Number($parts[0]);
if ($address === null || $network === null
|| $prefix < 24 || $prefix > 30) {
return false;
}
$block = 2 ** (32 - $prefix);
$base = (int) (floor($network / $block) * $block);
return $address > $base && $address < $base + $block - 1;
}
private static function ipv4Number(string $value): ?int
{
if (filter_var(
$value,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4
) === false) {
return null;
}
$packed = inet_pton($value);
if ($packed === false || strlen($packed) !== 4) {
return null;
}
$parts = unpack('Nnumber', $packed);
return is_array($parts) && isset($parts['number'])
? (int) sprintf('%u', $parts['number']) : null;
}
}