From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id B78561FF16D
	for <inbox@lore.proxmox.com>; Sun, 16 Mar 2025 10:36:57 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 6ED021BD20;
	Sun, 16 Mar 2025 10:36:46 +0100 (CET)
To: pve-devel@lists.proxmox.com
Date: Sat, 15 Mar 2025 13:08:55 +0100
MIME-Version: 1.0
Message-ID: <mailman.51.1742117805.416.pve-devel@lists.proxmox.com>
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Post: <mailto:pve-devel@lists.proxmox.com>
From: Victor Seva via pve-devel <pve-devel@lists.proxmox.com>
Precedence: list
Cc: Victor Seva <linuxmaniac@torreviejawireless.org>
X-Mailman-Version: 2.1.29
X-BeenThere: pve-devel@lists.proxmox.com
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
Subject: [pve-devel] [PATCH storage v4] fix #957 iscsi: improve
 iscsi_test_portal logic
Content-Type: multipart/mixed; boundary="===============4516780084399464762=="
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

--===============4516780084399464762==
Content-Type: message/rfc822
Content-Disposition: inline

Return-Path: <linuxmaniac@torreviejawireless.org>
X-Original-To: pve-devel@lists.proxmox.com
Delivered-To: pve-devel@lists.proxmox.com
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by lists.proxmox.com (Postfix) with ESMTPS id DA724C9391
	for <pve-devel@lists.proxmox.com>; Sun, 16 Mar 2025 10:36:44 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id B314E1BD00
	for <pve-devel@lists.proxmox.com>; Sun, 16 Mar 2025 10:36:14 +0100 (CET)
Received: from mail-106109.protonmail.ch (mail-106109.protonmail.ch [79.135.106.109])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by firstgate.proxmox.com (Proxmox) with ESMTPS
	for <pve-devel@lists.proxmox.com>; Sun, 16 Mar 2025 10:36:13 +0100 (CET)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
	d=torreviejawireless.org; s=protonmail; t=1742117752; x=1742376952;
	bh=PmMWg8P7CGiaGWSDZbkx38zL9LnIaKOsNCAlwsydues=;
	h=From:To:Cc:Subject:Date:Message-ID:From:To:Cc:Date:Subject:
	 Reply-To:Feedback-ID:Message-ID:BIMI-Selector:List-Unsubscribe:
	 List-Unsubscribe-Post;
	b=d1y1WtI+ApDvrWbHZsApepF2Ekp9/LzLMkuwuEpD9QrxtZ6nKeFPrL8AS9kItjqO/
	 jRpv1lo4voXdmiW6W11NgkmLibRPPosC0cLrNN4PlufsN9bbIN3ZoN7rfjTq/YI4W0
	 GSMmmpSV/A8Zgr+4/Bs/m6YvLN7ZWqPa4+qG/I6uacUdsV2GvseUkJ/omHn7rGDhFP
	 PMcepj2X6mbkYP8+A/2Lk6Yd1yUbM03loAqq7cNFNGPwjlUu/x4WpEDR6Kf3csnT1q
	 vdEmwWeKiwp7ArGhcI0cG5fCnluVp+pT8hPCM1ZOKPsEDUMS/Rd17f0bmSht/PSeo3
	 Xpz4CXjZRv6Vw==
X-Pm-Submission-Id: 4ZFtJR4bRzzFB
From: Victor Seva <linuxmaniac@torreviejawireless.org>
To: pve-devel@lists.proxmox.com
Subject: [PATCH storage v4] fix #957 iscsi: improve iscsi_test_portal logic
Date: Sat, 15 Mar 2025 13:08:55 +0100
Message-ID: <20250315120854.95412-2-linuxmaniac@torreviejawireless.org>
X-Mailer: git-send-email 2.43.0
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
	AWL                    -0.525 Adjusted score from AWL reputation of From: address
	BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
	DATE_IN_PAST_12_24      1.049 Date: is 12 to 24 hours before Received: date
	DKIM_SIGNED               0.1 Message has a DKIM or DK signature, not necessarily valid
	DKIM_VALID               -0.1 Message has at least one valid DKIM or DK signature
	DKIM_VALID_AU            -0.1 Message has a valid DKIM or DK signature from author's domain
	DKIM_VALID_EF            -0.1 Message has a valid DKIM or DK signature from envelope-from domain
	DMARC_PASS               -0.1 DMARC pass policy
	RCVD_IN_DNSWL_NONE     -0.0001 Sender listed at https://www.dnswl.org/, no trust
	RCVD_IN_VALIDITY_CERTIFIED_BLOCKED  0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked.  See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information.
	RCVD_IN_VALIDITY_RPBL_BLOCKED  0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked.  See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information.
	RCVD_IN_VALIDITY_SAFE_BLOCKED  0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked.  See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information.
	SPF_HELO_PASS          -0.001 SPF: HELO matches SPF record
	SPF_PASS               -0.001 SPF: sender matches SPF record

