all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH pve-network 02/10] sdn: ipam: write the lookup's cache entry under the cluster lock
Date: Mon,  2 Nov 2026 12:05:56 +0100	[thread overview]
Message-ID: <20261102110604.915693-3-h.laimer@proxmox.com> (raw)
In-Reply-To: <20261102110604.915693-1-h.laimer@proxmox.com>

The lookup that stores the IPAM's answer wrote the whole cache back
without the cluster lock the other writers take. A change made
between its read and its write was lost. The lookup now takes the
lock and reads the cache again before it stores its answer. The IPAM
is still asked outside the lock, since the lock is cluster-wide and a
request to an external IPAM does not belong under it. A cache write
that lands while the IPAM is asked may be newer than the answer, so
the answer is then returned without being stored, and the next lookup
stores it.

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
 src/PVE/Network/SDN/Ipams.pm        | 24 +++++++++++++++++++-----
 src/test/run_test_vnets_blackbox.pl | 11 +++++++++++
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/src/PVE/Network/SDN/Ipams.pm b/src/PVE/Network/SDN/Ipams.pm
index 7e7bec27b5ed..ae2c4337ef8d 100644
--- a/src/PVE/Network/SDN/Ipams.pm
+++ b/src/PVE/Network/SDN/Ipams.pm
@@ -135,8 +135,9 @@ sub complete_sdn_vnet {
 sub get_ips_from_mac {
     my ($mac, $zoneid, $zone) = @_;
 
-    my $macdb = read_macdb();
-    my $cached = $macdb->{macs}->{$mac};
+    PVE::Cluster::cfs_update(); # the lock refreshes, so the snapshot must too
+    my $version = PVE::Cluster::cfs_file_version($macdb_filename);
+    my $cached = read_macdb()->{macs}->{$mac};
     # an entry without an address was left behind by a lookup that missed, and
     # says nothing about its MAC
     return ($cached->{ip4}, $cached->{ip6})
@@ -149,9 +150,22 @@ sub get_ips_from_mac {
     # an empty answer is not cached, since the record may simply not exist yet
     return if !defined($ip4) && !defined($ip6);
 
-    $macdb->{macs}->{$mac}->{ip4} = $ip4 if defined($ip4);
-    $macdb->{macs}->{$mac}->{ip6} = $ip6 if defined($ip6);
-    write_macdb($macdb);
+    cfs_lock_file(
+        $macdb_filename,
+        undef,
+        sub {
+            # the IPAM was asked outside the lock, so a write since the miss may
+            # be newer than the answer. The fill is then left to the next lookup
+            return
+                if (PVE::Cluster::cfs_file_version($macdb_filename) // '') ne ($version // '');
+
+            my $db = read_macdb();
+            $db->{macs}->{$mac}->{ip4} = $ip4 if defined($ip4);
+            $db->{macs}->{$mac}->{ip6} = $ip6 if defined($ip6);
+            write_macdb($db);
+        },
+    );
+    warn "$@" if $@;
 
     return ($ip4, $ip6);
 }
diff --git a/src/test/run_test_vnets_blackbox.pl b/src/test/run_test_vnets_blackbox.pl
index 9724f1a8e4ef..fff5858f9c27 100755
--- a/src/test/run_test_vnets_blackbox.pl
+++ b/src/test/run_test_vnets_blackbox.pl
@@ -473,6 +473,17 @@ sub test_ipam_cache_lookup {
         "10.0.0.50",
         "$test_name: the answer is cached",
     );
+
+    # a write to the cache between the miss and the fill, which the file's
+    # version shows, keeps the IPAM's answer out of the cache
+    delete $test_state->{macdb}->{macs}->{$mac};
+    my $version = 0;
+    $mocked_pve_cluster_obj->mock(cfs_file_version => sub { return $version++ });
+    @ips = PVE::Network::SDN::Vnets::get_ips_from_mac($vnetid, $mac);
+    $mocked_pve_cluster_obj->unmock('cfs_file_version');
+    is($ips[0], "10.0.0.50", "$test_name: the lookup still answers");
+    ok(!exists $test_state->{macdb}->{macs}->{$mac},
+        "$test_name: but leaves a changed cache alone");
 }
 
 run_test(\&test_ipam_cache_lookup);
-- 
2.47.3





  parent reply	other threads:[~2026-11-02 11:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 11:05 [PATCH docs/network 00/10] sdn: improve MAC cache, DHCP reservations and scope both to the vnet Hannes Laimer
2026-11-02 11:05 ` [PATCH pve-network 01/10] sdn: ipam: stop caching IPAM misses Hannes Laimer
2026-11-02 11:05 ` Hannes Laimer [this message]
2026-11-02 11:05 ` [PATCH pve-network 03/10] sdn: ipam: read the MAC cache case-insensitively Hannes Laimer
2026-11-02 11:05 ` [PATCH pve-network 04/10] sdn: dhcp: replace a stale dnsmasq reservation instead of dropping it Hannes Laimer
2026-11-02 11:05 ` [PATCH pve-network 05/10] sdn: ipam: release an address the built-in IPAM no longer holds Hannes Laimer
2026-11-02 11:06 ` [PATCH pve-network 06/10] sdn: dhcp: update reservations when ip mappings change Hannes Laimer
2026-11-02 11:06 ` [PATCH pve-network 07/10] sdn: ipam: look a MAC up within the vnet's subnets Hannes Laimer
2026-11-02 11:06 ` [PATCH pve-network 08/10] sdn: ipam: keep the MAC cache per vnet Hannes Laimer
2026-11-02 11:06 ` [PATCH pve-network 09/10] sdn: ipam: give each zone a VRF in NetBox and a section in phpIPAM Hannes Laimer
2026-11-02 11:06 ` [PATCH pve-docs 10/10] sdn: ipam: document the per-zone VRF and section in NetBox and phpIPAM Hannes Laimer
2026-11-02 14:57 ` superseded: [PATCH docs/network 00/10] sdn: improve MAC cache, DHCP reservations and scope both to the vnet Hannes Laimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261102110604.915693-3-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal