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 [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 8CB531FF16B
	for <inbox@lore.proxmox.com>; Thu, 20 Mar 2025 15:38:18 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id E022D544E;
	Thu, 20 Mar 2025 15:38:16 +0100 (CET)
To: pve-devel@lists.proxmox.com
Date: Thu, 20 Mar 2025 15:23:20 +0100
MIME-Version: 1.0
Message-ID: <mailman.14.1742481496.359.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 v5] fix #957: iscsi: improve
 iscsi_test_portal logic
Content-Type: multipart/mixed; boundary="===============7790236955324720484=="
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

--===============7790236955324720484==
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 675A0C854F
	for <pve-devel@lists.proxmox.com>; Thu, 20 Mar 2025 15:38:15 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 474A95437
	for <pve-devel@lists.proxmox.com>; Thu, 20 Mar 2025 15:38:15 +0100 (CET)
Received: from mail-244104.protonmail.ch (mail-244104.protonmail.ch [109.224.244.104])
	(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 firstgate.proxmox.com (Proxmox) with ESMTPS
	for <pve-devel@lists.proxmox.com>; Thu, 20 Mar 2025 15:38:14 +0100 (CET)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
	d=torreviejawireless.org; s=protonmail; t=1742481028; x=1742740228;
	bh=+LYy1nvVkM1ESb3WGjTwXr4xosJf+7F4tF2P6gpU+wg=;
	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=mxKE7cxHHRxueMLKb9L8GRkMGoHlkhaxk/k9qM0Dhku/3lgpQVffefdf7uYGuRyvE
	 2ASCK+ImSi6CA2Y5WNKbVgBt4pSb0OWbxsU2dyOzKstIik/2pNY616CkoI0H3Cj2zf
	 WNF1t1TaeG9qFJHcz2dbPG7GokGGK1Qr3wECvi56XYjLRZVF1qperqH/5n1hbi4Rus
	 qEgwh+h2dntumm9HMkBNC4DWojRTAD8efmVAUqrPYjFCRltPbIHDC+CRgvZSGCuaGb
	 QiDfa2qASZaQJ04aPd8YbH/rNRci17l09eUGPo3QNj/58torEkLwcW+Gg+eaP1Q5S9
	 R7eT58AUQMAHw==
X-Pm-Submission-Id: 4ZJSfW4HlTz4wx9N
From: Victor Seva <linuxmaniac@torreviejawireless.org>
To: pve-devel@lists.proxmox.com
Subject: [PATCH storage v5] fix #957: iscsi: improve iscsi_test_portal logic
Date: Thu, 20 Mar 2025 15:23:20 +0100
Message-ID: <20250320142319.20411-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
	BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
	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_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
	URIBL_BLOCKED           0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked.  See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [torreviejawireless.org,iscsiplugin.pm,storage.pm]

Check if there is already a logged session present and
fall back to previous TCP check port connection.

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 v4:

* rework commit message wording and subject in proper format

* iscsi_test_session():
  use [0-9] instead of \d
  simplify return logic checking $state is defined too

* iscsi_test_portal():
  fallback to TCP check connection if session is not LOGGED_IN

* iscsi_discovery():
  rename $target to $target_in to avoid variable shadowing

 src/PVE/Storage.pm             |  2 +-
 src/PVE/Storage/ISCSIPlugin.pm | 39 ++++++++++++++++++++++++++--------
 2 files changed, 31 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..2c608a4 100644
--- a/src/PVE/Storage/ISCSIPlugin.pm
+++ b/src/PVE/Storage/ISCSIPlugin.pm
@@ -58,9 +58,30 @@ sub iscsi_session_list {
     return $res;
 }
 
-sub iscsi_test_portal {
-    my ($portal) = @_;
+sub iscsi_test_session {
+    my ($sid) = @_;
 
+    if ($sid !~ m/^[0-9]+$/) {
+	die "session_id: '$sid' is not a number\n";
+    }
+    my $state = file_read_firstline("/sys/class/iscsi_session/session${sid}/state");
+    return defined($state) && $state eq 'LOGGED_IN';
+}
+
+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;
+	    my $state = iscsi_test_session($session->{session_id});
+	    return $state if $state;
+	}
+    }
+    # 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 +118,13 @@ sub iscsi_portals {
 }
 
 sub iscsi_discovery {
-    my ($portals) = @_;
+    my ($target_in, $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_in, $portal, $cache); # fixme: raise exception here?
 
 	my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets', '--portal', $portal];
 	eval {
@@ -127,11 +148,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 +466,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 +580,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



--===============7790236955324720484==
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

--===============7790236955324720484==--