don't check tcp connection directly if there are already
sessions.

pvestatd is calling check_connection every 10 seconds.
This check produces a lot of noise at the iscsi server logging.

Signed-off-by: Victor Seva <linuxmaniac@torreviejawireless.org>
---

changes since v3:

* iscsi_test_session():
  read /sys/class/iscsi_session/session${sid}/state instead of
  $ISCSIADM call

* iscsi_test_portal():
  add optional parameters target and cache. If target is defined,
  used it to check session status instead of check tcp port

* iscsi_discovery():
  add optional parameters target and cache and pass it to
  iscsi_test_portal call

* iscsi_login():
  add optional parameter cache and pass it to iscsi_discovery call

* activate_storage():
  pass cache parameter to iscsi_login call

* check_connection():
  add target and cache parameters to iscsi_test_portal call  

 src/PVE/Storage.pm             |  2 +-
 src/PVE/Storage/ISCSIPlugin.pm | 41 ++++++++++++++++++++++++++--------
 2 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/src/PVE/Storage.pm b/src/PVE/Storage.pm
index 3b4f041..c5d4ff8 100755
--- a/src/PVE/Storage.pm
+++ b/src/PVE/Storage.pm
@@ -1479,7 +1479,7 @@ sub scan_iscsi {
 	die "unable to parse/resolve portal address '${portal_in}'\n";
     }
 
-    return PVE::Storage::ISCSIPlugin::iscsi_discovery([ $portal ]);
+    return PVE::Storage::ISCSIPlugin::iscsi_discovery(undef, [ $portal ]);
 }
 
 sub storage_default_format {
diff --git a/src/PVE/Storage/ISCSIPlugin.pm b/src/PVE/Storage/ISCSIPlugin.pm
index eb70453..c7bacea 100644
--- a/src/PVE/Storage/ISCSIPlugin.pm
+++ b/src/PVE/Storage/ISCSIPlugin.pm
@@ -58,9 +58,32 @@ sub iscsi_session_list {
     return $res;
 }
 
-sub iscsi_test_portal {
-    my ($portal) = @_;
+sub iscsi_test_session {
+    my ($sid) = @_;
 
+    if ($sid !~ m/^\d+$/) {
+	die "session_id: '$sid' is not a number\n";
+    }
+    my $state = file_read_firstline("/sys/class/iscsi_session/session${sid}/state");
+    if ($state eq 'LOGGED_IN') {
+	return 1;
+    }
+    return 0;
+}
+
+sub iscsi_test_portal {
+    my ($target, $portal, $cache) = @_;
+    $cache //= {};
+
+    if (defined($target)) {
+	# check session state instead if available
+	my $sessions = iscsi_session($cache, $target);
+	for my $session ($sessions->@*) {
+	    next if $session->{portal} ne $portal;
+	    return iscsi_test_session($session->{session_id});
+	}
+    }
+    # check portal via tcp
     my ($server, $port) = PVE::Tools::parse_host_and_port($portal);
     return 0 if !$server;
     return PVE::Network::tcp_ping($server, $port || 3260, 2);
@@ -97,13 +120,13 @@ sub iscsi_portals {
 }
 
 sub iscsi_discovery {
-    my ($portals) = @_;
+    my ($target, $portals, $cache) = @_;
 
     assert_iscsi_support();
 
     my $res = {};
     for my $portal ($portals->@*) {
-	next if !iscsi_test_portal($portal); # fixme: raise exception here?
+	next if !iscsi_test_portal($target, $portal, $cache); # fixme: raise exception here?
 
 	my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets', '--portal', $portal];
 	eval {
@@ -127,11 +150,11 @@ sub iscsi_discovery {
 }
 
 sub iscsi_login {
-    my ($target, $portals) = @_;
+    my ($target, $portals, $cache) = @_;
 
     assert_iscsi_support();
 
-    eval { iscsi_discovery($portals); };
+    eval { iscsi_discovery($target, $portals, $cache); };
     warn $@ if $@;
 
     # Disable retries to avoid blocking pvestatd for too long, next iteration will retry anyway
@@ -445,7 +468,7 @@ sub activate_storage {
     }
 
     if ($do_login) {
-	eval { iscsi_login($scfg->{target}, $portals); };
+	eval { iscsi_login($scfg->{target}, $portals, $cache); };
 	warn $@ if $@;
     } else {
 	# make sure we get all devices
@@ -559,11 +582,11 @@ sub activate_volume {
 
 sub check_connection {
     my ($class, $storeid, $scfg) = @_;
-
+    my $cache = {};
     my $portals = iscsi_portals($scfg->{target}, $scfg->{portal});
 
     for my $portal (@$portals) {
-	my $result = iscsi_test_portal($portal);
+	my $result = iscsi_test_portal($scfg->{target}, $portal, $cache);
 	return $result if $result;
     }
 
-- 
2.43.0



--===============4516780084399464762==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

--===============4516780084399464762==--